GEOS
ComputationalGeometry.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_MESH_UTILITIES_COMPUTATIONALGEOMETRY_HPP_
21 #define GEOS_MESH_UTILITIES_COMPUTATIONALGEOMETRY_HPP_
22 
23 #include "common/DataTypes.hpp"
24 #include "common/DataLayouts.hpp"
29 #include "LvArray/src/output.hpp"
30 #include "LvArray/src/tensorOps.hpp"
31 
32 namespace geos
33 {
34 namespace computationalGeometry
35 {
36 
38 constexpr real64 machinePrecision = LvArray::NumericLimits< real64 >::epsilon;
39 
53 template< typename LINEDIR_TYPE,
54  typename POINT_TYPE,
55  typename NORMAL_TYPE,
56  typename ORIGIN_TYPE,
57  typename INTPOINT_TYPE >
58 void LinePlaneIntersection( LINEDIR_TYPE const & lineDir,
59  POINT_TYPE const & linePoint,
60  NORMAL_TYPE const & planeNormal,
61  ORIGIN_TYPE const & planeOrigin,
62  INTPOINT_TYPE & intersectionPoint )
63 {
64  /* Find intersection line plane
65  * line equation: p - (d*lineDir + linePoing) = 0;
66  * plane equation: ( p - planeOrigin) * planeNormal = 0;
67  * d = (planeOrigin - linePoint) * planeNormal / (lineDir * planeNormal )
68  * pInt = d*lineDir+linePoint;
69  */
70  real64 dummy[ 3 ] = LVARRAY_TENSOROPS_INIT_LOCAL_3( planeOrigin );
71  LvArray::tensorOps::subtract< 3 >( dummy, linePoint );
72  real64 const d = LvArray::tensorOps::AiBi< 3 >( dummy, planeNormal ) /
73  LvArray::tensorOps::AiBi< 3 >( lineDir, planeNormal );
74 
75  LvArray::tensorOps::copy< 3 >( intersectionPoint, linePoint );
76  LvArray::tensorOps::scaledAdd< 3 >( intersectionPoint, lineDir, d );
77 }
78 
86 template< typename NORMAL_TYPE >
88  NORMAL_TYPE const & normal )
89 {
90  localIndex const numPoints = points.size( 0 );
91 
92  array2d< real64 > orderedPoints( numPoints, 3 );
93 
94  array1d< int > indices( numPoints );
95  array1d< real64 > angle( numPoints );
96 
97  // compute centroid of the set of points
98  real64 centroid[3];
99  LvArray::tensorOps::fill< 3 >( centroid, 0 );
100  for( localIndex a = 0; a < numPoints; ++a )
101  {
102  LvArray::tensorOps::add< 3 >( centroid, points[ a ] );
103  indices[ a ] = a;
104  }
105 
106  LvArray::tensorOps::scale< 3 >( centroid, 1.0 / numPoints );
107 
108  real64 v0[3] = LVARRAY_TENSOROPS_INIT_LOCAL_3( centroid );
109  LvArray::tensorOps::subtract< 3 >( v0, points[ 0 ] );
110  LvArray::tensorOps::normalize< 3 >( v0 );
111 
112  // compute angles
113  angle[ 0 ] = 0;
114  for( localIndex a = 1; a < numPoints; ++a )
115  {
116  real64 v[3] = LVARRAY_TENSOROPS_INIT_LOCAL_3( centroid );
117  LvArray::tensorOps::subtract< 3 >( v, points[ a ] );
118  real64 const dot = LvArray::tensorOps::AiBi< 3 >( v, v0 );
119 
120  real64 crossProduct[ 3 ];
121  LvArray::tensorOps::crossProduct( crossProduct, v, v0 );
122  real64 const det = LvArray::tensorOps::AiBi< 3 >( normal, crossProduct );
123 
124  angle[ a ] = std::atan2( det, dot );
125  }
126 
127  // sort the indices
128  std::sort( indices.begin(), indices.end(), [&]( int i, int j ) { return angle[ i ] < angle[ j ]; } );
129 
130  // copy the points in the reorderedPoints array.
131  for( localIndex a=0; a < numPoints; a++ )
132  {
133  // fill in with ordered
134  LvArray::tensorOps::copy< 3 >( orderedPoints[ a ], points[ indices[ a ] ] );
135  }
136 
137  for( localIndex a = 0; a < numPoints; a++ )
138  {
139  LvArray::tensorOps::copy< 3 >( points[a], orderedPoints[a] );
140  }
141 
142  return indices;
143 }
144 
152 template< typename NORMAL_TYPE >
154  NORMAL_TYPE const && normal )
155 {
156  real64 surfaceArea = 0.0;
157 
158  array2d< real64 > orderedPoints( points.size( 0 ), 3 );
159 
160  for( localIndex a = 0; a < points.size( 0 ); a++ )
161  {
162  LvArray::tensorOps::copy< 3 >( orderedPoints[a], points[a] );
163  }
164 
165  orderPointsCCW( orderedPoints, normal );
166 
167  for( localIndex a = 0; a < points.size( 0 ) - 2; ++a )
168  {
169  real64 v1[ 3 ] = LVARRAY_TENSOROPS_INIT_LOCAL_3( orderedPoints[ a + 1 ] );
170  real64 v2[ 3 ] = LVARRAY_TENSOROPS_INIT_LOCAL_3( orderedPoints[ a + 2 ] );
171 
172  LvArray::tensorOps::subtract< 3 >( v1, orderedPoints[ 0 ] );
173  LvArray::tensorOps::subtract< 3 >( v2, orderedPoints[ 0 ] );
174 
175  real64 triangleNormal[ 3 ];
176  LvArray::tensorOps::crossProduct( triangleNormal, v1, v2 );
177  surfaceArea += LvArray::tensorOps::l2Norm< 3 >( triangleNormal );
178  }
179 
180  return surfaceArea * 0.5;
181 }
182 
191 template< localIndex DIMENSION, typename POINT_COORDS_TYPE >
194 real64 computeDiameter( POINT_COORDS_TYPE points,
195  localIndex const & numPoints )
196 {
197  real64 diameter = 0;
198  for( localIndex numPoint = 0; numPoint < numPoints; ++numPoint )
199  {
200  for( localIndex numOthPoint = 0; numOthPoint < numPoint; ++numOthPoint )
201  {
202  real64 candidateDiameter = 0.0;
203  for( localIndex i = 0; i < DIMENSION; ++i )
204  {
205  real64 coordDiff = points[numPoint][i] - points[numOthPoint][i];
206  candidateDiameter += coordDiff * coordDiff;
207  }
208  if( diameter < candidateDiameter )
209  {
210  diameter = candidateDiameter;
211  }
212  }
213  }
214  return LvArray::math::sqrt< real64 >( diameter );
215 }
216 
231 template< typename CENTER_TYPE, typename NORMAL_TYPE >
236  CENTER_TYPE && center,
237  NORMAL_TYPE && normal,
238  real64 const areaTolerance = 0.0 )
239 {
240  real64 area = 0.0;
241  LvArray::tensorOps::fill< 3 >( center, 0 );
242  LvArray::tensorOps::fill< 3 >( normal, 0 );
243 
244  localIndex const numberOfPoints = pointsIndices.size();
245 
246  GEOS_ERROR_IF_LT( numberOfPoints, 2 );
247 
248  real64 current[ 3 ], next[ 3 ], crossProduct[ 3 ];
249 
250  LvArray::tensorOps::copy< 3 >( next, points[ pointsIndices[ numberOfPoints - 1 ] ] );
251 
252  for( localIndex a=0; a<numberOfPoints; ++a )
253  {
254  LvArray::tensorOps::copy< 3 >( current, next );
255  LvArray::tensorOps::copy< 3 >( next, points[ pointsIndices[ a ] ] );
256 
257  LvArray::tensorOps::crossProduct( crossProduct, current, next );
258 
259  LvArray::tensorOps::add< 3 >( normal, crossProduct );
260  LvArray::tensorOps::add< 3 >( center, next );
261  }
262 
263  area = LvArray::tensorOps::l2Norm< 3 >( normal );
264  LvArray::tensorOps::scale< 3 >( center, 1.0 / numberOfPoints );
265 
266  if( area > areaTolerance )
267  {
268  LvArray::tensorOps::normalize< 3 >( normal );
269  area *= 0.5;
270  }
271  else if( area < -areaTolerance )
272  {
273  for( localIndex a=0; a<numberOfPoints; ++a )
274  {
275  GEOS_LOG_RANK( "Points: " << points[ pointsIndices[ a ] ] << " " << pointsIndices[ a ] );
276  }
277  GEOS_ERROR( "Negative area found : " << area );
278  }
279  else
280  {
281  return 0.0;
282  }
283 
284  return area;
285 }
286 
292 template< typename NORMAL_TYPE >
294 void FixNormalOrientation_3D( NORMAL_TYPE && normal )
295 {
296  real64 const orientationTolerance = 10 * machinePrecision;
297 
298  // Orient local normal in global sense.
299  // First check: align with z direction
300  if( normal[ 2 ] <= -orientationTolerance )
301  {
302  LvArray::tensorOps::scale< 3 >( normal, -1.0 );
303  }
304  else if( std::fabs( normal[ 2 ] ) < orientationTolerance )
305  {
306  // If needed, second check: align with y direction
307  if( normal[ 1 ] <= -orientationTolerance )
308  {
309  LvArray::tensorOps::scale< 3 >( normal, -1.0 );
310  }
311  else if( fabs( normal[ 1 ] ) < orientationTolerance )
312  {
313  // If needed, third check: align with x direction
314  if( normal[ 0 ] <= -orientationTolerance )
315  {
316  LvArray::tensorOps::scale< 3 >( normal, -1.0 );
317  }
318  }
319  }
320 }
321 
329 template< typename NORMAL_TYPE, typename MATRIX_TYPE >
331 void RotationMatrix_3D( NORMAL_TYPE const & normal,
332  MATRIX_TYPE && rotationMatrix )
333 {
334  real64 m1[ 3 ] = { normal[ 2 ], 0.0, -normal[ 0 ] };
335  real64 m2[ 3 ] = { 0.0, normal[ 2 ], -normal[ 1 ] };
336  real64 const norm_m1 = LvArray::tensorOps::l2Norm< 3 >( m1 );
337  real64 const norm_m2 = LvArray::tensorOps::l2Norm< 3 >( m2 );
338 
339  // If present, looks for a vector with 0 norm
340  // Fix the uncertain case of norm_m1 very close to norm_m2
341  if( norm_m1+1.e+2*machinePrecision > norm_m2 )
342  {
343  LvArray::tensorOps::crossProduct( m2, normal, m1 );
344  LvArray::tensorOps::normalize< 3 >( m2 );
345  LvArray::tensorOps::normalize< 3 >( m1 );
346  }
347  else
348  {
349  LvArray::tensorOps::crossProduct( m1, normal, m2 );
350  LvArray::tensorOps::scale< 3 >( m1, -1 );
351  LvArray::tensorOps::normalize< 3 >( m1 );
352  LvArray::tensorOps::normalize< 3 >( m2 );
353  }
354 
355  // Save everything in the standard form (3x3 rotation matrix)
356  rotationMatrix[ 0 ][ 0 ] = normal[ 0 ];
357  rotationMatrix[ 1 ][ 0 ] = normal[ 1 ];
358  rotationMatrix[ 2 ][ 0 ] = normal[ 2 ];
359  rotationMatrix[ 0 ][ 1 ] = m1[ 0 ];
360  rotationMatrix[ 1 ][ 1 ] = m1[ 1 ];
361  rotationMatrix[ 2 ][ 1 ] = m1[ 2 ];
362  rotationMatrix[ 0 ][ 2 ] = m2[ 0 ];
363  rotationMatrix[ 1 ][ 2 ] = m2[ 1 ];
364  rotationMatrix[ 2 ][ 2 ] = m2[ 2 ];
365 
366  GEOS_ERROR_IF( fabs( LvArray::tensorOps::determinant< 3 >( rotationMatrix ) - 1.0 ) > 1.e+1 * machinePrecision,
367  "Rotation matrix with determinant different from +1.0" );
368 }
369 
376 template< typename T >
379 int sign( T const val )
380 {
381  return (T( 0 ) < val) - (val < T( 0 ));
382 }
383 
397 template< typename POINT_TYPE >
400  arraySlice1d< localIndex const > const & faceIndices,
401  ArrayOfArraysView< localIndex const > const & facesToNodes,
402  POINT_TYPE const & elemCenter,
403  POINT_TYPE const & point,
404  real64 const areaTolerance = 0.0 )
405 {
406  localIndex const numFaces = faceIndices.size();
407  R1Tensor faceCenter, faceNormal, cellToFaceVec;
408 
409  for( localIndex kf = 0; kf < numFaces; ++kf )
410  {
411  // compute the face normal at this face
412  localIndex const faceIndex = faceIndices[kf];
413  centroid_3DPolygon( facesToNodes[faceIndex], nodeCoordinates, faceCenter, faceNormal, areaTolerance );
414 
415  // make sure that the normal is outward pointing
416  LvArray::tensorOps::copy< 3 >( cellToFaceVec, faceCenter );
417  LvArray::tensorOps::subtract< 3 >( cellToFaceVec, elemCenter );
418  if( LvArray::tensorOps::AiBi< 3 >( cellToFaceVec, faceNormal ) < 0.0 )
419  {
420  LvArray::tensorOps::scale< 3 >( faceNormal, -1 );
421  }
422 
423  // compute the vector face center to query point
424  LvArray::tensorOps::subtract< 3 >( faceCenter, point );
425  int const s = sign( LvArray::tensorOps::AiBi< 3 >( faceNormal, faceCenter ) );
426 
427  // all dot products should be non-negative (we enforce outward normals)
428  if( s < 0 )
429  {
430  return false;
431  }
432  }
433  return true;
434 }
435 
446 template< typename POLYGON_TYPE, typename POINT_TYPE >
447 bool isPointInPolygon2d( POLYGON_TYPE const & polygon,
448  integer n,
449  POINT_TYPE const & point,
450  real64 const tol = 1e-10 )
451 {
452  integer count = 0;
453 
454  for( integer i = 0; i < n; ++i )
455  {
456  auto const & p1 = polygon[i];
457  auto const & p2 = polygon[(i + 1) % n];
458 
459  real64 y1 = p1[1], y2 = p2[1];
460  real64 x1 = p1[0], x2 = p2[0];
461  real64 py = point[1], px = point[0];
462 
463  // quick reject in y with tolerance
464  if( py + tol < std::min( y1, y2 ) || py - tol > std::max( y1, y2 ) )
465  continue;
466 
467  // check if point is (approximately) on the segment
468  // parametric t for projection on segment in y (if segment vertical-ish use x)
469  if( std::abs( (x2 - x1) * (py - y1) - (px - x1) * (y2 - y1) ) < tol *
470  ( std::hypot( x2 - x1, y2 - y1 ) + 1.0 ) )
471  {
472  // ensure px is between x1,x2 and py between y1,y2 (with tol)
473  if( px + tol >= std::min( x1, x2 ) && px - tol <= std::max( x1, x2 ) &&
474  py + tol >= std::min( y1, y2 ) && py - tol <= std::max( y1, y2 ) )
475  return true; // on boundary -> consider inside
476  }
477 
478  // ignore nearly-horizontal edges for intersection counting
479  if( std::abs( y2 - y1 ) < tol )
480  continue;
481 
482  // compute x coordinate of intersection of horizontal line py with segment p1-p2
483  real64 xIntersect = x1 + (py - y1) * (x2 - x1) / (y2 - y1);
484 
485  // count crossing where intersection is strictly to the right of point (robust with tol)
486  if( px < xIntersect - tol )
487  ++count;
488  }
489 
490  return (count % 2) == 1;
491 }
492 
503 template< typename POLYGON_TYPE, typename POINT_TYPE >
504 bool isPointInPolygon3d( POLYGON_TYPE const & polygon,
505  integer const n,
506  POINT_TYPE const & point,
507  real64 const tol = 1e-10 )
508 {
509  // Check if the point lies in the plane of the polygon
510  auto const & p0 = polygon[0];
511  POINT_TYPE normal = {0, 0, 0};
512  for( integer i = 1; i < n - 1; i++ )
513  {
514  auto const & p1 = polygon[i];
515  auto const & p2 = polygon[i + 1];
516  normal[0] += (p1[1] - p0[1]) * (p2[2] - p0[2]) - (p1[2] - p0[2]) * (p2[1] - p0[1]);
517  normal[1] += (p1[2] - p0[2]) * (p2[0] - p0[0]) - (p1[0] - p0[0]) * (p2[2] - p0[2]);
518  normal[2] += (p1[0] - p0[0]) * (p2[1] - p0[1]) - (p1[1] - p0[1]) * (p2[0] - p0[0]);
519  }
520 
521  real64 const dist = normal[0] * point[0] + normal[1] * point[1] + normal[2] * point[2] -(normal[0] * p0[0] + normal[1] * p0[1] + normal[2] * p0[2]);
522 
523  if( std::abs( dist ) > tol )
524  {
525  return false;
526  }
527 
528  // Determine the dominant component of the normal vector
529  int dominantIndex = 0;
530  if( std::abs( normal[1] ) > std::abs( normal[0] ))
531  {
532  dominantIndex = 1;
533  }
534  if( std::abs( normal[2] ) > std::abs( normal[dominantIndex] ))
535  {
536  dominantIndex = 2;
537  }
538 
539  // Project the polygon and the point onto a 2D plane
540  POLYGON_TYPE projectedPolygon( n );
541  POINT_TYPE projectedPoint;
542  if( dominantIndex == 0 ) // X is dominant, project onto YZ plane
543  {
544  for( int i = 0; i < n; i++ )
545  {
546  projectedPolygon[i][0] = polygon[i][1];
547  projectedPolygon[i][1] = polygon[i][2];
548  }
549  projectedPoint[0] = point[1];
550  projectedPoint[1] = point[2];
551  }
552  else if( dominantIndex == 1 ) // Y is dominant, project onto XZ plane
553  {
554  for( int i = 0; i < n; i++ )
555  {
556  projectedPolygon[i][0] = polygon[i][0];
557  projectedPolygon[i][1] = polygon[i][2];
558  }
559  projectedPoint[0] = point[0];
560  projectedPoint[1] = point[2];
561  }
562  else // Z is dominant, project onto XY plane
563  {
564  for( int i = 0; i < n; i++ )
565  {
566  projectedPolygon[i][0] = polygon[i][0];
567  projectedPolygon[i][1] = polygon[i][1];
568  }
569  projectedPoint[0] = point[0];
570  projectedPoint[1] = point[1];
571  }
572 
573  return isPointInPolygon2d( projectedPolygon, n, projectedPoint );
574 }
575 
588 template< typename COORD_TYPE, typename POINT_TYPE >
590 int lexicographicalCompareVertex( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
591  COORD_TYPE const bx, COORD_TYPE const by, COORD_TYPE const bz )
592 {
593  if( ax < bx )
594  return -1;
595  else if( ax > bx )
596  return 1;
597  if( ay < by )
598  return -1;
599  else if( ay > by )
600  return 1;
601  if( az < bz )
602  return -1;
603  else if( az > bz )
604  return 1;
605  return 0;
606 }
607 
623 template< typename COORD_TYPE, typename POINT_TYPE >
625 int lexicographicalCompareEdge( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
626  COORD_TYPE const e1x, COORD_TYPE const e1y, COORD_TYPE const e1z,
627  COORD_TYPE const e2x, COORD_TYPE const e2y, COORD_TYPE const e2z )
628 {
629  return lexicographicalCompareVertex( ( e1y - ay ) * ( e2x - ax ),
630  ( e1z - az ) * ( e2x - ax ),
631  ( e1z - az ) * ( e2y - ay ),
632  ( e1x - ax ) * ( e2y - ay ),
633  ( e1x - ax ) * ( e2z - az ),
634  ( e1y - ay ) * ( e2z - az ) );
635 }
636 
655 template< typename COORD_TYPE, typename POINT_TYPE >
657 int lexicographicalCompareTriangle( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
658  COORD_TYPE const t1x, COORD_TYPE const t1y, COORD_TYPE const t1z,
659  COORD_TYPE const t2x, COORD_TYPE const t2y, COORD_TYPE const t2z,
660  COORD_TYPE const t3x, COORD_TYPE const t3y, COORD_TYPE const t3z )
661 {
662  COORD_TYPE v1x = t1x - ax;
663  COORD_TYPE v1y = t1y - ay;
664  COORD_TYPE v1z = t1z - az;
665  COORD_TYPE v2x = t2x - ax;
666  COORD_TYPE v2y = t2y - ay;
667  COORD_TYPE v2z = t2z - az;
668  COORD_TYPE v3x = t3x - ax;
669  COORD_TYPE v3y = t3y - ay;
670  COORD_TYPE v3z = t3z - az;
671  COORD_TYPE sign = ( v1x * v2y - v1y * v2x ) * v3z +
672  ( v2x * v3y - v2y * v3x ) * v1z +
673  ( v3x * v1y - v3y * v1x ) * v2z;
674  if( sign > 0 )
675  return 1;
676  else if( sign < 0 )
677  return -1;
678  return 0;
679 }
687 template< typename ... LIST_TYPE >
690  arrayView1d< globalIndex const > const & elementGlobalIndex )
691 {
692  localIndex minElement = -1;
693  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
694  for( int i = 0; i < nodeElements.size(); i++ )
695  {
696  localIndex e = nodeElements( i );
697  if( elementGlobalIndex[ e ] < minElementGID )
698  {
699  minElementGID = elementGlobalIndex[ e ];
700  minElement = e;
701  }
702  }
703  return minElement;
704 }
705 
714 template< typename ... LIST_TYPE >
717  arraySlice1d< localIndex const > const & nodeElements2,
718  arrayView1d< globalIndex const > const & elementGlobalIndex )
719 {
720  localIndex minElement = -1;
721  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
722  for( int i = 0; i < nodeElements1.size(); i++ )
723  {
724  localIndex e1 = nodeElements1( i );
725  for( int j = 0; j < nodeElements2.size(); j++ )
726  {
727  localIndex e2 = nodeElements2( j );
728  if( e1 == e2 )
729  {
730  if( elementGlobalIndex[ e1 ] < minElementGID )
731  {
732  minElementGID = elementGlobalIndex[ e1 ];
733  minElement = e1;
734  }
735  }
736  }
737  }
738  return minElement;
739 }
740 
750 template< typename ... LIST_TYPE >
753  arraySlice1d< localIndex const > const & nodeElements2,
754  arraySlice1d< localIndex const > const & nodeElements3,
755  arrayView1d< globalIndex const > const & elementGlobalIndex )
756 {
757  localIndex minElement = -1;
758  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
759  for( int i = 0; i < nodeElements1.size(); i++ )
760  {
761  localIndex e1 = nodeElements1( i );
762  for( int j = 0; j < nodeElements2.size(); j++ )
763  {
764  localIndex e2 = nodeElements2( j );
765  for( int k = 0; k < nodeElements3.size(); k++ )
766  {
767  localIndex e3 = nodeElements3( k );
768  if( e1 == e2 && e2 == e3 )
769  {
770  if( elementGlobalIndex[ e1 ] < minElementGID )
771  {
772  minElementGID = elementGlobalIndex[ e1 ];
773  minElement = e1;
774  }
775  }
776  }
777  }
778  }
779  return minElement;
780 }
781 
796 template< typename COORD_TYPE, typename POINT_TYPE >
800  arrayView2d< localIndex const > const & elementsToFaces,
801  ArrayOfArraysView< localIndex const > const & facesToNodes,
802  ArrayOfArraysView< localIndex const > const & nodesToElements,
803  arrayView1d< globalIndex const > const & nodeLocalToGlobal,
804  arrayView1d< globalIndex const > const & elementLocalToGlobal,
805  POINT_TYPE const & elemCenter,
806  POINT_TYPE const & point )
807 {
808  arraySlice1d< localIndex const > const & faceIndices = elementsToFaces[ element ];
809  localIndex const numFaces = faceIndices.size();
810  int omega = 0;
811  for( localIndex kf = 0; kf < numFaces; ++kf )
812  {
813  // triangulate the face. The triangulation must be done in a consistent way across ranks.
814  // This can be achieved by always picking the vertex with the lowest global index as root.
815  localIndex const faceIndex = faceIndices[kf];
816  globalIndex minGlobalId = LvArray::NumericLimits< globalIndex >::max;
817  localIndex minVertex = -1;
818  localIndex numFaceVertices = facesToNodes[faceIndex].size();
819  for( localIndex v = 0; v < numFaceVertices; v++ )
820  {
821  localIndex vIndex = facesToNodes( faceIndex, v );
822  globalIndex globalId = nodeLocalToGlobal[ vIndex ];
823  if( globalId < minGlobalId )
824  {
825  minGlobalId = globalId;
826  minVertex = vIndex;
827  }
828  }
829  // triangulate the face using the minimum-id vertex as root
830  localIndex vi[ 3 ] = { minVertex, -1, -1 };
831  for( localIndex v = 0; v < numFaceVertices; v++ )
832  {
833  vi[ 1 ] = facesToNodes( faceIndex, v );
834  vi[ 2 ] = facesToNodes( faceIndex, (v + 1) % numFaceVertices );
835  if( vi[ 1 ] != minVertex && vi[ 2 ] != minVertex )
836  {
837  // To make the algorithm independent of rank, always take the two additional vertices in increasing global ID
838  if( nodeLocalToGlobal[ vi[ 1 ] ] > nodeLocalToGlobal[ vi[ 2 ] ] )
839  {
840  localIndex temp = vi[ 1 ];
841  vi[ 1 ] = vi[ 2 ];
842  vi[ 2 ] = temp;
843  }
844  COORD_TYPE v1x = nodeCoordinates( vi[ 0 ], 0 );
845  COORD_TYPE v1y = nodeCoordinates( vi[ 0 ], 1 );
846  COORD_TYPE v1z = nodeCoordinates( vi[ 0 ], 2 );
847  COORD_TYPE v2x = nodeCoordinates( vi[ 1 ], 0 );
848  COORD_TYPE v2y = nodeCoordinates( vi[ 1 ], 1 );
849  COORD_TYPE v2z = nodeCoordinates( vi[ 1 ], 2 );
850  COORD_TYPE v3x = nodeCoordinates( vi[ 2 ], 0 );
851  COORD_TYPE v3y = nodeCoordinates( vi[ 2 ], 1 );
852  COORD_TYPE v3z = nodeCoordinates( vi[ 2 ], 2 );
853  // check the orientation of this triangle
854  R1Tensor vv1 = { v2x - v1x, v2y - v1y, v2z - v1z };
855  R1Tensor vv2 = { v3x - v1x, v3y - v1y, v3z - v1z };
856  R1Tensor dist = { elemCenter[ 0 ] - ( v1x + v2x + v3x )/3.0,
857  elemCenter[ 1 ] - ( v1y + v2y + v3y )/3.0,
858  elemCenter[ 2 ] - ( v1z + v2z + v3z )/3.0 };
859  R1Tensor norm = { };
860  LvArray::tensorOps::crossProduct( norm, vv1, vv2 );
861  // check if face is oriented coherently, and change sign otherwise
862  int sign = LvArray::tensorOps::AiBi< 3 >( norm, dist ) > 0 ? -1 : +1;
863  // Compute the winding number contributed by this triangle
864  int cmp1 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v1x, v1y, v1z );
865  if( cmp1 == 0 )
866  {
867  return findVertexRefElement( nodesToElements[ vi[ 0 ] ], elementLocalToGlobal ) == element;
868  }
869  int cmp2 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v2x, v2y, v2z );
870  if( cmp2 == 0 )
871  {
872  return findVertexRefElement( nodesToElements[ vi[ 1 ] ], elementLocalToGlobal ) == element;
873  }
874  int cmp3 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v3x, v3y, v3z );
875  if( cmp3 == 0 )
876  {
877  return findVertexRefElement( nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
878  }
879  int facecmp = 0;
880  int edgecmp = 0;
881  if( cmp1 != cmp2 )
882  {
883  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
884  v1x, v1y, v1z,
885  v2x, v2y, v2z );
886  if( edgecmp == 0 )
887  {
888  return findEdgeRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 1 ] ], elementLocalToGlobal ) == element;
889  }
890  facecmp += sign * edgecmp;
891  }
892  if( cmp2 != cmp3 )
893  {
894  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
895  v2x, v2y, v2z,
896  v3x, v3y, v3z );
897  if( edgecmp == 0 )
898  {
899  return findEdgeRefElement( nodesToElements[ vi[ 1 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
900  }
901  facecmp += sign * edgecmp;
902  }
903  if( cmp3 != cmp1 )
904  {
905  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
906  v3x, v3y, v3z,
907  v1x, v1y, v1z );
908  if( edgecmp == 0 )
909  {
910  return findEdgeRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
911  }
912  facecmp += sign * edgecmp;
913  }
914  // if all edges are on the same side, this triangle does not contribute to the winding number
915  if( facecmp == 0 )
916  continue;
917  facecmp = lexicographicalCompareTriangle( point[ 0 ], point[ 1 ], point[ 2 ],
918  v1x, v1y, v1z,
919  v2x, v2y, v2z,
920  v3x, v3y, v3z );
921 
922  if( facecmp == 0 )
923  {
924  return findTriangleRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 1 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
925  }
926  omega += sign * facecmp;
927  }
928  }
929  }
930 
931  return omega;
932 }
933 
957 template< typename COORD_TYPE, typename POINT_TYPE >
961  arrayView2d< localIndex const > const & elementsToFaces,
962  ArrayOfArraysView< localIndex const > const & facesToNodes,
963  ArrayOfArraysView< localIndex const > const & nodesToElements,
964  arrayView1d< globalIndex const > const & nodeLocalToGlobal,
965  arrayView1d< globalIndex const > const & elementLocalToGlobal,
966  POINT_TYPE const & elemCenter,
967  POINT_TYPE const & point )
968 {
969  return computeWindingNumber( element, nodeCoordinates, elementsToFaces, facesToNodes, nodesToElements, nodeLocalToGlobal, elementLocalToGlobal, elemCenter, point ) > 0;
970 }
971 
981 template< typename NODE_MAP_TYPE, typename VEC_TYPE >
983 void getBoundingBox( localIndex const elemIndex,
984  NODE_MAP_TYPE const & pointIndices,
986  VEC_TYPE && boxDims )
987 {
988  // This holds the min coordinates of the set in each direction
989  R1Tensor minCoords = { LvArray::NumericLimits< real64 >::max,
990  LvArray::NumericLimits< real64 >::max,
991  LvArray::NumericLimits< real64 >::max };
992 
993  // boxDims is used to hold the max coordinates.
994  LvArray::tensorOps::fill< 3 >( boxDims, LvArray::NumericLimits< real64 >::lowest );
995 
996  // loop over all the vertices of the element to get the min and max coords
997  for( localIndex a = 0; a < pointIndices[elemIndex].size(); ++a )
998  {
999  localIndex const id = pointIndices( elemIndex, a );
1000  for( localIndex d = 0; d < 3; ++d )
1001  {
1002  minCoords[ d ] = fmin( minCoords[ d ], pointCoordinates( id, d ) );
1003  boxDims[ d ] = fmax( boxDims[ d ], pointCoordinates( id, d ) );
1004  }
1005  }
1006 
1007  LvArray::tensorOps::subtract< 3 >( boxDims, minCoords );
1008 }
1009 
1016 template< typename FE_TYPE >
1017 GEOS_HOST_DEVICE inline
1018 real64 elementVolume( real64 const (&X)[FE_TYPE::numNodes][3] )
1019 {
1020  real64 result{};
1021  for( localIndex q=0; q<FE_TYPE::numQuadraturePoints; ++q )
1022  {
1023  result = result + FE_TYPE::transformedQuadratureWeight( q, X );
1024  }
1025  return result;
1026 }
1027 
1034 inline
1035 real64 hexahedronVolume( real64 const (&X)[8][3] )
1036 {
1037  return elementVolume< finiteElement::H1_Hexahedron_Lagrange1_GaussLegendre2 >( X );
1038 }
1039 
1046 inline
1047 real64 tetrahedronVolume( real64 const (&X)[4][3] )
1048 {
1049  return elementVolume< finiteElement::H1_Tetrahedron_Lagrange1_Gauss1 >( X );
1050 }
1051 
1058 inline
1059 real64 wedgeVolume( real64 const (&X)[6][3] )
1060 {
1061  return elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( X );
1062 }
1063 
1070 inline
1071 real64 pyramidVolume( real64 const (&X)[5][3] )
1072 {
1073  return elementVolume< finiteElement::H1_Pyramid_Lagrange1_Gauss5 >( X );
1074 }
1075 
1086 template< integer N >
1088 inline
1089 real64 prismVolume( real64 const (&X)[2*N][3] )
1090 {
1091  static_assert( N > 4,
1092  "Function prismVolume can be called for a prism with N-sided polygon base where N > 5." );
1093 
1094  real64 result{};
1095 
1096  // Compute the barycenters of the prism bases
1097  real64 XGBot[3]{};
1098  real64 XGTop[3]{};
1099  for( integer a = 0; a < N; ++a )
1100  {
1101  LvArray::tensorOps::add< 3 >( XGBot, X[a] );
1102  }
1103  for( integer a = N; a < 2 * N; ++a )
1104  {
1105  LvArray::tensorOps::add< 3 >( XGTop, X[a] );
1106  }
1107  LvArray::tensorOps::scale< 3 >( XGBot, 1.0 / N );
1108  LvArray::tensorOps::scale< 3 >( XGTop, 1.0 / N );
1109 
1110  real64 XWedge[6][3];
1111  for( int a = 0; a < N - 1; ++a )
1112  {
1113 
1114  LvArray::tensorOps::copy< 3 >( XWedge[0], X[a] );
1115  LvArray::tensorOps::copy< 3 >( XWedge[1], X[a+N] );
1116  LvArray::tensorOps::copy< 3 >( XWedge[2], X[a+1] );
1117  LvArray::tensorOps::copy< 3 >( XWedge[3], X[a+1+N] );
1118  LvArray::tensorOps::copy< 3 >( XWedge[4], XGBot );
1119  LvArray::tensorOps::copy< 3 >( XWedge[5], XGTop );
1120  result = result + computationalGeometry::elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( XWedge );
1121  }
1122  LvArray::tensorOps::copy< 3 >( XWedge[0], X[N-1] );
1123  LvArray::tensorOps::copy< 3 >( XWedge[1], X[2*N-1] );
1124  LvArray::tensorOps::copy< 3 >( XWedge[2], X[0] );
1125  LvArray::tensorOps::copy< 3 >( XWedge[3], X[N] );
1126  LvArray::tensorOps::copy< 3 >( XWedge[4], XGBot );
1127  LvArray::tensorOps::copy< 3 >( XWedge[5], XGTop );
1128  result = result + computationalGeometry::elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( XWedge );
1129  return result;
1130 }
1131 
1132 } /* namespace computationalGeometry */
1133 } /* namespace geos */
1134 
1135 #endif /* GEOS_MESH_UTILITIES_COMPUTATIONALGEOMETRY_HPP_ */
GEOS_HOST_DEVICE int lexicographicalCompareTriangle(POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az, COORD_TYPE const t1x, COORD_TYPE const t1y, COORD_TYPE const t1z, COORD_TYPE const t2x, COORD_TYPE const t2y, COORD_TYPE const t2z, COORD_TYPE const t3x, COORD_TYPE const t3y, COORD_TYPE const t3z)
Method to perform lexicographic comparison of a node and a triangle based on coordinates.
GEOS_HOST_DEVICE void getBoundingBox(localIndex const elemIndex, NODE_MAP_TYPE const &pointIndices, arrayView2d< real64 const, nodes::REFERENCE_POSITION_USD > const &pointCoordinates, VEC_TYPE &&boxDims)
Compute the dimensions of the bounding box containing the element defined here by the coordinates of ...
GEOS_HOST_DEVICE bool isPointInsideConvexPolyhedronRobust(localIndex element, arrayView2d< COORD_TYPE const, nodes::REFERENCE_POSITION_USD > const &nodeCoordinates, arrayView2d< localIndex const > const &elementsToFaces, ArrayOfArraysView< localIndex const > const &facesToNodes, ArrayOfArraysView< localIndex const > const &nodesToElements, arrayView1d< globalIndex const > const &nodeLocalToGlobal, arrayView1d< globalIndex const > const &elementLocalToGlobal, POINT_TYPE const &elemCenter, POINT_TYPE const &point)
Check if a point is inside a convex polyhedron (3D polygon), using a robust method to avoid ambiguity...
GEOS_HOST_DEVICE void FixNormalOrientation_3D(NORMAL_TYPE &&normal)
Change the orientation of the input vector to be consistent in a global sense.
bool isPointInPolygon3d(POLYGON_TYPE const &polygon, integer const n, POINT_TYPE const &point, real64 const tol=1e-10)
Check if a point is inside a polygon (3D version)
GEOS_HOST_DEVICE real64 prismVolume(real64 const (&X)[2 *N][3])
Compute the volume of a prism with N-sided polygon base.
GEOS_HOST_DEVICE bool isPointInsidePolyhedron(arrayView2d< real64 const, nodes::REFERENCE_POSITION_USD > const &nodeCoordinates, arraySlice1d< localIndex const > const &faceIndices, ArrayOfArraysView< localIndex const > const &facesToNodes, POINT_TYPE const &elemCenter, POINT_TYPE const &point, real64 const areaTolerance=0.0)
Check if a point is inside a convex polyhedron (3D polygon)
GEOS_HOST_DEVICE GEOS_FORCE_INLINE real64 centroid_3DPolygon(arraySlice1d< localIndex const > const pointsIndices, arrayView2d< real64 const, nodes::REFERENCE_POSITION_USD > const &points, CENTER_TYPE &&center, NORMAL_TYPE &&normal, real64 const areaTolerance=0.0)
Calculate the centroid of a convex 3D polygon as well as the normal and the rotation matrix.
GEOS_HOST_DEVICE void RotationMatrix_3D(NORMAL_TYPE const &normal, MATRIX_TYPE &&rotationMatrix)
Calculate the rotation matrix for a face in the 3D space.
GEOS_HOST_DEVICE bool computeWindingNumber(localIndex element, arrayView2d< COORD_TYPE const, nodes::REFERENCE_POSITION_USD > const &nodeCoordinates, arrayView2d< localIndex const > const &elementsToFaces, ArrayOfArraysView< localIndex const > const &facesToNodes, ArrayOfArraysView< localIndex const > const &nodesToElements, arrayView1d< globalIndex const > const &nodeLocalToGlobal, arrayView1d< globalIndex const > const &elementLocalToGlobal, POINT_TYPE const &elemCenter, POINT_TYPE const &point)
Computes the winding number of a point with respect to a mesh element.
GEOS_HOST_DEVICE int findVertexRefElement(arraySlice1d< localIndex const > const &nodeElements, arrayView1d< globalIndex const > const &elementGlobalIndex)
Method to find the reference element touching a vertex. The element with the lowest global ID is chos...
bool isPointInPolygon2d(POLYGON_TYPE const &polygon, integer n, POINT_TYPE const &point, real64 const tol=1e-10)
Check if a point is inside a polygon (2D version)
constexpr real64 machinePrecision
Machine epsilon for double-precision calculations.
GEOS_HOST_DEVICE real64 elementVolume(real64 const (&X)[FE_TYPE::numNodes][3])
Compute the volume of an element (tetrahedron, pyramid, wedge, hexahedron)
GEOS_HOST_DEVICE int findTriangleRefElement(arraySlice1d< localIndex const > const &nodeElements1, arraySlice1d< localIndex const > const &nodeElements2, arraySlice1d< localIndex const > const &nodeElements3, arrayView1d< globalIndex const > const &elementGlobalIndex)
Method to find the reference element for a triangle. The element with the lowest global ID is chosen ...
GEOS_HOST_DEVICE real64 hexahedronVolume(real64 const (&X)[8][3])
Compute the volume of an hexahedron.
array1d< int > orderPointsCCW(arrayView2d< real64 > const &points, NORMAL_TYPE const &normal)
Reorder a set of points counter-clockwise.
GEOS_HOST_DEVICE int lexicographicalCompareVertex(POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az, COORD_TYPE const bx, COORD_TYPE const by, COORD_TYPE const bz)
Method to perform lexicographic comparison of two nodes based on coordinates.
GEOS_HOST_DEVICE real64 tetrahedronVolume(real64 const (&X)[4][3])
Compute the volume of an tetrahedron.
void LinePlaneIntersection(LINEDIR_TYPE const &lineDir, POINT_TYPE const &linePoint, NORMAL_TYPE const &planeNormal, ORIGIN_TYPE const &planeOrigin, INTPOINT_TYPE &intersectionPoint)
Calculate the intersection between a line and a plane.
GEOS_HOST_DEVICE int findEdgeRefElement(arraySlice1d< localIndex const > const &nodeElements1, arraySlice1d< localIndex const > const &nodeElements2, arrayView1d< globalIndex const > const &elementGlobalIndex)
Method to find the reference element for an edge. The element with the lowest global ID is chosen fro...
real64 ComputeSurfaceArea(arrayView2d< real64 const > const &points, NORMAL_TYPE const &&normal)
Calculate the area of a polygon given the set of points in ccw order defining it.
GEOS_HOST_DEVICE real64 wedgeVolume(real64 const (&X)[6][3])
Compute the volume of a wedge.
GEOS_HOST_DEVICE real64 pyramidVolume(real64 const (&X)[5][3])
Compute the volume of a pyramid.
GEOS_HOST_DEVICE GEOS_FORCE_INLINE real64 computeDiameter(POINT_COORDS_TYPE points, localIndex const &numPoints)
Calculate the diameter of a set of points in a given dimension.
GEOS_HOST_DEVICE GEOS_FORCE_INLINE int sign(T const val)
Return the sign of a given value as an integer.
GEOS_HOST_DEVICE int lexicographicalCompareEdge(POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az, COORD_TYPE const e1x, COORD_TYPE const e1y, COORD_TYPE const e1z, COORD_TYPE const e2x, COORD_TYPE const e2y, COORD_TYPE const e2z)
Method to perform lexicographic comparison of a node and an edge based on coordinates.
#define GEOS_HOST_DEVICE
Marks a host-device function.
Definition: GeosxMacros.hpp:49
#define GEOS_FORCE_INLINE
Marks a function or lambda for inlining.
Definition: GeosxMacros.hpp:51
#define GEOS_LOG_RANK(msg)
Log a message to the rank output stream.
Definition: Logger.hpp:126
#define GEOS_ERROR_IF_LT(lhs, rhs)
Raise a hard error if one value compares less than the other.
Definition: Logger.hpp:355
#define GEOS_ERROR(msg)
Raise a hard error and terminate the program.
Definition: Logger.hpp:157
#define GEOS_ERROR_IF(EXP, msg)
Conditionally raise a hard error and terminate the program.
Definition: Logger.hpp:142
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:179
Array< T, 2, PERMUTATION > array2d
Alias for 2D array.
Definition: DataTypes.hpp:191
GEOS_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:87
LvArray::ArrayOfArraysView< T, INDEX_TYPE const, CONST_SIZES, LvArray::ChaiBuffer > ArrayOfArraysView
View of array of variable-sized arrays. See LvArray::ArrayOfArraysView for details.
Definition: DataTypes.hpp:285
double real64
64-bit floating point type.
Definition: DataTypes.hpp:98
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:84
ArraySlice< T, 1, USD > arraySlice1d
Alias for 1D array slice.
Definition: DataTypes.hpp:183
ArrayView< T, 2, USD > arrayView2d
Alias for 2D array view.
Definition: DataTypes.hpp:195
int integer
Signed integer type.
Definition: DataTypes.hpp:81
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:175