1
2

3D model

Posted at
import numpy as np
from stl import mesh
import plotly.graph_objects as go

# STLファイルの読み込み
your_mesh = mesh.Mesh.from_file('3LandSat9Parts.stl')

# 頂点座標の取得
x, y, z = your_mesh.vectors.reshape(-1, 3).T

# 三角形の頂点インデックスの作成
i, j, k = np.arange(0, len(x), 3), np.arange(1, len(x), 3), np.arange(2, len(x), 3)

# Plotlyの3D図形オブジェクトの作成
mesh3D = go.Mesh3d(
    x=x, y=y, z=z,
    i=i, j=j, k=k,
    color='slategray',
    opacity=1,
)

# レイアウトの設定
layout = go.Layout(
    scene=dict(
        xaxis=dict(title='X軸', range=[-100, 100]),
        yaxis=dict(title='Y軸', range=[-100, 100]),
        zaxis=dict(title='Z軸', range=[-100, 100]),
        aspectmode='cube'
    ),
    margin=dict(l=0, r=0, b=0, t=0),
    width=1000,
    height=1000,
    hoverlabel_font_size=20
)

# 図の作成と表示
fig = go.Figure(data=[mesh3D], layout=layout)
fig.show()


import numpy as np
import plotly.graph_objects as go
import pywavefront
import logging

# 警告を抑制
logging.getLogger('pywavefront').setLevel(logging.ERROR)

def rotate_vertices(vertices, angle_x, angle_y, angle_z):
    # 回転行列の作成
    Rx = np.array([[1, 0, 0],
                   [0, np.cos(angle_x), -np.sin(angle_x)],
                   [0, np.sin(angle_x), np.cos(angle_x)]])
    
    Ry = np.array([[np.cos(angle_y), 0, np.sin(angle_y)],
                   [0, 1, 0],
                   [-np.sin(angle_y), 0, np.cos(angle_y)]])
    
    Rz = np.array([[np.cos(angle_z), -np.sin(angle_z), 0],
                   [np.sin(angle_z), np.cos(angle_z), 0],
                   [0, 0, 1]])
    
    R = np.dot(Rz, np.dot(Ry, Rx))
    
    # 頂点に回転を適用
    rotated_vertices = np.dot(vertices, R.T)
    return rotated_vertices

# OBJファイルの読み込み
obj = pywavefront.Wavefront('GlobalHawk/GlobalHawkOBJ.obj', collect_faces=True)

# 頂点とファセット(面)データの取得
vertices = np.array(obj.vertices)
faces = np.array([face for mesh in obj.mesh_list for face in mesh.faces])

# 回転角度の設定(ラジアン)
angle_x = np.pi / 4  # X軸周りに45度回転
angle_y = np.pi / 6  # Y軸周りに30度回転
angle_z = np.pi / 3  # Z軸周りに60度回転

# 頂点を回転
rotated_vertices = rotate_vertices(vertices, angle_x, angle_y, angle_z)

# 3Dメッシュオブジェクトの作成
mesh3D = go.Mesh3d(
    x=rotated_vertices[:, 0],
    y=rotated_vertices[:, 1],
    z=rotated_vertices[:, 2],
    i=faces[:, 0],
    j=faces[:, 1],
    k=faces[:, 2],
    color='slategray',
    opacity=1,
)

# サンプルの散布図データの生成(ランダムな点)
num_points = 100
x = np.random.randn(num_points) * 500
y = np.random.randn(num_points) * 500
z = np.random.randn(num_points) * 500

# 散布図オブジェクトの作成
scatter3D = go.Scatter3d(
    x=x, y=y, z=z,
    mode='markers',
    marker=dict(
        size=5,
        color=z,
        colorscale='Viridis',
        opacity=0.8
    )
)

# レイアウトの設定
layout = go.Layout(
    scene=dict(
        xaxis=dict(title='X軸', range=[-500, 500]),
        yaxis=dict(title='Y軸', range=[-500, 500]),
        zaxis=dict(title='Z軸', range=[-500, 500]),
        aspectmode='cube'
    ),
    margin=dict(l=0, r=0, b=0, t=0),
    width=1000,
    height=1000,
    hoverlabel_font_size=20
)

# 図の作成
fig = go.Figure(data=[mesh3D, scatter3D], layout=layout)

# 図の表示
fig.show()
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2