00001 #ifndef Impala_Core_Array_Trait_QbWatershedMarkers_h
00002 #define Impala_Core_Array_Trait_QbWatershedMarkers_h
00003
00004 #include "Core/Geometry/PointZ.h"
00005 #include "Core/Array/Pattern/FcvArray.h"
00006 #include "Core/Array/Pattern/QCollection.h"
00007 #include "Core/Array/Arrays.h"
00008
00009 namespace Impala
00010 {
00011 namespace Core
00012 {
00013 namespace Array
00014 {
00015 namespace Trait
00016 {
00017
00018
00019 template<class DstArrayT, class Src1ArrayT, class Src2ArrayT>
00020 class QbWatershedMarkers {
00021
00022 typedef typename DstArrayT::ArithType DstArithT;
00023 typedef typename Src1ArrayT::ArithType Src1ArithT;
00024 typedef typename Src2ArrayT::ArithType Src2ArithT;
00025
00026 public:
00027
00028 typedef struct structPointValue {
00029 DstArithT arith;
00030 Src1ArithT img;
00031 Src2ArithT mask;
00032 Geometry::PointZ point;
00033 int cost;
00034
00035 bool
00036 less(const struct structPointValue& rhs) const
00037 {
00038 if (img<rhs.img)
00039 return true;
00040 if (img>rhs.img)
00041 return false;
00042
00043 return localOrderCounter<rhs.localOrderCounter;
00044 }
00045
00046 bool
00047 operator < (const struct structPointValue& rhs) const
00048 {
00049
00050 return less(rhs);
00051 }
00052
00053 unsigned localOrderCounter;
00054 } PointValueT;
00055
00056
00057 typedef Pattern::Qset<PointValueT> QT;
00058
00059 typedef struct {
00060 Geometry::PointZ point;
00061 DstArithT arith;
00062 Src1ArithT img;
00063 Src2ArithT mask;
00064 } NeighborsT;
00065
00066 typedef Pattern::FcvArray<NeighborsT> VecNeighborsT;
00067
00068 QbWatershedMarkers(bool doLabelMask)
00069 {
00070 labelcode=0;
00071 doLabel = doLabelMask;
00072 globalOrderCounter=0;
00073 }
00074
00075 ~QbWatershedMarkers()
00076 {
00077 }
00078
00079 bool
00080 WantAnotherLoop()
00081 {
00082 return false;
00083 }
00084
00085 bool
00086 WantFreshStartLocalPixelInit()
00087 {
00088 return true;
00089 }
00090
00091 bool
00092 LocalPixelInit(PointValueT& vp, DstArithT&, Src1ArithT, Src2ArithT, bool& continueloop)
00093 {
00094 continueloop=false;
00095 return false;
00096 }
00097
00098 bool
00099 GlobalPixelInit(PointValueT& vp, DstArithT& arith, Src1ArithT img, Src2ArithT mask)
00100 {
00101 vp.arith=arith;
00102 vp.img=img;
00103 vp.mask=mask;
00104 bool q;
00105 if (vp.mask!=0) {
00106 q=true;
00107 if (doLabel) {
00108 labelcode++;
00109 vp.arith=labelcode;
00110 } else {
00111 vp.arith=vp.mask;
00112 }
00113 } else {
00114 vp.arith=0;
00115 q=false;
00116 }
00117 if (q) {
00118 vp.localOrderCounter=globalOrderCounter++;
00119
00120 }
00121 arith=vp.arith;
00122 return q;
00123 }
00124
00125 void
00126 First(const PointValueT& val, DstArithT, Src1ArithT, Src2ArithT)
00127 {
00128 center=val;
00129 }
00130
00131 void
00132 Calculate(VecNeighborsT& vecneighbors)
00133 {
00134 index=0;
00135 neighboursout.clear();
00136 actionsout.clear();
00137 for (int i=0; i<vecneighbors.size(); i++) {
00138 NeighborsT neighbor=vecneighbors[i];
00139 PointValueT val;
00140 val.point=neighbor.point;
00141 val.arith=neighbor.arith;
00142 val.img=neighbor.img;
00143 val.mask=neighbor.mask;
00144 if (val.arith==0) {
00145
00146 val.arith=center.arith;
00147 val.localOrderCounter=globalOrderCounter++;
00148
00149 neighboursout.push_back(val);
00150 actionsout.push_back(Pattern::WRITE);
00151 neighboursout.push_back(val);
00152 actionsout.push_back(Pattern::QUEUE);
00153 }
00154 }
00155 }
00156
00157 Pattern::QAction
00158 Result()
00159 {
00160 if (index>=neighboursout.size())
00161 return Pattern::STOP;
00162 resultVP=neighboursout[index];
00163 Pattern::QAction action=actionsout[index];
00164 index++;
00165 return action;
00166 }
00167
00168 void
00169 GetItemToQueue(PointValueT &vp)
00170 {
00171 vp=resultVP;
00172 }
00173
00174
00175
00176 void
00177 GetItemToRemove(PointValueT& lower, PointValueT& upper)
00178 {
00179 lower.cost=upper.cost=resultVP.cost;
00180 lower.localOrderCounter=0;
00181 upper.localOrderCounter=4000000000;
00182 }
00183
00184 bool
00185 KillThisOne(const PointValueT& vp)
00186 {
00187 if (resultVP.cost==vp.cost && resultVP.point==vp.point)
00188 return true;
00189 return false;
00190 }
00191
00192 void
00193 GetItemToWrite(Geometry::PointZ& point, DstArithT& arith)
00194 {
00195 point=resultVP.point;
00196 arith=resultVP.arith;
00197 }
00198
00199 private:
00200
00201 PointValueT center;
00202 Pattern::FcvArray<PointValueT> neighboursout;
00203 Pattern::FcvArray<Pattern::QAction> actionsout;
00204 int index;
00205 int labelcode;
00206 bool doLabel;
00207 PointValueT resultVP;
00208 unsigned globalOrderCounter;
00209 };
00210
00211 }
00212 }
00213 }
00214 }
00215
00216 #endif