00001
00014 #ifdef _MSC_VER
00015
00016 #include "msdevstudio/MSconfig.h"
00017 #endif //_MSC_VER
00018
00019 #include "Rectangle.h"
00020
00021 #ifdef _MSC_VER
00022 #define isnan _isnan
00023 #endif
00024
00025
00026 #ifdef __APPLE__
00027 #define _GLIBCPP_USE_C99 1
00028 #endif
00029
00030 #include <algorithm>
00031 #include <functional>
00032 #include <iostream>
00033
00034 #include <cmath>
00035 #include <cfloat>
00036
00037 #ifdef __APPLE__
00038 using std::isnan;
00039 #endif
00040
00041 using std::bind2nd;
00042 using std::greater;
00043 using std::less;
00044 using std::max;
00045 using std::min;
00046 using std::replace_if;
00047 using std::vector;
00048
00049 using namespace hippodraw;
00050
00051 Rect::Rect()
00052 : m_origin(), m_size()
00053 {
00054 }
00055
00056 Rect::
00057 Rect( double x, double y, double width, double height )
00058 : m_origin(), m_size()
00059 {
00060 setRect( x, y, width, height );
00061 }
00062
00063 Rect::Rect( double x, double y, double z,
00064 double width, double height, double depth )
00065 : m_origin(), m_size()
00066 {
00067 setRect( x, y, z, width, height, depth );
00068 }
00069
00070 void Rect::setRect( double x, double y, double width, double height )
00071 {
00072 m_origin.setPoint( x, y );
00073 m_size.setSize( width, height );
00074 }
00075
00076 void Rect::setRect( double x, double y, double z,
00077 double width, double height, double depth )
00078 {
00079 m_origin.setPoint( x, y, z );
00080 m_size.setSize( width, height, depth );
00081 }
00082
00083 void Rect::moveBy ( double x, double y )
00084 {
00085 m_origin.moveBy ( x, y );
00086 }
00087
00088 const Point & Rect::getOrigin() const
00089 {
00090 return m_origin;
00091 }
00092
00093 const Size & Rect::getSize() const
00094 {
00095 return m_size;
00096 }
00097
00098 double Rect::getZ() const
00099 {
00100 return getOrigin().getZ();
00101 }
00102
00103 void Rect::setZ ( double z )
00104 {
00105 m_origin.setZ ( z );
00106 }
00107
00108 double Rect::getWidth() const
00109 {
00110 return m_size.getWidth();
00111 }
00112
00113 double Rect::getHeight() const
00114 {
00115 return m_size.getHeight();
00116 }
00117
00118 double Rect::getDepth() const
00119 {
00120 return getSize().getDepth();
00121 }
00122
00123
00124 void Rect::setDepth ( double d )
00125 {
00126 m_size.setDepth ( d );
00127 }
00128
00136 bool Rect::isInDepth ( double z1 ) const
00137 {
00138 bool yes = false;
00139 double z_lo = m_origin.getZ ();
00140
00141 if ( z1 >= z_lo &&
00142 ( z1 ) <= z_lo + 1.00001*m_size.getDepth() ) yes = true;
00143
00144 return yes;
00145 }
00146
00149 bool Rect::isInBounds ( double x1, double y1 ) const
00150 {
00151 if ( isnan ( x1 ) || isnan ( y1 ) ) return false;
00152
00153 double x_lo = getX ();
00154 double y_lo = getY ();
00155
00156 double x_hi = x_lo + getWidth();
00157 double y_hi = y_lo + getHeight();
00158
00159 if( x1 < x_lo || x1 > x_hi ||
00160 y1 < y_lo || y1 > y_hi ) return false;
00161
00162 return true;
00163 }
00164
00165 bool Rect::isInBounds ( double x1, double y1, double z1 ) const
00166 {
00167 double x_lo = getX ();
00168 double y_lo = getY ();
00169 double z_lo = getZ ();
00170
00171 double x_hi = x_lo + getWidth();
00172 double y_hi = y_lo + getHeight();
00173 double z_hi = z_lo + getDepth();
00174
00175 if( x1 < x_lo || x1 > x_hi ||
00176 y1 < y_lo || y1 > y_hi ||
00177 z1 < z_lo || z1 > z_hi ) return false;
00178
00179 return true;
00180 }
00181
00182
00183
00184
00185 void Rect::makeInBounds ( double & x1, double & y1 ) const
00186 {
00187 double x_lo = m_origin.getX ();
00188 double y_lo = m_origin.getY ();
00189
00190 double x_hi = x_lo + m_size.getWidth();
00191 double y_hi = y_lo + m_size.getHeight();
00192
00193 if ( x1 < x_lo ) {
00194 x1 = x_lo;
00195 }
00196 else if ( x1 > x_hi ) {
00197 x1 = x_hi;
00198 }
00199 if ( y1 < y_lo ) {
00200 y1 = y_lo;
00201 }
00202 else if ( y1 > y_hi ) {
00203 y1 = y_hi;
00204 }
00205 }
00206
00207 void
00208 Rect::
00209 makeInBounds ( double & x1, double & y1, double & z1 ) const
00210 {
00211 double x_lo = m_origin.getX ();
00212 double y_lo = m_origin.getY ();
00213 double z_lo = m_origin.getZ ();
00214
00215 double x_hi = x_lo + m_size.getWidth();
00216 double y_hi = y_lo + m_size.getHeight();
00217 double z_hi = z_lo + m_size.getDepth();
00218
00219 x1 = max ( x1, x_lo );
00220 x1 = min ( x1, x_hi );
00221
00222 y1 = max ( y1, y_lo );
00223 y1 = min ( y1, y_hi );
00224
00225 z1 = max ( z1, z_lo );
00226 z1 = min ( z1, z_hi );
00227 }
00228
00229 void
00230 Rect::
00231 makeInBounds ( std::vector< double > & x,
00232 std::vector< double > & y ) const
00233 {
00234 double lo = getX ();
00235 double hi = lo + getWidth ();
00236
00237 replace_if ( x.begin (), x.end (),
00238 bind2nd ( less< double > (), lo ), lo );
00239
00240 replace_if ( x.begin (), x.end (),
00241 bind2nd ( greater< double > (), hi ), hi );
00242
00243 lo = getY ();
00244 hi = lo + getHeight ();
00245
00246 replace_if ( y.begin (), y.end (),
00247 bind2nd ( less< double > (), lo ), lo );
00248
00249 replace_if ( y.begin (), y.end (),
00250 bind2nd ( greater< double > (), hi ), hi );
00251
00252 }
00253
00254 void
00255 Rect::
00256 makeInBounds ( std::vector< double > & x,
00257 std::vector< double > & y,
00258 std::vector< double > & z ) const
00259 {
00260 double lo = getX ();
00261 double hi = lo + getWidth ();
00262
00263 replace_if ( x.begin (), x.end (),
00264 bind2nd ( less< double > (), lo ), lo );
00265
00266 replace_if ( x.begin (), x.end (),
00267 bind2nd ( greater< double > (), hi ), hi );
00268
00269 lo = getY ();
00270 hi = lo + getHeight ();
00271
00272 replace_if ( y.begin (), y.end (),
00273 bind2nd ( less< double > (), lo ), lo );
00274
00275 replace_if ( y.begin (), y.end (),
00276 bind2nd ( greater< double > (), hi ), hi );
00277
00278 lo = getZ ();
00279 hi = lo + getDepth ();
00280
00281 replace_if ( z.begin (), z.end (),
00282 bind2nd ( less< double > (), lo ), lo );
00283
00284 replace_if ( z.begin (), z.end (),
00285 bind2nd ( greater< double > (), hi ), hi );
00286
00287 }