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 #if defined(GEOS_DEVICE_COMPILE)
278  GEOS_ERROR( "Negative area found" );
279 #else
280  GEOS_ERROR( GEOS_FMT( "Negative area found : {}", area ) );
281 #endif
282  }
283  else
284  {
285  return 0.0;
286  }
287 
288  return area;
289 }
290 
296 template< typename NORMAL_TYPE >
298 void FixNormalOrientation_3D( NORMAL_TYPE && normal )
299 {
300  real64 const orientationTolerance = 10 * machinePrecision;
301 
302  // Orient local normal in global sense.
303  // First check: align with z direction
304  if( normal[ 2 ] <= -orientationTolerance )
305  {
306  LvArray::tensorOps::scale< 3 >( normal, -1.0 );
307  }
308  else if( std::fabs( normal[ 2 ] ) < orientationTolerance )
309  {
310  // If needed, second check: align with y direction
311  if( normal[ 1 ] <= -orientationTolerance )
312  {
313  LvArray::tensorOps::scale< 3 >( normal, -1.0 );
314  }
315  else if( fabs( normal[ 1 ] ) < orientationTolerance )
316  {
317  // If needed, third check: align with x direction
318  if( normal[ 0 ] <= -orientationTolerance )
319  {
320  LvArray::tensorOps::scale< 3 >( normal, -1.0 );
321  }
322  }
323  }
324 }
325 
333 template< typename NORMAL_TYPE, typename MATRIX_TYPE >
335 void RotationMatrix_3D( NORMAL_TYPE const & normal,
336  MATRIX_TYPE && rotationMatrix )
337 {
338  real64 m1[ 3 ] = { normal[ 2 ], 0.0, -normal[ 0 ] };
339  real64 m2[ 3 ] = { 0.0, normal[ 2 ], -normal[ 1 ] };
340  real64 const norm_m1 = LvArray::tensorOps::l2Norm< 3 >( m1 );
341  real64 const norm_m2 = LvArray::tensorOps::l2Norm< 3 >( m2 );
342 
343  // If present, looks for a vector with 0 norm
344  // Fix the uncertain case of norm_m1 very close to norm_m2
345  if( norm_m1+1.e+2*machinePrecision > norm_m2 )
346  {
347  LvArray::tensorOps::crossProduct( m2, normal, m1 );
348  LvArray::tensorOps::normalize< 3 >( m2 );
349  LvArray::tensorOps::normalize< 3 >( m1 );
350  }
351  else
352  {
353  LvArray::tensorOps::crossProduct( m1, normal, m2 );
354  LvArray::tensorOps::scale< 3 >( m1, -1 );
355  LvArray::tensorOps::normalize< 3 >( m1 );
356  LvArray::tensorOps::normalize< 3 >( m2 );
357  }
358 
359  // Save everything in the standard form (3x3 rotation matrix)
360  rotationMatrix[ 0 ][ 0 ] = normal[ 0 ];
361  rotationMatrix[ 1 ][ 0 ] = normal[ 1 ];
362  rotationMatrix[ 2 ][ 0 ] = normal[ 2 ];
363  rotationMatrix[ 0 ][ 1 ] = m1[ 0 ];
364  rotationMatrix[ 1 ][ 1 ] = m1[ 1 ];
365  rotationMatrix[ 2 ][ 1 ] = m1[ 2 ];
366  rotationMatrix[ 0 ][ 2 ] = m2[ 0 ];
367  rotationMatrix[ 1 ][ 2 ] = m2[ 1 ];
368  rotationMatrix[ 2 ][ 2 ] = m2[ 2 ];
369 
370  GEOS_ERROR_IF( fabs( LvArray::tensorOps::determinant< 3 >( rotationMatrix ) - 1.0 ) > 1.e+1 * machinePrecision,
371  "Rotation matrix with determinant different from +1.0" );
372 }
373 
380 template< typename T >
383 int sign( T const val )
384 {
385  return (T( 0 ) < val) - (val < T( 0 ));
386 }
387 
401 template< typename POINT_TYPE >
404  arraySlice1d< localIndex const > const & faceIndices,
405  ArrayOfArraysView< localIndex const > const & facesToNodes,
406  POINT_TYPE const & elemCenter,
407  POINT_TYPE const & point,
408  real64 const areaTolerance = 0.0 )
409 {
410  localIndex const numFaces = faceIndices.size();
411  R1Tensor faceCenter, faceNormal, cellToFaceVec;
412 
413  for( localIndex kf = 0; kf < numFaces; ++kf )
414  {
415  // compute the face normal at this face
416  localIndex const faceIndex = faceIndices[kf];
417  centroid_3DPolygon( facesToNodes[faceIndex], nodeCoordinates, faceCenter, faceNormal, areaTolerance );
418 
419  // make sure that the normal is outward pointing
420  LvArray::tensorOps::copy< 3 >( cellToFaceVec, faceCenter );
421  LvArray::tensorOps::subtract< 3 >( cellToFaceVec, elemCenter );
422  if( LvArray::tensorOps::AiBi< 3 >( cellToFaceVec, faceNormal ) < 0.0 )
423  {
424  LvArray::tensorOps::scale< 3 >( faceNormal, -1 );
425  }
426 
427  // compute the vector face center to query point
428  LvArray::tensorOps::subtract< 3 >( faceCenter, point );
429  int const s = sign( LvArray::tensorOps::AiBi< 3 >( faceNormal, faceCenter ) );
430 
431  // all dot products should be non-negative (we enforce outward normals)
432  if( s < 0 )
433  {
434  return false;
435  }
436  }
437  return true;
438 }
439 
450 template< typename POLYGON_TYPE, typename POINT_TYPE >
451 bool isPointInPolygon2d( POLYGON_TYPE const & polygon,
452  integer n,
453  POINT_TYPE const & point,
454  real64 const tol = 1e-10 )
455 {
456  integer count = 0;
457 
458  for( integer i = 0; i < n; ++i )
459  {
460  auto const & p1 = polygon[i];
461  auto const & p2 = polygon[(i + 1) % n];
462 
463  real64 y1 = p1[1], y2 = p2[1];
464  real64 x1 = p1[0], x2 = p2[0];
465  real64 py = point[1], px = point[0];
466 
467  // quick reject in y with tolerance
468  if( py + tol < std::min( y1, y2 ) || py - tol > std::max( y1, y2 ) )
469  continue;
470 
471  // check if point is (approximately) on the segment
472  // parametric t for projection on segment in y (if segment vertical-ish use x)
473  if( std::abs( (x2 - x1) * (py - y1) - (px - x1) * (y2 - y1) ) < tol *
474  ( std::hypot( x2 - x1, y2 - y1 ) + 1.0 ) )
475  {
476  // ensure px is between x1,x2 and py between y1,y2 (with tol)
477  if( px + tol >= std::min( x1, x2 ) && px - tol <= std::max( x1, x2 ) &&
478  py + tol >= std::min( y1, y2 ) && py - tol <= std::max( y1, y2 ) )
479  return true; // on boundary -> consider inside
480  }
481 
482  // ignore nearly-horizontal edges for intersection counting
483  if( std::abs( y2 - y1 ) < tol )
484  continue;
485 
486  // compute x coordinate of intersection of horizontal line py with segment p1-p2
487  real64 xIntersect = x1 + (py - y1) * (x2 - x1) / (y2 - y1);
488 
489  // count crossing where intersection is strictly to the right of point (robust with tol)
490  if( px < xIntersect - tol )
491  ++count;
492  }
493 
494  return (count % 2) == 1;
495 }
496 
507 template< typename POLYGON_TYPE, typename POINT_TYPE >
508 bool isPointInPolygon3d( POLYGON_TYPE const & polygon,
509  integer const n,
510  POINT_TYPE const & point,
511  real64 const tol = 1e-10 )
512 {
513  // Check if the point lies in the plane of the polygon
514  auto const & p0 = polygon[0];
515  POINT_TYPE normal = {0, 0, 0};
516  for( integer i = 1; i < n - 1; i++ )
517  {
518  auto const & p1 = polygon[i];
519  auto const & p2 = polygon[i + 1];
520  normal[0] += (p1[1] - p0[1]) * (p2[2] - p0[2]) - (p1[2] - p0[2]) * (p2[1] - p0[1]);
521  normal[1] += (p1[2] - p0[2]) * (p2[0] - p0[0]) - (p1[0] - p0[0]) * (p2[2] - p0[2]);
522  normal[2] += (p1[0] - p0[0]) * (p2[1] - p0[1]) - (p1[1] - p0[1]) * (p2[0] - p0[0]);
523  }
524 
525  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]);
526 
527  if( std::abs( dist ) > tol )
528  {
529  return false;
530  }
531 
532  // Determine the dominant component of the normal vector
533  int dominantIndex = 0;
534  if( std::abs( normal[1] ) > std::abs( normal[0] ))
535  {
536  dominantIndex = 1;
537  }
538  if( std::abs( normal[2] ) > std::abs( normal[dominantIndex] ))
539  {
540  dominantIndex = 2;
541  }
542 
543  // Project the polygon and the point onto a 2D plane
544  POLYGON_TYPE projectedPolygon( n );
545  POINT_TYPE projectedPoint;
546  if( dominantIndex == 0 ) // X is dominant, project onto YZ plane
547  {
548  for( int i = 0; i < n; i++ )
549  {
550  projectedPolygon[i][0] = polygon[i][1];
551  projectedPolygon[i][1] = polygon[i][2];
552  }
553  projectedPoint[0] = point[1];
554  projectedPoint[1] = point[2];
555  }
556  else if( dominantIndex == 1 ) // Y is dominant, project onto XZ plane
557  {
558  for( int i = 0; i < n; i++ )
559  {
560  projectedPolygon[i][0] = polygon[i][0];
561  projectedPolygon[i][1] = polygon[i][2];
562  }
563  projectedPoint[0] = point[0];
564  projectedPoint[1] = point[2];
565  }
566  else // Z is dominant, project onto XY plane
567  {
568  for( int i = 0; i < n; i++ )
569  {
570  projectedPolygon[i][0] = polygon[i][0];
571  projectedPolygon[i][1] = polygon[i][1];
572  }
573  projectedPoint[0] = point[0];
574  projectedPoint[1] = point[1];
575  }
576 
577  return isPointInPolygon2d( projectedPolygon, n, projectedPoint );
578 }
579 
592 template< typename COORD_TYPE, typename POINT_TYPE >
594 int lexicographicalCompareVertex( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
595  COORD_TYPE const bx, COORD_TYPE const by, COORD_TYPE const bz )
596 {
597  if( ax < bx )
598  return -1;
599  else if( ax > bx )
600  return 1;
601  if( ay < by )
602  return -1;
603  else if( ay > by )
604  return 1;
605  if( az < bz )
606  return -1;
607  else if( az > bz )
608  return 1;
609  return 0;
610 }
611 
627 template< typename COORD_TYPE, typename POINT_TYPE >
629 int lexicographicalCompareEdge( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
630  COORD_TYPE const e1x, COORD_TYPE const e1y, COORD_TYPE const e1z,
631  COORD_TYPE const e2x, COORD_TYPE const e2y, COORD_TYPE const e2z )
632 {
633  return lexicographicalCompareVertex( ( e1y - ay ) * ( e2x - ax ),
634  ( e1z - az ) * ( e2x - ax ),
635  ( e1z - az ) * ( e2y - ay ),
636  ( e1x - ax ) * ( e2y - ay ),
637  ( e1x - ax ) * ( e2z - az ),
638  ( e1y - ay ) * ( e2z - az ) );
639 }
640 
659 template< typename COORD_TYPE, typename POINT_TYPE >
661 int lexicographicalCompareTriangle( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
662  COORD_TYPE const t1x, COORD_TYPE const t1y, COORD_TYPE const t1z,
663  COORD_TYPE const t2x, COORD_TYPE const t2y, COORD_TYPE const t2z,
664  COORD_TYPE const t3x, COORD_TYPE const t3y, COORD_TYPE const t3z )
665 {
666  COORD_TYPE v1x = t1x - ax;
667  COORD_TYPE v1y = t1y - ay;
668  COORD_TYPE v1z = t1z - az;
669  COORD_TYPE v2x = t2x - ax;
670  COORD_TYPE v2y = t2y - ay;
671  COORD_TYPE v2z = t2z - az;
672  COORD_TYPE v3x = t3x - ax;
673  COORD_TYPE v3y = t3y - ay;
674  COORD_TYPE v3z = t3z - az;
675  COORD_TYPE sign = ( v1x * v2y - v1y * v2x ) * v3z +
676  ( v2x * v3y - v2y * v3x ) * v1z +
677  ( v3x * v1y - v3y * v1x ) * v2z;
678  if( sign > 0 )
679  return 1;
680  else if( sign < 0 )
681  return -1;
682  return 0;
683 }
691 template< typename ... LIST_TYPE >
694  arrayView1d< globalIndex const > const & elementGlobalIndex )
695 {
696  localIndex minElement = -1;
697  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
698  for( int i = 0; i < nodeElements.size(); i++ )
699  {
700  localIndex e = nodeElements( i );
701  if( elementGlobalIndex[ e ] < minElementGID )
702  {
703  minElementGID = elementGlobalIndex[ e ];
704  minElement = e;
705  }
706  }
707  return minElement;
708 }
709 
718 template< typename ... LIST_TYPE >
721  arraySlice1d< localIndex const > const & nodeElements2,
722  arrayView1d< globalIndex const > const & elementGlobalIndex )
723 {
724  localIndex minElement = -1;
725  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
726  for( int i = 0; i < nodeElements1.size(); i++ )
727  {
728  localIndex e1 = nodeElements1( i );
729  for( int j = 0; j < nodeElements2.size(); j++ )
730  {
731  localIndex e2 = nodeElements2( j );
732  if( e1 == e2 )
733  {
734  if( elementGlobalIndex[ e1 ] < minElementGID )
735  {
736  minElementGID = elementGlobalIndex[ e1 ];
737  minElement = e1;
738  }
739  }
740  }
741  }
742  return minElement;
743 }
744 
754 template< typename ... LIST_TYPE >
757  arraySlice1d< localIndex const > const & nodeElements2,
758  arraySlice1d< localIndex const > const & nodeElements3,
759  arrayView1d< globalIndex const > const & elementGlobalIndex )
760 {
761  localIndex minElement = -1;
762  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
763  for( int i = 0; i < nodeElements1.size(); i++ )
764  {
765  localIndex e1 = nodeElements1( i );
766  for( int j = 0; j < nodeElements2.size(); j++ )
767  {
768  localIndex e2 = nodeElements2( j );
769  for( int k = 0; k < nodeElements3.size(); k++ )
770  {
771  localIndex e3 = nodeElements3( k );
772  if( e1 == e2 && e2 == e3 )
773  {
774  if( elementGlobalIndex[ e1 ] < minElementGID )
775  {
776  minElementGID = elementGlobalIndex[ e1 ];
777  minElement = e1;
778  }
779  }
780  }
781  }
782  }
783  return minElement;
784 }
785 
800 template< typename COORD_TYPE, typename POINT_TYPE >
804  arrayView2d< localIndex const > const & elementsToFaces,
805  ArrayOfArraysView< localIndex const > const & facesToNodes,
806  ArrayOfArraysView< localIndex const > const & nodesToElements,
807  arrayView1d< globalIndex const > const & nodeLocalToGlobal,
808  arrayView1d< globalIndex const > const & elementLocalToGlobal,
809  POINT_TYPE const & elemCenter,
810  POINT_TYPE const & point )
811 {
812  arraySlice1d< localIndex const > const & faceIndices = elementsToFaces[ element ];
813  localIndex const numFaces = faceIndices.size();
814  int omega = 0;
815  for( localIndex kf = 0; kf < numFaces; ++kf )
816  {
817  // triangulate the face. The triangulation must be done in a consistent way across ranks.
818  // This can be achieved by always picking the vertex with the lowest global index as root.
819  localIndex const faceIndex = faceIndices[kf];
820  globalIndex minGlobalId = LvArray::NumericLimits< globalIndex >::max;
821  localIndex minVertex = -1;
822  localIndex numFaceVertices = facesToNodes[faceIndex].size();
823  for( localIndex v = 0; v < numFaceVertices; v++ )
824  {
825  localIndex vIndex = facesToNodes( faceIndex, v );
826  globalIndex globalId = nodeLocalToGlobal[ vIndex ];
827  if( globalId < minGlobalId )
828  {
829  minGlobalId = globalId;
830  minVertex = vIndex;
831  }
832  }
833  // triangulate the face using the minimum-id vertex as root
834  localIndex vi[ 3 ] = { minVertex, -1, -1 };
835  for( localIndex v = 0; v < numFaceVertices; v++ )
836  {
837  vi[ 1 ] = facesToNodes( faceIndex, v );
838  vi[ 2 ] = facesToNodes( faceIndex, (v + 1) % numFaceVertices );
839  if( vi[ 1 ] != minVertex && vi[ 2 ] != minVertex )
840  {
841  // To make the algorithm independent of rank, always take the two additional vertices in increasing global ID
842  if( nodeLocalToGlobal[ vi[ 1 ] ] > nodeLocalToGlobal[ vi[ 2 ] ] )
843  {
844  localIndex temp = vi[ 1 ];
845  vi[ 1 ] = vi[ 2 ];
846  vi[ 2 ] = temp;
847  }
848  COORD_TYPE v1x = nodeCoordinates( vi[ 0 ], 0 );
849  COORD_TYPE v1y = nodeCoordinates( vi[ 0 ], 1 );
850  COORD_TYPE v1z = nodeCoordinates( vi[ 0 ], 2 );
851  COORD_TYPE v2x = nodeCoordinates( vi[ 1 ], 0 );
852  COORD_TYPE v2y = nodeCoordinates( vi[ 1 ], 1 );
853  COORD_TYPE v2z = nodeCoordinates( vi[ 1 ], 2 );
854  COORD_TYPE v3x = nodeCoordinates( vi[ 2 ], 0 );
855  COORD_TYPE v3y = nodeCoordinates( vi[ 2 ], 1 );
856  COORD_TYPE v3z = nodeCoordinates( vi[ 2 ], 2 );
857  // check the orientation of this triangle
858  R1Tensor vv1 = { v2x - v1x, v2y - v1y, v2z - v1z };
859  R1Tensor vv2 = { v3x - v1x, v3y - v1y, v3z - v1z };
860  R1Tensor dist = { elemCenter[ 0 ] - ( v1x + v2x + v3x )/3.0,
861  elemCenter[ 1 ] - ( v1y + v2y + v3y )/3.0,
862  elemCenter[ 2 ] - ( v1z + v2z + v3z )/3.0 };
863  R1Tensor norm = { };
864  LvArray::tensorOps::crossProduct( norm, vv1, vv2 );
865  // check if face is oriented coherently, and change sign otherwise
866  int sign = LvArray::tensorOps::AiBi< 3 >( norm, dist ) > 0 ? -1 : +1;
867  // Compute the winding number contributed by this triangle
868  int cmp1 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v1x, v1y, v1z );
869  if( cmp1 == 0 )
870  {
871  return findVertexRefElement( nodesToElements[ vi[ 0 ] ], elementLocalToGlobal ) == element;
872  }
873  int cmp2 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v2x, v2y, v2z );
874  if( cmp2 == 0 )
875  {
876  return findVertexRefElement( nodesToElements[ vi[ 1 ] ], elementLocalToGlobal ) == element;
877  }
878  int cmp3 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v3x, v3y, v3z );
879  if( cmp3 == 0 )
880  {
881  return findVertexRefElement( nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
882  }
883  int facecmp = 0;
884  int edgecmp = 0;
885  if( cmp1 != cmp2 )
886  {
887  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
888  v1x, v1y, v1z,
889  v2x, v2y, v2z );
890  if( edgecmp == 0 )
891  {
892  return findEdgeRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 1 ] ], elementLocalToGlobal ) == element;
893  }
894  facecmp += sign * edgecmp;
895  }
896  if( cmp2 != cmp3 )
897  {
898  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
899  v2x, v2y, v2z,
900  v3x, v3y, v3z );
901  if( edgecmp == 0 )
902  {
903  return findEdgeRefElement( nodesToElements[ vi[ 1 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
904  }
905  facecmp += sign * edgecmp;
906  }
907  if( cmp3 != cmp1 )
908  {
909  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
910  v3x, v3y, v3z,
911  v1x, v1y, v1z );
912  if( edgecmp == 0 )
913  {
914  return findEdgeRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
915  }
916  facecmp += sign * edgecmp;
917  }
918  // if all edges are on the same side, this triangle does not contribute to the winding number
919  if( facecmp == 0 )
920  continue;
921  facecmp = lexicographicalCompareTriangle( point[ 0 ], point[ 1 ], point[ 2 ],
922  v1x, v1y, v1z,
923  v2x, v2y, v2z,
924  v3x, v3y, v3z );
925 
926  if( facecmp == 0 )
927  {
928  return findTriangleRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 1 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
929  }
930  omega += sign * facecmp;
931  }
932  }
933  }
934 
935  return omega;
936 }
937 
961 template< typename COORD_TYPE, typename POINT_TYPE >
965  arrayView2d< localIndex const > const & elementsToFaces,
966  ArrayOfArraysView< localIndex const > const & facesToNodes,
967  ArrayOfArraysView< localIndex const > const & nodesToElements,
968  arrayView1d< globalIndex const > const & nodeLocalToGlobal,
969  arrayView1d< globalIndex const > const & elementLocalToGlobal,
970  POINT_TYPE const & elemCenter,
971  POINT_TYPE const & point )
972 {
973  return computeWindingNumber( element, nodeCoordinates, elementsToFaces, facesToNodes, nodesToElements, nodeLocalToGlobal, elementLocalToGlobal, elemCenter, point ) > 0;
974 }
975 
985 template< typename NODE_MAP_TYPE, typename VEC_TYPE >
987 void getBoundingBox( localIndex const elemIndex,
988  NODE_MAP_TYPE const & pointIndices,
990  VEC_TYPE && boxDims )
991 {
992  // This holds the min coordinates of the set in each direction
993  R1Tensor minCoords = { LvArray::NumericLimits< real64 >::max,
994  LvArray::NumericLimits< real64 >::max,
995  LvArray::NumericLimits< real64 >::max };
996 
997  // boxDims is used to hold the max coordinates.
998  LvArray::tensorOps::fill< 3 >( boxDims, LvArray::NumericLimits< real64 >::lowest );
999 
1000  // loop over all the vertices of the element to get the min and max coords
1001  for( localIndex a = 0; a < pointIndices[elemIndex].size(); ++a )
1002  {
1003  localIndex const id = pointIndices( elemIndex, a );
1004  for( localIndex d = 0; d < 3; ++d )
1005  {
1006  minCoords[ d ] = fmin( minCoords[ d ], pointCoordinates( id, d ) );
1007  boxDims[ d ] = fmax( boxDims[ d ], pointCoordinates( id, d ) );
1008  }
1009  }
1010 
1011  LvArray::tensorOps::subtract< 3 >( boxDims, minCoords );
1012 }
1013 
1020 template< typename FE_TYPE >
1021 GEOS_HOST_DEVICE inline
1022 real64 elementVolume( real64 const (&X)[FE_TYPE::numNodes][3] )
1023 {
1024  real64 result{};
1025  for( localIndex q=0; q<FE_TYPE::numQuadraturePoints; ++q )
1026  {
1027  result = result + FE_TYPE::transformedQuadratureWeight( q, X );
1028  }
1029  return result;
1030 }
1031 
1038 inline
1039 real64 hexahedronVolume( real64 const (&X)[8][3] )
1040 {
1041  return elementVolume< finiteElement::H1_Hexahedron_Lagrange1_GaussLegendre2 >( X );
1042 }
1043 
1050 inline
1051 real64 tetrahedronVolume( real64 const (&X)[4][3] )
1052 {
1053  return elementVolume< finiteElement::H1_Tetrahedron_Lagrange1_Gauss1 >( X );
1054 }
1055 
1062 inline
1063 real64 wedgeVolume( real64 const (&X)[6][3] )
1064 {
1065  return elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( X );
1066 }
1067 
1074 inline
1075 real64 pyramidVolume( real64 const (&X)[5][3] )
1076 {
1077  return elementVolume< finiteElement::H1_Pyramid_Lagrange1_Gauss5 >( X );
1078 }
1079 
1090 template< integer N >
1092 inline
1093 real64 prismVolume( real64 const (&X)[2*N][3] )
1094 {
1095  static_assert( N > 4,
1096  "Function prismVolume can be called for a prism with N-sided polygon base where N > 5." );
1097 
1098  real64 result{};
1099 
1100  // Compute the barycenters of the prism bases
1101  real64 XGBot[3]{};
1102  real64 XGTop[3]{};
1103  for( integer a = 0; a < N; ++a )
1104  {
1105  LvArray::tensorOps::add< 3 >( XGBot, X[a] );
1106  }
1107  for( integer a = N; a < 2 * N; ++a )
1108  {
1109  LvArray::tensorOps::add< 3 >( XGTop, X[a] );
1110  }
1111  LvArray::tensorOps::scale< 3 >( XGBot, 1.0 / N );
1112  LvArray::tensorOps::scale< 3 >( XGTop, 1.0 / N );
1113 
1114  real64 XWedge[6][3];
1115  for( int a = 0; a < N - 1; ++a )
1116  {
1117 
1118  LvArray::tensorOps::copy< 3 >( XWedge[0], X[a] );
1119  LvArray::tensorOps::copy< 3 >( XWedge[1], X[a+N] );
1120  LvArray::tensorOps::copy< 3 >( XWedge[2], X[a+1] );
1121  LvArray::tensorOps::copy< 3 >( XWedge[3], X[a+1+N] );
1122  LvArray::tensorOps::copy< 3 >( XWedge[4], XGBot );
1123  LvArray::tensorOps::copy< 3 >( XWedge[5], XGTop );
1124  result = result + computationalGeometry::elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( XWedge );
1125  }
1126  LvArray::tensorOps::copy< 3 >( XWedge[0], X[N-1] );
1127  LvArray::tensorOps::copy< 3 >( XWedge[1], X[2*N-1] );
1128  LvArray::tensorOps::copy< 3 >( XWedge[2], X[0] );
1129  LvArray::tensorOps::copy< 3 >( XWedge[3], X[N] );
1130  LvArray::tensorOps::copy< 3 >( XWedge[4], XGBot );
1131  LvArray::tensorOps::copy< 3 >( XWedge[5], XGTop );
1132  result = result + computationalGeometry::elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( XWedge );
1133  return result;
1134 }
1135 
1136 } /* namespace computationalGeometry */
1137 } /* namespace geos */
1138 
1139 #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_ERROR(...)
Raise a hard error and terminate the program.
Definition: Logger.hpp:226
#define GEOS_LOG_RANK(msg)
Log a message to the rank output stream.
Definition: Logger.hpp:124
#define GEOS_ERROR_IF_LT(lhs, rhs)
Raise a hard error if one value compares less than the other.
Definition: Logger.hpp:530
#define GEOS_ERROR_IF(COND,...)
Conditionally raise a hard error and terminate the program.
Definition: Logger.hpp:217
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