VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
GbTriangularMesh3D.cpp
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
35
36#include <map>
37
39
43
44using namespace std;
45
47{
48 this->setName("new GbMesh");
49 this->nodes = new vector<GbPoint3D *>;
51 this->edges = new vector<GbLine3D *>;
52
53 this->pointinobjecttest = RAYCROSSING;
54
55 this->consistent = false;
56 x1min = x1max = x2min = x2max = x3min = x3max = 0.0;
57}
58/*=============================================================================================*/
60 : GbObject3D()
61{
62 if (name.size() == 0)
63 throw UbException(UB_EXARGS, "no name specified");
64 if (!nodes)
65 throw UbException(UB_EXARGS, "no nodes specified");
66 if (!triangles)
67 throw UbException(UB_EXARGS, "no triangles specified");
68
69 this->setName(name);
70 this->nodes = nodes;
71 this->triangles = triangles;
72 this->edges = new vector<GbLine3D *>;
73 this->pointinobjecttest = RAYCROSSING;
74
75 this->consistent = false;
76 x1min = x1max = x2min = x2max = x3min = x3max = 0.0;
77}
78/*=============================================================================================*/
80{
81 cout << "Das Teil erzeugt seinen KnotenVector aus den Dreiecken ...\n Es sollte deleteRedundantNodes() aufgerufen "
82 "werden \n";
83 if (name.size() == 0)
84 throw UbException(UB_EXARGS, "no name specified");
85 if (!tris)
86 throw UbException(UB_EXARGS, "no triangles specified");
87
89 this->triangles = new vector<GbTriangle3D *>;
90 GbPoint3D *p1 = NULL;
91 GbPoint3D *p2 = NULL;
92 GbPoint3D *p3 = NULL;
93 for (int u = 0; u < (int)tris->size(); u++) {
94 if (ub_math::zero((*tris)[u]->getArea())) {
95 (*tris)[u]->finalize();
96 delete (*tris)[u];
97 (*tris)[u] = NULL;
98 continue;
99 }
100 this->triangles->push_back((*tris)[u]);
101 p1 = (*tris)[u]->getPoint1();
102 p2 = (*tris)[u]->getPoint2();
103 p3 = (*tris)[u]->getPoint3();
104 points->push_back(p1);
105 points->push_back(p2);
106 points->push_back(p3);
107 }
108
109 this->setName(name);
110 this->nodes = points;
111 // this->triangles = triangles;
112 this->edges = new vector<GbLine3D *>;
113 this->edges->resize(0, NULL);
114 this->pointinobjecttest = RAYCROSSING;
115
116 this->consistent = false;
117 x1min = x1max = x2min = x2max = x3min = x3max = 0.0;
118}
119/*=============================================================================================*/
121 vector<GbTriangle3D *> *triangles)
122 : GbObject3D()
123{
124 if (name.size() == 0)
125 throw UbException(UB_EXARGS, "no name specified");
126 if (!nodes)
127 throw UbException(UB_EXARGS, "no nodes specified");
128 if (!triangles)
129 throw UbException(UB_EXARGS, "no triangles specified");
130 if (!edges)
131 throw UbException(UB_EXARGS, "no edges specified");
132
133 this->setName(name);
134 this->nodes = nodes;
135 this->edges = edges;
136 this->triangles = triangles;
137 this->pointinobjecttest = RAYCROSSING;
138
139 this->consistent = false;
140 x1min = x1max = x2min = x2max = x3min = x3max = 0.0;
141}
142/*=============================================================================================*/
144{
145 if (this->nodes) {
146 for (unsigned u = 0; u < nodes->size(); u++)
147 delete (*nodes)[u];
148 delete nodes;
149 }
150 if (triangles) {
151 for (unsigned u = 0; u < triangles->size(); u++)
152 delete (*triangles)[u];
153 delete triangles;
154 }
155}
156/*======================================================================*/
158{
159 std::map<GbPoint3D *, GbTriangle3D *> pointTriMap;
160 GbPoint3D *p1 = NULL;
161 GbPoint3D *p2 = NULL;
162 GbPoint3D *p3 = NULL;
164
165 for (int u = 0; u < (int)this->triangles->size(); u++) {
166 tri = (*this->triangles)[u];
167 p1 = tri->getPoint1();
168 p2 = tri->getPoint2();
169 p3 = tri->getPoint3();
173 }
174
175 cout << "Nodes before deleting redundant:" << this->nodes->size() << endl;
176 GbPoint3D *pA = NULL;
177 GbPoint3D *pB = NULL;
178 std::map<GbPoint3D *, GbTriangle3D *>::iterator mapIterator;
179 for (int u = 0; u < (int)this->nodes->size(); u++) {
180 // cout<<u<<" von "<<this->nodes->size()<<endl;
181 pA = (*this->nodes)[u];
182 if (pA == NULL)
183 continue;
184 for (int w = u + 1; w < (int)this->nodes->size(); w++) {
185 // cout<<w<<" Wvon "<<this->nodes->size()<<endl;
186 pB = (*this->nodes)[w];
187 if (pB == NULL)
188 continue;
189 if (pA->equals(pB)) {
190 // doppelter Knoten ...
191 mapIterator = pointTriMap.find(pB);
192 tri = dynamic_cast<GbTriangle3D *>(mapIterator->second);
193 if (!tri)
194 throw UbException(UB_EXARGS, "triangle not found");
195 p1 = tri->getPoint1();
196 p2 = tri->getPoint2();
197 p3 = tri->getPoint3();
198 if (pB == p1)
199 tri->setPoint(pA, 0);
200 else if (pB == p2)
201 tri->setPoint(pA, 1);
202 else if (pB == p3)
203 tri->setPoint(pA, 2);
204 else
205 throw UbException(UB_EXARGS, "node should be there");
206 delete pB;
207 (*this->nodes)[w] = NULL;
208 }
209 }
210 }
212 for (int u = 0; u < (int)this->nodes->size(); u++) {
213 pA = (*this->nodes)[u];
214 if (pA != NULL)
215 points->push_back(pA);
216 }
217 delete this->nodes;
218 this->nodes = points;
219 cout << "Nodes after deleting redundant:" << this->nodes->size() << endl;
220
221 // nochmal kontrolle ...
222 pointTriMap.clear();
223 for (int u = 0; u < (int)this->triangles->size(); u++) {
224 tri = (*this->triangles)[u];
225 p1 = tri->getPoint1();
226 p2 = tri->getPoint2();
227 p3 = tri->getPoint3();
231 }
232 for (int u = 0; u < (int)this->nodes->size(); u++) {
233 pA = (*this->nodes)[u];
234 if (pA == NULL)
235 throw UbException(UB_EXARGS, "sollte kein NULL pointer sein ...");
236 mapIterator = pointTriMap.find(pA);
237 tri = dynamic_cast<GbTriangle3D *>(mapIterator->second);
238 if (!tri)
239 throw UbException(UB_EXARGS, "triangle not found");
240 }
241}
242/*======================================================================*/
243void GbTriangularMesh3D::translate(const double &x1, const double &x2, const double &x3)
244{
245 GbPoint3D *pt;
246 for (int u = 0; u < (int)this->nodes->size(); u++) {
247 pt = (*nodes)[u];
248 pt->setX1(pt->getX1Coordinate() + x1);
249 pt->setX2(pt->getX2Coordinate() + x2);
250 pt->setX3(pt->getX3Coordinate() + x3);
251 }
252 this->consistent = false;
253}
254/*======================================================================*/
255void GbTriangularMesh3D::rotate(const double &alpha, const double &beta, const double &gamma)
256{
257 if (!this->consistent)
258 this->calculateValues();
259 double a1 = this->getX1Centroid();
260 double a2 = this->getX2Centroid();
261 double a3 = this->getX3Centroid();
262 CoordinateTransformation3D trafoFor(a1, a2, a3, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0);
263 CoordinateTransformation3D trafoBack(a1, a2, a3, 1.0, 1.0, 1.0, alpha, beta, gamma);
264
265 vector<GbPoint3D *> points;
266 GbPoint3D *p1 = NULL;
267 GbPoint3D *p2 = NULL;
268 GbPoint3D *p3 = NULL;
269 for (int u = 0; u < (int)this->triangles->size(); u++) {
270 p1 = (*triangles)[u]->getPoint1();
271 p2 = (*triangles)[u]->getPoint2();
272 p3 = (*triangles)[u]->getPoint3();
273 double p1x1 = trafoFor.transformForwardToX1Coordinate(p1->x1, p1->x2, p1->x3);
274 double p1x2 = trafoFor.transformForwardToX2Coordinate(p1->x1, p1->x2, p1->x3);
275 double p1x3 = trafoFor.transformForwardToX3Coordinate(p1->x1, p1->x2, p1->x3);
276 double p2x1 = trafoFor.transformForwardToX1Coordinate(p2->x1, p2->x2, p2->x3);
277 double p2x2 = trafoFor.transformForwardToX2Coordinate(p2->x1, p2->x2, p2->x3);
278 double p2x3 = trafoFor.transformForwardToX3Coordinate(p2->x1, p2->x2, p2->x3);
279 double p3x1 = trafoFor.transformForwardToX1Coordinate(p3->x1, p3->x2, p3->x3);
280 double p3x2 = trafoFor.transformForwardToX2Coordinate(p3->x1, p3->x2, p3->x3);
281 double p3x3 = trafoFor.transformForwardToX3Coordinate(p3->x1, p3->x2, p3->x3);
282 p1->x1 = trafoBack.transformBackwardToX1Coordinate(p1x1, p1x2, p1x3);
283 p1->x2 = trafoBack.transformBackwardToX2Coordinate(p1x1, p1x2, p1x3);
284 p1->x3 = trafoBack.transformBackwardToX3Coordinate(p1x1, p1x2, p1x3);
285 p2->x1 = trafoBack.transformBackwardToX1Coordinate(p2x1, p2x2, p2x3);
286 p2->x2 = trafoBack.transformBackwardToX2Coordinate(p2x1, p2x2, p2x3);
287 p2->x3 = trafoBack.transformBackwardToX3Coordinate(p2x1, p2x2, p2x3);
288 p3->x1 = trafoBack.transformBackwardToX1Coordinate(p3x1, p3x2, p3x3);
289 p3->x2 = trafoBack.transformBackwardToX2Coordinate(p3x1, p3x2, p3x3);
290 p3->x3 = trafoBack.transformBackwardToX3Coordinate(p3x1, p3x2, p3x3);
291 }
292 this->calculateValues();
293}
294/*======================================================================*/
300{
302 ss << "GbTriangularMesh3D[";
303 ss << (int)this->triangles->size() << "-Triangles, " << (int)this->nodes->size() << "-Nodes, "
304 << (int)this->edges->size() << "-Edges" << endl;
305 // ss<<"\""<<this->name<<", Area=sollt mal berechnet werden ;-)"<<"\"";
306 // ss<<", x1min="<<this->x1min;
307 // ss<<", x1max="<<this->x1max;
308 // ss<<", x2min="<<this->x2min;
309 // ss<<", x2max="<<this->x2max;
310 // ss<<", x3min="<<this->x3min;
311 // ss<<", x3max="<<this->x3max;
312 ss << "]";
313 return (ss.str());
314}
319// string GbTriangularMesh3D::getName(){ return(this->name); }
320
336{
337 if (!this->consistent)
338 this->calculateValues();
339 return (0.5 * (this->x1min + this->x1max));
340}
346{
347 if (!this->consistent)
348 this->calculateValues();
349 return (0.5 * (this->x2min + this->x2max));
350}
356{
357 if (!this->consistent)
358 this->calculateValues();
359 return (0.5 * (this->x3min + this->x3max));
360}
361
367{
368 if (!this->consistent)
369 this->calculateValues();
370 return (this->x1min);
371}
377{
378 if (!this->consistent)
379 this->calculateValues();
380 return (this->x1max);
381}
387{
388 if (!this->consistent)
389 this->calculateValues();
390 return (this->x2min);
391}
397{
398 if (!this->consistent)
399 this->calculateValues();
400 return (this->x2max);
401}
407{
408 if (!this->consistent)
409 this->calculateValues();
410 return (this->x3min);
411}
417{
418 if (!this->consistent)
419 this->calculateValues();
420 return (this->x3max);
421}
422
424{
425 double x1, x2, x3;
426
427 this->x1min = (*this->nodes)[0]->getX1Coordinate();
428 this->x1max = (*this->nodes)[0]->getX1Coordinate();
429 this->x2min = (*this->nodes)[0]->getX2Coordinate();
430 this->x2max = (*this->nodes)[0]->getX2Coordinate();
431 this->x3min = (*this->nodes)[0]->getX3Coordinate();
432 this->x3max = (*this->nodes)[0]->getX3Coordinate();
433
434 for (int i = 1; i < (int)this->nodes->size(); i++) {
435 x1 = (*this->nodes)[i]->getX1Coordinate();
436 x2 = (*this->nodes)[i]->getX2Coordinate();
437 x3 = (*this->nodes)[i]->getX3Coordinate();
438 if (x1 < this->x1min)
439 this->x1min = x1;
440 if (x1 > this->x1max)
441 this->x1max = x1;
442 if (x2 < this->x2min)
443 this->x2min = x2;
444 if (x2 > this->x2max)
445 this->x2max = x2;
446 if (x3 < this->x3min)
447 this->x3min = x3;
448 if (x3 > this->x3max)
449 this->x3max = x3;
450 }
451 this->consistent = true;
452}
453
459{
460 double area = 0.0;
461 for (int i = 0; i < (int)this->triangles->size(); i++)
462 area += (*this->triangles)[i]->getArea();
463 return (area);
464}
470{
472 GbPoint3D *p1;
473 GbPoint3D *p2;
474 GbPoint3D *p3;
475
476 double x1, x2, x3, y1, y2, y3, z1, z2, z3;
477 double G3i;
478 double volume = 0.0;
479 int size = (int)this->triangles->size();
480 for (int u = 0; u < size; u++) {
481 triangle = (*this->triangles)[u];
482 p1 = triangle->getPoint1();
483 p2 = triangle->getPoint2();
484 p3 = triangle->getPoint3();
485 x1 = p1->getX1Coordinate();
486 y1 = p1->getX2Coordinate();
487 z1 = p1->getX3Coordinate();
488 x2 = p2->getX1Coordinate();
489 y2 = p2->getX2Coordinate();
490 z2 = p2->getX3Coordinate();
491 x3 = p3->getX1Coordinate();
492 y3 = p3->getX2Coordinate();
493 z3 = p3->getX3Coordinate();
494 G3i = x1 * (y2 * z3 - z2 * y3) + y1 * (z2 * x3 - x2 * z3) + z1 * (x2 * y3 - y2 * x3);
495 volume = volume + G3i / 6.0;
496 }
497 return volume;
498}
499/*===============================================*/
501{
503 GbPoint3D *p1;
504 GbPoint3D *p2;
505 GbPoint3D *p3;
506
507 double x1, x2, x3, y1, y2, y3, z1, z2, z3;
508 double G3i;
509 double rSP1 = 0.0;
510 double rSP2 = 0.0;
511 double rSP3 = 0.0;
512 double volume = 0.0;
513 int size = (int)this->triangles->size();
514 for (int u = 0; u < size; u++) {
515 triangle = (*this->triangles)[u];
516 p1 = triangle->getPoint1();
517 p2 = triangle->getPoint2();
518 p3 = triangle->getPoint3();
519 x1 = p1->getX1Coordinate();
520 y1 = p1->getX2Coordinate();
521 z1 = p1->getX3Coordinate();
522 x2 = p2->getX1Coordinate();
523 y2 = p2->getX2Coordinate();
524 z2 = p2->getX3Coordinate();
525 x3 = p3->getX1Coordinate();
526 y3 = p3->getX2Coordinate();
527 z3 = p3->getX3Coordinate();
528 G3i = x1 * (y2 * z3 - z2 * y3) + y1 * (z2 * x3 - x2 * z3) + z1 * (x2 * y3 - y2 * x3);
529 volume = volume + G3i / 6.0;
530 rSP1 = rSP1 + G3i * (x1 + x2 + x3);
531 rSP2 = rSP2 + G3i * (y1 + y2 + y3);
532 rSP3 = rSP3 + G3i * (z1 + z2 + z3);
533 }
534 rSP1 = rSP1 / (24.0 * volume);
535 rSP2 = rSP2 / (24.0 * volume);
536 rSP3 = rSP3 / (24.0 * volume);
537
538 return { rSP1, rSP2, rSP3 };
539}
540/*===============================================*/
542{
544 GbPoint3D *p1;
545 GbPoint3D *p2;
546 GbPoint3D *p3;
547
548 double x1, x2, x3, y1, y2, y3, z1, z2, z3;
549 double G3i;
550 double xx, yy, zz, xy, yz, zx;
551 double rSP1 = 0.0;
552 double rSP2 = 0.0;
553 double rSP3 = 0.0;
554 double volume = 0.0;
555 double top11 = 0.0;
556 double top22 = 0.0;
557 double top33 = 0.0;
558 double top12 = 0.0;
559 double top23 = 0.0;
560 double top13 = 0.0;
561 int size = (int)this->triangles->size();
562 for (int u = 0; u < size; u++) {
563 triangle = (*this->triangles)[u];
564 p1 = triangle->getPoint1();
565 p2 = triangle->getPoint2();
566 p3 = triangle->getPoint3();
567 x1 = p1->getX1Coordinate();
568 y1 = p1->getX2Coordinate();
569 z1 = p1->getX3Coordinate();
570 x2 = p2->getX1Coordinate();
571 y2 = p2->getX2Coordinate();
572 z2 = p2->getX3Coordinate();
573 x3 = p3->getX1Coordinate();
574 y3 = p3->getX2Coordinate();
575 z3 = p3->getX3Coordinate();
576 G3i = x1 * (y2 * z3 - z2 * y3) + y1 * (z2 * x3 - x2 * z3) + z1 * (x2 * y3 - y2 * x3);
577 volume = volume + G3i / 6.0;
578 rSP1 = rSP1 + G3i * (x1 + x2 + x3);
579 rSP2 = rSP2 + G3i * (y1 + y2 + y3);
580 rSP3 = rSP3 + G3i * (z1 + z2 + z3);
581 }
582 rSP1 = rSP1 / (24.0 * volume);
583 rSP2 = rSP2 / (24.0 * volume);
584 rSP3 = rSP3 / (24.0 * volume);
585
586 double x1s = 0.0; // rSP1;//0.0;//
587 double x2s = 0.0; // rSP2;//0.0;//
588 double x3s = 0.0; // rSP3;//0.0;//
589
590 for (int u = 0; u < size; u++) {
591 triangle = (*this->triangles)[u];
592 p1 = triangle->getPoint1();
593 p2 = triangle->getPoint2();
594 p3 = triangle->getPoint3();
595 x1 = p1->getX1Coordinate() - x1s;
596 y1 = p1->getX2Coordinate() - x2s;
597 z1 = p1->getX3Coordinate() - x3s;
598 x2 = p2->getX1Coordinate() - x1s;
599 y2 = p2->getX2Coordinate() - x2s;
600 z2 = p2->getX3Coordinate() - x3s;
601 x3 = p3->getX1Coordinate() - x1s;
602 y3 = p3->getX2Coordinate() - x2s;
603 z3 = p3->getX3Coordinate() - x3s;
604 G3i = x1 * (y2 * z3 - z2 * y3) + y1 * (z2 * x3 - x2 * z3) + z1 * (x2 * y3 - y2 * x3);
605 // rSP1 = rSP1+G3i*(x1+x2+x3)/(24.0*volume);
606 // rSP2 = rSP2+G3i*(y1+y2+y3)/(24.0*volume);
607 // rSP3 = rSP3+G3i*(z1+z2+z3)/(24.0*volume);
608 xx = x1 * x1 + x2 * x2 + x3 * x3 + x1 * x2 + x2 * x3 + x3 * x1;
609 yy = y1 * y1 + y2 * y2 + y3 * y3 + y1 * y2 + y2 * y3 + y3 * y1;
610 zz = z1 * z1 + z2 * z2 + z3 * z3 + z1 * z2 + z2 * z3 + z3 * z1;
611 top11 = top11 + (yy + zz) * rhoP * G3i / 60.;
612 top22 = top22 + (xx + zz) * rhoP * G3i / 60.;
613 top33 = top33 + (yy + xx) * rhoP * G3i / 60.;
614 xy = 2.0 * (x1 * y1 + x2 * y2 + x3 * y3) + x2 * y3 + x3 * y1 + x1 * y2 + x3 * y2 + x1 * y3 + x2 * y1;
615 yz = 2.0 * (y1 * z1 + y2 * z2 + y3 * z3) + y2 * z3 + y3 * z1 + y1 * z2 + y3 * z2 + y1 * z3 + y2 * z1;
616 zx = 2.0 * (z1 * x1 + z2 * x2 + z3 * x3) + z2 * x3 + z3 * x1 + z1 * x2 + z3 * x2 + z1 * x3 + z2 * x1;
617 top12 = top12 - xy * rhoP * G3i / 120.;
618 top23 = top23 - yz * rhoP * G3i / 120.;
619 top13 = top13 - zx * rhoP * G3i / 120.;
620 }
621 // Satz von Steiner ...
622 top11 = top11 - rhoP * volume * (rSP2 * rSP2 + rSP3 + rSP3);
623 top22 = top22 - rhoP * volume * (rSP3 * rSP3 + rSP1 * rSP1);
624 top33 = top33 - rhoP * volume * (rSP1 * rSP1 + rSP2 * rSP2);
625 top12 = top12 + rhoP * volume * rSP1 * rSP2;
626 top23 = top23 + rhoP * volume * rSP2 * rSP3;
627 top13 = top13 + rhoP * volume * rSP3 * rSP1;
628
629 cout << "Volume:" << volume << "\n Traegheitsmomente:\n";
630 cout << " top11:" << top11 << " top22:" << top22 << " top33:" << top33 << endl;
631 cout << " top12:" << top12 << " top23:" << top23 << " top13:" << top13 << endl;
632
633 return { top11, top22, top33, top12, top23, top13 };
634}
635
645double GbTriangularMesh3D::getVolumeForRectangle(const double & /*p1x1*/, const double & /*p1x2*/,
646 const double & /*p2x1*/, const double & /*p2x2*/)
647{
648 throw UbException(UB_EXARGS, "not yet implemented");
649 // GbPolygon2D polygon;
650 // double volume = 0.0;
651 // double area1 = Math.abs((p1x1-p2x1)*(p1x2-p2x2));
652 // double area2 = 0.0;
653 // double t1min, t1max, t2min, t2max;
654 // double x1, x2;
655 // boolean f = false;
656
657 // for(int i=0; i<this.triangles.length; i++)
658 // {
659 // t1min = this.triangles[i].getX1Minimum();
660 // t1max = this.triangles[i].getX1Maximum();
661 // if(GbSystem.less2(t1min, t1max, p1x1, p2x1)) continue;
662 // if(GbSystem.greater2(t1min, t1max, p1x1, p2x1)) continue;
663
664 // t2min = this.triangles[i].getX2Minimum();
665 // t2max = this.triangles[i].getX2Maximum();
666 // if(GbSystem.less2(t2min, t2max, p1x2, p2x2)) continue;
667 // if(GbSystem.greater2(t2min, t2max, p1x2, p2x2)) continue;
668
669 // if(GbSystem.inOpenInterval(t1min, p1x1, p2x1) && GbSystem.inOpenInterval(t1max, p1x1, p2x1) &&
670 // GbSystem.inOpenInterval(t2min, p1x2, p2x2) && GbSystem.inOpenInterval(t2max, p1x2, p2x2))
671 // {
672 // volume += this.triangles[i].getVolume();
673 // area2 += this.triangles[i].getArea();
674 // f = true;
675 // }
676 // else
677 // {
678 // polygon = this.triangles[i].createClippedPolygon3D(p1x1, p1x2, p2x1, p2x2);
679
680 // if(polygon != null && polygon.size() > 2)
681 // {
682 // try
683 // {
684 // x1 = polygon.getX1Centroid();
685 // x2 = polygon.getX2Centroid();
686 // volume += this.triangles[i].getX3Coordinate(x1, x2) * Math.abs(polygon.getArea());
687 // area2 += Math.abs(polygon.getArea());
688 // f = true;
689 // }
690 // catch(Exception e){}
691 // }
692 // }
693 // if(GbSystem.greaterEqual(area2, area1)) break;
694 //}
695 // if(f) return(volume);
696 // else throw new NullPointerException();
697}
698
707vector<GbTriangle3D *> *GbTriangularMesh3D::getTrianglesForRectangle(const double & /*p1x1*/, const double & /*p1x2*/,
708 const double & /*p2x1*/, const double & /*p2x2*/)
709{
710 throw UbException(UB_EXARGS, "not yet implemented");
711 // QbList triangleList = new QbList(GbTriangle3D.class);
712 // GbPolygon2D polygon;
713 // double t1min, t1max, t2min, t2max;
714 // double area1 = Math.abs((p1x1-p2x1)*(p1x2-p2x2));
715 // double area2 = 0.0;
716
717 // for(int i=0; i<this.triangles.length; i++)
718 // {
719 // t1min = this.triangles[i].getX1Minimum();
720 // t1max = this.triangles[i].getX1Maximum();
721 // if(GbSystem.less2(t1min, t1max, p1x1, p2x1)) continue;
722 // if(GbSystem.greater2(t1min, t1max, p1x1, p2x1)) continue;
723
724 // t2min = this.triangles[i].getX2Minimum();
725 // t2max = this.triangles[i].getX2Maximum();
726 // if(GbSystem.less2(t2min, t2max, p1x2, p2x2)) continue;
727 // if(GbSystem.greater2(t2min, t2max, p1x2, p2x2)) continue;
728
729 // if(GbSystem.inOpenInterval(t1min, p1x1, p2x1) && GbSystem.inOpenInterval(t1max, p1x1, p2x1) &&
730 // GbSystem.inOpenInterval(t2min, p1x2, p2x2) && GbSystem.inOpenInterval(t2max, p1x2, p2x2))
731 // {
732 // try { triangleList.append(this.triangles[i]); } catch(Exception e){}
733 // area2 += this.triangles[i].getArea();
734 // }
735 // else
736 // {
737 // polygon = this.triangles[i].createClippedPolygon3D(p1x1, p1x2, p2x1, p2x2);
738 // if(polygon != null && polygon.size() > 2)
739 // {
740 // try { triangleList.append(this.triangles[i]); } catch(Exception e){}
741 // area2 += Math.abs(polygon.getArea());
742 // }
743 // }
744 // if(GbSystem.greaterEqual(area2, area1)) break;
745 //}
746 // return((GbTriangle3D[])triangleList.getObjectArray());
747}
756vector<GbPoint3D *> *GbTriangularMesh3D::getNodesForRectangle(const double & /*p1x1*/, const double & /*p1x2*/,
757 const double & /*p2x1*/, const double & /*p2x2*/)
758{
759 throw UbException(UB_EXARGS, "not implemented");
760 // QbList nodeList = new QbList(GbPoint3D.class);
761
762 // for(int i=0; i<this.edges.length; i++)
763 // {
764 // if(GbSystem.inClosedInterval(this.nodes[i].getX1Coordinate(), p1x1, p2x1) &&
765 // GbSystem.inClosedInterval(this.nodes[i].getX2Coordinate(), p1x2, p2x2))
766 //{
767 // try { nodeList.append(this.nodes[i]); } catch(Exception e){}
768 //}
769 // }
770 // return((GbPoint3D[])nodeList.getObjectArray());
771}
772
783double GbTriangularMesh3D::getX3RangeForRectangle(const double & /*p1x1*/, const double & /*p1x2*/,
784 const double & /*p2x1*/, const double & /*p2x2*/)
785{
786 throw UbException(UB_EXARGS, "not implemented");
787 // GbPolygon3D polygon;
788 // boolean f = false;
789 // double x3min = 0.0;
790 // double x3max = 0.0;
791 // double t1min, t1max, t2min, t2max;
792 // double area1 = Math.abs((p1x1-p2x1)*(p1x2-p2x2));
793 // double area2 = 0.0;
794
795 // for(int i=0; i<this.triangles.length; i++)
796 // {
797 // t1min = this.triangles[i].getX1Minimum();
798 // t1max = this.triangles[i].getX1Maximum();
799 // if(GbSystem.less2(t1min, t1max, p1x1, p2x1)) continue;
800 // if(GbSystem.greater2(t1min, t1max, p1x1, p2x1)) continue;
801
802 // t2min = this.triangles[i].getX2Minimum();
803 // t2max = this.triangles[i].getX2Maximum();
804 // if(GbSystem.less2(t2min, t2max, p1x2, p2x2)) continue;
805 // if(GbSystem.greater2(t2min, t2max, p1x2, p2x2)) continue;
806
807 // if(GbSystem.inOpenInterval(t1min, p1x1, p2x1) && GbSystem.inOpenInterval(t1max, p1x1, p2x1) &&
808 // GbSystem.inOpenInterval(t2min, p1x2, p2x2) && GbSystem.inOpenInterval(t2max, p1x2, p2x2))
809 // {
810 // if(f)
811 // {
812 // if(this.triangles[i].getX3Minimum() < x3min) x3min = this.triangles[i].getX3Minimum();
813 // if(this.triangles[i].getX3Maximum() > x3max) x3max = this.triangles[i].getX3Maximum();
814 // }
815 // else
816 // {
817 // x3min = this.triangles[i].getX3Minimum();
818 // x3max = this.triangles[i].getX3Maximum();
819 // f = true;
820 // }
821 // area2 += this.triangles[i].getArea();
822 //}
823 // else
824 // {
825 // polygon = this.triangles[i].createClippedPolygon3D(p1x1, p1x2, p2x1, p2x2);
826
827 // if(polygon != null && polygon.size() > 2)
828 // {
829 // if(f)
830 // {
831 // if(polygon.getX3Minimum() < x3min) x3min = polygon.getX3Minimum();
832 // if(polygon.getX3Maximum() > x3max) x3max = polygon.getX3Maximum();
833 // }
834 // else
835 // {
836 // x3min = polygon.getX3Minimum();
837 // x3max = polygon.getX3Maximum();
838 // f = true;
839 // }
840 // area2 += Math.abs(polygon.getArea());
841 // }
842 // }
843 // if(GbSystem.greaterEqual(area2, area1)) break;
844 // }
845 // if(f) return(x3max-x3min);
846 // else throw new NullPointerException();
847}
857double GbTriangularMesh3D::getX3MinimumForRectangle(const double & /*p1x1*/, const double & /*p1x2*/,
858 const double & /*p2x1*/, const double & /*p2x2*/)
859{
860 throw UbException(UB_EXARGS, "not implemented");
861 // GbPolygon3D polygon;
862 // boolean f = false;
863 // double x3min = 0.0;
864 // double t1min, t1max, t2min, t2max;
865 // double area1 = Math.abs((p1x1-p2x1)*(p1x2-p2x2));
866 // double area2 = 0.0;
867
868 // for(int i=0; i<this.triangles.length; i++)
869 // {
870 // t1min = this.triangles[i].getX1Minimum();
871 // t1max = this.triangles[i].getX1Maximum();
872 // if(GbSystem.less2(t1min, t1max, p1x1, p2x1)) continue;
873 // if(GbSystem.greater2(t1min, t1max, p1x1, p2x1)) continue;
874
875 // t2min = this.triangles[i].getX2Minimum();
876 // t2max = this.triangles[i].getX2Maximum();
877 // if(GbSystem.less2(t2min, t2max, p1x2, p2x2)) continue;
878 // if(GbSystem.greater2(t2min, t2max, p1x2, p2x2)) continue;
879
880 // if(GbSystem.inOpenInterval(t1min, p1x1, p2x1) && GbSystem.inOpenInterval(t1max, p1x1, p2x1) &&
881 // GbSystem.inOpenInterval(t2min, p1x2, p2x2) && GbSystem.inOpenInterval(t2max, p1x2, p2x2))
882 //{
883 // if(f)
884 // {
885 // if(this.triangles[i].getX3Minimum() < x3min) x3min = this.triangles[i].getX3Minimum();
886 // }
887 // else
888 // {
889 // x3min = this.triangles[i].getX3Minimum();
890 // f = true;
891 // }
892 // area2 += this.triangles[i].getArea();
893 //}
894 // else
895 //{
896 // polygon = this.triangles[i].createClippedPolygon3D(p1x1, p1x2, p2x1, p2x2);
897
898 // if(polygon != null && polygon.size() > 2)
899 // {
900 // if(f)
901 // {
902 // if(polygon.getX3Minimum() < x3min) x3min = polygon.getX3Minimum();
903 // }
904 // else
905 // {
906 // x3min = polygon.getX3Minimum();
907 // f = true;
908 // }
909 // area2 += Math.abs(polygon.getArea());
910 // }
911 //}
912 // if(GbSystem.greaterEqual(area2, area1)) break;
913 // }
914 // if(f) return(x3min);
915 // else throw new NullPointerException();
916}
926double GbTriangularMesh3D::getX3MaximumForRectangle(const double & /*p1x1*/, const double & /*p1x2*/,
927 const double & /*p2x1*/, const double & /*p2x2*/)
928{
929 throw UbException(UB_EXARGS, "not implemented");
930 // GbPolygon3D polygon;
931 // boolean f = false;
932 // double x3max = 0.0;
933 // double t1min, t1max, t2min, t2max;
934 // double area1 = Math.abs((p1x1-p2x1)*(p1x2-p2x2));
935 // double area2 = 0.0;
936
937 // for(int i=0; i<this.triangles.length; i++)
938 // {
939 // t1min = this.triangles[i].getX1Minimum();
940 // t1max = this.triangles[i].getX1Maximum();
941 // if(GbSystem.less2(t1min, t1max, p1x1, p2x1)) continue;
942 // if(GbSystem.greater2(t1min, t1max, p1x1, p2x1)) continue;
943
944 // t2min = this.triangles[i].getX2Minimum();
945 // t2max = this.triangles[i].getX2Maximum();
946 // if(GbSystem.less2(t2min, t2max, p1x2, p2x2)) continue;
947 // if(GbSystem.greater2(t2min, t2max, p1x2, p2x2)) continue;
948
949 // if(GbSystem.inOpenInterval(t1min, p1x1, p2x1) && GbSystem.inOpenInterval(t1max, p1x1, p2x1) &&
950 // GbSystem.inOpenInterval(t2min, p1x2, p2x2) && GbSystem.inOpenInterval(t2max, p1x2, p2x2))
951 //{
952 // if(f)
953 // {
954 // if(this.triangles[i].getX3Maximum() < x3max) x3max = this.triangles[i].getX3Maximum();
955 // }
956 // else
957 // {
958 // x3max = this.triangles[i].getX3Maximum();
959 // f = true;
960 // }
961 // area2 += this.triangles[i].getArea();
962 //}
963 // else
964 //{
965 // polygon = this.triangles[i].createClippedPolygon3D(p1x1, p1x2, p2x1, p2x2);
966
967 // if(polygon != null && polygon.size() > 2)
968 // {
969 // if(f)
970 // {
971 // if(polygon.getX3Maximum() < x3max) x3max = polygon.getX3Maximum();
972 // }
973 // else
974 // {
975 // x3max = polygon.getX3Maximum();
976 // f = true;
977 // }
978 // area2 += Math.abs(polygon.getArea());
979 // }
980 //}
981 // if(GbSystem.greaterEqual(area2, area1)) break;
982 // }
983 // if(f) return(x3max);
984 // else throw new NullPointerException();
985}
986/*======================================================================*/
988{
991 GbPoint3D *p1;
992 GbPoint3D *p2;
993 GbPoint3D *p3;
994 int size = (int)this->triangles->size();
995 for (int u = 0; u < size; u++) {
996 triangle = (*this->triangles)[u];
997 p1 = new GbPoint3D(triangle->getPoint1());
998 p2 = new GbPoint3D(triangle->getPoint2());
999 p3 = new GbPoint3D(triangle->getPoint3());
1000 tris.push_back(new GbTriangle3D(p1, p2, p3));
1001 }
1002 return tris;
1003}
1004/*======================================================================*/
1005/*
1006 * Function to determine if the point is inside the polyhedron defined as a 3D object
1007 * using the Halfspace algorithm
1008 * @param xp the x-coordinate of the point
1009 * @param yp the y-coordinate of the point
1010 * @param zp the z-coordinate of the point
1011 * @return true if point is inside else return false
1012 */
1013bool GbTriangularMesh3D::isPointInObject3DHalfSpace(const double &xp, const double &yp, const double &zp)
1014{
1016 int Trianglesize = (int)Triangles->size();
1017 // GbPoint3D Point(xp,yp,zp);
1018 for (int i = 0; i < Trianglesize; i++) {
1019 // GbPoint3D* point1 = (*Triangles)[i]->getPoint1();
1020 // GbPoint3D* point2 = (*Triangles)[i]->getPoint2();
1021 // GbPoint3D* point3 = (*Triangles)[i]->getPoint3();
1022
1023 // GbHalfSpace3D halfspace(point1, point2, point3);
1025 if (halfspace.ptInside(xp, yp, zp))
1026 return false;
1027 }
1028 return true;
1029}
1030/*======================================================================*/
1031bool GbTriangularMesh3D::isPointInObject3DRayCrossing(const double &xp, const double &yp, const double &zp, int radius,
1032 int /*numVertices*/, int numTriangles)
1033{
1034 GbVector3D point(xp, yp, zp);
1035
1036 if (this->InPolyhedron(numTriangles, point, radius))
1037 return true;
1038 else
1039 return false;
1040}
1041/*======================================================================*/
1042bool GbTriangularMesh3D::isPointInGbObject3D(const double &x1, const double &x2, const double &x3)
1043{
1044 double xmin = this->getX1Minimum();
1045 double xmax = this->getX1Maximum();
1046 double ymin = this->getX2Minimum();
1047 double ymax = this->getX2Maximum();
1048 double zmin = this->getX3Minimum();
1049 double zmax = this->getX3Maximum();
1050 double dX = (xmax - xmin) / 100.;
1051 double dY = (ymax - ymin) / 100.;
1052 double dZ = (zmax - zmin) / 100.;
1054 if (!boundingCube.isPointInGbObject3D(x1, x2, x3)) {
1055 boundingCube.finalize();
1056 return false;
1057 }
1058
1059 // Halfspace algorithm, Area of spherical polygons algorithm or Ray crossing algorithm
1060 GbVector3D bMin(boundingCube.getPoint1());
1061 GbVector3D bMax(boundingCube.getPoint2());
1062
1063 boundingCube.finalize();
1064
1065 bMin = bMax.Subtract(bMin);
1066 int radius = (int)(bMin.Length() + 1) + 1;
1067
1068 if (this->pointinobjecttest == HALFSPACE)
1069 return this->isPointInObject3DHalfSpace(x1, x2, x3);
1070 else if (this->pointinobjecttest == RAYCROSSING)
1071 return this->isPointInObject3DRayCrossing(x1, x2, x3, radius, (int)this->nodes->size(),
1072 (int)this->triangles->size());
1073 else
1074 throw UbException(UB_EXARGS, "no ptInObjTest");
1075}
1076/*======================================================================*/
1077bool GbTriangularMesh3D::isPointInGbObject3D(const double & /*x1*/, const double & /*x2*/, const double & /*x3*/,
1078 bool & /*pointIsOnBoundary*/)
1079{
1080 throw UbException(UB_EXARGS, "not implemented");
1081}
1082/*======================================================================*/
1084{
1085 throw UbException(UB_EXARGS, "not implemented");
1086}
1087/*======================================================================*/
1088void GbTriangularMesh3D::writeMesh(string filename, WbWriter *writer, bool writeNormals)
1089{
1092
1093 for (size_t i = 0; i < triangles->size(); i++) {
1094 GbTriangle3D &tri = *((*triangles)[i]);
1095 GbPoint3D &node1 = *tri.getPoint(0);
1096 GbPoint3D &node2 = *tri.getPoint(1);
1097 GbPoint3D &node3 = *tri.getPoint(2);
1098
1099 nodes[i * 3] =
1100 makeUbTuple((float)node1.getX1Coordinate(), (float)node1.getX2Coordinate(), (float)node1.getX3Coordinate());
1101 nodes[i * 3 + 1] =
1102 makeUbTuple((float)node2.getX1Coordinate(), (float)node2.getX2Coordinate(), (float)node2.getX3Coordinate());
1103 nodes[i * 3 + 2] =
1104 makeUbTuple((float)node3.getX1Coordinate(), (float)node3.getX2Coordinate(), (float)node3.getX3Coordinate());
1105
1106 tris[i] = makeUbTuple((int)i * 3, (int)i * 3 + 1, (int)i * 3 + 2);
1107 }
1108 writer->writeTriangles(filename, nodes, tris);
1109
1110 if (writeNormals) {
1112 vector<UbTupleInt2> lines(triangles->size());
1113 for (size_t i = 0; i < triangles->size(); i++) {
1114 GbVector3D vec = (*triangles)[i]->getNormal();
1115 lineNodes[i * 2] =
1116 makeUbTuple((float)(*triangles)[i]->getX1Centroid(), (float)(*triangles)[i]->getX2Centroid(),
1117 (float)(*triangles)[i]->getX3Centroid());
1118 lineNodes[i * 2 + 1] = makeUbTuple((float)((*triangles)[i]->getX1Centroid() + vec.X1()),
1119 (float)((*triangles)[i]->getX2Centroid() + vec.X2()),
1120 (float)((*triangles)[i]->getX3Centroid() + vec.X3()));
1121
1122 lines[i] = makeUbTuple((int)i * 2, (int)i * 2 + 1);
1123 }
1124 writer->writeLines(filename + "_normals", lineNodes, lines);
1125 }
1126}
1127
1128/*======================================================================*/
1129/*
1130This function returns a char:
1131'V': the query point a coincides with a Vertex of polyhedron P.
1132'E': the query point a is in the relative interior of an Edge of polyhedron P.
1133'F': the query point a is in the relative interior of a Face of polyhedron P.
1134'i': the query point a is strictly interior to polyhedron P.
1135'o': the query point a is strictly exterior to( or outside of) polyhedron P.
1136*/
1138{
1139 GbVector3D r; /* Ray endpoint. */
1140 GbVector3D p; /* Intersection point; not used. */
1141 int f, k = 0, crossings = 0;
1142 char code = '?';
1143
1144 while (k++ < F) {
1145 crossings = 0;
1146
1147 RandomRay(r, radius);
1148 r = q.Add(r);
1149 // printf("Ray endpoint: (%d,%d,%d)\n", r[0],r[1],r[2] );
1150
1151 for (f = 0; f < F; f++) /* Begin check each face */
1152 {
1153 if (BoxTest((*this->triangles)[f], q, r) == false)
1154 code = '0'; // printf("BoxTest = 0!\n");
1155 else
1156 code = SegTriInt((*this->triangles)[f], q, r,
1157 p); // printf( "Face = %d: BoxTest/SegTriInt returns %c\n\n", f, code );
1158
1159 /* If ray is degenerate, then goto outer while to generate another. */
1160 if (code == 'p' || code == 'v' || code == 'e')
1161 break; // goto LOOP; //printf("Degenerate ray\n");
1162 /* If ray hits face at interior point, increment crossings. */
1163 else if (code == 'f')
1164 crossings++; // printf( "crossings = %d\n", crossings );
1165 /* If query endpoint q sits on a V/E/F, return that code. */
1166 else if (code == 'V' || code == 'E' || code == 'F')
1167 return true;
1168 /* If ray misses triangle, do nothing. */
1169 else if (code == '0') { /*nothing to do*/
1170 } else
1171 throw UbException(UB_EXARGS, "Error");
1172 } /* End check each face */
1173
1174 /* No degeneracies encountered: ray is generic, so finished. */
1175 if (f >= F)
1176 break;
1177 } /* End while loop */
1178
1179 // printf( "Crossings = %d\n", crossings );
1180 /* q strictly interior to polyhedron iff an odd number of crossings. */
1181 if ((crossings % 2) == 1)
1182 return true;
1183
1184 return false;
1185}
1186
1187/* Return a random ray endpoint */
1189{
1190 double x, y, z, w, t;
1191
1192 double MAX_INT = 2147483647;
1193 /* Generate a random point on a sphere of radius 1. */
1194 /* the sphere is sliced at z, and a random point at angle t
1195 generated on the circle of intersection. */
1196 z = 2.0 * (double)rand() / MAX_INT - 1.0;
1197 t = 2.0 * ub_math::PI * (double)rand() / MAX_INT;
1198 w = sqrt(1 - z * z);
1199 x = w * cos(t);
1200 y = w * sin(t);
1201
1202 ray[0] = radius * x;
1203 ray[1] = radius * y;
1204 ray[2] = radius * z;
1205
1206 /*printf( "RandomRay returns %6d %6d %6d\n", ray[X], ray[Y], ray[Z] );*/
1207}
1208
1209/*---------------------------------------------------------------------
1210'p': The segment lies wholly within the plane.
1211'q': The q endpoint is on the plane (but not 'p').
1212'r': The r endpoint is on the plane (but not 'p').
1213'0': The segment lies strictly to one side or the other of the plane.
1214'1': The segement intersects the plane, and 'p' does not hold.
1215---------------------------------------------------------------------*/
1217{
1218 // cout<<"SegPlaneInt..\n";
1219 GbVector3D N;
1220 double D;
1221 GbVector3D rq;
1222 double num, denom, t;
1223 int i;
1224
1225 *m = PlaneCoeff(T, N, &D);
1226 /*printf("m=%d; plane=(%lf,%lf,%lf,%lf)\n", m, N[X],N[Y],N[Z],D);*/
1227 num = D - q.Dot(N);
1228 rq = r.Subtract(q);
1229 denom = rq.Dot(N);
1230 /*printf("SegPlaneInt: num=%lf, denom=%lf\n", num, denom );*/
1231
1232 if (denom == 0.0) { /* Segment is parallel to plane. */
1233 if (num == 0.0) /* q is on plane. */
1234 // if (ub_math::zero(denom)) { /* Segment is parallel to plane. */
1235 // if ( ub_math::zero(num)) /* q is on plane. */
1236 return 'p';
1237 else
1238 return '0';
1239 } else
1240 t = num / denom;
1241 /*printf("SegPlaneInt: t=%lf \n", t );*/
1242
1243 for (i = 0; i < 3; i++)
1244 p[i] = q[i] + t * (r[i] - q[i]);
1245
1246 if ((0.0 < t) && (t < 1.0))
1247 return '1';
1248 else if (num == 0.0) /* t == 0 */
1249 return 'q';
1250 else if (num == denom) /* t == 1 */
1251 return 'r';
1252 else
1253 return '0';
1254
1255 // if ( (0.0 < t) && (t < 1.0) )
1256 // return '1';
1257 // else if ( ub_math::zero(num)) /* t == 0 */
1258 // return 'q';
1259 // else if ( ub_math::equal(num , denom) ) /* t == 1 */
1260 // return 'r';
1261 // else return '0';
1262}
1263/*---------------------------------------------------------------------
1264Computes N & D and returns index m of largest component.
1265---------------------------------------------------------------------*/
1267{
1268 int i;
1269 double t; /* Temp storage */
1270 double biggest = 0.0; /* Largest component of normal vector. */
1271 int m = 0; /* Index of largest component. */
1272
1273 N = T->getNormal();
1274 /*printf("PlaneCoeff: N=(%lf,%lf,%lf)\n", N[X],N[Y],N[Z]);*/
1275 GbVector3D a(T->getPoint1());
1276
1277 *D = a.Dot(N);
1278
1279 /* Find the largest component of N. */
1280 for (i = 0; i < 3; i++) {
1281 t = std::fabs(N[i]);
1282 if (t > biggest) {
1283 biggest = t;
1284 m = i;
1285 }
1286 }
1287 return m;
1288}
1289
1290/* Assumption: p lies in the plane containing T.
1291Returns a char:
1292'V': the query point p coincides with a Vertex of triangle T.
1293'E': the query point p is in the relative interior of an Edge of triangle T.
1294'F': the query point p is in the relative interior of a Face of triangle T.
1295'0': the query point p does not intersect (misses) triangle T.
1296*/
1297
1299{
1300 // int i; /* Index for X,Y,Z */
1301 int j; /* Index for X,Y */
1302 // int k; /* Index for triangle vertex */
1303 GbVector3D pp; /* projected p */
1304 GbVector3D Tp[3]; /* projected T: three new vertices */
1305
1306 /* Project out coordinate m in both p and the triangular face */
1307 // j = 0;
1308 // for ( i = 0; i < 3; i++ ) {
1309 // if ( i != m ) { /* skip largest coordinate */
1310 // pp[j] = p[i];
1311 // //for ( k = 0; k < 3; k++ )
1312 // std::cout<<"aachtung###############################################";
1313 // // Tp[k][j] = Vertices[T[k]][i];
1314 // j++;
1315 // }
1316 //}
1317 j = 0;
1318 if (m != 0) {
1319 Tp[0][j] = T->getPoint1()->getX1Coordinate();
1320 Tp[1][j] = T->getPoint2()->getX1Coordinate();
1321 Tp[2][j] = T->getPoint3()->getX1Coordinate();
1322 j++;
1323 }
1324 if (m != 1) {
1325 Tp[0][j] = T->getPoint1()->getX2Coordinate();
1326 Tp[1][j] = T->getPoint2()->getX2Coordinate();
1327 Tp[2][j] = T->getPoint3()->getX2Coordinate();
1328 j++;
1329 }
1330 if (m != 2) {
1331 Tp[0][j] = T->getPoint1()->getX3Coordinate();
1332 Tp[1][j] = T->getPoint2()->getX3Coordinate();
1333 Tp[2][j] = T->getPoint3()->getX3Coordinate();
1334 j++;
1335 }
1336
1337 return (InTri2D(Tp, pp));
1338}
1339
1341{
1342 double area0, area1, area2;
1343
1344 /* compute three AreaSign() values for pp w.r.t. each edge of the face in 2D */
1345 area0 = AreaSign(pp, Tp[0], Tp[1]);
1346 area1 = AreaSign(pp, Tp[1], Tp[2]);
1347 area2 = AreaSign(pp, Tp[2], Tp[0]);
1348 // printf("area0=%f area1=%f area2=%f\n",area0,area1,area2);
1349
1350 if (((area0 == 0.) && (area1 > 0.) && (area2 > 0.)) || ((area1 == 0.) && (area0 > 0.) && (area2 > 0.)) ||
1351 ((area2 == 0.) && (area0 > 0.) && (area1 > 0.)))
1352 return 'E';
1353
1354 if (((area0 == 0.) && (area1 < 0.) && (area2 < 0.)) || ((area1 == 0.) && (area0 < 0.) && (area2 < 0.)) ||
1355 ((area2 == 0.) && (area0 < 0.) && (area1 < 0.)))
1356 return 'E';
1357
1358 if (((area0 > 0.) && (area1 > 0.) && (area2 > 0.)) || ((area0 < 0.) && (area1 < 0.) && (area2 < 0.)))
1359 return 'F';
1360
1361 if ((area0 == 0.0) && (area1 == 0.0) && (area2 == 0.0))
1362 fprintf(stderr, "Error in InTriD\n"), exit(EXIT_FAILURE);
1363
1364 if (((area0 == 0.) && (area1 == 0.)) || ((area0 == 0.) && (area2 == 0.)) || ((area1 == 0.) && (area2 == 0.)))
1365 return 'V';
1366
1367 else
1368 return '0';
1369}
1370
1372{
1373 double area2;
1374
1375 area2 = (b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1]);
1376
1377 return area2;
1378 /* The area should be an integer. */
1379 // FIXME: unrechable code
1380 // if ( area2 > 0.5 ) return 1;
1381 // else if ( area2 < -0.5 ) return -1;
1382 // else return 0;
1383}
1384
1386{
1387 int code = '?';
1388 int m = -1;
1389
1390 code = (unsigned char)SegPlaneInt(T, q, r, p, &m);
1391 // printf("SegPlaneInt code=%c, m=%d; p=(%lf,%lf,%lf)\n", code,m,p[0],p[1],p[2]);
1392
1393 if (code == '0')
1394 return '0';
1395 else if (code == 'q')
1396 return InTri3D(T, m, q);
1397 else if (code == 'r')
1398 return InTri3D(T, m, r);
1399 else if (code == 'p')
1400 return InPlane(T, m, q, r, p);
1401 else if (code == '1')
1402 return SegTriCross(T, q, r);
1403 else /* Error */
1404 return code;
1405}
1406
1407char GbTriangularMesh3D::InPlane(GbTriangle3D * /*T*/, int /*m*/, GbVector3D & /*q*/, GbVector3D & /*r*/,
1408 GbVector3D & /*p*/)
1409{
1410 // cout<<"inplane\n";
1411 /* NOT IMPLEMENTED */
1412 return 'p';
1413}
1414
1415/*---------------------------------------------------------------------
1416The signed volumes of three tetrahedra are computed, determined
1417by the segment qr, and each edge of the triangle.
1418Returns a char:
1419'v': the open segment includes a vertex of T.
1420'e': the open segment includes a point in the relative interior of an edge
1421of T.
1422'f': the open segment includes a point in the relative interior of a face
1423of T.
1424'0': the open segment does not intersect triangle T.
1425---------------------------------------------------------------------*/
1426
1428{
1429 // cout<<"SegTriCross\n";
1430 double vol0, vol1, vol2;
1431 GbVector3D vert0(T->getPoint1());
1432 GbVector3D vert1(T->getPoint2());
1433 GbVector3D vert2(T->getPoint3());
1434
1435 vol0 = VolumeSign(q, vert0, vert1, r);
1436 vol1 = VolumeSign(q, vert1, vert2, r);
1437 vol2 = VolumeSign(q, vert2, vert0, r);
1438
1439 // printf( "SegTriCross: vol0 = %d; vol1 = %d; vol2 = %d\n", vol0, vol1, vol2 );
1440
1441 /* Same sign: segment intersects interior of triangle. */
1442 if (((vol0 > 0.) && (vol1 > 0.) && (vol2 > 0.)) || ((vol0 < 0.) && (vol1 < 0.) && (vol2 < 0.)))
1443 // if ( ( ub_math::greater(vol0, 0. ) && ub_math::greater(vol1 , 0. ) && ub_math::greater(vol2 , 0. ) ) ||
1444 // ( ub_math::less(vol0, 0. ) && ub_math::less(vol1, 0. ) && ub_math::less(vol2, 0. ) ) )
1445 {
1446 return 'f';
1447 }
1448
1449 /* Opposite sign: no intersection between segment and triangle */
1450 if (((vol0 > 0.) || (vol1 > 0.) || (vol2 > 0.)) && ((vol0 < 0.) || (vol1 < 0.) || (vol2 < 0.))) {
1451 return '0';
1452 } else if ((vol0 == 0.0) && (vol1 == 0.0) && (vol2 == 0.0)) {
1453 std::cout << vol0 << " " << vol1 << " " << vol2 << std::endl;
1454 fprintf(stderr, "Error 1 in SegTriCross\n"), exit(EXIT_FAILURE);
1455 }
1456
1457 /* Two zeros: segment intersects vertex. */
1458 else if (((vol0 == 0.) && (vol1 == 0.)) || ((vol0 == 0.) && (vol2 == 0.)) || ((vol1 == 0.) && (vol2 == 0.))) {
1459 return 'v';
1460 }
1461
1462 /* One zero: segment intersects edge. */
1463 else if ((vol0 == 0.) || (vol1 == 0.) || (vol2 == 0.)) {
1464 return 'e';
1465 }
1466
1467 throw UbException(UB_EXARGS, "fprintf( stderr, Error 2 in SegTriCross\n ), exit(EXIT_FAILURE);");
1468}
1469
1471{
1472 double vol;
1473 double ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz;
1474 double bxdx, bydy, bzdz, cxdx, cydy, czdz;
1475
1476 ax = a[0];
1477 ay = a[1];
1478 az = a[2];
1479 bx = b[0];
1480 by = b[1];
1481 bz = b[2];
1482 cx = c[0];
1483 cy = c[1];
1484 cz = c[2];
1485 dx = d[0];
1486 dy = d[1];
1487 dz = d[2];
1488
1489 bxdx = bx - dx;
1490 bydy = by - dy;
1491 bzdz = bz - dz;
1492 cxdx = cx - dx;
1493 cydy = cy - dy;
1494 czdz = cz - dz;
1495 vol = (az - dz) * (bxdx * cydy - bydy * cxdx) + (ay - dy) * (bzdz * cxdx - bxdx * czdz) +
1496 (ax - dx) * (bydy * czdz - bzdz * cydy);
1497
1498 // std::cout<< vol<<std::endl;
1499 return vol;
1500
1501 /* The volume should be an integer. */
1502 // FIXME: unreachable code
1503 // if ( vol > 0.5 ) return 1;
1504 // else if ( vol < -0.5 ) return -1;
1505 // else return 0;
1506}
1507
1509{
1510 double minX1 = triangle->getX1Minimum();
1511 double minX2 = triangle->getX2Minimum();
1512 double minX3 = triangle->getX3Minimum();
1513
1514 double maxX1 = triangle->getX1Maximum();
1515 double maxX2 = triangle->getX2Maximum();
1516 double maxX3 = triangle->getX3Maximum();
1517
1518 if ((PointQ.X1() < minX1) && (PointR.X1() < minX1))
1519 return false;
1520 if ((PointQ.X2() < minX2) && (PointR.X2() < minX2))
1521 return false;
1522 if ((PointQ.X3() < minX3) && (PointR.X3() < minX3))
1523 return false;
1524 if ((PointQ.X1() > maxX1) && (PointR.X1() > maxX1))
1525 return false;
1526 if ((PointQ.X2() > maxX2) && (PointR.X2() > maxX2))
1527 return false;
1528 if ((PointQ.X3() > maxX3) && (PointR.X3() > maxX3))
1529 return false;
1530
1531 return true;
1532}
1533
A class provides 3d coordinate transformation.
This Class provides basic 3D box objects.
Definition GbCuboid3D.h:53
This Class provides basic 3D line objects.
Definition GbLine3D.h:59
This Interface provides basic 3D geometry objects methods.
Definition GbObject3D.h:64
This Class provides basic 3D point objects.
Definition GbPoint3D.h:49
double x1
Definition GbPoint3D.h:121
double getX3Coordinate() const
Definition GbPoint3D.h:84
double x3
Definition GbPoint3D.h:123
double getX1Coordinate() const
Definition GbPoint3D.h:82
void setX1(const double &x1)
Definition GbPoint3D.h:66
void finalize() override
Definition GbPoint3D.h:57
double getX2Coordinate() const
Definition GbPoint3D.h:83
double x2
Definition GbPoint3D.h:122
This Class provides basic 3D triangle objects.
std::vector< GbTriangle3D * > * triangles
std::vector< GbPoint3D * > * nodes
std::vector< GbLine3D * > * edges
This Class provides basic 3D vector objects.
Definition GbVector3D.h:47
void setName(std::string name)
Definition ObObject.h:52
A class implements a tuple.
Definition UbTuple.h:477
virtual std::string writeLines(const std::string &, std::vector< UbTupleFloat3 > &, std::vector< UbTupleInt2 > &)
Definition WbWriter.h:83
virtual std::string writeTriangles(const std::string &, std::vector< UbTupleFloat3 > &, std::vector< UbTupleInt3 > &)
Definition WbWriter.h:116
std::shared_ptr< T > SPtr
std::vector< GbTriangle3D * > getSurfaceTriangleSet() override
std::string toString() override
double getX2Maximum() override
void translate(const double &x1, const double &x2, const double &x3) override
std::vector< GbPoint3D * > * getNodesForRectangle(const double &p1x1, const double &p1x2, const double &p2x1, const double &p2x2)
char SegPlaneInt(GbTriangle3D *Tri, GbVector3D &q, GbVector3D &r, GbVector3D &p, int *m)
double getX1Maximum() override
bool isPointInObject3DRayCrossing(const double &xp, const double &yp, const double &zp, int radius, int numVertices, int numTriangles)
char InPlane(GbTriangle3D *T, int m, GbVector3D &q, GbVector3D &r, GbVector3D &p)
std::vector< GbPoint3D * > * getNodes()
bool isPointInObject3DHalfSpace(const double &xp, const double &yp, const double &zp)
double getX3Minimum() override
double getX3MaximumForRectangle(const double &p1x1, const double &p1x2, const double &p2x1, const double &p2x2)
double getX3RangeForRectangle(const double &p1x1, const double &p1x2, const double &p2x1, const double &p2x2)
double getX2Minimum() override
GbLine3D * createClippedLine3D(GbPoint3D &point1, GbPoint3D &point2) override
double AreaSign(GbVector3D &a, GbVector3D &b, GbVector3D &c)
void rotate(const double &alpha, const double &beta, const double &gamma) override
double getX1Centroid() override
UbTupleDouble3 calculateCenterOfGravity()
void writeMesh(std::string filename, WbWriter *writer, bool writeNormals=false)
double getX2Centroid() override
bool InPolyhedron(int F, GbVector3D &q, int radius)
void RandomRay(GbVector3D &ray, int radius)
int PlaneCoeff(GbTriangle3D *Tri, GbVector3D &Normal, double *D)
char SegTriCross(GbTriangle3D *T, GbVector3D &q, GbVector3D &r)
double getVolumeForRectangle(const double &p1x1, const double &p1x2, const double &p2x1, const double &p2x2)
UbTupleDouble6 calculateMomentOfInertia(double rhoP)
bool isPointInGbObject3D(const double &x1, const double &x2, const double &x3) override
std::vector< GbTriangle3D * > * getTriangles()
std::vector< GbTriangle3D * > * getTrianglesForRectangle(const double &p1x1, const double &p1x2, const double &p2x1, const double &p2x2)
double getX1Minimum() override
double VolumeSign(GbVector3D &a, GbVector3D &b, GbVector3D &c, GbVector3D &d)
char InTri3D(GbTriangle3D *T, int m, GbVector3D &p)
double getX3MinimumForRectangle(const double &p1x1, const double &p1x2, const double &p2x1, const double &p2x2)
char SegTriInt(GbTriangle3D *Tri, GbVector3D &q, GbVector3D &r, GbVector3D &p)
char InTri2D(GbVector3D Tp[3], GbVector3D &pp)
double getX3Maximum() override
bool BoxTest(GbTriangle3D *triangle, GbVector3D &PointQ, GbVector3D &PointR)
GbVector3D Add(GbVector3D &vector)
double Dot(const GbVector3D &rkV) const
double getX3Centroid() override
@ z
Definition Axis.h:44
@ x
Definition Axis.h:42
@ y
Definition Axis.h:43
const real dx
const real dz
const real dy
#define UB_EXARGS
Definition UbException.h:73
UbTuple< double, double, double, double, double, double > UbTupleDouble6
Definition UbTuple.h:636
UbTuple< T1 > makeUbTuple(T1 const &a1)
Definition UbTuple.h:554
const double PI
bool zero(const T &value)
Definition UbMath.h:139