Salome HOME
f578d744a375dd7ea6a4368d8e2b7c6685784a29
[tools/medcoupling.git] / src / INTERP_KERNEL / VolSurfUser.cxx
1 // Copyright (C) 2007-2020  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, or (at your option) any later version.
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 // Author : Anthony Geay (CEA/DEN)
20
21 #include "VolSurfUser.hxx"
22 #include "InterpKernelAutoPtr.hxx"
23 #include "InterpolationUtils.hxx"
24
25 #include <cmath>
26 #include <limits>
27 #include <algorithm>
28 #include <functional>
29
30 namespace INTERP_KERNEL
31 {
32   /* Orthogonal distance from a point to a plane defined by three points p1, p2, p3.
33    * Returns a signed distance, the normal of the plane being defined by (p1-p2)x(p3-p2)
34    */
35   double OrthoDistanceFromPtToPlaneInSpaceDim3(const double *p, const double *p1, const double *p2, const double *p3)
36   {
37     double prec = 1.0e-14;
38     double T[2][3] = {{p1[0] - p2[0], p1[1] - p2[1], p1[2] - p2[2]},
39                       {p3[0] - p2[0], p3[1] - p2[1], p3[2] - p2[2]}};
40     double N[3] = {T[0][1]*T[1][2]-T[0][2]*T[1][1],
41                    T[0][2]*T[1][0]-T[0][0]*T[1][2],
42                    T[0][0]*T[1][1]-T[0][1]*T[1][0]};
43
44     double norm2 = N[0]*N[0] + N[1]*N[1] + N[2]*N[2];
45     if (norm2 < prec)
46       throw INTERP_KERNEL::Exception("OrthoDistanceFromPtToPlaneInSpaceDim3: degenerated normal vector!");
47     double num = N[0]*(p[0]-p1[0]) + N[1]*(p[1]-p1[1]) + N[2]*(p[2]-p1[2]);
48     return num/sqrt(norm2);
49   }
50
51   double SquareDistanceFromPtToSegInSpaceDim2(const double *pt, const double *pt0Seg2, const double *pt1Seg2, std::size_t &nbOfHint)
52   {
53     double dx=pt1Seg2[0]-pt0Seg2[0],dy=pt1Seg2[1]-pt0Seg2[1];
54     double norm=sqrt(dx*dx+dy*dy);
55     if(norm==0.)
56       return (pt[0]-pt0Seg2[0])*(pt[0]-pt0Seg2[0])+(pt[1]-pt0Seg2[1])*(pt[1]-pt0Seg2[1]);//return std::numeric_limits<double>::max();
57     dx/=norm; dy/=norm;
58     double dx2=pt[0]-pt0Seg2[0],dy2=pt[1]-pt0Seg2[1];
59     double dotP=(dx2*dx+dy2*dy);
60     if(dotP<0. || dotP>norm)
61       return dotP<0.?(pt[0]-pt0Seg2[0])*(pt[0]-pt0Seg2[0])+(pt[1]-pt0Seg2[1])*(pt[1]-pt0Seg2[1]):(pt[0]-pt1Seg2[0])*(pt[0]-pt1Seg2[0])+(pt[1]-pt1Seg2[1])*(pt[1]-pt1Seg2[1]);
62     nbOfHint++;
63     double x=pt0Seg2[0]+dotP*dx,y=pt0Seg2[1]+dotP*dy;
64     return (x-pt[0])*(x-pt[0])+(y-pt[1])*(y-pt[1]);
65   }
66
67   /**
68    * See http://geomalgorithms.com/a02-_lines.html#Distance-to-Ray-or-Segment
69    */
70   double DistanceFromPtToSegInSpaceDim3(const double *pt, const double *pt0Seg2, const double *pt1Seg2)
71   {
72     double v[3], w[3];
73     for(int i=0; i < 3; i++) {
74         v[i]=pt1Seg2[i]-pt0Seg2[i];
75         w[i] = pt[i] - pt0Seg2[i];
76     }
77
78     double c1 = dotprod<3>(w,v);
79     if ( c1 <= 0 )
80       return norm<3>(w);
81     double c2 = dotprod<3>(v,v);
82     if ( c2 <= c1 )
83       {
84         for(int i=0; i < 3; i++)
85           w[i] = pt[i] - pt1Seg2[i];
86         return norm<3>(w);
87       }
88     double b = c1 / c2;
89     for(int i=0; i < 3; i++)
90       w[i] = pt0Seg2[i] + b * v[i] - pt[i];
91     return norm<3>(w);
92   }
93
94   /**
95      Helper for DistanceFromPtToTriInSpaceDim3
96    */
97   inline double _HelperDistancePtToTri3D_1(const double aXX, const double bX, const double c)
98   {
99     if (bX >= 0)
100       return c;
101     if (-bX >= aXX)
102       return aXX + 2*bX + c;
103     return bX*(-bX / aXX) + c;
104   }
105
106   /**
107     Helper for DistanceFromPtToTriInSpaceDim3
108    */
109   inline double _HelperDistancePtToTri3D_2(const double a01, const double aXX, const double aYY,
110                                            const double bX, const double bY, const double c)
111   {
112     double tmp0 = a01 + bX, tmp1 = aXX + bY;
113     if (tmp1 > tmp0) {
114         double numer = tmp1 - tmp0, denom = aXX - 2*a01 + aYY;
115         if (numer >= denom)
116           return aXX + 2*bX + c;
117         else {
118             double s, t;
119             s = numer / denom; t = 1 - s;
120             return s*(aXX*s + a01*t + 2*bX) + t*(a01*s + aYY*t + 2*bY) + c;
121         }
122     }
123     else
124       {
125         if (tmp1 <= 0)   return aYY + 2*bY + c;
126         else {
127             if (bY >= 0) return c;
128             else         return bY*(-bY / aYY) + c;
129         }
130       }
131   }
132
133   /**
134    * From https://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf
135    */
136   double DistanceFromPtToTriInSpaceDim3(const double *pt, const double *pt0Tri3, const double *pt1Tri3, const double *pt2Tri3)
137   {
138     double diff[3], edge0[3], edge1[3];
139     for(int i=0; i < 3; i++) diff[i]=pt0Tri3[i]-pt[i];
140     for(int i=0; i < 3; i++) edge0[i]=pt1Tri3[i]-pt0Tri3[i];
141     for(int i=0; i < 3; i++) edge1[i]=pt2Tri3[i]-pt0Tri3[i];
142
143     double a00=dotprod<3>(edge0, edge0), a01=dotprod<3>(edge0,edge1), a11=dotprod<3>(edge1,edge1);
144     double b0=dotprod<3>(diff, edge0), b1=dotprod<3>(diff, edge1), c=dotprod<3>(diff, diff);
145     double det = fabs(a00*a11 - a01*a01);
146     double s = a01*b1 - a11*b0, t = a01*b0 - a00*b1;
147     double sDist;
148
149     if (s + t <= det)
150       {
151         if (s < 0)  {
152             if (t < 0) { // region 4
153                 if (b0 < 0) {
154                     if (-b0 >= a00)  sDist = a00 + 2*b0 + c;
155                     else             sDist = b0*(-b0 / a00) + c;
156                   }
157                 else
158                   sDist = _HelperDistancePtToTri3D_1(a11, b1, c);
159               }
160             else  // region 3
161               sDist = _HelperDistancePtToTri3D_1(a11, b1, c);
162           }
163         else       {
164             if (t < 0)  // region 5
165               sDist = _HelperDistancePtToTri3D_1(a00, b0, c);
166             else  // region 0
167               {
168                 // minimum at interior point
169                 if (fabs(det) < 1.0e-12)
170                   {
171                     // points are colinear (degenerated triangle)
172                     // => Compute distance between segments
173                      double distance = std::min(DistanceFromPtToSegInSpaceDim3(pt, pt0Tri3, pt1Tri3),
174                                                 DistanceFromPtToSegInSpaceDim3(pt, pt1Tri3, pt2Tri3));
175                      return distance;
176                   }
177
178                 // else we can divide by non-zero
179                 double invDet = 1 / det;
180                 s *= invDet;    t *= invDet;
181                 sDist = s*(a00*s + a01*t + 2*b0) + t*(a01*s + a11*t + 2*b1) + c;
182               }
183           }
184       }
185     else  // s+t > det
186       {
187         if (s < 0.0)  // region 2
188           sDist = _HelperDistancePtToTri3D_2(a01, a00, a11, b0, b1, c);
189         else {
190             if (t < 0.0)  // region 6
191               sDist = _HelperDistancePtToTri3D_2(a01, a11, a00, b1, b0, c);
192             else {  // region 1
193                 double numer = a11 + b1 - a01 - b0;
194                 if (numer <= 0.0)
195                   sDist = a11 + 2*b1 + c;
196                 else {
197                     double denom = a00 - 2*a01 + a11;
198                     if (numer >= denom)
199                       sDist = a00 + 2*b0 + c;
200                     else {
201                         s = numer / denom; t = 1 - s;
202                         sDist = s*(a00*s + a01*t + 2*b0) + t*(a01*s + a11*t + 2*b1) + c;
203                     }
204                 }
205             }
206         }
207       }
208     // Account for numerical round-off error.
209     if (sDist < 0)
210       sDist = 0.0;
211
212     return sqrt(sDist);
213   }
214
215   double DistanceFromPtToPolygonInSpaceDim3(const double *pt, const mcIdType *connOfPolygonBg, const mcIdType *connOfPolygonEnd, const double *coords)
216   {
217     std::size_t nbOfEdges=std::distance(connOfPolygonBg,connOfPolygonEnd);
218     if(nbOfEdges<3)
219       throw INTERP_KERNEL::Exception("DistanceFromPtToPolygonInSpaceDim3 : trying to compute a distance to a polygon containing less than 3 edges !");
220     double baryOfNodes[3]={0.,0.,0.};
221     for(std::size_t i=0;i<nbOfEdges;i++)
222       { baryOfNodes[0]+=coords[3*connOfPolygonBg[i]]; baryOfNodes[1]+=coords[3*connOfPolygonBg[i]+1]; baryOfNodes[2]+=coords[3*connOfPolygonBg[i]+2]; }
223     std::transform(baryOfNodes,baryOfNodes+3,baryOfNodes,std::bind(std::multiplies<double>(),std::placeholders::_1,1./((double)nbOfEdges)));
224     double matrix[12];
225     if(!ComputeRotTranslationMatrixToPut3PointsOnOXY(coords+3*connOfPolygonBg[0],coords+3*connOfPolygonBg[1],baryOfNodes,matrix))
226       return std::numeric_limits<double>::max();
227     INTERP_KERNEL::AutoPtr<double> ptXY=new double[2*nbOfEdges]; ptXY[0]=0.; ptXY[1]=0.;
228     ptXY[2]=matrix[0]*coords[3*connOfPolygonBg[1]]+matrix[1]*coords[3*connOfPolygonBg[1]+1]+matrix[2]*coords[3*connOfPolygonBg[1]+2]+matrix[3]; ptXY[3]=0.;
229     for(std::size_t i=2;i<nbOfEdges;i++)
230       {
231         ptXY[2*i]=matrix[0]*coords[3*connOfPolygonBg[i]]+matrix[1]*coords[3*connOfPolygonBg[i]+1]+matrix[2]*coords[3*connOfPolygonBg[i]+2]+matrix[3];
232         ptXY[2*i+1]=matrix[4]*coords[3*connOfPolygonBg[i]]+matrix[5]*coords[3*connOfPolygonBg[i]+1]+matrix[6]*coords[3*connOfPolygonBg[i]+2]+matrix[7];
233       }
234     double xy[2]={matrix[0]*pt[0]+matrix[1]*pt[1]+matrix[2]*pt[2]+matrix[3],matrix[4]*pt[0]+matrix[5]*pt[1]+matrix[6]*pt[2]+matrix[7]};
235     double z=matrix[8]*pt[0]+matrix[9]*pt[1]+matrix[10]*pt[2]+matrix[11];
236     double ret=std::numeric_limits<double>::max();
237     std::size_t nbOfHint=0;
238     for(std::size_t i=0;i<nbOfEdges;i++)
239       {
240         double tmp=SquareDistanceFromPtToSegInSpaceDim2(xy,((double *)ptXY)+2*i,((double *)ptXY)+2*((i+1)%nbOfEdges),nbOfHint);
241         ret=std::min(ret,z*z+tmp);
242       }
243     if(nbOfHint==nbOfEdges)
244       ret=std::min(ret,z*z);
245     return sqrt(ret);
246   }
247
248   /*!
249    * \param [out] matrix contain a dense matrix of size 12 with 3 rows containing each 4 columns. This matrix is the reduction of 4x4 matrix but the last
250    *              line containing [0,0,0,1] is omitted.
251    */
252   bool ComputeRotTranslationMatrixToPut3PointsOnOXY(const double *p0, const double *p1, const double *p2, double *matrix)
253   {
254     double norm=sqrt((p1[0]-p0[0])*(p1[0]-p0[0])+(p1[1]-p0[1])*(p1[1]-p0[1])+(p1[2]-p0[2])*(p1[2]-p0[2]));
255     double c=(p1[0]-p0[0])/norm;
256     double s=sqrt(1-c*c);
257     double y=p1[2]-p0[2],z=p0[1]-p1[1];
258     norm=sqrt(y*y+z*z);
259     if(norm!=0.)
260       { y/=norm; z/=norm; }
261     double r0[9]={c,-z*s,y*s,
262                   z*s,y*y*(1-c)+c,y*z*(1-c),
263                   -y*s,z*y*(1-c),z*z*(1-c)+c};
264     // 2nd rotation matrix
265     double x=p2[0]-p0[0];
266     y=p2[1]-p0[1]; z=p2[2]-p0[2];
267     double y1=x*r0[3]+y*r0[4]+z*r0[5],z1=x*r0[6]+y*r0[7]+z*r0[8];
268     c=y1/sqrt(y1*y1+z1*z1);
269     s=sqrt(1.-c*c);
270     //
271     std::copy(r0,r0+3,matrix);
272     matrix[4]=c*r0[3]-s*r0[6]; matrix[5]=c*r0[4]-s*r0[7]; matrix[6]=c*r0[5]-s*r0[8];
273     matrix[8]=s*r0[3]+c*r0[6]; matrix[9]=s*r0[4]+c*r0[7]; matrix[10]=s*r0[5]+c*r0[8];
274     matrix[3]=-p0[0]*matrix[0]-p0[1]*matrix[1]-p0[2]*matrix[2];
275     matrix[7]=-p0[0]*matrix[4]-p0[1]*matrix[5]-p0[2]*matrix[6];
276     matrix[11]=-p0[0]*matrix[8]-p0[1]*matrix[9]-p0[2]*matrix[10];
277     return true;
278   }
279 }
280