private void Add(List <byte> voxelList, BoundingCube test, IOctTreeInsertable obj, double precision) { int ind = voxelList.Count; voxelList.Add(0); // 0 means: this is a final voxel, belonging to the object (we know, test interferes with the object) if (test.XDiff > precision) { BoundingCube[] sc = subCubes(test); for (int i = 0; i < 8; i++) { if (obj.HitTest(ref sc[i], precision)) { voxelList[ind] |= (byte)(1 << i); // this adds a subvoxel to the list, it is no more final Add(voxelList, sc[i], obj, precision); } } } }
/// <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(); }