MeshProjector.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 // Include max() and min() missing from Microsoft Visual C++.
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "MeshProjector.h"
18 
19 #include "axes/AxisModelBase.h"
20 #include "axes/Range.h"
21 
23 #include "datasrcs/NTuple.h"
24 
25 #include <algorithm>
26 #include <cfloat>
27 #include <climits>
28 
29 #include <cassert>
30 
31 using namespace hippodraw;
32 
33 #ifdef ITERATOR_MEMBER_DEFECT
34 using namespace std;
35 #else
36 using std::find;
37 using std::max;
38 using std::min;
39 using std::string;
40 using std::vector;
41 #endif
42 
44  : NTupleProjector ( 5 ),
45  m_x_option ( "X width (optional)" ),
46  m_y_option ( "Y width (optional)" )
47 {
48  m_binding_options.push_back ( "X" );
49  m_binding_options.push_back ( "Y" );
50  m_binding_options.push_back ( "Z" );
51  m_binding_options.push_back ( "X width" );
52  m_binding_options.push_back ( "Y width" );
53  m_min_bindings = 5;
54  addPointReps();
55 }
56 
62 MeshProjector ( const MeshProjector & projector )
63  : ProjectorBase ( projector ),
64  NTupleProjector ( projector )
65 {
66  addPointReps();
67 }
68 
69 // For some reason, implementing empty destructor decrease code size
70 // by 5 kbytes.
72 {
73 }
74 
76 {
77  return new MeshProjector( *this );
78 }
79 
80 void MeshProjector::setXErrorOption ( bool enable )
81 {
82  const string name ( m_x_option );
83  vector< string >:: iterator first
84  = find ( m_binding_options.begin (),
85  m_binding_options.end (),
86  name );
87 
88  if ( first != m_binding_options.end () && !enable ) {
89  m_binding_options.erase ( first );
90  m_columns[3] = UINT_MAX;
91  }
92  else if ( enable ) {
93  m_binding_options.push_back ( name );
94  }
95 }
96 
99 void MeshProjector::setYErrorOption ( bool enable )
100 {
101  const string name ( m_y_option );
102  vector< string >:: iterator first
103  = find ( m_binding_options.begin (),
104  m_binding_options.end (),
105  name );
106  if ( first != m_binding_options.end () && !enable ) {
107  m_binding_options.erase ( first );
108  m_columns[4] = UINT_MAX;
109  }
110  else if ( enable ) {
111  m_binding_options.push_back ( name );
112  }
113 }
114 
116 {
117  unsigned int cols = m_ntuple->columns () - 1;
118  if ( m_columns[0] > cols ) m_columns[0] = cols;
119  if ( m_columns[1] > cols ) m_columns[1] = cols;
120  if ( m_columns[2] > cols ) m_columns[2] = cols;
121  if ( m_columns[3] > cols ) m_columns[3] = cols;
122  if ( m_columns[4] > cols ) m_columns[4] = cols;
123 }
124 
126 {
127  return dataRangeOn ( Axes::Z );
128 }
129 
130 Range
133 {
134  assert ( axis == Axes::X || axis == Axes::Y || axis == Axes::Z );
135 
136  if ( axis == Axes::X ) {
137  if ( m_columns[3] == UINT_MAX ) {
138  return dataRange ( m_columns[0] );
139  } else {
140  return dataRangeWithError ( m_columns[0], m_columns[3] );
141  }
142  }
143  if ( axis == Axes::Y ) {
144  if ( m_columns[4] == UINT_MAX ) {
145  return dataRange ( m_columns[1] );
146  }
147  else {
148  return dataRangeWithError ( m_columns[1], m_columns[4] );
149  }
150  }
151  // has to be Z
152  return dataRangeOnValue ( );
153 }
154 
155 namespace dp = hippodraw::DataPoint3DTuple;
156 
157 Range
160 {
161  MeshProjector * mp = const_cast < MeshProjector * > ( this );
162  mp -> prepareValues ();
163  if ( m_proj_values -> empty () ) {
164  return Range ( 0.0, 1.0, 0.5 );
165  }
166  const vector < double > & values = m_proj_values -> getColumn ( dp::Z );
167 
168  return Range ( values );
169 }
170 
171 double
174 {
175  assert ( axis == Axes::X || axis == Axes::Y || axis == Axes::Z );
176 
177  if ( axis == Axes::X ) {
178  if ( m_columns[3] == UINT_MAX ) { // Was m_columns[2], should be a bug
179  return getPos ( m_columns[0] );
180  } else {
181  return getPosWithError ( m_columns[0], m_columns[3] );
182  }
183  }
184  if ( axis == Axes::Y ) {
185  if ( m_columns[4] == UINT_MAX ) { // Was m_coloumns[3], should be a bug
186  return getPos ( m_columns[1] );
187  }
188  else {
189  return getPosWithError ( m_columns[1], m_columns[4] );
190  }
191  }
192  // has to be Z
193  return getPos ( m_columns[2] );
194 }
195 
197 {
198  m_pointreps.push_back ( "ColorBox" );
199 }
200 
201 DataSource *
203 createNTuple () const
204 {
205 
206  unsigned int x_col = m_columns[0];
207  unsigned int y_col = m_columns[1];
208  unsigned int z_col = m_columns[2]; // Was 3, should be a bug
209 
210  unsigned int x_err = m_columns[3];
211  unsigned int y_err = m_columns[4];
212 
213  unsigned int columns = dp::SIZE;
214  NTuple * ntuple = new NTuple ( columns );
215 
216  vector < string > labels;
217  labels.push_back ( m_ntuple -> getLabelAt ( x_col ) );
218  labels.push_back ( m_ntuple -> getLabelAt ( y_col ) );
219  labels.push_back ( m_ntuple -> getLabelAt ( z_col ) );
220 
221  if ( x_err < UINT_MAX ) {
222  labels.push_back ( m_ntuple -> getLabelAt ( x_err ) );
223  } else {
224  labels.push_back ( dp::XWIDTH );
225  }
226 
227  if ( y_err < UINT_MAX ) {
228  labels.push_back ( m_ntuple -> getLabelAt ( y_err ) );
229  } else {
230  labels.push_back ( dp::YWIDTH );
231  }
232  labels.push_back ( " z error" ); // for z error
233 
234  ntuple->setLabels ( labels );
235 
236  unsigned int size = m_ntuple -> rows ();
237  ntuple -> reserve ( size );
238 
239  fillProjectedValues ( ntuple );
240 
241  return ntuple;
242 }
243 
251 void
253 fillProjectedValues ( DataSource * ntuple, bool in_range ) const
254 {
255  ntuple -> clear ();
256 
257  unsigned int x_col = m_columns[0];
258  unsigned int y_col = m_columns[1];
259  unsigned int z_col = m_columns[2];
260 
261  unsigned int x_err = m_columns[3];
262  unsigned int y_err = m_columns[4];
263 
264  const vector < string > & labels = m_ntuple -> getLabels ();
265  unsigned int size = labels.size();
266  if ( size > 3 ) {
267  if ( x_err == UINT_MAX &&
268  labels [ dp::XERR ] == dp::XWIDTH ) x_err = dp::XERR;
269  if ( size > 3 ) {
270  if ( y_err == UINT_MAX &&
271  labels [ dp::YERR ] == dp::YWIDTH ) y_err = dp::YERR;
272  }
273  }
274  size = m_ntuple -> rows ();
275  vector < double > row ( dp::SIZE );
276  for ( unsigned int i = 0; i < size; i++ ) {
277  if ( acceptRow ( i, m_cut_list ) == false ||
278  ( in_range == true && inRange ( i ) == false ) ) continue;
279 
280  row[dp::X] = m_ntuple -> valueAt ( i, x_col );
281  row[dp::Y] = m_ntuple -> valueAt ( i, y_col );
282  row[dp::Z] = m_ntuple -> valueAt ( i, z_col );
283 
284 
285  double xe
286  = x_err < UINT_MAX ? m_ntuple -> valueAt ( i, x_err ) : 0.0;
287  double ye
288  = y_err < UINT_MAX ? m_ntuple -> valueAt( i, y_err ) : 0.0;
289 
290  row[dp::XERR] = xe;
291  row[dp::YERR] = ye;
292 
293  ntuple -> addRow ( row );
294  }
295 }
296 
297 void
300 {
301  if ( m_proj_values == 0 ) {
303  }
304  else if ( isDirty () ) {
306  }
307 
308  setDirty ( false );
309 }
310 
311 bool
313 inRangeWithZ ( int row, bool with_z ) const
314 {
315  bool accept = true;
316 
317  for ( unsigned int i = 0; i < 2; i++ ) {
318  AxisModelBase * model = i == 0 ? m_x_axis : m_y_axis;
319  const Range & range = model -> getRange ( false );
320  unsigned int vcolumn = m_columns[i];
321  unsigned int wcolumn = m_columns[i+3];
322  double value = m_ntuple -> valueAt ( row, vcolumn );
323  double width = m_ntuple -> valueAt ( row, wcolumn );
324  bool in = range.includes ( value + width ) ||
325  range.includes ( value - width );
326  accept &= in;
327  }
328  if ( with_z ) {
329  const Range & range = m_z_axis -> getRange ( false );
330  double value = m_ntuple -> valueAt ( row, m_columns[2] );
331  bool in = range.includes ( value );
332  accept &= in;
333  }
334 
335  return accept;
336 }
337 
338 bool
340 inRange ( int row ) const
341 {
342  return inRangeWithZ ( row, true );
343 }
344 
345 Range
348 {
349  Range range;
350  double low = DBL_MAX;
351  double pos = DBL_MAX;
352  double high = -DBL_MIN;
353  if ( axis == Axes::Z ) {
354  std::size_t rows = m_ntuple -> rows ();
355  for ( unsigned int row = 0; row < rows; row++ ) {
356  bool accept = inRangeWithZ ( row, false );
357  if ( accept ) {
358  double value = m_ntuple -> valueAt ( row, m_columns[2] );
359  low = std::min ( low, value );
360  if ( value > 0 ) {
361  pos = std::min ( pos, value );
362  }
363  high = std::max ( high, value );
364  }
365  }
366  range.setRange ( low, high, pos );
367  }
368  else {
369  range = ProjectorBase::preferredRange ( axis );
370  }
371 
372  return range;
373 }
374 
375 
376 const string & MeshProjector::getZLabel () const
377 {
378  return m_proj_values->getLabelAt ( dp::Z );
379 }
380 
381 
386 double MeshProjector::getZValue ( double x, double y ) const
387 {
388 
389  double retval = 0;
390 
391  const vector < double > & xs = m_proj_values -> getColumn ( dp::X );
392  const vector < double > & ys = m_proj_values -> getColumn ( dp::Y );
393  const vector < double > & zs = m_proj_values -> getColumn ( dp::Z );
394  const vector < double > & xerr = m_proj_values -> getColumn ( dp::XERR );
395  const vector < double > & yerr = m_proj_values -> getColumn ( dp::YERR );
396 
397  unsigned int size = xs.size();
398  for ( unsigned int i = 0; i < size; i++ ) {
399  if ( x>xs[i]-xerr[i] && x<xs[i]+xerr[i] &&
400  y>ys[i]-yerr[i] && y<ys[i]+yerr[i] ) {
401  retval = zs[i];
402 
403  // Assume (x,y) can not be in another box
404  break;
405  }
406  }
407 
408  return retval;
409 
410 
411  /* TO REMOVE: Old algorithm, search for the nestest data point.
412 
413  const Range & xr = m_x_axis->getRange ( true );
414  const Range & yr = m_y_axis->getRange ( true );
415 
416  double xe = 0.1 * xr.length();
417  double ye = 0.1 * yr.length();
418  double distanceSquare = xe*xe+ye*ye;
419 
420  if ( (x-xs[i])*(x-xs[i])+(y-ys[i])*(y-ys[i]) < distanceSquare) {
421  // Update the nearest point info.
422  distanceSquare = (x-xs[i])*(x-xs[i])+(y-ys[i])*(y-ys[i]);
423  retval = zs[i];
424  }
425  */
426 }

Generated for HippoDraw Class Library by doxygen