Ejemplo n.º 1
0
 public void AsText(StringBuilder b, int pad)
 {
     b.Append(' ', pad);
     b.AppendLine("SceneCachedValues:");
     b.Append(' ', pad++);
     b.AppendLine("{");
     b.Append(' ', pad);
     b.AppendLine("Unknown1: 0x" + Unknown1.ToString("X8") + " (" + Unknown1 + ")");
     b.Append(' ', pad);
     b.AppendLine("Unknown2: 0x" + Unknown2.ToString("X8") + " (" + Unknown2 + ")");
     b.Append(' ', pad);
     b.AppendLine("Unknown3: 0x" + Unknown3.ToString("X8") + " (" + Unknown3 + ")");
     AABB1.AsText(b, pad);
     AABB2.AsText(b, pad);
     b.Append(' ', pad);
     b.AppendLine("Unknown4:");
     b.Append(' ', pad);
     b.AppendLine("{");
     for (int i = 0; i < Unknown4.Length;)
     {
         b.Append(' ', pad + 1);
         for (int j = 0; j < 8 && i < Unknown4.Length; j++, i++)
         {
             b.Append("0x" + Unknown4[i].ToString("X8") + ", ");
         }
         b.AppendLine();
     }
     b.Append(' ', pad);
     b.AppendLine("}");
     b.AppendLine();
     b.Append(' ', pad);
     b.AppendLine("Unknown5: 0x" + Unknown5.ToString("X8") + " (" + Unknown5 + ")");
     b.Append(' ', --pad);
     b.AppendLine("}");
 }
Ejemplo n.º 2
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);
    }
Ejemplo n.º 3
0
 /// <summary>
 /// Encodes SceneCachedValues to given GameBitBuffer.
 /// </summary>
 /// <param name="buffer">The GameBitBuffer to write.</param>
 public void Encode(GameBitBuffer buffer)
 {
     buffer.WriteInt(32, Unknown1);
     buffer.WriteInt(32, Unknown2);
     buffer.WriteInt(32, Unknown3);
     AABB1.Encode(buffer);
     AABB2.Encode(buffer);
     for (int i = 0; i < Unknown4.Length; i++)
     {
         buffer.WriteInt(32, Unknown4[i]);
     }
     buffer.WriteInt(32, Unknown5);
 }
Ejemplo n.º 4
0
    //Are two AABB intersecting?
    private void AABB_AABB()
    {
        MyVector2 t1_p1 = t1_p1_trans.position.ToMyVector2();
        MyVector2 t1_p2 = t1_p2_trans.position.ToMyVector2();
        MyVector2 t1_p3 = t1_p3_trans.position.ToMyVector2();

        MyVector2 t2_p1 = t2_p1_trans.position.ToMyVector2();
        MyVector2 t2_p2 = t2_p2_trans.position.ToMyVector2();
        MyVector2 t2_p3 = t2_p3_trans.position.ToMyVector2();

        AABB2 r1 = new AABB2(new List <MyVector2>()
        {
            t1_p1, t1_p2, t1_p3
        });
        AABB2 r2 = new AABB2(new List <MyVector2>()
        {
            t2_p1, t2_p2, t2_p3
        });

        bool isIntersecting = _Intersections.AABB_AABB_2D(r1, r2);

        Debug.Log("AABB intersecting: " + isIntersecting);

        //Display the rectangles and the vertices we use to make the rectangles
        Vector3 r1_size = new Vector3(r1.maxX - r1.minX, 0.01f, r1.maxY - r1.minY);
        Vector3 r2_size = new Vector3(r2.maxX - r2.minX, 0.01f, r2.maxY - r2.minY);

        Vector3 r1_center = new Vector3(r1.minX + (r1_size.x * 0.5f), 0f, r1.minY + (r1_size.z * 0.5f));
        Vector3 r2_center = new Vector3(r2.minX + (r2_size.x * 0.5f), 0f, r2.minY + (r2_size.z * 0.5f));

        Gizmos.color = Color.white;

        Gizmos.DrawCube(r1_center, r1_size);

        //float r = 0.1f;

        //Gizmos.DrawWireSphere(t1_p1.ToVector3(), r);
        //Gizmos.DrawWireSphere(t1_p2.ToVector3(), r);
        //Gizmos.DrawWireSphere(t1_p3.ToVector3(), r);

        Gizmos.color = isIntersecting ? Color.red : Color.white;


        Gizmos.DrawCube(r2_center, r2_size);

        //Gizmos.DrawWireSphere(t2_p1.ToVector3(), r);
        //Gizmos.DrawWireSphere(t2_p2.ToVector3(), r);
        //Gizmos.DrawWireSphere(t2_p3.ToVector3(), r);
    }
    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);
        }
    }
Ejemplo n.º 6
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);
    }
Ejemplo n.º 7
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);
    }
Ejemplo n.º 9
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);
    }
Ejemplo n.º 10
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);
        }
    }
Ejemplo n.º 11
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);
        }
    }
Ejemplo n.º 12
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);
    }
Ejemplo n.º 13
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);
    }
Ejemplo n.º 14
0
    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 = new AABB2(new List <MyVector2>(points_2d));

        dMax = HelpMethods.CalculateDMax(normalizingBox);

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


        //Visualization 1. Delaunay flip edges
        //DelaunayFlipEdgesVisual flipEdges = GetComponent<DelaunayFlipEdgesVisual>();

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


        //Visualization 2. Delaunay point-by-point
        //DelaunayPointByPointVisual pointByPoint = GetComponent<DelaunayPointByPointVisual>();

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


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

        //if (visibleEdge)
        //{
        //    visibleEdge.StartVisualization(points_2d_normalized);
        //}


        //Visualization 4. Gift wrapping
        GiftWrappingVisualizer giftWrapping = GetComponent <GiftWrappingVisualizer>();

        if (giftWrapping)
        {
            giftWrapping.InitVisualization(points_2d_normalized);
        }
    }