Bsoft  2.1.0
Bernard's software package
model_links.cpp
Go to the documentation of this file.
1 
9 #include "model_links.h"
10 #include "model_transform.h"
11 #include "model_compare.h"
12 #include "model_util.h"
13 #include "matrix_util.h"
14 #include "linked_list.h"
15 #include "utilities.h"
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
29 {
30  if ( !model ) return 0;
31 
32  long i, j, n(0);
33  Blink* link = NULL;
34  Bcomponent* comp1;
35  Bcomponent* comp2;
36 
37  for ( n=0, link = model->link; link; link = link->next, n++ ) {
38  comp1 = link->comp[0];
39  comp2 = link->comp[1];
40  for ( i=0; i<MAXLINK && comp1->link[i] && comp1->link[i] != comp2; i++ ) ;
41  for ( j=0; j<MAXLINK && comp2->link[j] && comp2->link[j] != comp1; j++ ) ;
42  if ( comp1->link[i] == comp2 && comp2->link[j] == comp1 ) {
43  if ( verbose & VERB_FULL )
44  cerr << "Error: " << comp1->id << " already linked to " << comp2->id <<
45  "! (" << i << " -" << j << ")" << endl;
46  } else {
47  if ( i < MAXLINK && j < MAXLINK ) {
48  comp1->link[i] = comp2;
49  comp2->link[j] = comp1;
50  comp1->flag[i] = comp2->flag[j] = 1; // flag=1 indicates link
51  } else {
52  cerr << "Warning: Link count too large!" << endl;
53  }
54  }
55  if ( link->length < 1e-6 ) link->length = comp1->loc.distance(comp2->loc);
56  }
57 
58  return n;
59 }
60 
70 long model_link_list_generate(Bmodel* model, double maxlength)
71 {
72  if ( !model ) return 0;
73 
74  long n(0);
75  double linkrad(0.1*maxlength);
76  double d;
77  Bcomponent* comp;
78  Bcomponent* comp2;
79  Blink* link = NULL;
80 
81  if ( verbose )
82  cout << "Generating a model link list with maximum link length " << maxlength << endl;
83 
84  link = model->link;
85  if ( link ) linkrad = link->radius;
86  for ( comp = model->comp; comp && comp->next; comp = comp->next ) if ( comp->sel ) {
87  for ( comp2 = comp->next; comp2; comp2 = comp2->next ) if ( comp2->sel ) {
88  d = comp->loc.distance(comp2->loc);
89  if ( d <= maxlength ) {
90  link = link_add(&link, comp, comp2, d, linkrad);
91  if ( !model->link ) model->link = link;
92  n++;
93  }
94  }
95  }
96 
97  if ( verbose )
98  cout << "Number of links generated " << n << endl << endl;
99 
100  return n;
101 }
102 
112 long model_set_link_length(Bmodel* model, double linklength)
113 {
114  if ( !model ) return 0;
115 
116  long nsel(0);
117  Blink* link;
118 
119  if ( verbose & VERB_FULL )
120  cout << "Setting reference link lengths to " << linklength << endl << endl;
121 
122  for ( link = model->link; link; link = link->next ) if ( link->sel ) {
123  link->length = linklength;
124  nsel++;
125  }
126 
127  return nsel;
128 }
129 
139 long model_set_link_radius(Bmodel* model, double linkrad)
140 {
141  if ( !model ) return 0;
142 
143  long nsel(0);
144  Blink* link = NULL;
145 
146  for ( link = model->link; link; link = link->next ) if ( link->sel ) {
147  link->radius = linkrad;
148  nsel++;
149  }
150 
151  return nsel;
152 }
153 
155 {
156  if ( !comp->sel ) return NULL;
157 
158  *avg_loc += comp->loc;
159  (*n)++;
160 
161  int i;
162  Bstring id("1");
163  Bcomponent* comp_sub = NULL;
164  Bcomponent* comp_link = NULL;
165 
166  if ( model ) {
167  comp_sub = component_add(&model->comp, id);
168  component_copy(comp, comp_sub);
169  }
170 
171  comp->sel = 0;
172 
173  for ( i=0; i<MAXLINK && comp->link[i]; i++ ) if ( comp->link[i]->sel ) {
174  comp_link = model_linked_submodel(comp->link[i], model, n, avg_loc);
175  if ( comp_link ) link_add(&model->link, comp_sub, comp_link, 1, 1);
176  }
177 
178  return comp_sub;
179 }
180 
194 long model_reduce_linked(Bmodel* model, Bstring& submodname, int flags)
195 {
196  long i, nn=0;
197  Bstring id;
198  Bmodel* mp;
199  Bmodel* model_sub = NULL;
200  Bmodel* mpt = NULL;
201  Bmodel* mps = NULL;
202  Bcomponent* comp;
203  Bcomponent* comp_list = NULL;
204  Bcomponent* comp_new = NULL;
205  Bcomponent* comp_sub = NULL;
206  Bcomponent* comp_temp = NULL;
207  Bcomptype* ct;
208  Transform t;
209 
210  if ( verbose & VERB_PROCESS )
211  cout << "Reducing models and writing sub-models to file " << submodname << endl << endl;
212 
213 
214  for ( mp = model; mp; mp = mp->next ) if ( mp->sel ) {
215  comp_list = comp_new = NULL;
216  for ( ct = mp->type; ct; ct = ct->next ) ct->count = 0;
217  for ( i=0, comp = mp->comp; comp; comp = comp->next ) if ( comp->sel ) {
218  id = Bstring(++i, "%d");
219  comp_new = component_add(&comp_new, id);
220  if ( !comp_list ) comp_list = comp_new;
221  component_copy(comp, comp_new);
222  comp_new->sel = 0;
223  comp_new->loc = 0;
224  comp_new->type->count++;
225  mpt = NULL;
226  mpt = model_add(&mpt, id);
227  mpt->type_id = comp_new->type->id;
228  model_linked_submodel(comp, mpt, &comp_new->sel, &comp_new->loc);
229  comp_new->loc /= comp_new->sel;
230  for ( mps = model_sub; mps && mps->type_id != mpt->type_id; mps = mps->next ) ;
231  if ( !mps ) {
232  if ( verbose )
233  cout << "Adding sub-model " << mpt->type_id << endl;
234  if ( !model_sub ) model_sub = mps = model_copy(mpt);
235  else {
236  for ( mps = model_sub; mps->next; mps = mps->next ) ;
237  mps->next = model_copy(mpt);
238  mps = mps->next;
239  }
240  comp_new->type->fmod = submodname;
241  comp_new->type->num = id.integer();
242  model_add_type_by_id(mps, comp_new->type->id);
243  for ( comp_sub = mps->comp; comp_sub; comp_sub = comp_sub->next ) {
244  comp_sub->vel = comp_sub->loc; // Keep sum in velocity vector
245  comp_sub->sel = 1;
246  }
247  } else { // Fit this model onto the type model
248  if ( model_component_number_difference(mpt, mps) == 0 ) {
249  t = model_find_transform(mpt, mps);
250  model_rotate(mpt, t);
251 // comp_new->view = view_from_angle_and_axis3(t.angle, t.axis);
252  comp_new->view = View(t.angle, t.axis);
253  if ( verbose )
254  cout << "Fitting sub-model " << mpt->type_id << ": RMSD = " << model_compare(mpt, mps) << endl;
255  for ( comp_sub = mps->comp, comp_temp = mpt->comp; comp_sub && comp_temp;
256  comp_sub = comp_sub->next, comp_temp = comp_temp->next ) {
257  comp_sub->vel += comp_temp->loc; // Sum in velocity vector
258  comp_sub->sel++;
259  cout << comp_sub->id << tab << comp_temp->id << tab << comp_sub->loc.distance(comp_temp->loc) << endl;
260  }
261  }
262  model_kill(mpt);
263  }
264  }
266  mp->link = NULL;
268  mp->comp = comp_list;
269  nn += i;
270  }
271 
272  for ( mps = model_sub; mps; mps = mps->next )
273  for ( comp_sub = mps->comp; comp_sub; comp_sub = comp_sub->next )
274  if ( comp_sub->sel ) comp_sub->loc = comp_sub->vel/comp_sub->sel;
275 
276  write_model(submodname, model_sub);
277  model_kill(model_sub);
278 
279  return nn;
280 }
281 
295 {
296  if ( !model ) {
297  cerr << "Error: No model selected!" << endl << endl;
298  return(-1);
299  }
300 
301  if ( verbose )
302  cout << "Generating links to ensure minimum valency of " << valency << endl;
303 
305 
306  double dcut = matrix_find_cutoff_for_number(m, valency);
307 
308  long nl = model_link_list_generate(model, dcut);
309 
310  return nl;
311 }
312 
313 
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
Bcomponent
Definition: rwmodel.h:51
matrix_util.h
Utility functions for matrices.
Bcomptype::fmod
Bstring fmod
Definition: rwmodel.h:29
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
Bstring
Definition: Bstring.h:29
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
matrix_find_cutoff_for_number
double matrix_find_cutoff_for_number(Matrix m, int n)
Calculates a cutoff to ensure that the given number of elements fall below it.
Definition: matrix_util.cpp:78
component_add
Bcomponent * component_add(Bcomponent **comp, Bstring &id)
Adds a component to a linked list.
Definition: rwmodel.cpp:270
Bcomptype
Definition: rwmodel.h:25
Vector3< float >
link_add
Blink * link_add(Blink **link, Bcomponent *comp1, Bcomponent *comp2, double length, double radius)
Adds a component link to a linked list.
Definition: rwmodel.cpp:318
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
model_copy
Bmodel * model_copy(Bmodel *model)
Copies a model.
Definition: rwmodel.cpp:688
View
Definition: View.h:26
Bmodel::link
Blink * link
Definition: rwmodel.h:133
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
model_compare.h
Functions to compare models and components.
model_compare
double model_compare(Bmodel *model1, Bmodel *model2)
Compares two models.
Definition: model_compare.cpp:66
component_list_kill
int component_list_kill(Bcomponent *comp)
Deallocates memory for a list of components.
Definition: rwmodel.cpp:894
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
Bcomponent::vel
Vector3< float > vel
Definition: rwmodel.h:61
model_kill
int model_kill(Bmodel *model)
Deallocates all memory in the list.
Definition: rwmodel.cpp:1127
Bmodel::sel
int sel
Definition: rwmodel.h:129
Bcomponent::flag
int flag[MAXLINK]
Definition: rwmodel.h:57
Bmodel
Definition: rwmodel.h:119
model_find_transform
Transform model_find_transform(Bmodel *model, Bmodel *refmod)
A model is fitted to a reference model.
Definition: model_transform.cpp:470
utilities.h
Header file for general utilities.
write_model
int write_model(Bstring &filename, Bmodel *model)
Writes model parameters.
Definition: rwmodel.cpp:92
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomptype::num
int num
Definition: rwmodel.h:30
model_distance_matrix
Matrix model_distance_matrix(Bmodel *model, int view_flag)
Calculates the distance matrix for a model.
Definition: model_compare.cpp:189
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
Bcomptype::id
Bstring id
Definition: rwmodel.h:27
model
Bmodel * model
Definition: bshow.cpp:34
linked_list.h
Header file for generalized linked list functions.
model_add
Bmodel * model_add(Bmodel **model, Bstring &id)
Adds a model to a linked list.
Definition: rwmodel.cpp:181
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
model_transform.h
Library routines used for model transformation.
model_util.h
Library routines used for model processing.
model_add_type_by_id
Bcomptype * model_add_type_by_id(Bmodel *model, Bstring &id)
Add a type to a model if the identifier does not exist already.
Definition: rwmodel.cpp:414
Transform::axis
Vector3< double > axis
Definition: Transform.h:28
Transform::angle
double angle
Definition: Transform.h:29
Bmodel::type
Bcomptype * type
Definition: rwmodel.h:131
Bcomptype::next
Bcomptype * next
Definition: rwmodel.h:26
Transform
Definition: Transform.h:24
component_copy
int component_copy(Bcomponent *fromcomp, Bcomponent *tocomp)
Copies all elements from one component to another.
Definition: rwmodel.cpp:786
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
model_link_list_kill
int model_link_list_kill(Bmodel *model)
Deallocates memory for a list of component links.
Definition: rwmodel.cpp:953
model_rotate
long model_rotate(Bmodel *model, Matrix3 mat)
Rotates the model.
Definition: model_transform.cpp:238
Matrix
Definition: Matrix.h:22
MAXLINK
#define MAXLINK
Definition: rwmodel.h:14
Bcomponent::link
Bcomponent * link[MAXLINK]
Definition: rwmodel.h:55
Bcomptype::count
int count
Definition: rwmodel.h:31
Bmodel::type_id
Bstring type_id
Definition: rwmodel.h:122
Bcomponent::sel
int sel
Definition: rwmodel.h:66