TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/plugin/numeric/coordinateSystem.hpp
Go to the documentation of this file.
00001 #ifndef _TUTTLE_PLUGIN_COORDONATESYSTEM_HPP_
00002 #define _TUTTLE_PLUGIN_COORDONATESYSTEM_HPP_
00003 
00004 #include <terry/point/operations.hpp>
00005 #include <boost/gil/utilities.hpp>
00006 #include <boost/static_assert.hpp>
00007 
00008 namespace tuttle {
00009 namespace plugin {
00010 
00011 typedef ::boost::gil::point2<double> Point2Double;
00012 
00013 enum ECoordinateSystem
00014 {
00015         eCoordinateSystemXYc,
00016         eCoordinateSystemXY,
00017         eCoordinateSystemXYcn,
00018         eCoordinateSystemXYn,
00019         eCoordinateSystemXXc,
00020         eCoordinateSystemXX,
00021         eCoordinateSystemXXcn,
00022         eCoordinateSystemXXn,
00023 };
00024 
00025 template<ECoordinateSystem coord>
00026 struct NotCenteredCoodinate { static const ECoordinateSystem value = coord; };
00027 template<>
00028 struct NotCenteredCoodinate<eCoordinateSystemXYc> { static const ECoordinateSystem value = eCoordinateSystemXY; };
00029 template<>
00030 struct NotCenteredCoodinate<eCoordinateSystemXYcn> { static const ECoordinateSystem value = eCoordinateSystemXYn; };
00031 template<>
00032 struct NotCenteredCoodinate<eCoordinateSystemXXc> { static const ECoordinateSystem value = eCoordinateSystemXX; };
00033 template<>
00034 struct NotCenteredCoodinate<eCoordinateSystemXXcn> { static const ECoordinateSystem value = eCoordinateSystemXXn; };
00035 
00036 
00037 template<ECoordinateSystem coord>
00038 struct NotNormalizedCoodinate { static const ECoordinateSystem value = coord; };
00039 template<>
00040 struct NotNormalizedCoodinate<eCoordinateSystemXYcn> { static const ECoordinateSystem value = eCoordinateSystemXYc; };
00041 template<>
00042 struct NotNormalizedCoodinate<eCoordinateSystemXYn> { static const ECoordinateSystem value = eCoordinateSystemXY; };
00043 template<>
00044 struct NotNormalizedCoodinate<eCoordinateSystemXXc> { static const ECoordinateSystem value = eCoordinateSystemXXc; };
00045 template<>
00046 struct NotNormalizedCoodinate<eCoordinateSystemXXn> { static const ECoordinateSystem value = eCoordinateSystemXX; };
00047 
00048 
00049 template<ECoordinateSystem coord>
00050 struct CoordinateSystemAxisXY { static const ECoordinateSystem value = coord; };
00051 template<>
00052 struct CoordinateSystemAxisXY<eCoordinateSystemXXc> { static const ECoordinateSystem value = eCoordinateSystemXYc; };
00053 template<>
00054 struct CoordinateSystemAxisXY<eCoordinateSystemXX> { static const ECoordinateSystem value = eCoordinateSystemXY; };
00055 template<>
00056 struct CoordinateSystemAxisXY<eCoordinateSystemXXcn> { static const ECoordinateSystem value = eCoordinateSystemXYcn; };
00057 template<>
00058 struct CoordinateSystemAxisXY<eCoordinateSystemXXn> { static const ECoordinateSystem value = eCoordinateSystemXYn; };
00059 
00060 
00061 template<typename Value, typename Point>
00062 Value canonicalXXToNormalizedXX( const Value& v, const Point& imgSize ) { return v / imgSize.x; }
00063 template<typename Value, typename Point>
00064 Value normalizedXXToCanonicalXX( const Value& v, const Point& imgSize ) { return v * imgSize.x; }
00065 
00066 template<typename Point>
00067 Point pointCanonicalXYToNormalizedXY( const Point& point, const Point& imgSize ) { return point / imgSize; }
00068 template<typename Point>
00069 Point pointNormalizedXYToCanonicalXY( const Point& point, const Point& imgSize ) { return point * imgSize; }
00070 
00071 //      template<typename Point>
00072 //    Point pointCanonicalXYToNormalizedXX( const Point& point, const Point& imgSize )
00073 //      {
00074 //              TUTTLE_TLOG( TUTTLE_TRACE, "canonicalXY : " << point.x << ", " << point.y );
00075 //              Point p( point.x / imgSize.x, ((point.y+((imgSize.x-imgSize.y)*0.5)) / imgSize.x) );
00076 //              TUTTLE_TLOG( TUTTLE_TRACE, "normalizedXX : " << p.x << ", " << p.y );
00077 //              return p;
00078 //      }
00079 
00080 template<typename Point>
00081 Point pointCanonicalXYToNormalizedXX( const Point& point, const Point& imgSize )
00082 {
00083         if( imgSize.x == 0 )
00084         {
00085                 Point p;
00086                 p.x = 0;
00087                 p.y = 0;
00088                 return p;
00089         }
00090         Point p;
00091         p.x = ( point.x / imgSize.x );
00092         p.y = ( ( point.y + ( ( imgSize.x - imgSize.y ) * 0.5 ) ) / imgSize.x );
00093         return p;
00094 }
00095 
00096 template<typename Point>
00097 Point pointCanonicalXYToNormalizedXXc( const Point& point, const Point& imgSize )
00098 {
00099         Point p( pointCanonicalXYToNormalizedXX( point, imgSize ) );
00100 
00101         p.x -= 0.5;
00102         p.y -= 0.5;
00103         return p;
00104 }
00105 
00106 template<typename Point>
00107 Point pointNormalizedXXToCanonicalXX( const Point& point, const Point& imgSize )
00108 {
00109         Point p;
00110 
00111         p.x = point.x * imgSize.x;
00112         p.y = point.y * imgSize.x;
00113         return p;
00114 }
00115 
00116 template<typename Point>
00117 Point pointCanonicalXXToNormalizedXX( const Point& point, const Point& imgSize )
00118 {
00119         Point p(0,0);
00120 
00121         if( imgSize.x != 0 )
00122                 p.x = point.x / imgSize.x;
00123         if( imgSize.y != 0 )
00124                 p.y = point.y / imgSize.x;
00125         return p;
00126 }
00127 
00128 template<typename Point>
00129 Point pointNormalizedXXToCanonicalXY( const Point& point, const Point& imgSize )
00130 {
00131         Point p;
00132 
00133         p.x = point.x * imgSize.x;
00134         p.y = ( point.y * imgSize.x ) - ( ( imgSize.x - imgSize.y ) * 0.5 );
00135         return p;
00136 }
00137 
00138 template<typename Point>
00139 Point pointNormalizedXXcToCanonicalXY( const Point& point, const Point& imgSize )
00140 {
00141         Point p;
00142 
00143         p.x = ( point.x + 0.5 ) * imgSize.x;
00144         p.y = ( ( point.y + 0.5 ) * imgSize.x ) - ( ( imgSize.x - imgSize.y ) * 0.5 );
00145         return p;
00146 }
00147 
00148 template<ECoordinateSystem from, ECoordinateSystem to>
00149 inline Point2Double pointConvertCoordinateSystem( const Point2Double& point, const Point2Double& imgSize ) { BOOST_STATIC_ASSERT( ( from == to ) ); return point; }
00150 
00151 template<>
00152 inline Point2Double pointConvertCoordinateSystem<eCoordinateSystemXY, eCoordinateSystemXYn>( const Point2Double& point, const Point2Double& imgSize )
00153 {
00154         return pointCanonicalXYToNormalizedXY( point, imgSize );
00155 }
00156 
00157 template<>
00158 inline Point2Double pointConvertCoordinateSystem<eCoordinateSystemXYn, eCoordinateSystemXY>( const Point2Double& point, const Point2Double& imgSize )
00159 {
00160         return pointNormalizedXYToCanonicalXY( point, imgSize );
00161 }
00162 
00163 template<>
00164 inline Point2Double pointConvertCoordinateSystem<eCoordinateSystemXY, eCoordinateSystemXXn>( const Point2Double& point, const Point2Double& imgSize )
00165 {
00166         return pointCanonicalXYToNormalizedXX( point, imgSize );
00167 }
00168 
00169 template<>
00170 inline Point2Double pointConvertCoordinateSystem<eCoordinateSystemXY, eCoordinateSystemXXcn>( const Point2Double& point, const Point2Double& imgSize )
00171 {
00172         return pointCanonicalXYToNormalizedXXc( point, imgSize );
00173 }
00174 
00175 template<>
00176 inline Point2Double pointConvertCoordinateSystem<eCoordinateSystemXXn, eCoordinateSystemXX>( const Point2Double& point, const Point2Double& imgSize )
00177 {
00178         return pointNormalizedXXToCanonicalXX( point, imgSize );
00179 }
00180 
00181 template<>
00182 inline Point2Double pointConvertCoordinateSystem<eCoordinateSystemXXn, eCoordinateSystemXY>( const Point2Double& point, const Point2Double& imgSize )
00183 {
00184         return pointNormalizedXXToCanonicalXY( point, imgSize );
00185 }
00186 
00187 template<>
00188 inline Point2Double pointConvertCoordinateSystem<eCoordinateSystemXXcn, eCoordinateSystemXY>( const Point2Double& point, const Point2Double& imgSize )
00189 {
00190         return pointNormalizedXXcToCanonicalXY( point, imgSize );
00191 }
00192 
00193 }
00194 }
00195 
00196 #endif
00197