00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef PLOTDATA_H_
00028 #define PLOTDATA_H_
00029
00030 #include <graphics/GenericPlotter/PlotOptions.h>
00031
00032 #include <casa/Arrays/Matrix.h>
00033 #include <casa/Arrays/Vector.h>
00034
00035 #include <casa/BasicSL/String.h>
00036 #include <casa/Utilities/CountedPtr.h>
00037
00038 #include <casa/namespace.h>
00039 using namespace std;
00040
00041 namespace casa {
00042
00043
00044 typedef pair<double, double> ppoint_t;
00045
00047
00049
00050
00051
00052
00053
00054
00055 class PlotData {
00056 public:
00057 PlotData() { }
00058
00059 virtual ~PlotData() { }
00060
00061
00062
00063
00064
00065 virtual bool isValid() const = 0;
00066
00067
00068
00069 virtual bool willDeleteData() const = 0;
00070
00071
00072
00073 virtual void setDeleteData(bool del = true) = 0;
00074 };
00075 typedef CountedPtr<PlotData> PlotDataPtr;
00076
00077
00078
00079 class PlotSingleData : public virtual PlotData {
00080 public:
00081 PlotSingleData() { }
00082
00083 virtual ~PlotSingleData() { }
00084
00085
00086
00087
00088
00089 virtual unsigned int size() const = 0;
00090
00091
00092 virtual double at(unsigned int i) const = 0;
00093
00094
00095 virtual bool minMax(double& min, double& max) = 0;
00096 };
00097 INHERITANCE_POINTER2(PlotSingleData, PlotSingleDataPtr, PlotData, PlotDataPtr)
00098
00099
00100
00101
00102 class PlotPointData : public virtual PlotData {
00103 public:
00104 PlotPointData() { }
00105
00106 virtual ~PlotPointData() { }
00107
00108
00109
00110
00111
00112 virtual unsigned int size() const = 0;
00113
00114
00115 virtual double xAt(unsigned int i) const = 0;
00116
00117
00118 virtual double yAt(unsigned int i) const = 0;
00119
00120
00121 virtual bool minsMaxes(double& xMin, double& xMax, double& yMin,
00122 double& yMax) = 0;
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 virtual void xAndYAt(unsigned int index, double& x, double& y) const;
00133 };
00134 INHERITANCE_POINTER2(PlotPointData, PlotPointDataPtr, PlotData, PlotDataPtr)
00135
00136
00137
00138 class PlotMaskedPointData : public virtual PlotPointData {
00139 public:
00140 PlotMaskedPointData() { }
00141
00142 virtual ~PlotMaskedPointData() { }
00143
00144
00145
00146
00147
00148 virtual unsigned int sizeMasked() const = 0;
00149
00150
00151 virtual unsigned int sizeUnmasked() const = 0;
00152
00153
00154 virtual bool maskedAt(unsigned int index) const = 0;
00155
00156
00157 virtual bool maskedMinsMaxes(double& xMin, double& xMax, double& yMin,
00158 double& yMax) = 0;
00159
00160
00161 virtual bool unmaskedMinsMaxes(double& xMin, double& xMax, double& yMin,
00162 double& yMax) = 0;
00163
00164
00165
00166
00167
00168
00169 virtual void xyAndMaskAt(unsigned int index, double& x, double& y,
00170 bool& mask) const;
00171 };
00172 INHERITANCE_POINTER(PlotMaskedPointData, PlotMaskedPointDataPtr, PlotPointData,
00173 PlotPointDataPtr, PlotData, PlotDataPtr)
00174
00175
00176
00177 class PlotErrorData : public virtual PlotPointData {
00178 public:
00179 PlotErrorData() { }
00180
00181 virtual ~PlotErrorData() { }
00182
00183
00184
00185
00186
00187 virtual double xLeftErrorAt(unsigned int i) const = 0;
00188
00189
00190 virtual double xRightErrorAt(unsigned int i) const = 0;
00191
00192
00193 virtual double yBottomErrorAt(unsigned int i) const = 0;
00194
00195
00196 virtual double yTopErrorAt(unsigned int i) const = 0;
00197
00198
00199 virtual bool errorMaxes(double& xLeft, double& xRight, double& yBottom,
00200 double& yTop) = 0;
00201
00202
00203
00204
00205
00206
00207 virtual void xyAndErrorsAt(unsigned int index, double& x, double& y,
00208 double& xLeftError, double& xRightError, double& yBottomError,
00209 double& yTopError) const;
00210 };
00211 INHERITANCE_POINTER(PlotErrorData, PlotErrorDataPtr, PlotPointData,
00212 PlotDataPtr, PlotData, PlotDataPtr)
00213
00214
00215
00216
00217 class PlotBinnedData : public virtual PlotPointData {
00218 public:
00219
00220 PlotBinnedData() { }
00221
00222
00223 virtual ~PlotBinnedData() { }
00224
00225
00226
00227
00228
00229 virtual unsigned int numBins() const = 0;
00230
00231
00232
00233 virtual unsigned int binAt(unsigned int i) const = 0;
00234
00235
00236
00237
00238
00239 virtual bool isBinned() const { return numBins() > 1; }
00240 };
00241 INHERITANCE_POINTER(PlotBinnedData, PlotBinnedDataPtr, PlotPointData,
00242 PlotDataPtr, PlotData, PlotDataPtr)
00243
00244
00245
00246
00247 class PlotRasterData : public virtual PlotData {
00248 public:
00249
00250 enum Format {
00251 RGB32,
00252 ARGB32,
00253 SPECTROGRAM
00254 };
00255
00256
00257
00258 enum Origin {
00259 LLEFT, LRIGHT, ULEFT, URIGHT
00260 };
00261
00262
00263 PlotRasterData() { }
00264
00265 virtual ~PlotRasterData() { }
00266
00267
00268
00269
00270
00271 virtual Origin origin() const = 0;
00272
00273
00274 virtual void setOrigin(Origin o) = 0;
00275
00276
00277 virtual prange_t xRange() const = 0;
00278
00279
00280 virtual prange_t yRange() const = 0;
00281
00282
00283 virtual void setXRange(double from, double to) = 0;
00284
00285
00286 virtual void setYRange(double from, double to) = 0;
00287
00288
00289 virtual prange_t valueRange() const = 0;
00290
00291
00292 virtual double valueAt(double x, double y) const = 0;
00293
00294
00295 virtual vector<double>* colorBarValues(unsigned int max = 1000) const = 0;
00296 };
00297 INHERITANCE_POINTER2(PlotRasterData, PlotRasterDataPtr, PlotData, PlotDataPtr)
00298
00299
00300
00301
00303
00304
00305
00306
00307
00308 template <class T>
00309 class PlotSingleDataImpl : public virtual PlotSingleData {
00310 public:
00311
00312 PlotSingleDataImpl(): m_vector(NULL), m_cvector(NULL), m_array(NULL),
00313 m_arraySize(0), m_shouldDelete(false) { }
00314
00315
00316
00317 PlotSingleDataImpl(vector<T>& value, bool shouldDelete = false):
00318 m_vector(&value), m_cvector(NULL), m_array(NULL), m_arraySize(0),
00319 m_shouldDelete(shouldDelete) {
00320 recalculateMinMax(); }
00321 PlotSingleDataImpl(Vector<T>& value, bool shouldDelete = false):
00322 m_vector(NULL), m_cvector(&value), m_array(NULL), m_arraySize(0),
00323 m_shouldDelete(shouldDelete) {
00324 recalculateMinMax(); }
00325 PlotSingleDataImpl(T*& value, unsigned int size, bool shouldDelete= false):
00326 m_vector(NULL), m_cvector(NULL), m_array(value), m_arraySize(size),
00327 m_shouldDelete(shouldDelete) {
00328 recalculateMinMax(); }
00329
00330
00331
00332 ~PlotSingleDataImpl() {
00333 if(m_shouldDelete) {
00334 if(m_vector != NULL) delete m_vector;
00335 if(m_cvector != NULL) delete m_cvector;
00336 if(m_array != NULL) delete m_array;
00337 }
00338 }
00339
00340
00341
00342 bool isValid() const {
00343 return m_vector != NULL || m_cvector != NULL || m_array != NULL; }
00344
00345
00346 bool willDeleteData() const { return m_shouldDelete; }
00347
00348
00349 void setDeleteData(bool del = true) { m_shouldDelete = del; }
00350
00351
00352 unsigned int size() const {
00353 if(m_vector != NULL) return m_vector->size();
00354 if(m_cvector != NULL) return m_cvector->size();
00355 if(m_array != NULL) return m_arraySize;
00356 return 0;
00357 }
00358
00359
00360 double at(unsigned int i) const {
00361 if(m_vector != NULL) return (double)(*m_vector)[i];
00362 if(m_cvector != NULL) return (double)(*m_cvector)[i];
00363 if(m_array != NULL) return (double)m_array[i];
00364 return 0;
00365 }
00366
00367
00368 bool minMax(double& min, double& max) {
00369 if(!isValid() || size() == 0) return false;
00370 min = m_min; max = m_max;
00371 return true;
00372 }
00373
00374
00375
00376 void recalculateMinMax() {
00377 if(!isValid()) return;
00378 unsigned int n = size();
00379 if(n == 0) return;
00380 double temp = (double)at(0);
00381 m_min = m_max = temp;
00382 if(m_vector != NULL) {
00383 for(unsigned int i = 1; i < n; i++) {
00384 temp = (double)(*m_vector)[i];
00385 if(temp < m_min) m_min = temp;
00386 if(temp > m_max) m_max = temp;
00387 }
00388 } else if(m_cvector != NULL) {
00389 for(unsigned int i = 1; i < n; i++) {
00390 temp = (double)(*m_cvector)[i];
00391 if(temp < m_min) m_min = temp;
00392 if(temp > m_max) m_max = temp;
00393 }
00394 } else if(m_array != NULL) {
00395 for(unsigned int i = 1; i < n; i++) {
00396 temp = (double)m_array[i];
00397 if(temp < m_min) m_min = temp;
00398 if(temp > m_max) m_max = temp;
00399 }
00400 }
00401 }
00402
00403 private:
00404 vector<T>* m_vector;
00405 Vector<T>* m_cvector;
00406 T* m_array;
00407 unsigned int m_arraySize;
00408 bool m_shouldDelete;
00409 double m_min, m_max;
00410 };
00411
00412 typedef PlotSingleDataImpl<int> PlotSingleIntData;
00413 typedef PlotSingleDataImpl<unsigned int> PlotSingleUIntData;
00414 typedef PlotSingleDataImpl<float> PlotSingleFloatData;
00415 typedef PlotSingleDataImpl<double> PlotSingleDoubleData;
00416
00417
00418
00419
00420
00421
00422
00423 template <class T>
00424 class PlotPointDataImpl : public virtual PlotPointData {
00425 public:
00426
00427
00428 PlotPointDataImpl(vector<T>& x, vector<T>& y, bool shouldDelete = false) :
00429 m_xData(x, shouldDelete), m_yData(y, shouldDelete) { }
00430 PlotPointDataImpl(Vector<T>& x, Vector<T>& y, bool shouldDelete = false) :
00431 m_xData(x, shouldDelete), m_yData(y, shouldDelete) { }
00432 PlotPointDataImpl(T*& x, T*& y, unsigned int size, bool shouldDel = false):
00433 m_xData(x, size, shouldDel), m_yData(y, size, shouldDel) { }
00434
00435
00436
00437
00438 PlotPointDataImpl(vector<T>& y, bool shouldDelete = false) :
00439 m_yData(y, shouldDelete) { }
00440 PlotPointDataImpl(Vector<T>& y, bool shouldDelete = false) :
00441 m_yData(y, shouldDelete) { }
00442 PlotPointDataImpl(T*& y, unsigned int size, bool shouldDel = false):
00443 m_yData(y, size, shouldDel) { }
00444
00445
00446 virtual ~PlotPointDataImpl() { }
00447
00448
00449
00450 bool isValid() const { return m_yData.isValid(); }
00451
00452
00453 virtual bool willDeleteData() const {
00454 return (m_yData.isValid() && m_yData.willDeleteData()) &&
00455 (!m_xData.isValid() || m_xData.willDeleteData());
00456 }
00457
00458
00459 virtual void setDeleteData(bool del = true) {
00460 if(m_xData.isValid()) m_xData.setDeleteData(del);
00461 if(m_yData.isValid()) m_yData.setDeleteData(del);
00462 }
00463
00464
00465 unsigned int size() const {
00466 if(!m_xData.isValid()) return m_yData.size();
00467 else return min(m_xData.size(), m_yData.size());
00468 }
00469
00470
00471
00472 double xAt(unsigned int i) const {
00473 if(m_xData.isValid()) return m_xData.at(i);
00474 else return i;
00475 }
00476
00477
00478 double yAt(unsigned int i) const { return m_yData.at(i); }
00479
00480
00481 bool minsMaxes(double& xMin, double& xMax, double& yMin, double& yMax) {
00482 if(!m_xData.isValid()) {
00483 xMin = 0;
00484 xMax = m_yData.size();
00485 return m_yData.minMax(yMin, yMax);
00486 } else {
00487 return m_xData.minMax(xMin, xMax) && m_yData.minMax(yMin, yMax);
00488 }
00489 }
00490
00491 private:
00492 PlotSingleDataImpl<T> m_xData;
00493 PlotSingleDataImpl<T> m_yData;
00494 };
00495
00496 typedef PlotPointDataImpl<int> PlotPointIntData;
00497 typedef PlotPointDataImpl<unsigned int> PlotPointUIntData;
00498 typedef PlotPointDataImpl<float> PlotPointFloatData;
00499 typedef PlotPointDataImpl<double> PlotPointDoubleData;
00500
00501
00502
00503
00504
00505
00506
00507 class PlotHistogramData : public virtual PlotPointData {
00508 public:
00509
00510 PlotHistogramData(PlotSingleDataPtr data, unsigned int numBins);
00511
00512
00513 virtual ~PlotHistogramData();
00514
00515
00516
00517 virtual bool isValid() const;
00518
00519
00520 virtual bool willDeleteData() const;
00521
00522
00523 virtual void setDeleteData(bool del = true);
00524
00525
00526
00527 virtual unsigned int size() const { return numBins(); }
00528
00529
00530 virtual double xAt(unsigned int i) const;
00531
00532
00533 virtual double yAt(unsigned int i) const;
00534
00535
00536 virtual bool minsMaxes(double& xMin, double& xMax, double& yMin,
00537 double& yMax);
00538
00539
00540
00541 virtual void recalculateBins(unsigned int numBins);
00542
00543
00544 virtual unsigned int numBins() const;
00545
00546
00547 virtual prange_t rangeAt(unsigned int i) const;
00548
00549 private:
00550 PlotSingleDataPtr m_data;
00551 vector<unsigned int> m_bins;
00552 vector<prange_t> m_ranges;
00553 unsigned int m_max;
00554 };
00555
00556
00557
00558 template <class T>
00559 class PlotMaskedPointDataImpl : public virtual PlotMaskedPointData,
00560 public PlotPointDataImpl<T> {
00561 public:
00562
00563
00564 PlotMaskedPointDataImpl(vector<T>& x, vector<T>& y, vector<bool>& mask,
00565 bool shouldDelete = false) :
00566 PlotPointDataImpl<T>(x, y, shouldDelete), m_maskVector(&mask),
00567 m_maskCVector(NULL), m_maskArray(NULL), m_maskArraySize(0),
00568 m_shouldDeleteMask(shouldDelete) { }
00569 PlotMaskedPointDataImpl(Vector<T>& x, Vector<T>& y, Vector<bool>& mask,
00570 bool shouldDelete = false) :
00571 PlotPointDataImpl<T>(x, y, shouldDelete), m_maskVector(NULL),
00572 m_maskCVector(&mask), m_maskArray(NULL), m_maskArraySize(0),
00573 m_shouldDeleteMask(shouldDelete) { }
00574 PlotMaskedPointDataImpl(T*& x, T*& y, bool*& mask, unsigned int size,
00575 bool shouldDel = false) :
00576 PlotPointDataImpl<T>(x, y, size, shouldDel), m_maskVector(NULL),
00577 m_maskCVector(NULL), m_maskArray(mask), m_maskArraySize(size),
00578 m_shouldDeleteMask(shouldDel) { }
00579
00580
00581
00582
00583 PlotMaskedPointDataImpl(vector<T>& y, vector<bool>& mask,
00584 bool shouldDelete = false) :
00585 PlotPointDataImpl<T>(y, shouldDelete), m_maskVector(&mask),
00586 m_maskCVector(NULL), m_maskArray(NULL), m_maskArraySize(0),
00587 m_shouldDeleteMask(shouldDelete) { }
00588 PlotMaskedPointDataImpl(Vector<T>& y, Vector<bool>& mask,
00589 bool shouldDelete = false) :
00590 PlotPointDataImpl<T>(y, shouldDelete), m_maskVector(NULL),
00591 m_maskCVector(&mask), m_maskArray(NULL), m_maskArraySize(0),
00592 m_shouldDeleteMask(shouldDelete) { }
00593 PlotMaskedPointDataImpl(T*& y, bool*& mask, unsigned int size,
00594 bool shouldDel = false) :
00595 PlotPointDataImpl<T>(y, size, shouldDel), m_maskVector(NULL),
00596 m_maskCVector(NULL), m_maskArray(mask), m_maskArraySize(size),
00597 m_shouldDeleteMask(shouldDel) { }
00598
00599
00600
00601 ~PlotMaskedPointDataImpl() {
00602 if(m_shouldDeleteMask) {
00603 if(m_maskVector != NULL) delete m_maskVector;
00604 if(m_maskCVector != NULL) delete m_maskCVector;
00605 if(m_maskArray != NULL) delete m_maskArray;
00606 }
00607 }
00608
00609
00610 bool willDeleteData() const {
00611 return PlotPointDataImpl<T>::willDeleteData() && m_shouldDeleteMask; }
00612
00613
00614 void setDeleteData(bool del = true) {
00615 PlotPointDataImpl<T>::setDeleteData(del);
00616 m_shouldDeleteMask = del;
00617 }
00618
00619
00620 unsigned int sizeMasked() const { return sizeMaskedOrUnmasked(true); }
00621
00622
00623 unsigned int sizeUnmasked() const { return sizeMaskedOrUnmasked(false); }
00624
00625
00626 bool maskedAt(unsigned int index) const {
00627 if(m_maskVector != NULL) return (*m_maskVector)[index];
00628 if(m_maskCVector != NULL) return (*m_maskCVector)[index];
00629 if(m_maskArray != NULL) return m_maskArray[index];
00630 return false;
00631 }
00632
00633
00634 bool maskedMinsMaxes(double& xMin, double& xMax, double& yMin,
00635 double& yMax) {
00636 return getMaskedOrUnmaskedMinsMaxes(xMin, xMax, yMin, yMax, true); }
00637
00638
00639 bool unmaskedMinsMaxes(double& xMin, double& xMax, double& yMin,
00640 double& yMax) {
00641 return getMaskedOrUnmaskedMinsMaxes(xMin, xMax, yMin, yMax, false); }
00642
00643 private:
00644 vector<bool>* m_maskVector;
00645 Vector<bool>* m_maskCVector;
00646 bool* m_maskArray;
00647 unsigned int m_maskArraySize;
00648 bool m_shouldDeleteMask;
00649
00650
00651 unsigned int sizeMaskedOrUnmasked(bool masked) const {
00652 unsigned int n = size();
00653 unsigned int count = 0;
00654 if(m_maskArray != NULL) {
00655 for(unsigned int i = 0; i < m_maskArraySize; i++)
00656 if(m_maskArray[i]) count++;
00657 } else if(m_maskVector != NULL) {
00658 for(unsigned int i = 0; i < m_maskVector->size(); i++)
00659 if((*m_maskVector)[i]) count++;
00660 } else if(m_maskCVector != NULL) {
00661 for(unsigned int i = 0; i < m_maskCVector->size(); i++)
00662 if((*m_maskCVector)[i]) count++;
00663 } else return n;
00664 if(masked) return min(count, n);
00665 else return min(n - count, n);
00666 }
00667
00668
00669 bool getMaskedOrUnmaskedMinsMaxes(double& xMin, double& xMax, double& yMin,
00670 double& yMax, bool masked) {
00671 if(!isValid()) return false;
00672 unsigned int n = size();
00673 if(n == 0) return false;
00674 if(m_maskArray == NULL && m_maskVector == NULL &&
00675 m_maskCVector == NULL) return minsMaxes(xMin, xMax, yMin, yMax);
00676
00677 unsigned int i = 0;
00678 bool m;
00679 for(; i < n; i++) {
00680 m = maskedAt(i);
00681 if((masked && m) || (!masked && !m)) {
00682 xMin = xMax = xAt(i);
00683 yMin = yMax = yAt(i);
00684 break;
00685 }
00686 }
00687 if(i == n) return false;
00688 double temp;
00689 for(; i < n; i++) {
00690 m = maskedAt(i);
00691 if((masked && m) || (!masked && !m)) {
00692 temp = xAt(i);
00693 if(temp < xMin) xMin = temp;
00694 if(temp > xMax) xMax = temp;
00695 temp = yAt(i);
00696 if(temp < yMin) yMin = temp;
00697 if(temp > yMax) yMax = temp;
00698 }
00699 }
00700 return true;
00701 }
00702 };
00703
00704 typedef PlotMaskedPointDataImpl<int> PlotMaskedPointIntData;
00705 typedef PlotMaskedPointDataImpl<unsigned int> PlotMaskedPointUIntData;
00706 typedef PlotMaskedPointDataImpl<float> PlotMaskedPointFloatData;
00707 typedef PlotMaskedPointDataImpl<double> PlotMaskedPointDoubleData;
00708
00709
00710
00711
00712 template <class T>
00713 class PlotScalarErrorDataImpl : public virtual PlotErrorData,
00714 public PlotPointDataImpl<T> {
00715 public:
00716
00717
00718 PlotScalarErrorDataImpl(vector<T>& x, vector<T>& y, T xLeftError,
00719 T xRightError, T yBottomError, T yTopError,
00720 bool shouldDelete=false): PlotPointDataImpl<T>(x, y, shouldDelete),
00721 m_xLeftError(xLeftError), m_xRightError(xRightError),
00722 m_yBottomError(yBottomError), m_yTopError(yTopError) { }
00723 PlotScalarErrorDataImpl(Vector<T>& x, Vector<T>& y, T xLeftError,
00724 T xRightError, T yBottomError, T yTopError,
00725 bool shouldDelete=false): PlotPointDataImpl<T>(x, y, shouldDelete),
00726 m_xLeftError(xLeftError), m_xRightError(xRightError),
00727 m_yBottomError(yBottomError), m_yTopError(yTopError) { }
00728 PlotScalarErrorDataImpl(T*& x, T*& y, unsigned int size, T xLeftError,
00729 T xRightError, T yBottomError, T yTopError,
00730 bool shouldDelete = false) :
00731 PlotPointDataImpl<T>(x, y, size, shouldDelete),
00732 m_xLeftError(xLeftError), m_xRightError(xRightError),
00733 m_yBottomError(yBottomError), m_yTopError(yTopError) { }
00734
00735
00736
00737
00738 PlotScalarErrorDataImpl(vector<T>& x, vector<T>& y, T xError, T yError,
00739 bool shouldDelete=false): PlotPointDataImpl<T>(x, y, shouldDelete),
00740 m_xLeftError(xError), m_xRightError(xError),
00741 m_yBottomError(yError), m_yTopError(yError) { }
00742 PlotScalarErrorDataImpl(Vector<T>& x, Vector<T>& y, T xError, T yError,
00743 bool shouldDelete=false): PlotPointDataImpl<T>(x, y, shouldDelete),
00744 m_xLeftError(xError), m_xRightError(xError),
00745 m_yBottomError(yError), m_yTopError(yError) { }
00746 PlotScalarErrorDataImpl(T*& x, T*& y,unsigned int size, T xError, T yError,
00747 bool shouldDelete = false) :
00748 PlotPointDataImpl<T>(x, y, size, shouldDelete),
00749 m_xLeftError(xError), m_xRightError(xError),
00750 m_yBottomError(yError), m_yTopError(yError) { }
00751
00752
00753
00754
00755 PlotScalarErrorDataImpl(vector<T>& x, vector<T>& y, T error,
00756 bool shouldDelete=false): PlotPointDataImpl<T>(x, y, shouldDelete),
00757 m_xLeftError(error), m_xRightError(error), m_yBottomError(error),
00758 m_yTopError(error) { }
00759 PlotScalarErrorDataImpl(Vector<T>& x, Vector<T>& y, T error,
00760 bool shouldDelete=false): PlotPointDataImpl<T>(x, y, shouldDelete),
00761 m_xLeftError(error), m_xRightError(error), m_yBottomError(error),
00762 m_yTopError(error) { }
00763 PlotScalarErrorDataImpl(T*& x, T*& y, unsigned int size, T error,
00764 bool shouldDelete = false) :
00765 PlotPointDataImpl<T>(x, y, size, shouldDelete),
00766 m_xLeftError(error), m_xRightError(error), m_yBottomError(error),
00767 m_yTopError(error) { }
00768
00769
00770
00771 ~PlotScalarErrorDataImpl() { }
00772
00773
00774
00775 double xLeftErrorAt(unsigned int i) const { return m_xLeftError; }
00776 double xRightErrorAt(unsigned int i) const { return m_xRightError; }
00777 double yBottomErrorAt(unsigned int i) const { return m_yBottomError; }
00778 double yTopErrorAt(unsigned int i) const { return m_yTopError; }
00779
00780
00781
00782 bool errorMaxes(double& xLeft, double& xRight, double& yBottom,
00783 double& yTop) {
00784 xLeft = m_xLeftError;
00785 xRight = m_xRightError;
00786 yBottom = m_yBottomError;
00787 yTop = m_yTopError;
00788 return true;
00789 }
00790
00791 private:
00792 T m_xLeftError, m_xRightError, m_yBottomError, m_yTopError;
00793 };
00794
00795 typedef PlotScalarErrorDataImpl<int> PlotScalarErrorIntData;
00796 typedef PlotScalarErrorDataImpl<unsigned int> PlotScalarErrorUIntData;
00797 typedef PlotScalarErrorDataImpl<float> PlotScalarErrorFloatData;
00798 typedef PlotScalarErrorDataImpl<double> PlotScalarErrorDoubleData;
00799
00800
00801
00802
00803 template <class T>
00804 class PlotErrorDataImpl : public virtual PlotErrorData,
00805 public PlotPointDataImpl<T> {
00806 public:
00807
00808
00809 PlotErrorDataImpl(T*& x, T*& y, T*& xError, T*& yError, unsigned int size,
00810 bool shouldDelete = true) :
00811 PlotPointDataImpl<T>(x, y, size, shouldDelete),
00812 m_xError(xError, xError, size, shouldDelete),
00813 m_yError(yError, yError, size, shouldDelete) { }
00814 PlotErrorDataImpl(vector<T>& x, vector<T>& y, vector<T>& xError,
00815 vector<T>& yError, bool shouldDelete = false) :
00816 PlotPointDataImpl<T>(x, y, shouldDelete),
00817 m_xError(xError, xError, shouldDelete),
00818 m_yError(yError, yError, shouldDelete) { }
00819 PlotErrorDataImpl(Vector<T>& x, Vector<T>& y, Vector<T>& xError,
00820 Vector<T>& yError, bool shouldDelete = false) :
00821 PlotPointDataImpl<T>(x, y, shouldDelete),
00822 m_xError(xError, xError, shouldDelete),
00823 m_yError(yError, yError, shouldDelete) { }
00824
00825
00826
00827
00828 PlotErrorDataImpl(T*& x, T*& y, T*& xLeftError, T*& xRightError,
00829 T*& yBottomError, T*& yTopError, unsigned int size,
00830 bool shouldDelete = true) :
00831 PlotPointDataImpl<T>(x, y, size, shouldDelete),
00832 m_xError(xLeftError, xRightError, size, shouldDelete),
00833 m_yError(yBottomError, yTopError, size, shouldDelete) { }
00834 PlotErrorDataImpl(vector<T>& x, vector<T>& y, vector<T>& xLeftError,
00835 vector<T>& xRightError, vector<T>& yBottomError,
00836 vector<T>& yTopError, bool shouldDelete = false) :
00837 PlotPointDataImpl<T>(x, y, shouldDelete),
00838 m_xError(xLeftError, xRightError, shouldDelete),
00839 m_yError(yBottomError, yTopError, shouldDelete) { }
00840 PlotErrorDataImpl(Vector<T>& x, Vector<T>& y, Vector<T>& xLeftError,
00841 Vector<T>& xRightError, Vector<T>& yBottomError,
00842 Vector<T>& yTopError, bool shouldDelete = false) :
00843 PlotPointDataImpl<T>(x, y, shouldDelete),
00844 m_xError(xLeftError, xRightError, shouldDelete),
00845 m_yError(yBottomError, yTopError, shouldDelete) { }
00846
00847
00848 ~PlotErrorDataImpl() { }
00849
00850
00851 bool willDeleteData() const {
00852 return PlotPointDataImpl<T>::willDeleteData() &&
00853 m_xError.willDeleteData() && m_yError.willDeleteData();
00854 }
00855
00856
00857 void setDeleteData(bool del = true) {
00858 m_xError.setDeleteData(del);
00859 m_yError.setDeleteData(del);
00860 PlotPointDataImpl<T>::setDeleteData(del);
00861 }
00862
00863
00864
00865 double xLeftErrorAt(unsigned int i) const { return m_xError.xAt(i); }
00866 double xRightErrorAt(unsigned int i) const { return m_xError.yAt(i); }
00867 double yBottomErrorAt(unsigned int i) const { return m_yError.xAt(i); }
00868 double yTopErrorAt(unsigned int i) const { return m_yError.yAt(i); }
00869
00870
00871
00872 bool errorMaxes(double& xLeft, double& xRight, double& yBottom,
00873 double& yTop) {
00874 double temp;
00875 return m_xError.minsMaxes(temp, xLeft, temp, xRight) &&
00876 m_yError.minsMaxes(temp, yBottom, temp, yTop);
00877 }
00878
00879 private:
00880 PlotPointDataImpl<T> m_xError, m_yError;
00881 };
00882
00883 typedef PlotErrorDataImpl<int> PlotErrorIntData;
00884 typedef PlotErrorDataImpl<unsigned int> PlotErrorUIntData;
00885 typedef PlotErrorDataImpl<float> PlotErrorFloatData;
00886 typedef PlotErrorDataImpl<double> PlotErrorDoubleData;
00887
00888
00889
00890 template <class T>
00891 class PlotRasterMatrixData : public virtual PlotRasterData {
00892 public:
00893
00894 enum Indexing {
00895 ROW_COL, X_Y
00896 };
00897
00898 PlotRasterMatrixData(Matrix<T>& data, bool shouldDelete = false) :
00899 m_data(&data), m_origin(LLEFT), m_indexing(ROW_COL),
00900 m_shouldDelete(shouldDelete) {
00901 IPosition shape = data.shape();
00902 unsigned int n0 = shape[0] - 1, n1 = shape[1] - 1;
00903
00904 m_0From = 0;
00905 m_0To = n0 + 1;
00906 m_1From = 0;
00907 m_1To = n1 + 1;
00908 m_0Pieces = (n0 + 1) / (m_0To - m_0From);
00909 m_1Pieces = (n1 + 1) / (m_1To - m_1From);
00910
00911 double val = static_cast<double>(data(0, 0));
00912 m_valFrom = m_valTo = val;
00913 for(uInt i = 0; i < data.nrow(); i++) {
00914 for(uInt j = 0; j < data.ncolumn(); j++) {
00915 val = static_cast<double>(data(i, j));
00916 if(val < m_valFrom) m_valFrom = val;
00917 if(val > m_valTo) m_valTo = val;
00918 }
00919 }
00920 }
00921
00922 ~PlotRasterMatrixData() { if(m_shouldDelete) delete m_data; }
00923
00924
00925 bool isValid() const { return true; }
00926
00927
00928 bool willDeleteData() const { return m_shouldDelete; }
00929
00930
00931 void setDeleteData(bool del = true) { m_shouldDelete = del; }
00932
00933
00934 Origin origin() const { return m_origin; }
00935
00936
00937 void setOrigin(Origin o) {
00938 if(m_origin != o) {
00939 m_origin = o;
00940 }
00941 }
00942
00943
00944 prange_t xRange() const {
00945 if(m_indexing == X_Y) return prange_t(m_0From, m_0To);
00946 else return prange_t(m_1From, m_1To);
00947 }
00948
00949
00950 prange_t yRange() const {
00951 if(m_indexing == X_Y) return prange_t(m_1From, m_1To);
00952 else return prange_t(m_0From, m_0To);
00953 }
00954
00955
00956 void setXRange(double from, double to) {
00957 if(from == to) return;
00958 if(from > to) {
00959 double temp = from;
00960 from = to;
00961 to = temp;
00962 }
00963
00964 if(m_indexing == X_Y) {
00965 m_0From = from;
00966 m_0To = to;
00967 m_0Pieces = (m_data->shape()[0]) / (m_0To - m_0From);
00968 } else {
00969 m_1From = from;
00970 m_1To = to;
00971 m_1Pieces = (m_data->shape()[1]) / (m_1To - m_1From);
00972 }
00973 }
00974
00975
00976 void setYRange(double from, double to) {
00977 if(from == to) return;
00978 if(from > to) {
00979 double temp = from;
00980 from = to;
00981 to = temp;
00982 }
00983
00984 if(m_indexing == X_Y) {
00985 m_1From = from;
00986 m_1To = to;
00987 m_1Pieces = (m_data->shape()[1]) / (m_1To - m_1From);
00988 } else {
00989 m_0From = from;
00990 m_0To = to;
00991 m_0Pieces = (m_data->shape()[0]) / (m_0To - m_0From);
00992 }
00993 }
00994
00995
00996 prange_t valueRange() const { return prange_t(m_valFrom, m_valTo); }
00997
00998
00999 double valueAt(double x, double y) const {
01000 if(m_indexing == X_Y) {
01001 if(x < m_0From || x > m_0To || y < m_1From || y > m_1To) return 0;
01002
01003 int xi = (int)((x - m_0From) * m_0Pieces);
01004 int yi = (int)((y - m_1From) * m_1Pieces);
01005 if(xi >= m_data->shape()[0]) xi = m_data->shape()[0] - 1;
01006 if(yi >= m_data->shape()[1]) yi = m_data->shape()[1] - 1;
01007
01008 return static_cast<double>((*m_data)(xi, yi));
01009
01010 } else {
01011 if(x < m_1From || x > m_1To || y < m_0From || y > m_0To) return 0;
01012
01013 int xi = (int)((x - m_1From) * m_1Pieces);
01014 int yi = (int)((y - m_0From) * m_0Pieces);
01015 if(xi >= m_data->shape()[1]) xi = m_data->shape()[1] - 1;
01016 if(yi >= m_data->shape()[0]) yi = m_data->shape()[0] - 1;
01017
01018 return static_cast<double>((*m_data)(yi, xi));
01019 }
01020 }
01021
01022
01023 vector<double>* colorBarValues(unsigned int max = 1000) const {
01024 vector<double>* v = new vector<double>();
01025
01026 double val;
01027 bool found;
01028 for(unsigned int i = 0; i < m_data->nrow() && v->size() <= max; i++) {
01029 for(unsigned int j = 0; j < m_data->ncolumn() && v->size() <= max;
01030 j++) {
01031 val = static_cast<double>((*m_data)(i, j));
01032 found = false;
01033 for(unsigned int k = 0; k < v->size() && !found; k++)
01034 if(v->at(k) == val) found = true;
01035 if(!found) v->push_back(val);
01036 }
01037 }
01038
01039 return v;
01040 }
01041
01042
01043
01044 Indexing indexing() const { return m_indexing; }
01045 void setIndexing(Indexing i) { m_indexing = i; }
01046
01047
01048
01049
01050 Matrix<T>* matrix() { return m_data; }
01051 void setMatrix(Matrix<T>* m, bool shouldDelete = true) {
01052 if(m_shouldDelete) delete m_data;
01053 m_data = m;
01054 m_shouldDelete = shouldDelete;
01055 }
01056
01057
01058 private:
01059 Matrix<T>* m_data;
01060 double m_0From, m_0To;
01061 double m_1From, m_1To;
01062 double m_0Pieces, m_1Pieces;
01063 double m_valFrom, m_valTo;
01064 Origin m_origin;
01065 Indexing m_indexing;
01066 bool m_shouldDelete;
01067 };
01068
01069 }
01070
01071 #endif