public static KCLOctree FromTriangles(Triangle[] Triangles, KCLHeader Header, int MaxRootSize = 2048, int MinRootSize = 128, int MinCubeSize = 32, int MaxNrTris = 10)//35) { Header.Unknown1 = 30; Header.Unknown2 = 25; Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue); Dictionary <ushort, Triangle> tt = new Dictionary <ushort, Triangle>(); ushort index = 0; foreach (var t in Triangles) { if (t.PointA.X < min.X) { min.X = t.PointA.X; } if (t.PointA.Y < min.Y) { min.Y = t.PointA.Y; } if (t.PointA.Z < min.Z) { min.Z = t.PointA.Z; } if (t.PointA.X > max.X) { max.X = t.PointA.X; } if (t.PointA.Y > max.Y) { max.Y = t.PointA.Y; } if (t.PointA.Z > max.Z) { max.Z = t.PointA.Z; } if (t.PointB.X < min.X) { min.X = t.PointB.X; } if (t.PointB.Y < min.Y) { min.Y = t.PointB.Y; } if (t.PointB.Z < min.Z) { min.Z = t.PointB.Z; } if (t.PointB.X > max.X) { max.X = t.PointB.X; } if (t.PointB.Y > max.Y) { max.Y = t.PointB.Y; } if (t.PointB.Z > max.Z) { max.Z = t.PointB.Z; } if (t.PointC.X < min.X) { min.X = t.PointC.X; } if (t.PointC.Y < min.Y) { min.Y = t.PointC.Y; } if (t.PointC.Z < min.Z) { min.Z = t.PointC.Z; } if (t.PointC.X > max.X) { max.X = t.PointC.X; } if (t.PointC.Y > max.Y) { max.Y = t.PointC.Y; } if (t.PointC.Z > max.Z) { max.Z = t.PointC.Z; } tt.Add(index, t); index++; } //in real mkds, 25 is subtracted from the min pos //TODO: after that, from some of the components (may be more than one) 30 is subtracted aswell => How do I know from which ones I have to do that? min -= new Vector3(50f, 80f, 50f); max += new Vector3(50f, 50f, 50f); //TODO: +30 Header.OctreeOrigin = min; Header.OctreeMax = max; Vector3 size = max - min; float mincomp = Math.Min(Math.Min(size.X, size.Y), size.Z); int CoordShift = MathUtil.GetNearest2Power(mincomp); if (CoordShift > MathUtil.GetNearest2Power(MaxRootSize)) { CoordShift = MathUtil.GetNearest2Power(MaxRootSize); } //else if (CoordShift < Get2Power(MinRootSize)) CoordShift = Get2Power(MinRootSize); Header.CoordShift = (uint)CoordShift; int cubesize = 1 << CoordShift; int NrX = (1 << MathUtil.GetNearest2Power(size.X)) / cubesize; int NrY = (1 << MathUtil.GetNearest2Power(size.Y)) / cubesize; int NrZ = (1 << MathUtil.GetNearest2Power(size.Z)) / cubesize; if (NrX <= 0) { NrX = 1; } if (NrY <= 0) { NrY = 1; } if (NrZ <= 0) { NrZ = 1; } Header.YShift = (uint)(MathUtil.GetNearest2Power(size.X) - CoordShift); Header.ZShift = (uint)(MathUtil.GetNearest2Power(size.X) - CoordShift + MathUtil.GetNearest2Power(size.Y) - CoordShift); Header.XMask = 0xFFFFFFFF << MathUtil.GetNearest2Power(size.X); Header.YMask = 0xFFFFFFFF << MathUtil.GetNearest2Power(size.Y); Header.ZMask = 0xFFFFFFFF << MathUtil.GetNearest2Power(size.Z); Header.n_x = (float)KCLOctree.next_exponent(max.X - min.X); Header.n_y = (float)KCLOctree.next_exponent(max.Y - min.Y); Header.n_z = (float)KCLOctree.next_exponent(max.Z - min.Z); KCLOctree k = new KCLOctree(); k.RootNodes = new KCLOctreeNode[NrX * NrY * NrZ]; int i = 0; for (int z = 0; z < NrZ; z++) { for (int y = 0; y < NrY; y++) { for (int x = 0; x < NrX; x++) { Vector3 pos = min + ((float)cubesize) * new Vector3(x, y, z); k.RootNodes[i] = KCLOctreeNode.Generate(tt, pos, cubesize, MaxNrTris, MinCubeSize); i++; } } } return(k); }