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