1 | #include "molecules.hpp"
|
---|
2 | #include "boundary.hpp"
|
---|
3 |
|
---|
4 | #define DEBUG 0
|
---|
5 |
|
---|
6 | // ======================================== Points on Boundary =================================
|
---|
7 |
|
---|
8 | BoundaryPointSet::BoundaryPointSet()
|
---|
9 | {
|
---|
10 | LinesCount = 0;
|
---|
11 | Nr = -1;
|
---|
12 | }
|
---|
13 | ;
|
---|
14 |
|
---|
15 | BoundaryPointSet::BoundaryPointSet(atom *Walker)
|
---|
16 | {
|
---|
17 | node = Walker;
|
---|
18 | LinesCount = 0;
|
---|
19 | Nr = Walker->nr;
|
---|
20 | }
|
---|
21 | ;
|
---|
22 |
|
---|
23 | BoundaryPointSet::~BoundaryPointSet()
|
---|
24 | {
|
---|
25 | cout << Verbose(5) << "Erasing point nr. " << Nr << "." << endl;
|
---|
26 | node = NULL;
|
---|
27 | }
|
---|
28 | ;
|
---|
29 |
|
---|
30 | void
|
---|
31 | BoundaryPointSet::AddLine(class BoundaryLineSet *line)
|
---|
32 | {
|
---|
33 | cout << Verbose(6) << "Adding " << *this << " to line " << *line << "."
|
---|
34 | << endl;
|
---|
35 | if (line->endpoints[0] == this)
|
---|
36 | {
|
---|
37 | lines.insert(LinePair(line->endpoints[1]->Nr, line));
|
---|
38 | }
|
---|
39 | else
|
---|
40 | {
|
---|
41 | lines.insert(LinePair(line->endpoints[0]->Nr, line));
|
---|
42 | }
|
---|
43 | LinesCount++;
|
---|
44 | }
|
---|
45 | ;
|
---|
46 |
|
---|
47 | ostream &
|
---|
48 | operator <<(ostream &ost, BoundaryPointSet &a)
|
---|
49 | {
|
---|
50 | ost << "[" << a.Nr << "|" << a.node->Name << "]";
|
---|
51 | return ost;
|
---|
52 | }
|
---|
53 | ;
|
---|
54 |
|
---|
55 | // ======================================== Lines on Boundary =================================
|
---|
56 |
|
---|
57 | BoundaryLineSet::BoundaryLineSet()
|
---|
58 | {
|
---|
59 | for (int i = 0; i < 2; i++)
|
---|
60 | endpoints[i] = NULL;
|
---|
61 | TrianglesCount = 0;
|
---|
62 | Nr = -1;
|
---|
63 | }
|
---|
64 | ;
|
---|
65 |
|
---|
66 | BoundaryLineSet::BoundaryLineSet(class BoundaryPointSet *Point[2], int number)
|
---|
67 | {
|
---|
68 | // set number
|
---|
69 | Nr = number;
|
---|
70 | // set endpoints in ascending order
|
---|
71 | SetEndpointsOrdered(endpoints, Point[0], Point[1]);
|
---|
72 | // add this line to the hash maps of both endpoints
|
---|
73 | Point[0]->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding.
|
---|
74 | Point[1]->AddLine(this); //
|
---|
75 | // clear triangles list
|
---|
76 | TrianglesCount = 0;
|
---|
77 | cout << Verbose(5) << "New Line with endpoints " << *this << "." << endl;
|
---|
78 | }
|
---|
79 | ;
|
---|
80 |
|
---|
81 | BoundaryLineSet::~BoundaryLineSet()
|
---|
82 | {
|
---|
83 | for (int i = 0; i < 2; i++)
|
---|
84 | {
|
---|
85 | cout << Verbose(5) << "Erasing Line Nr. " << Nr << " in boundary point "
|
---|
86 | << *endpoints[i] << "." << endl;
|
---|
87 | endpoints[i]->lines.erase(Nr);
|
---|
88 | LineMap::iterator tester = endpoints[i]->lines.begin();
|
---|
89 | tester++;
|
---|
90 | if (tester == endpoints[i]->lines.end())
|
---|
91 | {
|
---|
92 | cout << Verbose(5) << *endpoints[i]
|
---|
93 | << " has no more lines it's attached to, erasing." << endl;
|
---|
94 | //delete(endpoints[i]);
|
---|
95 | }
|
---|
96 | else
|
---|
97 | cout << Verbose(5) << *endpoints[i]
|
---|
98 | << " has still lines it's attached to." << endl;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | ;
|
---|
102 |
|
---|
103 | void
|
---|
104 | BoundaryLineSet::AddTriangle(class BoundaryTriangleSet *triangle)
|
---|
105 | {
|
---|
106 | cout << Verbose(6) << "Add " << triangle->Nr << " to line " << *this << "."
|
---|
107 | << endl;
|
---|
108 | triangles.insert(TrianglePair(TrianglesCount, triangle));
|
---|
109 | TrianglesCount++;
|
---|
110 | }
|
---|
111 | ;
|
---|
112 |
|
---|
113 | ostream &
|
---|
114 | operator <<(ostream &ost, BoundaryLineSet &a)
|
---|
115 | {
|
---|
116 | ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << ","
|
---|
117 | << a.endpoints[1]->node->Name << "]";
|
---|
118 | return ost;
|
---|
119 | }
|
---|
120 | ;
|
---|
121 |
|
---|
122 | // ======================================== Triangles on Boundary =================================
|
---|
123 |
|
---|
124 |
|
---|
125 | BoundaryTriangleSet::BoundaryTriangleSet()
|
---|
126 | {
|
---|
127 | for (int i = 0; i < 3; i++)
|
---|
128 | {
|
---|
129 | endpoints[i] = NULL;
|
---|
130 | lines[i] = NULL;
|
---|
131 | }
|
---|
132 | Nr = -1;
|
---|
133 | }
|
---|
134 | ;
|
---|
135 |
|
---|
136 | BoundaryTriangleSet::BoundaryTriangleSet(class BoundaryLineSet *line[3],
|
---|
137 | int number)
|
---|
138 | {
|
---|
139 | // set number
|
---|
140 | Nr = number;
|
---|
141 | // set lines
|
---|
142 | cout << Verbose(5) << "New triangle " << Nr << ":" << endl;
|
---|
143 | for (int i = 0; i < 3; i++)
|
---|
144 | {
|
---|
145 | lines[i] = line[i];
|
---|
146 | lines[i]->AddTriangle(this);
|
---|
147 | }
|
---|
148 | // get ascending order of endpoints
|
---|
149 | map<int, class BoundaryPointSet *> OrderMap;
|
---|
150 | for (int i = 0; i < 3; i++)
|
---|
151 | // for all three lines
|
---|
152 | for (int j = 0; j < 2; j++)
|
---|
153 | { // for both endpoints
|
---|
154 | OrderMap.insert(pair<int, class BoundaryPointSet *> (
|
---|
155 | line[i]->endpoints[j]->Nr, line[i]->endpoints[j]));
|
---|
156 | // and we don't care whether insertion fails
|
---|
157 | }
|
---|
158 | // set endpoints
|
---|
159 | int Counter = 0;
|
---|
160 | cout << Verbose(6) << " with end points ";
|
---|
161 | for (map<int, class BoundaryPointSet *>::iterator runner = OrderMap.begin(); runner
|
---|
162 | != OrderMap.end(); runner++)
|
---|
163 | {
|
---|
164 | endpoints[Counter] = runner->second;
|
---|
165 | cout << " " << *endpoints[Counter];
|
---|
166 | Counter++;
|
---|
167 | }
|
---|
168 | if (Counter < 3)
|
---|
169 | {
|
---|
170 | cerr << "ERROR! We have a triangle with only two distinct endpoints!"
|
---|
171 | << endl;
|
---|
172 | //exit(1);
|
---|
173 | }
|
---|
174 | cout << "." << endl;
|
---|
175 | }
|
---|
176 | ;
|
---|
177 |
|
---|
178 | BoundaryTriangleSet::~BoundaryTriangleSet()
|
---|
179 | {
|
---|
180 | for (int i = 0; i < 3; i++)
|
---|
181 | {
|
---|
182 | cout << Verbose(5) << "Erasing triangle Nr." << Nr << endl;
|
---|
183 | lines[i]->triangles.erase(Nr);
|
---|
184 | TriangleMap::iterator tester = lines[i]->triangles.begin();
|
---|
185 | tester++;
|
---|
186 | if (tester == lines[i]->triangles.end())
|
---|
187 | {
|
---|
188 | cout << Verbose(5) << *lines[i]
|
---|
189 | << " is no more attached to any triangle, erasing." << endl;
|
---|
190 | delete (lines[i]);
|
---|
191 | }
|
---|
192 | else
|
---|
193 | cout << Verbose(5) << *lines[i] << " is still attached to a triangle."
|
---|
194 | << endl;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | ;
|
---|
198 |
|
---|
199 | void
|
---|
200 | BoundaryTriangleSet::GetNormalVector(Vector &OtherVector)
|
---|
201 | {
|
---|
202 | // get normal vector
|
---|
203 | NormalVector.MakeNormalVector(&endpoints[0]->node->x, &endpoints[1]->node->x,
|
---|
204 | &endpoints[2]->node->x);
|
---|
205 |
|
---|
206 | // make it always point inward (any offset vector onto plane projected onto normal vector suffices)
|
---|
207 | if (endpoints[0]->node->x.Projection(&OtherVector) > 0)
|
---|
208 | NormalVector.Scale(-1.);
|
---|
209 | }
|
---|
210 | ;
|
---|
211 |
|
---|
212 | ostream &
|
---|
213 | operator <<(ostream &ost, BoundaryTriangleSet &a)
|
---|
214 | {
|
---|
215 | ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << ","
|
---|
216 | << a.endpoints[1]->node->Name << "," << a.endpoints[2]->node->Name << "]";
|
---|
217 | return ost;
|
---|
218 | }
|
---|
219 | ;
|
---|
220 |
|
---|
221 | // ========================================== F U N C T I O N S =================================
|
---|
222 |
|
---|
223 | /** Finds the endpoint two lines are sharing.
|
---|
224 | * \param *line1 first line
|
---|
225 | * \param *line2 second line
|
---|
226 | * \return point which is shared or NULL if none
|
---|
227 | */
|
---|
228 | class BoundaryPointSet *
|
---|
229 | GetCommonEndpoint(class BoundaryLineSet * line1, class BoundaryLineSet * line2)
|
---|
230 | {
|
---|
231 | class BoundaryLineSet * lines[2] =
|
---|
232 | { line1, line2 };
|
---|
233 | class BoundaryPointSet *node = NULL;
|
---|
234 | map<int, class BoundaryPointSet *> OrderMap;
|
---|
235 | pair<map<int, class BoundaryPointSet *>::iterator, bool> OrderTest;
|
---|
236 | for (int i = 0; i < 2; i++)
|
---|
237 | // for both lines
|
---|
238 | for (int j = 0; j < 2; j++)
|
---|
239 | { // for both endpoints
|
---|
240 | OrderTest = OrderMap.insert(pair<int, class BoundaryPointSet *> (
|
---|
241 | lines[i]->endpoints[j]->Nr, lines[i]->endpoints[j]));
|
---|
242 | if (!OrderTest.second)
|
---|
243 | { // if insertion fails, we have common endpoint
|
---|
244 | node = OrderTest.first->second;
|
---|
245 | cout << Verbose(5) << "Common endpoint of lines " << *line1
|
---|
246 | << " and " << *line2 << " is: " << *node << "." << endl;
|
---|
247 | j = 2;
|
---|
248 | i = 2;
|
---|
249 | break;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | return node;
|
---|
253 | }
|
---|
254 | ;
|
---|
255 |
|
---|
256 | /** Determines the boundary points of a cluster.
|
---|
257 | * Does a projection per axis onto the orthogonal plane, transforms into spherical coordinates, sorts them by the angle
|
---|
258 | * and looks at triples: if the middle has less a distance than the allowed maximum height of the triangle formed by the plane's
|
---|
259 | * center and first and last point in the triple, it is thrown out.
|
---|
260 | * \param *out output stream for debugging
|
---|
261 | * \param *mol molecule structure representing the cluster
|
---|
262 | */
|
---|
263 | Boundaries *
|
---|
264 | GetBoundaryPoints(ofstream *out, molecule *mol)
|
---|
265 | {
|
---|
266 | atom *Walker = NULL;
|
---|
267 | PointMap PointsOnBoundary;
|
---|
268 | LineMap LinesOnBoundary;
|
---|
269 | TriangleMap TrianglesOnBoundary;
|
---|
270 |
|
---|
271 | *out << Verbose(1) << "Finding all boundary points." << endl;
|
---|
272 | Boundaries *BoundaryPoints = new Boundaries[NDIM]; // first is alpha, second is (r, nr)
|
---|
273 | BoundariesTestPair BoundaryTestPair;
|
---|
274 | Vector AxisVector, AngleReferenceVector, AngleReferenceNormalVector;
|
---|
275 | double radius, angle;
|
---|
276 | // 3a. Go through every axis
|
---|
277 | for (int axis = 0; axis < NDIM; axis++)
|
---|
278 | {
|
---|
279 | AxisVector.Zero();
|
---|
280 | AngleReferenceVector.Zero();
|
---|
281 | AngleReferenceNormalVector.Zero();
|
---|
282 | AxisVector.x[axis] = 1.;
|
---|
283 | AngleReferenceVector.x[(axis + 1) % NDIM] = 1.;
|
---|
284 | AngleReferenceNormalVector.x[(axis + 2) % NDIM] = 1.;
|
---|
285 | // *out << Verbose(1) << "Axisvector is ";
|
---|
286 | // AxisVector.Output(out);
|
---|
287 | // *out << " and AngleReferenceVector is ";
|
---|
288 | // AngleReferenceVector.Output(out);
|
---|
289 | // *out << "." << endl;
|
---|
290 | // *out << " and AngleReferenceNormalVector is ";
|
---|
291 | // AngleReferenceNormalVector.Output(out);
|
---|
292 | // *out << "." << endl;
|
---|
293 | // 3b. construct set of all points, transformed into cylindrical system and with left and right neighbours
|
---|
294 | Walker = mol->start;
|
---|
295 | while (Walker->next != mol->end)
|
---|
296 | {
|
---|
297 | Walker = Walker->next;
|
---|
298 | Vector ProjectedVector;
|
---|
299 | ProjectedVector.CopyVector(&Walker->x);
|
---|
300 | ProjectedVector.ProjectOntoPlane(&AxisVector);
|
---|
301 | // correct for negative side
|
---|
302 | //if (Projection(y) < 0)
|
---|
303 | //angle = 2.*M_PI - angle;
|
---|
304 | radius = ProjectedVector.Norm();
|
---|
305 | if (fabs(radius) > MYEPSILON)
|
---|
306 | angle = ProjectedVector.Angle(&AngleReferenceVector);
|
---|
307 | else
|
---|
308 | angle = 0.; // otherwise it's a vector in Axis Direction and unimportant for boundary issues
|
---|
309 |
|
---|
310 | //*out << "Checking sign in quadrant : " << ProjectedVector.Projection(&AngleReferenceNormalVector) << "." << endl;
|
---|
311 | if (ProjectedVector.Projection(&AngleReferenceNormalVector) > 0)
|
---|
312 | {
|
---|
313 | angle = 2. * M_PI - angle;
|
---|
314 | }
|
---|
315 | //*out << Verbose(2) << "Inserting " << *Walker << ": (r, alpha) = (" << radius << "," << angle << "): ";
|
---|
316 | //ProjectedVector.Output(out);
|
---|
317 | //*out << endl;
|
---|
318 | BoundaryTestPair = BoundaryPoints[axis].insert(BoundariesPair(angle,
|
---|
319 | DistancePair (radius, Walker)));
|
---|
320 | if (BoundaryTestPair.second)
|
---|
321 | { // successfully inserted
|
---|
322 | }
|
---|
323 | else
|
---|
324 | { // same point exists, check first r, then distance of original vectors to center of gravity
|
---|
325 | *out << Verbose(2)
|
---|
326 | << "Encountered two vectors whose projection onto axis "
|
---|
327 | << axis << " is equal: " << endl;
|
---|
328 | *out << Verbose(2) << "Present vector: ";
|
---|
329 | BoundaryTestPair.first->second.second->x.Output(out);
|
---|
330 | *out << endl;
|
---|
331 | *out << Verbose(2) << "New vector: ";
|
---|
332 | Walker->x.Output(out);
|
---|
333 | *out << endl;
|
---|
334 | double tmp = ProjectedVector.Norm();
|
---|
335 | if (tmp > BoundaryTestPair.first->second.first)
|
---|
336 | {
|
---|
337 | BoundaryTestPair.first->second.first = tmp;
|
---|
338 | BoundaryTestPair.first->second.second = Walker;
|
---|
339 | *out << Verbose(2) << "Keeping new vector." << endl;
|
---|
340 | }
|
---|
341 | else if (tmp == BoundaryTestPair.first->second.first)
|
---|
342 | {
|
---|
343 | if (BoundaryTestPair.first->second.second->x.ScalarProduct(
|
---|
344 | &BoundaryTestPair.first->second.second->x)
|
---|
345 | < Walker->x.ScalarProduct(&Walker->x))
|
---|
346 | { // Norm() does a sqrt, which makes it a lot slower
|
---|
347 | BoundaryTestPair.first->second.second = Walker;
|
---|
348 | *out << Verbose(2) << "Keeping new vector." << endl;
|
---|
349 | }
|
---|
350 | else
|
---|
351 | {
|
---|
352 | *out << Verbose(2) << "Keeping present vector." << endl;
|
---|
353 | }
|
---|
354 | }
|
---|
355 | else
|
---|
356 | {
|
---|
357 | *out << Verbose(2) << "Keeping present vector." << endl;
|
---|
358 | }
|
---|
359 | }
|
---|
360 | }
|
---|
361 | // printing all inserted for debugging
|
---|
362 | // {
|
---|
363 | // *out << Verbose(2) << "Printing list of candidates for axis " << axis << " which we have inserted so far." << endl;
|
---|
364 | // int i=0;
|
---|
365 | // for(Boundaries::iterator runner = BoundaryPoints[axis].begin(); runner != BoundaryPoints[axis].end(); runner++) {
|
---|
366 | // if (runner != BoundaryPoints[axis].begin())
|
---|
367 | // *out << ", " << i << ": " << *runner->second.second;
|
---|
368 | // else
|
---|
369 | // *out << i << ": " << *runner->second.second;
|
---|
370 | // i++;
|
---|
371 | // }
|
---|
372 | // *out << endl;
|
---|
373 | // }
|
---|
374 | // 3c. throw out points whose distance is less than the mean of left and right neighbours
|
---|
375 | bool flag = false;
|
---|
376 | do
|
---|
377 | { // do as long as we still throw one out per round
|
---|
378 | *out << Verbose(1)
|
---|
379 | << "Looking for candidates to kick out by convex condition ... "
|
---|
380 | << endl;
|
---|
381 | flag = false;
|
---|
382 | Boundaries::iterator left = BoundaryPoints[axis].end();
|
---|
383 | Boundaries::iterator right = BoundaryPoints[axis].end();
|
---|
384 | for (Boundaries::iterator runner = BoundaryPoints[axis].begin(); runner
|
---|
385 | != BoundaryPoints[axis].end(); runner++)
|
---|
386 | {
|
---|
387 | // set neighbours correctly
|
---|
388 | if (runner == BoundaryPoints[axis].begin())
|
---|
389 | {
|
---|
390 | left = BoundaryPoints[axis].end();
|
---|
391 | }
|
---|
392 | else
|
---|
393 | {
|
---|
394 | left = runner;
|
---|
395 | }
|
---|
396 | left--;
|
---|
397 | right = runner;
|
---|
398 | right++;
|
---|
399 | if (right == BoundaryPoints[axis].end())
|
---|
400 | {
|
---|
401 | right = BoundaryPoints[axis].begin();
|
---|
402 | }
|
---|
403 | // check distance
|
---|
404 |
|
---|
405 | // construct the vector of each side of the triangle on the projected plane (defined by normal vector AxisVector)
|
---|
406 | {
|
---|
407 | Vector SideA, SideB, SideC, SideH;
|
---|
408 | SideA.CopyVector(&left->second.second->x);
|
---|
409 | SideA.ProjectOntoPlane(&AxisVector);
|
---|
410 | // *out << "SideA: ";
|
---|
411 | // SideA.Output(out);
|
---|
412 | // *out << endl;
|
---|
413 |
|
---|
414 | SideB.CopyVector(&right->second.second->x);
|
---|
415 | SideB.ProjectOntoPlane(&AxisVector);
|
---|
416 | // *out << "SideB: ";
|
---|
417 | // SideB.Output(out);
|
---|
418 | // *out << endl;
|
---|
419 |
|
---|
420 | SideC.CopyVector(&left->second.second->x);
|
---|
421 | SideC.SubtractVector(&right->second.second->x);
|
---|
422 | SideC.ProjectOntoPlane(&AxisVector);
|
---|
423 | // *out << "SideC: ";
|
---|
424 | // SideC.Output(out);
|
---|
425 | // *out << endl;
|
---|
426 |
|
---|
427 | SideH.CopyVector(&runner->second.second->x);
|
---|
428 | SideH.ProjectOntoPlane(&AxisVector);
|
---|
429 | // *out << "SideH: ";
|
---|
430 | // SideH.Output(out);
|
---|
431 | // *out << endl;
|
---|
432 |
|
---|
433 | // calculate each length
|
---|
434 | double a = SideA.Norm();
|
---|
435 | //double b = SideB.Norm();
|
---|
436 | //double c = SideC.Norm();
|
---|
437 | double h = SideH.Norm();
|
---|
438 | // calculate the angles
|
---|
439 | double alpha = SideA.Angle(&SideH);
|
---|
440 | double beta = SideA.Angle(&SideC);
|
---|
441 | double gamma = SideB.Angle(&SideH);
|
---|
442 | double delta = SideC.Angle(&SideH);
|
---|
443 | double MinDistance = a * sin(beta) / (sin(delta)) * (((alpha
|
---|
444 | < M_PI / 2.) || (gamma < M_PI / 2.)) ? 1. : -1.);
|
---|
445 | // *out << Verbose(2) << " I calculated: a = " << a << ", h = " << h << ", beta(" << left->second.second->Name << "," << left->second.second->Name << "-" << right->second.second->Name << ") = " << beta << ", delta(" << left->second.second->Name << "," << runner->second.second->Name << ") = " << delta << ", Min = " << MinDistance << "." << endl;
|
---|
446 | //*out << Verbose(1) << "Checking CoG distance of runner " << *runner->second.second << " " << h << " against triangle's side length spanned by (" << *left->second.second << "," << *right->second.second << ") of " << MinDistance << "." << endl;
|
---|
447 | if ((fabs(h / fabs(h) - MinDistance / fabs(MinDistance))
|
---|
448 | < MYEPSILON) && (h < MinDistance))
|
---|
449 | {
|
---|
450 | // throw out point
|
---|
451 | //*out << Verbose(1) << "Throwing out " << *runner->second.second << "." << endl;
|
---|
452 | BoundaryPoints[axis].erase(runner);
|
---|
453 | flag = true;
|
---|
454 | }
|
---|
455 | }
|
---|
456 | }
|
---|
457 | }
|
---|
458 | while (flag);
|
---|
459 | }
|
---|
460 | return BoundaryPoints;
|
---|
461 | }
|
---|
462 | ;
|
---|
463 |
|
---|
464 | /** Determines greatest diameters of a cluster defined by its convex envelope.
|
---|
465 | * Looks at lines parallel to one axis and where they intersect on the projected planes
|
---|
466 | * \param *out output stream for debugging
|
---|
467 | * \param *BoundaryPoints NDIM set of boundary points defining the convex envelope on each projected plane
|
---|
468 | * \param *mol molecule structure representing the cluster
|
---|
469 | * \param IsAngstroem whether we have angstroem or atomic units
|
---|
470 | * \return NDIM array of the diameters
|
---|
471 | */
|
---|
472 | double *
|
---|
473 | GetDiametersOfCluster(ofstream *out, Boundaries *BoundaryPtr, molecule *mol,
|
---|
474 | bool IsAngstroem)
|
---|
475 | {
|
---|
476 | // get points on boundary of NULL was given as parameter
|
---|
477 | bool BoundaryFreeFlag = false;
|
---|
478 | Boundaries *BoundaryPoints = BoundaryPtr;
|
---|
479 | if (BoundaryPoints == NULL)
|
---|
480 | {
|
---|
481 | BoundaryFreeFlag = true;
|
---|
482 | BoundaryPoints = GetBoundaryPoints(out, mol);
|
---|
483 | }
|
---|
484 | else
|
---|
485 | {
|
---|
486 | *out << Verbose(1) << "Using given boundary points set." << endl;
|
---|
487 | }
|
---|
488 | // determine biggest "diameter" of cluster for each axis
|
---|
489 | Boundaries::iterator Neighbour, OtherNeighbour;
|
---|
490 | double *GreatestDiameter = new double[NDIM];
|
---|
491 | for (int i = 0; i < NDIM; i++)
|
---|
492 | GreatestDiameter[i] = 0.;
|
---|
493 | double OldComponent, tmp, w1, w2;
|
---|
494 | Vector DistanceVector, OtherVector;
|
---|
495 | int component, Othercomponent;
|
---|
496 | for (int axis = 0; axis < NDIM; axis++)
|
---|
497 | { // regard each projected plane
|
---|
498 | //*out << Verbose(1) << "Current axis is " << axis << "." << endl;
|
---|
499 | for (int j = 0; j < 2; j++)
|
---|
500 | { // and for both axis on the current plane
|
---|
501 | component = (axis + j + 1) % NDIM;
|
---|
502 | Othercomponent = (axis + 1 + ((j + 1) & 1)) % NDIM;
|
---|
503 | //*out << Verbose(1) << "Current component is " << component << ", Othercomponent is " << Othercomponent << "." << endl;
|
---|
504 | for (Boundaries::iterator runner = BoundaryPoints[axis].begin(); runner
|
---|
505 | != BoundaryPoints[axis].end(); runner++)
|
---|
506 | {
|
---|
507 | //*out << Verbose(2) << "Current runner is " << *(runner->second.second) << "." << endl;
|
---|
508 | // seek for the neighbours pair where the Othercomponent sign flips
|
---|
509 | Neighbour = runner;
|
---|
510 | Neighbour++;
|
---|
511 | if (Neighbour == BoundaryPoints[axis].end()) // make it wrap around
|
---|
512 | Neighbour = BoundaryPoints[axis].begin();
|
---|
513 | DistanceVector.CopyVector(&runner->second.second->x);
|
---|
514 | DistanceVector.SubtractVector(&Neighbour->second.second->x);
|
---|
515 | do
|
---|
516 | { // seek for neighbour pair where it flips
|
---|
517 | OldComponent = DistanceVector.x[Othercomponent];
|
---|
518 | Neighbour++;
|
---|
519 | if (Neighbour == BoundaryPoints[axis].end()) // make it wrap around
|
---|
520 | Neighbour = BoundaryPoints[axis].begin();
|
---|
521 | DistanceVector.CopyVector(&runner->second.second->x);
|
---|
522 | DistanceVector.SubtractVector(&Neighbour->second.second->x);
|
---|
523 | //*out << Verbose(3) << "OldComponent is " << OldComponent << ", new one is " << DistanceVector.x[Othercomponent] << "." << endl;
|
---|
524 | }
|
---|
525 | while ((runner != Neighbour) && (fabs(OldComponent / fabs(
|
---|
526 | OldComponent) - DistanceVector.x[Othercomponent] / fabs(
|
---|
527 | DistanceVector.x[Othercomponent])) < MYEPSILON)); // as long as sign does not flip
|
---|
528 | if (runner != Neighbour)
|
---|
529 | {
|
---|
530 | OtherNeighbour = Neighbour;
|
---|
531 | if (OtherNeighbour == BoundaryPoints[axis].begin()) // make it wrap around
|
---|
532 | OtherNeighbour = BoundaryPoints[axis].end();
|
---|
533 | OtherNeighbour--;
|
---|
534 | //*out << Verbose(2) << "The pair, where the sign of OtherComponent flips, is: " << *(Neighbour->second.second) << " and " << *(OtherNeighbour->second.second) << "." << endl;
|
---|
535 | // now we have found the pair: Neighbour and OtherNeighbour
|
---|
536 | OtherVector.CopyVector(&runner->second.second->x);
|
---|
537 | OtherVector.SubtractVector(&OtherNeighbour->second.second->x);
|
---|
538 | //*out << Verbose(2) << "Distances to Neighbour and OtherNeighbour are " << DistanceVector.x[component] << " and " << OtherVector.x[component] << "." << endl;
|
---|
539 | //*out << Verbose(2) << "OtherComponents to Neighbour and OtherNeighbour are " << DistanceVector.x[Othercomponent] << " and " << OtherVector.x[Othercomponent] << "." << endl;
|
---|
540 | // do linear interpolation between points (is exact) to extract exact intersection between Neighbour and OtherNeighbour
|
---|
541 | w1 = fabs(OtherVector.x[Othercomponent]);
|
---|
542 | w2 = fabs(DistanceVector.x[Othercomponent]);
|
---|
543 | tmp = fabs((w1 * DistanceVector.x[component] + w2
|
---|
544 | * OtherVector.x[component]) / (w1 + w2));
|
---|
545 | // mark if it has greater diameter
|
---|
546 | //*out << Verbose(2) << "Comparing current greatest " << GreatestDiameter[component] << " to new " << tmp << "." << endl;
|
---|
547 | GreatestDiameter[component] = (GreatestDiameter[component]
|
---|
548 | > tmp) ? GreatestDiameter[component] : tmp;
|
---|
549 | } //else
|
---|
550 | //*out << Verbose(2) << "Saw no sign flip, probably top or bottom node." << endl;
|
---|
551 | }
|
---|
552 | }
|
---|
553 | }
|
---|
554 | *out << Verbose(0) << "RESULT: The biggest diameters are "
|
---|
555 | << GreatestDiameter[0] << " and " << GreatestDiameter[1] << " and "
|
---|
556 | << GreatestDiameter[2] << " " << (IsAngstroem ? "angstrom"
|
---|
557 | : "atomiclength") << "." << endl;
|
---|
558 |
|
---|
559 | // free reference lists
|
---|
560 | if (BoundaryFreeFlag)
|
---|
561 | delete[] (BoundaryPoints);
|
---|
562 |
|
---|
563 | return GreatestDiameter;
|
---|
564 | }
|
---|
565 | ;
|
---|
566 |
|
---|
567 | /*
|
---|
568 | * This function creates the tecplot file, displaying the tesselation of the hull.
|
---|
569 | * \param *out output stream for debugging
|
---|
570 | * \param *tecplot output stream for tecplot data
|
---|
571 | * \param N arbitrary number to differentiate various zones in the tecplot format
|
---|
572 | */
|
---|
573 | void
|
---|
574 | write_tecplot_file(ofstream *out, ofstream *tecplot,
|
---|
575 | class Tesselation *TesselStruct, class molecule *mol, int N)
|
---|
576 | {
|
---|
577 | if (tecplot != NULL)
|
---|
578 | {
|
---|
579 | *tecplot << "TITLE = \"3D CONVEX SHELL\"" << endl;
|
---|
580 | *tecplot << "VARIABLES = \"X\" \"Y\" \"Z\"" << endl;
|
---|
581 | *tecplot << "ZONE T=\"TRIANGLES" << N << "\", N="
|
---|
582 | << TesselStruct->PointsOnBoundaryCount << ", E="
|
---|
583 | << TesselStruct->TrianglesOnBoundaryCount
|
---|
584 | << ", DATAPACKING=POINT, ZONETYPE=FETRIANGLE" << endl;
|
---|
585 | int *LookupList = new int[mol->AtomCount];
|
---|
586 | for (int i = 0; i < mol->AtomCount; i++)
|
---|
587 | LookupList[i] = -1;
|
---|
588 |
|
---|
589 | // print atom coordinates
|
---|
590 | *out << Verbose(2) << "The following triangles were created:";
|
---|
591 | int Counter = 1;
|
---|
592 | atom *Walker = NULL;
|
---|
593 | for (PointMap::iterator target = TesselStruct->PointsOnBoundary.begin(); target
|
---|
594 | != TesselStruct->PointsOnBoundary.end(); target++)
|
---|
595 | {
|
---|
596 | Walker = target->second->node;
|
---|
597 | LookupList[Walker->nr] = Counter++;
|
---|
598 | *tecplot << Walker->x.x[0] << " " << Walker->x.x[1] << " "
|
---|
599 | << Walker->x.x[2] << " " << endl;
|
---|
600 | }
|
---|
601 | *tecplot << endl;
|
---|
602 | // print connectivity
|
---|
603 | for (TriangleMap::iterator runner =
|
---|
604 | TesselStruct->TrianglesOnBoundary.begin(); runner
|
---|
605 | != TesselStruct->TrianglesOnBoundary.end(); runner++)
|
---|
606 | {
|
---|
607 | *out << " " << runner->second->endpoints[0]->node->Name << "<->"
|
---|
608 | << runner->second->endpoints[1]->node->Name << "<->"
|
---|
609 | << runner->second->endpoints[2]->node->Name;
|
---|
610 | *tecplot << LookupList[runner->second->endpoints[0]->node->nr] << " "
|
---|
611 | << LookupList[runner->second->endpoints[1]->node->nr] << " "
|
---|
612 | << LookupList[runner->second->endpoints[2]->node->nr] << endl;
|
---|
613 | }
|
---|
614 | delete[] (LookupList);
|
---|
615 | *out << endl;
|
---|
616 | }
|
---|
617 | }
|
---|
618 |
|
---|
619 | /** Determines the volume of a cluster.
|
---|
620 | * Determines first the convex envelope, then tesselates it and calculates its volume.
|
---|
621 | * \param *out output stream for debugging
|
---|
622 | * \param *tecplot output stream for tecplot data
|
---|
623 | * \param *configuration needed for path to store convex envelope file
|
---|
624 | * \param *BoundaryPoints NDIM set of boundary points on the projected plane per axis, on return if desired
|
---|
625 | * \param *mol molecule structure representing the cluster
|
---|
626 | * \return determined volume of the cluster in cubed config:GetIsAngstroem()
|
---|
627 | */
|
---|
628 | double
|
---|
629 | VolumeOfConvexEnvelope(ofstream *out, ofstream *tecplot, config *configuration,
|
---|
630 | Boundaries *BoundaryPtr, molecule *mol)
|
---|
631 | {
|
---|
632 | bool IsAngstroem = configuration->GetIsAngstroem();
|
---|
633 | atom *Walker = NULL;
|
---|
634 | struct Tesselation *TesselStruct = new Tesselation;
|
---|
635 | bool BoundaryFreeFlag = false;
|
---|
636 | Boundaries *BoundaryPoints = BoundaryPtr;
|
---|
637 | double volume = 0.;
|
---|
638 | double PyramidVolume = 0.;
|
---|
639 | double G, h;
|
---|
640 | Vector x, y;
|
---|
641 | double a, b, c;
|
---|
642 |
|
---|
643 | //Find_non_convex_border(out, tecplot, *TesselStruct, mol); // Is now called from command line.
|
---|
644 |
|
---|
645 | // 1. calculate center of gravity
|
---|
646 | *out << endl;
|
---|
647 | Vector *CenterOfGravity = mol->DetermineCenterOfGravity(out);
|
---|
648 |
|
---|
649 | // 2. translate all points into CoG
|
---|
650 | *out << Verbose(1) << "Translating system to Center of Gravity." << endl;
|
---|
651 | Walker = mol->start;
|
---|
652 | while (Walker->next != mol->end)
|
---|
653 | {
|
---|
654 | Walker = Walker->next;
|
---|
655 | Walker->x.Translate(CenterOfGravity);
|
---|
656 | }
|
---|
657 |
|
---|
658 | // 3. Find all points on the boundary
|
---|
659 | if (BoundaryPoints == NULL)
|
---|
660 | {
|
---|
661 | BoundaryFreeFlag = true;
|
---|
662 | BoundaryPoints = GetBoundaryPoints(out, mol);
|
---|
663 | }
|
---|
664 | else
|
---|
665 | {
|
---|
666 | *out << Verbose(1) << "Using given boundary points set." << endl;
|
---|
667 | }
|
---|
668 |
|
---|
669 | // 4. fill the boundary point list
|
---|
670 | for (int axis = 0; axis < NDIM; axis++)
|
---|
671 | for (Boundaries::iterator runner = BoundaryPoints[axis].begin(); runner
|
---|
672 | != BoundaryPoints[axis].end(); runner++)
|
---|
673 | {
|
---|
674 | TesselStruct->AddPoint(runner->second.second);
|
---|
675 | }
|
---|
676 |
|
---|
677 | *out << Verbose(2) << "I found " << TesselStruct->PointsOnBoundaryCount
|
---|
678 | << " points on the convex boundary." << endl;
|
---|
679 | // now we have the whole set of edge points in the BoundaryList
|
---|
680 |
|
---|
681 | // listing for debugging
|
---|
682 | // *out << Verbose(1) << "Listing PointsOnBoundary:";
|
---|
683 | // for(PointMap::iterator runner = PointsOnBoundary.begin(); runner != PointsOnBoundary.end(); runner++) {
|
---|
684 | // *out << " " << *runner->second;
|
---|
685 | // }
|
---|
686 | // *out << endl;
|
---|
687 |
|
---|
688 | // 5a. guess starting triangle
|
---|
689 | TesselStruct->GuessStartingTriangle(out);
|
---|
690 |
|
---|
691 | // 5b. go through all lines, that are not yet part of two triangles (only of one so far)
|
---|
692 | TesselStruct->TesselateOnBoundary(out, configuration, mol);
|
---|
693 |
|
---|
694 | *out << Verbose(2) << "I created " << TesselStruct->TrianglesOnBoundaryCount
|
---|
695 | << " triangles with " << TesselStruct->LinesOnBoundaryCount
|
---|
696 | << " lines and " << TesselStruct->PointsOnBoundaryCount << " points."
|
---|
697 | << endl;
|
---|
698 |
|
---|
699 | // 6a. Every triangle forms a pyramid with the center of gravity as its peak, sum up the volumes
|
---|
700 | *out << Verbose(1)
|
---|
701 | << "Calculating the volume of the pyramids formed out of triangles and center of gravity."
|
---|
702 | << endl;
|
---|
703 | for (TriangleMap::iterator runner = TesselStruct->TrianglesOnBoundary.begin(); runner
|
---|
704 | != TesselStruct->TrianglesOnBoundary.end(); runner++)
|
---|
705 | { // go through every triangle, calculate volume of its pyramid with CoG as peak
|
---|
706 | x.CopyVector(&runner->second->endpoints[0]->node->x);
|
---|
707 | x.SubtractVector(&runner->second->endpoints[1]->node->x);
|
---|
708 | y.CopyVector(&runner->second->endpoints[0]->node->x);
|
---|
709 | y.SubtractVector(&runner->second->endpoints[2]->node->x);
|
---|
710 | a = sqrt(runner->second->endpoints[0]->node->x.Distance(
|
---|
711 | &runner->second->endpoints[1]->node->x));
|
---|
712 | b = sqrt(runner->second->endpoints[0]->node->x.Distance(
|
---|
713 | &runner->second->endpoints[2]->node->x));
|
---|
714 | c = sqrt(runner->second->endpoints[2]->node->x.Distance(
|
---|
715 | &runner->second->endpoints[1]->node->x));
|
---|
716 | G = sqrt(((a * a + b * b + c * c) * (a * a + b * b + c * c) - 2 * (a * a
|
---|
717 | * a * a + b * b * b * b + c * c * c * c)) / 16.); // area of tesselated triangle
|
---|
718 | x.MakeNormalVector(&runner->second->endpoints[0]->node->x,
|
---|
719 | &runner->second->endpoints[1]->node->x,
|
---|
720 | &runner->second->endpoints[2]->node->x);
|
---|
721 | x.Scale(runner->second->endpoints[1]->node->x.Projection(&x));
|
---|
722 | h = x.Norm(); // distance of CoG to triangle
|
---|
723 | PyramidVolume = (1. / 3.) * G * h; // this formula holds for _all_ pyramids (independent of n-edge base or (not) centered peak)
|
---|
724 | *out << Verbose(2) << "Area of triangle is " << G << " "
|
---|
725 | << (IsAngstroem ? "angstrom" : "atomiclength") << "^2, height is "
|
---|
726 | << h << " and the volume is " << PyramidVolume << " "
|
---|
727 | << (IsAngstroem ? "angstrom" : "atomiclength") << "^3." << endl;
|
---|
728 | volume += PyramidVolume;
|
---|
729 | }
|
---|
730 | *out << Verbose(0) << "RESULT: The summed volume is " << setprecision(10)
|
---|
731 | << volume << " " << (IsAngstroem ? "angstrom" : "atomiclength") << "^3."
|
---|
732 | << endl;
|
---|
733 |
|
---|
734 | // 7. translate all points back from CoG
|
---|
735 | *out << Verbose(1) << "Translating system back from Center of Gravity."
|
---|
736 | << endl;
|
---|
737 | CenterOfGravity->Scale(-1);
|
---|
738 | Walker = mol->start;
|
---|
739 | while (Walker->next != mol->end)
|
---|
740 | {
|
---|
741 | Walker = Walker->next;
|
---|
742 | Walker->x.Translate(CenterOfGravity);
|
---|
743 | }
|
---|
744 |
|
---|
745 | // 8. Store triangles in tecplot file
|
---|
746 | write_tecplot_file(out, tecplot, TesselStruct, mol, 0);
|
---|
747 |
|
---|
748 | // free reference lists
|
---|
749 | if (BoundaryFreeFlag)
|
---|
750 | delete[] (BoundaryPoints);
|
---|
751 |
|
---|
752 | return volume;
|
---|
753 | }
|
---|
754 | ;
|
---|
755 |
|
---|
756 | /** Creates multiples of the by \a *mol given cluster and suspends them in water with a given final density.
|
---|
757 | * We get cluster volume by VolumeOfConvexEnvelope() and its diameters by GetDiametersOfCluster()
|
---|
758 | * \param *out output stream for debugging
|
---|
759 | * \param *configuration needed for path to store convex envelope file
|
---|
760 | * \param *mol molecule structure representing the cluster
|
---|
761 | * \param ClusterVolume guesstimated cluster volume, if equal 0 we used VolumeOfConvexEnvelope() instead.
|
---|
762 | * \param celldensity desired average density in final cell
|
---|
763 | */
|
---|
764 | void
|
---|
765 | PrepareClustersinWater(ofstream *out, config *configuration, molecule *mol,
|
---|
766 | double ClusterVolume, double celldensity)
|
---|
767 | {
|
---|
768 | // transform to PAS
|
---|
769 | mol->PrincipalAxisSystem(out, true);
|
---|
770 |
|
---|
771 | // some preparations beforehand
|
---|
772 | bool IsAngstroem = configuration->GetIsAngstroem();
|
---|
773 | Boundaries *BoundaryPoints = GetBoundaryPoints(out, mol);
|
---|
774 | double clustervolume;
|
---|
775 | if (ClusterVolume == 0)
|
---|
776 | clustervolume = VolumeOfConvexEnvelope(out, NULL, configuration,
|
---|
777 | BoundaryPoints, mol);
|
---|
778 | else
|
---|
779 | clustervolume = ClusterVolume;
|
---|
780 | double *GreatestDiameter = GetDiametersOfCluster(out, BoundaryPoints, mol,
|
---|
781 | IsAngstroem);
|
---|
782 | Vector BoxLengths;
|
---|
783 | int repetition[NDIM] =
|
---|
784 | { 1, 1, 1 };
|
---|
785 | int TotalNoClusters = 1;
|
---|
786 | for (int i = 0; i < NDIM; i++)
|
---|
787 | TotalNoClusters *= repetition[i];
|
---|
788 |
|
---|
789 | // sum up the atomic masses
|
---|
790 | double totalmass = 0.;
|
---|
791 | atom *Walker = mol->start;
|
---|
792 | while (Walker->next != mol->end)
|
---|
793 | {
|
---|
794 | Walker = Walker->next;
|
---|
795 | totalmass += Walker->type->mass;
|
---|
796 | }
|
---|
797 | *out << Verbose(0) << "RESULT: The summed mass is " << setprecision(10)
|
---|
798 | << totalmass << " atomicmassunit." << endl;
|
---|
799 |
|
---|
800 | *out << Verbose(0) << "RESULT: The average density is " << setprecision(10)
|
---|
801 | << totalmass / clustervolume << " atomicmassunit/"
|
---|
802 | << (IsAngstroem ? "angstrom" : "atomiclength") << "^3." << endl;
|
---|
803 |
|
---|
804 | // solve cubic polynomial
|
---|
805 | *out << Verbose(1) << "Solving equidistant suspension in water problem ..."
|
---|
806 | << endl;
|
---|
807 | double cellvolume;
|
---|
808 | if (IsAngstroem)
|
---|
809 | cellvolume = (TotalNoClusters * totalmass / SOLVENTDENSITY_A - (totalmass
|
---|
810 | / clustervolume)) / (celldensity - 1);
|
---|
811 | else
|
---|
812 | cellvolume = (TotalNoClusters * totalmass / SOLVENTDENSITY_a0 - (totalmass
|
---|
813 | / clustervolume)) / (celldensity - 1);
|
---|
814 | *out << Verbose(1) << "Cellvolume needed for a density of " << celldensity
|
---|
815 | << " g/cm^3 is " << cellvolume << " " << (IsAngstroem ? "angstrom"
|
---|
816 | : "atomiclength") << "^3." << endl;
|
---|
817 |
|
---|
818 | double minimumvolume = TotalNoClusters * (GreatestDiameter[0]
|
---|
819 | * GreatestDiameter[1] * GreatestDiameter[2]);
|
---|
820 | *out << Verbose(1)
|
---|
821 | << "Minimum volume of the convex envelope contained in a rectangular box is "
|
---|
822 | << minimumvolume << " atomicmassunit/" << (IsAngstroem ? "angstrom"
|
---|
823 | : "atomiclength") << "^3." << endl;
|
---|
824 | if (minimumvolume > cellvolume)
|
---|
825 | {
|
---|
826 | cerr << Verbose(0)
|
---|
827 | << "ERROR: the containing box already has a greater volume than the envisaged cell volume!"
|
---|
828 | << endl;
|
---|
829 | cout << Verbose(0)
|
---|
830 | << "Setting Box dimensions to minimum possible, the greatest diameters."
|
---|
831 | << endl;
|
---|
832 | for (int i = 0; i < NDIM; i++)
|
---|
833 | BoxLengths.x[i] = GreatestDiameter[i];
|
---|
834 | mol->CenterEdge(out, &BoxLengths);
|
---|
835 | }
|
---|
836 | else
|
---|
837 | {
|
---|
838 | BoxLengths.x[0] = (repetition[0] * GreatestDiameter[0] + repetition[1]
|
---|
839 | * GreatestDiameter[1] + repetition[2] * GreatestDiameter[2]);
|
---|
840 | BoxLengths.x[1] = (repetition[0] * repetition[1] * GreatestDiameter[0]
|
---|
841 | * GreatestDiameter[1] + repetition[0] * repetition[2]
|
---|
842 | * GreatestDiameter[0] * GreatestDiameter[2] + repetition[1]
|
---|
843 | * repetition[2] * GreatestDiameter[1] * GreatestDiameter[2]);
|
---|
844 | BoxLengths.x[2] = minimumvolume - cellvolume;
|
---|
845 | double x0 = 0., x1 = 0., x2 = 0.;
|
---|
846 | if (gsl_poly_solve_cubic(BoxLengths.x[0], BoxLengths.x[1],
|
---|
847 | BoxLengths.x[2], &x0, &x1, &x2) == 1) // either 1 or 3 on return
|
---|
848 | *out << Verbose(0) << "RESULT: The resulting spacing is: " << x0
|
---|
849 | << " ." << endl;
|
---|
850 | else
|
---|
851 | {
|
---|
852 | *out << Verbose(0) << "RESULT: The resulting spacings are: " << x0
|
---|
853 | << " and " << x1 << " and " << x2 << " ." << endl;
|
---|
854 | x0 = x2; // sorted in ascending order
|
---|
855 | }
|
---|
856 |
|
---|
857 | cellvolume = 1;
|
---|
858 | for (int i = 0; i < NDIM; i++)
|
---|
859 | {
|
---|
860 | BoxLengths.x[i] = repetition[i] * (x0 + GreatestDiameter[i]);
|
---|
861 | cellvolume *= BoxLengths.x[i];
|
---|
862 | }
|
---|
863 |
|
---|
864 | // set new box dimensions
|
---|
865 | *out << Verbose(0) << "Translating to box with these boundaries." << endl;
|
---|
866 | mol->CenterInBox((ofstream *) &cout, &BoxLengths);
|
---|
867 | }
|
---|
868 | // update Box of atoms by boundary
|
---|
869 | mol->SetBoxDimension(&BoxLengths);
|
---|
870 | *out << Verbose(0) << "RESULT: The resulting cell dimensions are: "
|
---|
871 | << BoxLengths.x[0] << " and " << BoxLengths.x[1] << " and "
|
---|
872 | << BoxLengths.x[2] << " with total volume of " << cellvolume << " "
|
---|
873 | << (IsAngstroem ? "angstrom" : "atomiclength") << "^3." << endl;
|
---|
874 | }
|
---|
875 | ;
|
---|
876 |
|
---|
877 | // =========================================================== class TESSELATION ===========================================
|
---|
878 |
|
---|
879 | /** Constructor of class Tesselation.
|
---|
880 | */
|
---|
881 | Tesselation::Tesselation()
|
---|
882 | {
|
---|
883 | PointsOnBoundaryCount = 0;
|
---|
884 | LinesOnBoundaryCount = 0;
|
---|
885 | TrianglesOnBoundaryCount = 0;
|
---|
886 | TriangleFilesWritten = 0;
|
---|
887 | }
|
---|
888 | ;
|
---|
889 |
|
---|
890 | /** Constructor of class Tesselation.
|
---|
891 | * We have to free all points, lines and triangles.
|
---|
892 | */
|
---|
893 | Tesselation::~Tesselation()
|
---|
894 | {
|
---|
895 | cout << Verbose(1) << "Free'ing TesselStruct ... " << endl;
|
---|
896 | for (TriangleMap::iterator runner = TrianglesOnBoundary.begin(); runner
|
---|
897 | != TrianglesOnBoundary.end(); runner++)
|
---|
898 | {
|
---|
899 | delete (runner->second);
|
---|
900 | }
|
---|
901 | }
|
---|
902 | ;
|
---|
903 |
|
---|
904 | /** Gueses first starting triangle of the convex envelope.
|
---|
905 | * We guess the starting triangle by taking the smallest distance between two points and looking for a fitting third.
|
---|
906 | * \param *out output stream for debugging
|
---|
907 | * \param PointsOnBoundary set of boundary points defining the convex envelope of the cluster
|
---|
908 | */
|
---|
909 | void
|
---|
910 | Tesselation::GuessStartingTriangle(ofstream *out)
|
---|
911 | {
|
---|
912 | // 4b. create a starting triangle
|
---|
913 | // 4b1. create all distances
|
---|
914 | DistanceMultiMap DistanceMMap;
|
---|
915 | double distance, tmp;
|
---|
916 | Vector PlaneVector, TrialVector;
|
---|
917 | PointMap::iterator A, B, C; // three nodes of the first triangle
|
---|
918 | A = PointsOnBoundary.begin(); // the first may be chosen arbitrarily
|
---|
919 |
|
---|
920 | // with A chosen, take each pair B,C and sort
|
---|
921 | if (A != PointsOnBoundary.end())
|
---|
922 | {
|
---|
923 | B = A;
|
---|
924 | B++;
|
---|
925 | for (; B != PointsOnBoundary.end(); B++)
|
---|
926 | {
|
---|
927 | C = B;
|
---|
928 | C++;
|
---|
929 | for (; C != PointsOnBoundary.end(); C++)
|
---|
930 | {
|
---|
931 | tmp = A->second->node->x.Distance(&B->second->node->x);
|
---|
932 | distance = tmp * tmp;
|
---|
933 | tmp = A->second->node->x.Distance(&C->second->node->x);
|
---|
934 | distance += tmp * tmp;
|
---|
935 | tmp = B->second->node->x.Distance(&C->second->node->x);
|
---|
936 | distance += tmp * tmp;
|
---|
937 | DistanceMMap.insert(DistanceMultiMapPair(distance, pair<
|
---|
938 | PointMap::iterator, PointMap::iterator> (B, C)));
|
---|
939 | }
|
---|
940 | }
|
---|
941 | }
|
---|
942 | // // listing distances
|
---|
943 | // *out << Verbose(1) << "Listing DistanceMMap:";
|
---|
944 | // for(DistanceMultiMap::iterator runner = DistanceMMap.begin(); runner != DistanceMMap.end(); runner++) {
|
---|
945 | // *out << " " << runner->first << "(" << *runner->second.first->second << ", " << *runner->second.second->second << ")";
|
---|
946 | // }
|
---|
947 | // *out << endl;
|
---|
948 | // 4b2. pick three baselines forming a triangle
|
---|
949 | // 1. we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
|
---|
950 | DistanceMultiMap::iterator baseline = DistanceMMap.begin();
|
---|
951 | for (; baseline != DistanceMMap.end(); baseline++)
|
---|
952 | {
|
---|
953 | // we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
|
---|
954 | // 2. next, we have to check whether all points reside on only one side of the triangle
|
---|
955 | // 3. construct plane vector
|
---|
956 | PlaneVector.MakeNormalVector(&A->second->node->x,
|
---|
957 | &baseline->second.first->second->node->x,
|
---|
958 | &baseline->second.second->second->node->x);
|
---|
959 | *out << Verbose(2) << "Plane vector of candidate triangle is ";
|
---|
960 | PlaneVector.Output(out);
|
---|
961 | *out << endl;
|
---|
962 | // 4. loop over all points
|
---|
963 | double sign = 0.;
|
---|
964 | PointMap::iterator checker = PointsOnBoundary.begin();
|
---|
965 | for (; checker != PointsOnBoundary.end(); checker++)
|
---|
966 | {
|
---|
967 | // (neglecting A,B,C)
|
---|
968 | if ((checker == A) || (checker == baseline->second.first) || (checker
|
---|
969 | == baseline->second.second))
|
---|
970 | continue;
|
---|
971 | // 4a. project onto plane vector
|
---|
972 | TrialVector.CopyVector(&checker->second->node->x);
|
---|
973 | TrialVector.SubtractVector(&A->second->node->x);
|
---|
974 | distance = TrialVector.Projection(&PlaneVector);
|
---|
975 | if (fabs(distance) < 1e-4) // we need to have a small epsilon around 0 which is still ok
|
---|
976 | continue;
|
---|
977 | *out << Verbose(3) << "Projection of " << checker->second->node->Name
|
---|
978 | << " yields distance of " << distance << "." << endl;
|
---|
979 | tmp = distance / fabs(distance);
|
---|
980 | // 4b. Any have different sign to than before? (i.e. would lie outside convex hull with this starting triangle)
|
---|
981 | if ((sign != 0) && (tmp != sign))
|
---|
982 | {
|
---|
983 | // 4c. If so, break 4. loop and continue with next candidate in 1. loop
|
---|
984 | *out << Verbose(2) << "Current candidates: "
|
---|
985 | << A->second->node->Name << ","
|
---|
986 | << baseline->second.first->second->node->Name << ","
|
---|
987 | << baseline->second.second->second->node->Name << " leave "
|
---|
988 | << checker->second->node->Name << " outside the convex hull."
|
---|
989 | << endl;
|
---|
990 | break;
|
---|
991 | }
|
---|
992 | else
|
---|
993 | { // note the sign for later
|
---|
994 | *out << Verbose(2) << "Current candidates: "
|
---|
995 | << A->second->node->Name << ","
|
---|
996 | << baseline->second.first->second->node->Name << ","
|
---|
997 | << baseline->second.second->second->node->Name << " leave "
|
---|
998 | << checker->second->node->Name << " inside the convex hull."
|
---|
999 | << endl;
|
---|
1000 | sign = tmp;
|
---|
1001 | }
|
---|
1002 | // 4d. Check whether the point is inside the triangle (check distance to each node
|
---|
1003 | tmp = checker->second->node->x.Distance(&A->second->node->x);
|
---|
1004 | int innerpoint = 0;
|
---|
1005 | if ((tmp < A->second->node->x.Distance(
|
---|
1006 | &baseline->second.first->second->node->x)) && (tmp
|
---|
1007 | < A->second->node->x.Distance(
|
---|
1008 | &baseline->second.second->second->node->x)))
|
---|
1009 | innerpoint++;
|
---|
1010 | tmp = checker->second->node->x.Distance(
|
---|
1011 | &baseline->second.first->second->node->x);
|
---|
1012 | if ((tmp < baseline->second.first->second->node->x.Distance(
|
---|
1013 | &A->second->node->x)) && (tmp
|
---|
1014 | < baseline->second.first->second->node->x.Distance(
|
---|
1015 | &baseline->second.second->second->node->x)))
|
---|
1016 | innerpoint++;
|
---|
1017 | tmp = checker->second->node->x.Distance(
|
---|
1018 | &baseline->second.second->second->node->x);
|
---|
1019 | if ((tmp < baseline->second.second->second->node->x.Distance(
|
---|
1020 | &baseline->second.first->second->node->x)) && (tmp
|
---|
1021 | < baseline->second.second->second->node->x.Distance(
|
---|
1022 | &A->second->node->x)))
|
---|
1023 | innerpoint++;
|
---|
1024 | // 4e. If so, break 4. loop and continue with next candidate in 1. loop
|
---|
1025 | if (innerpoint == 3)
|
---|
1026 | break;
|
---|
1027 | }
|
---|
1028 | // 5. come this far, all on same side? Then break 1. loop and construct triangle
|
---|
1029 | if (checker == PointsOnBoundary.end())
|
---|
1030 | {
|
---|
1031 | *out << "Looks like we have a candidate!" << endl;
|
---|
1032 | break;
|
---|
1033 | }
|
---|
1034 | }
|
---|
1035 | if (baseline != DistanceMMap.end())
|
---|
1036 | {
|
---|
1037 | BPS[0] = baseline->second.first->second;
|
---|
1038 | BPS[1] = baseline->second.second->second;
|
---|
1039 | BLS[0] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
|
---|
1040 | BPS[0] = A->second;
|
---|
1041 | BPS[1] = baseline->second.second->second;
|
---|
1042 | BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
|
---|
1043 | BPS[0] = baseline->second.first->second;
|
---|
1044 | BPS[1] = A->second;
|
---|
1045 | BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
|
---|
1046 |
|
---|
1047 | // 4b3. insert created triangle
|
---|
1048 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
|
---|
1049 | TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
|
---|
1050 | TrianglesOnBoundaryCount++;
|
---|
1051 | for (int i = 0; i < NDIM; i++)
|
---|
1052 | {
|
---|
1053 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BTS->lines[i]));
|
---|
1054 | LinesOnBoundaryCount++;
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | *out << Verbose(1) << "Starting triangle is " << *BTS << "." << endl;
|
---|
1058 | }
|
---|
1059 | else
|
---|
1060 | {
|
---|
1061 | *out << Verbose(1) << "No starting triangle found." << endl;
|
---|
1062 | exit(255);
|
---|
1063 | }
|
---|
1064 | }
|
---|
1065 | ;
|
---|
1066 |
|
---|
1067 | /** Tesselates the convex envelope of a cluster from a single starting triangle.
|
---|
1068 | * The starting triangle is made out of three baselines. Each line in the final tesselated cluster may belong to at most
|
---|
1069 | * 2 triangles. Hence, we go through all current lines:
|
---|
1070 | * -# if the lines contains to only one triangle
|
---|
1071 | * -# We search all points in the boundary
|
---|
1072 | * -# if the triangle with the baseline and the current point has the smallest of angles (comparison between normal vectors
|
---|
1073 | * -# if the triangle is in forward direction of the baseline (at most 90 degrees angle between vector orthogonal to
|
---|
1074 | * baseline in triangle plane pointing out of the triangle and normal vector of new triangle)
|
---|
1075 | * -# then we have a new triangle, whose baselines we again add (or increase their TriangleCount)
|
---|
1076 | * \param *out output stream for debugging
|
---|
1077 | * \param *configuration for IsAngstroem
|
---|
1078 | * \param *mol the cluster as a molecule structure
|
---|
1079 | */
|
---|
1080 | void
|
---|
1081 | Tesselation::TesselateOnBoundary(ofstream *out, config *configuration,
|
---|
1082 | molecule *mol)
|
---|
1083 | {
|
---|
1084 | bool flag;
|
---|
1085 | PointMap::iterator winner;
|
---|
1086 | class BoundaryPointSet *peak = NULL;
|
---|
1087 | double SmallestAngle, TempAngle;
|
---|
1088 | Vector NormalVector, VirtualNormalVector, CenterVector, TempVector,
|
---|
1089 | PropagationVector;
|
---|
1090 | LineMap::iterator LineChecker[2];
|
---|
1091 | do
|
---|
1092 | {
|
---|
1093 | flag = false;
|
---|
1094 | for (LineMap::iterator baseline = LinesOnBoundary.begin(); baseline
|
---|
1095 | != LinesOnBoundary.end(); baseline++)
|
---|
1096 | if (baseline->second->TrianglesCount == 1)
|
---|
1097 | {
|
---|
1098 | *out << Verbose(2) << "Current baseline is between "
|
---|
1099 | << *(baseline->second) << "." << endl;
|
---|
1100 | // 5a. go through each boundary point if not _both_ edges between either endpoint of the current line and this point exist (and belong to 2 triangles)
|
---|
1101 | SmallestAngle = M_PI;
|
---|
1102 | BTS = baseline->second->triangles.begin()->second; // there is only one triangle so far
|
---|
1103 | // get peak point with respect to this base line's only triangle
|
---|
1104 | for (int i = 0; i < 3; i++)
|
---|
1105 | if ((BTS->endpoints[i] != baseline->second->endpoints[0])
|
---|
1106 | && (BTS->endpoints[i] != baseline->second->endpoints[1]))
|
---|
1107 | peak = BTS->endpoints[i];
|
---|
1108 | *out << Verbose(3) << " and has peak " << *peak << "." << endl;
|
---|
1109 | // normal vector of triangle
|
---|
1110 | BTS->GetNormalVector(NormalVector);
|
---|
1111 | *out << Verbose(4) << "NormalVector of base triangle is ";
|
---|
1112 | NormalVector.Output(out);
|
---|
1113 | *out << endl;
|
---|
1114 | // offset to center of triangle
|
---|
1115 | CenterVector.Zero();
|
---|
1116 | for (int i = 0; i < 3; i++)
|
---|
1117 | CenterVector.AddVector(&BTS->endpoints[i]->node->x);
|
---|
1118 | CenterVector.Scale(1. / 3.);
|
---|
1119 | *out << Verbose(4) << "CenterVector of base triangle is ";
|
---|
1120 | CenterVector.Output(out);
|
---|
1121 | *out << endl;
|
---|
1122 | // vector in propagation direction (out of triangle)
|
---|
1123 | // project center vector onto triangle plane (points from intersection plane-NormalVector to plane-CenterVector intersection)
|
---|
1124 | TempVector.CopyVector(&baseline->second->endpoints[0]->node->x);
|
---|
1125 | TempVector.SubtractVector(&baseline->second->endpoints[1]->node->x);
|
---|
1126 | PropagationVector.MakeNormalVector(&TempVector, &NormalVector);
|
---|
1127 | TempVector.CopyVector(&CenterVector);
|
---|
1128 | TempVector.SubtractVector(&baseline->second->endpoints[0]->node->x); // TempVector is vector on triangle plane pointing from one baseline egde towards center!
|
---|
1129 | //*out << Verbose(2) << "Projection of propagation onto temp: " << PropagationVector.Projection(&TempVector) << "." << endl;
|
---|
1130 | if (PropagationVector.Projection(&TempVector) > 0) // make sure normal propagation vector points outward from baseline
|
---|
1131 | PropagationVector.Scale(-1.);
|
---|
1132 | *out << Verbose(4) << "PropagationVector of base triangle is ";
|
---|
1133 | PropagationVector.Output(out);
|
---|
1134 | *out << endl;
|
---|
1135 | winner = PointsOnBoundary.end();
|
---|
1136 | for (PointMap::iterator target = PointsOnBoundary.begin(); target
|
---|
1137 | != PointsOnBoundary.end(); target++)
|
---|
1138 | if ((target->second != baseline->second->endpoints[0])
|
---|
1139 | && (target->second != baseline->second->endpoints[1]))
|
---|
1140 | { // don't take the same endpoints
|
---|
1141 | *out << Verbose(3) << "Target point is " << *(target->second)
|
---|
1142 | << ":";
|
---|
1143 | bool continueflag = true;
|
---|
1144 |
|
---|
1145 | VirtualNormalVector.CopyVector(
|
---|
1146 | &baseline->second->endpoints[0]->node->x);
|
---|
1147 | VirtualNormalVector.AddVector(
|
---|
1148 | &baseline->second->endpoints[0]->node->x);
|
---|
1149 | VirtualNormalVector.Scale(-1. / 2.); // points now to center of base line
|
---|
1150 | VirtualNormalVector.AddVector(&target->second->node->x); // points from center of base line to target
|
---|
1151 | TempAngle = VirtualNormalVector.Angle(&PropagationVector);
|
---|
1152 | continueflag = continueflag && (TempAngle < (M_PI/2.)); // no bends bigger than Pi/2 (90 degrees)
|
---|
1153 | if (!continueflag)
|
---|
1154 | {
|
---|
1155 | *out << Verbose(4)
|
---|
1156 | << "Angle between propagation direction and base line to "
|
---|
1157 | << *(target->second) << " is " << TempAngle
|
---|
1158 | << ", bad direction!" << endl;
|
---|
1159 | continue;
|
---|
1160 | }
|
---|
1161 | else
|
---|
1162 | *out << Verbose(4)
|
---|
1163 | << "Angle between propagation direction and base line to "
|
---|
1164 | << *(target->second) << " is " << TempAngle
|
---|
1165 | << ", good direction!" << endl;
|
---|
1166 | LineChecker[0] = baseline->second->endpoints[0]->lines.find(
|
---|
1167 | target->first);
|
---|
1168 | LineChecker[1] = baseline->second->endpoints[1]->lines.find(
|
---|
1169 | target->first);
|
---|
1170 | // if (LineChecker[0] != baseline->second->endpoints[0]->lines.end())
|
---|
1171 | // *out << Verbose(4) << *(baseline->second->endpoints[0]) << " has line " << *(LineChecker[0]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[0]->second->TrianglesCount << " triangles." << endl;
|
---|
1172 | // else
|
---|
1173 | // *out << Verbose(4) << *(baseline->second->endpoints[0]) << " has no line to " << *(target->second) << " as endpoint." << endl;
|
---|
1174 | // if (LineChecker[1] != baseline->second->endpoints[1]->lines.end())
|
---|
1175 | // *out << Verbose(4) << *(baseline->second->endpoints[1]) << " has line " << *(LineChecker[1]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[1]->second->TrianglesCount << " triangles." << endl;
|
---|
1176 | // else
|
---|
1177 | // *out << Verbose(4) << *(baseline->second->endpoints[1]) << " has no line to " << *(target->second) << " as endpoint." << endl;
|
---|
1178 | // check first endpoint (if any connecting line goes to target or at least not more than 1)
|
---|
1179 | continueflag = continueflag && (((LineChecker[0]
|
---|
1180 | == baseline->second->endpoints[0]->lines.end())
|
---|
1181 | || (LineChecker[0]->second->TrianglesCount == 1)));
|
---|
1182 | if (!continueflag)
|
---|
1183 | {
|
---|
1184 | *out << Verbose(4) << *(baseline->second->endpoints[0])
|
---|
1185 | << " has line " << *(LineChecker[0]->second)
|
---|
1186 | << " to " << *(target->second)
|
---|
1187 | << " as endpoint with "
|
---|
1188 | << LineChecker[0]->second->TrianglesCount
|
---|
1189 | << " triangles." << endl;
|
---|
1190 | continue;
|
---|
1191 | }
|
---|
1192 | // check second endpoint (if any connecting line goes to target or at least not more than 1)
|
---|
1193 | continueflag = continueflag && (((LineChecker[1]
|
---|
1194 | == baseline->second->endpoints[1]->lines.end())
|
---|
1195 | || (LineChecker[1]->second->TrianglesCount == 1)));
|
---|
1196 | if (!continueflag)
|
---|
1197 | {
|
---|
1198 | *out << Verbose(4) << *(baseline->second->endpoints[1])
|
---|
1199 | << " has line " << *(LineChecker[1]->second)
|
---|
1200 | << " to " << *(target->second)
|
---|
1201 | << " as endpoint with "
|
---|
1202 | << LineChecker[1]->second->TrianglesCount
|
---|
1203 | << " triangles." << endl;
|
---|
1204 | continue;
|
---|
1205 | }
|
---|
1206 | // check whether the envisaged triangle does not already exist (if both lines exist and have same endpoint)
|
---|
1207 | continueflag = continueflag && (!(((LineChecker[0]
|
---|
1208 | != baseline->second->endpoints[0]->lines.end())
|
---|
1209 | && (LineChecker[1]
|
---|
1210 | != baseline->second->endpoints[1]->lines.end())
|
---|
1211 | && (GetCommonEndpoint(LineChecker[0]->second,
|
---|
1212 | LineChecker[1]->second) == peak))));
|
---|
1213 | if (!continueflag)
|
---|
1214 | {
|
---|
1215 | *out << Verbose(4) << "Current target is peak!" << endl;
|
---|
1216 | continue;
|
---|
1217 | }
|
---|
1218 | // in case NOT both were found
|
---|
1219 | if (continueflag)
|
---|
1220 | { // create virtually this triangle, get its normal vector, calculate angle
|
---|
1221 | flag = true;
|
---|
1222 | VirtualNormalVector.MakeNormalVector(
|
---|
1223 | &baseline->second->endpoints[0]->node->x,
|
---|
1224 | &baseline->second->endpoints[1]->node->x,
|
---|
1225 | &target->second->node->x);
|
---|
1226 | // make it always point inward
|
---|
1227 | if (baseline->second->endpoints[0]->node->x.Projection(
|
---|
1228 | &VirtualNormalVector) > 0)
|
---|
1229 | VirtualNormalVector.Scale(-1.);
|
---|
1230 | // calculate angle
|
---|
1231 | TempAngle = NormalVector.Angle(&VirtualNormalVector);
|
---|
1232 | *out << Verbose(4) << "NormalVector is ";
|
---|
1233 | VirtualNormalVector.Output(out);
|
---|
1234 | *out << " and the angle is " << TempAngle << "." << endl;
|
---|
1235 | if (SmallestAngle > TempAngle)
|
---|
1236 | { // set to new possible winner
|
---|
1237 | SmallestAngle = TempAngle;
|
---|
1238 | winner = target;
|
---|
1239 | }
|
---|
1240 | }
|
---|
1241 | }
|
---|
1242 | // 5b. The point of the above whose triangle has the greatest angle with the triangle the current line belongs to (it only belongs to one, remember!): New triangle
|
---|
1243 | if (winner != PointsOnBoundary.end())
|
---|
1244 | {
|
---|
1245 | *out << Verbose(2) << "Winning target point is "
|
---|
1246 | << *(winner->second) << " with angle " << SmallestAngle
|
---|
1247 | << "." << endl;
|
---|
1248 | // create the lins of not yet present
|
---|
1249 | BLS[0] = baseline->second;
|
---|
1250 | // 5c. add lines to the line set if those were new (not yet part of a triangle), delete lines that belong to two triangles)
|
---|
1251 | LineChecker[0] = baseline->second->endpoints[0]->lines.find(
|
---|
1252 | winner->first);
|
---|
1253 | LineChecker[1] = baseline->second->endpoints[1]->lines.find(
|
---|
1254 | winner->first);
|
---|
1255 | if (LineChecker[0]
|
---|
1256 | == baseline->second->endpoints[0]->lines.end())
|
---|
1257 | { // create
|
---|
1258 | BPS[0] = baseline->second->endpoints[0];
|
---|
1259 | BPS[1] = winner->second;
|
---|
1260 | BLS[1] = new class BoundaryLineSet(BPS,
|
---|
1261 | LinesOnBoundaryCount);
|
---|
1262 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount,
|
---|
1263 | BLS[1]));
|
---|
1264 | LinesOnBoundaryCount++;
|
---|
1265 | }
|
---|
1266 | else
|
---|
1267 | BLS[1] = LineChecker[0]->second;
|
---|
1268 | if (LineChecker[1]
|
---|
1269 | == baseline->second->endpoints[1]->lines.end())
|
---|
1270 | { // create
|
---|
1271 | BPS[0] = baseline->second->endpoints[1];
|
---|
1272 | BPS[1] = winner->second;
|
---|
1273 | BLS[2] = new class BoundaryLineSet(BPS,
|
---|
1274 | LinesOnBoundaryCount);
|
---|
1275 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount,
|
---|
1276 | BLS[2]));
|
---|
1277 | LinesOnBoundaryCount++;
|
---|
1278 | }
|
---|
1279 | else
|
---|
1280 | BLS[2] = LineChecker[1]->second;
|
---|
1281 | BTS = new class BoundaryTriangleSet(BLS,
|
---|
1282 | TrianglesOnBoundaryCount);
|
---|
1283 | TrianglesOnBoundary.insert(TrianglePair(
|
---|
1284 | TrianglesOnBoundaryCount, BTS));
|
---|
1285 | TrianglesOnBoundaryCount++;
|
---|
1286 | }
|
---|
1287 | else
|
---|
1288 | {
|
---|
1289 | *out << Verbose(1)
|
---|
1290 | << "I could not determine a winner for this baseline "
|
---|
1291 | << *(baseline->second) << "." << endl;
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | // 5d. If the set of lines is not yet empty, go to 5. and continue
|
---|
1295 | }
|
---|
1296 | else
|
---|
1297 | *out << Verbose(2) << "Baseline candidate " << *(baseline->second)
|
---|
1298 | << " has a triangle count of "
|
---|
1299 | << baseline->second->TrianglesCount << "." << endl;
|
---|
1300 | }
|
---|
1301 | while (flag);
|
---|
1302 |
|
---|
1303 | }
|
---|
1304 | ;
|
---|
1305 |
|
---|
1306 | /** Adds an atom to the tesselation::PointsOnBoundary list.
|
---|
1307 | * \param *Walker atom to add
|
---|
1308 | */
|
---|
1309 | void
|
---|
1310 | Tesselation::AddPoint(atom *Walker)
|
---|
1311 | {
|
---|
1312 | PointTestPair InsertUnique;
|
---|
1313 | BPS[0] = new class BoundaryPointSet(Walker);
|
---|
1314 | InsertUnique = PointsOnBoundary.insert(PointPair(Walker->nr, BPS[0]));
|
---|
1315 | if (InsertUnique.second) // if new point was not present before, increase counter
|
---|
1316 | PointsOnBoundaryCount++;
|
---|
1317 | }
|
---|
1318 | ;
|
---|
1319 |
|
---|
1320 | /** Adds point to Tesselation::PointsOnBoundary if not yet present.
|
---|
1321 | * Tesselation::TPS is set to either this new BoundaryPointSet or to the existing one of not unique.
|
---|
1322 | * @param Candidate point to add
|
---|
1323 | * @param n index for this point in Tesselation::TPS array
|
---|
1324 | */
|
---|
1325 | void
|
---|
1326 | Tesselation::AddTrianglePoint(atom* Candidate, int n)
|
---|
1327 | {
|
---|
1328 | PointTestPair InsertUnique;
|
---|
1329 | TPS[n] = new class BoundaryPointSet(Candidate);
|
---|
1330 | InsertUnique = PointsOnBoundary.insert(PointPair(Candidate->nr, TPS[n]));
|
---|
1331 | if (InsertUnique.second) // if new point was not present before, increase counter
|
---|
1332 | {
|
---|
1333 | PointsOnBoundaryCount++;
|
---|
1334 | }
|
---|
1335 | else
|
---|
1336 | {
|
---|
1337 | delete TPS[n];
|
---|
1338 | cout << Verbose(2) << "Atom " << *((InsertUnique.first)->second->node)
|
---|
1339 | << " gibt's schon in der PointMap." << endl;
|
---|
1340 | TPS[n] = (InsertUnique.first)->second;
|
---|
1341 | }
|
---|
1342 | }
|
---|
1343 | ;
|
---|
1344 |
|
---|
1345 | /** Function tries to add line from current Points in BPS to BoundaryLineSet.
|
---|
1346 | * If succesful it raises the line count and inserts the new line into the BLS,
|
---|
1347 | * if unsuccesful, it writes the line which had been present into the BLS, deleting the new constructed one.
|
---|
1348 | * @param *a first endpoint
|
---|
1349 | * @param *b second endpoint
|
---|
1350 | * @param n index of Tesselation::BLS giving the line with both endpoints
|
---|
1351 | */
|
---|
1352 | void
|
---|
1353 | Tesselation::AddTriangleLine(class BoundaryPointSet *a,
|
---|
1354 | class BoundaryPointSet *b, int n)
|
---|
1355 | {
|
---|
1356 | LineMap::iterator LineWalker;
|
---|
1357 | //cout << "Manually checking endpoints for line." << endl;
|
---|
1358 | if ((a->lines.find(b->node->nr))->first == b->node->nr)
|
---|
1359 | //If a line is there, how do I recognize that beyond a shadow of a doubt?
|
---|
1360 | {
|
---|
1361 | //cout << Verbose(2) << "Line exists already, retrieving it from LinesOnBoundarySet" << endl;
|
---|
1362 |
|
---|
1363 | LineWalker = LinesOnBoundary.end();
|
---|
1364 | LineWalker--;
|
---|
1365 |
|
---|
1366 | while (LineWalker->second->endpoints[0]->node->nr != min(a->node->nr,
|
---|
1367 | b->node->nr) or LineWalker->second->endpoints[1]->node->nr != max(
|
---|
1368 | a->node->nr, b->node->nr))
|
---|
1369 | {
|
---|
1370 | //cout << Verbose(1) << "Looking for line which already exists"<< endl;
|
---|
1371 | LineWalker--;
|
---|
1372 | }
|
---|
1373 | BPS[0] = LineWalker->second->endpoints[0];
|
---|
1374 | BPS[1] = LineWalker->second->endpoints[1];
|
---|
1375 | BLS[n] = LineWalker->second;
|
---|
1376 |
|
---|
1377 | }
|
---|
1378 | else
|
---|
1379 | {
|
---|
1380 | cout << Verbose(2)
|
---|
1381 | << "Adding line which has not been used before between "
|
---|
1382 | << *(a->node) << " and " << *(b->node) << "." << endl;
|
---|
1383 | BPS[0] = a;
|
---|
1384 | BPS[1] = b;
|
---|
1385 | BLS[n] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
|
---|
1386 |
|
---|
1387 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[n]));
|
---|
1388 | LinesOnBoundaryCount++;
|
---|
1389 |
|
---|
1390 | }
|
---|
1391 | }
|
---|
1392 | ;
|
---|
1393 |
|
---|
1394 | /** Function tries to add Triangle just created to Triangle and remarks if already existent (Failure of algorithm).
|
---|
1395 | * Furthermore it adds the triangle to all of its lines, in order to recognize those which are saturated later.
|
---|
1396 | */
|
---|
1397 | void
|
---|
1398 | Tesselation::AddTriangleToLines()
|
---|
1399 | {
|
---|
1400 |
|
---|
1401 | cout << Verbose(1) << "Adding triangle to its lines" << endl;
|
---|
1402 | int i = 0;
|
---|
1403 | TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
|
---|
1404 | TrianglesOnBoundaryCount++;
|
---|
1405 |
|
---|
1406 | /*
|
---|
1407 | * this is apparently done when constructing triangle
|
---|
1408 |
|
---|
1409 | for (i=0; i<3; i++)
|
---|
1410 | {
|
---|
1411 | BLS[i]->AddTriangle(BTS);
|
---|
1412 | }
|
---|
1413 | */
|
---|
1414 | }
|
---|
1415 | ;
|
---|
1416 |
|
---|
1417 | /**
|
---|
1418 | * Function returns center of sphere with RADIUS, which rests on points a, b, c
|
---|
1419 | * @param Center this vector will be used for return
|
---|
1420 | * @param a vector first point of triangle
|
---|
1421 | * @param b vector second point of triangle
|
---|
1422 | * @param c vector third point of triangle
|
---|
1423 | * @param Direction vector indicates up/down
|
---|
1424 | * @param Halfplaneindicator double indicates whether Direction is up or down
|
---|
1425 | * @param alpha double angle at a
|
---|
1426 | * @param beta double, angle at b
|
---|
1427 | * @param gamma, double, angle at c
|
---|
1428 | * @param Radius, double
|
---|
1429 | * @param Umkreisradius double radius of circumscribing circle
|
---|
1430 | */
|
---|
1431 |
|
---|
1432 | void Get_center_of_sphere(Vector* Center, Vector a, Vector b, Vector c, Vector* Direction, double HalfplaneIndicator,
|
---|
1433 | double alpha, double beta, double gamma, double RADIUS, double Umkreisradius)
|
---|
1434 | {
|
---|
1435 | Vector TempNormal, helper;
|
---|
1436 | double Restradius;
|
---|
1437 |
|
---|
1438 | *Center = a * sin(2.*alpha) + b * sin(2.*beta) + c * sin(2.*gamma) ;
|
---|
1439 | Center->Scale(1/(sin(2*alpha) + sin(2*beta) + sin(2*gamma)));
|
---|
1440 | // Here we calculated center of circumscribing circle, using barycentric coordinates
|
---|
1441 |
|
---|
1442 | TempNormal.CopyVector(&a);
|
---|
1443 | TempNormal.SubtractVector(&b);
|
---|
1444 | helper.CopyVector(&a);
|
---|
1445 | helper.SubtractVector(&c);
|
---|
1446 | TempNormal.VectorProduct(&helper);
|
---|
1447 | if (TempNormal.ScalarProduct(Direction)<0 && HalfplaneIndicator >0 || TempNormal.ScalarProduct(Direction)>0 && HalfplaneIndicator<0)
|
---|
1448 | {
|
---|
1449 | TempNormal.Scale(-1);
|
---|
1450 | }
|
---|
1451 |
|
---|
1452 | TempNormal.Normalize();
|
---|
1453 | Restradius = sqrt(RADIUS*RADIUS - Umkreisradius*Umkreisradius);
|
---|
1454 | TempNormal.Scale(Restradius);
|
---|
1455 |
|
---|
1456 | Center->AddVector(&TempNormal);
|
---|
1457 | }
|
---|
1458 | ;
|
---|
1459 |
|
---|
1460 |
|
---|
1461 | /** This recursive function finds a third point, to form a triangle with two given ones.
|
---|
1462 | * Two atoms are fixed, a candidate is supplied, additionally two vectors for direction distinction, a Storage area to \
|
---|
1463 | * supply results to the calling function, the radius of the sphere which the triangle shall support and the molecule \
|
---|
1464 | * upon which we operate.
|
---|
1465 | * If the candidate is more fitting to support the sphere than the already stored atom is, then we write its general \
|
---|
1466 | * direction and angle into Storage.
|
---|
1467 | * We the determine the recursive level we have reached and if this is not on the threshold yet, call this function again, \
|
---|
1468 | * with all neighbours of the candidate.
|
---|
1469 | * @param a first point
|
---|
1470 | * @param b second point
|
---|
1471 | * @param Candidate base point along whose bonds to start looking from
|
---|
1472 | * @param Parent point to avoid during search as its wrong direction
|
---|
1473 | * @param RecursionLevel contains current recursion depth
|
---|
1474 | * @param Chord baseline vector of first and second point
|
---|
1475 | * @param direction1 second in plane vector (along with \a Chord) of the triangle the baseline belongs to
|
---|
1476 | * @param OldNormal normal of the triangle which the baseline belongs to
|
---|
1477 | * @param ReferencePoint Vector of center of circumscribing circle from which we look towards center of sphere
|
---|
1478 | * @param Opt_Candidate candidate reference to return
|
---|
1479 | * @param Storage array containing two angles of current Opt_Candidate
|
---|
1480 | * @param RADIUS radius of ball
|
---|
1481 | * @param mol molecule structure with atoms and bonds
|
---|
1482 | */
|
---|
1483 |
|
---|
1484 | void Find_next_suitable_point_via_Angle_of_Sphere(atom* a, atom* b, atom* Candidate, atom* Parent,
|
---|
1485 | int RecursionLevel, Vector *Chord, Vector *direction1, Vector *OldNormal, Vector ReferencePoint,
|
---|
1486 | atom*& Opt_Candidate, double *Storage, const double RADIUS, molecule* mol)
|
---|
1487 | {
|
---|
1488 |
|
---|
1489 | cout << Verbose(1) << "Candidate is "<< Candidate->nr << endl;
|
---|
1490 | cout << "ReferencePoint is " << ReferencePoint.x[0] << " "<< ReferencePoint.x[1] << " "<< ReferencePoint.x[2] << " "<< endl;
|
---|
1491 | /* OldNormal is normal vector on the old triangle
|
---|
1492 | * direction1 is normal on the triangle line, from which we come, as well as on OldNormal.
|
---|
1493 | * Chord points from b to a!!!
|
---|
1494 | */
|
---|
1495 | Vector dif_a; //Vector from a to candidate
|
---|
1496 | Vector dif_b; //Vector from b to candidate
|
---|
1497 | Vector AngleCheck;
|
---|
1498 | Vector TempNormal, Umkreismittelpunkt;
|
---|
1499 | Vector Mittelpunkt;
|
---|
1500 |
|
---|
1501 | double CurrentEpsilon = 0.1;
|
---|
1502 | double alpha, beta, gamma, SideA, SideB, SideC, sign, Umkreisradius, Restradius, Distance;
|
---|
1503 | double BallAngle;
|
---|
1504 | atom *Walker; // variable atom point
|
---|
1505 |
|
---|
1506 |
|
---|
1507 | dif_a.CopyVector(&(a->x));
|
---|
1508 | dif_a.SubtractVector(&(Candidate->x));
|
---|
1509 | dif_b.CopyVector(&(b->x));
|
---|
1510 | dif_b.SubtractVector(&(Candidate->x));
|
---|
1511 | AngleCheck.CopyVector(&(Candidate->x));
|
---|
1512 | AngleCheck.SubtractVector(&(a->x));
|
---|
1513 | AngleCheck.ProjectOntoPlane(Chord);
|
---|
1514 |
|
---|
1515 | SideA = dif_b.Norm();
|
---|
1516 | SideB = dif_a.Norm();
|
---|
1517 | SideC = Chord->Norm();
|
---|
1518 | //Chord->Scale(-1);
|
---|
1519 |
|
---|
1520 | alpha = Chord->Angle(&dif_a);
|
---|
1521 | beta = M_PI - Chord->Angle(&dif_b);
|
---|
1522 | gamma = dif_a.Angle(&dif_b);
|
---|
1523 |
|
---|
1524 | if (a != Candidate and b != Candidate)
|
---|
1525 | {
|
---|
1526 |
|
---|
1527 | Umkreisradius = SideA / 2.0 / sin(alpha);
|
---|
1528 | //cout << Umkreisradius << endl;
|
---|
1529 | //cout << SideB / 2.0 / sin(beta) << endl;
|
---|
1530 | //cout << SideC / 2.0 / sin(gamma) << endl;
|
---|
1531 |
|
---|
1532 | if (Umkreisradius < RADIUS) //Checking whether ball will at least rest on points.
|
---|
1533 | {
|
---|
1534 | sign = AngleCheck.ScalarProduct(direction1);
|
---|
1535 | sign /= fabs(sign); // +1 if in direction of triangle plane, -1 if in other direction...
|
---|
1536 |
|
---|
1537 | Get_center_of_sphere(&Mittelpunkt, (a->x), (b->x), (Candidate->x), OldNormal, sign, alpha, beta, gamma, RADIUS, Umkreisradius);
|
---|
1538 |
|
---|
1539 | AngleCheck.CopyVector(&ReferencePoint);
|
---|
1540 | AngleCheck.Scale(-1);
|
---|
1541 | cout << "AngleCheck is " << AngleCheck.x[0] << " "<< AngleCheck.x[1] << " "<< AngleCheck.x[2] << " "<< endl;
|
---|
1542 | AngleCheck.AddVector(&Mittelpunkt);
|
---|
1543 | cout << "AngleCheck is " << AngleCheck.x[0] << " "<< AngleCheck.x[1] << " "<< AngleCheck.x[2] << " "<< endl;
|
---|
1544 |
|
---|
1545 | BallAngle = AngleCheck.Angle(OldNormal);
|
---|
1546 | cout << "direction1 is " << direction1->x[0] <<" "<< direction1->x[1] <<" "<< direction1->x[2] <<" " << endl;
|
---|
1547 | cout << "AngleCheck is " << AngleCheck.x[0] << " "<< AngleCheck.x[1] << " "<< AngleCheck.x[2] << " "<< endl;
|
---|
1548 | sign = AngleCheck.ScalarProduct(direction1);
|
---|
1549 | sign /= fabs(sign);
|
---|
1550 |
|
---|
1551 |
|
---|
1552 | if (sign >0)
|
---|
1553 | {
|
---|
1554 | if (Storage[0]< -1.5) // first Candidate at all
|
---|
1555 | {
|
---|
1556 |
|
---|
1557 | cout << "Next better candidate is " << *Candidate << " with ";
|
---|
1558 | Opt_Candidate = Candidate;
|
---|
1559 | Storage[0] = sign;
|
---|
1560 | Storage[1] = BallAngle;
|
---|
1561 | cout << "Angle is " << Storage[1] << ", Halbraum ist "
|
---|
1562 | << Storage[0] << endl;
|
---|
1563 |
|
---|
1564 |
|
---|
1565 | }
|
---|
1566 | else
|
---|
1567 | {
|
---|
1568 | if ( Storage[1] > BallAngle)
|
---|
1569 | {
|
---|
1570 | cout << "Next better candidate is " << *Candidate << " with ";
|
---|
1571 | Opt_Candidate = Candidate;
|
---|
1572 | Storage[0] = sign;
|
---|
1573 | Storage[1] = BallAngle;
|
---|
1574 | cout << "Angle is " << Storage[1] << ", Halbraum ist "
|
---|
1575 | << Storage[0] << endl;
|
---|
1576 | }
|
---|
1577 | else
|
---|
1578 | {
|
---|
1579 | if (DEBUG)
|
---|
1580 | {
|
---|
1581 | cout << "Looses to better candidate" << endl;
|
---|
1582 | }
|
---|
1583 | }
|
---|
1584 | }
|
---|
1585 | }
|
---|
1586 | else
|
---|
1587 | {
|
---|
1588 | cout << "Refused due to sign which is " << sign << endl;
|
---|
1589 | }
|
---|
1590 | }
|
---|
1591 | else
|
---|
1592 | {
|
---|
1593 | if (DEBUG)
|
---|
1594 | {
|
---|
1595 | cout << "Doesn't satisfy requirements for circumscribing circle" << endl;
|
---|
1596 | }
|
---|
1597 | }
|
---|
1598 | }
|
---|
1599 | else
|
---|
1600 | {
|
---|
1601 | if (DEBUG)
|
---|
1602 | {
|
---|
1603 | cout << "identisch mit Ursprungslinie" << endl;
|
---|
1604 | }
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 |
|
---|
1608 |
|
---|
1609 | if (RecursionLevel < 7) // Five is the recursion level threshold.
|
---|
1610 | {
|
---|
1611 | for (int i = 0; i < mol->NumberOfBondsPerAtom[Candidate->nr]; i++) // go through all bond
|
---|
1612 | {
|
---|
1613 | Walker = mol->ListOfBondsPerAtom[Candidate->nr][i]->GetOtherAtom(
|
---|
1614 | Candidate);
|
---|
1615 | if (Walker == Parent)
|
---|
1616 | { // don't go back the same bond
|
---|
1617 | continue;
|
---|
1618 | }
|
---|
1619 | else
|
---|
1620 | {
|
---|
1621 | Find_next_suitable_point_via_Angle_of_Sphere(a, b, Walker, Candidate, RecursionLevel
|
---|
1622 | + 1, Chord, direction1, OldNormal, ReferencePoint, Opt_Candidate, Storage, RADIUS,
|
---|
1623 | mol); //call function again
|
---|
1624 | }
|
---|
1625 | }
|
---|
1626 | }
|
---|
1627 | }
|
---|
1628 | ;
|
---|
1629 |
|
---|
1630 |
|
---|
1631 | /** This recursive function finds a third point, to form a triangle with two given ones.
|
---|
1632 | * Two atoms are fixed, a candidate is supplied, additionally two vectors for direction distinction, a Storage area to \
|
---|
1633 | * supply results to the calling function, the radius of the sphere which the triangle shall support and the molecule \
|
---|
1634 | * upon which we operate.
|
---|
1635 | * If the candidate is more fitting to support the sphere than the already stored atom is, then we write its general \
|
---|
1636 | * direction and angle into Storage.
|
---|
1637 | * We the determine the recursive level we have reached and if this is not on the threshold yet, call this function again, \
|
---|
1638 | * with all neighbours of the candidate.
|
---|
1639 | * @param a first point
|
---|
1640 | * @param b second point
|
---|
1641 | * @param Candidate base point along whose bonds to start looking from
|
---|
1642 | * @param Parent point to avoid during search as its wrong direction
|
---|
1643 | * @param RecursionLevel contains current recursion depth
|
---|
1644 | * @param Chord baseline vector of first and second point
|
---|
1645 | * @param d1 second in plane vector (along with \a Chord) of the triangle the baseline belongs to
|
---|
1646 | * @param OldNormal normal of the triangle which the baseline belongs to
|
---|
1647 | * @param Opt_Candidate candidate reference to return
|
---|
1648 | * @param Opt_Mittelpunkt Centerpoint of ball, when resting on Opt_Candidate
|
---|
1649 | * @param Storage array containing two angles of current Opt_Candidate
|
---|
1650 | * @param RADIUS radius of ball
|
---|
1651 | * @param mol molecule structure with atoms and bonds
|
---|
1652 | */
|
---|
1653 |
|
---|
1654 | void Find_next_suitable_point(atom* a, atom* b, atom* Candidate, atom* Parent,
|
---|
1655 | int RecursionLevel, Vector *Chord, Vector *d1, Vector *OldNormal,
|
---|
1656 | atom*& Opt_Candidate, Vector *Opt_Mittelpunkt, double *Storage, const double RADIUS, molecule* mol)
|
---|
1657 | {
|
---|
1658 | /* OldNormal is normal vector on the old triangle
|
---|
1659 | * d1 is normal on the triangle line, from which we come, as well as on OldNormal.
|
---|
1660 | * Chord points from b to a!!!
|
---|
1661 | */
|
---|
1662 | Vector dif_a; //Vector from a to candidate
|
---|
1663 | Vector dif_b; //Vector from b to candidate
|
---|
1664 | Vector AngleCheck, AngleCheckReference, DirectionCheckPoint;
|
---|
1665 | Vector TempNormal, Umkreismittelpunkt, Mittelpunkt;
|
---|
1666 |
|
---|
1667 | double CurrentEpsilon = 0.1;
|
---|
1668 | double alpha, beta, gamma, SideA, SideB, SideC, sign, Umkreisradius, Restradius, Distance;
|
---|
1669 | double BallAngle;
|
---|
1670 | atom *Walker; // variable atom point
|
---|
1671 |
|
---|
1672 |
|
---|
1673 | dif_a.CopyVector(&(a->x));
|
---|
1674 | dif_a.SubtractVector(&(Candidate->x));
|
---|
1675 | dif_b.CopyVector(&(b->x));
|
---|
1676 | dif_b.SubtractVector(&(Candidate->x));
|
---|
1677 | DirectionCheckPoint.CopyVector(&dif_a);
|
---|
1678 | DirectionCheckPoint.Scale(-1);
|
---|
1679 | DirectionCheckPoint.ProjectOntoPlane(Chord);
|
---|
1680 |
|
---|
1681 | SideA = dif_b.Norm();
|
---|
1682 | SideB = dif_a.Norm();
|
---|
1683 | SideC = Chord->Norm();
|
---|
1684 | //Chord->Scale(-1);
|
---|
1685 |
|
---|
1686 | alpha = Chord->Angle(&dif_a);
|
---|
1687 | beta = M_PI - Chord->Angle(&dif_b);
|
---|
1688 | gamma = dif_a.Angle(&dif_b);
|
---|
1689 |
|
---|
1690 |
|
---|
1691 | if (DEBUG)
|
---|
1692 | {
|
---|
1693 | cout << "Atom number" << Candidate->nr << endl;
|
---|
1694 | Candidate->x.Output((ofstream *) &cout);
|
---|
1695 | cout << "number of bonds " << mol->NumberOfBondsPerAtom[Candidate->nr]
|
---|
1696 | << endl;
|
---|
1697 | }
|
---|
1698 |
|
---|
1699 | if (a != Candidate and b != Candidate)
|
---|
1700 | {
|
---|
1701 | // alpha = dif_a.Angle(&dif_b) / 2.;
|
---|
1702 | // SideA = Chord->Norm() / 2.;// (Chord->Norm()/2.) / sin(0.5*alpha);
|
---|
1703 | // SideB = dif_a.Norm();
|
---|
1704 | // centerline = SideA * SideA + SideB * SideB - 2. * SideA * SideB * cos(
|
---|
1705 | // alpha); // note this is squared of center line length
|
---|
1706 | // centerline = (Chord->Norm()/2.) / sin(0.5*alpha);
|
---|
1707 | // Those are remains from Freddie. Needed?
|
---|
1708 |
|
---|
1709 |
|
---|
1710 |
|
---|
1711 | Umkreisradius = SideA / 2.0 / sin(alpha);
|
---|
1712 | //cout << Umkreisradius << endl;
|
---|
1713 | //cout << SideB / 2.0 / sin(beta) << endl;
|
---|
1714 | //cout << SideC / 2.0 / sin(gamma) << endl;
|
---|
1715 |
|
---|
1716 | if (Umkreisradius < RADIUS && DirectionCheckPoint.ScalarProduct(&(Candidate->x))>0) //Checking whether ball will at least rest o points.
|
---|
1717 | {
|
---|
1718 |
|
---|
1719 | // intermediate calculations to aquire centre of sphere, called Mittelpunkt:
|
---|
1720 |
|
---|
1721 | Umkreismittelpunkt = (a->x) * sin(2.*alpha) + b->x * sin(2.*beta) + (Candidate->x) * sin(2.*gamma) ;
|
---|
1722 | Umkreismittelpunkt.Scale(1/(sin(2*alpha) + sin(2*beta) + sin(2*gamma)));
|
---|
1723 |
|
---|
1724 | TempNormal.CopyVector(&dif_a);
|
---|
1725 | TempNormal.VectorProduct(&dif_b);
|
---|
1726 | if (TempNormal.ScalarProduct(OldNormal)<0 && sign>0 || TempNormal.ScalarProduct(OldNormal)>0 && sign<0)
|
---|
1727 | {
|
---|
1728 | TempNormal.Scale(-1);
|
---|
1729 | }
|
---|
1730 | TempNormal.Normalize();
|
---|
1731 | Restradius = sqrt(RADIUS*RADIUS - Umkreisradius*Umkreisradius);
|
---|
1732 | TempNormal.Scale(Restradius);
|
---|
1733 |
|
---|
1734 | Mittelpunkt.CopyVector(&Umkreismittelpunkt);
|
---|
1735 | Mittelpunkt.AddVector(&TempNormal); //this is center of sphere supported by a, b and Candidate
|
---|
1736 |
|
---|
1737 | AngleCheck.CopyVector(Chord);
|
---|
1738 | AngleCheck.Scale(-0.5);
|
---|
1739 | AngleCheck.SubtractVector(&(b->x));
|
---|
1740 | AngleCheckReference.CopyVector(&AngleCheck);
|
---|
1741 | AngleCheckReference.AddVector(Opt_Mittelpunkt);
|
---|
1742 | AngleCheck.AddVector(&Mittelpunkt);
|
---|
1743 |
|
---|
1744 | BallAngle = AngleCheck.Angle(&AngleCheckReference);
|
---|
1745 |
|
---|
1746 | d1->ProjectOntoPlane(&AngleCheckReference);
|
---|
1747 | sign = AngleCheck.ScalarProduct(d1);
|
---|
1748 | sign /= fabs(sign); // +1 if in direction of triangle plane, -1 if in other direction...
|
---|
1749 |
|
---|
1750 |
|
---|
1751 | if (Storage[0]< -1.5) // first Candidate at all
|
---|
1752 | {
|
---|
1753 |
|
---|
1754 | cout << "Next better candidate is " << *Candidate << " with ";
|
---|
1755 | Opt_Candidate = Candidate;
|
---|
1756 | Storage[0] = sign;
|
---|
1757 | Storage[1] = BallAngle;
|
---|
1758 | Opt_Mittelpunkt->CopyVector(&Mittelpunkt);
|
---|
1759 | cout << "Angle is " << Storage[1] << ", Halbraum ist "
|
---|
1760 | << Storage[0] << endl;
|
---|
1761 |
|
---|
1762 |
|
---|
1763 | }
|
---|
1764 | else
|
---|
1765 | {
|
---|
1766 | /*
|
---|
1767 | * removed due to change in criterium, now checking angle of ball to old normal.
|
---|
1768 | //We will now check for non interference, that is if the new candidate would have the Opt_Candidate
|
---|
1769 | //within the ball.
|
---|
1770 |
|
---|
1771 | Distance = Opt_Candidate->x.Distance(&Mittelpunkt);
|
---|
1772 | //cout << "Opt_Candidate " << Opt_Candidate << " has distance " << Distance << " to Center of Candidate " << endl;
|
---|
1773 |
|
---|
1774 |
|
---|
1775 | if (Distance >RADIUS) // We have no interference and may now check whether the new point is better.
|
---|
1776 | */
|
---|
1777 | {
|
---|
1778 | //cout << "Atom " << Candidate << " has distance " << Candidate->x.Distance(Opt_Mittelpunkt) << " to Center of Candidate " << endl;
|
---|
1779 |
|
---|
1780 | if (((Storage[0] < 0 && fabs(sign - Storage[0]) > CurrentEpsilon))) //This will give absolute preference to those in "right-hand" quadrants
|
---|
1781 | //(Candidate->x.Distance(Opt_Mittelpunkt) < RADIUS)) //and those where Candidate would be within old Sphere.
|
---|
1782 | {
|
---|
1783 | cout << "Next better candidate is " << *Candidate << " with ";
|
---|
1784 | Opt_Candidate = Candidate;
|
---|
1785 | Storage[0] = sign;
|
---|
1786 | Storage[1] = BallAngle;
|
---|
1787 | Opt_Mittelpunkt->CopyVector(&Mittelpunkt);
|
---|
1788 | cout << "Angle is " << Storage[1] << ", Halbraum ist "
|
---|
1789 | << Storage[0] << endl;
|
---|
1790 |
|
---|
1791 |
|
---|
1792 | }
|
---|
1793 | else
|
---|
1794 | {
|
---|
1795 | if ((fabs(sign - Storage[0]) < CurrentEpsilon && sign > 0
|
---|
1796 | && Storage[1] > BallAngle) ||
|
---|
1797 | (fabs(sign - Storage[0]) < CurrentEpsilon && sign < 0
|
---|
1798 | && Storage[1] < BallAngle))
|
---|
1799 | //Depending on quadrant we prefer higher or lower atom with respect to Triangle normal first.
|
---|
1800 | {
|
---|
1801 | cout << "Next better candidate is " << *Candidate << " with ";
|
---|
1802 | Opt_Candidate = Candidate;
|
---|
1803 | Storage[0] = sign;
|
---|
1804 | Storage[1] = BallAngle;
|
---|
1805 | Opt_Mittelpunkt->CopyVector(&Mittelpunkt);
|
---|
1806 | cout << "Angle is " << Storage[1] << ", Halbraum ist "
|
---|
1807 | << Storage[0] << endl;
|
---|
1808 | }
|
---|
1809 |
|
---|
1810 | }
|
---|
1811 | }
|
---|
1812 | /*
|
---|
1813 | * This is for checking point-angle and presence of Candidates in Ball, currently we are checking the ball Angle.
|
---|
1814 | *
|
---|
1815 | else
|
---|
1816 | {
|
---|
1817 | if (sign>0 && BallAngle>0 && Storage[0]<0)
|
---|
1818 | {
|
---|
1819 | cout << "Next better candidate is " << *Candidate << " with ";
|
---|
1820 | Opt_Candidate = Candidate;
|
---|
1821 | Storage[0] = sign;
|
---|
1822 | Storage[1] = BallAngle;
|
---|
1823 | Opt_Mittelpunkt->CopyVector(&Mittelpunkt);
|
---|
1824 | cout << "Angle is " << Storage[1] << ", Halbraum ist "
|
---|
1825 | << Storage[0] << endl;
|
---|
1826 |
|
---|
1827 | //Debugging purposes only
|
---|
1828 | cout << "Umkreismittelpunkt has coordinates" << Umkreismittelpunkt.x[0] << " "<< Umkreismittelpunkt.x[1] <<" "<<Umkreismittelpunkt.x[2] << endl;
|
---|
1829 | cout << "Candidate has coordinates" << Candidate->x.x[0]<< " " << Candidate->x.x[1] << " " << Candidate->x.x[2] << endl;
|
---|
1830 | cout << "a has coordinates" << a->x.x[0]<< " " << a->x.x[1] << " " << a->x.x[2] << endl;
|
---|
1831 | cout << "b has coordinates" << b->x.x[0]<< " " << b->x.x[1] << " " << b->x.x[2] << endl;
|
---|
1832 | cout << "Mittelpunkt has coordinates" << Mittelpunkt.x[0] << " " << Mittelpunkt.x[1]<< " " <<Mittelpunkt.x[2] << endl;
|
---|
1833 | cout << "Umkreisradius ist " << Umkreisradius << endl;
|
---|
1834 | cout << "Restradius ist " << Restradius << endl;
|
---|
1835 | cout << "TempNormal has coordinates " << TempNormal.x[0] << " " << TempNormal.x[1] << " " << TempNormal.x[2] << " " << endl;
|
---|
1836 | cout << "OldNormal has coordinates " << OldNormal->x[0] << " " << OldNormal->x[1] << " " << OldNormal->x[2] << " " << endl;
|
---|
1837 | cout << "Dist a to UmkreisMittelpunkt " << a->x.Distance(&Umkreismittelpunkt) << endl;
|
---|
1838 | cout << "Dist b to UmkreisMittelpunkt " << b->x.Distance(&Umkreismittelpunkt) << endl;
|
---|
1839 | cout << "Dist Candidate to UmkreisMittelpunkt " << Candidate->x.Distance(&Umkreismittelpunkt) << endl;
|
---|
1840 | cout << "Dist a to Mittelpunkt " << a->x.Distance(&Mittelpunkt) << endl;
|
---|
1841 | cout << "Dist b to Mittelpunkt " << b->x.Distance(&Mittelpunkt) << endl;
|
---|
1842 | cout << "Dist Candidate to Mittelpunkt " << Candidate->x.Distance(&Mittelpunkt) << endl;
|
---|
1843 |
|
---|
1844 |
|
---|
1845 |
|
---|
1846 | }
|
---|
1847 | else
|
---|
1848 | {
|
---|
1849 | if (DEBUG)
|
---|
1850 | cout << "Looses to better candidate" << endl;
|
---|
1851 | }
|
---|
1852 | }
|
---|
1853 | */
|
---|
1854 | }
|
---|
1855 | }
|
---|
1856 | else
|
---|
1857 | {
|
---|
1858 | if (DEBUG)
|
---|
1859 | {
|
---|
1860 | cout << "Doesn't satisfy requirements for circumscribing circle" << endl;
|
---|
1861 | }
|
---|
1862 | }
|
---|
1863 | }
|
---|
1864 |
|
---|
1865 | else
|
---|
1866 | {
|
---|
1867 | if (DEBUG)
|
---|
1868 | cout << "identisch mit Ursprungslinie" << endl;
|
---|
1869 | }
|
---|
1870 |
|
---|
1871 | if (RecursionLevel < 9) // Five is the recursion level threshold.
|
---|
1872 | {
|
---|
1873 | for (int i = 0; i < mol->NumberOfBondsPerAtom[Candidate->nr]; i++) // go through all bond
|
---|
1874 | {
|
---|
1875 | Walker = mol->ListOfBondsPerAtom[Candidate->nr][i]->GetOtherAtom(
|
---|
1876 | Candidate);
|
---|
1877 | if (Walker == Parent)
|
---|
1878 | { // don't go back the same bond
|
---|
1879 | continue;
|
---|
1880 | }
|
---|
1881 | else
|
---|
1882 | {
|
---|
1883 | Find_next_suitable_point(a, b, Walker, Candidate, RecursionLevel
|
---|
1884 | + 1, Chord, d1, OldNormal, Opt_Candidate, Opt_Mittelpunkt, Storage, RADIUS,
|
---|
1885 | mol); //call function again
|
---|
1886 |
|
---|
1887 | }
|
---|
1888 | }
|
---|
1889 | }
|
---|
1890 | }
|
---|
1891 | ;
|
---|
1892 |
|
---|
1893 | /** This function finds a triangle to a line, adjacent to an existing one.
|
---|
1894 | * @param out output stream for debugging
|
---|
1895 | * @param tecplot output stream for writing found triangles in TecPlot format
|
---|
1896 | * @param mol molecule structure with all atoms and bonds
|
---|
1897 | * @param Line current baseline to search from
|
---|
1898 | * @param T current triangle which \a Line is edge of
|
---|
1899 | * @param RADIUS radius of the rolling ball
|
---|
1900 | * @param N number of found triangles
|
---|
1901 | */
|
---|
1902 | void Tesselation::Find_next_suitable_triangle(ofstream *out, ofstream *tecplot,
|
---|
1903 | molecule* mol, BoundaryLineSet &Line, BoundaryTriangleSet &T,
|
---|
1904 | const double& RADIUS, int N)
|
---|
1905 | {
|
---|
1906 | cout << Verbose(1) << "Looking for next suitable triangle \n";
|
---|
1907 | Vector direction1;
|
---|
1908 | Vector helper;
|
---|
1909 | Vector Chord;
|
---|
1910 | ofstream *tempstream = NULL;
|
---|
1911 | char filename[255];
|
---|
1912 | //atom* Walker;
|
---|
1913 |
|
---|
1914 | double Storage[2];
|
---|
1915 | Storage[0] = -2.; // This direction is either +1 or -1 one, so any result will take precedence over initial values
|
---|
1916 | Storage[1] = 9999999.; // This is also lower then any value produced by an eligible atom, which are all positive
|
---|
1917 | atom* Opt_Candidate = NULL;
|
---|
1918 | Vector Opt_Mittelpunkt;
|
---|
1919 |
|
---|
1920 | cout << Verbose(1) << "Constructing helpful vectors ... " << endl;
|
---|
1921 | helper.CopyVector(&(Line.endpoints[0]->node->x));
|
---|
1922 | for (int i = 0; i < 3; i++)
|
---|
1923 | {
|
---|
1924 | if (T.endpoints[i]->node->nr != Line.endpoints[0]->node->nr
|
---|
1925 | && T.endpoints[i]->node->nr != Line.endpoints[1]->node->nr)
|
---|
1926 | {
|
---|
1927 | helper.SubtractVector(&T.endpoints[i]->node->x);
|
---|
1928 | break;
|
---|
1929 | }
|
---|
1930 | }
|
---|
1931 |
|
---|
1932 | direction1.CopyVector(&Line.endpoints[0]->node->x);
|
---|
1933 | direction1.SubtractVector(&Line.endpoints[1]->node->x);
|
---|
1934 | direction1.VectorProduct(&(T.NormalVector));
|
---|
1935 |
|
---|
1936 | if (direction1.ScalarProduct(&helper) < 0)
|
---|
1937 | {
|
---|
1938 | direction1.Scale(-1);
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 | Chord.CopyVector(&(Line.endpoints[0]->node->x)); // bring into calling function
|
---|
1942 | Chord.SubtractVector(&(Line.endpoints[1]->node->x));
|
---|
1943 |
|
---|
1944 |
|
---|
1945 | Vector Umkreismittelpunkt, a, b, c;
|
---|
1946 | double alpha, beta, gamma;
|
---|
1947 | a.CopyVector(&(T.endpoints[0]->node->x));
|
---|
1948 | b.CopyVector(&(T.endpoints[1]->node->x));
|
---|
1949 | c.CopyVector(&(T.endpoints[2]->node->x));
|
---|
1950 | a.SubtractVector(&(T.endpoints[1]->node->x));
|
---|
1951 | b.SubtractVector(&(T.endpoints[2]->node->x));
|
---|
1952 | c.SubtractVector(&(T.endpoints[0]->node->x));
|
---|
1953 |
|
---|
1954 | alpha = M_PI - a.Angle(&c);
|
---|
1955 | beta = M_PI - b.Angle(&a);
|
---|
1956 | gamma = M_PI - c.Angle(&b);
|
---|
1957 |
|
---|
1958 | Umkreismittelpunkt = (T.endpoints[0]->node->x) * sin(2.*alpha) + T.endpoints[1]->node->x * sin(2.*beta) + (T.endpoints[2]->node->x) * sin(2.*gamma) ;
|
---|
1959 | cout << "UmkreisMittelpunkt is " << Umkreismittelpunkt.x[0] << " "<< Umkreismittelpunkt.x[1] << " "<< Umkreismittelpunkt.x[2] << " "<< endl;
|
---|
1960 | Umkreismittelpunkt.Scale(1/(sin(2*alpha) + sin(2*beta) + sin(2*gamma)));
|
---|
1961 | cout << "UmkreisMittelpunkt is " << Umkreismittelpunkt.x[0] << " "<< Umkreismittelpunkt.x[1] << " "<< Umkreismittelpunkt.x[2] << " "<< endl;
|
---|
1962 |
|
---|
1963 |
|
---|
1964 | cout << Verbose(1) << "Looking for third point candidates for triangle ... " << endl;
|
---|
1965 |
|
---|
1966 | Find_next_suitable_point_via_Angle_of_Sphere(Line.endpoints[0]->node, Line.endpoints[1]->node,
|
---|
1967 | Line.endpoints[0]->node, Line.endpoints[1]->node, 0, &Chord, &direction1,
|
---|
1968 | &(T.NormalVector), Umkreismittelpunkt, Opt_Candidate, Storage, RADIUS, mol);
|
---|
1969 | Find_next_suitable_point_via_Angle_of_Sphere(Line.endpoints[0]->node, Line.endpoints[1]->node,
|
---|
1970 | Line.endpoints[1]->node, Line.endpoints[0]->node, 0, &Chord, &direction1,
|
---|
1971 | &(T.NormalVector), Umkreismittelpunkt, Opt_Candidate, Storage, RADIUS, mol);
|
---|
1972 |
|
---|
1973 |
|
---|
1974 | if ((TrianglesOnBoundaryCount % 10) == 0)
|
---|
1975 | {
|
---|
1976 | sprintf(filename, "testEnvelope-%d.dat", TriangleFilesWritten);
|
---|
1977 | tempstream = new ofstream(filename, ios::trunc);
|
---|
1978 | write_tecplot_file(out, tempstream, this, mol, TriangleFilesWritten++);
|
---|
1979 | tempstream->close();
|
---|
1980 | tempstream->flush();
|
---|
1981 | delete(tempstream);
|
---|
1982 | }
|
---|
1983 |
|
---|
1984 | if (TrianglesOnBoundaryCount >1000 )
|
---|
1985 | {
|
---|
1986 | cout << Verbose(1)
|
---|
1987 | << "No new Atom found, triangle construction will crash" << endl;
|
---|
1988 | write_tecplot_file(out, tecplot, this, mol, TriangleFilesWritten);
|
---|
1989 | cout << "This is currently added candidate" << Opt_Candidate << endl;
|
---|
1990 | }
|
---|
1991 | // Konstruiere nun neues Dreieck am Ende der Liste der Dreiecke
|
---|
1992 |
|
---|
1993 | cout << " Optimal candidate is " << *Opt_Candidate << endl;
|
---|
1994 |
|
---|
1995 | AddTrianglePoint(Opt_Candidate, 0);
|
---|
1996 | AddTrianglePoint(Line.endpoints[0]->node, 1);
|
---|
1997 | AddTrianglePoint(Line.endpoints[1]->node, 2);
|
---|
1998 |
|
---|
1999 | AddTriangleLine(TPS[0], TPS[1], 0);
|
---|
2000 | AddTriangleLine(TPS[0], TPS[2], 1);
|
---|
2001 | AddTriangleLine(TPS[1], TPS[2], 2);
|
---|
2002 |
|
---|
2003 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
|
---|
2004 | AddTriangleToLines();
|
---|
2005 | cout << "New triangle with " << *BTS << endl;
|
---|
2006 | cout << "We have "<< TrianglesOnBoundaryCount << endl;
|
---|
2007 | cout << Verbose(1) << "Constructing normal vector for this triangle ... " << endl;
|
---|
2008 |
|
---|
2009 | BTS->GetNormalVector(BTS->NormalVector);
|
---|
2010 |
|
---|
2011 | if ((BTS->NormalVector.ScalarProduct(&(T.NormalVector)) < 0 && Storage[0] > 0) ||
|
---|
2012 | (BTS->NormalVector.ScalarProduct(&(T.NormalVector)) > 0 && Storage[0] < 0))
|
---|
2013 | {
|
---|
2014 | BTS->NormalVector.Scale(-1);
|
---|
2015 | };
|
---|
2016 |
|
---|
2017 | }
|
---|
2018 | ;
|
---|
2019 |
|
---|
2020 | void Find_second_point_for_Tesselation(atom* a, atom* Candidate, atom* Parent,
|
---|
2021 | int RecursionLevel, Vector Oben, atom*& Opt_Candidate, double Storage[2],
|
---|
2022 | molecule* mol, double RADIUS)
|
---|
2023 | {
|
---|
2024 | cout << Verbose(1)
|
---|
2025 | << "Looking for second point of starting triangle, recursive level "
|
---|
2026 | << RecursionLevel << endl;;
|
---|
2027 | int i;
|
---|
2028 | Vector AngleCheck;
|
---|
2029 | atom* Walker;
|
---|
2030 | double norm = -1.;
|
---|
2031 |
|
---|
2032 | // check if we only have one unique point yet ...
|
---|
2033 | if (a != Candidate)
|
---|
2034 | {
|
---|
2035 | AngleCheck.CopyVector(&(Candidate->x));
|
---|
2036 | AngleCheck.SubtractVector(&(a->x));
|
---|
2037 | norm = AngleCheck.Norm();
|
---|
2038 | // second point shall have smallest angle with respect to Oben vector
|
---|
2039 | if (norm < RADIUS)
|
---|
2040 | {
|
---|
2041 | if (AngleCheck.Angle(&Oben) < Storage[0])
|
---|
2042 | {
|
---|
2043 | //cout << Verbose(1) << "Old values of Storage: %lf %lf \n", Storage[0], Storage[1]);
|
---|
2044 | cout << "Next better candidate is " << *Candidate
|
---|
2045 | << " with distance " << norm << ".\n";
|
---|
2046 | Opt_Candidate = Candidate;
|
---|
2047 | Storage[0] = AngleCheck.Angle(&Oben);
|
---|
2048 | //cout << Verbose(1) << "Changing something in Storage: %lf %lf. \n", Storage[0], Storage[1]);
|
---|
2049 | }
|
---|
2050 | else
|
---|
2051 | {
|
---|
2052 | cout << Verbose(1) << "Supposedly looses to a better candidate "
|
---|
2053 | << *Opt_Candidate << endl;
|
---|
2054 | }
|
---|
2055 | }
|
---|
2056 | else
|
---|
2057 | {
|
---|
2058 | cout << Verbose(1) << *Candidate << " refused due to Radius " << norm
|
---|
2059 | << endl;
|
---|
2060 | }
|
---|
2061 | }
|
---|
2062 |
|
---|
2063 | // if not recursed to deeply, look at all its bonds
|
---|
2064 | if (RecursionLevel < 7)
|
---|
2065 | {
|
---|
2066 | for (i = 0; i < mol->NumberOfBondsPerAtom[Candidate->nr]; i++)
|
---|
2067 | {
|
---|
2068 | Walker = mol->ListOfBondsPerAtom[Candidate->nr][i]->GetOtherAtom(
|
---|
2069 | Candidate);
|
---|
2070 | if (Walker == Parent) // don't go back along the bond we came from
|
---|
2071 | continue;
|
---|
2072 | else
|
---|
2073 | Find_second_point_for_Tesselation(a, Walker, Candidate,
|
---|
2074 | RecursionLevel + 1, Oben, Opt_Candidate, Storage, mol, RADIUS);
|
---|
2075 | };
|
---|
2076 | };
|
---|
2077 | }
|
---|
2078 | ;
|
---|
2079 |
|
---|
2080 | void Tesselation::Find_starting_triangle(molecule* mol, const double RADIUS)
|
---|
2081 | {
|
---|
2082 | cout << Verbose(1) << "Looking for starting triangle \n";
|
---|
2083 | int i = 0;
|
---|
2084 | atom* Walker;
|
---|
2085 | atom* FirstPoint;
|
---|
2086 | atom* SecondPoint;
|
---|
2087 | atom* max_index[3];
|
---|
2088 | double max_coordinate[3];
|
---|
2089 | Vector Oben;
|
---|
2090 | Vector helper;
|
---|
2091 | Vector Chord;
|
---|
2092 | Vector CenterOfFirstLine;
|
---|
2093 |
|
---|
2094 | Oben.Zero();
|
---|
2095 |
|
---|
2096 | for (i = 0; i < 3; i++)
|
---|
2097 | {
|
---|
2098 | max_index[i] = NULL;
|
---|
2099 | max_coordinate[i] = -1;
|
---|
2100 | }
|
---|
2101 | cout << Verbose(1) << "Molecule mol is there and has " << mol->AtomCount
|
---|
2102 | << " Atoms \n";
|
---|
2103 |
|
---|
2104 | // 1. searching topmost atom with respect to each axis
|
---|
2105 | Walker = mol->start;
|
---|
2106 | while (Walker->next != mol->end)
|
---|
2107 | {
|
---|
2108 | Walker = Walker->next;
|
---|
2109 | for (i = 0; i < 3; i++)
|
---|
2110 | {
|
---|
2111 | if (Walker->x.x[i] > max_coordinate[i])
|
---|
2112 | {
|
---|
2113 | max_coordinate[i] = Walker->x.x[i];
|
---|
2114 | max_index[i] = Walker;
|
---|
2115 | }
|
---|
2116 | }
|
---|
2117 | }
|
---|
2118 |
|
---|
2119 | cout << Verbose(1) << "Found maximum coordinates. " << endl;
|
---|
2120 | //Koennen dies fuer alle Richtungen, legen hier erstmal Richtung auf k=0
|
---|
2121 | const int k = 1;
|
---|
2122 | Oben.x[k] = 1.;
|
---|
2123 | FirstPoint = max_index[k];
|
---|
2124 |
|
---|
2125 | cout << Verbose(1) << "Coordinates of start atom " << *FirstPoint << ": "
|
---|
2126 | << FirstPoint->x.x[0] << endl;
|
---|
2127 | double Storage[2];
|
---|
2128 | atom* Opt_Candidate = NULL;
|
---|
2129 | Storage[0] = 999999.; // This will contain the angle, which will be always positive (when looking for second point), when looking for third point this will be the quadrant.
|
---|
2130 | Storage[1] = 999999.; // This will be an angle looking for the third point.
|
---|
2131 | cout << Verbose(1) << "Number of Bonds: "
|
---|
2132 | << mol->NumberOfBondsPerAtom[FirstPoint->nr] << endl;
|
---|
2133 |
|
---|
2134 | Find_second_point_for_Tesselation(FirstPoint, FirstPoint, FirstPoint, 0,
|
---|
2135 | Oben, Opt_Candidate, Storage, mol, RADIUS); // we give same point as next candidate as its bonds are looked into in find_second_...
|
---|
2136 | SecondPoint = Opt_Candidate;
|
---|
2137 | cout << Verbose(1) << "Found second point is " << *SecondPoint << ".\n";
|
---|
2138 |
|
---|
2139 | helper.CopyVector(&(FirstPoint->x));
|
---|
2140 | helper.SubtractVector(&(SecondPoint->x));
|
---|
2141 | helper.Normalize();
|
---|
2142 | Oben.ProjectOntoPlane(&helper);
|
---|
2143 | Oben.Normalize();
|
---|
2144 | helper.VectorProduct(&Oben);
|
---|
2145 | Storage[0] = -2.; // This will indicate the quadrant.
|
---|
2146 | Storage[1] = 9999999.; // This will be an angle looking for the third point.
|
---|
2147 |
|
---|
2148 | Chord.CopyVector(&(FirstPoint->x)); // bring into calling function
|
---|
2149 | Chord.SubtractVector(&(SecondPoint->x));
|
---|
2150 | // Now, oben and helper are two orthonormalized vectors in the plane defined by Chord (not normalized)
|
---|
2151 |
|
---|
2152 | cout << Verbose(1) << "Looking for third point candidates \n";
|
---|
2153 | // look in one direction of baseline for initial candidate
|
---|
2154 | Opt_Candidate = NULL;
|
---|
2155 | CenterOfFirstLine.CopyVector(&Chord);
|
---|
2156 | CenterOfFirstLine.Scale(-0.5);
|
---|
2157 | CenterOfFirstLine.AddVector(&(SecondPoint->x));
|
---|
2158 |
|
---|
2159 | Find_next_suitable_point_via_Angle_of_Sphere(FirstPoint, SecondPoint, SecondPoint, FirstPoint, 0,
|
---|
2160 | &Chord, &helper, &Oben, CenterOfFirstLine, Opt_Candidate, Storage, RADIUS, mol);
|
---|
2161 | // look in other direction of baseline for possible better candidate
|
---|
2162 | Find_next_suitable_point_via_Angle_of_Sphere(FirstPoint, SecondPoint, FirstPoint, SecondPoint, 0,
|
---|
2163 | &Chord, &helper, &Oben, CenterOfFirstLine, Opt_Candidate, Storage, RADIUS, mol);
|
---|
2164 | cout << Verbose(1) << "Third Point is " << *Opt_Candidate << endl;
|
---|
2165 |
|
---|
2166 | // FOUND Starting Triangle: FirstPoint, SecondPoint, Opt_Candidate
|
---|
2167 |
|
---|
2168 | cout << Verbose(1) << "The found starting triangle consists of "
|
---|
2169 | << *FirstPoint << ", " << *SecondPoint << " and " << *Opt_Candidate
|
---|
2170 | << "." << endl;
|
---|
2171 |
|
---|
2172 | // Finally, we only have to add the found points
|
---|
2173 | AddTrianglePoint(FirstPoint, 0);
|
---|
2174 | AddTrianglePoint(SecondPoint, 1);
|
---|
2175 | AddTrianglePoint(Opt_Candidate, 2);
|
---|
2176 | // ... and respective lines
|
---|
2177 | AddTriangleLine(TPS[0], TPS[1], 0);
|
---|
2178 | AddTriangleLine(TPS[1], TPS[2], 1);
|
---|
2179 | AddTriangleLine(TPS[0], TPS[2], 2);
|
---|
2180 | // ... and triangles to the Maps of the Tesselation class
|
---|
2181 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
|
---|
2182 | AddTriangleToLines();
|
---|
2183 | // ... and calculate its normal vector (with correct orientation)
|
---|
2184 | Oben.Scale(-1.);
|
---|
2185 | BTS->GetNormalVector(Oben);
|
---|
2186 | }
|
---|
2187 | ;
|
---|
2188 |
|
---|
2189 | void Find_non_convex_border(ofstream *out, ofstream *tecplot, molecule* mol)
|
---|
2190 | {
|
---|
2191 | int N = 0;
|
---|
2192 | struct Tesselation *Tess = new Tesselation;
|
---|
2193 | cout << Verbose(1) << "Entering search for non convex hull. " << endl;
|
---|
2194 | cout << flush;
|
---|
2195 | const double RADIUS = 6.;
|
---|
2196 | LineMap::iterator baseline;
|
---|
2197 | Tess->Find_starting_triangle(mol, RADIUS);
|
---|
2198 |
|
---|
2199 | baseline = Tess->LinesOnBoundary.begin();
|
---|
2200 | while (baseline != Tess->LinesOnBoundary.end())
|
---|
2201 | {
|
---|
2202 | if (baseline->second->TrianglesCount == 1)
|
---|
2203 | {
|
---|
2204 | cout << Verbose(1) << "Begin of Tesselation ... " << endl;
|
---|
2205 | Tess->Find_next_suitable_triangle(out, tecplot, mol,
|
---|
2206 | *(baseline->second),
|
---|
2207 | *(((baseline->second->triangles.begin()))->second), RADIUS, N); //the line is there, so there is a triangle, but only one.
|
---|
2208 | cout << Verbose(1) << "End of Tesselation ... " << endl;
|
---|
2209 | }
|
---|
2210 | else
|
---|
2211 | {
|
---|
2212 | cout << Verbose(1) << "There is a line with "
|
---|
2213 | << baseline->second->TrianglesCount << " triangles adjacent"
|
---|
2214 | << endl;
|
---|
2215 | }
|
---|
2216 | N++;
|
---|
2217 | baseline++;
|
---|
2218 | }
|
---|
2219 | write_tecplot_file(out, tecplot, Tess, mol, -1);
|
---|
2220 |
|
---|
2221 | }
|
---|
2222 | ;
|
---|