/** * Convert a point to a grid index. */ private int pointToGridIdx(Grid grid, float[] point) { int[] idx = new int[3]; float[] size = grid.getSize (); for (int i = 0; i < 3; ++i) { idx [i] = (int)Math.Floor ((point [i] - grid.getMin () [i]) / size [i]); if (idx [i] >= grid.getDivision () [i]) { idx [i] = grid.getDivision () [i] - 1; } } return idx [0] + grid.getDivision () [0] * (idx [1] + grid.getDivision () [1] * idx [2]); }
public SortableVertex[] sortVertices(Grid grid, float[] v) { // Prepare sort vertex array int vc = v.Length / Mesh.CTM_POSITION_ELEMENT_COUNT; SortableVertex[] sortVertices = new SortableVertex[vc]; for (int i = 0; i < vc; ++i) { // Store vertex properties in the sort vertex array float[] point = new float[]{v [i * 3], v [i * 3 + 1], v [i * 3 + 2]}; int p2g = pointToGridIdx (grid, point); sortVertices [i] = new SortableVertex (v [i * 3], p2g, i); } // Sort vertices. The elements are first sorted by their grid indices, and // scondly by their x coordinates. Array.Sort (sortVertices); return sortVertices; }
/** * Calculate various forms of derivatives in order to reduce data entropy. */ private int[] makeVertexDeltas(float[] vertices, SortableVertex[] sortVertices, Grid grid) { int vc = sortVertices.Length; // Vertex scaling factor float scale = 1.0f / vertexPrecision; float prevGridIndex = 0x7fffffff; int prevDeltaX = 0; int[] intVertices = new int[vc * Mesh.CTM_POSITION_ELEMENT_COUNT]; for (int i = 0; i < vc; ++i) { // Get grid box origin int gridIdx = sortVertices [i].gridIndex; float[] gridOrigin = CommonAlgorithm.gridIdxToPoint (grid, gridIdx); // Get old vertex coordinate index (before vertex sorting) int oldIdx = sortVertices [i].originalIndex; // Store delta to the grid box origin in the integer vertex array. For the // X axis (which is sorted) we also do the delta to the previous coordinate // in the box. int deltaX = (int)Math.Floor (scale * (vertices [oldIdx * 3] - gridOrigin [0]) + 0.5f); if (gridIdx == prevGridIndex) { intVertices [i * 3] = deltaX - prevDeltaX; } else { intVertices [i * 3] = deltaX; } intVertices [i * 3 + 1] = (int)Math.Floor (scale * (vertices [oldIdx * 3 + 1] - gridOrigin [1]) + 0.5f); intVertices [i * 3 + 2] = (int)Math.Floor (scale * (vertices [oldIdx * 3 + 2] - gridOrigin [2]) + 0.5f); prevGridIndex = gridIdx; prevDeltaX = deltaX; } return intVertices; }
private float[] readVertices(CtmInputStream input, Grid grid, int vcount, float precision) { checkTag(input.readLittleInt(), VERT); int[] intVertices = input.readPackedInts(vcount, Mesh.CTM_POSITION_ELEMENT_COUNT, false); checkTag(input.readLittleInt(), GIDX); int[] gridIndices = input.readPackedInts(vcount, 1, false); for (int i = 1; i < vcount; i++) { gridIndices[i] += gridIndices[i - 1]; } return CommonAlgorithm.restoreVertices(intVertices, gridIndices, grid, precision); }