Path: blob/master/3rdparty/openexr/Imath/ImathInterval.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536#ifndef INCLUDED_IMATHINTERVAL_H37#define INCLUDED_IMATHINTERVAL_H383940//-------------------------------------------------------------------41//42// class Imath::Interval<class T>43// --------------------------------44//45// An Interval has a min and a max and some miscellaneous46// functions. It is basically a Box<T> that allows T to be47// a scalar.48//49//-------------------------------------------------------------------5051#include "ImathVec.h"5253namespace Imath {545556template <class T>57class Interval58{59public:6061//-------------------------62// Data Members are public63//-------------------------6465T min;66T max;6768//-----------------------------------------------------69// Constructors - an "empty" Interval is created by default70//-----------------------------------------------------7172Interval();73Interval(const T& point);74Interval(const T& minT, const T& maxT);7576//--------------------------------77// Operators: we get != from STL78//--------------------------------7980bool operator == (const Interval<T> &src) const;8182//------------------83// Interval manipulation84//------------------8586void makeEmpty();87void extendBy(const T& point);88void extendBy(const Interval<T>& interval);8990//---------------------------------------------------91// Query functions - these compute results each time92//---------------------------------------------------9394T size() const;95T center() const;96bool intersects(const T &point) const;97bool intersects(const Interval<T> &interval) const;9899//----------------100// Classification101//----------------102103bool hasVolume() const;104bool isEmpty() const;105};106107108//--------------------109// Convenient typedefs110//--------------------111112113typedef Interval <float> Intervalf;114typedef Interval <double> Intervald;115typedef Interval <short> Intervals;116typedef Interval <int> Intervali;117118//----------------119// Implementation120//----------------121122123template <class T>124inline Interval<T>::Interval()125{126makeEmpty();127}128129template <class T>130inline Interval<T>::Interval(const T& point)131{132min = point;133max = point;134}135136template <class T>137inline Interval<T>::Interval(const T& minV, const T& maxV)138{139min = minV;140max = maxV;141}142143template <class T>144inline bool145Interval<T>::operator == (const Interval<T> &src) const146{147return (min == src.min && max == src.max);148}149150template <class T>151inline void152Interval<T>::makeEmpty()153{154min = limits<T>::max();155max = limits<T>::min();156}157158template <class T>159inline void160Interval<T>::extendBy(const T& point)161{162if ( point < min )163min = point;164165if ( point > max )166max = point;167}168169template <class T>170inline void171Interval<T>::extendBy(const Interval<T>& interval)172{173if ( interval.min < min )174min = interval.min;175176if ( interval.max > max )177max = interval.max;178}179180template <class T>181inline bool182Interval<T>::intersects(const T& point) const183{184return point >= min && point <= max;185}186187template <class T>188inline bool189Interval<T>::intersects(const Interval<T>& interval) const190{191return interval.max >= min && interval.min <= max;192}193194template <class T>195inline T196Interval<T>::size() const197{198return max-min;199}200201template <class T>202inline T203Interval<T>::center() const204{205return (max+min)/2;206}207208template <class T>209inline bool210Interval<T>::isEmpty() const211{212return max < min;213}214215template <class T>216inline bool Interval<T>::hasVolume() const217{218return max > min;219}220221} // namespace Imath222223#endif224225226