1/*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2013 Xidorn Quan (quanxunzhen@gmail.com)
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1.  Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 * 2.  Redistributions in binary form must reproduce the above copyright
13 *     notice, this list of conditions and the following disclaimer in the
14 *     documentation and/or other materials provided with the distribution.
15 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16 *     its contributors may be used to endorse or promote products derived
17 *     from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "platform/geometry/FloatQuad.h"
33
34#include <algorithm>
35#include <limits>
36
37namespace blink {
38
39static inline float min4(float a, float b, float c, float d)
40{
41    return std::min(std::min(a, b), std::min(c, d));
42}
43
44static inline float max4(float a, float b, float c, float d)
45{
46    return std::max(std::max(a, b), std::max(c, d));
47}
48
49inline float dot(const FloatSize& a, const FloatSize& b)
50{
51    return a.width() * b.width() + a.height() * b.height();
52}
53
54inline float determinant(const FloatSize& a, const FloatSize& b)
55{
56    return a.width() * b.height() - a.height() * b.width();
57}
58
59inline bool isPointInTriangle(const FloatPoint& p, const FloatPoint& t1, const FloatPoint& t2, const FloatPoint& t3)
60{
61    // Compute vectors
62    FloatSize v0 = t3 - t1;
63    FloatSize v1 = t2 - t1;
64    FloatSize v2 = p - t1;
65
66    // Compute dot products
67    float dot00 = dot(v0, v0);
68    float dot01 = dot(v0, v1);
69    float dot02 = dot(v0, v2);
70    float dot11 = dot(v1, v1);
71    float dot12 = dot(v1, v2);
72
73    // Compute barycentric coordinates
74    float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
75    float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
76    float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
77
78    // Check if point is in triangle
79    return (u >= 0) && (v >= 0) && (u + v <= 1);
80}
81
82FloatRect FloatQuad::boundingBox() const
83{
84    float left   = min4(m_p1.x(), m_p2.x(), m_p3.x(), m_p4.x());
85    float top    = min4(m_p1.y(), m_p2.y(), m_p3.y(), m_p4.y());
86
87    float right  = max4(m_p1.x(), m_p2.x(), m_p3.x(), m_p4.x());
88    float bottom = max4(m_p1.y(), m_p2.y(), m_p3.y(), m_p4.y());
89
90    return FloatRect(left, top, right - left, bottom - top);
91}
92
93static inline bool withinEpsilon(float a, float b)
94{
95    return fabs(a - b) < std::numeric_limits<float>::epsilon();
96}
97
98bool FloatQuad::isRectilinear() const
99{
100    return (withinEpsilon(m_p1.x(), m_p2.x()) && withinEpsilon(m_p2.y(), m_p3.y()) && withinEpsilon(m_p3.x(), m_p4.x()) && withinEpsilon(m_p4.y(), m_p1.y()))
101        || (withinEpsilon(m_p1.y(), m_p2.y()) && withinEpsilon(m_p2.x(), m_p3.x()) && withinEpsilon(m_p3.y(), m_p4.y()) && withinEpsilon(m_p4.x(), m_p1.x()));
102}
103
104bool FloatQuad::containsPoint(const FloatPoint& p) const
105{
106    return isPointInTriangle(p, m_p1, m_p2, m_p3) || isPointInTriangle(p, m_p1, m_p3, m_p4);
107}
108
109// Note that we only handle convex quads here.
110bool FloatQuad::containsQuad(const FloatQuad& other) const
111{
112    return containsPoint(other.p1()) && containsPoint(other.p2()) && containsPoint(other.p3()) && containsPoint(other.p4());
113}
114
115static inline FloatPoint rightMostCornerToVector(const FloatRect& rect, const FloatSize& vector)
116{
117    // Return the corner of the rectangle that if it is to the left of the vector
118    // would mean all of the rectangle is to the left of the vector.
119    // The vector here represents the side between two points in a clockwise convex polygon.
120    //
121    //  Q  XXX
122    // QQQ XXX   If the lower left corner of X is left of the vector that goes from the top corner of Q to
123    //  QQQ      the right corner of Q, then all of X is left of the vector, and intersection impossible.
124    //   Q
125    //
126    FloatPoint point;
127    if (vector.width() >= 0)
128        point.setY(rect.maxY());
129    else
130        point.setY(rect.y());
131    if (vector.height() >= 0)
132        point.setX(rect.x());
133    else
134        point.setX(rect.maxX());
135    return point;
136}
137
138bool FloatQuad::intersectsRect(const FloatRect& rect) const
139{
140    // For each side of the quad clockwise we check if the rectangle is to the left of it
141    // since only content on the right can onlap with the quad.
142    // This only works if the quad is convex.
143    FloatSize v1, v2, v3, v4;
144
145    // Ensure we use clockwise vectors.
146    if (!isCounterclockwise()) {
147        v1 = m_p2 - m_p1;
148        v2 = m_p3 - m_p2;
149        v3 = m_p4 - m_p3;
150        v4 = m_p1 - m_p4;
151    } else {
152        v1 = m_p4 - m_p1;
153        v2 = m_p1 - m_p2;
154        v3 = m_p2 - m_p3;
155        v4 = m_p3 - m_p4;
156    }
157
158    FloatPoint p = rightMostCornerToVector(rect, v1);
159    if (determinant(v1, p - m_p1) < 0)
160        return false;
161
162    p = rightMostCornerToVector(rect, v2);
163    if (determinant(v2, p - m_p2) < 0)
164        return false;
165
166    p = rightMostCornerToVector(rect, v3);
167    if (determinant(v3, p - m_p3) < 0)
168        return false;
169
170    p = rightMostCornerToVector(rect, v4);
171    if (determinant(v4, p - m_p4) < 0)
172        return false;
173
174    // If not all of the rectangle is outside one of the quad's four sides, then that means at least
175    // a part of the rectangle is overlapping the quad.
176    return true;
177}
178
179// Tests whether the line is contained by or intersected with the circle.
180static inline bool lineIntersectsCircle(const FloatPoint& center, float radius, const FloatPoint& p0, const FloatPoint& p1)
181{
182    float x0 = p0.x() - center.x(), y0 = p0.y() - center.y();
183    float x1 = p1.x() - center.x(), y1 = p1.y() - center.y();
184    float radius2 = radius * radius;
185    if ((x0 * x0 + y0 * y0) <= radius2 || (x1 * x1 + y1 * y1) <= radius2)
186        return true;
187    if (p0 == p1)
188        return false;
189
190    float a = y0 - y1;
191    float b = x1 - x0;
192    float c = x0 * y1 - x1 * y0;
193    float distance2 = c * c / (a * a + b * b);
194    // If distance between the center point and the line > the radius,
195    // the line doesn't cross (or is contained by) the ellipse.
196    if (distance2 > radius2)
197        return false;
198
199    // The nearest point on the line is between p0 and p1?
200    float x = - a * c / (a * a + b * b);
201    float y = - b * c / (a * a + b * b);
202    return (((x0 <= x && x <= x1) || (x0 >= x && x >= x1))
203        && ((y0 <= y && y <= y1) || (y1 <= y && y <= y0)));
204}
205
206bool FloatQuad::intersectsCircle(const FloatPoint& center, float radius) const
207{
208    return containsPoint(center) // The circle may be totally contained by the quad.
209        || lineIntersectsCircle(center, radius, m_p1, m_p2)
210        || lineIntersectsCircle(center, radius, m_p2, m_p3)
211        || lineIntersectsCircle(center, radius, m_p3, m_p4)
212        || lineIntersectsCircle(center, radius, m_p4, m_p1);
213}
214
215bool FloatQuad::intersectsEllipse(const FloatPoint& center, const FloatSize& radii) const
216{
217    // Transform the ellipse to an origin-centered circle whose radius is the product of major radius and minor radius.
218    // Here we apply the same transformation to the quad.
219    FloatQuad transformedQuad(*this);
220    transformedQuad.move(-center.x(), -center.y());
221    transformedQuad.scale(radii.height(), radii.width());
222
223    FloatPoint originPoint;
224    return transformedQuad.intersectsCircle(originPoint, radii.height() * radii.width());
225
226}
227
228bool FloatQuad::isCounterclockwise() const
229{
230    // Return if the two first vectors are turning clockwise. If the quad is convex then all following vectors will turn the same way.
231    return determinant(m_p2 - m_p1, m_p3 - m_p2) < 0;
232}
233
234} // namespace blink
235