| 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 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 "FloatQuad.h" |
| 33 | |
| 34 | #include <algorithm> |
| 35 | #include <limits> |
| 36 | #include <wtf/MathExtras.h> |
| 37 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | static inline float min4(float a, float b, float c, float d) |
| 41 | { |
| 42 | return std::min(std::min(a, b), std::min(c, d)); |
| 43 | } |
| 44 | |
| 45 | static inline float max4(float a, float b, float c, float d) |
| 46 | { |
| 47 | return std::max(std::max(a, b), std::max(c, d)); |
| 48 | } |
| 49 | |
| 50 | inline float dot(const FloatSize& a, const FloatSize& b) |
| 51 | { |
| 52 | return a.width() * b.width() + a.height() * b.height(); |
| 53 | } |
| 54 | |
| 55 | inline float determinant(const FloatSize& a, const FloatSize& b) |
| 56 | { |
| 57 | return a.width() * b.height() - a.height() * b.width(); |
| 58 | } |
| 59 | |
| 60 | inline bool isPointInTriangle(const FloatPoint& p, const FloatPoint& t1, const FloatPoint& t2, const FloatPoint& t3) |
| 61 | { |
| 62 | // Compute vectors |
| 63 | FloatSize v0 = t3 - t1; |
| 64 | FloatSize v1 = t2 - t1; |
| 65 | FloatSize v2 = p - t1; |
| 66 | |
| 67 | // Compute dot products |
| 68 | float dot00 = dot(v0, v0); |
| 69 | float dot01 = dot(v0, v1); |
| 70 | float dot02 = dot(v0, v2); |
| 71 | float dot11 = dot(v1, v1); |
| 72 | float dot12 = dot(v1, v2); |
| 73 | |
| 74 | // Compute barycentric coordinates |
| 75 | float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01); |
| 76 | float u = (dot11 * dot02 - dot01 * dot12) * invDenom; |
| 77 | float v = (dot00 * dot12 - dot01 * dot02) * invDenom; |
| 78 | |
| 79 | // Check if point is in triangle |
| 80 | return (u >= 0) && (v >= 0) && (u + v <= 1); |
| 81 | } |
| 82 | |
| 83 | FloatRect FloatQuad::boundingBox() const |
| 84 | { |
| 85 | float left = min4(m_p1.x(), m_p2.x(), m_p3.x(), m_p4.x()); |
| 86 | float top = min4(m_p1.y(), m_p2.y(), m_p3.y(), m_p4.y()); |
| 87 | |
| 88 | float right = max4(m_p1.x(), m_p2.x(), m_p3.x(), m_p4.x()); |
| 89 | float bottom = max4(m_p1.y(), m_p2.y(), m_p3.y(), m_p4.y()); |
| 90 | |
| 91 | return FloatRect(left, top, right - left, bottom - top); |
| 92 | } |
| 93 | |
| 94 | bool FloatQuad::isRectilinear() const |
| 95 | { |
| 96 | return (WTF::areEssentiallyEqual(m_p1.x(), m_p2.x()) && WTF::areEssentiallyEqual(m_p2.y(), m_p3.y()) && WTF::areEssentiallyEqual(m_p3.x(), m_p4.x()) && WTF::areEssentiallyEqual(m_p4.y(), m_p1.y())) |
| 97 | || (WTF::areEssentiallyEqual(m_p1.y(), m_p2.y()) && WTF::areEssentiallyEqual(m_p2.x(), m_p3.x()) && WTF::areEssentiallyEqual(m_p3.y(), m_p4.y()) && WTF::areEssentiallyEqual(m_p4.x(), m_p1.x())); |
| 98 | } |
| 99 | |
| 100 | bool FloatQuad::containsPoint(const FloatPoint& p) const |
| 101 | { |
| 102 | return isPointInTriangle(p, m_p1, m_p2, m_p3) || isPointInTriangle(p, m_p1, m_p3, m_p4); |
| 103 | } |
| 104 | |
| 105 | // Note that we only handle convex quads here. |
| 106 | bool FloatQuad::containsQuad(const FloatQuad& other) const |
| 107 | { |
| 108 | return containsPoint(other.p1()) && containsPoint(other.p2()) && containsPoint(other.p3()) && containsPoint(other.p4()); |
| 109 | } |
| 110 | |
| 111 | static inline FloatPoint rightMostCornerToVector(const FloatRect& rect, const FloatSize& vector) |
| 112 | { |
| 113 | // Return the corner of the rectangle that if it is to the left of the vector |
| 114 | // would mean all of the rectangle is to the left of the vector. |
| 115 | // The vector here represents the side between two points in a clockwise convex polygon. |
| 116 | // |
| 117 | // Q XXX |
| 118 | // QQQ XXX If the lower left corner of X is left of the vector that goes from the top corner of Q to |
| 119 | // QQQ the right corner of Q, then all of X is left of the vector, and intersection impossible. |
| 120 | // Q |
| 121 | // |
| 122 | FloatPoint point; |
| 123 | if (vector.width() >= 0) |
| 124 | point.setY(rect.maxY()); |
| 125 | else |
| 126 | point.setY(rect.y()); |
| 127 | if (vector.height() >= 0) |
| 128 | point.setX(rect.x()); |
| 129 | else |
| 130 | point.setX(rect.maxX()); |
| 131 | return point; |
| 132 | } |
| 133 | |
| 134 | bool FloatQuad::intersectsRect(const FloatRect& rect) const |
| 135 | { |
| 136 | // For each side of the quad clockwise we check if the rectangle is to the left of it |
| 137 | // since only content on the right can onlap with the quad. |
| 138 | // This only works if the quad is convex. |
| 139 | FloatSize v1, v2, v3, v4; |
| 140 | |
| 141 | // Ensure we use clockwise vectors. |
| 142 | if (!isCounterclockwise()) { |
| 143 | v1 = m_p2 - m_p1; |
| 144 | v2 = m_p3 - m_p2; |
| 145 | v3 = m_p4 - m_p3; |
| 146 | v4 = m_p1 - m_p4; |
| 147 | } else { |
| 148 | v1 = m_p4 - m_p1; |
| 149 | v2 = m_p1 - m_p2; |
| 150 | v3 = m_p2 - m_p3; |
| 151 | v4 = m_p3 - m_p4; |
| 152 | } |
| 153 | |
| 154 | FloatPoint p = rightMostCornerToVector(rect, v1); |
| 155 | if (determinant(v1, p - m_p1) < 0) |
| 156 | return false; |
| 157 | |
| 158 | p = rightMostCornerToVector(rect, v2); |
| 159 | if (determinant(v2, p - m_p2) < 0) |
| 160 | return false; |
| 161 | |
| 162 | p = rightMostCornerToVector(rect, v3); |
| 163 | if (determinant(v3, p - m_p3) < 0) |
| 164 | return false; |
| 165 | |
| 166 | p = rightMostCornerToVector(rect, v4); |
| 167 | if (determinant(v4, p - m_p4) < 0) |
| 168 | return false; |
| 169 | |
| 170 | // If not all of the rectangle is outside one of the quad's four sides, then that means at least |
| 171 | // a part of the rectangle is overlapping the quad. |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | // Tests whether the line is contained by or intersected with the circle. |
| 176 | static inline bool lineIntersectsCircle(const FloatPoint& center, float radius, const FloatPoint& p0, const FloatPoint& p1) |
| 177 | { |
| 178 | float x0 = p0.x() - center.x(), y0 = p0.y() - center.y(); |
| 179 | float x1 = p1.x() - center.x(), y1 = p1.y() - center.y(); |
| 180 | float radius2 = radius * radius; |
| 181 | if ((x0 * x0 + y0 * y0) <= radius2 || (x1 * x1 + y1 * y1) <= radius2) |
| 182 | return true; |
| 183 | if (p0 == p1) |
| 184 | return false; |
| 185 | |
| 186 | float a = y0 - y1; |
| 187 | float b = x1 - x0; |
| 188 | float c = x0 * y1 - x1 * y0; |
| 189 | float distance2 = c * c / (a * a + b * b); |
| 190 | // If distance between the center point and the line > the radius, |
| 191 | // the line doesn't cross (or is contained by) the ellipse. |
| 192 | if (distance2 > radius2) |
| 193 | return false; |
| 194 | |
| 195 | // The nearest point on the line is between p0 and p1? |
| 196 | float x = - a * c / (a * a + b * b); |
| 197 | float y = - b * c / (a * a + b * b); |
| 198 | return (((x0 <= x && x <= x1) || (x0 >= x && x >= x1)) |
| 199 | && ((y0 <= y && y <= y1) || (y1 <= y && y <= y0))); |
| 200 | } |
| 201 | |
| 202 | bool FloatQuad::intersectsCircle(const FloatPoint& center, float radius) const |
| 203 | { |
| 204 | return containsPoint(center) // The circle may be totally contained by the quad. |
| 205 | || lineIntersectsCircle(center, radius, m_p1, m_p2) |
| 206 | || lineIntersectsCircle(center, radius, m_p2, m_p3) |
| 207 | || lineIntersectsCircle(center, radius, m_p3, m_p4) |
| 208 | || lineIntersectsCircle(center, radius, m_p4, m_p1); |
| 209 | } |
| 210 | |
| 211 | bool FloatQuad::intersectsEllipse(const FloatPoint& center, const FloatSize& radii) const |
| 212 | { |
| 213 | // Transform the ellipse to an origin-centered circle whose radius is the product of major radius and minor radius. |
| 214 | // Here we apply the same transformation to the quad. |
| 215 | FloatQuad transformedQuad(*this); |
| 216 | transformedQuad.move(-center.x(), -center.y()); |
| 217 | transformedQuad.scale(radii.height(), radii.width()); |
| 218 | |
| 219 | FloatPoint originPoint; |
| 220 | return transformedQuad.intersectsCircle(originPoint, radii.height() * radii.width()); |
| 221 | |
| 222 | } |
| 223 | |
| 224 | bool FloatQuad::isCounterclockwise() const |
| 225 | { |
| 226 | // Return if the two first vectors are turning clockwise. If the quad is convex then all following vectors will turn the same way. |
| 227 | return determinant(m_p2 - m_p1, m_p3 - m_p2) < 0; |
| 228 | } |
| 229 | |
| 230 | } // namespace WebCore |
| 231 | |