-
Notifications
You must be signed in to change notification settings - Fork 137
new JS Mesh class #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new JS Mesh class #272
Changes from all commits
97950bf
c164efa
3ef4fb1
c107564
be649c1
4b77e24
2dab35b
df3e458
16abc37
606e47d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
// limitations under the License. | ||
|
||
#include <emscripten/bind.h> | ||
#include <emscripten/val.h> | ||
|
||
using namespace emscripten; | ||
|
||
|
@@ -55,6 +56,48 @@ std::vector<SimplePolygon> ToPolygon( | |
return simplePolygons; | ||
} | ||
|
||
val GetMeshJS(const Manifold& manifold) { | ||
MeshGL mesh = manifold.GetMeshGL(); | ||
val meshJS = val::object(); | ||
|
||
meshJS.set("triVerts", | ||
val(typed_memory_view(mesh.triVerts.size(), mesh.triVerts.data())) | ||
.call<val>("slice")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a documentation for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, some of their docs are pretty well-hidden: https://emscripten.org/docs/porting/connecting_cpp_and_javascript/em
10000
bind.html?highlight=typed_memory_view#memory-views and Now that I know about this, I'm thinking it would be better to move more of our logic from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a comment. I'm not sure if I'm right, but I feel there is some overhead in using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That may be true, but |
||
meshJS.set("vertPos", | ||
val(typed_memory_view(mesh.vertPos.size(), mesh.vertPos.data())) | ||
.call<val>("slice")); | ||
meshJS.set("vertNormal", val(typed_memory_view(mesh.vertNormal.size(), | ||
mesh.vertNormal.data())) | ||
.call<val>("slice")); | ||
meshJS.set("halfedgeTangent", | ||
val(typed_memory_view(mesh.halfedgeTangent.size(), | ||
mesh.halfedgeTangent.data())) | ||
.call<val>("slice")); | ||
|
||
return meshJS; | ||
} | ||
|
||
MeshGL MeshJS2GL(const val& mesh) { | ||
MeshGL out; | ||
out.triVerts = convertJSArrayToNumberVector<uint32_t>(mesh["triVerts"]); | ||
out.vertPos = convertJSArrayToNumberVector<float>(mesh["vertPos"]); | ||
if (mesh["vertNormal"] != val::undefined()) { | ||
out.vertNormal = convertJSArrayToNumberVector<float>(mesh["vertNormal"]); | ||
} | ||
if (mesh["halfedgeTangent"] != val::undefined()) { | ||
out.halfedgeTangent = | ||
convertJSArrayToNumberVector<float>(mesh["halfedgeTangent"]); | ||
} | ||
return out; | ||
} | ||
|
||
Manifold FromMeshJS(const val& mesh) { return Manifold(MeshJS2GL(mesh)); } | ||
|
||
Manifold Smooth(const val& mesh, | ||
const std::vector<Smoothness>& sharpenedEdges = {}) { | ||
return Manifold::Smooth(MeshJS2GL(mesh), sharpenedEdges); | ||
} | ||
|
||
Manifold Extrude(std::vector<std::vector<glm::vec2>>& polygons, float height, | ||
int nDivisions, float twistDegrees, glm::vec2 scaleTop) { | ||
return Manifold::Extrude(ToPolygon(polygons), height, nDivisions, | ||
|
@@ -158,27 +201,12 @@ EMSCRIPTEN_BINDINGS(whatever) { | |
register_vector<BaryRef>("Vector_baryRef"); | ||
register_vector<glm::vec4>("Vector_vec4"); | ||
|
||
value_object<Mesh>("Mesh") | ||
.field("vertPos", &Mesh::vertPos) | ||
.field("triVerts", &Mesh::triVerts) | ||
.field("vertNormal", &Mesh::vertNormal) | ||
.field("halfedgeTangent", &Mesh::halfedgeTangent); | ||
|
||
class_<MeshGL>("MeshGL") | ||
.constructor<int, int>() | ||
.property("numVert", &MeshGL::numVert) | ||
.property("numTri", &MeshGL::numTri) | ||
.function("vertPos", &MeshGL::vertPos, allow_raw_pointers()) | ||
.function("triVerts", &MeshGL::triVerts, allow_raw_pointers()) | ||
.function("vertNormal", &MeshGL::vertNormal, allow_raw_pointers()); | ||
|
||
class_<Manifold>("Manifold") | ||
.constructor<Mesh>() | ||
.constructor(&FromMeshJS) | ||
.function("add", &Union) | ||
.function("subtract", &Difference) | ||
.function("intersect", &Intersection) | ||
.function("_GetMesh", & 6D4E Manifold::GetMesh) | ||
.function("_GetMeshGL", &Manifold::GetMeshGL) | ||
.function("_GetMeshJS", &GetMeshJS) | ||
.function("refine", &Manifold::Refine) | ||
.function("_Warp", &Warp) | ||
.function("_Transform", &Transform) | ||
|
@@ -204,7 +232,7 @@ EMSCRIPTEN_BINDINGS(whatever) { | |
function("_Cylinder", &Manifold::Cylinder); | ||
function("_Sphere", &Manifold::Sphere); | ||
function("tetrahedron", &Manifold::Tetrahedron); | ||
function("_Smooth", &Manifold::Smooth); | ||
function("_Smooth", &Smooth); | ||
function("_Extrude", &Extrude); | ||
function("_Revolve", &Revolve); | ||
function("_LevelSet", &LevelSetJs); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maybe we should put this into
.gitignore
to reduce noise. Btw you can usecompile_commands.json
instead of manually configuring it, see https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html and https://code.visualstudio.com/docs/cpp/faq-cpp#_how-do-i-get-intellisense-to-work-correctly