Salome HOME
59a3067532d9dfc302a84be932bf218a3d5d738d
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingUMesh_internal.hxx
1 // Copyright (C) 2007-2016  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 (EdF)
20
21 using namespace MEDCoupling;
22
23 class MinusOneSonsGenerator
24 {
25 public:
26   MinusOneSonsGenerator(const INTERP_KERNEL::CellModel& cm):_cm(cm) { }
27   unsigned getNumberOfSons2(const int *conn, int lgth) const { return _cm.getNumberOfSons2(conn,lgth); }
28   unsigned fillSonCellNodalConnectivity2(int sonId, const int *nodalConn, int lgth, int *sonNodalConn, INTERP_KERNEL::NormalizedCellType& typeOfSon) const { return _cm.fillSonCellNodalConnectivity2(sonId,nodalConn,lgth,sonNodalConn,typeOfSon); }
29   static const int DELTA=1;
30 private:
31   const INTERP_KERNEL::CellModel& _cm;
32 };
33
34 class MinusOneSonsGeneratorBiQuadratic
35 {
36 public:
37   MinusOneSonsGeneratorBiQuadratic(const INTERP_KERNEL::CellModel& cm):_cm(cm) { }
38   unsigned getNumberOfSons2(const int *conn, int lgth) const { return _cm.getNumberOfSons2(conn,lgth); }
39   unsigned fillSonCellNodalConnectivity2(int sonId, const int *nodalConn, int lgth, int *sonNodalConn, INTERP_KERNEL::NormalizedCellType& typeOfSon) const { return _cm.fillSonCellNodalConnectivity4(sonId,nodalConn,lgth,sonNodalConn,typeOfSon); }
40   static const int DELTA=1;
41 private:
42   const INTERP_KERNEL::CellModel& _cm;
43 };
44
45 class MinusTwoSonsGenerator
46 {
47 public:
48   MinusTwoSonsGenerator(const INTERP_KERNEL::CellModel& cm):_cm(cm) { }
49   unsigned getNumberOfSons2(const int *conn, int lgth) const { return _cm.getNumberOfEdgesIn3D(conn,lgth); }
50   unsigned fillSonCellNodalConnectivity2(int sonId, const int *nodalConn, int lgth, int *sonNodalConn, INTERP_KERNEL::NormalizedCellType& typeOfSon) const { return _cm.fillSonEdgesNodalConnectivity3D(sonId,nodalConn,lgth,sonNodalConn,typeOfSon); }
51   static const int DELTA=2;
52 private:
53   const INTERP_KERNEL::CellModel& _cm;
54 };
55
56 class MicroEdgesGenerator2D
57 {
58 public:
59   MicroEdgesGenerator2D(const INTERP_KERNEL::CellModel& cm):_cm(cm) { }
60   unsigned getNumberOfSons2(const int *conn, int lgth) const { return _cm.getNumberOfMicroEdges(); }
61   unsigned fillSonCellNodalConnectivity2(int sonId, const int *nodalConn, int lgth, int *sonNodalConn, INTERP_KERNEL::NormalizedCellType& typeOfSon) const { return _cm.fillMicroEdgeNodalConnectivity(sonId,nodalConn,sonNodalConn,typeOfSon); }
62   static const int DELTA=1;
63 private:
64   const INTERP_KERNEL::CellModel& _cm;
65 };
66
67 class MicroEdgesGenerator3D
68 {
69 public:
70   MicroEdgesGenerator3D(const INTERP_KERNEL::CellModel& cm):_cm(cm) { }
71   unsigned getNumberOfSons2(const int *conn, int lgth) const { return _cm.getNumberOfMicroEdges(); }
72   unsigned fillSonCellNodalConnectivity2(int sonId, const int *nodalConn, int lgth, int *sonNodalConn, INTERP_KERNEL::NormalizedCellType& typeOfSon) const { return _cm.fillMicroEdgeNodalConnectivity(sonId,nodalConn,sonNodalConn,typeOfSon); }
73   static const int DELTA=2;
74 private:
75   const INTERP_KERNEL::CellModel& _cm;
76 };
77
78 int MEDCouplingFastNbrer(int id, unsigned nb, const INTERP_KERNEL::CellModel& cm, bool compute, const int *conn1, const int *conn2);
79 int MEDCouplingOrientationSensitiveNbrer(int id, unsigned nb, const INTERP_KERNEL::CellModel& cm, bool compute, const int *conn1, const int *conn2);
80
81 namespace MEDCoupling
82 {
83   template<const int SPACEDIMM>
84   class DummyClsMCUG
85   {
86   public:
87     static const int MY_SPACEDIM=SPACEDIMM;
88     static const int MY_MESHDIM=8;
89     typedef int MyConnType;
90     static const INTERP_KERNEL::NumberingPolicy My_numPol=INTERP_KERNEL::ALL_C_MODE;
91     // begin
92     // useless, but for windows compilation ...
93     const double* getCoordinatesPtr() const { return 0; }
94     const int* getConnectivityPtr() const { return 0; }
95     const int* getConnectivityIndexPtr() const { return 0; }
96     INTERP_KERNEL::NormalizedCellType getTypeOfElement(int) const { return (INTERP_KERNEL::NormalizedCellType)0; }
97     // end
98   };
99 }
100
101 template<int SPACEDIM>
102 void MEDCouplingUMesh::getCellsContainingPointsAlg(const double *coords, const double *pos, int nbOfPoints,
103                                                    double eps, MCAuto<DataArrayInt>& elts, MCAuto<DataArrayInt>& eltsIndex) const
104 {
105   // Override precision for this method only:
106   INTERP_KERNEL::QuadraticPlanarPrecision prec(eps);
107
108   elts=DataArrayInt::New(); eltsIndex=DataArrayInt::New(); eltsIndex->alloc(nbOfPoints+1,1); eltsIndex->setIJ(0,0,0); elts->alloc(0,1);
109   int *eltsIndexPtr(eltsIndex->getPointer());
110   MCAuto<DataArrayDouble> bboxArr(getBoundingBoxForBBTree(eps));
111   const double *bbox(bboxArr->begin());
112   int nbOfCells=getNumberOfCells();
113   const int *conn=_nodal_connec->getConstPointer();
114   const int *connI=_nodal_connec_index->getConstPointer();
115   double bb[2*SPACEDIM];
116   BBTree<SPACEDIM,int> myTree(&bbox[0],0,0,nbOfCells,-eps);
117   for(int i=0;i<nbOfPoints;i++)
118     {
119       eltsIndexPtr[i+1]=eltsIndexPtr[i];
120       for(int j=0;j<SPACEDIM;j++)
121         {
122           bb[2*j]=pos[SPACEDIM*i+j];
123           bb[2*j+1]=pos[SPACEDIM*i+j];
124         }
125       std::vector<int> candidates;
126       myTree.getIntersectingElems(bb,candidates);
127       for(std::vector<int>::const_iterator iter=candidates.begin();iter!=candidates.end();iter++)
128         {
129           int sz(connI[(*iter)+1]-connI[*iter]-1);
130           INTERP_KERNEL::NormalizedCellType ct((INTERP_KERNEL::NormalizedCellType)conn[connI[*iter]]);
131           bool status(false);
132           if(ct!=INTERP_KERNEL::NORM_POLYGON && ct!=INTERP_KERNEL::NORM_QPOLYG)
133             status=INTERP_KERNEL::PointLocatorAlgos<DummyClsMCUG<SPACEDIM> >::isElementContainsPoint(pos+i*SPACEDIM,ct,coords,conn+connI[*iter]+1,sz,eps);
134           else
135             {
136               if(SPACEDIM!=2)
137                 throw INTERP_KERNEL::Exception("MEDCouplingUMesh::getCellsContainingPointsAlg : not implemented yet for POLYGON and QPOLYGON in spaceDim 3 !");
138               std::vector<INTERP_KERNEL::Node *> nodes(sz);
139               INTERP_KERNEL::QuadraticPolygon *pol(0);
140               for(int j=0;j<sz;j++)
141                 {
142                   int nodeId(conn[connI[*iter]+1+j]);
143                   nodes[j]=new INTERP_KERNEL::Node(coords[nodeId*SPACEDIM],coords[nodeId*SPACEDIM+1]);
144                 }
145               if(!INTERP_KERNEL::CellModel::GetCellModel(ct).isQuadratic())
146                 pol=INTERP_KERNEL::QuadraticPolygon::BuildLinearPolygon(nodes);
147               else
148                 pol=INTERP_KERNEL::QuadraticPolygon::BuildArcCirclePolygon(nodes);
149               INTERP_KERNEL::Node *n(new INTERP_KERNEL::Node(pos[i*SPACEDIM],pos[i*SPACEDIM+1]));
150               double a(0.),b(0.),c(0.);
151               a=pol->normalizeMe(b,c); n->applySimilarity(b,c,a);
152               status=pol->isInOrOut2(n);
153               delete pol; n->decrRef();
154             }
155           if(status)
156             {
157               eltsIndexPtr[i+1]++;
158               elts->pushBackSilent(*iter);
159             }
160         }
161     }
162 }
163
164 /*!
165  * \b WARNING this method do the assumption that connectivity lies on the coordinates set.
166  * For speed reasons no check of this will be done.
167  */
168 template<class SonsGenerator>
169 MEDCouplingUMesh *MEDCouplingUMesh::buildDescendingConnectivityGen(DataArrayInt *desc, DataArrayInt *descIndx, DataArrayInt *revDesc, DataArrayInt *revDescIndx, DimM1DescNbrer nbrer) const
170 {
171   if(!desc || !descIndx || !revDesc || !revDescIndx)
172     throw INTERP_KERNEL::Exception("MEDCouplingUMesh::buildDescendingConnectivityGen : present of a null pointer in input !");
173   checkConnectivityFullyDefined();
174   int nbOfCells=getNumberOfCells();
175   int nbOfNodes=getNumberOfNodes();
176   MCAuto<DataArrayInt> revNodalIndx=DataArrayInt::New(); revNodalIndx->alloc(nbOfNodes+1,1); revNodalIndx->fillWithZero();
177   int *revNodalIndxPtr=revNodalIndx->getPointer();
178   const int *conn=_nodal_connec->getConstPointer();
179   const int *connIndex=_nodal_connec_index->getConstPointer();
180   std::string name="Mesh constituent of "; name+=getName();
181   MCAuto<MEDCouplingUMesh> ret=MEDCouplingUMesh::New(name,getMeshDimension()-SonsGenerator::DELTA);
182   ret->setCoords(getCoords());
183   ret->allocateCells(2*nbOfCells);
184   descIndx->alloc(nbOfCells+1,1);
185   MCAuto<DataArrayInt> revDesc2(DataArrayInt::New()); revDesc2->reserve(2*nbOfCells);
186   int *descIndxPtr=descIndx->getPointer(); *descIndxPtr++=0;
187   for(int eltId=0;eltId<nbOfCells;eltId++,descIndxPtr++)
188     {
189       int pos=connIndex[eltId];
190       int posP1=connIndex[eltId+1];
191       const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel((INTERP_KERNEL::NormalizedCellType)conn[pos]);
192       SonsGenerator sg(cm);
193       unsigned nbOfSons=sg.getNumberOfSons2(conn+pos+1,posP1-pos-1);
194       INTERP_KERNEL::AutoPtr<int> tmp=new int[posP1-pos];
195       for(unsigned i=0;i<nbOfSons;i++)
196         {
197           INTERP_KERNEL::NormalizedCellType cmsId;
198           unsigned nbOfNodesSon=sg.fillSonCellNodalConnectivity2(i,conn+pos+1,posP1-pos-1,tmp,cmsId);
199           for(unsigned k=0;k<nbOfNodesSon;k++)
200             if(tmp[k]>=0)
201               revNodalIndxPtr[tmp[k]+1]++;
202           ret->insertNextCell(cmsId,nbOfNodesSon,tmp);
203           revDesc2->pushBackSilent(eltId);
204         }
205       descIndxPtr[0]=descIndxPtr[-1]+(int)nbOfSons;
206     }
207   int nbOfCellsM1=ret->getNumberOfCells();
208   std::transform(revNodalIndxPtr+1,revNodalIndxPtr+nbOfNodes+1,revNodalIndxPtr,revNodalIndxPtr+1,std::plus<int>());
209   MCAuto<DataArrayInt> revNodal=DataArrayInt::New(); revNodal->alloc(revNodalIndx->back(),1);
210   std::fill(revNodal->getPointer(),revNodal->getPointer()+revNodalIndx->back(),-1);
211   int *revNodalPtr=revNodal->getPointer();
212   const int *connM1=ret->getNodalConnectivity()->getConstPointer();
213   const int *connIndexM1=ret->getNodalConnectivityIndex()->getConstPointer();
214   for(int eltId=0;eltId<nbOfCellsM1;eltId++)
215     {
216       const int *strtNdlConnOfCurCell=connM1+connIndexM1[eltId]+1;
217       const int *endNdlConnOfCurCell=connM1+connIndexM1[eltId+1];
218       for(const int *iter=strtNdlConnOfCurCell;iter!=endNdlConnOfCurCell;iter++)
219         if(*iter>=0)//for polyhedrons
220           *std::find_if(revNodalPtr+revNodalIndxPtr[*iter],revNodalPtr+revNodalIndxPtr[*iter+1],std::bind2nd(std::equal_to<int>(),-1))=eltId;
221     }
222   //
223   DataArrayInt *commonCells=0,*commonCellsI=0;
224   FindCommonCellsAlg(3,0,ret->getNodalConnectivity(),ret->getNodalConnectivityIndex(),revNodal,revNodalIndx,commonCells,commonCellsI);
225   MCAuto<DataArrayInt> commonCellsTmp(commonCells),commonCellsITmp(commonCellsI);
226   const int *commonCellsPtr(commonCells->getConstPointer()),*commonCellsIPtr(commonCellsI->getConstPointer());
227   int newNbOfCellsM1=-1;
228   MCAuto<DataArrayInt> o2nM1=DataArrayInt::ConvertIndexArrayToO2N(nbOfCellsM1,commonCells->begin(),
229                                                                                                             commonCellsI->begin(),commonCellsI->end(),newNbOfCellsM1);
230   std::vector<bool> isImpacted(nbOfCellsM1,false);
231   for(const int *work=commonCellsI->begin();work!=commonCellsI->end()-1;work++)
232     for(int work2=work[0];work2!=work[1];work2++)
233       isImpacted[commonCellsPtr[work2]]=true;
234   const int *o2nM1Ptr=o2nM1->getConstPointer();
235   MCAuto<DataArrayInt> n2oM1=o2nM1->invertArrayO2N2N2OBis(newNbOfCellsM1);
236   const int *n2oM1Ptr=n2oM1->getConstPointer();
237   MCAuto<MEDCouplingUMesh> ret2=static_cast<MEDCouplingUMesh *>(ret->buildPartOfMySelf(n2oM1->begin(),n2oM1->end(),true));
238   ret2->copyTinyInfoFrom(this);
239   desc->alloc(descIndx->back(),1);
240   int *descPtr=desc->getPointer();
241   const INTERP_KERNEL::CellModel& cmsDft=INTERP_KERNEL::CellModel::GetCellModel(INTERP_KERNEL::NORM_POINT1);
242   for(int i=0;i<nbOfCellsM1;i++,descPtr++)
243     {
244       if(!isImpacted[i])
245         *descPtr=nbrer(o2nM1Ptr[i],0,cmsDft,false,0,0);
246       else
247         {
248           if(i!=n2oM1Ptr[o2nM1Ptr[i]])
249             {
250               const INTERP_KERNEL::CellModel& cms=INTERP_KERNEL::CellModel::GetCellModel((INTERP_KERNEL::NormalizedCellType)connM1[connIndexM1[i]]);
251               *descPtr=nbrer(o2nM1Ptr[i],connIndexM1[i+1]-connIndexM1[i]-1,cms,true,connM1+connIndexM1[n2oM1Ptr[o2nM1Ptr[i]]]+1,connM1+connIndexM1[i]+1);
252             }
253           else
254             *descPtr=nbrer(o2nM1Ptr[i],0,cmsDft,false,0,0);
255         }
256     }
257   revDesc->reserve(newNbOfCellsM1);
258   revDescIndx->alloc(newNbOfCellsM1+1,1);
259   int *revDescIndxPtr=revDescIndx->getPointer(); *revDescIndxPtr++=0;
260   const int *revDesc2Ptr=revDesc2->getConstPointer();
261   for(int i=0;i<newNbOfCellsM1;i++,revDescIndxPtr++)
262     {
263       int oldCellIdM1=n2oM1Ptr[i];
264       if(!isImpacted[oldCellIdM1])
265         {
266           revDesc->pushBackSilent(revDesc2Ptr[oldCellIdM1]);
267           revDescIndxPtr[0]=revDescIndxPtr[-1]+1;
268         }
269       else
270         {
271           for(int j=commonCellsIPtr[0];j<commonCellsIPtr[1];j++)
272             revDesc->pushBackSilent(revDesc2Ptr[commonCellsPtr[j]]);
273           revDescIndxPtr[0]=revDescIndxPtr[-1]+commonCellsIPtr[1]-commonCellsIPtr[0];
274           commonCellsIPtr++;
275         }
276     }
277   //
278   return ret2.retn();
279 }
280
281