A. Cablenet and Faces

1. Network & Faces

Network datastructure is capable of maintaining information of nodes and edges. It is efficient to search connectivity between nodes but hard to represent connectivity between faces bounded by edges.

2. Ex: Visualize fabric of your cable-net formwork

Question:

You have created your cable-net. Now you want to visualize the fabric for every face in your cable-net with different colors. (You could start from scratch, or load the cable-net Network that was saved as .json last week.)

Answer:

2.a: Polygon from point list

A polygon is an ordered collection of points connected by straight line segments, which bounds the polygon area, or the face. compas.geometry.Polygon could be used to represent a face.

# ==============================================================================
# Import
# ==============================================================================
from random import random

from compas.geometry import Polygon
from compas.utilities import i_to_rgb
from compas_plotters import GeometryPlotter

# ==============================================================================
# Parameters
# ==============================================================================
row = 5
col = 5
unit_len = 10

# ==============================================================================
# Generating the polygon and Visualization
# ==============================================================================
plotter = GeometryPlotter(show_axes=True)

for i in range(row + 1):
    for j in range(col + 1):
        
        if i != row or i!= col: 
            pt_0_x = i * unit_len
            pt_0_y = j * unit_len
            pt_0 = [pt_0_x, pt_0_y, 0]

            pt_1_x = (i + 1) * unit_len
            pt_1_y = j * unit_len
            pt_1 = [pt_1_x, pt_1_y, 0]

            pt_2_x = (i + 1) * unit_len
            pt_2_y = (j + 1) * unit_len
            pt_2 = [pt_2_x, pt_2_y, 0]

            pt_3_x = i * unit_len
            pt_3_y = (j + 1) * unit_len
            pt_3 = [pt_3_x, pt_3_y, 0]

            polygon = Polygon([pt_0, pt_1, pt_2, pt_3])
            plotter.add(polygon, facecolor=i_to_rgb(random(), normalize=True))

plotter.zoom_extents()
plotter.show()

2.b: Polygon from Network nodes

We can also use the cable-net Network that we created in the last week, and extract the node coordinates to create the polygon.

# ==============================================================================
# Import
# ==============================================================================
import os
from random import seed, random

from compas.datastructures import Network
from compas.geometry import Polygon
from compas.utilities import i_to_rgb
from compas_plotters import GeometryPlotter

# ==============================================================================
# Unserialize
# ==============================================================================
HERE = os.path.dirname(__file__)
FILE = os.path.join(HERE, 'data', 'cablenet.json')

cablenet = Network.from_json(FILE)

# ==============================================================================
# Parameters
# ==============================================================================
row = 5
col = 5

# ==============================================================================
# Generating the polygon and Visualization
# ==============================================================================
plotter = GeometryPlotter(show_axes=True)

for i in range(row + 1):
    for j in range(col + 1):
 
        if i < row and j < col:
            random_seed = seed(i * (row + 1) + j)
            
            pt_0 = cablenet.node_coordinates(i * (row + 1) + j)
            pt_1 = cablenet.node_coordinates((i + 1) * (row + 1) + j)
            pt_2 = cablenet.node_coordinates((i + 1) * (row + 1) + j + 1)
            pt_3 = cablenet.node_coordinates(i * (row + 1) + j + 1)

            polygon = Polygon([pt_0, pt_1, pt_2, pt_3])
            plotter.add(polygon, facecolor=i_to_rgb(random(), normalize=True))

plotter.zoom_extents()
plotter.show()

2.c: Polygon line segments to vectors

Each Polygon is constructed following the counter-clockwise sequence: left-bottom -> right-bottom -> right-top -> left-top. Now turn these line segments into Vector.

for i in range(row + 1):
    for j in range(col + 1):
        
        if i < row and j < col:
            random_seed = seed(i * (row + 1) + j)
            color = i_to_rgb(random(), normalize=True)
            
            pt_0 = cablenet.node_coordinates(i * (row + 1) + j)
            pt_1 = cablenet.node_coordinates((i + 1) * (row + 1) + j)
            pt_2 = cablenet.node_coordinates((i + 1) * (row + 1) + j + 1)
            pt_3 = cablenet.node_coordinates(i * (row + 1) + j + 1)

            vector_1 = Vector.from_start_end(pt_0, pt_1)
            plotter.add(vector_1, point=Point(*pt_0), color=color)
            vector_2 = Vector.from_start_end(pt_1, pt_2)
            plotter.add(vector_2, point=Point(*pt_1), color=color)
            vector_3 = Vector.from_start_end(pt_2, pt_3)
            plotter.add(vector_3, point=Point(*pt_2), color=color)
            vector_4 = Vector.from_start_end(pt_3, pt_0)
            plotter.add(vector_4, point=Point(*pt_3), color=color)

2.d: Scale vectors

Scale and move the vectors a bit so they don't overlap.

vector_1 = Vector.from_start_end(pt_0, pt_1)
vector_1.scale(scale)
vector_1_start = Point(pt_0[0] + 1, pt_0[1] + 1, 0)
plotter.add(vector_1, point=vector_1_start, color=color)

vector_2 = Vector.from_start_end(pt_1, pt_2)
vector_2.scale(scale)
vector_2_start = Point(pt_1[0] - 1, pt_1[1] + 1, 0)
plotter.add(vector_2, point=vector_2_start, color=color)

vector_3 = Vector.from_start_end(pt_2, pt_3)
vector_3.scale(scale)
vector_3_start = Point(pt_2[0] - 1, pt_2[1] - 1, 0)
plotter.add(vector_3, point=Point(*pt_2), color=color)

vector_4 = Vector.from_start_end(pt_3, pt_0)
vector_4.scale(scale)
vector_4_start = Point(pt_3[0] + 1, pt_3[1] - 1, 0)
plotter.add(vector_4, point=vector_4_start, color=color)

3. Conclusion from the exercise

In this exercise, some conclusion could be drawn:

  • a face can be represented by an ordered sequence of points.

  • each edge can be represented by two half-edges with opposite orientation unless it is on the boundary.

Last updated