1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #ifndef __SPLITTERTETRA_TXX__
20 #define __SPLITTERTETRA_TXX__
22 #include "SplitterTetra.hxx"
24 #include "TetraAffineTransform.hxx"
25 #include "TransformedTriangle.hxx"
26 #include "MeshUtils.hxx"
27 #include "VectorUtils.hxx"
28 #include "CellModel.hxx"
30 #include "UnitTetraIntersectionBary.hxx"
31 #include "VolSurfFormulae.hxx"
39 namespace INTERP_KERNEL
41 template<class MyMeshType>
42 const double SplitterTetra<MyMeshType>::SPARSE_TRUNCATION_LIMIT=1.0e-14;
45 * output is expected to be allocated with 24*sizeof(void*) in order to store the 24 tetras.
46 * These tetras have to be deallocated.
48 template<class MyMeshType>
49 void SplitterTetra<MyMeshType>::splitIntoDualCells(SplitterTetra<MyMeshType> **output)
52 const double *tmp2[4]={tmp,tmp+3,tmp+6,tmp+9};
53 typename MyMeshType::MyConnType conn[4]={-1,-1,-1,-1};
56 splitMySelfForDual(tmp,i,conn[0]);
57 output[i]=new SplitterTetra<MyMeshType>(_src_mesh,tmp2,conn);
62 * SplitterTetra class computes for a list of cell ids of a given mesh \a srcMesh (badly named) the intersection with a
63 * single TETRA4 cell given by \a tetraCorners (of length 4) and \a nodesId (of length 4 too). \a nodedIds is given only to establish
64 * if a partial computation of a triangle has already been performed (to increase performance).
66 * The \a srcMesh can contain polyhedron cells.
69 * Constructor creating object from the four corners of the tetrahedron.
71 * @param srcMesh mesh containing the source elements
72 * @param tetraCorners array of four pointers to double[3] arrays containing the coordinates of the
73 * corners of the tetrahedron
75 template<class MyMeshType>
76 SplitterTetra<MyMeshType>::SplitterTetra(const MyMeshType& srcMesh, const double** tetraCorners, const typename MyMeshType::MyConnType *nodesId)
77 : _t(0), _src_mesh(srcMesh)
79 std::copy(nodesId,nodesId+4,_conn);
80 _coords[0]=tetraCorners[0][0]; _coords[1]=tetraCorners[0][1]; _coords[2]=tetraCorners[0][2];
81 _coords[3]=tetraCorners[1][0]; _coords[4]=tetraCorners[1][1]; _coords[5]=tetraCorners[1][2];
82 _coords[6]=tetraCorners[2][0]; _coords[7]=tetraCorners[2][1]; _coords[8]=tetraCorners[2][2];
83 _coords[9]=tetraCorners[3][0]; _coords[10]=tetraCorners[3][1]; _coords[11]=tetraCorners[3][2];
84 // create the affine transform
85 _t=new TetraAffineTransform(_coords);
89 * SplitterTetra class computes for a list of cell ids of a given mesh \a srcMesh (badly named) the intersection with a
90 * single TETRA4 cell given by \a tetraCorners (of length 4) and \a nodesId (of length 4 too). \a nodedIds is given only to establish
91 * if a partial computation of a triangle has already been performed (to increase performance).
93 * The \a srcMesh can contain polyhedron cells.
96 * Constructor creating object from the four corners of the tetrahedron.
98 * \param [in] srcMesh mesh containing the source elements
99 * \param [in] tetraCorners array 4*3 doubles containing corners of input tetrahedron (P0X,P0Y,P0Y,P1X,P1Y,P1Z,P2X,P2Y,P2Z,P3X,P3Y,P3Z).
101 template<class MyMeshType>
102 SplitterTetra<MyMeshType>::SplitterTetra(const MyMeshType& srcMesh, const double tetraCorners[12], const ConnType *conn): _t(0),_src_mesh(srcMesh)
105 { _conn[0]=0; _conn[1]=1; _conn[2]=2; _conn[3]=3; }
107 { _conn[0]=conn[0]; _conn[1]=conn[1]; _conn[2]=conn[2]; _conn[3]=conn[3]; }
108 _coords[0]=tetraCorners[0]; _coords[1]=tetraCorners[1]; _coords[2]=tetraCorners[2]; _coords[3]=tetraCorners[3]; _coords[4]=tetraCorners[4]; _coords[5]=tetraCorners[5];
109 _coords[6]=tetraCorners[6]; _coords[7]=tetraCorners[7]; _coords[8]=tetraCorners[8]; _coords[9]=tetraCorners[9]; _coords[10]=tetraCorners[10]; _coords[11]=tetraCorners[11];
110 // create the affine transform
111 _t=new TetraAffineTransform(_coords);
117 * Deletes _t and the coordinates (double[3] vectors) in _nodes
120 template<class MyMeshType>
121 SplitterTetra<MyMeshType>::~SplitterTetra()
124 for(typename HashMap< ConnType, double* >::iterator iter = _nodes.begin(); iter != _nodes.end() ; ++iter)
125 delete[] iter->second;
129 * \Forget already calculated triangles, which is crucial for calculation of barycenter of intersection
131 template<class MyMeshType>
132 void SplitterTetra<MyMeshType>::clearVolumesCache()
138 * This method destroys the 4 pointers pointed by tetraCorners[0],tetraCorners[1],tetraCorners[2] and tetraCorners[3]
139 * @param i is in 0..23 included.
140 * @param output is expected to be sized of 12 in order to.
142 template<class MyMeshType>
143 void SplitterTetra<MyMeshType>::splitMySelfForDual(double* output, int i, typename MyMeshType::MyConnType& nodeId)
147 nodeId=_conn[offset];
148 tmp[0]=_coords+3*offset; tmp[1]=_coords+((offset+1)%4)*3; tmp[2]=_coords+((offset+2)%4)*3; tmp[3]=_coords+((offset+3)%4)*3;
150 int case1=caseToTreat/2;
151 int case2=caseToTreat%2;
152 const int tab[3][2]={{1,2},{3,2},{1,3}};
153 const int *curTab=tab[case1];
154 double pt0[3]; pt0[0]=(tmp[curTab[case2]][0]+tmp[0][0])/2.; pt0[1]=(tmp[curTab[case2]][1]+tmp[0][1])/2.; pt0[2]=(tmp[curTab[case2]][2]+tmp[0][2])/2.;
155 double pt1[3]; pt1[0]=(tmp[0][0]+tmp[curTab[0]][0]+tmp[curTab[1]][0])/3.; pt1[1]=(tmp[0][1]+tmp[curTab[0]][1]+tmp[curTab[1]][1])/3.; pt1[2]=(tmp[0][2]+tmp[curTab[0]][2]+tmp[curTab[1]][2])/3.;
156 double pt2[3]; pt2[0]=(tmp[0][0]+tmp[1][0]+tmp[2][0]+tmp[3][0])/4.; pt2[1]=(tmp[0][1]+tmp[1][1]+tmp[2][1]+tmp[3][1])/4.; pt2[2]=(tmp[0][2]+tmp[1][2]+tmp[2][2]+tmp[3][2])/4.;
157 std::copy(pt1,pt1+3,output+case2*3);
158 std::copy(pt0,pt0+3,output+(abs(case2-1))*3);
159 std::copy(pt2,pt2+3,output+2*3);
160 std::copy(tmp[0],tmp[0]+3,output+3*3);
164 * Calculates the volume of intersection of an element in the source mesh and the target element.
165 * It first calculates the transformation that takes the target tetrahedron into the unit tetrahedron. After that, the
166 * faces of the source element are triangulated and the calculated transformation is applied
167 * to each triangle. The algorithm of Grandy, implemented in INTERP_KERNEL::TransformedTriangle is used
168 * to calculate the contribution to the volume from each triangle. The volume returned is the sum of these contributions
169 * divided by the determinant of the transformation.
171 * The class will cache the intermediary calculations of transformed nodes of source cells and volumes associated
172 * with triangulated faces to avoid having to recalculate these.
174 * @param element global number of the source element in C mode.
176 template<class MyMeshType>
177 double SplitterTetra<MyMeshType>::intersectSourceCell(typename MyMeshType::MyConnType element,
180 typedef typename MyMeshType::MyConnType ConnType;
181 const NumberingPolicy numPol=MyMeshType::My_numPol;
182 //{ could be done on outside?
183 // check if we have planar tetra element
184 if(_t->determinant() == 0.0)
187 LOG(2, "Planar tetra -- volume 0");
192 NormalizedCellType normCellType=_src_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(element));
193 const CellModel& cellModelCell=CellModel::GetCellModel(normCellType);
194 ConnType nbOfNodes4Type=cellModelCell.isDynamic() ? _src_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(element)) : cellModelCell.getNumberOfNodes();
195 // halfspace filtering
196 bool isOutside[8] = {true, true, true, true, true, true, true, true};
197 bool isTargetOutside = false;
199 // calculate the coordinates of the nodes
200 ConnType *cellNodes=new ConnType[nbOfNodes4Type];
201 for(ConnType i = 0;i<nbOfNodes4Type;++i)
203 // we could store mapping local -> global numbers too, but not sure it is worth it
204 const ConnType globalNodeNum = getGlobalNumberOfNode(i, OTT<ConnType,numPol>::indFC(element), _src_mesh);
205 cellNodes[i]=globalNodeNum;
206 if(_nodes.find(globalNodeNum) == _nodes.end())
208 //for(HashMap< int , double* >::iterator iter3=_nodes.begin();iter3!=_nodes.end();iter3++)
209 // std::cout << (*iter3).first << " ";
210 //std::cout << std::endl << "*** " << globalNodeNum << std::endl;
211 calculateNode(globalNodeNum);
213 CheckIsOutside(_nodes[globalNodeNum], isOutside);
216 // halfspace filtering check
217 // NB : might not be beneficial for caching of triangles
218 for(int i = 0; i < 8; ++i)
222 isTargetOutside = true;
226 double totalVolume = 0.0;
230 /// calculator of intersection barycentre
231 UnitTetraIntersectionBary baryCalculator( _t->determinant() < 0.);
233 // get nb of sons of a cell
234 const ConnType* rawCellConn = _src_mesh.getConnectivityPtr() + OTT<ConnType,numPol>::conn2C( _src_mesh.getConnectivityIndexPtr()[ element ]);
235 const ConnType rawNbCellNodes = _src_mesh.getConnectivityIndexPtr()[ element+1 ] - _src_mesh.getConnectivityIndexPtr()[ element ];
236 unsigned nbOfSons = cellModelCell.getNumberOfSons2(rawCellConn, rawNbCellNodes);
238 for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
240 // get sons connectivity
241 NormalizedCellType faceType;
242 ConnType *faceNodes, nbFaceNodes=-1;
243 if ( cellModelCell.isDynamic() )
245 faceNodes=new ConnType[nbOfNodes4Type];
246 nbFaceNodes = cellModelCell.fillSonCellNodalConnectivity2(ii,rawCellConn,rawNbCellNodes,faceNodes,faceType);
247 for ( ConnType i = 0; i < nbFaceNodes; ++i )
248 faceNodes[i] = OTT<ConnType,numPol>::coo2C(faceNodes[i]);
252 faceType = cellModelCell.getSonType(ii);
253 const CellModel& faceModel=CellModel::GetCellModel(faceType);
254 assert(faceModel.getDimension() == 2);
255 nbFaceNodes = cellModelCell.getNumberOfNodesConstituentTheSon(ii);
256 faceNodes = new ConnType[nbFaceNodes];
257 cellModelCell.fillSonCellNodalConnectivity(ii,cellNodes,faceNodes);
259 // intersect a son with the unit tetra
264 // create the face key
265 TriangleFaceKey key = TriangleFaceKey(faceNodes[0], faceNodes[1], faceNodes[2]);
267 // calculate the triangle if needed
268 if(_volumes.find(key) == _volumes.end())
270 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1]], _nodes[faceNodes[2]]);
271 calculateVolume(tri, key);
272 totalVolume += _volumes[key];
274 baryCalculator.addSide( tri );
276 // count negative as face has reversed orientation
277 totalVolume -= _volumes[key];
284 // simple triangulation of faces along a diagonal :
295 //? not sure if this always works
297 // calculate the triangles if needed
299 // local nodes 1, 2, 3
300 TriangleFaceKey key1 = TriangleFaceKey(faceNodes[0], faceNodes[1], faceNodes[2]);
301 if(_volumes.find(key1) == _volumes.end())
303 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1]], _nodes[faceNodes[2]]);
304 calculateVolume(tri, key1);
305 totalVolume += _volumes[key1];
307 // count negative as face has reversed orientation
308 totalVolume -= _volumes[key1];
311 // local nodes 1, 3, 4
312 TriangleFaceKey key2 = TriangleFaceKey(faceNodes[0], faceNodes[2], faceNodes[3]);
313 if(_volumes.find(key2) == _volumes.end())
315 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[2]], _nodes[faceNodes[3]]);
316 calculateVolume(tri, key2);
317 totalVolume += _volumes[key2];
321 // count negative as face has reversed orientation
322 totalVolume -= _volumes[key2];
329 ConnType nbTria = nbFaceNodes - 2; // split polygon into nbTria triangles
330 for ( ConnType iTri = 0; iTri < nbTria; ++iTri )
332 TriangleFaceKey key = TriangleFaceKey(faceNodes[0], faceNodes[1+iTri], faceNodes[2+iTri]);
333 if(_volumes.find(key) == _volumes.end())
335 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1+iTri]], _nodes[faceNodes[2+iTri]]);
336 calculateVolume(tri, key);
337 totalVolume += _volumes[key];
341 totalVolume -= _volumes[key];
348 std::cout << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment." << std::endl;
355 baryCalculator.getBary( baryCentre );
356 _t->reverseApply( baryCentre, baryCentre );
360 // reset if it is very small to keep the matrix sparse
361 // is this a good idea?
362 if(epsilonEqual(totalVolume, 0.0, SPARSE_TRUNCATION_LIMIT))
367 LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
369 // NB : fault in article, Grandy, [8] : it is the determinant of the inverse transformation
370 // that should be used (which is equivalent to dividing by the determinant)
371 return std::fabs(1.0 / _t->determinant() * totalVolume) ;
375 * Calculates the intersection surface of two coplanar triangles.
377 * @param palneNormal normal of the plane for the first triangle
378 * @param planeConstant constant of the equation of the plane for the first triangle
379 * @param p1 coordinates of the first node of the first triangle
380 * @param p2 coordinates of the second node of the first triangle
381 * @param p3 coordinates of the third node of the first triangle
382 * @param p4 coordinates of the first node of the second triangle
383 * @param p5 coordinates of the second node of the second triangle
384 * @param p6 coordinates of the third node of the second triangle
385 * @param dimCaracteristic characteristic size of the meshes containing the triangles
386 * @param precision precision for double float data used for comparison
388 template<class MyMeshType>
389 double SplitterTetra<MyMeshType>::CalculateIntersectionSurfaceOfCoplanarTriangles(const double *const planeNormal,
390 const double planeConstant,
391 const double *const p1, const double *const p2, const double *const p3,
392 const double *const p4, const double *const p5, const double *const p6,
393 const double dimCaracteristic, const double precision)
395 typedef typename MyMeshType::MyConnType ConnType;
396 typedef double Vect2[2];
397 typedef double Triangle2[3][2];
399 const double *const tri0[3] = {p1, p2, p3};
400 const double *const tri1[3] = {p4, p5, p6};
402 // Plane of the first triangle defined by the normal of the triangle and the constant
403 // Project triangles onto coordinate plane most aligned with plane normal
405 double fmax = std::abs(planeNormal[0]);
406 double absMax = std::abs(planeNormal[1]);
412 absMax = std::abs(planeNormal[2]);
418 Triangle2 projTri0, projTri1;
423 // Project onto yz-plane.
424 for (i = 0; i < 3; ++i)
426 projTri0[i][0] = tri0[i][1];
427 projTri0[i][1] = tri0[i][2];
428 projTri1[i][0] = tri1[i][1];
429 projTri1[i][1] = tri1[i][2];
432 else if (maxNormal == 1)
434 // Project onto xz-plane.
435 for (i = 0; i < 3; ++i)
437 projTri0[i][0] = tri0[i][0];
438 projTri0[i][1] = tri0[i][2];
439 projTri1[i][0] = tri1[i][0];
440 projTri1[i][1] = tri1[i][2];
445 // Project onto xy-plane.
446 for (i = 0; i < 3; ++i)
448 projTri0[i][0] = tri0[i][0];
449 projTri0[i][1] = tri0[i][1];
450 projTri1[i][0] = tri1[i][0];
451 projTri1[i][1] = tri1[i][1];
455 // 2D triangle intersection routines require counterclockwise ordering.
459 for (int ii = 0; ii < 2; ++ii)
461 edge0[ii] = projTri0[1][ii] - projTri0[0][ii];
462 edge1[ii] = projTri0[2][ii] - projTri0[0][ii];
464 if ((edge0[0] * edge1[1] - edge0[1] * edge1[0]) < (double) 0.)
466 // Triangle is clockwise, reorder it.
467 for (int ii = 0; ii < 2; ++ii)
469 save[ii] = projTri0[1][ii];
470 projTri0[1][ii] = projTri0[2][ii];
471 projTri0[2][ii] = save[ii];
475 for (int ii = 0; ii < 2; ++ii)
477 edge0[ii] = projTri1[1][ii] - projTri1[0][ii];
478 edge1[ii] = projTri1[2][ii] - projTri1[0][ii];
480 if ((edge0[0] * edge1[1] - edge0[1] * edge1[0]) < (double) 0.)
482 // Triangle is clockwise, reorder it.
483 for (int ii = 0; ii < 2; ++ii)
485 save[ii] = projTri1[1][ii];
486 projTri1[1][ii] = projTri1[2][ii];
487 projTri1[2][ii] = save[ii];
491 std::vector<double> inter2;
492 intersec_de_triangle(projTri0[0], projTri0[1], projTri0[2],
493 projTri1[0], projTri1[1], projTri1[2],
495 dimCaracteristic, precision);
496 ConnType nb_inter=((ConnType)inter2.size())/2;
498 if(nb_inter >3) inter2=reconstruct_polygon(inter2);
501 std::vector<double> inter3;
502 inter3.resize(3 * nb_inter);
503 // Map 2D intersections back to the 3D triangle space.
506 double invNX = ((double) 1.) / planeNormal[0];
507 for (i = 0; i < nb_inter; i++)
509 inter3[3 * i + 1] = inter2[2 * i];
510 inter3[3 * i + 2] = inter2[2 * i + 1];
511 inter3[3 * i] = invNX * (planeConstant - planeNormal[1] * inter3[3 * i + 1] - planeNormal[2] * inter3[3 * i + 2]);
514 else if (maxNormal == 1)
516 double invNY = ((double) 1.) / planeNormal[1];
517 for (i = 0; i < nb_inter; i++)
519 inter3[3 * i] = inter2[2 * i];
520 inter3[3 * i + 2] = inter2[2 * i + 1];
521 inter3[3 * i + 1] = invNY * (planeConstant - planeNormal[0] * inter3[3 * i] - planeNormal[2] * inter3[3 * i + 2]);
526 double invNZ = ((double) 1.) / planeNormal[2];
527 for (i = 0; i < nb_inter; i++)
529 inter3[3 * i] = inter2[2 * i];
530 inter3[3 * i + 1] = inter2[2 * i + 1];
531 inter3[3 * i + 2] = invNZ * (planeConstant - planeNormal[0] * inter3[3 * i] - planeNormal[1] * inter3[3 * i + 1]);
534 surface = polygon_area<3>(inter3);
540 * Determine if a face is coplanar with a triangle.
541 * The first face is characterized by the equation of her plane
543 * @param palneNormal normal of the plane for the first triangle
544 * @param planeConstant constant of the equation of the plane for the first triangle
545 * @param coordsFace coordinates of the triangle face
546 * @param precision precision for double float data used for comparison
548 template<class MyMeshType>
549 bool SplitterTetra<MyMeshType>::IsFacesCoplanar(const double *const planeNormal,
550 const double planeConstant,
551 const double *const *const coordsFace,
552 const double precision)
554 // Compute the signed distances of triangle vertices to the plane. Use an epsilon-thick plane test.
555 // For faces not left
557 for (int i = 0; i < 3; ++i)
559 const double distance = dot(planeNormal, coordsFace[i]) - planeConstant;
560 if (epsilonEqual(distance, precision))
569 * Calculates the surface of intersection of a polygon face in the source mesh and a cell of the target mesh.
570 * It first calculates the transformation that takes the target tetrahedron into the unit tetrahedron. After that, the
571 * faces of the source element are triangulated and the calculated transformation is applied
573 * The algorithm is based on the algorithm of Grandy used in intersectSourceCell to compute
574 * the volume of intersection of two cell elements.
575 * The case with a source face colinear to one of the face of tetrahedrons is taking into account:
576 * the contribution of the face must not be counted two times.
578 * The class will cache the intermediary calculations of transformed nodes of source faces and surfaces associated
579 * with triangulated faces to avoid having to recalculate these.
581 * @param polyType type of the polygon source face
582 * @param polyNodesNbr number of the nodes of the polygon source face
583 * @param polyNodes numbers of the nodes of the polygon source face
584 * @param polyCoords coordinates of the nodes of the polygon source face
585 * @param dimCaracteristic characteristic size of the meshes containing the triangles
586 * @param precision precision for double float data used for comparison
587 * @param listOfTetraFacesTreated list of tetra faces treated
588 * @param listOfTetraFacesColinear list of tetra faces colinear with the polygon source faces
590 template<class MyMeshType>
591 double SplitterTetra<MyMeshType>::intersectSourceFace(const NormalizedCellType polyType,
592 const ConnType polyNodesNbr,
593 const ConnType *const polyNodes,
594 const double *const *const polyCoords,
595 const double dimCaracteristic,
596 const double precision,
597 std::multiset<TriangleFaceKey>& listOfTetraFacesTreated,
598 std::set<TriangleFaceKey>& listOfTetraFacesColinear)
600 double totalSurface = 0.0;
602 // check if we have planar tetra element
603 if(_t->determinant() == 0.0)
606 LOG(2, "Planar tetra -- volume 0");
610 // halfspace filtering
611 bool isOutside[8] = {true, true, true, true, true, true, true, true};
612 bool isStrictlyOutside[8] = {true, true, true, true, true, true, true, true};
613 bool isTargetStrictlyOutside = false;
614 bool isTargetOutside = false;
616 // calculate the coordinates of the nodes
617 for(ConnType i = 0;i<polyNodesNbr;++i)
619 const ConnType globalNodeNum = polyNodes[i];
620 if(_nodes.find(globalNodeNum) == _nodes.end())
622 calculateNode2(globalNodeNum, polyCoords[i]);
625 CheckIsStrictlyOutside(_nodes[globalNodeNum], isStrictlyOutside, precision);
626 CheckIsOutside(_nodes[globalNodeNum], isOutside, precision);
629 // halfspace filtering check
630 // NB : might not be beneficial for caching of triangles
631 for(int i = 0; i < 8; ++i)
633 if(isStrictlyOutside[i])
635 isTargetStrictlyOutside = true;
638 else if (isOutside[i])
640 isTargetOutside = true;
644 if (!isTargetStrictlyOutside)
649 // Faces are parallel
650 const int tetraFacesNodesConn[4][3] = {
655 double planeNormal[3];
656 for (int iTetraFace = 0; iTetraFace < 4; ++iTetraFace)
658 const int * const tetraFaceNodesConn = tetraFacesNodesConn[iTetraFace];
659 TriangleFaceKey key = TriangleFaceKey(_conn[tetraFaceNodesConn[0]],
660 _conn[tetraFaceNodesConn[1]],
661 _conn[tetraFaceNodesConn[2]]);
662 if (listOfTetraFacesTreated.find(key) == listOfTetraFacesTreated.end())
664 const double * const coordsTetraTriNode1 = _coords + tetraFaceNodesConn[0] * MyMeshType::MY_SPACEDIM;
665 const double * const coordsTetraTriNode2 = _coords + tetraFaceNodesConn[1] * MyMeshType::MY_SPACEDIM;
666 const double * const coordsTetraTriNode3 = _coords + tetraFaceNodesConn[2] * MyMeshType::MY_SPACEDIM;
667 calculateNormalForTria(coordsTetraTriNode1, coordsTetraTriNode2, coordsTetraTriNode3, planeNormal);
668 const double normOfTetraTriNormal = norm(planeNormal);
669 if (epsilonEqual(normOfTetraTriNormal, 0.))
671 for (int i = 0; i < 3; ++i)
678 const double invNormOfTetraTriNormal = 1. / normOfTetraTriNormal;
679 for (int i = 0; i < 3; ++i)
681 planeNormal[i] *= invNormOfTetraTriNormal;
684 double planeConstant = dot(planeNormal, coordsTetraTriNode1);
685 if (IsFacesCoplanar(planeNormal, planeConstant, polyCoords, precision))
687 ConnType nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
688 for (ConnType iTri = 0; iTri < nbrPolyTri; ++iTri)
690 double volume = CalculateIntersectionSurfaceOfCoplanarTriangles(planeNormal,
693 polyCoords[1 + iTri],
694 polyCoords[2 + iTri],
700 if (!epsilonEqual(volume, 0.))
702 totalSurface += volume;
703 listOfTetraFacesColinear.insert(key);
708 listOfTetraFacesTreated.insert(key);
713 // intersect a son with the unit tetra
718 // create the face key
719 TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
721 // calculate the triangle if needed
722 if (_volumes.find(key) == _volumes.end())
724 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
725 calculateSurface(tri, key);
726 totalSurface += _volumes[key];
730 // count negative as face has reversed orientation
731 totalSurface -= _volumes[key];
738 // simple triangulation of faces along a diagonal :
749 //? not sure if this always works
751 // calculate the triangles if needed
753 // local nodes 1, 2, 3
754 TriangleFaceKey key1 = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
755 if (_volumes.find(key1) == _volumes.end())
757 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
758 calculateSurface(tri, key1);
759 totalSurface += _volumes[key1];
763 // count negative as face has reversed orientation
764 totalSurface -= _volumes[key1];
767 // local nodes 1, 3, 4
768 TriangleFaceKey key2 = TriangleFaceKey(polyNodes[0], polyNodes[2], polyNodes[3]);
769 if (_volumes.find(key2) == _volumes.end())
771 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[2]], _nodes[polyNodes[3]]);
772 calculateSurface(tri, key2);
773 totalSurface += _volumes[key2];
777 // count negative as face has reversed orientation
778 totalSurface -= _volumes[key2];
785 ConnType nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
786 for (ConnType iTri = 0; iTri < nbrPolyTri; ++iTri)
788 TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1 + iTri], polyNodes[2 + iTri]);
789 if (_volumes.find(key) == _volumes.end())
791 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1 + iTri]],
792 _nodes[polyNodes[2 + iTri]]);
793 calculateSurface(tri, key);
794 totalSurface += _volumes[key];
798 totalSurface -= _volumes[key];
806 << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment."
814 // reset if it is very small to keep the matrix sparse
815 // is this a good idea?
816 if(epsilonEqual(totalSurface, 0.0, SPARSE_TRUNCATION_LIMIT))
821 LOG(2, "Volume = " << totalSurface << ", det= " << _t->determinant());
827 * Calculates the volume of intersection of this tetrahedron with another one.
829 template<class MyMeshType>
830 double SplitterTetra<MyMeshType>::intersectTetra(const double** tetraCorners)
832 //{ could be done on outside?
833 // check if we have planar tetra element
834 if(_t->determinant() == 0.0)
837 LOG(2, "Planar tetra -- volume 0");
841 const unsigned nbOfNodes4Type=4;
842 // halfspace filtering
843 bool isOutside[8] = {true, true, true, true, true, true, true, true};
844 bool isTargetOutside = false;
846 // calculate the transformed coordinates of the nodes
847 double nodes[nbOfNodes4Type][3];
848 for(int i = 0;i<(int)nbOfNodes4Type;++i)
850 _t->apply(nodes[i], tetraCorners[i]);
851 CheckIsOutside(nodes[i], isOutside);
854 // halfspace filtering check
855 // NB : might not be beneficial for caching of triangles
856 for(int i = 0; i < 8; ++i)
860 isTargetOutside = true;
864 double totalVolume = 0.0;
868 const CellModel& cellModelCell=CellModel::GetCellModel(NORM_TETRA4);
869 ConnType cellNodes[4] = { 0, 1, 2, 3 }, faceNodes[3];
871 for(unsigned ii = 0 ; ii < 4 ; ++ii)
873 cellModelCell.fillSonCellNodalConnectivity(ii,cellNodes,faceNodes);
875 TransformedTriangle tri(nodes[faceNodes[0]], nodes[faceNodes[1]], nodes[faceNodes[2]]);
876 double vol = tri.calculateIntersectionVolume();
880 // reset if it is very small to keep the matrix sparse
881 // is this a good idea?
882 if(epsilonEqual(totalVolume, 0.0, SPARSE_TRUNCATION_LIMIT))
887 LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
889 // NB : fault in article, Grandy, [8] : it is the determinant of the inverse transformation
890 // that should be used (which is equivalent to dividing by the determinant)
891 return std::fabs(1.0 / _t->determinant() * totalVolume) ;
894 ////////////////////////////////////////////////////////
896 template<class MyMeshTypeT, class MyMeshTypeS>
897 SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::SplitterTetra2(const MyMeshTypeT& targetMesh, const MyMeshTypeS& srcMesh, SplittingPolicy policy)
898 :_target_mesh(targetMesh),_src_mesh(srcMesh),_splitting_pol(policy)
902 template<class MyMeshTypeT, class MyMeshTypeS>
903 SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::~SplitterTetra2()
908 template<class MyMeshTypeT, class MyMeshTypeS>
909 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::releaseArrays()
911 // free potential sub-mesh nodes that have been allocated
912 if(_nodes.size()>=/*8*/_node_ids.size())
914 typename MyMeshTypeT::MyConnType nbOfNodesT = static_cast<typename MyMeshTypeT::MyConnType>(_node_ids.size());
915 std::vector<const double*>::iterator iter = _nodes.begin() + /*8*/nbOfNodesT;
916 while(iter != _nodes.end())
926 * \param [in] targetCell in C mode.
927 * \param [out] tetra is the output result tetra containers.
929 template<class MyMeshTypeT, class MyMeshTypeS>
930 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitTargetCell2(typename MyMeshTypeT::MyConnType targetCell, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
932 typedef typename MyMeshTypeT::MyConnType TConnType;
933 const TConnType *refConn(_target_mesh.getConnectivityPtr());
934 const TConnType *cellConn(refConn+_target_mesh.getConnectivityIndexPtr()[targetCell]);
935 INTERP_KERNEL::NormalizedCellType gt(_target_mesh.getTypeOfElement(targetCell));
936 std::vector<TConnType> tetrasNodalConn;
937 std::vector<double> addCoords;
938 const double *coords(_target_mesh.getCoordinatesPtr());
939 SplitIntoTetras(_splitting_pol,gt,cellConn,refConn+_target_mesh.getConnectivityIndexPtr()[targetCell+1],coords,tetrasNodalConn,addCoords);
940 std::size_t nbTetras(tetrasNodalConn.size()/4); tetra.resize(nbTetras);
942 typename MyMeshTypeS::MyConnType tmp2[4];
943 for(std::size_t i=0;i<nbTetras;i++)
947 TConnType cellId(tetrasNodalConn[4*i+j]);
951 tmp[j*3+0]=coords[3*cellId+0];
952 tmp[j*3+1]=coords[3*cellId+1];
953 tmp[j*3+2]=coords[3*cellId+2];
957 tmp[j*3+0]=addCoords[3*(-cellId-1)+0];
958 tmp[j*3+1]=addCoords[3*(-cellId-1)+1];
959 tmp[j*3+2]=addCoords[3*(-cellId-1)+2];
962 tetra[i]=new SplitterTetra<MyMeshTypeS>(_src_mesh,tmp,tmp2);
967 * @param targetCell in C mode.
968 * @param tetra is the output result tetra containers.
970 template<class MyMeshTypeT, class MyMeshTypeS>
971 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitTargetCell(typename MyMeshTypeT::MyConnType targetCell,
972 typename MyMeshTypeT::MyConnType nbOfNodesT,
973 typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
975 typedef typename MyMeshTypeT::MyConnType ConnType;
976 const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
977 const int numTetra = static_cast<int>(_splitting_pol);
983 const double *nodes[4];
985 for(int node = 0; node < 4 ; ++node)
987 nodes[node]=getCoordsOfNode2(node, OTT<ConnType,numPol>::indFC(targetCell),_target_mesh,conn[node]);
989 std::copy(conn,conn+4,_node_ids.begin());
990 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
994 // Issue 0020634. To pass nbOfNodesT to calculateSubNodes (don't want to add an arg)
995 _node_ids.resize(nbOfNodesT);
997 // pre-calculate nodes
998 calculateSubNodes(_target_mesh, OTT<ConnType,numPol>::indFC(targetCell));
1000 tetra.reserve(numTetra);
1001 _nodes.reserve(30); // we never have more than this
1003 switch ( nbOfNodesT )
1007 switch(_splitting_pol)
1011 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
1012 fiveSplit(subZone,tetra);
1018 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
1019 sixSplit(subZone,tetra);
1025 calculateGeneral24Tetra(tetra);
1031 calculateGeneral48Tetra(tetra);
1046 splitConvex(targetCell, tetra);
1052 * Splits the hexahedron into five tetrahedra.
1053 * This method adds five SplitterTetra objects to the vector tetra.
1055 * @param subZone the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the
1056 * splitting to be reused on the subzones of the GENERAL_* types of splitting
1058 template<class MyMeshTypeT, class MyMeshTypeS>
1059 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::fiveSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1061 // create tetrahedra
1062 for(int i = 0; i < 5; ++i)
1064 const double* nodes[4];
1065 typename MyMeshTypeS::MyConnType conn[4];
1066 for(int j = 0; j < 4; ++j)
1068 conn[j] = subZone[ SPLIT_NODES_5[4*i+j] ];
1069 nodes[j] = getCoordsOfSubNode(conn[j]);
1071 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1077 * Splits the hexahedron into six tetrahedra.
1078 * This method adds six SplitterTetra objects to the vector tetra.
1080 * @param subZone the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the
1081 * splitting to be reused on the subzones of the GENERAL_* types of splitting
1083 template<class MyMeshTypeT, class MyMeshTypeS>
1084 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::sixSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1086 for(int i = 0; i < 6; ++i)
1088 const double* nodes[4];
1089 typename MyMeshTypeS::MyConnType conn[4];
1090 for(int j = 0; j < 4; ++j)
1092 conn[j] = subZone[SPLIT_NODES_6[4*i+j]];
1093 nodes[j] = getCoordsOfSubNode(conn[j]);
1095 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1101 * Splits the hexahedron into 24 tetrahedra.
1102 * The splitting is done by combining the barycenter of the tetrahedron, the barycenter of each face
1103 * and the nodes of each edge of the face. This creates 6 faces * 4 edges / face = 24 tetrahedra.
1104 * The submesh nodes introduced are the barycenters of the faces and the barycenter of the cell.
1107 template<class MyMeshTypeT, class MyMeshTypeS>
1108 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral24Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1110 // The two nodes of the original mesh cell used in each tetrahedron.
1111 // The tetrahedra all have nodes (cellCenter, faceCenter, edgeNode1, edgeNode2)
1112 // For the correspondence of the nodes, see the GENERAL_48_SUB_NODES table in calculateSubNodes
1114 // nodes to use for tetrahedron
1115 const double* nodes[4];
1116 typename MyMeshTypeS::MyConnType conn[4];
1117 // get the cell center
1119 nodes[0] = getCoordsOfSubNode(conn[0]);
1121 for(int faceCenterNode = 8; faceCenterNode < 14; ++faceCenterNode)
1123 // get the face center
1124 conn[1] = faceCenterNode;
1125 nodes[1] = getCoordsOfSubNode(conn[1]);
1126 for(int j = 0; j < 4; ++j)
1128 const int row = 4*(faceCenterNode - 8) + j;
1129 conn[2] = TETRA_EDGES_GENERAL_24[2*row];
1130 conn[3] = TETRA_EDGES_GENERAL_24[2*row + 1];
1131 nodes[2] = getCoordsOfSubNode(conn[2]);
1132 nodes[3] = getCoordsOfSubNode(conn[3]);
1134 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes, conn);
1142 * Splits the hexahedron into 48 tetrahedra.
1143 * The splitting is done by introducing the midpoints of all the edges
1144 * and the barycenter of the element as submesh nodes. The 8 hexahedral subzones thus defined
1145 * are then split into 6 tetrahedra each, as in Grandy, p. 449. The division of the subzones
1146 * is done by calling sixSplit().
1149 template<class MyMeshTypeT, class MyMeshTypeS>
1150 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral48Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1152 for(int i = 0; i < 8; ++i)
1154 sixSplit(GENERAL_48_SUBZONES+8*i,tetra);
1159 * Splits the NORM_PYRA5 into 2 tetrahedra.
1161 template<class MyMeshTypeT, class MyMeshTypeS>
1162 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitPyram5(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1164 static const int SPLIT_PYPA5[2][4] =
1174 // create tetrahedra
1175 const double* nodes[4];
1176 typename MyMeshTypeS::MyConnType conn[4];
1177 for(int i = 0; i < 2; ++i)
1179 for(int j = 0; j < 4; ++j)
1180 nodes[j] = getCoordsOfSubNode2(SPLIT_PYPA5[i][j],conn[j]);
1181 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1187 * Splits a convex cell into tetrahedra.
1189 template<class MyMeshTypeT, class MyMeshTypeS>
1190 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitConvex(typename MyMeshTypeT::MyConnType targetCell,
1191 typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1193 // Each face of a cell is split into triangles and
1194 // each of triangles and a cell barycenter form a tetrahedron.
1196 typedef typename MyMeshTypeT::MyConnType ConnType;
1197 const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
1199 // get type of cell and nb of cell nodes
1200 NormalizedCellType normCellType=_target_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(targetCell));
1201 const CellModel& cellModelCell=CellModel::GetCellModel(normCellType);
1202 ConnType nbOfCellNodes=cellModelCell.isDynamic() ? _target_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(targetCell)) : cellModelCell.getNumberOfNodes();
1204 // get nb of cell sons (faces)
1205 const ConnType* rawCellConn = _target_mesh.getConnectivityPtr() + OTT<ConnType,numPol>::conn2C( _target_mesh.getConnectivityIndexPtr()[ targetCell ]);
1206 const ConnType rawNbCellNodes = _target_mesh.getConnectivityIndexPtr()[ targetCell+1 ] - _target_mesh.getConnectivityIndexPtr()[ targetCell ];
1207 unsigned nbOfSons = cellModelCell.getNumberOfSons2(rawCellConn, rawNbCellNodes);
1209 // indices of nodes of a son
1210 static std::vector<ConnType> allNodeIndices; // == 0,1,2,...,nbOfCellNodes-1
1211 while ( allNodeIndices.size() < (std::size_t)nbOfCellNodes )
1212 allNodeIndices.push_back( static_cast<ConnType>(allNodeIndices.size()) );
1213 std::vector<ConnType> classicFaceNodes(4);
1214 if(cellModelCell.isQuadratic())
1215 throw INTERP_KERNEL::Exception("SplitterTetra2::splitConvex : quadratic 3D cells are not implemented yet !");
1216 ConnType* faceNodes = cellModelCell.isDynamic() ? &allNodeIndices[0] : &classicFaceNodes[0];
1218 // nodes of tetrahedron
1219 typename MyMeshTypeS::MyConnType conn[4];
1220 const double* nodes[4];
1221 nodes[3] = getCoordsOfSubNode2( nbOfCellNodes,conn[3]); // barycenter
1223 for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
1225 // get indices of son's nodes: it's just next portion of allNodeIndices for polyhedron
1226 // and some of allNodeIndices accodring to cell model for a classsic cell
1227 unsigned nbFaceNodes = cellModelCell.getNumberOfNodesConstituentTheSon2(ii, rawCellConn, rawNbCellNodes);
1228 if ( normCellType != NORM_POLYHED )
1229 cellModelCell.fillSonCellNodalConnectivity(ii,&allNodeIndices[0],faceNodes);
1231 int nbTetra = nbFaceNodes - 2; // split polygon into nbTetra triangles
1233 // create tetrahedra
1234 for(int i = 0; i < nbTetra; ++i)
1236 nodes[0] = getCoordsOfSubNode2( faceNodes[0], conn[0]);
1237 nodes[1] = getCoordsOfSubNode2( faceNodes[1+i],conn[1]);
1238 nodes[2] = getCoordsOfSubNode2( faceNodes[2+i],conn[2]);
1239 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1243 if ( normCellType == NORM_POLYHED )
1244 faceNodes += nbFaceNodes; // go to the next face
1249 * Precalculates all the nodes.
1250 * Retrieves the mesh nodes and allocates the necessary sub-mesh
1251 * nodes according to the splitting policy used.
1252 * This method is meant to be called once by the constructor.
1254 * @param targetMesh the target mesh
1255 * @param targetCell the global number of the cell that the object represents, in targetMesh mode.
1256 * @param policy the splitting policy of the object
1259 template<class MyMeshTypeT, class MyMeshTypeS>
1260 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateSubNodes(const MyMeshTypeT& targetMesh, typename MyMeshTypeT::MyConnType targetCell)
1262 // retrieve real mesh nodes
1264 typename MyMeshTypeT::MyConnType nbOfNodesT = static_cast<typename MyMeshTypeT::MyConnType>(_node_ids.size());// Issue 0020634. _node_ids.resize(8);
1265 for(int node = 0; node < nbOfNodesT ; ++node)
1267 // calculate only normal nodes
1268 _nodes.push_back(getCoordsOfNode2(node, targetCell, targetMesh,_node_ids[node]));
1271 switch ( nbOfNodesT )
1275 // create sub-mesh nodes if needed
1276 switch(_splitting_pol)
1280 for(int i = 0; i < 7; ++i)
1282 double* barycenter = new double[3];
1283 calcBarycenter(4, barycenter, &GENERAL_24_SUB_NODES[4*i]);
1284 _nodes.push_back(barycenter);
1291 for(int i = 0; i < 19; ++i)
1293 double* barycenter = new double[3];
1294 calcBarycenter(2, barycenter, &GENERAL_48_SUB_NODES[2*i]);
1295 _nodes.push_back(barycenter);
1304 case 5: // NORM_PYRA5
1307 default: // convex 3d cell
1309 // add barycenter of a cell
1310 std::vector<int> allIndices(nbOfNodesT);
1311 for ( int i = 0; i < nbOfNodesT; ++i ) allIndices[i] = i;
1312 double* barycenter = new double[3];
1313 calcBarycenter(nbOfNodesT, barycenter, &allIndices[0]);
1314 _nodes.push_back(barycenter);