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 "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::getHeapMemorySize() const
45 return MEDCouplingMesh::getHeapMemorySize();
48 void MEDCouplingStructuredMesh::copyTinyStringsFrom(const MEDCouplingMesh *other) throw(INTERP_KERNEL::Exception)
50 MEDCouplingMesh::copyTinyStringsFrom(other);
53 bool MEDCouplingStructuredMesh::isEqualIfNotWhy(const MEDCouplingMesh *other, double prec, std::string& reason) const throw(INTERP_KERNEL::Exception)
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) throw(INTERP_KERNEL::Exception)
68 return INTERP_KERNEL::NORM_HEXA8;
70 return INTERP_KERNEL::NORM_QUAD4;
72 return INTERP_KERNEL::NORM_SEG2;
74 throw INTERP_KERNEL::Exception("Unexpected dimension for MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension !");
78 std::set<INTERP_KERNEL::NormalizedCellType> MEDCouplingStructuredMesh::getAllGeoTypes() const
80 std::set<INTERP_KERNEL::NormalizedCellType> ret2;
81 ret2.insert(getTypeOfCell(0));
85 int MEDCouplingStructuredMesh::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
87 int ret=getNumberOfCells();
88 if(type==getTypeOfCell(0))
90 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
91 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCellsWithType : no specified type ! Type available is " << cm.getRepr() << " !";
92 throw INTERP_KERNEL::Exception(oss.str().c_str());
95 DataArrayInt *MEDCouplingStructuredMesh::giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
97 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
98 if(getTypeOfCell(0)==type)
100 ret->alloc(getNumberOfCells(),1);
108 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfNodesPerCell() const throw(INTERP_KERNEL::Exception)
110 int nbCells=getNumberOfCells();
111 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
112 ret->alloc(nbCells,1);
113 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
114 ret->fillWithValue((int)cm.getNumberOfNodes());
118 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfFacesPerCell() const throw(INTERP_KERNEL::Exception)
120 int nbCells=getNumberOfCells();
121 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
122 ret->alloc(nbCells,1);
123 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
124 ret->fillWithValue((int)cm.getNumberOfSons());
129 * This method computes effective number of nodes per cell. That is to say nodes appearing several times in nodal connectivity of a cell,
130 * will be counted only once here whereas it will be counted several times in MEDCouplingMesh::computeNbOfNodesPerCell method.
131 * Here for structured mesh it returns exactly as MEDCouplingStructuredMesh::computeNbOfNodesPerCell does.
133 * \return DataArrayInt * - new object to be deallocated by the caller.
135 DataArrayInt *MEDCouplingStructuredMesh::computeEffectiveNbOfNodesPerCell() const throw(INTERP_KERNEL::Exception)
137 return computeNbOfNodesPerCell();
140 void MEDCouplingStructuredMesh::getNodeIdsOfCell(int cellId, std::vector<int>& conn) const
142 int meshDim=getMeshDimension();
143 int tmpCell[3],tmpNode[3];
144 getSplitCellValues(tmpCell);
145 getSplitNodeValues(tmpNode);
147 GetPosFromId(cellId,meshDim,tmpCell,tmp2);
151 conn.push_back(tmp2[0]); conn.push_back(tmp2[0]+1);
154 conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1);
155 conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]+1); conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]);
158 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]);
159 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]);
160 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]);
161 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]);
164 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getNodeIdsOfCell : big problem spacedim must be in 1,2 or 3 !");
169 * See MEDCouplingUMesh::getDistributionOfTypes for more information
171 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const throw(INTERP_KERNEL::Exception)
173 //only one type of cell
174 std::vector<int> ret(3);
175 ret[0]=getTypeOfCell(0);
176 ret[1]=getNumberOfCells();
177 ret[2]=-1; //ret[3*k+2]==-1 because it has no sense here
182 * This method tries to minimize at most the number of deep copy.
183 * So if \a idsPerType is not empty it can be returned directly (without copy, but with ref count incremented) in return.
185 * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
187 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
189 int nbOfCells=getNumberOfCells();
191 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : invalid input code should be exactly of size 3 !");
192 if(code[0]!=(int)getTypeOfCell(0))
194 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : Mismatch of geometric type ! Asking for " << code[0] << " whereas the geometric type is \a this is " << getTypeOfCell(0) << " !";
195 throw INTERP_KERNEL::Exception(oss.str().c_str());
199 if(code[1]==nbOfCells)
203 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : mismatch between the number of cells in this (" << nbOfCells << ") and the number of non profile (" << code[1] << ") !";
204 throw INTERP_KERNEL::Exception(oss.str().c_str());
208 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : single geo type mesh ! 0 or -1 is expected at pos #2 of input code !");
209 if(idsPerType.size()!=1)
210 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input code points to DataArrayInt #0 whereas the size of idsPerType is not equal to 1 !");
211 const DataArrayInt *pfl=idsPerType[0];
213 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : the input code points to a NULL DataArrayInt at rank 0 !");
214 if(pfl->getNumberOfComponents()!=1)
215 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input profile should have exactly one component !");
216 pfl->checkAllIdsInRange(0,nbOfCells);
218 return const_cast<DataArrayInt *>(pfl);
222 * 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.
223 * 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.
224 * This method has 1 input \a profile and 3 outputs \a code \a idsInPflPerType and \a idsPerType.
226 * \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.
227 * \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,
228 * \a idsInPflPerType[i] stores the tuple ids in \a profile that correspond to the geometric type code[3*i+0]
229 * \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.
230 * This vector can be empty in case of all geometric type cells are fully covered in ascending in the given input \a profile.
232 * \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.
234 * \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
237 * - Before \a this has 3 cells \a profile contains [0,1,2]
238 * - After \a code contains [NORM_...,nbCells,-1], \a idsInPflPerType [[0,1,2]] and \a idsPerType is empty <br>
241 * - Before \a this has 3 cells \a profile contains [1,2]
242 * - After \a code contains [NORM_...,nbCells,0], \a idsInPflPerType [[0,1]] and \a idsPerType is [[1,2]] <br>
245 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
248 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile is NULL !");
249 if(profile->getNumberOfComponents()!=1)
250 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile should have exactly one component !");
251 int nbTuples=profile->getNumberOfTuples();
252 int nbOfCells=getNumberOfCells();
253 code.resize(3); idsInPflPerType.resize(1);
254 code[0]=(int)getTypeOfCell(0); code[1]=nbOfCells;
255 idsInPflPerType.resize(1);
256 if(profile->isIdentity() && nbTuples==nbOfCells)
259 idsInPflPerType[0]=const_cast<DataArrayInt *>(profile); idsInPflPerType[0]->incrRef();
263 profile->checkAllIdsInRange(0,nbOfCells);
264 idsPerType.resize(1);
265 idsPerType[0]=const_cast<DataArrayInt *>(profile); idsPerType[0]->incrRef();
266 idsInPflPerType[0]=DataArrayInt::Range(0,nbTuples,1);
270 * Creates a new unstructured mesh (MEDCoupling1SGTUMesh) from \a this structured one.
271 * \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
272 * delete this array using decrRef() as it is no more needed.
273 * \throw If \a this->getMeshDimension() is not among [1,2,3].
275 MEDCoupling1SGTUMesh *MEDCouplingStructuredMesh::build1SGTUnstructured() const throw(INTERP_KERNEL::Exception)
277 int meshDim=getMeshDimension();
278 if(meshDim<0 || meshDim>3)
279 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::build1SGTUnstructured : meshdim must be in [1,2,3] !");
280 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(getCoordinatesAndOwner());
282 getNodeGridStructure(ns);
283 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(Build1GTNodalConnectivity(ns,ns+meshDim));
284 MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret(MEDCoupling1SGTUMesh::New(getName().c_str(),GetGeoTypeGivenMeshDimension(meshDim)));
285 ret->setNodalConnectivity(conn); ret->setCoords(coords);
290 * Creates a new unstructured mesh (MEDCouplingUMesh) from \a this structured one.
291 * \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
292 * delete this array using decrRef() as it is no more needed.
293 * \throw If \a this->getMeshDimension() is not among [1,2,3].
295 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const throw(INTERP_KERNEL::Exception)
297 MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret0(build1SGTUnstructured());
298 return ret0->buildUnstructured();
302 * Creates a new MEDCouplingUMesh containing a part of cells of \a this mesh.
303 * The cells to include to the
304 * result mesh are specified by an array of cell ids.
305 * \param [in] start - an array of cell ids to include to the result mesh.
306 * \param [in] end - specifies the end of the array \a start, so that
307 * the last value of \a start is \a end[ -1 ].
308 * \return MEDCouplingMesh * - a new instance of MEDCouplingUMesh. The caller is to
309 * delete this mesh using decrRef() as it is no more needed.
311 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
313 MEDCouplingUMesh *um=buildUnstructured();
314 MEDCouplingMesh *ret=um->buildPart(start,end);
319 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
321 std::vector<int> cgs(getCellGridStructure());
322 std::vector< std::pair<int,int> > cellPartFormat,nodePartFormat;
323 if(IsPartStructured(start,end,cgs,cellPartFormat))
325 MEDCouplingAutoRefCountObjectPtr<MEDCouplingStructuredMesh> ret(buildStructuredSubPart(cellPartFormat));
326 nodePartFormat=cellPartFormat;
327 for(std::vector< std::pair<int,int> >::iterator it=nodePartFormat.begin();it!=nodePartFormat.end();it++)
329 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp1(BuildExplicitIdsFrom(getNodeGridStructure(),nodePartFormat));
330 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp2(DataArrayInt::New()); tmp2->alloc(getNumberOfNodes(),1);
331 tmp2->fillWithValue(-1);
332 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp3(DataArrayInt::New()); tmp3->alloc(tmp1->getNumberOfTuples(),1); tmp3->iota(0);
333 tmp2->setPartOfValues3(tmp3,tmp1->begin(),tmp1->end(),0,1,1);
339 MEDCouplingUMesh *um=buildUnstructured();
340 MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
346 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy) throw(INTERP_KERNEL::Exception)
348 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
352 * Returns a new MEDCouplingFieldDouble holding normal vectors to cells of \a this
353 * 2D mesh. The computed vectors have 3 components and are normalized.
354 * \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on
355 * cells and one time. The caller is to delete this field using decrRef() as
356 * it is no more needed.
357 * \throw If \a this->getMeshDimension() != 2.
359 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
361 if(getMeshDimension()!=2)
362 throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
363 MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
364 DataArrayDouble *array=DataArrayDouble::New();
365 int nbOfCells=getNumberOfCells();
366 array->alloc(nbOfCells,3);
367 double *vals=array->getPointer();
368 for(int i=0;i<nbOfCells;i++)
369 { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
370 ret->setArray(array);
377 * \return DataArrayInt * - newly allocated instance of nodal connectivity compatible for MEDCoupling1SGTMesh instance
379 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity(const int *nodeStBg, const int *nodeStEnd) throw(INTERP_KERNEL::Exception)
381 std::size_t dim=std::distance(nodeStBg,nodeStEnd);
385 return Build1GTNodalConnectivity1D(nodeStBg);
387 return Build1GTNodalConnectivity2D(nodeStBg);
389 return Build1GTNodalConnectivity3D(nodeStBg);
391 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::Build1GTNodalConnectivity : only dimension in [1,2,3] supported !");
395 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity1D(const int *nodeStBg) throw(INTERP_KERNEL::Exception)
397 int nbOfCells(*nodeStBg-1);
398 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
399 conn->alloc(2*nbOfCells,1);
400 int *cp=conn->getPointer();
401 for(int i=0;i<nbOfCells;i++)
409 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity2D(const int *nodeStBg) throw(INTERP_KERNEL::Exception)
411 int n1=nodeStBg[0]-1;
412 int n2=nodeStBg[1]-1;
413 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
414 conn->alloc(4*n1*n2,1);
415 int *cp=conn->getPointer();
417 for(int j=0;j<n2;j++)
418 for(int i=0;i<n1;i++,pos++)
420 cp[4*pos+0]=i+1+j*(n1+1);
421 cp[4*pos+1]=i+j*(n1+1);
422 cp[4*pos+2]=i+(j+1)*(n1+1);
423 cp[4*pos+3]=i+1+(j+1)*(n1+1);
428 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity3D(const int *nodeStBg) throw(INTERP_KERNEL::Exception)
430 int n1=nodeStBg[0]-1;
431 int n2=nodeStBg[1]-1;
432 int n3=nodeStBg[2]-1;
433 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
434 conn->alloc(8*n1*n2*n3,1);
435 int *cp=conn->getPointer();
437 for(int k=0;k<n3;k++)
438 for(int j=0;j<n2;j++)
439 for(int i=0;i<n1;i++,pos++)
441 int tmp=(n1+1)*(n2+1);
442 cp[8*pos+0]=i+1+j*(n1+1)+k*tmp;
443 cp[8*pos+1]=i+j*(n1+1)+k*tmp;
444 cp[8*pos+2]=i+(j+1)*(n1+1)+k*tmp;
445 cp[8*pos+3]=i+1+(j+1)*(n1+1)+k*tmp;
446 cp[8*pos+4]=i+1+j*(n1+1)+(k+1)*tmp;
447 cp[8*pos+5]=i+j*(n1+1)+(k+1)*tmp;
448 cp[8*pos+6]=i+(j+1)*(n1+1)+(k+1)*tmp;
449 cp[8*pos+7]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
455 * Returns a cell id by its (i,j,k) index. The cell is located between the i-th and
456 * ( i + 1 )-th nodes along X axis etc.
457 * \param [in] i - a index of node coordinates array along X axis.
458 * \param [in] j - a index of node coordinates array along Y axis.
459 * \param [in] k - a index of node coordinates array along Z axis.
460 * \return int - a cell id in \a this mesh.
462 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
466 int meshDim=getMeshDimension();
467 getSplitCellValues(tmp2);
468 std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
469 return std::accumulate(tmp,tmp+meshDim,0);
473 * Returns a node id by its (i,j,k) index.
474 * \param [in] i - a index of node coordinates array along X axis.
475 * \param [in] j - a index of node coordinates array along Y axis.
476 * \param [in] k - a index of node coordinates array along Z axis.
477 * \return int - a node id in \a this mesh.
479 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
483 int meshDim=getMeshDimension();
484 getSplitNodeValues(tmp2);
485 std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
486 return std::accumulate(tmp,tmp+meshDim,0);
489 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
492 for(int i=meshDim-1;i>=0;i--)
494 int pos=work/split[i];
500 std::vector<int> MEDCouplingStructuredMesh::getCellGridStructure() const throw(INTERP_KERNEL::Exception)
502 std::vector<int> ret(getNodeGridStructure());
503 std::transform(ret.begin(),ret.end(),ret.begin(),std::bind2nd(std::plus<int>(),-1));
508 * 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.
509 * If true is returned \a partCompactFormat will contain the information to build the corresponding part.
511 * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom
513 bool MEDCouplingStructuredMesh::IsPartStructured(const int *startIds, const int *stopIds, const std::vector<int>& st, std::vector< std::pair<int,int> >& partCompactFormat) throw(INTERP_KERNEL::Exception)
515 int dim((int)st.size());
516 partCompactFormat.resize(dim);
518 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::isPartStructured : input structure must be of dimension in [1,2,3] !");
519 std::vector<int> tmp2(dim),tmp(dim),tmp3(dim),tmp4(dim); tmp2[0]=1;
520 for(int i=1;i<dim;i++)
521 tmp2[i]=tmp2[i-1]*st[i-1];
522 std::size_t sz(std::distance(startIds,stopIds));
524 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : empty input !");
525 GetPosFromId(*startIds,dim,&tmp2[0],&tmp[0]);
526 partCompactFormat.resize(dim);
527 for(int i=0;i<dim;i++)
528 partCompactFormat[i].first=tmp[i];
529 if(tmp[dim-1]<0 || tmp[dim-1]>=st[dim-1])
530 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : first id in input is not in valid range !");
533 for(int i=0;i<dim;i++)
534 partCompactFormat[i].second=tmp[i]+1;
537 GetPosFromId(startIds[sz-1],dim,&tmp2[0],&tmp3[0]);
539 for(int i=0;i<dim;i++)
541 if(tmp3[i]<0 || tmp3[i]>=st[i])
542 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : last id in input is not in valid range !");
543 partCompactFormat[i].second=tmp3[i]+1;
544 tmp4[i]=partCompactFormat[i].second-partCompactFormat[i].first;
551 const int *w(startIds);
556 for(int i=0;i<tmp4[2];i++)
558 int a=tmp2[2]*(partCompactFormat[2].first+i);
559 for(int j=0;j<tmp4[1];j++)
561 int b=tmp2[1]*(partCompactFormat[1].first+j);
562 for(int k=0;k<tmp4[0];k++,w++)
564 if(partCompactFormat[0].first+k+b+a!=*w)
573 for(int j=0;j<tmp4[1];j++)
575 int b=tmp2[1]*(partCompactFormat[1].first+j);
576 for(int k=0;k<tmp4[0];k++,w++)
578 if(partCompactFormat[0].first+k+b!=*w)
586 for(int k=0;k<tmp4[0];k++,w++)
588 if(partCompactFormat[0].first+k!=*w)
594 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : internal error !");
599 * This method builds the explicit entity array from the structure in \a st and the range in \a partCompactFormat.
600 *If the range contains invalid values regarding sructure an exception will be thrown.
602 * \return DataArrayInt * - a new object.
603 * \sa MEDCouplingStructuredMesh::IsPartStructured
605 DataArrayInt *MEDCouplingStructuredMesh::BuildExplicitIdsFrom(const std::vector<int>& st, const std::vector< std::pair<int,int> >& partCompactFormat) throw(INTERP_KERNEL::Exception)
607 if(st.size()!=partCompactFormat.size())
608 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : input arrays must have the same size !");
610 std::vector<int> dims(st.size());
611 for(std::size_t i=0;i<st.size();i++)
613 if(partCompactFormat[i].first<0 || partCompactFormat[i].first>st[i])
614 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 1 !");
615 if(partCompactFormat[i].second<0 || partCompactFormat[i].second>st[i])
616 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 2 !");
617 if(partCompactFormat[i].second<=partCompactFormat[i].first)
618 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 3 !");
619 dims[i]=partCompactFormat[i].second-partCompactFormat[i].first;
622 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
623 ret->alloc(nbOfItems,1);
624 int *pt(ret->getPointer());
629 for(int i=0;i<dims[2];i++)
631 int a=(partCompactFormat[2].first+i)*st[0]*st[1];
632 for(int j=0;j<dims[1];j++)
634 int b=(partCompactFormat[1].first+j)*st[0];
635 for(int k=0;k<dims[0];k++,pt++)
636 *pt=partCompactFormat[0].first+k+b+a;
643 for(int j=0;j<dims[1];j++)
645 int b=(partCompactFormat[1].first+j)*st[0];
646 for(int k=0;k<dims[0];k++,pt++)
647 *pt=partCompactFormat[0].first+k+b;
653 for(int k=0;k<dims[0];k++,pt++)
654 *pt=partCompactFormat[0].first+k;
658 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : Dimension supported are 1,2 or 3 !");