Surface flattening
Blocks with flat top surface
import os
import json
from compas.datastructures import Mesh
from compas.geometry import intersection_line_plane
from compas_rhino.artists import MeshArtist
from compas_cloud import Proxy
from compas_rv2.rhino import ErrorHandler
errorHandler = ErrorHandler(title="Server side Error", showLocalTraceback=False)
proxy = Proxy(errorHandler=errorHandler, background=False)
# proxy.restart()
bestfit = proxy.function('compas.geometry.bestfit_plane_numpy')
HERE = os.path.dirname(__file__)
FILE_I = os.path.join(HERE, 'blocks.json')
FILE_O = os.path.join(HERE, 'blocksflat.json')
with open(FILE_I, 'r') as f:
blocks = [Mesh.from_data(data) for data in json.load(f)]
for block in blocks:
bottom = block.face_vertices(0)
top = block.face_vertices(1)[::-1]
bottom_points = block.vertices_attributes('xyz', keys=bottom)
top_points = block.vertices_attributes('xyz', keys=top)
plane = bestfit(top_points)
top_new = []
for a, b in zip(bottom_points, top_points):
b = intersection_line_plane((a, b), plane)
top_new.append(b)
for vertex, point in zip(top, top_new):
block.vertex_attributes(vertex, 'xyz', point)
with open(FILE_O, 'w+') as f:
data = [block.to_data()for block in blocks]
json.dump(data, f)
artist = MeshArtist(None, layer="RV2::BlocksFlat")
artist.clear_layer()
for block in blocks:
artist.mesh = block
artist.draw_faces(color=(0, 255, 0), join_faces=True)
artist.redraw()
Last updated