Home | History | Annotate | Download | only in shapes
      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 ShapeInterval_h
     31 #define ShapeInterval_h
     32 
     33 #include "wtf/Vector.h"
     34 
     35 namespace blink {
     36 
     37 template <typename T>
     38 class ShapeInterval {
     39     WTF_MAKE_FAST_ALLOCATED;
     40 public:
     41     ShapeInterval()
     42         : m_x1(-1)
     43         , m_x2(-2)
     44     {
     45         // The initial values of m_x1,x2 don't matter (unless you're looking
     46         // at them in the debugger) so long as isUndefined() is true.
     47         ASSERT(isUndefined());
     48     }
     49 
     50     ShapeInterval(T x1, T x2)
     51         : m_x1(x1)
     52         , m_x2(x2)
     53     {
     54         ASSERT(x2 >= x1);
     55     }
     56 
     57     bool isUndefined() const { return m_x2 < m_x1; }
     58     T x1() const { return isUndefined() ? 0 : m_x1; }
     59     T x2() const { return isUndefined() ? 0 : m_x2; }
     60     T width() const { return isUndefined() ? 0 : m_x2 - m_x1; }
     61     bool isEmpty() const { return isUndefined() ? true : m_x1 == m_x2; }
     62 
     63     void set(T x1, T x2)
     64     {
     65         ASSERT(x2 >= x1);
     66         m_x1 = x1;
     67         m_x2 = x2;
     68     }
     69 
     70     bool overlaps(const ShapeInterval<T>& interval) const
     71     {
     72         if (isUndefined() || interval.isUndefined())
     73             return false;
     74         return x2() >= interval.x1() && x1() <= interval.x2();
     75     }
     76 
     77     bool contains(const ShapeInterval<T>& interval) const
     78     {
     79         if (isUndefined() || interval.isUndefined())
     80             return false;
     81         return x1() <= interval.x1() && x2() >= interval.x2();
     82     }
     83 
     84     bool operator==(const ShapeInterval<T>& other) const { return x1() == other.x1() && x2() == other.x2(); }
     85     bool operator!=(const ShapeInterval<T>& other) const { return !operator==(other); }
     86 
     87     void unite(const ShapeInterval<T>& interval)
     88     {
     89         if (interval.isUndefined())
     90             return;
     91         if (isUndefined())
     92             set(interval.x1(), interval.x2());
     93         else
     94             set(std::min<T>(x1(), interval.x1()), std::max<T>(x2(), interval.x2()));
     95     }
     96 
     97 private:
     98     T m_x1;
     99     T m_x2;
    100 };
    101 
    102 typedef ShapeInterval<int> IntShapeInterval;
    103 typedef ShapeInterval<float> FloatShapeInterval;
    104 
    105 typedef Vector<IntShapeInterval> IntShapeIntervals;
    106 typedef Vector<FloatShapeInterval> FloatShapeIntervals;
    107 
    108 } // namespace blink
    109 
    110 #endif // ShapeInterval_h
    111