#ifndef _OTB_TRISKELE_ARRAY_TREE_WEIGHT_TPP #define _OTB_TRISKELE_ARRAY_TREE_WEIGHT_TPP // ======================================== template inline DimNodeId WeightBase::pointIdx (const Point &p) const { return point2idx (size, p); } template inline const Size & WeightBase::getSize () const { return size; } template inline const PixelT & WeightBase::getValue (const DimImg &idx) const { return pixels[idx]; } template inline const PixelT & WeightBase::getValue (const Point &p) const { return getValue (pointIdx (p)); } template inline bool WeightBase::getDecr () const { return false; } template inline const PixelT & WeightBase::getMaxPixel () const { return maxPixel; } template inline const PixelT & WeightBase::getHalfPixel () const { return halfPixel; } template inline void WeightBase::reset (const PixelT *pixels, const Size &size) { this->pixels = pixels; this->size = size; maxPixel = std::numeric_limits::max (); // only if unsigned halfPixel = maxPixel / 2; if (typeid (PixelT) != typeid (float)) halfPixel++; } template inline WeightBase::WeightBase (const PixelT *pixels, const Size &size) : pixels (nullptr), size (NullSize) { reset (pixels, size); } // ======================================== template inline bool WeightBase::isWeightInf (const WeightT &a, const WeightT &b) { return a < b; } template inline bool WeightBase::isEdgeInf (const Edge &a, const Edge &b) { return isWeightInf (a.weight, b.weight); } template inline void WeightBase::sort (Edge *edges, DimEdge count) { std::sort (edges, edges+count, isEdgeInf); } template inline void WeightBase::copyPixelsBound (PixelT *leafAPTree, const DimImg &minVal, const DimImg &maxVal) const { for (DimImg i = minVal; i < maxVal; ++i) leafAPTree[i] = pixels [i]; } template inline void WeightBase::weight2valueBound (PixelT *compAPTree, const WeightT *compWeights, const DimImg &minVal, const DimImg &maxVal) const { //memcpy (compAPTree+minVal, compWeights+minVal, maxVal-minVal); for (DimImg compIdx = minVal; compIdx < maxVal; ++compIdx) compAPTree[compIdx] = compWeights[compIdx]; } // ======================================== template inline bool MinWeight::getDecr () const { return true; } template inline bool MinWeight::isWeightInf (const WeightT &a, const WeightT &b) { return a > b; } template inline bool MinWeight::isEdgeInf (const Edge &a, const Edge &b) { return isWeightInf (a.weight, b.weight); } template inline void MinWeight::sort (Edge *edges, DimEdge count) { std::sort (edges, edges+count, isEdgeInf); } template inline MinWeight::MinWeight () : WB (nullptr, NullSize) { } template inline MinWeight::MinWeight (const PixelT *pixels, const Size &size) : WB (pixels, size) { } template inline MinWeight::MinWeight (const MinWeight &model, const PixelT *pixels, const Size &size) : WB (pixels, size) { } template inline WeightT MinWeight::getWeight (const DimImg &idx) const { return WB::getValue (idx); } template inline WeightT MinWeight::getWeight (const Point &a, const Point &b) const { return std::min (getWeight (WB::pointIdx (a)), getWeight (WB::pointIdx (b))); } // ======================================== template inline MaxWeight::MaxWeight () : WB (nullptr, NullSize) { } template inline MaxWeight::MaxWeight (const PixelT *pixels, const Size &size) : WB (pixels, size) { } template inline MaxWeight::MaxWeight (const MaxWeight &model, const PixelT *pixels, const Size &size) : WB (pixels, size) { } template inline WeightT MaxWeight::getWeight (const DimImg &idx) const { return WB::getValue (idx); } template inline WeightT MaxWeight::getWeight (const Point &a, const Point &b) const { return std::max (getWeight (WB::pointIdx (a)), getWeight (WB::pointIdx (b))); } // ======================================== template inline DiffWeight::DiffWeight () : WB (nullptr, NullSize) { } template inline DiffWeight::DiffWeight (const PixelT *pixels, const Size &size) : WB (pixels, size) { } template inline DiffWeight::DiffWeight (const DiffWeight &model, const PixelT *pixels, const Size &size) : WB (pixels, size) { } template inline WeightT DiffWeight::getWeight (const DimImg &idx) const { return 0; } template inline WeightT DiffWeight::getWeight (const Point &a, const Point &b) const { PixelT va = WB::getValue (a), vb = WB::getValue (b); return std::max (va, vb) - std::min (va, vb); } // ======================================== template inline bool MedianWeight::getDecr () const { return true; } template inline bool MedianWeight::isWeightInf (const WeightT &a, const WeightT &b) { return a > b; } template inline bool MedianWeight::isEdgeInf (const Edge &a, const Edge &b) { return isWeightInf (a.weight, b.weight); } template inline void MedianWeight::sort (Edge *edges, DimEdge count) { std::sort (edges, edges+count, isEdgeInf); } template inline const PixelT & MedianWeight::getMedian () const { return median; } template inline const PixelT & MedianWeight::getThresholdPixel () const { return thresholdPixel; } template inline const WeightT & MedianWeight::getThresholdWeight () const { return thresholdWeight; } template inline WeightT MedianWeight::value2weight (const PixelT &val) const { if (median < WB::halfPixel) { if (val >= thresholdPixel) return val; return val < median ? (median-val)*2 - 1 : (val-median)*2; } if (val < thresholdPixel) return WB::maxPixel - val; return val < median ? (median-val)*2 - 1 : (val-median)*2; } template inline PixelT MedianWeight::weight2value (const WeightT &weight) const { if (median < WB::halfPixel) { if (weight >= thresholdWeight) return weight; return int (weight) % 2 ? median - 1 - weight/2 : median + weight/2; } if (weight > thresholdWeight) return WB::maxPixel - weight; return int (weight) % 2 ? median - weight/2 : median + weight/2; } // ======================================== template inline MedianWeight::MedianWeight () : WB (nullptr, NullSize), median (0), thresholdPixel (0), thresholdWeight (0) { } template inline MedianWeight::MedianWeight (const PixelT *pixels, const GraphWalker &graphWalker) : WB (pixels, graphWalker.getSize ()) { median = graphWalker.getMedian (*this); thresholdPixel = median < WB::halfPixel ? median * 2 : WB::maxPixel - (WB::maxPixel-median) * 2; thresholdWeight = median < WB::halfPixel ? median * 2 : (WB::maxPixel-median) * 2; } template inline MedianWeight::MedianWeight (const MedianWeight &model, const PixelT *pixels, const Size &size) : WB (pixels, size), median (model.median), thresholdPixel (model.thresholdPixel), thresholdWeight (model.thresholdWeight) { } template inline WeightT MedianWeight::getWeight (const DimImg &idx) const { return value2weight (WB::getValue (idx)); } template inline WeightT MedianWeight::getWeight (const Point &a, const Point &b) const { return std::min (getWeight (WB::pointIdx (a)), getWeight (WB::pointIdx (b))); } template inline void MedianWeight::weight2valueBound (PixelT *compAPTree, const WeightT *compWeights, const DimImg &minVal, const DimImg &maxVal) const { for (DimImg compIdx = minVal; compIdx < maxVal; ++compIdx) compAPTree[compIdx] = weight2value (compWeights[compIdx]); } // ======================================== #endif // _OTB_TRISKELE_ARRAY_TREE_WEIGHT_TPP