Skip to content

Commit

Permalink
use result update function
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvanmele committed Feb 29, 2024
1 parent 3e91e0e commit dcd3f5e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
31 changes: 13 additions & 18 deletions docs/examples/example_mesh_constrained_on-arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

constraints = [None] * mesh.number_of_vertices()
for vertex in mesh.vertices_where(x=5):
if vertex in fixed:
continue
constraints[vertex] = constraint
fixed.append(vertex)

Expand All @@ -44,37 +46,30 @@

result = dr_constrained_numpy(indata=inputdata, constraints=constraints)

for vertex in mesh.vertices():
mesh.vertex_attributes(vertex, "xyz", result.xyz[vertex])
result.update_mesh(mesh)

# =============================================================================
# Visualization
# =============================================================================

forcecolor = Color.green().darkened(50)

viewer = App()
viewer.view.camera.position = [-7, -10, 5]
viewer.view.camera.look_at([5, 5, 2])

viewer.add(mesh)
viewer.add(arch.to_polyline(), linecolor=Color.cyan(), linewidth=3)

for vertex in mesh.vertices():
for vertex in fixed:
point = Point(*mesh.vertex_coordinates(vertex))
residual = Vector(*result.residuals[vertex])
residual = Vector(*result.residuals[vertex]) * 0.1

if vertex in fixed:
ball = Sphere(radius=0.1, point=point)
ball = Sphere(radius=0.1, point=point).to_brep()
line = Line(point, point - residual)
ballcolor = Color.blue() if constraints[vertex] else Color.red()

if constraints[vertex]:
viewer.add(ball.to_brep(), facecolor=Color.blue())
else:
viewer.add(ball.to_brep(), facecolor=Color.red())

viewer.add(
Line(point, point - residual * 0.1),
linecolor=Color.green().darkened(50),
linewidth=3,
)

viewer.add(arch.to_polyline(), linecolor=Color.cyan(), linewidth=3)
viewer.add(ball, facecolor=ballcolor)
viewer.add(line, linecolor=forcecolor, linewidth=3)

viewer.run()
21 changes: 21 additions & 0 deletions src/compas_dr/numdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,24 @@ def __init__(self, xyz, q, forces, lengths, residuals):
self.forces = forces
self.lengths = lengths
self.residuals = residuals

def update_mesh(self, mesh, vertex_index=None):
# type: (compas.datastructures.Mesh, dict[int, int] | None) -> None
"""Update the geometry of a mesh with the results.
Parameters
----------
mesh
vertex_index
Returns
-------
None
"""
if not vertex_index:
vertex_index = {vertex: index for index, vertex in enumerate(mesh.vertices())}

for vertex in mesh.vertices():
index = vertex_index[vertex]
mesh.vertex_attributes(vertex, "xyz", self.xyz[index])

0 comments on commit dcd3f5e

Please sign in to comment.