Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Rectangle.cxx

Go to the documentation of this file.
00001 
00014 #ifdef _MSC_VER
00015 // Include max() and min() missing from MicroSoft Visual C++.
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 //To have isnan.
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 HippoRectangle::HippoRectangle()
00050   : m_origin(), m_size()
00051 {
00052 }
00053 
00054 HippoRectangle::
00055 HippoRectangle( double x, double y, double width, double height )
00056   : m_origin(), m_size()
00057 {
00058   setRect( x, y, width, height );
00059 }
00060 
00061 HippoRectangle::HippoRectangle( double x, double y, double z,
00062                       double width, double height, double depth )
00063   : m_origin(), m_size()
00064 {
00065   setRect( x, y, z, width, height, depth );
00066 }
00067 
00068 void HippoRectangle::setRect( double x, double y, double width, double height )
00069 {
00070   m_origin.setPoint( x, y );
00071   m_size.setSize( width, height );
00072 }
00073 
00074 void HippoRectangle::setRect( double x, double y, double z,
00075                          double width, double height, double depth )
00076 {
00077   m_origin.setPoint( x, y, z );
00078   m_size.setSize( width, height, depth );
00079 }
00080 
00081 void HippoRectangle::moveBy ( double x, double y )
00082 {
00083   m_origin.moveBy ( x, y );
00084 }
00085 
00086 const Point & HippoRectangle::getOrigin() const
00087 {
00088   return m_origin;
00089 }
00090 
00091 const Size & HippoRectangle::getSize() const
00092 {
00093   return m_size;
00094 }
00095 
00096 double HippoRectangle::getZ() const
00097 {
00098   return getOrigin().getZ();
00099 }
00100 
00101 void HippoRectangle::setZ ( double z ) 
00102 {
00103   m_origin.setZ ( z );
00104 }
00105 
00106 double HippoRectangle::getWidth() const
00107 {
00108   return m_size.getWidth();
00109 }
00110 
00111 double HippoRectangle::getHeight() const
00112 {
00113   return m_size.getHeight();
00114 }
00115 
00116 double HippoRectangle::getDepth() const
00117 {
00118   return getSize().getDepth();
00119 }
00120 
00121 void HippoRectangle::setDepth ( double d )
00122 {
00123   m_size.setDepth ( d );
00124 }
00125 
00133 bool HippoRectangle::isInDepth ( double z1 ) const
00134 {
00135   bool yes = false;
00136   double z_lo = m_origin.getZ ();
00137 
00138   if ( z1 >= z_lo && 
00139        ( z1 - z_lo)  <= 1.00001*m_size.getDepth() ) yes = true;
00140 
00141   return yes;
00142 }
00143 
00144 bool HippoRectangle::isInBounds ( double x1, double y1 ) const
00145 {
00146   if ( isnan ( x1 ) || isnan ( y1 ) ) return false;
00147 
00148   double x_lo = getX ();
00149   double y_lo = getY ();
00150 
00151   double x_hi = x_lo + getWidth();
00152   double y_hi = y_lo + getHeight();
00153 
00154   if( x1 < x_lo || x1 > x_hi ||
00155       y1 < y_lo || y1 > y_hi ) return false;
00156 
00157   return true;
00158 }
00159 
00160 bool HippoRectangle::isInBounds ( double x1, double y1, double z1 ) const
00161 {
00162   double x_lo = getX ();
00163   double y_lo = getY ();
00164   double z_lo = getZ ();
00165 
00166   double x_hi = x_lo + getWidth();
00167   double y_hi = y_lo + getHeight();
00168   double z_hi = z_lo + getDepth();
00169 
00170   if( x1 < x_lo || x1 > x_hi ||
00171       y1 < y_lo || y1 > y_hi ||
00172       z1 < z_lo || z1 > z_hi ) return false;
00173 
00174   return true;
00175 }
00176 
00177 /* Note: the if statements were measured faster than the use of max()
00178    and min(). 
00179 */
00180 void HippoRectangle::makeInBounds ( double & x1, double & y1 ) const
00181 {
00182   double x_lo = m_origin.getX ();
00183   double y_lo = m_origin.getY ();
00184 
00185   double x_hi = x_lo + m_size.getWidth();
00186   double y_hi = y_lo + m_size.getHeight();
00187 
00188   if ( x1 < x_lo ) {
00189     x1 = x_lo;
00190   } 
00191   else if ( x1 > x_hi ) {
00192     x1 = x_hi;
00193   }
00194   if ( y1 < y_lo ) {
00195     y1 = y_lo;
00196   }
00197   else if ( y1 > y_hi ) {
00198     y1 = y_hi;
00199   }
00200 }
00201 
00202 void
00203 HippoRectangle::
00204 makeInBounds ( double & x1, double & y1, double & z1 ) const
00205 {
00206   double x_lo = m_origin.getX ();
00207   double y_lo = m_origin.getY ();
00208   double z_lo = m_origin.getZ ();
00209 
00210   double x_hi = x_lo + m_size.getWidth();
00211   double y_hi = y_lo + m_size.getHeight();
00212   double z_hi = z_lo + m_size.getDepth();
00213 
00214   x1 = max ( x1, x_lo );
00215   x1 = min ( x1, x_hi );
00216 
00217   y1 = max ( y1, y_lo );
00218   y1 = min ( y1, y_hi );
00219 
00220   z1 = max ( z1, z_lo );
00221   z1 = min ( z1, z_hi );
00222 }
00223 
00224 void
00225 HippoRectangle::
00226 makeInBounds ( std::vector< double > & x, 
00227                std::vector< double > & y  ) const
00228 {
00229   double lo = getX ();
00230   double hi = lo + getWidth ();
00231 
00232   replace_if ( x.begin (), x.end (),
00233                bind2nd ( less< double > (), lo ), lo );
00234 
00235   replace_if ( x.begin (), x.end (),
00236                bind2nd ( greater< double > (), hi ), hi );
00237 
00238   lo = getY ();
00239   hi = lo + getHeight ();
00240 
00241   replace_if ( y.begin (), y.end (),
00242                bind2nd ( less< double > (), lo ), lo );
00243 
00244   replace_if ( y.begin (), y.end (),
00245                bind2nd ( greater< double > (), hi ), hi );
00246 
00247 }
00248 
00249 void
00250 HippoRectangle::
00251 makeInBounds ( std::vector< double > & x, 
00252                std::vector< double > & y,
00253                std::vector< double > & z  ) const
00254 {
00255   double lo = getX ();
00256   double hi = lo + getWidth ();
00257 
00258   replace_if ( x.begin (), x.end (),
00259                bind2nd ( less< double > (), lo ), lo );
00260 
00261   replace_if ( x.begin (), x.end (),
00262                bind2nd ( greater< double > (), hi ), hi );
00263 
00264   lo = getY ();
00265   hi = lo + getHeight ();
00266 
00267   replace_if ( y.begin (), y.end (),
00268                bind2nd ( less< double > (), lo ), lo );
00269 
00270   replace_if ( y.begin (), y.end (),
00271                bind2nd ( greater< double > (), hi ), hi );
00272 
00273   lo = getZ ();
00274   hi = lo + getDepth ();
00275 
00276   replace_if ( z.begin (), z.end (),
00277                bind2nd ( less< double > (), lo ), lo );
00278 
00279   replace_if ( z.begin (), z.end (),
00280                bind2nd ( greater< double > (), hi ), hi );
00281 
00282 }
00283 
00284 std::ostream & operator << ( std::ostream & o, const HippoRectangle & rect )
00285 {
00286   o << "HippoRectangle = { x = "
00287     << rect.getX() << ", y = "
00288     << rect.getY() << ", w = "
00289     << rect.getWidth() << ", h = "
00290     << rect.getHeight() << "}";
00291   if( rect.getZ() > 0.0 )
00292     o << "Z = { " << rect.getZ() << ", " << rect.getDepth() << " }";
00293   return o;
00294 }

Generated for HippoDraw-1.14.8.5 by doxygen 1.4.3