Salome HOME
Small but useful DataArrayInt::checkUniformAndGuess
[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   elts=DataArrayInt::New(); eltsIndex=DataArrayInt::New(); eltsIndex->alloc(nbOfPoints+1,1); eltsIndex->setIJ(0,0,0); elts->alloc(0,1);
106   int *eltsIndexPtr(eltsIndex->getPointer());
107   MCAuto<DataArrayDouble> bboxArr(getBoundingBoxForBBTree(eps));
108   const double *bbox(bboxArr->begin());
109   int nbOfCells=getNumberOfCells();
110   const int *conn=_nodal_connec->getConstPointer();
111   const int *connI=_nodal_connec_index->getConstPointer();
112   double bb[2*SPACEDIM];
113   BBTree<SPACEDIM,int> myTree(&bbox[0],0,0,nbOfCells,-eps);
114   for(int i=0;i<nbOfPoints;i++)
115     {
116       eltsIndexPtr[i+1]=eltsIndexPtr[i];
117       for(int j=0;j<SPACEDIM;j++)
118         {
119           bb[2*j]=pos[SPACEDIM*i+j];
120           bb[2*j+1]=pos[SPACEDIM*i+j];
121         }
122       std::vector<int> candidates;
123       myTree.getIntersectingElems(bb,candidates);
124       for(std::vector<int>::const_iterator iter=candidates.begin();iter!=candidates.end();iter++)
125         {
126           int sz(connI[(*iter)+1]-connI[*iter]-1);
127           INTERP_KERNEL::NormalizedCellType ct((INTERP_KERNEL::NormalizedCellType)conn[connI[*iter]]);
128           bool status(false);
129           if(ct!=INTERP_KERNEL::NORM_POLYGON && ct!=INTERP_KERNEL::NORM_QPOLYG)
130             status=INTERP_KERNEL::PointLocatorAlgos<DummyClsMCUG<SPACEDIM> >::isElementContainsPoint(pos+i*SPACEDIM,ct,coords,conn+connI[*iter]+1,sz,eps);
131           else
132             {
133               if(SPACEDIM!=2)
134                 throw INTERP_KERNEL::Exception("MEDCouplingUMesh::getCellsContainingPointsAlg : not implemented yet for POLYGON and QPOLYGON in spaceDim 3 !");
135               INTERP_KERNEL::QUADRATIC_PLANAR::_precision=eps;
136               INTERP_KERNEL::QUADRATIC_PLANAR::_arc_detection_precision=eps;
137               std::vector<INTERP_KERNEL::Node *> nodes(sz);
138               INTERP_KERNEL::QuadraticPolygon *pol(0);
139               for(int j=0;j<sz;j++)
140                 {
141                   int nodeId(conn[connI[*iter]+1+j]);
142                   nodes[j]=new INTERP_KERNEL::Node(coords[nodeId*SPACEDIM],coords[nodeId*SPACEDIM+1]);
143                 }
144               if(!INTERP_KERNEL::CellModel::GetCellModel(ct).isQuadratic())
145                 pol=INTERP_KERNEL::QuadraticPolygon::BuildLinearPolygon(nodes);
146               else
147                 pol=INTERP_KERNEL::QuadraticPolygon::BuildArcCirclePolygon(nodes);
148               INTERP_KERNEL::Node *n(new INTERP_KERNEL::Node(pos[i*SPACEDIM],pos[i*SPACEDIM+1]));
149               double a(0.),b(0.),c(0.);
150               a=pol->normalizeMe(b,c); n->applySimilarity(b,c,a);
151               status=pol->isInOrOut2(n);
152               delete pol; n->decrRef();
153             }
154           if(status)
155             {
156               eltsIndexPtr[i+1]++;
157               elts->pushBackSilent(*iter);
158             }
159         }
160     }
161 }
162
163 /*!
164  * \b WARNING this method do the assumption that connectivity lies on the coordinates set.
165  * For speed reasons no check of this will be done.
166  */
167 template<class SonsGenerator>
168 MEDCouplingUMesh *MEDCouplingUMesh::buildDescendingConnectivityGen(DataArrayInt *desc, DataArrayInt *descIndx, DataArrayInt *revDesc, DataArrayInt *revDescIndx, DimM1DescNbrer nbrer) const
169 {
170   if(!desc || !descIndx || !revDesc || !revDescIndx)
171     throw INTERP_KERNEL::Exception("MEDCouplingUMesh::buildDescendingConnectivityGen : present of a null pointer in input !");
172   checkConnectivityFullyDefined();
173   int nbOfCells=getNumberOfCells();
174   int nbOfNodes=getNumberOfNodes();
175   MCAuto<DataArrayInt> revNodalIndx=DataArrayInt::New(); revNodalIndx->alloc(nbOfNodes+1,1); revNodalIndx->fillWithZero();
176   int *revNodalIndxPtr=revNodalIndx->getPointer();
177   const int *conn=_nodal_connec->getConstPointer();
178   const int *connIndex=_nodal_connec_index->getConstPointer();
179   std::string name="Mesh constituent of "; name+=getName();
180   MCAuto<MEDCouplingUMesh> ret=MEDCouplingUMesh::New(name,getMeshDimension()-SonsGenerator::DELTA);
181   ret->setCoords(getCoords());
182   ret->allocateCells(2*nbOfCells);
183   descIndx->alloc(nbOfCells+1,1);
184   MCAuto<DataArrayInt> revDesc2(DataArrayInt::New()); revDesc2->reserve(2*nbOfCells);
185   int *descIndxPtr=descIndx->getPointer(); *descIndxPtr++=0;
186   for(int eltId=0;eltId<nbOfCells;eltId++,descIndxPtr++)
187     {
188       int pos=connIndex[eltId];
189       int posP1=connIndex[eltId+1];
190       const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel((INTERP_KERNEL::NormalizedCellType)conn[pos]);
191       SonsGenerator sg(cm);
192       unsigned nbOfSons=sg.getNumberOfSons2(conn+pos+1,posP1-pos-1);
193       INTERP_KERNEL::AutoPtr<int> tmp=new int[posP1-pos];
194       for(unsigned i=0;i<nbOfSons;i++)
195         {
196           INTERP_KERNEL::NormalizedCellType cmsId;
197           unsigned nbOfNodesSon=sg.fillSonCellNodalConnectivity2(i,conn+pos+1,posP1-pos-1,tmp,cmsId);
198           for(unsigned k=0;k<nbOfNodesSon;k++)
199             if(tmp[k]>=0)
200               revNodalIndxPtr[tmp[k]+1]++;
201           ret->insertNextCell(cmsId,nbOfNodesSon,tmp);
202           revDesc2->pushBackSilent(eltId);
203         }
204       descIndxPtr[0]=descIndxPtr[-1]+(int)nbOfSons;
205     }
206   int nbOfCellsM1=ret->getNumberOfCells();
207   std::transform(revNodalIndxPtr+1,revNodalIndxPtr+nbOfNodes+1,revNodalIndxPtr,revNodalIndxPtr+1,std::plus<int>());
208   MCAuto<DataArrayInt> revNodal=DataArrayInt::New(); revNodal->alloc(revNodalIndx->back(),1);
209   std::fill(revNodal->getPointer(),revNodal->getPointer()+revNodalIndx->back(),-1);
210   int *revNodalPtr=revNodal->getPointer();
211   const int *connM1=ret->getNodalConnectivity()->getConstPointer();
212   const int *connIndexM1=ret->getNodalConnectivityIndex()->getConstPointer();
213   for(int eltId=0;eltId<nbOfCellsM1;eltId++)
214     {
215       const int *strtNdlConnOfCurCell=connM1+connIndexM1[eltId]+1;
216       const int *endNdlConnOfCurCell=connM1+connIndexM1[eltId+1];
217       for(const int *iter=strtNdlConnOfCurCell;iter!=endNdlConnOfCurCell;iter++)
218         if(*iter>=0)//for polyhedrons
219           *std::find_if(revNodalPtr+revNodalIndxPtr[*iter],revNodalPtr+revNodalIndxPtr[*iter+1],std::bind2nd(std::equal_to<int>(),-1))=eltId;
220     }
221   //
222   DataArrayInt *commonCells=0,*commonCellsI=0;
223   FindCommonCellsAlg(3,0,ret->getNodalConnectivity(),ret->getNodalConnectivityIndex(),revNodal,revNodalIndx,commonCells,commonCellsI);
224   MCAuto<DataArrayInt> commonCellsTmp(commonCells),commonCellsITmp(commonCellsI);
225   const int *commonCellsPtr(commonCells->getConstPointer()),*commonCellsIPtr(commonCellsI->getConstPointer());
226   int newNbOfCellsM1=-1;
227   MCAuto<DataArrayInt> o2nM1=DataArrayInt::ConvertIndexArrayToO2N(nbOfCellsM1,commonCells->begin(),
228                                                                                                             commonCellsI->begin(),commonCellsI->end(),newNbOfCellsM1);
229   std::vector<bool> isImpacted(nbOfCellsM1,false);
230   for(const int *work=commonCellsI->begin();work!=commonCellsI->end()-1;work++)
231     for(int work2=work[0];work2!=work[1];work2++)
232       isImpacted[commonCellsPtr[work2]]=true;
233   const int *o2nM1Ptr=o2nM1->getConstPointer();
234   MCAuto<DataArrayInt> n2oM1=o2nM1->invertArrayO2N2N2OBis(newNbOfCellsM1);
235   const int *n2oM1Ptr=n2oM1->getConstPointer();
236   MCAuto<MEDCouplingUMesh> ret2=static_cast<MEDCouplingUMesh *>(ret->buildPartOfMySelf(n2oM1->begin(),n2oM1->end(),true));
237   ret2->copyTinyInfoFrom(this);
238   desc->alloc(descIndx->back(),1);
239   int *descPtr=desc->getPointer();
240   const INTERP_KERNEL::CellModel& cmsDft=INTERP_KERNEL::CellModel::GetCellModel(INTERP_KERNEL::NORM_POINT1);
241   for(int i=0;i<nbOfCellsM1;i++,descPtr++)
242     {
243       if(!isImpacted[i])
244         *descPtr=nbrer(o2nM1Ptr[i],0,cmsDft,false,0,0);
245       else
246         {
247           if(i!=n2oM1Ptr[o2nM1Ptr[i]])
248             {
249               const INTERP_KERNEL::CellModel& cms=INTERP_KERNEL::CellModel::GetCellModel((INTERP_KERNEL::NormalizedCellType)connM1[connIndexM1[i]]);
250               *descPtr=nbrer(o2nM1Ptr[i],connIndexM1[i+1]-connIndexM1[i]-1,cms,true,connM1+connIndexM1[n2oM1Ptr[o2nM1Ptr[i]]]+1,connM1+connIndexM1[i]+1);
251             }
252           else
253             *descPtr=nbrer(o2nM1Ptr[i],0,cmsDft,false,0,0);
254         }
255     }
256   revDesc->reserve(newNbOfCellsM1);
257   revDescIndx->alloc(newNbOfCellsM1+1,1);
258   int *revDescIndxPtr=revDescIndx->getPointer(); *revDescIndxPtr++=0;
259   const int *revDesc2Ptr=revDesc2->getConstPointer();
260   for(int i=0;i<newNbOfCellsM1;i++,revDescIndxPtr++)
261     {
262       int oldCellIdM1=n2oM1Ptr[i];
263       if(!isImpacted[oldCellIdM1])
264         {
265           revDesc->pushBackSilent(revDesc2Ptr[oldCellIdM1]);
266           revDescIndxPtr[0]=revDescIndxPtr[-1]+1;
267         }
268       else
269         {
270           for(int j=commonCellsIPtr[0];j<commonCellsIPtr[1];j++)
271             revDesc->pushBackSilent(revDesc2Ptr[commonCellsPtr[j]]);
272           revDescIndxPtr[0]=revDescIndxPtr[-1]+commonCellsIPtr[1]-commonCellsIPtr[0];
273           commonCellsIPtr++;
274         }
275     }
276   //
277   return ret2.retn();
278 }
279
280