2 * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifndef FloatPolygon_h
31 #define FloatPolygon_h
33 #include "FloatPoint.h"
34 #include "FloatRect.h"
35 #include "PODIntervalTree.h"
38 #include <wtf/Vector.h>
42 class FloatPolygonEdge;
46 FloatPolygon(std::unique_ptr<Vector<FloatPoint>> vertices, WindRule fillRule);
48 const FloatPoint& vertexAt(unsigned index) const { return (*m_vertices)[index]; }
49 unsigned numberOfVertices() const { return m_vertices->size(); }
51 WindRule fillRule() const { return m_fillRule; }
53 const FloatPolygonEdge& edgeAt(unsigned index) const { return m_edges[index]; }
54 unsigned numberOfEdges() const { return m_edges.size(); }
56 FloatRect boundingBox() const { return m_boundingBox; }
57 bool overlappingEdges(float minY, float maxY, Vector<const FloatPolygonEdge*>& result) const;
58 bool contains(const FloatPoint&) const;
59 bool isEmpty() const { return m_empty; }
62 typedef PODInterval<float, FloatPolygonEdge*> EdgeInterval;
63 typedef PODIntervalTree<float, FloatPolygonEdge*> EdgeIntervalTree;
65 bool containsNonZero(const FloatPoint&) const;
66 bool containsEvenOdd(const FloatPoint&) const;
68 std::unique_ptr<Vector<FloatPoint>> m_vertices;
70 FloatRect m_boundingBox;
72 Vector<FloatPolygonEdge> m_edges;
73 EdgeIntervalTree m_edgeTree; // Each EdgeIntervalTree node stores minY, maxY, and a ("UserData") pointer to a FloatPolygonEdge.
79 virtual ~VertexPair() { }
81 virtual const FloatPoint& vertex1() const = 0;
82 virtual const FloatPoint& vertex2() const = 0;
84 float minX() const { return std::min(vertex1().x(), vertex2().x()); }
85 float minY() const { return std::min(vertex1().y(), vertex2().y()); }
86 float maxX() const { return std::max(vertex1().x(), vertex2().x()); }
87 float maxY() const { return std::max(vertex1().y(), vertex2().y()); }
89 bool overlapsRect(const FloatRect&) const;
90 bool intersection(const VertexPair&, FloatPoint&) const;
93 class FloatPolygonEdge : public VertexPair {
94 friend class FloatPolygon;
96 const FloatPoint& vertex1() const override
99 return m_polygon->vertexAt(m_vertexIndex1);
102 const FloatPoint& vertex2() const override
105 return m_polygon->vertexAt(m_vertexIndex2);
108 const FloatPolygonEdge& previousEdge() const
110 ASSERT(m_polygon && m_polygon->numberOfEdges() > 1);
111 return m_polygon->edgeAt((m_edgeIndex + m_polygon->numberOfEdges() - 1) % m_polygon->numberOfEdges());
114 const FloatPolygonEdge& nextEdge() const
116 ASSERT(m_polygon && m_polygon->numberOfEdges() > 1);
117 return m_polygon->edgeAt((m_edgeIndex + 1) % m_polygon->numberOfEdges());
120 const FloatPolygon* polygon() const { return m_polygon; }
121 unsigned vertexIndex1() const { return m_vertexIndex1; }
122 unsigned vertexIndex2() const { return m_vertexIndex2; }
123 unsigned edgeIndex() const { return m_edgeIndex; }
126 // Edge vertex index1 is less than index2, except the last edge, where index2 is 0. When a polygon edge
127 // is defined by 3 or more colinear vertices, index2 can be the the index of the last colinear vertex.
128 unsigned m_vertexIndex1;
129 unsigned m_vertexIndex2;
130 unsigned m_edgeIndex;
131 const FloatPolygon* m_polygon;
134 // This structure is used by PODIntervalTree for debugging.
136 template<> struct ValueToString<FloatPolygonEdge*> {
137 static String string(const FloatPolygonEdge* edge) { return String::format("%p (%f,%f %f,%f)", edge, edge->vertex1().x(), edge->vertex1().y(), edge->vertex2().x(), edge->vertex2().y()); }
141 } // namespace WebCore
143 #endif // FloatPolygon_h