import math
import numpy as np
import stl
# Define the dimensions of the pergola
WIDTH = 4.0 # meters
DEPTH = 3.0 # meters
HEIGHT = 2.5 # meters
# Define the geometry of the pergola
POST_RADIUS = 0.1 # meters
BEAM_RADIUS = 0.05 # meters
RESOLUTION = 30
# Create the mesh data
vertices = []
faces = []
# Define the vertices of the posts
for i in range(4):
x = (i % 2) * WIDTH
y = (i // 2) * DEPTH
z = 0.0
vertices.append(np.array([x - POST_RADIUS, y - POST_RADIUS, z]))
vertices.append(np.array([x - POST_RADIUS, y + POST_RADIUS, z]))
vertices.append(np.array([x + POST_RADIUS, y + POST_RADIUS, z]))
vertices.append(np.array([x + POST_RADIUS, y - POST_RADIUS, z]))
# Define the vertices of the beams
for i in range(2):
x = i * WIDTH
z = HEIGHT
for j in range(RESOLUTION):
angle = math.radians(float(j) / RESOLUTION * 180.0)
y = (DEPTH / 2.0) * math.cos(angle)
vertices.append(np.array([x - BEAM_RADIUS, y, z]))
vertices.append(np.array([x + BEAM_RADIUS, y, z]))
# Define the faces of the mesh
for i in range(4):
i1 = 4 * i
i2 = 4 * i + 1
i3 = 4 * i + 2
i4 = 4 * i + 3
faces.append([i1, i2, i4])
faces.append([i2, i3, i4])
for i in range(2):
i1 = 16 + 2 * i * RESOLUTION
for j in range(RESOLUTION):
i2 = i1 + j
i3 = i1 + (j + 1) % RESOLUTION
i4 = i1 + RESOLUTION + j
i5 = i1 + RESOLUTION + (j + 1) % RESOLUTION
faces.append([i2, i3, i5])
faces.append([i2, i5, i4])
# Create the mesh object
mesh_data = np.zeros(len(faces), dtype=stl.mesh.Mesh.dtype)
for i, f in enumerate(faces):
for j in range(3):
mesh_data['vectors'][i][j] = vertices[f[j]]
mesh = stl.mesh.Mesh(mesh_data)
# Write the mesh to an STL file
mesh.save('pergola.stl')