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))
534 * Calculates the surface of intersection of a polygon face in the source mesh and a cell of the target mesh.
535 * It first calculates the transformation that takes the target tetrahedron into the unit tetrahedron. After that, the
536 * faces of the source element are triangulated and the calculated transformation is applied
538 * The algorithm is based on the algorithm of Grandy used in intersectSourceCell to compute
539 * the volume of intersection of two cell elements.
540 * The case with a source face colinear to one of the face of tetrahedrons is taking into account:
541 * the contribution of the face must not be counted two times.
543 * The class will cache the intermediary calculations of transformed nodes of source faces and surfaces associated
544 * with triangulated faces to avoid having to recalculate these.
546 * @param polyType type of the polygon source face
547 * @param polyNodesNbr number of the nodes of the polygon source face
548 * @param polyNodes numbers of the nodes of the polygon source face
549 * @param polyCoords coordinates of the nodes of the polygon source face
550 * @param polyCoords coordinates of the nodes of the polygon source face
551 * @param dimCaracteristic characteristic size of the meshes containing the triangles
552 * @param precision precision for double float data used for comparison
553 * @param listOfTetraFacesTreated list of tetra faces treated
554 * @param listOfTetraFacesColinear list of tetra faces colinear with the polygon source faces
556 template<class MyMeshType>
557 double SplitterTetra<MyMeshType>::intersectSourceFace(const NormalizedCellType polyType,
558 const int polyNodesNbr,
559 const int *const polyNodes,
560 const double *const *const polyCoords,
561 const double dimCaracteristic,
562 const double precision,
563 std::multiset<TriangleFaceKey>& listOfTetraFacesTreated,
564 std::set<TriangleFaceKey>& listOfTetraFacesColinear)
566 typedef typename MyMeshType::MyConnType ConnType;
568 double totalSurface = 0.0;
570 // check if we have planar tetra element
571 if(_t->determinant() == 0.0)
574 LOG(2, "Planar tetra -- volume 0");
578 // halfspace filtering
579 bool isOutside[8] = {true, true, true, true, true, true, true, true};
580 bool isStrictlyOutside[8] = {true, true, true, true, true, true, true, true};
581 bool isTargetStrictlyOutside = false;
582 bool isTargetOutside = false;
584 // calculate the coordinates of the nodes
585 for(int i = 0;i<(int)polyNodesNbr;++i)
587 const int globalNodeNum = polyNodes[i];
588 if(_nodes.find(globalNodeNum) == _nodes.end())
590 calculateNode2(globalNodeNum, polyCoords[i]);
593 checkIsStrictlyOutside(_nodes[globalNodeNum], isStrictlyOutside, precision);
594 checkIsOutside(_nodes[globalNodeNum], isOutside, precision);
597 // halfspace filtering check
598 // NB : might not be beneficial for caching of triangles
599 for(int i = 0; i < 8; ++i)
601 if(isStrictlyOutside[i])
603 isTargetStrictlyOutside = true;
606 else if (isOutside[i])
608 isTargetOutside = true;
612 if (!isTargetStrictlyOutside)
617 // Faces are parallel
618 const int tetraFacesNodesConn[4][3] = {
623 double planeNormal[3];
624 for (int iTetraFace = 0; iTetraFace < 4; ++iTetraFace)
626 const int * const tetraFaceNodesConn = tetraFacesNodesConn[iTetraFace];
627 TriangleFaceKey key = TriangleFaceKey(_conn[tetraFaceNodesConn[0]],
628 _conn[tetraFaceNodesConn[1]],
629 _conn[tetraFaceNodesConn[2]]);
630 if (listOfTetraFacesTreated.find(key) == listOfTetraFacesTreated.end())
632 const double * const coordsTetraTriNode1 = _coords + tetraFaceNodesConn[0] * MyMeshType::MY_SPACEDIM;
633 const double * const coordsTetraTriNode2 = _coords + tetraFaceNodesConn[1] * MyMeshType::MY_SPACEDIM;
634 const double * const coordsTetraTriNode3 = _coords + tetraFaceNodesConn[2] * MyMeshType::MY_SPACEDIM;
635 calculateNormalForTria(coordsTetraTriNode1, coordsTetraTriNode2, coordsTetraTriNode3, planeNormal);
636 const double normOfTetraTriNormal = norm(planeNormal);
637 if (epsilonEqual(normOfTetraTriNormal, 0.))
639 for (int i = 0; i < 3; ++i)
646 const double invNormOfTetraTriNormal = 1. / normOfTetraTriNormal;
647 for (int i = 0; i < 3; ++i)
649 planeNormal[i] *= invNormOfTetraTriNormal;
652 double planeConstant = dot(planeNormal, coordsTetraTriNode1);
653 if (IsFacesCoplanar(planeNormal, planeConstant, polyCoords, precision))
655 int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
656 for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
658 double volume = CalculateIntersectionSurfaceOfCoplanarTriangles(planeNormal,
661 polyCoords[1 + iTri],
662 polyCoords[2 + iTri],
668 if (!epsilonEqual(volume, 0.))
670 totalSurface += volume;
671 listOfTetraFacesColinear.insert(key);
676 listOfTetraFacesTreated.insert(key);
681 // intersect a son with the unit tetra
686 // create the face key
687 TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
689 // calculate the triangle if needed
690 if (_volumes.find(key) == _volumes.end())
692 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
693 calculateSurface(tri, key);
694 totalSurface += _volumes[key];
698 // count negative as face has reversed orientation
699 totalSurface -= _volumes[key];
706 // simple triangulation of faces along a diagonal :
717 //? not sure if this always works
719 // calculate the triangles if needed
721 // local nodes 1, 2, 3
722 TriangleFaceKey key1 = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
723 if (_volumes.find(key1) == _volumes.end())
725 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
726 calculateSurface(tri, key1);
727 totalSurface += _volumes[key1];
731 // count negative as face has reversed orientation
732 totalSurface -= _volumes[key1];
735 // local nodes 1, 3, 4
736 TriangleFaceKey key2 = TriangleFaceKey(polyNodes[0], polyNodes[2], polyNodes[3]);
737 if (_volumes.find(key2) == _volumes.end())
739 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[2]], _nodes[polyNodes[3]]);
740 calculateSurface(tri, key2);
741 totalSurface += _volumes[key2];
745 // count negative as face has reversed orientation
746 totalSurface -= _volumes[key2];
753 int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
754 for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
756 TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1 + iTri], polyNodes[2 + iTri]);
757 if (_volumes.find(key) == _volumes.end())
759 TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1 + iTri]],
760 _nodes[polyNodes[2 + iTri]]);
761 calculateSurface(tri, key);
762 totalSurface += _volumes[key];
766 totalSurface -= _volumes[key];
774 << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment."
782 // reset if it is very small to keep the matrix sparse
783 // is this a good idea?
784 if(epsilonEqual(totalSurface, 0.0, SPARSE_TRUNCATION_LIMIT))
789 LOG(2, "Volume = " << totalSurface << ", det= " << _t->determinant());
795 * Calculates the volume of intersection of this tetrahedron with another one.
797 template<class MyMeshType>
798 double SplitterTetra<MyMeshType>::intersectTetra(const double** tetraCorners)
800 //{ could be done on outside?
801 // check if we have planar tetra element
802 if(_t->determinant() == 0.0)
805 LOG(2, "Planar tetra -- volume 0");
809 const unsigned nbOfNodes4Type=4;
810 // halfspace filtering
811 bool isOutside[8] = {true, true, true, true, true, true, true, true};
812 bool isTargetOutside = false;
814 // calculate the transformed coordinates of the nodes
815 double nodes[nbOfNodes4Type][3];
816 for(int i = 0;i<(int)nbOfNodes4Type;++i)
818 _t->apply(nodes[i], tetraCorners[i]);
819 checkIsOutside(nodes[i], isOutside);
822 // halfspace filtering check
823 // NB : might not be beneficial for caching of triangles
824 for(int i = 0; i < 8; ++i)
828 isTargetOutside = true;
832 double totalVolume = 0.0;
836 const CellModel& cellModelCell=CellModel::GetCellModel(NORM_TETRA4);
837 int cellNodes[4] = { 0, 1, 2, 3 }, faceNodes[3];
839 for(unsigned ii = 0 ; ii < 4 ; ++ii)
841 cellModelCell.fillSonCellNodalConnectivity(ii,cellNodes,faceNodes);
843 TransformedTriangle tri(nodes[faceNodes[0]], nodes[faceNodes[1]], nodes[faceNodes[2]]);
844 double vol = tri.calculateIntersectionVolume();
848 // reset if it is very small to keep the matrix sparse
849 // is this a good idea?
850 if(epsilonEqual(totalVolume, 0.0, SPARSE_TRUNCATION_LIMIT))
855 LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
857 // NB : fault in article, Grandy, [8] : it is the determinant of the inverse transformation
858 // that should be used (which is equivalent to dividing by the determinant)
859 return std::fabs(1.0 / _t->determinant() * totalVolume) ;
862 ////////////////////////////////////////////////////////
864 template<class MyMeshTypeT, class MyMeshTypeS>
865 SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::SplitterTetra2(const MyMeshTypeT& targetMesh, const MyMeshTypeS& srcMesh, SplittingPolicy policy)
866 :_target_mesh(targetMesh),_src_mesh(srcMesh),_splitting_pol(policy)
870 template<class MyMeshTypeT, class MyMeshTypeS>
871 SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::~SplitterTetra2()
876 template<class MyMeshTypeT, class MyMeshTypeS>
877 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::releaseArrays()
879 // free potential sub-mesh nodes that have been allocated
880 typename MyMeshTypeT::MyConnType nbOfNodesT = _node_ids.size();// Issue 0020634.
881 if((int)_nodes.size()>=/*8*/nbOfNodesT)
883 std::vector<const double*>::iterator iter = _nodes.begin() + /*8*/nbOfNodesT;
884 while(iter != _nodes.end())
894 * @param targetCell in C mode.
895 * @param tetra is the output result tetra containers.
897 template<class MyMeshTypeT, class MyMeshTypeS>
898 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitTargetCell(typename MyMeshTypeT::MyConnType targetCell,
899 typename MyMeshTypeT::MyConnType nbOfNodesT,
900 typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
902 typedef typename MyMeshTypeT::MyConnType ConnType;
903 const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
904 const int numTetra = static_cast<int>(_splitting_pol);
910 const double *nodes[4];
912 for(int node = 0; node < 4 ; ++node)
914 nodes[node]=getCoordsOfNode2(node, OTT<ConnType,numPol>::indFC(targetCell),_target_mesh,conn[node]);
916 std::copy(conn,conn+4,_node_ids.begin());
917 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
921 // Issue 0020634. To pass nbOfNodesT to calculateSubNodes (don't want to add an arg)
922 _node_ids.resize(nbOfNodesT);
924 // pre-calculate nodes
925 calculateSubNodes(_target_mesh, OTT<ConnType,numPol>::indFC(targetCell));
927 tetra.reserve(numTetra);
928 _nodes.reserve(30); // we never have more than this
930 switch ( nbOfNodesT )
934 switch(_splitting_pol)
938 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
939 fiveSplit(subZone,tetra);
945 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
946 sixSplit(subZone,tetra);
952 calculateGeneral24Tetra(tetra);
958 calculateGeneral48Tetra(tetra);
973 splitConvex(targetCell, tetra);
979 * Splits the hexahedron into five tetrahedra.
980 * This method adds five SplitterTetra objects to the vector tetra.
982 * @param subZone the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the
983 * splitting to be reused on the subzones of the GENERAL_* types of splitting
985 template<class MyMeshTypeT, class MyMeshTypeS>
986 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::fiveSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
989 for(int i = 0; i < 5; ++i)
991 const double* nodes[4];
993 for(int j = 0; j < 4; ++j)
995 conn[j] = subZone[ SPLIT_NODES_5[4*i+j] ];
996 nodes[j] = getCoordsOfSubNode(conn[j]);
998 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1004 * Splits the hexahedron into six tetrahedra.
1005 * This method adds six SplitterTetra objects to the vector tetra.
1007 * @param subZone the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the
1008 * splitting to be reused on the subzones of the GENERAL_* types of splitting
1010 template<class MyMeshTypeT, class MyMeshTypeS>
1011 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::sixSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1013 for(int i = 0; i < 6; ++i)
1015 const double* nodes[4];
1017 for(int j = 0; j < 4; ++j)
1019 conn[j] = subZone[SPLIT_NODES_6[4*i+j]];
1020 nodes[j] = getCoordsOfSubNode(conn[j]);
1022 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1028 * Splits the hexahedron into 24 tetrahedra.
1029 * The splitting is done by combining the barycenter of the tetrahedron, the barycenter of each face
1030 * and the nodes of each edge of the face. This creates 6 faces * 4 edges / face = 24 tetrahedra.
1031 * The submesh nodes introduced are the barycenters of the faces and the barycenter of the cell.
1034 template<class MyMeshTypeT, class MyMeshTypeS>
1035 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral24Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1037 // The two nodes of the original mesh cell used in each tetrahedron.
1038 // The tetrahedra all have nodes (cellCenter, faceCenter, edgeNode1, edgeNode2)
1039 // For the correspondance of the nodes, see the GENERAL_48_SUB_NODES table in calculateSubNodes
1041 // nodes to use for tetrahedron
1042 const double* nodes[4];
1044 // get the cell center
1046 nodes[0] = getCoordsOfSubNode(conn[0]);
1048 for(int faceCenterNode = 8; faceCenterNode < 14; ++faceCenterNode)
1050 // get the face center
1051 conn[1] = faceCenterNode;
1052 nodes[1] = getCoordsOfSubNode(conn[1]);
1053 for(int j = 0; j < 4; ++j)
1055 const int row = 4*(faceCenterNode - 8) + j;
1056 conn[2] = TETRA_EDGES_GENERAL_24[2*row];
1057 conn[3] = TETRA_EDGES_GENERAL_24[2*row + 1];
1058 nodes[2] = getCoordsOfSubNode(conn[2]);
1059 nodes[3] = getCoordsOfSubNode(conn[3]);
1061 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes, conn);
1069 * Splits the hexahedron into 48 tetrahedra.
1070 * The splitting is done by introducing the midpoints of all the edges
1071 * and the barycenter of the element as submesh nodes. The 8 hexahedral subzones thus defined
1072 * are then split into 6 tetrahedra each, as in Grandy, p. 449. The division of the subzones
1073 * is done by calling sixSplit().
1076 template<class MyMeshTypeT, class MyMeshTypeS>
1077 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral48Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1079 // Define 8 hexahedral subzones as in Grandy, p449
1080 // the values correspond to the nodes that correspond to nodes 1,2,3,4,5,6,7,8 in the subcell
1081 // For the correspondance of the nodes, see the GENERAL_48_SUB_NODES table in calculateSubNodes
1082 static const int subZones[64] =
1084 0,8,21,12,9,20,26,22,
1085 8,1,13,21,20,10,23,26,
1086 12,21,16,3,22,26,25,17,
1087 21,13,2,16,26,23,18,25,
1088 9,20,26,22,4,11,24,14,
1089 20,10,23,26,11,5,15,24,
1090 22,26,25,17,14,24,19,7,
1091 26,23,18,25,24,15,6,19
1094 for(int i = 0; i < 8; ++i)
1096 sixSplit(&subZones[8*i],tetra);
1101 * Splits the NORM_PYRA5 into 2 tetrahedra.
1103 template<class MyMeshTypeT, class MyMeshTypeS>
1104 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitPyram5(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1106 static const int SPLIT_PYPA5[2][4] =
1116 // create tetrahedra
1117 const double* nodes[4];
1119 for(int i = 0; i < 2; ++i)
1121 for(int j = 0; j < 4; ++j)
1122 nodes[j] = getCoordsOfSubNode2(SPLIT_PYPA5[i][j],conn[j]);
1123 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1129 * Splits a convex cell into tetrahedra.
1131 template<class MyMeshTypeT, class MyMeshTypeS>
1132 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitConvex(typename MyMeshTypeT::MyConnType targetCell,
1133 typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1135 // Each face of a cell is split into triangles and
1136 // each of triangles and a cell barycenter form a tetrahedron.
1138 typedef typename MyMeshTypeT::MyConnType ConnType;
1139 const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
1141 // get type of cell and nb of cell nodes
1142 NormalizedCellType normCellType=_target_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(targetCell));
1143 const CellModel& cellModelCell=CellModel::GetCellModel(normCellType);
1144 unsigned nbOfCellNodes=cellModelCell.isDynamic() ? _target_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(targetCell)) : cellModelCell.getNumberOfNodes();
1146 // get nb of cell sons (faces)
1147 const ConnType* rawCellConn = _target_mesh.getConnectivityPtr() + OTT<ConnType,numPol>::conn2C( _target_mesh.getConnectivityIndexPtr()[ targetCell ]);
1148 const int rawNbCellNodes = _target_mesh.getConnectivityIndexPtr()[ targetCell+1 ] - _target_mesh.getConnectivityIndexPtr()[ targetCell ];
1149 unsigned nbOfSons = cellModelCell.getNumberOfSons2(rawCellConn, rawNbCellNodes);
1151 // indices of nodes of a son
1152 static std::vector<int> allNodeIndices; // == 0,1,2,...,nbOfCellNodes-1
1153 while ( allNodeIndices.size() < nbOfCellNodes )
1154 allNodeIndices.push_back( allNodeIndices.size() );
1155 std::vector<int> classicFaceNodes(4);
1156 int* faceNodes = cellModelCell.isDynamic() ? &allNodeIndices[0] : &classicFaceNodes[0];
1158 // nodes of tetrahedron
1160 const double* nodes[4];
1161 nodes[3] = getCoordsOfSubNode2( nbOfCellNodes,conn[3]); // barycenter
1163 for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
1165 // get indices of son's nodes: it's just next portion of allNodeIndices for polyhedron
1166 // and some of allNodeIndices accodring to cell model for a classsic cell
1167 unsigned nbFaceNodes = cellModelCell.getNumberOfNodesConstituentTheSon2(ii, rawCellConn, rawNbCellNodes);
1168 if ( normCellType != NORM_POLYHED )
1169 cellModelCell.fillSonCellNodalConnectivity(ii,&allNodeIndices[0],faceNodes);
1171 int nbTetra = nbFaceNodes - 2; // split polygon into nbTetra triangles
1173 // create tetrahedra
1174 for(int i = 0; i < nbTetra; ++i)
1176 nodes[0] = getCoordsOfSubNode2( faceNodes[0], conn[0]);
1177 nodes[1] = getCoordsOfSubNode2( faceNodes[1+i],conn[1]);
1178 nodes[2] = getCoordsOfSubNode2( faceNodes[2+i],conn[2]);
1179 SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1183 if ( normCellType == NORM_POLYHED )
1184 faceNodes += nbFaceNodes; // go to the next face
1189 * Precalculates all the nodes.
1190 * Retrieves the mesh nodes and allocates the necessary sub-mesh
1191 * nodes according to the splitting policy used.
1192 * This method is meant to be called once by the constructor.
1194 * @param targetMesh the target mesh
1195 * @param targetCell the global number of the cell that the object represents, in targetMesh mode.
1196 * @param policy the splitting policy of the object
1199 template<class MyMeshTypeT, class MyMeshTypeS>
1200 void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateSubNodes(const MyMeshTypeT& targetMesh, typename MyMeshTypeT::MyConnType targetCell)
1202 // retrieve real mesh nodes
1204 typename MyMeshTypeT::MyConnType nbOfNodesT = _node_ids.size();// Issue 0020634. _node_ids.resize(8);
1205 for(int node = 0; node < nbOfNodesT ; ++node)
1207 // calculate only normal nodes
1208 _nodes.push_back(getCoordsOfNode2(node, targetCell, targetMesh,_node_ids[node]));
1211 switch ( nbOfNodesT )
1215 // create sub-mesh nodes if needed
1216 switch(_splitting_pol)
1220 for(int i = 0; i < 7; ++i)
1222 double* barycenter = new double[3];
1223 calcBarycenter(4, barycenter, &GENERAL_24_SUB_NODES[4*i]);
1224 _nodes.push_back(barycenter);
1231 // Each sub-node is the barycenter of two other nodes.
1232 // For the edges, these lie on the original mesh.
1233 // For the faces, these are the edge sub-nodes.
1234 // For the cell these are two face sub-nodes.
1235 static const int GENERAL_48_SUB_NODES[38] =
1237 0,1, // sub-node 9 (edge)
1238 0,4, // sub-node 10 (edge)
1239 1,5, // sub-node 11 (edge)
1240 4,5, // sub-node 12 (edge)
1241 0,3, // sub-node 13 (edge)
1242 1,2, // sub-node 14 (edge)
1243 4,7, // sub-node 15 (edge)
1244 5,6, // sub-node 16 (edge)
1245 2,3, // sub-node 17 (edge)
1246 3,7, // sub-node 18 (edge)
1247 2,6, // sub-node 19 (edge)
1248 6,7, // sub-node 20 (edge)
1249 8,11, // sub-node 21 (face)
1250 12,13, // sub-node 22 (face)
1251 9,17, // sub-node 23 (face)
1252 10,18, // sub-node 24 (face)
1253 14,15, // sub-node 25 (face)
1254 16,19, // sub-node 26 (face)
1255 20,25 // sub-node 27 (cell)
1258 for(int i = 0; i < 19; ++i)
1260 double* barycenter = new double[3];
1261 calcBarycenter(2, barycenter, &GENERAL_48_SUB_NODES[2*i]);
1262 _nodes.push_back(barycenter);
1271 case 5: // NORM_PYRA5
1274 default: // convex 3d cell
1276 // add barycenter of a cell
1277 std::vector<int> allIndices(nbOfNodesT);
1278 for ( int i = 0; i < nbOfNodesT; ++i ) allIndices[i] = i;
1279 double* barycenter = new double[3];
1280 calcBarycenter(nbOfNodesT, barycenter, &allIndices[0]);
1281 _nodes.push_back(barycenter);