1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "InterpKernelCellSimplify.hxx"
21 #include "CellModel.hxx"
32 using namespace INTERP_KERNEL;
35 * This method takes as input a cell with type 'type' and whose connectivity is defined by (conn,lgth)
36 * It retrieves the same cell with a potentially different type (in return) whose connectivity is defined by (retConn,retLgth)
37 * \b WARNING for optimization reason the arrays 'retConn' and 'conn' can overlapped !
39 INTERP_KERNEL::NormalizedCellType CellSimplify::simplifyDegeneratedCell(INTERP_KERNEL::NormalizedCellType type, const int *conn, int lgth,
40 int *retConn, int& retLgth) throw(INTERP_KERNEL::Exception)
42 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type);
43 std::set<int> c(conn,conn+lgth);
45 bool isObviousNonDegeneratedCell=((int)c.size()==lgth);
46 if(cm.isQuadratic() || isObviousNonDegeneratedCell)
47 {//quadratic do nothing for the moment.
49 int *tmp=new int[lgth];//no direct std::copy ! overlapping of conn and retConn !
50 std::copy(conn,conn+lgth,tmp);
51 std::copy(tmp,tmp+lgth,retConn);
55 if(cm.getDimension()==2)
57 int *tmp=new int[lgth];
60 for(int i=1;i<lgth;i++)
61 if(std::find(tmp,tmp+newPos,conn[i])==tmp+newPos)
62 tmp[newPos++]=conn[i];
63 INTERP_KERNEL::NormalizedCellType ret=tryToUnPoly2D(cm.isQuadratic(),tmp,newPos,retConn,retLgth);
67 if(cm.getDimension()==3)
69 int nbOfFaces,lgthOfPolyhConn;
70 int *zipFullReprOfPolyh=getFullPolyh3DCell(type,conn,lgth,nbOfFaces,lgthOfPolyhConn);
71 INTERP_KERNEL::NormalizedCellType ret=tryToUnPoly3D(zipFullReprOfPolyh,nbOfFaces,lgthOfPolyhConn,retConn,retLgth);
72 delete [] zipFullReprOfPolyh;
75 throw INTERP_KERNEL::Exception("CellSimplify::simplifyDegeneratedCell : works only with 2D and 3D cell !");
80 * This static method tries to unpolygonize a cell whose connectivity is given by 'conn' and 'lgth'.
81 * Contrary to INTERP_KERNEL::CellSimplify::simplifyDegeneratedCell method 'conn' and 'retConn' do not overlap.
83 INTERP_KERNEL::NormalizedCellType CellSimplify::tryToUnPoly2D(bool isQuad, const int *conn, int lgth, int *retConn, int& retLgth)
86 std::copy(conn,conn+lgth,retConn);
92 return INTERP_KERNEL::NORM_TRI3;
94 return INTERP_KERNEL::NORM_QUAD4;
96 return INTERP_KERNEL::NORM_POLYGON;
104 return INTERP_KERNEL::NORM_TRI6;
106 return INTERP_KERNEL::NORM_QUAD8;
108 return INTERP_KERNEL::NORM_QPOLYG;
114 * This method takes as input a 3D linear cell and put its representation in returned array. Warning the returned array has to be deallocated.
115 * The length of the returned array is specified by out parameter
116 * The format of output array is the following :
117 * 1,2,3,-1,3,4,2,-1,3,4,1,-1,1,2,4,NORM_TRI3,NORM_TRI3,NORM_TRI3 (faces type at the end of classical polyhedron nodal description)
119 int *CellSimplify::getFullPolyh3DCell(INTERP_KERNEL::NormalizedCellType type, const int *conn, int lgth,
120 int& retNbOfFaces, int& retLgth)
122 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type);
123 unsigned nbOfFaces=cm.getNumberOfSons2(conn,lgth);
124 int *tmp=new int[nbOfFaces*(lgth+1)];
126 std::vector<int> faces;
127 for(unsigned j=0;j<nbOfFaces;j++)
129 INTERP_KERNEL::NormalizedCellType type2;
130 unsigned offset=cm.fillSonCellNodalConnectivity2(j,conn,lgth,work,type2);
132 int *tmp2=new int[offset];
135 for(unsigned k=1;k<offset;k++)
136 if(std::find(tmp2,tmp2+newPos,work[k])==tmp2+newPos)
137 tmp2[newPos++]=work[k];
144 faces.push_back(tryToUnPoly2D(CellModel::GetCellModel(type2).isQuadratic(),tmp2,newPos,work,tmp3));
150 std::copy(faces.begin(),faces.end(),--work);
151 retNbOfFaces=(int)faces.size();
152 retLgth=(int)std::distance(tmp,work);
157 * This static method tries to unpolygonize a cell whose connectivity is given by 'conn' (format is the same as specified in
158 * method INTERP_KERNEL::CellSimplify::getFullPolyh3DCell ) and 'lgth'+'nbOfFaces'.
159 * Contrary to INTERP_KERNEL::CellSimplify::simplifyDegeneratedCell method 'conn' and 'retConn' do not overlap.
161 INTERP_KERNEL::NormalizedCellType CellSimplify::tryToUnPoly3D(const int *conn, int nbOfFaces, int lgth, int *retConn, int& retLgth)
163 std::set<int> nodes(conn,conn+lgth);
165 int nbOfNodes=(int)nodes.size();
166 int magicNumber=100*nbOfNodes+nbOfFaces;
170 return tryToUnPolyHex8(conn,nbOfFaces,lgth,retConn,retLgth);
172 return tryToUnPolyHexp12(conn,nbOfFaces,lgth,retConn,retLgth);
174 return tryToUnPolyPenta6(conn,nbOfFaces,lgth,retConn,retLgth);
176 return tryToUnPolyPyra5(conn,nbOfFaces,lgth,retConn,retLgth);
178 return tryToUnPolyTetra4(conn,nbOfFaces,lgth,retConn,retLgth);
181 std::copy(conn,conn+lgth,retConn);
182 return INTERP_KERNEL::NORM_POLYHED;
186 bool CellSimplify::orientOppositeFace(const int *baseFace, int *retConn, const int *sideFace, int lgthBaseFace)
188 std::vector<int> tmp2;
189 std::set<int> bases(baseFace,baseFace+lgthBaseFace);
190 std::set<int> sides(sideFace,sideFace+4);
191 std::set_intersection(bases.begin(),bases.end(),sides.begin(),sides.end(),std::back_insert_iterator< std::vector<int> >(tmp2));
194 std::vector< std::pair<int,int> > baseEdges(lgthBaseFace);
195 std::vector< std::pair<int,int> > oppEdges(lgthBaseFace);
196 std::vector< std::pair<int,int> > sideEdges(4);
197 for(int i=0;i<lgthBaseFace;i++)
199 baseEdges[i]=std::pair<int,int>(baseFace[i],baseFace[(i+1)%lgthBaseFace]);
200 oppEdges[i]=std::pair<int,int>(retConn[i],retConn[(i+1)%lgthBaseFace]);
203 sideEdges[i]=std::pair<int,int>(sideFace[i],sideFace[(i+1)%4]);
204 std::vector< std::pair<int,int> > tmp;
205 std::set< std::pair<int,int> > baseEdgesS(baseEdges.begin(),baseEdges.end());
206 std::set< std::pair<int,int> > sideEdgesS(sideEdges.begin(),sideEdges.end());
207 std::set_intersection(baseEdgesS.begin(),baseEdgesS.end(),sideEdgesS.begin(),sideEdgesS.end(),std::back_insert_iterator< std::vector< std::pair<int,int> > >(tmp));
213 std::pair<int,int> p=sideEdges[i];
214 std::pair<int,int> r(p.second,p.first);
217 //end reverse sideFace
218 std::set< std::pair<int,int> > baseEdgesS2(baseEdges.begin(),baseEdges.end());
219 std::set< std::pair<int,int> > sideEdgesS2(sideEdges.begin(),sideEdges.end());
220 std::set_intersection(baseEdgesS2.begin(),baseEdgesS2.end(),sideEdgesS2.begin(),sideEdgesS2.end(),std::back_insert_iterator< std::vector< std::pair<int,int> > >(tmp));
227 std::pair<int,int> pInOpp;
228 for(int i=0;i<4 && !found;i++)
229 {//finding the pair(edge) in sideFace that do not include any node of tmp[0] edge
230 found=(tmp[0].first!=sideEdges[i].first && tmp[0].first!=sideEdges[i].second &&
231 tmp[0].second!=sideEdges[i].first && tmp[0].second!=sideEdges[i].second);
233 {//found ! reverse it
234 pInOpp.first=sideEdges[i].second;
235 pInOpp.second=sideEdges[i].first;
240 int pos=(int)std::distance(baseEdges.begin(),std::find(baseEdges.begin(),baseEdges.end(),tmp[0]));
241 std::vector< std::pair<int,int> >::iterator it=std::find(oppEdges.begin(),oppEdges.end(),pInOpp);
242 if(it==oppEdges.end())//the opposite edge of side face is not found opposite face ... maybe problem of orientation of polyhedron
244 int pos2=(int)std::distance(oppEdges.begin(),it);
247 offset+=lgthBaseFace;
248 //this is the end copy the result
249 int *tmp3=new int[lgthBaseFace];
250 for(int i=0;i<lgthBaseFace;i++)
251 tmp3[(offset+i)%lgthBaseFace]=oppEdges[i].first;
252 std::copy(tmp3,tmp3+lgthBaseFace,retConn);
257 bool CellSimplify::isWellOriented(const int *baseFace, int *retConn, const int *sideFace, int lgthBaseFace)
263 * This method is trying to permute the connectivity of 'oppFace' face so that the k_th node of 'baseFace' is associated to the
264 * k_th node in retConnOfOppFace. Excluded faces 'baseFace' and 'oppFace' all the other faces in 'conn' must be QUAD4 faces.
265 * If the arragement process succeeds true is returned and retConnOfOppFace is filled.
267 bool CellSimplify::tryToArrangeOppositeFace(const int *conn, int lgth, int lgthBaseFace, const int *baseFace, const int *oppFace, int nbOfFaces, int *retConnOfOppFace)
269 retConnOfOppFace[0]=oppFace[0];
270 for(int j=1;j<lgthBaseFace;j++)
271 retConnOfOppFace[j]=oppFace[lgthBaseFace-j];
272 const int *curFace=conn;
275 for(int i=0;i<nbOfFaces && ret;i++)
277 if(curFace!=baseFace && curFace!=oppFace)
280 ret=orientOppositeFace(baseFace,retConnOfOppFace,curFace,lgthBaseFace);
282 ret=isWellOriented(baseFace,retConnOfOppFace,curFace,lgthBaseFace);
285 curFace=std::find(curFace,conn+lgth,-1);
292 * Cell with 'conn' connectivity has been detected as a good candidate. Full check of this. If yes NORM_HEXA8 is returned.
293 * This method is only callable if in 'conn' there is 8 nodes and 6 faces.
294 * If fails a POLYHED is returned.
296 INTERP_KERNEL::NormalizedCellType CellSimplify::tryToUnPolyHex8(const int *conn, int nbOfFaces, int lgth, int *retConn, int& retLgth)
298 if(std::find_if(conn+lgth,conn+lgth+nbOfFaces,std::bind2nd(std::not_equal_to<int>(),(int)INTERP_KERNEL::NORM_QUAD4))==conn+lgth+nbOfFaces)
299 {//6 faces are QUAD4.
301 std::set<int> conn1(conn,conn+4);
302 for(int i=1;i<6 && oppositeFace<0;i++)
304 std::vector<int> tmp;
305 std::set<int> conn2(conn+5*i,conn+5*i+4);
306 std::set_intersection(conn1.begin(),conn1.end(),conn2.begin(),conn2.end(),std::back_insert_iterator< std::vector<int> >(tmp));
311 {//oppositeFace of face#0 found.
313 if(tryToArrangeOppositeFace(conn,lgth,4,conn,conn+5*oppositeFace,6,tmp2))
315 std::copy(conn,conn+4,retConn);
316 std::copy(tmp2,tmp2+4,retConn+4);
318 return INTERP_KERNEL::NORM_HEXA8;
323 std::copy(conn,conn+lgth,retConn);
324 return INTERP_KERNEL::NORM_POLYHED;
327 INTERP_KERNEL::NormalizedCellType CellSimplify::tryToUnPolyHexp12(const int *conn, int nbOfFaces, int lgth, int *retConn, int& retLgth)
329 std::size_t nbOfHexagon=std::count(conn+lgth,conn+lgth+nbOfFaces,(int)INTERP_KERNEL::NORM_POLYGON);
330 std::size_t nbOfQuad=std::count(conn+lgth,conn+lgth+nbOfFaces,(int)INTERP_KERNEL::NORM_QUAD4);
331 if(nbOfQuad==6 && nbOfHexagon==2)
333 const int *hexag0=std::find(conn+lgth,conn+lgth+nbOfFaces,(int)INTERP_KERNEL::NORM_POLYGON);
334 std::size_t hexg0Id=std::distance(conn+lgth,hexag0);
335 const int *hexag1=std::find(hexag0+1,conn+lgth+nbOfFaces,(int)INTERP_KERNEL::NORM_POLYGON);
336 std::size_t hexg1Id=std::distance(conn+lgth,hexag1);
337 const int *connHexag0=conn+5*hexg0Id;
338 std::size_t lgthH0=std::distance(connHexag0,std::find(connHexag0,conn+lgth,-1));
341 const int *connHexag1=conn+5*hexg0Id+7+(hexg1Id-hexg0Id-1)*5;
342 std::size_t lgthH1=std::distance(connHexag1,std::find(connHexag1,conn+lgth,-1));
345 std::vector<int> tmp;
346 std::set<int> conn1(connHexag0,connHexag0+6);
347 std::set<int> conn2(connHexag1,connHexag1+6);
348 std::set_intersection(conn1.begin(),conn1.end(),conn2.begin(),conn2.end(),std::back_insert_iterator< std::vector<int> >(tmp));
352 if(tryToArrangeOppositeFace(conn,lgth,6,connHexag0,connHexag1,8,tmp2))
354 std::copy(connHexag0,connHexag0+6,retConn);
355 std::copy(tmp2,tmp2+6,retConn+6);
357 return INTERP_KERNEL::NORM_HEXGP12;
364 std::copy(conn,conn+lgth,retConn);
365 return INTERP_KERNEL::NORM_POLYHED;
369 * Cell with 'conn' connectivity has been detected as a good candidate. Full check of this. If yes NORM_PENTA6 is returned.
370 * If fails a POLYHED is returned.
372 INTERP_KERNEL::NormalizedCellType CellSimplify::tryToUnPolyPenta6(const int *conn, int nbOfFaces, int lgth, int *retConn, int& retLgth)
374 std::size_t nbOfTriFace=std::count(conn+lgth,conn+lgth+nbOfFaces,(int)INTERP_KERNEL::NORM_TRI3);
375 std::size_t nbOfQuadFace=std::count(conn+lgth,conn+lgth+nbOfFaces,(int)INTERP_KERNEL::NORM_QUAD4);
376 if(nbOfTriFace==2 && nbOfQuadFace==3)
378 std::size_t tri3_0=std::distance(conn+lgth,std::find(conn+lgth,conn+lgth+nbOfFaces,(int)INTERP_KERNEL::NORM_TRI3));
379 std::size_t tri3_1=std::distance(conn+lgth,std::find(conn+lgth+tri3_0+1,conn+lgth+nbOfFaces,(int)INTERP_KERNEL::NORM_TRI3));
380 const int *tri_0=0,*tri_1=0;
382 for(std::size_t i=0;i<5;i++)
388 w=std::find(w,conn+lgth,-1);
391 std::vector<int> tmp;
392 std::set<int> conn1(tri_0,tri_0+3);
393 std::set<int> conn2(tri_1,tri_1+3);
394 std::set_intersection(conn1.begin(),conn1.end(),conn2.begin(),conn2.end(),std::back_insert_iterator< std::vector<int> >(tmp));
398 if(tryToArrangeOppositeFace(conn,lgth,3,tri_0,tri_1,5,tmp2))
400 std::copy(tri_0,tri_0+3,retConn);
401 std::copy(tmp2,tmp2+3,retConn+3);
403 return INTERP_KERNEL::NORM_PENTA6;
408 std::copy(conn,conn+lgth,retConn);
409 return INTERP_KERNEL::NORM_POLYHED;
413 * Cell with 'conn' connectivity has been detected as a good candidate. Full check of this. If yes NORM_PYRA5 is returned.
414 * If fails a POLYHED is returned.
416 INTERP_KERNEL::NormalizedCellType CellSimplify::tryToUnPolyPyra5(const int *conn, int nbOfFaces, int lgth, int *retConn, int& retLgth)
418 std::size_t nbOfTriFace=std::count(conn+lgth,conn+lgth+nbOfFaces,(int)INTERP_KERNEL::NORM_TRI3);
419 std::size_t nbOfQuadFace=std::count(conn+lgth,conn+lgth+nbOfFaces,(int)INTERP_KERNEL::NORM_QUAD4);
420 if(nbOfTriFace==4 && nbOfQuadFace==1)
422 std::size_t quad4_pos=std::distance(conn+lgth,std::find(conn+lgth,conn+lgth+nbOfFaces,(int)INTERP_KERNEL::NORM_QUAD4));
425 for(std::size_t i=0;i<5 && quad4==0;i++)
429 w=std::find(w,conn+lgth,-1);
432 std::set<int> quad4S(quad4,quad4+4);
436 for(std::size_t i=0;i<5 && ok;i++)
440 std::vector<int> tmp;
441 std::set<int> conn2(w,w+3);
442 std::set_intersection(conn2.begin(),conn2.end(),quad4S.begin(),quad4S.end(),std::back_insert_iterator< std::vector<int> >(tmp));
445 std::set_difference(conn2.begin(),conn2.end(),quad4S.begin(),quad4S.end(),std::back_insert_iterator< std::vector<int> >(tmp));
446 ok=ok && tmp.size()==1;
455 w=std::find(w,conn+lgth,-1);
460 std::copy(quad4,quad4+4,retConn);
463 return INTERP_KERNEL::NORM_PYRA5;
467 std::copy(conn,conn+lgth,retConn);
468 return INTERP_KERNEL::NORM_POLYHED;
472 * Cell with 'conn' connectivity has been detected as a good candidate. Full check of this. If yes NORM_TETRA4 is returned.
473 * If fails a POLYHED is returned.
475 INTERP_KERNEL::NormalizedCellType CellSimplify::tryToUnPolyTetra4(const int *conn, int nbOfFaces, int lgth, int *retConn, int& retLgth)
477 if(std::find_if(conn+lgth,conn+lgth+nbOfFaces,std::bind2nd(std::not_equal_to<int>(),(int)INTERP_KERNEL::NORM_TRI3))==conn+lgth+nbOfFaces)
479 std::set<int> tribase(conn,conn+3);
482 for(int i=1;i<4 && ok;i++)
484 std::vector<int> tmp;
485 std::set<int> conn2(conn+i*4,conn+4*i+3);
486 std::set_intersection(conn2.begin(),conn2.end(),tribase.begin(),tribase.end(),std::back_insert_iterator< std::vector<int> >(tmp));
489 std::set_difference(conn2.begin(),conn2.end(),tribase.begin(),tribase.end(),std::back_insert_iterator< std::vector<int> >(tmp));
490 ok=ok && tmp.size()==1;
501 std::copy(conn,conn+3,retConn);
504 return INTERP_KERNEL::NORM_TETRA4;
508 std::copy(conn,conn+lgth,retConn);
509 return INTERP_KERNEL::NORM_POLYHED;