Python :: 3D graphics


Чернетка! Стаття не завершена...

K3D-jupyter

import k3d
import trimesh
import numpy as np

plot = k3d.plot(name='toolpath')

# read coordinates of segments:
# ...
# 100.000, 100.000, 100.000
# 26.000, 79.000, 100.000
# 26.000, 79.000, 0.000
# ...
segments = np.genfromtxt('./g-code/out3d.txt', delimiter=',')

# Draw Lines
plt_line = k3d.line(segments, shader='mesh', width=0.1)
plot += plt_line

# Draw Points
plt_points = k3d.points(positions=segments, point_size=0.3, shader='3d', color=0xff00ff)
plot += plt_points

# Draw Mesh
body = trimesh.load_mesh('Part9.stl')
mesh = k3d.mesh(body.vertices, body.faces)
plot += mesh

plot.display()

Open3d

mesh

import open3d as o3d

# Import mesh (stl/obj/...)
mesh = o3d.io.read_triangle_mesh("Part9.stl")
print(mesh)

# Show 3D model
mesh.compute_vertex_normals()
o3d.visualization.draw_geometries([mesh])

voxels

# Voxelization
voxel_grid = o3d.geometry.VoxelGrid.create_from_triangle_mesh(mesh, voxel_size=0.5)
print(voxel_grid)

# Show 3D model
3d.visualization.draw_geometries([voxel_grid])

# Save to file
print(voxel_grid.get_voxels())
o3d.io.write_voxel_grid("voxels.ply", voxel_grid, write_ascii=True, compressed=False, print_progress=True)

cloud points

# Downsample the point cloud with a voxel
pcd = o3d.io.read_point_cloud("voxel_grid.ply")
print(pcd)
downpcd = pcd.voxel_down_sample(voxel_size=0.5)
o3d.visualization.draw_geometries([downpcd],
                                  zoom=0.3412,
                                  front=[0.4257, -0.2125, -0.8795],
                                  lookat=[2.6172, 2.0475, 1.532],
                                  up=[-0.0694, -0.9768, 0.2024])

# write to file by column
pcd= o3d.io.read_point_cloud("cloud_cd.ply")
array=np.asarray(pcd.points)

with open("points.txt", mode='w') as f:
    for i in range(len(array)):
        f.write("%f    "%float(array[i][0].item()))
        f.write("%f    "%float(array[i][1].item()))
        f.write("%f    \n"%float(array[i][2].item()))

Trimesh

import trimesh
import numpy as np
import random

mesh = trimesh.load_mesh('Part9.stl')
print(mesh.bounds)

# ============================== ray-mesh intersection=============================== #

ray_origins = ([[0,0,0]])
ray_directions = ([[0,1,1]])

for i in range(100):
    ray_origins = np.insert(ray_origins, 1, [random.randrange(-100,5), random.randrange(-5,70), 0], axis=0)
    ray_directions = np.insert(ray_directions, 1, [random.randint(0, 2), random.randint(0, 2), 1], axis=0)

# run the mesh-ray query
locations, index_ray, index_tri = mesh.ray.intersects_location(ray_origins=ray_origins, ray_directions=ray_directions)

# ================================== visualization ================================== #

scene = trimesh.Scene()
# Rays (line segments for visualization as Path3D)
rays = trimesh.load_path(np.hstack((ray_origins,ray_origins + ray_directions*30.0)).reshape(-1, 2, 3))
scene.add_geometry(rays)

# Path3D
segments = np.genfromtxt('./g-code/out3d.txt', delimiter=',')
path = trimesh.load_path(segments, color=[0, 0, 255, 125])
scene.add_geometry(path)
# very slowly
#for item in range(1, len(segments)-1):
#    if segments[item-1].tolist() != segments[item].tolist():
#        cylinder = trimesh.creation.cylinder(radius=0.5, segment=([segments[item-1].tolist(), segments[item].tolist()]))
#        cylinder.visual.face_colors = [0, 0, 1., 0.5]
#        scene.add_geometry(cylinder)

# mesh
mesh.unmerge_vertices() # unmerge so viewer doesn't smooth
mesh.visual.face_colors = [255,255,255,255]
mesh.visual.face_colors[index_tri] = [255, 0, 0, 255]
scene.add_geometry(mesh)

# axis
axis = trimesh.creation.axis(origin_size=1.0, transform=None, origin_color=None, axis_radius=0.2, axis_length=15.0)
scene.add_geometry(axis)

# plane
plane = trimesh.creation.box(extents=[50, 50, 0.01])
plane.visual.face_colors = [0, 0, 255, 125]
scene.add_geometry(plane)

# show the visualization
scene.show()