From: ageay Date: Tue, 26 Mar 2013 11:48:45 +0000 (+0000) Subject: Barycenter on polyhedron with volume == 0... X-Git-Tag: V6_main_FINAL~235 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=572a082bcd1ce3dce9b2b33b306a074eac20b8c6;p=tools%2Fmedcoupling.git Barycenter on polyhedron with volume == 0... --- diff --git a/src/INTERP_KERNEL/VolSurfFormulae.hxx b/src/INTERP_KERNEL/VolSurfFormulae.hxx index c59737068..1b2c56573 100644 --- a/src/INTERP_KERNEL/VolSurfFormulae.hxx +++ b/src/INTERP_KERNEL/VolSurfFormulae.hxx @@ -495,6 +495,39 @@ namespace INTERP_KERNEL } } + template + inline void computePolygonBarycenter3D(const ConnType *connec, int lgth, const double *coords, double *res) + { + double area[3]; + areaVectorOfPolygon(connec,lgth,coords,area); + double norm=sqrt(area[0]*area[0]+area[1]*area[1]+area[2]*area[2]); + area[0]/=norm; area[1]/=norm; area[2]/=norm; + res[0]=0.; res[1]=0.; res[2]=0.; + for(int i=1;i::coo2C(connec[0])]+ + coords[3*OTT::coo2C(connec[i])]+ + coords[3*OTT::coo2C(connec[i+1])])/3.; + v[1]=(coords[3*OTT::coo2C(connec[0])+1]+ + coords[3*OTT::coo2C(connec[i])+1]+ + coords[3*OTT::coo2C(connec[i+1])+1])/3.; + v[2]=(coords[3*OTT::coo2C(connec[0])+2]+ + coords[3*OTT::coo2C(connec[i])+2]+ + coords[3*OTT::coo2C(connec[i+1])+2])/3.; + ConnType tmpConn[3]={connec[0],connec[i],connec[i+1]}; + areaVectorOfPolygon(tmpConn,3,coords,tmpArea); + double norm2=sqrt(tmpArea[0]*tmpArea[0]+tmpArea[1]*tmpArea[1]+tmpArea[2]*tmpArea[2]); + if(norm2>1e-12) + { + tmpArea[0]/=norm2; tmpArea[1]/=norm2; tmpArea[2]/=norm2; + double signOfArea=area[0]*tmpArea[0]+area[1]*tmpArea[1]+area[2]*tmpArea[2]; + res[0]+=signOfArea*norm2*v[0]/norm; res[1]+=signOfArea*norm2*v[1]/norm; res[2]+=signOfArea*norm2*v[2]/norm; + } + } + } + inline double integrationOverA3DLine(double u1, double v1, double u2, double v2, double A, double B, double C) { 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))+ @@ -515,6 +548,8 @@ namespace INTERP_KERNEL double normal[3]; areaVectorOfPolygon(work,nbOfNodesOfCurFace,coords,normal); double normOfNormal=sqrt(normal[0]*normal[0]+normal[1]*normal[1]+normal[2]*normal[2]); + if(normOfNormal::min()) + continue; normal[0]/=normOfNormal; normal[1]/=normOfNormal; normal[2]/=normOfNormal; double u[2]={normal[1],-normal[0]}; double s=sqrt(u[0]*u[0]+u[1]*u[1]); @@ -561,7 +596,32 @@ namespace INTERP_KERNEL work=work2+1; } double vol=calculateVolumeForPolyh2(connec,lgth,coords); - res[0]/=vol; res[1]/=vol; res[2]/=vol; + if(fabs(vol)>std::numeric_limits::min()) + { + res[0]/=vol; res[1]/=vol; res[2]/=vol; + } + else + { + double sum=0.; + res[0]=0.; res[1]=0.; res[2]=0.; + work=connec; + for(std::size_t i=0;i(work,nbOfNodesOfCurFace,coords,normal); + double normOfNormal=sqrt(normal[0]*normal[0]+normal[1]*normal[1]+normal[2]*normal[2]); + if(normOfNormal::min()) + continue; + sum+=normOfNormal; + double tmpBary[3]; + computePolygonBarycenter3D(work,nbOfNodesOfCurFace,coords,tmpBary); + res[0]+=normOfNormal*tmpBary[0]; res[1]+=normOfNormal*tmpBary[1]; res[2]+=normOfNormal*tmpBary[2]; + work=work2+1; + } + res[0]/=sum; res[1]/=sum; res[2]/=sum; + } } // ============================================================================================================================================ @@ -686,39 +746,6 @@ namespace INTERP_KERNEL res[0]/=3.*area; res[1]/=3.*area; } - - template - inline void computePolygonBarycenter3D(const ConnType *connec, int lgth, const double *coords, double *res) - { - double area[3]; - areaVectorOfPolygon(connec,lgth,coords,area); - double norm=sqrt(area[0]*area[0]+area[1]*area[1]+area[2]*area[2]); - area[0]/=norm; area[1]/=norm; area[2]/=norm; - res[0]=0.; res[1]=0.; res[2]=0.; - for(int i=1;i::coo2C(connec[0])]+ - coords[3*OTT::coo2C(connec[i])]+ - coords[3*OTT::coo2C(connec[i+1])])/3.; - v[1]=(coords[3*OTT::coo2C(connec[0])+1]+ - coords[3*OTT::coo2C(connec[i])+1]+ - coords[3*OTT::coo2C(connec[i+1])+1])/3.; - v[2]=(coords[3*OTT::coo2C(connec[0])+2]+ - coords[3*OTT::coo2C(connec[i])+2]+ - coords[3*OTT::coo2C(connec[i+1])+2])/3.; - ConnType tmpConn[3]={connec[0],connec[i],connec[i+1]}; - areaVectorOfPolygon(tmpConn,3,coords,tmpArea); - double norm2=sqrt(tmpArea[0]*tmpArea[0]+tmpArea[1]*tmpArea[1]+tmpArea[2]*tmpArea[2]); - if(norm2>1e-12) - { - tmpArea[0]/=norm2; tmpArea[1]/=norm2; tmpArea[2]/=norm2; - double signOfArea=area[0]*tmpArea[0]+area[1]*tmpArea[1]+area[2]*tmpArea[2]; - res[0]+=signOfArea*norm2*v[0]/norm; res[1]+=signOfArea*norm2*v[1]/norm; res[2]+=signOfArea*norm2*v[2]/norm; - } - } - } } #endif diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index e20306b2a..eec0bc36a 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -11714,7 +11714,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertTrue(d.isEqual(DataArrayInt([6,16,5,15,4,14,3,13,2,12,1,11,0,10],7,2))) pass - def testSwig2DAPow1(self): + def testSwigDAPow1(self): d=DataArrayInt(10) d.iota(0) d1=d.deepCpy() @@ -11758,6 +11758,27 @@ class MEDCouplingBasicsTest(unittest.TestCase): d4**=d3 self.assertTrue(d4.isEqual(DataArrayDouble([1.,sqrt(2.),1.4422495703074083,sqrt(2.)]),1e-14)) pass + + def testSwig2Baryenter3DForCellsWithVolumeZero1(self): + coo=DataArrayDouble([0.,0.,0.,1.,0.,0.,0.,1.,0.],3,3) + m2=MEDCouplingUMesh("mesh",2) + m2.allocateCells(0) + m2.insertNextCell(NORM_POLYGON,[0,1,2]) + m2.setCoords(coo) + m2.checkCoherency1() + # + coo2=DataArrayDouble([0.,0.,0.,0.,0.,0.,0.,0.,2.],3,3) + m1=MEDCouplingUMesh("mesh",1) + m1.allocateCells(0) + m1.insertNextCell(NORM_SEG2,[0,1]) + m1.insertNextCell(NORM_SEG2,[1,2]) + m1.setCoords(coo2) + m1.checkCoherency1() + # + m3=m2.buildExtrudedMesh(m1,0) + m3.insertNextCell(NORM_POLYHED,[3,4,5,-1,8,7,6,-1,4,3,6,7,-1,5,4,7,8,-1,5,4,-1,3,5,8,6])# addition of face #4 with null surface + self.assertTrue(m3.getBarycenterAndOwner().isEqual(DataArrayDouble([0.3333333333333333,0.3333333333333333,0.,0.3333333333333333,0.3333333333333333,1.,0.3333333333333333,0.3333333333333333,1.],3,3),1e-13)) + pass def setUp(self): pass