Beispiel #1
0
        /// <summary>
        /// Determines if a Vertex is inside the gPolygon using Fast Winding Number method
        /// </summary>
        /// <param name="vertex"></param>
        /// <returns></returns>
        public bool ContainsVertex(Vertex vertex)
        {
            // http://geomalgorithms.com/a03-_inclusion.html
            Vertex maxVertex   = vertices.OrderByDescending(v => v.DistanceTo(vertex)).First();
            double maxDistance = vertex.DistanceTo(maxVertex) * 1.5;
            Vertex v2          = Vertex.ByCoordinates(vertex.X + maxDistance, vertex.Y, vertex.Z);
            Edge   ray         = Edge.ByStartVertexEndVertex(vertex, v2);
            int    windNumber  = 0;

            foreach (Edge edge in edges)
            {
                if (vertex.OnEdge(edge))
                {
                    return(true);
                }
                Vertex intersection = ray.Intersection(edge) as Vertex;
                if (intersection is Vertex)
                {
                    if (edge.StartVertex.Y <= vertex.Y)
                    {
                        if (edge.EndVertex.Y > vertex.Y)
                        {
                            if (IsLeft(edge, vertex) > 0)
                            {
                                ++windNumber;
                            }
                        }
                    }
                    else
                    {
                        if (edge.EndVertex.Y < vertex.Y)
                        {
                            if (IsLeft(edge, vertex) < 0)
                            {
                                --windNumber;
                            }
                        }
                    }
                }
            }

            // If windNumber is different from 0, vertex is in polygon
            return(windNumber != 0);
        }
Beispiel #2
0
        public GeometryBase Intersection(Edge other)
        {
            // http://mathworld.wolfram.com/Line-LineIntersection.html
            if (!this.BoundingBox.Intersects(other.BoundingBox))
            {
                return(null);
            }
            if (!this.IsCoplanarTo(other))
            {
                return(null);
            }
            if (this.Equals(other))
            {
                return(this);
            }                                        // Issues if same polygon id???

            var a = this.Direction;
            var b = other.Direction;

            if (a.IsParallelTo(b))
            {
                // Fully contains the test edge
                if (other.StartVertex.OnEdge(this) && other.EndVertex.OnEdge(this))
                {
                    return(other);
                }
                // Is fully contained by test edge
                else if (this.StartVertex.OnEdge(other) && this.EndVertex.OnEdge(other))
                {
                    return(this);
                }
                // Not fully inclusive but overlapping
                else if (this.StartVertex.OnEdge(other) || this.EndVertex.OnEdge(other))
                {
                    Vertex[] vertices = new Vertex[4]
                    {
                        this.StartVertex,
                        this.EndVertex,
                        other.StartVertex,
                        other.EndVertex
                    };
                    var sorted = vertices.OrderBy(v => v.Y).ThenBy(v => v.X).ThenBy(v => v.Z).ToList();
                    return(Edge.ByStartVertexEndVertex(sorted[1], sorted[2]));
                }
                // Not intersecting
                else
                {
                    return(null);
                }
            }

            // No parallels but intersecting on one of the extreme vertices
            if (other.Contains(this.StartVertex))
            {
                return(this.StartVertex);
            }
            else if (other.Contains(this.EndVertex))
            {
                return(this.EndVertex);
            }


            // No coincident nor same extremes
            var c   = Vector.ByTwoVertices(this.StartVertex, other.StartVertex);
            var cxb = c.Cross(b);
            var axb = a.Cross(b);
            var dot = cxb.Dot(axb);

            // If dot == 0 it means that other edge contains at least a vertex from this edge
            // and they are parallel or perpendicular. Cannot be parallel as that was tested before.
            // It might also mean they don't intersect but the would if extending the projections
            double s = (dot) / Math.Pow(axb.Length, 2);

            if (s.AlmostEqualTo(0))
            {
                if (this.StartVertex.OnEdge(other))
                {
                    return(this.StartVertex);
                }
                else if (this.EndVertex.OnEdge(other))
                {
                    return(this.EndVertex);
                }
                else if (other.StartVertex.OnEdge(this))
                {
                    return(other.StartVertex);
                }
                else if (other.EndVertex.OnEdge(this))
                {
                    return(other.EndVertex);
                }
                else
                {
                    return(null);
                }
            }



            // s > 1, means that "intersection" vertex is not on either edge
            // s == NaN means they are parallels so never intersect
            if (s < 0 || s > 1 || Double.IsNaN(s))
            {
                return(null);
            }

            Vertex intersection = this.StartVertex.Translate(a.Scale(s));

            if (intersection.Equals(other.StartVertex))
            {
                return(other.StartVertex);
            }
            if (intersection.Equals(other.EndVertex))
            {
                return(other.EndVertex);
            }
            if (!intersection.OnEdge(other))
            {
                return(null);
            }

            return(intersection);
        }