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 polyCoords coordinates of the nodes of the polygon source face
561 * @param dimCaracteristic characteristic size of the meshes containing the triangles
562 * @param precision precision for double float data used for comparison
563 * @param listOfTetraFacesTreated list of tetra faces treated
564 * @param listOfTetraFacesColinear list of tetra faces colinear with the polygon source faces
566 template<class MyMeshType>
567 double SplitterTetra<MyMeshType>::intersectSourceFace(const NormalizedCellType polyType,
568 const int polyNodesNbr,
569 const int *const polyNodes,
570 const double *const *const polyCoords,
571 const double dimCaracteristic,
572 const double precision,
573 std::multiset<TriangleFaceKey>& listOfTetraFacesTreated,
574 std::set<TriangleFaceKey>& listOfTetraFacesColinear)
576 typedef typename MyMeshType::MyConnType ConnType;
578 double totalSurface = 0.0;
580 // check if we have planar tetra element
581 if(_t->determinant() == 0.0)
584 LOG(2, "Planar tetra -- volume 0");
588 // halfspace filtering
589 bool isOutside[8] = {true, true, true, true, true, true, true, true};
590 bool isStrictlyOutside[8] = {true, true, true, true, true, true, true, true};
591 bool isTargetStrictlyOutside = false;
592 bool isTargetOutside = false;
594 // calculate the coordinates of the nodes
595 for(int i = 0;i<(int)polyNodesNbr;++i)
597 const int globalNodeNum = polyNodes[i];
598 if(_nodes.find(globalNodeNum) == _nodes.end())
600 calculateNode2(globalNodeNum, polyCoords[i]);
603 checkIsStrictlyOutside(_nodes[globalNodeNum], isStrictlyOutside, precision);
604 checkIsOutside(_nodes[globalNodeNum], isOutside, precision);
607 // halfspace filtering check
608 // NB : might not be beneficial for caching of triangles
609 for(int i = 0; i < 8; ++i)
611 if(isStrictlyOutside[i])
613 isTargetStrictlyOutside = true;
616 else if (isOutside[i])
618 isTargetOutside = true;
622 if (!isTargetStrictlyOutside)
627 // Faces are parallel
628 const int tetraFacesNodesConn[4][3] = {
633 double planeNormal[3];
634 for (int iTetraFace = 0; iTetraFace < 4; ++iTetraFace)
636 const int * const tetraFaceNodesConn = tetraFacesNodesConn[iTetraFace];
637 TriangleFaceKey key = TriangleFaceKey(_conn[tetraFaceNodesConn[0]],
638 _conn[tetraFaceNodesConn[1]],
639 _conn[tetraFaceNodesConn[2]]);
640 if (listOfTetraFacesTreated.find(key) == listOfTetraFacesTreated.end())
642 const double * const coordsTetraTriNode1 = _coords + tetraFaceNodesConn[0] * MyMeshType::MY_SPACEDIM;
643 const double * const coordsTetraTriNode2 = _coords + tetraFaceNodesConn[1] * MyMeshType::MY_SPACEDIM;
644 const double * const coordsTetraTriNode3 = _coords + tetraFaceNodesConn[2] * MyMeshType::MY_SPACEDIM;
645 calculateNormalForTria(coordsTetraTriNode1, coordsTetraTriNode2, coordsTetraTriNode3, planeNormal);
646 const double normOfTetraTriNormal = norm(planeNormal);
647 if (epsilonEqual(normOfTetraTriNormal, 0.))
649 for (int i = 0; i < 3; ++i)
656 const double invNormOfTetraTriNormal = 1. / normOfTetraTriNormal;
657 for (int i = 0; i < 3; ++i)
659 planeNormal[i] *= invNormOfTetraTriNormal;
662 double planeConstant = dot(planeNormal, coordsTetraTriNode1);
663 if (IsFacesCoplanar(planeNormal, planeConstant, polyCoords, precision))
665 int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
666 for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
668 double volume = CalculateIntersectionSurfaceOfCoplanarTriangles(planeNormal,
671 polyCoords[1 + iTri],
672 polyCoords[2 + iTri],
678 if (!epsilonEqual(volume, 0.))
680 totalSurface += volume;
681 listOfTetraFacesColinear.insert(key);
686 listOfTetraFacesTreated.insert(key);
691 // intersect a son with the unit tetra
696 // create the face key
697 TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
699 // calculate the triangle if needed
700 if (_volumes.find(key) == _volumes.end())
702 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
703 calculateSurface(tri, key);
704 totalSurface += _volumes[key];
708 // count negative as face has reversed orientation
709 totalSurface -= _volumes[key];
716 // simple triangulation of faces along a diagonal :
727 //? not sure if this always works
729 // calculate the triangles if needed
731 // local nodes 1, 2, 3
732 TriangleFaceKey key1 = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
733 if (_volumes.find(key1) == _volumes.end())
735 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
736 calculateSurface(tri, key1);
737 totalSurface += _volumes[key1];
741 // count negative as face has reversed orientation
742 totalSurface -= _volumes[key1];
745 // local nodes 1, 3, 4
746 TriangleFaceKey key2 = TriangleFaceKey(polyNodes[0], polyNodes[2], polyNodes[3]);
747 if (_volumes.find(key2) == _volumes.end())
749 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[2]], _nodes[polyNodes[3]]);
750 calculateSurface(tri, key2);
751 totalSurface += _volumes[key2];
755 // count negative as face has reversed orientation
756 totalSurface -= _volumes[key2];
763 int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
764 for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
766 TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1 + iTri], polyNodes[2 + iTri]);
767 if (_volumes.find(key) == _volumes.end())
769 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1 + iTri]],
770 _nodes[polyNodes[2 + iTri]]);
771 calculateSurface(tri, key);
772 totalSurface += _volumes[key];
776 totalSurface -= _volumes[key];
784 << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment."
792 // reset if it is very small to keep the matrix sparse
793 // is this a good idea?
794 if(epsilonEqual(totalSurface, 0.0, SPARSE_TRUNCATION_LIMIT))
799 LOG(2, "Volume = " << totalSurface << ", det= " << _t->determinant());
805 * Calculates the volume of intersection of this tetrahedron with another one.
807 template<class MyMeshType>
808 double SplitterTetra<MyMeshType>::intersectTetra(const double** tetraCorners)
810 //{ could be done on outside?
811 // check if we have planar tetra element
812 if(_t->determinant() == 0.0)
815 LOG(2, "Planar tetra -- volume 0");
819 const unsigned nbOfNodes4Type=4;
820 // halfspace filtering
821 bool isOutside[8] = {true, true, true, true, true, true, true, true};
822 bool isTargetOutside = false;
824 // calculate the transformed coordinates of the nodes
825 double nodes[nbOfNodes4Type][3];
826 for(int i = 0;i<(int)nbOfNodes4Type;++i)
828 _t->apply(nodes[i], tetraCorners[i]);
829 checkIsOutside(nodes[i], isOutside);
832 // halfspace filtering check
833 // NB : might not be beneficial for caching of triangles
834 for(int i = 0; i < 8; ++i)
838 isTargetOutside = true;
842 double totalVolume = 0.0;
846 const CellModel& cellModelCell=CellModel::GetCellModel(NORM_TETRA4);
847 int cellNodes[4] = { 0, 1, 2, 3 }, faceNodes[3];
849 for(unsigned ii = 0 ; ii < 4 ; ++ii)
851 cellModelCell.fillSonCellNodalConnectivity(ii,cellNodes,faceNodes);
853 TransformedTriangle tri(nodes[faceNodes[0]], nodes[faceNodes[1]], nodes[faceNodes[2]]);
854 double vol = tri.calculateIntersectionVolume();
858 // reset if it is very small to keep the matrix sparse
859 // is this a good idea?
860 if(epsilonEqual(totalVolume, 0.0, SPARSE_TRUNCATION_LIMIT))
865 LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
867 // NB : fault in article, Grandy, [8] : it is the determinant of the inverse transformation
868 // that should be used (which is equivalent to dividing by the determinant)
869 return std::fabs(1.0 / _t->determinant() * totalVolume) ;
872 ////////////////////////////////////////////////////////
874 template<class MyMeshTypeT, class MyMeshTypeS>
875 SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::SplitterTetra2(const MyMeshTypeT& targetMesh, const MyMeshTypeS& srcMesh, SplittingPolicy policy)
876 :_target_mesh(targetMesh),_src_mesh(srcMesh),_splitting_pol(policy)
880 template<class MyMeshTypeT, class MyMeshTypeS>
881 SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::~SplitterTetra2()
886 template<class MyMeshTypeT, class MyMeshTypeS>
887 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::releaseArrays()
889 // free potential sub-mesh nodes that have been allocated
890 typename MyMeshTypeT::MyConnType nbOfNodesT = _node_ids.size();// Issue 0020634.
891 if((int)_nodes.size()>=/*8*/nbOfNodesT)
893 std::vector<const double*>::iterator iter = _nodes.begin() + /*8*/nbOfNodesT;
894 while(iter != _nodes.end())
904 * @param targetCell in C mode.
905 * @param tetra is the output result tetra containers.
907 template<class MyMeshTypeT, class MyMeshTypeS>
908 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitTargetCell(typename MyMeshTypeT::MyConnType targetCell,
909 typename MyMeshTypeT::MyConnType nbOfNodesT,
910 typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
912 typedef typename MyMeshTypeT::MyConnType ConnType;
913 const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
914 const int numTetra = static_cast<int>(_splitting_pol);
920 const double *nodes[4];
922 for(int node = 0; node < 4 ; ++node)
924 nodes[node]=getCoordsOfNode2(node, OTT<ConnType,numPol>::indFC(targetCell),_target_mesh,conn[node]);
926 std::copy(conn,conn+4,_node_ids.begin());
927 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
931 // Issue 0020634. To pass nbOfNodesT to calculateSubNodes (don't want to add an arg)
932 _node_ids.resize(nbOfNodesT);
934 // pre-calculate nodes
935 calculateSubNodes(_target_mesh, OTT<ConnType,numPol>::indFC(targetCell));
937 tetra.reserve(numTetra);
938 _nodes.reserve(30); // we never have more than this
940 switch ( nbOfNodesT )
944 switch(_splitting_pol)
948 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
949 fiveSplit(subZone,tetra);
955 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
956 sixSplit(subZone,tetra);
962 calculateGeneral24Tetra(tetra);
968 calculateGeneral48Tetra(tetra);
983 splitConvex(targetCell, tetra);
989 * Splits the hexahedron into five tetrahedra.
990 * This method adds five SplitterTetra objects to the vector tetra.
992 * @param subZone the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the
993 * splitting to be reused on the subzones of the GENERAL_* types of splitting
995 template<class MyMeshTypeT, class MyMeshTypeS>
996 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::fiveSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
999 for(int i = 0; i < 5; ++i)
1001 const double* nodes[4];
1003 for(int j = 0; j < 4; ++j)
1005 conn[j] = subZone[ SPLIT_NODES_5[4*i+j] ];
1006 nodes[j] = getCoordsOfSubNode(conn[j]);
1008 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1014 * Splits the hexahedron into six tetrahedra.
1015 * This method adds six SplitterTetra objects to the vector tetra.
1017 * @param subZone the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the
1018 * splitting to be reused on the subzones of the GENERAL_* types of splitting
1020 template<class MyMeshTypeT, class MyMeshTypeS>
1021 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::sixSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1023 for(int i = 0; i < 6; ++i)
1025 const double* nodes[4];
1027 for(int j = 0; j < 4; ++j)
1029 conn[j] = subZone[SPLIT_NODES_6[4*i+j]];
1030 nodes[j] = getCoordsOfSubNode(conn[j]);
1032 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1038 * Splits the hexahedron into 24 tetrahedra.
1039 * The splitting is done by combining the barycenter of the tetrahedron, the barycenter of each face
1040 * and the nodes of each edge of the face. This creates 6 faces * 4 edges / face = 24 tetrahedra.
1041 * The submesh nodes introduced are the barycenters of the faces and the barycenter of the cell.
1044 template<class MyMeshTypeT, class MyMeshTypeS>
1045 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral24Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1047 // The two nodes of the original mesh cell used in each tetrahedron.
1048 // The tetrahedra all have nodes (cellCenter, faceCenter, edgeNode1, edgeNode2)
1049 // For the correspondance of the nodes, see the GENERAL_48_SUB_NODES table in calculateSubNodes
1051 // nodes to use for tetrahedron
1052 const double* nodes[4];
1054 // get the cell center
1056 nodes[0] = getCoordsOfSubNode(conn[0]);
1058 for(int faceCenterNode = 8; faceCenterNode < 14; ++faceCenterNode)
1060 // get the face center
1061 conn[1] = faceCenterNode;
1062 nodes[1] = getCoordsOfSubNode(conn[1]);
1063 for(int j = 0; j < 4; ++j)
1065 const int row = 4*(faceCenterNode - 8) + j;
1066 conn[2] = TETRA_EDGES_GENERAL_24[2*row];
1067 conn[3] = TETRA_EDGES_GENERAL_24[2*row + 1];
1068 nodes[2] = getCoordsOfSubNode(conn[2]);
1069 nodes[3] = getCoordsOfSubNode(conn[3]);
1071 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes, conn);
1079 * Splits the hexahedron into 48 tetrahedra.
1080 * The splitting is done by introducing the midpoints of all the edges
1081 * and the barycenter of the element as submesh nodes. The 8 hexahedral subzones thus defined
1082 * are then split into 6 tetrahedra each, as in Grandy, p. 449. The division of the subzones
1083 * is done by calling sixSplit().
1086 template<class MyMeshTypeT, class MyMeshTypeS>
1087 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral48Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1089 // Define 8 hexahedral subzones as in Grandy, p449
1090 // the values correspond to the nodes that correspond to nodes 1,2,3,4,5,6,7,8 in the subcell
1091 // For the correspondance of the nodes, see the GENERAL_48_SUB_NODES table in calculateSubNodes
1092 static const int subZones[64] =
1094 0,8,21,12,9,20,26,22,
1095 8,1,13,21,20,10,23,26,
1096 12,21,16,3,22,26,25,17,
1097 21,13,2,16,26,23,18,25,
1098 9,20,26,22,4,11,24,14,
1099 20,10,23,26,11,5,15,24,
1100 22,26,25,17,14,24,19,7,
1101 26,23,18,25,24,15,6,19
1104 for(int i = 0; i < 8; ++i)
1106 sixSplit(&subZones[8*i],tetra);
1111 * Splits the NORM_PYRA5 into 2 tetrahedra.
1113 template<class MyMeshTypeT, class MyMeshTypeS>
1114 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitPyram5(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1116 static const int SPLIT_PYPA5[2][4] =
1126 // create tetrahedra
1127 const double* nodes[4];
1129 for(int i = 0; i < 2; ++i)
1131 for(int j = 0; j < 4; ++j)
1132 nodes[j] = getCoordsOfSubNode2(SPLIT_PYPA5[i][j],conn[j]);
1133 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1139 * Splits a convex cell into tetrahedra.
1141 template<class MyMeshTypeT, class MyMeshTypeS>
1142 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitConvex(typename MyMeshTypeT::MyConnType targetCell,
1143 typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1145 // Each face of a cell is split into triangles and
1146 // each of triangles and a cell barycenter form a tetrahedron.
1148 typedef typename MyMeshTypeT::MyConnType ConnType;
1149 const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
1151 // get type of cell and nb of cell nodes
1152 NormalizedCellType normCellType=_target_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(targetCell));
1153 const CellModel& cellModelCell=CellModel::GetCellModel(normCellType);
1154 unsigned nbOfCellNodes=cellModelCell.isDynamic() ? _target_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(targetCell)) : cellModelCell.getNumberOfNodes();
1156 // get nb of cell sons (faces)
1157 const ConnType* rawCellConn = _target_mesh.getConnectivityPtr() + OTT<ConnType,numPol>::conn2C( _target_mesh.getConnectivityIndexPtr()[ targetCell ]);
1158 const int rawNbCellNodes = _target_mesh.getConnectivityIndexPtr()[ targetCell+1 ] - _target_mesh.getConnectivityIndexPtr()[ targetCell ];
1159 unsigned nbOfSons = cellModelCell.getNumberOfSons2(rawCellConn, rawNbCellNodes);
1161 // indices of nodes of a son
1162 static std::vector<int> allNodeIndices; // == 0,1,2,...,nbOfCellNodes-1
1163 while ( allNodeIndices.size() < nbOfCellNodes )
1164 allNodeIndices.push_back( allNodeIndices.size() );
1165 std::vector<int> classicFaceNodes(4);
1166 int* faceNodes = cellModelCell.isDynamic() ? &allNodeIndices[0] : &classicFaceNodes[0];
1168 // nodes of tetrahedron
1170 const double* nodes[4];
1171 nodes[3] = getCoordsOfSubNode2( nbOfCellNodes,conn[3]); // barycenter
1173 for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
1175 // get indices of son's nodes: it's just next portion of allNodeIndices for polyhedron
1176 // and some of allNodeIndices accodring to cell model for a classsic cell
1177 unsigned nbFaceNodes = cellModelCell.getNumberOfNodesConstituentTheSon2(ii, rawCellConn, rawNbCellNodes);
1178 if ( normCellType != NORM_POLYHED )
1179 cellModelCell.fillSonCellNodalConnectivity(ii,&allNodeIndices[0],faceNodes);
1181 int nbTetra = nbFaceNodes - 2; // split polygon into nbTetra triangles
1183 // create tetrahedra
1184 for(int i = 0; i < nbTetra; ++i)
1186 nodes[0] = getCoordsOfSubNode2( faceNodes[0], conn[0]);
1187 nodes[1] = getCoordsOfSubNode2( faceNodes[1+i],conn[1]);
1188 nodes[2] = getCoordsOfSubNode2( faceNodes[2+i],conn[2]);
1189 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1193 if ( normCellType == NORM_POLYHED )
1194 faceNodes += nbFaceNodes; // go to the next face
1199 * Precalculates all the nodes.
1200 * Retrieves the mesh nodes and allocates the necessary sub-mesh
1201 * nodes according to the splitting policy used.
1202 * This method is meant to be called once by the constructor.
1204 * @param targetMesh the target mesh
1205 * @param targetCell the global number of the cell that the object represents, in targetMesh mode.
1206 * @param policy the splitting policy of the object
1209 template<class MyMeshTypeT, class MyMeshTypeS>
1210 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateSubNodes(const MyMeshTypeT& targetMesh, typename MyMeshTypeT::MyConnType targetCell)
1212 // retrieve real mesh nodes
1214 typename MyMeshTypeT::MyConnType nbOfNodesT = _node_ids.size();// Issue 0020634. _node_ids.resize(8);
1215 for(int node = 0; node < nbOfNodesT ; ++node)
1217 // calculate only normal nodes
1218 _nodes.push_back(getCoordsOfNode2(node, targetCell, targetMesh,_node_ids[node]));
1221 switch ( nbOfNodesT )
1225 // create sub-mesh nodes if needed
1226 switch(_splitting_pol)
1230 for(int i = 0; i < 7; ++i)
1232 double* barycenter = new double[3];
1233 calcBarycenter(4, barycenter, &GENERAL_24_SUB_NODES[4*i]);
1234 _nodes.push_back(barycenter);
1241 // Each sub-node is the barycenter of two other nodes.
1242 // For the edges, these lie on the original mesh.
1243 // For the faces, these are the edge sub-nodes.
1244 // For the cell these are two face sub-nodes.
1245 static const int GENERAL_48_SUB_NODES[38] =
1247 0,1, // sub-node 9 (edge)
1248 0,4, // sub-node 10 (edge)
1249 1,5, // sub-node 11 (edge)
1250 4,5, // sub-node 12 (edge)
1251 0,3, // sub-node 13 (edge)
1252 1,2, // sub-node 14 (edge)
1253 4,7, // sub-node 15 (edge)
1254 5,6, // sub-node 16 (edge)
1255 2,3, // sub-node 17 (edge)
1256 3,7, // sub-node 18 (edge)
1257 2,6, // sub-node 19 (edge)
1258 6,7, // sub-node 20 (edge)
1259 8,11, // sub-node 21 (face)
1260 12,13, // sub-node 22 (face)
1261 9,17, // sub-node 23 (face)
1262 10,18, // sub-node 24 (face)
1263 14,15, // sub-node 25 (face)
1264 16,19, // sub-node 26 (face)
1265 20,25 // sub-node 27 (cell)
1268 for(int i = 0; i < 19; ++i)
1270 double* barycenter = new double[3];
1271 calcBarycenter(2, barycenter, &GENERAL_48_SUB_NODES[2*i]);
1272 _nodes.push_back(barycenter);
1281 case 5: // NORM_PYRA5
1284 default: // convex 3d cell
1286 // add barycenter of a cell
1287 std::vector<int> allIndices(nbOfNodesT);
1288 for ( int i = 0; i < nbOfNodesT; ++i ) allIndices[i] = i;
1289 double* barycenter = new double[3];
1290 calcBarycenter(nbOfNodesT, barycenter, &allIndices[0]);
1291 _nodes.push_back(barycenter);