Skip to content

Commit

Permalink
SDF Refactor (#876)
Browse files Browse the repository at this point in the history
* added static LevelSet

* removed Morton codes

* store voxels

* new levelset working

* cleanup

* moved LevelSet to MeshGL

* small updates

* removed sdf library

* updated WASM bindings

* cleanup FindSurface

* updated test

* fix formatting

* loosen test slightly

* off-by-one

* fix sizing

* tweak test
  • Loading branch information
elalish authored Jul 26, 2024
1 parent a93c0c6 commit 3bf7f4b
Show file tree
Hide file tree
Showing 31 changed files with 246 additions and 231 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"program": "${workspaceFolder}/build/test/manifold_test",
"args": [
"--gtest_break_on_failure",
"--gtest_filter=Smooth.RefineQuads"
"--gtest_catch_exceptions=0",
"--gtest_filter=SDF.Gyroid"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build/test",
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"-DMANIFOLD_PYBIND=ON",
"-DMANIFOLD_EXPORT=ON",
"-DMANIFOLD_DEBUG=ON",
"-DMANIFOLD_EXCEPTIONS=ON",
"-DMANIFOLD_PAR=TBB",
"-DCODE_COVERAGE=OFF",
"-DCMAKE_CXX_FLAGS=''" //'-fsanitize=address,undefined'"
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endif()

target_link_libraries(
${PROJECT_NAME}
PRIVATE manifold sdf cross_section
PRIVATE manifold cross_section
)

target_include_directories(${PROJECT_NAME} PUBLIC
Expand Down
1 change: 0 additions & 1 deletion bindings/c/conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "cross_section.h"
#include "manifold.h"
#include "public.h"
#include "sdf.h"
#include "types.h"

ManifoldManifold *to_c(manifold::Manifold *m) {
Expand Down
1 change: 0 additions & 1 deletion bindings/c/include/conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "cross_section.h"
#include "manifold.h"
#include "public.h"
#include "sdf.h"
#include "types.h"

using namespace manifold;
Expand Down
4 changes: 2 additions & 2 deletions bindings/c/include/manifoldc.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ ManifoldMeshGL *manifold_meshgl_merge(void *mem, ManifoldMeshGL *m);
ManifoldMeshGL *manifold_level_set(void *mem,
float (*sdf)(float, float, float, void *),
ManifoldBox *bounds, float edge_length,
float level, void *ctx);
float level, float precision, void *ctx);
ManifoldMeshGL *manifold_level_set_seq(
void *mem, float (*sdf)(float, float, float, void *), ManifoldBox *bounds,
float edge_length, float level, void *ctx);
float edge_length, float level, float precision, void *ctx);

// Manifold Vectors

Expand Down
15 changes: 7 additions & 8 deletions bindings/c/manifoldc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <manifold.h>
#include <manifoldc.h>
#include <public.h>
#include <sdf.h>

#include <vector>

Expand All @@ -38,16 +37,16 @@ namespace {
ManifoldMeshGL *level_set(void *mem,
float (*sdf_context)(float, float, float, void *),
ManifoldBox *bounds, float edge_length, float level,
bool seq, void *ctx) {
float precision, bool seq, void *ctx) {
// Bind function with context argument to one without
using namespace std::placeholders;
std::function<float(float, float, float)> sdf =
std::bind(sdf_context, _1, _2, _3, ctx);
std::function<float(glm::vec3)> fun = [sdf](glm::vec3 v) {
return (sdf(v.x, v.y, v.z));
};
auto mesh = LevelSet(fun, *from_c(bounds), edge_length, level, !seq);
return to_c(new (mem) MeshGL(mesh));
return to_c(new (mem) MeshGL(MeshGL::LevelSet(
fun, *from_c(bounds), edge_length, level, precision, !seq)));
}
} // namespace

Expand Down Expand Up @@ -275,14 +274,14 @@ ManifoldManifold *manifold_warp(void *mem, ManifoldManifold *m,
ManifoldMeshGL *manifold_level_set(void *mem,
float (*sdf)(float, float, float, void *),
ManifoldBox *bounds, float edge_length,
float level, void *ctx) {
return level_set(mem, sdf, bounds, edge_length, level, false, ctx);
float level, float precision, void *ctx) {
return level_set(mem, sdf, bounds, edge_length, level, precision, false, ctx);
}

ManifoldMeshGL *manifold_level_set_seq(
void *mem, float (*sdf)(float, float, float, void *), ManifoldBox *bounds,
float edge_length, float level, void *ctx) {
return level_set(mem, sdf, bounds, edge_length, level, true, ctx);
float edge_length, float level, float precision, void *ctx) {
return level_set(mem, sdf, bounds, edge_length, level, precision, true, ctx);
}

ManifoldManifold *manifold_smooth_by_normals(void *mem, ManifoldManifold *m,
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ nanobind_add_module(
NB_STATIC STABLE_ABI LTO
autogen_docstrings.inl
manifold3d.cpp)
target_link_libraries(manifold3d PRIVATE manifold sdf polygon cross_section)
target_link_libraries(manifold3d PRIVATE manifold polygon cross_section)
target_compile_options(manifold3d PRIVATE ${MANIFOLD_FLAGS} -DMODULE_NAME=manifold3d)
target_compile_features(manifold3d PUBLIC cxx_std_17)
set_target_properties(manifold3d PROPERTIES OUTPUT_NAME "manifold3d")
Expand Down
13 changes: 7 additions & 6 deletions bindings/python/manifold3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "nanobind/stl/tuple.h"
#include "nanobind/stl/vector.h"
#include "polygon.h"
#include "sdf.h"

namespace nb = nanobind;
using namespace manifold;
Expand Down Expand Up @@ -554,20 +553,20 @@ NB_MODULE(manifold3d, m) {
.def_static(
"level_set",
[](const std::function<float(float, float, float)> &f,
std::vector<float> bounds, float edgeLength, float level = 0.0) {
std::vector<float> bounds, float edgeLength, float level = 0.0,
float precision = -1) {
// Same format as Manifold.bounding_box
Box bound = {glm::vec3(bounds[0], bounds[1], bounds[2]),
glm::vec3(bounds[3], bounds[4], bounds[5])};

std::function<float(glm::vec3)> cppToPython = [&f](glm::vec3 v) {
return f(v.x, v.y, v.z);
};
Mesh result =
LevelSet(cppToPython, bound, edgeLength, level, false);
return MeshGL(result);
return MeshGL::LevelSet(cppToPython, bound, edgeLength, level,
precision, false);
},
nb::arg("f"), nb::arg("bounds"), nb::arg("edgeLength"),
nb::arg("level") = 0.0,
nb::arg("level") = 0.0, nb::arg("precision") = -1,
"Constructs a level-set Mesh from the input Signed-Distance Function "
"(SDF) This uses a form of Marching Tetrahedra (akin to Marching "
"Cubes, but better for manifoldness). Instead of using a cubic grid, "
Expand All @@ -587,6 +586,8 @@ NB_MODULE(manifold3d, m) {
"strong effect on performance."
":param level: You can inset your Mesh by using a positive value, or "
"outset it with a negative value."
":param precision: The verts will be within this distance of the "
"real surface."
":return Mesh: This mesh is guaranteed to be manifold."
"Use Manifold.from_mesh(mesh) to create a Manifold")
.def("merge", &MeshGL::Merge, mesh_gl__merge);
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ project(wasm)
add_executable(manifoldjs bindings.cpp)

set_source_files_properties(bindings.cpp PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/bindings.js)
target_link_libraries(manifoldjs manifold sdf cross_section polygon)
target_link_libraries(manifoldjs manifold cross_section polygon)
target_compile_options(manifoldjs PRIVATE ${MANIFOLD_FLAGS})
target_link_options(manifoldjs PUBLIC --pre-js ${CMAKE_CURRENT_SOURCE_DIR}/bindings.js --bind -sALLOW_TABLE_GROWTH=1
-sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction -sMODULARIZE=1 -sEXPORT_ES6=1)
Expand Down
1 change: 0 additions & 1 deletion bindings/wasm/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "helpers.cpp"
#include "manifold.h"
#include "polygon.h"
#include "sdf.h"

using namespace emscripten;
using namespace manifold;
Expand Down
6 changes: 4 additions & 2 deletions bindings/wasm/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,8 @@ Module.setup = function() {
Module.Manifold.difference = manifoldBatchbool('Difference');
Module.Manifold.intersection = manifoldBatchbool('Intersection');

Module.Manifold.levelSet = function(sdf, bounds, edgeLength, level = 0) {
Module.Manifold.levelSet = function(
sdf, bounds, edgeLength, level = 0, precision = -1) {
const bounds2 = {
min: {x: bounds.min[0], y: bounds.min[1], z: bounds.min[2]},
max: {x: bounds.max[0], y: bounds.max[1], z: bounds.max[2]},
Expand All @@ -644,7 +645,8 @@ Module.setup = function() {
const vert = [x, y, z];
return sdf(vert);
}, 'fi');
const out = Module._LevelSet(wasmFuncPtr, bounds2, edgeLength, level);
const out =
Module._LevelSet(wasmFuncPtr, bounds2, edgeLength, level, precision);
removeFunction(wasmFuncPtr);
return out;
};
Expand Down
8 changes: 3 additions & 5 deletions bindings/wasm/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "cross_section.h"
#include "manifold.h"
#include "polygon.h"
#include "sdf.h"

using namespace emscripten;
using namespace manifold;
Expand Down Expand Up @@ -206,12 +205,11 @@ Manifold SetProperties(Manifold& manifold, int numProp, uintptr_t funcPtr) {
return manifold.SetProperties(numProp, f);
}

Manifold LevelSet(uintptr_t funcPtr, Box bounds, float edgeLength,
float level) {
Manifold LevelSet(uintptr_t funcPtr, Box bounds, float edgeLength, float level,
float precision) {
float (*f)(const glm::vec3&) =
reinterpret_cast<float (*)(const glm::vec3&)>(funcPtr);
Mesh m = LevelSet(f, bounds, edgeLength, level);
return Manifold(m);
return Manifold(MeshGL::LevelSet(f, bounds, edgeLength, level, precision));
}

std::vector<Manifold> Split(Manifold& a, Manifold& b) {
Expand Down
6 changes: 5 additions & 1 deletion bindings/wasm/manifold-encapsulated-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,14 @@ export class Manifold {
* performance.
* @param level You can inset your Mesh by using a positive value, or outset
* it with a negative value.
* @param precision Ensure each vertex is within this distance of the true
* surface. Defaults to -1, which will return the interpolated
* crossing-point based on the two nearest grid points. Small positive values
* will require more sdf evaluations per output vertex.
*/
static levelSet(
sdf: (point: Vec3) => number, bounds: Box, edgeLength: number,
level?: number): Manifold;
level?: number, precision?: number): Manifold;

// Transformations

Expand Down
2 changes: 1 addition & 1 deletion samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ target_include_directories(samples
)

target_link_libraries(samples
PUBLIC manifold sdf cross_section
PUBLIC manifold cross_section
)

target_compile_options(samples PRIVATE ${MANIFOLD_FLAGS})
Expand Down
6 changes: 3 additions & 3 deletions samples/src/gyroid_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "manifold.h"
#include "samples.h"
#include "sdf.h"

namespace {
using namespace manifold;
Expand Down Expand Up @@ -47,8 +46,9 @@ namespace manifold {
Manifold GyroidModule(float size, int n) {
auto gyroid = [&](float level) {
const float period = glm::two_pi<float>();
return Manifold(LevelSet(Gyroid(), {glm::vec3(-period), glm::vec3(period)},
period / n, level))
return Manifold(MeshGL::LevelSet(Gyroid(),
{glm::vec3(-period), glm::vec3(period)},
period / n, level))
.Scale(glm::vec3(size / period));
};

Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ add_subdirectory(collider)
add_subdirectory(cross_section)
add_subdirectory(polygon)
add_subdirectory(manifold)
add_subdirectory(sdf)
2 changes: 1 addition & 1 deletion src/manifold/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC
$<INSTALL_INTERFACE:include/${CMAKE_PROJECT_NAME}>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
target_link_libraries(${PROJECT_NAME}
PUBLIC utilities sdf
PUBLIC utilities
PRIVATE $<BUILD_INTERFACE:collider> polygon ${MANIFOLD_INCLUDE} quickhull
)

Expand Down
4 changes: 4 additions & 0 deletions src/manifold/include/manifold.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ struct MeshGL {
MeshGL() = default;
MeshGL(const Mesh& mesh);

static MeshGL LevelSet(std::function<float(glm::vec3)> sdf, Box bounds,
float edgeLength, float level = 0,
float precision = -1, bool canParallel = true);

bool Merge();
};
/** @} */
Expand Down
Loading

0 comments on commit 3bf7f4b

Please sign in to comment.