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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #ifndef HxHistogram_h
00045 #define HxHistogram_h
00046
00047 #include <list>
00048 #include "HxRcPtr.h"
00049 #include "HxString.h"
00050 #include "HxPointInt.h"
00051 #include "HxScalarInt.h"
00052 #include "HxScalarDouble.h"
00053 #include "HxVec2Int.h"
00054 #include "HxVec2Double.h"
00055 #include "HxVec3Int.h"
00056 #include "HxVec3Double.h"
00057 #include "HxHistogramData.h"
00058 #include "HxIo.h"
00059
00071 class HxHistogram {
00072 public:
00073
00075
00078 HxHistogram();
00079
00081 HxHistogram(const HxHistogram&);
00082
00086 HxHistogram(int dimSize);
00087
00091 HxHistogram(int dimSize1, int dimSize2);
00092
00096 HxHistogram(int dimSize1, int dimSize2, int dimSize3);
00097
00102 HxHistogram(HxValueType dataType, int dimensions,
00103 int dimSize1, int dimSize2 = 0, int dimSize3 = 0);
00104
00108 HxHistogram(HxValueType dataType, int dimensions,
00109 double lowBin1, double highBin1, int nBins1,
00110 double lowBin2, double highBin2, int nBins2,
00111 double lowBin3, double highBin3, int nBins3);
00112
00114 HxHistogram(HxString filename);
00115
00116
00118
00120
00122 ~HxHistogram();
00124
00126
00128 HxHistogram& operator=(const HxHistogram&);
00129
00131 int isNull() const;
00132
00134 operator int() const;
00136
00138
00140 int ident() const;
00141
00145 HxValueType dataType() const;
00146
00148 int dimensionality() const;
00149
00153 int dimensionSize(int dim) const;
00154
00157 int nrOfBins() const;
00158
00160 double lowBin(int dim) const;
00161
00163 double highBin(int dim) const;
00164
00166 double binWidth(int dim) const;
00167
00169 double binToValue(int bin, int dimension) const;
00170
00172 int valueToBin(double value, int dimension) const;
00173
00175 int valueToBin(double val) const;
00176
00178 int clipBin(int bin) const;
00179
00181 double get(int bin1) const;
00182
00184 double get(int bin1, int bin2) const;
00185
00187 double get(int bin1, int bin2, int bin3) const;
00189
00191
00193 void insertValChecked(int val);
00194
00196 void insertValChecked(double val);
00197
00199 void insertValChecked(HxScalarInt val);
00200
00202 void insertValChecked(HxScalarDouble val);
00203
00205 void insertValChecked(HxVec2Int val);
00206
00208 void insertValChecked(HxVec2Double val);
00209
00211 void insertValChecked(HxVec3Int val);
00212
00214 void insertValChecked(HxVec3Double val);
00215
00220 void incBinChecked(int bin);
00221
00226 void incBinChecked(int bin1, int bin2);
00227
00232 void incBinChecked(int bin1, int bin2, int bin3);
00234
00236
00239 void insertVal(int val);
00240
00242 void insertVal(double val);
00243
00245 void insertVal(double val, double sigma);
00246
00248 void insertVal(HxScalarInt val);
00249
00251 void insertVal(HxScalarDouble val);
00252
00254 void insertVal(HxVec2Int val);
00255
00257 void insertVal(HxVec2Double val);
00258
00260 void insertVal(HxVec3Int val);
00261
00263 void insertVal(HxVec3Double val);
00264
00265
00269 void incBin(int bin);
00270
00274 void incBin(int bin1, int bin2);
00275
00279 void incBin(int bin1, int bin2, int bin3);
00280
00284 void setBin(int bin1, long val);
00285
00289 void setBin(int bin1, int bin2, long val);
00290
00294 void setBin(int bin1, int bin2, int bin3, long val);
00295
00299 void setBin(int bin1, double val);
00300
00304 void setBin(int bin1, int bin2, double val);
00305
00309 void setBin(int bin1, int bin2, int bin3, double val);
00310
00312
00314
00316 HxHistogram smooth(double sigma = 3.0);
00317
00319 std::list<HxVec2Double> modes();
00320
00322 HxHistogram normalize(double weight = 1.0);
00323
00325 double sum() const;
00326
00328 double minVal() const;
00329
00333 double minVal(int *index) const;
00334
00336 double maxVal() const;
00337
00341 double maxVal(int *index) const;
00342
00344 double intersection(const HxHistogram&) const;
00345
00347 double chiSquare(const HxHistogram&) const;
00348
00350 double chiSquareNorm(const HxHistogram&) const;
00351
00353
00355
00356 int computeIsodataThreshold(void);
00357 int computeEntropyThreshold(void);
00359
00360
00362
00364 HxHistogram convert(HxValueType dataType);
00365
00371 void getTheData(double *x, double* y);
00372
00378 void getTheData2(double *x1, double *x2, double* y);
00379
00385 void getTheData3(double *x1, double *x2, double *x3,
00386 double* y);
00387
00391 void getDataDouble(double* data);
00392
00396 void getDataInt(int* data);
00397
00402 void render3d(int* data, int dataWidth, int dataHeight,
00403 double elevation, double alpha, double threshold);
00404
00406 STD_OSTREAM& put(STD_OSTREAM&, HxString delimit = " ") const;
00407
00409 int write(HxString filename);
00410
00412
00414
00419 HxHistogram threshold(double valThreshold);
00420
00424 int countBins(double valThreshold=0.0);
00425
00430 HxHistogram reduceRange(int binMin1, int binMax1=-1,
00431 int binMin2=0, int binMax2=-1,
00432 int binMin3=0, int binMax3=-1);
00433
00440 HxHistogram reduceRangeVal(double binValMin1, double binValMax1,
00441 double binValMin2=0, double binValMax2=0,
00442 double binValMin3=0, double binValMax3=0);
00443
00447 HxHistogram to1D(int dim=1);
00448
00450 private:
00451
00452 HxHistogram(const HxHistogramData*);
00453
00454 int isEqualSize(const HxHistogram&) const;
00455
00456 HxRcPtr<HxHistogramData> _data;
00457
00458 };
00459
00460 inline STD_OSTREAM&
00461 operator<<(STD_OSTREAM& os, const HxHistogram& his)
00462 {
00463 return his.put(os);
00464 }
00465
00466 inline HxString
00467 makeString(const HxHistogram& his)
00468 {
00469 return HxString("HxHistogram") + makeString(his.ident());
00470 }
00471
00472 inline double
00473 HxHistogram::get(int bin1) const
00474 {
00475 return _data ? _data->getBin(bin1) : 0;
00476 }
00477
00478 inline double
00479 HxHistogram::get(int bin1, int bin2) const
00480 {
00481 return _data ? _data->getBin(bin2 * _data->_dimSize1 + bin1) : 0;
00482 }
00483
00484 inline double
00485 HxHistogram::get(int bin1, int bin2, int bin3) const
00486 {
00487 return _data ? _data->getBin((bin3 * _data->_dimSize2 + bin2) *
00488 _data->_dimSize1 + bin1) : 0;
00489 }
00490
00491 inline void
00492 HxHistogram::incBin(int bin)
00493 {
00494 if ((bin >= 0) && (bin < _data->_dimSize1))
00495 _data->incBin(bin);
00496 }
00497
00498 inline void
00499 HxHistogram::incBin(int bin1, int bin2)
00500 {
00501 if ((bin1 >= 0) && (bin1 < _data->_dimSize1) &&
00502 (bin2 >= 0) && (bin2 < _data->_dimSize2))
00503 _data->incBin(bin2 * _data->_dimSize1 + bin1);
00504 }
00505
00506 inline void
00507 HxHistogram::incBin(int bin1, int bin2, int bin3)
00508 {
00509 if ((bin1 >= 0) && (bin1 < _data->_dimSize1) &&
00510 (bin2 >= 0) && (bin2 < _data->_dimSize2) &&
00511 (bin3 >= 0) && (bin3 < _data->_dimSize3))
00512 _data->incBin((bin3 * _data->_dimSize2 + bin2) *
00513 _data->_dimSize1 + bin1);
00514 }
00515
00516 inline void
00517 HxHistogram::setBin(int bin1, long val)
00518 {
00519 if ((bin1 >= 0) && (bin1 < _data->_dimSize1))
00520 _data->setBin(bin1, val);
00521 }
00522
00523 inline void
00524 HxHistogram::setBin(int bin1, int bin2, long val)
00525 {
00526 if ((bin1 >= 0) && (bin1 < _data->_dimSize1) &&
00527 (bin2 >= 0) && (bin2 < _data->_dimSize2))
00528 _data->setBin(bin2 * _data->_dimSize1 + bin1, val);
00529 }
00530
00531 inline void
00532 HxHistogram::setBin(int bin1, int bin2, int bin3, long val)
00533 {
00534 if ((bin1 >= 0) && (bin1 < _data->_dimSize1) &&
00535 (bin2 >= 0) && (bin2 < _data->_dimSize2) &&
00536 (bin3 >= 0) && (bin3 < _data->_dimSize3))
00537 _data->setBin((bin3 * _data->_dimSize2 + bin2) *
00538 _data->_dimSize1 + bin1, val);
00539 }
00540
00541 inline void
00542 HxHistogram::setBin(int bin1, double val)
00543 {
00544 if ((bin1 >= 0) && (bin1 < _data->_dimSize1))
00545 _data->setBin(bin1, val);
00546 }
00547
00548 inline void
00549 HxHistogram::setBin(int bin1, int bin2, double val)
00550 {
00551 if ((bin1 >= 0) && (bin1 < _data->_dimSize1) &&
00552 (bin2 >= 0) && (bin2 < _data->_dimSize2))
00553 _data->setBin(bin2 * _data->_dimSize1 + bin1, val);
00554 }
00555
00556 inline void
00557 HxHistogram::setBin(int bin1, int bin2, int bin3, double val)
00558 {
00559 if ((bin1 >= 0) && (bin1 < _data->_dimSize1) &&
00560 (bin2 >= 0) && (bin2 < _data->_dimSize2) &&
00561 (bin3 >= 0) && (bin3 < _data->_dimSize3))
00562 _data->setBin((bin3 * _data->_dimSize2 + bin2) *
00563 _data->_dimSize1 + bin1, val);
00564 }
00565
00566 inline int
00567 HxHistogram::valueToBin(double val, int dimension) const
00568 {
00569 if (!_data)
00570 return -1;
00571 switch (dimension) {
00572 case 1: return (int) ((val - _data->_lowRange1) * _data->_binFac1);
00573 case 2: return (int) ((val - _data->_lowRange2) * _data->_binFac2);
00574 case 3: return (int) ((val - _data->_lowRange3) * _data->_binFac3);
00575 }
00576 return -1;
00577 }
00578
00579 inline int
00580 HxHistogram::valueToBin(double val) const
00581 {
00582 if (!_data)
00583 return -1;
00584 return (int) ((val - _data->_lowRange1) * _data->_binFac1);
00585 }
00586
00587 inline int
00588 HxHistogram::clipBin(int bin) const
00589 {
00590
00591 return (bin < 0) ? 0 : (bin > (nrOfBins()-1)) ? nrOfBins()-1 : bin;
00592 }
00593
00594 inline void
00595 HxHistogram::insertVal(int val)
00596 {
00597 incBin(clipBin(valueToBin((double) val)));
00598 }
00599
00600 inline void
00601 HxHistogram::insertVal(double val)
00602 {
00603 incBin(clipBin(valueToBin(val)));
00604 }
00605
00606 inline void
00607 HxHistogram::insertVal(HxScalarInt val)
00608 {
00609 insertVal(val.x());
00610 return;
00611 }
00612
00613 inline void
00614 HxHistogram::insertVal(HxScalarDouble val)
00615 {
00616 insertVal(val.x());
00617 return;
00618 }
00619
00620 inline void
00621 HxHistogram::insertVal(HxVec2Int val)
00622 {
00623 int bin1 = (int) ((val.x() - _data->_lowRange1) * _data->_binFac1);
00624 if (bin1 >= _data->_dimSize1)
00625 {
00626 if (val.x()==_data->_highBin1)
00627 bin1=_data->_dimSize1-1;
00628 else
00629 return;
00630 }
00631 int bin2 = (int) ((val.y() - _data->_lowRange2) * _data->_binFac2);
00632 if (bin2 >= _data->_dimSize2)
00633 {
00634 if (val.y()==_data->_highBin2)
00635 bin2=_data->_dimSize2-1;
00636 else
00637 return;
00638 }
00639 incBin(bin1,bin2);
00640 }
00641
00642 inline void
00643 HxHistogram::insertVal(HxVec2Double val)
00644 {
00645 int bin1 = (int) ((val.x() - _data->_lowRange1) * _data->_binFac1);
00646 if (bin1 >= _data->_dimSize1)
00647 {
00648 if (val.x()==_data->_highBin1)
00649 bin1=_data->_dimSize1-1;
00650 else
00651 return;
00652 }
00653 int bin2 = (int) ((val.y() - _data->_lowRange2) * _data->_binFac2);
00654 if (bin2 >= _data->_dimSize2)
00655 {
00656 if (val.y()==_data->_highBin2)
00657 bin2=_data->_dimSize2-1;
00658 else
00659 return;
00660 }
00661 incBin(bin1,bin2);
00662 }
00663
00664 inline void
00665 HxHistogram::insertVal(HxVec3Int val)
00666 {
00667 int bin1 = (int) ((val.x() - _data->_lowRange1) * _data->_binFac1);
00668 if (bin1 >= _data->_dimSize1)
00669 {
00670 if (val.x()==_data->_highBin1)
00671 bin1=_data->_dimSize1-1;
00672 else
00673 return;
00674 }
00675 int bin2 = (int) ((val.y() - _data->_lowRange2) * _data->_binFac2);
00676 if (bin2 >= _data->_dimSize2)
00677 {
00678 if (val.y()==_data->_highBin2)
00679 bin2=_data->_dimSize2-1;
00680 else
00681 return;
00682 }
00683 int bin3 = (int) ((val.z() - _data->_lowRange3) * _data->_binFac3);
00684 if (bin3 >= _data->_dimSize3)
00685 {
00686 if (val.z()==_data->_highBin3)
00687 bin3=_data->_dimSize3-1;
00688 else
00689 return;
00690 }
00691 incBin(bin1,bin2,bin3);
00692 }
00693
00694 inline void
00695 HxHistogram::insertVal(HxVec3Double val)
00696 {
00697 int bin1 = (int) ((val.x() - _data->_lowRange1) * _data->_binFac1);
00698 if (bin1 >= _data->_dimSize1)
00699 {
00700 if (val.x()==_data->_highBin1)
00701 bin1=_data->_dimSize1-1;
00702 else
00703 return;
00704 }
00705 int bin2 = (int) ((val.y() - _data->_lowRange2) * _data->_binFac2);
00706 if (bin2 >= _data->_dimSize2)
00707 {
00708 if (val.y()==_data->_highBin2)
00709 bin2=_data->_dimSize2-1;
00710 else
00711 return;
00712 }
00713 int bin3 = (int) ((val.z() - _data->_lowRange3) * _data->_binFac3);
00714 if (bin3 >= _data->_dimSize3)
00715 {
00716 if (val.z()==_data->_highBin3)
00717 bin3=_data->_dimSize3-1;
00718 else
00719 return;
00720 }
00721 incBin(bin1,bin2,bin3);
00722 }
00723
00724 #endif