MapMatrixProjector.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 "MapMatrixProjector.h"
18 
19 #include "axes/AxisModelBase.h"
20 
22 #include "datasrcs/RTuple.h"
23 
24 #include <algorithm>
25 #include <numeric>
26 
27 #include <cfloat>
28 #include <climits>
29 #include <cmath>
30 
31 #include <cassert>
32 
33 using namespace hippodraw;
34 
35 #ifdef ITERATOR_MEMBER_DEFECT
36 using namespace std;
37 #else
38 using std::abs;
39 using std::accumulate;
40 using std::find;
41 using std::max;
42 using std::min;
43 using std::sqrt;
44 using std::string;
45 using std::vector;
46 #endif
47 
49  : NTupleProjector ( 2 ),
50  m_x_label ( "x" ),
51  m_y_label ( "y" ),
52  m_cols ( 0 ),
53  m_rows ( 0 ),
54  m_x_step ( 1.0 ),
55  m_y_step ( 1.0 ),
56  m_x_origin ( 0.0 ),
57  m_y_origin ( 0.0 ),
58  m_null_value ( 0.0 ),
59  m_transpose ( false )
60 {
61  m_binding_options.push_back ( "Z" );
62  m_min_bindings = 1;
63  addPointReps();
64 }
65 
72  : ProjectorBase ( projector ),
73  NTupleProjector( projector ),
74  m_x_label ( projector.m_x_label ),
75  m_y_label ( projector.m_y_label ),
76  m_cols ( projector.m_cols ),
77  m_rows ( projector.m_rows ),
78  m_x_step ( projector.m_x_step ),
79  m_y_step ( projector.m_y_step ),
80  m_x_origin ( projector.m_x_origin ),
81  m_y_origin ( projector.m_y_origin ),
82  m_null_value ( projector.m_null_value ),
83  m_transpose ( projector.m_transpose )
84 {
85  addPointReps();
86 }
87 
89 {
90  return new MapMatrixProjector( *this );
91 }
92 
93 void
95 setNumberOfBins ( hippodraw::Axes::Type axis, unsigned int number )
96 {
97  assert ( axis == Axes::X || axis == Axes::Y );
98 
99  if ( axis == Axes::X ) m_cols = number;
100  else m_rows = number;
101 }
102 
103 int
106 {
107  assert ( axis != Axes::Z );
108  int bins = axis == Axes::X ? m_cols : m_rows;
109 
110  return bins;
111 }
112 
113 const Range &
115 setBinWidth ( hippodraw::Axes::Type axis, double step )
116 {
117  if ( axis == Axes::X ) {
118  m_x_step = step;
119  }
120  else if ( axis == Axes::Y ) {
121  m_y_step = step;
122  }
123  else if ( axis == Axes::Z ) {
124  m_scale_factor = step;
125  }
126 
127  return getRange ( axis );
128 }
129 
130 double
133 {
134  if ( axis == Axes::X ) {
135  return m_x_step;
136  }
137  if ( axis == Axes::Y ) {
138  return m_y_step;
139  }
140  if ( axis == Axes::Z ) {
141  return m_scale_factor;
142  }
143  assert ( false );
144  return 0.0;
145 }
146 
147 void
149 setOffset ( hippodraw::Axes::Type axis, double origin )
150 {
151  if ( axis == Axes::X ) {
152  m_x_origin = origin;
153  return;
154  }
155  if ( axis == Axes::Y ) {
156  m_y_origin = origin;
157  return;
158  }
159  assert ( false );
160 }
161 
162 double
165 {
166  if ( axis == Axes::X ) {
167  return m_x_origin;
168  }
169  if ( axis == Axes::Y ) {
170  return m_y_origin;
171  }
172  assert ( false );
173  return 0.0;
174 }
175 
176 bool
178 inRange ( int row ) const
179 {
180  return inRangeWithZ ( row, true );
181 }
182 
183 bool
185 inRangeWithZ ( int row, bool use_z ) const
186 {
187  bool accept = true;
188 
189  std::size_t cindex = calcColumnIndex ( row );
190  double lvalue = m_x_origin + cindex * m_x_step;
191  const Range & x_range = m_x_axis -> getRange ( false );
192  bool in = x_range.includes ( lvalue ) ||
193  x_range.includes ( lvalue + m_x_step );
194  accept &= in;
195 
196  if ( accept ) {
197  std::size_t rindex = calcRowIndex ( row );
198  double bvalue = m_y_origin + rindex * m_y_step;
199  const Range & y_range = m_y_axis -> getRange ( false );
200  in = y_range.includes ( bvalue ) ||
201  y_range.includes ( bvalue + m_y_step );
202  accept &= in;
203  }
204 
205  if ( accept && use_z == true ) {
206  const Range & z_range = m_z_axis->getRange ( false );
207  double value = m_ntuple -> valueAt ( row, m_columns[0] );
208  accept &= z_range.includes ( value );
209  }
210 
211  return accept;
212 }
213 
215 {
216  unsigned int cols = m_ntuple->columns () - 1;
217  if ( m_columns[0] > cols ) m_columns[0] = cols;
218  if ( m_columns[1] > cols ) m_columns[1] = UINT_MAX;
219 }
220 
222 {
223  return dataRangeOn ( Axes::Z );
224 }
225 
226 namespace dp = hippodraw::DataPoint3DTuple;
227 
228 Range
231 {
232  MapMatrixProjector * mmp = const_cast < MapMatrixProjector *> ( this );
233  mmp -> prepareValues ();
234  if ( m_proj_values -> empty () ) {
235  return Range ( 0.0, 1.0, 0.5 );
236  }
237  const vector < double > & values = m_proj_values -> getColumn ( dp::Z );
238 
239  return Range ( values );
240 }
241 
242 Range
245 {
246  assert ( axis == Axes::X || axis == Axes::Y || axis == Axes::Z );
247 
248  Range range;
249 
250  if ( axis == Axes::X ) {
251  double len = m_x_origin + m_cols * m_x_step;
252  if ( m_x_step < 0. ) {
253  range.setRange ( len, m_x_origin, - m_x_step );
254  } else {
255  range.setRange ( m_x_origin, len, m_x_step );
256  }
257  }
258 
259  if ( axis == Axes::Y ) {
260  double len = m_y_origin + m_rows * m_y_step;
261  if ( m_y_step < 0. ) {
262  range.setRange ( len, m_y_origin, - m_y_step );
263  }
264  else {
265  range.setRange ( m_y_origin, len, m_y_step );
266  }
267  }
268 
269  if ( axis == Axes::Z ) {
270  range = dataRangeOnValue ();
271  }
272 
273  return range;
274 }
275 
276 /* @bug @@@@ This method can create low range > high range from
277  transform log log */
278 Range
281 {
282  Range range;
283  double low = DBL_MAX;
284  double pos = DBL_MAX;
285  double high = -DBL_MIN;
286 
287  if ( axis == Axes::Z ) {
288  std::size_t rows = m_ntuple -> rows ();
289  unsigned int used = 0;
290  for ( unsigned int row = 0; row < rows; row++ ) {
291  bool accept = inRangeWithZ ( row, false );
292  if ( accept ) {
293  double value = m_ntuple -> valueAt ( row, m_columns[0] );
294  low = std::min ( low, value );
295  if ( value > 0 ) {
296  pos = std::min ( pos, value );
297  }
298  high = std::max ( high, value );
299  used++;
300  }
301  }
302  range.setRange ( low, high, pos );
303  }
304  else {
305  range = ProjectorBase::preferredRange ( axis );
306  }
307 
308  return range;
309 }
310 double
313 {
314  assert ( axis == Axes::X || axis == Axes::Y || axis == Axes::Z );
315 
316  if ( axis == Axes::X ) {
317  return 0.5 * std::abs ( m_x_step );
318  }
319  if ( axis == Axes::Y ) {
320  return 0.5 * std::abs ( m_y_step );
321  }
322  if ( m_columns[1] == UINT_MAX ) {
323  return getPos ( m_columns[0] );
324  }
325  //It has to be Y with an error.
326  return getPosWithError ( m_columns[0], m_columns[1] );
327 }
328 
329 const string & MapMatrixProjector::getXLabel() const
330 {
331  return m_x_label;
332 }
333 
334 const string & MapMatrixProjector::getYLabel ( bool ) const
335 {
336  return m_y_label;
337 }
338 
339 const string &
341 getZLabel () const
342 {
343  return m_ntuple->getLabelAt( m_columns[0] );
344 }
345 
346 
351 double
354 {
355  MapMatrixProjector * p = const_cast < MapMatrixProjector * > ( this );
356  p -> prepareValues ();
357 
358  unsigned int col = 3; // bad value
359  switch ( axis ) {
360 
361  case Axes::X:
362  col = dp::X;
363  break;
364 
365  case Axes::Y:
366  col = dp::Y;
367  break;
368 
369  case Axes::Z:
370  col = dp::Z;
371  break;
372 
373  default:
374  break;
375  }
376  assert ( col < 3 );
377 
378  const DataSource * ntuple = getProjectedValues ();
379  const vector < double > & data = ntuple -> getColumn ( col );
380 
381  unsigned int size = ntuple -> rows ();
382 
383  double sum = 0.0;
384  sum = accumulate ( data.begin(), data.end(), sum );
385 
386  return sum / size;
387 }
388 
390 {
391  m_pointreps.push_back ( "ColorBox" );
392  m_pointreps.push_back ( "Contour" );
393 }
394 
395 void
397 setNTuple ( const DataSource * ntuple )
398 {
399  NTupleProjector::setNTuple ( ntuple );
400 
401  unsigned int size = ntuple->rows ();
402  double s = static_cast < double > ( size );
403  double side = sqrt ( s );
404 
405  m_rows = static_cast < unsigned int > ( side );
406  m_cols = static_cast < unsigned int > ( side );
407 
408  setDirty ();
409 }
410 
411 void
413 matrixTranspose ( bool yes )
414 {
415  m_transpose = yes;
416 }
417 
422 double
424 getZValue ( double x, double y ) const
425 {
426  double xx = ( x - m_x_origin ) / m_x_step;
427  double yy = ( y - m_y_origin ) / m_y_step;
428 
429  unsigned int i_x = static_cast < unsigned int> ( xx );
430  unsigned int i_y = static_cast < unsigned int> ( yy );
431 
432  unsigned int row;
433  if ( m_transpose ) {
434  row = i_x + m_cols * i_y;
435  }
436  else {
437  row = i_x * m_rows + i_y;
438  }
439 
440  unsigned int size = m_ntuple -> rows ();
441  double value = 0.0;
442 
443  if ( row < size ) {
444  value = m_ntuple -> valueAt ( row, m_columns[0] );
445  }
446  return value;
447 }
448 
449 DataSource *
451 createNTuple () const
452 {
453  unsigned int z_err = m_columns[1];
454 
455  unsigned int columns = 6;
456  RTuple * ntuple = new RTuple ( columns );
457 
458  // using setLabelAt save 2KB compared to setLabels()
459  ntuple -> setLabelAt ( getXLabel (), 0 );
460  ntuple -> setLabelAt ( getYLabel ( false ), 1 );
461  ntuple -> setLabelAt ( getZLabel (), 2 );
462  ntuple -> setLabelAt ( "Width", 3 );
463  ntuple -> setLabelAt ( "Height", 4 );
464  if ( z_err < UINT_MAX ) {
465  ntuple -> setLabelAt ( m_ntuple -> getLabelAt ( z_err ), 5 );
466  }
467  else {
468  ntuple -> setLabelAt ( "Error", 5 );
469  }
470 
471  fillProjectedValues ( ntuple );
472 
473  return ntuple;
474 }
475 
479 void
481 fillProjectedValues ( DataSource * ntuple, bool ) const // in_range ) const
482 {
483  ntuple -> clear();
484 
485  double width_x = m_x_step;
486  double next_x = m_x_origin + 0.5 * width_x;
487 
488  vector < double > row ( dp::SIZE );
489  row[dp::XERR] = abs ( m_x_step );
490  row[dp::YERR] = abs ( m_y_step );
491  row[dp::ZERR] = 1.0;
492 
493  unsigned int l = 0;
494  for ( unsigned int i = 0; i < m_cols; i++ ) {
495 
496  double x = next_x;
497  next_x += width_x;
498 
499  double width_y = m_y_step;
500  double next_y = m_y_origin + 0.5 * width_y;
501 
502  for ( unsigned int j = 0; j < m_rows; j++ ) {
503  double y = next_y;
504  next_y += width_y;
505 
506  // Calculate our own inex to get value because calling
507  // getValueAt takes too long
508  int index;
509  if ( m_transpose ) {
510  index = i + m_cols * j;
511  }
512  else {
513  index = i * m_rows + j;
514  }
515  double value = m_ntuple -> valueAt ( index, m_columns [0] );
516 
517 // if ( acceptRow ( l ) == false ||
518 // ( in_range == true && inRange ( l ) == false ) ) {
519 // l++;o
520 // continue;
521 // }
522  if ( acceptRow ( l, m_cut_list ) == true ) {
523  row[dp::Z] = value;
524  }
525  else {
526  row[dp::Z] = m_null_value;
527  }
528  row[dp::X] = x;
529  row[dp::Y] = y;
530 
531  ntuple -> addRow ( row );
532  l++;
533  }
534  }
535 
536  vector < unsigned int > shape ( 3 );
537  shape[0] = m_cols;
538  shape[1] = m_rows;
539  shape[2] = dp::SIZE;
540 
541  ntuple -> setShape ( shape );
542 }
543 
544 void
547 {
548  if ( m_proj_values == 0 ) {
550  }
551  else {
552  if ( isDirty () ) {
554  }
555  }
556 
557  setDirty ( false );
558 }
559 
560 bool
563 {
564  return true;
565 }

Generated for HippoDraw Class Library by doxygen