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