From 8d229bbd1fba2147e9b0db8a4b0166484f4a5348 Mon Sep 17 00:00:00 2001 From: Louis Gombert Date: Thu, 22 Aug 2024 12:21:27 +0000 Subject: [PATCH] WASM binding v1 --- src/CMakeLists.txt | 5 ++ src/SMESH/SMESH_Mesh.hxx | 1 - src/webassembly/Bindings.cxx | 109 +++++++++++++++++++++++++++++++++ src/webassembly/CMakeLists.txt | 39 ++++++++++++ src/webassembly/app.html | 55 +++++++++++++++++ 5 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 src/webassembly/Bindings.cxx create mode 100644 src/webassembly/CMakeLists.txt create mode 100644 src/webassembly/app.html diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f0d3aa105..e44c25c2f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -64,10 +64,15 @@ IF(SALOME_BUILD_GUI) ) ENDIF(SALOME_BUILD_GUI) +IF (DEFINED EMSCRIPTEN) + SET(SUBDIRS_WASM webassembly) +ENDIF() + SET(SUBDIRS ${SUBDIRS_COMMON} ${SUBDIRS_CGNS} ${SUBDIRS_GUI} + ${SUBDIRS_WASM} ) FOREACH(dir ${SUBDIRS}) diff --git a/src/SMESH/SMESH_Mesh.hxx b/src/SMESH/SMESH_Mesh.hxx index 99817219e..b6b8cb4cf 100644 --- a/src/SMESH/SMESH_Mesh.hxx +++ b/src/SMESH/SMESH_Mesh.hxx @@ -186,7 +186,6 @@ class SMESH_EXPORT SMESH_Mesh SMESH_Mesh* FindMesh( int meshId ) const; SMESHDS_Mesh * GetMeshDS() { return _meshDS; } - const SMESHDS_Mesh * GetMeshDS() const { return _meshDS; } SMESH_Gen *GetGen() { return _gen; } diff --git a/src/webassembly/Bindings.cxx b/src/webassembly/Bindings.cxx new file mode 100644 index 000000000..6e20ea64d --- /dev/null +++ b/src/webassembly/Bindings.cxx @@ -0,0 +1,109 @@ +#include "GEOMImpl_Gen.hxx" +#include "SMESHDS_Mesh.hxx" +#include "SMESH_ControlsDef.hxx" +#include "SMESH_Gen.hxx" +#include "SMESH_Mesh.hxx" +#include "StdMeshers_Hexa_3D.hxx" +#include "StdMeshers_NumberOfSegments.hxx" +#include "StdMeshers_Quadrangle_2D.hxx" +#include "StdMeshers_Regular_1D.hxx" + +#include + +/** + * Compute the total volume of the mesh + */ +double getTotalVolume(SMESH::Controls::Volume &volControl, SMESH_Mesh *mesh) { + std::cout << "Getting total volume" << std::endl; + double vol = 0; + SMDS_Mesh* ds = static_cast(mesh->GetMeshDS()); + std::cout << "DS has " << ds->NbElements() << " elements" << std::endl; + volControl.SetMesh(ds); + for (int elemID = 0; elemID <= ds->NbElements(); elemID++) { + vol += volControl.GetValue(elemID); + } + return vol; +} + +const std::string addHypothesis(SMESH_Mesh &mesh, TopoDS_Shape &shape, int id) { + std::string e; + mesh.AddHypothesis(shape, id, &e); + return e; +} + +bool compute(SMESH_Gen& gen, SMESH_Mesh* mesh, TopoDS_Shape& shape) { + return gen.Compute(*mesh, shape); +} + +// Required wrapping because SMESH_Mesh::GetMeshDS has 2 overloads, one const +// and one not const, so the reference cannot decide which one to choose +// SMDS_Mesh *getMeshDS(SMESH_Mesh &mesh) { +// return static_cast(mesh.GetMeshDS()); +// } + +// void setMesh(SMESH::Controls::Volume &vol, SMDS_Mesh* mesh) { +// vol.SetMesh(mesh); +// } + +EMSCRIPTEN_BINDINGS(smesh) { + // OCCT bindings + emscripten::class_("CAS_Shape").constructor<>(); + + // GEOM Bindings + emscripten::class_("GEOM_Gen") + .constructor<>() + .function("GetI3DPrimOperations", &GEOMImpl_Gen::GetI3DPrimOperations, + emscripten::allow_raw_pointers()); + + // CAS Pointer + emscripten::class_("Handle_GEOM_Object") + .function("get", &Handle_GEOM_Object::get, + emscripten::allow_raw_pointers()); + + emscripten::class_("GEOM_Object") + .function("GetValue", &GEOM_Object::GetValue); + + emscripten::class_("GEOM_PrimOperations") + .function("MakeBoxDXDYDZ", &GEOMImpl_I3DPrimOperations::MakeBoxDXDYDZ); + + // SMESH Bindings + emscripten::class_("SMESH_Gen") + .constructor<>() + .function("CreateMesh", &SMESH_Gen::CreateMesh, + emscripten::allow_raw_pointers()) + .function("Compute", &compute, + emscripten::allow_raw_pointers()); + + emscripten::class_("SMESH_DS_Mesh") + .function("NbElements", &SMESHDS_Mesh::NbElements); + emscripten::class_("SMESH_SMDS_Mesh"); + + emscripten::class_("SMESH_Hypothesis") + .class_function("IsStatusFatal", &SMESH_Hypothesis::IsStatusFatal); + + emscripten::class_("SMESH_Mesh") + .function("ShapeToMesh", &SMESH_Mesh::ShapeToMesh) + .function("AddHypothesis", &addHypothesis, + emscripten::allow_raw_pointers()); + // .function("GetMeshDS", &getMeshDS, emscripten::allow_raw_pointers()); + + // Meshers + emscripten::class_("SMESH_Meshers_Regular_1D") + .constructor(); + emscripten::class_( + "SMESH_Meshers_NumberOfSegments") + .constructor() + .function("SetNumberOfSegments", + &StdMeshers_NumberOfSegments::SetNumberOfSegments); + emscripten::class_("SMESH_Meshers_Quadrangle_2D") + .constructor(); + emscripten::class_("SMESH_Meshers_Hexa_3D") + .constructor(); + + // Measurements + emscripten::class_("SMESH_Controls_Volume") + .constructor<>() + // .function("SetMesh", &setMesh, emscripten::allow_raw_pointers()) + // .function("GetValue", &SMESH::Controls::Volume::GetValue) + .function("GetTotal", &getTotalVolume, emscripten::allow_raw_pointers()); +} diff --git a/src/webassembly/CMakeLists.txt b/src/webassembly/CMakeLists.txt new file mode 100644 index 000000000..41d05230d --- /dev/null +++ b/src/webassembly/CMakeLists.txt @@ -0,0 +1,39 @@ +add_executable(smeshjs Bindings.cxx) + +set_target_properties(smeshjs PROPERTIES + OUTPUT_NAME smesh +) + +target_link_libraries(smeshjs PRIVATE SalomeIDLKernel GEOMImpl SMESHDS SMESHimpl StdMeshers SMESHEngine) +target_include_directories(smeshjs PRIVATE + ${OpenCASCADE_INCLUDE_DIR} + ${KERNEL_INCLUDE_DIRS} + ${KERNEL_INCLUDE_DIRS} + ${MEDFILE_INCLUDE_DIRS} + ${MEDCOUPLING_INCLUDE_DIRS} + ${PROJECT_SOURCE_DIR}/src/Controls + ${PROJECT_SOURCE_DIR}/src/SMDS + ${PROJECT_SOURCE_DIR}/src/SMESH + ${PROJECT_SOURCE_DIR}/src/SMESHDS + ${PROJECT_SOURCE_DIR}/src/StdMeshers + ${PROJECT_SOURCE_DIR}/src/SMESHUtils +) +target_link_options(smeshjs PRIVATE + "$,-Oz,-O0>" + "--bind" + "--closure 1" + "SHELL:-sEXPORT_NAME=smesh" + "SHELL:-sALLOW_MEMORY_GROWTH=1" + "SHELL:-sEMULATE_FUNCTION_POINTER_CASTS=0" + "SHELL:-sMODULARIZE=1" + "SHELL:-sWASM=1" + "SHELL:-sFORCE_FILESYSTEM" + "SHELL:-sEXPORTED_RUNTIME_METHODS=FS" + "SHELL:-sWASM_BIGINT" + "SHELL:-sNO_DISABLE_EXCEPTION_CATCHING" +) + +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/app.html" + "${CMAKE_BINARY_DIR}/src/webassembly/index.html" + COPYONLY) diff --git a/src/webassembly/app.html b/src/webassembly/app.html new file mode 100644 index 000000000..ed9366dbb --- /dev/null +++ b/src/webassembly/app.html @@ -0,0 +1,55 @@ + + + + + + + SMESH Web + + + + +
+
+ + + + +