1 // Copyright (C) 2007-2012 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 INTERP_KERNEL::NormalizedCellType ret;
75 switch(getMeshDimension())
78 ret=INTERP_KERNEL::NORM_HEXA8;
81 ret=INTERP_KERNEL::NORM_QUAD4;
84 ret=INTERP_KERNEL::NORM_SEG2;
87 throw INTERP_KERNEL::Exception("Unexpected dimension for MEDCouplingStructuredMesh::getAllGeoTypes !");
89 std::set<INTERP_KERNEL::NormalizedCellType> ret2;
94 int MEDCouplingStructuredMesh::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
96 int ret=getNumberOfCells();
97 int dim=getMeshDimension();
100 case INTERP_KERNEL::NORM_HEXA8:
103 case INTERP_KERNEL::NORM_QUAD4:
106 case INTERP_KERNEL::NORM_SEG2:
110 throw INTERP_KERNEL::Exception("Unexpected dimension for MEDCouplingStructuredMesh::getTypeOfCell !");
115 void MEDCouplingStructuredMesh::getNodeIdsOfCell(int cellId, std::vector<int>& conn) const
117 int meshDim=getMeshDimension();
118 int tmpCell[3],tmpNode[3];
119 getSplitCellValues(tmpCell);
120 getSplitNodeValues(tmpNode);
122 GetPosFromId(cellId,meshDim,tmpCell,tmp2);
126 conn.push_back(tmp2[0]); conn.push_back(tmp2[0]+1);
129 conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1);
130 conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]+1); conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]);
133 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]);
134 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]);
135 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]);
136 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]);
139 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getNodeIdsOfCell : big problem spacedim must be in 1,2 or 3 !");
144 * See MEDCouplingUMesh::getDistributionOfTypes for more information
146 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const throw(INTERP_KERNEL::Exception)
148 //only one type of cell
149 std::vector<int> ret(3);
150 ret[0]=getTypeOfCell(0);
151 ret[1]=getNumberOfCells();
152 ret[2]=0; //ret[3*k+2]==0 because it has no sense here
157 * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
159 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
162 throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code is empty, should not !");
163 std::size_t sz=code.size();
165 throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code should be of size 3 exactly !");
167 int nbCells=getNumberOfCellsWithType((INTERP_KERNEL::NormalizedCellType)code[0]);
173 throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : number of cells mismatch !");
178 throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code[2]<-1 mismatch !");
179 if(code[2]>=(int)idsPerType.size())
180 throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code[2]>size idsPerType !");
181 return idsPerType[code[2]]->deepCpy();
186 * See MEDCouplingUMesh::splitProfilePerType for more information
188 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
190 int nbCells=getNumberOfCells();
192 code[0]=(int)getTypeOfCell(0);
195 idsInPflPerType.push_back(profile->deepCpy());
196 idsPerType.push_back(profile->deepCpy());
199 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const throw(INTERP_KERNEL::Exception)
201 int meshDim=getMeshDimension();
202 MEDCouplingUMesh *ret=MEDCouplingUMesh::New(getName(),meshDim);
203 DataArrayDouble *coords=getCoordinatesAndOwner();
204 ret->setCoords(coords);
209 fill1DUnstructuredMesh(ret);
212 fill2DUnstructuredMesh(ret);
215 fill3DUnstructuredMesh(ret);
218 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::buildUnstructured : big problem spacedim must be in 1,2 or 3 !");
223 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
225 MEDCouplingUMesh *um=buildUnstructured();
226 MEDCouplingMesh *ret=um->buildPart(start,end);
231 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
233 MEDCouplingUMesh *um=buildUnstructured();
234 MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
239 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy) throw(INTERP_KERNEL::Exception)
241 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
244 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
246 if(getMeshDimension()!=2)
247 throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
248 MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
249 DataArrayDouble *array=DataArrayDouble::New();
250 int nbOfCells=getNumberOfCells();
251 array->alloc(nbOfCells,3);
252 double *vals=array->getPointer();
253 for(int i=0;i<nbOfCells;i++)
254 { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
255 ret->setArray(array);
261 void MEDCouplingStructuredMesh::fill1DUnstructuredMesh(MEDCouplingUMesh *m) const
264 getNodeGridStructure(&nbOfCells);
266 DataArrayInt *connI=DataArrayInt::New();
267 connI->alloc(nbOfCells+1,1);
268 int *ci=connI->getPointer();
269 DataArrayInt *conn=DataArrayInt::New();
270 conn->alloc(3*nbOfCells,1);
272 int *cp=conn->getPointer();
273 for(int i=0;i<nbOfCells;i++)
275 cp[3*i]=(int)INTERP_KERNEL::NORM_SEG2;
280 m->setConnectivity(conn,connI,true);
285 void MEDCouplingStructuredMesh::fill2DUnstructuredMesh(MEDCouplingUMesh *m) const
288 getNodeGridStructure(ns);
291 DataArrayInt *connI=DataArrayInt::New();
292 connI->alloc(n1*n2+1,1);
293 int *ci=connI->getPointer();
294 DataArrayInt *conn=DataArrayInt::New();
295 conn->alloc(5*n1*n2,1);
297 int *cp=conn->getPointer();
299 for(int j=0;j<n2;j++)
300 for(int i=0;i<n1;i++,pos++)
302 cp[5*pos]=(int)INTERP_KERNEL::NORM_QUAD4;
303 cp[5*pos+1]=i+1+j*(n1+1);
304 cp[5*pos+2]=i+j*(n1+1);
305 cp[5*pos+3]=i+(j+1)*(n1+1);
306 cp[5*pos+4]=i+1+(j+1)*(n1+1);
309 m->setConnectivity(conn,connI,true);
314 void MEDCouplingStructuredMesh::fill3DUnstructuredMesh(MEDCouplingUMesh *m) const
317 getNodeGridStructure(ns);
321 DataArrayInt *connI=DataArrayInt::New();
322 connI->alloc(n1*n2*n3+1,1);
323 int *ci=connI->getPointer();
324 DataArrayInt *conn=DataArrayInt::New();
325 conn->alloc(9*n1*n2*n3,1);
327 int *cp=conn->getPointer();
329 for(int k=0;k<n3;k++)
330 for(int j=0;j<n2;j++)
331 for(int i=0;i<n1;i++,pos++)
333 cp[9*pos]=(int)INTERP_KERNEL::NORM_HEXA8;
334 int tmp=(n1+1)*(n2+1);
335 cp[9*pos+1]=i+1+j*(n1+1)+k*tmp;
336 cp[9*pos+2]=i+j*(n1+1)+k*tmp;
337 cp[9*pos+3]=i+(j+1)*(n1+1)+k*tmp;
338 cp[9*pos+4]=i+1+(j+1)*(n1+1)+k*tmp;
339 cp[9*pos+5]=i+1+j*(n1+1)+(k+1)*tmp;
340 cp[9*pos+6]=i+j*(n1+1)+(k+1)*tmp;
341 cp[9*pos+7]=i+(j+1)*(n1+1)+(k+1)*tmp;
342 cp[9*pos+8]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
345 m->setConnectivity(conn,connI,true);
350 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
354 int meshDim=getMeshDimension();
355 getSplitCellValues(tmp2);
356 std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
357 return std::accumulate(tmp,tmp+meshDim,0);
360 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
364 int meshDim=getMeshDimension();
365 getSplitNodeValues(tmp2);
366 std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
367 return std::accumulate(tmp,tmp+meshDim,0);
370 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
373 for(int i=meshDim-1;i>=0;i--)
375 int pos=work/split[i];