Home | History | Annotate | Download | only in geometry
      1 /*
      2  * Copyright (C) 2003, 2006, 2007 Apple Inc.  All rights reserved.
      3  * Copyright (C) 2005 Nokia.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "platform/geometry/FloatRect.h"
     29 
     30 #include "platform/FloatConversion.h"
     31 #include "platform/geometry/IntRect.h"
     32 #include "platform/geometry/LayoutRect.h"
     33 #include "third_party/skia/include/core/SkRect.h"
     34 #include "wtf/MathExtras.h"
     35 
     36 #include <algorithm>
     37 #include <math.h>
     38 
     39 namespace blink {
     40 
     41 FloatRect::FloatRect(const IntRect& r) : m_location(r.location()), m_size(r.size())
     42 {
     43 }
     44 
     45 FloatRect::FloatRect(const LayoutRect& r) : m_location(r.location()), m_size(r.size())
     46 {
     47 }
     48 
     49 FloatRect::FloatRect(const SkRect& r) : m_location(r.fLeft, r.fTop), m_size(r.width(), r.height())
     50 {
     51 }
     52 
     53 FloatRect FloatRect::narrowPrecision(double x, double y, double width, double height)
     54 {
     55     return FloatRect(narrowPrecisionToFloat(x), narrowPrecisionToFloat(y), narrowPrecisionToFloat(width), narrowPrecisionToFloat(height));
     56 }
     57 
     58 bool FloatRect::isExpressibleAsIntRect() const
     59 {
     60     return isWithinIntRange(x()) && isWithinIntRange(y())
     61         && isWithinIntRange(width()) && isWithinIntRange(height())
     62         && isWithinIntRange(maxX()) && isWithinIntRange(maxY());
     63 }
     64 
     65 bool FloatRect::intersects(const FloatRect& other) const
     66 {
     67     // Checking emptiness handles negative widths as well as zero.
     68     return !isEmpty() && !other.isEmpty()
     69         && x() < other.maxX() && other.x() < maxX()
     70         && y() < other.maxY() && other.y() < maxY();
     71 }
     72 
     73 bool FloatRect::contains(const FloatRect& other) const
     74 {
     75     return x() <= other.x() && maxX() >= other.maxX()
     76         && y() <= other.y() && maxY() >= other.maxY();
     77 }
     78 
     79 bool FloatRect::contains(const FloatPoint& point, ContainsMode containsMode) const
     80 {
     81     if (containsMode == InsideOrOnStroke)
     82         return contains(point.x(), point.y());
     83     return x() < point.x() && maxX() > point.x() && y() < point.y() && maxY() > point.y();
     84 }
     85 
     86 void FloatRect::intersect(const FloatRect& other)
     87 {
     88     float left = std::max(x(), other.x());
     89     float top = std::max(y(), other.y());
     90     float right = std::min(maxX(), other.maxX());
     91     float bottom = std::min(maxY(), other.maxY());
     92 
     93     // Return a clean empty rectangle for non-intersecting cases.
     94     if (left >= right || top >= bottom) {
     95         left = 0;
     96         top = 0;
     97         right = 0;
     98         bottom = 0;
     99     }
    100 
    101     setLocationAndSizeFromEdges(left, top, right, bottom);
    102 }
    103 
    104 void FloatRect::unite(const FloatRect& other)
    105 {
    106     // Handle empty special cases first.
    107     if (other.isEmpty())
    108         return;
    109     if (isEmpty()) {
    110         *this = other;
    111         return;
    112     }
    113 
    114     uniteEvenIfEmpty(other);
    115 }
    116 
    117 void FloatRect::uniteEvenIfEmpty(const FloatRect& other)
    118 {
    119     float minX = std::min(x(), other.x());
    120     float minY = std::min(y(), other.y());
    121     float maxX = std::max(this->maxX(), other.maxX());
    122     float maxY = std::max(this->maxY(), other.maxY());
    123 
    124     setLocationAndSizeFromEdges(minX, minY, maxX, maxY);
    125 }
    126 
    127 void FloatRect::uniteIfNonZero(const FloatRect& other)
    128 {
    129     // Handle empty special cases first.
    130     if (other.isZero())
    131         return;
    132     if (isZero()) {
    133         *this = other;
    134         return;
    135     }
    136 
    137     uniteEvenIfEmpty(other);
    138 }
    139 
    140 void FloatRect::extend(const FloatPoint& p)
    141 {
    142     float minX = std::min(x(), p.x());
    143     float minY = std::min(y(), p.y());
    144     float maxX = std::max(this->maxX(), p.x());
    145     float maxY = std::max(this->maxY(), p.y());
    146 
    147     setLocationAndSizeFromEdges(minX, minY, maxX, maxY);
    148 }
    149 
    150 void FloatRect::scale(float sx, float sy)
    151 {
    152     m_location.setX(x() * sx);
    153     m_location.setY(y() * sy);
    154     m_size.setWidth(width() * sx);
    155     m_size.setHeight(height() * sy);
    156 }
    157 
    158 FloatRect unionRect(const Vector<FloatRect>& rects)
    159 {
    160     FloatRect result;
    161 
    162     size_t count = rects.size();
    163     for (size_t i = 0; i < count; ++i)
    164         result.unite(rects[i]);
    165 
    166     return result;
    167 }
    168 
    169 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1)
    170 {
    171     float left = std::min(p0.x(), p1.x());
    172     float top = std::min(p0.y(), p1.y());
    173     float right = std::max(p0.x(), p1.x());
    174     float bottom = std::max(p0.y(), p1.y());
    175 
    176     setLocationAndSizeFromEdges(left, top, right, bottom);
    177 }
    178 
    179 namespace {
    180 // Helpers for 3- and 4-way max and min.
    181 
    182 template <typename T>
    183 T min3(const T& v1, const T& v2, const T& v3)
    184 {
    185     return std::min(std::min(v1, v2), v3);
    186 }
    187 
    188 template <typename T>
    189 T max3(const T& v1, const T& v2, const T& v3)
    190 {
    191     return std::max(std::max(v1, v2), v3);
    192 }
    193 
    194 template <typename T>
    195 T min4(const T& v1, const T& v2, const T& v3, const T& v4)
    196 {
    197     return std::min(std::min(v1, v2), std::min(v3, v4));
    198 }
    199 
    200 template <typename T>
    201 T max4(const T& v1, const T& v2, const T& v3, const T& v4)
    202 {
    203     return std::max(std::max(v1, v2), std::max(v3, v4));
    204 }
    205 
    206 } // anonymous namespace
    207 
    208 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2)
    209 {
    210     float left = min3(p0.x(), p1.x(), p2.x());
    211     float top = min3(p0.y(), p1.y(), p2.y());
    212     float right = max3(p0.x(), p1.x(), p2.x());
    213     float bottom = max3(p0.y(), p1.y(), p2.y());
    214 
    215     setLocationAndSizeFromEdges(left, top, right, bottom);
    216 }
    217 
    218 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3)
    219 {
    220     float left = min4(p0.x(), p1.x(), p2.x(), p3.x());
    221     float top = min4(p0.y(), p1.y(), p2.y(), p3.y());
    222     float right = max4(p0.x(), p1.x(), p2.x(), p3.x());
    223     float bottom = max4(p0.y(), p1.y(), p2.y(), p3.y());
    224 
    225     setLocationAndSizeFromEdges(left, top, right, bottom);
    226 }
    227 
    228 IntRect enclosingIntRect(const FloatRect& rect)
    229 {
    230     IntPoint location = flooredIntPoint(rect.minXMinYCorner());
    231     IntPoint maxPoint = ceiledIntPoint(rect.maxXMaxYCorner());
    232 
    233     return IntRect(location, maxPoint - location);
    234 }
    235 
    236 IntRect enclosedIntRect(const FloatRect& rect)
    237 {
    238     IntPoint location = ceiledIntPoint(rect.minXMinYCorner());
    239     IntPoint maxPoint = flooredIntPoint(rect.maxXMaxYCorner());
    240     IntSize size = maxPoint - location;
    241     size.clampNegativeToZero();
    242 
    243     return IntRect(location, size);
    244 }
    245 
    246 IntRect roundedIntRect(const FloatRect& rect)
    247 {
    248     return IntRect(roundedIntPoint(rect.location()), roundedIntSize(rect.size()));
    249 }
    250 
    251 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect)
    252 {
    253     if (!srcRect.width() || !srcRect.height())
    254         return FloatRect();
    255 
    256     float widthScale = destRect.width() / srcRect.width();
    257     float heightScale = destRect.height() / srcRect.height();
    258     return FloatRect(destRect.x() + (r.x() - srcRect.x()) * widthScale,
    259         destRect.y() + (r.y() - srcRect.y()) * heightScale,
    260         r.width() * widthScale, r.height() * heightScale);
    261 }
    262 
    263 }
    264