Bsoft  2.1.0
Bernard's software package
model_neighbors.cpp
Go to the documentation of this file.
1 
9 #include "model_neighbors.h"
10 #include "model_util.h"
11 #include "linked_list.h"
12 #include "utilities.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
17 
28 int model_set_neighbors(Bmodel* model, int number)
29 {
30  if ( number > MAXLINK ) number = MAXLINK;
31 
32  int i, imax(0);
33  double d[MAXLINK], d1, dmax;
34  Bcomponent* comp;
35  Bcomponent* comp2;
36 
37  for ( comp = model->comp; comp; comp = comp->next ) {
38  for ( i=0; i<MAXLINK; i++ ) {
39  d[i] = 1e30;
40  comp->neighbor[i] = NULL;
41  }
42  for ( comp2 = model->comp; comp2; comp2 = comp2->next ) if ( comp != comp2 ) {
43  for ( i=0, dmax = 0; i<number; i++ ) if ( dmax < d[i] ) {
44  dmax = d[i];
45  imax = i;
46  }
47  d1 = comp->loc.distance(comp2->loc);
48  if ( d1 < dmax ) {
49  d[imax] = d1;
50  comp->neighbor[imax] = comp2;
51  }
52  }
53  }
54 
56 
57  return 0;
58 }
59 
60 int model_set_neighbors(Bmodel* model, int number, double distance)
61 {
62  if ( number > MAXLINK ) number = MAXLINK;
63 
64  int i, j, imin, n[MAXLINK+1], ncomp(0), nd(0);
65  double a, d[MAXLINK], d1, dmin, dist(0);
66  Vector3<double> vz(0,0,1), v0[MAXLINK], v[MAXLINK], vc;
67  Matrix3 mat;
68  Bcomponent* comp;
69  Bcomponent* comp2;
70 
72 
73  if ( verbose ) {
74  cout << "Setting up neighbors for model: " << model->id << endl;
75  cout << "Number: " << number << endl;
76  cout << "Distance: " << distance << " A" << endl << endl;
77  }
78 
79  // Set up the guide positions
80  for ( i=0; i<number; i++ ) {
81  a = TWOPI*i*1.0L/number;
82  v0[i][0] = distance*cos(a);
83  v0[i][1] = distance*sin(a);
84  }
85 
86  for ( i=0; i<=number; i++ ) n[i] = 0;
87 
88  for ( comp = model->comp; comp; comp = comp->next, ncomp++ ) {
89  vc = comp->loc - com;
90  mat = Matrix3(vz, vc);
91  for ( i=0; i<MAXLINK; i++ ) {
92  d[i] = 1e30;
93  comp->neighbor[i] = NULL;
94  }
95  for ( i=0; i<number; i++ ) // Transform the position vectors for this component
96  v[i] = mat * v0[i] + comp->loc;
97  for ( comp2 = model->comp; comp2; comp2 = comp2->next ) if ( comp != comp2 ) {
98  for ( i=imin=0, dmin=1e30; i<number; i++ ) { // Find the closest guide position
99  d1 = comp2->loc.distance(v[i]);
100  if ( dmin > d1 ) {
101  dmin = d1;
102  imin = i;
103  }
104  }
105  d1 = comp->loc.distance(comp2->loc);
106  if ( d1 < d[imin] ) {
107  d[imin] = d1;
108  comp->neighbor[imin] = comp2;
109  }
110  }
111  for ( i=j=0; i<MAXLINK; i++ ) { // Shift all neighbor references to the head of the array
112  if ( comp->neighbor[i] ) {
113  if ( i > j ) {
114  comp->neighbor[j] = comp->neighbor[i];
115  comp->neighbor[i] = NULL;
116  }
117  dist += comp->loc.distance(comp->neighbor[j]->loc);
118  nd++;
119  j++;
120  }
121  }
122  n[j]++;
123  }
124 
125  dist /= nd;
126 
127  if ( verbose ) {
128  cout << "Neighbor numbers assigned:\nNumber\tCount" << endl;
129  for ( i=0; i<=number; i++ )
130  cout << i << tab << n[i] << endl;
131  cout << "Average distance: " << dist << " A" << endl << endl;
132  }
133 
135 
136  return 0;
137 }
138 
150 {
151  int i, j, nr(0), nn(0);
152  Bcomponent* comp, *compnb;
153 
154  if ( verbose & VERB_DEBUG )
155  cout << "DEBUG model_neighbor_reciprocity: neigbor list:" << endl;
156 
157  for ( comp = model->comp; comp; comp = comp->next ) {
158  for ( i = 0; i<MAXLINK && comp->neighbor[i]; i++ ) {
159  compnb = comp->neighbor[i];
160  for ( j=0; j<MAXLINK && compnb->neighbor[j]; j++ )
161  if ( compnb->neighbor[j]->id == comp->id ) break;
162  if ( compnb->neighbor[j] ) nr++;
163  else nn++;
164  if ( verbose & VERB_DEBUG )
165  cout << comp->id << " - " << compnb->id << endl;
166  }
167  }
168 
169  if ( verbose ) {
170  cout << "Reciprocated neighbors: " << nr << endl;
171  cout << "Non-reciprocated neighbors: " << nn << endl << endl;
172  }
173 
174  return nn;
175 }
176 
177 
178 
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
Bcomponent
Definition: rwmodel.h:51
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
Vector3< double >
model_neighbor_reciprocity
int model_neighbor_reciprocity(Bmodel *model)
Counts how many neighbors of a component also has that component as a neighbor.
Definition: model_neighbors.cpp:149
model_center_of_mass
Vector3< double > model_center_of_mass(Bmodel *model)
Definition: model_util.cpp:475
model_set_neighbors
int model_set_neighbors(Bmodel *model, int number)
Sets the desired number of components as neighbors for each component.
Definition: model_neighbors.cpp:28
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
Matrix3
Definition: Matrix3.h:18
Bmodel::id
Bstring id
Definition: rwmodel.h:121
distance
double distance(vector< T1 > &v1, vector< T2 > &v2)
Definition: Vector3.h:538
Bmodel
Definition: rwmodel.h:119
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
model
Bmodel * model
Definition: bshow.cpp:34
linked_list.h
Header file for generalized linked list functions.
model_neighbors.h
Functions to manipulate model component neighbors.
model_util.h
Library routines used for model processing.
verbose
int verbose
MAXLINK
#define MAXLINK
Definition: rwmodel.h:14
Bcomponent::neighbor
Bcomponent * neighbor[MAXLINK]
Definition: rwmodel.h:56
TWOPI
#define TWOPI
Definition: utilities.h:72