Android Open Source - 4est Vindexer






From Project

Back to project page 4est.

License

The source code is released under:

MIT License

If you think the Android project 4est listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.wordsaretoys.rise.meshutil;
//from  ww w  .j av  a2 s. c  om
import android.util.Log;

/**
 * Index generator for 3D vertexes
 * 
 * implements an octree in a fixed-size array;
 * as the mesh object only allows short ints
 * to be used for indexes, we don't bother to
 * make the array length > 64k
 * 
 * @author chris
 *
 */

public class Vindexer {

  static public final int INDEX_MASK = 0xFFFF;
  static public final int ADD_VERTEX= 0xFFFF0000;
  
  static private final int MAX_INDEX = 65536;
  static private final float MIN_LIMIT = 0.0001f;

  private float[] vertex;
  private int[] child;

  private int length;
  
  private int hits;
  
  /**
   * constructor, generates fixed array
   */
  public Vindexer() {
    vertex = new float[3 * MAX_INDEX];
    child = new int[8 * MAX_INDEX];
    reset();
  }
  
  /**
   * call prior to generating new indexes
   */
  public void reset() {
    length = 0;
    hits = 0;
  }
  
  /**
   * check that a specific vertex is in the octree,
   * create it if it isn't. either way, return the
   * index of the vertex.
   * 
   * if the indexer hasn't seen this vertex yet, I
   * assume the calling code needs to add it to a
   * vertex buffer, so the ADD_VERTEX flag is ORed
   * with the index. typically, you'd do something
   * like this:
   * 
   *     int result = vindexer.check(x, y, z);
   *     int index = result & Vindexer.ADD_VERTEX
   *     if (index != result) {
   *       // add vertex to buffer
   *       vertexbuffer.set(x, y, z);
   *     }
   *     indexbuffer.set(index);
   * 
   * 
   * @param x, y, z coordinates of vertex
   * @return index of new or stored vertex, maybe ORed with 
   * ADD_VERTEX if new vertex (see above)
   */
  public int check(float x, float y, float z) {
    int vin, cin, index = 0;
    
    while (index < length) {
      
      vin = index * 3;
      cin = index * 8;
      
      float vx = vertex[vin];
      float vy = vertex[vin + 1];
      float vz = vertex[vin + 2];
      
      // if this entry matches our vertex
      if ( (Math.abs(x - vx) < MIN_LIMIT) && (Math.abs(y - vy) < MIN_LIMIT) && (Math.abs(z - vz) < MIN_LIMIT)) {
        // our search is over
        hits++;
        return index;
      }
      
      // traverse to the next child, if available
      int ch = cin + ((x > vx) ? 4 : 0) + ((y > vy) ? 2 : 0) + ((z > vz) ? 1 : 0);
      if (child[ch] != -1) {
        index = child[ch];
      } else {
        child[ch] = (int)length;
        index = length;
      }
      
    }

    // no entries found, create one
    vin = length * 3;
    cin = length * 8;

    vertex[vin] = x;
    vertex[vin + 1] = y;
    vertex[vin + 2] = z;

    for (int i = 0; i < 8; i++) {
      child[cin + i] = -1;
    }
    
    index = length++;
    
    // this should never happen, obviously
    if (length >= MAX_INDEX) {
      throw new RuntimeException("Vindexer exceeded index limits");
    }
    
    // return index with "add vertex" flag in upper word
    return index | ADD_VERTEX;
  }
  
  public void report() {
    int hpi = hits / length;
    Log.i("4est", "vindexer generated " + length + " indexes, with " + hits + " hits (" + hpi + " hits/index)");
  }
  
}




Java Source Code List

com.wordsaretoys.forest.Audio.java
com.wordsaretoys.forest.Debris.java
com.wordsaretoys.forest.Game.java
com.wordsaretoys.forest.GlView.java
com.wordsaretoys.forest.MainActivity.java
com.wordsaretoys.forest.Map.java
com.wordsaretoys.forest.Player.java
com.wordsaretoys.forest.Render.java
com.wordsaretoys.forest.Rotors.java
com.wordsaretoys.forest.Shared.java
com.wordsaretoys.forest.Skybox.java
com.wordsaretoys.rise.geometry.Camera.java
com.wordsaretoys.rise.geometry.Geom.java
com.wordsaretoys.rise.geometry.Mote.java
com.wordsaretoys.rise.geometry.Ortho.java
com.wordsaretoys.rise.geometry.Quaternion.java
com.wordsaretoys.rise.geometry.Vector.java
com.wordsaretoys.rise.glwrapper.Mesh.java
com.wordsaretoys.rise.glwrapper.Shader.java
com.wordsaretoys.rise.glwrapper.Texture.java
com.wordsaretoys.rise.meshutil.HeightMapper.java
com.wordsaretoys.rise.meshutil.IndexBuffer.java
com.wordsaretoys.rise.meshutil.SurfaceMapper.java
com.wordsaretoys.rise.meshutil.VertexBuffer.java
com.wordsaretoys.rise.meshutil.Vindexer.java
com.wordsaretoys.rise.pattern.Bitmap.java
com.wordsaretoys.rise.pattern.F2FSumMap.java
com.wordsaretoys.rise.pattern.I2FCutMap.java
com.wordsaretoys.rise.pattern.I2FMap.java
com.wordsaretoys.rise.pattern.I2IMap.java
com.wordsaretoys.rise.pattern.Pattern.java
com.wordsaretoys.rise.pattern.Ring.java
com.wordsaretoys.rise.utility.Asset.java
com.wordsaretoys.rise.utility.Board.java
com.wordsaretoys.rise.utility.Dbg.java
com.wordsaretoys.rise.utility.Interval.java
com.wordsaretoys.rise.utility.Misc.java
com.wordsaretoys.rise.utility.Needle.java