Salome HOME
Move medtool folder to MED base repository
[modules/med.git] / medtool / src / INTERP_KERNEL / VolSurfUser.cxx
1 // Copyright (C) 2007-2015  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
24 #include <cmath>
25 #include <limits>
26 #include <algorithm>
27 #include <functional>
28
29 namespace INTERP_KERNEL
30 {
31   double SquareDistanceFromPtToSegInSpaceDim2(const double *pt, const double *pt0Seg2, const double *pt1Seg2, std::size_t &nbOfHint)
32   {
33     double dx=pt1Seg2[0]-pt0Seg2[0],dy=pt1Seg2[1]-pt0Seg2[1];
34     double norm=sqrt(dx*dx+dy*dy);
35     if(norm==0.)
36       return (pt[0]-pt0Seg2[0])*(pt[0]-pt0Seg2[0])+(pt[1]-pt0Seg2[1])*(pt[1]-pt0Seg2[1]);//return std::numeric_limits<double>::max();
37     dx/=norm; dy/=norm;
38     double dx2=pt[0]-pt0Seg2[0],dy2=pt[1]-pt0Seg2[1];
39     double dotP=(dx2*dx+dy2*dy);
40     if(dotP<0. || dotP>norm)
41       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]);
42     nbOfHint++;
43     double x=pt0Seg2[0]+dotP*dx,y=pt0Seg2[1]+dotP*dy;
44     return (x-pt[0])*(x-pt[0])+(y-pt[1])*(y-pt[1]);
45   }
46
47   double DistanceFromPtToTriInSpaceDim3(const double *pt, const double *pt0Tri3, const double *pt1Tri3, const double *pt2Tri3)
48   {
49     double matrix[12];
50     if(!ComputeRotTranslationMatrixToPut3PointsOnOXY(pt0Tri3,pt1Tri3,pt2Tri3,matrix))
51       return std::numeric_limits<double>::max();
52     double xy0[2],xy1[2],xy2[2],xy[2]; xy0[0]=0.; xy0[1]=0.;
53     xy1[0]=matrix[0]*pt1Tri3[0]+matrix[1]*pt1Tri3[1]+matrix[2]*pt1Tri3[2]+matrix[3]; xy1[1]=0.;
54     xy2[0]=matrix[0]*pt2Tri3[0]+matrix[1]*pt2Tri3[1]+matrix[2]*pt2Tri3[2]+matrix[3];
55     xy2[1]=matrix[4]*pt2Tri3[0]+matrix[5]*pt2Tri3[1]+matrix[6]*pt2Tri3[2]+matrix[7];
56     xy[0]=matrix[0]*pt[0]+matrix[1]*pt[1]+matrix[2]*pt[2]+matrix[3];
57     xy[1]=matrix[4]*pt[0]+matrix[5]*pt[1]+matrix[6]*pt[2]+matrix[7];
58     double z=matrix[8]*pt[0]+matrix[9]*pt[1]+matrix[10]*pt[2]+matrix[11];
59     double ret=std::numeric_limits<double>::max();
60     std::size_t nbOfHint=0;
61     if(xy[0]>0. && xy[0]<xy1[0])
62       { ret=std::min(ret,z*z+xy[1]*xy[1]); nbOfHint++; } //distance pt to edge [pt0Tri3,pt1Tri3]
63     double tmp=SquareDistanceFromPtToSegInSpaceDim2(xy,xy1,xy2,nbOfHint); //distance pt to edge [pt1Tri3,pt2Tri3]
64     ret=std::min(ret,z*z+tmp);
65     tmp=SquareDistanceFromPtToSegInSpaceDim2(xy,xy2,xy0,nbOfHint);//distance pt to edge [pt2Tri3,pt0Tri3]
66     ret=std::min(ret,z*z+tmp);
67     if(nbOfHint==3)
68       ret=std::min(ret,z*z);
69   return sqrt(ret);
70   }
71
72   double DistanceFromPtToPolygonInSpaceDim3(const double *pt, const int *connOfPolygonBg, const int *connOfPolygonEnd, const double *coords)
73   {
74     std::size_t nbOfEdges=std::distance(connOfPolygonBg,connOfPolygonEnd);
75     if(nbOfEdges<3)
76       throw INTERP_KERNEL::Exception("DistanceFromPtToPolygonInSpaceDim3 : trying to compute a distance to a polygon containing less than 3 edges !");
77     double baryOfNodes[3]={0.,0.,0.};
78     for(std::size_t i=0;i<nbOfEdges;i++)
79       { baryOfNodes[0]+=coords[3*connOfPolygonBg[i]]; baryOfNodes[1]+=coords[3*connOfPolygonBg[i]+1]; baryOfNodes[2]+=coords[3*connOfPolygonBg[i]+2]; }
80     std::transform(baryOfNodes,baryOfNodes+3,baryOfNodes,std::bind2nd(std::multiplies<double>(),1./((double)nbOfEdges)));
81     double matrix[12];
82     if(!ComputeRotTranslationMatrixToPut3PointsOnOXY(coords+3*connOfPolygonBg[0],coords+3*connOfPolygonBg[1],baryOfNodes,matrix))
83       return std::numeric_limits<double>::max();
84     INTERP_KERNEL::AutoPtr<double> ptXY=new double[2*nbOfEdges]; ptXY[0]=0.; ptXY[1]=0.;
85     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.;
86     for(std::size_t i=2;i<nbOfEdges;i++)
87       {
88         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];
89         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];
90       }
91     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]};
92     double z=matrix[8]*pt[0]+matrix[9]*pt[1]+matrix[10]*pt[2]+matrix[11];
93     double ret=std::numeric_limits<double>::max();
94     std::size_t nbOfHint=0;
95     for(std::size_t i=0;i<nbOfEdges;i++)
96       {
97         double tmp=SquareDistanceFromPtToSegInSpaceDim2(xy,((double *)ptXY)+2*i,((double *)ptXY)+2*((i+1)%nbOfEdges),nbOfHint);
98         ret=std::min(ret,z*z+tmp);
99       }
100     if(nbOfHint==nbOfEdges)
101       ret=std::min(ret,z*z);
102     return sqrt(ret);
103   }
104
105   /*!
106    * \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
107    *              line containing [0,0,0,1] is omitted.
108    */
109   bool ComputeRotTranslationMatrixToPut3PointsOnOXY(const double *p0, const double *p1, const double *p2, double *matrix)
110   {
111     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]));
112     double c=(p1[0]-p0[0])/norm;
113     double s=sqrt(1-c*c);
114     double y=p1[2]-p0[2],z=p0[1]-p1[1];
115     norm=sqrt(y*y+z*z);
116     if(norm!=0.)
117       { y/=norm; z/=norm; }
118     double r0[9]={c,-z*s,y*s,
119                   z*s,y*y*(1-c)+c,y*z*(1-c),
120                   -y*s,z*y*(1-c),z*z*(1-c)+c};
121     // 2nd rotation matrix
122     double x=p2[0]-p0[0];
123     y=p2[1]-p0[1]; z=p2[2]-p0[2];
124     double y1=x*r0[3]+y*r0[4]+z*r0[5],z1=x*r0[6]+y*r0[7]+z*r0[8];
125     c=y1/sqrt(y1*y1+z1*z1);
126     s=sqrt(1.-c*c);
127     //
128     std::copy(r0,r0+3,matrix);
129     matrix[4]=c*r0[3]-s*r0[6]; matrix[5]=c*r0[4]-s*r0[7]; matrix[6]=c*r0[5]-s*r0[8];
130     matrix[8]=s*r0[3]+c*r0[6]; matrix[9]=s*r0[4]+c*r0[7]; matrix[10]=s*r0[5]+c*r0[8];
131     matrix[3]=-p0[0]*matrix[0]-p0[1]*matrix[1]-p0[2]*matrix[2];
132     matrix[7]=-p0[0]*matrix[4]-p0[1]*matrix[5]-p0[2]*matrix[6];
133     matrix[11]=-p0[0]*matrix[8]-p0[1]*matrix[9]-p0[2]*matrix[10];
134     return true;
135   }
136 }
137