Skip to content
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

No way to opt-out of SimplifyTopology when creating Manifold from MeshGL #679

Closed
wrongbad opened this issue Dec 31, 2023 · 6 comments
Closed

Comments

@wrongbad
Copy link
Contributor

wrongbad commented Dec 31, 2023

So I'm stuck again getting my verts and triangles auto-deleted before I can use them.

This time I prototyped a refine_to_length() function in python using MeshGL interface - if the longest edge is >threshold, cut it in half.

from badcad import *
t = text('yo', size=10, font='monospace')
yo = t.offset(1,'round').extrude(2) - t.extrude(2)
yom = yo.to_mesh()
verts = yom.vert_properties.astype(np.float32)
tris = yom.tri_verts.astype(np.uint32)

def refine_to_length(verts, tris, target=1):
    verts = verts.tolist()
    tris = tris.tolist()
    print(len(tris))
    mids = {}
    i = 0
    while i < len(tris):
        tri = tris[i]
        v = [verts[i] for i in tri]
        dv = v - np.roll(v, 1, 0)
        l = np.linalg.norm(dv, axis=-1)
        mi = np.argmax(l)
        ml = l[mi]
        if ml > target:
            key = (min(tri[mi],tri[mi-1]), max(tri[mi],tri[mi-1]))
            if key not in mids:
                mididx = len(verts)
                midv = [(v[mi][j] + v[mi-1][j])/2 for j in [0,1,2]]
                verts += [midv]
                mids[key] = mididx
            else:
                mididx = mids[key]
            tri2 = [*tri]
            tri2[mi-1] = mididx
            tris += [tri2]
            tri[mi] = mididx
        else:
            i += 1
    print(len(tris))
    return np.array(verts, np.float32), np.array(tris, np.int32)

verts, tris = refine_to_length(verts, tris, 1)

display((verts, tris), wireframe=True)
Screen Shot 2023-12-31 at 11 59 10 AM

But when I import it into a Manifold in order to use for CSG and Warp, it forcibly simplifies back

display(Manifold(manifold3d.Mesh(verts, tris)), wireframe=True)
Screen Shot 2023-12-31 at 12 00 08 PM
@wrongbad wrongbad changed the title No way to opt-out of SimplyTopology when creating Manifold from MeshGL No way to opt-out of SimplifyTopology when creating Manifold from MeshGL Dec 31, 2023
@wrongbad
Copy link
Contributor Author

I'm thinking this might be another case where we should disable simplify by default, since importing Meshes doesn't inherently create redundant faces, and it's more likely the existing mesh structure is intentional by the user.

@wrongbad
Copy link
Contributor Author

On a semi related note, is it possible to tell which triangles are touching the last boolean op surface? I wonder if it makes sense to apply simplification only to affected triangles and leave the rest untouched.

@wrongbad
Copy link
Contributor Author

I found a work-around - assigning a unique face_id for every triangle stops simplification here.

m = manifold3d.Mesh(verts, tris, face_id=np.arange(len(tris)))

@pca006132
Copy link
Collaborator

I do think that this is desirable, and in general we should probably not collapse triangles that are not created due to boolean operations. @elalish what do you think?

@elalish
Copy link
Owner

elalish commented Jan 1, 2024

As a performance thing I do think it may be useful to only look for edge collapses among the newly-created verts in a Boolean. I think that shouldn't even be too hard, since we start by putting all the new verts at the end of the list.

I think assigning unique face IDs is the right approach to the OP issue though. Otherwise the behavior would change when exporting and importing a mesh in the middle of several Boolean ops.

@pca006132
Copy link
Collaborator

Not sure what you mean by the behavior would change when exporting and importing a mesh in the middle of several boolean ops. I think the mesh is still simplified at the end of each boolean operations (before export), and ideally importing the mesh should not require any further simplification? Currently we do have different behavior if we export and import the mesh again, because our simplification is only best-effort and applying it multiple times can have different results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants