Salome HOME
Start debugging 3D interpolation error on OCTA12 in target mesh
[modules/med.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   template<class MyMeshType> 
42   const double SplitterTetra<MyMeshType>::SPARSE_TRUNCATION_LIMIT=1.0e-14;
43
44   /*!
45    * output is expected to be allocated with 24*sizeof(void*) in order to store the 24 tetras.
46    * These tetras have to be deallocated.
47    */
48   template<class MyMeshType>
49   void SplitterTetra<MyMeshType>::splitIntoDualCells(SplitterTetra<MyMeshType> **output)
50   {
51     double tmp[12];
52     const double *tmp2[4]={tmp,tmp+3,tmp+6,tmp+9};
53     typename MyMeshType::MyConnType conn[4]={-1,-1,-1,-1};
54     for(int i=0;i<24;i++)
55       {
56         splitMySelfForDual(tmp,i,conn[0]);
57         output[i]=new SplitterTetra<MyMeshType>(_src_mesh,tmp2,conn);
58       }
59   }
60
61   /**
62    * SplitterTetra class computes for a list of cell ids of a given mesh \a srcMesh (badly named) the intersection with a 
63    * single TETRA4 cell given by \a tetraCorners (of length 4) and \a nodesId (of length 4 too). \a nodedIds is given only to establish
64    * if a partial computation of a triangle has already been performed (to increase performance).
65    *
66    * The \a srcMesh can contain polyhedron cells.
67    * 
68    * 
69    * Constructor creating object from the four corners of the tetrahedron.
70    *
71    * @param srcMesh       mesh containing the source elements
72    * @param tetraCorners  array of four pointers to double[3] arrays containing the coordinates of the
73    *                      corners of the tetrahedron
74    */
75   template<class MyMeshType>
76   SplitterTetra<MyMeshType>::SplitterTetra(const MyMeshType& srcMesh, const double** tetraCorners, const typename MyMeshType::MyConnType *nodesId)
77     : _t(0), _src_mesh(srcMesh)
78   {
79     std::copy(nodesId,nodesId+4,_conn);
80     _coords[0]=tetraCorners[0][0]; _coords[1]=tetraCorners[0][1]; _coords[2]=tetraCorners[0][2];
81     _coords[3]=tetraCorners[1][0]; _coords[4]=tetraCorners[1][1]; _coords[5]=tetraCorners[1][2];
82     _coords[6]=tetraCorners[2][0]; _coords[7]=tetraCorners[2][1]; _coords[8]=tetraCorners[2][2];
83     _coords[9]=tetraCorners[3][0]; _coords[10]=tetraCorners[3][1]; _coords[11]=tetraCorners[3][2];
84     // create the affine transform
85     _t=new TetraAffineTransform(_coords);
86   }
87   
88   /**
89    * Destructor
90    *
91    * Deletes _t and the coordinates (double[3] vectors) in _nodes
92    *
93    */
94   template<class MyMeshType>
95   SplitterTetra<MyMeshType>::~SplitterTetra()
96   {
97     delete _t;
98     for(HashMap< int, double* >::iterator iter = _nodes.begin(); iter != _nodes.end() ; ++iter)
99       delete[] iter->second;
100   }
101
102   /*!
103    * \Forget already calculated triangles, which is crucial for calculation of barycenter of intersection
104    */
105   template<class MyMeshType>
106   void SplitterTetra<MyMeshType>::clearVolumesCache()
107   {
108     _volumes.clear();
109   }
110
111   /*!
112    * This method destroys the 4 pointers pointed by tetraCorners[0],tetraCorners[1],tetraCorners[2] and tetraCorners[3]
113    * @param i is in 0..23 included.
114    * @param output is expected to be sized of 12 in order to.
115    */
116   template<class MyMeshType>
117   void SplitterTetra<MyMeshType>::splitMySelfForDual(double* output, int i, typename MyMeshType::MyConnType& nodeId)
118   {
119     double *tmp[4];
120     int offset=i/6;
121     nodeId=_conn[offset];
122     tmp[0]=_coords+3*offset; tmp[1]=_coords+((offset+1)%4)*3; tmp[2]=_coords+((offset+2)%4)*3; tmp[3]=_coords+((offset+3)%4)*3;
123     int caseToTreat=i%6;
124     int case1=caseToTreat/2;
125     int case2=caseToTreat%2;
126     const int tab[3][2]={{1,2},{3,2},{1,3}};
127     const int *curTab=tab[case1];
128     double pt0[3]; pt0[0]=(tmp[curTab[case2]][0]+tmp[0][0])/2.; pt0[1]=(tmp[curTab[case2]][1]+tmp[0][1])/2.; pt0[2]=(tmp[curTab[case2]][2]+tmp[0][2])/2.;
129     double pt1[3]; pt1[0]=(tmp[0][0]+tmp[curTab[0]][0]+tmp[curTab[1]][0])/3.; pt1[1]=(tmp[0][1]+tmp[curTab[0]][1]+tmp[curTab[1]][1])/3.; pt1[2]=(tmp[0][2]+tmp[curTab[0]][2]+tmp[curTab[1]][2])/3.;
130     double pt2[3]; pt2[0]=(tmp[0][0]+tmp[1][0]+tmp[2][0]+tmp[3][0])/4.; pt2[1]=(tmp[0][1]+tmp[1][1]+tmp[2][1]+tmp[3][1])/4.; pt2[2]=(tmp[0][2]+tmp[1][2]+tmp[2][2]+tmp[3][2])/4.;
131     std::copy(pt1,pt1+3,output+case2*3);
132     std::copy(pt0,pt0+3,output+(abs(case2-1))*3);
133     std::copy(pt2,pt2+3,output+2*3);
134     std::copy(tmp[0],tmp[0]+3,output+3*3);
135   }
136   
137   /**
138    * Calculates the volume of intersection of an element in the source mesh and the target element.
139    * It first calculates the transformation that takes the target tetrahedron into the unit tetrahedron. After that, the 
140    * faces of the source element are triangulated and the calculated transformation is applied 
141    * to each triangle. The algorithm of Grandy, implemented in INTERP_KERNEL::TransformedTriangle is used
142    * to calculate the contribution to the volume from each triangle. The volume returned is the sum of these contributions
143    * divided by the determinant of the transformation.
144    *
145    * The class will cache the intermediary calculations of transformed nodes of source cells and volumes associated 
146    * with triangulated faces to avoid having to recalculate these.
147    *
148    * @param element      global number of the source element in C mode.
149    */
150   template<class MyMeshType>
151   double SplitterTetra<MyMeshType>::intersectSourceCell(typename MyMeshType::MyConnType element,
152                                                         double*                         baryCentre)
153   {
154     typedef typename MyMeshType::MyConnType ConnType;
155     const NumberingPolicy numPol=MyMeshType::My_numPol;
156     //{ could be done on outside?
157     // check if we have planar tetra element
158     if(_t->determinant() == 0.0)
159       {
160         // tetra is planar
161         LOG(2, "Planar tetra -- volume 0");
162         return 0.0;
163       }
164
165     // get type of cell
166     NormalizedCellType normCellType=_src_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(element));
167     const CellModel& cellModelCell=CellModel::GetCellModel(normCellType);
168     unsigned nbOfNodes4Type=cellModelCell.isDynamic() ? _src_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(element)) : cellModelCell.getNumberOfNodes();
169     // halfspace filtering
170     bool isOutside[8] = {true, true, true, true, true, true, true, true};
171     bool isTargetOutside = false;
172
173     // calculate the coordinates of the nodes
174     int *cellNodes=new int[nbOfNodes4Type];
175     for(int i = 0;i<(int)nbOfNodes4Type;++i)
176       {
177         // we could store mapping local -> global numbers too, but not sure it is worth it
178         const int globalNodeNum = getGlobalNumberOfNode(i, OTT<ConnType,numPol>::indFC(element), _src_mesh);
179         cellNodes[i]=globalNodeNum;
180         if(_nodes.find(globalNodeNum) == _nodes.end()) 
181           {
182             //for(HashMap< int , double* >::iterator iter3=_nodes.begin();iter3!=_nodes.end();iter3++)
183             //  std::cout << (*iter3).first << " ";
184             //std::cout << std::endl << "*** " << globalNodeNum << std::endl;
185             calculateNode(globalNodeNum);
186           }
187
188         checkIsOutside(_nodes[globalNodeNum], isOutside);       
189       }
190
191     // halfspace filtering check
192     // NB : might not be beneficial for caching of triangles
193     for(int i = 0; i < 8; ++i)
194       {
195         if(isOutside[i])
196           {
197             isTargetOutside = true;
198           }
199       }
200
201     double totalVolume = 0.0;
202
203     if(!isTargetOutside)
204       {
205         /// calculator of intersection barycentre
206         UnitTetraIntersectionBary baryCalculator( _t->determinant() < 0.);
207
208         // get nb of sons of a cell
209         const ConnType* rawCellConn = _src_mesh.getConnectivityPtr() + OTT<ConnType,numPol>::conn2C( _src_mesh.getConnectivityIndexPtr()[ element ]);
210         const int rawNbCellNodes = _src_mesh.getConnectivityIndexPtr()[ element+1 ] - _src_mesh.getConnectivityIndexPtr()[ element ];
211         unsigned nbOfSons = cellModelCell.getNumberOfSons2(rawCellConn, rawNbCellNodes);
212
213         for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
214           {
215             // get sons connectivity
216             NormalizedCellType faceType;
217             int *faceNodes, nbFaceNodes=-1;
218             if ( cellModelCell.isDynamic() )
219               {
220                 faceNodes=new int[nbOfNodes4Type];
221                 nbFaceNodes = cellModelCell.fillSonCellNodalConnectivity2(ii,rawCellConn,rawNbCellNodes,faceNodes,faceType);
222                 for ( int i = 0; i < nbFaceNodes; ++i )
223                   faceNodes[i] = OTT<ConnType,numPol>::coo2C(faceNodes[i]);
224               }
225             else
226               {
227                 faceType = cellModelCell.getSonType(ii);
228                 const CellModel& faceModel=CellModel::GetCellModel(faceType);
229                 assert(faceModel.getDimension() == 2);
230                 faceNodes=new int[faceModel.getNumberOfNodes()];      
231                 cellModelCell.fillSonCellNodalConnectivity(ii,cellNodes,faceNodes);
232               }
233             // intersect a son with the unit tetra
234             switch(faceType)
235               {
236               case NORM_TRI3:
237                 {
238                   // create the face key
239                   TriangleFaceKey key = TriangleFaceKey(faceNodes[0], faceNodes[1], faceNodes[2]);
240
241                   // calculate the triangle if needed
242                   if(_volumes.find(key) == _volumes.end())
243                     {
244                       TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1]], _nodes[faceNodes[2]]);
245                       calculateVolume(tri, key);
246                       totalVolume += _volumes[key];
247                       if ( baryCentre )
248                         baryCalculator.addSide( tri );
249                     } else {    
250                       // count negative as face has reversed orientation
251                       totalVolume -= _volumes[key];
252                     }
253                 }
254                 break;
255
256               case NORM_QUAD4:
257
258                 // simple triangulation of faces along a diagonal :
259                 //
260                 // 2 ------ 3
261                 // |      / |
262                 // |     /  |
263                 // |    /   |
264                 // |   /    |
265                 // |  /     |
266                 // | /      |
267                 // 1 ------ 4
268                 //
269                 //? not sure if this always works 
270                 {
271                   // calculate the triangles if needed
272
273                   // local nodes 1, 2, 3
274                   TriangleFaceKey key1 = TriangleFaceKey(faceNodes[0], faceNodes[1], faceNodes[2]);
275                   if(_volumes.find(key1) == _volumes.end())
276                     {
277                       TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1]], _nodes[faceNodes[2]]);
278                       calculateVolume(tri, key1);
279                       totalVolume += _volumes[key1];
280                     } else {
281                       // count negative as face has reversed orientation
282                       totalVolume -= _volumes[key1];
283                     }
284
285                   // local nodes 1, 3, 4
286                   TriangleFaceKey key2 = TriangleFaceKey(faceNodes[0], faceNodes[2], faceNodes[3]);
287                   if(_volumes.find(key2) == _volumes.end())
288                     {
289                       TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[2]], _nodes[faceNodes[3]]);
290                       calculateVolume(tri, key2);
291                       totalVolume += _volumes[key2];
292                     }
293                   else
294                     { 
295                       // count negative as face has reversed orientation
296                       totalVolume -= _volumes[key2];
297                     }
298                 }
299                 break;
300
301               case NORM_POLYGON:
302                 {
303                   int nbTria = nbFaceNodes - 2; // split polygon into nbTria triangles
304                   for ( int iTri = 0; iTri < nbTria; ++iTri )
305                     {
306                       TriangleFaceKey key = TriangleFaceKey(faceNodes[0], faceNodes[1+iTri], faceNodes[2+iTri]);
307                       if(_volumes.find(key) == _volumes.end())
308                         {
309                           TransformedTriangle tri(_nodes[faceNodes[0]], _nodes[faceNodes[1+iTri]], _nodes[faceNodes[2+iTri]]);
310                           calculateVolume(tri, key);
311                           totalVolume += _volumes[key];
312                         }
313                       else
314                         {
315                           totalVolume -= _volumes[key];
316                         }
317                     }
318                 }
319                 break;
320
321               default:
322                 std::cout << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment." << std::endl;
323                 assert(false);
324               }
325             delete [] faceNodes;
326           }
327
328         if ( baryCentre ) {
329           baryCalculator.getBary( baryCentre );
330           _t->reverseApply( baryCentre, baryCentre );
331         }
332       }
333     delete [] cellNodes;
334     // reset if it is very small to keep the matrix sparse
335     // is this a good idea?
336     if(epsilonEqual(totalVolume, 0.0, SPARSE_TRUNCATION_LIMIT))
337       {
338         totalVolume = 0.0;
339       }
340
341     LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
342
343     // NB : fault in article, Grandy, [8] : it is the determinant of the inverse transformation
344     // that should be used (which is equivalent to dividing by the determinant)
345     return std::fabs(1.0 / _t->determinant() * totalVolume) ;
346   }
347
348   /**
349    * Calculates the intersection surface of two coplanar triangles.
350    *
351    * @param palneNormal normal of the plane for the first triangle
352    * @param planeConstant constant of the equation of the plane for the first triangle
353    * @param p1 coordinates of the first  node of the first  triangle
354    * @param p2 coordinates of the second node of the first  triangle
355    * @param p3 coordinates of the third  node of the first  triangle
356    * @param p4 coordinates of the first  node of the second triangle
357    * @param p5 coordinates of the second node of the second triangle
358    * @param p6 coordinates of the third  node of the second triangle
359    * @param dimCaracteristic characteristic size of the meshes containing the triangles
360    * @param precision precision for double float data used for comparison
361    */
362   template<class MyMeshType>
363   double SplitterTetra<MyMeshType>::CalculateIntersectionSurfaceOfCoplanarTriangles(const double *const planeNormal,
364                                                                                     const double planeConstant,
365                                                                                     const double *const p1, const double *const p2, const double *const p3,
366                                                                                     const double *const p4, const double *const p5, const double *const p6,
367                                                                                     const double dimCaracteristic, const double precision)
368   {
369     typedef typename MyMeshType::MyConnType ConnType;
370     typedef double Vect2[2];
371     typedef double Vect3[3];
372     typedef double Triangle2[3][2];
373
374     const double *const tri0[3] = {p1, p2, p3};
375     const double *const tri1[3] = {p4, p5, p6};
376
377     // Plane of the first triangle defined by the normal of the triangle and the constant
378     // Project triangles onto coordinate plane most aligned with plane normal
379     int maxNormal = 0;
380     double fmax = std::abs(planeNormal[0]);
381     double absMax = std::abs(planeNormal[1]);
382     if (absMax > fmax)
383       {
384         maxNormal = 1;
385         fmax = absMax;
386       }
387     absMax = std::abs(planeNormal[2]);
388     if (absMax > fmax)
389       {
390         maxNormal = 2;
391       }
392
393     Triangle2 projTri0, projTri1;
394     int i;
395
396     if (maxNormal == 0)
397       {
398         // Project onto yz-plane.
399         for (i = 0; i < 3; ++i)
400           {
401             projTri0[i][0] = tri0[i][1];
402             projTri0[i][1] = tri0[i][2];
403             projTri1[i][0] = tri1[i][1];
404             projTri1[i][1] = tri1[i][2];
405           }
406       }
407     else if (maxNormal == 1)
408       {
409         // Project onto xz-plane.
410         for (i = 0; i < 3; ++i)
411           {
412             projTri0[i][0] = tri0[i][0];
413             projTri0[i][1] = tri0[i][2];
414             projTri1[i][0] = tri1[i][0];
415             projTri1[i][1] = tri1[i][2];
416           }
417       }
418     else
419       {
420         // Project onto xy-plane.
421         for (i = 0; i < 3; ++i)
422           {
423             projTri0[i][0] = tri0[i][0];
424             projTri0[i][1] = tri0[i][1];
425             projTri1[i][0] = tri1[i][0];
426             projTri1[i][1] = tri1[i][1];
427           }
428       }
429
430     // 2D triangle intersection routines require counterclockwise ordering.
431     Vect2 save;
432     Vect2 edge0;
433     Vect2 edge1;
434     for (int ii = 0; ii < 2; ++ii)
435       {
436         edge0[ii] = projTri0[1][ii] - projTri0[0][ii];
437         edge1[ii] = projTri0[2][ii] - projTri0[0][ii];
438       }
439     if ((edge0[0] * edge1[1] - edge0[1] * edge1[0]) < (double) 0.)
440       {
441         // Triangle is clockwise, reorder it.
442         for (int ii = 0; ii < 2; ++ii)
443           {
444             save[ii] = projTri0[1][ii];
445             projTri0[1][ii] = projTri0[2][ii];
446             projTri0[2][ii] = save[ii];
447           }
448       }
449
450     for (int ii = 0; ii < 2; ++ii)
451       {
452         edge0[ii] = projTri1[1][ii] - projTri1[0][ii];
453         edge1[ii] = projTri1[2][ii] - projTri1[0][ii];
454       }
455     if ((edge0[0] * edge1[1] - edge0[1] * edge1[0]) < (double) 0.)
456       {
457         // Triangle is clockwise, reorder it.
458       for (int ii = 0; ii < 2; ++ii)
459         {
460           save[ii] = projTri1[1][ii];
461           projTri1[1][ii] = projTri1[2][ii];
462           projTri1[2][ii] = save[ii];
463         }
464       }
465
466     std::vector<double> inter2;
467     intersec_de_triangle(projTri0[0], projTri0[1], projTri0[2],
468                          projTri1[0], projTri1[1], projTri1[2],
469                          inter2,
470                          dimCaracteristic, precision);
471     ConnType nb_inter=((ConnType)inter2.size())/2;
472     double surface = 0.;
473     if(nb_inter >3) inter2=reconstruct_polygon(inter2);
474     if (nb_inter > 0)
475       {
476         std::vector<double> inter3;
477         inter3.resize(3 * nb_inter);
478         // Map 2D intersections back to the 3D triangle space.
479         if (maxNormal == 0)
480           {
481             double invNX = ((double) 1.) / planeNormal[0];
482             for (i = 0; i < nb_inter; i++)
483               {
484                 inter3[3 * i + 1] = inter2[2 * i];
485                 inter3[3 * i + 2] = inter2[2 * i + 1];
486                 inter3[3 * i] = invNX * (planeConstant - planeNormal[1] * inter3[3 * i + 1] - planeNormal[2] * inter3[3 * i + 2]);
487               }
488           }
489         else if (maxNormal == 1)
490           {
491             double invNY = ((double) 1.) / planeNormal[1];
492             for (i = 0; i < nb_inter; i++)
493               {
494                 inter3[3 * i] = inter2[2 * i];
495                 inter3[3 * i + 2] = inter2[2 * i + 1];
496                 inter3[3 * i + 1] = invNY * (planeConstant - planeNormal[0] * inter3[3 * i] - planeNormal[2] * inter3[3 * i + 2]);
497               }
498           }
499         else
500           {
501             double invNZ = ((double) 1.) / planeNormal[2];
502             for (i = 0; i < nb_inter; i++)
503               {
504                 inter3[3 * i] = inter2[2 * i];
505                 inter3[3 * i + 1] = inter2[2 * i + 1];
506                 inter3[3 * i + 2] = invNZ * (planeConstant - planeNormal[0] * inter3[3 * i] - planeNormal[1] * inter3[3 * i + 1]);
507               }
508           }
509         surface = polygon_area<3>(inter3);
510       }
511     return surface;
512   }
513
514   /**
515    * Determine if a face is coplanar with a triangle.
516    * The first face is characterized by the equation of her plane
517    *
518    * @param palneNormal normal of the plane for the first triangle
519    * @param planeConstant constant of the equation of the plane for the first triangle
520    * @param coordsFace coordinates of the triangle face
521    * @param precision precision for double float data used for comparison
522    */
523   template<class MyMeshType>
524   bool SplitterTetra<MyMeshType>::IsFacesCoplanar(const double *const planeNormal,
525                                                   const double planeConstant,
526                                                   const double *const *const coordsFace,
527                                                   const double precision)
528   {
529       // Compute the signed distances of triangle vertices to the plane. Use an epsilon-thick plane test.
530       // For faces not left
531       int counter = 0;
532       for (int i = 0; i < 3; ++i)
533         {
534           const double distance = dot(planeNormal, coordsFace[i]) - planeConstant;
535           if (epsilonEqual(distance, precision))
536             {
537               counter++;
538             }
539         }
540       return counter == 3;
541   }
542
543   /**
544    * Calculates the surface of intersection of a polygon face in the source mesh and a cell of the target mesh.
545    * It first calculates the transformation that takes the target tetrahedron into the unit tetrahedron. After that, the
546    * faces of the source element are triangulated and the calculated transformation is applied
547    * to each triangle.
548    * The algorithm is based on the algorithm of Grandy used in intersectSourceCell to compute
549    * the volume of intersection of two cell elements.
550    * The case with a source face colinear to one of the face of tetrahedrons is taking into account:
551    * the contribution of the face must not be counted two times.
552    *
553    * The class will cache the intermediary calculations of transformed nodes of source faces and surfaces associated
554    * with triangulated faces to avoid having to recalculate these.
555    *
556    * @param polyType type of the polygon source face
557    * @param polyNodesNbr number of the nodes of the polygon source face
558    * @param polyNodes numbers of the nodes of the polygon source face
559    * @param polyCoords coordinates of the nodes of the polygon source face
560    * @param dimCaracteristic characteristic size of the meshes containing the triangles
561    * @param precision precision for double float data used for comparison
562    * @param listOfTetraFacesTreated list of tetra faces treated
563    * @param listOfTetraFacesColinear list of tetra faces colinear with the polygon source faces
564    */
565   template<class MyMeshType>
566   double SplitterTetra<MyMeshType>::intersectSourceFace(const NormalizedCellType polyType,
567                                                         const int polyNodesNbr,
568                                                         const int *const polyNodes,
569                                                         const double *const *const polyCoords,
570                                                         const double dimCaracteristic,
571                                                         const double precision,
572                                                         std::multiset<TriangleFaceKey>& listOfTetraFacesTreated,
573                                                         std::set<TriangleFaceKey>& listOfTetraFacesColinear)
574   {
575     typedef typename MyMeshType::MyConnType ConnType;
576
577     double totalSurface = 0.0;
578
579     // check if we have planar tetra element
580     if(_t->determinant() == 0.0)
581       {
582         // tetra is planar
583         LOG(2, "Planar tetra -- volume 0");
584         return 0.0;
585       }
586
587     // halfspace filtering
588     bool isOutside[8] = {true, true, true, true, true, true, true, true};
589     bool isStrictlyOutside[8] = {true, true, true, true, true, true, true, true};
590     bool isTargetStrictlyOutside = false;
591     bool isTargetOutside = false;
592
593     // calculate the coordinates of the nodes
594     for(int i = 0;i<(int)polyNodesNbr;++i)
595       {
596         const int globalNodeNum = polyNodes[i];
597         if(_nodes.find(globalNodeNum) == _nodes.end())
598           {
599             calculateNode2(globalNodeNum, polyCoords[i]);
600           }
601
602         checkIsStrictlyOutside(_nodes[globalNodeNum], isStrictlyOutside, precision);
603         checkIsOutside(_nodes[globalNodeNum], isOutside, precision);
604       }
605
606     // halfspace filtering check
607     // NB : might not be beneficial for caching of triangles
608     for(int i = 0; i < 8; ++i)
609       {
610         if(isStrictlyOutside[i])
611           {
612             isTargetStrictlyOutside = true;
613             break;
614           }
615         else if (isOutside[i])
616           {
617             isTargetOutside = true;
618           }
619       }
620
621     if (!isTargetStrictlyOutside)
622       {
623
624         if (isTargetOutside)
625           {
626             // Faces are parallel
627             const int tetraFacesNodesConn[4][3] = {
628                 { 0, 1, 2 },
629                 { 0, 2, 3 },
630                 { 0, 3, 1 },
631                 { 1, 2, 3 } };
632             double planeNormal[3];
633             for (int iTetraFace = 0; iTetraFace < 4; ++iTetraFace)
634               {
635                 const int * const tetraFaceNodesConn = tetraFacesNodesConn[iTetraFace];
636                 TriangleFaceKey key = TriangleFaceKey(_conn[tetraFaceNodesConn[0]],
637                                                       _conn[tetraFaceNodesConn[1]],
638                                                       _conn[tetraFaceNodesConn[2]]);
639                 if (listOfTetraFacesTreated.find(key) == listOfTetraFacesTreated.end())
640                   {
641                     const double * const coordsTetraTriNode1 = _coords + tetraFaceNodesConn[0] * MyMeshType::MY_SPACEDIM;
642                     const double * const coordsTetraTriNode2 = _coords + tetraFaceNodesConn[1] * MyMeshType::MY_SPACEDIM;
643                     const double * const coordsTetraTriNode3 = _coords + tetraFaceNodesConn[2] * MyMeshType::MY_SPACEDIM;
644                     calculateNormalForTria(coordsTetraTriNode1, coordsTetraTriNode2, coordsTetraTriNode3, planeNormal);
645                     const double normOfTetraTriNormal = norm(planeNormal);
646                     if (epsilonEqual(normOfTetraTriNormal, 0.))
647                       {
648                         for (int i = 0; i < 3; ++i)
649                           {
650                             planeNormal[i] = 0.;
651                           }
652                       }
653                     else
654                       {
655                         const double invNormOfTetraTriNormal = 1. / normOfTetraTriNormal;
656                         for (int i = 0; i < 3; ++i)
657                           {
658                             planeNormal[i] *= invNormOfTetraTriNormal;
659                           }
660                       }
661                     double planeConstant = dot(planeNormal, coordsTetraTriNode1);
662                     if (IsFacesCoplanar(planeNormal, planeConstant, polyCoords, precision))
663                       {
664                         int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
665                         for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
666                           {
667                             double volume = CalculateIntersectionSurfaceOfCoplanarTriangles(planeNormal,
668                                                                                             planeConstant,
669                                                                                             polyCoords[0],
670                                                                                             polyCoords[1 + iTri],
671                                                                                             polyCoords[2 + iTri],
672                                                                                             coordsTetraTriNode1,
673                                                                                             coordsTetraTriNode2,
674                                                                                             coordsTetraTriNode3,
675                                                                                             dimCaracteristic,
676                                                                                             precision);
677                             if (!epsilonEqual(volume, 0.))
678                               {
679                                 totalSurface += volume;
680                                 listOfTetraFacesColinear.insert(key);
681                               }
682                           }
683                       }
684                   }
685                 listOfTetraFacesTreated.insert(key);
686               }
687           }
688         else
689           {
690               // intersect a son with the unit tetra
691               switch (polyType)
692                 {
693                 case NORM_TRI3:
694                   {
695                     // create the face key
696                     TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
697
698                     // calculate the triangle if needed
699                     if (_volumes.find(key) == _volumes.end())
700                       {
701                         TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
702                         calculateSurface(tri, key);
703                         totalSurface += _volumes[key];
704                       }
705                     else
706                       {
707                         // count negative as face has reversed orientation
708                         totalSurface -= _volumes[key];
709                       }
710                   }
711                   break;
712
713                 case NORM_QUAD4:
714
715                   // simple triangulation of faces along a diagonal :
716                   //
717                   // 2 ------ 3
718                   // |      / |
719                   // |     /  |
720                   // |    /   |
721                   // |   /    |
722                   // |  /     |
723                   // | /      |
724                   // 1 ------ 4
725                   //
726                   //? not sure if this always works
727                   {
728                     // calculate the triangles if needed
729
730                     // local nodes 1, 2, 3
731                     TriangleFaceKey key1 = TriangleFaceKey(polyNodes[0], polyNodes[1], polyNodes[2]);
732                     if (_volumes.find(key1) == _volumes.end())
733                       {
734                         TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1]], _nodes[polyNodes[2]]);
735                         calculateSurface(tri, key1);
736                         totalSurface += _volumes[key1];
737                       }
738                     else
739                       {
740                         // count negative as face has reversed orientation
741                         totalSurface -= _volumes[key1];
742                       }
743
744                     // local nodes 1, 3, 4
745                     TriangleFaceKey key2 = TriangleFaceKey(polyNodes[0], polyNodes[2], polyNodes[3]);
746                     if (_volumes.find(key2) == _volumes.end())
747                       {
748                         TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[2]], _nodes[polyNodes[3]]);
749                         calculateSurface(tri, key2);
750                         totalSurface += _volumes[key2];
751                       }
752                     else
753                       {
754                         // count negative as face has reversed orientation
755                         totalSurface -= _volumes[key2];
756                       }
757                   }
758                   break;
759
760                 case NORM_POLYGON:
761                   {
762                     int nbrPolyTri = polyNodesNbr - 2; // split polygon into nbrPolyTri triangles
763                     for (int iTri = 0; iTri < nbrPolyTri; ++iTri)
764                       {
765                         TriangleFaceKey key = TriangleFaceKey(polyNodes[0], polyNodes[1 + iTri], polyNodes[2 + iTri]);
766                         if (_volumes.find(key) == _volumes.end())
767                           {
768                             TransformedTriangle tri(_nodes[polyNodes[0]], _nodes[polyNodes[1 + iTri]],
769                                 _nodes[polyNodes[2 + iTri]]);
770                             calculateSurface(tri, key);
771                             totalSurface += _volumes[key];
772                           }
773                         else
774                           {
775                             totalSurface -= _volumes[key];
776                           }
777                       }
778                   }
779                   break;
780
781                 default:
782                   std::cout
783                       << "+++ Error : Only elements with triangular and quadratilateral faces are supported at the moment."
784                       << std::endl;
785                   assert(false);
786                 }
787
788           }
789       }
790
791     // reset if it is very small to keep the matrix sparse
792     // is this a good idea?
793     if(epsilonEqual(totalSurface, 0.0, SPARSE_TRUNCATION_LIMIT))
794       {
795         totalSurface = 0.0;
796       }
797     
798     LOG(2, "Volume = " << totalSurface << ", det= " << _t->determinant());
799
800     return totalSurface;
801   }
802
803   /**
804    * Calculates the volume of intersection of this tetrahedron with another one.
805    */
806   template<class MyMeshType>
807   double SplitterTetra<MyMeshType>::intersectTetra(const double** tetraCorners)
808   {
809     //{ could be done on outside?
810     // check if we have planar tetra element
811     if(_t->determinant() == 0.0)
812     {
813       // tetra is planar
814       LOG(2, "Planar tetra -- volume 0");
815       return 0.0;
816     }
817
818     const unsigned nbOfNodes4Type=4;
819     // halfspace filtering
820     bool isOutside[8] = {true, true, true, true, true, true, true, true};
821     bool isTargetOutside = false;
822
823     // calculate the transformed coordinates of the nodes
824     double nodes[nbOfNodes4Type][3];
825     for(int i = 0;i<(int)nbOfNodes4Type;++i)
826     {
827       _t->apply(nodes[i], tetraCorners[i]);
828       checkIsOutside(nodes[i], isOutside);
829     }
830
831     // halfspace filtering check
832     // NB : might not be beneficial for caching of triangles
833     for(int i = 0; i < 8; ++i)
834     {
835       if(isOutside[i])
836       {
837         isTargetOutside = true;
838       }
839     }
840
841     double totalVolume = 0.0;
842
843     if(!isTargetOutside)
844     {
845       const CellModel& cellModelCell=CellModel::GetCellModel(NORM_TETRA4);
846       int cellNodes[4] = { 0, 1, 2, 3 }, faceNodes[3];
847
848       for(unsigned ii = 0 ; ii < 4 ; ++ii)
849       {
850         cellModelCell.fillSonCellNodalConnectivity(ii,cellNodes,faceNodes);
851         
852         TransformedTriangle tri(nodes[faceNodes[0]], nodes[faceNodes[1]], nodes[faceNodes[2]]);
853         double vol = tri.calculateIntersectionVolume();
854         totalVolume += vol;
855       }
856       
857       // reset if it is very small to keep the matrix sparse
858       // is this a good idea?
859       if(epsilonEqual(totalVolume, 0.0, SPARSE_TRUNCATION_LIMIT))
860       {
861         totalVolume = 0.0;
862       }
863     }
864     LOG(2, "Volume = " << totalVolume << ", det= " << _t->determinant());
865
866     // NB : fault in article, Grandy, [8] : it is the determinant of the inverse transformation 
867     // that should be used (which is equivalent to dividing by the determinant)
868     return std::fabs(1.0 / _t->determinant() * totalVolume) ;
869   }
870
871   ////////////////////////////////////////////////////////
872
873   template<class MyMeshTypeT, class MyMeshTypeS>
874   SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::SplitterTetra2(const MyMeshTypeT& targetMesh, const MyMeshTypeS& srcMesh, SplittingPolicy policy)
875     :_target_mesh(targetMesh),_src_mesh(srcMesh),_splitting_pol(policy)
876   {
877   }
878
879   template<class MyMeshTypeT, class MyMeshTypeS>
880   SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::~SplitterTetra2()
881   {
882     releaseArrays();
883   }
884
885   template<class MyMeshTypeT, class MyMeshTypeS>
886   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::releaseArrays()
887   {
888     // free potential sub-mesh nodes that have been allocated
889     typename MyMeshTypeT::MyConnType nbOfNodesT = _node_ids.size();// Issue 0020634.
890     if((int)_nodes.size()>=/*8*/nbOfNodesT)
891       {
892         std::vector<const double*>::iterator iter = _nodes.begin() + /*8*/nbOfNodesT;
893         while(iter != _nodes.end())
894           {
895             delete[] *iter;
896             ++iter;
897           }
898       }
899     _nodes.clear();
900   }
901
902   /*!
903    * @param targetCell in C mode.
904    * @param tetra is the output result tetra containers.
905    */
906   template<class MyMeshTypeT, class MyMeshTypeS>
907   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitTargetCell(typename MyMeshTypeT::MyConnType targetCell,
908                                                                  typename MyMeshTypeT::MyConnType nbOfNodesT,
909                                                                  typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
910   {
911     typedef typename MyMeshTypeT::MyConnType ConnType;
912     const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
913     const int numTetra = static_cast<int>(_splitting_pol);
914     if(nbOfNodesT==4)
915       {
916         _nodes.resize(8);
917         _node_ids.resize(8);
918         tetra.reserve(1);
919         const double *nodes[4];
920         int conn[4];
921         for(int node = 0; node < 4 ; ++node)
922           {
923             nodes[node]=getCoordsOfNode2(node, OTT<ConnType,numPol>::indFC(targetCell),_target_mesh,conn[node]);
924           }
925         std::copy(conn,conn+4,_node_ids.begin());
926         SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
927         tetra.push_back(t);
928         return ;
929       }
930     // Issue 0020634. To pass nbOfNodesT to calculateSubNodes (don't want to add an arg)
931     _node_ids.resize(nbOfNodesT);
932
933     // pre-calculate nodes
934     calculateSubNodes(_target_mesh, OTT<ConnType,numPol>::indFC(targetCell));
935
936     tetra.reserve(numTetra);
937     _nodes.reserve(30); // we never have more than this
938
939     switch ( nbOfNodesT )
940       {
941       case 8:
942         {
943           switch(_splitting_pol)
944             {
945             case PLANAR_FACE_5:
946               {
947                 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
948                 fiveSplit(subZone,tetra);
949               }
950               break;
951
952             case PLANAR_FACE_6:
953               {
954                 const int subZone[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
955                 sixSplit(subZone,tetra);
956               }
957               break;
958
959             case GENERAL_24:
960               {
961                 calculateGeneral24Tetra(tetra);
962               }
963               break;
964
965             case GENERAL_48:
966               {
967                 calculateGeneral48Tetra(tetra);
968               }
969               break;
970             default:
971               assert(false);
972             }
973           break;
974         }
975       case 5:
976         {
977           splitPyram5(tetra);
978           break;
979         }
980       default:
981         {
982           splitConvex(targetCell, tetra);
983         }
984       }
985   }
986
987   /**
988    * Splits the hexahedron into five tetrahedra.
989    * This method adds five SplitterTetra objects to the vector tetra. 
990    *
991    * @param subZone  the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the 
992    *                 splitting to be reused on the subzones of the GENERAL_* types of splitting
993    */
994   template<class MyMeshTypeT, class MyMeshTypeS>
995   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::fiveSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
996   { 
997     // create tetrahedra
998     for(int i = 0; i < 5; ++i)
999       {
1000         const double* nodes[4];
1001         int conn[4];
1002         for(int j = 0; j < 4; ++j)
1003           {
1004             conn[j] = subZone[ SPLIT_NODES_5[4*i+j] ];
1005             nodes[j] = getCoordsOfSubNode(conn[j]);
1006           }
1007         SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1008         tetra.push_back(t);
1009       }
1010   }
1011
1012   /**
1013    * Splits the hexahedron into six tetrahedra.
1014    * This method adds six SplitterTetra objects to the vector tetra. 
1015    *
1016    * @param subZone  the local node numbers corresponding to the hexahedron corners - these are mapped onto {0,..,7}. Providing this allows the 
1017    *                 splitting to be reused on the subzones of the GENERAL_* types of splitting
1018    */
1019   template<class MyMeshTypeT, class MyMeshTypeS>
1020   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::sixSplit(const int* const subZone, typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1021   {
1022     for(int i = 0; i < 6; ++i)
1023       {
1024         const double* nodes[4];
1025         int conn[4];
1026         for(int j = 0; j < 4; ++j)
1027           {
1028             conn[j] = subZone[SPLIT_NODES_6[4*i+j]];
1029             nodes[j] = getCoordsOfSubNode(conn[j]);
1030           }
1031         SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1032         tetra.push_back(t);
1033       }
1034   }
1035
1036   /**
1037    * Splits the hexahedron into 24 tetrahedra.
1038    * The splitting is done by combining the barycenter of the tetrahedron, the barycenter of each face 
1039    * and the nodes of each edge of the face. This creates 6 faces * 4 edges / face = 24 tetrahedra.
1040    * The submesh nodes introduced are the barycenters of the faces and the barycenter of the cell.
1041    * 
1042    */
1043   template<class MyMeshTypeT, class MyMeshTypeS>
1044   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral24Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1045   {
1046     // The two nodes of the original mesh cell used in each tetrahedron.
1047     // The tetrahedra all have nodes (cellCenter, faceCenter, edgeNode1, edgeNode2)
1048     // For the correspondance of the nodes, see the GENERAL_48_SUB_NODES table in calculateSubNodes
1049     
1050     // nodes to use for tetrahedron
1051     const double* nodes[4];
1052     int conn[4];
1053     // get the cell center
1054     conn[0] = 14;
1055     nodes[0] = getCoordsOfSubNode(conn[0]);
1056
1057     for(int faceCenterNode = 8; faceCenterNode < 14; ++faceCenterNode)
1058       {
1059         // get the face center
1060         conn[1] = faceCenterNode;
1061         nodes[1] = getCoordsOfSubNode(conn[1]);
1062         for(int j = 0; j < 4; ++j)
1063           {
1064             const int row = 4*(faceCenterNode - 8) + j;
1065             conn[2] = TETRA_EDGES_GENERAL_24[2*row];
1066             conn[3] = TETRA_EDGES_GENERAL_24[2*row + 1];
1067             nodes[2] = getCoordsOfSubNode(conn[2]);
1068             nodes[3] = getCoordsOfSubNode(conn[3]);
1069
1070             SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes, conn);
1071             tetra.push_back(t);
1072           }
1073       }
1074   }
1075
1076
1077   /**
1078    * Splits the hexahedron into 48 tetrahedra.
1079    * The splitting is done by introducing the midpoints of all the edges 
1080    * and the barycenter of the element as submesh nodes. The 8 hexahedral subzones thus defined
1081    * are then split into 6 tetrahedra each, as in Grandy, p. 449. The division of the subzones 
1082    * is done by calling sixSplit().
1083    * 
1084    */
1085   template<class MyMeshTypeT, class MyMeshTypeS>
1086   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateGeneral48Tetra(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1087   { 
1088     for(int i = 0; i < 8; ++i)
1089       {
1090         sixSplit(GENERAL_48_SUBZONES+8*i,tetra);
1091       }
1092   }
1093   
1094   /**
1095    * Splits the NORM_PYRA5 into 2 tetrahedra.
1096    */
1097   template<class MyMeshTypeT, class MyMeshTypeS>
1098   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitPyram5(typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1099   {
1100     static const int SPLIT_PYPA5[2][4] = 
1101       {
1102         {
1103           0, 1, 2, 4
1104         },
1105         {
1106           0, 2, 3, 4
1107         }
1108       };
1109     
1110     // create tetrahedra
1111     const double* nodes[4];
1112     int conn[4];
1113     for(int i = 0; i < 2; ++i)
1114       {
1115         for(int j = 0; j < 4; ++j)
1116           nodes[j] = getCoordsOfSubNode2(SPLIT_PYPA5[i][j],conn[j]);
1117         SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1118         tetra.push_back(t);
1119       }
1120   }
1121   
1122   /**
1123    * Splits a convex cell into tetrahedra.
1124    */
1125   template<class MyMeshTypeT, class MyMeshTypeS>
1126   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::splitConvex(typename MyMeshTypeT::MyConnType targetCell,
1127                                                              typename std::vector< SplitterTetra<MyMeshTypeS>* >& tetra)
1128   {
1129     // Each face of a cell is split into triangles and
1130     // each of triangles and a cell barycenter form a tetrahedron.
1131
1132     typedef typename MyMeshTypeT::MyConnType ConnType;
1133     const NumberingPolicy numPol=MyMeshTypeT::My_numPol;
1134
1135     // get type of cell and nb of cell nodes
1136     NormalizedCellType normCellType=_target_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(targetCell));
1137     const CellModel& cellModelCell=CellModel::GetCellModel(normCellType);
1138     unsigned nbOfCellNodes=cellModelCell.isDynamic() ? _target_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(targetCell)) : cellModelCell.getNumberOfNodes();
1139
1140     // get nb of cell sons (faces)
1141     const ConnType* rawCellConn = _target_mesh.getConnectivityPtr() + OTT<ConnType,numPol>::conn2C( _target_mesh.getConnectivityIndexPtr()[ targetCell ]);
1142     const int rawNbCellNodes = _target_mesh.getConnectivityIndexPtr()[ targetCell+1 ] - _target_mesh.getConnectivityIndexPtr()[ targetCell ];
1143     unsigned nbOfSons = cellModelCell.getNumberOfSons2(rawCellConn, rawNbCellNodes);
1144
1145     // indices of nodes of a son
1146     static std::vector<int> allNodeIndices; // == 0,1,2,...,nbOfCellNodes-1
1147     while ( allNodeIndices.size() < nbOfCellNodes )
1148       allNodeIndices.push_back( allNodeIndices.size() );
1149     std::vector<int> classicFaceNodes(4);
1150     int* faceNodes = cellModelCell.isDynamic() ? &allNodeIndices[0] : &classicFaceNodes[0];
1151
1152     // nodes of tetrahedron
1153     int conn[4];
1154     const double* nodes[4];
1155     nodes[3] = getCoordsOfSubNode2( nbOfCellNodes,conn[3]); // barycenter
1156
1157     for(unsigned ii = 0 ; ii < nbOfSons; ++ii)
1158       {
1159         // get indices of son's nodes: it's just next portion of allNodeIndices for polyhedron
1160         // and some of allNodeIndices accodring to cell model for a classsic cell 
1161         unsigned nbFaceNodes = cellModelCell.getNumberOfNodesConstituentTheSon2(ii, rawCellConn, rawNbCellNodes);
1162         if ( normCellType != NORM_POLYHED )
1163           cellModelCell.fillSonCellNodalConnectivity(ii,&allNodeIndices[0],faceNodes);
1164
1165         int nbTetra = nbFaceNodes - 2; // split polygon into nbTetra triangles
1166
1167         // create tetrahedra
1168         for(int i = 0; i < nbTetra; ++i)
1169           {
1170             nodes[0] = getCoordsOfSubNode2( faceNodes[0],  conn[0]);
1171             nodes[1] = getCoordsOfSubNode2( faceNodes[1+i],conn[1]);
1172             nodes[2] = getCoordsOfSubNode2( faceNodes[2+i],conn[2]);
1173             SplitterTetra<MyMeshTypeS>* t = new SplitterTetra<MyMeshTypeS>(_src_mesh, nodes,conn);
1174             tetra.push_back(t);
1175           }
1176
1177         if ( normCellType == NORM_POLYHED )
1178           faceNodes += nbFaceNodes; // go to the next face
1179       }
1180   }
1181   
1182   /**
1183    * Precalculates all the nodes.
1184    * Retrieves the mesh nodes and allocates the necessary sub-mesh 
1185    * nodes according to the splitting policy used.
1186    * This method is meant to be called once by the constructor.
1187    *
1188    * @param targetMesh  the target mesh
1189    * @param targetCell  the global number of the cell that the object represents, in targetMesh mode.
1190    * @param policy      the splitting policy of the object
1191    *
1192    */
1193   template<class MyMeshTypeT, class MyMeshTypeS>
1194   void SplitterTetra2<MyMeshTypeT, MyMeshTypeS>::calculateSubNodes(const MyMeshTypeT& targetMesh, typename MyMeshTypeT::MyConnType targetCell)
1195   {
1196     // retrieve real mesh nodes
1197     
1198     typename MyMeshTypeT::MyConnType nbOfNodesT = _node_ids.size();// Issue 0020634. _node_ids.resize(8);
1199     for(int node = 0; node < nbOfNodesT ; ++node)
1200       {
1201         // calculate only normal nodes
1202         _nodes.push_back(getCoordsOfNode2(node, targetCell, targetMesh,_node_ids[node]));
1203       }
1204
1205     switch ( nbOfNodesT )
1206       {
1207       case 8:
1208
1209         // create sub-mesh nodes if needed
1210         switch(_splitting_pol)
1211           {
1212           case GENERAL_24:
1213             {
1214               for(int i = 0; i < 7; ++i)
1215                 {
1216                   double* barycenter = new double[3];
1217                   calcBarycenter(4, barycenter, &GENERAL_24_SUB_NODES[4*i]);
1218                   _nodes.push_back(barycenter);
1219                 }
1220             }
1221             break;
1222
1223           case GENERAL_48:
1224             {
1225               for(int i = 0; i < 19; ++i)
1226                 {
1227                   double* barycenter = new double[3];
1228                   calcBarycenter(2, barycenter, &GENERAL_48_SUB_NODES[2*i]);
1229                   _nodes.push_back(barycenter);
1230                 }
1231             }
1232             break;
1233
1234           default:
1235             break;
1236           }
1237
1238       case 5: // NORM_PYRA5
1239         break;
1240
1241       default: // convex 3d cell
1242         {
1243           // add barycenter of a cell
1244           std::vector<int> allIndices(nbOfNodesT);
1245           for ( int i = 0; i < nbOfNodesT; ++i ) allIndices[i] = i;
1246           double* barycenter = new double[3];
1247           calcBarycenter(nbOfNodesT, barycenter, &allIndices[0]);
1248           _nodes.push_back(barycenter);
1249         }
1250       }
1251
1252   }
1253 }
1254
1255 #endif