Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / INTERP_KERNEL / VolSurfFormulae.hxx
1 // Copyright (C) 2007-2012  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
20 #ifndef __VOLSURFFORMULAE_HXX__
21 #define __VOLSURFFORMULAE_HXX__
22
23 #include "InterpolationUtils.hxx"
24
25 #include <cmath>
26
27 namespace INTERP_KERNEL
28 {
29   inline void calculateBarycenterDyn(const double **pts, int nbPts,
30                                      int dim, double *bary);
31
32   inline double calculateAreaForPolyg(const double **coords, int nbOfPtsInPolygs,
33                                       int spaceDim);
34
35
36   inline double calculateLgthForSeg2(const double *p1, const double *p2, int spaceDim)
37   {
38     if(spaceDim==1)
39       return *p2-*p1;
40     else
41       {
42         double ret=0;
43         for(int i=0;i<spaceDim;i++)
44           ret+=(p2[i]-p1[i])*(p2[i]-p1[i]);
45         return sqrt(ret);
46       }
47   }
48
49   // ===========================
50   // Calculate Area for triangle
51   // ===========================
52   inline double calculateAreaForTria(const double *p1, const double *p2,
53                                      const double *p3, int spaceDim)
54   {
55     double area ;
56
57     if ( spaceDim == 2 )
58       {
59         area = -((p2[0]-p1[0])*(p3[1]-p1[1]) - (p3[0]-p1[0])*(p2[1]-p1[1]))/2.0;
60       }
61     else
62       {
63         area = sqrt(((p2[1]-p1[1])*(p3[2]-p1[2]) - (p3[1]-p1[1])*(p2[2]-p1[2]))*
64                     ((p2[1]-p1[1])*(p3[2]-p1[2]) - (p3[1]-p1[1])*(p2[2]-p1[2]))
65                     +
66                     ((p3[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p3[2]-p1[2]))*
67                     ((p3[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p3[2]-p1[2]))
68                     +
69                     ((p2[0]-p1[0])*(p3[1]-p1[1]) - (p3[0]-p1[0])*(p2[1]-p1[1]))*
70                     ((p2[0]-p1[0])*(p3[1]-p1[1]) - (p3[0]-p1[0])*(p2[1]-p1[1])))/2.0;
71       }
72
73     return area ;
74   }
75
76   // =============================
77   // Calculate Area for quadrangle
78   // =============================
79   inline double calculateAreaForQuad(const double *p1, const double *p2,
80                                      const double *p3, const double *p4,
81                                      int spaceDim)
82   {
83     double area ;
84
85     if (spaceDim==2)
86       {
87         double a1 = (p2[0]-p1[0])/4.0, a2 = (p2[1]-p1[1])/4.0;
88         double b1 = (p3[0]-p4[0])/4.0, b2 = (p3[1]-p4[1])/4.0;
89         double c1 = (p3[0]-p2[0])/4.0, c2 = (p3[1]-p2[1])/4.0;
90         double d1 = (p4[0]-p1[0])/4.0, d2 = (p4[1]-p1[1])/4.0;
91
92         area = - 4.0*(  b1*c2 - c1*b2 + a1*c2 - c1*a2
93                         + b1*d2 - d1*b2 + a1*d2 - d1*a2);
94       }
95     else
96       {
97         area = (sqrt(((p2[1]-p1[1])*(p4[2]-p1[2]) - (p4[1]-p1[1])*(p2[2]-p1[2]))*
98                      ((p2[1]-p1[1])*(p4[2]-p1[2]) - (p4[1]-p1[1])*(p2[2]-p1[2]))
99                      + ((p4[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p4[2]-p1[2]))*
100                      ((p4[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p4[2]-p1[2]))
101                      + ((p2[0]-p1[0])*(p4[1]-p1[1]) - (p4[0]-p1[0])*(p2[1]-p1[1]))*
102                      ((p2[0]-p1[0])*(p4[1]-p1[1]) - (p4[0]-p1[0])*(p2[1]-p1[1])))
103                 +
104                 sqrt(((p4[1]-p3[1])*(p2[2]-p3[2]) - (p2[1]-p3[1])*(p4[2]-p3[2]))*
105                      ((p4[1]-p3[1])*(p2[2]-p3[2]) - (p2[1]-p3[1])*(p4[2]-p3[2]))
106                      + ((p2[0]-p3[0])*(p4[2]-p3[2]) - (p4[0]-p3[0])*(p2[2]-p3[2]))*
107                      ((p2[0]-p3[0])*(p4[2]-p3[2]) - (p4[0]-p3[0])*(p2[2]-p3[2]))
108                      + ((p4[0]-p3[0])*(p2[1]-p3[1]) - (p2[0]-p3[0])*(p4[1]-p3[1]))*
109                      ((p4[0]-p3[0])*(p2[1]-p3[1]) - (p2[0]-p3[0])*(p4[1]-p3[1])))
110                 )/2.0;
111       }
112
113     return area ;
114   }
115
116   // ====================================
117   // Calculate Normal Vector for Triangle
118   // ====================================
119   inline void calculateNormalForTria(const double *p1, const double *p2,
120                                      const double *p3, double *normal)
121   {
122     normal[0] = ((p2[1]-p1[1])*(p3[2]-p1[2]) - (p3[1]-p1[1])*(p2[2]-p1[2]))/2.0;
123     normal[1] = ((p3[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p3[2]-p1[2]))/2.0;
124     normal[2] = ((p2[0]-p1[0])*(p3[1]-p1[1]) - (p3[0]-p1[0])*(p2[1]-p1[1]))/2.0;
125   }
126
127   // ======================================
128   // Calculate Normal Vector for Quadrangle
129   // ======================================
130   inline void calculateNormalForQuad(const double *p1, const double *p2,
131                                      const double *p3, const double *p4,
132                                      double *normal)
133   {
134     double xnormal1 = (p2[1]-p1[1])*(p4[2]-p1[2]) - (p4[1]-p1[1])*(p2[2]-p1[2]);
135     double xnormal2 = (p4[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p4[2]-p1[2]);
136     double xnormal3 = (p2[0]-p1[0])*(p4[1]-p1[1]) - (p4[0]-p1[0])*(p2[1]-p1[1]);
137     double xarea = sqrt(xnormal1*xnormal1 + xnormal2*xnormal2 + xnormal3*xnormal3);
138     xnormal1 = xnormal1/xarea;
139     xnormal2 = xnormal2/xarea;
140     xnormal3 = xnormal3/xarea;
141     xarea = calculateAreaForQuad(p1,p2,p3,p4,3);
142     normal[0] = xnormal1*xarea ;
143     normal[1] = xnormal2*xarea ;
144     normal[2] = xnormal3*xarea ;
145   }
146
147   // ===================================
148   // Calculate Normal Vector for Polygon
149   // ===================================
150   inline void calculateNormalForPolyg(const double **coords, int nbOfPtsInPolygs,
151                                       double *normal)
152   {
153     double coordOfBary[3];
154
155     calculateBarycenterDyn(coords,nbOfPtsInPolygs,3,coordOfBary);
156     double xnormal1 = (coords[0][1]-coords[1][1]) * (coordOfBary[2]-coords[1][2])
157       - (coords[0][2]-coords[1][2]) * (coordOfBary[1]-coords[1][1]);
158
159     double xnormal2 = (coords[0][2]-coords[1][2]) * (coordOfBary[0]-coords[1][0])
160       - (coords[0][0]-coords[1][0]) * (coordOfBary[2]-coords[1][2]);
161
162     double xnormal3 = (coords[0][0]-coords[1][0]) * (coordOfBary[1]-coords[1][1])
163       - (coords[0][1]-coords[1][1]) * (coordOfBary[0]-coords[1][0]);
164
165     double xarea=sqrt(xnormal1*xnormal1 + xnormal2*xnormal2 + xnormal3*xnormal3);
166
167     if ( xarea < 1.e-6 )
168       {
169         //std::string diagnosis"Can not calculate normal - polygon is singular";
170         throw std::exception();
171       }
172
173     xnormal1 = -xnormal1/xarea;
174     xnormal2 = -xnormal2/xarea;
175     xnormal3 = -xnormal3/xarea;
176     xarea = calculateAreaForPolyg(coords,nbOfPtsInPolygs,3);
177     normal[0] = xnormal1*xarea ;
178     normal[1] = xnormal2*xarea ;
179     normal[2] = xnormal3*xarea ;
180   }
181
182   // ==========================
183   // Calculate Area for Polygon
184   // ==========================
185   inline double calculateAreaForPolyg(const double **coords, int nbOfPtsInPolygs,
186                                       int spaceDim)
187   {
188     double ret=0.;
189     double coordOfBary[3];
190
191     calculateBarycenterDyn(coords,nbOfPtsInPolygs,spaceDim,coordOfBary);
192     for ( int i=0; i<nbOfPtsInPolygs; i++ )
193       {
194         double tmp = calculateAreaForTria(coords[i],coords[(i+1)%nbOfPtsInPolygs],
195                                           coordOfBary,spaceDim);
196         ret+=tmp;
197       }
198     return ret;
199   }
200
201   // ==========================
202   // Calculate Volume for Tetra
203   // ==========================
204   inline double calculateVolumeForTetra(const double *p1, const double *p2,
205                                         const double *p3, const double *p4)
206   {
207     return (  (p3[0]-p1[0])*(  (p2[1]-p1[1])*(p4[2]-p1[2])
208                                - (p2[2]-p1[2])*(p4[1]-p1[1]) )
209               - (p2[0]-p1[0])*(  (p3[1]-p1[1])*(p4[2]-p1[2])
210                                  - (p3[2]-p1[2])*(p4[1]-p1[1]) )
211               + (p4[0]-p1[0])*(  (p3[1]-p1[1])*(p2[2]-p1[2])
212                                  - (p3[2]-p1[2])*(p2[1]-p1[1]) )
213               ) / 6.0 ;
214   }
215
216   // =========================
217   // Calculate Volume for Pyra
218   // =========================
219   inline double calculateVolumeForPyra(const double *p1, const double *p2,
220                                        const double *p3, const double *p4,
221                                        const double *p5)
222   {
223     return ( ((p3[0]-p1[0])*(  (p2[1]-p1[1])*(p5[2]-p1[2])
224                                - (p2[2]-p1[2])*(p5[1]-p1[1]) )
225               -(p2[0]-p1[0])*(  (p3[1]-p1[1])*(p5[2]-p1[2])
226                                 - (p3[2]-p1[2])*(p5[1]-p1[1]) )
227               +(p5[0]-p1[0])*(  (p3[1]-p1[1])*(p2[2]-p1[2])
228                                 - (p3[2]-p1[2])*(p2[1]-p1[1]) ))
229              +
230              ((p4[0]-p1[0])*(  (p3[1]-p1[1])*(p5[2]-p1[2])
231                                - (p3[2]-p1[2])*(p5[1]-p1[1]) )
232               -(p3[0]-p1[0])*(  (p4[1]-p1[1])*(p5[2]-p1[2])
233                                 - (p4[2]-p1[2])*(p5[1]-p1[1]))
234               +(p5[0]-p1[0])*(  (p4[1]-p1[1])*(p3[2]-p1[2])
235                                 - (p4[2]-p1[2])*(p3[1]-p1[1]) ))
236              ) / 6.0 ;
237   }
238
239   // ==========================
240   // Calculate Volume for Penta
241   // ==========================
242   inline double calculateVolumeForPenta(const double *p1, const double *p2,
243                                         const double *p3, const double *p4,
244                                         const double *p5, const double *p6)
245   {
246     double a1 = (p2[0]-p3[0])/2.0, a2 = (p2[1]-p3[1])/2.0, a3 = (p2[2]-p3[2])/2.0;
247     double b1 = (p5[0]-p6[0])/2.0, b2 = (p5[1]-p6[1])/2.0, b3 = (p5[2]-p6[2])/2.0;
248     double c1 = (p4[0]-p1[0])/2.0, c2 = (p4[1]-p1[1])/2.0, c3 = (p4[2]-p1[2])/2.0;
249     double d1 = (p5[0]-p2[0])/2.0, d2 = (p5[1]-p2[1])/2.0, d3 = (p5[2]-p2[2])/2.0;
250     double e1 = (p6[0]-p3[0])/2.0, e2 = (p6[1]-p3[1])/2.0, e3 = (p6[2]-p3[2])/2.0;
251     double f1 = (p1[0]-p3[0])/2.0, f2 = (p1[1]-p3[1])/2.0, f3 = (p1[2]-p3[2])/2.0;
252     double h1 = (p4[0]-p6[0])/2.0, h2 = (p4[1]-p6[1])/2.0, h3 = (p4[2]-p6[2])/2.0;
253
254     double A = a1*c2*f3 - a1*c3*f2 - a2*c1*f3 + a2*c3*f1 + a3*c1*f2 - a3*c2*f1;
255     double B = b1*c2*h3 - b1*c3*h2 - b2*c1*h3 + b2*c3*h1 + b3*c1*h2 - b3*c2*h1;
256     double C = (a1*c2*h3 + b1*c2*f3) - (a1*c3*h2 + b1*c3*f2)
257       - (a2*c1*h3 + b2*c1*f3) + (a2*c3*h1 + b2*c3*f1)
258       + (a3*c1*h2 + b3*c1*f2) - (a3*c2*h1 + b3*c2*f1);
259     double D = a1*d2*f3 - a1*d3*f2 - a2*d1*f3 + a2*d3*f1 + a3*d1*f2 - a3*d2*f1;
260     double E = b1*d2*h3 - b1*d3*h2 - b2*d1*h3 + b2*d3*h1 + b3*d1*h2 - b3*d2*h1;
261     double F = (a1*d2*h3 + b1*d2*f3) - (a1*d3*h2 + b1*d3*f2)
262       - (a2*d1*h3 + b2*d1*f3) + (a2*d3*h1 + b2*d3*f1)
263       + (a3*d1*h2 + b3*d1*f2) - (a3*d2*h1 + b3*d2*f1);
264     double G = a1*e2*f3 - a1*e3*f2 - a2*e1*f3 + a2*e3*f1 + a3*e1*f2 - a3*e2*f1;
265     double H = b1*e2*h3 - b1*e3*h2 - b2*e1*h3 + b2*e3*h1 + b3*e1*h2 - b3*e2*h1;
266     double P = (a1*e2*h3 + b1*e2*f3) - (a1*e3*h2 + b1*e3*f2)
267       - (a2*e1*h3 + b2*e1*f3) + (a2*e3*h1 + b2*e3*f1)
268       + (a3*e1*h2 + b3*e1*f2) - (a3*e2*h1 + b3*e2*f1);
269
270     return (-2.0*(2.0*(A + B + D + E + G + H) + C + F + P)/9.0);
271   }
272
273   // =========================
274   // Calculate Volume for Hexa
275   // =========================
276   inline double calculateVolumeForHexa(const double *pt1, const double *pt2,
277                                        const double *pt3, const double *pt4,
278                                        const double *pt5, const double *pt6,
279                                        const double *pt7, const double *pt8)
280   {
281     double a1=(pt3[0]-pt4[0])/8.0, a2=(pt3[1]-pt4[1])/8.0, a3=(pt3[2]-pt4[2])/8.0;
282     double b1=(pt2[0]-pt1[0])/8.0, b2=(pt2[1]-pt1[1])/8.0, b3=(pt2[2]-pt1[2])/8.0;
283     double c1=(pt7[0]-pt8[0])/8.0, c2=(pt7[1]-pt8[1])/8.0, c3=(pt7[2]-pt8[2])/8.0;
284     double d1=(pt6[0]-pt5[0])/8.0, d2=(pt6[1]-pt5[1])/8.0, d3=(pt6[2]-pt5[2])/8.0;
285     double e1=(pt3[0]-pt2[0])/8.0, e2=(pt3[1]-pt2[1])/8.0, e3=(pt3[2]-pt2[2])/8.0;
286     double f1=(pt4[0]-pt1[0])/8.0, f2=(pt4[1]-pt1[1])/8.0, f3=(pt4[2]-pt1[2])/8.0;
287     double h1=(pt7[0]-pt6[0])/8.0, h2=(pt7[1]-pt6[1])/8.0, h3=(pt7[2]-pt6[2])/8.0;
288     double p1=(pt8[0]-pt5[0])/8.0, p2=(pt8[1]-pt5[1])/8.0, p3=(pt8[2]-pt5[2])/8.0;
289     double q1=(pt3[0]-pt7[0])/8.0, q2=(pt3[1]-pt7[1])/8.0, q3=(pt3[2]-pt7[2])/8.0;
290     double r1=(pt4[0]-pt8[0])/8.0, r2=(pt4[1]-pt8[1])/8.0, r3=(pt4[2]-pt8[2])/8.0;
291     double s1=(pt2[0]-pt6[0])/8.0, s2=(pt2[1]-pt6[1])/8.0, s3=(pt2[2]-pt6[2])/8.0;
292     double t1=(pt1[0]-pt5[0])/8.0, t2=(pt1[1]-pt5[1])/8.0, t3=(pt1[2]-pt5[2])/8.0;
293
294     double A = a1*e2*q3 - a1*e3*q2 - a2*e1*q3 + a2*e3*q1 + a3*e1*q2 - a3*e2*q1;
295     double B = c1*h2*q3 - c1*h3*q2 - c2*h1*q3 + c2*h3*q1 + c3*h1*q2 - c3*h2*q1;
296     double C = (a1*h2 + c1*e2)*q3 - (a1*h3 + c1*e3)*q2
297       - (a2*h1 + c2*e1)*q3 + (a2*h3 + c2*e3)*q1
298       + (a3*h1 + c3*e1)*q2 - (a3*h2 + c3*e2)*q1;
299     double D = b1*e2*s3 - b1*e3*s2 - b2*e1*s3 + b2*e3*s1 + b3*e1*s2 - b3*e2*s1;
300     double E = d1*h2*s3 - d1*h3*s2 - d2*h1*s3 + d2*h3*s1 + d3*h1*s2 - d3*h2*s1;
301     double F = (b1*h2 + d1*e2)*s3 - (b1*h3 + d1*e3)*s2
302       - (b2*h1 + d2*e1)*s3 + (b2*h3 + d2*e3)*s1
303       + (b3*h1 + d3*e1)*s2 - (b3*h2 + d3*e2)*s1;
304     double G = (a1*e2*s3 + b1*e2*q3) - (a1*e3*s2 + b1*e3*q2)
305       - (a2*e1*s3 + b2*e1*q3) + (a2*e3*s1 + b2*e3*q1)
306       + (a3*e1*s2 + b3*e1*q2) - (a3*e2*s1 + b3*e2*q1);
307     double H = (c1*h2*s3 + d1*h2*q3) - (c1*h3*s2 + d1*h3*q2)
308       - (c2*h1*s3 + d2*h1*q3) + (c2*h3*s1 + d2*h3*q1)
309       + (c3*h1*s2 + d3*h1*q2) - (c3*h2*s1 + d3*h2*q1);
310     double I = ((a1*h2 + c1*e2)*s3 + (b1*h2 + d1*e2)*q3)
311       - ((a1*h3 + c1*e3)*s2 + (b1*h3 + d1*e3)*q2)
312       - ((a2*h1 + c2*e1)*s3 + (b2*h1 + d2*e1)*q3)
313       + ((a2*h3 + c2*e3)*s1 + (b2*h3 + d2*e3)*q1)
314       + ((a3*h1 + c3*e1)*s2 + (b3*h1 + d3*e1)*q2)
315       - ((a3*h2 + c3*e2)*s1 + (b3*h2 + d3*e2)*q1);
316     double J = a1*f2*r3 - a1*f3*r2 - a2*f1*r3 + a2*f3*r1 + a3*f1*r2 - a3*f2*r1;
317     double K = c1*p2*r3 - c1*p3*r2 - c2*p1*r3 + c2*p3*r1 + c3*p1*r2 - c3*p2*r1;
318     double L = (a1*p2 + c1*f2)*r3 - (a1*p3 + c1*f3)*r2
319       - (a2*p1 + c2*f1)*r3 + (a2*p3 + c2*f3)*r1
320       + (a3*p1 + c3*f1)*r2 - (a3*p2 + c3*f2)*r1;
321     double M = b1*f2*t3 - b1*f3*t2 - b2*f1*t3 + b2*f3*t1 + b3*f1*t2 - b3*f2*t1;
322     double N = d1*p2*t3 - d1*p3*t2 - d2*p1*t3 + d2*p3*t1 + d3*p1*t2 - d3*p2*t1;
323     double O = (b1*p2 + d1*f2)*t3 - (b1*p3 + d1*f3)*t2
324       - (b2*p1 + d2*f1)*t3 + (b2*p3 + d2*f3)*t1
325       + (b3*p1 + d3*f1)*t2 - (b3*p2 + d3*f2)*t1;
326     double P = (a1*f2*t3 + b1*f2*r3) - (a1*f3*t2 + b1*f3*r2)
327       - (a2*f1*t3 + b2*f1*r3) + (a2*f3*t1 + b2*f3*r1)
328       + (a3*f1*t2 + b3*f1*r2) - (a3*f2*t1 + b3*f2*r1);
329     double Q = (c1*p2*t3 + d1*p2*r3) - (c1*p3*t2 + d1*p3*r2)
330       - (c2*p1*t3 + d2*p1*r3) + (c2*p3*t1 + d2*p3*r1)
331       + (c3*p1*t2 + d3*p1*r2) - (c3*p2*t1 + d3*p2*r1);
332     double R = ((a1*p2 + c1*f2)*t3 + (b1*p2 + d1*f2)*r3)
333       - ((a1*p3 + c1*f3)*t2 + (b1*p3 + d1*f3)*r2)
334       - ((a2*p1 + c2*f1)*t3 + (b2*p1 + d2*f1)*r3)
335       + ((a2*p3 + c2*f3)*t1 + (b2*p3 + d2*f3)*r1)
336       + ((a3*p1 + c3*f1)*t2 + (b3*p1 + d3*f1)*r2)
337       - ((a3*p2 + c3*f2)*t1 + (b3*p2 + d3*f2)*r1);
338     double S = (a1*e2*r3 + a1*f2*q3) - (a1*e3*r2 + a1*f3*q2)
339       - (a2*e1*r3 + a2*f1*q3) + (a2*e3*r1 + a2*f3*q1)
340       + (a3*e1*r2 + a3*f1*q2) - (a3*e2*r1 + a3*f2*q1);
341     double T = (c1*h2*r3 + c1*p2*q3) - (c1*h3*r2 + c1*p3*q2)
342       - (c2*h1*r3 + c2*p1*q3) + (c2*h3*r1 + c2*p3*q1)
343       + (c3*h1*r2 + c3*p1*q2) - (c3*h2*r1 + c3*p2*q1);
344     double U = ((a1*h2 + c1*e2)*r3 + (a1*p2 + c1*f2)*q3)
345       - ((a1*h3 + c1*e3)*r2 + (a1*p3 + c1*f3)*q2)
346       - ((a2*h1 + c2*e1)*r3 + (a2*p1 + c2*f1)*q3)
347       + ((a2*h3 + c2*e3)*r1 + (a2*p3 + c2*f3)*q1)
348       + ((a3*h1 + c3*e1)*r2 + (a3*p1 + c3*f1)*q2)
349       - ((a3*h2 + c3*e2)*r1 + (a3*p2 + c3*f2)*q1);
350     double V = (b1*e2*t3 + b1*f2*s3) - (b1*e3*t2 + b1*f3*s2)
351       - (b2*e1*t3 + b2*f1*s3) + (b2*e3*t1 + b2*f3*s1)
352       + (b3*e1*t2 + b3*f1*s2) - (b3*e2*t1 + b3*f2*s1);
353     double W = (d1*h2*t3 + d1*p2*s3) - (d1*h3*t2 + d1*p3*s2)
354       - (d2*h1*t3 + d2*p1*s3) + (d2*h3*t1 + d2*p3*s1)
355       + (d3*h1*t2 + d3*p1*s2) - (d3*h2*t1 + d3*p2*s1);
356     double X = ((b1*h2 + d1*e2)*t3 + (b1*p2 + d1*f2)*s3)
357       - ((b1*h3 + d1*e3)*t2 + (b1*p3 + d1*f3)*s2)
358       - ((b2*h1 + d2*e1)*t3 + (b2*p1 + d2*f1)*s3)
359       + ((b2*h3 + d2*e3)*t1 + (b2*p3 + d2*f3)*s1)
360       + ((b3*h1 + d3*e1)*t2 + (b3*p1 + d3*f1)*s2)
361       - ((b3*h2 + d3*e2)*t1 + (b3*p2 + d3*f2)*s1);
362     double Y = (a1*e2*t3 + a1*f2*s3 + b1*e2*r3 + b1*f2*q3)
363       - (a1*e3*t2 + a1*f3*s2 + b1*e3*r2 + b1*f3*q2)
364       - (a2*e1*t3 + a2*f1*s3 + b2*e1*r3 + b2*f1*q3)
365       + (a2*e3*t1 + a2*f3*s1 + b2*e3*r1 + b2*f3*q1)
366       + (a3*e1*t2 + a3*f1*s2 + b3*e1*r2 + b3*f1*q2)
367       - (a3*e2*t1 + a3*f2*s1 + b3*e2*r1 + b3*f2*q1);
368     double Z = (c1*h2*t3 + c1*p2*s3 + d1*h2*r3 + d1*p2*q3)
369       - (c1*h3*t2 + c1*p3*s2 + d1*h3*r2 + d1*p3*q2)
370       - (c2*h1*t3 + c2*p1*s3 + d2*h1*r3 + d2*p1*q3)
371       + (c2*h3*t1 + c2*p3*s1 + d2*h3*r1 + d2*p3*q1)
372       + (c3*h1*t2 + c3*p1*s2 + d3*h1*r2 + d3*p1*q2)
373       - (c3*h2*t1 + c3*p2*s1 + d3*h2*r1 + d3*p2*q1);
374     double AA = ((a1*h2 + c1*e2)*t3 + (a1*p2 + c1*f2)*s3
375                  +(b1*h2 + d1*e2)*r3 + (b1*p2 + d1*f2)*q3)
376       - ((a1*h3 + c1*e3)*t2 + (a1*p3 + c1*f3)*s2
377          +(b1*h3 + d1*e3)*r2 + (b1*p3 + d1*f3)*q2)
378       - ((a2*h1 + c2*e1)*t3 + (a2*p1 + c2*f1)*s3
379          +(b2*h1 + d2*e1)*r3 + (b2*p1 + d2*f1)*q3)
380       + ((a2*h3 + c2*e3)*t1 + (a2*p3 + c2*f3)*s1
381          +(b2*h3 + d2*e3)*r1 + (b2*p3 + d2*f3)*q1)
382       + ((a3*h1 + c3*e1)*t2 + (a3*p1 + c3*f1)*s2
383          +(b3*h1 + d3*e1)*r2 + (b3*p1 + d3*f1)*q2)
384       - ((a3*h2 + c3*e2)*t1 + (a3*p2 + c3*f2)*s1
385          + (b3*h2 + d3*e2)*r1 + (b3*p2 + d3*f2)*q1);
386
387     return  64.0*(  8.0*(A + B + D + E + J + K + M + N)
388                     + 4.0*(C + F + G + H + L + O + P + Q + S + T + V + W)
389                     + 2.0*(I + R + U + X + Y + Z) + AA ) / 27.0 ;
390   }
391
392   // =========================================================================================================================
393   // Calculate Volume for Generic Polyedron, even not convex one, WARNING !!! The polyedron's faces must be correctly ordered
394   // =========================================================================================================================
395   inline double calculateVolumeForPolyh(const double ***pts,
396                                         const int *nbOfNodesPerFaces,
397                                         int nbOfFaces,
398                                         const double *bary)
399   {
400     double volume=0.;
401
402     for ( int i=0; i<nbOfFaces; i++ )
403       {
404         double normal[3];
405         double vecForAlt[3];
406
407         calculateNormalForPolyg(pts[i],nbOfNodesPerFaces[i],normal);
408         vecForAlt[0]=bary[0]-pts[i][0][0];
409         vecForAlt[1]=bary[1]-pts[i][0][1];
410         vecForAlt[2]=bary[2]-pts[i][0][2];
411         volume+=vecForAlt[0]*normal[0]+vecForAlt[1]*normal[1]+vecForAlt[2]*normal[2];
412       }
413     return -volume/3.;
414   }
415
416   /*!
417    * Calculate Volume for Generic Polyedron, even not convex one, WARNING !!! The polyedron's faces must be correctly ordered.
418    * 2nd API avoiding to create double** arrays. The returned value could be negative if polyhedrons faces are not oriented with normal outside of the
419    * polyhedron
420    */
421   template<class ConnType, NumberingPolicy numPol>
422   inline double calculateVolumeForPolyh2(const ConnType *connec, int lgth, const double *coords)
423   {
424     std::size_t nbOfFaces=std::count(connec,connec+lgth,-1)+1;
425     double volume=0.;
426     const int *work=connec;
427     for(std::size_t iFace=0;iFace<nbOfFaces;iFace++)
428       {
429         const int *work2=std::find(work+1,connec+lgth,-1);
430         std::size_t nbOfNodesOfCurFace=std::distance(work,work2);
431         double areaVector[3]={0.,0.,0.};
432         for(std::size_t ptId=0;ptId<nbOfNodesOfCurFace;ptId++)
433           {
434             const double *pti=coords+3*OTT<ConnType,numPol>::coo2C(work[ptId]);
435             const double *pti1=coords+3*OTT<ConnType,numPol>::coo2C(work[(ptId+1)%nbOfNodesOfCurFace]);
436             areaVector[0]+=pti[1]*pti1[2]-pti[2]*pti1[1];
437             areaVector[1]+=pti[2]*pti1[0]-pti[0]*pti1[2];
438             areaVector[2]+=pti[0]*pti1[1]-pti[1]*pti1[0];
439           }
440         const double *pt=coords+3*work[0];
441         volume+=pt[0]*areaVector[0]+pt[1]*areaVector[1]+pt[2]*areaVector[2];
442         work=work2+1;
443       }
444     return volume/6.;
445   }
446
447   /*!
448    * This method returns the area oriented vector of a polygon. This method is useful for normal computation without
449    * any troubles if several edges are colinears.
450    * @param res must be of size at least 3 to store the result.
451    */
452   template<class ConnType, NumberingPolicy numPol>
453   inline void areaVectorOfPolygon(const ConnType *connec, int lgth, const double *coords, double *res)
454   {
455     res[0]=0.; res[1]=0.; res[2]=0.;
456     for(int ptId=0;ptId<lgth;ptId++)
457       {
458         const double *pti=coords+3*OTT<ConnType,numPol>::coo2C(connec[ptId]);
459         const double *pti1=coords+3*OTT<ConnType,numPol>::coo2C(connec[(ptId+1)%lgth]);
460         res[0]+=pti[1]*pti1[2]-pti[2]*pti1[1];
461         res[1]+=pti[2]*pti1[0]-pti[0]*pti1[2];
462         res[2]+=pti[0]*pti1[1]-pti[1]*pti1[0];
463       }
464   }
465
466   inline double integrationOverA3DLine(double u1, double v1, double u2, double v2, double A, double B, double C)
467   {
468     return (u1-u2)*(6.*C*C*(v1+v2)+B*B*(v1*v1*v1+v1*v1*v2+v1*v2*v2+v2*v2*v2)+A*A*(2.*u1*u2*(v1+v2)+u1*u1*(3.*v1+v2)+u2*u2*(v1+3.*v2))+ 
469                     4.*C*(A*(2*u1*v1+u2*v1+u1*v2+2.*u2*v2)+B*(v1*v1+v1*v2+v2*v2))+A*B*(u1*(3.*v1*v1+2.*v1*v2+v2*v2)+u2*(v1*v1+2.*v1*v2+3.*v2*v2)))/24.;
470   }
471
472   template<class ConnType, NumberingPolicy numPol>
473   inline void barycenterOfPolyhedron(const ConnType *connec, int lgth, const double *coords, double *res)
474   {
475     std::size_t nbOfFaces=std::count(connec,connec+lgth,-1)+1;
476     res[0]=0.; res[1]=0.; res[2]=0.;
477     const int *work=connec;
478     for(std::size_t i=0;i<nbOfFaces;i++)
479       {
480         const int *work2=std::find(work+1,connec+lgth,-1);
481         int nbOfNodesOfCurFace=(int)std::distance(work,work2);
482         // projection to (u,v) of each faces of polyh to compute integral(x^2/2) on each faces.
483         double normal[3];
484         areaVectorOfPolygon<ConnType,numPol>(work,nbOfNodesOfCurFace,coords,normal);
485         double normOfNormal=sqrt(normal[0]*normal[0]+normal[1]*normal[1]+normal[2]*normal[2]);
486         normal[0]/=normOfNormal; normal[1]/=normOfNormal; normal[2]/=normOfNormal;
487         double u[2]={normal[1],-normal[0]};
488         double s=sqrt(u[0]*u[0]+u[1]*u[1]);
489         double c=normal[2];
490         if(fabs(s)>1e-12)
491           {
492             u[0]/=std::abs(s); u[1]/=std::abs(s);
493           }
494         else
495           { u[0]=1.; u[1]=0.; }
496         //C : high in plane of polyhedron face : always constant
497         double w=normal[0]*coords[3*OTT<ConnType,numPol>::coo2C(work[0])]+
498           normal[1]*coords[3*OTT<ConnType,numPol>::coo2C(work[0])+1]+
499           normal[2]*coords[3*OTT<ConnType,numPol>::coo2C(work[0])+2];
500         // A,B,D,F,G,H,L,M,N coeffs of rotation matrix defined by (u,c,s)
501         double A=u[0]*u[0]*(1-c)+c;
502         double B=u[0]*u[1]*(1-c);
503         double D=u[1]*s;
504         double F=B;
505         double G=u[1]*u[1]*(1-c)+c;
506         double H=-u[0]*s;
507         double L=-u[1]*s;
508         double M=u[0]*s;
509         double N=c;
510         double CX=-w*D;
511         double CY=-w*H;
512         double CZ=-w*N;
513         for(int j=0;j<nbOfNodesOfCurFace;j++)
514           {
515             const double *p1=coords+3*OTT<ConnType,numPol>::coo2C(work[j]);
516             const double *p2=coords+3*OTT<ConnType,numPol>::coo2C(work[(j+1)%nbOfNodesOfCurFace]);
517             double u1=A*p1[0]+B*p1[1]+D*p1[2];
518             double u2=A*p2[0]+B*p2[1]+D*p2[2];
519             double v1=F*p1[0]+G*p1[1]+H*p1[2];
520             double v2=F*p2[0]+G*p2[1]+H*p2[2];
521             //
522             double gx=integrationOverA3DLine(u1,v1,u2,v2,A,B,CX);
523             double gy=integrationOverA3DLine(u1,v1,u2,v2,F,G,CY);
524             double gz=integrationOverA3DLine(u1,v1,u2,v2,L,M,CZ);
525             res[0]+=gx*normal[0];
526             res[1]+=gy*normal[1];
527             res[2]+=gz*normal[2];
528           }
529         work=work2+1;
530       }
531     double vol=calculateVolumeForPolyh2<ConnType,numPol>(connec,lgth,coords);
532     res[0]/=vol; res[1]/=vol; res[2]/=vol;
533   }
534
535   // ============================================================================================================================================
536   // Calculate Volume for NON Generic Polyedron. Only polydrons with bary included in pts is supported by this method. Result is always positive.
537   // ============================================================================================================================================
538   inline double calculateVolumeForPolyhAbs(const double ***pts,
539                                            const int *nbOfNodesPerFaces,
540                                            int nbOfFaces,
541                                            const double *bary)
542   {
543     double volume=0.;
544     
545     for ( int i=0; i<nbOfFaces; i++ )
546       {
547         double normal[3];
548         double vecForAlt[3];
549
550         calculateNormalForPolyg(pts[i],nbOfNodesPerFaces[i],normal);
551         vecForAlt[0]=bary[0]-pts[i][0][0];
552         vecForAlt[1]=bary[1]-pts[i][0][1];
553         vecForAlt[2]=bary[2]-pts[i][0][2];
554         volume+=fabs(vecForAlt[0]*normal[0]+vecForAlt[1]*normal[1]+vecForAlt[2]*normal[2]);
555       }
556     return volume/3.;
557   }
558
559   template<int N>
560   inline double addComponentsOfVec(const double **pts, int rk)
561   {
562     return pts[N-1][rk]+addComponentsOfVec<N-1>(pts,rk);
563   }
564
565   template<>
566   inline double addComponentsOfVec<1>(const double **pts, int rk)
567   {
568     return pts[0][rk];
569   }
570
571   template<int N, int DIM>
572   inline void calculateBarycenter(const double **pts, double *bary)
573   {
574     bary[DIM-1]=addComponentsOfVec<N>(pts,DIM-1)/N;
575     calculateBarycenter<N,DIM-1>(pts,bary);
576   }
577
578   template<>
579   inline void calculateBarycenter<2,0>(const double **/*pts*/, double */*bary*/)
580   {
581   }
582   
583   template<>
584   inline void calculateBarycenter<3,0>(const double **/*pts*/, double */*bary*/)
585   {
586   }
587   
588   template<>
589   inline void calculateBarycenter<4,0>(const double **/*pts*/, double */*bary*/)
590   {
591   }
592   
593   template<>
594   inline void calculateBarycenter<5,0>(const double **/*pts*/, double */*bary*/)
595   {
596   }
597   
598   template<>
599   inline void calculateBarycenter<6,0>(const double **/*pts*/, double */*bary*/)
600   {
601   }
602   
603   template<>
604   inline void calculateBarycenter<7,0>(const double **/*pts*/, double */*bary*/)
605   {
606   }
607   
608   template<>
609   inline void calculateBarycenter<8,0>(const double **/*pts*/, double */*bary*/)
610   {
611   }
612
613   inline void calculateBarycenterDyn(const double **pts, int nbPts,
614                                      int dim, double *bary)
615   {
616     for(int i=0;i<dim;i++)
617       {
618         double temp=0.;
619         for(int j=0;j<nbPts;j++)
620           {
621             temp+=pts[j][i];
622           }
623         bary[i]=temp/nbPts;
624       }
625   }
626
627   template<int SPACEDIM>
628   inline void calculateBarycenterDyn2(const double *pts, int nbPts, double *bary)
629   {
630     for(int i=0;i<SPACEDIM;i++)
631       {
632         double temp=0.;
633         for(int j=0;j<nbPts;j++)
634           {
635             temp+=pts[j*SPACEDIM+i];
636           }
637         bary[i]=temp/nbPts;
638       }
639   }
640   
641   template<class ConnType, NumberingPolicy numPol>
642   inline void computePolygonBarycenter2D(const ConnType *connec, int lgth, const double *coords, double *res)
643   {
644     double area=0.;
645     res[0]=0.; res[1]=0.;
646     for(int i=0;i<lgth;i++)
647       {
648         double cp=coords[2*OTT<ConnType,numPol>::coo2C(connec[i])]*coords[2*OTT<ConnType,numPol>::coo2C(connec[(i+1)%lgth])+1]-
649           coords[2*OTT<ConnType,numPol>::coo2C(connec[i])+1]*coords[2*OTT<ConnType,numPol>::coo2C(connec[(i+1)%lgth])];
650         area+=cp;
651         res[0]+=cp*(coords[2*OTT<ConnType,numPol>::coo2C(connec[i])]+coords[2*OTT<ConnType,numPol>::coo2C(connec[(i+1)%lgth])]);
652         res[1]+=cp*(coords[2*OTT<ConnType,numPol>::coo2C(connec[i])+1]+coords[2*OTT<ConnType,numPol>::coo2C(connec[(i+1)%lgth])+1]);
653       }
654     res[0]/=3.*area;
655     res[1]/=3.*area;
656   }
657
658   template<class ConnType, NumberingPolicy numPol>
659   inline void computePolygonBarycenter3D(const ConnType *connec, int lgth, const double *coords, double *res)
660   {
661     double area[3];
662     areaVectorOfPolygon<ConnType,numPol>(connec,lgth,coords,area);
663     double norm=sqrt(area[0]*area[0]+area[1]*area[1]+area[2]*area[2]);
664     area[0]/=norm; area[1]/=norm; area[2]/=norm;
665     res[0]=0.; res[1]=0.; res[2]=0.;
666     for(int i=1;i<lgth-1;i++)
667       {
668         double v[3];
669         double tmpArea[3];
670         v[0]=(coords[3*OTT<ConnType,numPol>::coo2C(connec[0])]+
671               coords[3*OTT<ConnType,numPol>::coo2C(connec[i])]+
672               coords[3*OTT<ConnType,numPol>::coo2C(connec[i+1])])/3.;
673         v[1]=(coords[3*OTT<ConnType,numPol>::coo2C(connec[0])+1]+
674               coords[3*OTT<ConnType,numPol>::coo2C(connec[i])+1]+
675               coords[3*OTT<ConnType,numPol>::coo2C(connec[i+1])+1])/3.;
676         v[2]=(coords[3*OTT<ConnType,numPol>::coo2C(connec[0])+2]+
677               coords[3*OTT<ConnType,numPol>::coo2C(connec[i])+2]+
678               coords[3*OTT<ConnType,numPol>::coo2C(connec[i+1])+2])/3.;
679         ConnType tmpConn[3]={connec[0],connec[i],connec[i+1]};
680         areaVectorOfPolygon<ConnType,numPol>(tmpConn,3,coords,tmpArea);
681         double norm2=sqrt(tmpArea[0]*tmpArea[0]+tmpArea[1]*tmpArea[1]+tmpArea[2]*tmpArea[2]);
682         if(norm2>1e-12)
683           {
684             tmpArea[0]/=norm2; tmpArea[1]/=norm2; tmpArea[2]/=norm2;
685             double signOfArea=area[0]*tmpArea[0]+area[1]*tmpArea[1]+area[2]*tmpArea[2];
686             res[0]+=signOfArea*norm2*v[0]/norm; res[1]+=signOfArea*norm2*v[1]/norm; res[2]+=signOfArea*norm2*v[2]/norm;
687           }
688       }   
689   }
690 }
691
692 #endif