pmneila / PyMCubes

Marching cubes (and related tools) for Python
BSD 3-Clause "New" or "Revised" License
692 stars 87 forks source link

box sdf doesn't work #24

Closed asyniakov closed 4 years ago

asyniakov commented 4 years ago

Hello,

I found out that if I use box function as sdf, it creates only 3 sides of cube.

def box(): x, y, z = np.mgrid[:100, :100, :100]

def func(x, y, z):
    b0 = 1
    b1 = 1
    b2 = 1

    q0 = abs(x) - b0
    q1 = abs(y) - b1
    q2 = abs(z) - b2

    d0 = max(q0, 0)
    d1 = max(q1, 0)
    d2 = max(q2, 0)

    d = sqrt(d0*d0 + d1*d1 + d2*d2)

    return d + min(max(q0,max(q1,q2)),0)

vertices2, triangles2 = mcubes.marching_cubes_func(
    (0, 0, 0),
    (99, 99, 99),
    100, 100, 100,
    func, 0.0
)

sdf function is taken from here https://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm (I've translated to python code, though I did it on C++ side)

pmneila commented 4 years ago

Hi @asyniakov

The cube you are trying to build is centered at (0, 0, 0) and has a side of 2 units. Since you are running marching cubes between 0 and 99, you are just building one corner of the entire cube. It is missing 3 sides because those sides are not there.

Change your bounds to something like

vertices2, triangles2 = mcubes.marching_cubes_func(
    (-2, -2, -2),
    (2, 2, 2),
    5, 5, 5,
    func, 0.0
)

and it will build the entire cube.

asyniakov commented 4 years ago

sorry, you are 100% correct.