Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / geometry / FloatPolygon.h
1 /*
2  * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above
9  *    copyright notice, this list of conditions and the following
10  *    disclaimer.
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.
15  *
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.
28  */
29
30 #ifndef FloatPolygon_h
31 #define FloatPolygon_h
32
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"
40
41 namespace blink {
42
43 class FloatPolygonEdge;
44
45 // This class is used by PODIntervalTree for debugging.
46 #ifndef NDEBUG
47 template <class> struct ValueToString;
48 #endif
49
50 class PLATFORM_EXPORT FloatPolygon {
51 public:
52     FloatPolygon(PassOwnPtr<Vector<FloatPoint> > vertices, WindRule fillRule);
53
54     const FloatPoint& vertexAt(unsigned index) const { return (*m_vertices)[index]; }
55     unsigned numberOfVertices() const { return m_vertices->size(); }
56
57     WindRule fillRule() const { return m_fillRule; }
58
59     const FloatPolygonEdge& edgeAt(unsigned index) const { return m_edges[index]; }
60     unsigned numberOfEdges() const { return m_edges.size(); }
61
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; }
66
67 private:
68     typedef PODInterval<float, FloatPolygonEdge*> EdgeInterval;
69     typedef PODIntervalTree<float, FloatPolygonEdge*> EdgeIntervalTree;
70
71     bool containsNonZero(const FloatPoint&) const;
72     bool containsEvenOdd(const FloatPoint&) const;
73
74     OwnPtr<Vector<FloatPoint> > m_vertices;
75     WindRule m_fillRule;
76     FloatRect m_boundingBox;
77     bool m_empty;
78     Vector<FloatPolygonEdge> m_edges;
79     EdgeIntervalTree m_edgeTree; // Each EdgeIntervalTree node stores minY, maxY, and a ("UserData") pointer to a FloatPolygonEdge.
80
81 };
82
83 class PLATFORM_EXPORT VertexPair {
84 public:
85     virtual ~VertexPair() { }
86
87     virtual const FloatPoint& vertex1() const = 0;
88     virtual const FloatPoint& vertex2() const = 0;
89
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()); }
94
95     bool intersection(const VertexPair&, FloatPoint&) const;
96 };
97
98 class PLATFORM_EXPORT FloatPolygonEdge : public VertexPair {
99     friend class FloatPolygon;
100 public:
101     virtual const FloatPoint& vertex1() const override
102     {
103         ASSERT(m_polygon);
104         return m_polygon->vertexAt(m_vertexIndex1);
105     }
106
107     virtual const FloatPoint& vertex2() const override
108     {
109         ASSERT(m_polygon);
110         return m_polygon->vertexAt(m_vertexIndex2);
111     }
112
113     const FloatPolygonEdge& previousEdge() const
114     {
115         ASSERT(m_polygon && m_polygon->numberOfEdges() > 1);
116         return m_polygon->edgeAt((m_edgeIndex + m_polygon->numberOfEdges() - 1) % m_polygon->numberOfEdges());
117     }
118
119     const FloatPolygonEdge& nextEdge() const
120     {
121         ASSERT(m_polygon && m_polygon->numberOfEdges() > 1);
122         return m_polygon->edgeAt((m_edgeIndex + 1) % m_polygon->numberOfEdges());
123     }
124
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; }
129
130 private:
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;
137 };
138
139 // These structures are used by PODIntervalTree for debugging.
140 #ifndef NDEBUG
141 template <> struct ValueToString<float> {
142     static String string(const float value) { return String::number(value); }
143 };
144
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()); }
147 };
148 #endif
149
150 } // namespace blink
151
152 #endif // FloatPolygon_h