| Bsoft 2.0.5 Bernard's software package

model_transform.cpp
Go to the documentation of this file.
1
9 #include "model_transform.h"
10 #include "model_compare.h"
11 #include "model_util.h"
12 #include "Transform.h"
13 #include "linked_list.h"
14 #include "utilities.h"
15
16 // Declaration of global variables
17 extern int verbose; // Level of output to the screen
18
27 long model_center(Bmodel* model)
28 {
29 Vector3<double> com = model_center_of_mass(model);
30
31 return model_shift(model, -com);
32 }
33
43 long model_shift(Bmodel* model, Vector3<double> shift)
44 {
45 long ncomp(0);
46 Bcomponent* comp;
47
48 if ( verbose & VERB_FULL )
49 cout << "Shifting " << model->id << " by " << shift << endl;
50
51 for ( comp = model->comp; comp; comp = comp->next, ncomp++ )
52 comp->loc += shift;
53
54 return ncomp;
55 }
56
66 long models_shift(Bmodel* model, Vector3<double> shift)
67 {
68 long ncomp(0);
69 Bmodel* mp;
70
71 for ( mp = model; mp; mp = mp->next )
72 ncomp += model_shift(mp, shift);
73
74 return ncomp;
75 }
76
87 long model_scale(Bmodel* model, Vector3<double> scale, Vector3<double> origin)
88 {
89 if ( scale == 1 ) return 0;
90
91 long ncomp(0);
92 Bcomponent* comp;
93 Blink* link;
94
95 if ( verbose & VERB_FULL )
96 cout << "Scaling " << model->id << " by " << scale << endl;
97
98 for ( link = model->link; link; link = link->next )
99 link->length /= link->comp[0]->loc.distance(link->comp[1]->loc);
100
101 for ( comp = model->comp; comp; comp = comp->next, ncomp++ )
102 comp->loc = (comp->loc - origin) * scale + origin;
103
104 for ( link = model->link; link; link = link->next )
105 link->length *= link->comp[0]->loc.distance(link->comp[1]->loc);
106
107 return ncomp;
108 }
109
117 long models_scale(Bmodel* model, Vector3<double> scale, Vector3<double> origin)
118 {
119 if ( scale == 1 ) return 0;
120
121 long ncomp(0);
122 Bmodel* mp;
123
124 if ( verbose & VERB_FULL )
125 cout << "Scaling: " << scale << endl << endl;
126
127 for ( mp = model; mp; mp = mp->next )
128 ncomp += model_scale(mp, scale, origin);
129
130 return ncomp;
131 }
132
143 long model_reflect(Bmodel* model, Vector3<double> normal, Vector3<double> origin)
144 {
145 long ncomp(0);
146 double d;
147 Bcomponent* comp;
148
149 normal.normalize();
150
151 if ( verbose & VERB_FULL )
152 cout << "Reflecting " << model->id << " through " << normal << endl;
153
154 for ( comp = model->comp; comp; comp = comp->next, ncomp++ ) {
155 comp->loc -= origin;
156 d = -2 * comp->loc.scalar(normal);
157 comp->loc += normal * d;
158 comp->loc += origin;
159 }
160
161 return ncomp;
162 }
163
171 long models_reflect(Bmodel* model, Vector3<double> normal, Vector3<double> origin)
172 {
173 long ncomp(0);
174 Bmodel* mp;
175
176 for ( mp = model; mp; mp = mp->next )
177 ncomp += model_scale(mp, normal, origin);
178
179 return ncomp;
180 }
181
192 double model_reflect_and_compare(Bmodel* model, Vector3<double> normal, Vector3<double> origin)
193 {
194 Bcomponent* comp;
195 Bcomponent* complist = NULL;
196 Bcomponent* comp2 = NULL;
197
198 long n = 0;
199 double d, dmin, R = 0;
200
201 normal.normalize();
202
203 for ( comp = model->comp; comp; comp = comp->next ) {
204 comp2 = (Bcomponent *) add_item((char **) &comp2, sizeof(Bcomponent));
205 if ( !complist ) complist = comp2;
206 comp2->loc = comp->loc - origin;;
207 d = -2 * comp2->loc.scalar(normal);
208 comp2->loc += normal * d;
209 comp2->loc += origin;
210 }
211
212 for ( comp = model->comp; comp; comp = comp->next ) {
213 dmin = 1e30;
214 for ( comp2 = complist; comp2; comp2 = comp2->next ) {
215 d = comp->loc.distance(comp2->loc);
216 if ( dmin > d ) dmin = d;
217 }
218 R += dmin*dmin;
219 n++;
220 }
221
222 R = sqrt(R/n);
223
224 kill_list((char *) complist, sizeof(Bcomponent));
225
226 return R;
227 }
228
238 long model_rotate(Bmodel* model, Matrix3 mat)
239 {
240 Vector3<double> origin, shift;
241 return model_rotate(model, mat, origin, shift);
242 }
243
254 long model_rotate(Bmodel* model, Matrix3 mat, Vector3<double> origin)
255 {
256 Vector3<double> shift;
257 return model_rotate(model, mat, origin, shift);
258 }
259
271 long model_rotate(Bmodel* model, Matrix3 mat, Vector3<double> origin, Vector3<double> shift)
272 {
273 long ncomp(0);
274 Bcomponent* comp;
275
276 if ( verbose & VERB_FULL ) {
277 cout << "Rotating " << model->id << endl;
278 cout << "Rotation matrix:" << endl;
279 cout << mat << endl;
280 cout << "Origin: " << origin << endl;
281 cout << "Translation: " << shift << endl << endl;
282 }
283
284 Matrix3 vmat;
285
286 for ( comp = model->comp; comp; comp = comp->next, ncomp++ ) {
287 comp->loc = comp->loc - origin;
288 comp->loc = mat * comp->loc;
289 comp->loc = comp->loc + shift;
290 vmat = comp->view.matrix();
291 vmat = vmat * mat;
292 comp->view = View(vmat);
293 // comp->view = (View(vmat)).backward();
294 // cout << comp->view << endl;
295 }
296
297 return 0;
298 }
299
309 long model_rotate(Bmodel* model, View view)
310 {
311 Vector3<double> origin, shift;
312 Matrix3 mat = view.matrix();
313 return model_rotate(model, mat, origin, shift);
314 }
315
327 long model_rotate(Bmodel* model, View view, Vector3<double> origin, Vector3<double> shift)
328 {
329 Matrix3 mat = view.matrix();
330 return model_rotate(model, mat, origin, shift);
331 }
332
342 long model_rotate(Bmodel* model, Transform t)
343 {
344 if ( verbose ) {
345 cout << "Rotating:" << endl;
346 cout << "Axis: " << t.axis << endl;
347 cout << "Angle: " << t.angle*180.0/M_PI << endl;
348 cout << "Origin: " << t.origin << endl;
349 cout << "Translation: " << t.trans << endl << endl;
350 }
351
352 Matrix3 mat = Matrix3(t.axis, t.angle);
353 Vector3<double> shift = t.origin + t.trans;
354
355 // cout << mat << endl;
356
357 return model_rotate(model, mat, t.origin, shift);
358 }
359
369 double model_rotate_and_compare(Bmodel* model, Transform t)
370 {
371 Bcomponent* comp;
372 Bcomponent* complist = NULL;
373 Bcomponent* comp2 = NULL;
374
375 long n = 0;
376 double d, dmin, R = 0;
377
378 Matrix3 mat = Matrix3(t.axis, t.angle);
379 Vector3<double> shift = t.origin + t.trans;
380
381 for ( comp = model->comp; comp; comp = comp->next ) {
382 comp2 = (Bcomponent *) add_item((char **) &comp2, sizeof(Bcomponent));
383 if ( !complist ) complist = comp2;
384 comp2->loc = comp->loc - t.origin;
385 comp2->loc = mat * comp2->loc;
386 comp2->loc = comp2->loc + shift;
387 }
388
389 for ( comp = model->comp; comp; comp = comp->next ) {
390 dmin = 1e30;
391 for ( comp2 = complist; comp2; comp2 = comp2->next ) {
392 d = comp->loc.distance(comp2->loc);
393 if ( dmin > d ) dmin = d;
394 }
395 R += dmin*dmin;
396 n++;
397 }
398
399 R = sqrt(R/n);
400
401 kill_list((char *) complist, sizeof(Bcomponent));
402
403 return R;
404 }
405
416 long model_adjust_for_binning(Bmodel* model, Vector3<long> bin)
417 {
418 if ( bin[0]*bin[1]*bin[2] < 2 ) return 0;
419
420 long ncomp(0);
421 Bcomponent* comp = NULL;
422 Vector3<double> binf(bin[0], bin[1], bin[2]);
423
424 if ( verbose )
425 cout << "Unbinning: " << binf << endl << endl;
426
427 for ( comp = model->comp; comp; comp = comp->next, ncomp++ )
428 comp->loc *= binf;
429
430 return ncomp;
431 }
432
443 long model_align_to_guide(Bmodel* model, Bmodel* guide)
444 {
445 Vector3<double> eigenvec[3];
446
447 model_principal_axes(guide, eigenvec);
448
449 Matrix3 mat(eigenvec[0][0], eigenvec[0][1], eigenvec[0][2],
450 eigenvec[1][0], eigenvec[1][1], eigenvec[1][2],
451 eigenvec[2][0], eigenvec[2][1], eigenvec[2][2]);
452
453 mat = mat.transpose();
454
455 cout << mat << endl;
456
457 return model_rotate(model, mat);
458 }
459
470 Transform model_find_transform(Bmodel* model, Bmodel* refmod)
471 {
472 Transform t;
473
474 if ( model_component_number_difference(model, refmod) ) return t;
475
476 if ( verbose & VERB_PROCESS )
477 cout << "Mapping model " << model->id << " to reference " << refmod->id << endl;
478
479 long i, j;
480 Bcomponent* comp, *compr;
481 // double bx[4], by[4], bz[4], v[4] = {0,0,0,1};
482 vector<double> bx(4), by(4), bz(4), v(4,0);
483 Matrix a(4,4);
484
485 v[3] = 1;
486
487 for ( i=0; i<4; i++ ) bx[i] = by[i] = bz[i] = 0;
488
489 Vector3<double> com = model_center_of_mass(model);
490 Vector3<double> comr = model_center_of_mass(refmod);
491 Vector3<double> loc, locr;
492
493 for ( comp = model->comp, compr = refmod->comp; comp && compr; comp = comp->next, compr = compr->next ) {
494 if ( verbose )
495 cout << "Mapping component " << comp->id << " to reference " << compr->id << endl;
496 loc = comp->loc - com;
497 locr = compr->loc - comr;
498 cout << loc[2] << tab << locr[2] << tab << loc[2] - locr[2] << endl;
499 v[0] = loc[0];
500 v[1] = loc[1];
501 v[2] = loc[2];
502 for ( i=0; i<4; i++ ) {
503 bx[i] += v[i]*locr[0];
504 by[i] += v[i]*locr[1];
505 bz[i] += v[i]*locr[2];
506 for ( j=0; j<=i; j++ ) a[i][j] += v[i]*v[j];
507 }
508 }
509
510 t = transform_matrix_solve(a, bx, by, bz, 0);
511 t.trans = comr - com;
512 t.origin = com;
513
514 return t;
515 }
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
Vector3::normalize
double normalize()
Definition: Vector3.h:236
model_rotate_and_compare
double model_rotate_and_compare(Bmodel *model, Transform t)
Copies and rotates the model and compares it with the original.
Definition: model_transform.cpp:369
Blink
Definition: rwmodel.h:82
Bmodel::id
Bstring id
Definition: rwmodel.h:121
utilities.h
Header file for general utilities.
model_util.h
Library routines used for model processing.
model_shift
long model_shift(Bmodel *model, Vector3< double > shift)
Shifts a model.
Definition: model_transform.cpp:43
Transform::angle
double angle
Definition: Transform.h:29
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
Matrix3
Definition: Matrix3.h:17
Transform.h
Class for a generalized transform object.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
Blink::comp
Bcomponent * comp[2]
Definition: rwmodel.h:84
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
model_center
long model_center(Bmodel *model)
Shifts one models to its center of mass.
Definition: model_transform.cpp:27
Transform
Definition: Transform.h:24
M_PI
#define M_PI
Definition: utilities.h:59
linked_list.h
Header file for generalized linked list functions.
model_adjust_for_binning
long model_adjust_for_binning(Bmodel *model, Vector3< long > bin)
A binned model list is converted back to the unbinned version.
Definition: model_transform.cpp:416
Blink::length
float length
Definition: rwmodel.h:86
Bcomponent::view
View view
Definition: rwmodel.h:59
Blink::next
Blink * next
Definition: rwmodel.h:83
Vector3::scalar
double scalar(const Vector3 &v) const
Definition: Vector3.h:247
models_scale
long models_scale(Bmodel *model, Vector3< double > scale, Vector3< double > origin)
Scales a model.
Definition: model_transform.cpp:117
model_reflect
long model_reflect(Bmodel *model, Vector3< double > normal, Vector3< double > origin)
Reflects the model through a mirror plane.
Definition: model_transform.cpp:143
model_rotate
long model_rotate(Bmodel *model, Matrix3 mat)
Rotates the model.
Definition: model_transform.cpp:238
Transform::trans
Vector3< double > trans
Definition: Transform.h:30
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
model
Bmodel * model
Definition: bshow.cpp:32
Transform::origin
Vector3< double > origin
Definition: Transform.h:27
model_reflect_and_compare
double model_reflect_and_compare(Bmodel *model, Vector3< double > normal, Vector3< double > origin)
Copies and reflects the model and compares it with the original.
Definition: model_transform.cpp:192
Matrix
Definition: Matrix.h:21
model_scale
long model_scale(Bmodel *model, Vector3< double > scale, Vector3< double > origin)
Scales a model.
Definition: model_transform.cpp:87
View::matrix
Matrix3 matrix()
Definition: View.h:150
Bmodel::link
Blink * link
Definition: rwmodel.h:133
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
model_principal_axes
Vector3< double > model_principal_axes(Bmodel *model, Vector3< double > *eigenvec)
Calculates the principal axes of a model.
Definition: model_util.cpp:534
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
models_shift
long models_shift(Bmodel *model, Vector3< double > shift)
Shifts all models.
Definition: model_transform.cpp:66
Vector3< double >
model_find_transform
Transform model_find_transform(Bmodel *model, Bmodel *refmod)
A model is fitted to a reference model.
Definition: model_transform.cpp:470
View
Definition: View.h:26
model_align_to_guide
long model_align_to_guide(Bmodel *model, Bmodel *guide)
A model is oriented to coincide with a guide model.
Definition: model_transform.cpp:443
verbose
int verbose
Bmodel
Definition: rwmodel.h:119
models_reflect
long models_reflect(Bmodel *model, Vector3< double > normal, Vector3< double > origin)
Reflects the model through a mirror plane.
Definition: model_transform.cpp:171
Transform::axis
Vector3< double > axis
Definition: Transform.h:28
transform_matrix_solve
Transform transform_matrix_solve(Matrix a, vector< double > &bx, vector< double > &by, vector< double > &bz, int flag)
Solves a 3D geometry fit matrix.
Definition: Transform.cpp:30
model_center_of_mass
Vector3< double > model_center_of_mass(Bmodel *model)
Definition: model_util.cpp:475
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
model_transform.h
Library routines used for model transformation.
model_compare.h
Functions to compare models and components.
model_component_number_difference
long model_component_number_difference(Bmodel *model1, Bmodel *model2)
Compares the numbers of components in two models.
Definition: model_compare.cpp:26
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bcomponent
Definition: rwmodel.h:51