Skip to content

Commit c37f49f

Browse files
committed
🧇 Add cut with polyline supports & bump npm packages to version 0.6.4.
1 parent 8b9ba5f commit c37f49f

File tree

10 files changed

+571
-44
lines changed

10 files changed

+571
-44
lines changed

examples/web/threejs/editor/js/Sidebar.Object.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,14 @@ function SidebarObject( editor ) {
253253
const result = editor.mrmesh.cutMeshWithPolylineImpl( curMeshWrapper.mesh, floatVec );
254254
// const result = curMeshWrapper.cutMeshWithPolylineImpl( floatVec );
255255

256-
const newVertices = result.innerMesh.vertices;
257-
const newIndices = new Uint32Array( result.innerMesh.indices );
258-
showMesh( newVertices, newIndices );
256+
const innerVertices = result.innerMesh.vertices;
257+
const innerIndices = new Uint32Array(result.innerMesh.indices);
258+
259+
const outerVertices = result.outerMesh.vertices;
260+
const outerIndices = new Uint32Array(result.outerMesh.indices);
261+
262+
showMesh( innerVertices, innerIndices );
263+
showMesh( outerVertices, outerIndices );
259264
break;
260265

261266
case 'wasmOpSegmentByPoints':

examples/web/threejs/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/web/threejs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "three-meshsdk-editor",
3-
"version": "0.6.3",
3+
"version": "0.6.4",
44
"description": "MeshMaster - ThreeJS editor for `meshsdk`.",
55
"keywords": [
66
"threejs",

source/MRJavaScript/MRContoursCut.cpp

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,7 @@ class BoolResults {
3636
}
3737
};
3838

39-
/**
40-
*@brief Cut mesh with polyline
41-
*
42-
* 1. Project points of polyline to mesh
43-
* 2. Convert result vector to `cutMesh()` input type
44-
* 3. Do `cutMesh()` (it works only with contours without self-intersections)
45-
*
46-
* @param mesh
47-
* @param coordinates Must be closed
48-
* @param coordinatesLength
49-
* @return val
50-
*/
51-
val cutMeshWithPolylineImpl( Mesh& mesh, const std::vector<float>& coordinates )
39+
val cutMeshWithPolylineImplTest( Mesh& mesh, const std::vector<float>& coordinates )
5240
{
5341
std::vector<Vector3f> polyline;
5442

@@ -121,22 +109,15 @@ val cutMeshWithPolylineImpl( Mesh& mesh, const std::vector<float>& coordinates )
121109
}
122110
}
123111

124-
// FIXME:
125-
// auto [innerMesh, outerMesh] = returnParts_( mesh, cutResults.resultCut );
126-
127-
auto innerBitSet = fillContourLeft( mesh.topology, cutResults.resultCut );
128-
Mesh innerMesh = mesh.cloneRegion( innerBitSet );
129-
112+
auto [innerMesh, outerMesh] = MRJS::returnParts( mesh, cutResults.resultCut );
130113
val innerMeshData = MRJS::exportMeshMemoryView( innerMesh );
131-
// val outerMeshData = MRJS::exportMeshMemoryView( outerMesh );
132-
133-
114+
val outerMeshData = MRJS::exportMeshMemoryView( outerMesh );
134115

135116

136117
val obj = val::object();
137118
obj.set( "success", true );
138119
obj.set( "innerMesh", innerMeshData );
139-
// obj.set( "outerMesh", outerMeshData );
120+
obj.set( "outerMesh", outerMeshData );
140121
obj.set( "jsTestProjectedPoint", jsTestProjectedPoint );
141122
obj.set( "jsTestProjectedContour", jsTestProjectedContour );
142123
obj.set( "jsTestCutPoints", jsTestCutPoints );
@@ -153,6 +134,78 @@ val cutMeshWithPolylineImpl( Mesh& mesh, const std::vector<float>& coordinates )
153134
}
154135
}
155136

137+
/**
138+
*@brief Cut mesh with polyline
139+
*
140+
* 1. Project points of polyline to mesh
141+
* 2. Convert result vector to `cutMesh()` input type
142+
* 3. Do `cutMesh()` (it works only with contours without self-intersections)
143+
*
144+
* @param mesh
145+
* @param coordinates Must be closed
146+
* @param coordinatesLength
147+
* @return val
148+
*/
149+
val cutMeshWithPolylineImpl( Mesh& mesh, const std::vector<float>& coordinates )
150+
{
151+
std::vector<Vector3f> polyline;
152+
153+
int coordinatesLength = coordinates.size();
154+
if (coordinatesLength % 3 != 0) {
155+
val obj = val::object();
156+
obj.set( "success", false );
157+
obj.set( "error", "Coordinates length must be a multiple of 3!" );
158+
159+
return obj;
160+
}
161+
162+
polyline.reserve( coordinatesLength / 3 );
163+
164+
for ( size_t i = 0; i < coordinatesLength; i += 3 )
165+
{
166+
polyline.emplace_back( coordinates[i], coordinates[i + 1], coordinates[i + 2] );
167+
}
168+
Polyline3 initialPolyline;
169+
initialPolyline.addFromPoints( polyline.data(), polyline.size(), true );
170+
171+
std::vector<MeshTriPoint> projectedPolyline;
172+
projectedPolyline.reserve( initialPolyline.points.size() );
173+
MeshPart m = MeshPart( mesh, nullptr );
174+
175+
mesh.getAABBTree(); // Create tree in parallel before loop
176+
for ( Vector3f pt : initialPolyline.points )
177+
{
178+
MeshProjectionResult mpr = findProjection( pt, m );
179+
projectedPolyline.push_back( mpr.mtp );
180+
}
181+
182+
auto meshContour = convertMeshTriPointsToMeshContour( mesh, projectedPolyline );
183+
if ( meshContour )
184+
{
185+
CutMeshResult cutResults = cutMesh( mesh, { *meshContour } );
186+
187+
auto [innerMesh, outerMesh] = MRJS::returnParts( mesh, cutResults.resultCut );
188+
val innerMeshData = MRJS::exportMeshMemoryView( innerMesh );
189+
val outerMeshData = MRJS::exportMeshMemoryView( outerMesh );
190+
191+
192+
val obj = val::object();
193+
obj.set( "success", true );
194+
obj.set( "innerMesh", innerMeshData );
195+
obj.set( "outerMesh", outerMeshData );
196+
197+
return obj;
198+
} else {
199+
std::string error = meshContour.error();
200+
201+
val obj = val::object();
202+
obj.set( "success", false );
203+
obj.set( "error", "convertMeshTriPointsToMeshContour: " + error );
204+
205+
return obj;
206+
}
207+
}
208+
156209
/**
157210
*@brief Cut and extrude mesh with polyline
158211
*
@@ -285,4 +338,5 @@ EMSCRIPTEN_BINDINGS( ContoursCutModule )
285338
.field( "fbsWithContourIntersections", &CutMeshResult::fbsWithContourIntersections );
286339

287340
function( "cutMeshWithPolylineImpl", &cutMeshWithPolylineImpl );
341+
function( "cutMeshWithPolylineImplTest", &cutMeshWithPolylineImplTest );
288342
}

source/MRJavaScript/MRUtils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ std::vector<Vector3f> parseJSCoordinates( const std::vector<float>& coordinates
117117
*/
118118
std::pair<Mesh, Mesh> returnParts( const Mesh& mesh, const std::vector<EdgePath>& cut )
119119
{
120-
Mesh innerMesh;
120+
// NOTE: This works!
121+
// auto innerBitSet = fillContourLeft( mesh.topology, cut );
122+
// Mesh innerMesh = mesh.cloneRegion( innerBitSet );
123+
124+
Mesh innerMesh;
121125
auto innerBitSet = fillContourLeft( mesh.topology, cut );
122126
innerMesh.addMeshPart( {mesh, &innerBitSet} );
123127

wasm/meshsdk/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wasm/meshsdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alpinebuster/meshsdk",
3-
"version": "0.6.3",
3+
"version": "0.6.4",
44
"description": "MeshSDK: JS/TS binding for the meshlib.",
55
"type": "module",
66
"main": "lib/index.js",

0 commit comments

Comments
 (0)