-
Notifications
You must be signed in to change notification settings - Fork 2
/
bisGPLTestFunctions.cpp
361 lines (270 loc) · 12.7 KB
/
bisGPLTestFunctions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* License
_This file is Copyright 2018 by the Image Processing and Analysis Group (BioImage Suite Team). Dept. of Radiology & Biomedical Imaging, Yale School of Medicine._ It is released under the terms of the GPL v2.
----
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
See also http: www.gnu.org/licenses/gpl.html
If this software is modified please retain this statement and add a notice
that it had been modified (and by whom).
Endlicense */
#include "bisGPLTestFunctions.h"
#include "bisSimpleDataStructures.h"
#include "bisJSONParameterList.h"
#include "bisMatrixTransformation.h"
#include "bisGridTransformation.h"
#include "bisOptimizer.h"
#include "bisApproximateLandmarkDisplacementsWithGridTransform.h"
#include "bisRPMCorrespondenceFinder.h"
#include "bisPointLocator.h"
#include <memory>
class bisTestOptimizable : public bisOptimizableAlgorithm {
public:
bisTestOptimizable() : bisOptimizableAlgorithm("test") { };
float computeGradient(std::vector<float>& params,std::vector<float>& grad) {
int num=params.size();
if (num==2) {
float x=params[0],y=params[1];
float dx=2*(x-9), dy=2*y;
float s=float(sqrt(dx*dx+dy*dy)+0.00001);
grad[0]=dx/s; grad[1]=dy/s;
return s;
}
double x=params[0], dx=2.0f*(x-9.0f);
grad[0]=float(dx/fabs(dx));
return float(fabs(dx));
};
float computeValue(std::vector<float>& params) {
int num=params.size();
float x=params[0];
float v=0.0,y=0.0;
if (num==2)
y=params[1];
v=(x-9)*(x-9)+y*y;
return v;
};
float comparePos(std::vector<float> p,std::vector<float> tp) {
float sum=0.0;
for (unsigned int ia=0;ia<p.size();ia++)
sum+=powf(p[ia]-tp[ia],2.0f);
return sum;
}
};
int test_optimizer(int numparam) {
int numdof=numparam;
int numfail=0;
for (int mode=0;mode<=2;mode++)
{
std::unique_ptr<bisTestOptimizable> test_optimizable(new bisTestOptimizable());
std::unique_ptr<bisOptimizer> optimizer(new bisOptimizer(test_optimizable.get()));
std::vector<float> position(numdof);
position[0]=15;
if (numdof>1)
position[1]=5;
std::vector<float> truepos(numdof);
truepos[0]=9;
if (numdof>1)
truepos[1]=0;
if (position.size()==2)
std::cout << std::endl << "________________ mode=" << mode << " pos=" << position[0] << ", " << position[1] << std::endl;
else
std::cout << std::endl << "________________ mode=" << mode << " pos=" << position[0] << ", " << std::endl;
if (mode==0)
optimizer->computeSlowClimb(position,0.5,25);
else if (mode==1)
optimizer->computeGradientDescent(position,25,0.01f);
else
optimizer->computeConjugateGradient(position,25,0.01f);
float d=test_optimizable->comparePos(position,truepos);
if (d>0.1)
numfail++;
if (position.size()==2)
std::cout << "\t\t Final " << position[0] << ", " << position[1] << ". Diff2=" << d << " numfail=" << numfail << std::endl;
else
std::cout << "\t\t Final " << position[0] << ". Diff2=" << d << " numfail=" << numfail << std::endl;
if (d>0.1)
return numfail;
}
return numfail;
}
// ------------------------------------------------------------------------
unsigned char* test_landmarkApproximationWASM(unsigned char* in_source_ptr,
unsigned char* in_target_ptr,
const char* jsonstring,
int debug)
{
std::unique_ptr<bisJSONParameterList> params(new bisJSONParameterList());
if (!params->parseJSONString(jsonstring))
return 0;
float spacing=params->getFloatValue("spacing",20.0);
if (debug)
std::cout << "___ spacing=" << spacing << std::endl;
std::unique_ptr<bisSimpleMatrix<float> > source(new bisSimpleMatrix<float>("source_points_json"));
if (!source->linkIntoPointer(in_source_ptr))
return 0;
if (debug)
std::cout << "___ Ref Allocated = " << source->getNumRows() << "*" << source->getNumCols() << std::endl;
std::unique_ptr<bisSimpleMatrix<float> > target(new bisSimpleMatrix<float>("target_points_json"));
if (!target->linkIntoPointer(in_target_ptr))
return 0;
if (debug)
std::cout << "___ Target Allocated = " << target->getNumRows() << "*" << target->getNumCols() << std::endl;
int numpoints=source->getNumRows();
std::unique_ptr<bisSimpleVector<float> > weights(new bisSimpleVector<float>("weights_points_json"));
weights->allocate(numpoints);
weights->fill(1.0);
if (debug)
std::cout << "___ Weights Allocated = " << weights->getLength() << std::endl;
float minc[3],maxc[3];
float* pts=source->getData();
// Find min and max and create bounds
for (int ia=0;ia<=2;ia++) {
minc[ia]=pts[ia];
maxc[ia]=pts[ia];
}
for (int index=1;index<numpoints;index++) {
int offset=index*3;
for (int ia=0;ia<=2;ia++) {
float p=pts[offset+ia];
if (minc[ia]>p) minc[ia]=p;
if (maxc[ia]<p) maxc[ia]=p;
}
}
int dim[3];
float ori[3],spa[3];
for (int ia=0;ia<=2;ia++) {
float l=maxc[ia]-minc[ia];
dim[ia]=int(l/spacing)+1;
float l2=(dim[ia]-1)*spacing;
ori[ia]=minc[ia]-(l2-l)*0.5;
spa[ia]=spacing;
}
if (debug) {
std::cout << "__ Creating Grid " << std::endl;
std::cout << "__ min bounds " << minc[0] << "," << minc[1] << "," << minc[2] << std::endl;
std::cout << "__ max bounds " << maxc[0] << "," << maxc[1] << "," << maxc[2] << std::endl;
std::cout << "__ grid dim " << dim[0] << "," << dim[1] << "," << dim[2] << std::endl;
std::cout << "__ grid spa " << spa[0] << "," << spa[1] << "," << spa[2] << std::endl;
std::cout << "__ grid ori " << ori[0] << "," << ori[1] << "," << ori[2] << std::endl;
}
std::unique_ptr<bisGridTransformation> grid(new bisGridTransformation());
grid->initializeGrid(dim,spa,ori,1);
std::unique_ptr<bisApproximateLandmarkDisplacementsWithGridTransform> landmarkFit(new bisApproximateLandmarkDisplacementsWithGridTransform());
landmarkFit->run(source.get(),target.get(),weights.get(),grid.get(),params.get(),debug);
if (debug)
std::cout << "___ Transforming points " << std::endl;
std::unique_ptr<bisSimpleMatrix<float> > output(new bisSimpleMatrix<float>("warped_points_json"));
output->zero(source->getNumRows(),3);
float* srcPts=source->getData();
float* outPts=output->getData();
for (int i=0;i<source->getNumRows();i++) {
float x1[3],x2[3];
for (int ia=0;ia<=2;ia++)
x1[ia]=srcPts[i*3+ia];
grid->transformPoint(x1,x2);
for (int ia=0;ia<=2;ia++)
outPts[i*3+ia]=x2[ia];
}
return output->releaseAndReturnRawArray();
}
// ------------------------------------------------------------------------
unsigned char* test_rpmCorrespondenceEstimatorWASM(unsigned char* in_source,
unsigned char* in_target,
const char* jsonstring,
int debug)
{
std::unique_ptr<bisJSONParameterList> params(new bisJSONParameterList());
if (!params->parseJSONString(jsonstring))
return 0;
float temperature=params->getFloatValue("temperature",10.0);
int mode=params->getIntValue("mode",2);
int numlandmarks=params->getIntValue("numpoints",1000);
if (debug)
std::cout << "___ temperature=" << temperature << " mode=" << mode << " numpoints=" << numlandmarks << std::endl;
std::unique_ptr<bisSimpleMatrix<float> > source(new bisSimpleMatrix<float>("source_points_json"));
if (!source->linkIntoPointer(in_source))
return 0;
if (debug)
std::cout << "___ Ref Allocated = " << source->getNumRows() << "*" << source->getNumCols() << std::endl;
std::shared_ptr<bisSimpleMatrix<float> > target(new bisSimpleMatrix<float>("target_points_json"));
if (!target->linkIntoPointer(in_target))
return 0;
if (debug)
std::cout << "___ Target Allocated = " << target->getNumRows() << "*" << target->getNumCols() << std::endl;
std::unique_ptr<bisPointLocator> locator(new bisPointLocator());
locator->initialize(target,0.2,0);
std::unique_ptr<bisMatrixTransformation> matrix(new bisMatrixTransformation());
matrix->identity();
int numref=source->getNumRows();
int numtarget=target->getNumRows();
std::unique_ptr<bisSimpleMatrix<float> > OutputRefLandmarks(new bisSimpleMatrix<float>());
std::unique_ptr<bisSimpleMatrix<float> > OutputTargetLandmarks(new bisSimpleMatrix<float>());
std::unique_ptr<bisSimpleVector<float> > OutputWeights(new bisSimpleVector<float>());
OutputRefLandmarks->zero(numref,3);
OutputTargetLandmarks->zero(numref,3);
OutputWeights->zero(numref);
std::unique_ptr<bisSimpleVector<int> > RefLabels(new bisSimpleVector<int>());
RefLabels->zero(numref);
std::unique_ptr<bisSimpleVector<int> > TargLabels(new bisSimpleVector<int>());
TargLabels->zero(numtarget);
if (mode == 0) {
bisRPMCorrespondenceFinder::computeCorrespodnencesICP(matrix.get(),locator.get(),
source->getData(),
OutputRefLandmarks->getData(),
OutputTargetLandmarks->getData(),
OutputWeights->getData(),
numref,debug);
return OutputTargetLandmarks->releaseAndReturnRawArray();
}
bisRPMCorrespondenceFinder::computeCorrespondencesRPM(matrix.get(),locator.get(),
mode,
source->getData(),RefLabels->getData(),
target->getData(),TargLabels->getData(),
OutputRefLandmarks->getData(),
OutputTargetLandmarks->getData(),
OutputWeights->getData(),
temperature,numref,numtarget,debug);
// std::cout << "M=" << M << std::endl;
return OutputTargetLandmarks->releaseAndReturnRawArray();
}
unsigned char* test_rpmSamplingWASM(unsigned char* in_points,
unsigned char* in_labels,
const char* jsonstring,
int debug)
{
std::unique_ptr<bisJSONParameterList> params(new bisJSONParameterList());
if (!params->parseJSONString(jsonstring))
return 0;
int prefsampling=params->getIntValue("prefsampling",2);
int numlandmarks=params->getIntValue("numpoints",1000);
if (debug)
std::cout << " prefsampling=" << prefsampling << " numpoints=" << numlandmarks << std::endl;
std::unique_ptr<bisSimpleMatrix<float> > points(new bisSimpleMatrix<float>("points_points_json"));
if (!points->linkIntoPointer(in_points))
return 0;
if (debug)
std::cout << "___ Ref Allocated = " << points->getNumRows() << "*" << points->getNumCols() << std::endl;
std::unique_ptr<bisSimpleVector<int> > labels(new bisSimpleVector<int>("labels_points_json"));
if (!labels->linkIntoPointer(in_labels))
return 0;
if (debug)
std::cout << "___ Labels Allocated = " << labels->getLength() << std::endl;
std::unique_ptr<bisSimpleMatrix<float> > out_points(new bisSimpleMatrix<float>("out_points_points_json"));
std::unique_ptr<bisSimpleVector<int> > out_labels(new bisSimpleVector<int>("out_labels_points_json"));
bisRPMCorrespondenceFinder::samplePoints(points.get(),
labels.get(),
numlandmarks,
prefsampling,
out_points.get(),
out_labels.get(),
debug);
return out_points->releaseAndReturnRawArray();
}