From 622292a27e08d284314ccafc6f163e537908e606 Mon Sep 17 00:00:00 2001 From: Louis Gombert Date: Fri, 30 Aug 2024 08:34:47 +0000 Subject: [PATCH] clean up JS emscripten bindings --- src/webassembly/Bindings.cxx | 28 +++++----------------------- src/webassembly/CMakeLists.txt | 23 +++++++++++++++++++++-- src/webassembly/app.html | 2 -- src/webassembly/server.py | 17 +++++++++++++++++ 4 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 src/webassembly/server.py diff --git a/src/webassembly/Bindings.cxx b/src/webassembly/Bindings.cxx index 6e20ea64d..2d150d7ab 100644 --- a/src/webassembly/Bindings.cxx +++ b/src/webassembly/Bindings.cxx @@ -10,14 +10,11 @@ #include -/** - * Compute the total volume of the mesh - */ +// Define easier interfaces to use in Javascript + 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; + SMDS_Mesh *ds = static_cast(mesh->GetMeshDS()); volControl.SetMesh(ds); for (int elemID = 0; elemID <= ds->NbElements(); elemID++) { vol += volControl.GetValue(elemID); @@ -31,20 +28,10 @@ const std::string addHypothesis(SMESH_Mesh &mesh, TopoDS_Shape &shape, int id) { return e; } -bool compute(SMESH_Gen& gen, SMESH_Mesh* mesh, TopoDS_Shape& shape) { +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<>(); @@ -71,8 +58,7 @@ EMSCRIPTEN_BINDINGS(smesh) { .constructor<>() .function("CreateMesh", &SMESH_Gen::CreateMesh, emscripten::allow_raw_pointers()) - .function("Compute", &compute, - emscripten::allow_raw_pointers()); + .function("Compute", &compute, emscripten::allow_raw_pointers()); emscripten::class_("SMESH_DS_Mesh") .function("NbElements", &SMESHDS_Mesh::NbElements); @@ -85,8 +71,6 @@ EMSCRIPTEN_BINDINGS(smesh) { .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(); @@ -103,7 +87,5 @@ EMSCRIPTEN_BINDINGS(smesh) { // 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 index 41d05230d..1a58c4df0 100644 --- a/src/webassembly/CMakeLists.txt +++ b/src/webassembly/CMakeLists.txt @@ -18,8 +18,9 @@ target_include_directories(smeshjs PRIVATE ${PROJECT_SOURCE_DIR}/src/StdMeshers ${PROJECT_SOURCE_DIR}/src/SMESHUtils ) + target_link_options(smeshjs PRIVATE - "$,-Oz,-O0>" + "-O0" "--bind" "--closure 1" "SHELL:-sEXPORT_NAME=smesh" @@ -33,7 +34,25 @@ target_link_options(smeshjs PRIVATE "SHELL:-sNO_DISABLE_EXCEPTION_CATCHING" ) +target_compile_options(smeshjs PRIVATE + "-Oz" "-flto" +) + configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/app.html" - "${CMAKE_BINARY_DIR}/src/webassembly/index.html" + "${CMAKE_BINARY_DIR}/webassembly/index.html" COPYONLY) + +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/server.py" + "${CMAKE_BINARY_DIR}/webassembly/server.py" + COPYONLY) + +install(FILES + ${CMAKE_BINARY_DIR}/webassembly/index.html + ${CMAKE_BINARY_DIR}/webassembly/server.py + ${CMAKE_BINARY_DIR}/webassembly/server.py + ${CMAKE_BINARY_DIR}/src/webassembly/smesh.wasm + ${CMAKE_BINARY_DIR}/src/webassembly/smesh.js + + DESTINATION webassembly) diff --git a/src/webassembly/app.html b/src/webassembly/app.html index ed9366dbb..a65f0cd4a 100644 --- a/src/webassembly/app.html +++ b/src/webassembly/app.html @@ -10,8 +10,6 @@ -
-