public Octree(Primatives.TriangleMesh Mesh, int MaxLevels) { Vector3[] maxmin = maxMin(Mesh); Vector3 max = maxmin[0]; Vector3 min = maxmin[1]; this.mesh = Mesh; this.maxDivisions = MaxLevels; center = (maxmin[0] + maxmin[1]) / 2; size = (maxmin[0] - maxmin[1]); }
public float LineTriangle(Primatives.Triangle Triangle, Primatives.Line Line) { float x = -Vector3.Dot((Line.Vertices[0] - Triangle.Vertices[0]), Triangle.Normal) / Vector3.Dot(Line.Vertices[1] - Line.Vertices[0], Triangle.Normal); if (x < 0 || x > 1) return -1; else { Vector3 intersection = Line.Vertices[0] + x * Vector3.Normalize(Line.Vertices[1] - Line.Vertices[0]); if (PointInTriangle(intersection, Triangle.Vertices[0], Triangle.Vertices[1], Triangle.Vertices[2])) return x; else return -1; } }
Vector3[] maxMin(Primatives.TriangleMesh Mesh) { Vector3 max = Vector3.Zero; Vector3 min = Vector3.Zero; Vector3[] maxmin = new Vector3[2]; foreach(Primatives.Triangle Triangle in Mesh.Triangles) { for(int i = 0; i < 3; i++) { if (Triangle.Vertices[i].X > max.X) max.X = Triangle.Vertices[i].X; if (Triangle.Vertices[i].Y > max.Y) max.Y = Triangle.Vertices[i].Y; if (Triangle.Vertices[i].Z > max.Z) max.Z = Triangle.Vertices[i].Z; if (Triangle.Vertices[i].X < min.X) min.X = Triangle.Vertices[i].X; if (Triangle.Vertices[i].Y < min.Y) min.Y = Triangle.Vertices[i].Y; if (Triangle.Vertices[i].Z < min.Z) min.Z = Triangle.Vertices[i].Z; } } maxmin[0] = max; maxmin[1] = min; return maxmin; }