| Bsoft 2.0.5 Bernard's software package

model_plane.cpp
Go to the documentation of this file.
1
9 #include "model_create.h"
10 #include "model_transform.h"
11 #include "model_views.h"
12 #include "model_util.h"
13 #include "utilities.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18
30 Vector3<double> model_fit_plane(Bmodel* model)
31 {
32 int i, j, n;
33 double offset, d, dstd(0), dmin(1e30);
34 vector<double> b(3);
35 Matrix a(3,3);
36 Vector3<double> cen, loc, normal;
37 Bcomponent* comp;
38
39 for ( i=0; i<3; i++ ) b[i] = 0;
40
41 for ( n=0, comp = model->comp; comp; comp = comp->next, n++ ) {
42 loc = comp->loc;
43 cen += loc;
44 for ( i=0; i<3; i++ ) {
45 for ( j=0; j<=i; j++ )
46 a[j][i] += loc[i]*loc[j];
47 b[i] += loc[i];
48 }
49 }
50 cen /= n;
51 for ( i=1; i<3; i++ )
52 for ( j=0; j<i; j++ )
53 a[i][j] = a[j][i];
54
55 offset = 0;
56 if ( a[0][0] == 0 ) {
57 normal = Vector3<double>(1, 0, 0);
58 } else if ( a[1][1] == 0 ) {
59 normal = Vector3<double>(0, 1, 0);
60 } else if ( a[2][2] == 0 ) {
61 normal = Vector3<double>(0, 0, 1);
62 } else {
63 a.LU_decomposition(b);
64 normal = Vector3<double>(b[0], b[1], b[2]);
65 normal.normalize();
66 }
67
68 offset = cen.scalar(normal);
69
70 for ( n=0, comp = model->comp; comp; comp = comp->next, n++ ) {
71 d = comp->loc.scalar(normal) - offset;
72 dstd += d*d;
73 if ( fabs(dmin) > fabs(d) ) dmin = d;
74 comp->fom = d;
75 }
76
77 dstd /= n;
78 model->fom = dstd;
79
80 if ( dmin < 0 ) normal = -normal;
81 for ( comp = model->comp; comp; comp = comp->next )
82 comp->view = View(normal);
83
84 return normal;
85 }
86
102 double model_distance_to_guide(Vector3<double> loc, Vector3<double> vec, Bmodel* guide, double sigma2)
103 {
104 double dp, dl, dpa(0), w, wa(0);
105 Vector3<double> dvec;
106 Bcomponent* comp;
107
108 for ( comp = guide->comp; comp; comp = comp->next ) {
109 dvec = comp->loc - loc;
110 dp = dvec.scalar(vec); // distance to the general guide plane
111 dl = dvec.length2() - dp*dp; // lateral distance to the guide component
112 w = exp(-dl/sigma2);
113 dpa += dp*w;
114 wa += w;
115 }
116
117 // cout << dpa << tab << wa << endl;
118
119 if ( wa ) dpa /= wa;
120
121 return dpa;
122 }
123
136 int model_adjust_to_guide(Bmodel* model, Bmodel* gmod, double sigma)
137 {
138 int n;
139 double d, davg(0), dstd(0), sigma2(2*sigma*sigma);
140 Vector3<double> center;
141 Bcomponent* comp = NULL;
142
143 if ( verbose ) {
144 cout << "Adjusting the model " << model->id << " to the guide " << gmod->id << endl;
145 cout << "Sigma: " << sigma << endl << endl;
146 }
147
148 if ( model->fmap.length() < 1 && gmod->fmap.length() ) model->fmap = gmod->fmap;
149
150 for ( n=0, comp = model->comp; comp; comp = comp->next, n++ ) {
151 comp->vec = comp->view.vector3();
152 d = model_distance_to_guide(comp->loc, comp->vec, gmod, sigma2);
153 if ( !isfinite(d) ) cerr << "d not finite: " << d << endl;
154 if ( verbose )
155 cout << comp->id << tab << comp->loc << tab << comp->vec << tab << d << endl;
156 comp->loc += comp->vec * d;
157 davg += d;
158 dstd += d*d;
159 }
160
161 davg /= n;
162 dstd /= n;
163 dstd -= davg*davg;
164 if ( dstd > 0 ) dstd = sqrt(dstd);
165 else dstd = 0;
166
167 if ( verbose )
168 cout << "Average displacement: " << davg << " (" << dstd << ")" << endl;
169
170 return 0;
171 }
172
173
186 Bmodel* model_generate_from_plane_guide(Bmodel* guide, double separation, double sigma)
187 {
188 if ( !guide ) {
189 cerr << "Error: No guide model!" << endl;
190 return NULL;
191 }
192
193 Vector3<double>* eigenvec = new Vector3<double>[3];
194 Vector3<double> pax = model_principal_axes(guide, eigenvec);
195
196 Bmodel* model = model_create_plane(2*pax[0], 2*pax[1], 0, separation);
197
198 if ( !model ) {
199 cerr << "Error: The model creation from " << guide->id << " failed!" << endl;
200 return NULL;
201 }
202
203 model->id = guide->id;
204 model->fmap = guide->fmap;
205 model->img_num = guide->img_num;
206 model->comment = guide->comment;
207
208 int i;
209 Vector3<double> origin;
210 Vector3<double> shift = model_center_of_mass(guide);
211 Matrix3 mat;
212
213 for ( i=0; i<3; i++ ) mat[i] = eigenvec[i];
214
215 model_rotate(model, mat, origin, shift);
216
217 View view(eigenvec[2][0], eigenvec[2][1], eigenvec[2][2], 0);
218 model_set_views(model, view);
219
220 delete[] eigenvec;
221
222 model_adjust_to_guide(model, guide, sigma);
223
224 return model;
225 }
226
227
228
Bmodel::img_num
int img_num
Definition: rwmodel.h:127
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
model_create.h
Library routines used for model creation.
Vector3::normalize
double normalize()
Definition: Vector3.h:236
Bmodel::id
Bstring id
Definition: rwmodel.h:121
model_create_plane
Bmodel * model_create_plane(double length, double width, double z, double distance)
Creates a plane with the given length, width, height, and component separation.
Definition: model_create.cpp:1170
model_views.h
Library routines used for analysing model component views.
model_fit_plane
Vector3< double > model_fit_plane(Bmodel *model)
Fits a plane through a model.
Definition: model_plane.cpp:30
utilities.h
Header file for general utilities.
model_util.h
Library routines used for model processing.
Matrix3
Definition: Matrix3.h:17
Bstring::length
long length() const
Definition: Bstring.cpp:263
model_set_views
long model_set_views(Bmodel *model, View view)
Sets views.
Definition: model_views.cpp:81
Bmodel::comment
Bstring comment
Definition: rwmodel.h:126
Matrix::LU_decomposition
double LU_decomposition()
Matrix inversion by LU decomposition.
Definition: matrix.cpp:78
isfinite
bool isfinite(complex< T > &value)
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomponent::fom
float fom
Definition: rwmodel.h:65
Vector3::scalar
double scalar(const Vector3 &v) const
Definition: Vector3.h:247
verbose
int verbose
model_adjust_to_guide
int model_adjust_to_guide(Bmodel *model, Bmodel *gmod, double sigma)
Adjusts a shell model to the faces of a polyhedral guide model.
Definition: model_plane.cpp:136
View::vector3
Vector3< double > vector3()
Definition: View.h:126
Vector3::length2
double length2()
Definition: Vector3.h:221
model_rotate
long model_rotate(Bmodel *model, Matrix3 mat)
Rotates the model.
Definition: model_transform.cpp:238
model_generate_from_plane_guide
Bmodel * model_generate_from_plane_guide(Bmodel *guide, double separation, double sigma)
Generates a plane model and fits a plane through it.
Definition: model_plane.cpp:186
model
Bmodel * model
Definition: bshow.cpp:32
Bmodel::fom
float fom
Definition: rwmodel.h:128
Matrix
Definition: Matrix.h:21
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
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::vec
Vector3< float > vec
Definition: rwmodel.h:60
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
Vector3< double >
View
Definition: View.h:26
Bmodel
Definition: rwmodel.h:119
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
model_distance_to_guide
double model_distance_to_guide(Vector3< double > loc, Vector3< double > vec, Bmodel *guide, double sigma2)
Calculates the distance between a location and the local guide model.
Definition: model_plane.cpp:102
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
model_transform.h
Library routines used for model transformation.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bcomponent
Definition: rwmodel.h:51