public ModelInstance (String n, Model baseM, Vector3 pos, Quaternion rot, uint modelId, int rID, string lName) { name = n; baseModel = baseM; position = pos; transform = new Matrix4(new Vector4(Vector3.Transform(Vector3.UnitX, rot), 0), new Vector4(Vector3.Transform(Vector3.UnitY, rot), 0), new Vector4(Vector3.Transform(Vector3.UnitZ, rot), 0), new Vector4(pos, 1)); id = modelId; roomID = rID; layoutName = lName; //Need to make bounding boxes bounds = new BoundingBox[meshes.Length]; for (int i = 0; i < bounds.Length; i++) { bounds[i] = new BoundingBox(meshes[i].bounds, transform); } }
private void build(List<Triangle> triangles, int maxTriangles, int maxDepth, BoundingBox box) { bounds = box; if (triangles.Count <= maxTriangles || maxDepth == 0) { tris = triangles.ToArray(); count = tris.Length; } else { children = new Octree[8]; count = 0; BoundingBox newBox; List<Triangle> childrenTriangles; List<Triangle> used = new List<Triangle>(); Vector3 lengths = (bounds.max - bounds.min) / 2; Vector3 halfLengths = lengths / 2; for (int i = 0; i < 8; i++) { childrenTriangles = new List<Triangle>(); Vector3 ooffset = Vector3.Multiply(lengths, offsets[i]); newBox = new BoundingBox(bounds.center + ooffset, halfLengths.X, halfLengths.Y, halfLengths.Z); foreach (Triangle t in triangles) { if (newBox.triangleIntersects(t)) { childrenTriangles.Add(t); } } children[i] = new Octree(childrenTriangles, maxTriangles, maxDepth - 1, newBox); count += children[i].count; } } }
//Bounding box with offset and rotation from another bounding box public BoundingBox(BoundingBox bb, Matrix4 transform) { center = bb.center + transform.Row3.Xyz; //make the 8 corners, rotate them, then find the max and min for each axis Vector3[] normal = new Vector3[8]; //Top normal[0] = max; normal[1] = new Vector3(max.X, min.Y, max.Z); normal[2] = new Vector3(min.X, max.Y, max.Z); normal[3] = new Vector3(min.X, min.Y, max.Z); //Bottom normal[4] = min; normal[5] = new Vector3(min.X, max.Y, min.Z); normal[6] = new Vector3(max.X, min.Y, min.Z); normal[7] = new Vector3(max.X, max.Y, min.Z); //Rotate all the vectors Vector3[] rotated = new Vector3[8]; for (int i = 0; i < rotated.Length; i++) { rotated[i] = Vector3.Transform(normal[i], transform) - transform.Row3.Xyz; } //find the min and max of the rotated corners float minX = rotated[0].X; float minY = rotated[0].Y; float minZ = rotated[0].Z; float maxX = rotated[0].X; float maxY = rotated[0].Y; float maxZ = rotated[0].Z; for (int i = 1; i < rotated.Length; i++) { float x = rotated[i].X; float y = rotated[i].Y; float z = rotated[i].Z; minX = Math.Min(x, minX); minY = Math.Min(y, minY); minZ = Math.Min(z, minZ); maxX = Math.Max(x, maxX); maxY = Math.Max(y, maxY); maxZ = Math.Max(z, maxZ); } max = new Vector3(maxX, maxY, maxZ); min = new Vector3(minX, minY, minZ); calculateOtherPoints(); }
public Boolean boxIntersects(BoundingBox b) { return !(min.X > b.max.X || min.Y > b.max.Y || min.Z > b.max.Z || b.min.X > max.X || b.min.Y > max.Y || b.min.Z > max.Z); }
public Octree(List<Triangle> triangles, int maxTriangles, int maxDepth, BoundingBox box) { build(triangles, maxTriangles, maxDepth, box); }
private void build(List <Triangle> triangles, int maxTriangles, int maxDepth, BoundingBox box) { bounds = box; if (triangles.Count <= maxTriangles || maxDepth == 0) { tris = triangles.ToArray(); count = tris.Length; } else { children = new Octree[8]; count = 0; BoundingBox newBox; List <Triangle> childrenTriangles; List <Triangle> used = new List <Triangle>(); Vector3 lengths = (bounds.max - bounds.min) / 2; Vector3 halfLengths = lengths / 2; for (int i = 0; i < 8; i++) { childrenTriangles = new List <Triangle>(); Vector3 ooffset = Vector3.Multiply(lengths, offsets[i]); newBox = new BoundingBox(bounds.center + ooffset, halfLengths.X, halfLengths.Y, halfLengths.Z); foreach (Triangle t in triangles) { if (newBox.triangleIntersects(t)) { childrenTriangles.Add(t); } } children[i] = new Octree(childrenTriangles, maxTriangles, maxDepth - 1, newBox); count += children[i].count; } } }
public Octree(List <Triangle> triangles, int maxTriangles, int maxDepth, BoundingBox box) { build(triangles, maxTriangles, maxDepth, box); }