|
| 1 | +import random |
| 2 | + |
| 3 | +from compas.colors import Color |
| 4 | +from compas.colors import ColorMap |
| 5 | +from compas.geometry import Line |
| 6 | +from compas.geometry import Point |
| 7 | +from compas.tolerance import Tolerance |
| 8 | +from compas_viewer.scene import Tag |
| 9 | +from compas_viewer.viewer import Viewer |
| 10 | + |
| 11 | +from compas_timber.connections import JointTopology |
| 12 | +from compas_timber.connections import MaxNCompositeAnalyzer |
| 13 | +from compas_timber.elements import Beam |
| 14 | +from compas_timber.model import TimberModel |
| 15 | + |
| 16 | + |
| 17 | +def create_viewer(): |
| 18 | + # draw inflated centerlines |
| 19 | + viewer = Viewer() |
| 20 | + viewer.renderer.camera.far = 1000000.0 |
| 21 | + viewer.renderer.camera.position = [10000.0, 10000.0, 10000.0] |
| 22 | + viewer.renderer.camera.pandelta = 0.05 |
| 23 | + viewer.renderer.rendermode = "ghosted" |
| 24 | + return viewer |
| 25 | + |
| 26 | + |
| 27 | +def create_color_map(keys): |
| 28 | + # map cluster sizes to random colors which are hopefully visually distinct |
| 29 | + cmap = ColorMap.from_palette("imola") |
| 30 | + n = len(keys) |
| 31 | + values = [i / max(n - 1, 1) for i in range(n)] |
| 32 | + random.shuffle(values) |
| 33 | + return {key: cmap(val) for key, val in zip(keys, values)} |
| 34 | + |
| 35 | + |
| 36 | +def main(): |
| 37 | + lines = [ |
| 38 | + Line(Point(x=-10.0, y=-10.0, z=0.0), Point(x=300.0, y=200.0, z=0.0)), |
| 39 | + Line(Point(x=-10.0, y=-10.0, z=0.0), Point(x=-40.0, y=270.0, z=0.0)), |
| 40 | + Line(Point(x=-10.0, y=-10.0, z=0.0), Point(x=0.0, y=20.0, z=160.0)), |
| 41 | + Line(Point(x=45.89488087618746, y=234.15459672257862, z=0.0), Point(x=168.58797240614388, y=-95.31137353132192, z=0.0)), |
| 42 | + Line(Point(x=45.89488087618746, y=234.15459672257862, z=0.0), Point(x=330.0, y=350.0, z=0.0)), |
| 43 | + Line(Point(x=300.0, y=200.0, z=0.0), Point(x=500.0, y=0.0, z=0.0)), |
| 44 | + Line(Point(x=300.0, y=200.0, z=0.0), Point(x=220.0, y=170.0, z=-120.0)), |
| 45 | + Line(Point(x=-10.0, y=-10.0, z=0.0), Point(x=90.0, y=-220.0, z=0.0)), |
| 46 | + Line(Point(x=45.89488087618746, y=234.15459672257862, z=0.0), Point(x=0.0, y=220.0, z=130.0)), |
| 47 | + Line(Point(x=45.89488087618746, y=234.15459672257862, z=0.0), Point(x=0.0, y=260.0, z=-120.0)), |
| 48 | + ] |
| 49 | + |
| 50 | + model = TimberModel() |
| 51 | + |
| 52 | + height, width = (12, 6) |
| 53 | + |
| 54 | + for line in lines: |
| 55 | + beam = Beam.from_centerline(centerline=line, height=height, width=width) |
| 56 | + model.add_element(beam) |
| 57 | + |
| 58 | + # pairs of adjacent beams are connected by GenericJoint instances |
| 59 | + model.connect_adjacent_beams() |
| 60 | + |
| 61 | + # setup the viewer |
| 62 | + viewer = create_viewer() |
| 63 | + |
| 64 | + for joint in model.joints: |
| 65 | + topo_name = JointTopology.get_name(joint.topology).split("_")[-1] |
| 66 | + viewer.scene.add(Tag(text=topo_name, position=joint.location, height=40, color=Color.blue())) |
| 67 | + |
| 68 | + # draw centerline |
| 69 | + for beam in model.beams: |
| 70 | + viewer.scene.add(beam.centerline) |
| 71 | + viewer.scene.add(beam.geometry) |
| 72 | + viewer.scene.add(Tag(text=str(beam.graph_node), position=beam.centerline.midpoint, height=40, color=Color.black())) |
| 73 | + |
| 74 | + tol = Tolerance(absolute=0.01) |
| 75 | + num_of_beams = len(list(model.beams)) |
| 76 | + |
| 77 | + ### find triplets only |
| 78 | + # analyzer = TripletAnalyzer(model, tolerance=Tolerance(absolute=0.01)) |
| 79 | + |
| 80 | + ### find quads only |
| 81 | + # analyzer = QuadAnalyzer(model, tolerance=Tolerance(absolute=0.01)) |
| 82 | + |
| 83 | + ### find all clusters up to n beams, then n-1 beams, then n-2 beams, etc. |
| 84 | + analyzer = MaxNCompositeAnalyzer(model, n=num_of_beams, tolerance=tol) |
| 85 | + |
| 86 | + clusters = analyzer.find() |
| 87 | + |
| 88 | + size_to_color = create_color_map(list(sorted({len(cluster) for cluster in clusters}))) |
| 89 | + for cluster in clusters: |
| 90 | + size = len(cluster) |
| 91 | + color = size_to_color[size] |
| 92 | + viewer.scene.add(cluster.location, pointsize=50, color=color) |
| 93 | + |
| 94 | + viewer.show() |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + main() |
0 commit comments