visualisation de mesh dans plotly #1

Open
opened 2020-07-21 15:55:24 +00:00 by guillaumeredoules · 0 comments
import numpy as np
from stl import mesh  # pip install numpy-stl
import plotly.graph_objects as go


def stl2mesh3d(path):
    stl_mesh = mesh.Mesh.from_file(path)
    # stl file from: https://github.com/stephenyeargin/stl-files/blob/master/AT%26T%20Building.stl
    stl_mesh.vectors.shape

    # stl_mesh is read by nympy-stl from a stl file; it is  an array of faces/triangles (i.e. three 3d points) 
    # this function extracts the unique vertices and the lists I, J, K to define a Plotly mesh3d
    p, q, r = stl_mesh.vectors.shape #(p, 3, 3)
    # the array stl_mesh.vectors.reshape(p*q, r) can contain multiple copies of the same vertex;
    # extract unique vertices from all mesh triangles
    vertices, ixr = np.unique(stl_mesh.vectors.reshape(p*q, r), return_inverse=True, axis=0)
    I = np.take(ixr, [3*k for k in range(p)])
    J = np.take(ixr, [3*k+1 for k in range(p)])
    K = np.take(ixr, [3*k+2 for k in range(p)])

    x, y, z = vertices.T
    colorscale= [[0, '#e5dee5'], [1, '#e5dee5']]        
    mesh3D = go.Mesh3d(
            x=x,
            y=y,
            z=z, 
            i=I, 
            j=J, 
            k=K, 
            flatshading=False,
            colorscale=colorscale, 
            intensity=z, 
            #name='AT&T',
            showscale=False)

    return mesh3D




fig = go.Figure(data=[stl2mesh3d("CAO.stl")])
fig.write_html("visu.html")
```python import numpy as np from stl import mesh # pip install numpy-stl import plotly.graph_objects as go def stl2mesh3d(path): stl_mesh = mesh.Mesh.from_file(path) # stl file from: https://github.com/stephenyeargin/stl-files/blob/master/AT%26T%20Building.stl stl_mesh.vectors.shape # stl_mesh is read by nympy-stl from a stl file; it is an array of faces/triangles (i.e. three 3d points) # this function extracts the unique vertices and the lists I, J, K to define a Plotly mesh3d p, q, r = stl_mesh.vectors.shape #(p, 3, 3) # the array stl_mesh.vectors.reshape(p*q, r) can contain multiple copies of the same vertex; # extract unique vertices from all mesh triangles vertices, ixr = np.unique(stl_mesh.vectors.reshape(p*q, r), return_inverse=True, axis=0) I = np.take(ixr, [3*k for k in range(p)]) J = np.take(ixr, [3*k+1 for k in range(p)]) K = np.take(ixr, [3*k+2 for k in range(p)]) x, y, z = vertices.T colorscale= [[0, '#e5dee5'], [1, '#e5dee5']] mesh3D = go.Mesh3d( x=x, y=y, z=z, i=I, j=J, k=K, flatshading=False, colorscale=colorscale, intensity=z, #name='AT&T', showscale=False) return mesh3D fig = go.Figure(data=[stl2mesh3d("CAO.stl")]) fig.write_html("visu.html") ```
Sign in to join this conversation.
No Label
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: guillaumeredoules/redoules.github.io#1
No description provided.