Exemple #1
0
    private void GenerateDelaunay(HashSet <MyVector2> points_2d)
    {
        //Normalize
        AABB2 normalizingBox = new AABB2(new List <MyVector2>(points_2d));

        float dMax = HelpMethods.CalculateDMax(normalizingBox);

        HashSet <MyVector2> points_2d_normalized = HelpMethods.Normalize(points_2d, normalizingBox, dMax);


        //Generate delaunay
        //HalfEdgeData2 delaunayData = _Delaunay.FlippingEdges(points_2d_normalized, new HalfEdgeData2());
        HalfEdgeData2 delaunayData = _Delaunay.PointByPoint(points_2d_normalized, new HalfEdgeData2());


        //UnNormalize
        HalfEdgeData2 triangleData = HelpMethods.UnNormalize(delaunayData, normalizingBox, dMax);

        //From halfedge to triangle
        HashSet <Triangle2> triangles = _TransformBetweenDataStructures.HalfEdge2ToTriangle2(triangleData);

        //Make sure they have the correct orientation
        triangles = HelpMethods.OrientTrianglesClockwise(triangles);

        //2d to 3d
        HashSet <Triangle3> triangles_3d = new HashSet <Triangle3>();


        int counter = -1;

        foreach (Triangle2 t in triangles)
        {
            counter++;

            //if (counter != 2)
            //{
            //    continue;
            //}

            triangles_3d.Add(new Triangle3(t.p1.ToMyVector3_Yis3D(), t.p2.ToMyVector3_Yis3D(), t.p3.ToMyVector3_Yis3D()));

            //Debug.Log($"p1: {t.p1.x} {t.p1.y} p2: {t.p2.x} {t.p2.y} p3: {t.p3.x} {t.p3.y}");

            //MyVector2 circleCenter = _Geometry.CalculateCircleCenter(t.p1, t.p2, t.p3);

            //Debug.Log("Circle center: " + circleCenter.x + " " + circleCenter.y);
        }

        Mesh delaunayMesh = _TransformBetweenDataStructures.Triangle3ToCompressedMesh(triangles_3d);

        //Display the delaunay triangles
        TestAlgorithmsHelpMethods.DisplayMeshEdges(delaunayMesh, Color.black);
    }
    private void TestGreinerHormann(List <MyVector2> poly, List <MyVector2> clipPoly)
    {
        //Normalize to range 0-1
        //We have to use all data to normalize
        List <MyVector2> allPoints = new List <MyVector2>();

        allPoints.AddRange(poly);
        allPoints.AddRange(clipPoly);

        AABB2 normalizingBox = new AABB2(allPoints);

        float dMax = HelpMethods.CalculateDMax(normalizingBox);

        List <MyVector2> poly_normalized = HelpMethods.Normalize(poly, normalizingBox, dMax);

        List <MyVector2> clipPoly_normalized = HelpMethods.Normalize(clipPoly, normalizingBox, dMax);



        //In this case we can get back multiple parts of the polygon because one of the
        //polygons doesnt have to be convex
        //If you pick boolean operation: intersection you should get the same result as with the Sutherland-Hodgman
        List <List <MyVector2> > finalPolygon = GreinerHormann.ClipPolygons(poly_normalized, clipPoly_normalized, BooleanOperation.Intersection);

        Debug.Log("Total polygons: " + finalPolygon.Count);

        for (int i = 0; i < finalPolygon.Count; i++)
        {
            List <MyVector2> thisPolygon_normalized = finalPolygon[i];

            Debug.Log("Vertices in this polygon: " + thisPolygon_normalized.Count);

            //Unnormalized
            List <MyVector2> thisPolygon = HelpMethods.UnNormalize(thisPolygon_normalized, normalizingBox, dMax);

            //2d to 3d
            List <Vector3> polygonAfterClipping3D = new List <Vector3>();

            foreach (MyVector2 v in thisPolygon)
            {
                polygonAfterClipping3D.Add(v.ToVector3());
            }

            //Display
            DisplayPolygon(polygonAfterClipping3D, Color.red);
        }
    }
Exemple #3
0
    private void OnDrawGizmos()
    {
        //
        // Init the sites
        //

        //HashSet<Vector3> sites_3d = GetRandomSites();
        //HashSet<Vector3> sites_3d = GetCustomSites();
        HashSet <Vector3> sites_3d = GetCustomSites2();

        //3d to 2d
        HashSet <MyVector2> sites_2d = new HashSet <MyVector2>();

        foreach (Vector3 v in sites_3d)
        {
            sites_2d.Add(v.ToMyVector2());
        }


        //Normalize
        AABB2 normalizingBox = new AABB2(new List <MyVector2>(sites_2d));

        float dMax = HelpMethods.CalculateDMax(normalizingBox);

        HashSet <MyVector2> randomSites_2d_normalized = HelpMethods.Normalize(sites_2d, normalizingBox, dMax);


        //Generate the voronoi
        List <VoronoiCell2> voronoiCells = _Voronoi.DelaunyToVoronoi(randomSites_2d_normalized);


        //Unnormalize
        voronoiCells = HelpMethods.UnNormalize(voronoiCells, normalizingBox, dMax);


        //Display the voronoi diagram
        DisplayVoronoiCells(voronoiCells);

        //Display the sites
        TestAlgorithmsHelpMethods.DisplayPoints(sites_3d, 0.5f, Color.black);

        //Generate delaunay for comparisons
        GenerateDelaunay(sites_2d);
    }
Exemple #4
0
    private void Start()
    {
        //Init GUI
        flipText.text = "Flipped edges: " + 0;

        //Generate the points we want to triangulate
        HashSet <Vector3> points = TestAlgorithmsHelpMethods.GenerateRandomPoints(seed, halfMapSize, numberOfPoints);

        //From 3d to 2d
        HashSet <MyVector2> points_2d = new HashSet <MyVector2>();

        foreach (Vector3 v in points)
        {
            points_2d.Add(v.ToMyVector2());
        }


        //Normalize to range 0-1
        //We should use all points, including the constraints
        List <MyVector2> allPoints = new List <MyVector2>();

        allPoints.AddRange(new List <MyVector2>(points_2d));

        normalizingBox = HelpMethods.GetAABB(new List <MyVector2>(points_2d));

        dMax = HelpMethods.CalculateDMax(normalizingBox);

        HashSet <MyVector2> points_2d_normalized = HelpMethods.Normalize(points_2d, normalizingBox, dMax);


        //Run delaunay with some algorithm
        FlipEdgesVisual flipEdges = GetComponent <FlipEdgesVisual>();

        if (flipEdges != null)
        {
            flipEdges.Delaunay_FlipEdges_Visualizer(points_2d_normalized, new HalfEdgeData2());
        }
    }
Exemple #5
0
    private void GenerateDelaunay(HashSet <MyVector2> points_2d)
    {
        //Normalize
        AABB2 normalizingBox = new AABB2(new List <MyVector2>(points_2d));

        float dMax = HelpMethods.CalculateDMax(normalizingBox);

        HashSet <MyVector2> points_2d_normalized = HelpMethods.Normalize(points_2d, normalizingBox, dMax);


        //Generate delaunay
        //HalfEdgeData2 delaunayData = _Delaunay.FlippingEdges(points_2d_normalized, new HalfEdgeData2());
        HalfEdgeData2 delaunayData = _Delaunay.PointByPoint(points_2d_normalized, new HalfEdgeData2());


        //UnNormalize
        HalfEdgeData2 triangleData = HelpMethods.UnNormalize(delaunayData, normalizingBox, dMax);

        //From halfedge to triangle
        HashSet <Triangle2> triangles = _TransformBetweenDataStructures.HalfEdge2ToTriangle2(triangleData);

        //Make sure they have the correct orientation
        triangles = HelpMethods.OrientTrianglesClockwise(triangles);

        //2d to 3d
        HashSet <Triangle3> triangles_3d = new HashSet <Triangle3>();

        foreach (Triangle2 t in triangles)
        {
            triangles_3d.Add(new Triangle3(t.p1.ToMyVector3(), t.p2.ToMyVector3(), t.p3.ToMyVector3()));
        }

        Mesh delaunayMesh = _TransformBetweenDataStructures.Triangle3ToCompressedMesh(triangles_3d);

        //Display the delaunay triangles
        TestAlgorithmsHelpMethods.DisplayMeshEdges(delaunayMesh, Color.black);
    }
    private void TestSutherlandHodgman(List <MyVector2> poly, List <MyVector2> clipPoly)
    {
        //Normalize to range 0-1
        //We have to use all data to normalize
        List <MyVector2> allPoints = new List <MyVector2>();

        allPoints.AddRange(poly);
        allPoints.AddRange(clipPoly);

        AABB2 normalizingBox = new AABB2(allPoints);

        float dMax = HelpMethods.CalculateDMax(normalizingBox);

        List <MyVector2> poly_normalized = HelpMethods.Normalize(poly, normalizingBox, dMax);

        List <MyVector2> clipPoly_normalized = HelpMethods.Normalize(clipPoly, normalizingBox, dMax);


        //Main algorithm
        List <MyVector2> polygonAfterClipping_Normalized = SutherlandHodgman.ClipPolygon(poly_normalized, clipPoly_normalized);


        //UnNormalize
        List <MyVector2> polygonAfterClipping = HelpMethods.UnNormalize(polygonAfterClipping_Normalized, normalizingBox, dMax);

        //2d to 3d
        List <Vector3> polygonAfterClipping3D = new List <Vector3>();

        foreach (MyVector2 v in polygonAfterClipping)
        {
            polygonAfterClipping3D.Add(v.ToVector3());
        }

        //Display
        DisplayPolygon(polygonAfterClipping3D, Color.red);
    }
Exemple #7
0
    public void GenererateTriangulation()
    {
        //Get the random points
        HashSet <Vector3> points = TestAlgorithmsHelpMethods.GenerateRandomPoints(seed, halfMapSize, numberOfPoints);


        //From 3d to 2d
        HashSet <MyVector2> points_2d = new HashSet <MyVector2>();

        foreach (Vector3 v in points)
        {
            points_2d.Add(v.ToMyVector2());
        }

        List <MyVector2> constraints_2d = new List <MyVector2>();

        foreach (Vector3 v in constraints)
        {
            constraints_2d.Add(v.ToMyVector2());
        }

        //Normalize to range 0-1
        //We should use all points, including the constraints
        List <MyVector2> allPoints = new List <MyVector2>();

        allPoints.AddRange(new List <MyVector2>(points_2d));
        allPoints.AddRange(constraints_2d);

        AABB2 normalizingBox = new AABB2(new List <MyVector2>(points_2d));

        float dMax = HelpMethods.CalculateDMax(normalizingBox);

        HashSet <MyVector2> points_2d_normalized = HelpMethods.Normalize(points_2d, normalizingBox, dMax);

        List <MyVector2> constraints_2d_normalized = HelpMethods.Normalize(constraints_2d, normalizingBox, dMax);



        //
        // Generate the triangulation
        //

        //Algorithm 1. Delaunay by triangulate all points with some bad algorithm and then flip edges until we get a delaunay triangulation
        //HalfEdgeData2 triangleData_normalized = _Delaunay.FlippingEdges(points_2d_normalized, new HalfEdgeData2());


        //Algorithm 2. Delaunay by inserting point-by-point while flipping edges after inserting a single point
        //HalfEdgeData2 triangleData_normalized = _Delaunay.PointByPoint(points_2d_normalized, new HalfEdgeData2());


        //Algorithm 3. Constrained delaunay
        HalfEdgeData2 triangleData_normalized = _Delaunay.ConstrainedBySloan(points_2d_normalized, constraints_2d_normalized, false, new HalfEdgeData2());



        //UnNormalize
        HalfEdgeData2 triangleData = HelpMethods.UnNormalize(triangleData_normalized, normalizingBox, dMax);

        //From half-edge to triangle
        HashSet <Triangle2> triangles_2d = _TransformBetweenDataStructures.HalfEdge2ToTriangle2(triangleData);

        //From triangulation to mesh

        //Make sure the triangles have the correct orientation
        triangles_2d = HelpMethods.OrientTrianglesClockwise(triangles_2d);

        //From 2d to 3d
        HashSet <Triangle3> triangles_3d = new HashSet <Triangle3>();

        foreach (Triangle2 t in triangles_2d)
        {
            triangles_3d.Add(new Triangle3(t.p1.ToMyVector3(), t.p2.ToMyVector3(), t.p3.ToMyVector3()));
        }

        triangulatedMesh = _TransformBetweenDataStructures.Triangle3ToCompressedMesh(triangles_3d);
    }
    private void Start()
    {
        //Init GUI
        flipText.text = "Flipped edges: " + 0;

        //Create the material we use to display meshes with a single color
        blackMaterial = new Material(Shader.Find("Unlit/Color"));

        blackMaterial.color = Color.black;

        //Set active point to be outside of screen
        activePoint = new MyVector2(-10000f, -10000f);

        //Generate the points we want to triangulate
        HashSet <Vector3> points = TestAlgorithmsHelpMethods.GenerateRandomPoints(seed, halfMapSize, numberOfPoints);

        //From 3d to 2d
        HashSet <MyVector2> points_2d = new HashSet <MyVector2>();

        foreach (Vector3 v in points)
        {
            points_2d.Add(v.ToMyVector2());
        }


        //Normalize to range 0-1
        //We should use all points, including the constraints
        List <MyVector2> allPoints = new List <MyVector2>();

        allPoints.AddRange(new List <MyVector2>(points_2d));

        normalizingBox = HelpMethods.GetAABB(new List <MyVector2>(points_2d));

        dMax = HelpMethods.CalculateDMax(normalizingBox);

        HashSet <MyVector2> points_2d_normalized = HelpMethods.Normalize(points_2d, normalizingBox, dMax);


        //Run delaunay with some algorithm
        //DelaunayFlipEdgesVisual flipEdges = GetComponent<DelaunayFlipEdgesVisual>();

        //if (flipEdges != null)
        //{
        //    flipEdges.StartVisualizer(points_2d_normalized, new HalfEdgeData2());
        //}

        //DelaunayPointByPointVisual pointByPoint = GetComponent<DelaunayPointByPointVisual>();

        //if (pointByPoint != null)
        //{
        //    pointByPoint.StartVisualizer(points_2d_normalized, new HalfEdgeData2());
        //}

        //Triangulate with visible edges
        VisibleEdgeVisualizer visibleEdge = GetComponent <VisibleEdgeVisualizer>();

        if (visibleEdge)
        {
            visibleEdge.StartVisualization(points_2d_normalized);
        }
    }
Exemple #9
0
    public void TriangulateThePoints()
    {
        if (pointsOnHull != null)
        {
            pointsOnHull.Clear();
        }

        //
        // Get points to triangulate
        //

        //Random points
        //points = TestAlgorithmsHelpMethods.GenerateRandomPoints(seed, mapSize, numberOfPoints);

        //Points from a plane mesh to test colinear points
        points = TestAlgorithmsHelpMethods.GeneratePointsFromPlane(planeTrans);



        //
        // Prepare the points
        //

        //3d to 2d
        HashSet <MyVector2> points_2d = new HashSet <MyVector2>();

        foreach (Vector3 v in points)
        {
            points_2d.Add(v.ToMyVector2());
        }

        //Normalize to range 0-1
        AABB2 normalizingBox = new AABB2(new List <MyVector2>(points_2d));

        float dMax = HelpMethods.CalculateDMax(normalizingBox);

        HashSet <MyVector2> points_2d_normalized = HelpMethods.Normalize(points_2d, normalizingBox, dMax);



        //
        // Triangulate points on convex hull and points inside of convex hull
        //

        //Method 1
        //Sort the points and then add triangles by checking which edge is visible to that point
        //HashSet<Triangle2> triangles_2d_normalized = _TriangulatePoints.VisibleEdgesTriangulation(points_2d_normalized);


        //Method 2
        //Triangulate the convex polygon, then add the rest of the points one-by-one
        //The old triangle the point ends up in is split into tree new triangles
        //HashSet<Triangle2> triangles_2d_normalized = _TriangulatePoints.TriangleSplitting(points_2d_normalized, addColinearPoints: true);



        //
        // Triangulate points on convex hull
        //

        //First find the convex hull of the points
        //This means that we first need to find the points on the convex hull
        List <MyVector2> pointsOnHull_normalized = _ConvexHull.JarvisMarch(points_2d_normalized);

        //Method 1
        //Go through the convex hull point-by-point and build triangles while anchoring to the first vertex
        HashSet <Triangle2> triangles_2d_normalized = _TriangulatePoints.PointsOnConvexHull(pointsOnHull_normalized, addColinearPoints: true);


        //Method 2
        //Add a point inside of the convex hull to deal with colinear points
        //MyVector2 insidePoint = HelpMethods.NormalizePoint(planeTrans.position.ToMyVector2(), normalizingBox, dMax);

        //HashSet<Triangle2> triangles_2d_normalized = _TriangulatePoints.PointsOnConvexHull(pointsOnHull_normalized, insidePoint);



        //
        // Display
        //
        Debug.Log("Number of triangles: " + triangles_2d_normalized.Count);

        /*
         * if (pointsOnHull_normalized != null)
         * {
         *  pointsOnHull = HelpMethods.UnNormalize(pointsOnHull_normalized, normalizingBox, dMax);
         * }
         */
        if (triangles_2d_normalized != null)
        {
            //Unnormalized the triangles
            HashSet <Triangle2> triangles_2d = HelpMethods.UnNormalize(triangles_2d_normalized, normalizingBox, dMax);

            testTriangles = triangles_2d;

            //Make sure the triangles have the correct orientation
            triangles_2d = HelpMethods.OrientTrianglesClockwise(triangles_2d);

            //From 2d to 3d
            HashSet <Triangle3> triangles_3d = new HashSet <Triangle3>();

            foreach (Triangle2 t in triangles_2d)
            {
                triangles_3d.Add(new Triangle3(t.p1.ToMyVector3_Yis3D(), t.p2.ToMyVector3_Yis3D(), t.p3.ToMyVector3_Yis3D()));
            }

            triangulatedMesh = _TransformBetweenDataStructures.Triangle3ToCompressedMesh(triangles_3d);
        }
    }
Exemple #10
0
    private void OnDrawGizmos()
    {
        //
        // Generate the points we are going to find the convex hull from
        //

        //Random points
        //HashSet<Vector3> points = TestAlgorithmsHelpMethods.GenerateRandomPoints(seed, mapSize, numberOfPoints);

        //Points from a plane mesh
        HashSet <Vector3> points = TestAlgorithmsHelpMethods.GeneratePointsFromPlane(planeTrans);


        //
        // Prepare the points
        //

        //From 3d to 2d
        HashSet <MyVector2> points_2d = new HashSet <MyVector2>();

        foreach (Vector3 v in points)
        {
            points_2d.Add(v.ToMyVector2());
        }

        //Normalize to range 0-1
        AABB2 normalizingBox = new AABB2(new List <MyVector2>(points_2d));

        float dMax = HelpMethods.CalculateDMax(normalizingBox);

        HashSet <MyVector2> points_2d_normalized = HelpMethods.Normalize(points_2d, normalizingBox, dMax);



        //
        // Generate the convex hull
        //



        //Algorithm 1. Jarvis March - slow but simple
        //List<MyVector2> pointsOnConvexHull_2d_normalized = _ConvexHull.JarvisMarch(points_2d_normalized);


        //Algorithm 2. Quickhull
        //List<MyVector2> pointsOnConvexHull_2d_normalized = _ConvexHull.Quickhull(points_2d_normalized, includeColinearPoints: true, normalizingBox, dMax);
        List <MyVector2> pointsOnConvexHull_2d_normalized = _ConvexHull.Quickhull(points_2d_normalized, includeColinearPoints: true);

        if (pointsOnConvexHull_2d_normalized == null)
        {
            Debug.Log("Couldnt find a convex hull");
        }
        else
        {
            Debug.Log($"Found a hull with: {pointsOnConvexHull_2d_normalized.Count} points");
        }



        //
        // Display
        //

        //Display points on the hull and lines between the points
        if (pointsOnConvexHull_2d_normalized != null)
        {
            //UnNormalize
            List <MyVector2> pointsOnConvexHull_2d = HelpMethods.UnNormalize(pointsOnConvexHull_2d_normalized, normalizingBox, dMax);

            //From 2d to 3d
            List <Vector3> pointsOnConvexHull = new List <Vector3>();

            foreach (MyVector2 v in pointsOnConvexHull_2d)
            {
                pointsOnConvexHull.Add(v.ToVector3());
            }

            //print(pointsOnConvexHull.Count);

            for (int i = 0; i < pointsOnConvexHull.Count; i++)
            {
                int i_minus_one = MathUtility.ClampListIndex(i - 1, pointsOnConvexHull.Count);

                Gizmos.DrawLine(pointsOnConvexHull[i_minus_one], pointsOnConvexHull[i]);
            }

            float size = 0.1f;
            for (int i = 1; i < pointsOnConvexHull.Count; i++)
            {
                Gizmos.DrawWireSphere(pointsOnConvexHull[i], size);

                //So we can see in which order they were added
                size += 0.01f;
            }
        }

        //Display all the original points
        foreach (Vector3 p in points)
        {
            Gizmos.DrawSphere(p, 0.1f);
        }
    }
Exemple #11
0
    public void GenerateTriangulation()
    {
        //Get the random points
        //HashSet<Vector3> randomPoints = TestAlgorithmsHelpMethods.GenerateRandomPoints(seed, halfMapSize, numberOfPoints);

        //From 3d to 2d
        //HashSet<MyVector2> randomPoints_2d = new HashSet<MyVector2>(randomPoints.Select(x => x.ToMyVector2()));

        /*
         * List<MyVector2> constraints_2d = constraints.Select(x => x.ToMyVector2()).ToList();
         *
         * //Normalize to range 0-1
         * //We should use all points, including the constraints because the hole may be outside of the random points
         * List<MyVector2> allPoints = new List<MyVector2>();
         *
         * allPoints.AddRange(new List<MyVector2>(points_2d));
         * allPoints.AddRange(constraints_2d);
         *
         * AABB2 normalizingBox = new AABB2(new List<MyVector2>(points_2d));
         *
         * float dMax = HelpMethods.CalculateDMax(normalizingBox);
         *
         * HashSet<MyVector2> points_2d_normalized = HelpMethods.Normalize(points_2d, normalizingBox, dMax);
         *
         * List<MyVector2> constraints_2d_normalized = HelpMethods.Normalize(constraints_2d, normalizingBox, dMax);
         */


        //Hull
        List <Vector3> hullPoints = TestAlgorithmsHelpMethods.GetPointsFromParent(hullConstraintParent);

        List <MyVector2> hullPoints_2d = hullPoints.Select(x => x.ToMyVector2()).ToList();;

        //Holes
        HashSet <List <MyVector2> > allHolePoints_2d = new HashSet <List <MyVector2> >();

        foreach (Transform holeParent in holeConstraintParents)
        {
            List <Vector3> holePoints = TestAlgorithmsHelpMethods.GetPointsFromParent(holeParent);

            if (holePoints != null)
            {
                List <MyVector2> holePoints_2d = holePoints.Select(x => x.ToMyVector2()).ToList();

                allHolePoints_2d.Add(holePoints_2d);
            }
        }


        //Normalize to range 0-1
        //We should use all points, including the constraints because the hole may be outside of the random points
        List <MyVector2> allPoints = new List <MyVector2>();

        //allPoints.AddRange(randomPoints_2d);

        allPoints.AddRange(hullPoints_2d);

        foreach (List <MyVector2> hole in allHolePoints_2d)
        {
            allPoints.AddRange(hole);
        }

        AABB2 normalizingBox = new AABB2(allPoints);

        float dMax = HelpMethods.CalculateDMax(normalizingBox);

        List <MyVector2> hullPoints_2d_normalized = HelpMethods.Normalize(hullPoints_2d, normalizingBox, dMax);

        HashSet <List <MyVector2> > allHolePoints_2d_normalized = new HashSet <List <MyVector2> >();

        foreach (List <MyVector2> hole in allHolePoints_2d)
        {
            List <MyVector2> hole_normalized = HelpMethods.Normalize(hole, normalizingBox, dMax);

            allHolePoints_2d_normalized.Add(hole_normalized);
        }



        //
        // Generate the triangulation
        //

        //Algorithm 1. Delaunay by triangulate all points with some bad algorithm and then flip edges until we get a delaunay triangulation
        //HalfEdgeData2 triangleData_normalized = _Delaunay.FlippingEdges(points_2d_normalized, new HalfEdgeData2());


        //Algorithm 2. Delaunay by inserting point-by-point while flipping edges after inserting a single point
        //HalfEdgeData2 triangleData_normalized = _Delaunay.PointByPoint(points_2d_normalized, new HalfEdgeData2());


        //Algorithm 3. Constrained delaunay
        HalfEdgeData2 triangleData_normalized = _Delaunay.ConstrainedBySloan(null, hullPoints_2d_normalized, allHolePoints_2d_normalized, shouldRemoveTriangles: true, new HalfEdgeData2());



        //UnNormalize
        HalfEdgeData2 triangleData = HelpMethods.UnNormalize(triangleData_normalized, normalizingBox, dMax);

        //From half-edge to triangle
        HashSet <Triangle2> triangles_2d = _TransformBetweenDataStructures.HalfEdge2ToTriangle2(triangleData);

        //From triangulation to mesh

        //Make sure the triangles have the correct orientation
        triangles_2d = HelpMethods.OrientTrianglesClockwise(triangles_2d);

        //From 2d to 3d
        HashSet <Triangle3> triangles_3d = new HashSet <Triangle3>();

        foreach (Triangle2 t in triangles_2d)
        {
            triangles_3d.Add(new Triangle3(t.p1.ToMyVector3_Yis3D(), t.p2.ToMyVector3_Yis3D(), t.p3.ToMyVector3_Yis3D()));
        }

        triangulatedMesh = _TransformBetweenDataStructures.Triangle3ToCompressedMesh(triangles_3d);
    }
Exemple #12
0
    public void GenerateTriangulation()
    {
        List <Vector3> hullVertices = GetPointsFromParent(hullParent);

        if (hullVertices == null)
        {
            Debug.Log("We have no points on the hull");

            return;
        }

        //Ear Clipping is a 2d algorithm so convert
        List <MyVector2> hullVertices_2d = hullVertices.Select(p => new MyVector2(p.x, p.z)).ToList();


        //Holes
        List <List <MyVector2> > allHoleVertices_2d = new List <List <MyVector2> >();

        foreach (Transform holeParentTrans in holeParents)
        {
            List <Vector3> holeVertices = GetPointsFromParent(holeParentTrans);

            List <MyVector2> holeVertices_2d = null;

            if (holeVertices != null)
            {
                holeVertices_2d = holeVertices.Select(p => new MyVector2(p.x, p.z)).ToList();

                allHoleVertices_2d.Add(holeVertices_2d);
            }
            else
            {
                Debug.Log("A hole has no points");
            }
        }



        //Normalize to range 0-1
        //The holes are always inside this shape, so dont need to take them into account when calculating the normalization values
        AABB2 normalizingBox = new AABB2(new List <MyVector2>(hullVertices_2d));

        float dMax = HelpMethods.CalculateDMax(normalizingBox);

        List <MyVector2> hullVertices_2d_normalized = HelpMethods.Normalize(hullVertices_2d, normalizingBox, dMax);

        //Normalize the holes
        List <List <MyVector2> > allHoleVertices_2d_normalized = new List <List <MyVector2> >();

        foreach (List <MyVector2> holeVertices_2d in allHoleVertices_2d)
        {
            List <MyVector2> holeVertices_2d_normalized = HelpMethods.Normalize(holeVertices_2d, normalizingBox, dMax);

            allHoleVertices_2d_normalized.Add(holeVertices_2d_normalized);
        }


        //Debug.Log(hullVertices_2d_normalized.Count);

        //Triangulate
        triangulation = _EarClipping.Triangulate(hullVertices_2d, allHoleVertices_2d, optimizeTriangles: true);
        //HashSet<Triangle2> triangulation_normalized = EarClipping.Triangulate(hullVertices_2d_normalized, allHoleVertices_2d_normalized);

        //Debug.Log($"Number of triangles from ear clipping: {triangulation_normalized.Count}");


        //Unnormalize
        //triangulation = HelpMethods.UnNormalize(triangulation_normalized, normalizingBox, dMax);
    }