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()