1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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 "MEDCoupling1GTUMesh.hxx"
25 #include "MEDCouplingUMesh.hxx"
29 using namespace ParaMEDMEM;
31 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh()
35 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh(const MEDCouplingStructuredMesh& other, bool deepCopy):MEDCouplingMesh(other)
39 MEDCouplingStructuredMesh::~MEDCouplingStructuredMesh()
43 std::size_t MEDCouplingStructuredMesh::getHeapMemorySizeWithoutChildren() const
45 return MEDCouplingMesh::getHeapMemorySizeWithoutChildren();
48 void MEDCouplingStructuredMesh::copyTinyStringsFrom(const MEDCouplingMesh *other)
50 MEDCouplingMesh::copyTinyStringsFrom(other);
53 bool MEDCouplingStructuredMesh::isEqualIfNotWhy(const MEDCouplingMesh *other, double prec, std::string& reason) const
55 return MEDCouplingMesh::isEqualIfNotWhy(other,prec,reason);
58 INTERP_KERNEL::NormalizedCellType MEDCouplingStructuredMesh::getTypeOfCell(int cellId) const
60 return GetGeoTypeGivenMeshDimension(getMeshDimension());
63 INTERP_KERNEL::NormalizedCellType MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(int meshDim)
68 return INTERP_KERNEL::NORM_HEXA8;
70 return INTERP_KERNEL::NORM_QUAD4;
72 return INTERP_KERNEL::NORM_SEG2;
74 return INTERP_KERNEL::NORM_POINT1;
76 throw INTERP_KERNEL::Exception("Unexpected dimension for MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension !");
80 std::set<INTERP_KERNEL::NormalizedCellType> MEDCouplingStructuredMesh::getAllGeoTypes() const
82 std::set<INTERP_KERNEL::NormalizedCellType> ret2;
83 ret2.insert(getTypeOfCell(0));
87 int MEDCouplingStructuredMesh::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
89 int ret=getNumberOfCells();
90 if(type==getTypeOfCell(0))
92 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
93 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCellsWithType : no specified type ! Type available is " << cm.getRepr() << " !";
94 throw INTERP_KERNEL::Exception(oss.str().c_str());
97 DataArrayInt *MEDCouplingStructuredMesh::giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
99 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
100 if(getTypeOfCell(0)==type)
102 ret->alloc(getNumberOfCells(),1);
110 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfNodesPerCell() const
112 int nbCells=getNumberOfCells();
113 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
114 ret->alloc(nbCells,1);
115 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
116 ret->fillWithValue((int)cm.getNumberOfNodes());
120 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfFacesPerCell() const
122 int nbCells=getNumberOfCells();
123 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
124 ret->alloc(nbCells,1);
125 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
126 ret->fillWithValue((int)cm.getNumberOfSons());
131 * This method computes effective number of nodes per cell. That is to say nodes appearing several times in nodal connectivity of a cell,
132 * will be counted only once here whereas it will be counted several times in MEDCouplingMesh::computeNbOfNodesPerCell method.
133 * Here for structured mesh it returns exactly as MEDCouplingStructuredMesh::computeNbOfNodesPerCell does.
135 * \return DataArrayInt * - new object to be deallocated by the caller.
137 DataArrayInt *MEDCouplingStructuredMesh::computeEffectiveNbOfNodesPerCell() const
139 return computeNbOfNodesPerCell();
142 void MEDCouplingStructuredMesh::getNodeIdsOfCell(int cellId, std::vector<int>& conn) const
144 int meshDim=getMeshDimension();
145 int tmpCell[3],tmpNode[3];
146 getSplitCellValues(tmpCell);
147 getSplitNodeValues(tmpNode);
149 GetPosFromId(cellId,meshDim,tmpCell,tmp2);
153 conn.push_back(tmp2[0]); conn.push_back(tmp2[0]+1);
156 conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]); conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+1);
157 conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]);
160 conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+tmp2[2]*tmpNode[2]); conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+1+tmp2[2]*tmpNode[2]);
161 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]);
162 conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+(tmp2[2]+1)*tmpNode[2]); conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+1+(tmp2[2]+1)*tmpNode[2]);
163 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]);
166 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getNodeIdsOfCell : big problem spacedim must be in 1,2 or 3 !");
171 * This method returns the mesh dimension of \a this. It can be different from space dimension in case of a not null dimension contains only one node.
173 int MEDCouplingStructuredMesh::getMeshDimension() const
175 std::vector<int> ngs(getNodeGridStructure());
177 for(std::vector<int>::const_iterator it=ngs.begin();it!=ngs.end();it++,pos++)
181 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getMeshDimension : At pos #" << pos << " number of nodes is " << *it << " ! Must be > 0 !";
182 throw INTERP_KERNEL::Exception(oss.str().c_str());
191 * This method returns the space dimension by only considering the node grid structure.
192 * For cartesian mesh the returned value is equal to those returned by getSpaceDimension.
193 * But for curvelinear is could be different !
195 int MEDCouplingStructuredMesh::getSpaceDimensionOnNodeStruct() const
197 std::vector<int> nodeStr(getNodeGridStructure());
199 for(std::vector<int>::const_iterator it=nodeStr.begin();it!=nodeStr.end();it++,pos++)
204 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getSpaceDimensionOnNodeStruct : At pos #" << pos << " value of node grid structure is " << *it << " ! must be >=1 !";
205 throw INTERP_KERNEL::Exception(oss.str().c_str());
213 * This method returns the number of cells of unstructured sub level mesh, without building it.
215 int MEDCouplingStructuredMesh::getNumberOfCellsOfSubLevelMesh() const
217 std::vector<int> cgs(getCellGridStructure());
218 return GetNumberOfCellsOfSubLevelMesh(cgs,getMeshDimension());
222 * See MEDCouplingUMesh::getDistributionOfTypes for more information
224 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const
226 //only one type of cell
227 std::vector<int> ret(3);
228 ret[0]=getTypeOfCell(0);
229 ret[1]=getNumberOfCells();
230 ret[2]=-1; //ret[3*k+2]==-1 because it has no sense here
235 * This method tries to minimize at most the number of deep copy.
236 * So if \a idsPerType is not empty it can be returned directly (without copy, but with ref count incremented) in return.
238 * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
240 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const
242 int nbOfCells=getNumberOfCells();
244 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : invalid input code should be exactly of size 3 !");
245 if(code[0]!=(int)getTypeOfCell(0))
247 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : Mismatch of geometric type ! Asking for " << code[0] << " whereas the geometric type is \a this is " << getTypeOfCell(0) << " !";
248 throw INTERP_KERNEL::Exception(oss.str().c_str());
252 if(code[1]==nbOfCells)
256 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : mismatch between the number of cells in this (" << nbOfCells << ") and the number of non profile (" << code[1] << ") !";
257 throw INTERP_KERNEL::Exception(oss.str().c_str());
261 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : single geo type mesh ! 0 or -1 is expected at pos #2 of input code !");
262 if(idsPerType.size()!=1)
263 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input code points to DataArrayInt #0 whereas the size of idsPerType is not equal to 1 !");
264 const DataArrayInt *pfl=idsPerType[0];
266 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : the input code points to a NULL DataArrayInt at rank 0 !");
267 if(pfl->getNumberOfComponents()!=1)
268 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input profile should have exactly one component !");
269 pfl->checkAllIdsInRange(0,nbOfCells);
271 return const_cast<DataArrayInt *>(pfl);
275 * 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.
276 * 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.
277 * This method has 1 input \a profile and 3 outputs \a code \a idsInPflPerType and \a idsPerType.
279 * \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.
280 * \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,
281 * \a idsInPflPerType[i] stores the tuple ids in \a profile that correspond to the geometric type code[3*i+0]
282 * \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.
283 * This vector can be empty in case of all geometric type cells are fully covered in ascending in the given input \a profile.
285 * \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.
287 * \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
290 * - Before \a this has 3 cells \a profile contains [0,1,2]
291 * - After \a code contains [NORM_...,nbCells,-1], \a idsInPflPerType [[0,1,2]] and \a idsPerType is empty <br>
294 * - Before \a this has 3 cells \a profile contains [1,2]
295 * - After \a code contains [NORM_...,nbCells,0], \a idsInPflPerType [[0,1]] and \a idsPerType is [[1,2]] <br>
298 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const
300 if(!profile || !profile->isAllocated())
301 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile is NULL or not allocated !");
302 if(profile->getNumberOfComponents()!=1)
303 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile should have exactly one component !");
304 int nbTuples=profile->getNumberOfTuples();
305 int nbOfCells=getNumberOfCells();
306 code.resize(3); idsInPflPerType.resize(1);
307 code[0]=(int)getTypeOfCell(0); code[1]=nbOfCells;
308 idsInPflPerType.resize(1);
309 if(profile->isIdentity() && nbTuples==nbOfCells)
312 idsInPflPerType[0]=0;
316 code[1]=profile->getNumberOfTuples();
318 profile->checkAllIdsInRange(0,nbOfCells);
319 idsPerType.resize(1);
320 idsPerType[0]=profile->deepCpy();
321 idsInPflPerType[0]=DataArrayInt::Range(0,nbTuples,1);
325 * Creates a new unstructured mesh (MEDCoupling1SGTUMesh) from \a this structured one.
326 * \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
327 * delete this array using decrRef() as it is no more needed.
328 * \throw If \a this->getMeshDimension() is not among [1,2,3].
330 MEDCoupling1SGTUMesh *MEDCouplingStructuredMesh::build1SGTUnstructured() const
332 int meshDim(getMeshDimension()),spaceDim(getSpaceDimensionOnNodeStruct());
333 if((meshDim<0 || meshDim>3) || (spaceDim<0 || spaceDim>3))
334 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::build1SGTUnstructured : meshdim and spacedim must be in [1,2,3] !");
335 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(getCoordinatesAndOwner());
337 getNodeGridStructure(ns);
338 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(Build1GTNodalConnectivity(ns,ns+spaceDim));
339 MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret(MEDCoupling1SGTUMesh::New(getName(),GetGeoTypeGivenMeshDimension(meshDim)));
340 ret->setNodalConnectivity(conn); ret->setCoords(coords);
345 * This method returns the unstructured mesh (having single geometric type) of the sub level mesh of \a this.
346 * This method is equivalent to computing MEDCouplingUMesh::buildDescendingConnectivity on the unstructurized \a this mesh.
348 * The caller is to delete the returned mesh using decrRef() as it is no more needed.
350 MEDCoupling1SGTUMesh *MEDCouplingStructuredMesh::build1SGTSubLevelMesh() const
352 int meshDim(getMeshDimension());
353 if(meshDim<1 || meshDim>3)
354 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::build1SGTSubLevelMesh : meshdim must be in [2,3] !");
355 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(getCoordinatesAndOwner());
357 getNodeGridStructure(ns);
358 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(Build1GTNodalConnectivityOfSubLevelMesh(ns,ns+meshDim));
359 MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret(MEDCoupling1SGTUMesh::New(getName(),GetGeoTypeGivenMeshDimension(meshDim-1)));
360 ret->setNodalConnectivity(conn); ret->setCoords(coords);
365 * Creates a new unstructured mesh (MEDCouplingUMesh) from \a this structured one.
366 * \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
367 * delete this array using decrRef() as it is no more needed.
368 * \throw If \a this->getMeshDimension() is not among [1,2,3].
370 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const
372 MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret0(build1SGTUnstructured());
373 return ret0->buildUnstructured();
377 * Creates a new MEDCouplingUMesh containing a part of cells of \a this mesh.
378 * The cells to include to the
379 * result mesh are specified by an array of cell ids.
380 * \param [in] start - an array of cell ids to include to the result mesh.
381 * \param [in] end - specifies the end of the array \a start, so that
382 * the last value of \a start is \a end[ -1 ].
383 * \return MEDCouplingMesh * - a new instance of MEDCouplingUMesh. The caller is to
384 * delete this mesh using decrRef() as it is no more needed.
386 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
388 MEDCouplingUMesh *um=buildUnstructured();
389 MEDCouplingMesh *ret=um->buildPart(start,end);
394 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
396 std::vector<int> cgs(getCellGridStructure());
397 std::vector< std::pair<int,int> > cellPartFormat,nodePartFormat;
398 if(IsPartStructured(start,end,cgs,cellPartFormat))
400 MEDCouplingAutoRefCountObjectPtr<MEDCouplingStructuredMesh> ret(buildStructuredSubPart(cellPartFormat));
401 nodePartFormat=cellPartFormat;
402 for(std::vector< std::pair<int,int> >::iterator it=nodePartFormat.begin();it!=nodePartFormat.end();it++)
404 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp1(BuildExplicitIdsFrom(getNodeGridStructure(),nodePartFormat));
405 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp2(DataArrayInt::New()); tmp2->alloc(getNumberOfNodes(),1);
406 tmp2->fillWithValue(-1);
407 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp3(DataArrayInt::New()); tmp3->alloc(tmp1->getNumberOfTuples(),1); tmp3->iota(0);
408 tmp2->setPartOfValues3(tmp3,tmp1->begin(),tmp1->end(),0,1,1);
414 MEDCouplingUMesh *um=buildUnstructured();
415 MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
421 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy)
423 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
427 * Returns a new MEDCouplingFieldDouble holding normal vectors to cells of \a this
428 * 2D mesh. The computed vectors have 3 components and are normalized.
429 * \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on
430 * cells and one time. The caller is to delete this field using decrRef() as
431 * it is no more needed.
432 * \throw If \a this->getMeshDimension() != 2.
434 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
436 if(getMeshDimension()!=2)
437 throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
438 MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
439 DataArrayDouble *array=DataArrayDouble::New();
440 int nbOfCells=getNumberOfCells();
441 array->alloc(nbOfCells,3);
442 double *vals=array->getPointer();
443 for(int i=0;i<nbOfCells;i++)
444 { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
445 ret->setArray(array);
451 void MEDCouplingStructuredMesh::getReverseNodalConnectivity(DataArrayInt *revNodal, DataArrayInt *revNodalIndx) const
453 std::vector<int> ngs(getNodeGridStructure());
454 int dim(getSpaceDimension());
458 return GetReverseNodalConnectivity1(ngs,revNodal,revNodalIndx);
460 return GetReverseNodalConnectivity2(ngs,revNodal,revNodalIndx);
462 return GetReverseNodalConnectivity3(ngs,revNodal,revNodalIndx);
464 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getReverseNodalConnectivity : only dimensions 1, 2 and 3 are supported !");
468 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity1(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
471 revNodalIndx->alloc(nbNodes+1,1);
473 { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
475 { revNodal->alloc(1,1); revNodal->setIJ(0,0,0); revNodalIndx->setIJ(0,0,0); revNodalIndx->setIJ(1,0,1); return ; }
476 revNodal->alloc(2*(nbNodes-1),1);
477 int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
478 *rni++=0; *rni=1; *rn++=0;
479 for(int i=1;i<nbNodes-1;i++,rni++)
485 rn[0]=nbNodes-2; rni[1]=rni[0]+1;
488 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity2(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
490 int nbNodesX(ngs[0]),nbNodesY(ngs[1]);
491 int nbNodes(nbNodesX*nbNodesY);
492 if(nbNodesX==0 || nbNodesY==0)
493 { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
494 if(nbNodesX==1 || nbNodesY==1)
495 { std::vector<int> ngs2(1); ngs2[0]=std::max(nbNodesX,nbNodesY); return GetReverseNodalConnectivity1(ngs2,revNodal,revNodalIndx); }
496 revNodalIndx->alloc(nbNodes+1,1);
497 int nbCellsX(nbNodesX-1),nbCellsY(nbNodesY-1);
498 revNodal->alloc(4*(nbNodesX-2)*(nbNodesY-2)+2*2*(nbNodesX-2)+2*2*(nbNodesY-2)+4,1);
499 int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
500 *rni++=0; *rni=1; *rn++=0;
501 for(int i=1;i<nbNodesX-1;i++,rni++,rn+=2)
506 rni[1]=rni[0]+1; *rn++=nbCellsX-1;
508 for(int j=1;j<nbNodesY-1;j++)
510 int off(nbCellsX*(j-1)),off2(nbCellsX*j);
511 rni[1]=rni[0]+2; rn[0]=off; rn[1]=off2;
513 for(int i=1;i<nbNodesX-1;i++,rni++,rn+=4)
515 rn[0]=i-1+off; rn[1]=i+off; rn[2]=i-1+off2; rn[3]=i+off2;
518 rni[1]=rni[0]+2; rn[0]=off+nbCellsX-1; rn[1]=off2+nbCellsX-1;
521 int off3(nbCellsX*(nbCellsY-1));
524 for(int i=1;i<nbNodesX-1;i++,rni++,rn+=2)
526 rn[0]=i-1+off3; rn[1]=i+off3;
529 rni[1]=rni[0]+1; rn[0]=nbCellsX*nbCellsY-1;
532 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity3(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
534 int nbNodesX(ngs[0]),nbNodesY(ngs[1]),nbNodesZ(ngs[2]);
535 int nbNodes(nbNodesX*nbNodesY*nbNodesZ);
536 if(nbNodesX==0 || nbNodesY==0 || nbNodesZ==0)
537 { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
538 if(nbNodesX==1 || nbNodesY==1 || nbNodesZ==1)
540 std::vector<int> ngs2(2);
546 { ngs2[pos++]=ngs[i]; }
551 { ngs2[pos++]=ngs[i]; }
554 return GetReverseNodalConnectivity2(ngs2,revNodal,revNodalIndx);
556 revNodalIndx->alloc(nbNodes+1,1);
557 int nbCellsX(nbNodesX-1),nbCellsY(nbNodesY-1),nbCellsZ(nbNodesZ-1);
558 revNodal->alloc(8*(nbNodesX-2)*(nbNodesY-2)*(nbNodesZ-2)+4*(2*(nbNodesX-2)*(nbNodesY-2)+2*(nbNodesX-2)*(nbNodesZ-2)+2*(nbNodesY-2)*(nbNodesZ-2))+2*4*(nbNodesX-2)+2*4*(nbNodesY-2)+2*4*(nbNodesZ-2)+8,1);
559 int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
561 for(int k=0;k<nbNodesZ;k++)
563 bool factZ(k!=0 && k!=nbNodesZ-1);
564 int offZ0((k-1)*nbCellsX*nbCellsY),offZ1(k*nbCellsX*nbCellsY);
565 for(int j=0;j<nbNodesY;j++)
567 bool factYZ(factZ && (j!=0 && j!=nbNodesY-1));
568 int off00((j-1)*nbCellsX+offZ0),off01(j*nbCellsX+offZ0),off10((j-1)*nbCellsX+offZ1),off11(j*nbCellsX+offZ1);
569 for(int i=0;i<nbNodesX;i++,rni++)
571 int fact(factYZ && (i!=0 && i!=nbNodesX-1));
573 {//most of points fall in this part of code
574 rn[0]=off00+i-1; rn[1]=off00+i; rn[2]=off01+i-1; rn[3]=off01+i;
575 rn[4]=off10+i-1; rn[5]=off10+i; rn[6]=off11+i-1; rn[7]=off11+i;
582 if(k>=1 && j>=1 && i>=1)
584 if(k>=1 && j>=1 && i<nbCellsX)
586 if(k>=1 && j<nbCellsY && i>=1)
588 if(k>=1 && j<nbCellsY && i<nbCellsX)
591 if(k<nbCellsZ && j>=1 && i>=1)
593 if(k<nbCellsZ && j>=1 && i<nbCellsX)
595 if(k<nbCellsZ && j<nbCellsY && i>=1)
597 if(k<nbCellsZ && j<nbCellsY && i<nbCellsX)
599 rni[1]=rni[0]+(int)(std::distance(rnRef,rn));
607 * \return DataArrayInt * - newly allocated instance of nodal connectivity compatible for MEDCoupling1SGTMesh instance
609 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity(const int *nodeStBg, const int *nodeStEnd)
612 int dim(ZipNodeStructure(nodeStBg,nodeStEnd,zippedNodeSt));
617 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
618 conn->alloc(1,1); conn->setIJ(0,0,0);
622 return Build1GTNodalConnectivity1D(zippedNodeSt);
624 return Build1GTNodalConnectivity2D(zippedNodeSt);
626 return Build1GTNodalConnectivity3D(zippedNodeSt);
628 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::Build1GTNodalConnectivity : only dimension in [0,1,2,3] supported !");
632 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh(const int *nodeStBg, const int *nodeStEnd)
634 std::size_t dim(std::distance(nodeStBg,nodeStEnd));
638 return Build1GTNodalConnectivityOfSubLevelMesh3D(nodeStBg);
640 return Build1GTNodalConnectivityOfSubLevelMesh2D(nodeStBg);
642 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh: only dimension in [2,3] supported !");
647 * This method retrieves the number of entities (it can be cells or nodes) given a range in compact standard format
648 * used in methods like BuildExplicitIdsFrom,IsPartStructured.
650 * \sa BuildExplicitIdsFrom,IsPartStructured
652 int MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt(const std::vector< std::pair<int,int> >& partCompactFormat)
655 bool isFetched(false);
657 for(std::vector< std::pair<int,int> >::const_iterator it=partCompactFormat.begin();it!=partCompactFormat.end();it++,ii++)
659 int a((*it).first),b((*it).second);
660 if(a<0 || b<0 || b-a<0)
662 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt : invalid input at dimension " << ii << " !";
663 throw INTERP_KERNEL::Exception(oss.str().c_str());
671 return isFetched?ret:0;
674 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity1D(const int *nodeStBg)
676 int nbOfCells(*nodeStBg-1);
677 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
678 conn->alloc(2*nbOfCells,1);
679 int *cp=conn->getPointer();
680 for(int i=0;i<nbOfCells;i++)
688 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity2D(const int *nodeStBg)
690 int n1=nodeStBg[0]-1;
691 int n2=nodeStBg[1]-1;
692 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
693 conn->alloc(4*n1*n2,1);
694 int *cp=conn->getPointer();
696 for(int j=0;j<n2;j++)
697 for(int i=0;i<n1;i++,pos++)
699 cp[4*pos+0]=i+1+j*(n1+1);
700 cp[4*pos+1]=i+j*(n1+1);
701 cp[4*pos+2]=i+(j+1)*(n1+1);
702 cp[4*pos+3]=i+1+(j+1)*(n1+1);
707 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity3D(const int *nodeStBg)
709 int n1=nodeStBg[0]-1;
710 int n2=nodeStBg[1]-1;
711 int n3=nodeStBg[2]-1;
712 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
713 conn->alloc(8*n1*n2*n3,1);
714 int *cp=conn->getPointer();
716 for(int k=0;k<n3;k++)
717 for(int j=0;j<n2;j++)
718 for(int i=0;i<n1;i++,pos++)
720 int tmp=(n1+1)*(n2+1);
721 cp[8*pos+0]=i+1+j*(n1+1)+k*tmp;
722 cp[8*pos+1]=i+j*(n1+1)+k*tmp;
723 cp[8*pos+2]=i+(j+1)*(n1+1)+k*tmp;
724 cp[8*pos+3]=i+1+(j+1)*(n1+1)+k*tmp;
725 cp[8*pos+4]=i+1+j*(n1+1)+(k+1)*tmp;
726 cp[8*pos+5]=i+j*(n1+1)+(k+1)*tmp;
727 cp[8*pos+6]=i+(j+1)*(n1+1)+(k+1)*tmp;
728 cp[8*pos+7]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
733 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh3D(const int *nodeStBg)
735 std::vector<int> ngs(3);
736 int n0(nodeStBg[0]-1),n1(nodeStBg[1]-1),n2(nodeStBg[2]-1); ngs[0]=n0; ngs[1]=n1; ngs[2]=n2;
737 int off0(nodeStBg[0]),off1(nodeStBg[0]*nodeStBg[1]);
738 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
739 conn->alloc(4*GetNumberOfCellsOfSubLevelMesh(ngs,3));
740 int *cp(conn->getPointer());
742 for(int i=0;i<nodeStBg[0];i++)
743 for(int j=0;j<n1;j++)
744 for(int k=0;k<n2;k++,cp+=4)
745 { cp[0]=k*off1+j*off0+i; cp[1]=(k+1)*off1+j*off0+i; cp[2]=(k+1)*off1+(j+1)*off0+i; cp[3]=k*off1+(j+1)*off0+i; }
747 for(int j=0;j<nodeStBg[1];j++)
748 for(int i=0;i<n0;i++)
749 for(int k=0;k<n2;k++,cp+=4)
750 { cp[0]=k*off1+j*off0+i; cp[1]=(k+1)*off1+j*off0+i; cp[2]=(k+1)*off1+j*off0+(i+1); cp[3]=k*off1+j*off0+(i+1); }
752 for(int k=0;k<nodeStBg[2];k++)
753 for(int i=0;i<n0;i++)
754 for(int j=0;j<n1;j++,cp+=4)
755 { cp[0]=k*off1+j*off0+i; cp[1]=k*off1+j*off0+(i+1); cp[2]=k*off1+(j+1)*off0+(i+1); cp[3]=k*off1+(j+1)*off0+i; }
760 * This method computes given the nodal structure defined by [ \a nodeStBg , \a nodeStEnd ) the zipped form.
761 * std::distance( \a nodeStBg, \a nodeStEnd ) is equal to the space dimension. The returned value is equal to
762 * the meshDimension (or the zipped spaceDimension).
764 * \param [out] zipNodeSt - The zipped node strucutre
767 int MEDCouplingStructuredMesh::ZipNodeStructure(const int *nodeStBg, const int *nodeStEnd, int zipNodeSt[3])
769 int spaceDim((int)std::distance(nodeStBg,nodeStEnd));
770 if(spaceDim>3 || spaceDim<1)
771 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ZipNodeStructure : spaceDim must in [1,2,3] !");
772 zipNodeSt[0]=0; zipNodeSt[1]=0; zipNodeSt[2]=0;
774 for(int i=0;i<spaceDim;i++)
776 int elt(nodeStBg[i]);
779 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::ZipNodeStructure : the input nodal structure at pos#" << i << "(" << nodeStBg[i] << ") is invalid !";
780 throw INTERP_KERNEL::Exception(oss.str().c_str());
783 zipNodeSt[zippedI++]=elt;
788 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh2D(const int *nodeStBg)
790 std::vector<int> ngs(2);
791 int n0(nodeStBg[0]-1),n1(nodeStBg[1]-1); ngs[0]=n0; ngs[1]=n1;
792 int off0(nodeStBg[0]);
793 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
794 conn->alloc(2*GetNumberOfCellsOfSubLevelMesh(ngs,2));
795 int *cp(conn->getPointer());
797 for(int i=0;i<nodeStBg[0];i++)
798 for(int j=0;j<n1;j++,cp+=2)
799 { cp[0]=j*off0+i; cp[1]=(j+1)*off0+i; }
801 for(int j=0;j<nodeStBg[1];j++)
802 for(int i=0;i<n0;i++,cp+=2)
803 { cp[0]=j*off0+i; cp[1]=j*off0+(i+1); }
808 * Returns a cell id by its (i,j,k) index. The cell is located between the i-th and
809 * ( i + 1 )-th nodes along X axis etc.
810 * \param [in] i - a index of node coordinates array along X axis.
811 * \param [in] j - a index of node coordinates array along Y axis.
812 * \param [in] k - a index of node coordinates array along Z axis.
813 * \return int - a cell id in \a this mesh.
815 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
819 int meshDim(getMeshDimension());
820 getSplitCellValues(tmp2);
821 std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
822 return std::accumulate(tmp,tmp+meshDim,0);
826 * Returns a node id by its (i,j,k) index.
827 * \param [in] i - a index of node coordinates array along X axis.
828 * \param [in] j - a index of node coordinates array along Y axis.
829 * \param [in] k - a index of node coordinates array along Z axis.
830 * \return int - a node id in \a this mesh.
832 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
836 int spaceDim(getSpaceDimension());
837 getSplitNodeValues(tmp2);
838 std::transform(tmp,tmp+spaceDim,tmp2,tmp,std::multiplies<int>());
839 return std::accumulate(tmp,tmp+spaceDim,0);
843 int MEDCouplingStructuredMesh::getNumberOfCells() const
845 std::vector<int> ngs(getNodeGridStructure());
847 bool isCatched(false);
849 for(std::vector<int>::const_iterator it=ngs.begin();it!=ngs.end();it++,ii++)
854 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCells : at pos #" << ii << " the number of nodes in nodeStructure is " << *it << " ! Must be > 0 !";
855 throw INTERP_KERNEL::Exception(oss.str().c_str());
863 return isCatched?ret:0;
866 int MEDCouplingStructuredMesh::getNumberOfNodes() const
868 std::vector<int> ngs(getNodeGridStructure());
870 for(std::vector<int>::const_iterator it=ngs.begin();it!=ngs.end();it++)
875 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
878 for(int i=meshDim-1;i>=0;i--)
880 int pos=work/split[i];
886 std::vector<int> MEDCouplingStructuredMesh::getCellGridStructure() const
888 std::vector<int> ret(getNodeGridStructure());
889 std::transform(ret.begin(),ret.end(),ret.begin(),std::bind2nd(std::plus<int>(),-1));
894 * This method states if given part ids [ \a startIds, \a stopIds) and a structure \a st returns if it can be considered as a structured dataset.
895 * If true is returned \a partCompactFormat will contain the information to build the corresponding part.
897 * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom, MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt
899 bool MEDCouplingStructuredMesh::IsPartStructured(const int *startIds, const int *stopIds, const std::vector<int>& st, std::vector< std::pair<int,int> >& partCompactFormat)
901 int dim((int)st.size());
902 partCompactFormat.resize(dim);
904 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::isPartStructured : input structure must be of dimension in [1,2,3] !");
905 std::vector<int> tmp2(dim),tmp(dim),tmp3(dim),tmp4(dim); tmp2[0]=1;
906 for(int i=1;i<dim;i++)
907 tmp2[i]=tmp2[i-1]*st[i-1];
908 std::size_t sz(std::distance(startIds,stopIds));
910 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : empty input !");
911 GetPosFromId(*startIds,dim,&tmp2[0],&tmp[0]);
912 partCompactFormat.resize(dim);
913 for(int i=0;i<dim;i++)
914 partCompactFormat[i].first=tmp[i];
915 if(tmp[dim-1]<0 || tmp[dim-1]>=st[dim-1])
916 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : first id in input is not in valid range !");
919 for(int i=0;i<dim;i++)
920 partCompactFormat[i].second=tmp[i]+1;
923 GetPosFromId(startIds[sz-1],dim,&tmp2[0],&tmp3[0]);
925 for(int i=0;i<dim;i++)
927 if(tmp3[i]<0 || tmp3[i]>=st[i])
928 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : last id in input is not in valid range !");
929 partCompactFormat[i].second=tmp3[i]+1;
930 tmp4[i]=partCompactFormat[i].second-partCompactFormat[i].first;
937 const int *w(startIds);
942 for(int i=0;i<tmp4[2];i++)
944 int a=tmp2[2]*(partCompactFormat[2].first+i);
945 for(int j=0;j<tmp4[1];j++)
947 int b=tmp2[1]*(partCompactFormat[1].first+j);
948 for(int k=0;k<tmp4[0];k++,w++)
950 if(partCompactFormat[0].first+k+b+a!=*w)
959 for(int j=0;j<tmp4[1];j++)
961 int b=tmp2[1]*(partCompactFormat[1].first+j);
962 for(int k=0;k<tmp4[0];k++,w++)
964 if(partCompactFormat[0].first+k+b!=*w)
972 for(int k=0;k<tmp4[0];k++,w++)
974 if(partCompactFormat[0].first+k!=*w)
980 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : internal error !");
985 * This method builds the explicit entity array from the structure in \a st and the range in \a partCompactFormat.
986 * If the range contains invalid values regarding sructure an exception will be thrown.
988 * \return DataArrayInt * - a new object.
989 * \sa MEDCouplingStructuredMesh::IsPartStructured, MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt
991 DataArrayInt *MEDCouplingStructuredMesh::BuildExplicitIdsFrom(const std::vector<int>& st, const std::vector< std::pair<int,int> >& partCompactFormat)
993 if(st.size()!=partCompactFormat.size())
994 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : input arrays must have the same size !");
996 std::vector<int> dims(st.size());
997 for(std::size_t i=0;i<st.size();i++)
999 if(partCompactFormat[i].first<0 || partCompactFormat[i].first>st[i])
1000 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 1 !");
1001 if(partCompactFormat[i].second<0 || partCompactFormat[i].second>st[i])
1002 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 2 !");
1003 if(partCompactFormat[i].second<=partCompactFormat[i].first)
1004 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 3 !");
1005 dims[i]=partCompactFormat[i].second-partCompactFormat[i].first;
1008 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
1009 ret->alloc(nbOfItems,1);
1010 int *pt(ret->getPointer());
1015 for(int i=0;i<dims[2];i++)
1017 int a=(partCompactFormat[2].first+i)*st[0]*st[1];
1018 for(int j=0;j<dims[1];j++)
1020 int b=(partCompactFormat[1].first+j)*st[0];
1021 for(int k=0;k<dims[0];k++,pt++)
1022 *pt=partCompactFormat[0].first+k+b+a;
1029 for(int j=0;j<dims[1];j++)
1031 int b=(partCompactFormat[1].first+j)*st[0];
1032 for(int k=0;k<dims[0];k++,pt++)
1033 *pt=partCompactFormat[0].first+k+b;
1039 for(int k=0;k<dims[0];k++,pt++)
1040 *pt=partCompactFormat[0].first+k;
1044 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : Dimension supported are 1,2 or 3 !");
1049 int MEDCouplingStructuredMesh::GetNumberOfCellsOfSubLevelMesh(const std::vector<int>& cgs, int mdim)
1052 for(int i=0;i<mdim;i++)
1055 for(int j=0;j<mdim;j++)