Version: 8.3.0
SMESH_MesherHelper.hxx
Go to the documentation of this file.
1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 
23 // File: SMESH_MesherHelper.hxx
24 // Created: 15.02.06 14:48:09
25 // Author: Sergey KUUL
26 //
27 #ifndef SMESH_MesherHelper_HeaderFile
28 #define SMESH_MesherHelper_HeaderFile
29 
30 #include "SMESH_SMESH.hxx"
31 
32 #include "SMESH_MeshEditor.hxx" // needed for many meshers
33 
34 #include <Geom_Surface.hxx>
35 #include <ShapeAnalysis_Surface.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <gp_Pnt2d.hxx>
39 
40 #include <map>
41 #include <vector>
42 
43 class GeomAPI_ProjectPointOnCurve;
44 class GeomAPI_ProjectPointOnSurf;
45 class SMDS_MeshNode;
46 class SMESHDS_Hypothesis;
47 class SMESH_Gen;
49 
50 typedef std::map<SMESH_TLink, const SMDS_MeshNode*> TLinkNodeMap;
51 typedef std::map<SMESH_TLink, const SMDS_MeshNode*>::iterator ItTLinkNode;
52 
54 typedef boost::shared_ptr< PShapeIterator > PShapeIteratorPtr;
55 
56 typedef std::vector<const SMDS_MeshNode* > TNodeColumn;
57 typedef std::map< double, TNodeColumn > TParam2ColumnMap;
58 
59 typedef gp_XY (*xyFunPtr)(const gp_XY& uv1, const gp_XY& uv2);
60 
61 //=======================================================================
73 //=======================================================================
74 
76 {
77  public:
78  // ---------- PUBLIC UTILITIES ----------
79 
87  static bool IsSameElemGeometry(const SMESHDS_SubMesh* smDS,
89  const bool nullSubMeshRes = true);
90 
104  static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
105  const TopoDS_Face& theFace,
106  const std::list<TopoDS_Edge>& theBaseSide,
107  SMESHDS_Mesh* theMesh,
108  SMESH_ProxyMesh* theProxyMesh=0);
112  static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
113  const TopoDS_Face& theFace,
114  const TopoDS_Edge& theBaseEdge,
115  SMESHDS_Mesh* theMesh,
116  SMESH_ProxyMesh* theProxyMesh=0);
120  static bool IsStructured( SMESH_subMesh* faceSM );
121 
125  static bool IsDistorted2D( SMESH_subMesh* faceSM,
126  bool checkUV = false,
127  SMESH_MesherHelper* faceHelper = NULL);
128 
135  static bool IsMedium(const SMDS_MeshNode* node,
136  const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
144  static TopoDS_Shape GetSubShapeByNode(const SMDS_MeshNode* node,
145  const SMESHDS_Mesh* meshDS);
146 
153  static inline int WrapIndex(int ind, const int nbNodes) {
154  return (( ind %= nbNodes ) < 0 ) ? ind + nbNodes : ind;
155  }
156 
178  inline static gp_XY calcTFI(double x, double y,
179  const gp_XY& a0,const gp_XY& a1,const gp_XY& a2,const gp_XY& a3,
180  const gp_XY& p0,const gp_XY& p1,const gp_XY& p2,const gp_XY& p3);
181 
185  inline static gp_XYZ calcTFI(double x, double y,
186  const gp_XYZ& a0,const gp_XYZ& a1,const gp_XYZ& a2,const gp_XYZ& a3,
187  const gp_XYZ& p0,const gp_XYZ& p1,const gp_XYZ& p2,const gp_XYZ& p3);
195  static int Count(const TopoDS_Shape& shape,
196  const TopAbs_ShapeEnum type,
197  const bool ignoreSame);
198 
202  static int NbAncestors(const TopoDS_Shape& shape,
203  const SMESH_Mesh& mesh,
204  TopAbs_ShapeEnum ancestorType=TopAbs_SHAPE);
208  static PShapeIteratorPtr GetAncestors(const TopoDS_Shape& shape,
209  const SMESH_Mesh& mesh,
210  TopAbs_ShapeEnum ancestorType,
211  const TopoDS_Shape* container = 0);
215  static TopoDS_Shape GetCommonAncestor(const TopoDS_Shape& shape1,
216  const TopoDS_Shape& shape2,
217  const SMESH_Mesh& mesh,
218  TopAbs_ShapeEnum ancestorType);
222  static TopAbs_Orientation GetSubShapeOri(const TopoDS_Shape& shape,
223  const TopoDS_Shape& subShape);
224 
225  static bool IsSubShape( const TopoDS_Shape& shape, const TopoDS_Shape& mainShape );
226 
227  static bool IsSubShape( const TopoDS_Shape& shape, SMESH_Mesh* aMesh );
228 
229  static bool IsBlock( const TopoDS_Shape& shape );
230 
231  static double MaxTolerance( const TopoDS_Shape& shape );
232 
233  static double GetAngle( const TopoDS_Edge & E1, const TopoDS_Edge & E2,
234  const TopoDS_Face & F, const TopoDS_Vertex & V,
235  gp_Vec* faceNormal=0);
236 
237  static bool IsClosedEdge( const TopoDS_Edge& anEdge );
238 
239  static TopoDS_Vertex IthVertex( const bool is2nd, TopoDS_Edge anEdge, const bool CumOri=true );
240 
241  static TopAbs_ShapeEnum GetGroupType(const TopoDS_Shape& group,
242  const bool avoidCompound=false);
243 
244  static TopoDS_Shape GetShapeOfHypothesis( const SMESHDS_Hypothesis * hyp,
245  const TopoDS_Shape& shape,
246  SMESH_Mesh* mesh);
247 
248 
249 public:
250  // ---------- PUBLIC INSTANCE METHODS ----------
251 
252  // constructor
253  SMESH_MesherHelper(SMESH_Mesh& theMesh);
254 
255  SMESH_Gen* GetGen() const;
256 
257  SMESH_Mesh* GetMesh() const { return myMesh; }
258 
259  SMESHDS_Mesh* GetMeshDS() const;
260 
265  bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
266 
270  void SetIsQuadratic(const bool theBuildQuadratic)
271  { myCreateQuadratic = theBuildQuadratic; }
272 
276  void SetIsBiQuadratic(const bool theBuildBiQuadratic)
277  { myCreateBiQuadratic = theBuildBiQuadratic; }
278 
282  bool GetIsQuadratic() const { return myCreateQuadratic; }
283 
284  /*
285  * \brief Find out elements orientation on a geometrical face
286  */
287  bool IsReversedSubMesh (const TopoDS_Face& theFace);
288 
292  bool GetIsBiQuadratic() const { return myCreateBiQuadratic; }
293 
299  void FixQuadraticElements(SMESH_ComputeErrorPtr& error, bool volumeOnly=true);
300 
306  bool SetElementsOnShape(bool toSet)
307  { bool res = mySetElemOnShape; mySetElemOnShape = toSet; return res; }
308 
312  void SetSubShape(const int subShapeID);
313  void SetSubShape(const TopoDS_Shape& subShape);
318  int GetSubShapeID() const { return myShapeID; }
322  const TopoDS_Shape& GetSubShape() const { return myShape; }
327  void CopySubShapeInfo(const SMESH_MesherHelper& other);
328 
329 
333  int ShapeToIndex( const TopoDS_Shape& S ) const;
334 
338  SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0, double u=0., double v=0.);
342  SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
343  const SMDS_MeshNode* n2,
344  const int id = 0,
345  const bool force3d = true);
349  SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
350  const SMDS_MeshNode* n2,
351  const SMDS_MeshNode* n3,
352  const int id=0,
353  const bool force3d = false);
357  SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
358  const SMDS_MeshNode* n2,
359  const SMDS_MeshNode* n3,
360  const SMDS_MeshNode* n4,
361  const int id = 0,
362  const bool force3d = false);
366  SMDS_MeshFace* AddPolygonalFace (const std::vector<const SMDS_MeshNode*>& nodes,
367  const int id = 0,
368  const bool force3d = false);
372  SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
373  const SMDS_MeshNode* n2,
374  const SMDS_MeshNode* n3,
375  const SMDS_MeshNode* n4,
376  const int id = 0,
377  const bool force3d = true);
381  SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
382  const SMDS_MeshNode* n2,
383  const SMDS_MeshNode* n3,
384  const SMDS_MeshNode* n4,
385  const SMDS_MeshNode* n5,
386  const int id = 0,
387  const bool force3d = true);
391  SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
392  const SMDS_MeshNode* n2,
393  const SMDS_MeshNode* n3,
394  const SMDS_MeshNode* n4,
395  const SMDS_MeshNode* n5,
396  const SMDS_MeshNode* n6,
397  const int id = 0,
398  const bool force3d = true);
402  SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
403  const SMDS_MeshNode* n2,
404  const SMDS_MeshNode* n3,
405  const SMDS_MeshNode* n4,
406  const SMDS_MeshNode* n5,
407  const SMDS_MeshNode* n6,
408  const SMDS_MeshNode* n7,
409  const SMDS_MeshNode* n8,
410  const int id = 0,
411  bool force3d = true);
412 
416  SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
417  const SMDS_MeshNode* n2,
418  const SMDS_MeshNode* n3,
419  const SMDS_MeshNode* n4,
420  const SMDS_MeshNode* n5,
421  const SMDS_MeshNode* n6,
422  const SMDS_MeshNode* n7,
423  const SMDS_MeshNode* n8,
424  const SMDS_MeshNode* n9,
425  const SMDS_MeshNode* n10,
426  const SMDS_MeshNode* n11,
427  const SMDS_MeshNode* n12,
428  const int id = 0,
429  bool force3d = true);
430 
434  SMDS_MeshVolume* AddPolyhedralVolume (const std::vector<const SMDS_MeshNode*>& nodes,
435  const std::vector<int>& quantities,
436  const int ID=0,
437  const bool force3d = true);
444  void ToFixNodeParameters(bool toFix);
445 
449  double GetNodeU(const TopoDS_Edge& theEdge,
450  const SMDS_MeshNode* theNode,
451  const SMDS_MeshNode* inEdgeNode=0,
452  bool* check=0) const;
458  gp_XY GetNodeUV(const TopoDS_Face& F,
459  const SMDS_MeshNode* n,
460  const SMDS_MeshNode* inFaceNode=0,
461  bool* check=0) const;
468  bool CheckNodeUV(const TopoDS_Face& F,
469  const SMDS_MeshNode* n,
470  gp_XY& uv,
471  const double tol,
472  const bool force=false,
473  double distXYZ[4]=0) const;
480  bool CheckNodeU(const TopoDS_Edge& E,
481  const SMDS_MeshNode* n,
482  double& u,
483  const double tol,
484  const bool force=false,
485  double distXYZ[4]=0) const;
489  static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
490  const gp_XY& uv1,
491  const gp_XY& uv2);
495  static gp_XY GetCenterUV(const gp_XY& uv1,
496  const gp_XY& uv2,
497  const gp_XY& uv3,
498  const gp_XY& uv12,
499  const gp_XY& uv23,
500  const gp_XY& uv31,
501  bool * isBadTria=0);
509 #define gp_XY_FunPtr(meth) \
510  static gp_XY __gpXY_##meth (const gp_XY& uv1, const gp_XY& uv2) { return uv1.meth( uv2 ); } \
511  static xyFunPtr gp_XY_##meth = & __gpXY_##meth
512 
518  static gp_XY ApplyIn2D(Handle(Geom_Surface) surface,
519  const gp_XY& uv1,
520  const gp_XY& uv2,
521  xyFunPtr fun,
522  const bool resultInPeriod=true);
523 
530  void AdjustByPeriod( const TopoDS_Face& face, gp_XY uv[], const int nbUV );
531 
539  bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
540 
544  GeomAPI_ProjectPointOnSurf& GetProjector(const TopoDS_Face& F,
545  TopLoc_Location& loc,
546  double tol=0 ) const;
550  Handle(ShapeAnalysis_Surface) GetSurface(const TopoDS_Face& F ) const;
551 
559  bool IsDegenShape(const int subShape) const
560  { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
566  bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
567 
576  bool IsSeamShape(const int subShape) const
577  { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
586  bool IsSeamShape(const TopoDS_Shape& subShape) const
587  { return IsSeamShape( ShapeToIndex( subShape )); }
592  bool IsRealSeam(const int subShape) const
593  { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
598  bool IsRealSeam(const TopoDS_Shape& subShape) const
599  { return IsRealSeam( ShapeToIndex( subShape )); }
606  bool HasSeam() const { return !mySeamShapeIds.empty(); }
612  bool HasRealSeam() const { return HasSeam() && ( *mySeamShapeIds.begin() < 0 ); }
617  int GetPeriodicIndex() const { return myParIndex; }
621  double GetOtherParam(const double param) const;
625  int IsOnSeam(const gp_XY& uv) const;
626 
636  const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
637  const SMDS_MeshNode* n2,
638  const bool force3d,
639  TopAbs_ShapeEnum expectedSupport=TopAbs_SHAPE);
646  const SMDS_MeshNode* GetCentralNode(const SMDS_MeshNode* n1,
647  const SMDS_MeshNode* n2,
648  const SMDS_MeshNode* n3,
649  const SMDS_MeshNode* n4,
650  const SMDS_MeshNode* n12,
651  const SMDS_MeshNode* n23,
652  const SMDS_MeshNode* n34,
653  const SMDS_MeshNode* n41,
654  bool force3d);
661  const SMDS_MeshNode* GetCentralNode(const SMDS_MeshNode* n1,
662  const SMDS_MeshNode* n2,
663  const SMDS_MeshNode* n3,
664  const SMDS_MeshNode* n12,
665  const SMDS_MeshNode* n23,
666  const SMDS_MeshNode* n31,
667  bool force3d);
671  std::pair<int, TopAbs_ShapeEnum> GetMediumPos(const SMDS_MeshNode* n1,
672  const SMDS_MeshNode* n2,
673  const bool useCurSubShape=false,
674  TopAbs_ShapeEnum expectedSupport=TopAbs_SHAPE);
678  void AddTLinkNode(const SMDS_MeshNode* n1,
679  const SMDS_MeshNode* n2,
680  const SMDS_MeshNode* n12);
684  void AddTLinkNodeMap(const TLinkNodeMap& aMap)
685  { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
686 
687  bool AddTLinks(const SMDS_MeshEdge* edge);
688  bool AddTLinks(const SMDS_MeshFace* face);
689  bool AddTLinks(const SMDS_MeshVolume* vol);
690 
694  const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
695 
701  enum MType{ LINEAR, QUADRATIC, COMP };
702  MType IsQuadraticMesh();
703 
704  virtual ~SMESH_MesherHelper();
705 
706  static void WriteShape(const TopoDS_Shape& s);
707 
708 
709  protected:
710 
717  gp_Pnt2d getUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
718 
719  const SMDS_MeshNode* getMediumNodeOnComposedWire(const SMDS_MeshNode* n1,
720  const SMDS_MeshNode* n2,
721  bool force3d);
722 
723  double getFaceMaxTol( const TopoDS_Shape& face ) const;
724 
725 
726  private:
727 
728  // forbidden copy constructor
729  SMESH_MesherHelper (const SMESH_MesherHelper& theOther);
730 
731  // key of a map of bi-quadratic face to it's central node
732  struct TBiQuad: public std::pair<int, std::pair<int, int> >
733  {
735  const SMDS_MeshNode* n2,
736  const SMDS_MeshNode* n3,
737  const SMDS_MeshNode* n4=0)
738  {
740  s.insert(n1);
741  s.insert(n2);
742  s.insert(n3);
743  if ( n4 ) s.insert(n4);
744  TIDSortedNodeSet::iterator n = s.begin();
745  first = (*n++)->GetID();
746  second.first = (*n++)->GetID();
747  second.second = (*n++)->GetID();
748  }
749  };
750 
751  // maps used during creation of quadratic elements
752  TLinkNodeMap myTLinkNodeMap; // medium nodes on links
753  std::map< TBiQuad, const SMDS_MeshNode* > myMapWithCentralNode; // central nodes of faces
754 
755  std::set< int > myDegenShapeIds;
756  std::set< int > mySeamShapeIds;
757  double myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
758  int myParIndex; // bounds' index (1-U, 2-V, 3-both)
759 
760  std::map< int, double > myFaceMaxTol;
761 
762  typedef std::map< int, Handle(ShapeAnalysis_Surface)> TID2Surface;
763  typedef std::map< int, GeomAPI_ProjectPointOnSurf* > TID2ProjectorOnSurf;
764  typedef std::map< int, GeomAPI_ProjectPointOnCurve* > TID2ProjectorOnCurve;
768 
769  TopoDS_Shape myShape;
772 
777 
778  std::map< int,bool > myNodePosShapesValidity;
779  bool toCheckPosOnShape(int shapeID ) const;
780  void setPosOnShapeValidity(int shapeID, bool ok ) const;
781 };
782 
783 //=======================================================================
784 inline gp_XY
785 SMESH_MesherHelper::calcTFI(double x, double y,
786  const gp_XY& a0,const gp_XY& a1,const gp_XY& a2,const gp_XY& a3,
787  const gp_XY& p0,const gp_XY& p1,const gp_XY& p2,const gp_XY& p3)
788 {
789  return
790  ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
791  ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
792 }
793 //=======================================================================
794 inline gp_XYZ
795 SMESH_MesherHelper::calcTFI(double x, double y,
796  const gp_XYZ& a0,const gp_XYZ& a1,const gp_XYZ& a2,const gp_XYZ& a3,
797  const gp_XYZ& p0,const gp_XYZ& p1,const gp_XYZ& p2,const gp_XYZ& p3)
798 {
799  return
800  ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
801  ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
802 }
803 //=======================================================================
804 
805 #endif