From 3bf53b32e90b05848102ebbee3853ed63b66bfba Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Fri, 17 Feb 2017 09:54:42 +0100 Subject: [PATCH] Useful MEDCouplingUMesh.Build1DMeshFromCoords method --- src/MEDCoupling/MEDCouplingUMesh.cxx | 37 ++++++++++++++++--- src/MEDCoupling/MEDCouplingUMesh.hxx | 1 + .../MEDCouplingBasicsTest5.py | 22 +++++++++++ src/MEDCoupling_Swig/MEDCouplingCommon.i | 7 ++++ 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingUMesh.cxx b/src/MEDCoupling/MEDCouplingUMesh.cxx index 39bf67ac5..cbff1a12d 100644 --- a/src/MEDCoupling/MEDCouplingUMesh.cxx +++ b/src/MEDCoupling/MEDCouplingUMesh.cxx @@ -19,6 +19,7 @@ // Author : Anthony Geay (CEA/DEN) #include "MEDCouplingUMesh.hxx" +#include "MEDCouplingCMesh.hxx" #include "MEDCoupling1GTUMesh.hxx" #include "MEDCouplingFieldDouble.hxx" #include "MEDCouplingSkyLineArray.hxx" @@ -8037,15 +8038,16 @@ MEDCouplingUMesh *MEDCouplingUMesh::Build0DMeshFromCoords(DataArrayDouble *da) if(!da) throw INTERP_KERNEL::Exception("MEDCouplingUMesh::Build0DMeshFromCoords : instance of DataArrayDouble must be not null !"); da->checkAllocated(); - MCAuto ret=MEDCouplingUMesh::New(da->getName(),0); + std::string name(da->getName()); + MCAuto ret(MEDCouplingUMesh::New(name,0)); + if(name.empty()) + ret->setName("Mesh"); ret->setCoords(da); - int nbOfTuples=da->getNumberOfTuples(); - MCAuto c=DataArrayInt::New(); - MCAuto cI=DataArrayInt::New(); + int nbOfTuples(da->getNumberOfTuples()); + MCAuto c(DataArrayInt::New()),cI(DataArrayInt::New()); c->alloc(2*nbOfTuples,1); cI->alloc(nbOfTuples+1,1); - int *cp=c->getPointer(); - int *cip=cI->getPointer(); + int *cp(c->getPointer()),*cip(cI->getPointer()); *cip++=0; for(int i=0;isetConnectivity(c,cI,true); return ret.retn(); } + +MCAuto MEDCouplingUMesh::Build1DMeshFromCoords(DataArrayDouble *da) +{ + if(!da) + throw INTERP_KERNEL::Exception("MEDCouplingUMesh::Build01MeshFromCoords : instance of DataArrayDouble must be not null !"); + da->checkAllocated(); + std::string name(da->getName()); + MCAuto ret; + { + MCAuto tmp(MEDCouplingCMesh::New()); + MCAuto arr(DataArrayDouble::New()); + arr->alloc(da->getNumberOfTuples()); + tmp->setCoordsAt(0,arr); + ret=tmp->buildUnstructured(); + } + ret->setCoords(da); + if(name.empty()) + ret->setName("Mesh"); + else + ret->setName(name); + return ret; +} + /*! * Creates a new MEDCouplingUMesh by concatenating two given meshes of the same dimension. * Cells and nodes of diff --git a/src/MEDCoupling/MEDCouplingUMesh.hxx b/src/MEDCoupling/MEDCouplingUMesh.hxx index 2241c267e..ab2f47650 100644 --- a/src/MEDCoupling/MEDCouplingUMesh.hxx +++ b/src/MEDCoupling/MEDCouplingUMesh.hxx @@ -233,6 +233,7 @@ namespace MEDCoupling MEDCOUPLING_EXPORT DataArrayInt *colinearize2D(double eps); MEDCOUPLING_EXPORT int split2DCells(const DataArrayInt *desc, const DataArrayInt *descI, const DataArrayInt *subNodesInSeg, const DataArrayInt *subNodesInSegI, const DataArrayInt *midOpt=0, const DataArrayInt *midOptI=0); MEDCOUPLING_EXPORT static MEDCouplingUMesh *Build0DMeshFromCoords(DataArrayDouble *da); + MEDCOUPLING_EXPORT static MCAuto Build1DMeshFromCoords(DataArrayDouble *da); MEDCOUPLING_EXPORT static MEDCouplingUMesh *MergeUMeshes(const MEDCouplingUMesh *mesh1, const MEDCouplingUMesh *mesh2); MEDCOUPLING_EXPORT static MEDCouplingUMesh *MergeUMeshes(const std::vector& a); MEDCOUPLING_EXPORT static MEDCouplingUMesh *MergeUMeshesOnSameCoords(const MEDCouplingUMesh *mesh1, const MEDCouplingUMesh *mesh2); diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py index 6e9febf06..4bbfe6087 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py @@ -4852,6 +4852,28 @@ class MEDCouplingBasicsTest5(unittest.TestCase): self.assertEqual(f2.getArray().getHiddenCppPointer(),f3.getArray().getHiddenCppPointer()) self.assertEqual(f3.getTime(),[0.5,2,3]) pass + + def testBuild1DMeshFromCoords1(self): + da=DataArrayDouble([(3,4),(5,6),(7,8)]) + da.setName("ZeArr") + da0=da.deepCopy() + m=MEDCouplingUMesh.Build1DMeshFromCoords(da0) + m.checkConsistencyLight() + self.assertEqual(da0.getHiddenCppPointer(),m.getCoords().getHiddenCppPointer()) + self.assertTrue(da.isEqual(da0,1e-12)) + self.assertEqual(m.getName(),da.getName()) + self.assertEqual(m.getMeshDimension(),1) + self.assertTrue(isinstance(m,MEDCouplingUMesh)) + m1=MEDCoupling1SGTUMesh(m) + m1.checkConsistencyLight() + self.assertTrue(m1.getNodalConnectivity().isEqual(DataArrayInt([0,1,1,2]))) + # + da0.setName("") + m2=MEDCouplingUMesh.Build1DMeshFromCoords(da0) + m2.checkConsistencyLight() + self.assertEqual(da0.getHiddenCppPointer(),m2.getCoords().getHiddenCppPointer()) + self.assertEqual(m2.getName(),"Mesh") + pass pass diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index 99bfc86c7..a8036fbba 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -315,6 +315,7 @@ using namespace INTERP_KERNEL; %newobject MEDCoupling::MEDCouplingUMesh::buildPartOrthogonalField; %newobject MEDCoupling::MEDCouplingUMesh::keepCellIdsByType; %newobject MEDCoupling::MEDCouplingUMesh::Build0DMeshFromCoords; +%newobject MEDCoupling::MEDCouplingUMesh::Build1DMeshFromCoords; %newobject MEDCoupling::MEDCouplingUMesh::findAndCorrectBadOriented3DExtrudedCells; %newobject MEDCoupling::MEDCouplingUMesh::findAndCorrectBadOriented3DCells; %newobject MEDCoupling::MEDCouplingUMesh::convertIntoSingleGeoTypeMesh; @@ -1896,6 +1897,12 @@ namespace MEDCoupling return self->cellIterator(); } + static MEDCouplingUMesh *Build1DMeshFromCoords(DataArrayDouble *da) throw(INTERP_KERNEL::Exception) + { + MCAuto ret(MEDCouplingUMesh::Build1DMeshFromCoords(da)); + return ret.retn(); + } + PyObject *getAllGeoTypesSorted() const throw(INTERP_KERNEL::Exception) { std::vector result=self->getAllGeoTypesSorted(); -- 2.39.2