/* * SOLID - Software Library for Interference Detection * * Copyright (C) 2001-2003 Dtecta. All rights reserved. * * This library may be distributed under the terms of the Q Public License * (QPL) as defined by Trolltech AS of Norway and appearing in the file * LICENSE.QPL included in the packaging of this file. * * This library may be distributed and/or modified under the terms of the * GNU General Public License (GPL) version 2 as published by the Free Software * Foundation and appearing in the file LICENSE.GPL included in the * packaging of this file. * * This library is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * Commercial use or any other use of this library not covered by either * the QPL or the GPL requires an additional license from Dtecta. * Please contact info@dtecta.com for enquiries about the terms of commercial * use of this library. */ #ifndef INTERVAL_H #define INTERVAL_H #if defined (__sgi) #include #else #include #endif #include #include namespace MT { template class Interval { public: Interval() {} #if _MSC_VER <= 1200 explicit Interval(const Scalar& x) : m_lb(x), m_ub(x) {} Interval(const Scalar& lb, const Scalar& ub) : m_lb(lb), m_ub(ub) { assert(lb <= ub); } #else template explicit Interval(const Scalar2& x) : m_lb(x), m_ub(x) {} template Interval(const Scalar2& lb, const Scalar2& ub) : m_lb(lb), m_ub(ub) { assert(lb <= ub); } template Interval(const Interval& z) { *this = z; } template Interval& operator=(const Interval& z) { m_lb = Scalar(z.lower()); m_ub = Scalar(z.upper()); return *this; } #endif Scalar& lower() { return m_lb; } const Scalar& lower() const { return m_lb; } Scalar& upper() { return m_ub; } const Scalar& upper() const { return m_ub; } Scalar center() const { return (m_lb + m_ub) * Scalar(0.5); } Scalar extent() const { return (m_ub - m_lb) * Scalar(0.5); } protected: Scalar m_lb, m_ub; }; template inline Interval operator+(const Interval& z1, const Interval& z2) { return Interval(z1.lower() + z2.lower(), z1.upper() + z2.upper()); } template inline Interval operator-(const Interval& z1, const Interval& z2) { return Interval(z1.lower() - z2.upper(), z1.upper() - z2.lower()); } template inline std::ostream& operator<<(std::ostream& os, const Interval& z) { return os << '[' << z.lower() << ", " << z.upper() << ']'; } template inline Scalar median(const Interval& z) { return (z.lower() + z.upper()) * Scalar(0.5); } template inline Scalar width(const Interval& z) { return z.upper() - z.lower(); } template inline bool overlap(const Interval& z1, const Interval& z2) { return z1.lower() <= z2.upper() && z2.lower() <= z1.upper(); } template inline bool in(const Interval& z1, const Interval& z2) { return z2.lower() <= z1.lower() && z1.upper() <= z2.upper(); } template inline bool in(Scalar x, const Interval& z) { return z.lower() <= x && x <= z.upper(); } template inline Interval widen(const Interval& z, const Scalar& x) { return Interval(z.lower() - x, z.upper() + x); } template inline Interval hull(const Interval& z1, const Interval& z2) { return Interval(GEN_min(z1.lower(), z2.lower()), GEN_max(z1.upper(), z2.upper())); } template inline Interval operator+(Scalar x, const Interval& z) { return Interval(x + z.lower(), x + z.upper()); } } #endif