public VoxelIterator(VoxelTree vt) //byte[] voxels, int index, BoundingCube start) { voxels = vt.voxels; index = 0; double size; current = new BoundingCube(); if (vt.baseCube >= 0) { size = 1 << vt.baseCube; } else { size = 1.0 / (1 << -vt.baseCube); } current.Set(-size, size, -size, size, -size, size); // this BoundingCube contains the whole object for (int i = 0; i < vt.startCube.Length; i++) { GeoPoint m = current.GetCenter(); switch (vt.startCube[i]) { case 0: current = new BoundingCube(m.x, current.Xmax, m.y, current.Ymax, m.z, current.Zmax); break; case 1: current = new BoundingCube(current.Xmin, m.x, m.y, current.Ymax, m.z, current.Zmax); break; case 2: current = new BoundingCube(m.x, current.Xmax, current.Ymin, m.y, m.z, current.Zmax); break; case 3: current = new BoundingCube(current.Xmin, m.x, current.Ymin, m.y, m.z, current.Zmax); break; case 4: current = new BoundingCube(m.x, current.Xmax, m.y, current.Ymax, current.Zmin, m.z); break; case 5: current = new BoundingCube(current.Xmin, m.x, m.y, current.Ymax, current.Zmin, m.z); break; case 6: current = new BoundingCube(m.x, current.Xmax, current.Ymin, m.y, current.Zmin, m.z); break; case 7: current = new BoundingCube(current.Xmin, m.x, current.Ymin, m.y, current.Zmin, m.z); break; } } }
/// <summary> /// Creates the voxel representation of the provided <paramref name="obj"/> (only GetExtent and HitTest are beeing used) /// </summary> /// <param name="obj">the object for which the voxel represenation is to be created</param> /// <param name="precision">the precision, size of the smallest voxel</param> public VoxelTree(IOctTreeInsertable obj, double precision) { BoundingCube ext = obj.GetExtent(precision); BoundingCube unit = new BoundingCube(-1, 1, -1, 1, -1, 1); baseCube = 1; double size = 1; while (unit.Contains(ext)) { size /= 2; baseCube--; unit.Set(-size, size, -size, size, -size, size); } if (baseCube == 1) { // ext is not contained in -1..1 size = 2; unit.Set(-2, 2, -2, 2, -2, 2); while (!unit.Contains(ext)) { size *= 2; baseCube++; unit.Set(-size, size, -size, size, -size, size); } } if (baseCube >= 0) { size = 1 << baseCube; } else { size = 1.0 / (1 << -baseCube); } unit.Set(-size, size, -size, size, -size, size); // this BoundingCube contains the whole object List <byte> startCubesList = new List <byte>(); do { byte found = 255; BoundingCube[] sc = subCubes(unit); for (byte i = 0; i < 8; i++) { if (sc[i].Contains(ext)) { if (found == 255) { found = i; } else { // two different cubes contain ext, so we must stop here found = 255; break; } } } if (found != 255) { startCubesList.Add(found); unit = sc[found]; } else { break; } } while (true); startCube = startCubesList.ToArray(); List <byte> voxelList = new List <byte>(); Add(voxelList, unit, obj, precision); voxels = voxelList.ToArray(); }