Rectangle.cxx
Go to the documentation of this file.
1 
14 #ifdef _MSC_VER
15 // Include max() and min() missing from MicroSoft Visual C++.
16 #include "msdevstudio/MSconfig.h"
17 #endif //_MSC_VER
18 
19 #include "Rectangle.h"
20 
21 #ifdef _MSC_VER
22 #define isnan _isnan
23 #endif
24 
25 //To have isnan.
26 #ifdef __APPLE__
27 #define _GLIBCPP_USE_C99 1
28 #endif
29 
30 #include <algorithm>
31 #include <functional>
32 #include <iostream>
33 
34 #include <cmath>
35 #include <cfloat>
36 
37 #ifdef __APPLE__
38 using std::isnan;
39 #endif
40 
41 using std::bind2nd;
42 using std::greater;
43 using std::less;
44 using std::max;
45 using std::min;
46 using std::replace_if;
47 using std::vector;
48 
49 using namespace hippodraw;
50 
52  : m_origin(), m_size()
53 {
54 }
55 
56 Rect::
57 Rect( double x, double y, double width, double height )
58  : m_origin(), m_size()
59 {
60  setRect( x, y, width, height );
61 }
62 
63 Rect::Rect( double x, double y, double z,
64  double width, double height, double depth )
65  : m_origin(), m_size()
66 {
67  setRect( x, y, z, width, height, depth );
68 }
69 
70 void Rect::setRect( double x, double y, double width, double height )
71 {
72  m_origin.setPoint( x, y );
73  m_size.setSize( width, height );
74 }
75 
76 void Rect::setRect( double x, double y, double z,
77  double width, double height, double depth )
78 {
79  m_origin.setPoint( x, y, z );
80  m_size.setSize( width, height, depth );
81 }
82 
83 void Rect::moveBy ( double x, double y )
84 {
85  m_origin.moveBy ( x, y );
86 }
87 
88 const Point & Rect::getOrigin() const
89 {
90  return m_origin;
91 }
92 
93 const Size & Rect::getSize() const
94 {
95  return m_size;
96 }
97 
98 double Rect::getZ() const
99 {
100  return getOrigin().getZ();
101 }
102 
103 void Rect::setZ ( double z )
104 {
105  m_origin.setZ ( z );
106 }
107 
108 double Rect::getWidth() const
109 {
110  return m_size.getWidth();
111 }
112 
113 double Rect::getHeight() const
114 {
115  return m_size.getHeight();
116 }
117 
118 double Rect::getDepth() const
119 {
120  return getSize().getDepth();
121 }
122 
123 
124 void Rect::setDepth ( double d )
125 {
126  m_size.setDepth ( d );
127 }
128 
136 bool Rect::isInDepth ( double z1 ) const
137 {
138  bool yes = false;
139  double z_lo = m_origin.getZ ();
140 
141  if ( z1 >= z_lo &&
142  ( z1 ) <= z_lo + 1.00001*m_size.getDepth() ) yes = true;
143 
144  return yes;
145 }
146 
149 bool Rect::isInBounds ( double x1, double y1 ) const
150 {
151  if ( isnan ( x1 ) || isnan ( y1 ) ) return false;
152 
153  double x_lo = getX ();
154  double y_lo = getY ();
155 
156  double x_hi = x_lo + getWidth();
157  double y_hi = y_lo + getHeight();
158 
159  if( x1 < x_lo || x1 > x_hi ||
160  y1 < y_lo || y1 > y_hi ) return false;
161 
162  return true;
163 }
164 
165 bool Rect::isInBounds ( double x1, double y1, double z1 ) const
166 {
167  double x_lo = getX ();
168  double y_lo = getY ();
169  double z_lo = getZ ();
170 
171  double x_hi = x_lo + getWidth();
172  double y_hi = y_lo + getHeight();
173  double z_hi = z_lo + getDepth();
174 
175  if( x1 < x_lo || x1 > x_hi ||
176  y1 < y_lo || y1 > y_hi ||
177  z1 < z_lo || z1 > z_hi ) return false;
178 
179  return true;
180 }
181 
182 /* Note: the if statements were measured faster than the use of max()
183  and min().
184 */
185 void Rect::makeInBounds ( double & x1, double & y1 ) const
186 {
187  double x_lo = m_origin.getX ();
188  double y_lo = m_origin.getY ();
189 
190  double x_hi = x_lo + m_size.getWidth();
191  double y_hi = y_lo + m_size.getHeight();
192 
193  if ( x1 < x_lo ) {
194  x1 = x_lo;
195  }
196  else if ( x1 > x_hi ) {
197  x1 = x_hi;
198  }
199  if ( y1 < y_lo ) {
200  y1 = y_lo;
201  }
202  else if ( y1 > y_hi ) {
203  y1 = y_hi;
204  }
205 }
206 
207 void
208 Rect::
209 makeInBounds ( double & x1, double & y1, double & z1 ) const
210 {
211  double x_lo = m_origin.getX ();
212  double y_lo = m_origin.getY ();
213  double z_lo = m_origin.getZ ();
214 
215  double x_hi = x_lo + m_size.getWidth();
216  double y_hi = y_lo + m_size.getHeight();
217  double z_hi = z_lo + m_size.getDepth();
218 
219  x1 = max ( x1, x_lo );
220  x1 = min ( x1, x_hi );
221 
222  y1 = max ( y1, y_lo );
223  y1 = min ( y1, y_hi );
224 
225  z1 = max ( z1, z_lo );
226  z1 = min ( z1, z_hi );
227 }
228 
229 void
230 Rect::
231 makeInBounds ( std::vector< double > & x,
232  std::vector< double > & y ) const
233 {
234  double lo = getX ();
235  double hi = lo + getWidth ();
236 
237  replace_if ( x.begin (), x.end (),
238  bind2nd ( less< double > (), lo ), lo );
239 
240  replace_if ( x.begin (), x.end (),
241  bind2nd ( greater< double > (), hi ), hi );
242 
243  lo = getY ();
244  hi = lo + getHeight ();
245 
246  replace_if ( y.begin (), y.end (),
247  bind2nd ( less< double > (), lo ), lo );
248 
249  replace_if ( y.begin (), y.end (),
250  bind2nd ( greater< double > (), hi ), hi );
251 
252 }
253 
254 void
255 Rect::
256 makeInBounds ( std::vector< double > & x,
257  std::vector< double > & y,
258  std::vector< double > & z ) const
259 {
260  double lo = getX ();
261  double hi = lo + getWidth ();
262 
263  replace_if ( x.begin (), x.end (),
264  bind2nd ( less< double > (), lo ), lo );
265 
266  replace_if ( x.begin (), x.end (),
267  bind2nd ( greater< double > (), hi ), hi );
268 
269  lo = getY ();
270  hi = lo + getHeight ();
271 
272  replace_if ( y.begin (), y.end (),
273  bind2nd ( less< double > (), lo ), lo );
274 
275  replace_if ( y.begin (), y.end (),
276  bind2nd ( greater< double > (), hi ), hi );
277 
278  lo = getZ ();
279  hi = lo + getDepth ();
280 
281  replace_if ( z.begin (), z.end (),
282  bind2nd ( less< double > (), lo ), lo );
283 
284  replace_if ( z.begin (), z.end (),
285  bind2nd ( greater< double > (), hi ), hi );
286 
287 }

Generated for HippoDraw Class Library by doxygen