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