From e37410e54208e231c971b2858ed9015b150296a9 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Wed, 28 Aug 2024 14:50:31 +0200 Subject: [PATCH] [EDF30834] : Work b4 integration. Redefinition of dependancies. RAII usage. Templating. Full access of API thru python wrapping. salome test integration of recogn shape tests. --- CMakeLists.txt | 10 +- src/CMakeLists.txt | 8 +- src/CTestTestfileInstall.cmake.in | 5 + src/MEDCoupling/MCAuto.hxx | 1 + src/PyWrapping/CMakeLists.txt | 15 +- .../TestPyWrapGathered_medcoupling.py | 12 +- src/PyWrapping/medcoupling.i | 23 +- src/ShapeRecogn/Areas.cxx | 29 +- src/ShapeRecogn/Areas.hxx | 13 +- src/ShapeRecogn/AreasBuilder.cxx | 19 + src/ShapeRecogn/AreasBuilder.hxx | 9 +- src/ShapeRecogn/CMakeLists.txt | 7 +- src/ShapeRecogn/MathOps.cxx | 37 +- src/ShapeRecogn/MathOps.hxx | 9 +- src/ShapeRecogn/Nodes.cxx | 26 +- src/ShapeRecogn/Nodes.hxx | 17 +- src/ShapeRecogn/NodesBuilder.cxx | 31 +- src/ShapeRecogn/NodesBuilder.hxx | 9 +- src/ShapeRecogn/PrimitiveType.cxx | 116 +++++++ src/ShapeRecogn/PrimitiveType.hxx | 90 ++--- src/ShapeRecogn/README.md | 2 +- src/ShapeRecogn/ShapeRecognMesh.cxx | 103 ++---- src/ShapeRecogn/ShapeRecognMesh.hxx | 71 ++-- src/ShapeRecogn/ShapeRecognMeshBuilder.cxx | 324 +++++++++++------- src/ShapeRecogn/ShapeRecognMeshBuilder.hxx | 72 ++-- src/ShapeRecogn/ShapeRecongConstants.hxx | 9 +- src/ShapeRecogn/Swig/CMakeLists.txt | 3 +- src/ShapeRecogn/Swig/ShapeRecogn.i | 222 +++++++++++- src/ShapeRecogn/Swig/ShapeRecognCommon.i | 61 ++++ src/ShapeRecogn/Swig/ShapeRecognImpl.i | 246 +++++++++++++ src/ShapeRecogn/Test/CMakeLists.txt | 46 ++- .../Test/CTestTestfileInstall.cmake | 21 +- src/ShapeRecogn/Test/ConeTest.cxx | 26 +- src/ShapeRecogn/Test/ConeTest.hxx | 15 +- src/ShapeRecogn/Test/CylinderTest.cxx | 26 +- src/ShapeRecogn/Test/CylinderTest.hxx | 15 +- src/ShapeRecogn/Test/MathOpsTest.cxx | 19 + src/ShapeRecogn/Test/MathOpsTest.hxx | 9 +- src/ShapeRecogn/Test/PlaneTest.cxx | 26 +- src/ShapeRecogn/Test/PlaneTest.hxx | 16 +- src/ShapeRecogn/Test/ShapeRecognTest.cxx | 28 ++ src/ShapeRecogn/Test/ShapeRecognTest.hxx | 27 ++ src/ShapeRecogn/Test/SphereTest.cxx | 26 +- src/ShapeRecogn/Test/SphereTest.hxx | 15 +- src/ShapeRecogn/Test/TestShapeRecogn.cxx | 2 +- src/ShapeRecogn/Test/TestShapeRecogn.py | 80 +++++ src/ShapeRecogn/Test/TorusTest.cxx | 26 +- src/ShapeRecogn/Test/TorusTest.hxx | 15 +- 48 files changed, 1512 insertions(+), 525 deletions(-) create mode 100644 src/ShapeRecogn/PrimitiveType.cxx create mode 100644 src/ShapeRecogn/Swig/ShapeRecognCommon.i create mode 100644 src/ShapeRecogn/Swig/ShapeRecognImpl.i create mode 100644 src/ShapeRecogn/Test/ShapeRecognTest.cxx create mode 100644 src/ShapeRecogn/Test/ShapeRecognTest.hxx create mode 100644 src/ShapeRecogn/Test/TestShapeRecogn.py diff --git a/CMakeLists.txt b/CMakeLists.txt index c080f174a..fff7065d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,7 +88,7 @@ OPTION(MEDCOUPLING_MICROMED "Build MED without MED file dependency." OFF) OPTION(MEDCOUPLING_ENABLE_PYTHON "Build PYTHON bindings." ON) OPTION(MEDCOUPLING_ENABLE_PARTITIONER "Build MEDPartitioner." ON) OPTION(MEDCOUPLING_ENABLE_RENUMBER "Build Renumber." ON) -OPTION(MEDCOUPLING_ENABLE_SHAPERECOGN "Build ShapeRecogn" OFF) +OPTION(MEDCOUPLING_ENABLE_SHAPERECOGN "Build Shape Recognition module." OFF) OPTION(MEDCOUPLING_WITH_FILE_EXAMPLES "Install examples of files containing meshes and fields of different formats." ON) OPTION(MEDCOUPLING_USE_MPI "(Use MPI containers) - For MED this triggers the build of ParaMEDMEM." OFF) OPTION(MEDCOUPLING_BUILD_TESTS "Build MEDCoupling C++ tests." ON) @@ -121,17 +121,17 @@ CMAKE_DEPENDENT_OPTION(MEDCOUPLING_BUILD_FRENCH_DOC "Generate MEDCOUPLING French IF(MEDCOUPLING_BUILD_STATIC) SET(BUILD_SHARED_LIBS 0) SET(CMAKE_POSITION_INDEPENDENT_CODE 1) # -fPIC option -ELSE() +ELSE(MEDCOUPLING_BUILD_STATIC) SET(BUILD_SHARED_LIBS 1) -ENDIF() +ENDIF(MEDCOUPLING_BUILD_STATIC) IF(${MEDCOUPLING_PARTITIONER_PARMETIS} AND ${MEDCOUPLING_PARTITIONER_METIS}) MESSAGE(FATAL_ERROR "ParMetis and Metis are mutually exclusive! Make a choice (options MEDCOUPLING_PARTITIONER_METIS and MEDCOUPLING_PARTITIONER_PARMETIS).") -ENDIF() +ENDIF(${MEDCOUPLING_PARTITIONER_PARMETIS} AND ${MEDCOUPLING_PARTITIONER_METIS}) IF(${MEDCOUPLING_PARTITIONER_PTSCOTCH} AND ${MEDCOUPLING_PARTITIONER_SCOTCH}) MESSAGE(FATAL_ERROR "PTScotch and Scotch are mutually exclusive! Make a choice (options MEDCOUPLING_PARTITIONER_SCOTCH and MEDCOUPLING_PARTITIONER_PTSCOTCH).") -ENDIF() +ENDIF(${MEDCOUPLING_PARTITIONER_PTSCOTCH} AND ${MEDCOUPLING_PARTITIONER_SCOTCH}) # # Set list of prerequisites diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e4d70409c..5edb2b3b2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,6 +31,11 @@ ENDIF(MEDCOUPLING_BUILD_TESTS) ADD_SUBDIRECTORY(MEDCoupling) ADD_SUBDIRECTORY(ICoCo) +# Shape recognition +IF(MEDCOUPLING_ENABLE_SHAPERECOGN) + ADD_SUBDIRECTORY(ShapeRecogn) +ENDIF(MEDCOUPLING_ENABLE_SHAPERECOGN) + IF(MEDCOUPLING_ENABLE_PYTHON) ADD_SUBDIRECTORY(MEDCoupling_Swig) @@ -82,9 +87,6 @@ IF(MEDCOUPLING_USE_MPI) ENDIF(MEDCOUPLING_BUILD_TESTS) ENDIF(MEDCOUPLING_USE_MPI) -IF(MEDCOUPLING_ENABLE_SHAPERECOGN) - ADD_SUBDIRECTORY(ShapeRecogn) -ENDIF(MEDCOUPLING_ENABLE_SHAPERECOGN) # Application tests configure_file(CTestTestfileInstall.cmake.in "CTestTestfileST.cmake" @ONLY) diff --git a/src/CTestTestfileInstall.cmake.in b/src/CTestTestfileInstall.cmake.in index f71d5f0f2..b0cf5538c 100644 --- a/src/CTestTestfileInstall.cmake.in +++ b/src/CTestTestfileInstall.cmake.in @@ -21,6 +21,7 @@ SET(COMPONENT_NAME MEDCOUPLING) SET(TIMEOUT 120) SET(MEDCOUPLING_BUILD_PY_TESTS $) SET(MEDCOUPLING_USE_MPI_BOOL $) +SET(MEDCOUPLING_ENABLE_SHAPERECOGN $) SUBDIRS(INTERP_KERNELTest) SUBDIRS(MEDCoupling) @@ -35,6 +36,10 @@ if(MEDCOUPLING_USE_MPI_BOOL) SUBDIRS(ParaMEDMEM_Swig) endif() +if(MEDCOUPLING_ENABLE_SHAPERECOGN) + SUBDIRS(ShapeRecogn) +endif() + SUBDIRS(MEDPartitioner_Swig) SUBDIRS(RENUMBER_Swig) SUBDIRS(PyWrapping) diff --git a/src/MEDCoupling/MCAuto.hxx b/src/MEDCoupling/MCAuto.hxx index 4b6347704..0774928b0 100644 --- a/src/MEDCoupling/MCAuto.hxx +++ b/src/MEDCoupling/MCAuto.hxx @@ -139,6 +139,7 @@ namespace MEDCoupling bool operator==(const MCConstAuto& other) const { return _ptr==other._ptr; } bool operator==(const T *other) const { return _ptr==other; } MCConstAuto &operator=(const MCConstAuto& other) { if(_ptr!=other._ptr) { destroyPtr(); referPtr(other._ptr); } return *this; } + MCConstAuto &operator=(const typename MEDCoupling::MCAuto& other) { if(_ptr!=(const T*)other) { destroyPtr(); referPtr((const T*)other); } return *this; } MCConstAuto &operator=(const T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; } void takeRef(const T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; if(_ptr) _ptr->incrRef(); } } const T *operator->() { return _ptr ; } diff --git a/src/PyWrapping/CMakeLists.txt b/src/PyWrapping/CMakeLists.txt index 642ce08f5..e1b4e2f5a 100644 --- a/src/PyWrapping/CMakeLists.txt +++ b/src/PyWrapping/CMakeLists.txt @@ -63,16 +63,23 @@ INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/../ParaMEDMEM ${CMAKE_CURRENT_SOURCE_DIR}/../ParaMEDMEM/MPIAccess ${CMAKE_CURRENT_SOURCE_DIR}/../ParaMEDMEM_Swig + ${CMAKE_CURRENT_SOURCE_DIR}/../ShapeRecogn + ${CMAKE_CURRENT_SOURCE_DIR}/../ShapeRecogn/Swig ${PROJECT_BINARY_DIR}/doc ) - -IF(WIN32 OR CYGWIN) + + IF(WIN32 OR CYGWIN) SET(medcoupling_LIB_dependancies ${PYTHON_LIBRARIES} ${PLATFORM_LIBS} medcouplingremapper medicoco) -ELSE(WIN32) + ELSE(WIN32 OR CYGWIN) # ${PYTHON_LIBRARIES} not needed see https://www.python.org/dev/peps/pep-0513/#libpythonx-y-so-1 SET(medcoupling_LIB_dependancies ${PLATFORM_LIBS} medcouplingremapper medicoco) -ENDIF(WIN32 OR CYGWIN) + ENDIF(WIN32 OR CYGWIN) + IF(MEDCOUPLING_ENABLE_SHAPERECOGN) + LIST(APPEND SWIG_MODULE_medcoupling_EXTRA_FLAGS -DWITH_SHAPE_RECOGN) + LIST(APPEND medcoupling_LIB_dependancies shaperecogn) + ENDIF(MEDCOUPLING_ENABLE_SHAPERECOGN) + IF(NOT MEDCOUPLING_MICROMED) LIST(APPEND SWIG_MODULE_medcoupling_EXTRA_FLAGS -DWITH_MED_FILE) LIST(APPEND medcoupling_LIB_dependancies medloader) diff --git a/src/PyWrapping/TestPyWrapGathered_medcoupling.py b/src/PyWrapping/TestPyWrapGathered_medcoupling.py index 10ce28a1b..553fefc96 100644 --- a/src/PyWrapping/TestPyWrapGathered_medcoupling.py +++ b/src/PyWrapping/TestPyWrapGathered_medcoupling.py @@ -141,7 +141,17 @@ class medcouplingTest(unittest.TestCase): a,b,c=f3.getTime() self.assertEqual(b,10) ; self.assertEqual(c,13) ; self.assertAlmostEqual(a,10.75,14); pass - + + @unittest.skipUnless(HasShapeRecognitionExt(),"Requires Shape recognition extension activated") + def test6(self): + m = MEDCouplingCMesh() + arr = DataArrayDouble(5) ; arr.iota() + m.setCoords(arr,arr) + m = m.buildUnstructured() + m.simplexize(0) + m.changeSpaceDimension(3,0.) + srMesh = ShapeRecognMeshBuilder( m ) + rem = srMesh.recognize() def partitionerTesterHelper(self,algoSelected): arr=DataArrayDouble(10) ; arr.iota() diff --git a/src/PyWrapping/medcoupling.i b/src/PyWrapping/medcoupling.i index 4e9436cae..c8f20df05 100644 --- a/src/PyWrapping/medcoupling.i +++ b/src/PyWrapping/medcoupling.i @@ -48,6 +48,10 @@ %include "ParaMEDMEMCommon.i" #endif +#ifdef WITH_SHAPE_RECOGN +%include "ShapeRecognCommon.i" +#endif + %constant const char __version__[]=MEDCOUPLING_GIT_SHA1; %constant const char __config_datetime__[]=MEDCOUPLING_CONFIG_DT; @@ -57,9 +61,10 @@ static const char RENUM_EXT[]="Renumberer"; static const char PART_EXT[]="Partitioner"; static const char PAR_INTERPOL_EXT[]="Parallel interpolator (SPMD paradigm)"; - static const char IT_STATS_EXT[] = "Iterative statistics"; - - static const char *EXTENSIONS[]={SEQ_INTERPOL_EXT,MEDFILEIO_EXT,RENUM_EXT,PART_EXT,PAR_INTERPOL_EXT,IT_STATS_EXT}; + static const char IT_STATS_EXT[]="Iterative statistics"; + static const char SHAPE_RECOGNITION_EXT[]="Shape Recognition"; + + static const char *EXTENSIONS[]={SEQ_INTERPOL_EXT,MEDFILEIO_EXT,RENUM_EXT,PART_EXT,PAR_INTERPOL_EXT,IT_STATS_EXT,SHAPE_RECOGNITION_EXT}; static const int NB_OF_EXTENSIONS=sizeof(EXTENSIONS)/sizeof(const char *); %} @@ -133,6 +138,15 @@ return false; #endif } + + bool HasShapeRecognitionExt() + { +#ifdef WITH_SHAPE_RECOGN + return true; +#else + return false; +#endif + } std::vector ActiveExtensions() { @@ -152,6 +166,9 @@ #endif #ifdef WITH_ITERATIVE_STATISTICS ret.push_back(std::string(IT_STATS_EXT)); +#endif +#ifdef WITH_SHAPE_RECOGN + ret.push_back(std::string(SHAPE_RECOGNITION_EXT)); #endif return ret; } diff --git a/src/ShapeRecogn/Areas.cxx b/src/ShapeRecogn/Areas.cxx index f8441cb48..2cc82210f 100644 --- a/src/ShapeRecogn/Areas.cxx +++ b/src/ShapeRecogn/Areas.cxx @@ -1,3 +1,22 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "Areas.hxx" #include "MathOps.hxx" @@ -45,7 +64,7 @@ void Areas::removeArea(mcIdType areaId) void Areas::addNode(mcIdType areaId, mcIdType nodeId) { removeNode(nodeId); - areaIdByNodes[nodeId] = areaId; + areaIdByNodes[nodeId] = FromIdType(areaId); Area &area = areas[areaId]; area.nodeIds.push_back(nodeId); size_t nbNodes = area.nodeIds.size(); @@ -109,12 +128,12 @@ PrimitiveType Areas::getPrimitiveType(mcIdType areaId) const std::string Areas::getPrimitiveTypeName(mcIdType areaId) const { - return convertPrimitiveToString(getPrimitiveType(areaId)); + return ConvertPrimitiveToString(getPrimitiveType(areaId)); } int Areas::getPrimitiveTypeInt(mcIdType areaId) const { - return convertPrimitiveToInt(getPrimitiveType(areaId)); + return ConvertPrimitiveToInt(getPrimitiveType(areaId)); } const std::vector &Areas::getNodeIds(mcIdType areaId) const @@ -219,7 +238,7 @@ void Areas::cleanArea(mcIdType areaId, mcIdType newAreaId = -1) { Area &area = areas[areaId]; for (mcIdType nodeId : area.nodeIds) - areaIdByNodes[nodeId] = newAreaId; + areaIdByNodes[nodeId] = FromIdType( newAreaId ); area.primitive = PrimitiveType::Unknown; area.k1 = 0.0; area.k2 = 0.0; @@ -479,7 +498,7 @@ void Areas::computeTorusProperties(mcIdType areaId) area.center[i] = xc * base2d[i] + yc * base2d[3 + i] + meanMajorRadiusNodes[i]; } -const std::vector &Areas::getAreaIdByNodes() const +const std::vector &Areas::getAreaIdByNodes() const { return areaIdByNodes; } diff --git a/src/ShapeRecogn/Areas.hxx b/src/ShapeRecogn/Areas.hxx index b1b6f6bca..8e3158a56 100644 --- a/src/ShapeRecogn/Areas.hxx +++ b/src/ShapeRecogn/Areas.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,8 +17,7 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __AREAS_HXX__ -#define __AREAS_HXX__ +#pragma once #include "PrimitiveType.hxx" #include "Nodes.hxx" @@ -60,7 +59,7 @@ namespace MEDCoupling void cleanInvalidNodeAreas(); mcIdType getAreaId(mcIdType nodeId) const; - const std::vector &getAreaIdByNodes() const; + const std::vector &getAreaIdByNodes() const; bool isEmpty(mcIdType areaId) const; size_t getNumberOfAreas() const; @@ -101,8 +100,6 @@ namespace MEDCoupling std::vector areas; const Nodes *nodes; - std::vector areaIdByNodes; + std::vector areaIdByNodes; }; -}; - -#endif // __AREAS_HXX__ +} diff --git a/src/ShapeRecogn/AreasBuilder.cxx b/src/ShapeRecogn/AreasBuilder.cxx index 947a54ed0..a09174170 100644 --- a/src/ShapeRecogn/AreasBuilder.cxx +++ b/src/ShapeRecogn/AreasBuilder.cxx @@ -1,3 +1,22 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "AreasBuilder.hxx" #include "MathOps.hxx" #include "ShapeRecongConstants.hxx" diff --git a/src/ShapeRecogn/AreasBuilder.hxx b/src/ShapeRecogn/AreasBuilder.hxx index 9742af648..679a93570 100644 --- a/src/ShapeRecogn/AreasBuilder.hxx +++ b/src/ShapeRecogn/AreasBuilder.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,8 +17,7 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __AREASBUILDER_HXX__ -#define __AREASBUILDER_HXX__ +#pragma once #include "Nodes.hxx" #include "Areas.hxx" @@ -69,6 +68,4 @@ namespace MEDCoupling size_t threshold = 5; }; -}; - -#endif // __AREASBUILDER_HXX__ \ No newline at end of file +} diff --git a/src/ShapeRecogn/CMakeLists.txt b/src/ShapeRecogn/CMakeLists.txt index 1f63ae163..b312932e1 100644 --- a/src/ShapeRecogn/CMakeLists.txt +++ b/src/ShapeRecogn/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2024 CEA, EDF +# Copyright (C) 2024 CEA, EDF # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -35,7 +35,6 @@ INCLUDE_DIRECTORIES( ${MEDFILE_INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS} ${LAPACKE_INCLUDE_DIRS} - ${CMAKE_CURRENT_SOURCE_DIR}/../MEDLoader ${CMAKE_CURRENT_SOURCE_DIR}/../MEDCoupling ${CMAKE_CURRENT_SOURCE_DIR}/../INTERP_KERNEL ${CMAKE_CURRENT_SOURCE_DIR}/../INTERP_KERNEL/Bases @@ -49,15 +48,15 @@ SET(shaperecogn_SOURCES AreasBuilder.cxx ShapeRecognMeshBuilder.cxx ShapeRecognMesh.cxx + PrimitiveType.cxx ) ADD_LIBRARY(shaperecogn ${shaperecogn_SOURCES}) SET_TARGET_PROPERTIES(shaperecogn PROPERTIES COMPILE_FLAGS "") -TARGET_LINK_LIBRARIES(shaperecogn medcouplingcpp medloader ${MEDFILE_C_LIBRARIES} ${HDF5_LIBRARIES} ${LAPACK_LIBRARIES}) +TARGET_LINK_LIBRARIES(shaperecogn medcouplingcpp ${LAPACK_LIBRARIES}) INSTALL(TARGETS shaperecogn EXPORT ${PROJECT_NAME}TargetGroup DESTINATION ${MEDCOUPLING_INSTALL_LIBS}) FILE(GLOB shaperecogn_HEADERS_HXX "${CMAKE_CURRENT_SOURCE_DIR}/*.hxx") -INSTALL(FILES ${shaperecogn_HEADERS_HXX} DESTINATION ${MEDCOUPLING_INSTALL_HEADERS}) # To allow usage as SWIG dependencies: SET(shaperecogn_HEADERS_HXX PARENT_SCOPE) diff --git a/src/ShapeRecogn/MathOps.cxx b/src/ShapeRecogn/MathOps.cxx index 8f1705ace..099d7da1e 100644 --- a/src/ShapeRecogn/MathOps.cxx +++ b/src/ShapeRecogn/MathOps.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -18,12 +18,15 @@ // #include "MathOps.hxx" +#include "MCIdType.hxx" #include #include #include #include #include +#include +#include using namespace MEDCoupling; @@ -64,11 +67,11 @@ std::vector MathOps::lstsqRow( std::vector &a, const std::vector &b) { - int m = b.size(); - int n = 3; - int nrhs = 1; - int ldb = std::max(m, n); - int lds = std::min(m, n); + auto m = b.size(); + std::size_t n = 3; + std::size_t nrhs = 1; + std::size_t ldb = std::max(m, n); + std::size_t lds = std::min(m, n); std::vector x(ldb, 0.0); for (size_t i = 0; i < b.size(); ++i) x[i] = b[i]; @@ -77,9 +80,9 @@ std::vector MathOps::lstsqRow( int rank = 0; int info = LAPACKE_dgelsd( LAPACK_ROW_MAJOR, - m, n, nrhs, - a.data(), n, - x.data(), nrhs, + FromIdType(m), FromIdType(n), FromIdType(nrhs), + a.data(), FromIdType(n), + x.data(), FromIdType(nrhs), s.data(), rcond, &rank); @@ -152,7 +155,7 @@ double MathOps::mean(const std::vector &values) double mean = 0.0; for (double value : values) mean += value; - return mean / values.size(); + return mean / double( values.size() ); } std::array MathOps::meanCoordinates(const std::vector &coordinates) @@ -165,9 +168,9 @@ std::array MathOps::meanCoordinates(const std::vector &coordi coordsMean[1] += coordinates[3 * nodeId + 1]; coordsMean[2] += coordinates[3 * nodeId + 2]; } - coordsMean[0] /= nbNodes; - coordsMean[1] /= nbNodes; - coordsMean[2] /= nbNodes; + coordsMean[0] /= double( nbNodes ); + coordsMean[1] /= double( nbNodes ); + coordsMean[2] /= double( nbNodes ); return coordsMean; } @@ -234,13 +237,13 @@ double MathOps::computeQuantile(const std::vector &values, double q) { std::vector sortedValues(values); std::sort(sortedValues.begin(), sortedValues.end()); - double pos = q * (sortedValues.size() - 1); + double pos = q * double(sortedValues.size() - 1); size_t index = static_cast(pos); - if (pos == index) + if ( std::abs( pos - double(index) ) < 1e-12 ) return sortedValues[index]; else { - double frac = pos - index; + double frac = pos - double(index); return sortedValues[index] * (1 - frac) + sortedValues[index + 1] * frac; } } @@ -265,7 +268,7 @@ std::vector MathOps::computeAngles( cblas_dgemv( CBLAS_LAYOUT::CblasRowMajor, CBLAS_TRANSPOSE::CblasNoTrans, - nbDirections, 3, 1.0, + int(nbDirections), 3, 1.0, directions.data(), 3, axis.data(), 1, 0.0, angles.data(), 1); diff --git a/src/ShapeRecogn/MathOps.hxx b/src/ShapeRecogn/MathOps.hxx index e511845da..a0ba03e3d 100644 --- a/src/ShapeRecogn/MathOps.hxx +++ b/src/ShapeRecogn/MathOps.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,8 +17,7 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __MATHOPS_HXX__ -#define __MATHOPS_HXX__ +#pragma once #include #include @@ -66,6 +65,4 @@ namespace MEDCoupling static double computeVariance(std::vector values); static std::array computeBaseFromNormal(std::array normal); }; -}; - -#endif //__MATHOPS_HXX__ +} diff --git a/src/ShapeRecogn/Nodes.cxx b/src/ShapeRecogn/Nodes.cxx index 615971ad4..885c90868 100644 --- a/src/ShapeRecogn/Nodes.cxx +++ b/src/ShapeRecogn/Nodes.cxx @@ -1,3 +1,22 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "Nodes.hxx" using namespace MEDCoupling; @@ -10,13 +29,6 @@ Nodes::Nodes( { } -Nodes::~Nodes() -{ - mesh->decrRef(); - neighbors->decrRef(); - neighborsIdx->decrRef(); -} - mcIdType Nodes::getNbNodes() const { return coords->getNumberOfTuples(); diff --git a/src/ShapeRecogn/Nodes.hxx b/src/ShapeRecogn/Nodes.hxx index 1d47d3060..fbd445736 100644 --- a/src/ShapeRecogn/Nodes.hxx +++ b/src/ShapeRecogn/Nodes.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,8 +17,7 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __NODES_HXX__ -#define __NODES_HXX__ +#pragma once #include #include "MEDCouplingUMesh.hxx" @@ -33,7 +32,7 @@ namespace MEDCoupling Nodes(const MEDCouplingUMesh *mesh, const DataArrayInt64 *neighbors, const DataArrayInt64 *neighborsIdx); - ~Nodes(); + ~Nodes() = default; mcIdType getNbNodes() const; const std::vector &getK1() const; @@ -57,13 +56,13 @@ namespace MEDCoupling std::array getCoordinates(mcIdType nodeId) const; private: - const MEDCouplingUMesh *mesh; + MCConstAuto mesh; const DataArrayDouble *coords; // normals 3 * nbNodes std::vector normals; // neighbors - const DataArrayInt64 *neighbors; - const DataArrayInt64 *neighborsIdx; + MCConstAuto neighbors; + MCConstAuto neighborsIdx; // curvature std::vector k1; std::vector k2; @@ -73,6 +72,4 @@ namespace MEDCoupling std::vector mainDirections; std::vector primitives; }; -}; - -#endif //__NODES_HXX__ +} diff --git a/src/ShapeRecogn/NodesBuilder.cxx b/src/ShapeRecogn/NodesBuilder.cxx index 1302d3203..ced79eee0 100644 --- a/src/ShapeRecogn/NodesBuilder.cxx +++ b/src/ShapeRecogn/NodesBuilder.cxx @@ -1,3 +1,22 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "NodesBuilder.hxx" #include "Nodes.hxx" @@ -41,15 +60,15 @@ void NodesBuilder::computeNormals() } // nodes->normals.resize(3 * nbNodes, 1.0); - DataArrayInt64 *revNodal = DataArrayInt64::New(); - DataArrayInt64 *revNodalIdx = DataArrayInt64::New(); + MCAuto revNodal = DataArrayInt64::New(); + MCAuto revNodalIdx = DataArrayInt64::New(); mesh->getReverseNodalConnectivity(revNodal, revNodalIdx); for (size_t nodeId = 0; nodeId < (size_t)nbNodes; nodeId++) { - int nbCells = revNodalIdx->getIJ(nodeId + 1, 0) - - revNodalIdx->getIJ(nodeId, 0); + mcIdType nbCells = revNodalIdx->getIJ(nodeId + 1, 0) - + revNodalIdx->getIJ(nodeId, 0); std::vector cellIds(nbCells, 0); - int start = revNodalIdx->getIJ(nodeId, 0); + mcIdType start = revNodalIdx->getIJ(nodeId, 0); for (size_t i = 0; i < cellIds.size(); ++i) cellIds[i] = revNodal->getIJ(start + i, 0); double normal = 0.0; @@ -67,8 +86,6 @@ void NodesBuilder::computeNormals() for (size_t i = 0; i < 3; i++) nodes->normals[3 * nodeId + i] /= sqrt(normal); } - revNodal->decrRef(); - revNodalIdx->decrRef(); } void NodesBuilder::computeCurvatures(double tol) diff --git a/src/ShapeRecogn/NodesBuilder.hxx b/src/ShapeRecogn/NodesBuilder.hxx index 190118064..a58e06538 100644 --- a/src/ShapeRecogn/NodesBuilder.hxx +++ b/src/ShapeRecogn/NodesBuilder.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,8 +17,7 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __NODECURVATURECALCULATOR_HXX__ -#define __NODECURVATURECALCULATOR_HXX__ +#pragma once #include #include @@ -57,6 +56,4 @@ namespace MEDCoupling const MEDCouplingUMesh *mesh; Nodes *nodes; }; -}; - -#endif //__NODECURVATURECALCULATOR_HXX__ \ No newline at end of file +} diff --git a/src/ShapeRecogn/PrimitiveType.cxx b/src/ShapeRecogn/PrimitiveType.cxx new file mode 100644 index 000000000..d380e996b --- /dev/null +++ b/src/ShapeRecogn/PrimitiveType.cxx @@ -0,0 +1,116 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "PrimitiveType.hxx" + +#include +#include + +constexpr char PLANE_STR[] = "Plane"; +constexpr char SPHERE_STR[] = "Sphere"; +constexpr char CYLINDER_STR[] = "Cylinder"; +constexpr char CONE_STR[] = "Cone"; +constexpr char TORUS_STR[] = "Torus"; +constexpr char UNKNOWN_STR[] = "Unknown"; + +std::vector MEDCoupling::AllManagedPrimitives() +{ + return {PrimitiveType::Plane,PrimitiveType::Sphere,PrimitiveType::Cylinder,PrimitiveType::Cone,PrimitiveType::Torus,PrimitiveType::Unknown}; +} + +std::vector MEDCoupling::AllManagedPrimitivesStr() +{ + std::vector apt( AllManagedPrimitives() ); + std::vector ret; + std::transform(apt.begin(),apt.end(),std::back_inserter(ret),[](PrimitiveType type) { return MEDCoupling::ConvertPrimitiveToString(type); } ); + return ret; +} + +std::string MEDCoupling::ConvertPrimitiveToString(MEDCoupling::PrimitiveType type) +{ + std::string typeName; + switch (type) + { + case PrimitiveType::Plane: + typeName = PLANE_STR; + break; + case PrimitiveType::Sphere: + typeName = SPHERE_STR; + break; + case PrimitiveType::Cylinder: + typeName = CYLINDER_STR; + break; + case PrimitiveType::Cone: + typeName = CONE_STR; + break; + case PrimitiveType::Torus: + typeName = TORUS_STR; + break; + case PrimitiveType::Unknown: + typeName = UNKNOWN_STR; + break; + default: + break; + } + return typeName; +}; + +MEDCoupling::PrimitiveType MEDCoupling::ConvertStringToPrimitive(const std::string& type) +{ + if( type == PLANE_STR ) + return PrimitiveType::Plane; + if( type == SPHERE_STR ) + return PrimitiveType::Sphere; + if( type == CYLINDER_STR ) + return PrimitiveType::Cylinder; + if( type == CONE_STR ) + return PrimitiveType::Cone; + if( type == TORUS_STR ) + return PrimitiveType::Torus; + if( type == UNKNOWN_STR ) + return PrimitiveType::Unknown; + return PrimitiveType::Unknown; +} + +int MEDCoupling::ConvertPrimitiveToInt(MEDCoupling::PrimitiveType type) +{ + int typeInt = 5; + switch (type) + { + case PrimitiveType::Plane: + typeInt = 0; + break; + case PrimitiveType::Sphere: + typeInt = 1; + break; + case PrimitiveType::Cylinder: + typeInt = 2; + break; + case PrimitiveType::Cone: + typeInt = 3; + break; + case PrimitiveType::Torus: + typeInt = 4; + break; + case PrimitiveType::Unknown: + default: + break; + } + return typeInt; +} diff --git a/src/ShapeRecogn/PrimitiveType.hxx b/src/ShapeRecogn/PrimitiveType.hxx index b131da84a..88f428932 100644 --- a/src/ShapeRecogn/PrimitiveType.hxx +++ b/src/ShapeRecogn/PrimitiveType.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,77 +17,31 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __PRIMITIVETYPE_HXX__ -#define __PRIMITIVETYPE_HXX__ +#pragma once +#include #include +#include + namespace MEDCoupling { - enum PrimitiveType - { - Plane = 0, - Sphere = 1, - Cylinder = 2, - Cone = 3, - Torus = 4, - Unknown = 5 - }; + enum class PrimitiveType : std::uint8_t + { + Plane = 0, + Sphere = 1, + Cylinder = 2, + Cone = 3, + Torus = 4, + Unknown = 5 + }; - inline std::string convertPrimitiveToString(PrimitiveType type) - { - std::string typeName = ""; - switch (type) - { - case PrimitiveType::Plane: - typeName = "Plane"; - break; - case PrimitiveType::Sphere: - typeName = "Sphere"; - break; - case PrimitiveType::Cylinder: - typeName = "Cylinder"; - break; - case PrimitiveType::Cone: - typeName = "Cone"; - break; - case PrimitiveType::Torus: - typeName = "Torus"; - break; - case PrimitiveType::Unknown: - typeName = "Unknown"; - break; - default: - break; - } - return typeName; - }; + std::vector AllManagedPrimitives(); - inline int convertPrimitiveToInt(PrimitiveType type) - { - int typeInt = 5; - switch (type) - { - case PrimitiveType::Plane: - typeInt = 0; - break; - case PrimitiveType::Sphere: - typeInt = 1; - break; - case PrimitiveType::Cylinder: - typeInt = 2; - break; - case PrimitiveType::Cone: - typeInt = 3; - break; - case PrimitiveType::Torus: - typeInt = 4; - break; - case PrimitiveType::Unknown: - default: - break; - } - return typeInt; - }; -}; + std::vector AllManagedPrimitivesStr(); -#endif // __PRIMITIVETYPE_HXX__ \ No newline at end of file + std::string ConvertPrimitiveToString(PrimitiveType type); + + PrimitiveType ConvertStringToPrimitive(const std::string& type); + + int ConvertPrimitiveToInt(PrimitiveType type); +}; diff --git a/src/ShapeRecogn/README.md b/src/ShapeRecogn/README.md index 61b9eb4d0..f0e2e39da 100644 --- a/src/ShapeRecogn/README.md +++ b/src/ShapeRecogn/README.md @@ -16,7 +16,7 @@ A tool leveraging the MEDCoupling library for recognizing canonical shapes in 3D >> >> shape_recogn_builder = sr.ShapeRecognMeshBuilder("resources/ShapeRecognCone.med") >> shape_recogn = shape_recogn_builder.recognize() ->> shape_recogn.save("ShapeRecognCone_areas.med") +>> #shape_recogn.save("ShapeRecognCone_areas.med") ``` ### Without output file diff --git a/src/ShapeRecogn/ShapeRecognMesh.cxx b/src/ShapeRecogn/ShapeRecognMesh.cxx index 4c8ee5f9f..61b664d09 100644 --- a/src/ShapeRecogn/ShapeRecognMesh.cxx +++ b/src/ShapeRecogn/ShapeRecognMesh.cxx @@ -1,31 +1,32 @@ -#include "ShapeRecognMesh.hxx" +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// -#include "MEDLoader.hxx" +#include "ShapeRecognMesh.hxx" using namespace MEDCoupling; ShapeRecognMesh::ShapeRecognMesh() - : nodeK1(0), nodeK2(0), nodePrimitiveType(0), - nodeNormal(0), areaId(0), areaPrimitiveType(0), - areaNormal(0), minorRadius(0), radius(0), - angle(0), center(0), axis(0), apex(0) -{ -} -ShapeRecognMesh::~ShapeRecognMesh() + : nodeK1(nullptr), nodeK2(nullptr), nodePrimitiveType(nullptr), + nodeNormal(nullptr), areaId(nullptr), areaPrimitiveType(nullptr), + areaNormal(nullptr), minorRadius(nullptr), radius(nullptr), + angle(nullptr), center(nullptr), axis(nullptr), apex(nullptr) { - nodeK1->decrRef(); - nodeK2->decrRef(); - nodePrimitiveType->decrRef(); - nodeNormal->decrRef(); - areaId->decrRef(); - areaPrimitiveType->decrRef(); - areaNormal->decrRef(); - minorRadius->decrRef(); - radius->decrRef(); - angle->decrRef(); - center->decrRef(); - axis->decrRef(); - apex->decrRef(); } std::size_t ShapeRecognMesh::getHeapMemorySizeWithoutChildren() const @@ -57,99 +58,67 @@ ShapeRecognMesh *ShapeRecognMesh::New() return new ShapeRecognMesh; } -void ShapeRecognMesh::save(const std::string &outputFile, bool writeFromScratch) const -{ - // Nodes - // - k1 - WriteField(outputFile, nodeK1, writeFromScratch); - // - k2 - WriteField(outputFile, nodeK2, false); - // - primitive types - WriteField(outputFile, nodePrimitiveType, false); - // - Normal - WriteField(outputFile, nodeNormal, false); - // Areas - // - Area Id - WriteField(outputFile, areaId, false); - // - Primitive Types - WriteField(outputFile, areaPrimitiveType, false); - // - Normal - WriteField(outputFile, areaNormal, false); - // - Minor Radius - WriteField(outputFile, minorRadius, false); - // - Radius - WriteField(outputFile, radius, false); - // - Angle - WriteField(outputFile, angle, false); - // - Center - WriteField(outputFile, center, false); - // - Axis - WriteField(outputFile, axis, false); - // - Apex - WriteField(outputFile, apex, false); -} - -MEDCouplingFieldDouble *ShapeRecognMesh::getNodeK1() const +const MEDCouplingFieldDouble *ShapeRecognMesh::getNodeK1() const { return nodeK1; } -MEDCouplingFieldDouble *ShapeRecognMesh::getNodeK2() const +const MEDCouplingFieldDouble *ShapeRecognMesh::getNodeK2() const { return nodeK2; } -MEDCouplingFieldDouble *ShapeRecognMesh::getNodePrimitiveType() const +const MEDCouplingFieldInt32 *ShapeRecognMesh::getNodePrimitiveType() const { return nodePrimitiveType; } -MEDCouplingFieldDouble *ShapeRecognMesh::getNodeNormal() const +const MEDCouplingFieldDouble *ShapeRecognMesh::getNodeNormal() const { return nodeNormal; } -MEDCouplingFieldDouble *ShapeRecognMesh::getAreaId() const +const MEDCouplingFieldInt32 *ShapeRecognMesh::getAreaId() const { return areaId; } -MEDCouplingFieldDouble *ShapeRecognMesh::getAreaPrimitiveType() const +const MEDCouplingFieldInt32 *ShapeRecognMesh::getAreaPrimitiveType() const { return areaPrimitiveType; } -MEDCouplingFieldDouble *ShapeRecognMesh::getAreaNormal() const +const MEDCouplingFieldDouble *ShapeRecognMesh::getAreaNormal() const { return areaNormal; } -MEDCouplingFieldDouble *ShapeRecognMesh::getMinorRadius() const +const MEDCouplingFieldDouble *ShapeRecognMesh::getMinorRadius() const { return minorRadius; } -MEDCouplingFieldDouble *ShapeRecognMesh::getRadius() const +const MEDCouplingFieldDouble *ShapeRecognMesh::getRadius() const { return radius; } -MEDCouplingFieldDouble *ShapeRecognMesh::getAngle() const +const MEDCouplingFieldDouble *ShapeRecognMesh::getAngle() const { return angle; } -MEDCouplingFieldDouble *ShapeRecognMesh::getCenter() const +const MEDCouplingFieldDouble *ShapeRecognMesh::getCenter() const { return center; } -MEDCouplingFieldDouble *ShapeRecognMesh::getAxis() const +const MEDCouplingFieldDouble *ShapeRecognMesh::getAxis() const { return axis; } -MEDCouplingFieldDouble *ShapeRecognMesh::getApex() const +const MEDCouplingFieldDouble *ShapeRecognMesh::getApex() const { return apex; } diff --git a/src/ShapeRecogn/ShapeRecognMesh.hxx b/src/ShapeRecogn/ShapeRecognMesh.hxx index 02983e34a..efbb312e8 100644 --- a/src/ShapeRecogn/ShapeRecognMesh.hxx +++ b/src/ShapeRecogn/ShapeRecognMesh.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,12 +17,12 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __SHAPERECOGNMESH_HXX__ -#define __SHAPERECOGNMESH_HXX__ +#pragma once #include #include "MEDCouplingUMesh.hxx" +#include "MEDCouplingFieldInt32.hxx" #include "MEDCouplingFieldDouble.hxx" #include "MEDCouplingRefCountObject.hxx" @@ -37,44 +37,45 @@ namespace MEDCoupling std::size_t getHeapMemorySizeWithoutChildren() const; std::vector getDirectChildrenWithNull() const; - void save(const std::string &outputFile, bool writeFromScratch = true) const; - // Node properties - MEDCoupling::MEDCouplingFieldDouble *getNodeK1() const; - MEDCoupling::MEDCouplingFieldDouble *getNodeK2() const; - MEDCoupling::MEDCouplingFieldDouble *getNodePrimitiveType() const; - MEDCoupling::MEDCouplingFieldDouble *getNodeNormal() const; + const MEDCouplingFieldDouble *getNodeK1() const; + const MEDCouplingFieldDouble *getNodeK2() const; + const MEDCouplingFieldInt32 *getNodePrimitiveType() const; + const MEDCouplingFieldDouble *getNodeNormal() const; + // see ShapeRecognMeshBuilder::buildNodeWeakDirections + // see ShapeRecognMeshBuilder::buildNodeMainDirections // Area properties - MEDCoupling::MEDCouplingFieldDouble *getAreaId() const; - MEDCoupling::MEDCouplingFieldDouble *getAreaPrimitiveType() const; - MEDCoupling::MEDCouplingFieldDouble *getAreaNormal() const; - MEDCoupling::MEDCouplingFieldDouble *getMinorRadius() const; - MEDCoupling::MEDCouplingFieldDouble *getRadius() const; - MEDCoupling::MEDCouplingFieldDouble *getAngle() const; - MEDCoupling::MEDCouplingFieldDouble *getCenter() const; - MEDCoupling::MEDCouplingFieldDouble *getAxis() const; - MEDCoupling::MEDCouplingFieldDouble *getApex() const; + const MEDCouplingFieldInt32 *getAreaId() const; + const MEDCouplingFieldInt32 *getAreaPrimitiveType() const; + const MEDCouplingFieldDouble *getAreaNormal() const; + const MEDCouplingFieldDouble *getMinorRadius() const; + const MEDCouplingFieldDouble *getRadius() const; + const MEDCouplingFieldDouble *getAngle() const; + const MEDCouplingFieldDouble *getCenter() const; + const MEDCouplingFieldDouble *getAxis() const; + const MEDCouplingFieldDouble *getApex() const; + + // see ShapeRecognMeshBuilder::buildAreaAxisPoint + // see ShapeRecognMeshBuilder::buildAreaAffinePoint protected: ShapeRecognMesh(); - ~ShapeRecognMesh(); + ~ShapeRecognMesh() = default; private: - MEDCoupling::MEDCouplingFieldDouble *nodeK1; - MEDCoupling::MEDCouplingFieldDouble *nodeK2; - MEDCoupling::MEDCouplingFieldDouble *nodePrimitiveType; - MEDCoupling::MEDCouplingFieldDouble *nodeNormal; - MEDCoupling::MEDCouplingFieldDouble *areaId; - MEDCoupling::MEDCouplingFieldDouble *areaPrimitiveType; - MEDCoupling::MEDCouplingFieldDouble *areaNormal; - MEDCoupling::MEDCouplingFieldDouble *minorRadius; - MEDCoupling::MEDCouplingFieldDouble *radius; - MEDCoupling::MEDCouplingFieldDouble *angle; - MEDCoupling::MEDCouplingFieldDouble *center; - MEDCoupling::MEDCouplingFieldDouble *axis; - MEDCoupling::MEDCouplingFieldDouble *apex; + MCAuto nodeK1; + MCAuto nodeK2; + MCAuto nodePrimitiveType; + MCAuto nodeNormal; + MCAuto areaId; + MCAuto areaPrimitiveType; + MCAuto areaNormal; + MCAuto minorRadius; + MCAuto radius; + MCAuto angle; + MCAuto center; + MCAuto axis; + MCAuto apex; }; -}; - -#endif // __SHAPERECOGNMESH_HXX__ +} diff --git a/src/ShapeRecogn/ShapeRecognMeshBuilder.cxx b/src/ShapeRecogn/ShapeRecognMeshBuilder.cxx index f0a2b0581..8b817eb2e 100644 --- a/src/ShapeRecogn/ShapeRecognMeshBuilder.cxx +++ b/src/ShapeRecogn/ShapeRecognMeshBuilder.cxx @@ -1,39 +1,67 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "ShapeRecognMeshBuilder.hxx" #include "NodesBuilder.hxx" #include "AreasBuilder.hxx" -#include "MEDLoader.hxx" #include "ShapeRecognMesh.hxx" + #include "MEDCouplingFieldDouble.hxx" +#include "MEDCouplingFieldInt64.hxx" +#include "MEDCouplingFieldInt32.hxx" + +#include +#include +#include using namespace MEDCoupling; -ShapeRecognMeshBuilder::ShapeRecognMeshBuilder(const std::string &fileName, int meshDimRelToMax) +ShapeRecognMeshBuilder::ShapeRecognMeshBuilder(MCAuto< MEDCouplingUMesh > mesh) +{ + assign( mesh ); +} + +ShapeRecognMeshBuilder::ShapeRecognMeshBuilder(MEDCouplingUMesh *mesh) +{ + MCAuto< MEDCouplingUMesh > meshIn = MCAuto< MEDCouplingUMesh >::TakeRef( mesh ); + assign( meshIn ); +} + +void ShapeRecognMeshBuilder::assign(MCAuto< MEDCouplingUMesh > mesh) { - mesh = ReadUMeshFromFile(fileName, meshDimRelToMax); + this->mesh = mesh; if (mesh->getMeshDimension() != 2) throw INTERP_KERNEL::Exception("Expect a mesh with a dimension equal to 2"); if (mesh->getNumberOfCellsWithType(INTERP_KERNEL::NORM_TRI3) != mesh->getNumberOfCells()) throw INTERP_KERNEL::Exception("Expect a mesh containing exclusively triangular cells"); } -ShapeRecognMeshBuilder::~ShapeRecognMeshBuilder() -{ - if (areas != nullptr) - delete areas; - if (nodes != nullptr) - delete nodes; - mesh->decrRef(); -} -ShapeRecognMesh *ShapeRecognMeshBuilder::recognize() +MCAuto ShapeRecognMeshBuilder::recognize() { mesh->incrRef(); NodesBuilder nodesBuilder(mesh); - nodes = nodesBuilder.build(); - AreasBuilder areasBuilder(nodes); + nodes.reset( nodesBuilder.build() ); + AreasBuilder areasBuilder(nodes.get()); areasBuilder.build(); - areas = areasBuilder.getAreas(); + areas.reset( areasBuilder.getAreas() ); MCAuto recognMesh = ShapeRecognMesh::New(); recognMesh->nodeK1 = buildNodeK1(); recognMesh->nodeK2 = buildNodeK2(); @@ -48,203 +76,253 @@ ShapeRecognMesh *ShapeRecognMeshBuilder::recognize() recognMesh->center = buildCenter(); recognMesh->axis = buildAxis(); recognMesh->apex = buildApex(); - return recognMesh.retn(); + return recognMesh; } const Nodes *ShapeRecognMeshBuilder::getNodes() const { - return nodes; + return nodes.get(); } const Areas *ShapeRecognMeshBuilder::getAreas() const { - return areas; + return areas.get(); } -MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildNodeK1() const +void ShapeRecognMeshBuilder::checkNodesBeforeBuildingField() const { - if (nodes == nullptr) + if ( ! nodes.get() ) throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); - return buildField("K1 (Node)", 1, nodes->getK1()); } -MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildNodeK2() const +void ShapeRecognMeshBuilder::checkAreasBeforeBuildingField() const { - if (nodes == nullptr) + if ( ! areas.get() ) throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); - return buildField("K2 (Node)", 1, nodes->getK2()); } -MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildNodePrimitiveType() const +template +typename Traits::FieldType *buildField( + const std::string &name, + MCAuto::ArrayType> data, + const MEDCouplingUMesh *mesh) { - if (nodes == nullptr) - throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); - return buildField("Primitive Type (Node)", 1, nodes->getPrimitiveType()); + using ZeField = typename Traits::FieldType; + ZeField *field = ZeField::New(ON_NODES); + mcIdType nbNodes = data->getNumberOfTuples(); + size_t nbOfCompo = data->getNumberOfComponents(); + field->setName(name); + field->setMesh(mesh); + if (nbOfCompo == 3) + { + data->setInfoOnComponent(0, "X"); + data->setInfoOnComponent(1, "Y"); + data->setInfoOnComponent(2, "Z"); + } + field->setArray(data); + return field; +} + +template +typename Traits::FieldType *buildField( + const std::string &name, + size_t nbOfCompo, + const std::vector &values, + const MEDCouplingUMesh *mesh) +{ + using ZeArray = typename Traits::ArrayType; + mcIdType nbNodes = mesh->getNumberOfNodes(); + MCAuto data( ZeArray::New() ); + data->setName(name); + data->alloc(nbNodes, nbOfCompo); + std::copy(values.begin(), values.end(), data->getPointer()); + data->declareAsNew(); + return buildField(name, data, mesh); +} + +template +typename Traits::FieldType *buildField( + const std::string &name, + size_t nbOfCompo, + T *values, + const MEDCouplingUMesh *mesh) +{ + using ZeArray = typename Traits::ArrayType; + mcIdType nbNodes = mesh->getNumberOfNodes(); + ZeArray *data = ZeArray::New(); + data->setName(name); + data->useArray( + values, + true, + MEDCoupling::DeallocType::CPP_DEALLOC, + nbNodes, + nbOfCompo); + return buildField(name, data, mesh); +} + +template +T *buildAreaArrayT(Areas *areas, Nodes *nodes, std::function areaFunc) +{ + const std::vector &areaIdByNodes = areas->getAreaIdByNodes(); + T *values = new T[nodes->getNbNodes()]; + for (size_t nodeId = 0; nodeId < areaIdByNodes.size(); ++nodeId) + { + Int32 areaId = areaIdByNodes[nodeId]; + if (areaId != -1) + values[nodeId] = areaFunc(areas, areaId); + else + values[nodeId] = std::numeric_limits::max(); + } + return values; +} + +MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildNodeK1() const +{ + checkNodesBeforeBuildingField(); + return buildField("K1 (Node)", 1, nodes->getK1(), mesh); +} + +MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildNodeK2() const +{ + checkNodesBeforeBuildingField(); + return buildField("K2 (Node)", 1, nodes->getK2(), mesh); +} + +MEDCouplingFieldInt32 *ShapeRecognMeshBuilder::buildNodePrimitiveType() const +{ + checkNodesBeforeBuildingField(); + std::vector tmp; + std::transform(nodes->getPrimitiveType().begin(),nodes->getPrimitiveType().end(),std::back_inserter(tmp),[](const PrimitiveType& elt){ return Int32(elt); }); + return buildField("Primitive Type (Node)", 1, tmp, mesh); } MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildNodeNormal() const { - if (nodes == nullptr) - throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); - return buildField("Normal (Node)", 3, nodes->getNormals()); + checkNodesBeforeBuildingField(); + return buildField("Normal (Node)", 3, nodes->getNormals(), mesh); } -MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildAreaId() const +MEDCoupling::MCAuto ShapeRecognMeshBuilder::buildNodeWeakDirections() const { - if (areas == nullptr) - throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); - return buildField("Area Id", 1, areas->getAreaIdByNodes()); + checkNodesBeforeBuildingField(); + return MEDCoupling::MCAuto(buildField("WeakDirection (Node)", 3, nodes->getWeakDirections(), mesh)); } -MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildAreaPrimitiveType() const +MEDCoupling::MCAuto ShapeRecognMeshBuilder::buildNodeMainDirections() const { - if (areas == nullptr) - throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); - double *values = buildAreaArray([](Areas *areas, mcIdType areaId) -> double - { return (double)areas->getPrimitiveType(areaId); }); - return buildField("Primitive Type (Area)", 1, values); + checkNodesBeforeBuildingField(); + return MEDCoupling::MCAuto(buildField("MainDirection (Node)", 3, nodes->getMainDirections(), mesh)); +} + +MEDCouplingFieldInt32 *ShapeRecognMeshBuilder::buildAreaId() const +{ + checkAreasBeforeBuildingField(); + return buildField("Area Id", 1, areas->getAreaIdByNodes(), mesh); +} + +MEDCouplingFieldInt32 *ShapeRecognMeshBuilder::buildAreaPrimitiveType() const +{ + checkAreasBeforeBuildingField(); + Int32 *values = buildAreaArrayT(areas.get(),nodes.get(),[](Areas *areas, mcIdType areaId) -> Int32 + { return (Int32)areas->getPrimitiveType(areaId); }); + return buildField("Primitive Type (Area)", 1, values, mesh); } MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildAreaNormal() const { - if (areas == nullptr) - throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); + checkAreasBeforeBuildingField(); double *values = buildArea3DArray([](Areas *areas, mcIdType areaId) -> const std::array & { return areas->getNormal(areaId); }); - return buildField("Normal (Area)", 3, values); + return buildField("Normal (Area)", 3, values, mesh); } MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildMinorRadius() const { - if (areas == nullptr) - throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); + checkAreasBeforeBuildingField(); double *values = buildAreaArray([](Areas *areas, mcIdType areaId) -> double { return areas->getMinorRadius(areaId); }); - return buildField("Minor Radius (Area)", 1, values); + return buildField("Minor Radius (Area)", 1, values, mesh); } MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildRadius() const { - if (areas == nullptr) - throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); + checkAreasBeforeBuildingField(); double *values = buildAreaArray([](Areas *areas, mcIdType areaId) -> double { return areas->getRadius(areaId); }); - return buildField("Radius (Area)", 1, values); + return buildField("Radius (Area)", 1, values, mesh); } MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildAngle() const { - if (areas == nullptr) - throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); + checkAreasBeforeBuildingField(); double *values = buildAreaArray([](Areas *areas, mcIdType areaId) -> double { return areas->getAngle(areaId); }); - return buildField("Angle (Area)", 1, values); + return buildField("Angle (Area)", 1, values, mesh); } MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildCenter() const { - if (areas == nullptr) - throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); - double *values = buildArea3DArray([](Areas *areas, mcIdType areaId) -> const std::array & + checkAreasBeforeBuildingField(); + double *values = buildArea3DArray([](Areas *areas, mcIdType areaId) -> std::array { return areas->getCenter(areaId); }); - return buildField("Center (Area)", 3, values); + return buildField("Center (Area)", 3, values, mesh); } MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildAxis() const { - if (areas == nullptr) - throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); - double *values = buildArea3DArray([](Areas *areas, mcIdType areaId) -> const std::array & + checkAreasBeforeBuildingField(); + double *values = buildArea3DArray([](Areas *areas, mcIdType areaId) -> std::array { return areas->getAxis(areaId); }); - return buildField("Axis (Area)", 3, values); + return buildField("Axis (Area)", 3, values, mesh); } MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildApex() const { - if (areas == nullptr) - throw INTERP_KERNEL::Exception("recognize must be called before building any fields"); - double *values = buildArea3DArray([](Areas *areas, mcIdType areaId) -> const std::array & + checkAreasBeforeBuildingField(); + double *values = buildArea3DArray([](Areas *areas, mcIdType areaId) -> std::array { return areas->getApex(areaId); }); - return buildField("Apex (Area)", 3, values); -} - -template -MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildField( - const std::string &name, - size_t nbOfCompo, - const std::vector &values) const -{ - DataArrayDouble *data = DataArrayDouble::New(); - data->setName(name); - data->alloc(nodes->getNbNodes(), nbOfCompo); - std::copy(values.begin(), values.end(), data->getPointer()); - data->declareAsNew(); - return buildField(name, nbOfCompo, data); + return buildField("Apex (Area)", 3, values, mesh); } -MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildField( - const std::string &name, - size_t nbOfCompo, - double *values) const +MEDCoupling::MCAuto ShapeRecognMeshBuilder::buildAreaAxisPoint() const { - DataArrayDouble *data = DataArrayDouble::New(); - data->setName(name); - data->useArray( - values, - true, - MEDCoupling::DeallocType::CPP_DEALLOC, - nodes->getNbNodes(), - nbOfCompo); - return buildField(name, nbOfCompo, data); + checkAreasBeforeBuildingField(); + double *values = buildArea3DArray([](Areas *areas, mcIdType areaId) -> std::array + { return areas->getAxisPoint(areaId); }); + return MEDCoupling::MCAuto( buildField("AxisPoint (Area)", 3, values, mesh) ); } -MEDCouplingFieldDouble *ShapeRecognMeshBuilder::buildField( - const std::string &name, - size_t nbOfCompo, - DataArrayDouble *data) const +MEDCoupling::MCAuto ShapeRecognMeshBuilder::buildAreaAffinePoint() const { - MEDCouplingFieldDouble *field = MEDCouplingFieldDouble::New(ON_NODES); - field->setName(name); - field->setMesh(mesh); - if (nbOfCompo == 3) - { - data->setInfoOnComponent(0, "X"); - data->setInfoOnComponent(1, "Y"); - data->setInfoOnComponent(2, "Z"); - } - field->setArray(data); - data->decrRef(); - return field; + checkAreasBeforeBuildingField(); + double *values = buildArea3DArray([](Areas *areas, mcIdType areaId) -> std::array + { return areas->getAffinePoint(areaId); }); + return MEDCoupling::MCAuto( buildField("AffinePoint (Area)", 3, values, mesh) ); } -double *ShapeRecognMeshBuilder::buildArea3DArray( - const std::array &(*areaFunc)(Areas *, mcIdType)) const +double *ShapeRecognMeshBuilder::buildArea3DArray(std::function(Areas *, Int32)> areaFunc) const { double *values = new double[3 * nodes->getNbNodes()]; - const std::vector &areaIdByNodes = areas->getAreaIdByNodes(); + const std::vector &areaIdByNodes = areas->getAreaIdByNodes(); for (size_t nodeId = 0; nodeId < areaIdByNodes.size(); ++nodeId) { - mcIdType areaId = areaIdByNodes[nodeId]; + Int32 areaId = areaIdByNodes[nodeId]; if (areaId != -1) { - const std::array &areaValues = areaFunc(areas, areaId); + const std::array areaValues( areaFunc(areas.get(), areaId) ); values[3 * nodeId] = areaValues[0]; values[3 * nodeId + 1] = areaValues[1]; values[3 * nodeId + 2] = areaValues[2]; } + else + { + std::for_each(values + 3 * nodeId, values + 3 * (nodeId + 1), [](double& val) { val = std::numeric_limits::max(); }); + } } return values; } -double *ShapeRecognMeshBuilder::buildAreaArray(double (*areaFunc)(Areas *, mcIdType)) const +double *ShapeRecognMeshBuilder::buildAreaArray(std::function areaFunc) const { - const std::vector &areaIdByNodes = areas->getAreaIdByNodes(); - double *values = new double[nodes->getNbNodes()]; - for (size_t nodeId = 0; nodeId < areaIdByNodes.size(); ++nodeId) - { - mcIdType areaId = areaIdByNodes[nodeId]; - if (areaId != -1) - values[nodeId] = areaFunc(areas, areaId); - } - return values; + return buildAreaArrayT(areas.get(), nodes.get(), areaFunc ); } diff --git a/src/ShapeRecogn/ShapeRecognMeshBuilder.hxx b/src/ShapeRecogn/ShapeRecognMeshBuilder.hxx index eee7c1507..ab20016ea 100644 --- a/src/ShapeRecogn/ShapeRecognMeshBuilder.hxx +++ b/src/ShapeRecogn/ShapeRecognMeshBuilder.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,41 +17,55 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __SHAPERECOGNMESHBUILDER_HXX__ -#define __SHAPERECOGNMESHBUILDER_HXX__ +#pragma once #include #include "MEDCouplingUMesh.hxx" -#include "MEDCouplingFieldDouble.hxx" + +#include "Nodes.hxx" +#include "Areas.hxx" + +#include +#include namespace MEDCoupling { - class Nodes; - class Areas; + class MEDCouplingFieldInt32; + class MEDCouplingFieldInt64; + class MEDCouplingFieldDouble; + class ShapeRecognMesh; class ShapeRecognMeshBuilder { public: - ShapeRecognMeshBuilder(const std::string &fileName, int meshDimRelToMax = 0); - ~ShapeRecognMeshBuilder(); + ShapeRecognMeshBuilder(MCAuto< MEDCouplingUMesh > mesh); + ShapeRecognMeshBuilder(MEDCouplingUMesh *mesh); + ~ShapeRecognMeshBuilder() = default; const Nodes *getNodes() const; const Areas *getAreas() const; - ShapeRecognMesh *recognize(); + MCAuto recognize(); + + // Node properties + MEDCoupling::MCAuto buildNodeWeakDirections() const; + MEDCoupling::MCAuto buildNodeMainDirections() const; + //Area properties + MEDCoupling::MCAuto buildAreaAxisPoint() const; + MEDCoupling::MCAuto buildAreaAffinePoint() const; private: // Node properties MEDCoupling::MEDCouplingFieldDouble *buildNodeK1() const; MEDCoupling::MEDCouplingFieldDouble *buildNodeK2() const; - MEDCoupling::MEDCouplingFieldDouble *buildNodePrimitiveType() const; + MEDCoupling::MEDCouplingFieldInt32 *buildNodePrimitiveType() const; MEDCoupling::MEDCouplingFieldDouble *buildNodeNormal() const; - + // Area properties - MEDCoupling::MEDCouplingFieldDouble *buildAreaId() const; - MEDCoupling::MEDCouplingFieldDouble *buildAreaPrimitiveType() const; + MEDCoupling::MEDCouplingFieldInt32 *buildAreaId() const; + MEDCoupling::MEDCouplingFieldInt32 *buildAreaPrimitiveType() const; MEDCoupling::MEDCouplingFieldDouble *buildAreaNormal() const; MEDCoupling::MEDCouplingFieldDouble *buildMinorRadius() const; MEDCoupling::MEDCouplingFieldDouble *buildRadius() const; @@ -60,26 +74,14 @@ namespace MEDCoupling MEDCoupling::MEDCouplingFieldDouble *buildAxis() const; MEDCoupling::MEDCouplingFieldDouble *buildApex() const; - template - MEDCouplingFieldDouble *buildField( - const std::string &name, - size_t nbOfCompo, - const std::vector &values) const; - MEDCouplingFieldDouble *buildField( - const std::string &name, - size_t nbOfCompo, - double *values) const; - MEDCouplingFieldDouble *buildField( - const std::string &name, - size_t nbOfCompo, - DataArrayDouble *values) const; - double *buildArea3DArray(const std::array &(*areaFunc)(Areas *, mcIdType)) const; - double *buildAreaArray(double (*areaFunc)(Areas *, mcIdType)) const; - - const MEDCouplingUMesh *mesh; - Nodes *nodes = nullptr; - Areas *areas = nullptr; + double *buildArea3DArray(std::function(Areas *, Int32)> areaFunc) const; + double *buildAreaArray(std::function areaFunc) const; + void assign(MCAuto< MEDCouplingUMesh > mesh); + void checkNodesBeforeBuildingField() const; + void checkAreasBeforeBuildingField() const; + private: + MCConstAuto< MEDCouplingUMesh > mesh; + std::unique_ptr nodes; + std::unique_ptr areas; }; -}; - -#endif // __SHAPERECOGNMESHBUILDER_HXX__ +} diff --git a/src/ShapeRecogn/ShapeRecongConstants.hxx b/src/ShapeRecogn/ShapeRecongConstants.hxx index 801df4dfa..02062b44f 100644 --- a/src/ShapeRecogn/ShapeRecongConstants.hxx +++ b/src/ShapeRecogn/ShapeRecongConstants.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,8 +17,7 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __SHAPERECOGNCONSTANTS_HXX__ -#define __SHAPERECOGNCONSTANTS_HXX__ +#pragma once namespace MEDCoupling { @@ -38,6 +37,4 @@ namespace MEDCoupling // - Thresholds constexpr int THRESHOLD_MIN_NB_NODES = 5; constexpr int THRESHOLD_MAX_NB_AREAS = 500; -}; - -#endif //__SHAPERECOGNCONSTANTS_HXX__ \ No newline at end of file +} diff --git a/src/ShapeRecogn/Swig/CMakeLists.txt b/src/ShapeRecogn/Swig/CMakeLists.txt index 8e839d156..1a73c982c 100644 --- a/src/ShapeRecogn/Swig/CMakeLists.txt +++ b/src/ShapeRecogn/Swig/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2024 CEA, EDF +# Copyright (C) 2024 CEA, EDF # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -75,7 +75,6 @@ IF(WIN32) ENDIF(WIN32) INSTALL(TARGETS _ShapeRecogn DESTINATION ${MEDCOUPLING_INSTALL_PYTHON}) -INSTALL(FILES ShapeRecogn.i DESTINATION ${MEDCOUPLING_INSTALL_HEADERS}) SALOME_INSTALL_SCRIPTS( ${CMAKE_CURRENT_BINARY_DIR}/ShapeRecogn.py diff --git a/src/ShapeRecogn/Swig/ShapeRecogn.i b/src/ShapeRecogn/Swig/ShapeRecogn.i index 58893b0c6..0b129593b 100644 --- a/src/ShapeRecogn/Swig/ShapeRecogn.i +++ b/src/ShapeRecogn/Swig/ShapeRecogn.i @@ -1,15 +1,217 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + %module ShapeRecogn -%include "std_string.i" -%include "MEDCouplingCommon.i" +%include "ShapeRecognCommon.i" -%{ -#include "ShapeRecognMesh.hxx" -#include "ShapeRecognMeshBuilder.hxx" -using namespace MEDCoupling; +%pythoncode %{ +def MEDCouplingDataArrayDoubleIadd(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayDouble____iadd___(self, self, *args) +def MEDCouplingDataArrayDoubleIsub(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayDouble____isub___(self, self, *args) +def MEDCouplingDataArrayDoubleImul(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayDouble____imul___(self, self, *args) +def MEDCouplingDataArrayDoubleIdiv(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayDouble____idiv___(self, self, *args) +def MEDCouplingDataArrayDoubleIpow(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayDouble____ipow___(self, self, *args) +def MEDCouplingDataArrayInt32Iadd(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt32____iadd___(self, self, *args) +def MEDCouplingDataArrayInt32Isub(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt32____isub___(self, self, *args) +def MEDCouplingDataArrayInt32Imul(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt32____imul___(self, self, *args) +def MEDCouplingDataArrayInt32Idiv(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt32____idiv___(self, self, *args) +def MEDCouplingDataArrayInt32Imod(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt32____imod___(self, self, *args) +def MEDCouplingDataArrayInt32Ipow(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt32____ipow___(self, self, *args) +def MEDCouplingDataArrayInt64Iadd(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt64____iadd___(self, self, *args) +def MEDCouplingDataArrayInt64Isub(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt64____isub___(self, self, *args) +def MEDCouplingDataArrayInt64Imul(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt64____imul___(self, self, *args) +def MEDCouplingDataArrayInt64Idiv(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt64____idiv___(self, self, *args) +def MEDCouplingDataArrayInt64Imod(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt64____imod___(self, self, *args) +def MEDCouplingDataArrayInt64Ipow(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt64____ipow___(self, self, *args) +def MEDCouplingDataArrayFloatIadd(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayFloat____iadd___(self, self, *args) +def MEDCouplingDataArrayFloatIsub(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayFloat____isub___(self, self, *args) +def MEDCouplingDataArrayFloatImul(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayFloat____imul___(self, self, *args) +def MEDCouplingDataArrayFloatIdiv(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayFloat____idiv___(self, self, *args) +def MEDCouplingDataArrayDoubleTupleIadd(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayDoubleTuple____iadd___(self, self, *args) +def MEDCouplingDataArrayDoubleTupleIsub(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayDoubleTuple____isub___(self, self, *args) +def MEDCouplingDataArrayDoubleTupleImul(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayDoubleTuple____imul___(self, self, *args) +def MEDCouplingDataArrayDoubleTupleIdiv(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayDoubleTuple____idiv___(self, self, *args) +def MEDCouplingDataArrayInt32TupleIadd(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt32Tuple____iadd___(self, self, *args) +def MEDCouplingDataArrayInt32TupleIsub(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt32Tuple____isub___(self, self, *args) +def MEDCouplingDataArrayInt32TupleImul(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt32Tuple____imul___(self, self, *args) +def MEDCouplingDataArrayInt32TupleIdiv(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt32Tuple____idiv___(self, self, *args) +def MEDCouplingDataArrayInt32TupleImod(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt32Tuple____imod___(self, self, *args) +def MEDCouplingDataArrayInt64TupleIadd(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt64Tuple____iadd___(self, self, *args) +def MEDCouplingDataArrayInt64TupleIsub(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt64Tuple____isub___(self, self, *args) +def MEDCouplingDataArrayInt64TupleImul(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt64Tuple____imul___(self, self, *args) +def MEDCouplingDataArrayInt64TupleIdiv(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt64Tuple____idiv___(self, self, *args) +def MEDCouplingDataArrayInt64TupleImod(self,*args): + import _MEDRenumber + return _MEDRenumber.DataArrayInt64Tuple____imod___(self, self, *args) %} -%ignore getAreas() const; -%ignore getNodes() const; -%include "ShapeRecognMesh.hxx" -%include "ShapeRecognMeshBuilder.hxx" + +%pythoncode %{ +InterpKernelException.__reduce__=INTERPKERNELExceptionReduce +DataArrayDouble.__reduce__=MEDCouplingDataArrayDoubleReduce +DataArrayDouble.__iadd__=MEDCouplingDataArrayDoubleIadd +DataArrayDouble.__isub__=MEDCouplingDataArrayDoubleIsub +DataArrayDouble.__imul__=MEDCouplingDataArrayDoubleImul +DataArrayDouble.__idiv__=MEDCouplingDataArrayDoubleIdiv +DataArrayDouble.__ipow__=MEDCouplingDataArrayDoubleIpow + +DataArrayInt32.__reduce__=MEDCouplingDataArrayInt32Reduce +DataArrayInt32.__iadd__=MEDCouplingDataArrayInt32Iadd +DataArrayInt32.__isub__=MEDCouplingDataArrayInt32Isub +DataArrayInt32.__imul__=MEDCouplingDataArrayInt32Imul +DataArrayInt32.__idiv__=MEDCouplingDataArrayInt32Idiv +DataArrayInt32.__imod__=MEDCouplingDataArrayInt32Imod +DataArrayInt32.__ipow__=MEDCouplingDataArrayInt32Ipow +DataArrayInt64.__reduce__=MEDCouplingDataArrayInt64Reduce +DataArrayInt64.__iadd__=MEDCouplingDataArrayInt64Iadd +DataArrayInt64.__isub__=MEDCouplingDataArrayInt64Isub +DataArrayInt64.__imul__=MEDCouplingDataArrayInt64Imul +DataArrayInt64.__idiv__=MEDCouplingDataArrayInt64Idiv +DataArrayInt64.__imod__=MEDCouplingDataArrayInt64Imod +DataArrayInt64.__ipow__=MEDCouplingDataArrayInt64Ipow + +DataArrayDoubleTuple.__iadd__=MEDCouplingDataArrayDoubleTupleIadd +DataArrayDoubleTuple.__isub__=MEDCouplingDataArrayDoubleTupleIsub +DataArrayDoubleTuple.__imul__=MEDCouplingDataArrayDoubleTupleImul +DataArrayDoubleTuple.__idiv__=MEDCouplingDataArrayDoubleTupleIdiv + +DataArrayInt32Tuple.__iadd__=MEDCouplingDataArrayInt32TupleIadd +DataArrayInt32Tuple.__isub__=MEDCouplingDataArrayInt32TupleIsub +DataArrayInt32Tuple.__imul__=MEDCouplingDataArrayInt32TupleImul +DataArrayInt32Tuple.__idiv__=MEDCouplingDataArrayInt32TupleIdiv +DataArrayInt32Tuple.__itruediv__=MEDCouplingDataArrayInt32TupleIdiv +DataArrayInt32Tuple.__ifloordiv__=MEDCouplingDataArrayInt32TupleIdiv +DataArrayInt32Tuple.__imod__=MEDCouplingDataArrayInt32TupleImod + +DataArrayInt64Tuple.__iadd__=MEDCouplingDataArrayInt64TupleIadd +DataArrayInt64Tuple.__isub__=MEDCouplingDataArrayInt64TupleIsub +DataArrayInt64Tuple.__imul__=MEDCouplingDataArrayInt64TupleImul +DataArrayInt64Tuple.__idiv__=MEDCouplingDataArrayInt64TupleIdiv +DataArrayInt64Tuple.__itruediv__=MEDCouplingDataArrayInt64TupleIdiv +DataArrayInt64Tuple.__ifloordiv__=MEDCouplingDataArrayInt64TupleIdiv +DataArrayInt64Tuple.__imod__=MEDCouplingDataArrayInt64TupleImod + + + + +del INTERPKERNELExceptionReduce +del MEDCouplingDataArrayDoubleIadd +del MEDCouplingDataArrayDoubleIdiv +del MEDCouplingDataArrayDoubleImul +del MEDCouplingDataArrayDoubleIpow +del MEDCouplingDataArrayDoubleIsub +del MEDCouplingDataArrayDoubleReduce +del MEDCouplingDataArrayDoubleTupleIadd +del MEDCouplingDataArrayDoubleTupleIdiv +del MEDCouplingDataArrayDoubleTupleImul +del MEDCouplingDataArrayDoubleTupleIsub +del MEDCouplingDataArrayInt32Iadd +del MEDCouplingDataArrayInt32Idiv +del MEDCouplingDataArrayInt32Imod +del MEDCouplingDataArrayInt32Imul +del MEDCouplingDataArrayInt32Ipow +del MEDCouplingDataArrayInt32Isub +del MEDCouplingDataArrayInt32Reduce +del MEDCouplingDataArrayInt32TupleIadd +del MEDCouplingDataArrayInt32TupleIdiv +del MEDCouplingDataArrayInt32TupleImod +del MEDCouplingDataArrayInt32TupleImul +del MEDCouplingDataArrayInt32TupleIsub +del MEDCouplingDataArrayInt64Iadd +del MEDCouplingDataArrayInt64Idiv +del MEDCouplingDataArrayInt64Imod +del MEDCouplingDataArrayInt64Imul +del MEDCouplingDataArrayInt64Ipow +del MEDCouplingDataArrayInt64Isub +del MEDCouplingDataArrayInt64Reduce +del MEDCouplingDataArrayInt64TupleIadd +del MEDCouplingDataArrayInt64TupleIdiv +del MEDCouplingDataArrayInt64TupleImod +del MEDCouplingDataArrayInt64TupleImul +del MEDCouplingDataArrayInt64TupleIsub + +%} diff --git a/src/ShapeRecogn/Swig/ShapeRecognCommon.i b/src/ShapeRecogn/Swig/ShapeRecognCommon.i new file mode 100644 index 000000000..66e959206 --- /dev/null +++ b/src/ShapeRecogn/Swig/ShapeRecognCommon.i @@ -0,0 +1,61 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +%include "std_string.i" +%include "MEDCouplingCommon.i" + +%{ +#include "MEDCouplingMemArray.txx" +#include "MCAuto.hxx" +#include "MEDCouplingDataArrayTypemaps.i" + +using namespace MEDCoupling; +using namespace INTERP_KERNEL; +%} + +#ifdef WITH_NUMPY +%init %{ import_array(); %} +#endif + +%init %{ initializeMe_shape_recogn(); %} + +%feature("autodoc", "1"); +%feature("docstring"); + +%nodefaultctor; + +%rename (InterpKernelException) INTERP_KERNEL::Exception; + +%{ + void initializeMe_shape_recogn() + {// AGY : here initialization of C++ traits in MEDCouplingDataArrayTypemaps.i for code factorization. Awful, I know, but no other solutions. + SWIGTITraits::TI=SWIGTYPE_p_MEDCoupling__DataArrayDouble; + SWIGTITraits::TI=SWIGTYPE_p_MEDCoupling__DataArrayFloat; + } +%} + +%include "ShapeRecognImpl.i" + +%pythoncode %{ +import os +__filename=os.environ.get('PYTHONSTARTUP') +if __filename and os.path.isfile(__filename): + with open(__filename) as __fp: + exec(__fp.read()) +%} diff --git a/src/ShapeRecogn/Swig/ShapeRecognImpl.i b/src/ShapeRecogn/Swig/ShapeRecognImpl.i new file mode 100644 index 000000000..0d894c7ce --- /dev/null +++ b/src/ShapeRecogn/Swig/ShapeRecognImpl.i @@ -0,0 +1,246 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +%{ +#include "ShapeRecognMesh.hxx" +#include "ShapeRecognMeshBuilder.hxx" +#include "Areas.hxx" + + #include +%} + +%feature("unref") ShapeRecognMesh "$this->decrRef();" + +%newobject Areas::getNodeIds; + +%newobject ShapeRecognMeshBuilder::recognize; + +%newobject ShapeRecognMesh::getNodeK1; +%newobject ShapeRecognMesh::getNodeK2; +%newobject ShapeRecognMesh::getNodePrimitiveType; +%newobject ShapeRecognMesh::getNodeNormal; +%newobject ShapeRecognMesh::getAreaId; +%newobject ShapeRecognMesh::getAreaPrimitiveType; +%newobject ShapeRecognMesh::getAreaNormal; +%newobject ShapeRecognMesh::getMinorRadius; +%newobject ShapeRecognMesh::getRadius; +%newobject ShapeRecognMesh::getAngle; +%newobject ShapeRecognMesh::getCenter; +%newobject ShapeRecognMesh::getAxis; +%newobject ShapeRecognMesh::getApex; +%newobject ShapeRecognMeshBuilder::buildNodeWeakDirections; +%newobject ShapeRecognMeshBuilder::buildNodeMainDirections; +%newobject ShapeRecognMeshBuilder::buildAreaAxisPoint; +%newobject ShapeRecognMeshBuilder::buildAreaAffinePoint; + +%rename (ConvertStringToPrimitive) ConvertStringToPrimitiveSwig; +%rename (ConvertPrimitiveToString) ConvertPrimitiveToStringSwig; + +class Areas +{ +public: + double getMinorRadius(mcIdType areaId) const; + double getRadius(mcIdType areaId) const; + double getAngle(mcIdType areaId) const; + // + bool isEmpty(mcIdType areaId) const; + size_t getNumberOfAreas() const; + size_t getNumberOfNodes(mcIdType areaId) const; + int getPrimitiveType(mcIdType areaId) const; + std::string getPrimitiveTypeName(mcIdType areaId) const; + %extend + { + DataArrayIdType *getNodeIds(mcIdType areaId) const + { + const std::vector &res = self->getNodeIds(areaId); + MCAuto< DataArrayIdType > ret( DataArrayIdType::New() ); + ret->alloc(res.size(),1); + std::copy(res.begin(),res.end(),ret->getPointer()); + return ret.retn(); + } + } +private: + Areas(); + ~Areas(); +}; + +using namespace MEDCoupling; + +std::vector AllManagedPrimitivesStr(); + +%inline +{ + std::string ConvertPrimitiveToStringSwig(int type) + { + return ConvertPrimitiveToString(static_cast(type)); + } + + int ConvertStringToPrimitiveSwig(const std::string& type) + { + return static_cast>( ConvertStringToPrimitive(type) ); + } +} + +class ShapeRecognMesh : public RefCountObject +{ +public: + ~ShapeRecognMesh(); + %extend + { + // Node properties + MEDCouplingFieldDouble *getNodeK1() const + { + MEDCouplingFieldDouble *ret = const_cast( self->getNodeK1() ); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *getNodeK2() const + { + MEDCouplingFieldDouble *ret = const_cast( self->getNodeK2() ); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldInt32 *getNodePrimitiveType() const + { + MEDCouplingFieldInt32 *ret = const_cast( self->getNodePrimitiveType() ); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *getNodeNormal() const + { + MEDCouplingFieldDouble *ret = const_cast( self->getNodeNormal() ); + ret->incrRef(); + return ret; + } + + // Area properties + + MEDCouplingFieldInt32 *getAreaId() const + { + MEDCouplingFieldInt32 *ret = const_cast( self->getAreaId() ); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldInt32 *getAreaPrimitiveType() const + { + MEDCouplingFieldInt32 *ret = const_cast( self->getAreaPrimitiveType() ); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *getAreaNormal() const + { + MEDCouplingFieldDouble *ret = const_cast( self->getAreaNormal() ); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *getMinorRadius() const + { + MEDCouplingFieldDouble *ret = const_cast( self->getMinorRadius() ); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *getRadius() const + { + MEDCouplingFieldDouble *ret = const_cast( self->getRadius() ); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *getAngle() const + { + MEDCouplingFieldDouble *ret = const_cast( self->getAngle() ); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *getCenter() const + { + MEDCouplingFieldDouble *ret = const_cast( self->getCenter() ); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *getAxis() const + { + MEDCouplingFieldDouble *ret = const_cast( self->getAxis() ); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *getApex() const + { + MEDCouplingFieldDouble *ret = const_cast( self->getApex() ); + ret->incrRef(); + return ret; + } + } +private: + ShapeRecognMesh(); +}; + +class ShapeRecognMeshBuilder +{ +public: + ShapeRecognMeshBuilder(MEDCoupling::MEDCouplingUMesh *mesh); + ~ShapeRecognMeshBuilder(); + const Areas *getAreas() const; + %extend + { + MEDCouplingFieldDouble *buildNodeWeakDirections() const + { + MCAuto ret = self->buildNodeWeakDirections(); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *buildNodeMainDirections() const + { + MCAuto ret = self->buildNodeMainDirections(); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *buildAreaAxisPoint() const + { + MCAuto ret = self->buildAreaAxisPoint(); + ret->incrRef(); + return ret; + } + + MEDCouplingFieldDouble *buildAreaAffinePoint() const + { + MCAuto ret = self->buildAreaAffinePoint(); + ret->incrRef(); + return ret; + } + + ShapeRecognMesh *recognize() + { + MCAuto ret = self->recognize(); + return ret.retn(); + } + } +}; diff --git a/src/ShapeRecogn/Test/CMakeLists.txt b/src/ShapeRecogn/Test/CMakeLists.txt index 2dc29d57c..a7df4179a 100644 --- a/src/ShapeRecogn/Test/CMakeLists.txt +++ b/src/ShapeRecogn/Test/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2024 CEA, EDF +# Copyright (C) 2024 CEA, EDF # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -32,6 +32,7 @@ INCLUDE_DIRECTORIES( SET(TestShapeRecogn_SOURCES TestShapeRecogn.cxx MathOpsTest.cxx + ShapeRecognTest.cxx PlaneTest.cxx CylinderTest.cxx ConeTest.cxx @@ -42,24 +43,37 @@ SET(TestShapeRecogn_SOURCES SALOME_ACCUMULATE_ENVIRONMENT(MEDCOUPLING_RESOURCE_DIR "${CMAKE_BINARY_DIR}/resources") SALOME_GENERATE_TESTS_ENVIRONMENT(tests_env) -ADD_EXECUTABLE(TestShapeRecogn ${TestShapeRecogn_SOURCES}) -TARGET_LINK_LIBRARIES(TestShapeRecogn shaperecogn InterpKernelTestUtils ${CPPUNIT_LIBRARIES} ${PLATFORM_LIBS}) -INSTALL(TARGETS TestShapeRecogn DESTINATION ${MEDCOUPLING_INSTALL_BINS}) +IF(NOT MEDCOUPLING_MICROMED) + SET(TESTSHAPE_RECOGN0 TestShapeRecogn) + ADD_EXECUTABLE(${TESTSHAPE_RECOGN0} ${TestShapeRecogn_SOURCES}) + TARGET_LINK_LIBRARIES(${TESTSHAPE_RECOGN0} shaperecogn InterpKernelTestUtils medloader ${MEDFILE_C_LIBRARIES} ${HDF5_LIBRARIES} ${CPPUNIT_LIBRARIES} ${PLATFORM_LIBS}) -SET(BASE_TESTS TestShapeRecogn) + INSTALL(TARGETS ${TESTSHAPE_RECOGN0} DESTINATION ${MEDCOUPLING_INSTALL_BINS}) + + #### Test section -FOREACH(test ${BASE_TESTS}) - ADD_TEST(NAME ${test} - COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/MCTestLauncher.py ${CMAKE_CURRENT_BINARY_DIR}/${test}) - SET_TESTS_PROPERTIES(${test} PROPERTIES ENVIRONMENT "${tests_env}") -ENDFOREACH() + SET(TEST_INSTALL_DIRECTORY ${MEDCOUPLING_INSTALL_TESTS}/ShapeRecogn) + SET(BASE_TESTS ${TESTSHAPE_RECOGN0}) -# Application tests + FOREACH(test ${BASE_TESTS}) + ADD_TEST(NAME ${test} + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${test}) + SET_TESTS_PROPERTIES(${test} PROPERTIES ENVIRONMENT "${tests_env}") + ENDFOREACH() + INSTALL(TARGETS ${TESTSHAPE_RECOGN0} DESTINATION ${TEST_INSTALL_DIRECTORY}) -SET(TEST_INSTALL_DIRECTORY ${MEDCOUPLING_INSTALL_TESTS}/ShapeRecogn) -INSTALL(TARGETS TestShapeRecogn DESTINATION ${TEST_INSTALL_DIRECTORY}) -INSTALL(FILES CTestTestfileInstall.cmake - DESTINATION ${TEST_INSTALL_DIRECTORY} - RENAME CTestTestfile.cmake) + SET(TESTSHAPE_RECOGN1 "TestShapeRecogn.py") + SET(test ${TESTSHAPE_RECOGN1}) + SET(testname "TestShapeRecognPy") + ADD_TEST(NAME ${testname} COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/${test}) + SET_TESTS_PROPERTIES(${testname} PROPERTIES ENVIRONMENT "${tests_env}") + INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${TESTSHAPE_RECOGN1} DESTINATION ${TEST_INSTALL_DIRECTORY}) + + INSTALL(FILES CTestTestfileInstall.cmake + DESTINATION ${TEST_INSTALL_DIRECTORY} + RENAME CTestTestfile.cmake) + + #### End Test section +ENDIF(NOT MEDCOUPLING_MICROMED) diff --git a/src/ShapeRecogn/Test/CTestTestfileInstall.cmake b/src/ShapeRecogn/Test/CTestTestfileInstall.cmake index ab32ede46..ae6f6c409 100644 --- a/src/ShapeRecogn/Test/CTestTestfileInstall.cmake +++ b/src/ShapeRecogn/Test/CTestTestfileInstall.cmake @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2024 CEA, EDF +# Copyright (C) 2024 CEA, EDF # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,15 +17,12 @@ # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # -SET(TEST_NAMES - TestShapeRecogn -) +SET(TEST_NAME "${COMPONENT_NAME}_ShapeRecogn") +ADD_TEST(${TEST_NAME} TestShapeRecogn) +SET_TESTS_PROPERTIES( ${TEST_NAME} PROPERTIES LABELS "${COMPONENT_NAME}" TIMEOUT ${TIMEOUT} ) -FOREACH(tfile ${TEST_NAMES}) - SET(TEST_NAME ${COMPONENT_NAME}_${tfile}) - ADD_TEST(${TEST_NAME} python3 MCTestLauncher.py ${tfile}) - SET_TESTS_PROPERTIES(${TEST_NAME} PROPERTIES - LABELS "${COMPONENT_NAME}" - TIMEOUT ${TIMEOUT} - ) -ENDFOREACH() +SET(tfile "TestShapeRecogn.py") +set(BASE_NAME "ShapeRecognPy") +SET(TEST_NAME ${COMPONENT_NAME}_${BASE_NAME}) +ADD_TEST(${TEST_NAME} python3 ${tfile}) +SET_TESTS_PROPERTIES( ${TEST_NAME} PROPERTIES LABELS "${COMPONENT_NAME}" TIMEOUT ${TIMEOUT} ) diff --git a/src/ShapeRecogn/Test/ConeTest.cxx b/src/ShapeRecogn/Test/ConeTest.cxx index d25f4a792..2da2193b7 100644 --- a/src/ShapeRecogn/Test/ConeTest.cxx +++ b/src/ShapeRecogn/Test/ConeTest.cxx @@ -1,24 +1,44 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "ConeTest.hxx" #include "ShapeRecognMeshBuilder.hxx" +#include "ShapeRecognMesh.hxx" #include "Areas.hxx" #include "MathOps.hxx" #include "TestInterpKernelUtils.hxx" // getResourceFile() +#include "ShapeRecognTest.hxx" + using namespace MEDCoupling; void ConeTest::setUp() { std::string file = INTERP_TEST::getResourceFile("ShapeRecognCone.med", 3); - srMesh = new ShapeRecognMeshBuilder(file); + srMesh = BuildShapeRecognMeshBuilderFromFile(file); srMesh->recognize(); areas = srMesh->getAreas(); } void ConeTest::tearDown() { - if (srMesh != 0) - delete srMesh; areas = 0; } diff --git a/src/ShapeRecogn/Test/ConeTest.hxx b/src/ShapeRecogn/Test/ConeTest.hxx index 20b1bef3f..5f6d6a154 100644 --- a/src/ShapeRecogn/Test/ConeTest.hxx +++ b/src/ShapeRecogn/Test/ConeTest.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,12 +17,15 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __CONETEST_HXX__ -#define __CONETEST_HXX__ +#pragma once #include #include +#include "ShapeRecognMeshBuilder.hxx" + +#include + namespace MEDCoupling { class ShapeRecognMeshBuilder; @@ -54,9 +57,7 @@ namespace MEDCoupling void testThirdArea(); private: - ShapeRecognMeshBuilder *srMesh = 0; + std::unique_ptr srMesh; const Areas *areas; }; -}; - -#endif // __CONETEST_HXX__ +} diff --git a/src/ShapeRecogn/Test/CylinderTest.cxx b/src/ShapeRecogn/Test/CylinderTest.cxx index 47f6ac3d3..2e08bdf10 100644 --- a/src/ShapeRecogn/Test/CylinderTest.cxx +++ b/src/ShapeRecogn/Test/CylinderTest.cxx @@ -1,24 +1,44 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "CylinderTest.hxx" #include "ShapeRecognMeshBuilder.hxx" +#include "ShapeRecognMesh.hxx" #include "Areas.hxx" #include "MathOps.hxx" #include "TestInterpKernelUtils.hxx" // getResourceFile() +#include "ShapeRecognTest.hxx" + using namespace MEDCoupling; void CylinderTest::setUp() { std::string file = INTERP_TEST::getResourceFile("ShapeRecognCylinder.med", 3); - srMesh = new ShapeRecognMeshBuilder(file); + srMesh = BuildShapeRecognMeshBuilderFromFile(file); srMesh->recognize(); areas = srMesh->getAreas(); } void CylinderTest::tearDown() { - if (srMesh != 0) - delete srMesh; areas = 0; } diff --git a/src/ShapeRecogn/Test/CylinderTest.hxx b/src/ShapeRecogn/Test/CylinderTest.hxx index a35ac1481..351983b5c 100644 --- a/src/ShapeRecogn/Test/CylinderTest.hxx +++ b/src/ShapeRecogn/Test/CylinderTest.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,12 +17,13 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __CYLINDERTEST_HXX__ -#define __CYLINDERTEST_HXX__ - #include #include +#include "ShapeRecognMeshBuilder.hxx" + +#include + namespace MEDCoupling { class ShapeRecognMeshBuilder; @@ -47,9 +48,7 @@ namespace MEDCoupling void testThirdArea(); private: - ShapeRecognMeshBuilder *srMesh = 0; + std::unique_ptr< ShapeRecognMeshBuilder > srMesh; const Areas *areas; }; -}; - -#endif // __CYLINDERTEST_HXX__ +} diff --git a/src/ShapeRecogn/Test/MathOpsTest.cxx b/src/ShapeRecogn/Test/MathOpsTest.cxx index 033f9921b..6841fe097 100644 --- a/src/ShapeRecogn/Test/MathOpsTest.cxx +++ b/src/ShapeRecogn/Test/MathOpsTest.cxx @@ -1,3 +1,22 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "MathOpsTest.hxx" #include "MathOps.hxx" diff --git a/src/ShapeRecogn/Test/MathOpsTest.hxx b/src/ShapeRecogn/Test/MathOpsTest.hxx index 2988a950b..8cbc635f1 100644 --- a/src/ShapeRecogn/Test/MathOpsTest.hxx +++ b/src/ShapeRecogn/Test/MathOpsTest.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,8 +17,7 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __MATHOPSTEST_HXX__ -#define __MATHOPSTEST_HXX__ +#pragma once #include #include @@ -46,6 +45,4 @@ namespace MEDCoupling static void testComputeAngles(); static void testComputeBaseFromNormal(); }; -}; - -#endif // __MATHOPSTEST_HXX__ +} diff --git a/src/ShapeRecogn/Test/PlaneTest.cxx b/src/ShapeRecogn/Test/PlaneTest.cxx index 6f47f161c..8f41a30f4 100644 --- a/src/ShapeRecogn/Test/PlaneTest.cxx +++ b/src/ShapeRecogn/Test/PlaneTest.cxx @@ -1,24 +1,44 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "PlaneTest.hxx" #include "ShapeRecognMeshBuilder.hxx" +#include "ShapeRecognMesh.hxx" #include "Areas.hxx" #include "MathOps.hxx" #include "TestInterpKernelUtils.hxx" // getResourceFile() +#include "ShapeRecognTest.hxx" + using namespace MEDCoupling; void PlaneTest::setUp() { std::string file = INTERP_TEST::getResourceFile("ShapeRecognPlane.med", 3); - srMesh = new ShapeRecognMeshBuilder(file); + srMesh = BuildShapeRecognMeshBuilderFromFile(file); srMesh->recognize(); areas = srMesh->getAreas(); } void PlaneTest::tearDown() { - if (srMesh != 0) - delete srMesh; areas = 0; } diff --git a/src/ShapeRecogn/Test/PlaneTest.hxx b/src/ShapeRecogn/Test/PlaneTest.hxx index af6eb4755..63e007063 100644 --- a/src/ShapeRecogn/Test/PlaneTest.hxx +++ b/src/ShapeRecogn/Test/PlaneTest.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,12 +17,16 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __PLANETEST_HXX__ -#define __PLANETEST_HXX__ +#pragma once #include #include +#include "ShapeRecognMeshBuilder.hxx" +#include "ShapeRecognTest.hxx" + +#include + namespace MEDCoupling { class ShapeRecognMeshBuilder; @@ -41,9 +45,7 @@ namespace MEDCoupling void testArea(); private: - ShapeRecognMeshBuilder *srMesh = 0; + std::unique_ptr srMesh; const Areas *areas; }; -}; - -#endif // __PLANETEST_HXX__ +} diff --git a/src/ShapeRecogn/Test/ShapeRecognTest.cxx b/src/ShapeRecogn/Test/ShapeRecognTest.cxx new file mode 100644 index 000000000..c9faa14ea --- /dev/null +++ b/src/ShapeRecogn/Test/ShapeRecognTest.cxx @@ -0,0 +1,28 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "ShapeRecognTest.hxx" + +#include "MEDLoader.hxx" + +std::unique_ptr BuildShapeRecognMeshBuilderFromFile(const std::string& fileName, int meshDimRelToMax) +{ + MEDCoupling::MCAuto mesh = MEDCoupling::ReadUMeshFromFile(fileName, 0); + return std::make_unique(mesh); +} \ No newline at end of file diff --git a/src/ShapeRecogn/Test/ShapeRecognTest.hxx b/src/ShapeRecogn/Test/ShapeRecognTest.hxx new file mode 100644 index 000000000..8f1e39e46 --- /dev/null +++ b/src/ShapeRecogn/Test/ShapeRecognTest.hxx @@ -0,0 +1,27 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#pragma once + +#include "ShapeRecognMeshBuilder.hxx" + +#include +#include + +std::unique_ptr BuildShapeRecognMeshBuilderFromFile(const std::string& fileName, int meshDimRelToMax = 0); diff --git a/src/ShapeRecogn/Test/SphereTest.cxx b/src/ShapeRecogn/Test/SphereTest.cxx index 4d7f64ecb..b219e1ad8 100644 --- a/src/ShapeRecogn/Test/SphereTest.cxx +++ b/src/ShapeRecogn/Test/SphereTest.cxx @@ -1,24 +1,44 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "SphereTest.hxx" #include "ShapeRecognMeshBuilder.hxx" +#include "ShapeRecognMesh.hxx" #include "Areas.hxx" #include "MathOps.hxx" #include "TestInterpKernelUtils.hxx" // getResourceFile() +#include "ShapeRecognTest.hxx" + using namespace MEDCoupling; void SphereTest::setUp() { std::string file = INTERP_TEST::getResourceFile("ShapeRecognSphere.med", 3); - srMesh = new ShapeRecognMeshBuilder(file); + srMesh = BuildShapeRecognMeshBuilderFromFile(file); srMesh->recognize(); areas = srMesh->getAreas(); } void SphereTest::tearDown() { - if (srMesh != 0) - delete srMesh; areas = 0; } diff --git a/src/ShapeRecogn/Test/SphereTest.hxx b/src/ShapeRecogn/Test/SphereTest.hxx index 6500def00..833690f3e 100644 --- a/src/ShapeRecogn/Test/SphereTest.hxx +++ b/src/ShapeRecogn/Test/SphereTest.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,12 +17,15 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __SPHERETEST_HXX__ -#define __SPHERETEST_HXX__ +#pragma once #include #include +#include "ShapeRecognMeshBuilder.hxx" + +#include + namespace MEDCoupling { class ShapeRecognMeshBuilder; @@ -41,9 +44,7 @@ namespace MEDCoupling void testArea(); private: - ShapeRecognMeshBuilder *srMesh = 0; + std::unique_ptr srMesh; const Areas *areas; }; -}; - -#endif // __SPHERETEST_HXX__ +} diff --git a/src/ShapeRecogn/Test/TestShapeRecogn.cxx b/src/ShapeRecogn/Test/TestShapeRecogn.cxx index b7cd2af14..8d07cce9d 100644 --- a/src/ShapeRecogn/Test/TestShapeRecogn.cxx +++ b/src/ShapeRecogn/Test/TestShapeRecogn.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public diff --git a/src/ShapeRecogn/Test/TestShapeRecogn.py b/src/ShapeRecogn/Test/TestShapeRecogn.py new file mode 100644 index 000000000..6d6142d96 --- /dev/null +++ b/src/ShapeRecogn/Test/TestShapeRecogn.py @@ -0,0 +1,80 @@ +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- +# Copyright (C) 2024 CEA, EDF +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +import ShapeRecogn as sr +import MEDLoader as ml +from numpy.testing import assert_allclose +import unittest + +def getResourceFile(filename, levelUp = 2): + ROOT_DIR_0 = "MEDCOUPLING_ROOT_DIR" + ROOT_DIR_1 = "MEDCOUPLING_RESOURCE_DIR" + import os + from pathlib import Path + if ROOT_DIR_0 in os.environ: + resourceFile = Path( os.environ[ROOT_DIR_0] ).absolute() / "share" / "resources" / "med" / filename + if resourceFile.exists(): + return f"{resourceFile}" + if ROOT_DIR_1 in os.environ: + resourceFile = Path( os.environ[ROOT_DIR_1].split(":")[-1] ).absolute() / filename + if resourceFile.exists(): + return f"{resourceFile}" + p = Path.cwd() + for i in range(levelUp): + p = p.parent + resourceFile = p / "resources" / filename + if not resourceFile.exists(): + raise RuntimeError( f"getResourceFile: could not open resource test file {filename}" ) + return f"{resourceFile}" + +class MEDCouplingIterativeStatisticsTest(unittest.TestCase): + def testPlane(self): + """ + Direct translation of PlaneTest::testArea + """ + self.assertEqual( sr.AllManagedPrimitivesStr() , ('Plane', 'Sphere', 'Cylinder', 'Cone', 'Torus', 'Unknown') ) + fname = "ShapeRecognPlane.med" + mesh = ml.ReadUMeshFromFile( getResourceFile( fname, 3 ),0) + srMesh = sr.ShapeRecognMeshBuilder( mesh ) + rem = srMesh.recognize() + areas = srMesh.getAreas() + self.assertEqual( areas.getNumberOfAreas(), 1 ) + self.assertEqual( areas.getNumberOfNodes(0), 36 ) + nodeIds = areas.getNodeIds(0) + f = rem.getAreaPrimitiveType() + self.assertEqual( areas.getPrimitiveType(0), 0) + self.assertEqual( areas.getPrimitiveTypeName(0), "Plane") + self.assertEqual( sr.ConvertStringToPrimitive("Plane"),0) + self.assertEqual( sr.ConvertPrimitiveToString(0),"Plane") + f_normal = rem.getNodeNormal() + affinePoint = srMesh.buildAreaAffinePoint().getArray()[ nodeIds[0] ] + normal = f_normal.getArray()[ nodeIds[0] ] + normalRef = sr.DataArrayDouble([0.781525, 0.310606, -0.541056],1,3) + proportion0 = normal[0,0] / normalRef[0,0] + proportion1 = normal[0,1] / normalRef[0,1] + proportion2 = normal[0,2] / normalRef[0,2] + proportion3 = sr.DataArrayDouble.Dot(normal,affinePoint)[0] / sr.DataArrayDouble.Dot(normalRef,affinePoint)[0] + assert_allclose([proportion0,proportion1,proportion2,proportion3],[1.0,1.0,1.0,1.0] , rtol = 1e-5) + angle = sr.DataArrayDouble.CrossProduct(normal,normalRef).magnitude()[0] + assert_allclose( [angle], [0.0], atol = 1e-6 ) + +if __name__ == "__main__": + unittest.main() diff --git a/src/ShapeRecogn/Test/TorusTest.cxx b/src/ShapeRecogn/Test/TorusTest.cxx index 6e24d10ee..fecb7b364 100644 --- a/src/ShapeRecogn/Test/TorusTest.cxx +++ b/src/ShapeRecogn/Test/TorusTest.cxx @@ -1,24 +1,44 @@ +// Copyright (C) 2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "TorusTest.hxx" #include "ShapeRecognMeshBuilder.hxx" #include "Areas.hxx" #include "MathOps.hxx" #include "TestInterpKernelUtils.hxx" // getResourceFile() +#include "ShapeRecognMesh.hxx" + +#include "ShapeRecognTest.hxx" using namespace MEDCoupling; void TorusTest::setUp() { std::string file = INTERP_TEST::getResourceFile("ShapeRecognTorus.med", 3); - srMesh = new ShapeRecognMeshBuilder(file); + srMesh = BuildShapeRecognMeshBuilderFromFile(file); srMesh->recognize(); areas = srMesh->getAreas(); } void TorusTest::tearDown() { - if (srMesh != 0) - delete srMesh; areas = 0; } diff --git a/src/ShapeRecogn/Test/TorusTest.hxx b/src/ShapeRecogn/Test/TorusTest.hxx index 07ae86d3a..65890908a 100644 --- a/src/ShapeRecogn/Test/TorusTest.hxx +++ b/src/ShapeRecogn/Test/TorusTest.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2024 CEA, EDF +// Copyright (C) 2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -17,12 +17,15 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#ifndef __TORUSTEST_HXX__ -#define __TORUSTEST_HXX__ +#pragma once #include #include +#include "ShapeRecognMeshBuilder.hxx" + +#include + namespace MEDCoupling { class ShapeRecognMeshBuilder; @@ -41,9 +44,7 @@ namespace MEDCoupling void testArea(); private: - ShapeRecognMeshBuilder *srMesh = 0; + std::unique_ptr< ShapeRecognMeshBuilder > srMesh; const Areas *areas; }; -}; - -#endif // __TORUSTEST_HXX__ +} -- 2.39.2