15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* 25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved. 35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without 55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions 65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * are met: 75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 1. Redistributions of source code must retain the above 95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following 105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * disclaimer. 115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 2. Redistributions in binary form must reproduce the above 125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following 135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * disclaimer in the documentation and/or other materials 145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * provided with the distribution. 1553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * 165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THE POSSIBILITY OF SUCH DAMAGE. 285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */ 295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 30591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#ifndef ShapeInterval_h 31591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#define ShapeInterval_h 325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 33591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/Vector.h" 345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 35c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink { 365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 378abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)template <typename T> 388abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)class ShapeInterval { 398abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) WTF_MAKE_FAST_ALLOCATED; 405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)public: 41f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu ShapeInterval() 42f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu : m_x1(-1) 43f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu , m_x2(-2) 448abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) { 45f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu // The initial values of m_x1,x2 don't matter (unless you're looking 46f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu // at them in the debugger) so long as isUndefined() is true. 47f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu ASSERT(isUndefined()); 488abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) } 498abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) 50f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu ShapeInterval(T x1, T x2) 51f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu : m_x1(x1) 52f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu , m_x2(x2) 538abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) { 54f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu ASSERT(x2 >= x1); 558abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) } 568abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) 57f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu bool isUndefined() const { return m_x2 < m_x1; } 58f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu T x1() const { return isUndefined() ? 0 : m_x1; } 59f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu T x2() const { return isUndefined() ? 0 : m_x2; } 60f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu T width() const { return isUndefined() ? 0 : m_x2 - m_x1; } 61f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu bool isEmpty() const { return isUndefined() ? true : m_x1 == m_x2; } 628abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) 638abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) void set(T x1, T x2) 648abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) { 658abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) ASSERT(x2 >= x1); 668abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) m_x1 = x1; 678abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) m_x2 = x2; 688abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) } 698abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) 708abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) bool overlaps(const ShapeInterval<T>& interval) const 718abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) { 72f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu if (isUndefined() || interval.isUndefined()) 73f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu return false; 748abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) return x2() >= interval.x1() && x1() <= interval.x2(); 758abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) } 768abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) 778abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) bool contains(const ShapeInterval<T>& interval) const 788abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) { 79f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu if (isUndefined() || interval.isUndefined()) 80f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu return false; 818abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) return x1() <= interval.x1() && x2() >= interval.x2(); 828abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) } 838abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) 84a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch bool operator==(const ShapeInterval<T>& other) const { return x1() == other.x1() && x2() == other.x2(); } 85a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch bool operator!=(const ShapeInterval<T>& other) const { return !operator==(other); } 865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 87a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch void unite(const ShapeInterval<T>& interval) 888abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) { 89f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu if (interval.isUndefined()) 908abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) return; 91f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu if (isUndefined()) 92a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch set(interval.x1(), interval.x2()); 93a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch else 94a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch set(std::min<T>(x1(), interval.x1()), std::max<T>(x2(), interval.x2())); 958abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) } 968abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) 978abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)private: 988abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) T m_x1; 998abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) T m_x2; 1005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}; 1015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1028abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)typedef ShapeInterval<int> IntShapeInterval; 1038abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)typedef ShapeInterval<float> FloatShapeInterval; 1048abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) 1058abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)typedef Vector<IntShapeInterval> IntShapeIntervals; 1068abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)typedef Vector<FloatShapeInterval> FloatShapeIntervals; 1075c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 108c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink 1095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 110591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#endif // ShapeInterval_h 111