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 Total, S.A
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 
436 
449 template< typename COORD_TYPE, typename POINT_TYPE >
451 int lexicographicalCompareVertex( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
452  COORD_TYPE const bx, COORD_TYPE const by, COORD_TYPE const bz )
453 {
454  if( ax < bx )
455  return -1;
456  else if( ax > bx )
457  return 1;
458  if( ay < by )
459  return -1;
460  else if( ay > by )
461  return 1;
462  if( az < bz )
463  return -1;
464  else if( az > bz )
465  return 1;
466  return 0;
467 }
468 
484 template< typename COORD_TYPE, typename POINT_TYPE >
486 int lexicographicalCompareEdge( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
487  COORD_TYPE const e1x, COORD_TYPE const e1y, COORD_TYPE const e1z,
488  COORD_TYPE const e2x, COORD_TYPE const e2y, COORD_TYPE const e2z )
489 {
490  return lexicographicalCompareVertex( ( e1y - ay ) * ( e2x - ax ),
491  ( e1z - az ) * ( e2x - ax ),
492  ( e1z - az ) * ( e2y - ay ),
493  ( e1x - ax ) * ( e2y - ay ),
494  ( e1x - ax ) * ( e2z - az ),
495  ( e1y - ay ) * ( e2z - az ) );
496 }
497 
516 template< typename COORD_TYPE, typename POINT_TYPE >
518 int lexicographicalCompareTriangle( POINT_TYPE const ax, POINT_TYPE const ay, POINT_TYPE const az,
519  COORD_TYPE const t1x, COORD_TYPE const t1y, COORD_TYPE const t1z,
520  COORD_TYPE const t2x, COORD_TYPE const t2y, COORD_TYPE const t2z,
521  COORD_TYPE const t3x, COORD_TYPE const t3y, COORD_TYPE const t3z )
522 {
523  COORD_TYPE v1x = t1x - ax;
524  COORD_TYPE v1y = t1y - ay;
525  COORD_TYPE v1z = t1z - az;
526  COORD_TYPE v2x = t2x - ax;
527  COORD_TYPE v2y = t2y - ay;
528  COORD_TYPE v2z = t2z - az;
529  COORD_TYPE v3x = t3x - ax;
530  COORD_TYPE v3y = t3y - ay;
531  COORD_TYPE v3z = t3z - az;
532  COORD_TYPE sign = ( v1x * v2y - v1y * v2x ) * v3z +
533  ( v2x * v3y - v2y * v3x ) * v1z +
534  ( v3x * v1y - v3y * v1x ) * v2z;
535  if( sign > 0 )
536  return 1;
537  else if( sign < 0 )
538  return -1;
539  return 0;
540 }
548 template< typename ... LIST_TYPE >
551  arrayView1d< globalIndex const > const & elementGlobalIndex )
552 {
553  localIndex minElement = -1;
554  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
555  for( int i = 0; i < nodeElements.size(); i++ )
556  {
557  localIndex e = nodeElements( i );
558  if( elementGlobalIndex[ e ] < minElementGID )
559  {
560  minElementGID = elementGlobalIndex[ e ];
561  minElement = e;
562  }
563  }
564  return minElement;
565 }
566 
575 template< typename ... LIST_TYPE >
578  arraySlice1d< localIndex const > const & nodeElements2,
579  arrayView1d< globalIndex const > const & elementGlobalIndex )
580 {
581  localIndex minElement = -1;
582  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
583  for( int i = 0; i < nodeElements1.size(); i++ )
584  {
585  localIndex e1 = nodeElements1( i );
586  for( int j = 0; j < nodeElements2.size(); j++ )
587  {
588  localIndex e2 = nodeElements2( j );
589  if( e1 == e2 )
590  {
591  if( elementGlobalIndex[ e1 ] < minElementGID )
592  {
593  minElementGID = elementGlobalIndex[ e1 ];
594  minElement = e1;
595  }
596  }
597  }
598  }
599  return minElement;
600 }
601 
611 template< typename ... LIST_TYPE >
614  arraySlice1d< localIndex const > const & nodeElements2,
615  arraySlice1d< localIndex const > const & nodeElements3,
616  arrayView1d< globalIndex const > const & elementGlobalIndex )
617 {
618  localIndex minElement = -1;
619  globalIndex minElementGID = LvArray::NumericLimits< globalIndex >::max;
620  for( int i = 0; i < nodeElements1.size(); i++ )
621  {
622  localIndex e1 = nodeElements1( i );
623  for( int j = 0; j < nodeElements2.size(); j++ )
624  {
625  localIndex e2 = nodeElements2( j );
626  for( int k = 0; k < nodeElements3.size(); k++ )
627  {
628  localIndex e3 = nodeElements3( k );
629  if( e1 == e2 && e2 == e3 )
630  {
631  if( elementGlobalIndex[ e1 ] < minElementGID )
632  {
633  minElementGID = elementGlobalIndex[ e1 ];
634  minElement = e1;
635  }
636  }
637  }
638  }
639  }
640  return minElement;
641 }
642 
657 template< typename COORD_TYPE, typename POINT_TYPE >
661  arrayView2d< localIndex const > const & elementsToFaces,
662  ArrayOfArraysView< localIndex const > const & facesToNodes,
663  ArrayOfArraysView< localIndex const > const & nodesToElements,
664  arrayView1d< globalIndex const > const & nodeLocalToGlobal,
665  arrayView1d< globalIndex const > const & elementLocalToGlobal,
666  POINT_TYPE const & elemCenter,
667  POINT_TYPE const & point )
668 {
669  arraySlice1d< localIndex const > const & faceIndices = elementsToFaces[ element ];
670  localIndex const numFaces = faceIndices.size();
671  int omega = 0;
672  for( localIndex kf = 0; kf < numFaces; ++kf )
673  {
674  // triangulate the face. The triangulation must be done in a consistent way across ranks.
675  // This can be achieved by always picking the vertex with the lowest global index as root.
676  localIndex const faceIndex = faceIndices[kf];
677  globalIndex minGlobalId = LvArray::NumericLimits< globalIndex >::max;
678  localIndex minVertex = -1;
679  localIndex numFaceVertices = facesToNodes[faceIndex].size();
680  for( localIndex v = 0; v < numFaceVertices; v++ )
681  {
682  localIndex vIndex = facesToNodes( faceIndex, v );
683  globalIndex globalId = nodeLocalToGlobal[ vIndex ];
684  if( globalId < minGlobalId )
685  {
686  minGlobalId = globalId;
687  minVertex = vIndex;
688  }
689  }
690  // triangulate the face using the minimum-id vertex as root
691  localIndex vi[ 3 ] = { minVertex, -1, -1 };
692  for( localIndex v = 0; v < numFaceVertices; v++ )
693  {
694  vi[ 1 ] = facesToNodes( faceIndex, v );
695  vi[ 2 ] = facesToNodes( faceIndex, (v + 1) % numFaceVertices );
696  if( vi[ 1 ] != minVertex && vi[ 2 ] != minVertex )
697  {
698  // To make the algorithm independent of rank, always take the two additional vertices in increasing global ID
699  if( nodeLocalToGlobal[ vi[ 1 ] ] > nodeLocalToGlobal[ vi[ 2 ] ] )
700  {
701  localIndex temp = vi[ 1 ];
702  vi[ 1 ] = vi[ 2 ];
703  vi[ 2 ] = temp;
704  }
705  COORD_TYPE v1x = nodeCoordinates( vi[ 0 ], 0 );
706  COORD_TYPE v1y = nodeCoordinates( vi[ 0 ], 1 );
707  COORD_TYPE v1z = nodeCoordinates( vi[ 0 ], 2 );
708  COORD_TYPE v2x = nodeCoordinates( vi[ 1 ], 0 );
709  COORD_TYPE v2y = nodeCoordinates( vi[ 1 ], 1 );
710  COORD_TYPE v2z = nodeCoordinates( vi[ 1 ], 2 );
711  COORD_TYPE v3x = nodeCoordinates( vi[ 2 ], 0 );
712  COORD_TYPE v3y = nodeCoordinates( vi[ 2 ], 1 );
713  COORD_TYPE v3z = nodeCoordinates( vi[ 2 ], 2 );
714  // check the orientation of this triangle
715  R1Tensor vv1 = { v2x - v1x, v2y - v1y, v2z - v1z };
716  R1Tensor vv2 = { v3x - v1x, v3y - v1y, v3z - v1z };
717  R1Tensor dist = { elemCenter[ 0 ] - ( v1x + v2x + v3x )/3.0,
718  elemCenter[ 1 ] - ( v1y + v2y + v3y )/3.0,
719  elemCenter[ 2 ] - ( v1z + v2z + v3z )/3.0 };
720  R1Tensor norm = { };
721  LvArray::tensorOps::crossProduct( norm, vv1, vv2 );
722  // check if face is oriented coherently, and change sign otherwise
723  int sign = LvArray::tensorOps::AiBi< 3 >( norm, dist ) > 0 ? -1 : +1;
724  // Compute the winding number contributed by this triangle
725  int cmp1 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v1x, v1y, v1z );
726  if( cmp1 == 0 )
727  {
728  return findVertexRefElement( nodesToElements[ vi[ 0 ] ], elementLocalToGlobal ) == element;
729  }
730  int cmp2 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v2x, v2y, v2z );
731  if( cmp2 == 0 )
732  {
733  return findVertexRefElement( nodesToElements[ vi[ 1 ] ], elementLocalToGlobal ) == element;
734  }
735  int cmp3 = lexicographicalCompareVertex( point[ 0 ], point[ 1 ], point[ 2 ], v3x, v3y, v3z );
736  if( cmp3 == 0 )
737  {
738  return findVertexRefElement( nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
739  }
740  int facecmp = 0;
741  int edgecmp = 0;
742  if( cmp1 != cmp2 )
743  {
744  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
745  v1x, v1y, v1z,
746  v2x, v2y, v2z );
747  if( edgecmp == 0 )
748  {
749  return findEdgeRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 1 ] ], elementLocalToGlobal ) == element;
750  }
751  facecmp += sign * edgecmp;
752  }
753  if( cmp2 != cmp3 )
754  {
755  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
756  v2x, v2y, v2z,
757  v3x, v3y, v3z );
758  if( edgecmp == 0 )
759  {
760  return findEdgeRefElement( nodesToElements[ vi[ 1 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
761  }
762  facecmp += sign * edgecmp;
763  }
764  if( cmp3 != cmp1 )
765  {
766  edgecmp = lexicographicalCompareEdge( point[ 0 ], point[ 1 ], point[ 2 ],
767  v3x, v3y, v3z,
768  v1x, v1y, v1z );
769  if( edgecmp == 0 )
770  {
771  return findEdgeRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
772  }
773  facecmp += sign * edgecmp;
774  }
775  // if all edges are on the same side, this triangle does not contribute to the winding number
776  if( facecmp == 0 )
777  continue;
778  facecmp = lexicographicalCompareTriangle( point[ 0 ], point[ 1 ], point[ 2 ],
779  v1x, v1y, v1z,
780  v2x, v2y, v2z,
781  v3x, v3y, v3z );
782 
783  if( facecmp == 0 )
784  {
785  return findTriangleRefElement( nodesToElements[ vi[ 0 ] ], nodesToElements[ vi[ 1 ] ], nodesToElements[ vi[ 2 ] ], elementLocalToGlobal ) == element;
786  }
787  omega += sign * facecmp;
788  }
789  }
790  }
791 
792  return omega;
793 }
794 
818 template< typename COORD_TYPE, typename POINT_TYPE >
822  arrayView2d< localIndex const > const & elementsToFaces,
823  ArrayOfArraysView< localIndex const > const & facesToNodes,
824  ArrayOfArraysView< localIndex const > const & nodesToElements,
825  arrayView1d< globalIndex const > const & nodeLocalToGlobal,
826  arrayView1d< globalIndex const > const & elementLocalToGlobal,
827  POINT_TYPE const & elemCenter,
828  POINT_TYPE const & point )
829 {
830  return computeWindingNumber( element, nodeCoordinates, elementsToFaces, facesToNodes, nodesToElements, nodeLocalToGlobal, elementLocalToGlobal, elemCenter, point ) > 0;
831 }
832 
842 template< typename VEC_TYPE >
844 void getBoundingBox( localIndex const elemIndex,
847  VEC_TYPE && boxDims )
848 {
849  // This holds the min coordinates of the set in each direction
850  R1Tensor minCoords = { LvArray::NumericLimits< real64 >::max,
851  LvArray::NumericLimits< real64 >::max,
852  LvArray::NumericLimits< real64 >::max };
853 
854  // boxDims is used to hold the max coordinates.
855  LvArray::tensorOps::fill< 3 >( boxDims, LvArray::NumericLimits< real64 >::lowest );
856 
857  // loop over all the vertices of the element to get the min and max coords
858  for( localIndex a = 0; a < pointIndices.size( 1 ); ++a )
859  {
860  localIndex const id = pointIndices( elemIndex, a );
861  for( localIndex d = 0; d < 3; ++d )
862  {
863  minCoords[ d ] = fmin( minCoords[ d ], pointCoordinates( id, d ) );
864  boxDims[ d ] = fmax( boxDims[ d ], pointCoordinates( id, d ) );
865  }
866  }
867 
868  LvArray::tensorOps::subtract< 3 >( boxDims, minCoords );
869 }
870 
877 template< typename FE_TYPE >
878 GEOS_HOST_DEVICE inline
879 real64 elementVolume( real64 const (&X)[FE_TYPE::numNodes][3] )
880 {
881  real64 result{};
882  for( localIndex q=0; q<FE_TYPE::numQuadraturePoints; ++q )
883  {
884  result = result + FE_TYPE::transformedQuadratureWeight( q, X );
885  }
886  return result;
887 }
888 
895 inline
896 real64 hexahedronVolume( real64 const (&X)[8][3] )
897 {
898  return elementVolume< finiteElement::H1_Hexahedron_Lagrange1_GaussLegendre2 >( X );
899 }
900 
907 inline
908 real64 tetrahedronVolume( real64 const (&X)[4][3] )
909 {
910  return elementVolume< finiteElement::H1_Tetrahedron_Lagrange1_Gauss1 >( X );
911 }
912 
919 inline
920 real64 wedgeVolume( real64 const (&X)[6][3] )
921 {
922  return elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( X );
923 }
924 
931 inline
932 real64 pyramidVolume( real64 const (&X)[5][3] )
933 {
934  return elementVolume< finiteElement::H1_Pyramid_Lagrange1_Gauss5 >( X );
935 }
936 
947 template< integer N >
949 inline
950 real64 prismVolume( real64 const (&X)[2*N][3] )
951 {
952  static_assert( N > 4,
953  "Function prismVolume can be called for a prism with N-sided polygon base where N > 5." );
954 
955  real64 result{};
956 
957  // Compute the barycenters of the prism bases
958  real64 XGBot[3]{};
959  real64 XGTop[3]{};
960  for( integer a = 0; a < N; ++a )
961  {
962  LvArray::tensorOps::add< 3 >( XGBot, X[a] );
963  }
964  for( integer a = N; a < 2 * N; ++a )
965  {
966  LvArray::tensorOps::add< 3 >( XGTop, X[a] );
967  }
968  LvArray::tensorOps::scale< 3 >( XGBot, 1.0 / N );
969  LvArray::tensorOps::scale< 3 >( XGTop, 1.0 / N );
970 
971  real64 XWedge[6][3];
972  for( int a = 0; a < N - 1; ++a )
973  {
974 
975  LvArray::tensorOps::copy< 3 >( XWedge[0], X[a] );
976  LvArray::tensorOps::copy< 3 >( XWedge[1], X[a+N] );
977  LvArray::tensorOps::copy< 3 >( XWedge[2], X[a+1] );
978  LvArray::tensorOps::copy< 3 >( XWedge[3], X[a+1+N] );
979  LvArray::tensorOps::copy< 3 >( XWedge[4], XGBot );
980  LvArray::tensorOps::copy< 3 >( XWedge[5], XGTop );
981  result = result + computationalGeometry::elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( XWedge );
982  }
983  LvArray::tensorOps::copy< 3 >( XWedge[0], X[N-1] );
984  LvArray::tensorOps::copy< 3 >( XWedge[1], X[2*N-1] );
985  LvArray::tensorOps::copy< 3 >( XWedge[2], X[0] );
986  LvArray::tensorOps::copy< 3 >( XWedge[3], X[N] );
987  LvArray::tensorOps::copy< 3 >( XWedge[4], XGBot );
988  LvArray::tensorOps::copy< 3 >( XWedge[5], XGTop );
989  result = result + computationalGeometry::elementVolume< finiteElement::H1_Wedge_Lagrange1_Gauss6 >( XWedge );
990  return result;
991 }
992 
993 } /* namespace computationalGeometry */
994 } /* namespace geos */
995 
996 #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 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.
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 respecto 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...
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 void getBoundingBox(localIndex const elemIndex, arrayView2d< localIndex const, cells::NODE_MAP_USD > 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 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:180
Array< T, 2, PERMUTATION > array2d
Alias for 2D array.
Definition: DataTypes.hpp:192
GEOS_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:88
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:286
double real64
64-bit floating point type.
Definition: DataTypes.hpp:99
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:85
ArraySlice< T, 1, USD > arraySlice1d
Alias for 1D array slice.
Definition: DataTypes.hpp:184
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:82
ArrayView< T, 2, USD > arrayView2d
Alias for 2D array view.
Definition: DataTypes.hpp:196
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:176