1 // Project MULTIPR, IOLS WP1.2.1 - EDF/CS
2 // Partitioning/decimation module for the SALOME v3.2 platform
5 * \file MULTIPR_Family.cxx
7 * \brief see MULTIPR_Family.hxx
9 * \author Olivier LE ROUX - CS, Virtual Reality Dpt
14 //*****************************************************************************
16 //*****************************************************************************
18 #include "MULTIPR_Family.hxx"
19 #include "MULTIPR_Exceptions.hxx"
30 //*****************************************************************************
31 // Class Attributs implementation
32 //*****************************************************************************
34 Attributs::Attributs()
44 Attributs::~Attributs()
50 Attributs& Attributs::operator=(const Attributs& pAttr)
52 if (this != &pAttr) // check for self-assignement
58 if (mNum < 0) throw IllegalStateException("", __FILE__, __LINE__);
60 if (pAttr.mId != NULL)
62 mId = new med_int[mNum];
63 memcpy(mId, pAttr.mId, sizeof(med_int) * mNum);
66 if (pAttr.mVal != NULL)
68 mVal = new med_int[mNum];
69 memcpy(mVal, pAttr.mVal, sizeof(med_int) * mNum);
72 if (pAttr.mDesc != NULL)
74 mDesc = new char[MED_TAILLE_DESC * mNum + 1];
75 memcpy(mDesc, pAttr.mDesc, MED_TAILLE_DESC * mNum + 1);
83 void Attributs::reset()
87 if (mId != NULL) { delete[] mId; mId = NULL; }
88 if (mVal != NULL) { delete[] mVal; mVal = NULL; }
89 if (mDesc != NULL) { delete[] mDesc; mDesc = NULL; }
93 ostream& operator<<(ostream& pOs, Attributs& pA)
97 pOs << "NONE" << endl;
101 pOs << endl << " #attr=" << pA.mNum << endl;
106 for (int i = 0 ; i < pA.mNum ; i++)
108 pOs << pA.mId[i] << " ";
114 pOs << " Id =NULL" << endl;
120 for (int i = 0 ; i < pA.mNum ; i++)
122 pOs << pA.mVal[i] << " ";
131 if (pA.mDesc != NULL)
133 pOs << " Desc=|" << pA.mDesc << "|" << endl;
137 pOs << " Desc=NULL" << endl;
145 //*****************************************************************************
146 // Class Family implementation
147 //*****************************************************************************
171 mIsFamilyOfNodes = true;
173 mFlagPrintAll = false;
177 void Family::insertElt(med_int pIndexElt)
179 if (pIndexElt < 1) throw new IllegalArgumentException("", __FILE__, __LINE__);
181 mElt.insert(pIndexElt);
185 void Family::buildGroups(vector<Group*>& pGroups, map<string, Group*>& pGroupNameToGroup) const
187 // pGroups / pGroupNameToGroup can be empty or not
189 // for each group in this family
190 for (unsigned itGroup = 0 ; itGroup < mNameGroups.size() ; itGroup++)
192 const string& keyName = mNameGroups[itGroup];
194 // check if the group already exist
195 map<string, Group*>::iterator it = pGroupNameToGroup.find(keyName);
198 if (it != pGroupNameToGroup.end())
200 // the group already exists
201 group = (*it).second;
205 // a new group have been identified: create a new entry
207 group->setName(keyName);
208 group->setIsGroupOfNodes(isFamilyOfNodes());
210 pGroups.push_back(group);
211 pGroupNameToGroup.insert(make_pair(keyName, group));
214 // add all elements of the this family to the group
215 for (set<med_int>::iterator itElt = mElt.begin() ; itElt != mElt.end() ; itElt++)
217 group->insertElt(*itElt);
224 Family* Family::extractGroup(const char* pGroupName)
226 Family* family = new Family();
228 strcpy(family->mName, mName);
230 family->mAttributs = mAttributs;
232 if (pGroupName == NULL)
234 family->mStrNameGroups = mStrNameGroups;
235 family->mNameGroups = mNameGroups;
239 if (strlen(pGroupName) > MED_TAILLE_LNOM) throw IllegalArgumentException("", __FILE__, __LINE__);
240 if (strlen(pGroupName) == 0) throw IllegalArgumentException("", __FILE__, __LINE__);
242 if (strstr(mStrNameGroups.c_str(), pGroupName) == 0)
244 // pGroupName found in the list of groups => just keep pGroupName
245 family->mStrNameGroups = mStrNameGroups;
246 family->mNameGroups = mNameGroups;
250 // pGroupName not found in the current list of groups
251 // probably not a group of the same nature => keep all the groups
252 family->mStrNameGroups = pGroupName;
253 family->mNameGroups.push_back(pGroupName);
261 void Family::readMED(med_idt pMEDfile, char* pMeshName, med_int pIndex)
263 if (pMEDfile == 0) throw IOException("", __FILE__, __LINE__);
264 if (pMeshName == NULL) throw NullArgumentException("", __FILE__, __LINE__);
265 if (strlen(pMeshName) > MED_TAILLE_NOM) throw IllegalArgumentException("", __FILE__, __LINE__);
266 if (pIndex < 1) throw IllegalArgumentException("", __FILE__, __LINE__);
270 med_int numGroups = MEDnGroupe(pMEDfile, pMeshName, pIndex);
272 if (numGroups < 0) throw IOException("i/o error while reading number of groups in MED file", __FILE__, __LINE__);
274 med_int numAttr = MEDnAttribut(pMEDfile, pMeshName, pIndex);
276 med_int* attrId = new med_int[numAttr];
277 med_int* attrVal = new med_int[numAttr];
278 char* attrDesc = new char[MED_TAILLE_DESC * numAttr + 1];
281 char* nameGroups = new char[MED_TAILLE_LNOM * numGroups + 1];
282 nameGroups[0] = '\0';
284 med_err ret = MEDfamInfo(
297 if (ret != 0) throw IOException("i/o error while reading family information in MED file", __FILE__, __LINE__);
299 attrDesc[MED_TAILLE_DESC * numAttr] = '\0';
301 mAttributs.mNum = numAttr;
302 mAttributs.mId = attrId;
303 mAttributs.mVal = attrVal;
304 mAttributs.mDesc = attrDesc;
306 mStrNameGroups = nameGroups;
309 for (int itGroup = 0 ; itGroup < numGroups ; itGroup++)
311 char str[MED_TAILLE_LNOM + 1];
312 strncpy(str, nameGroups + itGroup * MED_TAILLE_LNOM, MED_TAILLE_LNOM);
313 str[MED_TAILLE_LNOM] = '\0';
314 mNameGroups.push_back(str);
319 // MEDfamLire is not necessary as we used MEDnoeudsLire/MEDelementsLire instead
323 void Family::writeMED(med_idt pMEDfile, char* pMeshName)
325 if (pMEDfile == 0) throw IOException("", __FILE__, __LINE__);
326 if (pMeshName == NULL) throw NullArgumentException("", __FILE__, __LINE__);
327 if (strlen(pMeshName) > MED_TAILLE_NOM) throw IllegalArgumentException("", __FILE__, __LINE__);
328 if (strlen(mName) > MED_TAILLE_NOM) throw IllegalArgumentException("", __FILE__, __LINE__);
329 if (mAttributs.mVal == NULL) throw IllegalStateException("", __FILE__, __LINE__);
330 if (mAttributs.mDesc == NULL) throw IllegalStateException("", __FILE__, __LINE__);
331 if (mAttributs.mId == NULL) throw IllegalStateException("", __FILE__, __LINE__);
333 med_err ret = MEDfamCr(
342 const_cast<char*>(mStrNameGroups.c_str()),
345 if (ret != 0) throw IOException("i/o error while creating family in MED file", __FILE__, __LINE__);
347 // MEDfamEcr is not necessary as we used MEDnoeudsEcr/MEDelementsEcr instead
352 ostream& operator<<(ostream& pOs, Family& pF)
354 pOs << "Family: " << endl;
355 pOs << " Name =|" << pF.mName << "| size=" << strlen(pF.mName) << endl;
356 pOs << " Id =" << pF.mId << endl;
357 pOs << " Family of =" << (pF.isFamilyOfNodes()?"NODES":"ELEMENTS") << endl;
359 pOs << " Groups: #groups=" << pF.mNameGroups.size() << endl;
360 for (unsigned itGroup = 0 ; itGroup < pF.mNameGroups.size() ; itGroup++)
362 pOs << " Group " << (itGroup + 1) << ": |" << pF.mNameGroups[itGroup] << "| size=" << pF.mNameGroups[itGroup].length() << endl;
364 pOs << " Attributs: " << pF.mAttributs;
366 if (pF.mElt.size() != 0)
368 set<med_int>::iterator itSet = pF.mElt.end();
370 pOs << " Elements: #elt=" << pF.mElt.size() << " min_id=" << (*(pF.mElt.begin())) << " max_id=" << (*itSet) << endl;
371 if (pF.mFlagPrintAll)
374 for (set<med_int>::iterator itSet = pF.mElt.begin() ; itSet != pF.mElt.end() ; itSet++)
376 pOs << (*itSet) << " ";
383 pOs << " Elements: #elt=0" << endl;
390 //*****************************************************************************
391 // Class Group implementation
392 //*****************************************************************************
410 mIsGroupOfNodes = true;
412 mFlagPrintAll = false;
416 void Group::setName(const string& pName)
418 if (pName.length() > MED_TAILLE_LNOM) throw IllegalArgumentException("", __FILE__, __LINE__);
424 void Group::insertElt(med_int pIndexElt)
426 if (pIndexElt < 1) throw IllegalArgumentException("", __FILE__, __LINE__);
428 mElt.insert(pIndexElt);
431 ostream& operator<<(ostream& pOs, Group& pG)
433 pOs << "Group: " << endl;
434 pOs << " Name =|" << pG.mName << "| size=" << pG.mName.length() << endl;
435 pOs << " Group of =" << (pG.isGroupOfNodes()?"NODES":"ELEMENTS") << endl;
437 if (pG.mElt.size() != 0)
439 set<med_int>::iterator itSet = pG.mElt.end();
441 pOs << " Elements: #elt=" << pG.mElt.size() << " min_id=" << (*(pG.mElt.begin())) << " max_id=" << (*itSet) << endl;
442 if (pG.mFlagPrintAll)
445 for (set<med_int>::iterator itSet = pG.mElt.begin() ; itSet != pG.mElt.end() ; itSet++)
447 pOs << (*itSet) << " ";
454 pOs << " Elements: #elt=0" << endl;
461 } // namespace multipr