-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathStructuralUnit.cpp
365 lines (320 loc) · 10.9 KB
/
StructuralUnit.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
362
363
364
365
/***********************************************************************
StructuralUnit - Base class for structural units in molecular
arrangements.
Copyright (c) 2003-2011 Oliver Kreylos
The Nanotech Construction Kit 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.
The Nanotech Construction Kit 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 the Nanotech Construction Kit; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include <assert.h>
#include <Misc/File.h>
#include <Misc/ConfigurationFile.h>
#include <Misc/StandardValueCoders.h>
#include <Math/Math.h>
#include <Geometry/Endianness.h>
#include "GhostUnit.h"
#include "StructuralUnit.h"
namespace NCK {
/***************************************
Static elements of class StructuralUnit:
***************************************/
Scalar StructuralUnit::vertexForceRadius;
Scalar StructuralUnit::vertexForceRadius2;
Scalar StructuralUnit::vertexForceStrength;
Scalar StructuralUnit::centralForceOvershoot;
Scalar StructuralUnit::centralForceStrength;
/*******************************
Methods of class StructuralUnit:
*******************************/
void StructuralUnit::initClass(const Misc::ConfigurationFileSection& configFileSection)
{
/* Read class settings: */
vertexForceRadius=configFileSection.retrieveValue<Scalar>("./vertexForceRadius",1.0);
vertexForceRadius2=Math::sqr(vertexForceRadius);
vertexForceStrength=configFileSection.retrieveValue<Scalar>("./vertexForceStrength",90.0);
centralForceOvershoot=configFileSection.retrieveValue<Scalar>("./centralForceOvershoot",1.0/3.0);
centralForceStrength=configFileSection.retrieveValue<Scalar>("./centralForceStrength",72.0);
}
StructuralUnit::StructuralUnit(int sNumVertices,Misc::File& file)
:pred(0),succ(0),cell(0),cellPred(0),cellSucc(0),
marked(false),locked(false),
numVertices(sNumVertices),vertexLinks(new VertexLink[numVertices])
{
/* Read position and orientation: */
file.read(position);
Scalar quaternion[4];
file.read<Scalar>(quaternion,4);
orientation=Rotation(quaternion);
/* Read velocities: */
file.read(linearVelocity);
file.read(angularVelocity);
}
void StructuralUnit::deinitClass(void)
{
}
void StructuralUnit::setVertexForceRadius(Scalar newVertexForceRadius)
{
vertexForceRadius=newVertexForceRadius;
vertexForceRadius2=Math::sqr(vertexForceRadius);
}
void StructuralUnit::setVertexForceStrength(Scalar newVertexForceStrength)
{
vertexForceStrength=newVertexForceStrength;
}
void StructuralUnit::setCentralForceOvershoot(Scalar newCentralForceOvershoot)
{
centralForceOvershoot=newCentralForceOvershoot;
}
void StructuralUnit::setCentralForceStrength(Scalar newCentralForceStrength)
{
centralForceStrength=newCentralForceStrength;
}
Scalar StructuralUnit::calcVertexRadius(Scalar unitRadius)
{
/* Calculate vertex offsets based on force parameters: */
Scalar ph=-Scalar(0.25)*(Scalar(4)*unitRadius-vertexForceRadius);
Scalar q=-Scalar(0.5)*unitRadius*(vertexForceRadius-Scalar(2)*unitRadius);
q+=Scalar(0.5)*centralForceStrength*unitRadius*(Math::sqr(vertexForceRadius)*centralForceOvershoot)/(vertexForceStrength*Math::sqr(Scalar(2)*unitRadius+centralForceOvershoot));
Scalar det2=Math::sqr(ph)-q;
Scalar det=Math::sqrt(det2);
Scalar vertexRadius1=-ph-det;
Scalar vertexRadius2=-ph+det;
return vertexRadius2;
}
void StructuralUnit::setColor(const StructuralUnit::Color& newColor)
{
color=newColor;
}
void StructuralUnit::clearVertexLinks(void)
{
int numVertices1=this->getNumVertices();
for(int i1=0;i1<numVertices1;++i1)
{
VertexLink& vl1=this->getVertexLink(i1);
StructuralUnit* other=vl1.unit;
if(other!=0)
{
/* Remove link: */
VertexLink& vl2=other->getVertexLink(vl1.vertexIndex);
vl1.unit=0;
vl1.vertexIndex=-1;
vl2.unit=0;
vl2.vertexIndex=-1;
}
}
}
void StructuralUnit::checkVertexLinks(Scalar timeStep)
{
//assert(dynamic_cast<GhostUnit*>(this)==0);
int numVertices1=this->getNumVertices();
for(int i1=0;i1<numVertices1;++i1)
{
VertexLink& vl1=this->getVertexLink(i1);
if(vl1.unit!=0&&this->id>vl1.unit->id)
{
//VertexLink& vl2=vl1.unit->getVertexLink(vl1.vertexIndex);
//assert(vl2.unit==this&&vl2.vertexIndex==i1);
/* Check if vertex link is still valid: */
Point p1=this->getVertex(i1);
Point p2=vl1.unit->getVertex(vl1.vertexIndex);
Vector dist=p2-p1;
Scalar distLen2=Geometry::sqr(dist);
if(distLen2>vertexForceRadius2)
{
/* Remove link: */
VertexLink& vl2=vl1.unit->getVertexLink(vl1.vertexIndex);
vl1.unit=0;
vl1.vertexIndex=-1;
vl2.unit=0;
vl2.vertexIndex=-1;
}
else
{
/* Calculate vertex attracting force: */
Scalar distLen=Math::sqrt(distLen2);
Vector force=dist*(vertexForceStrength*(vertexForceRadius-distLen)/vertexForceRadius2);
/* Apply forces to units: */
this->applyVertexForce(i1,force,timeStep);
vl1.unit->applyVertexForce(vl1.vertexIndex,force,-timeStep);
}
}
}
}
void StructuralUnit::establishVertexLinks(StructuralUnit* unit1,StructuralUnit* unit2)
{
/* Check if the units are close enough to link vertices: */
Scalar distLen2=Geometry::sqrDist(unit1->position,unit2->position);
if(distLen2>=Math::sqr(unit1->getRadius()+unit2->getRadius()+vertexForceRadius))
return;
/* Check if there is an existing vertex link between the units: */
int i1,i2;
int numVertices1=unit1->getNumVertices();
int numVertices2=unit2->getNumVertices();
for(i1=0;i1<numVertices1;++i1)
{
VertexLink& vl1=unit1->getVertexLink(i1);
if(vl1.unit==unit2)
{
i2=vl1.vertexIndex;
break;
}
}
if(i1>=numVertices1)
{
/* Try establishing a vertex link between the two units: */
for(i1=0;i1<numVertices1;++i1)
{
VertexLink& vl1=unit1->getVertexLink(i1);
if(vl1.unit==0)
{
Point p1=unit1->getVertex(i1);
for(i2=0;i2<numVertices2;++i2)
{
VertexLink& vl2=unit2->getVertexLink(i2);
if(vl2.unit==0)
{
Point p2=unit2->getVertex(i2);
Vector dist=p2-p1;
Scalar distLen2=Geometry::sqr(dist);
if(distLen2<=vertexForceRadius2)
{
/* Link the two vertices: */
vl1.unit=unit2;
vl1.vertexIndex=i2;
vl2.unit=unit1;
vl2.vertexIndex=i1;
/* There can be only one vertex link between two units; stop looking: */
goto foundVertexPair;
}
}
}
}
}
foundVertexPair:
; // Dummy statement to make compiler happy
}
}
void StructuralUnit::interact(StructuralUnit* unit1,StructuralUnit* unit2,Scalar timeStep)
{
//assert(dynamic_cast<GhostUnit*>(unit1)==0);
/* Calculate interaction between unit's centroids: */
Scalar radius1=unit1->getRadius();
Scalar radius2=unit2->getRadius();
Scalar centralForceRadius=radius1+radius2+centralForceOvershoot;
Scalar centralForceRadius2=Math::sqr(centralForceRadius);
Vector dist=unit2->position-unit1->position;
Scalar distLen2=Geometry::sqr(dist);
if(distLen2<centralForceRadius2)
{
/* Calculate centroid repelling force: */
Scalar distLen=Math::sqrt(distLen2);
Vector force=dist*(centralForceStrength*(distLen-centralForceRadius)/centralForceRadius2);
/* Apply forces to units: */
unit1->applyCentralForce(force,timeStep);
unit2->applyCentralForce(force,-timeStep);
}
/* Check for potential new vertex links if units are close enough: */
if(distLen2<Math::sqr(radius1+radius2+vertexForceRadius))
{
/* Check if there is an existing vertex link between the units: */
int i1;
int numVertices1=unit1->getNumVertices();
for(i1=0;i1<numVertices1&&unit1->getVertexLink(i1).unit!=unit2;++i1)
;
if(i1>=numVertices1)
{
/* Try establishing a vertex link between the two units: */
int i2;
int numVertices2=unit2->getNumVertices();
for(i1=0;i1<numVertices1;++i1)
{
VertexLink& vl1=unit1->getVertexLink(i1);
if(vl1.unit==0)
{
Point p1=unit1->getVertex(i1);
for(i2=0;i2<numVertices2;++i2)
{
VertexLink& vl2=unit2->getVertexLink(i2);
if(vl2.unit==0)
{
Point p2=unit2->getVertex(i2);
Vector dist=p2-p1;
Scalar distLen2=Geometry::sqr(dist);
if(distLen2<=vertexForceRadius2)
{
/* Link the two vertices: */
vl1.unit=unit2;
vl1.vertexIndex=i2;
vl2.unit=unit1;
vl2.vertexIndex=i1;
/* Calculate vertex attracting force: */
Scalar distLen=Math::sqrt(distLen2);
Vector force=dist*(vertexForceStrength*(vertexForceRadius-distLen)/vertexForceRadius2);
/* Apply forces to units: */
unit1->applyVertexForce(i1,force,timeStep);
unit2->applyVertexForce(i2,force,-timeStep);
/* There can be only one vertex link between two units; stop looking: */
goto foundVertexPair;
}
}
}
}
}
foundVertexPair:
; // Dummy statement to make compiler happy
}
}
}
void StructuralUnit::readVertexLinks(Misc::File& file,const StructuralUnit::IndexUnitMap& indexUnitMap)
{
/* Read all vertex links: */
int numVertexLinks=file.read<int>();
for(int i=0;i<numVertexLinks;++i)
{
/* Read vertex link from file: */
FileVertexLink fvl(file);
/* Get pointer to other unit: */
StructuralUnit* other=indexUnitMap.getEntry(fvl.otherUnitIndex).getDest();
/* Set vertex links: */
this->getVertexLink(fvl.thisVertexIndex).unit=other;
this->getVertexLink(fvl.thisVertexIndex).vertexIndex=fvl.otherVertexIndex;
other->getVertexLink(fvl.otherVertexIndex).unit=this;
other->getVertexLink(fvl.otherVertexIndex).vertexIndex=fvl.thisVertexIndex;
}
}
void StructuralUnit::writeState(Misc::File& file) const
{
/* Write position and orientation: */
file.write(position);
file.write<Scalar>(orientation.getQuaternion(),4);
/* Write velocities: */
file.write(linearVelocity);
file.write(angularVelocity);
}
void StructuralUnit::writeVertexLinks(Misc::File& file,const StructuralUnit::UnitIndexMap& unitIndexMap) const
{
/* Count number of valid vertex links: */
int numVertices=this->getNumVertices();
int numVertexLinks=0;
for(int i=0;i<numVertices;++i)
if(this->getVertexLink(i).unit!=0&&unitIndexMap.isEntry(this->getVertexLink(i).unit))
++numVertexLinks;
file.write<int>(numVertexLinks);
/* Write valid vertex links to file: */
for(int i=0;i<numVertices;++i)
if(this->getVertexLink(i).unit!=0&&unitIndexMap.isEntry(this->getVertexLink(i).unit))
{
/* Write vertex link structure: */
FileVertexLink fvl(i,unitIndexMap.getEntry(this->getVertexLink(i).unit).getDest(),this->getVertexLink(i).vertexIndex);
fvl.write(file);
}
}
}