Skip to content

Commit 1d8b746

Browse files
authored
Add ImplicitTriangleMesh3 and marchingCubes to Python API (#218)
1 parent 2c1aba4 commit 1d8b746

6 files changed

Lines changed: 113 additions & 10 deletions

File tree

include/jet/implicit_triangle_mesh3.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ class ImplicitTriangleMesh3 final : public ImplicitSurface3 {
2727
public:
2828
class Builder;
2929

30-
ImplicitTriangleMesh3(
31-
const TriangleMesh3Ptr& mesh,
32-
size_t resolutionX,
33-
double margin,
34-
const Transform3& transform = Transform3(),
35-
bool isNormalFlipped = false);
30+
//! Constructs an ImplicitSurface3 with mesh and other grid parameters.
31+
ImplicitTriangleMesh3(const TriangleMesh3Ptr& mesh, size_t resolutionX,
32+
double margin,
33+
const Transform3& transform = Transform3(),
34+
bool isNormalFlipped = false);
3635

3736
virtual ~ImplicitTriangleMesh3();
3837

@@ -55,8 +54,7 @@ class ImplicitTriangleMesh3 final : public ImplicitSurface3 {
5554

5655
BoundingBox3D boundingBoxLocal() const override;
5756

58-
Vector3D closestNormalLocal(
59-
const Vector3D& otherPoint) const override;
57+
Vector3D closestNormalLocal(const Vector3D& otherPoint) const override;
6058

6159
double signedDistanceLocal(const Vector3D& otherPoint) const override;
6260

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Copyright (c) 2018 Doyub Kim
5+
6+
I am making my contributions/submissions to this project solely in my personal
7+
capacity and am not conveying any rights to any intellectual property of any
8+
third parties.
9+
"""
10+
11+
from pyjet import *
12+
import numpy as np
13+
import os
14+
15+
ANIM_NUM_FRAMES = 360
16+
ANIM_FPS = 60
17+
18+
19+
def main():
20+
# Create APIC solver
21+
resX = 100
22+
solver = ApicSolver3(resolution=(resX, resX, resX), domainSizeX=1.0)
23+
solver.useCompressedLinearSystem = True
24+
25+
# Setup emitter
26+
bunny_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../resources/bunny.obj')
27+
bunny_mesh = TriangleMesh3()
28+
bunny_mesh.readObj(bunny_filename)
29+
bunny_sdf = ImplicitTriangleMesh3(mesh=bunny_mesh, resolutionX=64, margin=0)
30+
emitter = VolumeParticleEmitter3(implicitSurface=bunny_sdf, spacing=1.0 / (2 * resX), isOneShot=False)
31+
solver.particleEmitter = emitter
32+
33+
# Convert to surface
34+
grid_size = 1.0 / resX
35+
grid = CellCenteredScalarGrid3((resX, resX, resX), (grid_size, grid_size, grid_size))
36+
37+
def write_surface(frame_cnt, pos):
38+
converter = SphPointsToImplicit3(2.0 * grid_size, 0.5)
39+
converter.convert(pos.tolist(), grid)
40+
surface_mesh = marchingCubes(grid, (grid_size, grid_size, grid_size), (0, 0, 0), 0.0, DIRECTION_ALL)
41+
surface_mesh.writeObj('frame_{:06d}.obj'.format(frame_cnt))
42+
43+
# Make first frame
44+
frame = Frame(0, 1.0 / ANIM_FPS)
45+
46+
for i in range(ANIM_NUM_FRAMES):
47+
print('Frame {:d}'.format(i))
48+
solver.update(frame)
49+
pos = np.array(solver.particleSystemData.positions, copy=False)
50+
write_surface(i, pos)
51+
frame.advance()
52+
53+
54+
if __name__ == '__main__':
55+
Logging.mute()
56+
main()

src/python/implicit_triangle_mesh.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ void addImplicitTriangleMesh3(pybind11::module& m) {
2727
// CTOR
2828
.def(py::init<TriangleMesh3Ptr, size_t, double, Transform3, bool>(),
2929
R"pbdoc(
30-
Constructs a triangle with given `points`, `normals`, and `uvs`.
30+
Constructs an ImplicitSurface3 with mesh and other grid parameters.
3131
)pbdoc",
32-
py::arg("points"), py::arg("normals"), py::arg("uvs"),
32+
py::arg("mesh"), py::arg("resolutionX"), py::arg("margin"),
3333
py::arg("transform") = Transform3(),
3434
py::arg("isNormalFlipped") = false)
3535
.def_property_readonly("grid", &ImplicitTriangleMesh3::grid,

src/python/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "level_set_liquid_solver.h"
5555
#include "level_set_solver.h"
5656
#include "logging.h"
57+
#include "marching_cubes.h"
5758
#include "particle_emitter.h"
5859
#include "particle_emitter_set.h"
5960
#include "particle_system_data.h"
@@ -305,6 +306,9 @@ PYBIND11_MODULE(pyjet, m) {
305306
addPciSphSolver2(m);
306307
addPciSphSolver3(m);
307308

309+
// Global functions
310+
addMarchingCubes(m);
311+
308312
#ifdef VERSION_INFO
309313
m.attr("__version__") = py::str(VERSION_INFO);
310314
#else

src/python/marching_cubes.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2018 Doyub Kim
2+
//
3+
// I am making my contributions/submissions to this project solely in my
4+
// personal capacity and am not conveying any rights to any intellectual
5+
// property of any third parties.
6+
7+
#include "marching_cubes.h"
8+
#include "pybind11_utils.h"
9+
10+
#include <jet/marching_cubes.h>
11+
#include <jet/scalar_grid3.h>
12+
13+
namespace py = pybind11;
14+
using namespace jet;
15+
16+
void addMarchingCubes(pybind11::module& m) {
17+
m.def("marchingCubes",
18+
[](ScalarGrid3Ptr grid, py::object gridSize, py::object origin,
19+
double isoValue, int bndFlag) -> TriangleMesh3Ptr {
20+
auto mesh = TriangleMesh3::builder().makeShared();
21+
marchingCubes(
22+
grid->constDataAccessor(), objectToVector3D(gridSize),
23+
objectToVector3D(origin), mesh.get(), isoValue, bndFlag);
24+
return mesh;
25+
},
26+
R"pbdoc(
27+
Computes marching cubes and extract triangle mesh from grid.
28+
)pbdoc");
29+
}

src/python/marching_cubes.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2018 Doyub Kim
2+
//
3+
// I am making my contributions/submissions to this project solely in my
4+
// personal capacity and am not conveying any rights to any intellectual
5+
// property of any third parties.
6+
7+
#ifndef SRC_PYTHON_MARCHING_CUBES_H_
8+
#define SRC_PYTHON_MARCHING_CUBES_H_
9+
10+
#include <pybind11/functional.h>
11+
#include <pybind11/pybind11.h>
12+
#include <pybind11/stl.h>
13+
14+
void addMarchingCubes(pybind11::module& m);
15+
16+
#endif // SRC_PYTHON_MARCHING_CUBES_H_

0 commit comments

Comments
 (0)