示例#1
0
    private int PlanebTriangleIntersection(b_Plane plane, bTriangle triangle,
                                           out ActiveNode <bVertex>[] intersection,
                                           bool duplicateIntersectionPoints = false,
                                           bool useSecondLookupTable        = false)
    {
        intersection    = new ActiveNode <bVertex> [3];
        intersection[0] = null; intersection[1] = null; intersection[2] = null;
        int nullCount = 0;

        Vector3 vec;
        float   interp;

        for (int i = 0; i < 3; i++)
        {
            int j = (i + 1) % 3;

            int vi = triangle.GetVertexIndex(i);
            int vj = triangle.GetVertexIndex(j);

            if (LineIntersectLookup(vi, vj, !useSecondLookupTable) == lineLookupBlank)
            {
                nullCount++;
                continue;
            }
            else if (LineIntersectLookup(vi, vj, !useSecondLookupTable) != null)
            {
                intersection[i] = LineIntersectLookup(vi, vj, !useSecondLookupTable);
            }
            else if (PlaneSegmentIntersection(plane, triangle.GetVertex(i), triangle.GetVertex(j), out vec, out interp) == 1)
            {
                bVertex newVert = new bVertex(
                    new Vector3(vec.x, vec.y, vec.z),
                    Vector2.Lerp(triangle.GetUV(i), triangle.GetUV(j), interp)
                    );

                intersection[i] = AddVertex(newVert, true);
                SetLineIntersect(vi, vj, intersection[i], !useSecondLookupTable);

                if (duplicateIntersectionPoints)
                {
                    AddVertex(new bVertex(
                                  new Vector3(vec.x, vec.y, vec.z),
                                  Vector2.Lerp(triangle.GetUV(i), triangle.GetUV(j), interp)),
                              true);
                }
            }
            else
            {
                SetLineIntersect(vi, vj, lineLookupBlank, !useSecondLookupTable);
                nullCount++;
            }
        }

        if (nullCount > 1)
        {
            return(0);
        }

        return(1);
    }
示例#2
0
    private ActiveNode <bTriangle> AddbTriangle(bTriangle triangle, bool active = true)
    {
        ActiveNode <bTriangle> ret = triangles.Add(triangle, active);

        history.AddChange(ret, null, false, true);

        return(ret);
    }
示例#3
0
    private void ChangebTriangle(ActiveNode <bTriangle> node, bTriangle newVal, bool activityToggle = false)
    {
        history.AddChange(node, node.data, activityToggle);

        if (newVal != null)
        {
            node.data = newVal;
        }

        if (activityToggle)
        {
            triangles.SetActivity(node, !node.IsActive());
        }
    }
示例#4
0
    public void AddChange(ActiveNode <bTriangle> node, bTriangle oldData, bool activityToggle, bool newNode = false)
    {
        if (MaxCount <= 0)
        {
            return;
        }

        bTriangle_Change t;

        t.node = node;
        if (oldData != null)
        {
            t.oldData = oldData.GetCopy();
        }
        else
        {
            t.oldData = null;
        }
        t.activityToggle = activityToggle;
        t.newNode        = newNode;

        runningbTriangleChanges.Add(t);
    }
示例#5
0
    public bTriangle GetCopy()
    {
        bTriangle t = new bTriangle(n1, n2, n3);

        return(t);
    }
示例#6
0
    private void bTriangleBisection(b_Plane plane, Vector3 translationSide, ActiveNode <bTriangle> triangleNode,
                                    HashSet <ActiveNode <bVertex> > verticesToBeTranslated,
                                    List <ActiveNode <bTriangle> > trianglesInBetween = null,
                                    bool useSecondLookupTable = false)
    {
        bTriangle triangle = triangleNode.data;

        ActiveNode <bVertex>[] ints;

        bool duplicateIntersectionPoints = trianglesInBetween == null;

        // Check if the plane intersects the triangle, output intersections to 'ints'
        // Vertices are automatically created in the 'vertices' list by this function
        if (PlanebTriangleIntersection(plane, triangle, out ints, duplicateIntersectionPoints, useSecondLookupTable) == 0)
        {
            // If no intersections...
            if (plane.uPlane.SameSide(triangle.GetVertex(0), translationSide))
            {
                if (trianglesInBetween != null)
                {
                    trianglesInBetween.Add(triangleNode);
                }
                else
                {
                    // This is a triangle that needs to be translated
                    for (int i = 0; i < 3; i++)
                    {
                        // Add it's vertices to the list to be translated
                        verticesToBeTranslated.Add(triangle.GetNode(i));
                    }
                }
            }
            else
            {
                if (useSecondLookupTable)
                {
                    // This is a triangle that needs to be translated
                    for (int i = 0; i < 3; i++)
                    {
                        // Add it's vertices to the list to be translated
                        verticesToBeTranslated.Add(triangle.GetNode(i));
                    }
                }
            }

            // Return, we don't care about this triangle beyond which side of the plane it's on
            return;
        }

        // Find the point that is on it's own
        int singleInd = -1;

        for (singleInd = 0; singleInd < 3; singleInd++)
        {
            if (ints[singleInd] == null)
            {
                singleInd = (singleInd + 2) % 3;
                break;
            }
        }

        // Check if the single point is on the translation side
        bool translatedVertex = plane.uPlane.SameSide(triangle.GetVertex(singleInd), translationSide) ? true : false;

        // Get the generated intersection points
        ActiveNode <bVertex> single1  = ints[singleInd];
        ActiveNode <bVertex> single1T = single1.nextActiveNode;
        ActiveNode <bVertex> single2  = ints[(singleInd + 2) % 3];
        ActiveNode <bVertex> single2T = single2.nextActiveNode;

        // Add the single1T and single2T to the list
        if (!useSecondLookupTable)
        {
            verticesToBeTranslated.Add(single1T);
            verticesToBeTranslated.Add(single2T);
        }

        // If the single point is not on the translation side
        if (!translatedVertex)
        {
            // Switch the intersection points (switch the references, so the triangles are made properly)
            if (duplicateIntersectionPoints)
            {
                single1  = single1.nextActiveNode;
                single1T = single1T.prevActiveNode;
                single2  = single2.nextActiveNode;
                single2T = single2T.prevActiveNode;
            }


            if (!useSecondLookupTable)
            {
                // singleInd+1 and singleInd+2 need to be translated, add them to the list
                verticesToBeTranslated.Add(triangle.GetNode((singleInd + 1) % 3));
                verticesToBeTranslated.Add(triangle.GetNode((singleInd + 2) % 3));
            }
            else
            {
                // SingleInd needs to be translated
                verticesToBeTranslated.Add(triangle.GetNode(singleInd));
            }
        }
        else
        {
            if (!useSecondLookupTable)
            {
                // SingleInd needs to be translated
                verticesToBeTranslated.Add(triangle.GetNode(singleInd));
            }
            else
            {
                // singleInd+1 and singleInd+2 need to be translated, add them to the list
                verticesToBeTranslated.Add(triangle.GetNode((singleInd + 1) % 3));
                verticesToBeTranslated.Add(triangle.GetNode((singleInd + 2) % 3));
            }
        }

        if (!duplicateIntersectionPoints)
        {
            single1T = single1;
            single2T = single2;

            verticesToBeTranslated.Add(single1);
            verticesToBeTranslated.Add(single2);
        }

        /////// Remake the triangle (possibly with duplicate intersections [when expanding])

        // Make the new quad bridging the parts of the triangle
        if (duplicateIntersectionPoints)
        {
            AddbTriangle(new bTriangle(single1T, single1, single2));
            AddbTriangle(new bTriangle(single2T, single1T, single2));
        }

        // Make the quad "base" of the bisected triangle
        ActiveNode <bTriangle> t1 = AddbTriangle(new bTriangle(
                                                     triangle.GetNode((singleInd + 1) % 3),
                                                     triangle.GetNode((singleInd + 2) % 3),
                                                     single1), true);
        ActiveNode <bTriangle> t2 = AddbTriangle(new bTriangle(
                                                     single2,
                                                     single1,
                                                     triangle.GetNode((singleInd + 2) % 3)), true);

        // Remake the "tip" of the triangle
        ChangebTriangle(triangleNode, new bTriangle(triangle.GetNode(singleInd), single1T, single2T));

        if (trianglesInBetween != null)
        {
            if (translatedVertex)
            {
                trianglesInBetween.Add(triangleNode);
            }
            else
            {
                trianglesInBetween.Add(t1);
                trianglesInBetween.Add(t2);
            }
        }
    }