00001
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #else
00015
00016 #ifdef _MSC_VER
00017
00018 #include "msdevstudio/MSconfig.h"
00019 #endif
00020
00021 #endif
00022
00023 #include "Map2Projector.h"
00024
00025 #include "axes/Range.h"
00026
00027 #include "datasrcs/DataPointTuple.h"
00028 #include "datasrcs/NTuple.h"
00029
00030 #include <algorithm>
00031
00032 #include <cfloat>
00033
00034 #include <cassert>
00035
00036 using namespace hippodraw;
00037
00038 #ifdef ITERATOR_MEMBER_DEFECT
00039 using namespace std;
00040 #else
00041 using std::find;
00042 using std::max;
00043 using std::min;
00044 using std::string;
00045 using std::vector;
00046 #endif
00047
00048 Map2Projector::Map2Projector ( )
00049 : NTupleProjector ( 4 ),
00050 m_x_option ( "X error (optional)" ),
00051 m_y_option ( "Y error (optional)" )
00052 {
00053 m_binding_options.push_back ( "X" );
00054 m_binding_options.push_back ( "Y" );
00055 m_min_bindings = 2;
00056 addPointReps();
00057 }
00058
00059 Map2Projector::Map2Projector ( const Map2Projector & projector )
00060 : NTupleProjector ( projector )
00061 {
00062 addPointReps();
00063 }
00064
00065 Map2Projector::~Map2Projector()
00066 {
00067 }
00068
00069 ProjectorBase * Map2Projector::clone()
00070 {
00071 return new Map2Projector( *this );
00072 }
00073
00074 void Map2Projector::setXErrorOption ( bool enable )
00075 {
00076 const string name ( m_x_option );
00077 vector< string >:: iterator first
00078 = find ( m_binding_options.begin (),
00079 m_binding_options.end (),
00080 name );
00081
00082 if ( first != m_binding_options.end () && !enable ) {
00083 m_binding_options.erase ( first );
00084 m_columns[2] = UINT_MAX;
00085 }
00086 else if ( enable ) {
00087 m_binding_options.push_back ( name );
00088 }
00089 }
00090
00093 void Map2Projector::setYErrorOption ( bool enable )
00094 {
00095 const string name ( m_y_option );
00096 vector< string >:: iterator first
00097 = find ( m_binding_options.begin (),
00098 m_binding_options.end (),
00099 name );
00100 if ( first != m_binding_options.end () && !enable ) {
00101 m_binding_options.erase ( first );
00102 m_columns[3] = UINT_MAX;
00103 }
00104 else if ( enable ) {
00105 m_binding_options.push_back ( name );
00106 }
00107 }
00108
00109 void Map2Projector::changedNTuple()
00110 {
00111 unsigned int cols = m_ntuple->columns () - 1;
00112 if ( m_columns[0] > cols ) m_columns[0] = cols;
00113 if ( m_columns[1] > cols ) m_columns[1] = cols;
00114 if ( m_columns[2] > cols ) m_columns[2] = UINT_MAX;
00115 if ( m_columns[3] > cols ) m_columns[3] = UINT_MAX;
00116 }
00117
00118 Range Map2Projector::valueRange () const
00119 {
00120 return dataRangeOn ( Axes::Y );
00121 }
00122
00123 Range
00124 Map2Projector::
00125 dataRangeOn ( hippodraw::Axes::Type axis ) const
00126 {
00127 assert ( axis == Axes::X || axis == Axes::Y );
00128
00129 if ( axis == Axes::X ) {
00130 if ( m_columns[2] == UINT_MAX ) {
00131 return dataRange ( m_columns[0] );
00132 } else {
00133 return dataRangeWithError ( m_columns[0], m_columns[2] );
00134 }
00135 }
00136
00137 if ( m_columns[3] == UINT_MAX ) {
00138 return dataRange ( m_columns[1] );
00139 }
00140
00141 return dataRangeWithError ( m_columns[1], m_columns[3] );
00142 }
00143
00144 double
00145 Map2Projector::
00146 getPosOn ( hippodraw::Axes::Type axis ) const
00147 {
00148 assert ( axis == Axes::X || axis == Axes::Y );
00149
00150 if ( axis == Axes::X ) {
00151 if ( m_columns[2] == UINT_MAX ) {
00152 return getPos ( m_columns[0] );
00153 } else {
00154 return getPosWithError ( m_columns[0], m_columns[2] );
00155 }
00156 }
00157
00158 if ( m_columns[3] == UINT_MAX ) {
00159 return getPos ( m_columns[1] );
00160 }
00161
00162 return getPosWithError ( m_columns[1], m_columns[3] );
00163 }
00164
00165 void Map2Projector::addPointReps()
00166 {
00167 m_pointreps.push_back ( "Symbol" );
00168 m_pointreps.push_back ( "Line" );
00169 m_pointreps.push_back ( "Column" );
00170 }
00171
00172 namespace dp = hippodraw::DataPoint2DTuple;
00173
00174 DataSource *
00175 Map2Projector::
00176 createNTuple () const
00177 {
00178
00179 unsigned int x_col = m_columns[0];
00180 unsigned int y_col = m_columns[1];
00181
00182 unsigned int x_err = m_columns[2];
00183 unsigned int y_err = m_columns[3];
00184
00185 unsigned int columns = 4;
00186 NTuple * ntuple = new NTuple ( columns );
00187
00188 vector < string > labels;
00189 labels.push_back ( m_ntuple -> getLabelAt ( x_col ) );
00190 labels.push_back ( m_ntuple -> getLabelAt ( y_col ) );
00191
00192 if ( x_err < UINT_MAX ) {
00193 labels.push_back ( m_ntuple -> getLabelAt ( x_err ) );
00194 } else {
00195 labels.push_back ( dp::WIDTH );
00196 }
00197
00198 if ( y_err < UINT_MAX ) {
00199 labels.push_back ( m_ntuple -> getLabelAt ( y_err ) );
00200 } else {
00201 labels.push_back ( dp::ERROR );
00202 }
00203 ntuple->setLabels ( labels );
00204
00205 unsigned int size = m_ntuple -> rows ();
00206 ntuple -> reserve ( size );
00207
00208 fillProjectedValues ( ntuple );
00209
00210 return ntuple;
00211 }
00212
00220 void
00221 Map2Projector::
00222 fillProjectedValues ( DataSource * ntuple, bool in_range ) const
00223 {
00224 ntuple -> clear ();
00225
00226 unsigned int x_col = m_columns[0];
00227 unsigned int y_col = m_columns[1];
00228
00229 unsigned int x_err = m_columns[2];
00230 unsigned int y_err = m_columns[3];
00231
00232 const vector < string > & labels = m_ntuple -> getLabels ();
00233 unsigned int size = labels.size();
00234 if ( size > 2 ) {
00235 if ( x_err == UINT_MAX &&
00236 labels [ dp::XERR ] == dp::WIDTH ) x_err = dp::XERR;
00237 if ( size > 3 ) {
00238 if ( y_err == UINT_MAX &&
00239 labels [ dp::YERR ] == dp::ERROR ) y_err = dp::YERR;
00240 }
00241 }
00242 size = m_ntuple -> rows ();
00243 vector < double > row ( dp::SIZE );
00244 for ( unsigned int i = 0; i < size; i++ ) {
00245 if ( acceptRow ( i ) == false ||
00246 ( in_range == true && inRange ( i ) == false ) ) continue;
00247
00248 row[dp::X] = m_ntuple -> valueAt ( i, x_col );
00249 row[dp::Y] = m_ntuple -> valueAt ( i, y_col );
00250
00251
00252 double xe
00253 = x_err < UINT_MAX ? m_ntuple -> valueAt ( i, x_err ) : 0.0;
00254 double ye
00255 = y_err < UINT_MAX ? m_ntuple -> valueAt( i, y_err ) : 0.0;
00256
00257 row[dp::XERR] = xe;
00258 row[dp::YERR] = ye;
00259
00260 ntuple -> addRow ( row );
00261 }
00262 }
00263
00264 void
00265 Map2Projector::
00266 fillDataSource ( DataSource * ntuple, bool in_range ) const
00267 {
00268 ntuple -> clear ();
00269
00270 unsigned int x_col = m_columns[0];
00271 unsigned int y_col = m_columns[1];
00272
00273 unsigned int x_err = m_columns[2];
00274 unsigned int y_err = m_columns[3];
00275
00276 const vector < string > & labels = m_ntuple -> getLabels ();
00277 unsigned int size = labels.size();
00278 if ( size > 2 ) {
00279 if ( x_err == UINT_MAX &&
00280 labels [ dp::XERR ] == dp::WIDTH ) x_err = dp::XERR;
00281 if ( size > 3 ) {
00282 if ( y_err == UINT_MAX &&
00283 labels [ dp::YERR ] == dp::ERROR ) y_err = dp::YERR;
00284 }
00285 }
00286 size = m_ntuple -> rows ();
00287 vector < double > row ( dp::SIZE );
00288 for ( unsigned int i = 0; i < size; i++ ) {
00289 if ( acceptRow ( i ) == false ||
00290 ( in_range == true && inRange ( i ) == false ) ) continue;
00291
00292 row[dp::X] = m_ntuple -> valueAt ( i, x_col );
00293 row[dp::Y] = m_ntuple -> valueAt ( i, y_col );
00294
00295
00296 double xe
00297 = x_err < UINT_MAX ? m_ntuple -> valueAt ( i, x_err ) : 0.0;
00298 double ye
00299 = y_err < UINT_MAX ? m_ntuple -> valueAt( i, y_err ) : 0.0;
00300
00301 row[dp::XERR] = xe;
00302 row[dp::YERR] = ye;
00303
00304 ntuple -> addRow ( row );
00305 }
00306 }
00307
00308 void
00309 Map2Projector::
00310 prepareValues ()
00311 {
00312 if ( m_proj_values == 0 ) {
00313 m_proj_values = createNTuple ();
00314 }
00315 else if ( isDirty () ) {
00316 fillProjectedValues ( m_proj_values, false );
00317 }
00318
00319 setDirty ( false );
00320 }