public static List <Polygon> Tesselate(Polygon footprint, double height)
        {
            var points = footprint.ExteriorRing.Points;

            var data        = new List <double>();
            var holeIndices = new List <int>();

            foreach (var p in points)
            {
                data.Add((double)p.X);
                data.Add((double)p.Y);
            }

            var trianglesIndices = Earcut.Tessellate(data, holeIndices);


            var polygons = new List <Polygon>();

            for (var i = 0; i < trianglesIndices.Count / 3; i++)
            {
                var t = new Polygon();

                var a = trianglesIndices[i * 3];
                var b = trianglesIndices[i * 3 + 1];
                var c = trianglesIndices[i * 3 + 2];

                t.ExteriorRing.Points.Add(GetPoint(data, a, height));
                t.ExteriorRing.Points.Add(GetPoint(data, b, height));
                t.ExteriorRing.Points.Add(GetPoint(data, c, height));
                t.ExteriorRing.Points.Add(GetPoint(data, a, height));

                polygons.Add(t);
            }
            return(polygons);
        }
Example #2
0
        public static void AreaTest(List <double> data, List <int> holeIndices, int expectedTriangles, double expectedDeviation)
        {
            var triangles = Earcut.Tessellate(data, holeIndices);

            if (expectedTriangles > 0)
            {
                Assert.AreEqual(triangles.Count / 3, expectedTriangles);
            }

            var actualDeviation = Earcut.Deviation(data, holeIndices, triangles);

            Assert.IsTrue(actualDeviation < expectedDeviation);
        }
Example #3
0
    public void Triangulate()
    {
        LightList <int> output = Earcut.Tessellate(new LightList <float>(new float[] {
            10, 0,
            0, 50,
            60, 60,
            70, 10
        }), null);

        Assert.AreEqual(output[0], 1);
        Assert.AreEqual(output[1], 0);
        Assert.AreEqual(output[2], 3);

        Assert.AreEqual(output[3], 3);
        Assert.AreEqual(output[4], 2);
        Assert.AreEqual(output[5], 1);
    }
Example #4
0
        private static List <int> Tesselate(Polygon footprint)
        {
            var points = footprint.ExteriorRing.Points;

            var data        = new List <double>();
            var holeIndices = new List <int>();

            for (var p = 0; p < points.Count - 1; p++)
            {
                data.Add((double)points[p].X);
                data.Add((double)points[p].Y);
            }

            var trianglesIndices = Earcut.Tessellate(data, holeIndices);

            return(trianglesIndices);
        }
Example #5
0
        void Build()
        {
            if (!_mesh)
            {
                _mesh           = new Mesh();
                _mesh.hideFlags = HideFlags.HideAndDontSave;
            }

            bool hasStroke = _strokeThickness > 0;

            int vertexCount = _points.Length * (hasStroke ? 3 : 1);

            if (_vertices == null || _vertices.Length != vertexCount)
            {
                _vertices = new Vector3[vertexCount];
            }

            /*
             * if( hasStroke ) {
             *
             *
             *
             * if( _strokeAdjustedPoints == null || _strokeAdjustedPoints.Length != _points.Length ) {
             *              _strokeAdjustedPoints = new Vector2[ _points.Length ];
             *      }
             *      if( _uv0 )
             *
             *      DrawMath.ComputeNormalizedDirections( _points, ref _directions );
             *
             *      float halfStrokeThickness = _strokeThickness * 0.5f;
             *      int lastIndex = pointCount - 1;
             *      int i = 0, v = 0;
             *      Vector2 prev = Vector2.zero;
             *      Vector2 prevOffset = Vector2.zero;
             *      Vector2 nextPoint = Vector2.zero;
             *      for( int p = 0; p < pointCount; p++ ) {
             *              int v0 = v;
             *              Vector2 point = _points[ p ];
             *              Vector2 dir = _directions[ p ];
             *              Vector2 offset = new Vector2( -dir.y * halfStrokeThickness, dir.x * halfStrokeThickness ); // Rotate 45 degrees and scale
             *              if( p < lastIndex ) nextPoint = _points[ p + 1 ];
             *
             *              if( p == 0 || p == lastIndex ) {
             *                      // Two verts for begin and end.
             *                      _vertices[ v++ ] = point + offset;
             *                      _vertices[ v++ ] = point - offset;
             *              } else {
             *                      // Four verts for inner points.
             *                      Vector2 intersection;
             *                      if( DrawMath.TryIntersectLineLine( prev + prevOffset, point + prevOffset, point + offset, nextPoint + offset, out intersection ) ) {
             *                              _vertices[ v++ ] = intersection;
             *                      } else {
             *                              _vertices[ v++ ] = point + offset;
             *                      }
             *                      if( DrawMath.TryIntersectLineLine( prev - prevOffset, point - prevOffset, point - offset, nextPoint - offset, out intersection ) ) {
             *                              _vertices[ v++ ] = intersection;
             *                      } else {
             *                              _vertices[ v++ ] = point - offset;
             *                      }
             *                      _vertices[ v++ ] = _vertices[ v - 3 ];
             *                      _vertices[ v++ ] = _vertices[ v - 3 ];
             *                      v0 += 2;
             *              }
             *
             *              if( p < lastIndex ) {
             *                      Vector4 points;
             *                      if( p == 0 ) {
             *                              points = new Vector4( point.x + dir.x * halfStrokeThickness, point.y + dir.y * halfStrokeThickness, nextPoint.x, nextPoint.y );
             *                      } else if( p == lastIndex - 1 ) {
             *                              points = new Vector4( point.x, point.y, nextPoint.x - dir.x * halfStrokeThickness, nextPoint.y - dir.y * halfStrokeThickness );
             *                      } else {
             *                              points = new Vector4( point.x, point.y, nextPoint.x, nextPoint.y );
             *                      }
             *                      for( int j = 0, vn = v0; j < 4; j++, vn++ ) {
             *                              _uv0[ vn ] = points;
             *                      }
             *                      _indices[ i++ ] = v0;
             *                      _indices[ i++ ] = v0 + 2;
             *                      _indices[ i++ ] = v0 + 3;
             *                      _indices[ i++ ] = v0 + 1;
             *              }
             *
             *              prev = point;
             *              prevOffset = offset;
             *      }
             * }
             */

            for (int p = 0; p < _points.Length; p++)
            {
                _vertices[p] = _points[p];
            }

            Earcut.Tessellate(_points, _points.Length, noHoles, ref _polygonIndices);


            _mesh.SetVertices(_vertices);
            _mesh.SetIndices(_polygonIndices, MeshTopology.Triangles, 0);

            _isDirty = false;
        }