public MechanismEnviroment(Model sceneModel) { this.sceneModel = sceneModel; List<TriangleData> triangles = GetFaces(); octree = new OctreeNode(new Vector3(-450, -450, -50), new Vector3(550, 550, 100), 0); foreach (TriangleData face in triangles) { octree.AddTriangle(face); } octree.Divide(); }
public void Divide() { if (triangles.Count < 5) return; octree = new OctreeNode[8]; Vector3 step = (max - min)/2; int quadIndex = 0; for (float x = min.X; x < max.X; x += step.X) { for (float y = min.Y; y < max.Y; y += step.Y) { for (float z = min.Z; z < max.Z; z += step.Z) { Vector3 childMin2 = new Vector3(x, y, z); Vector3 childMax2 = childMin2 + step; octree[quadIndex] = new OctreeNode( childMin2, childMax2, level + 1); quadIndex++; } } } Vector3 mid = (max + min) / 2; foreach (TriangleData triangle in triangles) { for (int i = 0; i < 3; i++) { Vector3 pos = triangle[i] - mid; bool[] bits = new bool[] { pos.X > 0, pos.Y > 0, pos.Z > 0 }; quadIndex = (Convert.ToByte(bits[0]) << 2) + (Convert.ToByte(bits[1]) << 1) + Convert.ToByte(bits[2]); octree[quadIndex].AddTriangle(triangle); } } if (level < maxLevel) { foreach (OctreeNode oct in octree) { oct.Divide(); } } }