00001 #include <j2k/Fred/Standard.hpp>
00002 #include <j2k/Fred/MathCst.hpp>
00003 #include <math.h>
00004
00005
00006 #ifndef Type
00007 #error "Type is not defined."
00008 #endif
00009
00010 #ifndef FType
00011 #error "FType is not defined."
00012 #endif
00013
00014 #ifndef JPoint
00015 #error "JPoint is not defined."
00016 #endif
00017
00018 #ifndef JPoint_t
00019 #error "JPoint_t is not defined."
00020 #endif
00021
00022 struct JPoint_t {
00023 Type x;
00024 Type y;
00025
00026 BOOL PolarCache;
00027 FType r;
00028 FType theta;
00029 };
00030
00031 class JPoint {
00032 public:
00033
00034 inline JPoint() {
00035 memset( &pt, 0, sizeof( JPoint_t ) );
00036 }
00037
00038 inline JPoint( Type X ) {
00039 memset( &pt, 0, sizeof( JPoint_t ) );
00040 pt.x = X;
00041 }
00042
00043 inline JPoint( Type X, Type Y ) {
00044 memset( &pt, 0, sizeof( JPoint_t ) );
00045 pt.x = X;
00046 pt.y = Y;
00047 }
00048
00049 inline JPoint( const JPoint& p ) {
00050 memcpy( &pt, &p.pt, sizeof( JPoint_t ) );
00051 }
00052
00053 inline const JPoint& operator=( const JPoint& p ) {
00054 memcpy( &pt, &p.pt, sizeof( JPoint_t ) );
00055 return *this;
00056 }
00057
00058
00059 inline BOOL operator==( const JPoint& p ) {
00060 return !memcmp( &pt, &p.pt, (sizeof( Type ) * 2) );
00061 }
00062
00063 inline BOOL operator!=( const JPoint& p ) {
00064 return memcmp( &pt, &p.pt, (sizeof( Type ) * 2) );
00065 }
00066
00067 inline const JPoint& operator++() {
00068 if ( !pt.PolarCache ) getPolarfromXY();
00069 pt.r++;
00070 getXYfromPolar();
00071 return *this;
00072 }
00073
00074 inline const JPoint& operator++( int ) {
00075 if ( !pt.PolarCache ) getPolarfromXY();
00076 pt.r++;
00077 getXYfromPolar();
00078 return *this;
00079 }
00080
00081 inline const JPoint& operator--() {
00082 if ( !pt.PolarCache ) getPolarfromXY();
00083 pt.r--;
00084 getXYfromPolar();
00085 return *this;
00086 }
00087
00088 inline const JPoint& operator--( int ) {
00089 if ( !pt.PolarCache ) getPolarfromXY();
00090 pt.r--;
00091 getXYfromPolar();
00092 return *this;
00093 }
00094
00095
00096 inline const JPoint& setPoint( Type X, Type Y ) {
00097 pt.x = X;
00098 pt.y = Y;
00099 pt.PolarCache = FALSE;
00100 return *this;
00101 }
00102
00103
00104 inline const JPoint& setPolar( FType R, FType Theta ) {
00105 pt.r = R;
00106 pt.theta = Theta;
00107 getXYfromPolar();
00108 return *this;
00109 }
00110
00111 inline const JPoint& setX( FType X ) {
00112 pt.x = X;
00113 pt.PolarCache = FALSE;
00114 return *this;
00115 }
00116
00117 inline const JPoint& setY( FType Y ) {
00118 pt.y = Y;
00119 pt.PolarCache = FALSE;
00120 return *this;
00121 }
00122
00123 inline const JPoint& setR( FType R ) {
00124 pt.r = R;
00125 getXYfromPolar();
00126 return *this;
00127 }
00128
00129 inline const JPoint& setAngle( FType Theta ) {
00130 pt.theta = Theta;
00131 getXYfromPolar();
00132 return *this;
00133 }
00134
00135 inline Type getX() const {
00136 return pt.x;
00137 }
00138
00139 inline Type getY() const {
00140 return pt.y;
00141 }
00142
00143 inline FType getR() {
00144 if ( !pt.PolarCache )
00145 getPolarfromXY();
00146 return pt.r;
00147 }
00148
00149 inline FType getAngle() {
00150 if ( !pt.PolarCache )
00151 getPolarfromXY();
00152 return pt.theta;
00153 }
00154
00155
00156 inline friend ostream& operator << ( ostream& os, const JPoint& p )
00157 {
00158 os << '(' << p.pt.x << ", " << p.pt.y << ')';
00159 return os;
00160 }
00161
00162 protected:
00163 struct JPoint_t pt;
00164
00165
00166 void getXYfromPolar() {
00167 pt.PolarCache = TRUE;
00168 pt.x = pt.r * cos( pt.theta );
00169 pt.y = pt.r * sin( pt.theta );
00170 }
00171
00172
00173 void getPolarfromXY() {
00174 pt.r = sqrt( pt.x * pt.x + pt.y * pt.y );
00175 pt.theta = atan2( pt.y, pt.x );
00176 pt.PolarCache = TRUE;
00177 }
00178 };
00179