00001 #ifndef _Beh_SearchForObject_txx
00002 #define _Beh_SearchForObject_txx
00003
00004 #include "Beh_SearchForObject.h"
00005
00006 namespace mial {
00007
00008 template<class Type, class TInputImage, int nDims>
00009 Beh_SearchForObject<Type,TInputImage,nDims>::Beh_SearchForObject()
00010 :Behavior<Type,nDims>("Beh_SearchForObject")
00011 {
00012
00013 srand ( time(NULL) );
00014 avgIntensitySensor = AvgIntensitySensorType::New();
00015 translateBehavior = TranslateBehaviorType::New();
00016 scaleBehavior = ScaleBehaviorType::New();
00017
00018 startTime =0;
00019 endTime =0;
00020 found = false;
00021 }
00022 template<class Type, class TInputImage, int nDims>
00023 bool Beh_SearchForObject<Type,TInputImage,nDims>::run(typename Behavior<Type,nDims>::behaviorIn * i,std::stringstream *s)
00024 {
00025
00026 typename behaviorIn::Pointer in;
00027
00028 if(i != NULL && s != NULL)
00029 {
00030 Error e;
00031 e.msg = "Only one of struct or stream input may be provided.";
00032 e.name = "Beh_SearchForObject";
00033 throw e;
00034 }
00035 else if(i != NULL)
00036 in = reinterpret_cast<behaviorIn *>(i);
00037 else if( s!= NULL)
00038 {
00039 in = behaviorIn::New();
00040 in->fillFromStream(*s);
00041 }
00042 else
00043 {
00044 Error e;
00045 e.msg = "Either struct or stream input must be provided.";
00046 e.name = "Beh_SearchForObject";
00047 throw e;
00048 }
00049
00050 input = in;
00051 startTime = this->physLayer->getTime();
00052 endTime = startTime+in->duration;
00053
00054
00055 this->translateBehavior->setPhysLayer(this->physLayer);
00056 scaleBehavior->setPhysLayer(this->physLayer);
00057
00058 if(!(this->isFinished()))
00059 {
00060
00061 VectorType dir = in->translateLoc - geomLayer->getCentroid();
00062 dir.normalize();
00063
00064
00065
00066 this->translate(dir);
00067 }
00068 else
00069 {
00070 if(scaleBehavior->isFinished())
00071 this->scale();
00072 }
00073
00074
00075 return false;
00076 }
00077 template<class Type, class TInputImage, int nDims>
00078 bool Beh_SearchForObject<Type,TInputImage,nDims>::update()
00079 {
00080 VectorType dir = VectorType(nDims);
00081 if(!this->isFinished())
00082 {
00083 if(!found)
00084 {
00085 if(translateBehavior->isFinished())
00086 {
00087 for(int i=0;i<nDims;i++)
00088 dir.put(i,rand()-0.5);
00089 dir.normalize();
00090 this->translate(dir);
00091 }
00092 }
00093 else
00094 {
00095 std::cout << "Intensity level located." << std::endl;
00096 if(scaleBehavior->isFinished())
00097 this->scale();
00098
00099 }
00100 }
00101 return false;
00102 }
00103 template<class Type, class TInputImage, int nDims>
00104 bool Beh_SearchForObject<Type,TInputImage,nDims>::isFinished()
00105 {
00106
00107 typename AvgIntensitySensorType::sensorIn::Pointer avgInput = AvgIntensitySensorType::sensorIn::New();
00108
00109 this->avgInput->imageIn = image;
00110 this->avgInput->geom = geomLayer;
00111 avgIntensitySensor->run(avgInput);
00112 typename AvgIntensitySensorType::sensorOut::Pointer output = (typename AvgIntensitySensorType::sensorOut *) (avgIntensitySensor->getOutput()).GetPointer();
00113
00114 if( (this->output->avgIntensity >= input->intensityRequirement))
00115 found = true;
00116 else
00117 found = false;
00118
00119 return found && (this->physLayer->getTime()>endTime);
00120 }
00121 template<class Type, class TInputImage, int nDims>
00122 void Beh_SearchForObject<Type,TInputImage,nDims>::scale()
00123 {
00124
00125 scaleBehavior->cleanUp();
00126
00127 std::stringstream scaleArgs;
00128
00129 scaleArgs << 2.0 << " ";
00130 scaleArgs << 1.5 << " ";
00131 scaleBehavior->run(NULL,&scaleArgs);
00132
00133 }
00134
00135 template<class Type, class TInputImage, int nDims>
00136 void Beh_SearchForObject<Type,TInputImage,nDims>::translate(VectorType &loc)
00137 {
00138
00139 translateBehavior->cleanUp();
00140
00141 std::stringstream transArgs;
00142
00143 transArgs << 5.0 << " ";
00144 for(int i=0;i<nDims;i++)
00145 {
00146 transArgs << 25*loc(i) << " ";
00147 }
00148 translateBehavior->run(NULL,&transArgs);
00149
00150 }
00151 template<class Type, class TInputImage, int nDims>
00152 void Beh_SearchForObject<Type,TInputImage,nDims>::cleanUp()
00153 {
00154 startTime =0;
00155 endTime =0;
00156 found = false;
00157 }
00158 }
00159
00160 #endif