Salome HOME
Copyright update 2021
[modules/smesh.git] / src / SMESHUtils / SMESH_TypeDefs.hxx
1 // Copyright (C) 2007-2021  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 // File      : SMESH_TypeDefs.hxx
23 // Created   : Thu Jan 27 18:38:33 2011
24 // Author    : Edward AGAPOV (eap)
25
26
27 #ifndef __SMESH_TypeDefs_HXX__
28 #define __SMESH_TypeDefs_HXX__
29
30 #include "SMESH_Utils.hxx"
31
32 #include "SMDS_SetIterator.hxx"
33 #include "SMDS_MeshNode.hxx"
34
35 #include <gp_XYZ.hxx>
36 #include <gp_XY.hxx>
37
38 #include <map>
39 #include <list>
40 #include <set>
41 #include <cassert>
42
43 #include <boost/make_shared.hpp>
44
45 typedef std::map<const SMDS_MeshElement*,
46                  std::list<const SMDS_MeshElement*>, TIDCompare > TElemOfElemListMap;
47 typedef std::map<const SMDS_MeshElement*,
48                  std::list<const SMDS_MeshNode*>,    TIDCompare > TElemOfNodeListMap;
49 typedef std::map<const SMDS_MeshNode*,
50                  const SMDS_MeshNode*,               TIDCompare>  TNodeNodeMap;
51
52 //!< Set of elements sorted by ID, to be used to assure predictability of edition
53 typedef std::set< const SMDS_MeshElement*, TIDCompare >      TIDSortedElemSet;
54 typedef std::set< const SMDS_MeshNode*,    TIDCompare >      TIDSortedNodeSet;
55
56 typedef std::pair< const SMDS_MeshNode*, const SMDS_MeshNode* >   NLink;
57
58 struct FaceQuadStruct; // defined in StdMeshers_Quadrangle_2D.hxx
59 typedef boost::shared_ptr<FaceQuadStruct> TFaceQuadStructPtr;
60
61
62 namespace SMESHUtils
63 {
64   /*!
65    * \brief Enforce freeing memory allocated by std::vector
66    */
67   template <class TVECTOR>
68   void FreeVector(TVECTOR& vec)
69   {
70     TVECTOR v2;
71     vec.swap( v2 );
72   }
73   template <class TVECTOR>
74   void CompactVector(TVECTOR& vec)
75   {
76     TVECTOR v2( vec );
77     vec.swap( v2 );
78   }
79
80   /*!
81    * \brief Auto pointer
82    */
83   template <typename TOBJ>
84   struct Deleter
85   {
86     TOBJ* _obj;
87     explicit Deleter( TOBJ* obj = (TOBJ*)NULL ): _obj( obj ) {}
88     ~Deleter() { delete _obj; _obj = 0; }
89     TOBJ& operator*()  const { return *_obj; }
90     TOBJ* operator->() const { return _obj; }
91     operator bool()    const { return _obj; }
92   private:
93     Deleter( const Deleter& );
94   };
95
96   /*!
97    * \brief Auto pointer to array
98    */
99   template <typename TOBJ>
100   struct ArrayDeleter
101   {
102     TOBJ* _obj;
103     ArrayDeleter( TOBJ* obj ): _obj( obj ) {}
104     ~ArrayDeleter() { delete [] _obj; _obj = 0; }
105   private:
106     ArrayDeleter( const ArrayDeleter& );
107   };
108
109   /*!
110    * \return SMDS_ElemIteratorPtr on an std container of SMDS_MeshElement's
111    */
112   template < class ELEM_SET >
113   SMDS_ElemIteratorPtr elemSetIterator( const ELEM_SET& elements )
114   {
115     typedef SMDS_SetIterator
116       < SMDS_pElement, typename ELEM_SET::const_iterator> TSetIterator;
117     return boost::make_shared< TSetIterator >( elements.begin(), elements.end() );
118   }
119
120   /*!
121    * \brief Increment enum value
122    */
123   template < typename ENUM >
124   void Increment( ENUM& v, int delta=1 )
125   {
126     v = ENUM( int(v)+delta );
127   }
128
129   /*!
130    * \brief Return incremented enum value
131    */
132   template < typename ENUM >
133   ENUM Add( ENUM v, int delta )
134   {
135     return ENUM( int(v)+delta );
136   }
137 }
138
139 //=======================================================================
140 /*!
141  * \brief A sorted pair of nodes
142  */
143 //=======================================================================
144
145 struct SMESH_TLink: public NLink
146 {
147   SMESH_TLink(const SMDS_MeshNode* n1, const SMDS_MeshNode* n2 ):NLink( n1, n2 )
148   { if ( n1->GetID() < n2->GetID() ) std::swap( first, second ); }
149   SMESH_TLink(const NLink& link ):NLink( link )
150   { if ( first->GetID() < second->GetID() ) std::swap( first, second ); }
151   const SMDS_MeshNode* node1() const { return first; }
152   const SMDS_MeshNode* node2() const { return second; }
153
154   // methods for usage of SMESH_TLink as a hasher in NCollection maps
155   static int HashCode(const SMESH_TLink& link, int aLimit)
156   {
157     return ::HashCode( link.node1()->GetID() + link.node2()->GetID(), aLimit );
158   }
159   static Standard_Boolean IsEqual(const SMESH_TLink& l1, const SMESH_TLink& l2)
160   {
161     return ( l1.node1() == l2.node1() && l1.node2() == l2.node2() );
162   }
163 };
164 typedef SMESH_TLink SMESH_Link;
165
166 //=======================================================================
167 /*!
168  * \brief SMESH_TLink knowing its orientation
169  */
170 //=======================================================================
171
172 struct SMESH_OrientedLink: public SMESH_TLink
173 {
174   bool _reversed;
175   SMESH_OrientedLink(const SMDS_MeshNode* n1, const SMDS_MeshNode* n2 )
176     : SMESH_TLink( n1, n2 ), _reversed( n1 != node1() ) {}
177 };
178
179 //------------------------------------------
180 /*!
181  * \brief SMDS_MeshNode -> gp_XYZ converter
182  */
183 //------------------------------------------
184 struct SMESH_TNodeXYZ : public gp_XYZ
185 {
186   const SMDS_MeshNode* _node;
187   SMESH_TNodeXYZ( const SMDS_MeshElement* e=0):gp_XYZ(0,0,0),_node(0)
188   {
189     Set(e);
190   }
191   bool Set( const SMDS_MeshElement* e=0 )
192   {
193     if (e) {
194       assert( e->GetType() == SMDSAbs_Node );
195       _node = static_cast<const SMDS_MeshNode*>(e);
196       _node->GetXYZ( ChangeData() ); // - thread safe getting coords
197       return true;
198     }
199     return false;
200   }
201   const SMDS_MeshNode* Node() const { return _node; }
202   double Distance(const SMDS_MeshNode* n)       const { return (SMESH_TNodeXYZ( n )-*this).Modulus(); }
203   double SquareDistance(const SMDS_MeshNode* n) const { return (SMESH_TNodeXYZ( n )-*this).SquareModulus(); }
204   bool operator==(const SMESH_TNodeXYZ& other)  const { return _node == other._node; }
205   bool operator!=(const SMESH_TNodeXYZ& other)  const { return _node != other._node; }
206   bool operator!() const { return !_node; }
207   const SMDS_MeshNode* operator->() const { return _node; }
208 };
209 typedef SMESH_TNodeXYZ SMESH_NodeXYZ;
210
211 // --------------------------------------------------------------------------------
212 // SMESH_Hasher provide methods needed to put mesh data to NCollection maps
213
214 struct SMESH_Hasher
215 {
216   static Standard_Integer HashCode(const SMDS_MeshElement* e, const Standard_Integer upper)
217   {
218     return ::HashCode( e->GetID(), upper );
219   }
220   static Standard_Boolean IsEqual( const SMDS_MeshElement* e1, const SMDS_MeshElement* e2 )
221   {
222     return ( e1 == e2 );
223   }
224 };
225
226 //--------------------------------------------------
227 /*!
228  * \brief Data of a node generated on FACE boundary
229  */
230 //--------------------------------------------------
231 typedef struct uvPtStruct
232 {
233   double param;
234   double normParam;
235   double u, v; // original 2d parameter
236   double x, y; // 2d parameter, normalized [0,1]
237   const SMDS_MeshNode * node;
238
239   uvPtStruct(const SMDS_MeshNode* n = 0): node(n) {}
240
241   inline gp_XY UV() const { return gp_XY( u, v ); }
242   inline void  SetUV( const gp_XY& uv ) { u = uv.X(); v = uv.Y(); }
243
244   struct NodeAccessor // accessor to iterate on nodes in UVPtStructVec
245   {
246     static const SMDS_MeshNode* value(std::vector< uvPtStruct >::const_iterator it)
247     { return it->node; }
248   };
249 } UVPtStruct;
250
251 typedef std::vector< UVPtStruct > UVPtStructVec;
252
253 // --------------------------------------------------------------------------------
254 // class SMESH_SequenceOfElemPtr
255
256 typedef std::vector< const SMDS_MeshElement* > SMESH_SequenceOfElemPtr;
257
258 // --------------------------------------------------------------------------------
259 // class SMESH_SequenceOfNode
260 #include <NCollection_DefineSequence.hxx>
261 typedef const SMDS_MeshNode* SMDS_MeshNodePtr;
262
263 DEFINE_SEQUENCE(SMESH_SequenceOfNode,
264                 SMESH_BaseCollectionNodePtr, SMDS_MeshNodePtr)
265
266 #endif