1 // Copyright (C) 2007-2013 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.
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
42 * output is expected to be allocated with 24*sizeof(void*) in order to store the 24 tetras.
43 * These tetras have to be deallocated.
45 template<class MyMeshType>
46 void SplitterTetra<MyMeshType>::splitIntoDualCells(SplitterTetra<MyMeshType> **output)
49 const double *tmp2[4]={tmp,tmp+3,tmp+6,tmp+9};
50 typename MyMeshType::MyConnType conn[4]={-1,-1,-1,-1};
53 splitMySelfForDual(tmp,i,conn[0]);
54 output[i]=new SplitterTetra<MyMeshType>(_src_mesh,tmp2,conn);
59 * Constructor creating object from the four corners of the tetrahedron.
61 * @param srcMesh mesh containing the source elements
62 * @param tetraCorners array of four pointers to double[3] arrays containing the coordinates of the
63 * corners of the tetrahedron
65 template<class MyMeshType>
66 SplitterTetra<MyMeshType>::SplitterTetra(const MyMeshType& srcMesh, const double** tetraCorners, const typename MyMeshType::MyConnType *nodesId)
67 : _t(0), _src_mesh(srcMesh)
69 std::copy(nodesId,nodesId+4,_conn);
70 _coords[0]=tetraCorners[0][0]; _coords[1]=tetraCorners[0][1]; _coords[2]=tetraCorners[0][2];
71 _coords[3]=tetraCorners[1][0]; _coords[4]=tetraCorners[1][1]; _coords[5]=tetraCorners[1][2];
72 _coords[6]=tetraCorners[2][0]; _coords[7]=tetraCorners[2][1]; _coords[8]=tetraCorners[2][2];
73 _coords[9]=tetraCorners[3][0]; _coords[10]=tetraCorners[3][1]; _coords[11]=tetraCorners[3][2];
74 // create the affine transform
75 createAffineTransform(tetraCorners);
81 * Deletes _t and the coordinates (double[3] vectors) in _nodes
84 template<class MyMeshType>
85 SplitterTetra<MyMeshType>::~SplitterTetra()
88 for(HashMap< int, double* >::iterator iter = _nodes.begin(); iter != _nodes.end() ; ++iter)
89 delete[] iter->second;
93 * \Forget already calculated triangles, which is crucial for calculation of barycenter of intersection
95 template<class MyMeshType>
96 void SplitterTetra<MyMeshType>::clearVolumesCache()
102 * This method destroys the 4 pointers pointed by tetraCorners[0],tetraCorners[1],tetraCorners[2] and tetraCorners[3]
103 * @param i is in 0..23 included.
104 * @param output is expected to be sized of 12 in order to.
106 template<class MyMeshType>
107 void SplitterTetra<MyMeshType>::splitMySelfForDual(double* output, int i, typename MyMeshType::MyConnType& nodeId)
111 nodeId=_conn[offset];
112 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;
114 int case1=caseToTreat/2;
115 int case2=caseToTreat%2;
116 const int tab[3][2]={{1,2},{3,2},{1,3}};
117 const int *curTab=tab[case1];
118 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.;
119 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.;
120 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.;
121 std::copy(pt1,pt1+3,output+case2*3);
122 std::copy(pt0,pt0+3,output+(abs(case2-1))*3);
123 std::copy(pt2,pt2+3,output+2*3);
124 std::copy(tmp[0],tmp[0]+3,output+3*3);
128 * Calculates the volume of intersection of an element in the source mesh and the target element.
129 * It first calculates the transformation that takes the target tetrahedron into the unit tetrahedron. After that, the
130 * faces of the source element are triangulated and the calculated transformation is applied
131 * to each triangle. The algorithm of Grandy, implemented in INTERP_KERNEL::TransformedTriangle is used
132 * to calculate the contribution to the volume from each triangle. The volume returned is the sum of these contributions
133 * divided by the determinant of the transformation.
135 * The class will cache the intermediary calculations of transformed nodes of source cells and volumes associated
136 * with triangulated faces to avoid having to recalculate these.
138 * @param element global number of the source element in C mode.
140 template<class MyMeshType>
141 double SplitterTetra<MyMeshType>::intersectSourceCell(typename MyMeshType::MyConnType element,
144 typedef typename MyMeshType::MyConnType ConnType;
145 const NumberingPolicy numPol=MyMeshType::My_numPol;
146 //{ could be done on outside?
147 // check if we have planar tetra element
148 if(_t->determinant() == 0.0)
151 LOG(2, "Planar tetra -- volume 0");
156 NormalizedCellType normCellType=_src_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(element));
157 const CellModel& cellModelCell=CellModel::GetCellModel(normCellType);
158 unsigned nbOfNodes4Type=cellModelCell.isDynamic() ? _src_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(element)) : cellModelCell.getNumberOfNodes();
159 // halfspace filtering
160 bool isOutside[8] = {true, true, true, true, true, true, true, true};
161 bool isTargetOutside = false;
163 // calculate the coordinates of the nodes
164 int *cellNodes=new int[nbOfNodes4Type];
165 for(int i = 0;i<(int)nbOfNodes4Type;++i)
167 // we could store mapping local -> global numbers too, but not sure it is worth it
168 const int globalNodeNum = getGlobalNumberOfNode(i, OTT<ConnType,numPol>::indFC(element), _src_mesh);
169 cellNodes[i]=globalNodeNum;
170 if(_nodes.find(globalNodeNum) == _nodes.end())
172 //for(HashMap< int , double* >::iterator iter3=_nodes.begin();iter3!=_nodes.end();iter3++)
173 // std::cout << (*iter3).first << " ";
174 //std::cout << std::endl << "*** " << globalNodeNum << std::endl;
175 calculateNode(globalNodeNum);
178 checkIsOutside(_nodes[globalNodeNum], isOutside);
181 // halfspace filtering check
182 // NB : might not be beneficial for caching of triangles
183 for(int i = 0; i < 8; ++i)
187 isTargetOutside = true;
191 double totalVolume = 0.0;
195 /// calculator of intersection barycentre
196 UnitTetraIntersectionBary baryCalculator( _t->determinant() < 0.);
198 // get nb of sons of a cell
199 const ConnType* rawCellConn = _src_mesh.getConnectivityPtr() + OTT<ConnType,numPol>::conn2C( _src_mesh.getConnectivityIndexPtr()[ element ]);
200 const int rawNbCellNodes = _src_mesh.getConnectivityIndexPtr()[ element+1 ] - _src_mesh.getConnectivityIndexPtr()[ element ];
201 unsigned nbOfSons = cellModelCell.getNumberOfSons2(rawCellConn, rawNbCellNodes);
203 for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
205 // get sons connectivity
206 NormalizedCellType faceType;
207 int *faceNodes, nbFaceNodes=-1;
208 if ( cellModelCell.isDynamic() )
210 faceNodes=new int[nbOfNodes4Type];
211 nbFaceNodes = cellModelCell.fillSonCellNodalConnectivity2(ii,rawCellConn,rawNbCellNodes,faceNodes,faceType);
212 for ( int i = 0; i < nbFaceNodes; ++i )
213 faceNodes[i] = OTT<ConnType,numPol>::coo2C(faceNodes[i]);
217 faceType = cellModelCell.getSonType(ii);
218 const CellModel& faceModel=CellModel::GetCellModel(faceType);
219 assert(faceModel.getDimension() == 2);
220 faceNodes=new int[faceModel.getNumberOfNodes()];
221 cellModelCell.fillSonCellNodalConnectivity(ii,cellNodes,faceNodes);
223 // intersect a son with the unit tetra
228 // create the face key
229 TriangleFaceKey key = TriangleFaceKey(faceNodes[0], faceNodes[1], faceNodes[2]);
231 // calculate the triangle if needed
232 if(_volumes.find(key) == _volumes.end())
234 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1]], _nodes[faceNodes[2]]);
235 calculateVolume(tri, key);
236 totalVolume += _volumes[key];
238 baryCalculator.addSide( tri );
240 // count negative as face has reversed orientation
241 totalVolume -= _volumes[key];
248 // simple triangulation of faces along a diagonal :
259 //? not sure if this always works
261 // calculate the triangles if needed
263 // local nodes 1, 2, 3
264 TriangleFaceKey key1 = TriangleFaceKey(faceNodes[0], faceNodes[1], faceNodes[2]);
265 if(_volumes.find(key1) == _volumes.end())
267 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1]], _nodes[faceNodes[2]]);
268 calculateVolume(tri, key1);
269 totalVolume += _volumes[key1];
271 // count negative as face has reversed orientation
272 totalVolume -= _volumes[key1];
275 // local nodes 1, 3, 4
276 TriangleFaceKey key2 = TriangleFaceKey(faceNodes[0], faceNodes[2], faceNodes[3]);
277 if(_volumes.find(key2) == _volumes.end())
279 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[2]], _nodes[faceNodes[3]]);
280 calculateVolume(tri, key2);
281 totalVolume += _volumes[key2];
285 // count negative as face has reversed orientation
286 totalVolume -= _volumes[key2];
293 int nbTria = nbFaceNodes - 2; // split polygon into nbTria triangles
294 for ( int iTri = 0; iTri < nbTria; ++iTri )
296 TriangleFaceKey key = TriangleFaceKey(faceNodes[0], faceNodes[1+iTri], faceNodes[2+iTri]);
297 if(_volumes.find(key) == _volumes.end())
299 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1+iTri]], _nodes[faceNodes[2+iTri]]);
300 calculateVolume(tri, key);
301 totalVolume += _volumes[key];
305 totalVolume -= _volumes[key];
312 std::cout << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment." << std::endl;
319 baryCalculator.getBary( baryCentre );
320 _t->reverseApply( baryCentre, baryCentre );
324 // reset if it is very small to keep the matrix sparse
325 // is this a good idea?
326 if(epsilonEqual(totalVolume, 0.0, SPARSE_TRUNCATION_LIMIT))
331 LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
333 // NB : fault in article, Grandy, [8] : it is the determinant of the inverse transformation
334 // that should be used (which is equivalent to dividing by the determinant)
335 return std::fabs(1.0 / _t->determinant() * totalVolume) ;
339 * Calculates the intersection surface of two coplanar triangles.
341 * @param palneNormal normal of the plane for the first triangle
342 * @param planeConstant constant of the equation of the plane for the first triangle
343 * @param p1 coordinates of the first node of the first triangle
344 * @param p2 coordinates of the second node of the first triangle
345 * @param p3 coordinates of the third node of the first triangle
346 * @param p4 coordinates of the first node of the second triangle
347 * @param p5 coordinates of the second node of the second triangle
348 * @param p6 coordinates of the third node of the second triangle
349 * @param dimCaracteristic characteristic size of the meshes containing the triangles
350 * @param precision precision for double float data used for comparison
352 template<class MyMeshType>
353 double SplitterTetra<MyMeshType>::CalculateIntersectionSurfaceOfCoplanarTriangles(const double *const planeNormal,
354 const double planeConstant,
355 const double *const p1, const double *const p2, const double *const p3,
356 const double *const p4, const double *const p5, const double *const p6,
357 const double dimCaracteristic, const double precision)
359 typedef typename MyMeshType::MyConnType ConnType;
360 typedef double Vect2[2];
361 typedef double Vect3[3];
362 typedef double Triangle2[3][2];
364 const double *const tri0[3] = {p1, p2, p3};
365 const double *const tri1[3] = {p4, p5, p6};
367 // Plane of the first triangle defined by the normal of the triangle and the constant
368 // Project triangles onto coordinate plane most aligned with plane normal
370 double fmax = std::abs(planeNormal[0]);
371 double absMax = std::abs(planeNormal[1]);
377 absMax = std::abs(planeNormal[2]);
383 Triangle2 projTri0, projTri1;
388 // Project onto yz-plane.
389 for (i = 0; i < 3; ++i)
391 projTri0[i][0] = tri0[i][1];
392 projTri0[i][1] = tri0[i][2];
393 projTri1[i][0] = tri1[i][1];
394 projTri1[i][1] = tri1[i][2];
397 else if (maxNormal == 1)
399 // Project onto xz-plane.
400 for (i = 0; i < 3; ++i)
402 projTri0[i][0] = tri0[i][0];
403 projTri0[i][1] = tri0[i][2];
404 projTri1[i][0] = tri1[i][0];
405 projTri1[i][1] = tri1[i][2];
410 // Project onto xy-plane.
411 for (i = 0; i < 3; ++i)
413 projTri0[i][0] = tri0[i][0];
414 projTri0[i][1] = tri0[i][1];
415 projTri1[i][0] = tri1[i][0];
416 projTri1[i][1] = tri1[i][1];
420 // 2D triangle intersection routines require counterclockwise ordering.
424 for (int ii = 0; ii < 2; ++ii)
426 edge0[ii] = projTri0[1][ii] - projTri0[0][ii];
427 edge1[ii] = projTri0[2][ii] - projTri0[0][ii];
429 if ((edge0[0] * edge1[1] - edge0[1] * edge1[0]) < (double) 0.)
431 // Triangle is clockwise, reorder it.
432 for (int ii = 0; ii < 2; ++ii)
434 save[ii] = projTri0[1][ii];
435 projTri0[1][ii] = projTri0[2][ii];
436 projTri0[2][ii] = save[ii];
440 for (int ii = 0; ii < 2; ++ii)
442 edge0[ii] = projTri1[1][ii] - projTri1[0][ii];
443 edge1[ii] = projTri1[2][ii] - projTri1[0][ii];
445 if ((edge0[0] * edge1[1] - edge0[1] * edge1[0]) < (double) 0.)
447 // Triangle is clockwise, reorder it.
448 for (int ii = 0; ii < 2; ++ii)
450 save[ii] = projTri1[1][ii];
451 projTri1[1][ii] = projTri1[2][ii];
452 projTri1[2][ii] = save[ii];
456 std::vector<double> inter2;
457 intersec_de_triangle(projTri0[0], projTri0[1], projTri0[2],
458 projTri1[0], projTri1[1], projTri1[2],
460 dimCaracteristic, precision);
461 ConnType nb_inter=((ConnType)inter2.size())/2;
463 if(nb_inter >3) inter2=reconstruct_polygon(inter2);
466 std::vector<double> inter3;
467 inter3.resize(3 * nb_inter);
468 // Map 2D intersections back to the 3D triangle space.
471 double invNX = ((double) 1.) / planeNormal[0];
472 for (i = 0; i < nb_inter; i++)
474 inter3[3 * i + 1] = inter2[2 * i];
475 inter3[3 * i + 2] = inter2[2 * i + 1];
476 inter3[3 * i] = invNX * (planeConstant - planeNormal[1] * inter3[3 * i + 1] - planeNormal[2] * inter3[3 * i + 2]);
479 else if (maxNormal == 1)
481 double invNY = ((double) 1.) / planeNormal[1];
482 for (i = 0; i < nb_inter; i++)
484 inter3[3 * i] = inter2[2 * i];
485 inter3[3 * i + 2] = inter2[2 * i + 1];
486 inter3[3 * i + 1] = invNY * (planeConstant - planeNormal[0] * inter3[3 * i] - planeNormal[2] * inter3[3 * i + 2]);
491 double invNZ = ((double) 1.) / planeNormal[2];
492 for (i = 0; i < nb_inter; i++)
494 inter3[3 * i] = inter2[2 * i];
495 inter3[3 * i + 1] = inter2[2 * i + 1];
496 inter3[3 * i + 2] = invNZ * (planeConstant - planeNormal[0] * inter3[3 * i] - planeNormal[1] * inter3[3 * i + 1]);
499 surface = polygon_area<3>(inter3);
505 * Determine if a face is coplanar with a triangle.
506 * The first face is characterized by the equation of her plane
508 * @param palneNormal normal of the plane for the first triangle
509 * @param planeConstant constant of the equation of the plane for the first triangle
510 * @param coordsFace coordinates of the triangle face
511 * @param precision precision for double float data used for comparison
513 template<class MyMeshType>
514 bool SplitterTetra<MyMeshType>::IsFacesCoplanar(const double *const planeNormal,
515 const double planeConstant,
516 const double *const *const coordsFace,
517 const double precision)
519 // Compute the signed distances of triangle vertices to the plane. Use an epsilon-thick plane test.
520 // For faces not left
522 for (int i = 0; i < 3; ++i)
524 const double distance = dot(planeNormal, coordsFace[i]) - planeConstant;
525 if (epsilonEqual(distance, precision))
537 * Calculates the surface of intersection of a polygon face in the source mesh and a cell of the target mesh.
538 * It first calculates the transformation that takes the target tetrahedron into the unit tetrahedron. After that, the
539 * faces of the source element are triangulated and the calculated transformation is applied
541 * The algorithm is based on the algorithm of Grandy used in intersectSourceCell to compute
542 * the volume of intersection of two cell elements.
543 * The case with a source face colinear to one of the face of tetrahedrons is taking into account:
544 * the contribution of the face must not be counted two times.
546 * The class will cache the intermediary calculations of transformed nodes of source faces and surfaces associated
547 * with triangulated faces to avoid having to recalculate these.
549 * @param polyType type of the polygon source face
550 * @param polyNodesNbr number of the nodes of the polygon source face
551 * @param polyNodes numbers of the nodes of the polygon source face
552 * @param polyCoords coordinates of the nodes of the polygon source face
553 * @param polyCoords coordinates of the nodes of the polygon source face
554 * @param dimCaracteristic characteristic size of the meshes containing the triangles
555 * @param precision precision for double float data used for comparison
556 * @param listOfTetraFacesTreated list of tetra faces treated
557 * @param listOfTetraFacesColinear list of tetra faces colinear with the polygon source faces
559 template<class MyMeshType>
560 double SplitterTetra<MyMeshType>::intersectSourceFace(const NormalizedCellType polyType,
561 const int polyNodesNbr,
562 const int *const polyNodes,
563 const double *const *const polyCoords,
564 const double dimCaracteristic,
565 const double precision,
566 std::multiset<TriangleFaceKey>& listOfTetraFacesTreated,
567 std::set<TriangleFaceKey>& listOfTetraFacesColinear)
569 typedef typename MyMeshType::MyConnType ConnType;
571 double totalSurface = 0.0;
573 // check if we have planar tetra element
574 if(_t->determinant() == 0.0)
577 LOG(2, "Planar tetra -- volume 0");
581 // halfspace filtering
582 bool isOutside[8] = {true, true, true, true, true, true, true, true};
583 bool isStrictlyOutside[8] = {true, true, true, true, true, true, true, true};
584 bool isTargetStrictlyOutside = false;
585 bool isTargetOutside = false;
587 // calculate the coordinates of the nodes
588 for(int i = 0;i<(int)polyNodesNbr;++i)
590 const int globalNodeNum = polyNodes[i];
591 if(_nodes.find(globalNodeNum) == _nodes.end())
593 //for(HashMap< int , double* >::iterator iter3=_nodes.begin();iter3!=_nodes.end();iter3++)
594 // std::cout << (*iter3).first << " ";
595 //std::cout << std::endl << "*** " << globalNodeNum << std::endl;
596 calculateNode2(globalNodeNum, polyCoords[i]);
599 checkIsStrictlyOutside(_nodes[globalNodeNum], isStrictlyOutside, precision);
600 checkIsOutside(_nodes[globalNodeNum], isOutside, precision);
603 // halfspace filtering check
604 // NB : might not be beneficial for caching of triangles
605 for(int i = 0; i < 8; ++i)
607 if(isStrictlyOutside[i])
609 isTargetStrictlyOutside = true;
612 else if (isOutside[i])
614 isTargetOutside = true;
618 if (!isTargetStrictlyOutside)
623 // Faces are parallel
624 const int tetraFacesNodesConn[4][3] = {
629 double planeNormal[3];
630 for (int iTetraFace = 0; iTetraFace < 4; ++iTetraFace)
632 const int * const tetraFaceNodesConn = tetraFacesNodesConn[iTetraFace];
633 TriangleFaceKey key = TriangleFaceKey(_conn[tetraFaceNodesConn[0]],
634 _conn[tetraFaceNodesConn[1]],
635 _conn[tetraFaceNodesConn[2]]);
636 if (listOfTetraFacesTreated.find(key) == listOfTetraFacesTreated.end())
638 const double * const coordsTetraTriNode1 = _coords + tetraFaceNodesConn[0] * MyMeshType::MY_SPACEDIM;
639 const double * const coordsTetraTriNode2 = _coords + tetraFaceNodesConn[1] * MyMeshType::MY_SPACEDIM;
640 const double * const coordsTetraTriNode3 = _coords + tetraFaceNodesConn[2] * MyMeshType::MY_SPACEDIM;
641 calculateNormalForTria(coordsTetraTriNode1, coordsTetraTriNode2, coordsTetraTriNode3, planeNormal);
642 const double normOfTetraTriNormal = norm(planeNormal);
643 if (epsilonEqual(normOfTetraTriNormal, 0.))
645 for (int i = 0; i < 3; ++i)
652 const double invNormOfTetraTriNormal = 1. / normOfTetraTriNormal;
653 for (int i = 0; i < 3; ++i)
655 planeNormal[i] *= invNormOfTetraTriNormal;
658 double planeConstant = dot(planeNormal, coordsTetraTriNode1);
659 if (IsFacesCoplanar(planeNormal, planeConstant, polyCoords, precision))
661 int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
662 for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
664 double volume = CalculateIntersectionSurfaceOfCoplanarTriangles(planeNormal,
667 polyCoords[1 + iTri],
668 polyCoords[2 + iTri],
674 if (!epsilonEqual(volume, 0.))
676 totalSurface += volume;
677 listOfTetraFacesColinear.insert(key);
682 listOfTetraFacesTreated.insert(key);
687 // intersect a son with the unit tetra
692 // create the face key
693 TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
695 // calculate the triangle if needed
696 if (_volumes.find(key) == _volumes.end())
698 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
699 calculateSurface(tri, key);
700 totalSurface += _volumes[key];
704 // count negative as face has reversed orientation
705 totalSurface -= _volumes[key];
712 // simple triangulation of faces along a diagonal :
723 //? not sure if this always works
725 // calculate the triangles if needed
727 // local nodes 1, 2, 3
728 TriangleFaceKey key1 = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
729 if (_volumes.find(key1) == _volumes.end())
731 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
732 calculateSurface(tri, key1);
733 totalSurface += _volumes[key1];
737 // count negative as face has reversed orientation
738 totalSurface -= _volumes[key1];
741 // local nodes 1, 3, 4
742 TriangleFaceKey key2 = TriangleFaceKey(polyNodes[0], polyNodes[2], polyNodes[3]);
743 if (_volumes.find(key2) == _volumes.end())
745 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[2]], _nodes[polyNodes[3]]);
746 calculateSurface(tri, key2);
747 totalSurface += _volumes[key2];
751 // count negative as face has reversed orientation
752 totalSurface -= _volumes[key2];
759 int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
760 for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
762 TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1 + iTri], polyNodes[2 + iTri]);
763 if (_volumes.find(key) == _volumes.end())
765 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1 + iTri]],
766 _nodes[polyNodes[2 + iTri]]);
767 calculateSurface(tri, key);
768 totalSurface += _volumes[key];
772 totalSurface -= _volumes[key];
780 << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment."
788 // reset if it is very small to keep the matrix sparse
789 // is this a good idea?
790 if(epsilonEqual(totalSurface, 0.0, SPARSE_TRUNCATION_LIMIT))
795 LOG(2, "Volume = " << totalSurface << ", det= " << _t->determinant());
801 * Calculates the volume of intersection of this tetrahedron with another one.
803 template<class MyMeshType>
804 double SplitterTetra<MyMeshType>::intersectTetra(const double** tetraCorners)
806 //{ could be done on outside?
807 // check if we have planar tetra element
808 if(_t->determinant() == 0.0)
811 LOG(2, "Planar tetra -- volume 0");
815 const unsigned nbOfNodes4Type=4;
816 // halfspace filtering
817 bool isOutside[8] = {true, true, true, true, true, true, true, true};
818 bool isTargetOutside = false;
820 // calculate the transformed coordinates of the nodes
821 double nodes[nbOfNodes4Type][3];
822 for(int i = 0;i<(int)nbOfNodes4Type;++i)
824 _t->apply(nodes[i], tetraCorners[i]);
825 checkIsOutside(nodes[i], isOutside);
828 // halfspace filtering check
829 // NB : might not be beneficial for caching of triangles
830 for(int i = 0; i < 8; ++i)
834 isTargetOutside = true;
838 double totalVolume = 0.0;
842 const CellModel& cellModelCell=CellModel::GetCellModel(NORM_TETRA4);
843 int cellNodes[4] = { 0, 1, 2, 3 }, faceNodes[3];
845 for(unsigned ii = 0 ; ii < 4 ; ++ii)
847 cellModelCell.fillSonCellNodalConnectivity(ii,cellNodes,faceNodes);
849 TransformedTriangle tri(nodes[faceNodes[0]], nodes[faceNodes[1]], nodes[faceNodes[2]]);
850 double vol = tri.calculateIntersectionVolume();
854 // reset if it is very small to keep the matrix sparse
855 // is this a good idea?
856 if(epsilonEqual(totalVolume, 0.0, SPARSE_TRUNCATION_LIMIT))
861 LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
863 // NB : fault in article, Grandy, [8] : it is the determinant of the inverse transformation
864 // that should be used (which is equivalent to dividing by the determinant)
865 return std::fabs(1.0 / _t->determinant() * totalVolume) ;
868 ////////////////////////////////////////////////////////
870 template<class MyMeshTypeT, class MyMeshTypeS>
871 SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::SplitterTetra2(const MyMeshTypeT& targetMesh, const MyMeshTypeS& srcMesh, SplittingPolicy policy)
872 :_target_mesh(targetMesh),_src_mesh(srcMesh),_splitting_pol(policy)
876 template<class MyMeshTypeT, class MyMeshTypeS>
877 SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::~SplitterTetra2()
882 template<class MyMeshTypeT, class MyMeshTypeS>
883 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::releaseArrays()
885 // free potential sub-mesh nodes that have been allocated
886 typename MyMeshTypeT::MyConnType nbOfNodesT = _node_ids.size();// Issue 0020634.
887 if((int)_nodes.size()>=/*8*/nbOfNodesT)
889 std::vector<const double*>::iterator iter = _nodes.begin() + /*8*/nbOfNodesT;
890 while(iter != _nodes.end())
900 * @param targetCell in C mode.
901 * @param tetra is the output result tetra containers.
903 template<class MyMeshTypeT, class MyMeshTypeS>
904 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitTargetCell(typename MyMeshTypeT::MyConnType targetCell,
905 typename MyMeshTypeT::MyConnType nbOfNodesT,
906 typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
908 typedef typename MyMeshTypeT::MyConnType ConnType;
909 const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
910 const int numTetra = static_cast<int>(_splitting_pol);
916 const double *nodes[4];
918 for(int node = 0; node < 4 ; ++node)
920 nodes[node]=getCoordsOfNode2(node, OTT<ConnType,numPol>::indFC(targetCell),_target_mesh,conn[node]);
922 std::copy(conn,conn+4,_node_ids.begin());
923 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
927 // Issue 0020634. To pass nbOfNodesT to calculateSubNodes (don't want to add an arg)
928 _node_ids.resize(nbOfNodesT);
930 // pre-calculate nodes
931 calculateSubNodes(_target_mesh, OTT<ConnType,numPol>::indFC(targetCell));
933 tetra.reserve(numTetra);
934 _nodes.reserve(30); // we never have more than this
936 switch ( nbOfNodesT )
940 switch(_splitting_pol)
944 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
945 fiveSplit(subZone,tetra);
951 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
952 sixSplit(subZone,tetra);
958 calculateGeneral24Tetra(tetra);
964 calculateGeneral48Tetra(tetra);
979 splitConvex(targetCell, tetra);
985 * Splits the hexahedron into five tetrahedra.
986 * This method adds five SplitterTetra objects to the vector tetra.
988 * @param subZone the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the
989 * splitting to be reused on the subzones of the GENERAL_* types of splitting
991 template<class MyMeshTypeT, class MyMeshTypeS>
992 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::fiveSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
995 for(int i = 0; i < 5; ++i)
997 const double* nodes[4];
999 for(int j = 0; j < 4; ++j)
1001 conn[j] = subZone[ SPLIT_NODES_5[4*i+j] ];
1002 nodes[j] = getCoordsOfSubNode(conn[j]);
1004 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1010 * Splits the hexahedron into six tetrahedra.
1011 * This method adds six SplitterTetra objects to the vector tetra.
1013 * @param subZone the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the
1014 * splitting to be reused on the subzones of the GENERAL_* types of splitting
1016 template<class MyMeshTypeT, class MyMeshTypeS>
1017 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::sixSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1019 for(int i = 0; i < 6; ++i)
1021 const double* nodes[4];
1023 for(int j = 0; j < 4; ++j)
1025 conn[j] = subZone[SPLIT_NODES_6[4*i+j]];
1026 nodes[j] = getCoordsOfSubNode(conn[j]);
1028 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1034 * Splits the hexahedron into 24 tetrahedra.
1035 * The splitting is done by combining the barycenter of the tetrahedron, the barycenter of each face
1036 * and the nodes of each edge of the face. This creates 6 faces * 4 edges / face = 24 tetrahedra.
1037 * The submesh nodes introduced are the barycenters of the faces and the barycenter of the cell.
1040 template<class MyMeshTypeT, class MyMeshTypeS>
1041 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral24Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1043 // The two nodes of the original mesh cell used in each tetrahedron.
1044 // The tetrahedra all have nodes (cellCenter, faceCenter, edgeNode1, edgeNode2)
1045 // For the correspondance of the nodes, see the GENERAL_48_SUB_NODES table in calculateSubNodes
1047 // nodes to use for tetrahedron
1048 const double* nodes[4];
1050 // get the cell center
1052 nodes[0] = getCoordsOfSubNode(conn[0]);
1054 for(int faceCenterNode = 8; faceCenterNode < 14; ++faceCenterNode)
1056 // get the face center
1057 conn[1] = faceCenterNode;
1058 nodes[1] = getCoordsOfSubNode(conn[1]);
1059 for(int j = 0; j < 4; ++j)
1061 const int row = 4*(faceCenterNode - 8) + j;
1062 conn[2] = TETRA_EDGES_GENERAL_24[2*row];
1063 conn[3] = TETRA_EDGES_GENERAL_24[2*row + 1];
1064 nodes[2] = getCoordsOfSubNode(conn[2]);
1065 nodes[3] = getCoordsOfSubNode(conn[3]);
1067 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes, conn);
1075 * Splits the hexahedron into 48 tetrahedra.
1076 * The splitting is done by introducing the midpoints of all the edges
1077 * and the barycenter of the element as submesh nodes. The 8 hexahedral subzones thus defined
1078 * are then split into 6 tetrahedra each, as in Grandy, p. 449. The division of the subzones
1079 * is done by calling sixSplit().
1082 template<class MyMeshTypeT, class MyMeshTypeS>
1083 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral48Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1085 // Define 8 hexahedral subzones as in Grandy, p449
1086 // the values correspond to the nodes that correspond to nodes 1,2,3,4,5,6,7,8 in the subcell
1087 // For the correspondance of the nodes, see the GENERAL_48_SUB_NODES table in calculateSubNodes
1088 static const int subZones[64] =
1090 0,8,21,12,9,20,26,22,
1091 8,1,13,21,20,10,23,26,
1092 12,21,16,3,22,26,25,17,
1093 21,13,2,16,26,23,18,25,
1094 9,20,26,22,4,11,24,14,
1095 20,10,23,26,11,5,15,24,
1096 22,26,25,17,14,24,19,7,
1097 26,23,18,25,24,15,6,19
1100 for(int i = 0; i < 8; ++i)
1102 sixSplit(&subZones[8*i],tetra);
1107 * Splits the NORM_PYRA5 into 2 tetrahedra.
1109 template<class MyMeshTypeT, class MyMeshTypeS>
1110 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitPyram5(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1112 static const int SPLIT_PYPA5[2][4] =
1122 // create tetrahedra
1123 const double* nodes[4];
1125 for(int i = 0; i < 2; ++i)
1127 for(int j = 0; j < 4; ++j)
1128 nodes[j] = getCoordsOfSubNode2(SPLIT_PYPA5[i][j],conn[j]);
1129 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1135 * Splits a convex cell into tetrahedra.
1137 template<class MyMeshTypeT, class MyMeshTypeS>
1138 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitConvex(typename MyMeshTypeT::MyConnType targetCell,
1139 typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1141 // Each face of a cell is split into triangles and
1142 // each of triangles and a cell barycenter form a tetrahedron.
1144 typedef typename MyMeshTypeT::MyConnType ConnType;
1145 const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
1147 // get type of cell and nb of cell nodes
1148 NormalizedCellType normCellType=_target_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(targetCell));
1149 const CellModel& cellModelCell=CellModel::GetCellModel(normCellType);
1150 unsigned nbOfCellNodes=cellModelCell.isDynamic() ? _target_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(targetCell)) : cellModelCell.getNumberOfNodes();
1152 // get nb of cell sons (faces)
1153 const ConnType* rawCellConn = _target_mesh.getConnectivityPtr() + OTT<ConnType,numPol>::conn2C( _target_mesh.getConnectivityIndexPtr()[ targetCell ]);
1154 const int rawNbCellNodes = _target_mesh.getConnectivityIndexPtr()[ targetCell+1 ] - _target_mesh.getConnectivityIndexPtr()[ targetCell ];
1155 unsigned nbOfSons = cellModelCell.getNumberOfSons2(rawCellConn, rawNbCellNodes);
1157 // indices of nodes of a son
1158 static std::vector<int> allNodeIndices; // == 0,1,2,...,nbOfCellNodes-1
1159 while ( allNodeIndices.size() < nbOfCellNodes )
1160 allNodeIndices.push_back( allNodeIndices.size() );
1161 std::vector<int> classicFaceNodes(4);
1162 int* faceNodes = cellModelCell.isDynamic() ? &allNodeIndices[0] : &classicFaceNodes[0];
1164 // nodes of tetrahedron
1166 const double* nodes[4];
1167 nodes[3] = getCoordsOfSubNode2( nbOfCellNodes,conn[3]); // barycenter
1169 for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
1171 // get indices of son's nodes: it's just next portion of allNodeIndices for polyhedron
1172 // and some of allNodeIndices accodring to cell model for a classsic cell
1173 unsigned nbFaceNodes = cellModelCell.getNumberOfNodesConstituentTheSon2(ii, rawCellConn, rawNbCellNodes);
1174 if ( normCellType != NORM_POLYHED )
1175 cellModelCell.fillSonCellNodalConnectivity(ii,&allNodeIndices[0],faceNodes);
1177 int nbTetra = nbFaceNodes - 2; // split polygon into nbTetra triangles
1179 // create tetrahedra
1180 for(int i = 0; i < nbTetra; ++i)
1182 nodes[0] = getCoordsOfSubNode2( faceNodes[0], conn[0]);
1183 nodes[1] = getCoordsOfSubNode2( faceNodes[1+i],conn[1]);
1184 nodes[2] = getCoordsOfSubNode2( faceNodes[2+i],conn[2]);
1185 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1189 if ( normCellType == NORM_POLYHED )
1190 faceNodes += nbFaceNodes; // go to the next face
1195 * Precalculates all the nodes.
1196 * Retrieves the mesh nodes and allocates the necessary sub-mesh
1197 * nodes according to the splitting policy used.
1198 * This method is meant to be called once by the constructor.
1200 * @param targetMesh the target mesh
1201 * @param targetCell the global number of the cell that the object represents, in targetMesh mode.
1202 * @param policy the splitting policy of the object
1205 template<class MyMeshTypeT, class MyMeshTypeS>
1206 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateSubNodes(const MyMeshTypeT& targetMesh, typename MyMeshTypeT::MyConnType targetCell)
1208 // retrieve real mesh nodes
1210 typename MyMeshTypeT::MyConnType nbOfNodesT = _node_ids.size();// Issue 0020634. _node_ids.resize(8);
1211 for(int node = 0; node < nbOfNodesT ; ++node)
1213 // calculate only normal nodes
1214 _nodes.push_back(getCoordsOfNode2(node, targetCell, targetMesh,_node_ids[node]));
1217 switch ( nbOfNodesT )
1221 // create sub-mesh nodes if needed
1222 switch(_splitting_pol)
1226 for(int i = 0; i < 7; ++i)
1228 double* barycenter = new double[3];
1229 calcBarycenter(4, barycenter, &GENERAL_24_SUB_NODES[4*i]);
1230 _nodes.push_back(barycenter);
1237 // Each sub-node is the barycenter of two other nodes.
1238 // For the edges, these lie on the original mesh.
1239 // For the faces, these are the edge sub-nodes.
1240 // For the cell these are two face sub-nodes.
1241 static const int GENERAL_48_SUB_NODES[38] =
1243 0,1, // sub-node 9 (edge)
1244 0,4, // sub-node 10 (edge)
1245 1,5, // sub-node 11 (edge)
1246 4,5, // sub-node 12 (edge)
1247 0,3, // sub-node 13 (edge)
1248 1,2, // sub-node 14 (edge)
1249 4,7, // sub-node 15 (edge)
1250 5,6, // sub-node 16 (edge)
1251 2,3, // sub-node 17 (edge)
1252 3,7, // sub-node 18 (edge)
1253 2,6, // sub-node 19 (edge)
1254 6,7, // sub-node 20 (edge)
1255 8,11, // sub-node 21 (face)
1256 12,13, // sub-node 22 (face)
1257 9,17, // sub-node 23 (face)
1258 10,18, // sub-node 24 (face)
1259 14,15, // sub-node 25 (face)
1260 16,19, // sub-node 26 (face)
1261 20,25 // sub-node 27 (cell)
1264 for(int i = 0; i < 19; ++i)
1266 double* barycenter = new double[3];
1267 calcBarycenter(2, barycenter, &GENERAL_48_SUB_NODES[2*i]);
1268 _nodes.push_back(barycenter);
1277 case 5: // NORM_PYRA5
1280 default: // convex 3d cell
1282 // add barycenter of a cell
1283 std::vector<int> allIndices(nbOfNodesT);
1284 for ( int i = 0; i < nbOfNodesT; ++i ) allIndices[i] = i;
1285 double* barycenter = new double[3];
1286 calcBarycenter(nbOfNodesT, barycenter, &allIndices[0]);
1287 _nodes.push_back(barycenter);