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 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());
196 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const throw(INTERP_KERNEL::Exception)
198 int meshDim=getMeshDimension();
199 MEDCouplingUMesh *ret=MEDCouplingUMesh::New(getName(),meshDim);
200 DataArrayDouble *coords=getCoordinatesAndOwner();
201 ret->setCoords(coords);
206 fill1DUnstructuredMesh(ret);
209 fill2DUnstructuredMesh(ret);
212 fill3DUnstructuredMesh(ret);
215 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::buildUnstructured : big problem spacedim must be in 1,2 or 3 !");
220 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
222 MEDCouplingUMesh *um=buildUnstructured();
223 MEDCouplingMesh *ret=um->buildPart(start,end);
228 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
230 MEDCouplingUMesh *um=buildUnstructured();
231 MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
236 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy) throw(INTERP_KERNEL::Exception)
238 throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
241 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
243 if(getMeshDimension()!=2)
244 throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
245 MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
246 DataArrayDouble *array=DataArrayDouble::New();
247 int nbOfCells=getNumberOfCells();
248 array->alloc(nbOfCells,3);
249 double *vals=array->getPointer();
250 for(int i=0;i<nbOfCells;i++)
251 { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
252 ret->setArray(array);
258 void MEDCouplingStructuredMesh::fill1DUnstructuredMesh(MEDCouplingUMesh *m) const
261 getNodeGridStructure(&nbOfCells);
263 DataArrayInt *connI=DataArrayInt::New();
264 connI->alloc(nbOfCells+1,1);
265 int *ci=connI->getPointer();
266 DataArrayInt *conn=DataArrayInt::New();
267 conn->alloc(3*nbOfCells,1);
269 int *cp=conn->getPointer();
270 for(int i=0;i<nbOfCells;i++)
272 cp[3*i]=(int)INTERP_KERNEL::NORM_SEG2;
277 m->setConnectivity(conn,connI,true);
282 void MEDCouplingStructuredMesh::fill2DUnstructuredMesh(MEDCouplingUMesh *m) const
285 getNodeGridStructure(ns);
288 DataArrayInt *connI=DataArrayInt::New();
289 connI->alloc(n1*n2+1,1);
290 int *ci=connI->getPointer();
291 DataArrayInt *conn=DataArrayInt::New();
292 conn->alloc(5*n1*n2,1);
294 int *cp=conn->getPointer();
296 for(int j=0;j<n2;j++)
297 for(int i=0;i<n1;i++,pos++)
299 cp[5*pos]=(int)INTERP_KERNEL::NORM_QUAD4;
300 cp[5*pos+1]=i+1+j*(n1+1);
301 cp[5*pos+2]=i+j*(n1+1);
302 cp[5*pos+3]=i+(j+1)*(n1+1);
303 cp[5*pos+4]=i+1+(j+1)*(n1+1);
306 m->setConnectivity(conn,connI,true);
311 void MEDCouplingStructuredMesh::fill3DUnstructuredMesh(MEDCouplingUMesh *m) const
314 getNodeGridStructure(ns);
318 DataArrayInt *connI=DataArrayInt::New();
319 connI->alloc(n1*n2*n3+1,1);
320 int *ci=connI->getPointer();
321 DataArrayInt *conn=DataArrayInt::New();
322 conn->alloc(9*n1*n2*n3,1);
324 int *cp=conn->getPointer();
326 for(int k=0;k<n3;k++)
327 for(int j=0;j<n2;j++)
328 for(int i=0;i<n1;i++,pos++)
330 cp[9*pos]=(int)INTERP_KERNEL::NORM_HEXA8;
331 int tmp=(n1+1)*(n2+1);
332 cp[9*pos+1]=i+1+j*(n1+1)+k*tmp;
333 cp[9*pos+2]=i+j*(n1+1)+k*tmp;
334 cp[9*pos+3]=i+(j+1)*(n1+1)+k*tmp;
335 cp[9*pos+4]=i+1+(j+1)*(n1+1)+k*tmp;
336 cp[9*pos+5]=i+1+j*(n1+1)+(k+1)*tmp;
337 cp[9*pos+6]=i+j*(n1+1)+(k+1)*tmp;
338 cp[9*pos+7]=i+(j+1)*(n1+1)+(k+1)*tmp;
339 cp[9*pos+8]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
342 m->setConnectivity(conn,connI,true);
347 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
351 int meshDim=getMeshDimension();
352 getSplitCellValues(tmp2);
353 std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
354 return std::accumulate(tmp,tmp+meshDim,0);
357 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
361 int meshDim=getMeshDimension();
362 getSplitNodeValues(tmp2);
363 std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
364 return std::accumulate(tmp,tmp+meshDim,0);
367 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
370 for(int i=meshDim-1;i>=0;i--)
372 int pos=work/split[i];