1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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 #include "VolSurfUser.hxx"
22 #include "InterpKernelAutoPtr.hxx"
23 #include "InterpolationUtils.hxx"
30 namespace INTERP_KERNEL
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)
35 double OrthoDistanceFromPtToPlaneInSpaceDim3(const double *p, const double *p1, const double *p2, const double *p3)
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]};
44 double norm2 = N[0]*N[0] + N[1]*N[1] + N[2]*N[2];
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);
51 double SquareDistanceFromPtToSegInSpaceDim2(const double *pt, const double *pt0Seg2, const double *pt1Seg2, std::size_t &nbOfHint)
53 double dx=pt1Seg2[0]-pt0Seg2[0],dy=pt1Seg2[1]-pt0Seg2[1];
54 double norm=sqrt(dx*dx+dy*dy);
56 return (pt[0]-pt0Seg2[0])*(pt[0]-pt0Seg2[0])+(pt[1]-pt0Seg2[1])*(pt[1]-pt0Seg2[1]);//return std::numeric_limits<double>::max();
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]);
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]);
68 * See http://geomalgorithms.com/a02-_lines.html#Distance-to-Ray-or-Segment
70 double DistanceFromPtToSegInSpaceDim3(const double *pt, const double *pt0Seg2, const double *pt1Seg2)
73 for(int i=0; i < 3; i++) {
74 v[i]=pt1Seg2[i]-pt0Seg2[i];
75 w[i] = pt[i] - pt0Seg2[i];
78 double c1 = dotprod<3>(w,v);
81 double c2 = dotprod<3>(v,v);
84 for(int i=0; i < 3; i++)
85 w[i] = pt[i] - pt1Seg2[i];
89 for(int i=0; i < 3; i++)
90 w[i] = pt0Seg2[i] + b * v[i] - pt[i];
95 Helper for DistanceFromPtToTriInSpaceDim3
97 inline double _HelperDistancePtToTri3D_1(const double aXX, const double bX, const double c)
102 return aXX + 2*bX + c;
103 return bX*(-bX / aXX) + c;
107 Helper for DistanceFromPtToTriInSpaceDim3
109 inline double _HelperDistancePtToTri3D_2(const double a01, const double aXX, const double aYY,
110 const double bX, const double bY, const double c)
112 double tmp0 = a01 + bX, tmp1 = aXX + bY;
114 double numer = tmp1 - tmp0, denom = aXX - 2*a01 + aYY;
116 return aXX + 2*bX + c;
119 s = numer / denom; t = 1 - s;
120 return s*(aXX*s + a01*t + 2*bX) + t*(a01*s + aYY*t + 2*bY) + c;
125 if (tmp1 <= 0) return aYY + 2*bY + c;
127 if (bY >= 0) return c;
128 else return bY*(-bY / aYY) + c;
134 * From https://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf
136 double DistanceFromPtToTriInSpaceDim3(const double *pt, const double *pt0Tri3, const double *pt1Tri3, const double *pt2Tri3)
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];
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;
152 if (t < 0) { // region 4
154 if (-b0 >= a00) sDist = a00 + 2*b0 + c;
155 else sDist = b0*(-b0 / a00) + c;
158 sDist = _HelperDistancePtToTri3D_1(a11, b1, c);
161 sDist = _HelperDistancePtToTri3D_1(a11, b1, c);
164 if (t < 0) // region 5
165 sDist = _HelperDistancePtToTri3D_1(a00, b0, c);
168 // minimum at interior point
169 if (fabs(det) < 1.0e-12)
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));
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;
187 if (s < 0.0) // region 2
188 sDist = _HelperDistancePtToTri3D_2(a01, a00, a11, b0, b1, c);
190 if (t < 0.0) // region 6
191 sDist = _HelperDistancePtToTri3D_2(a01, a11, a00, b1, b0, c);
193 double numer = a11 + b1 - a01 - b0;
195 sDist = a11 + 2*b1 + c;
197 double denom = a00 - 2*a01 + a11;
199 sDist = a00 + 2*b0 + c;
201 s = numer / denom; t = 1 - s;
202 sDist = s*(a00*s + a01*t + 2*b0) + t*(a01*s + a11*t + 2*b1) + c;
208 // Account for numerical round-off error.
215 double DistanceFromPtToPolygonInSpaceDim3(const double *pt, const int *connOfPolygonBg, const int *connOfPolygonEnd, const double *coords)
217 std::size_t nbOfEdges=std::distance(connOfPolygonBg,connOfPolygonEnd);
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::bind2nd(std::multiplies<double>(),1./((double)nbOfEdges)));
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++)
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];
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++)
240 double tmp=SquareDistanceFromPtToSegInSpaceDim2(xy,((double *)ptXY)+2*i,((double *)ptXY)+2*((i+1)%nbOfEdges),nbOfHint);
241 ret=std::min(ret,z*z+tmp);
243 if(nbOfHint==nbOfEdges)
244 ret=std::min(ret,z*z);
249 * \param [out] matrix contain a dense matrix of size 12 with 3 rows containing each 4 colums. This matrix is the reduction of 4x4 matrix but the last
250 * line containing [0,0,0,1] is omitted.
252 bool ComputeRotTranslationMatrixToPut3PointsOnOXY(const double *p0, const double *p1, const double *p2, double *matrix)
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];
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);
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];