Salome HOME
Some factorization before integration of ParaMEDMEM into medcoupling python module
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingVoronoi.cxx
index 646cde82b36b2e62d5f3c6a7aec2c4da23eaa512..12807437dad38c6d93be14d39c999d30d8fe98c4 100644 (file)
@@ -33,6 +33,11 @@ Voronizer::~Voronizer()
 {
 }
 
+int Voronizer1D::getDimension() const
+{
+  return 1;
+}
+
 int Voronizer2D::getDimension() const
 {
   return 2;
@@ -145,6 +150,40 @@ MCAuto<MEDCouplingUMesh> MergeVorCells(const std::vector< MCAuto<MEDCouplingUMes
   return MergeVorCells2D(p,eps,true);
 }
 
+/*!
+ * suppress additional sub points on edges
+ */
+MCAuto<MEDCouplingUMesh> SimplifyPolygon(const MEDCouplingUMesh *m, double eps)
+{
+  if(m->getNumberOfCells()!=1)
+    throw INTERP_KERNEL::Exception("SimplifyPolygon : internal error !");
+  const int *conn(m->getNodalConnectivity()->begin()),*conni(m->getNodalConnectivityIndex()->begin());
+  int nbPtsInPolygon(conni[1]-conni[0]-1);
+  const double *coo(m->getCoords()->begin());
+  std::vector<int> resConn;
+  for(int i=0;i<nbPtsInPolygon;i++)
+    {
+      int prev(conn[(i+nbPtsInPolygon-1)%nbPtsInPolygon+1]),current(conn[i%nbPtsInPolygon+1]),zeNext(conn[(i+1)%nbPtsInPolygon+1]);
+      double a[3]={
+        coo[3*prev+0]-coo[3*current+0],
+        coo[3*prev+1]-coo[3*current+1],
+        coo[3*prev+2]-coo[3*current+2],
+      },b[3]={
+        coo[3*current+0]-coo[3*zeNext+0],
+        coo[3*current+1]-coo[3*zeNext+1],
+        coo[3*current+2]-coo[3*zeNext+2],
+      };
+      double c[3]={a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]};
+      if(sqrt(c[0]*c[0]+c[1]*c[1]+c[2]*c[2])>eps)
+        resConn.push_back(current);
+    }
+  MCAuto<MEDCouplingUMesh> ret(MEDCouplingUMesh::New("",2));
+  ret->setCoords(m->getCoords());
+  ret->allocateCells();
+  ret->insertNextCell(INTERP_KERNEL::NORM_POLYGON,resConn.size(),&resConn[0]);
+  return ret;
+}
+
 MCAuto<MEDCouplingUMesh> MergeVorCells3D(const std::vector< MCAuto<MEDCouplingUMesh> >& vcs, double eps)
 {
   std::size_t sz(vcs.size());
@@ -194,6 +233,7 @@ MCAuto<MEDCouplingUMesh> MergeVorCells3D(const std::vector< MCAuto<MEDCouplingUM
         tmp2=tmp;
       else
         tmp2=MergeVorCells2D(tmp,eps,false);
+      tmp2=SimplifyPolygon(tmp2,eps);
       const int *cPtr(tmp2->getNodalConnectivity()->begin()),*ciPtr(tmp2->getNodalConnectivityIndex()->begin());
       conn.insert(conn.end(),cPtr+1,cPtr+ciPtr[1]);
     }
@@ -212,6 +252,117 @@ MCAuto<MEDCouplingUMesh> MergeVorCells3D(const std::vector< MCAuto<MEDCouplingUM
   return ret;
 }
 
+MCAuto<MEDCouplingUMesh> MergeVorCells1D(const std::vector< MCAuto<MEDCouplingUMesh> >& vcs, double eps)
+{
+  static const int CONN_SEG2_DFT[2]={0,1};
+  if(vcs.empty())
+    throw INTERP_KERNEL::Exception("MergeVorCells1D : internal error 1 !");
+  if(vcs.size()==1)
+    return vcs[0];
+  if(vcs.size()>2)
+    throw INTERP_KERNEL::Exception("MergeVorCells1D : internal error 2 !");
+  double a0,b0,a1,b1;
+  {
+    const int *connPtr(vcs[0]->getNodalConnectivity()->begin());
+    const double *coordPtr(vcs[0]->getCoords()->begin());
+    a0=coordPtr[connPtr[1]]; b0=coordPtr[connPtr[2]];
+  }
+  {
+    const int *connPtr(vcs[1]->getNodalConnectivity()->begin());
+    const double *coordPtr(vcs[1]->getCoords()->begin());
+    a1=coordPtr[connPtr[1]]; b1=coordPtr[connPtr[2]];
+  }
+  MCAuto<MEDCouplingUMesh> ret(MEDCouplingUMesh::New("",1)); ret->allocateCells(); ret->insertNextCell(INTERP_KERNEL::NORM_SEG2,2,CONN_SEG2_DFT);
+  MCAuto<DataArrayDouble> coo(DataArrayDouble::New()); coo->alloc(2,1); ret->setCoords(coo);
+  if(fabs(b0-a1)<eps)
+    { coo->setIJ(0,0,a0); coo->setIJ(1,0,b1); }
+  else if(fabs(b1-a0)<eps)
+    { coo->setIJ(0,0,b0); coo->setIJ(1,0,a1); }
+  return ret;
+}
+
+MCAuto<MEDCouplingUMesh> MEDCoupling::Voronizer1D::doIt(const MEDCouplingUMesh *m, const DataArrayDouble *points, double eps) const
+{
+  static const int CONN_SEG2_DFT[2]={0,1};
+  if(!m || !points)
+    throw INTERP_KERNEL::Exception("Voronoize1D : null pointer !");
+  m->checkConsistencyLight();
+  points->checkAllocated();
+  if(m->getMeshDimension()!=1 || m->getSpaceDimension()!=1 || points->getNumberOfComponents()!=1)
+    throw INTERP_KERNEL::Exception("Voronoize1D : spacedim must be equal to 1 and meshdim also equal to 1 !");
+  if(m->getNumberOfCells()!=1)
+    throw INTERP_KERNEL::Exception("Voronoize1D : mesh is expected to have only one cell !");
+  int nbPts(points->getNumberOfTuples());
+  if(nbPts<1)
+    throw INTERP_KERNEL::Exception("Voronoize1D : at least one point expected !");
+  std::vector<double> bbox(4);
+  m->getBoundingBox(&bbox[0]);
+  std::vector< MCAuto<MEDCouplingUMesh> > l0(1,MCAuto<MEDCouplingUMesh>(m->deepCopy()));
+  const double *pts(points->begin());
+  for(int i=1;i<nbPts;i++)
+    {
+      MCAuto<MEDCouplingUMesh> vorTess;
+      {
+        std::vector< const MEDCouplingUMesh * > l0Bis(VecAutoToVecOfCstPt(l0));
+        vorTess=MEDCouplingUMesh::MergeUMeshes(l0Bis);
+      }
+      {
+        bool dummy;
+        int newNbNodes;
+        MCAuto<DataArrayInt> dummy3(vorTess->mergeNodes(eps,dummy,newNbNodes));
+      }
+      std::vector<int> polygsToIterOn;
+      const double *pt(pts+i);
+      vorTess->getCellsContainingPoint(pt,eps,polygsToIterOn);
+      if(polygsToIterOn.empty())
+        throw INTERP_KERNEL::Exception("Voronoize1D : a point is outside domain !");
+      if(polygsToIterOn.size()>2)
+        throw INTERP_KERNEL::Exception("Voronoize1D : overlap of points !");
+      std::vector< MCAuto<MEDCouplingUMesh> > newVorCells;
+      for(std::vector<int>::const_iterator it=polygsToIterOn.begin();it!=polygsToIterOn.end();it++)
+        {
+          int poly(*it);
+          //
+          double seed(pts[poly]),zept(*pt);
+          double mid((seed+zept)/2.);
+          //
+          MCAuto<MEDCouplingUMesh> tile(l0[poly]);
+          tile->zipCoords();
+          double a,b;
+          {
+            const int *connPtr(tile->getNodalConnectivity()->begin());
+            const double *coordPtr(tile->getCoords()->begin());
+            a=coordPtr[connPtr[1]]; b=coordPtr[connPtr[2]];
+          }
+          double pol0[2],pol1[2];
+          MCAuto<DataArrayDouble> t0(DataArrayDouble::New()); t0->alloc(3,1); t0->setIJ(0,0,zept); t0->setIJ(1,0,mid); t0->setIJ(2,0,seed);
+          t0->applyLin(1.,-a);
+          if(t0->isMonotonic(true,eps))
+            { pol0[0]=a; pol0[1]=mid; pol1[0]=mid; pol1[1]=b; }
+          else
+            { pol1[0]=a; pol1[1]=mid; pol0[0]=mid; pol0[1]=b; }
+          MCAuto<MEDCouplingUMesh> modifiedCell(MEDCouplingUMesh::New("",1)); modifiedCell->allocateCells();
+          MCAuto<DataArrayDouble> coo1(DataArrayDouble::New()); coo1->alloc(2,1); coo1->setIJ(0,0,pol1[0]); coo1->setIJ(1,0,pol1[1]);
+          modifiedCell->setCoords(coo1); modifiedCell->insertNextCell(INTERP_KERNEL::NORM_SEG2,2,CONN_SEG2_DFT);
+          //
+          MCAuto<MEDCouplingUMesh> newVorCell(MEDCouplingUMesh::New("",1)); newVorCell->allocateCells();
+          MCAuto<DataArrayDouble> coo2(DataArrayDouble::New()); coo2->alloc(2,1); coo2->setIJ(0,0,pol0[0]); coo2->setIJ(1,0,pol0[1]);
+          newVorCell->setCoords(coo2); newVorCell->insertNextCell(INTERP_KERNEL::NORM_SEG2,2,CONN_SEG2_DFT);
+          //
+          l0[poly]=modifiedCell;
+          newVorCells.push_back(newVorCell);
+        }
+      l0.push_back(MergeVorCells1D(newVorCells,eps));
+    }
+  std::vector< const MEDCouplingUMesh * > l0Bis(VecAutoToVecOfCstPt(l0));
+  MCAuto<MEDCouplingUMesh> ret(MEDCouplingUMesh::MergeUMeshes(l0Bis));
+  {
+    bool dummy; int dummy2;
+    MCAuto<DataArrayInt> dummy3(ret->mergeNodes(eps,dummy,dummy2));
+  }
+  return ret;
+}
+
 MCAuto<MEDCouplingUMesh> MEDCoupling::Voronizer2D::doIt(const MEDCouplingUMesh *m, const DataArrayDouble *points, double eps) const
 {
   if(!m || !points)
@@ -289,7 +440,7 @@ MCAuto<MEDCouplingUMesh> MEDCoupling::Voronizer2D::doIt(const MEDCouplingUMesh *
             newCoords=a->getCoords()->selectByTupleId(tmp->begin(),tmp->end());
           }
           const double *cPtr(newCoords->begin());
-          for(int i=0;i<newCoords->getNumberOfTuples();i++,cPtr+=2)
+          for(int j=0;j<newCoords->getNumberOfTuples();j++,cPtr+=2)
             {
               std::set<int> zeCandidates;
               {
@@ -297,9 +448,9 @@ MCAuto<MEDCouplingUMesh> MEDCoupling::Voronizer2D::doIt(const MEDCouplingUMesh *
                 vorTess->getCellsContainingPoint(cPtr,eps,zeCandidatesTmp);
                 zeCandidates.insert(zeCandidatesTmp.begin(),zeCandidatesTmp.end());
               }
-              std::set<int> tmp,newElementsToDo;
-              std::set_difference(zeCandidates.begin(),zeCandidates.end(),elemsDone.begin(),elemsDone.end(),std::inserter(tmp,tmp.begin()));
-              std::set_union(elemsToDo.begin(),elemsToDo.end(),tmp.begin(),tmp.end(),std::inserter(newElementsToDo,newElementsToDo.begin()));
+              std::set<int> tmp2,newElementsToDo;
+              std::set_difference(zeCandidates.begin(),zeCandidates.end(),elemsDone.begin(),elemsDone.end(),std::inserter(tmp2,tmp2.begin()));
+              std::set_union(elemsToDo.begin(),elemsToDo.end(),tmp2.begin(),tmp2.end(),std::inserter(newElementsToDo,newElementsToDo.begin()));
               elemsToDo=newElementsToDo;
             }
           newVorCells.push_back(newVorCell);
@@ -308,6 +459,10 @@ MCAuto<MEDCouplingUMesh> MEDCoupling::Voronizer2D::doIt(const MEDCouplingUMesh *
     }
   std::vector< const MEDCouplingUMesh * > l0Bis(VecAutoToVecOfCstPt(l0));
   MCAuto<MEDCouplingUMesh> ret(MEDCouplingUMesh::MergeUMeshes(l0Bis));
+  {
+    bool dummy; int dummy2;
+    MCAuto<DataArrayInt> dummy3(ret->mergeNodes(eps,dummy,dummy2));
+  }
   return ret;
 }
 
@@ -321,10 +476,9 @@ MCAuto<MEDCouplingUMesh> Split3DCellInParts(const MEDCouplingUMesh *m, const dou
   return res;
 }
 
-#include <sstream>
-
 MCAuto<MEDCouplingUMesh> MEDCoupling::Voronizer3D::doIt(const MEDCouplingUMesh *m, const DataArrayDouble *points, double eps) const
 {
+  double eps2(1.-sqrt(eps));// 2nd eps for interpolation. Here the eps is computed to feet cos(eps) ~ 1-eps^2
   if(!m || !points)
     throw INTERP_KERNEL::Exception("Voronoize3D : null pointer !");
   m->checkConsistencyLight();
@@ -355,15 +509,9 @@ MCAuto<MEDCouplingUMesh> MEDCoupling::Voronizer3D::doIt(const MEDCouplingUMesh *
       vorTess->getCellsContainingPoint(pt,eps,polygsToIterOn);
       if(polygsToIterOn.size()<1)
         throw INTERP_KERNEL::Exception("Voronoize3D : presence of a point outside the given cell !");
-      std::set<int> elemsToDo(polygsToIterOn.begin(),polygsToIterOn.end()),elemsDone;
-      std::size_t ii(0);
       std::vector< MCAuto<MEDCouplingUMesh> > newVorCells;
-      MCAuto<DataArrayInt> d(DataArrayInt::New()),dI(DataArrayInt::New()),rd(DataArrayInt::New()),rdI(DataArrayInt::New());
-      MCAuto<MEDCouplingUMesh> faces(vorTess->buildDescendingConnectivity(d,dI,rd,rdI));
-      //
-      while(!elemsToDo.empty())
+      for(int poly=0;poly<vorTess->getNumberOfCells();poly++)
         {
-          int poly(*elemsToDo.begin()); elemsToDo.erase(elemsToDo.begin()); elemsDone.insert(poly);
           const double *seed(pts+3*poly);
           MCAuto<MEDCouplingUMesh> tile(l0[poly]);
           tile->zipCoords();
@@ -381,48 +529,16 @@ MCAuto<MEDCouplingUMesh> MEDCoupling::Voronizer3D::doIt(const MEDCouplingUMesh *
           newVorCell->zipCoords();
           MCAuto<MEDCouplingUMesh> modifiedCell(cells->buildPartOfMySelfSlice(0,1,1,true));
           modifiedCell->zipCoords();
-          l0[poly]=modifiedCell;
-          if(std::find(polygsToIterOn.begin(),polygsToIterOn.end(),poly)!=polygsToIterOn.end())// we iterate on a polyhedron containg the point to add pt -> add cells sharing faces with just computed newVorCell
-            {
-              MCAuto<MEDCouplingUMesh> faces2;
-              {
-                MCAuto<DataArrayInt> d2(DataArrayInt::New()),d2I(DataArrayInt::New()),rd2(DataArrayInt::New()),rd2I(DataArrayInt::New());
-                faces2=newVorCell->buildDescendingConnectivity(d2,d2I,rd2,rd2I);
-              }
-              MCAuto<MEDCouplingUMesh> faces3(faces2->buildPartOfMySelfSlice(1,faces2->getNumberOfCells(),1,true));// suppress internal face
-              MCAuto<MEDCouplingUMesh> facesOfCurSplitPol(faces->buildPartOfMySelf(d->begin()+dI->getIJ(poly,0),d->begin()+dI->getIJ(poly+1,0),true));
-              // intersection between the out faces of newVorCell and the neighbor faces of poly polyhedron -> candidates
-              MEDCouplingNormalizedUnstructuredMesh<3,2> source_mesh_wrapper(facesOfCurSplitPol);
-              MEDCouplingNormalizedUnstructuredMesh<3,2> target_mesh_wrapper(faces3);
-              INTERP_KERNEL::Interpolation3DSurf interpolation;
-              interpolation.setMinDotBtwPlane3DSurfIntersect(1.*10000.*eps);
-              interpolation.setMaxDistance3DSurfIntersect(eps);
-              interpolation.setPrecision(1e-12);
-              std::vector<std::map<int,double> > matrix;
-              interpolation.interpolateMeshes(source_mesh_wrapper,target_mesh_wrapper,matrix,"P0P0");
-              std::set<int> zeCandidates;
-              for(std::vector<std::map<int,double> >::const_iterator it2=matrix.begin();it2!=matrix.end();it2++)
-                for(std::map<int,double>::const_iterator it3=(*it2).begin();it3!=(*it2).end();it3++)
-                  {
-                    int faceIdInVorTess(d->getIJ(dI->getIJ(poly,0)+(*it3).first,0));
-                    for(const int *it4=rd->begin()+rdI->getIJ(faceIdInVorTess,0);it4!=rd->begin()+rdI->getIJ(faceIdInVorTess+1,0);it4++)
-                      {
-                        if(*it4!=poly)
-                          zeCandidates.insert(*it4);
-                      }
-                  }
-              std::set<int> tmp,newElementsToDo;
-              std::set_difference(zeCandidates.begin(),zeCandidates.end(),elemsDone.begin(),elemsDone.end(),std::inserter(tmp,tmp.begin()));
-              std::set_union(elemsToDo.begin(),elemsToDo.end(),tmp.begin(),tmp.end(),std::inserter(newElementsToDo,newElementsToDo.begin()));
-              elemsToDo=newElementsToDo;
-            }
-          //
           newVorCells.push_back(newVorCell);
-          ii++;
+          l0[poly]=modifiedCell;
         }
       l0.push_back(MergeVorCells3D(newVorCells,eps));
     }
   std::vector< const MEDCouplingUMesh * > l0Bis(VecAutoToVecOfCstPt(l0));
   MCAuto<MEDCouplingUMesh> ret(MEDCouplingUMesh::MergeUMeshes(l0Bis));
+  {
+    bool dummy; int dummy2;
+    MCAuto<DataArrayInt> dummy3(ret->mergeNodes(eps,dummy,dummy2));
+  }
   return ret;
 }