Salome HOME
adding a new test for makeMesh method.
[modules/med.git] / src / MULTIPR / MULTIPR_Elements.hxx
1 //  Copyright (C) 2007-2008  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.
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 // Partitioning/decimation module for the SALOME v3.2 platform
20 //
21 /**
22  * \file    MULTIPR_Elements.hxx
23  *
24  * \brief   Class Elements = table of elements in a mesh.
25  *          All elements share the same type (e.g. MED_MAILLE) and the same geometric description (e.g. TETRA10).
26  *          Each element has a family and a table of connectivity.
27  *          Optional: each element has a name and an id.
28  *
29  * \author  Olivier LE ROUX - CS, Virtual Reality Dpt
30  * 
31  * \date    01/2007
32  */
33
34 #ifndef MULTIPR_ELEMENTS_HXX
35 #define MULTIPR_ELEMENTS_HXX
36
37 //*****************************************************************************
38 // Includes section
39 //*****************************************************************************
40
41 extern "C"
42 {
43     #include "med.h"
44 }
45
46 #include <iostream>
47 #include <set>
48 #include <vector>
49
50
51 namespace multipr
52 {
53
54 //*****************************************************************************
55 // Predeclaration
56 //*****************************************************************************
57
58 class Nodes;
59
60
61 //*****************************************************************************
62 // Class Elements
63 //*****************************************************************************
64
65 class Elements
66 {
67 public:
68
69     /** 
70      * Builds an empty set of elements (default constructor).
71      */
72     Elements();
73
74     /** 
75      * Builds an empty set of elements with of the defined geometry type.
76      * \param pGeomType The geometry type.
77      */
78     Elements(med_geometrie_element pGeomType);
79
80         
81     /**
82      * Destructor. Removes everything.
83      */
84     ~Elements();
85     
86     /**
87      * Resets this object in its state by default (empty). Cleans memory.
88      */
89     void reset();
90     
91     //---------------------------------------------------------------------
92     // Basic accessors/mutators
93     //---------------------------------------------------------------------
94     
95     /**
96      * Returns true iff this object contains no elements (table of elements is empty).
97      * \return true iff this object contains no elements.
98      */
99     bool isEmpty() const { return (mNum == 0); }
100     
101     /**
102      * Returns true iff elements have a name.
103      * \return true iff elements have a name.
104      */
105     bool isNames() const { return (mNames != NULL); }
106     
107     /**
108      * Returns true iff elements have an identifier (= a number).
109      * \return true iff elements have an identifier (= a number).
110      */
111     bool isIdentifiers() const { return (mId != NULL); }    
112     
113     /**
114      * Returns the number of elements.
115      * \return the number of elements.
116      */
117     int getNumberOfElements() const { return mNum; }
118     
119     /**
120      * Returns the type of primitives (e.g. MED_TETRA10).
121      * \return the type of primitives
122      */
123     med_geometrie_element getTypeOfPrimitives() const { return mGeom; }
124     
125     /**
126      * Returns the number of nodes that composed an element.
127      * \return the number of nodes that composed an element.
128      */
129      int getNumberOfNodesByElement() const { return mNumNodesByElt; }
130      
131     /**
132      * Returns the family associated with an element.
133      * \param  pIndex index of any element in [0..NUMBER_OF_ELEMENTS-1].
134      * \return the family associated with an element.
135      * \throw  IndexOutOfBoundsException if pIndex is invalid.
136      */
137     med_int getFamilyIdentifier(med_int pIndex) const;
138     
139     /**
140      * Returns the connectivity of one element.
141      * \param  pIndex must be in [0..NUMBER_OF_ELEMENTS-1].
142      * \return a pointer towards the table of connectivity of the element.
143      * \throw  IndexOutOfBoundsException if pIndex is invalid.
144      */
145     const med_int* getConnectivity(int pIndex) const;
146     
147     /**
148      * Returns the coordinates of all the nodes of one element.
149      * \param  pIndexElt must be in [0..NUMBER_OF_ELEMENTS-1].
150      * \param  pNodes    table of nodes, used to retrieve coordinates.
151      * \param  pCoo      (out) table of coordinates of nodes (e.g. 30 = 3 * 10 med_float for a TETRA10).
152      * \param  pFirst    only get the pFirst coordinates.
153      * \throw  IndexOutOfBoundsException if pIndex is invalid.
154      */
155     void getCoordinates(med_int pIndexElt, const Nodes* pNodes, med_float* pCoo, int pFirst = 100) const;
156     
157     //---------------------------------------------------------------------
158     // Algorithms
159     //---------------------------------------------------------------------
160     
161     /**
162      * Constructor. Creates a subset of this set of elements from a set of indices.
163      * \param  pSetIndices set of indices (start at 1).
164      * \return a restriction of this set of elements.
165      */
166     Elements*   extractSubSet(const std::set<med_int>& pSetIndices) const;
167     
168     /**
169      * Create a subset of elements containing the given nodes. 
170      * \param pSetOfNodes The set of nodes.
171      * \param pSubSetOfElements The subset of elements to fill.
172      * \throw IllegalStateException if pSetOfNodes and pSubSetOfElements are not different.
173      */
174     void        extractSubSetFromNodes(const std::set<med_int>& pSetOfNodes, std::set<med_int>& pSubSetOfElements) const;
175     
176     /**
177      * Returns the set of indices (starting at 1) of all nodes used by this set of elements.
178      * \return the set of indices of all nodes used by this set of elements.
179      */
180     const std::set<med_int>& getSetOfNodes();
181     
182     /**
183      * Returns the set of families referenced by this set of elements.
184      * Each family is described by its identifier.
185      * \return Returns the set of families referenced by this set of elements.
186      */
187     std::set<med_int> getSetOfFamilies() const;
188     
189     /**
190      * Remaps this set of elements such that indices of nodes are continous 1 2 3 4 5 ... *
191      * This method is intended to be used after extractSubSet().
192      */
193     void remap(void);
194     
195         
196     /**
197      * Remaps this set of elements such that indices of nodes are continous 1 2 3 4 5 ... *
198      * This method is intended to be used after extractSubSet().
199          * This remap method allows to use the same set of nodes with several mesh type.
200          * \param pSetOfNodes The set of nodes index extracted from the original med file.
201      */
202     void remap(std::set<med_int>& pSetOfNodes);
203         
204         
205     /**
206      * Builds a new set of elements by merging two set of elements (this and pElements) if possible.
207      * Merge is partial because Id and Names are not take into account.
208      * \param  pElements any set of nodes to be merged with this.
209      * \param  pOffset
210      * \return a new set of nodes.
211      * \throw  IllegalStateException if the two sets of elements are incompatible.
212      */
213     Elements* mergePartial(Elements* pElements, int pOffset);
214     Elements* mergePartial(std::vector<Elements*> pElements, std::vector<int>& pOffsets);
215     
216     //---------------------------------------------------------------------
217     // I/O
218     //---------------------------------------------------------------------
219     
220     /**
221      * Reads a set of elements from a MED file.
222      * You must give: name of mesh, dimension of the mesh, type of entity and type of elements.
223      * \param  pMEDfile   any valid MED file opened for reading.
224      * \param  pMeshName  name of the mesh.
225      * \param  pMeshDim   dimension of the mesh.
226      * \param  pEntity    entity to be read (e.g. MED_MAILLE).
227      * \param  pGeom      type of primitves to be read (e.g. MED_TETRA10).
228      * \throw  IOException if any i/o error occurs.
229      */
230     void readMED(
231         med_idt               pMEDfile, 
232         char*                 pMeshName, 
233         med_int               pMeshDim,
234         med_entite_maillage   pEntity, 
235         med_geometrie_element pGeom);
236     
237     /**
238      * Writes this set of elements to a MED file.
239      * WARNING: mesh must have been created and added to the MED file before.
240      * \param  pMEDfile  any valid MED file opened for writing.
241      * \param  pMeshName name of the mesh.
242      * \param  pMeshDim  dimension of the mesh.
243      * \throw  IOException if any i/o error occurs.
244      */
245     void writeMED(med_idt pMEDfile, char* pMeshName, med_int pMeshDim);
246     
247     /**
248      * Sets the flag which control the stream operator <<.
249      * \param  pFlag new flag value.
250      */
251     void setPrintAll(bool pFlag) { mFlagPrintAll = pFlag; } 
252     
253     /**
254      * Dumps any Elements to the given output stream.
255      * \param  pOs any output stream.
256      * \param  pE  any Elements.
257      * \return the output stream pOs.
258      */
259     friend std::ostream& operator<<(std::ostream& pOs, Elements& pE);
260
261 private:
262     
263     /**
264      * Builds the set of nodes used by this set of elements.
265      * If the set already exist, then it is cleared and a new set is computed.
266      */
267     void buildSetOfNodes();
268     
269 private:
270     
271     med_int               mNum;           /**< Number of elements. */
272     med_entite_maillage   mEntity;        /**< Type of entity, e.g. MED_MAILLE. */
273     med_geometrie_element mGeom;          /**< Type of primitive, e.g. MED_TETRA10. */
274     int                   mDim;           /**< Dimension of elements = mGeom / 100. */
275     int                   mNumNodesByElt; /**< Number of nodes by element = mGeom % 100. */
276     med_int*              mId;            /**< Optional; for each element, its identifier; NULL if undefined. */
277     med_int*              mFamIdent;      /**< For each element, its family (identifier). */
278     char*                 mNames;         /**< Optional; for each element, its name; NULL if undefined. */
279     med_int*              mCon;           /**< For each element, list of nodes (index in 1..*) = table of connectivity. */
280     std::set<med_int>     mSetOfNodes;    /**< Set of all nodes used by this set of elements. */
281     
282     bool                  mFlagPrintAll;  /** Flag to control the behaviour of the stream operator <<. */
283     
284 private:
285
286     // do not allow copy constructor
287     Elements(const Elements&);
288     
289     // do not allow copy
290     Elements& operator=(const Elements&);
291     
292     // do not allow operator ==
293     bool operator==(const Elements&); 
294     
295 }; // class Elements
296
297
298 } // namespace MULTIPR
299
300
301 #endif // MULTIPR_NODES_HXX
302
303 // EOF