Salome HOME
8364bb1b4a5782d9a547cf9e7cf820c0c47d7e31
[tools/medcoupling.git] / src / INTERP_KERNEL / SplitterTetra.txx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #ifndef __SPLITTERTETRA_TXX__
20 #define __SPLITTERTETRA_TXX__
21
22 #include "SplitterTetra.hxx"
23
24 #include "TetraAffineTransform.hxx"
25 #include "TransformedTriangle.hxx"
26 #include "MeshUtils.hxx"
27 #include "VectorUtils.hxx"
28 #include "CellModel.hxx"
29 #include "Log.hxx"
30 #include "UnitTetraIntersectionBary.hxx"
31 #include "VolSurfFormulae.hxx"
32
33 #include <cmath>
34 #include <cassert>
35 #include <string>
36 #include <sstream>
37 #include <vector>
38
39 namespace INTERP_KERNEL
40 {
41   /*!
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.
44    */
45   template<class MyMeshType>
46   void SplitterTetra<MyMeshType>::splitIntoDualCells(SplitterTetra<MyMeshType> **output)
47   {
48     double tmp[12];
49     const double *tmp2[4]={tmp,tmp+3,tmp+6,tmp+9};
50     typename MyMeshType::MyConnType conn[4]={-1,-1,-1,-1};
51     for(int i=0;i<24;i++)
52       {
53         splitMySelfForDual(tmp,i,conn[0]);
54         output[i]=new SplitterTetra<MyMeshType>(_src_mesh,tmp2,conn);
55       }
56   }
57
58   /**
59    * Constructor creating object from the four corners of the tetrahedron.
60    *
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
64    */
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)
68   {
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);
76   }
77   
78   /**
79    * Destructor
80    *
81    * Deletes _t and the coordinates (double[3] vectors) in _nodes
82    *
83    */
84   template<class MyMeshType>
85   SplitterTetra<MyMeshType>::~SplitterTetra()
86   {
87     delete _t;
88     for(HashMap< int, double* >::iterator iter = _nodes.begin(); iter != _nodes.end() ; ++iter)
89       delete[] iter->second;
90   }
91
92   /*!
93    * \Forget already calculated triangles, which is crucial for calculation of barycenter of intersection
94    */
95   template<class MyMeshType>
96   void SplitterTetra<MyMeshType>::clearVolumesCache()
97   {
98     _volumes.clear();
99   }
100
101   /*!
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.
105    */
106   template<class MyMeshType>
107   void SplitterTetra<MyMeshType>::splitMySelfForDual(double* output, int i, typename MyMeshType::MyConnType& nodeId)
108   {
109     double *tmp[4];
110     int offset=i/6;
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;
113     int caseToTreat=i%6;
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);
125   }
126   
127   /**
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.
134    *
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.
137    *
138    * @param element      global number of the source element in C mode.
139    */
140   template<class MyMeshType>
141   double SplitterTetra<MyMeshType>::intersectSourceCell(typename MyMeshType::MyConnType element,
142                                                         double*                         baryCentre)
143   {
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)
149       {
150         // tetra is planar
151         LOG(2, "Planar tetra -- volume 0");
152         return 0.0;
153       }
154
155     // get type of cell
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;
162
163     // calculate the coordinates of the nodes
164     int *cellNodes=new int[nbOfNodes4Type];
165     for(int i = 0;i<(int)nbOfNodes4Type;++i)
166       {
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()) 
171           {
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);
176           }
177
178         checkIsOutside(_nodes[globalNodeNum], isOutside);       
179       }
180
181     // halfspace filtering check
182     // NB : might not be beneficial for caching of triangles
183     for(int i = 0; i < 8; ++i)
184       {
185         if(isOutside[i])
186           {
187             isTargetOutside = true;
188           }
189       }
190
191     double totalVolume = 0.0;
192
193     if(!isTargetOutside)
194       {
195         /// calculator of intersection barycentre
196         UnitTetraIntersectionBary baryCalculator( _t->determinant() < 0.);
197
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);
202
203         for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
204           {
205             // get sons connectivity
206             NormalizedCellType faceType;
207             int *faceNodes, nbFaceNodes=-1;
208             if ( cellModelCell.isDynamic() )
209               {
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]);
214               }
215             else
216               {
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);
222               }
223             // intersect a son with the unit tetra
224             switch(faceType)
225               {
226               case NORM_TRI3:
227                 {
228                   // create the face key
229                   TriangleFaceKey key = TriangleFaceKey(faceNodes[0], faceNodes[1], faceNodes[2]);
230
231                   // calculate the triangle if needed
232                   if(_volumes.find(key) == _volumes.end())
233                     {
234                       TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1]], _nodes[faceNodes[2]]);
235                       calculateVolume(tri, key);
236                       totalVolume += _volumes[key];
237                       if ( baryCentre )
238                         baryCalculator.addSide( tri );
239                     } else {    
240                       // count negative as face has reversed orientation
241                       totalVolume -= _volumes[key];
242                     }
243                 }
244                 break;
245
246               case NORM_QUAD4:
247
248                 // simple triangulation of faces along a diagonal :
249                 //
250                 // 2 ------ 3
251                 // |      / |
252                 // |     /  |
253                 // |    /   |
254                 // |   /    |
255                 // |  /     |
256                 // | /      |
257                 // 1 ------ 4
258                 //
259                 //? not sure if this always works 
260                 {
261                   // calculate the triangles if needed
262
263                   // local nodes 1, 2, 3
264                   TriangleFaceKey key1 = TriangleFaceKey(faceNodes[0], faceNodes[1], faceNodes[2]);
265                   if(_volumes.find(key1) == _volumes.end())
266                     {
267                       TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1]], _nodes[faceNodes[2]]);
268                       calculateVolume(tri, key1);
269                       totalVolume += _volumes[key1];
270                     } else {
271                       // count negative as face has reversed orientation
272                       totalVolume -= _volumes[key1];
273                     }
274
275                   // local nodes 1, 3, 4
276                   TriangleFaceKey key2 = TriangleFaceKey(faceNodes[0], faceNodes[2], faceNodes[3]);
277                   if(_volumes.find(key2) == _volumes.end())
278                     {
279                       TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[2]], _nodes[faceNodes[3]]);
280                       calculateVolume(tri, key2);
281                       totalVolume += _volumes[key2];
282                     }
283                   else
284                     { 
285                       // count negative as face has reversed orientation
286                       totalVolume -= _volumes[key2];
287                     }
288                 }
289                 break;
290
291               case NORM_POLYGON:
292                 {
293                   int nbTria = nbFaceNodes - 2; // split polygon into nbTria triangles
294                   for ( int iTri = 0; iTri < nbTria; ++iTri )
295                     {
296                       TriangleFaceKey key = TriangleFaceKey(faceNodes[0], faceNodes[1+iTri], faceNodes[2+iTri]);
297                       if(_volumes.find(key) == _volumes.end())
298                         {
299                           TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1+iTri]], _nodes[faceNodes[2+iTri]]);
300                           calculateVolume(tri, key);
301                           totalVolume += _volumes[key];
302                         }
303                       else
304                         {
305                           totalVolume -= _volumes[key];
306                         }
307                     }
308                 }
309                 break;
310
311               default:
312                 std::cout << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment." << std::endl;
313                 assert(false);
314               }
315             delete [] faceNodes;
316           }
317
318         if ( baryCentre ) {
319           baryCalculator.getBary( baryCentre );
320           _t->reverseApply( baryCentre, baryCentre );
321         }
322       }
323     delete [] cellNodes;
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))
327       {
328         totalVolume = 0.0;
329       }
330
331     LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
332
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) ;
336   }
337
338   /**
339    * Calculates the intersection surface of two coplanar triangles.
340    *
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
351    */
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)
358   {
359     typedef typename MyMeshType::MyConnType ConnType;
360     typedef double Vect2[2];
361     typedef double Vect3[3];
362     typedef double Triangle2[3][2];
363
364     const double *const tri0[3] = {p1, p2, p3};
365     const double *const tri1[3] = {p4, p5, p6};
366
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
369     int maxNormal = 0;
370     double fmax = std::abs(planeNormal[0]);
371     double absMax = std::abs(planeNormal[1]);
372     if (absMax > fmax)
373       {
374         maxNormal = 1;
375         fmax = absMax;
376       }
377     absMax = std::abs(planeNormal[2]);
378     if (absMax > fmax)
379       {
380         maxNormal = 2;
381       }
382
383     Triangle2 projTri0, projTri1;
384     int i;
385
386     if (maxNormal == 0)
387       {
388         // Project onto yz-plane.
389         for (i = 0; i < 3; ++i)
390           {
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];
395           }
396       }
397     else if (maxNormal == 1)
398       {
399         // Project onto xz-plane.
400         for (i = 0; i < 3; ++i)
401           {
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];
406           }
407       }
408     else
409       {
410         // Project onto xy-plane.
411         for (i = 0; i < 3; ++i)
412           {
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];
417           }
418       }
419
420     // 2D triangle intersection routines require counterclockwise ordering.
421     Vect2 save;
422     Vect2 edge0;
423     Vect2 edge1;
424     for (int ii = 0; ii < 2; ++ii)
425       {
426         edge0[ii] = projTri0[1][ii] - projTri0[0][ii];
427         edge1[ii] = projTri0[2][ii] - projTri0[0][ii];
428       }
429     if ((edge0[0] * edge1[1] - edge0[1] * edge1[0]) < (double) 0.)
430       {
431         // Triangle is clockwise, reorder it.
432         for (int ii = 0; ii < 2; ++ii)
433           {
434             save[ii] = projTri0[1][ii];
435             projTri0[1][ii] = projTri0[2][ii];
436             projTri0[2][ii] = save[ii];
437           }
438       }
439
440     for (int ii = 0; ii < 2; ++ii)
441       {
442         edge0[ii] = projTri1[1][ii] - projTri1[0][ii];
443         edge1[ii] = projTri1[2][ii] - projTri1[0][ii];
444       }
445     if ((edge0[0] * edge1[1] - edge0[1] * edge1[0]) < (double) 0.)
446       {
447         // Triangle is clockwise, reorder it.
448       for (int ii = 0; ii < 2; ++ii)
449         {
450           save[ii] = projTri1[1][ii];
451           projTri1[1][ii] = projTri1[2][ii];
452           projTri1[2][ii] = save[ii];
453         }
454       }
455
456     std::vector<double> inter2;
457     intersec_de_triangle(projTri0[0], projTri0[1], projTri0[2],
458                          projTri1[0], projTri1[1], projTri1[2],
459                          inter2,
460                          dimCaracteristic, precision);
461     ConnType nb_inter=((ConnType)inter2.size())/2;
462     double surface = 0.;
463     if(nb_inter >3) inter2=reconstruct_polygon(inter2);
464     if (nb_inter > 0)
465       {
466         std::vector<double> inter3;
467         inter3.resize(3 * nb_inter);
468         // Map 2D intersections back to the 3D triangle space.
469         if (maxNormal == 0)
470           {
471             double invNX = ((double) 1.) / planeNormal[0];
472             for (i = 0; i < nb_inter; i++)
473               {
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]);
477               }
478           }
479         else if (maxNormal == 1)
480           {
481             double invNY = ((double) 1.) / planeNormal[1];
482             for (i = 0; i < nb_inter; i++)
483               {
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]);
487               }
488           }
489         else
490           {
491             double invNZ = ((double) 1.) / planeNormal[2];
492             for (i = 0; i < nb_inter; i++)
493               {
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]);
497               }
498           }
499         surface = polygon_area<3>(inter3);
500       }
501     return surface;
502   }
503
504   /**
505    * Determine if a face is coplanar with a triangle.
506    * The first face is characterized by the equation of her plane
507    *
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
512    */
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)
518   {
519       // Compute the signed distances of triangle vertices to the plane. Use an epsilon-thick plane test.
520       // For faces not left
521       int counter = 0;
522       for (int i = 0; i < 3; ++i)
523         {
524           const double distance = dot(planeNormal, coordsFace[i]) - planeConstant;
525           if (epsilonEqual(distance, precision))
526             {
527               counter++;
528             }
529         }
530       return counter == 3;
531   }
532
533   /**
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
537    * to each triangle.
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.
542    *
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.
545    *
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
555    */
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)
565   {
566     typedef typename MyMeshType::MyConnType ConnType;
567
568     double totalSurface = 0.0;
569
570     // check if we have planar tetra element
571     if(_t->determinant() == 0.0)
572       {
573         // tetra is planar
574         LOG(2, "Planar tetra -- volume 0");
575         return 0.0;
576       }
577
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;
583
584     // calculate the coordinates of the nodes
585     for(int i = 0;i<(int)polyNodesNbr;++i)
586       {
587         const int globalNodeNum = polyNodes[i];
588         if(_nodes.find(globalNodeNum) == _nodes.end())
589           {
590             calculateNode2(globalNodeNum, polyCoords[i]);
591           }
592
593         checkIsStrictlyOutside(_nodes[globalNodeNum], isStrictlyOutside, precision);
594         checkIsOutside(_nodes[globalNodeNum], isOutside, precision);
595       }
596
597     // halfspace filtering check
598     // NB : might not be beneficial for caching of triangles
599     for(int i = 0; i < 8; ++i)
600       {
601         if(isStrictlyOutside[i])
602           {
603             isTargetStrictlyOutside = true;
604             break;
605           }
606         else if (isOutside[i])
607           {
608             isTargetOutside = true;
609           }
610       }
611
612     if (!isTargetStrictlyOutside)
613       {
614
615         if (isTargetOutside)
616           {
617             // Faces are parallel
618             const int tetraFacesNodesConn[4][3] = {
619                 { 0, 1, 2 },
620                 { 0, 2, 3 },
621                 { 0, 3, 1 },
622                 { 1, 2, 3 } };
623             double planeNormal[3];
624             for (int iTetraFace = 0; iTetraFace < 4; ++iTetraFace)
625               {
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())
631                   {
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.))
638                       {
639                         for (int i = 0; i < 3; ++i)
640                           {
641                             planeNormal[i] = 0.;
642                           }
643                       }
644                     else
645                       {
646                         const double invNormOfTetraTriNormal = 1. / normOfTetraTriNormal;
647                         for (int i = 0; i < 3; ++i)
648                           {
649                             planeNormal[i] *= invNormOfTetraTriNormal;
650                           }
651                       }
652                     double planeConstant = dot(planeNormal, coordsTetraTriNode1);
653                     if (IsFacesCoplanar(planeNormal, planeConstant, polyCoords, precision))
654                       {
655                         int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
656                         for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
657                           {
658                             double volume = CalculateIntersectionSurfaceOfCoplanarTriangles(planeNormal,
659                                                                                             planeConstant,
660                                                                                             polyCoords[0],
661                                                                                             polyCoords[1 + iTri],
662                                                                                             polyCoords[2 + iTri],
663                                                                                             coordsTetraTriNode1,
664                                                                                             coordsTetraTriNode2,
665                                                                                             coordsTetraTriNode3,
666                                                                                             dimCaracteristic,
667                                                                                             precision);
668                             if (!epsilonEqual(volume, 0.))
669                               {
670                                 totalSurface += volume;
671                                 listOfTetraFacesColinear.insert(key);
672                               }
673                           }
674                       }
675                   }
676                 listOfTetraFacesTreated.insert(key);
677               }
678           }
679         else
680           {
681               // intersect a son with the unit tetra
682               switch (polyType)
683                 {
684                 case NORM_TRI3:
685                   {
686                     // create the face key
687                     TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
688
689                     // calculate the triangle if needed
690                     if (_volumes.find(key) == _volumes.end())
691                       {
692                         TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
693                         calculateSurface(tri, key);
694                         totalSurface += _volumes[key];
695                       }
696                     else
697                       {
698                         // count negative as face has reversed orientation
699                         totalSurface -= _volumes[key];
700                       }
701                   }
702                   break;
703
704                 case NORM_QUAD4:
705
706                   // simple triangulation of faces along a diagonal :
707                   //
708                   // 2 ------ 3
709                   // |      / |
710                   // |     /  |
711                   // |    /   |
712                   // |   /    |
713                   // |  /     |
714                   // | /      |
715                   // 1 ------ 4
716                   //
717                   //? not sure if this always works
718                   {
719                     // calculate the triangles if needed
720
721                     // local nodes 1, 2, 3
722                     TriangleFaceKey key1 = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
723                     if (_volumes.find(key1) == _volumes.end())
724                       {
725                         TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
726                         calculateSurface(tri, key1);
727                         totalSurface += _volumes[key1];
728                       }
729                     else
730                       {
731                         // count negative as face has reversed orientation
732                         totalSurface -= _volumes[key1];
733                       }
734
735                     // local nodes 1, 3, 4
736                     TriangleFaceKey key2 = TriangleFaceKey(polyNodes[0], polyNodes[2], polyNodes[3]);
737                     if (_volumes.find(key2) == _volumes.end())
738                       {
739                         TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[2]], _nodes[polyNodes[3]]);
740                         calculateSurface(tri, key2);
741                         totalSurface += _volumes[key2];
742                       }
743                     else
744                       {
745                         // count negative as face has reversed orientation
746                         totalSurface -= _volumes[key2];
747                       }
748                   }
749                   break;
750
751                 case NORM_POLYGON:
752                   {
753                     int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
754                     for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
755                       {
756                         TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1 + iTri], polyNodes[2 + iTri]);
757                         if (_volumes.find(key) == _volumes.end())
758                           {
759                             TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1 + iTri]],
760                                 _nodes[polyNodes[2 + iTri]]);
761                             calculateSurface(tri, key);
762                             totalSurface += _volumes[key];
763                           }
764                         else
765                           {
766                             totalSurface -= _volumes[key];
767                           }
768                       }
769                   }
770                   break;
771
772                 default:
773                   std::cout
774                       << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment."
775                       << std::endl;
776                   assert(false);
777                 }
778
779           }
780       }
781
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))
785       {
786         totalSurface = 0.0;
787       }
788     
789     LOG(2, "Volume = " << totalSurface << ", det= " << _t->determinant());
790
791     return totalSurface;
792   }
793
794   /**
795    * Calculates the volume of intersection of this tetrahedron with another one.
796    */
797   template<class MyMeshType>
798   double SplitterTetra<MyMeshType>::intersectTetra(const double** tetraCorners)
799   {
800     //{ could be done on outside?
801     // check if we have planar tetra element
802     if(_t->determinant() == 0.0)
803     {
804       // tetra is planar
805       LOG(2, "Planar tetra -- volume 0");
806       return 0.0;
807     }
808
809     const unsigned nbOfNodes4Type=4;
810     // halfspace filtering
811     bool isOutside[8] = {true, true, true, true, true, true, true, true};
812     bool isTargetOutside = false;
813
814     // calculate the transformed coordinates of the nodes
815     double nodes[nbOfNodes4Type][3];
816     for(int i = 0;i<(int)nbOfNodes4Type;++i)
817     {
818       _t->apply(nodes[i], tetraCorners[i]);
819       checkIsOutside(nodes[i], isOutside);
820     }
821
822     // halfspace filtering check
823     // NB : might not be beneficial for caching of triangles
824     for(int i = 0; i < 8; ++i)
825     {
826       if(isOutside[i])
827       {
828         isTargetOutside = true;
829       }
830     }
831
832     double totalVolume = 0.0;
833
834     if(!isTargetOutside)
835     {
836       const CellModel& cellModelCell=CellModel::GetCellModel(NORM_TETRA4);
837       int cellNodes[4] = { 0, 1, 2, 3 }, faceNodes[3];
838
839       for(unsigned ii = 0 ; ii < 4 ; ++ii)
840       {
841         cellModelCell.fillSonCellNodalConnectivity(ii,cellNodes,faceNodes);
842         
843         TransformedTriangle tri(nodes[faceNodes[0]], nodes[faceNodes[1]], nodes[faceNodes[2]]);
844         double vol = tri.calculateIntersectionVolume();
845         totalVolume += vol;
846       }
847       
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))
851       {
852         totalVolume = 0.0;
853       }
854     }
855     LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
856
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) ;
860   }
861
862   ////////////////////////////////////////////////////////
863
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)
867   {
868   }
869
870   template<class MyMeshTypeT, class MyMeshTypeS>
871   SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::~SplitterTetra2()
872   {
873     releaseArrays();
874   }
875
876   template<class MyMeshTypeT, class MyMeshTypeS>
877   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::releaseArrays()
878   {
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)
882       {
883         std::vector<const double*>::iterator iter = _nodes.begin() + /*8*/nbOfNodesT;
884         while(iter != _nodes.end())
885           {
886             delete[] *iter;
887             ++iter;
888           }
889       }
890     _nodes.clear();
891   }
892
893   /*!
894    * @param targetCell in C mode.
895    * @param tetra is the output result tetra containers.
896    */
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)
901   {
902     typedef typename MyMeshTypeT::MyConnType ConnType;
903     const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
904     const int numTetra = static_cast<int>(_splitting_pol);
905     if(nbOfNodesT==4)
906       {
907         _nodes.resize(8);
908         _node_ids.resize(8);
909         tetra.reserve(1);
910         const double *nodes[4];
911         int conn[4];
912         for(int node = 0; node < 4 ; ++node)
913           {
914             nodes[node]=getCoordsOfNode2(node, OTT<ConnType,numPol>::indFC(targetCell),_target_mesh,conn[node]);
915           }
916         std::copy(conn,conn+4,_node_ids.begin());
917         SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
918         tetra.push_back(t);
919         return ;
920       }
921     // Issue 0020634. To pass nbOfNodesT to calculateSubNodes (don't want to add an arg)
922     _node_ids.resize(nbOfNodesT);
923
924     // pre-calculate nodes
925     calculateSubNodes(_target_mesh, OTT<ConnType,numPol>::indFC(targetCell));
926
927     tetra.reserve(numTetra);
928     _nodes.reserve(30); // we never have more than this
929
930     switch ( nbOfNodesT )
931       {
932       case 8:
933         {
934           switch(_splitting_pol)
935             {
936             case PLANAR_FACE_5:
937               {
938                 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
939                 fiveSplit(subZone,tetra);
940               }
941               break;
942
943             case PLANAR_FACE_6:
944               {
945                 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
946                 sixSplit(subZone,tetra);
947               }
948               break;
949
950             case GENERAL_24:
951               {
952                 calculateGeneral24Tetra(tetra);
953               }
954               break;
955
956             case GENERAL_48:
957               {
958                 calculateGeneral48Tetra(tetra);
959               }
960               break;
961             default:
962               assert(false);
963             }
964           break;
965         }
966       case 5:
967         {
968           splitPyram5(tetra);
969           break;
970         }
971       default:
972         {
973           splitConvex(targetCell, tetra);
974         }
975       }
976   }
977
978   /**
979    * Splits the hexahedron into five tetrahedra.
980    * This method adds five SplitterTetra objects to the vector tetra. 
981    *
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
984    */
985   template<class MyMeshTypeT, class MyMeshTypeS>
986   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::fiveSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
987   { 
988     // create tetrahedra
989     for(int i = 0; i < 5; ++i)
990       {
991         const double* nodes[4];
992         int conn[4];
993         for(int j = 0; j < 4; ++j)
994           {
995             conn[j] = subZone[ SPLIT_NODES_5[4*i+j] ];
996             nodes[j] = getCoordsOfSubNode(conn[j]);
997           }
998         SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
999         tetra.push_back(t);
1000       }
1001   }
1002
1003   /**
1004    * Splits the hexahedron into six tetrahedra.
1005    * This method adds six SplitterTetra objects to the vector tetra. 
1006    *
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
1009    */
1010   template<class MyMeshTypeT, class MyMeshTypeS>
1011   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::sixSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1012   {
1013     for(int i = 0; i < 6; ++i)
1014       {
1015         const double* nodes[4];
1016         int conn[4];
1017         for(int j = 0; j < 4; ++j)
1018           {
1019             conn[j] = subZone[SPLIT_NODES_6[4*i+j]];
1020             nodes[j] = getCoordsOfSubNode(conn[j]);
1021           }
1022         SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1023         tetra.push_back(t);
1024       }
1025   }
1026
1027   /**
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.
1032    * 
1033    */
1034   template<class MyMeshTypeT, class MyMeshTypeS>
1035   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral24Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1036   {
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
1040     
1041     // nodes to use for tetrahedron
1042     const double* nodes[4];
1043     int conn[4];
1044     // get the cell center
1045     conn[0] = 14;
1046     nodes[0] = getCoordsOfSubNode(conn[0]);
1047
1048     for(int faceCenterNode = 8; faceCenterNode < 14; ++faceCenterNode)
1049       {
1050         // get the face center
1051         conn[1] = faceCenterNode;
1052         nodes[1] = getCoordsOfSubNode(conn[1]);
1053         for(int j = 0; j < 4; ++j)
1054           {
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]);
1060
1061             SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes, conn);
1062             tetra.push_back(t);
1063           }
1064       }
1065   }
1066
1067
1068   /**
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().
1074    * 
1075    */
1076   template<class MyMeshTypeT, class MyMeshTypeS>
1077   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral48Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1078   {
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] = 
1083       {
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
1092       };
1093     
1094     for(int i = 0; i < 8; ++i)
1095       {
1096         sixSplit(&subZones[8*i],tetra);
1097       }
1098   }
1099   
1100   /**
1101    * Splits the NORM_PYRA5 into 2 tetrahedra.
1102    */
1103   template<class MyMeshTypeT, class MyMeshTypeS>
1104   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitPyram5(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1105   {
1106     static const int SPLIT_PYPA5[2][4] = 
1107       {
1108         {
1109           0, 1, 2, 4
1110         },
1111         {
1112           0, 2, 3, 4
1113         }
1114       };
1115     
1116     // create tetrahedra
1117     const double* nodes[4];
1118     int conn[4];
1119     for(int i = 0; i < 2; ++i)
1120       {
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);
1124         tetra.push_back(t);
1125       }
1126   }
1127   
1128   /**
1129    * Splits a convex cell into tetrahedra.
1130    */
1131   template<class MyMeshTypeT, class MyMeshTypeS>
1132   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitConvex(typename MyMeshTypeT::MyConnType targetCell,
1133                                                              typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1134   {
1135     // Each face of a cell is split into triangles and
1136     // each of triangles and a cell barycenter form a tetrahedron.
1137
1138     typedef typename MyMeshTypeT::MyConnType ConnType;
1139     const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
1140
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();
1145
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);
1150
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];
1157
1158     // nodes of tetrahedron
1159     int conn[4];
1160     const double* nodes[4];
1161     nodes[3] = getCoordsOfSubNode2( nbOfCellNodes,conn[3]); // barycenter
1162
1163     for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
1164       {
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);
1170
1171         int nbTetra = nbFaceNodes - 2; // split polygon into nbTetra triangles
1172
1173         // create tetrahedra
1174         for(int i = 0; i < nbTetra; ++i)
1175           {
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);
1180             tetra.push_back(t);
1181           }
1182
1183         if ( normCellType == NORM_POLYHED )
1184           faceNodes += nbFaceNodes; // go to the next face
1185       }
1186   }
1187   
1188   /**
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.
1193    *
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
1197    *
1198    */
1199   template<class MyMeshTypeT, class MyMeshTypeS>
1200   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateSubNodes(const MyMeshTypeT& targetMesh, typename MyMeshTypeT::MyConnType targetCell)
1201   {
1202     // retrieve real mesh nodes
1203     
1204     typename MyMeshTypeT::MyConnType nbOfNodesT = _node_ids.size();// Issue 0020634. _node_ids.resize(8);
1205     for(int node = 0; node < nbOfNodesT ; ++node)
1206       {
1207         // calculate only normal nodes
1208         _nodes.push_back(getCoordsOfNode2(node, targetCell, targetMesh,_node_ids[node]));
1209       }
1210
1211     switch ( nbOfNodesT )
1212       {
1213       case 8:
1214
1215         // create sub-mesh nodes if needed
1216         switch(_splitting_pol)
1217           {
1218           case GENERAL_24:
1219             {
1220               for(int i = 0; i < 7; ++i)
1221                 {
1222                   double* barycenter = new double[3];
1223                   calcBarycenter(4, barycenter, &GENERAL_24_SUB_NODES[4*i]);
1224                   _nodes.push_back(barycenter);
1225                 }
1226             }
1227             break;
1228
1229           case GENERAL_48:
1230             {
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] = 
1236                 {
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)
1256                 };
1257
1258               for(int i = 0; i < 19; ++i)
1259                 {
1260                   double* barycenter = new double[3];
1261                   calcBarycenter(2, barycenter, &GENERAL_48_SUB_NODES[2*i]);
1262                   _nodes.push_back(barycenter);
1263                 }
1264             }
1265             break;
1266
1267           default:
1268             break;
1269           }
1270
1271       case 5: // NORM_PYRA5
1272         break;
1273
1274       default: // convex 3d cell
1275         {
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);
1282         }
1283       }
1284
1285   }
1286 }
1287
1288 #endif