clean up JS emscripten bindings

This commit is contained in:
Louis Gombert 2024-08-30 08:34:47 +00:00
parent dd63353c2b
commit 622292a27e
4 changed files with 43 additions and 27 deletions

View File

@ -10,14 +10,11 @@
#include <emscripten/bind.h>
/**
* 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<SMDS_Mesh *>(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);
@ -35,16 +32,6 @@ 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<SMDS_Mesh *>(mesh.GetMeshDS());
// }
// void setMesh(SMESH::Controls::Volume &vol, SMDS_Mesh* mesh) {
// vol.SetMesh(mesh);
// }
EMSCRIPTEN_BINDINGS(smesh) {
// OCCT bindings
emscripten::class_<TopoDS_Shape>("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_<SMESHDS_Mesh>("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_<StdMeshers_Regular_1D>("SMESH_Meshers_Regular_1D")
.constructor<int, SMESH_Gen *>();
@ -103,7 +87,5 @@ EMSCRIPTEN_BINDINGS(smesh) {
// Measurements
emscripten::class_<SMESH::Controls::Volume>("SMESH_Controls_Volume")
.constructor<>()
// .function("SetMesh", &setMesh, emscripten::allow_raw_pointers())
// .function("GetValue", &SMESH::Controls::Volume::GetValue)
.function("GetTotal", &getTotalVolume, emscripten::allow_raw_pointers());
}

View File

@ -18,8 +18,9 @@ target_include_directories(smeshjs PRIVATE
${PROJECT_SOURCE_DIR}/src/StdMeshers
${PROJECT_SOURCE_DIR}/src/SMESHUtils
)
target_link_options(smeshjs PRIVATE
"$<IF:$<CONFIG:Release>,-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)

View File

@ -10,8 +10,6 @@
</head>
<body>
<section class="section">
</section>
<script type="text/javascript" src="smesh.js"></script>
<script type="text/javascript">

17
src/webassembly/server.py Normal file
View File

@ -0,0 +1,17 @@
#!/usr/bin/env python3
from http.server import HTTPServer, SimpleHTTPRequestHandler
import socketserver
import sys
class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers (self):
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
SimpleHTTPRequestHandler.end_headers(self)
if __name__ == '__main__':
PORT = 8080
httpd = socketserver.TCPServer(("", PORT), CORSRequestHandler)
print("Listening on port {}. Press Ctrl+C to stop.".format(PORT))
httpd.serve_forever()