Пример #1
0
        void DrawOutput()
        {
            if (_g == null)
            {
                return;
            }

            //-----------
            //for GDI+ only
            bool flipYAxis  = chkFlipY.Checked;
            int  viewHeight = this.panel1.Height;

            //-----------
            //show tess
            _g.Clear(Color.White);
            int[]   contourEndIndices;
            float[] polygon1 = GetPolygonData(out contourEndIndices);
            if (polygon1 == null)
            {
                return;
            }
            //
            if (flipYAxis)
            {
                var transformMat = new System.Drawing.Drawing2D.Matrix();
                transformMat.Scale(1, -1);
                transformMat.Translate(0, -viewHeight);
                polygon1 = TransformPoints(polygon1, transformMat);

                //when we flipY, meaning of clockwise-counter clockwise is changed.
                //
                //see https://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-clockwise-order
                //...(comment)
                //...A minor caveat: this answer assumes a normal Cartesian coordinate system.
                //The reason that's worth mentioning is that some common contexts, like HTML5 canvas, use an inverted Y-axis.
                //Then the rule has to be flipped: if the area is negative, the curve is clockwise. – LarsH Oct 11 '13 at 20:49
            }


            using (Pen pen1 = new Pen(Color.LightGray, 6))
            {
                int    nn = polygon1.Length;
                int    a  = 0;
                PointF p0;
                PointF p1;

                int contourCount = contourEndIndices.Length;
                int startAt      = 3;
                for (int cnt_index = 0; cnt_index < contourCount; ++cnt_index)
                {
                    int endAt = contourEndIndices[cnt_index];
                    for (int m = startAt; m <= endAt;)
                    {
                        p0 = new PointF(polygon1[m - 3], polygon1[m - 2]);
                        p1 = new PointF(polygon1[m - 1], polygon1[m]);
                        _g.DrawLine(pen1, p0, p1);
                        _g.DrawString(a.ToString(), this.Font, Brushes.Black, p0);
                        m += 2;
                        a++;
                    }
                    //close contour

                    p0 = new PointF(polygon1[endAt - 1], polygon1[endAt]);
                    p1 = new PointF(polygon1[startAt - 3], polygon1[startAt - 2]);
                    _g.DrawLine(pen1, p0, p1);
                    _g.DrawString(a.ToString(), this.Font, Brushes.Black, p0);
                    //
                    startAt = (endAt + 1) + 3;
                }
            }
            //----------------------------------------------------------------------------

            //tess
            if (rdoTessSGI.Checked)
            {
                //SGI Tess Lib

                if (!_tessTool.TessPolygon(polygon1, _contourEnds))
                {
                    return;
                }

                //1.
                List <ushort> indexList = _tessTool.TessIndexList;
                //2.
                List <TessVertex2d> tempVertexList = _tessTool.TempVertexList;
                //3.
                int vertexCount = indexList.Count;
                //-----------------------------
                int     orgVertexCount = polygon1.Length / 2;
                float[] vtx            = new float[vertexCount * 2];//***
                int     n = 0;

                for (int p = 0; p < vertexCount; ++p)
                {
                    ushort index = indexList[p];
                    if (index >= orgVertexCount)
                    {
                        //extra coord (newly created)
                        TessVertex2d extraVertex = tempVertexList[index - orgVertexCount];
                        vtx[n]     = (float)extraVertex.x;
                        vtx[n + 1] = (float)extraVertex.y;
                    }
                    else
                    {
                        //original corrd
                        vtx[n]     = (float)polygon1[index * 2];
                        vtx[n + 1] = (float)polygon1[(index * 2) + 1];
                    }
                    n += 2;
                }
                //-----------------------------
                //draw tess result
                int j = vtx.Length;
                for (int i = 0; i < j;)
                {
                    var p0 = new PointF(vtx[i], vtx[i + 1]);
                    var p1 = new PointF(vtx[i + 2], vtx[i + 3]);
                    var p2 = new PointF(vtx[i + 4], vtx[i + 5]);

                    _g.DrawLine(Pens.Red, p0, p1);
                    _g.DrawLine(Pens.Red, p1, p2);
                    _g.DrawLine(Pens.Red, p2, p0);

                    i += 6;
                }
            }
            else
            {
                List <Poly2Tri.Polygon> outputPolygons = new List <Poly2Tri.Polygon>();
                Poly2TriExampleHelper.Triangulate(polygon1, contourEndIndices, flipYAxis, outputPolygons);
                foreach (Poly2Tri.Polygon polygon in outputPolygons)
                {
                    foreach (Poly2Tri.DelaunayTriangle tri in polygon.Triangles)
                    {
                        Poly2Tri.TriangulationPoint p0 = tri.P0;
                        Poly2Tri.TriangulationPoint p1 = tri.P1;
                        Poly2Tri.TriangulationPoint p2 = tri.P2;

                        _g.DrawLine(Pens.Red, (float)p0.X, (float)p0.Y, (float)p1.X, (float)p1.Y);
                        _g.DrawLine(Pens.Red, (float)p1.X, (float)p1.Y, (float)p2.X, (float)p2.Y);
                        _g.DrawLine(Pens.Red, (float)p2.X, (float)p2.Y, (float)p0.X, (float)p0.Y);
                    }
                }
            }
        }
Пример #2
0
        void DrawOutput()
        {
            if (_g == null)
            {
                return;
            }

            //-----------
            //for GDI+ only
            bool drawInvert = chkInvert.Checked;
            int  viewHeight = this.panel1.Height;

            //-----------
            //show tess
            _g.Clear(Color.White);
            int[]   contourEndIndices;
            float[] polygon1 = GetPolygonData(out contourEndIndices);
            if (polygon1 == null)
            {
                return;
            }
            //
            if (drawInvert)
            {
                var transformMat = new System.Drawing.Drawing2D.Matrix();
                transformMat.Scale(1, -1);
                transformMat.Translate(0, -viewHeight);
                //
                polygon1 = TransformPoints(polygon1, transformMat);
            }


            using (Pen pen1 = new Pen(Color.LightGray, 6))
            {
                int    nn = polygon1.Length;
                int    a  = 0;
                PointF p0;
                PointF p1;

                int contourCount = contourEndIndices.Length;
                int startAt      = 3;
                for (int cnt_index = 0; cnt_index < contourCount; ++cnt_index)
                {
                    int endAt = contourEndIndices[cnt_index];
                    for (int m = startAt; m <= endAt;)
                    {
                        p0 = new PointF(polygon1[m - 3], polygon1[m - 2]);
                        p1 = new PointF(polygon1[m - 1], polygon1[m]);
                        _g.DrawLine(pen1, p0, p1);
                        _g.DrawString(a.ToString(), this.Font, Brushes.Black, p0);
                        m += 2;
                        a++;
                    }
                    //close contour

                    p0 = new PointF(polygon1[endAt - 1], polygon1[endAt]);
                    p1 = new PointF(polygon1[startAt - 3], polygon1[startAt - 2]);
                    _g.DrawLine(pen1, p0, p1);
                    _g.DrawString(a.ToString(), this.Font, Brushes.Black, p0);
                    //
                    startAt = (endAt + 1) + 3;
                }
            }
            //----------------------------------------------------------------------------

            //tess
            if (rdoTessSGI.Checked)
            {
                //SGI Tess Lib

                if (!_tessTool.TessPolygon(polygon1, _contourEnds))
                {
                    return;
                }

                //1.
                List <ushort> indexList = _tessTool.TessIndexList;
                //2.
                List <TessVertex2d> tempVertexList = _tessTool.TempVertexList;
                //3.
                int vertexCount = indexList.Count;
                //-----------------------------
                int     orgVertexCount = polygon1.Length / 2;
                float[] vtx            = new float[vertexCount * 2];//***
                int     n = 0;

                for (int p = 0; p < vertexCount; ++p)
                {
                    ushort index = indexList[p];
                    if (index >= orgVertexCount)
                    {
                        //extra coord (newly created)
                        TessVertex2d extraVertex = tempVertexList[index - orgVertexCount];
                        vtx[n]     = (float)extraVertex.x;
                        vtx[n + 1] = (float)extraVertex.y;
                    }
                    else
                    {
                        //original corrd
                        vtx[n]     = (float)polygon1[index * 2];
                        vtx[n + 1] = (float)polygon1[(index * 2) + 1];
                    }
                    n += 2;
                }
                //-----------------------------
                //draw tess result
                int j = vtx.Length;
                for (int i = 0; i < j;)
                {
                    var p0 = new PointF(vtx[i], vtx[i + 1]);
                    var p1 = new PointF(vtx[i + 2], vtx[i + 3]);
                    var p2 = new PointF(vtx[i + 4], vtx[i + 5]);

                    _g.DrawLine(Pens.Red, p0, p1);
                    _g.DrawLine(Pens.Red, p1, p2);
                    _g.DrawLine(Pens.Red, p2, p0);

                    i += 6;
                }
            }
            else
            {
                Poly2Tri.Polygon mainPolygon = Poly2TriExampleHelper.Triangulate(polygon1, contourEndIndices);
                foreach (Poly2Tri.DelaunayTriangle tri in mainPolygon.Triangles)
                {
                    Poly2Tri.TriangulationPoint p0 = tri.P0;
                    Poly2Tri.TriangulationPoint p1 = tri.P1;
                    Poly2Tri.TriangulationPoint p2 = tri.P2;

                    _g.DrawLine(Pens.Red, (float)p0.X, (float)p0.Y, (float)p1.X, (float)p1.Y);
                    _g.DrawLine(Pens.Red, (float)p1.X, (float)p1.Y, (float)p2.X, (float)p2.Y);
                    _g.DrawLine(Pens.Red, (float)p2.X, (float)p2.Y, (float)p0.X, (float)p0.Y);
                }
            }
        }