-
Notifications
You must be signed in to change notification settings - Fork 0
/
Body.cpp
689 lines (628 loc) · 18.7 KB
/
Body.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/**
* @file Body.cpp
* @author Dan R. Lipsa
*
* Implementation of the Body class
*/
#include "Attribute.h"
#include "AttributeCreator.h"
#include "AttributeInfo.h"
#include "Body.h"
#include "Edge.h"
#include "Foam.h"
#include "DataProperties.h"
#include "Debug.h"
#include "Utils.h"
#include "Face.h"
#include "OrientedEdge.h"
#include "OrientedFace.h"
#include "ParsingData.h"
#include "ParsingDriver.h"
#include "ProcessBodyTorus.h"
#include "Vertex.h"
// Private Classes/Functions
// ======================================================================
/**
* STL unary function that converts a signed index into a vector of
* Face objects into a OrientedFace object. A negative index means
* that the Face object is listed in reverse order in the Body object
* than in the vector of Face objects.
*/
class indexToOrientedFace :
public unary_function<int, boost::shared_ptr<OrientedFace> >
{
public:
/**
* Constructor
* @param faces vector of Face pointers. This is where the indexes point to.
*/
indexToOrientedFace(const vector< boost::shared_ptr<Face> >& faces):
m_faces(faces) {}
/**
* Converts a 1-based index into an OrientedFace
* @param i index into a vector of Face pointers
* @return an OrientedFace pointer
*/
boost::shared_ptr<OrientedFace> operator() (int i)
{
bool reversed = false;
if (i < 0)
{
i = -i;
reversed = true;
}
i--;
return boost::make_shared<OrientedFace> (m_faces[i], reversed);
}
private:
/**
* Vector of Face pointers
*/
const vector<boost::shared_ptr<Face> >& m_faces;
};
Body::Neighbor pairToNeighbor (const pair<size_t, Body::Neighbor>& p)
{
return p.second;
}
// Methods Body::Neighbor
// ======================================================================
Body::Neighbor::Neighbor ()
{
}
Body::Neighbor::Neighbor (G3D::Vector3 centerOfReflection) :
m_centerReflection (centerOfReflection)
{
}
Body::Neighbor::Neighbor (
boost::shared_ptr<Body> body, G3D::Vector3int16 translation) :
m_body (body),
m_translation (translation),
m_centerReflection (G3D::Vector3::zero ())
{
}
// Methods Body
// ======================================================================
Body::Body(
const vector<int>& faceIndexes,
const vector<boost::shared_ptr<Face> >& faces,
size_t id,
ElementStatus::Enum duplicateStatus) :
Element(id, duplicateStatus),
m_hasFreeFace (false),
m_area (0),
m_volume (0),
m_growthRate (0),
m_deformationSimple (0),
m_pressureDeduced (false),
m_targetVolumeDeduced (false),
m_actualVolumeDeduced (false),
m_object (false)
{
m_orientedFaces.resize (faceIndexes.size ());
transform (faceIndexes.begin(), faceIndexes.end(), m_orientedFaces.begin(),
indexToOrientedFace(faces));
}
Body::Body (boost::shared_ptr<Face> face, size_t id) :
Element (id, ElementStatus::ORIGINAL),
m_hasFreeFace (false),
m_area (0),
m_volume (0),
m_growthRate (0),
m_deformationSimple (0),
m_pressureDeduced (false),
m_targetVolumeDeduced (false),
m_actualVolumeDeduced (false),
m_object (true)
{
m_orientedFaces.resize (1);
m_orientedFaces[0].reset (new OrientedFace (face, false));
}
void Body::calculatePhysicalVertices (
vector< boost::shared_ptr<Vertex> >* physicalVertices)
{
VertexSet vertices;
vector< boost::shared_ptr<Vertex> > tessellationVertices;
GetVertexSet (&vertices);
splitTessellationPhysical (
vertices, &tessellationVertices, physicalVertices);
}
void Body::splitTessellationPhysical (
const VertexSet& src,
vector< boost::shared_ptr<Vertex> >* destTessellation,
vector< boost::shared_ptr<Vertex> >* destPhysical)
{
destTessellation->resize (src.size ());
copy (src.begin (), src.end (), destTessellation->begin ());
vector< boost::shared_ptr<Vertex> >::iterator bp;
bp = partition (destTessellation->begin (), destTessellation->end (),
!boost::bind(&Vertex::IsPhysical, _1, Is2D ()));
destPhysical->resize (destTessellation->end () - bp);
copy (bp, destTessellation->end (), destPhysical->begin ());
destTessellation->resize (bp - destTessellation->begin ());
}
void Body::CalculateCenter ()
{
if (Is2D ())
{
m_center = GetFace (0).GetCenter ();
}
else
{
vector< boost::shared_ptr<Vertex> > physicalVertices;
calculatePhysicalVertices (&physicalVertices);
size_t size = physicalVertices.size ();
if (size >= 3)
{
m_center = accumulate (
physicalVertices.begin (), physicalVertices.end (),
G3D::Vector3::zero (),
boost::bind (plus<G3D::Vector3> (),
_1, boost::bind (&Vertex::GetVector, _2)));
}
else
{
VertexSet vertices;
GetVertexSet (&vertices);
size = vertices.size ();
m_center = accumulate (
vertices.begin (), vertices.end (), G3D::Vector3::zero (),
boost::bind (plus<G3D::Vector3> (),
_1, boost::bind (&Vertex::GetVector, _2)));
}
m_center /= G3D::Vector3(size, size, size);
}
}
void Body::UpdateAdjacentBody (const boost::shared_ptr<Body>& body)
{
for (size_t i = 0; i < m_orientedFaces.size (); i++)
{
boost::shared_ptr<OrientedFace> of = m_orientedFaces[i];
of->AddAdjacentBody (body, i);
of->UpdateAdjacentFace (of);
}
}
const Face& Body::GetFace (size_t i) const
{
return *GetOrientedFace (i).GetFace ();
}
Face& Body::GetFace (size_t i)
{
return *GetOrientedFace (i).GetFace ();
}
void Body::GetFaceSet (FaceSet* faceSet) const
{
const OrientedFaces& orientedFaces = GetOrientedFaces ();
BOOST_FOREACH (boost::shared_ptr<OrientedFace> of, orientedFaces)
faceSet->insert (of->GetFace ());
}
string Body::ToString () const
{
ostringstream ostr;
ostr << "Body " << GetId ();
if (IsObject ())
ostr << " Object (" << GetConstraintIndex () << "):";
else
ostr << " Bubble:";
ostr << endl;
ostr << m_orientedFaces.size () << " faces part of the body\n";
BOOST_FOREACH (boost::shared_ptr<OrientedFace> of, m_orientedFaces)
ostr << of->GetStringId () << " ";
ostr << "Body attributes:\n";
for (size_t i = 0; i < BodyAttribute::COUNT; ++i)
{
float value[BodyAttribute::MAX_NUMBER_OF_COMPONENTS];
GetAttributeValue (i, value);
ostr << BodyAttribute::ToString (i) << ": ";
ostr << BodyAttribute::ValueToString (i, value);
ostr << endl;
}
ostr << "\nBody center: " << m_center;
ostr << "\nDeformation eigen values: " << GetDeformationEigenValues ()
<< "\nDeformation eigen vectors: "
<< GetDeformationEigenVector (0) << ", "
<< GetDeformationEigenVector (1) << ", "
<< GetDeformationEigenVector (2)
<< "\nBounding box: " << GetBoundingBox ();
return ostr.str ();
}
bool Body::operator< (const Body& other) const
{
return GetId () < other.GetId ();
}
bool Body::operator< (size_t otherBodyId) const
{
return GetId () < otherBodyId;
}
void Body::GetVertexSet (VertexSet* vertexSet) const
{
return OrientedFace::GetVertexSetV (GetOrientedFaces (), vertexSet);
}
void Body::GetEdgeSet (EdgeSet* edgeSet) const
{
const OrientedFaces& orientedFaces = GetOrientedFaces ();
for_each (orientedFaces.begin (), orientedFaces.end (),
boost::bind (&OrientedFace::GetEdgeSet, _1, edgeSet));
}
bool Body::HasScalarValue (BodyScalar::Enum property,
bool* deduced) const
{
setPointerValue (deduced, false);
if (IsObject ())
{
if (property == BodyScalar::VELOCITY_X ||
property == BodyScalar::VELOCITY_Y ||
property == BodyScalar::VELOCITY_MAGNITUDE)
{
setPointerValue (deduced, true);
return true;
}
else if (property == BodyScalar::ACTUAL_VOLUME ||
property == BodyScalar::TARGET_VOLUME)
return HasAttribute (property - BodyScalar::DMP_BEGIN);
else
return false;
}
switch (property)
{
case BodyScalar::DEFORMATION_SIMPLE:
return HasAttribute (BodyScalar::TARGET_VOLUME -
BodyScalar::DMP_BEGIN);
case BodyScalar::PRESSURE:
setPointerValue (deduced, m_pressureDeduced);
return HasAttribute (property - BodyScalar::DMP_BEGIN);
case BodyScalar::TARGET_VOLUME:
setPointerValue (deduced, m_targetVolumeDeduced);
return HasAttribute (property - BodyScalar::DMP_BEGIN);
case BodyScalar::ACTUAL_VOLUME:
setPointerValue (deduced, m_actualVolumeDeduced);
return HasAttribute (property - BodyScalar::DMP_BEGIN);
default:
return true;
}
}
float Body::GetScalarValue (BodyScalar::Enum property) const
{
switch (property)
{
case BodyScalar::VELOCITY_X:
return GetVelocity ().x;
case BodyScalar::VELOCITY_Y:
return GetVelocity ().y;
case BodyScalar::VELOCITY_Z:
return GetVelocity ().z;
case BodyScalar::VELOCITY_MAGNITUDE:
return GetVelocity ().length ();
case BodyScalar::SIDES_PER_BUBBLE:
return GetSidesPerBody ();
case BodyScalar::DEFORMATION_SIMPLE:
return GetDeformationSimple ();
case BodyScalar::DEFORMATION_EIGEN:
return GetDeformationEigenScalar ();
case BodyScalar::PRESSURE:
case BodyScalar::TARGET_VOLUME:
case BodyScalar::ACTUAL_VOLUME:
return GetAttribute<RealAttribute, double> (
property - BodyScalar::DMP_BEGIN);
case BodyScalar::GROWTH_RATE:
return GetGrowthRate ();
case BodyScalar::COUNT:
ThrowException ("Invalid BodyScalar: ", property);
return 0;
}
return 0;
}
void Body::GetAttributeValue (size_t attribute, float* value) const
{
if (BodyAttribute::IsScalar (attribute))
{
BodyScalar::Enum bodyScalar = BodyScalar::FromSizeT(attribute);
float v = HasScalarValue (bodyScalar) ?
GetScalarValue (bodyScalar) : 0;
*value = v;
}
else if (BodyAttribute::IsVector (attribute))
{
G3D::Vector3 v = GetVelocity ();
value[0] = v.x;
value[1] = v.y;
value[2] = v.z;
}
else
GetDeformationTensor (value, G3D::Matrix3::identity ());
}
float Body::GetDeformationSimple () const
{
return m_deformationSimple;
}
G3D::Matrix3 Body::GetDeformationTensor (
const G3D::Matrix3& additionalRotation) const
{
// Practical Linear Algebra, A Geometry Toolbox,
// Gerald Farin, Dianne Hansford, Sec 7.5
G3D::Matrix3 l = G3D::Matrix3::fromDiagonal (GetDeformationEigenValues ());
G3D::Matrix3 r =
additionalRotation *
MatrixFromColumns (GetDeformationEigenVector (0),
GetDeformationEigenVector (1),
GetDeformationEigenVector (2));
return r * l * r.transpose ();
}
void Body::GetDeformationTensor (float* value,
const G3D::Matrix3& additionalRotation) const
{
G3D::Matrix3 m = GetDeformationTensor (additionalRotation);
for (size_t i = 0; i < BodyAttribute::TENSOR_NUMBER_OF_COMPONENTS; ++i)
value[i] = m[i / 3][i % 3];
}
float Body::GetDeformationEigenScalar () const
{
size_t maxIndex = 0;
size_t minIndex = Is2D () ? 1 : 2;
float deformationEigen = 1. - GetDeformationEigenValue (minIndex) /
GetDeformationEigenValue (maxIndex);
return deformationEigen;
}
size_t Body::GetSidesPerBody () const
{
if (Is2D ())
return GetOrientedFace (0).GetFace ()->GetEdgesPerFace (Is2D ());
else
// return the number of physical faces
return GetNeighbors ().size ();
}
void Body::SetPressureValue (double value)
{
SetAttribute<RealAttribute, double> (
BodyScalar::PRESSURE - BodyScalar::DMP_BEGIN, value);
}
void Body::CalculateBoundingBox ()
{
m_boundingBox = ::CalculateBoundingBox (*this);
}
float Body::calculateArea () const
{
float area = 0;
BOOST_FOREACH (boost::shared_ptr<OrientedFace> of, GetOrientedFaces ())
area += (of->GetArea ());
return area;
}
float Body::CalculateVolume () const
{
float volume = 0;
G3D::Vector3 d = GetCenter ();
BOOST_FOREACH (boost::shared_ptr<OrientedFace> of, GetOrientedFaces ())
{
if (of->size () != 3)
{
volume = 0;
break;
}
G3D::Vector3 a = of->GetOrientedEdge (0).GetBegin ().GetVector ();
G3D::Vector3 b = of->GetOrientedEdge (1).GetBegin ().GetVector ();
G3D::Vector3 c = of->GetOrientedEdge (2).GetBegin ().GetVector ();
volume += (a - d).dot ((b - d).cross (c - d)) / 6;
}
return volume;
}
float Body::GetBubbleDiameter (float volume, bool is2D)
{
if (is2D)
return 2 * sqrt (volume / M_PI);
else
return 2 * pow (volume * 3 / (4 * M_PI), 1.0 / 3.0);
}
float Body::GetBubbleDiameter () const
{
return GetBubbleDiameter (
GetScalarValue (BodyScalar::TARGET_VOLUME), Is2D ());
}
void Body::CalculateDeformationSimple ()
{
if (! HasScalarValue (BodyScalar::TARGET_VOLUME))
return;
m_area = calculateArea ();
if (Is2D ())
{
boost::shared_ptr<OrientedFace> of = GetOrientedFacePtr (0);
of->CalculatePerimeter ();
m_deformationSimple = of->GetPerimeter () /
sqrt (GetScalarValue (BodyScalar::TARGET_VOLUME));
}
else
{
m_deformationSimple =
GetArea () /
pow (GetScalarValue (BodyScalar::TARGET_VOLUME),
static_cast<float>(2.0 / 3.0));
}
}
const char* Body::GetAttributeKeywordString (BodyScalar::Enum bp)
{
using EvolverData::parser;
switch (bp)
{
case BodyScalar::PRESSURE:
return ParsingDriver::GetKeywordString(
parser::token::LAGRANGE_MULTIPLIER);
case BodyScalar::TARGET_VOLUME:
return ParsingDriver::GetKeywordString(parser::token::VOLUME);
case BodyScalar::ACTUAL_VOLUME:
return ParsingDriver::GetKeywordString(parser::token::ACTUAL);
default:
return 0;
}
}
void Body::CalculateNeighborsAndGrowthRate (const OOBox& originalDomain,
bool is2D)
{
if (is2D)
calculateNeighbors2D (originalDomain);
else
calculateNeighbors3D (originalDomain);
}
void Body::calculateNeighbors3D (const OOBox& originalDomain)
{
set<size_t> neighborId;
size_t i = 0;
BOOST_FOREACH (boost::shared_ptr<OrientedFace> of, GetOrientedFaces ())
{
// wall faces do not create neighbors (have only this as adjacent body)
if (of->HasConstraints ())
{
/**
* @todo Reflect for 3D faces with constraints?
*/
}
else if (of->GetAdjacentBodySize () == 2)
{
const AdjacentBody& ab = of->GetAdjacentBody (true);
boost::shared_ptr<Body> body = ab.GetBody ();
G3D::Vector3int16 translation;
originalDomain.IsWrap (GetCenter (), body->GetCenter (),
&translation);
Neighbor neighbor (body, Vector3int16Zero - translation);
// The calculations can be executed on original pressure
// (medians are not aligned) yielding the same results.
m_growthRate +=
(GetScalarValue (BodyScalar::PRESSURE) -
body->GetScalarValue (BodyScalar::PRESSURE)) *
of->GetArea ();
// insert the neighbor into the map
pair< set<size_t>::iterator, bool> p =
neighborId.insert (body->GetId ());
if (p.second)
// Neighbor that has not been seen before.
// As a physical face can have several tessellation faces
// a neighbor appears several times.
m_neighbors.push_back (neighbor);
}
else
{
// there are several free tessellation faces contained in one
// physical face
m_hasFreeFace = true;
// pressure of the outside is 0
m_growthRate += GetScalarValue (BodyScalar::PRESSURE) *
of->GetArea ();
/**
* @todo Reflect for free faces as well?
*/
}
++i;
}
}
void Body::calculateNeighbors2D (const OOBox& originalDomain)
{
const OrientedFace& of = GetOrientedFace (0);
m_neighbors.resize (of.size ());
size_t j = 0;
for (size_t i = 0; i < of.size (); ++i, ++j)
{
OrientedEdge oe = of.GetOrientedEdge (i);
if (oe.HasConstraints ())
{
G3D::Vector3 b = oe.GetBeginVector ();
G3D::Vector3 e = oe.GetEndVector ();
G3D::Vector3 m = (b + e) / 2;
/*
* Another way to do the reflection.
size_t i = oe.GetPointCount () / 2;
G3D::Vector3 m = oe.GetPoint (i);
*/
G3D::Vector3 c = GetCenter ();
m_neighbors[j] = Neighbor (c + 2 * (m - c));
}
else if (oe.GetAdjacentOrientedFacesSize () == 2)
{
const AdjacentOrientedFaces& aofs = oe.GetAdjacentOrientedFaces ();
AdjacentOrientedFaces::const_iterator it = aofs.begin ();
while (it != aofs.end () && (it->IsStandalone () ||
it->GetBodyId () == GetId ()))
++it;
if (it == aofs.end ())
{
--j;
continue;
}
boost::shared_ptr<Body> body = it->GetBody ();
G3D::Vector3int16 translation;
originalDomain.IsWrap (GetCenter (), body->GetCenter (),
&translation);
m_neighbors[j] = Neighbor (body,
Vector3int16Zero - translation);
m_growthRate +=
(GetScalarValue (BodyScalar::PRESSURE) -
body->GetScalarValue (BodyScalar::PRESSURE)) *
oe.GetLength ();
}
else
{
RuntimeAssert (
oe.GetAdjacentOrientedFacesSize () == 1,
"OrientedEdge::GetAdjacentOrientedFacesSize != 1:",
oe.GetAdjacentOrientedFacesSize ());
m_hasFreeFace = true;
m_growthRate += GetScalarValue (BodyScalar::PRESSURE) *
oe.GetLength ();
/**
* @todo Reflect for free faces as well?
*/
}
}
m_neighbors.resize (j);
}
void Body::CalculateDeformationTensor (const OOBox& originalDomain)
{
if (IsObject ())
return;
size_t bubbleNeighborsCount = 0;
G3D::Matrix3 textureTensor = G3D::Matrix3::zero ();
const vector<Neighbor>& neighbors = GetNeighbors ();
BOOST_FOREACH (Body::Neighbor neighbor, neighbors)
{
G3D::Vector3 s;
if (neighbor.GetBody ())
{
++bubbleNeighborsCount;
s = neighbor.GetBody ()->GetCenter ();
}
else
{
// debug: no reflection used in average computation.
// continue;
s = neighbor.GetCenterReflection ();
}
G3D::Vector3 first = GetCenter ();
G3D::Vector3 second =
originalDomain.TorusTranslate (s, neighbor.GetTranslation ());
G3D::Vector3 l = second - first;
textureTensor += G3D::Matrix3 (l.x * l.x, l.x * l.y, l.x * l.z,
l.y * l.x, l.y * l.y, l.y * l.z,
l.z * l.x, l.z * l.y, l.z * l.z);
}
textureTensor /= neighbors.size ();
SymmetricMatrixEigen().Calculate (textureTensor,
&m_deformationEigenValues[0],
&m_deformationEigenVectors[0]);
//textureTensor.eigenSolveSymmetric (
//&m_deformationEigenValues[0], &m_deformationEigenVectors[0]);
__LOG__(
ostream_iterator<float> of(cdbg, " ");
copy (m_deformationEigenValues.begin (),
m_deformationEigenValues.end (), of);
cdbg << endl;
ostream_iterator<G3D::Vector3> ov (cdbg, "\n");
copy (m_deformationEigenVectors.begin (),
m_deformationEigenVectors.end (), ov);
);
if (bubbleNeighborsCount == 0)
m_object = true;
}
size_t Body::GetConstraintIndex () const
{
return GetFace (0).GetOrientedEdge (0).GetConstraintIndex ();
}
vtkSmartPointer<vtkPolyData> Body::GetPolyData () const
{
return OrientedFace::GetPolyData (GetOrientedFaces ());
}