public BBTreeNode(BinaryReader br) { ball.Center.X = br.ReadSingle(); ball.Center.Y = br.ReadSingle(); ball.Center.Z = br.ReadSingle(); ball.Radius = br.ReadSingle(); int childrenCount = br.ReadInt32(); if (childrenCount == 0) { faceData = new TreeFace(); faceData.ReadData(br); } else { children = new BaseTreeNode[childrenCount]; for (int i = 0; i < childrenCount; i++) { children[i] = new AABBTreeNode(br); } } }
public AABBTreeNode(List <TreeFace> remains) { aabb.Min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); aabb.Max = new Vector3(float.MinValue, float.MinValue, float.MinValue); int rcount = remains.Count; for (int i = 0; i < rcount; i++) { Vector3 va = remains[i].a; Vector3 vb = remains[i].b; Vector3 vc = remains[i].c; if (va.X < aabb.Min.X) { aabb.Min.X = va.X; } if (va.X > aabb.Max.X) { aabb.Max.X = va.X; } if (va.Y < aabb.Min.Y) { aabb.Min.Y = va.Y; } if (va.Y > aabb.Max.Y) { aabb.Max.Y = va.Y; } if (va.Z < aabb.Min.Z) { aabb.Min.Z = va.Z; } if (va.Z > aabb.Max.Z) { aabb.Max.Z = va.Z; } if (vb.X < aabb.Min.X) { aabb.Min.X = vb.X; } if (vb.X > aabb.Max.X) { aabb.Max.X = vb.X; } if (vb.Y < aabb.Min.Y) { aabb.Min.Y = vb.Y; } if (vb.Y > aabb.Max.Y) { aabb.Max.Y = vb.Y; } if (vb.Z < aabb.Min.Z) { aabb.Min.Z = vb.Z; } if (vb.Z > aabb.Max.Z) { aabb.Max.Z = vb.Z; } if (vc.X < aabb.Min.X) { aabb.Min.X = vc.X; } if (vc.X > aabb.Max.X) { aabb.Max.X = vc.X; } if (vc.Y < aabb.Min.Y) { aabb.Min.Y = vc.Y; } if (vc.Y > aabb.Max.Y) { aabb.Max.Y = vc.Y; } if (vc.Z < aabb.Min.Z) { aabb.Min.Z = vc.Z; } if (vc.Z > aabb.Max.Z) { aabb.Max.Z = vc.Z; } } if (rcount > 1) { //centre可能不在AABB内 Vector3 centre = new Vector3(); for (int i = 0; i < rcount; i++) { Vector3.Add(ref centre, ref remains[i].centre, out centre); } Vector3.Multiply(ref centre, 1f / (float)rcount, out centre); float xoy = centre.Z; float yoz = centre.X; float xoz = centre.Y; List <TreeFace>[] lim = new List <TreeFace>[8] { new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>() }; for (int i = 0; i < rcount; i++) { float fxy = remains[i].centre.Z - xoy; // xoy[remains[i].mCentre]; float fyz = remains[i].centre.X - yoz; //yoz[remains[i].mCentre]; float fxz = remains[i].centre.Y - xoz; //xoz[remains[i].mCentre]; if (fxz > 0) // y>0 //1-4卦限 { if (fyz > 0) // x>0 { if (fxy > 0) // z>0 { lim[0].Add(remains[i]); } else { lim[3].Add(remains[i]); } } else if (fxy > 0) // z>0 { lim[1].Add(remains[i]); } else { lim[2].Add(remains[i]); } } else //5-8卦限 if (fyz > 0) // x>0 { if (fxy > 0) // z>0 { lim[4].Add(remains[i]); } else { lim[7].Add(remains[i]); } } else if (fxy > 0) // z>0 { lim[5].Add(remains[i]); } else { lim[6].Add(remains[i]); } } remains.Clear(); int childrenUBound = -1; bool[] usestat = new bool[8]; for (int i = 0; i < 8; i++) { if (lim[i].Count > 0) { childrenUBound++; usestat[i] = true; } else { usestat[i] = false; } } children = new BaseTreeNode[childrenUBound + 1]; int k = 0; for (int i = 0; i < 8; i++) { if (usestat[i]) { children[k] = new AABBTreeNode(lim[i]); k++; } } } else { //childrenUBound = -1; faceData = remains[0]; remains.Clear(); } }
public BBTreeNode(List <TreeFace> remains) { int i; int rcount = remains.Count; for (i = 0; i < rcount; i++) { ball.Center += remains[i].centre; } //for (i = 0; i < rcount; i++) // bBall.vCentre += remains[i].vA; ball.Center *= (1f / (float)i);// /= (float)i; ball.Radius = 0; for (i = 0; i < rcount; i++) { float dist = MathEx.Distance(ref ball.Center, ref remains[i].a); // bBall.vCentre & remains[i].vA; if (dist > ball.Radius) { ball.Radius = dist; } dist = MathEx.Distance(ref ball.Center, ref remains[i].b); // bBall.vCentre & remains[i].vB; if (dist > ball.Radius) { ball.Radius = dist; } dist = MathEx.Distance(ref ball.Center, ref remains[i].c); // bBall.vCentre & remains[i].vC; if (dist > ball.Radius) { ball.Radius = dist; } } if (rcount > 1) { float xoy = ball.Center.Z; float yoz = ball.Center.X; float xoz = ball.Center.Y; List <TreeFace>[] lim = new List <TreeFace>[8] { new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>(), new List <TreeFace>() }; for (i = 0; i < rcount; i++) { float fxy = remains[i].centre.Z - xoy; // xoy[remains[i].mCentre]; float fyz = remains[i].centre.X - yoz; //yoz[remains[i].mCentre]; float fxz = remains[i].centre.Y - xoz; //xoz[remains[i].mCentre]; if (fxz > 0) // y>0 //1-4卦限 { if (fyz > 0) // x>0 { if (fxy > 0) // z>0 { lim[0].Add(remains[i]); } else { lim[3].Add(remains[i]); } } else if (fxy > 0) // z>0 { lim[1].Add(remains[i]); } else { lim[2].Add(remains[i]); } } else //5-8卦限 if (fyz > 0) // x>0 { if (fxy > 0) // z>0 { lim[4].Add(remains[i]); } else { lim[7].Add(remains[i]); } } else if (fxy > 0) // z>0 { lim[5].Add(remains[i]); } else { lim[6].Add(remains[i]); } } //remains.Clear(); int childrenUBound = -1; bool[] usestat = new bool[8]; for (i = 0; i < 8; i++) { if (lim[i].Count > 0) { childrenUBound++; usestat[i] = true; } else { usestat[i] = false; } } children = new BaseTreeNode[childrenUBound + 1]; int k = 0; for (i = 0; i < 8; i++) { if (usestat[i]) { children[k] = new BBTreeNode(lim[i]); k++; } } } else { //childrenUBound = -1; faceData = remains[0]; remains.Clear(); } }