1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "MEDCouplingStructuredMesh.hxx"
22 #include "MEDCouplingFieldDouble.hxx"
23 #include "MEDCouplingMemArray.hxx"
24 #include "MEDCouplingUMesh.hxx"
28 using namespace ParaMEDMEM;
30 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh()
34 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh(const MEDCouplingStructuredMesh& other, bool deepCopy):MEDCouplingMesh(other)
38 MEDCouplingStructuredMesh::~MEDCouplingStructuredMesh()
42 std::size_t MEDCouplingStructuredMesh::getHeapMemorySize() const
44 return MEDCouplingMesh::getHeapMemorySize();
47 void MEDCouplingStructuredMesh::copyTinyStringsFrom(const MEDCouplingMesh *other) throw(INTERP_KERNEL::Exception)
49 MEDCouplingMesh::copyTinyStringsFrom(other);
52 bool MEDCouplingStructuredMesh::isEqualIfNotWhy(const MEDCouplingMesh *other, double prec, std::string& reason) const throw(INTERP_KERNEL::Exception)
54 return MEDCouplingMesh::isEqualIfNotWhy(other,prec,reason);
57 INTERP_KERNEL::NormalizedCellType MEDCouplingStructuredMesh::getTypeOfCell(int cellId) const
59 return GetGeoTypeGivenMeshDimension(getMeshDimension());
62 INTERP_KERNEL::NormalizedCellType MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(int meshDim) throw(INTERP_KERNEL::Exception)
67 return INTERP_KERNEL::NORM_HEXA8;
69 return INTERP_KERNEL::NORM_QUAD4;
71 return INTERP_KERNEL::NORM_SEG2;
73 throw INTERP_KERNEL::Exception("Unexpected dimension for MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension !");
77 std::set<INTERP_KERNEL::NormalizedCellType> MEDCouplingStructuredMesh::getAllGeoTypes() const
79 std::set<INTERP_KERNEL::NormalizedCellType> ret2;
80 ret2.insert(getTypeOfCell(0));
84 int MEDCouplingStructuredMesh::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
86 int ret=getNumberOfCells();
87 if(type==getTypeOfCell(0))
89 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
90 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCellsWithType : no specified type ! Type available is " << cm.getRepr() << " !";
91 throw INTERP_KERNEL::Exception(oss.str().c_str());
94 DataArrayInt *MEDCouplingStructuredMesh::giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
96 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
97 if(getTypeOfCell(0)==type)
99 ret->alloc(getNumberOfCells(),1);
107 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfNodesPerCell() const throw(INTERP_KERNEL::Exception)
109 int nbCells=getNumberOfCells();
110 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
111 ret->alloc(nbCells,1);
112 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
113 ret->fillWithValue((int)cm.getNumberOfNodes());
117 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfFacesPerCell() const throw(INTERP_KERNEL::Exception)
119 int nbCells=getNumberOfCells();
120 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
121 ret->alloc(nbCells,1);
122 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
123 ret->fillWithValue((int)cm.getNumberOfSons());
127 void MEDCouplingStructuredMesh::getNodeIdsOfCell(int cellId, std::vector<int>& conn) const
129 int meshDim=getMeshDimension();
130 int tmpCell[3],tmpNode[3];
131 getSplitCellValues(tmpCell);
132 getSplitNodeValues(tmpNode);
134 GetPosFromId(cellId,meshDim,tmpCell,tmp2);
138 conn.push_back(tmp2[0]); conn.push_back(tmp2[0]+1);
141 conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1);
142 conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]+1); conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]);
145 conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+tmp2[2]*tmpNode[2]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1+tmp2[2]*tmpNode[2]);
146 conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1+tmp2[2]*tmpNode[2]); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+tmp2[2]*tmpNode[2]);
147 conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+(tmp2[2]+1)*tmpNode[2]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1+(tmp2[2]+1)*tmpNode[2]);
148 conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1+(tmp2[2]+1)*tmpNode[2]); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+(tmp2[2]+1)*tmpNode[2]);
151 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getNodeIdsOfCell : big problem spacedim must be in 1,2 or 3 !");
156 * See MEDCouplingUMesh::getDistributionOfTypes for more information
158 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const throw(INTERP_KERNEL::Exception)
160 //only one type of cell
161 std::vector<int> ret(3);
162 ret[0]=getTypeOfCell(0);
163 ret[1]=getNumberOfCells();
164 ret[2]=-1; //ret[3*k+2]==-1 because it has no sense here
169 * This method tries to minimize at most the number of deep copy.
170 * So if \a idsPerType is not empty it can be returned directly (without copy, but with ref count incremented) in return.
172 * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
174 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
176 int nbOfCells=getNumberOfCells();
178 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : invalid input code should be exactly of size 3 !");
179 if(code[0]!=(int)getTypeOfCell(0))
181 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : Mismatch of geometric type ! Asking for " << code[0] << " whereas the geometric type is \a this is " << getTypeOfCell(0) << " !";
182 throw INTERP_KERNEL::Exception(oss.str().c_str());
186 if(code[1]==nbOfCells)
190 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : mismatch between the number of cells in this (" << nbOfCells << ") and the number of non profile (" << code[1] << ") !";
191 throw INTERP_KERNEL::Exception(oss.str().c_str());
195 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : single geo type mesh ! 0 or -1 is expected at pos #2 of input code !");
196 if(idsPerType.size()!=1)
197 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input code points to DataArrayInt #0 whereas the size of idsPerType is not equal to 1 !");
198 const DataArrayInt *pfl=idsPerType[0];
200 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : the input code points to a NULL DataArrayInt at rank 0 !");
201 if(pfl->getNumberOfComponents()!=1)
202 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input profile should have exactly one component !");
203 pfl->checkAllIdsInRange(0,nbOfCells);
205 return const_cast<DataArrayInt *>(pfl);
209 * This method is the opposite of MEDCouplingUMesh::checkTypeConsistencyAndContig method. Given a list of cells in \a profile it returns a list of sub-profiles sorted by geo type.
210 * The result is put in the array \a idsPerType. In the returned parameter \a code, foreach i \a code[3*i+2] refers (if different from -1) to a location into the \a idsPerType.
211 * This method has 1 input \a profile and 3 outputs \a code \a idsInPflPerType and \a idsPerType.
213 * \param [out] code is a vector of size 3*n where n is the number of different geometric type in \a this \b reduced to the profile \a profile. \a code has exactly the same semantic than in MEDCouplingUMesh::checkTypeConsistencyAndContig method.
214 * \param [out] idsInPflPerType is a vector of size of different geometric type in the subpart defined by \a profile of \a this ( equal to \a code.size()/3). For each i,
215 * \a idsInPflPerType[i] stores the tuple ids in \a profile that correspond to the geometric type code[3*i+0]
216 * \param [out] idsPerType is a vector of size of different sub profiles needed to be defined to represent the profile \a profile for a given geometric type.
217 * This vector can be empty in case of all geometric type cells are fully covered in ascending in the given input \a profile.
219 * \warning for performance reasons no deep copy will be performed, if \a profile can been used as this in output parameters \a idsInPflPerType and \a idsPerType.
221 * \throw if \a profile has not exactly one component. It throws too, if \a profile contains some values not in [0,getNumberOfCells()) or if \a this is not fully defined
224 * - Before \a this has 3 cells \a profile contains [0,1,2]
225 * - After \a code contains [NORM_...,nbCells,-1], \a idsInPflPerType [[0,1,2]] and \a idsPerType is empty <br>
228 * - Before \a this has 3 cells \a profile contains [1,2]
229 * - After \a code contains [NORM_...,nbCells,0], \a idsInPflPerType [[0,1]] and \a idsPerType is [[1,2]] <br>
232 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
235 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile is NULL !");
236 if(profile->getNumberOfComponents()!=1)
237 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile should have exactly one component !");
238 int nbTuples=profile->getNumberOfTuples();
239 int nbOfCells=getNumberOfCells();
240 code.resize(3); idsInPflPerType.resize(1);
241 code[0]=(int)getTypeOfCell(0); code[1]=nbOfCells;
242 idsInPflPerType.resize(1);
243 if(profile->isIdentity() && nbTuples==nbOfCells)
246 idsInPflPerType[0]=const_cast<DataArrayInt *>(profile); idsInPflPerType[0]->incrRef();
250 profile->checkAllIdsInRange(0,nbOfCells);
251 idsPerType.resize(1);
252 idsPerType[0]=const_cast<DataArrayInt *>(profile); idsPerType[0]->incrRef();
253 idsInPflPerType[0]=DataArrayInt::Range(0,nbTuples,1);
257 * Creates a new unstructured mesh (MEDCouplingUMesh) from \a this structured one.
258 * \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
259 * delete this array using decrRef() as it is no more needed.
260 * \throw If \a this->getMeshDimension() is not among [1,2,3].
262 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const throw(INTERP_KERNEL::Exception)
264 int meshDim=getMeshDimension();
265 MEDCouplingUMesh *ret=MEDCouplingUMesh::New(getName(),meshDim);
266 DataArrayDouble *coords=getCoordinatesAndOwner();
267 ret->setCoords(coords);
272 fill1DUnstructuredMesh(ret);
275 fill2DUnstructuredMesh(ret);
278 fill3DUnstructuredMesh(ret);
281 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::buildUnstructured : big problem spacedim must be in 1,2 or 3 !");
287 * Creates a new MEDCouplingUMesh containing a part of cells of \a this mesh.
288 * The cells to include to the
289 * result mesh are specified by an array of cell ids.
290 * \param [in] start - an array of cell ids to include to the result mesh.
291 * \param [in] end - specifies the end of the array \a start, so that
292 * the last value of \a start is \a end[ -1 ].
293 * \return MEDCouplingMesh * - a new instance of MEDCouplingUMesh. The caller is to
294 * delete this mesh using decrRef() as it is no more needed.
296 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
298 MEDCouplingUMesh *um=buildUnstructured();
299 MEDCouplingMesh *ret=um->buildPart(start,end);
304 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
306 MEDCouplingUMesh *um=buildUnstructured();
307 MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
312 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy) throw(INTERP_KERNEL::Exception)
314 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
318 * Returns a new MEDCouplingFieldDouble holding normal vectors to cells of \a this
319 * 2D mesh. The computed vectors have 3 components and are normalized.
320 * \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on
321 * cells and one time. The caller is to delete this field using decrRef() as
322 * it is no more needed.
323 * \throw If \a this->getMeshDimension() != 2.
325 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
327 if(getMeshDimension()!=2)
328 throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
329 MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
330 DataArrayDouble *array=DataArrayDouble::New();
331 int nbOfCells=getNumberOfCells();
332 array->alloc(nbOfCells,3);
333 double *vals=array->getPointer();
334 for(int i=0;i<nbOfCells;i++)
335 { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
336 ret->setArray(array);
342 void MEDCouplingStructuredMesh::fill1DUnstructuredMesh(MEDCouplingUMesh *m) const
345 getNodeGridStructure(&nbOfCells);
347 DataArrayInt *connI=DataArrayInt::New();
348 connI->alloc(nbOfCells+1,1);
349 int *ci=connI->getPointer();
350 DataArrayInt *conn=DataArrayInt::New();
351 conn->alloc(3*nbOfCells,1);
353 int *cp=conn->getPointer();
354 for(int i=0;i<nbOfCells;i++)
356 cp[3*i]=(int)INTERP_KERNEL::NORM_SEG2;
361 m->setConnectivity(conn,connI,true);
366 void MEDCouplingStructuredMesh::fill2DUnstructuredMesh(MEDCouplingUMesh *m) const
369 getNodeGridStructure(ns);
372 DataArrayInt *connI=DataArrayInt::New();
373 connI->alloc(n1*n2+1,1);
374 int *ci=connI->getPointer();
375 DataArrayInt *conn=DataArrayInt::New();
376 conn->alloc(5*n1*n2,1);
378 int *cp=conn->getPointer();
380 for(int j=0;j<n2;j++)
381 for(int i=0;i<n1;i++,pos++)
383 cp[5*pos]=(int)INTERP_KERNEL::NORM_QUAD4;
384 cp[5*pos+1]=i+1+j*(n1+1);
385 cp[5*pos+2]=i+j*(n1+1);
386 cp[5*pos+3]=i+(j+1)*(n1+1);
387 cp[5*pos+4]=i+1+(j+1)*(n1+1);
390 m->setConnectivity(conn,connI,true);
395 void MEDCouplingStructuredMesh::fill3DUnstructuredMesh(MEDCouplingUMesh *m) const
398 getNodeGridStructure(ns);
402 DataArrayInt *connI=DataArrayInt::New();
403 connI->alloc(n1*n2*n3+1,1);
404 int *ci=connI->getPointer();
405 DataArrayInt *conn=DataArrayInt::New();
406 conn->alloc(9*n1*n2*n3,1);
408 int *cp=conn->getPointer();
410 for(int k=0;k<n3;k++)
411 for(int j=0;j<n2;j++)
412 for(int i=0;i<n1;i++,pos++)
414 cp[9*pos]=(int)INTERP_KERNEL::NORM_HEXA8;
415 int tmp=(n1+1)*(n2+1);
416 cp[9*pos+1]=i+1+j*(n1+1)+k*tmp;
417 cp[9*pos+2]=i+j*(n1+1)+k*tmp;
418 cp[9*pos+3]=i+(j+1)*(n1+1)+k*tmp;
419 cp[9*pos+4]=i+1+(j+1)*(n1+1)+k*tmp;
420 cp[9*pos+5]=i+1+j*(n1+1)+(k+1)*tmp;
421 cp[9*pos+6]=i+j*(n1+1)+(k+1)*tmp;
422 cp[9*pos+7]=i+(j+1)*(n1+1)+(k+1)*tmp;
423 cp[9*pos+8]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
426 m->setConnectivity(conn,connI,true);
432 * Returns a cell id by its (i,j,k) index. The cell is located between the i-th and
433 * ( i + 1 )-th nodes along X axis etc.
434 * \param [in] i - a index of node coordinates array along X axis.
435 * \param [in] j - a index of node coordinates array along Y axis.
436 * \param [in] k - a index of node coordinates array along Z axis.
437 * \return int - a cell id in \a this mesh.
439 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
443 int meshDim=getMeshDimension();
444 getSplitCellValues(tmp2);
445 std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
446 return std::accumulate(tmp,tmp+meshDim,0);
450 * Returns a node id by its (i,j,k) index.
451 * \param [in] i - a index of node coordinates array along X axis.
452 * \param [in] j - a index of node coordinates array along Y axis.
453 * \param [in] k - a index of node coordinates array along Z axis.
454 * \return int - a node id in \a this mesh.
456 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
460 int meshDim=getMeshDimension();
461 getSplitNodeValues(tmp2);
462 std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
463 return std::accumulate(tmp,tmp+meshDim,0);
466 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
469 for(int i=meshDim-1;i>=0;i--)
471 int pos=work/split[i];