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 ], origin[ 3 ], crossProduct[ 3 ];
249 
250  LvArray::tensorOps::copy< 3 >( next, points[ pointsIndices[ numberOfPoints - 1 ] ] );
251  LvArray::tensorOps::copy< 3 >( origin, points[ pointsIndices[ 0 ]] );
252 
253  for( localIndex a=0; a<numberOfPoints; )
254  {
255  LvArray::tensorOps::copy< 3 >( current, points[ pointsIndices[ a++ ]] );
256  LvArray::tensorOps::scaledAdd< 3 >( current, origin, -1. );
257  LvArray::tensorOps::copy< 3 >( next, points[ pointsIndices[ a % numberOfPoints ] ] );
258  LvArray::tensorOps::scaledAdd< 3 >( next, origin, -1. );
259 
260  LvArray::tensorOps::crossProduct( crossProduct, current, next );
261 
262  LvArray::tensorOps::add< 3 >( normal, crossProduct );
263  LvArray::tensorOps::add< 3 >( center, next );
264  }
265 
266  area = LvArray::tensorOps::l2Norm< 3 >( normal );
267  LvArray::tensorOps::scale< 3 >( center, 1.0 / numberOfPoints );
268  LvArray::tensorOps::scaledAdd< 3 >( center, origin, 1. );
269 
270  if( area > areaTolerance )
271  {
272  LvArray::tensorOps::normalize< 3 >( normal );
273  area *= 0.5;
274  }
275  else if( area < -areaTolerance )
276  {
277  for( localIndex a=0; a<numberOfPoints; ++a )
278  {
279  GEOS_LOG_RANK( "Points: " << points[ pointsIndices[ a ] ] << " " << pointsIndices[ a ] );
280  }
281 #if defined(GEOS_DEVICE_COMPILE)
282  GEOS_ERROR( "Negative area found" );
283 #else
284  GEOS_ERROR( GEOS_FMT( "Negative area found : {}", area ) );
285 #endif
286  }
287  else
288  {
289  for( localIndex a=0; a<numberOfPoints; ++a )
290  {
291  GEOS_LOG_RANK( "Points: " << points[ pointsIndices[ a ] ] << " " << pointsIndices[ a ] );
292  }
293 #if defined(GEOS_DEVICE_COMPILE)
294  GEOS_ERROR( "Null area found" );
295 #else
296  GEOS_ERROR( GEOS_FMT( "Null area found : {}", area ) );
297 #endif
298 
299  return 0.0;
300  }
301 
302  return area;
303 }
304 
310 template< typename NORMAL_TYPE >
312 void FixNormalOrientation_3D( NORMAL_TYPE && normal )
313 {
314  real64 const orientationTolerance = 10 * machinePrecision;
315 
316  // Orient local normal in global sense.
317  // First check: align with z direction
318  if( normal[ 2 ] <= -orientationTolerance )
319  {
320  LvArray::tensorOps::scale< 3 >( normal, -1.0 );
321  }
322  else if( std::fabs( normal[ 2 ] ) < orientationTolerance )
323  {
324  // If needed, second check: align with y direction
325  if( normal[ 1 ] <= -orientationTolerance )
326  {
327  LvArray::tensorOps::scale< 3 >( normal, -1.0 );
328  }
329  else if( fabs( normal[ 1 ] ) < orientationTolerance )
330  {
331  // If needed, third check: align with x direction
332  if( normal[ 0 ] <= -orientationTolerance )
333  {
334  LvArray::tensorOps::scale< 3 >( normal, -1.0 );
335  }
336  }
337  }
338 }
339 
347 template< typename NORMAL_TYPE, typename MATRIX_TYPE >
349 void RotationMatrix_3D( NORMAL_TYPE const & normal,
350  MATRIX_TYPE && rotationMatrix )
351 {
352  real64 m1[ 3 ] = { normal[ 2 ], 0.0, -normal[ 0 ] };
353  real64 m2[ 3 ] = { 0.0, normal[ 2 ], -normal[ 1 ] };
354  real64 const norm_m1 = LvArray::tensorOps::l2Norm< 3 >( m1 );
355  real64 const norm_m2 = LvArray::tensorOps::l2Norm< 3 >( m2 );
356 
357  // If present, looks for a vector with 0 norm
358  // Fix the uncertain case of norm_m1 very close to norm_m2
359  if( norm_m1+1.e+2*machinePrecision > norm_m2 )
360  {
361  LvArray::tensorOps::crossProduct( m2, normal, m1 );
362  LvArray::tensorOps::normalize< 3 >( m2 );
363  LvArray::tensorOps::normalize< 3 >( m1 );
364  }
365  else
366  {
367  LvArray::tensorOps::crossProduct( m1, normal, m2 );
368  LvArray::tensorOps::scale< 3 >( m1, -1 );
369  LvArray::tensorOps::normalize< 3 >( m1 );
370  LvArray::tensorOps::normalize< 3 >( m2 );
371  }
372 
373  // Save everything in the standard form (3x3 rotation matrix)
374  rotationMatrix[ 0 ][ 0 ] = normal[ 0 ];
375  rotationMatrix[ 1 ][ 0 ] = normal[ 1 ];
376  rotationMatrix[ 2 ][ 0 ] = normal[ 2 ];
377  rotationMatrix[ 0 ][ 1 ] = m1[ 0 ];
378  rotationMatrix[ 1 ][ 1 ] = m1[ 1 ];
379  rotationMatrix[ 2 ][ 1 ] = m1[ 2 ];
380  rotationMatrix[ 0 ][ 2 ] = m2[ 0 ];
381  rotationMatrix[ 1 ][ 2 ] = m2[ 1 ];
382  rotationMatrix[ 2 ][ 2 ] = m2[ 2 ];
383 
384  GEOS_ERROR_IF( fabs( LvArray::tensorOps::determinant< 3 >( rotationMatrix ) - 1.0 ) > 1.e+1 * machinePrecision,
385  "Rotation matrix with determinant different from +1.0" );
386 }
387 
394 template< typename T >
397 int sign( T const val )
398 {
399  return (T( 0 ) < val) - (val < T( 0 ));
400 }
401 
415 template< typename POINT_TYPE >
418  arraySlice1d< localIndex const > const & faceIndices,
419  ArrayOfArraysView< localIndex const > const & facesToNodes,
420  POINT_TYPE const & elemCenter,
421  POINT_TYPE const & point,
422  real64 const areaTolerance = 0.0 )
423 {
424  localIndex const numFaces = faceIndices.size();
425  R1Tensor faceCenter, faceNormal, cellToFaceVec;
426 
427  for( localIndex kf = 0; kf < numFaces; ++kf )
428  {
429  // compute the face normal at this face
430  localIndex const faceIndex = faceIndices[kf];
431  centroid_3DPolygon( facesToNodes[faceIndex], nodeCoordinates, faceCenter, faceNormal, areaTolerance );
432 
433  // make sure that the normal is outward pointing
434  LvArray::tensorOps::copy< 3 >( cellToFaceVec, faceCenter );
435  LvArray::tensorOps::subtract< 3 >( cellToFaceVec, elemCenter );
436  if( LvArray::tensorOps::AiBi< 3 >( cellToFaceVec, faceNormal ) < 0.0 )
437  {
438  LvArray::tensorOps::scale< 3 >( faceNormal, -1 );
439  }
440 
441  // compute the vector face center to query point
442  LvArray::tensorOps::subtract< 3 >( faceCenter, point );
443  int const s = sign( LvArray::tensorOps::AiBi< 3 >( faceNormal, faceCenter ) );
444 
445  // all dot products should be non-negative (we enforce outward normals)
446  if( s < 0 )
447  {
448  return false;
449  }
450  }
451  return true;
452 }
453 
464 template< typename POLYGON_TYPE, typename POINT_TYPE >
465 bool isPointInPolygon2d( POLYGON_TYPE const & polygon,
466  integer n,
467  POINT_TYPE const & point,
468  real64 const tol = 1e-10 )
469 {
470  integer count = 0;
471 
472  for( integer i = 0; i < n; ++i )
473  {
474  auto const & p1 = polygon[i];
475  auto const & p2 = polygon[(i + 1) % n];
476 
477  real64 y1 = p1[1], y2 = p2[1];
478  real64 x1 = p1[0], x2 = p2[0];
479  real64 py = point[1], px = point[0];
480 
481  // quick reject in y with tolerance
482  if( py + tol < std::min( y1, y2 ) || py - tol > std::max( y1, y2 ) )
483  continue;
484 
485  // check if point is (approximately) on the segment
486  // parametric t for projection on segment in y (if segment vertical-ish use x)
487  if( std::abs( (x2 - x1) * (py - y1) - (px - x1) * (y2 - y1) ) < tol *
488  ( std::hypot( x2 - x1, y2 - y1 ) + 1.0 ) )
489  {
490  // ensure px is between x1,x2 and py between y1,y2 (with tol)
491  if( px + tol >= std::min( x1, x2 ) && px - tol <= std::max( x1, x2 ) &&
492  py + tol >= std::min( y1, y2 ) && py - tol <= std::max( y1, y2 ) )
493  return true; // on boundary -> consider inside
494  }
495 
496  // ignore nearly-horizontal edges for intersection counting
497  if( std::abs( y2 - y1 ) < tol )
498  continue;
499 
500  // compute x coordinate of intersection of horizontal line py with segment p1-p2
501  real64 xIntersect = x1 + (py - y1) * (x2 - x1) / (y2 - y1);
502 
503  // count crossing where intersection is strictly to the right of point (robust with tol)
504  if( px < xIntersect - tol )
505  ++count;
506  }
507 
508  return (count % 2) == 1;
509 }
510 
521 template< typename POLYGON_TYPE, typename POINT_TYPE >
522 bool isPointInPolygon3d( POLYGON_TYPE const & polygon,
523  integer const n,
524  POINT_TYPE const & point,
525  real64 const tol = 1e-10 )
526 {
527  // Check if the point lies in the plane of the polygon
528  auto const & p0 = polygon[0];
529  POINT_TYPE normal = {0, 0, 0};
530  for( integer i = 1; i < n - 1; i++ )
531  {
532  auto const & p1 = polygon[i];
533  auto const & p2 = polygon[i + 1];
534  normal[0] += (p1[1] - p0[1]) * (p2[2] - p0[2]) - (p1[2] - p0[2]) * (p2[1] - p0[1]);
535  normal[1] += (p1[2] - p0[2]) * (p2[0] - p0[0]) - (p1[0] - p0[0]) * (p2[2] - p0[2]);
536  normal[2] += (p1[0] - p0[0]) * (p2[1] - p0[1]) - (p1[1] - p0[1]) * (p2[0] - p0[0]);
537  }
538 
539  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]);
540 
541  if( std::abs( dist ) > tol )
542  {
543  return false;
544  }
545 
546  // Determine the dominant component of the normal vector
547  int dominantIndex = 0;
548  if( std::abs( normal[1] ) > std::abs( normal[0] ))
549  {
550  dominantIndex = 1;
551  }
552  if( std::abs( normal[2] ) > std::abs( normal[dominantIndex] ))
553  {
554  dominantIndex = 2;
555  }
556 
557  // Project the polygon and the point onto a 2D plane
558  POLYGON_TYPE projectedPolygon( n );
559  POINT_TYPE projectedPoint;
560  if( dominantIndex == 0 ) // X is dominant, project onto YZ plane
561  {
562  for( int i = 0; i < n; i++ )
563  {
564  projectedPolygon[i][0] = polygon[i][1];
565  projectedPolygon[i][1] = polygon[i][2];
566  }
567  projectedPoint[0] = point[1];
568  projectedPoint[1] = point[2];
569  }
570  else if( dominantIndex == 1 ) // Y is dominant, project onto XZ plane
571  {
572  for( int i = 0; i < n; i++ )
573  {
574  projectedPolygon[i][0] = polygon[i][0];
575  projectedPolygon[i][1] = polygon[i][2];
576  }
577  projectedPoint[0] = point[0];
578  projectedPoint[1] = point[2];
579  }
580  else // Z is dominant, project onto XY plane
581  {
582  for( int i = 0; i < n; i++ )
583  {
584  projectedPolygon[i][0] = polygon[i][0];
585  projectedPolygon[i][1] = polygon[i][1];
586  }
587  projectedPoint[0] = point[0];
588  projectedPoint[1] = point[1];
589  }
590 
591  return isPointInPolygon2d( projectedPolygon, n, projectedPoint );
592 }
593 
606 template< typename COORD_TYPE, typename POINT_TYPE >
608 int lexicographicalCompareVertex( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
609  COORD_TYPE const bx, COORD_TYPE const by, COORD_TYPE const bz )
610 {
611  if( ax < bx )
612  return -1;
613  else if( ax > bx )
614  return 1;
615  if( ay < by )
616  return -1;
617  else if( ay > by )
618  return 1;
619  if( az < bz )
620  return -1;
621  else if( az > bz )
622  return 1;
623  return 0;
624 }
625 
641 template< typename COORD_TYPE, typename POINT_TYPE >
643 int lexicographicalCompareEdge( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
644  COORD_TYPE const e1x, COORD_TYPE const e1y, COORD_TYPE const e1z,
645  COORD_TYPE const e2x, COORD_TYPE const e2y, COORD_TYPE const e2z )
646 {
647  return lexicographicalCompareVertex( ( e1y - ay ) * ( e2x - ax ),
648  ( e1z - az ) * ( e2x - ax ),
649  ( e1z - az ) * ( e2y - ay ),
650  ( e1x - ax ) * ( e2y - ay ),
651  ( e1x - ax ) * ( e2z - az ),
652  ( e1y - ay ) * ( e2z - az ) );
653 }
654 
673 template< typename COORD_TYPE, typename POINT_TYPE >
675 int lexicographicalCompareTriangle( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
676  COORD_TYPE const t1x, COORD_TYPE const t1y, COORD_TYPE const t1z,
677  COORD_TYPE const t2x, COORD_TYPE const t2y, COORD_TYPE const t2z,
678  COORD_TYPE const t3x, COORD_TYPE const t3y, COORD_TYPE const t3z )
679 {
680  COORD_TYPE v1x = t1x - ax;
681  COORD_TYPE v1y = t1y - ay;
682  COORD_TYPE v1z = t1z - az;
683  COORD_TYPE v2x = t2x - ax;
684  COORD_TYPE v2y = t2y - ay;
685  COORD_TYPE v2z = t2z - az;
686  COORD_TYPE v3x = t3x - ax;
687  COORD_TYPE v3y = t3y - ay;
688  COORD_TYPE v3z = t3z - az;
689  COORD_TYPE sign = ( v1x * v2y - v1y * v2x ) * v3z +
690  ( v2x * v3y - v2y * v3x ) * v1z +
691  ( v3x * v1y - v3y * v1x ) * v2z;
692  if( sign > 0 )
693  return 1;
694  else if( sign < 0 )
695  return -1;
696  return 0;
697 }
705 template< typename ... LIST_TYPE >
708  arrayView1d< globalIndex const > const & elementGlobalIndex )
709 {
710  localIndex minElement = -1;
711  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
712  for( int i = 0; i < nodeElements.size(); i++ )
713  {
714  localIndex e = nodeElements( i );
715  if( elementGlobalIndex[ e ] < minElementGID )
716  {
717  minElementGID = elementGlobalIndex[ e ];
718  minElement = e;
719  }
720  }
721  return minElement;
722 }
723 
732 template< typename ... LIST_TYPE >
735  arraySlice1d< localIndex const > const & nodeElements2,
736  arrayView1d< globalIndex const > const & elementGlobalIndex )
737 {
738  localIndex minElement = -1;
739  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
740  for( int i = 0; i < nodeElements1.size(); i++ )
741  {
742  localIndex e1 = nodeElements1( i );
743  for( int j = 0; j < nodeElements2.size(); j++ )
744  {
745  localIndex e2 = nodeElements2( j );
746  if( e1 == e2 )
747  {
748  if( elementGlobalIndex[ e1 ] < minElementGID )
749  {
750  minElementGID = elementGlobalIndex[ e1 ];
751  minElement = e1;
752  }
753  }
754  }
755  }
756  return minElement;
757 }
758 
768 template< typename ... LIST_TYPE >
771  arraySlice1d< localIndex const > const & nodeElements2,
772  arraySlice1d< localIndex const > const & nodeElements3,
773  arrayView1d< globalIndex const > const & elementGlobalIndex )
774 {
775  localIndex minElement = -1;
776  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
777  for( int i = 0; i < nodeElements1.size(); i++ )
778  {
779  localIndex e1 = nodeElements1( i );
780  for( int j = 0; j < nodeElements2.size(); j++ )
781  {
782  localIndex e2 = nodeElements2( j );
783  for( int k = 0; k < nodeElements3.size(); k++ )
784  {
785  localIndex e3 = nodeElements3( k );
786  if( e1 == e2 && e2 == e3 )
787  {
788  if( elementGlobalIndex[ e1 ] < minElementGID )
789  {
790  minElementGID = elementGlobalIndex[ e1 ];
791  minElement = e1;
792  }
793  }
794  }
795  }
796  }
797  return minElement;
798 }
799 
814 template< typename COORD_TYPE, typename POINT_TYPE >
818  arrayView2d< localIndex const > const & elementsToFaces,
819  ArrayOfArraysView< localIndex const > const & facesToNodes,
820  ArrayOfArraysView< localIndex const > const & nodesToElements,
821  arrayView1d< globalIndex const > const & nodeLocalToGlobal,
822  arrayView1d< globalIndex const > const & elementLocalToGlobal,
823  POINT_TYPE const & elemCenter,
824  POINT_TYPE const & point )
825 {
826  arraySlice1d< localIndex const > const & faceIndices = elementsToFaces[ element ];
827  localIndex const numFaces = faceIndices.size();
828  int omega = 0;
829  for( localIndex kf = 0; kf < numFaces; ++kf )
830  {
831  // triangulate the face. The triangulation must be done in a consistent way across ranks.
832  // This can be achieved by always picking the vertex with the lowest global index as root.
833  localIndex const faceIndex = faceIndices[kf];
834  globalIndex minGlobalId = LvArray::NumericLimits< globalIndex >::max;
835  localIndex minVertex = -1;
836  localIndex numFaceVertices = facesToNodes[faceIndex].size();
837  for( localIndex v = 0; v < numFaceVertices; v++ )
838  {
839  localIndex vIndex = facesToNodes( faceIndex, v );
840  globalIndex globalId = nodeLocalToGlobal[ vIndex ];
841  if( globalId < minGlobalId )
842  {
843  minGlobalId = globalId;
844  minVertex = vIndex;
845  }
846  }
847  // triangulate the face using the minimum-id vertex as root
848  localIndex vi[ 3 ] = { minVertex, -1, -1 };
849  for( localIndex v = 0; v < numFaceVertices; v++ )
850  {
851  vi[ 1 ] = facesToNodes( faceIndex, v );
852  vi[ 2 ] = facesToNodes( faceIndex, (v + 1) % numFaceVertices );
853  if( vi[ 1 ] != minVertex && vi[ 2 ] != minVertex )
854  {
855  // To make the algorithm independent of rank, always take the two additional vertices in increasing global ID
856  if( nodeLocalToGlobal[ vi[ 1 ] ] > nodeLocalToGlobal[ vi[ 2 ] ] )
857  {
858  localIndex temp = vi[ 1 ];
859  vi[ 1 ] = vi[ 2 ];
860  vi[ 2 ] = temp;
861  }
862  COORD_TYPE v1x = nodeCoordinates( vi[ 0 ], 0 );
863  COORD_TYPE v1y = nodeCoordinates( vi[ 0 ], 1 );
864  COORD_TYPE v1z = nodeCoordinates( vi[ 0 ], 2 );
865  COORD_TYPE v2x = nodeCoordinates( vi[ 1 ], 0 );
866  COORD_TYPE v2y = nodeCoordinates( vi[ 1 ], 1 );
867  COORD_TYPE v2z = nodeCoordinates( vi[ 1 ], 2 );
868  COORD_TYPE v3x = nodeCoordinates( vi[ 2 ], 0 );
869  COORD_TYPE v3y = nodeCoordinates( vi[ 2 ], 1 );
870  COORD_TYPE v3z = nodeCoordinates( vi[ 2 ], 2 );
871  // check the orientation of this triangle
872  R1Tensor vv1 = { v2x - v1x, v2y - v1y, v2z - v1z };
873  R1Tensor vv2 = { v3x - v1x, v3y - v1y, v3z - v1z };
874  R1Tensor dist = { elemCenter[ 0 ] - ( v1x + v2x + v3x )/3.0,
875  elemCenter[ 1 ] - ( v1y + v2y + v3y )/3.0,
876  elemCenter[ 2 ] - ( v1z + v2z + v3z )/3.0 };
877  R1Tensor norm = { };
878  LvArray::tensorOps::crossProduct( norm, vv1, vv2 );
879  // check if face is oriented coherently, and change sign otherwise
880  int sign = LvArray::tensorOps::AiBi< 3 >( norm, dist ) > 0 ? -1 : +1;
881  // Compute the winding number contributed by this triangle
882  int cmp1 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v1x, v1y, v1z );
883  if( cmp1 == 0 )
884  {
885  return findVertexRefElement( nodesToElements[ vi[ 0 ] ], elementLocalToGlobal ) == element;
886  }
887  int cmp2 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v2x, v2y, v2z );
888  if( cmp2 == 0 )
889  {
890  return findVertexRefElement( nodesToElements[ vi[ 1 ] ], elementLocalToGlobal ) == element;
891  }
892  int cmp3 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v3x, v3y, v3z );
893  if( cmp3 == 0 )
894  {
895  return findVertexRefElement( nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
896  }
897  int facecmp = 0;
898  int edgecmp = 0;
899  if( cmp1 != cmp2 )
900  {
901  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
902  v1x, v1y, v1z,
903  v2x, v2y, v2z );
904  if( edgecmp == 0 )
905  {
906  return findEdgeRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 1 ] ], elementLocalToGlobal ) == element;
907  }
908  facecmp += sign * edgecmp;
909  }
910  if( cmp2 != cmp3 )
911  {
912  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
913  v2x, v2y, v2z,
914  v3x, v3y, v3z );
915  if( edgecmp == 0 )
916  {
917  return findEdgeRefElement( nodesToElements[ vi[ 1 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
918  }
919  facecmp += sign * edgecmp;
920  }
921  if( cmp3 != cmp1 )
922  {
923  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
924  v3x, v3y, v3z,
925  v1x, v1y, v1z );
926  if( edgecmp == 0 )
927  {
928  return findEdgeRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
929  }
930  facecmp += sign * edgecmp;
931  }
932  // if all edges are on the same side, this triangle does not contribute to the winding number
933  if( facecmp == 0 )
934  continue;
935  facecmp = lexicographicalCompareTriangle( point[ 0 ], point[ 1 ], point[ 2 ],
936  v1x, v1y, v1z,
937  v2x, v2y, v2z,
938  v3x, v3y, v3z );
939 
940  if( facecmp == 0 )
941  {
942  return findTriangleRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 1 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
943  }
944  omega += sign * facecmp;
945  }
946  }
947  }
948 
949  return omega;
950 }
951 
975 template< typename COORD_TYPE, typename POINT_TYPE >
979  arrayView2d< localIndex const > const & elementsToFaces,
980  ArrayOfArraysView< localIndex const > const & facesToNodes,
981  ArrayOfArraysView< localIndex const > const & nodesToElements,
982  arrayView1d< globalIndex const > const & nodeLocalToGlobal,
983  arrayView1d< globalIndex const > const & elementLocalToGlobal,
984  POINT_TYPE const & elemCenter,
985  POINT_TYPE const & point )
986 {
987  return computeWindingNumber( element, nodeCoordinates, elementsToFaces, facesToNodes, nodesToElements, nodeLocalToGlobal, elementLocalToGlobal, elemCenter, point ) > 0;
988 }
989 
999 template< typename NODE_MAP_TYPE, typename VEC_TYPE >
1001 void getBoundingBox( localIndex const elemIndex,
1002  NODE_MAP_TYPE const & pointIndices,
1004  VEC_TYPE && boxDims )
1005 {
1006  // This holds the min coordinates of the set in each direction
1007  R1Tensor minCoords = { LvArray::NumericLimits< real64 >::max,
1008  LvArray::NumericLimits< real64 >::max,
1009  LvArray::NumericLimits< real64 >::max };
1010 
1011  // boxDims is used to hold the max coordinates.
1012  LvArray::tensorOps::fill< 3 >( boxDims, LvArray::NumericLimits< real64 >::lowest );
1013 
1014  // loop over all the vertices of the element to get the min and max coords
1015  for( localIndex a = 0; a < pointIndices[elemIndex].size(); ++a )
1016  {
1017  localIndex const id = pointIndices( elemIndex, a );
1018  for( localIndex d = 0; d < 3; ++d )
1019  {
1020  minCoords[ d ] = fmin( minCoords[ d ], pointCoordinates( id, d ) );
1021  boxDims[ d ] = fmax( boxDims[ d ], pointCoordinates( id, d ) );
1022  }
1023  }
1024 
1025  LvArray::tensorOps::subtract< 3 >( boxDims, minCoords );
1026 }
1027 
1034 template< typename FE_TYPE >
1035 GEOS_HOST_DEVICE inline
1036 real64 elementVolume( real64 const (&X)[FE_TYPE::numNodes][3] )
1037 {
1038  real64 result{};
1039  for( localIndex q=0; q<FE_TYPE::numQuadraturePoints; ++q )
1040  {
1041  result = result + FE_TYPE::transformedQuadratureWeight( q, X );
1042  }
1043  return result;
1044 }
1045 
1052 inline
1053 real64 hexahedronVolume( real64 const (&X)[8][3] )
1054 {
1055  return elementVolume< finiteElement::H1_Hexahedron_Lagrange1_GaussLegendre2 >( X );
1056 }
1057 
1064 inline
1065 real64 tetrahedronVolume( real64 const (&X)[4][3] )
1066 {
1067  return elementVolume< finiteElement::H1_Tetrahedron_Lagrange1_Gauss1 >( X );
1068 }
1069 
1076 inline
1077 real64 wedgeVolume( real64 const (&X)[6][3] )
1078 {
1079  return elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( X );
1080 }
1081 
1088 inline
1089 real64 pyramidVolume( real64 const (&X)[5][3] )
1090 {
1091  return elementVolume< finiteElement::H1_Pyramid_Lagrange1_Gauss5 >( X );
1092 }
1093 
1104 template< integer N >
1106 inline
1107 real64 prismVolume( real64 const (&X)[2*N][3] )
1108 {
1109  static_assert( N > 4,
1110  "Function prismVolume can be called for a prism with N-sided polygon base where N > 5." );
1111 
1112  real64 result{};
1113 
1114  // Compute the barycenters of the prism bases
1115  real64 XGBot[3]{};
1116  real64 XGTop[3]{};
1117  for( integer a = 0; a < N; ++a )
1118  {
1119  LvArray::tensorOps::add< 3 >( XGBot, X[a] );
1120  }
1121  for( integer a = N; a < 2 * N; ++a )
1122  {
1123  LvArray::tensorOps::add< 3 >( XGTop, X[a] );
1124  }
1125  LvArray::tensorOps::scale< 3 >( XGBot, 1.0 / N );
1126  LvArray::tensorOps::scale< 3 >( XGTop, 1.0 / N );
1127 
1128  real64 XWedge[6][3];
1129  for( int a = 0; a < N - 1; ++a )
1130  {
1131 
1132  LvArray::tensorOps::copy< 3 >( XWedge[0], X[a] );
1133  LvArray::tensorOps::copy< 3 >( XWedge[1], X[a+N] );
1134  LvArray::tensorOps::copy< 3 >( XWedge[2], X[a+1] );
1135  LvArray::tensorOps::copy< 3 >( XWedge[3], X[a+1+N] );
1136  LvArray::tensorOps::copy< 3 >( XWedge[4], XGBot );
1137  LvArray::tensorOps::copy< 3 >( XWedge[5], XGTop );
1138  result = result + computationalGeometry::elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( XWedge );
1139  }
1140  LvArray::tensorOps::copy< 3 >( XWedge[0], X[N-1] );
1141  LvArray::tensorOps::copy< 3 >( XWedge[1], X[2*N-1] );
1142  LvArray::tensorOps::copy< 3 >( XWedge[2], X[0] );
1143  LvArray::tensorOps::copy< 3 >( XWedge[3], X[N] );
1144  LvArray::tensorOps::copy< 3 >( XWedge[4], XGBot );
1145  LvArray::tensorOps::copy< 3 >( XWedge[5], XGTop );
1146  result = result + computationalGeometry::elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( XWedge );
1147  return result;
1148 }
1149 
1150 } /* namespace computationalGeometry */
1151 } /* namespace geos */
1152 
1153 #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.
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