TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/common/ofx/utilities.hpp
Go to the documentation of this file.
00001 #ifndef _TUTTLE_COMMON_OFX_UTILITIES_HPP_
00002 #define _TUTTLE_COMMON_OFX_UTILITIES_HPP_
00003 
00004 #include <tuttle/common/utils/global.hpp>
00005 
00006 #include <ofxCore.h>
00007 #include <ofxImageEffect.h>
00008 
00009 #include <boost/numeric/conversion/cast.hpp>
00010 
00011 #include <string>
00012 #include <vector>
00013 
00014 namespace tuttle {
00015 namespace ofx {
00016 
00017 /// class that is a std::vector of std::strings
00018 typedef std::vector<std::string> StringVec;
00019 
00020 inline void setStringVecValue( StringVec& sv, const std::string& value, size_t index = 0 )
00021 {
00022         size_t size = sv.size();
00023 
00024         if( size <= index )
00025         {
00026                 while( size < index )
00027                 {
00028                         sv.push_back( "" );
00029                         ++size;
00030                 }
00031                 sv.push_back( value );
00032         }
00033         else
00034                 sv[index] = value;
00035 }
00036 
00037 /// get the min value
00038 template<class T>
00039 inline T minimum( const T& a, const T& b )
00040 {
00041         return a < b ? a : b;
00042 }
00043 
00044 /// get the max value
00045 template<class T>
00046 inline T maximum( const T& a, const T& b )
00047 {
00048         return a > b ? a : b;
00049 }
00050 
00051 /// clamp the value
00052 template<class T>
00053 inline T clamp( const T& v, const T& min, const T& max )
00054 {
00055         if( v < min )
00056                 return min;
00057         if( v > max )
00058                 return max;
00059         return v;
00060 }
00061 
00062 /// clamp the rect in v to the given bounds
00063 inline OfxRectD clamp( const OfxRectD& v,
00064                        const OfxRectD& bounds )
00065 {
00066         OfxRectD r;
00067 
00068         r.x1 = clamp( v.x1, bounds.x1, bounds.x2 );
00069         r.x2 = clamp( v.x2, bounds.x1, bounds.x2 );
00070         r.y1 = clamp( v.y1, bounds.y1, bounds.y2 );
00071         r.y2 = clamp( v.y2, bounds.y1, bounds.y2 );
00072         return r;
00073 }
00074 
00075 /// clamp the rect in v to the given bounds
00076 inline OfxRectI clamp( const OfxRectI& v,
00077                        const OfxRectI& bounds )
00078 {
00079         OfxRectI r;
00080 
00081         r.x1 = clamp( v.x1, bounds.x1, bounds.x2 );
00082         r.x2 = clamp( v.x2, bounds.x1, bounds.x2 );
00083         r.y1 = clamp( v.y1, bounds.y1, bounds.y2 );
00084         r.y2 = clamp( v.y2, bounds.y1, bounds.y2 );
00085         return r;
00086 }
00087 
00088 /// get the union of the two rects
00089 inline OfxRectD rectUnion( const OfxRectD& a,
00090                            const OfxRectD& b )
00091 {
00092         OfxRectD r;
00093 
00094         r.x1 = minimum( a.x1, b.x1 );
00095         r.x2 = maximum( a.x2, b.x2 );
00096         r.y1 = minimum( a.y1, b.y1 );
00097         r.y2 = maximum( a.y2, b.y2 );
00098         return r;
00099 }
00100 
00101 inline bool isEmpty( const OfxRectD& r )
00102 {
00103         if( r.x2 - r.x1 <= 0.0 )
00104                 return true;
00105         if( r.y2 - r.y1 <= 0.0 )
00106                 return true;
00107         return false;
00108 }
00109 
00110 inline bool isEmpty( const OfxRectI& r )
00111 {
00112         if( r.x2 - r.x1 <= 0 )
00113                 return true;
00114         if( r.y2 - r.y1 <= 0 )
00115                 return true;
00116         return false;
00117 }
00118 
00119 /// create an infinite rectangle
00120 inline OfxRectD infiniteRectD()
00121 {
00122         OfxRectD rect;
00123         rect.x1 = kOfxFlagInfiniteMin;
00124         rect.y1 = kOfxFlagInfiniteMin;
00125         rect.x2 = kOfxFlagInfiniteMax;
00126         rect.y2 = kOfxFlagInfiniteMax;
00127         return rect;
00128 }
00129 
00130 /// Convert OfxRectD to OfxRectI
00131 inline OfxRectI rectDToRectI( const OfxRectD& rectD )
00132 {
00133         OfxRectI rectI;
00134         rectI.x1 = boost::numeric_cast<int>( std::floor( rectD.x1 ) );
00135         rectI.y1 = boost::numeric_cast<int>( std::floor( rectD.y1 ) );
00136         rectI.x2 = boost::numeric_cast<int>( std::ceil( rectD.x2 ) );
00137         rectI.y2 = boost::numeric_cast<int>( std::ceil( rectD.y2 ) );
00138         return rectI;
00139 }
00140 
00141 }
00142 }
00143 
00144 #endif
00145