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
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);
91 * Deletes _t and the coordinates (double[3] vectors) in _nodes
94 template<class MyMeshType>
95 SplitterTetra<MyMeshType>::~SplitterTetra()
98 for(HashMap< int, double* >::iterator iter = _nodes.begin(); iter != _nodes.end() ; ++iter)
99 delete[] iter->second;
103 * \Forget already calculated triangles, which is crucial for calculation of barycenter of intersection
105 template<class MyMeshType>
106 void SplitterTetra<MyMeshType>::clearVolumesCache()
112 * This method destroys the 4 pointers pointed by tetraCorners[0],tetraCorners[1],tetraCorners[2] and tetraCorners[3]
113 * @param i is in 0..23 included.
114 * @param output is expected to be sized of 12 in order to.
116 template<class MyMeshType>
117 void SplitterTetra<MyMeshType>::splitMySelfForDual(double* output, int i, typename MyMeshType::MyConnType& nodeId)
121 nodeId=_conn[offset];
122 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;
124 int case1=caseToTreat/2;
125 int case2=caseToTreat%2;
126 const int tab[3][2]={{1,2},{3,2},{1,3}};
127 const int *curTab=tab[case1];
128 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.;
129 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.;
130 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.;
131 std::copy(pt1,pt1+3,output+case2*3);
132 std::copy(pt0,pt0+3,output+(abs(case2-1))*3);
133 std::copy(pt2,pt2+3,output+2*3);
134 std::copy(tmp[0],tmp[0]+3,output+3*3);
138 * Calculates the volume of intersection of an element in the source mesh and the target element.
139 * It first calculates the transformation that takes the target tetrahedron into the unit tetrahedron. After that, the
140 * faces of the source element are triangulated and the calculated transformation is applied
141 * to each triangle. The algorithm of Grandy, implemented in INTERP_KERNEL::TransformedTriangle is used
142 * to calculate the contribution to the volume from each triangle. The volume returned is the sum of these contributions
143 * divided by the determinant of the transformation.
145 * The class will cache the intermediary calculations of transformed nodes of source cells and volumes associated
146 * with triangulated faces to avoid having to recalculate these.
148 * @param element global number of the source element in C mode.
150 template<class MyMeshType>
151 double SplitterTetra<MyMeshType>::intersectSourceCell(typename MyMeshType::MyConnType element,
154 typedef typename MyMeshType::MyConnType ConnType;
155 const NumberingPolicy numPol=MyMeshType::My_numPol;
156 //{ could be done on outside?
157 // check if we have planar tetra element
158 if(_t->determinant() == 0.0)
161 LOG(2, "Planar tetra -- volume 0");
166 NormalizedCellType normCellType=_src_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(element));
167 const CellModel& cellModelCell=CellModel::GetCellModel(normCellType);
168 unsigned nbOfNodes4Type=cellModelCell.isDynamic() ? _src_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(element)) : cellModelCell.getNumberOfNodes();
169 // halfspace filtering
170 bool isOutside[8] = {true, true, true, true, true, true, true, true};
171 bool isTargetOutside = false;
173 // calculate the coordinates of the nodes
174 int *cellNodes=new int[nbOfNodes4Type];
175 for(int i = 0;i<(int)nbOfNodes4Type;++i)
177 // we could store mapping local -> global numbers too, but not sure it is worth it
178 const int globalNodeNum = getGlobalNumberOfNode(i, OTT<ConnType,numPol>::indFC(element), _src_mesh);
179 cellNodes[i]=globalNodeNum;
180 if(_nodes.find(globalNodeNum) == _nodes.end())
182 //for(HashMap< int , double* >::iterator iter3=_nodes.begin();iter3!=_nodes.end();iter3++)
183 // std::cout << (*iter3).first << " ";
184 //std::cout << std::endl << "*** " << globalNodeNum << std::endl;
185 calculateNode(globalNodeNum);
188 checkIsOutside(_nodes[globalNodeNum], isOutside);
191 // halfspace filtering check
192 // NB : might not be beneficial for caching of triangles
193 for(int i = 0; i < 8; ++i)
197 isTargetOutside = true;
201 double totalVolume = 0.0;
205 /// calculator of intersection barycentre
206 UnitTetraIntersectionBary baryCalculator( _t->determinant() < 0.);
208 // get nb of sons of a cell
209 const ConnType* rawCellConn = _src_mesh.getConnectivityPtr() + OTT<ConnType,numPol>::conn2C( _src_mesh.getConnectivityIndexPtr()[ element ]);
210 const int rawNbCellNodes = _src_mesh.getConnectivityIndexPtr()[ element+1 ] - _src_mesh.getConnectivityIndexPtr()[ element ];
211 unsigned nbOfSons = cellModelCell.getNumberOfSons2(rawCellConn, rawNbCellNodes);
213 for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
215 // get sons connectivity
216 NormalizedCellType faceType;
217 int *faceNodes, nbFaceNodes=-1;
218 if ( cellModelCell.isDynamic() )
220 faceNodes=new int[nbOfNodes4Type];
221 nbFaceNodes = cellModelCell.fillSonCellNodalConnectivity2(ii,rawCellConn,rawNbCellNodes,faceNodes,faceType);
222 for ( int i = 0; i < nbFaceNodes; ++i )
223 faceNodes[i] = OTT<ConnType,numPol>::coo2C(faceNodes[i]);
227 faceType = cellModelCell.getSonType(ii);
228 const CellModel& faceModel=CellModel::GetCellModel(faceType);
229 assert(faceModel.getDimension() == 2);
230 faceNodes=new int[faceModel.getNumberOfNodes()];
231 cellModelCell.fillSonCellNodalConnectivity(ii,cellNodes,faceNodes);
233 // intersect a son with the unit tetra
238 // create the face key
239 TriangleFaceKey key = TriangleFaceKey(faceNodes[0], faceNodes[1], faceNodes[2]);
241 // calculate the triangle if needed
242 if(_volumes.find(key) == _volumes.end())
244 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1]], _nodes[faceNodes[2]]);
245 calculateVolume(tri, key);
246 totalVolume += _volumes[key];
248 baryCalculator.addSide( tri );
250 // count negative as face has reversed orientation
251 totalVolume -= _volumes[key];
258 // simple triangulation of faces along a diagonal :
269 //? not sure if this always works
271 // calculate the triangles if needed
273 // local nodes 1, 2, 3
274 TriangleFaceKey key1 = TriangleFaceKey(faceNodes[0], faceNodes[1], faceNodes[2]);
275 if(_volumes.find(key1) == _volumes.end())
277 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1]], _nodes[faceNodes[2]]);
278 calculateVolume(tri, key1);
279 totalVolume += _volumes[key1];
281 // count negative as face has reversed orientation
282 totalVolume -= _volumes[key1];
285 // local nodes 1, 3, 4
286 TriangleFaceKey key2 = TriangleFaceKey(faceNodes[0], faceNodes[2], faceNodes[3]);
287 if(_volumes.find(key2) == _volumes.end())
289 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[2]], _nodes[faceNodes[3]]);
290 calculateVolume(tri, key2);
291 totalVolume += _volumes[key2];
295 // count negative as face has reversed orientation
296 totalVolume -= _volumes[key2];
303 int nbTria = nbFaceNodes - 2; // split polygon into nbTria triangles
304 for ( int iTri = 0; iTri < nbTria; ++iTri )
306 TriangleFaceKey key = TriangleFaceKey(faceNodes[0], faceNodes[1+iTri], faceNodes[2+iTri]);
307 if(_volumes.find(key) == _volumes.end())
309 TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1+iTri]], _nodes[faceNodes[2+iTri]]);
310 calculateVolume(tri, key);
311 totalVolume += _volumes[key];
315 totalVolume -= _volumes[key];
322 std::cout << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment." << std::endl;
329 baryCalculator.getBary( baryCentre );
330 _t->reverseApply( baryCentre, baryCentre );
334 // reset if it is very small to keep the matrix sparse
335 // is this a good idea?
336 if(epsilonEqual(totalVolume, 0.0, SPARSE_TRUNCATION_LIMIT))
341 LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
343 // NB : fault in article, Grandy, [8] : it is the determinant of the inverse transformation
344 // that should be used (which is equivalent to dividing by the determinant)
345 return std::fabs(1.0 / _t->determinant() * totalVolume) ;
349 * Calculates the intersection surface of two coplanar triangles.
351 * @param palneNormal normal of the plane for the first triangle
352 * @param planeConstant constant of the equation of the plane for the first triangle
353 * @param p1 coordinates of the first node of the first triangle
354 * @param p2 coordinates of the second node of the first triangle
355 * @param p3 coordinates of the third node of the first triangle
356 * @param p4 coordinates of the first node of the second triangle
357 * @param p5 coordinates of the second node of the second triangle
358 * @param p6 coordinates of the third node of the second triangle
359 * @param dimCaracteristic characteristic size of the meshes containing the triangles
360 * @param precision precision for double float data used for comparison
362 template<class MyMeshType>
363 double SplitterTetra<MyMeshType>::CalculateIntersectionSurfaceOfCoplanarTriangles(const double *const planeNormal,
364 const double planeConstant,
365 const double *const p1, const double *const p2, const double *const p3,
366 const double *const p4, const double *const p5, const double *const p6,
367 const double dimCaracteristic, const double precision)
369 typedef typename MyMeshType::MyConnType ConnType;
370 typedef double Vect2[2];
371 typedef double Vect3[3];
372 typedef double Triangle2[3][2];
374 const double *const tri0[3] = {p1, p2, p3};
375 const double *const tri1[3] = {p4, p5, p6};
377 // Plane of the first triangle defined by the normal of the triangle and the constant
378 // Project triangles onto coordinate plane most aligned with plane normal
380 double fmax = std::abs(planeNormal[0]);
381 double absMax = std::abs(planeNormal[1]);
387 absMax = std::abs(planeNormal[2]);
393 Triangle2 projTri0, projTri1;
398 // Project onto yz-plane.
399 for (i = 0; i < 3; ++i)
401 projTri0[i][0] = tri0[i][1];
402 projTri0[i][1] = tri0[i][2];
403 projTri1[i][0] = tri1[i][1];
404 projTri1[i][1] = tri1[i][2];
407 else if (maxNormal == 1)
409 // Project onto xz-plane.
410 for (i = 0; i < 3; ++i)
412 projTri0[i][0] = tri0[i][0];
413 projTri0[i][1] = tri0[i][2];
414 projTri1[i][0] = tri1[i][0];
415 projTri1[i][1] = tri1[i][2];
420 // Project onto xy-plane.
421 for (i = 0; i < 3; ++i)
423 projTri0[i][0] = tri0[i][0];
424 projTri0[i][1] = tri0[i][1];
425 projTri1[i][0] = tri1[i][0];
426 projTri1[i][1] = tri1[i][1];
430 // 2D triangle intersection routines require counterclockwise ordering.
434 for (int ii = 0; ii < 2; ++ii)
436 edge0[ii] = projTri0[1][ii] - projTri0[0][ii];
437 edge1[ii] = projTri0[2][ii] - projTri0[0][ii];
439 if ((edge0[0] * edge1[1] - edge0[1] * edge1[0]) < (double) 0.)
441 // Triangle is clockwise, reorder it.
442 for (int ii = 0; ii < 2; ++ii)
444 save[ii] = projTri0[1][ii];
445 projTri0[1][ii] = projTri0[2][ii];
446 projTri0[2][ii] = save[ii];
450 for (int ii = 0; ii < 2; ++ii)
452 edge0[ii] = projTri1[1][ii] - projTri1[0][ii];
453 edge1[ii] = projTri1[2][ii] - projTri1[0][ii];
455 if ((edge0[0] * edge1[1] - edge0[1] * edge1[0]) < (double) 0.)
457 // Triangle is clockwise, reorder it.
458 for (int ii = 0; ii < 2; ++ii)
460 save[ii] = projTri1[1][ii];
461 projTri1[1][ii] = projTri1[2][ii];
462 projTri1[2][ii] = save[ii];
466 std::vector<double> inter2;
467 intersec_de_triangle(projTri0[0], projTri0[1], projTri0[2],
468 projTri1[0], projTri1[1], projTri1[2],
470 dimCaracteristic, precision);
471 ConnType nb_inter=((ConnType)inter2.size())/2;
473 if(nb_inter >3) inter2=reconstruct_polygon(inter2);
476 std::vector<double> inter3;
477 inter3.resize(3 * nb_inter);
478 // Map 2D intersections back to the 3D triangle space.
481 double invNX = ((double) 1.) / planeNormal[0];
482 for (i = 0; i < nb_inter; i++)
484 inter3[3 * i + 1] = inter2[2 * i];
485 inter3[3 * i + 2] = inter2[2 * i + 1];
486 inter3[3 * i] = invNX * (planeConstant - planeNormal[1] * inter3[3 * i + 1] - planeNormal[2] * inter3[3 * i + 2]);
489 else if (maxNormal == 1)
491 double invNY = ((double) 1.) / planeNormal[1];
492 for (i = 0; i < nb_inter; i++)
494 inter3[3 * i] = inter2[2 * i];
495 inter3[3 * i + 2] = inter2[2 * i + 1];
496 inter3[3 * i + 1] = invNY * (planeConstant - planeNormal[0] * inter3[3 * i] - planeNormal[2] * inter3[3 * i + 2]);
501 double invNZ = ((double) 1.) / planeNormal[2];
502 for (i = 0; i < nb_inter; i++)
504 inter3[3 * i] = inter2[2 * i];
505 inter3[3 * i + 1] = inter2[2 * i + 1];
506 inter3[3 * i + 2] = invNZ * (planeConstant - planeNormal[0] * inter3[3 * i] - planeNormal[1] * inter3[3 * i + 1]);
509 surface = polygon_area<3>(inter3);
515 * Determine if a face is coplanar with a triangle.
516 * The first face is characterized by the equation of her plane
518 * @param palneNormal normal of the plane for the first triangle
519 * @param planeConstant constant of the equation of the plane for the first triangle
520 * @param coordsFace coordinates of the triangle face
521 * @param precision precision for double float data used for comparison
523 template<class MyMeshType>
524 bool SplitterTetra<MyMeshType>::IsFacesCoplanar(const double *const planeNormal,
525 const double planeConstant,
526 const double *const *const coordsFace,
527 const double precision)
529 // Compute the signed distances of triangle vertices to the plane. Use an epsilon-thick plane test.
530 // For faces not left
532 for (int i = 0; i < 3; ++i)
534 const double distance = dot(planeNormal, coordsFace[i]) - planeConstant;
535 if (epsilonEqual(distance, precision))
544 * Calculates the surface of intersection of a polygon face in the source mesh and a cell of the target mesh.
545 * It first calculates the transformation that takes the target tetrahedron into the unit tetrahedron. After that, the
546 * faces of the source element are triangulated and the calculated transformation is applied
548 * The algorithm is based on the algorithm of Grandy used in intersectSourceCell to compute
549 * the volume of intersection of two cell elements.
550 * The case with a source face colinear to one of the face of tetrahedrons is taking into account:
551 * the contribution of the face must not be counted two times.
553 * The class will cache the intermediary calculations of transformed nodes of source faces and surfaces associated
554 * with triangulated faces to avoid having to recalculate these.
556 * @param polyType type of the polygon source face
557 * @param polyNodesNbr number of the nodes of the polygon source face
558 * @param polyNodes numbers of the nodes of the polygon source face
559 * @param polyCoords coordinates of the nodes of the polygon source face
560 * @param dimCaracteristic characteristic size of the meshes containing the triangles
561 * @param precision precision for double float data used for comparison
562 * @param listOfTetraFacesTreated list of tetra faces treated
563 * @param listOfTetraFacesColinear list of tetra faces colinear with the polygon source faces
565 template<class MyMeshType>
566 double SplitterTetra<MyMeshType>::intersectSourceFace(const NormalizedCellType polyType,
567 const int polyNodesNbr,
568 const int *const polyNodes,
569 const double *const *const polyCoords,
570 const double dimCaracteristic,
571 const double precision,
572 std::multiset<TriangleFaceKey>& listOfTetraFacesTreated,
573 std::set<TriangleFaceKey>& listOfTetraFacesColinear)
575 typedef typename MyMeshType::MyConnType ConnType;
577 double totalSurface = 0.0;
579 // check if we have planar tetra element
580 if(_t->determinant() == 0.0)
583 LOG(2, "Planar tetra -- volume 0");
587 // halfspace filtering
588 bool isOutside[8] = {true, true, true, true, true, true, true, true};
589 bool isStrictlyOutside[8] = {true, true, true, true, true, true, true, true};
590 bool isTargetStrictlyOutside = false;
591 bool isTargetOutside = false;
593 // calculate the coordinates of the nodes
594 for(int i = 0;i<(int)polyNodesNbr;++i)
596 const int globalNodeNum = polyNodes[i];
597 if(_nodes.find(globalNodeNum) == _nodes.end())
599 calculateNode2(globalNodeNum, polyCoords[i]);
602 checkIsStrictlyOutside(_nodes[globalNodeNum], isStrictlyOutside, precision);
603 checkIsOutside(_nodes[globalNodeNum], isOutside, precision);
606 // halfspace filtering check
607 // NB : might not be beneficial for caching of triangles
608 for(int i = 0; i < 8; ++i)
610 if(isStrictlyOutside[i])
612 isTargetStrictlyOutside = true;
615 else if (isOutside[i])
617 isTargetOutside = true;
621 if (!isTargetStrictlyOutside)
626 // Faces are parallel
627 const int tetraFacesNodesConn[4][3] = {
632 double planeNormal[3];
633 for (int iTetraFace = 0; iTetraFace < 4; ++iTetraFace)
635 const int * const tetraFaceNodesConn = tetraFacesNodesConn[iTetraFace];
636 TriangleFaceKey key = TriangleFaceKey(_conn[tetraFaceNodesConn[0]],
637 _conn[tetraFaceNodesConn[1]],
638 _conn[tetraFaceNodesConn[2]]);
639 if (listOfTetraFacesTreated.find(key) == listOfTetraFacesTreated.end())
641 const double * const coordsTetraTriNode1 = _coords + tetraFaceNodesConn[0] * MyMeshType::MY_SPACEDIM;
642 const double * const coordsTetraTriNode2 = _coords + tetraFaceNodesConn[1] * MyMeshType::MY_SPACEDIM;
643 const double * const coordsTetraTriNode3 = _coords + tetraFaceNodesConn[2] * MyMeshType::MY_SPACEDIM;
644 calculateNormalForTria(coordsTetraTriNode1, coordsTetraTriNode2, coordsTetraTriNode3, planeNormal);
645 const double normOfTetraTriNormal = norm(planeNormal);
646 if (epsilonEqual(normOfTetraTriNormal, 0.))
648 for (int i = 0; i < 3; ++i)
655 const double invNormOfTetraTriNormal = 1. / normOfTetraTriNormal;
656 for (int i = 0; i < 3; ++i)
658 planeNormal[i] *= invNormOfTetraTriNormal;
661 double planeConstant = dot(planeNormal, coordsTetraTriNode1);
662 if (IsFacesCoplanar(planeNormal, planeConstant, polyCoords, precision))
664 int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
665 for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
667 double volume = CalculateIntersectionSurfaceOfCoplanarTriangles(planeNormal,
670 polyCoords[1 + iTri],
671 polyCoords[2 + iTri],
677 if (!epsilonEqual(volume, 0.))
679 totalSurface += volume;
680 listOfTetraFacesColinear.insert(key);
685 listOfTetraFacesTreated.insert(key);
690 // intersect a son with the unit tetra
695 // create the face key
696 TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
698 // calculate the triangle if needed
699 if (_volumes.find(key) == _volumes.end())
701 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
702 calculateSurface(tri, key);
703 totalSurface += _volumes[key];
707 // count negative as face has reversed orientation
708 totalSurface -= _volumes[key];
715 // simple triangulation of faces along a diagonal :
726 //? not sure if this always works
728 // calculate the triangles if needed
730 // local nodes 1, 2, 3
731 TriangleFaceKey key1 = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
732 if (_volumes.find(key1) == _volumes.end())
734 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
735 calculateSurface(tri, key1);
736 totalSurface += _volumes[key1];
740 // count negative as face has reversed orientation
741 totalSurface -= _volumes[key1];
744 // local nodes 1, 3, 4
745 TriangleFaceKey key2 = TriangleFaceKey(polyNodes[0], polyNodes[2], polyNodes[3]);
746 if (_volumes.find(key2) == _volumes.end())
748 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[2]], _nodes[polyNodes[3]]);
749 calculateSurface(tri, key2);
750 totalSurface += _volumes[key2];
754 // count negative as face has reversed orientation
755 totalSurface -= _volumes[key2];
762 int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
763 for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
765 TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1 + iTri], polyNodes[2 + iTri]);
766 if (_volumes.find(key) == _volumes.end())
768 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1 + iTri]],
769 _nodes[polyNodes[2 + iTri]]);
770 calculateSurface(tri, key);
771 totalSurface += _volumes[key];
775 totalSurface -= _volumes[key];
783 << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment."
791 // reset if it is very small to keep the matrix sparse
792 // is this a good idea?
793 if(epsilonEqual(totalSurface, 0.0, SPARSE_TRUNCATION_LIMIT))
798 LOG(2, "Volume = " << totalSurface << ", det= " << _t->determinant());
804 * Calculates the volume of intersection of this tetrahedron with another one.
806 template<class MyMeshType>
807 double SplitterTetra<MyMeshType>::intersectTetra(const double** tetraCorners)
809 //{ could be done on outside?
810 // check if we have planar tetra element
811 if(_t->determinant() == 0.0)
814 LOG(2, "Planar tetra -- volume 0");
818 const unsigned nbOfNodes4Type=4;
819 // halfspace filtering
820 bool isOutside[8] = {true, true, true, true, true, true, true, true};
821 bool isTargetOutside = false;
823 // calculate the transformed coordinates of the nodes
824 double nodes[nbOfNodes4Type][3];
825 for(int i = 0;i<(int)nbOfNodes4Type;++i)
827 _t->apply(nodes[i], tetraCorners[i]);
828 checkIsOutside(nodes[i], isOutside);
831 // halfspace filtering check
832 // NB : might not be beneficial for caching of triangles
833 for(int i = 0; i < 8; ++i)
837 isTargetOutside = true;
841 double totalVolume = 0.0;
845 const CellModel& cellModelCell=CellModel::GetCellModel(NORM_TETRA4);
846 int cellNodes[4] = { 0, 1, 2, 3 }, faceNodes[3];
848 for(unsigned ii = 0 ; ii < 4 ; ++ii)
850 cellModelCell.fillSonCellNodalConnectivity(ii,cellNodes,faceNodes);
852 TransformedTriangle tri(nodes[faceNodes[0]], nodes[faceNodes[1]], nodes[faceNodes[2]]);
853 double vol = tri.calculateIntersectionVolume();
857 // reset if it is very small to keep the matrix sparse
858 // is this a good idea?
859 if(epsilonEqual(totalVolume, 0.0, SPARSE_TRUNCATION_LIMIT))
864 LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
866 // NB : fault in article, Grandy, [8] : it is the determinant of the inverse transformation
867 // that should be used (which is equivalent to dividing by the determinant)
868 return std::fabs(1.0 / _t->determinant() * totalVolume) ;
871 ////////////////////////////////////////////////////////
873 template<class MyMeshTypeT, class MyMeshTypeS>
874 SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::SplitterTetra2(const MyMeshTypeT& targetMesh, const MyMeshTypeS& srcMesh, SplittingPolicy policy)
875 :_target_mesh(targetMesh),_src_mesh(srcMesh),_splitting_pol(policy)
879 template<class MyMeshTypeT, class MyMeshTypeS>
880 SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::~SplitterTetra2()
885 template<class MyMeshTypeT, class MyMeshTypeS>
886 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::releaseArrays()
888 // free potential sub-mesh nodes that have been allocated
889 typename MyMeshTypeT::MyConnType nbOfNodesT = _node_ids.size();// Issue 0020634.
890 if((int)_nodes.size()>=/*8*/nbOfNodesT)
892 std::vector<const double*>::iterator iter = _nodes.begin() + /*8*/nbOfNodesT;
893 while(iter != _nodes.end())
903 * @param targetCell in C mode.
904 * @param tetra is the output result tetra containers.
906 template<class MyMeshTypeT, class MyMeshTypeS>
907 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitTargetCell(typename MyMeshTypeT::MyConnType targetCell,
908 typename MyMeshTypeT::MyConnType nbOfNodesT,
909 typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
911 typedef typename MyMeshTypeT::MyConnType ConnType;
912 const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
913 const int numTetra = static_cast<int>(_splitting_pol);
919 const double *nodes[4];
921 for(int node = 0; node < 4 ; ++node)
923 nodes[node]=getCoordsOfNode2(node, OTT<ConnType,numPol>::indFC(targetCell),_target_mesh,conn[node]);
925 std::copy(conn,conn+4,_node_ids.begin());
926 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
930 // Issue 0020634. To pass nbOfNodesT to calculateSubNodes (don't want to add an arg)
931 _node_ids.resize(nbOfNodesT);
933 // pre-calculate nodes
934 calculateSubNodes(_target_mesh, OTT<ConnType,numPol>::indFC(targetCell));
936 tetra.reserve(numTetra);
937 _nodes.reserve(30); // we never have more than this
939 switch ( nbOfNodesT )
943 switch(_splitting_pol)
947 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
948 fiveSplit(subZone,tetra);
954 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
955 sixSplit(subZone,tetra);
961 calculateGeneral24Tetra(tetra);
967 calculateGeneral48Tetra(tetra);
982 splitConvex(targetCell, tetra);
988 * Splits the hexahedron into five tetrahedra.
989 * This method adds five SplitterTetra objects to the vector tetra.
991 * @param subZone the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the
992 * splitting to be reused on the subzones of the GENERAL_* types of splitting
994 template<class MyMeshTypeT, class MyMeshTypeS>
995 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::fiveSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
998 for(int i = 0; i < 5; ++i)
1000 const double* nodes[4];
1002 for(int j = 0; j < 4; ++j)
1004 conn[j] = subZone[ SPLIT_NODES_5[4*i+j] ];
1005 nodes[j] = getCoordsOfSubNode(conn[j]);
1007 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1013 * Splits the hexahedron into six tetrahedra.
1014 * This method adds six SplitterTetra objects to the vector tetra.
1016 * @param subZone the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the
1017 * splitting to be reused on the subzones of the GENERAL_* types of splitting
1019 template<class MyMeshTypeT, class MyMeshTypeS>
1020 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::sixSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1022 for(int i = 0; i < 6; ++i)
1024 const double* nodes[4];
1026 for(int j = 0; j < 4; ++j)
1028 conn[j] = subZone[SPLIT_NODES_6[4*i+j]];
1029 nodes[j] = getCoordsOfSubNode(conn[j]);
1031 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1037 * Splits the hexahedron into 24 tetrahedra.
1038 * The splitting is done by combining the barycenter of the tetrahedron, the barycenter of each face
1039 * and the nodes of each edge of the face. This creates 6 faces * 4 edges / face = 24 tetrahedra.
1040 * The submesh nodes introduced are the barycenters of the faces and the barycenter of the cell.
1043 template<class MyMeshTypeT, class MyMeshTypeS>
1044 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral24Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1046 // The two nodes of the original mesh cell used in each tetrahedron.
1047 // The tetrahedra all have nodes (cellCenter, faceCenter, edgeNode1, edgeNode2)
1048 // For the correspondance of the nodes, see the GENERAL_48_SUB_NODES table in calculateSubNodes
1050 // nodes to use for tetrahedron
1051 const double* nodes[4];
1053 // get the cell center
1055 nodes[0] = getCoordsOfSubNode(conn[0]);
1057 for(int faceCenterNode = 8; faceCenterNode < 14; ++faceCenterNode)
1059 // get the face center
1060 conn[1] = faceCenterNode;
1061 nodes[1] = getCoordsOfSubNode(conn[1]);
1062 for(int j = 0; j < 4; ++j)
1064 const int row = 4*(faceCenterNode - 8) + j;
1065 conn[2] = TETRA_EDGES_GENERAL_24[2*row];
1066 conn[3] = TETRA_EDGES_GENERAL_24[2*row + 1];
1067 nodes[2] = getCoordsOfSubNode(conn[2]);
1068 nodes[3] = getCoordsOfSubNode(conn[3]);
1070 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes, conn);
1078 * Splits the hexahedron into 48 tetrahedra.
1079 * The splitting is done by introducing the midpoints of all the edges
1080 * and the barycenter of the element as submesh nodes. The 8 hexahedral subzones thus defined
1081 * are then split into 6 tetrahedra each, as in Grandy, p. 449. The division of the subzones
1082 * is done by calling sixSplit().
1085 template<class MyMeshTypeT, class MyMeshTypeS>
1086 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral48Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1088 for(int i = 0; i < 8; ++i)
1090 sixSplit(GENERAL_48_SUBZONES+8*i,tetra);
1095 * Splits the NORM_PYRA5 into 2 tetrahedra.
1097 template<class MyMeshTypeT, class MyMeshTypeS>
1098 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitPyram5(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1100 static const int SPLIT_PYPA5[2][4] =
1110 // create tetrahedra
1111 const double* nodes[4];
1113 for(int i = 0; i < 2; ++i)
1115 for(int j = 0; j < 4; ++j)
1116 nodes[j] = getCoordsOfSubNode2(SPLIT_PYPA5[i][j],conn[j]);
1117 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1123 * Splits a convex cell into tetrahedra.
1125 template<class MyMeshTypeT, class MyMeshTypeS>
1126 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitConvex(typename MyMeshTypeT::MyConnType targetCell,
1127 typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1129 // Each face of a cell is split into triangles and
1130 // each of triangles and a cell barycenter form a tetrahedron.
1132 typedef typename MyMeshTypeT::MyConnType ConnType;
1133 const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
1135 // get type of cell and nb of cell nodes
1136 NormalizedCellType normCellType=_target_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(targetCell));
1137 const CellModel& cellModelCell=CellModel::GetCellModel(normCellType);
1138 unsigned nbOfCellNodes=cellModelCell.isDynamic() ? _target_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(targetCell)) : cellModelCell.getNumberOfNodes();
1140 // get nb of cell sons (faces)
1141 const ConnType* rawCellConn = _target_mesh.getConnectivityPtr() + OTT<ConnType,numPol>::conn2C( _target_mesh.getConnectivityIndexPtr()[ targetCell ]);
1142 const int rawNbCellNodes = _target_mesh.getConnectivityIndexPtr()[ targetCell+1 ] - _target_mesh.getConnectivityIndexPtr()[ targetCell ];
1143 unsigned nbOfSons = cellModelCell.getNumberOfSons2(rawCellConn, rawNbCellNodes);
1145 // indices of nodes of a son
1146 static std::vector<int> allNodeIndices; // == 0,1,2,...,nbOfCellNodes-1
1147 while ( allNodeIndices.size() < nbOfCellNodes )
1148 allNodeIndices.push_back( allNodeIndices.size() );
1149 std::vector<int> classicFaceNodes(4);
1150 int* faceNodes = cellModelCell.isDynamic() ? &allNodeIndices[0] : &classicFaceNodes[0];
1152 // nodes of tetrahedron
1154 const double* nodes[4];
1155 nodes[3] = getCoordsOfSubNode2( nbOfCellNodes,conn[3]); // barycenter
1157 for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
1159 // get indices of son's nodes: it's just next portion of allNodeIndices for polyhedron
1160 // and some of allNodeIndices accodring to cell model for a classsic cell
1161 unsigned nbFaceNodes = cellModelCell.getNumberOfNodesConstituentTheSon2(ii, rawCellConn, rawNbCellNodes);
1162 if ( normCellType != NORM_POLYHED )
1163 cellModelCell.fillSonCellNodalConnectivity(ii,&allNodeIndices[0],faceNodes);
1165 int nbTetra = nbFaceNodes - 2; // split polygon into nbTetra triangles
1167 // create tetrahedra
1168 for(int i = 0; i < nbTetra; ++i)
1170 nodes[0] = getCoordsOfSubNode2( faceNodes[0], conn[0]);
1171 nodes[1] = getCoordsOfSubNode2( faceNodes[1+i],conn[1]);
1172 nodes[2] = getCoordsOfSubNode2( faceNodes[2+i],conn[2]);
1173 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1177 if ( normCellType == NORM_POLYHED )
1178 faceNodes += nbFaceNodes; // go to the next face
1183 * Precalculates all the nodes.
1184 * Retrieves the mesh nodes and allocates the necessary sub-mesh
1185 * nodes according to the splitting policy used.
1186 * This method is meant to be called once by the constructor.
1188 * @param targetMesh the target mesh
1189 * @param targetCell the global number of the cell that the object represents, in targetMesh mode.
1190 * @param policy the splitting policy of the object
1193 template<class MyMeshTypeT, class MyMeshTypeS>
1194 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateSubNodes(const MyMeshTypeT& targetMesh, typename MyMeshTypeT::MyConnType targetCell)
1196 // retrieve real mesh nodes
1198 typename MyMeshTypeT::MyConnType nbOfNodesT = _node_ids.size();// Issue 0020634. _node_ids.resize(8);
1199 for(int node = 0; node < nbOfNodesT ; ++node)
1201 // calculate only normal nodes
1202 _nodes.push_back(getCoordsOfNode2(node, targetCell, targetMesh,_node_ids[node]));
1205 switch ( nbOfNodesT )
1209 // create sub-mesh nodes if needed
1210 switch(_splitting_pol)
1214 for(int i = 0; i < 7; ++i)
1216 double* barycenter = new double[3];
1217 calcBarycenter(4, barycenter, &GENERAL_24_SUB_NODES[4*i]);
1218 _nodes.push_back(barycenter);
1225 for(int i = 0; i < 19; ++i)
1227 double* barycenter = new double[3];
1228 calcBarycenter(2, barycenter, &GENERAL_48_SUB_NODES[2*i]);
1229 _nodes.push_back(barycenter);
1238 case 5: // NORM_PYRA5
1241 default: // convex 3d cell
1243 // add barycenter of a cell
1244 std::vector<int> allIndices(nbOfNodesT);
1245 for ( int i = 0; i < nbOfNodesT; ++i ) allIndices[i] = i;
1246 double* barycenter = new double[3];
1247 calcBarycenter(nbOfNodesT, barycenter, &allIndices[0]);
1248 _nodes.push_back(barycenter);