00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "faceTriangulation.H"
00028 #include "plane.H"
00029
00030
00031
00032
00033 const Foam::scalar Foam::faceTriangulation::edgeRelTol = 1E-6;
00034
00035
00036
00037 Foam::label Foam::faceTriangulation::right(const label, label i)
00038 {
00039 return i;
00040 }
00041
00042
00043
00044 Foam::label Foam::faceTriangulation::left(const label size, label i)
00045 {
00046 return i ? i-1 : size-1;
00047 }
00048
00049
00050
00051
00052 Foam::tmp<Foam::vectorField> Foam::faceTriangulation::calcEdges
00053 (
00054 const face& f,
00055 const pointField& points
00056 )
00057 {
00058 tmp<vectorField> tedges(new vectorField(f.size()));
00059 vectorField& edges = tedges();
00060
00061 forAll(f, i)
00062 {
00063 point thisPt = points[f[i]];
00064 point nextPt = points[f[f.fcIndex(i)]];
00065
00066 vector vec(nextPt - thisPt);
00067 vec /= mag(vec) + VSMALL;
00068
00069 edges[i] = vec;
00070 }
00071
00072 return tedges;
00073 }
00074
00075
00076
00077 void Foam::faceTriangulation::calcHalfAngle
00078 (
00079 const vector& normal,
00080 const vector& e0,
00081 const vector& e1,
00082 scalar& cosHalfAngle,
00083 scalar& sinHalfAngle
00084 )
00085 {
00086
00087 scalar cos = max(-1, min(1, e0 & e1));
00088
00089 scalar sin = (e0 ^ e1) & normal;
00090
00091 if (sin < -ROOTVSMALL)
00092 {
00093
00094 cosHalfAngle = - Foam::sqrt(0.5*(1 + cos));
00095 sinHalfAngle = Foam::sqrt(0.5*(1 - cos));
00096 }
00097 else
00098 {
00099
00100 cosHalfAngle = Foam::sqrt(0.5*(1 + cos));
00101 sinHalfAngle = Foam::sqrt(0.5*(1 - cos));
00102 }
00103 }
00104
00105
00106
00107
00108 Foam::pointHit Foam::faceTriangulation::rayEdgeIntersect
00109 (
00110 const vector& normal,
00111 const point& rayOrigin,
00112 const vector& rayDir,
00113 const point& p1,
00114 const point& p2,
00115 scalar& posOnEdge
00116 )
00117 {
00118
00119 pointHit result(p1);
00120
00121
00122 const vector y = normal ^ rayDir;
00123
00124 posOnEdge = plane(rayOrigin, y).normalIntersect(p1, (p2-p1));
00125
00126
00127 if ((posOnEdge < 0) || (posOnEdge > 1))
00128 {
00129
00130 }
00131 else
00132 {
00133
00134 point intersectPt = p1 + posOnEdge * (p2 - p1);
00135
00136 if (((intersectPt - rayOrigin) & rayDir) < 0)
00137 {
00138
00139 }
00140 else
00141 {
00142
00143 result.setHit();
00144 result.setPoint(intersectPt);
00145 result.setDistance(mag(intersectPt - rayOrigin));
00146 }
00147 }
00148 return result;
00149 }
00150
00151
00152
00153
00154 bool Foam::faceTriangulation::triangleContainsPoint
00155 (
00156 const vector& n,
00157 const point& p0,
00158 const point& p1,
00159 const point& p2,
00160 const point& pt
00161 )
00162 {
00163 scalar area01Pt = triPointRef(p0, p1, pt).normal() & n;
00164 scalar area12Pt = triPointRef(p1, p2, pt).normal() & n;
00165 scalar area20Pt = triPointRef(p2, p0, pt).normal() & n;
00166
00167 if ((area01Pt > 0) && (area12Pt > 0) && (area20Pt > 0))
00168 {
00169 return true;
00170 }
00171 else if ((area01Pt < 0) && (area12Pt < 0) && (area20Pt < 0))
00172 {
00173 FatalErrorIn("triangleContainsPoint") << abort(FatalError);
00174 return false;
00175 }
00176 else
00177 {
00178 return false;
00179 }
00180 }
00181
00182
00183
00184
00185 void Foam::faceTriangulation::findDiagonal
00186 (
00187 const pointField& points,
00188 const face& f,
00189 const vectorField& edges,
00190 const vector& normal,
00191 const label startIndex,
00192 label& index1,
00193 label& index2
00194 )
00195 {
00196 const point& startPt = points[f[startIndex]];
00197
00198
00199 const vector& rightE = edges[right(f.size(), startIndex)];
00200 const vector leftE = -edges[left(f.size(), startIndex)];
00201
00202
00203 scalar cosHalfAngle = GREAT;
00204 scalar sinHalfAngle = GREAT;
00205 calcHalfAngle(normal, rightE, leftE, cosHalfAngle, sinHalfAngle);
00206 vector rayDir
00207 (
00208 cosHalfAngle*rightE
00209 + sinHalfAngle*(normal ^ rightE)
00210 );
00211
00212
00213 rayDir /= mag(rayDir) + VSMALL;
00214
00215
00216
00217
00218
00219
00220 label faceVertI = f.fcIndex(startIndex);
00221
00222 pointHit minInter(false, vector::zero, GREAT, true);
00223 label minIndex = -1;
00224 scalar minPosOnEdge = GREAT;
00225
00226 for (label i = 0; i < f.size() - 2; i++)
00227 {
00228 scalar posOnEdge;
00229 pointHit inter =
00230 rayEdgeIntersect
00231 (
00232 normal,
00233 startPt,
00234 rayDir,
00235 points[f[faceVertI]],
00236 points[f[f.fcIndex(faceVertI)]],
00237 posOnEdge
00238 );
00239
00240 if (inter.hit() && inter.distance() < minInter.distance())
00241 {
00242 minInter = inter;
00243 minIndex = faceVertI;
00244 minPosOnEdge = posOnEdge;
00245 }
00246
00247 faceVertI = f.fcIndex(faceVertI);
00248 }
00249
00250
00251 if (minIndex == -1)
00252 {
00253
00254
00255
00256
00257 index1 = -1;
00258 index2 = -1;
00259 return;
00260 }
00261
00262 const label leftIndex = minIndex;
00263 const label rightIndex = f.fcIndex(minIndex);
00264
00265
00266
00267
00268
00269 if (mag(minPosOnEdge) < edgeRelTol && f.fcIndex(startIndex) != leftIndex)
00270 {
00271 index1 = startIndex;
00272 index2 = leftIndex;
00273
00274 return;
00275 }
00276 if
00277 (
00278 mag(minPosOnEdge - 1) < edgeRelTol
00279 && f.fcIndex(rightIndex) != startIndex
00280 )
00281 {
00282 index1 = startIndex;
00283 index2 = rightIndex;
00284
00285 return;
00286 }
00287
00288
00289
00290
00291
00292 const point& leftPt = points[f[leftIndex]];
00293 const point& rightPt = points[f[rightIndex]];
00294
00295 minIndex = -1;
00296 scalar maxCos = -GREAT;
00297
00298
00299 faceVertI = f.fcIndex(f.fcIndex(startIndex));
00300 for (label i = 0; i < f.size() - 3; i++)
00301 {
00302 const point& pt = points[f[faceVertI]];
00303
00304 if
00305 (
00306 (faceVertI == leftIndex)
00307 || (faceVertI == rightIndex)
00308 || (triangleContainsPoint(normal, startPt, leftPt, rightPt, pt))
00309 )
00310 {
00311
00312
00313 vector edgePt0 = pt - startPt;
00314 edgePt0 /= mag(edgePt0);
00315
00316 scalar cos = rayDir & edgePt0;
00317 if (cos > maxCos)
00318 {
00319 maxCos = cos;
00320 minIndex = faceVertI;
00321 }
00322 }
00323 faceVertI = f.fcIndex(faceVertI);
00324 }
00325
00326 if (minIndex == -1)
00327 {
00328
00329
00330 index1 = startIndex;
00331
00332 if (f.fcIndex(startIndex) != leftIndex)
00333 {
00334 index2 = leftIndex;
00335 }
00336 else
00337 {
00338 index2 = rightIndex;
00339 }
00340
00341 return;
00342 }
00343
00344 index1 = startIndex;
00345 index2 = minIndex;
00346 }
00347
00348
00349
00350
00351
00352 Foam::label Foam::faceTriangulation::findStart
00353 (
00354 const face& f,
00355 const vectorField& edges,
00356 const vector& normal
00357 )
00358 {
00359 const label size = f.size();
00360
00361 scalar minCos = GREAT;
00362 label minIndex = -1;
00363
00364 forAll(f, fp)
00365 {
00366 const vector& rightEdge = edges[right(size, fp)];
00367 const vector leftEdge = -edges[left(size, fp)];
00368
00369 if (((rightEdge ^ leftEdge) & normal) < ROOTVSMALL)
00370 {
00371 scalar cos = rightEdge & leftEdge;
00372 if (cos < minCos)
00373 {
00374 minCos = cos;
00375 minIndex = fp;
00376 }
00377 }
00378 }
00379
00380 if (minIndex == -1)
00381 {
00382
00383 minCos = GREAT;
00384
00385 forAll(f, fp)
00386 {
00387 const vector& rightEdge = edges[right(size, fp)];
00388 const vector leftEdge = -edges[left(size, fp)];
00389
00390 scalar cos = rightEdge & leftEdge;
00391 if (cos < minCos)
00392 {
00393 minCos = cos;
00394 minIndex = fp;
00395 }
00396 }
00397 }
00398
00399 return minIndex;
00400 }
00401
00402
00403
00404
00405
00406
00407 bool Foam::faceTriangulation::split
00408 (
00409 const bool fallBack,
00410 const pointField& points,
00411 const face& f,
00412 const vector& normal,
00413 label& triI
00414 )
00415 {
00416 const label size = f.size();
00417
00418 if (size <= 2)
00419 {
00420 WarningIn
00421 (
00422 "split(const bool, const pointField&, const face&"
00423 ", const vector&, label&)"
00424 ) << "Illegal face:" << f
00425 << " with points " << UIndirectList<point>(points, f)()
00426 << endl;
00427
00428 return false;
00429 }
00430 else if (size == 3)
00431 {
00432
00433 triFace& tri = operator[](triI++);
00434 tri[0] = f[0];
00435 tri[1] = f[1];
00436 tri[2] = f[2];
00437
00438 return true;
00439 }
00440 else
00441 {
00442
00443
00444
00445 tmp<vectorField> tedges(calcEdges(f, points));
00446 const vectorField& edges = tedges();
00447
00448 label startIndex = findStart(f, edges, normal);
00449
00450
00451 label index1 = -1;
00452 label index2 = -1;
00453
00454 for (label iter = 0; iter < f.size(); iter++)
00455 {
00456 findDiagonal
00457 (
00458 points,
00459 f,
00460 edges,
00461 normal,
00462 startIndex,
00463 index1,
00464 index2
00465 );
00466
00467 if (index1 != -1 && index2 != -1)
00468 {
00469
00470 break;
00471 }
00472
00473
00474 startIndex = f.fcIndex(startIndex);
00475 }
00476
00477 if (index1 == -1 || index2 == -1)
00478 {
00479 if (fallBack)
00480 {
00481
00482
00483 label maxIndex = -1;
00484 scalar maxCos = -GREAT;
00485
00486 forAll(f, fp)
00487 {
00488 const vector& rightEdge = edges[right(size, fp)];
00489 const vector leftEdge = -edges[left(size, fp)];
00490
00491 scalar cos = rightEdge & leftEdge;
00492 if (cos > maxCos)
00493 {
00494 maxCos = cos;
00495 maxIndex = fp;
00496 }
00497 }
00498
00499 WarningIn
00500 (
00501 "split(const bool, const pointField&, const face&"
00502 ", const vector&, label&)"
00503 ) << "Cannot find valid diagonal on face " << f
00504 << " with points " << UIndirectList<point>(points, f)()
00505 << nl
00506 << "Returning naive triangulation starting from "
00507 << f[maxIndex] << " which might not be correct for a"
00508 << " concave or warped face" << endl;
00509
00510
00511 label fp = f.fcIndex(maxIndex);
00512
00513 for (label i = 0; i < size-2; i++)
00514 {
00515 label nextFp = f.fcIndex(fp);
00516
00517 triFace& tri = operator[](triI++);
00518 tri[0] = f[maxIndex];
00519 tri[1] = f[fp];
00520 tri[2] = f[nextFp];
00521
00522 fp = nextFp;
00523 }
00524
00525 return true;
00526 }
00527 else
00528 {
00529 WarningIn
00530 (
00531 "split(const bool, const pointField&, const face&"
00532 ", const vector&, label&)"
00533 ) << "Cannot find valid diagonal on face " << f
00534 << " with points " << UIndirectList<point>(points, f)()
00535 << nl
00536 << "Returning empty triFaceList" << endl;
00537
00538 return false;
00539 }
00540 }
00541
00542
00543
00544
00545
00546
00547
00548 label diff = 0;
00549 if (index2 > index1)
00550 {
00551 diff = index2 - index1;
00552 }
00553 else
00554 {
00555
00556 diff = index2 + size - index1;
00557 }
00558
00559 label nPoints1 = diff + 1;
00560 label nPoints2 = size - diff + 1;
00561
00562 if (nPoints1 == size || nPoints2 == size)
00563 {
00564 FatalErrorIn
00565 (
00566 "split(const bool, const pointField&, const face&"
00567 ", const vector&, label&)"
00568 ) << "Illegal split of face:" << f
00569 << " with points " << UIndirectList<point>(points, f)()
00570 << " at indices " << index1 << " and " << index2
00571 << abort(FatalError);
00572 }
00573
00574
00575
00576 face face1(nPoints1);
00577
00578 label faceVertI = index1;
00579 for (int i = 0; i < nPoints1; i++)
00580 {
00581 face1[i] = f[faceVertI];
00582 faceVertI = f.fcIndex(faceVertI);
00583 }
00584
00585
00586 face face2(nPoints2);
00587
00588 faceVertI = index2;
00589 for (int i = 0; i < nPoints2; i++)
00590 {
00591 face2[i] = f[faceVertI];
00592 faceVertI = f.fcIndex(faceVertI);
00593 }
00594
00595
00596
00597
00598
00599
00600
00601 bool splitOk =
00602 split(fallBack, points, face1, normal, triI)
00603 && split(fallBack, points, face2, normal, triI);
00604
00605
00606
00607 return splitOk;
00608 }
00609 }
00610
00611
00612
00613
00614
00615 Foam::faceTriangulation::faceTriangulation()
00616 :
00617 triFaceList()
00618 {}
00619
00620
00621
00622 Foam::faceTriangulation::faceTriangulation
00623 (
00624 const pointField& points,
00625 const face& f,
00626 const bool fallBack
00627 )
00628 :
00629 triFaceList(f.size()-2)
00630 {
00631 vector avgNormal = f.normal(points);
00632 avgNormal /= mag(avgNormal) + VSMALL;
00633
00634 label triI = 0;
00635
00636 bool valid = split(fallBack, points, f, avgNormal, triI);
00637
00638 if (!valid)
00639 {
00640 setSize(0);
00641 }
00642 }
00643
00644
00645
00646 Foam::faceTriangulation::faceTriangulation
00647 (
00648 const pointField& points,
00649 const face& f,
00650 const vector& n,
00651 const bool fallBack
00652 )
00653 :
00654 triFaceList(f.size()-2)
00655 {
00656 label triI = 0;
00657
00658 bool valid = split(fallBack, points, f, n, triI);
00659
00660 if (!valid)
00661 {
00662 setSize(0);
00663 }
00664 }
00665
00666
00667
00668 Foam::faceTriangulation::faceTriangulation(Istream& is)
00669 :
00670 triFaceList(is)
00671 {}
00672
00673
00674