1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #ifndef __VOLSURFFORMULAE_HXX__
22 #define __VOLSURFFORMULAE_HXX__
24 #include "InterpolationUtils.hxx"
28 namespace INTERP_KERNEL
30 inline void calculateBarycenterDyn(const double **pts, int nbPts,
31 int dim, double *bary);
33 inline double calculateAreaForPolyg(const double **coords, int nbOfPtsInPolygs,
37 inline double calculateLgthForSeg2(const double *p1, const double *p2, int spaceDim)
44 for(int i=0;i<spaceDim;i++)
45 ret+=(p2[i]-p1[i])*(p2[i]-p1[i]);
50 // ===========================
51 // Calculate Area for triangle
52 // ===========================
53 inline double calculateAreaForTria(const double *p1, const double *p2,
54 const double *p3, int spaceDim)
60 area = -((p2[0]-p1[0])*(p3[1]-p1[1]) - (p3[0]-p1[0])*(p2[1]-p1[1]))/2.0;
64 area = sqrt(((p2[1]-p1[1])*(p3[2]-p1[2]) - (p3[1]-p1[1])*(p2[2]-p1[2]))*
65 ((p2[1]-p1[1])*(p3[2]-p1[2]) - (p3[1]-p1[1])*(p2[2]-p1[2]))
67 ((p3[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p3[2]-p1[2]))*
68 ((p3[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p3[2]-p1[2]))
70 ((p2[0]-p1[0])*(p3[1]-p1[1]) - (p3[0]-p1[0])*(p2[1]-p1[1]))*
71 ((p2[0]-p1[0])*(p3[1]-p1[1]) - (p3[0]-p1[0])*(p2[1]-p1[1])))/2.0;
77 // =============================
78 // Calculate Area for quadrangle
79 // =============================
80 inline double calculateAreaForQuad(const double *p1, const double *p2,
81 const double *p3, const double *p4,
88 double a1 = (p2[0]-p1[0])/4.0, a2 = (p2[1]-p1[1])/4.0;
89 double b1 = (p3[0]-p4[0])/4.0, b2 = (p3[1]-p4[1])/4.0;
90 double c1 = (p3[0]-p2[0])/4.0, c2 = (p3[1]-p2[1])/4.0;
91 double d1 = (p4[0]-p1[0])/4.0, d2 = (p4[1]-p1[1])/4.0;
93 area = - 4.0*( b1*c2 - c1*b2 + a1*c2 - c1*a2
94 + b1*d2 - d1*b2 + a1*d2 - d1*a2);
98 area = (sqrt(((p2[1]-p1[1])*(p4[2]-p1[2]) - (p4[1]-p1[1])*(p2[2]-p1[2]))*
99 ((p2[1]-p1[1])*(p4[2]-p1[2]) - (p4[1]-p1[1])*(p2[2]-p1[2]))
100 + ((p4[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p4[2]-p1[2]))*
101 ((p4[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p4[2]-p1[2]))
102 + ((p2[0]-p1[0])*(p4[1]-p1[1]) - (p4[0]-p1[0])*(p2[1]-p1[1]))*
103 ((p2[0]-p1[0])*(p4[1]-p1[1]) - (p4[0]-p1[0])*(p2[1]-p1[1])))
105 sqrt(((p4[1]-p3[1])*(p2[2]-p3[2]) - (p2[1]-p3[1])*(p4[2]-p3[2]))*
106 ((p4[1]-p3[1])*(p2[2]-p3[2]) - (p2[1]-p3[1])*(p4[2]-p3[2]))
107 + ((p2[0]-p3[0])*(p4[2]-p3[2]) - (p4[0]-p3[0])*(p2[2]-p3[2]))*
108 ((p2[0]-p3[0])*(p4[2]-p3[2]) - (p4[0]-p3[0])*(p2[2]-p3[2]))
109 + ((p4[0]-p3[0])*(p2[1]-p3[1]) - (p2[0]-p3[0])*(p4[1]-p3[1]))*
110 ((p4[0]-p3[0])*(p2[1]-p3[1]) - (p2[0]-p3[0])*(p4[1]-p3[1])))
117 // ====================================
118 // Calculate Normal Vector for Triangle
119 // ====================================
120 inline void calculateNormalForTria(const double *p1, const double *p2,
121 const double *p3, double *normal)
123 normal[0] = ((p2[1]-p1[1])*(p3[2]-p1[2]) - (p3[1]-p1[1])*(p2[2]-p1[2]))/2.0;
124 normal[1] = ((p3[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p3[2]-p1[2]))/2.0;
125 normal[2] = ((p2[0]-p1[0])*(p3[1]-p1[1]) - (p3[0]-p1[0])*(p2[1]-p1[1]))/2.0;
128 // ======================================
129 // Calculate Normal Vector for Quadrangle
130 // ======================================
131 inline void calculateNormalForQuad(const double *p1, const double *p2,
132 const double *p3, const double *p4,
135 double xnormal1 = (p2[1]-p1[1])*(p4[2]-p1[2]) - (p4[1]-p1[1])*(p2[2]-p1[2]);
136 double xnormal2 = (p4[0]-p1[0])*(p2[2]-p1[2]) - (p2[0]-p1[0])*(p4[2]-p1[2]);
137 double xnormal3 = (p2[0]-p1[0])*(p4[1]-p1[1]) - (p4[0]-p1[0])*(p2[1]-p1[1]);
138 double xarea = sqrt(xnormal1*xnormal1 + xnormal2*xnormal2 + xnormal3*xnormal3);
139 xnormal1 = xnormal1/xarea;
140 xnormal2 = xnormal2/xarea;
141 xnormal3 = xnormal3/xarea;
142 xarea = calculateAreaForQuad(p1,p2,p3,p4,3);
143 normal[0] = xnormal1*xarea ;
144 normal[1] = xnormal2*xarea ;
145 normal[2] = xnormal3*xarea ;
148 // ===================================
149 // Calculate Normal Vector for Polygon
150 // ===================================
151 inline void calculateNormalForPolyg(const double **coords, int nbOfPtsInPolygs,
154 double coordOfBary[3];
156 calculateBarycenterDyn(coords,nbOfPtsInPolygs,3,coordOfBary);
157 double xnormal1 = (coords[0][1]-coords[1][1]) * (coordOfBary[2]-coords[1][2])
158 - (coords[0][2]-coords[1][2]) * (coordOfBary[1]-coords[1][1]);
160 double xnormal2 = (coords[0][2]-coords[1][2]) * (coordOfBary[0]-coords[1][0])
161 - (coords[0][0]-coords[1][0]) * (coordOfBary[2]-coords[1][2]);
163 double xnormal3 = (coords[0][0]-coords[1][0]) * (coordOfBary[1]-coords[1][1])
164 - (coords[0][1]-coords[1][1]) * (coordOfBary[0]-coords[1][0]);
166 double xarea=sqrt(xnormal1*xnormal1 + xnormal2*xnormal2 + xnormal3*xnormal3);
170 //std::string diagnosis"Can not calculate normal - polygon is singular";
171 throw std::exception();
174 xnormal1 = -xnormal1/xarea;
175 xnormal2 = -xnormal2/xarea;
176 xnormal3 = -xnormal3/xarea;
177 xarea = calculateAreaForPolyg(coords,nbOfPtsInPolygs,3);
178 normal[0] = xnormal1*xarea ;
179 normal[1] = xnormal2*xarea ;
180 normal[2] = xnormal3*xarea ;
183 // ==========================
184 // Calculate Area for Polygon
185 // ==========================
186 inline double calculateAreaForPolyg(const double **coords, int nbOfPtsInPolygs,
190 double coordOfBary[3];
192 calculateBarycenterDyn(coords,nbOfPtsInPolygs,spaceDim,coordOfBary);
193 for ( int i=0; i<nbOfPtsInPolygs; i++ )
195 double tmp = calculateAreaForTria(coords[i],coords[(i+1)%nbOfPtsInPolygs],
196 coordOfBary,spaceDim);
202 // ==========================
203 // Calculate Volume for Tetra
204 // ==========================
205 inline double calculateVolumeForTetra(const double *p1, const double *p2,
206 const double *p3, const double *p4)
208 return ( (p3[0]-p1[0])*( (p2[1]-p1[1])*(p4[2]-p1[2])
209 - (p2[2]-p1[2])*(p4[1]-p1[1]) )
210 - (p2[0]-p1[0])*( (p3[1]-p1[1])*(p4[2]-p1[2])
211 - (p3[2]-p1[2])*(p4[1]-p1[1]) )
212 + (p4[0]-p1[0])*( (p3[1]-p1[1])*(p2[2]-p1[2])
213 - (p3[2]-p1[2])*(p2[1]-p1[1]) )
217 // =========================
218 // Calculate Volume for Pyra
219 // =========================
220 inline double calculateVolumeForPyra(const double *p1, const double *p2,
221 const double *p3, const double *p4,
224 return ( ((p3[0]-p1[0])*( (p2[1]-p1[1])*(p5[2]-p1[2])
225 - (p2[2]-p1[2])*(p5[1]-p1[1]) )
226 -(p2[0]-p1[0])*( (p3[1]-p1[1])*(p5[2]-p1[2])
227 - (p3[2]-p1[2])*(p5[1]-p1[1]) )
228 +(p5[0]-p1[0])*( (p3[1]-p1[1])*(p2[2]-p1[2])
229 - (p3[2]-p1[2])*(p2[1]-p1[1]) ))
231 ((p4[0]-p1[0])*( (p3[1]-p1[1])*(p5[2]-p1[2])
232 - (p3[2]-p1[2])*(p5[1]-p1[1]) )
233 -(p3[0]-p1[0])*( (p4[1]-p1[1])*(p5[2]-p1[2])
234 - (p4[2]-p1[2])*(p5[1]-p1[1]))
235 +(p5[0]-p1[0])*( (p4[1]-p1[1])*(p3[2]-p1[2])
236 - (p4[2]-p1[2])*(p3[1]-p1[1]) ))
240 // ==========================
241 // Calculate Volume for Penta
242 // ==========================
243 inline double calculateVolumeForPenta(const double *p1, const double *p2,
244 const double *p3, const double *p4,
245 const double *p5, const double *p6)
247 double a1 = (p2[0]-p3[0])/2.0, a2 = (p2[1]-p3[1])/2.0, a3 = (p2[2]-p3[2])/2.0;
248 double b1 = (p5[0]-p6[0])/2.0, b2 = (p5[1]-p6[1])/2.0, b3 = (p5[2]-p6[2])/2.0;
249 double c1 = (p4[0]-p1[0])/2.0, c2 = (p4[1]-p1[1])/2.0, c3 = (p4[2]-p1[2])/2.0;
250 double d1 = (p5[0]-p2[0])/2.0, d2 = (p5[1]-p2[1])/2.0, d3 = (p5[2]-p2[2])/2.0;
251 double e1 = (p6[0]-p3[0])/2.0, e2 = (p6[1]-p3[1])/2.0, e3 = (p6[2]-p3[2])/2.0;
252 double f1 = (p1[0]-p3[0])/2.0, f2 = (p1[1]-p3[1])/2.0, f3 = (p1[2]-p3[2])/2.0;
253 double h1 = (p4[0]-p6[0])/2.0, h2 = (p4[1]-p6[1])/2.0, h3 = (p4[2]-p6[2])/2.0;
255 double A = a1*c2*f3 - a1*c3*f2 - a2*c1*f3 + a2*c3*f1 + a3*c1*f2 - a3*c2*f1;
256 double B = b1*c2*h3 - b1*c3*h2 - b2*c1*h3 + b2*c3*h1 + b3*c1*h2 - b3*c2*h1;
257 double C = (a1*c2*h3 + b1*c2*f3) - (a1*c3*h2 + b1*c3*f2)
258 - (a2*c1*h3 + b2*c1*f3) + (a2*c3*h1 + b2*c3*f1)
259 + (a3*c1*h2 + b3*c1*f2) - (a3*c2*h1 + b3*c2*f1);
260 double D = a1*d2*f3 - a1*d3*f2 - a2*d1*f3 + a2*d3*f1 + a3*d1*f2 - a3*d2*f1;
261 double E = b1*d2*h3 - b1*d3*h2 - b2*d1*h3 + b2*d3*h1 + b3*d1*h2 - b3*d2*h1;
262 double F = (a1*d2*h3 + b1*d2*f3) - (a1*d3*h2 + b1*d3*f2)
263 - (a2*d1*h3 + b2*d1*f3) + (a2*d3*h1 + b2*d3*f1)
264 + (a3*d1*h2 + b3*d1*f2) - (a3*d2*h1 + b3*d2*f1);
265 double G = a1*e2*f3 - a1*e3*f2 - a2*e1*f3 + a2*e3*f1 + a3*e1*f2 - a3*e2*f1;
266 double H = b1*e2*h3 - b1*e3*h2 - b2*e1*h3 + b2*e3*h1 + b3*e1*h2 - b3*e2*h1;
267 double P = (a1*e2*h3 + b1*e2*f3) - (a1*e3*h2 + b1*e3*f2)
268 - (a2*e1*h3 + b2*e1*f3) + (a2*e3*h1 + b2*e3*f1)
269 + (a3*e1*h2 + b3*e1*f2) - (a3*e2*h1 + b3*e2*f1);
271 return (-2.0*(2.0*(A + B + D + E + G + H) + C + F + P)/9.0);
274 // =========================
275 // Calculate Volume for Hexa
276 // =========================
277 inline double calculateVolumeForHexa(const double *pt1, const double *pt2,
278 const double *pt3, const double *pt4,
279 const double *pt5, const double *pt6,
280 const double *pt7, const double *pt8)
282 double a1=(pt3[0]-pt4[0])/8.0, a2=(pt3[1]-pt4[1])/8.0, a3=(pt3[2]-pt4[2])/8.0;
283 double b1=(pt2[0]-pt1[0])/8.0, b2=(pt2[1]-pt1[1])/8.0, b3=(pt2[2]-pt1[2])/8.0;
284 double c1=(pt7[0]-pt8[0])/8.0, c2=(pt7[1]-pt8[1])/8.0, c3=(pt7[2]-pt8[2])/8.0;
285 double d1=(pt6[0]-pt5[0])/8.0, d2=(pt6[1]-pt5[1])/8.0, d3=(pt6[2]-pt5[2])/8.0;
286 double e1=(pt3[0]-pt2[0])/8.0, e2=(pt3[1]-pt2[1])/8.0, e3=(pt3[2]-pt2[2])/8.0;
287 double f1=(pt4[0]-pt1[0])/8.0, f2=(pt4[1]-pt1[1])/8.0, f3=(pt4[2]-pt1[2])/8.0;
288 double h1=(pt7[0]-pt6[0])/8.0, h2=(pt7[1]-pt6[1])/8.0, h3=(pt7[2]-pt6[2])/8.0;
289 double p1=(pt8[0]-pt5[0])/8.0, p2=(pt8[1]-pt5[1])/8.0, p3=(pt8[2]-pt5[2])/8.0;
290 double q1=(pt3[0]-pt7[0])/8.0, q2=(pt3[1]-pt7[1])/8.0, q3=(pt3[2]-pt7[2])/8.0;
291 double r1=(pt4[0]-pt8[0])/8.0, r2=(pt4[1]-pt8[1])/8.0, r3=(pt4[2]-pt8[2])/8.0;
292 double s1=(pt2[0]-pt6[0])/8.0, s2=(pt2[1]-pt6[1])/8.0, s3=(pt2[2]-pt6[2])/8.0;
293 double t1=(pt1[0]-pt5[0])/8.0, t2=(pt1[1]-pt5[1])/8.0, t3=(pt1[2]-pt5[2])/8.0;
295 double A = a1*e2*q3 - a1*e3*q2 - a2*e1*q3 + a2*e3*q1 + a3*e1*q2 - a3*e2*q1;
296 double B = c1*h2*q3 - c1*h3*q2 - c2*h1*q3 + c2*h3*q1 + c3*h1*q2 - c3*h2*q1;
297 double C = (a1*h2 + c1*e2)*q3 - (a1*h3 + c1*e3)*q2
298 - (a2*h1 + c2*e1)*q3 + (a2*h3 + c2*e3)*q1
299 + (a3*h1 + c3*e1)*q2 - (a3*h2 + c3*e2)*q1;
300 double D = b1*e2*s3 - b1*e3*s2 - b2*e1*s3 + b2*e3*s1 + b3*e1*s2 - b3*e2*s1;
301 double E = d1*h2*s3 - d1*h3*s2 - d2*h1*s3 + d2*h3*s1 + d3*h1*s2 - d3*h2*s1;
302 double F = (b1*h2 + d1*e2)*s3 - (b1*h3 + d1*e3)*s2
303 - (b2*h1 + d2*e1)*s3 + (b2*h3 + d2*e3)*s1
304 + (b3*h1 + d3*e1)*s2 - (b3*h2 + d3*e2)*s1;
305 double G = (a1*e2*s3 + b1*e2*q3) - (a1*e3*s2 + b1*e3*q2)
306 - (a2*e1*s3 + b2*e1*q3) + (a2*e3*s1 + b2*e3*q1)
307 + (a3*e1*s2 + b3*e1*q2) - (a3*e2*s1 + b3*e2*q1);
308 double H = (c1*h2*s3 + d1*h2*q3) - (c1*h3*s2 + d1*h3*q2)
309 - (c2*h1*s3 + d2*h1*q3) + (c2*h3*s1 + d2*h3*q1)
310 + (c3*h1*s2 + d3*h1*q2) - (c3*h2*s1 + d3*h2*q1);
311 double I = ((a1*h2 + c1*e2)*s3 + (b1*h2 + d1*e2)*q3)
312 - ((a1*h3 + c1*e3)*s2 + (b1*h3 + d1*e3)*q2)
313 - ((a2*h1 + c2*e1)*s3 + (b2*h1 + d2*e1)*q3)
314 + ((a2*h3 + c2*e3)*s1 + (b2*h3 + d2*e3)*q1)
315 + ((a3*h1 + c3*e1)*s2 + (b3*h1 + d3*e1)*q2)
316 - ((a3*h2 + c3*e2)*s1 + (b3*h2 + d3*e2)*q1);
317 double J = a1*f2*r3 - a1*f3*r2 - a2*f1*r3 + a2*f3*r1 + a3*f1*r2 - a3*f2*r1;
318 double K = c1*p2*r3 - c1*p3*r2 - c2*p1*r3 + c2*p3*r1 + c3*p1*r2 - c3*p2*r1;
319 double L = (a1*p2 + c1*f2)*r3 - (a1*p3 + c1*f3)*r2
320 - (a2*p1 + c2*f1)*r3 + (a2*p3 + c2*f3)*r1
321 + (a3*p1 + c3*f1)*r2 - (a3*p2 + c3*f2)*r1;
322 double M = b1*f2*t3 - b1*f3*t2 - b2*f1*t3 + b2*f3*t1 + b3*f1*t2 - b3*f2*t1;
323 double N = d1*p2*t3 - d1*p3*t2 - d2*p1*t3 + d2*p3*t1 + d3*p1*t2 - d3*p2*t1;
324 double O = (b1*p2 + d1*f2)*t3 - (b1*p3 + d1*f3)*t2
325 - (b2*p1 + d2*f1)*t3 + (b2*p3 + d2*f3)*t1
326 + (b3*p1 + d3*f1)*t2 - (b3*p2 + d3*f2)*t1;
327 double P = (a1*f2*t3 + b1*f2*r3) - (a1*f3*t2 + b1*f3*r2)
328 - (a2*f1*t3 + b2*f1*r3) + (a2*f3*t1 + b2*f3*r1)
329 + (a3*f1*t2 + b3*f1*r2) - (a3*f2*t1 + b3*f2*r1);
330 double Q = (c1*p2*t3 + d1*p2*r3) - (c1*p3*t2 + d1*p3*r2)
331 - (c2*p1*t3 + d2*p1*r3) + (c2*p3*t1 + d2*p3*r1)
332 + (c3*p1*t2 + d3*p1*r2) - (c3*p2*t1 + d3*p2*r1);
333 double R = ((a1*p2 + c1*f2)*t3 + (b1*p2 + d1*f2)*r3)
334 - ((a1*p3 + c1*f3)*t2 + (b1*p3 + d1*f3)*r2)
335 - ((a2*p1 + c2*f1)*t3 + (b2*p1 + d2*f1)*r3)
336 + ((a2*p3 + c2*f3)*t1 + (b2*p3 + d2*f3)*r1)
337 + ((a3*p1 + c3*f1)*t2 + (b3*p1 + d3*f1)*r2)
338 - ((a3*p2 + c3*f2)*t1 + (b3*p2 + d3*f2)*r1);
339 double S = (a1*e2*r3 + a1*f2*q3) - (a1*e3*r2 + a1*f3*q2)
340 - (a2*e1*r3 + a2*f1*q3) + (a2*e3*r1 + a2*f3*q1)
341 + (a3*e1*r2 + a3*f1*q2) - (a3*e2*r1 + a3*f2*q1);
342 double T = (c1*h2*r3 + c1*p2*q3) - (c1*h3*r2 + c1*p3*q2)
343 - (c2*h1*r3 + c2*p1*q3) + (c2*h3*r1 + c2*p3*q1)
344 + (c3*h1*r2 + c3*p1*q2) - (c3*h2*r1 + c3*p2*q1);
345 double U = ((a1*h2 + c1*e2)*r3 + (a1*p2 + c1*f2)*q3)
346 - ((a1*h3 + c1*e3)*r2 + (a1*p3 + c1*f3)*q2)
347 - ((a2*h1 + c2*e1)*r3 + (a2*p1 + c2*f1)*q3)
348 + ((a2*h3 + c2*e3)*r1 + (a2*p3 + c2*f3)*q1)
349 + ((a3*h1 + c3*e1)*r2 + (a3*p1 + c3*f1)*q2)
350 - ((a3*h2 + c3*e2)*r1 + (a3*p2 + c3*f2)*q1);
351 double V = (b1*e2*t3 + b1*f2*s3) - (b1*e3*t2 + b1*f3*s2)
352 - (b2*e1*t3 + b2*f1*s3) + (b2*e3*t1 + b2*f3*s1)
353 + (b3*e1*t2 + b3*f1*s2) - (b3*e2*t1 + b3*f2*s1);
354 double W = (d1*h2*t3 + d1*p2*s3) - (d1*h3*t2 + d1*p3*s2)
355 - (d2*h1*t3 + d2*p1*s3) + (d2*h3*t1 + d2*p3*s1)
356 + (d3*h1*t2 + d3*p1*s2) - (d3*h2*t1 + d3*p2*s1);
357 double X = ((b1*h2 + d1*e2)*t3 + (b1*p2 + d1*f2)*s3)
358 - ((b1*h3 + d1*e3)*t2 + (b1*p3 + d1*f3)*s2)
359 - ((b2*h1 + d2*e1)*t3 + (b2*p1 + d2*f1)*s3)
360 + ((b2*h3 + d2*e3)*t1 + (b2*p3 + d2*f3)*s1)
361 + ((b3*h1 + d3*e1)*t2 + (b3*p1 + d3*f1)*s2)
362 - ((b3*h2 + d3*e2)*t1 + (b3*p2 + d3*f2)*s1);
363 double Y = (a1*e2*t3 + a1*f2*s3 + b1*e2*r3 + b1*f2*q3)
364 - (a1*e3*t2 + a1*f3*s2 + b1*e3*r2 + b1*f3*q2)
365 - (a2*e1*t3 + a2*f1*s3 + b2*e1*r3 + b2*f1*q3)
366 + (a2*e3*t1 + a2*f3*s1 + b2*e3*r1 + b2*f3*q1)
367 + (a3*e1*t2 + a3*f1*s2 + b3*e1*r2 + b3*f1*q2)
368 - (a3*e2*t1 + a3*f2*s1 + b3*e2*r1 + b3*f2*q1);
369 double Z = (c1*h2*t3 + c1*p2*s3 + d1*h2*r3 + d1*p2*q3)
370 - (c1*h3*t2 + c1*p3*s2 + d1*h3*r2 + d1*p3*q2)
371 - (c2*h1*t3 + c2*p1*s3 + d2*h1*r3 + d2*p1*q3)
372 + (c2*h3*t1 + c2*p3*s1 + d2*h3*r1 + d2*p3*q1)
373 + (c3*h1*t2 + c3*p1*s2 + d3*h1*r2 + d3*p1*q2)
374 - (c3*h2*t1 + c3*p2*s1 + d3*h2*r1 + d3*p2*q1);
375 double AA = ((a1*h2 + c1*e2)*t3 + (a1*p2 + c1*f2)*s3
376 +(b1*h2 + d1*e2)*r3 + (b1*p2 + d1*f2)*q3)
377 - ((a1*h3 + c1*e3)*t2 + (a1*p3 + c1*f3)*s2
378 +(b1*h3 + d1*e3)*r2 + (b1*p3 + d1*f3)*q2)
379 - ((a2*h1 + c2*e1)*t3 + (a2*p1 + c2*f1)*s3
380 +(b2*h1 + d2*e1)*r3 + (b2*p1 + d2*f1)*q3)
381 + ((a2*h3 + c2*e3)*t1 + (a2*p3 + c2*f3)*s1
382 +(b2*h3 + d2*e3)*r1 + (b2*p3 + d2*f3)*q1)
383 + ((a3*h1 + c3*e1)*t2 + (a3*p1 + c3*f1)*s2
384 +(b3*h1 + d3*e1)*r2 + (b3*p1 + d3*f1)*q2)
385 - ((a3*h2 + c3*e2)*t1 + (a3*p2 + c3*f2)*s1
386 + (b3*h2 + d3*e2)*r1 + (b3*p2 + d3*f2)*q1);
388 return 64.0*( 8.0*(A + B + D + E + J + K + M + N)
389 + 4.0*(C + F + G + H + L + O + P + Q + S + T + V + W)
390 + 2.0*(I + R + U + X + Y + Z) + AA ) / 27.0 ;
393 // =========================================================================================================================
394 // Calculate Volume for Generic Polyedron, even not convex one, WARNING !!! The polyedron's faces must be correctly ordered
395 // =========================================================================================================================
396 inline double calculateVolumeForPolyh(const double ***pts,
397 const int *nbOfNodesPerFaces,
403 for ( int i=0; i<nbOfFaces; i++ )
408 calculateNormalForPolyg(pts[i],nbOfNodesPerFaces[i],normal);
409 vecForAlt[0]=bary[0]-pts[i][0][0];
410 vecForAlt[1]=bary[1]-pts[i][0][1];
411 vecForAlt[2]=bary[2]-pts[i][0][2];
412 volume+=vecForAlt[0]*normal[0]+vecForAlt[1]*normal[1]+vecForAlt[2]*normal[2];
418 * Calculate Volume for Generic Polyedron, even not convex one, WARNING !!! The polyedron's faces must be correctly ordered.
419 * 2nd API avoiding to create double** arrays. The returned value could be negative if polyhedrons faces are not oriented with normal outside of the
422 template<class ConnType, NumberingPolicy numPol>
423 inline double calculateVolumeForPolyh2(const ConnType *connec, int lgth, const double *coords)
425 std::size_t nbOfFaces=std::count(connec,connec+lgth,-1)+1;
427 const int *work=connec;
428 for(std::size_t iFace=0;iFace<nbOfFaces;iFace++)
430 const int *work2=std::find(work+1,connec+lgth,-1);
431 std::size_t nbOfNodesOfCurFace=std::distance(work,work2);
432 double areaVector[3]={0.,0.,0.};
433 for(std::size_t ptId=0;ptId<nbOfNodesOfCurFace;ptId++)
435 const double *pti=coords+3*OTT<ConnType,numPol>::coo2C(work[ptId]);
436 const double *pti1=coords+3*OTT<ConnType,numPol>::coo2C(work[(ptId+1)%nbOfNodesOfCurFace]);
437 areaVector[0]+=pti[1]*pti1[2]-pti[2]*pti1[1];
438 areaVector[1]+=pti[2]*pti1[0]-pti[0]*pti1[2];
439 areaVector[2]+=pti[0]*pti1[1]-pti[1]*pti1[0];
441 const double *pt=coords+3*work[0];
442 volume+=pt[0]*areaVector[0]+pt[1]*areaVector[1]+pt[2]*areaVector[2];
449 * This method returns the area oriented vector of a polygon. This method is useful for normal computation without
450 * any troubles if several edges are colinears.
451 * @param res must be of size at least 3 to store the result.
453 template<class ConnType, NumberingPolicy numPol>
454 inline void areaVectorOfPolygon(const ConnType *connec, int lgth, const double *coords, double *res)
456 res[0]=0.; res[1]=0.; res[2]=0.;
457 for(int ptId=0;ptId<lgth;ptId++)
459 const double *pti=coords+3*OTT<ConnType,numPol>::coo2C(connec[ptId]);
460 const double *pti1=coords+3*OTT<ConnType,numPol>::coo2C(connec[(ptId+1)%lgth]);
461 res[0]+=pti[1]*pti1[2]-pti[2]*pti1[1];
462 res[1]+=pti[2]*pti1[0]-pti[0]*pti1[2];
463 res[2]+=pti[0]*pti1[1]-pti[1]*pti1[0];
467 inline double integrationOverA3DLine(double u1, double v1, double u2, double v2, double A, double B, double C)
469 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))+
470 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.;
473 template<class ConnType, NumberingPolicy numPol>
474 inline void barycenterOfPolyhedron(const ConnType *connec, int lgth, const double *coords, double *res)
476 std::size_t nbOfFaces=std::count(connec,connec+lgth,-1)+1;
477 res[0]=0.; res[1]=0.; res[2]=0.;
478 const int *work=connec;
479 for(std::size_t i=0;i<nbOfFaces;i++)
481 const int *work2=std::find(work+1,connec+lgth,-1);
482 int nbOfNodesOfCurFace=(int)std::distance(work,work2);
483 // projection to (u,v) of each faces of polyh to compute integral(x^2/2) on each faces.
485 areaVectorOfPolygon<ConnType,numPol>(work,nbOfNodesOfCurFace,coords,normal);
486 double normOfNormal=sqrt(normal[0]*normal[0]+normal[1]*normal[1]+normal[2]*normal[2]);
487 normal[0]/=normOfNormal; normal[1]/=normOfNormal; normal[2]/=normOfNormal;
488 double u[2]={normal[1],-normal[0]};
489 double s=sqrt(u[0]*u[0]+u[1]*u[1]);
493 u[0]/=std::abs(s); u[1]/=std::abs(s);
496 { u[0]=1.; u[1]=0.; }
497 //C : high in plane of polyhedron face : always constant
498 double w=normal[0]*coords[3*OTT<ConnType,numPol>::coo2C(work[0])]+
499 normal[1]*coords[3*OTT<ConnType,numPol>::coo2C(work[0])+1]+
500 normal[2]*coords[3*OTT<ConnType,numPol>::coo2C(work[0])+2];
501 // A,B,D,F,G,H,L,M,N coeffs of rotation matrix defined by (u,c,s)
502 double A=u[0]*u[0]*(1-c)+c;
503 double B=u[0]*u[1]*(1-c);
506 double G=u[1]*u[1]*(1-c)+c;
514 for(int j=0;j<nbOfNodesOfCurFace;j++)
516 const double *p1=coords+3*OTT<ConnType,numPol>::coo2C(work[j]);
517 const double *p2=coords+3*OTT<ConnType,numPol>::coo2C(work[(j+1)%nbOfNodesOfCurFace]);
518 double u1=A*p1[0]+B*p1[1]+D*p1[2];
519 double u2=A*p2[0]+B*p2[1]+D*p2[2];
520 double v1=F*p1[0]+G*p1[1]+H*p1[2];
521 double v2=F*p2[0]+G*p2[1]+H*p2[2];
523 double gx=integrationOverA3DLine(u1,v1,u2,v2,A,B,CX);
524 double gy=integrationOverA3DLine(u1,v1,u2,v2,F,G,CY);
525 double gz=integrationOverA3DLine(u1,v1,u2,v2,L,M,CZ);
526 res[0]+=gx*normal[0];
527 res[1]+=gy*normal[1];
528 res[2]+=gz*normal[2];
532 double vol=calculateVolumeForPolyh2<ConnType,numPol>(connec,lgth,coords);
533 res[0]/=vol; res[1]/=vol; res[2]/=vol;
536 // ============================================================================================================================================
537 // Calculate Volume for NON Generic Polyedron. Only polydrons with bary included in pts is supported by this method. Result is always positive.
538 // ============================================================================================================================================
539 inline double calculateVolumeForPolyhAbs(const double ***pts,
540 const int *nbOfNodesPerFaces,
546 for ( int i=0; i<nbOfFaces; i++ )
551 calculateNormalForPolyg(pts[i],nbOfNodesPerFaces[i],normal);
552 vecForAlt[0]=bary[0]-pts[i][0][0];
553 vecForAlt[1]=bary[1]-pts[i][0][1];
554 vecForAlt[2]=bary[2]-pts[i][0][2];
555 volume+=fabs(vecForAlt[0]*normal[0]+vecForAlt[1]*normal[1]+vecForAlt[2]*normal[2]);
561 inline double addComponentsOfVec(const double **pts, int rk)
563 return pts[N-1][rk]+addComponentsOfVec<N-1>(pts,rk);
567 inline double addComponentsOfVec<1>(const double **pts, int rk)
572 template<int N, int DIM>
573 inline void calculateBarycenter(const double **pts, double *bary)
575 bary[DIM-1]=addComponentsOfVec<N>(pts,DIM-1)/N;
576 calculateBarycenter<N,DIM-1>(pts,bary);
580 inline void calculateBarycenter<2,0>(const double **/*pts*/, double */*bary*/)
585 inline void calculateBarycenter<3,0>(const double **/*pts*/, double */*bary*/)
590 inline void calculateBarycenter<4,0>(const double **/*pts*/, double */*bary*/)
595 inline void calculateBarycenter<5,0>(const double **/*pts*/, double */*bary*/)
600 inline void calculateBarycenter<6,0>(const double **/*pts*/, double */*bary*/)
605 inline void calculateBarycenter<7,0>(const double **/*pts*/, double */*bary*/)
610 inline void calculateBarycenter<8,0>(const double **/*pts*/, double */*bary*/)
614 inline void calculateBarycenterDyn(const double **pts, int nbPts,
615 int dim, double *bary)
617 for(int i=0;i<dim;i++)
620 for(int j=0;j<nbPts;j++)
628 template<int SPACEDIM>
629 inline void calculateBarycenterDyn2(const double *pts, int nbPts, double *bary)
631 for(int i=0;i<SPACEDIM;i++)
634 for(int j=0;j<nbPts;j++)
636 temp+=pts[j*SPACEDIM+i];
642 template<class ConnType, NumberingPolicy numPol>
643 inline void computePolygonBarycenter2D(const ConnType *connec, int lgth, const double *coords, double *res)
646 res[0]=0.; res[1]=0.;
647 for(int i=0;i<lgth;i++)
649 double cp=coords[2*OTT<ConnType,numPol>::coo2C(connec[i])]*coords[2*OTT<ConnType,numPol>::coo2C(connec[(i+1)%lgth])+1]-
650 coords[2*OTT<ConnType,numPol>::coo2C(connec[i])+1]*coords[2*OTT<ConnType,numPol>::coo2C(connec[(i+1)%lgth])];
652 res[0]+=cp*(coords[2*OTT<ConnType,numPol>::coo2C(connec[i])]+coords[2*OTT<ConnType,numPol>::coo2C(connec[(i+1)%lgth])]);
653 res[1]+=cp*(coords[2*OTT<ConnType,numPol>::coo2C(connec[i])+1]+coords[2*OTT<ConnType,numPol>::coo2C(connec[(i+1)%lgth])+1]);
659 template<class ConnType, NumberingPolicy numPol>
660 inline void computePolygonBarycenter3D(const ConnType *connec, int lgth, const double *coords, double *res)
663 areaVectorOfPolygon<ConnType,numPol>(connec,lgth,coords,area);
664 double norm=sqrt(area[0]*area[0]+area[1]*area[1]+area[2]*area[2]);
665 area[0]/=norm; area[1]/=norm; area[2]/=norm;
666 res[0]=0.; res[1]=0.; res[2]=0.;
667 for(int i=1;i<lgth-1;i++)
671 v[0]=(coords[3*OTT<ConnType,numPol>::coo2C(connec[0])]+
672 coords[3*OTT<ConnType,numPol>::coo2C(connec[i])]+
673 coords[3*OTT<ConnType,numPol>::coo2C(connec[i+1])])/3.;
674 v[1]=(coords[3*OTT<ConnType,numPol>::coo2C(connec[0])+1]+
675 coords[3*OTT<ConnType,numPol>::coo2C(connec[i])+1]+
676 coords[3*OTT<ConnType,numPol>::coo2C(connec[i+1])+1])/3.;
677 v[2]=(coords[3*OTT<ConnType,numPol>::coo2C(connec[0])+2]+
678 coords[3*OTT<ConnType,numPol>::coo2C(connec[i])+2]+
679 coords[3*OTT<ConnType,numPol>::coo2C(connec[i+1])+2])/3.;
680 ConnType tmpConn[3]={connec[0],connec[i],connec[i+1]};
681 areaVectorOfPolygon<ConnType,numPol>(tmpConn,3,coords,tmpArea);
682 double norm2=sqrt(tmpArea[0]*tmpArea[0]+tmpArea[1]*tmpArea[1]+tmpArea[2]*tmpArea[2]);
685 tmpArea[0]/=norm2; tmpArea[1]/=norm2; tmpArea[2]/=norm2;
686 double signOfArea=area[0]*tmpArea[0]+area[1]*tmpArea[1]+area[2]*tmpArea[2];
687 res[0]+=signOfArea*norm2*v[0]/norm; res[1]+=signOfArea*norm2*v[1]/norm; res[2]+=signOfArea*norm2*v[2]/norm;