1 | #include <iostream> |
---|
2 | #include <iomanip> |
---|
3 | #include <math.h> |
---|
4 | #include <vector> |
---|
5 | #include <Cubes/cubes.hh> |
---|
6 | #include <Detection/detection.hh> |
---|
7 | #include <Utils/utils.hh> |
---|
8 | |
---|
9 | /** |
---|
10 | * Cube::ObjectMerger(): |
---|
11 | * A Function that takes a Cube's list of Detections and |
---|
12 | * combines those that are close (according to the |
---|
13 | * thresholds specified in the parameter list par). |
---|
14 | * It also excludes those that do not make the minimum |
---|
15 | * number of channels requirement. |
---|
16 | */ |
---|
17 | void Cube::ObjectMerger() |
---|
18 | { |
---|
19 | |
---|
20 | if(this->objectList.size() > 0){ |
---|
21 | |
---|
22 | // make a vector "currentList", which starts as a copy of the Cube's objectList, |
---|
23 | // but is the one worked on. |
---|
24 | vector <Detection> *currentList = new vector <Detection>(this->objectList.size()); |
---|
25 | *currentList = this->objectList; |
---|
26 | vector <Detection>::iterator iter; |
---|
27 | // for(int i=0;i<this->objectList.size();i++) currentList->push_back( this->objectList[i] ); |
---|
28 | |
---|
29 | // The Cube's objectList is cleared, and then the final objects are added to it at the end. |
---|
30 | this->objectList.clear(); |
---|
31 | |
---|
32 | int counter, compCounter,numLoops; |
---|
33 | bool areNear,isMatch; |
---|
34 | long gap; |
---|
35 | |
---|
36 | bool isVerb = this->par.isVerbose(); |
---|
37 | |
---|
38 | if(this->par.getFlagGrowth()) numLoops=2; |
---|
39 | else numLoops = 1; |
---|
40 | |
---|
41 | for(int nloop = 0; nloop < numLoops; nloop++){ |
---|
42 | |
---|
43 | counter=0; |
---|
44 | while( counter < (currentList->size()-1) ){ |
---|
45 | if(isVerb){ |
---|
46 | std::cout.setf(std::ios::right); |
---|
47 | if(nloop>0) std::cout << "Progress#2: " << std::setw(6) << counter << "/" ; |
---|
48 | else std::cout << "Progress: " << std::setw(6) << counter << "/" ; |
---|
49 | std::cout.unsetf(std::ios::right); |
---|
50 | std::cout.setf(std::ios::left); |
---|
51 | std::cout << std::setw(6) << currentList->size() |
---|
52 | << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" << std::flush; |
---|
53 | if(nloop>0) std::cout << "\b\b" << std::flush; ; |
---|
54 | std::cout.unsetf(std::ios::left); |
---|
55 | } |
---|
56 | |
---|
57 | compCounter = counter + 1; |
---|
58 | do { |
---|
59 | |
---|
60 | Detection *obj1 = new Detection; |
---|
61 | *obj1 = currentList->at(counter); |
---|
62 | Detection *obj2 = new Detection; |
---|
63 | *obj2 = currentList->at(compCounter); |
---|
64 | |
---|
65 | if(areClose(*obj1, *obj2, this->par)){ |
---|
66 | obj1->addAnObject( *obj2 ); |
---|
67 | iter = currentList->begin() + compCounter; |
---|
68 | currentList->erase(iter); |
---|
69 | iter = currentList->begin() + counter; |
---|
70 | currentList->erase(iter); |
---|
71 | currentList->push_back( *obj1 ); |
---|
72 | // currentList->insert(iter, *obj1 ); |
---|
73 | if(isVerb){ |
---|
74 | std::cout.setf(std::ios::right); |
---|
75 | std::cout << "Progress: " |
---|
76 | << std::setw(6) << counter << "/"; |
---|
77 | std::cout.unsetf(std::ios::right); |
---|
78 | std::cout.setf(std::ios::left); |
---|
79 | std::cout << std::setw(6) << currentList->size() |
---|
80 | << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" << std::flush; |
---|
81 | std::cout.unsetf(std::ios::left); |
---|
82 | } |
---|
83 | compCounter = counter + 1; |
---|
84 | |
---|
85 | } |
---|
86 | else compCounter++; |
---|
87 | |
---|
88 | delete obj2; |
---|
89 | delete obj1; |
---|
90 | |
---|
91 | } while( (compCounter<currentList->size()) ); |
---|
92 | |
---|
93 | counter++; |
---|
94 | |
---|
95 | } // end of while(counter<(currentList->size()-1)) loop |
---|
96 | |
---|
97 | if( (nloop==0) && (this->par.getFlagGrowth()) ) { |
---|
98 | std::cout << "Growing objects... "<< std::flush; |
---|
99 | this->setCubeStats(); |
---|
100 | vector <Detection> *newList = new vector <Detection>; |
---|
101 | std::cout<< "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"<<std::flush; |
---|
102 | std::cout << " Growing object # "<< std::flush; |
---|
103 | std::cout.setf(std::ios::left); |
---|
104 | for(int i=0;i<currentList->size();i++){ |
---|
105 | std::cout<< "\b\b\b\b\b\b"<<std::setw(6)<<i+1<<std::flush; |
---|
106 | Detection *obj = new Detection; |
---|
107 | *obj = currentList->at(i); |
---|
108 | growObject(*obj,*this); |
---|
109 | newList->push_back(*obj); |
---|
110 | delete obj; |
---|
111 | } |
---|
112 | delete currentList; |
---|
113 | currentList = newList; |
---|
114 | std::cout.unsetf(std::ios::left); |
---|
115 | std::cout<< "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"<<std::flush; |
---|
116 | } |
---|
117 | |
---|
118 | } |
---|
119 | // when you get here, every detection in the input list has been |
---|
120 | // examined and compared to every other detection. |
---|
121 | |
---|
122 | int listCounter = 0; |
---|
123 | while(listCounter < currentList->size()){ |
---|
124 | |
---|
125 | (*currentList)[listCounter].addOffsets(this->par); |
---|
126 | (*currentList)[listCounter].calcParams(); |
---|
127 | |
---|
128 | // Now, for each object in the newly modified list, we need to check if |
---|
129 | // they pass the requirements on the minimum no. of channels and minimum |
---|
130 | // number of spatial pixels |
---|
131 | // If not, remove from list. |
---|
132 | |
---|
133 | if( (*currentList)[listCounter].hasEnoughChannels(this->par.getMinChannels()) |
---|
134 | && ((*currentList)[listCounter].getSpatialSize() >= this->par.getMinPix()) ){ |
---|
135 | listCounter++; |
---|
136 | if(isVerb){ |
---|
137 | std::cout << "Final total:"<<std::setw(5)<<listCounter<<" " |
---|
138 | << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"<<std::flush; |
---|
139 | } |
---|
140 | } |
---|
141 | else currentList->erase(currentList->begin()+listCounter); |
---|
142 | |
---|
143 | } |
---|
144 | |
---|
145 | this->objectList = *currentList; |
---|
146 | currentList->clear(); |
---|
147 | delete currentList; |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | // vector<Detection> *finalList = new vector<Detection>; |
---|
154 | // for(int listCounter = 0; listCounter < currentList->size(); listCounter++){ |
---|
155 | // Detection *currentObject = new Detection; |
---|
156 | // *currentObject = currentList->at(listCounter); |
---|
157 | // currentObject->addOffsets(this->par); |
---|
158 | // currentObject->calcParams(); |
---|
159 | // // Now, for each object in the newly modified list, we need to check if |
---|
160 | // // they pass the minimum no. of channels requirement |
---|
161 | // int numChannels = currentObject->getZmax() - currentObject->getZmin() + 1; |
---|
162 | // if( numChannels >= this->par.getMinChannels() ){ |
---|
163 | |
---|
164 | // /* |
---|
165 | // // Now remove any multiply-appearing pixels in each good object |
---|
166 | // // and add to the output list |
---|
167 | // Detection *newObject = new Detection; |
---|
168 | // // the first pixel gets included -- then compare all others... |
---|
169 | // newObject->addPixel(currentObject->getPixel(0)); |
---|
170 | // for(int pix = 1; pix < currentObject->getSize(); pix++){ |
---|
171 | // bool addThisOne = true; |
---|
172 | // for(int newpix = 0; newpix < newObject->getSize(); newpix++){ |
---|
173 | // bool newOneMatches = ( newObject->getX(newpix) == currentObject->getX(pix) ) |
---|
174 | // && ( newObject->getY(newpix) == currentObject->getY(pix) ) |
---|
175 | // && ( newObject->getZ(newpix) == currentObject->getZ(pix) ); |
---|
176 | // addThisOne = addThisOne && !newOneMatches; |
---|
177 | // } |
---|
178 | // if(addThisOne) newObject->addPixel(currentObject->getPixel(pix)); |
---|
179 | // } |
---|
180 | // newObject->addOffsets(this->par); |
---|
181 | // newObject->calcParams(); |
---|
182 | // this->objectList.push_back(*newObject); |
---|
183 | // */ |
---|
184 | // finalList->push_back(*currentObject); |
---|
185 | // if(isVerb){ |
---|
186 | // std::cout << "Final total:"<<std::setw(5)<<finalList->size() |
---|
187 | // << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"<<std::flush; |
---|
188 | // } |
---|
189 | // // delete newObject; |
---|
190 | // } |
---|
191 | |
---|
192 | // delete currentObject; |
---|
193 | // } |
---|
194 | |
---|
195 | // currentList->clear(); |
---|
196 | // delete currentList; |
---|
197 | |
---|
198 | // this->objectList = *finalList; |
---|
199 | // finalList->clear(); |
---|
200 | // delete finalList; |
---|
201 | |
---|
202 | // } |
---|
203 | |
---|
204 | } |
---|
205 | |
---|