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 "platform/PODIntervalTree.h"
34 #include "platform/geometry/FloatPoint.h"
35 #include "platform/geometry/FloatRect.h"
36 #include "platform/graphics/GraphicsTypes.h"
37 #include "wtf/OwnPtr.h"
38 #include "wtf/PassOwnPtr.h"
39 #include "wtf/Vector.h"
43 class FloatPolygonEdge;
45 // This class is used by PODIntervalTree for debugging.
47 template <class> struct ValueToString;
50 class PLATFORM_EXPORT FloatPolygon {
52 FloatPolygon(PassOwnPtr<Vector<FloatPoint> > vertices, WindRule fillRule);
54 const FloatPoint& vertexAt(unsigned index) const { return (*m_vertices)[index]; }
55 unsigned numberOfVertices() const { return m_vertices->size(); }
57 WindRule fillRule() const { return m_fillRule; }
59 const FloatPolygonEdge& edgeAt(unsigned index) const { return m_edges[index]; }
60 unsigned numberOfEdges() const { return m_edges.size(); }
62 FloatRect boundingBox() const { return m_boundingBox; }
63 bool overlappingEdges(float minY, float maxY, Vector<const FloatPolygonEdge*>& result) const;
64 bool contains(const FloatPoint&) const;
65 bool isEmpty() const { return m_empty; }
68 typedef PODInterval<float, FloatPolygonEdge*> EdgeInterval;
69 typedef PODIntervalTree<float, FloatPolygonEdge*> EdgeIntervalTree;
71 bool containsNonZero(const FloatPoint&) const;
72 bool containsEvenOdd(const FloatPoint&) const;
74 OwnPtr<Vector<FloatPoint> > m_vertices;
76 FloatRect m_boundingBox;
78 Vector<FloatPolygonEdge> m_edges;
79 EdgeIntervalTree m_edgeTree; // Each EdgeIntervalTree node stores minY, maxY, and a ("UserData") pointer to a FloatPolygonEdge.
83 class PLATFORM_EXPORT VertexPair {
85 virtual ~VertexPair() { }
87 virtual const FloatPoint& vertex1() const = 0;
88 virtual const FloatPoint& vertex2() const = 0;
90 float minX() const { return std::min(vertex1().x(), vertex2().x()); }
91 float minY() const { return std::min(vertex1().y(), vertex2().y()); }
92 float maxX() const { return std::max(vertex1().x(), vertex2().x()); }
93 float maxY() const { return std::max(vertex1().y(), vertex2().y()); }
95 bool intersection(const VertexPair&, FloatPoint&) const;
98 class PLATFORM_EXPORT FloatPolygonEdge : public VertexPair {
99 friend class FloatPolygon;
101 virtual const FloatPoint& vertex1() const override
104 return m_polygon->vertexAt(m_vertexIndex1);
107 virtual const FloatPoint& vertex2() const override
110 return m_polygon->vertexAt(m_vertexIndex2);
113 const FloatPolygonEdge& previousEdge() const
115 ASSERT(m_polygon && m_polygon->numberOfEdges() > 1);
116 return m_polygon->edgeAt((m_edgeIndex + m_polygon->numberOfEdges() - 1) % m_polygon->numberOfEdges());
119 const FloatPolygonEdge& nextEdge() const
121 ASSERT(m_polygon && m_polygon->numberOfEdges() > 1);
122 return m_polygon->edgeAt((m_edgeIndex + 1) % m_polygon->numberOfEdges());
125 const FloatPolygon* polygon() const { return m_polygon; }
126 unsigned vertexIndex1() const { return m_vertexIndex1; }
127 unsigned vertexIndex2() const { return m_vertexIndex2; }
128 unsigned edgeIndex() const { return m_edgeIndex; }
131 // Edge vertex index1 is less than index2, except the last edge, where index2 is 0. When a polygon edge
132 // is defined by 3 or more colinear vertices, index2 can be the the index of the last colinear vertex.
133 unsigned m_vertexIndex1;
134 unsigned m_vertexIndex2;
135 unsigned m_edgeIndex;
136 const FloatPolygon* m_polygon;
139 // These structures are used by PODIntervalTree for debugging.
141 template <> struct ValueToString<float> {
142 static String string(const float value) { return String::number(value); }
145 template<> struct ValueToString<FloatPolygonEdge*> {
146 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()); }
152 #endif // FloatPolygon_h