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 switch(getMeshDimension())
62 return INTERP_KERNEL::NORM_HEXA8;
64 return INTERP_KERNEL::NORM_QUAD4;
66 return INTERP_KERNEL::NORM_SEG2;
68 throw INTERP_KERNEL::Exception("Unexpected dimension for MEDCouplingCurveLinearMesh::getTypeOfCell !");
72 std::set<INTERP_KERNEL::NormalizedCellType> MEDCouplingStructuredMesh::getAllGeoTypes() const
74 std::set<INTERP_KERNEL::NormalizedCellType> ret2;
75 ret2.insert(getTypeOfCell(0));
79 int MEDCouplingStructuredMesh::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
81 int ret=getNumberOfCells();
82 if(type==getTypeOfCell(0))
84 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
85 std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCellsWithType : no specified type ! Type available is " << cm.getRepr() << " !";
86 throw INTERP_KERNEL::Exception(oss.str().c_str());
89 DataArrayInt *MEDCouplingStructuredMesh::giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
91 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
92 if(getTypeOfCell(0)==type)
94 ret->alloc(getNumberOfCells(),1);
102 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfNodesPerCell() const throw(INTERP_KERNEL::Exception)
104 int nbCells=getNumberOfCells();
105 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
106 ret->alloc(nbCells,1);
107 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
108 ret->fillWithValue((int)cm.getNumberOfNodes());
112 void MEDCouplingStructuredMesh::getNodeIdsOfCell(int cellId, std::vector<int>& conn) const
114 int meshDim=getMeshDimension();
115 int tmpCell[3],tmpNode[3];
116 getSplitCellValues(tmpCell);
117 getSplitNodeValues(tmpNode);
119 GetPosFromId(cellId,meshDim,tmpCell,tmp2);
123 conn.push_back(tmp2[0]); conn.push_back(tmp2[0]+1);
126 conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1);
127 conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]+1); conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]);
130 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]);
131 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]);
132 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]);
133 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]);
136 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getNodeIdsOfCell : big problem spacedim must be in 1,2 or 3 !");
141 * See MEDCouplingUMesh::getDistributionOfTypes for more information
143 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const throw(INTERP_KERNEL::Exception)
145 //only one type of cell
146 std::vector<int> ret(3);
147 ret[0]=getTypeOfCell(0);
148 ret[1]=getNumberOfCells();
149 ret[2]=0; //ret[3*k+2]==0 because it has no sense here
154 * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
156 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
159 throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code is empty, should not !");
160 std::size_t sz=code.size();
162 throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code should be of size 3 exactly !");
164 int nbCells=getNumberOfCellsWithType((INTERP_KERNEL::NormalizedCellType)code[0]);
170 throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : number of cells mismatch !");
175 throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code[2]<-1 mismatch !");
176 if(code[2]>=(int)idsPerType.size())
177 throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code[2]>size idsPerType !");
178 return idsPerType[code[2]]->deepCpy();
183 * See MEDCouplingUMesh::splitProfilePerType for more information
185 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
187 int nbCells=getNumberOfCells();
189 code[0]=(int)getTypeOfCell(0);
192 idsInPflPerType.push_back(profile->deepCpy());
193 idsPerType.push_back(profile->deepCpy());
197 * Creates a new unstructured mesh (MEDCouplingUMesh) from \a this structured one.
198 * \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
199 * delete this array using decrRef() as it is no more needed.
200 * \throw If \a this->getMeshDimension() is not among [1,2,3].
202 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const throw(INTERP_KERNEL::Exception)
204 int meshDim=getMeshDimension();
205 MEDCouplingUMesh *ret=MEDCouplingUMesh::New(getName(),meshDim);
206 DataArrayDouble *coords=getCoordinatesAndOwner();
207 ret->setCoords(coords);
212 fill1DUnstructuredMesh(ret);
215 fill2DUnstructuredMesh(ret);
218 fill3DUnstructuredMesh(ret);
221 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::buildUnstructured : big problem spacedim must be in 1,2 or 3 !");
227 * Creates a new MEDCouplingUMesh containing a part of cells of \a this mesh.
228 * The cells to include to the
229 * result mesh are specified by an array of cell ids.
230 * \param [in] start - an array of cell ids to include to the result mesh.
231 * \param [in] end - specifies the end of the array \a start, so that
232 * the last value of \a start is \a end[ -1 ].
233 * \return MEDCouplingMesh * - a new instance of MEDCouplingUMesh. The caller is to
234 * delete this mesh using decrRef() as it is no more needed.
236 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
238 MEDCouplingUMesh *um=buildUnstructured();
239 MEDCouplingMesh *ret=um->buildPart(start,end);
244 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
246 MEDCouplingUMesh *um=buildUnstructured();
247 MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
252 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy) throw(INTERP_KERNEL::Exception)
254 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
258 * Returns a new MEDCouplingFieldDouble holding normal vectors to cells of \a this
259 * 2D mesh. The computed vectors have 3 components and are normalized.
260 * \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on
261 * cells and one time. The caller is to delete this field using decrRef() as
262 * it is no more needed.
263 * \throw If \a this->getMeshDimension() != 2.
265 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
267 if(getMeshDimension()!=2)
268 throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
269 MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
270 DataArrayDouble *array=DataArrayDouble::New();
271 int nbOfCells=getNumberOfCells();
272 array->alloc(nbOfCells,3);
273 double *vals=array->getPointer();
274 for(int i=0;i<nbOfCells;i++)
275 { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
276 ret->setArray(array);
282 void MEDCouplingStructuredMesh::fill1DUnstructuredMesh(MEDCouplingUMesh *m) const
285 getNodeGridStructure(&nbOfCells);
287 DataArrayInt *connI=DataArrayInt::New();
288 connI->alloc(nbOfCells+1,1);
289 int *ci=connI->getPointer();
290 DataArrayInt *conn=DataArrayInt::New();
291 conn->alloc(3*nbOfCells,1);
293 int *cp=conn->getPointer();
294 for(int i=0;i<nbOfCells;i++)
296 cp[3*i]=(int)INTERP_KERNEL::NORM_SEG2;
301 m->setConnectivity(conn,connI,true);
306 void MEDCouplingStructuredMesh::fill2DUnstructuredMesh(MEDCouplingUMesh *m) const
309 getNodeGridStructure(ns);
312 DataArrayInt *connI=DataArrayInt::New();
313 connI->alloc(n1*n2+1,1);
314 int *ci=connI->getPointer();
315 DataArrayInt *conn=DataArrayInt::New();
316 conn->alloc(5*n1*n2,1);
318 int *cp=conn->getPointer();
320 for(int j=0;j<n2;j++)
321 for(int i=0;i<n1;i++,pos++)
323 cp[5*pos]=(int)INTERP_KERNEL::NORM_QUAD4;
324 cp[5*pos+1]=i+1+j*(n1+1);
325 cp[5*pos+2]=i+j*(n1+1);
326 cp[5*pos+3]=i+(j+1)*(n1+1);
327 cp[5*pos+4]=i+1+(j+1)*(n1+1);
330 m->setConnectivity(conn,connI,true);
335 void MEDCouplingStructuredMesh::fill3DUnstructuredMesh(MEDCouplingUMesh *m) const
338 getNodeGridStructure(ns);
342 DataArrayInt *connI=DataArrayInt::New();
343 connI->alloc(n1*n2*n3+1,1);
344 int *ci=connI->getPointer();
345 DataArrayInt *conn=DataArrayInt::New();
346 conn->alloc(9*n1*n2*n3,1);
348 int *cp=conn->getPointer();
350 for(int k=0;k<n3;k++)
351 for(int j=0;j<n2;j++)
352 for(int i=0;i<n1;i++,pos++)
354 cp[9*pos]=(int)INTERP_KERNEL::NORM_HEXA8;
355 int tmp=(n1+1)*(n2+1);
356 cp[9*pos+1]=i+1+j*(n1+1)+k*tmp;
357 cp[9*pos+2]=i+j*(n1+1)+k*tmp;
358 cp[9*pos+3]=i+(j+1)*(n1+1)+k*tmp;
359 cp[9*pos+4]=i+1+(j+1)*(n1+1)+k*tmp;
360 cp[9*pos+5]=i+1+j*(n1+1)+(k+1)*tmp;
361 cp[9*pos+6]=i+j*(n1+1)+(k+1)*tmp;
362 cp[9*pos+7]=i+(j+1)*(n1+1)+(k+1)*tmp;
363 cp[9*pos+8]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
366 m->setConnectivity(conn,connI,true);
372 * Returns a cell id by its (i,j,k) index. The cell is located between the i-th and
373 * ( i + 1 )-th nodes along X axis etc.
374 * \param [in] i - a index of node coordinates array along X axis.
375 * \param [in] j - a index of node coordinates array along Y axis.
376 * \param [in] k - a index of node coordinates array along Z axis.
377 * \return int - a cell id in \a this mesh.
379 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
383 int meshDim=getMeshDimension();
384 getSplitCellValues(tmp2);
385 std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
386 return std::accumulate(tmp,tmp+meshDim,0);
390 * Returns a node id by its (i,j,k) index.
391 * \param [in] i - a index of node coordinates array along X axis.
392 * \param [in] j - a index of node coordinates array along Y axis.
393 * \param [in] k - a index of node coordinates array along Z axis.
394 * \return int - a node id in \a this mesh.
396 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
400 int meshDim=getMeshDimension();
401 getSplitNodeValues(tmp2);
402 std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
403 return std::accumulate(tmp,tmp+meshDim,0);
406 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
409 for(int i=meshDim-1;i>=0;i--)
411 int pos=work/split[i];