Code Monkey home page Code Monkey logo

Comments (4)

mikedh avatar mikedh commented on August 25, 2024 1

Hey, I think you want to take a look at mesh.face_adjacency:

In [1]: import trimesh

In [2]: m = trimesh.load('models/featuretype.STL')

In [3]: m
Out[3]: <trimesh.Trimesh(vertices.shape=(1722, 3), faces.shape=(3476, 3), name=`featuretype.STL`)>

In [4]: facet = m.facets[0]

In [5]: facet
Out[5]: 
array([  0, 286, ...  134, 133, 132, 130])

In [7]: import numpy as np

In [8]: facet_mask = np.zeros(len(m.faces), dtype=bool)

In [10]: facet_mask[facet] = True

# get pairs of faces where only *one* of them is included in the facet
In [11]: border = facet_mask[m.face_adjacency].sum(axis=1) == 1

In [12]: border.shape
Out[12]: (5214,)

In [14]: border
Out[14]: array([ True, False, False, ..., False, False, False])

In [15]: border.sum()
Out[15]: 374

# (m, 2) indexes of m.faces
In [16]: adjacent = m.face_adjacency[border]

In [21]: other_faces = adjacent.ravel()[~facet_mask[adjacent.ravel()]]

# make the facet blue
In [22]: m.visual.face_colors[facet] = [0,0,255,255]

# border faces red
In [23]: m.visual.face_colors[other_faces] = [255,0,0,255]

In [24]: m.show(smooth=False)

Screenshot from 2024-04-09 14-21-32

from trimesh.

shayan-nikoo avatar shayan-nikoo commented on August 25, 2024

Thank you for the answer. In case, my mesh has separated components (bodies), how can I only get the components attached to my facet? Sorry didn't mention it clearly in the beginning. This is why I wanted to use trimesh.graph.connected_components.

Is the below correct?
cc_facet = trimesh.graph.connected_components(edges=m.edges[facet], min_len=4)

min_len should be 4 or 3?

cc_mask = np.zeros(len(m.faces), dtype=np.bool)
cc_mask[np.concatenate(cc_mask)] = True

# make the facet blue
m.visual.face_colors[facet] = [0,0,255,255]

# attached facets red
m.visual.face_colors[cc_mask] = [255,0,0,255]   
m.show(smooth=False)

# create a new mesh from selected cc
cc_m = m.copy()
cc_m.update_faces(cc_mask)

from trimesh.

mikedh avatar mikedh commented on August 25, 2024

Have you looked at mesh.split? That might be close to what you want.

from trimesh.

shayan-nikoo avatar shayan-nikoo commented on August 25, 2024

Yes I tried that, but how can I extract from the split output the component of a given facet?

from trimesh.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.