/// <summary>
        /// Generate the shapes geometry.
        /// </summary>
        protected Geometry GenerateGeometry()
        {
            PathGeometry pathGeometry = new PathGeometry();

            if (Points.Count == 2 || Points.Count == 3)
            {
                // Make a straight line.
                PathFigure fig = new PathFigure();
                fig.IsClosed = false;
                fig.IsFilled = false;
                fig.StartPoint = Points[0];

                for (int i = 1; i < Points.Count; ++i)
                {
                    fig.Segments.Add(new LineSegment(Points[i], true));
                }

                pathGeometry.Figures.Add(fig);
            }
            else
            {
                PointCollection adjustedPoints = new PointCollection();
                adjustedPoints.Add(Points[0]);
                for (int i = 1; i < Points.Count; ++i)
                {
                    adjustedPoints.Add(Points[i]);
                }

                if (adjustedPoints.Count == 4)
                {
                    // Make a curved line.
                    PathFigure fig = new PathFigure();
                    fig.IsClosed = false;
                    fig.IsFilled = false;
                    fig.StartPoint = adjustedPoints[0];
                    fig.Segments.Add(new BezierSegment(adjustedPoints[1], adjustedPoints[2], adjustedPoints[3], true));

                    pathGeometry.Figures.Add(fig);
                }
                else if (adjustedPoints.Count >= 5)
                {
                    // Make a curved line.
                    PathFigure fig = new PathFigure();
                    fig.IsClosed = false;
                    fig.IsFilled = false;
                    fig.StartPoint = adjustedPoints[0];

                    adjustedPoints.RemoveAt(0);

                    while (adjustedPoints.Count > 3)
                    {
                        Point generatedPoint = adjustedPoints[1] + ((adjustedPoints[2] - adjustedPoints[1]) / 2);

                        fig.Segments.Add(new BezierSegment(adjustedPoints[0], adjustedPoints[1], generatedPoint, true));

                        adjustedPoints.RemoveAt(0);
                        adjustedPoints.RemoveAt(0);
                    }

                    if (adjustedPoints.Count == 2)
                    {
                        fig.Segments.Add(new BezierSegment(adjustedPoints[0], adjustedPoints[0], adjustedPoints[1], true));
                    }
                    else
                    {
                        Trace.Assert(adjustedPoints.Count == 2);

                        fig.Segments.Add(new BezierSegment(adjustedPoints[0], adjustedPoints[1], adjustedPoints[2], true));
                    }

                    pathGeometry.Figures.Add(fig);
                }
            }

            return pathGeometry;
        }
Exemple #2
0
 private PointCollection ClipPolygon(Polygon polygon, double Left, double Top, double Right, double Bottom)
 {
     Point point;
     Point point2;
     PointCollection clippedPolygon = new PointCollection();
     double xTop = 0.0;
     double yLeft = 0.0;
     double xBottom = 0.0;
     double yRight = 0.0;
     int num5 = 0;
     int num6 = -1;
     double num7 = 0.0;
     double num8 = 0.0;
     double num9 = 0.0;
     double num10 = 0.0;
     for (int i = 0; i < polygon.get_Points().get_Count(); i++)
     {
         point = polygon.get_Points().get_Item(i);
         if (point.get_X() < num7)
         {
             num7 = point.get_X();
         }
         if (point.get_X() > num8)
         {
             num8 = point.get_X();
         }
         if (point.get_Y() < num9)
         {
             num9 = point.get_Y();
         }
         if (point.get_Y() > num10)
         {
             num10 = point.get_Y();
         }
         if (((Left <= point.get_X()) && (point.get_X() <= Right)) && ((Top <= point.get_Y()) && (point.get_Y() <= Bottom)))
         {
             num6 = i;
             break;
         }
     }
     if (num6 == -1)
     {
         for (int k = 0; k < polygon.get_Points().get_Count(); k++)
         {
             point = polygon.get_Points().get_Item(k);
             point2 = (k == (polygon.get_Points().get_Count() - 1)) ? polygon.get_Points().get_Item(0) : polygon.get_Points().get_Item(k + 1);
             this.CalculateLineIntersection(point, point2, ref yLeft, ref xTop, ref yRight, ref xBottom, Left, Top, Right, Bottom);
             if (((((Top <= yLeft) && (yLeft <= Bottom)) && (((point.get_X() < Left) && (Left < point2.get_X())) || ((point2.get_X() < Left) && (Left < point.get_X())))) || (((Left <= xTop) && (xTop <= Right)) && (((point.get_Y() < Top) && (Top < point2.get_Y())) || ((point2.get_Y() < Top) && (Top < point.get_Y()))))) || ((((Top <= yRight) && (yRight <= Bottom)) && (((point.get_X() < Right) && (Right < point2.get_X())) || ((point2.get_X() < Right) && (Right < point.get_X())))) || (((Left <= xBottom) && (xBottom <= Right)) && (((point.get_Y() < Bottom) && (Bottom < point2.get_Y())) || ((point2.get_Y() < Bottom) && (Bottom < point.get_Y()))))))
             {
                 this.ClipLine(point, point2, yLeft, xTop, yRight, xBottom, Left, Top, Right, Bottom, ref clippedPolygon);
                 num5 = clippedPolygon.get_Count();
                 num6 = (k == (polygon.get_Points().get_Count() - 1)) ? 0 : (k + 1);
                 break;
             }
         }
     }
     if (num6 == -1)
     {
         if (((num7 <= Left) && (Right <= num8)) && ((num9 <= Top) && (Bottom <= num10)))
         {
             clippedPolygon.Add(new Point(Left, Top));
             clippedPolygon.Add(new Point(Right, Top));
             clippedPolygon.Add(new Point(Right, Bottom));
             clippedPolygon.Add(new Point(Left, Bottom));
         }
         return clippedPolygon;
     }
     for (int j = num6; j < (polygon.get_Points().get_Count() + num6); j++)
     {
         point = polygon.get_Points().get_Item((j < polygon.get_Points().get_Count()) ? j : (j - polygon.get_Points().get_Count()));
         point2 = ((j + 1) < polygon.get_Points().get_Count()) ? polygon.get_Points().get_Item(j + 1) : polygon.get_Points().get_Item((j + 1) - polygon.get_Points().get_Count());
         this.CalculateLineIntersection(point, point2, ref yLeft, ref xTop, ref yRight, ref xBottom, Left, Top, Right, Bottom);
         if (((Left <= point.get_X()) && (point.get_X() <= Right)) && ((Top <= point.get_Y()) && (point.get_Y() <= Bottom)))
         {
             if (((clippedPolygon.get_Count() == 0) || (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() != point.get_X())) || (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() != point.get_Y()))
             {
                 clippedPolygon.Add(point);
             }
             if (((point2.get_X() < Left) || (Right < point2.get_X())) || ((point2.get_Y() < Top) || (Bottom < point2.get_Y())))
             {
                 if (((Top <= yLeft) && (yLeft <= Bottom)) && ((point2.get_X() < Left) && (Left < point.get_X())))
                 {
                     clippedPolygon.Add(new Point(Left, yLeft));
                 }
                 else if (((Left <= xTop) && (xTop <= Right)) && ((point2.get_Y() < Top) && (Top < point.get_Y())))
                 {
                     clippedPolygon.Add(new Point(xTop, Top));
                 }
                 else if (((Top <= yRight) && (yRight <= Bottom)) && ((point.get_X() < Right) && (Right < point2.get_X())))
                 {
                     clippedPolygon.Add(new Point(Right, yRight));
                 }
                 else if (((Left <= xBottom) && (xBottom <= Right)) && ((point.get_Y() < Bottom) && (Bottom < point2.get_Y())))
                 {
                     clippedPolygon.Add(new Point(xBottom, Bottom));
                 }
             }
         }
         else
         {
             if (clippedPolygon.get_Count() > 0)
             {
                 if (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Left) && (point.get_X() < Left)) && (Left <= point2.get_X()))
                 {
                     if ((yLeft < Top) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() > Top))
                     {
                         clippedPolygon.Add(new Point(Left, Top));
                     }
                     else if ((yLeft > Bottom) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() < Bottom))
                     {
                         clippedPolygon.Add(new Point(Left, Bottom));
                     }
                 }
                 if (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Top) && (point.get_Y() < Top)) && (Top <= point2.get_Y()))
                 {
                     if ((xTop < Left) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() > Left))
                     {
                         clippedPolygon.Add(new Point(Left, Top));
                     }
                     else if ((xTop > Right) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() < Right))
                     {
                         clippedPolygon.Add(new Point(Right, Top));
                     }
                 }
                 if (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Right) && (point2.get_X() <= Right)) && (Right < point.get_X()))
                 {
                     if ((yRight < Top) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() > Top))
                     {
                         clippedPolygon.Add(new Point(Right, Top));
                     }
                     else if ((yRight > Bottom) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() < Bottom))
                     {
                         clippedPolygon.Add(new Point(Right, Bottom));
                     }
                 }
                 if (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Bottom) && (point2.get_Y() <= Bottom)) && (Bottom < point.get_Y()))
                 {
                     if ((xBottom > Right) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() < Right))
                     {
                         clippedPolygon.Add(new Point(Right, Bottom));
                     }
                     else if ((xBottom < Left) && (clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() > Left))
                     {
                         clippedPolygon.Add(new Point(Left, Bottom));
                     }
                 }
             }
             if (((Left <= point2.get_X()) && (point2.get_X() <= Right)) && ((Top <= point2.get_Y()) && (point2.get_Y() <= Bottom)))
             {
                 if (((Top <= yLeft) && (yLeft <= Bottom)) && ((point.get_X() < Left) && (Left < point2.get_X())))
                 {
                     if ((((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Top) && (point.get_Y() == Top)) && (point2.get_Y() == Top)) || (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Bottom) && (point.get_Y() == Bottom)) && (point2.get_Y() == Bottom)))
                     {
                         clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1);
                     }
                     else
                     {
                         clippedPolygon.Add(new Point(Left, yLeft));
                     }
                 }
                 else if (((Left <= xTop) && (xTop <= Right)) && ((point.get_Y() < Top) && (Top < point2.get_Y())))
                 {
                     if ((((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Left) && (point.get_X() == Left)) && (point2.get_X() == Left)) || (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Right) && (point.get_X() == Right)) && (point2.get_X() == Right)))
                     {
                         clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1);
                     }
                     else
                     {
                         clippedPolygon.Add(new Point(xTop, Top));
                     }
                 }
                 else if (((Top <= yRight) && (yRight <= Bottom)) && ((point2.get_X() < Right) && (Right < point.get_X())))
                 {
                     if ((((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Top) && (point.get_Y() == Top)) && (point2.get_Y() == Top)) || (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y() == Bottom) && (point.get_Y() == Bottom)) && (point2.get_Y() == Bottom)))
                     {
                         clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1);
                     }
                     else
                     {
                         clippedPolygon.Add(new Point(Right, yRight));
                     }
                 }
                 else if (((Left <= xBottom) && (xBottom <= Right)) && ((point2.get_Y() < Bottom) && (Bottom < point.get_Y())))
                 {
                     if ((((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Left) && (point.get_X() == Left)) && (point2.get_X() == Left)) || (((clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X() == Right) && (point.get_X() == Right)) && (point2.get_X() == Right)))
                     {
                         clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1);
                     }
                     else
                     {
                         clippedPolygon.Add(new Point(xBottom, Bottom));
                     }
                 }
             }
             else
             {
                 this.ClipLine(point, point2, yLeft, xTop, yRight, xBottom, Left, Top, Right, Bottom, ref clippedPolygon);
             }
         }
     }
     if (num5 > 0)
     {
         for (int m = 0; m < num5; m++)
         {
             clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1);
         }
         return clippedPolygon;
     }
     if ((clippedPolygon.get_Item(0).get_X() == clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_X()) && (clippedPolygon.get_Item(0).get_Y() == clippedPolygon.get_Item(clippedPolygon.get_Count() - 1).get_Y()))
     {
         clippedPolygon.RemoveAt(clippedPolygon.get_Count() - 1);
     }
     return clippedPolygon;
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="vertices"></param>
        /// <param name="normals"></param>
        /// <param name="indices"></param>
        /// <param name="textures"></param>
        protected void TriangleSubdivide(Point3DCollection vertices,
                                         Vector3DCollection normals,
                                         Int32Collection indices,
                                         PointCollection textures)
        {
            for (int i = 0; i < 3; i++)
            {
                verticesBase[2 - i] = vertices[vertices.Count - 1];
                normalsBase[2 - i] = normals[vertices.Count - 1];
                texturesBase[2 - i] = textures[vertices.Count - 1];

                vertices.RemoveAt(vertices.Count - 1);
                normals.RemoveAt(normals.Count - 1);
                indices.RemoveAt(indices.Count - 1);
                textures.RemoveAt(textures.Count - 1);
            }

            int indexStart = vertices.Count;

            for (int slice = 0; slice <= Slices; slice++)
            {
                double weight = (double)slice / Slices;

                Point3D vertex1 = Point3DWeight(verticesBase[0], verticesBase[1], weight);
                Point3D vertex2 = Point3DWeight(verticesBase[0], verticesBase[2], weight);

                Vector3D normal1 = Vector3DWeight(normalsBase[0], normalsBase[1], weight);
                Vector3D normal2 = Vector3DWeight(normalsBase[0], normalsBase[2], weight);

                Point texture1 = PointWeight(texturesBase[0], texturesBase[1], weight);
                Point texture2 = PointWeight(texturesBase[0], texturesBase[2], weight);

                for (int i = 0; i <= slice; i++)
                {
                    weight = (double)i / slice;

                    if (Double.IsNaN(weight))
                        weight = 0;

                    vertices.Add(Point3DWeight(vertex1, vertex2, weight));
                    normals.Add(Vector3DWeight(normal1, normal2, weight));
                    textures.Add(PointWeight(texture1, texture2, weight));
                }
            }

            for (int slice = 0; slice < Slices; slice++)
            {
                int base1 = (slice + 1) * slice / 2;
                int base2 = base1 + slice + 1;

                for (int i = 0; i <= 2 * slice; i++)
                {
                    int half = i / 2;

                    if ((i & 1) == 0)         // even
                    {
                        indices.Add(indexStart + base1 + half);
                        indices.Add(indexStart + base2 + half);
                        indices.Add(indexStart + base2 + half + 1);
                    }
                    else                    // odd
                    {
                        indices.Add(indexStart + base1 + half);
                        indices.Add(indexStart + base2 + half + 1);
                        indices.Add(indexStart + base1 + half + 1);
                    }
                }
            }
        }
Exemple #4
0
 private static Geometry CreatePolyLineGeometry(PointCollection points)
 {
     Point point = points[0];
     points.RemoveAt(0);
     PathGeometry pathGeometry = new PathGeometry();
     PathFigure pathFigure = new PathFigure();
     PolyLineSegment polyLineSegment = new PolyLineSegment();
     pathFigure.IsClosed = true;
     pathFigure.StartPoint = point;
     polyLineSegment.Points = points;
     pathFigure.Segments.Add((PathSegment)polyLineSegment);
     pathGeometry.Figures.Add(pathFigure);
     return (Geometry)pathGeometry;
 }