Esempio n. 1
0
        private int Split(Triangle t, ref List <Triangle> t1, ref List <Triangle> t2)
        {
            Vector3D normal = t.Normal;

            int[] v   = new int[3];
            int   val = 0;
            int   i   = 0;

            foreach (Vector3D pt in t.Points)
            {
                v[i] = TestPoint(pt);
                val += v[i];
                i++;
            }

            switch (val)
            {
            case 3:
                t1.Add(t);
                return(1);

            case -3:
                t2.Add(t);
                return(1);

            case 2:
                t1.Add(t);
                return(1);

            case -2:
                t2.Add(t);
                return(1);

            case 0:
                if (v[0] != 0 || v[1] != 0 || v[2] != 0)
                {
                    int pivot = 0, positive = 0, negative = 0;
                    if (0 == v[0])
                    {
                        pivot = 0;
                        if (v[1] > 0)
                        {
                            positive = 1; negative = 2;
                        }
                        else
                        {
                            positive = 2; negative = 1;
                        }
                    }
                    if (0 == v[1])
                    {
                        pivot = 1;
                        if (v[0] > 0)
                        {
                            positive = 0; negative = 2;
                        }
                        else
                        {
                            positive = 2; negative = 0;
                        }
                    }
                    if (0 == v[2])
                    {
                        pivot = 2;
                        if (v[0] > 0)
                        {
                            positive = 0; negative = 1;
                        }
                        else
                        {
                            positive = 1; negative = 0;
                        }
                    }
                    // here positive, pivot and negative are ready
                    Vector3D ptInter = Vector3D.Zero;
                    TestLine(t.Points[positive], t.Points[negative], ref ptInter);
                    t1.Add(new Triangle(t.PickId, t.Points[positive], t.EdgeDrawn(positive, negative), ptInter, false, t.Points[pivot], t.EdgeDrawn(pivot, positive), t.ColorFill));
                    t2.Add(new Triangle(t.PickId, t.Points[negative], t.EdgeDrawn(negative, pivot), t.Points[pivot], false, ptInter, t.EdgeDrawn(positive, negative), t.ColorFill));
                    return(2);
                }
                else
                {
                    t1.Add(t);
                    return(1);
                }

            case -1:
                if (0 == v[0] * v[1] * v[2])
                {
                    t2.Add(t);
                    return(1);
                }
                {
                    int positive = 0, negative1 = 0, negative2 = 0;
                    if (v[0] == 1)
                    {
                        positive = 0; negative1 = 1; negative2 = 2;
                    }
                    if (v[1] == 1)
                    {
                        positive = 1; negative1 = 0; negative2 = 2;
                    }
                    if (v[2] == 1)
                    {
                        positive = 2; negative1 = 0; negative2 = 1;
                    }
                    Vector3D ptInter1 = Vector3D.Zero, ptInter2 = Vector3D.Zero;
                    TestLine(t.Points[negative1], t.Points[positive], ref ptInter1);
                    TestLine(t.Points[negative2], t.Points[positive], ref ptInter2);
                    Triangle t11 = new Triangle(t.PickId, t.Points[positive], t.EdgeDrawn(positive, negative1), ptInter1, false, ptInter2, t.EdgeDrawn(negative2, positive), t.ColorFill);
                    Triangle t21 = new Triangle(t.PickId, t.Points[negative2], t.EdgeDrawn(negative2, positive), ptInter2, false, ptInter1, false, t.ColorFill);
                    Triangle t22 = new Triangle(t.PickId, t.Points[negative2], false, ptInter1, t.EdgeDrawn(positive, negative1), t.Points[negative1], t.EdgeDrawn(negative1, negative2), t.ColorFill);

                    if (!t11.IsFlat)
                    {
                        t1.Add(AlignNormal(t11, normal));
                    }
                    if (!t21.IsFlat)
                    {
                        t2.Add(AlignNormal(t21, normal));
                    }
                    if (!t22.IsFlat)
                    {
                        t2.Add(AlignNormal(t22, normal));
                    }
                    return(3);
                }

            case 1:
                if (0 == v[0] * v[1] * v[2])
                {
                    t1.Add(t);
                    return(1);
                }
                {
                    int negative = 0, positive1 = 0, positive2 = 0;
                    if (v[0] == -1)
                    {
                        negative = 0; positive1 = 1; positive2 = 2;
                    }
                    if (v[1] == -1)
                    {
                        negative = 1; positive1 = 0; positive2 = 2;
                    }
                    if (v[2] == -1)
                    {
                        negative = 2; positive1 = 0; positive2 = 1;
                    }
                    Vector3D ptInter1 = Vector3D.Zero, ptInter2 = Vector3D.Zero;
                    TestLine(t.Points[positive1], t.Points[negative], ref ptInter1);
                    TestLine(t.Points[positive2], t.Points[negative], ref ptInter2);
                    Triangle t11 = new Triangle(t.PickId, t.Points[positive1], t.EdgeDrawn(positive1, negative), ptInter1, false, ptInter2, false, t.ColorFill);
                    Triangle t12 = new Triangle(t.PickId, t.Points[positive1], false, ptInter2, t.EdgeDrawn(negative, positive2), t.Points[positive2], t.EdgeDrawn(positive2, positive1), t.ColorFill);
                    Triangle t21 = new Triangle(t.PickId, t.Points[negative], t.EdgeDrawn(negative, positive2), ptInter2, false, ptInter1, t.EdgeDrawn(positive1, negative), t.ColorFill);

                    if (!t11.IsFlat)
                    {
                        t1.Add(AlignNormal(t11, normal));
                    }
                    if (!t12.IsFlat)
                    {
                        t1.Add(AlignNormal(t12, normal));
                    }
                    if (!t21.IsFlat)
                    {
                        t2.Add(AlignNormal(t21, normal));
                    }
                    return(3);
                }

            default:
            {
                StringBuilder sb = new StringBuilder();
                foreach (Vector3D pt in t.Points)
                {
                    sb.AppendLine(pt.ToString());
                }
                throw new Exception(sb.ToString());
            }
            }
        }
Esempio n. 2
0
 public BSPNodeTri(Triangle t)
 {
     Triangle = t;
     InitEquation(t, ref A, ref B, ref C, ref D);
 }
Esempio n. 3
0
 public void AddTriangle(Triangle triangle) => Triangles.Add(triangle);