You have learned Point class in the last session. In this session, we will learn some more geometry classes and visualize them with the plotter.
vector
line
polygon
circle
pointcloud
transformation
1. Vector
A vector is a geometric object that has magnitude and direction. In Python, it could be represented through a list of XYZ components.
my_vector = [1,2,0] # xyz coordinates of the vector
A vector can be added, subtracted, multiplied by another vector. For example, here's the example to add two vectors in Python.
vector1 = [1,2,0]vector2 = [0,1,0]vector3 = [vector1[i]+ vector2[i]for i inrange(3)]print(vector3)
[1, 3, 0]
compas.geometry provides geometric operation functions to simplify the use of basic Python operations. These functions always return native Python objects. The last example can be realized by using compas.geometry.add_vectors.
from compas.geometry import add_vectorsvector1 = [1,2,0]vector2 = [0,1,0]vector3 =add_vectors(vector1, vector2)print(vector3)
COMPAS Vector class which contains useful geometric properties and methods.
Now let's create a Vector object and check its length. In Vector class, length is the attribute of the object.
from compas.geometry import Vectormy_vector =Vector(1, 2, 0)print(my_vector)print(my_vector.length)
Vector(1.000, 2.000, 0.000)
2.23606797749979
A vector could also be constructed from start and end points. COMPAS geometry types could be used interchangeably with native Python types. Both a COMPAS Point object or a list composed of 3 components could be used as input.
Operators such as + or * involving COMPAS geometry objects always return a new COMPAS geometry object. For example, scaling my_vector by 2 can use the class method Vector.scale(n) as well as * 2.
Question: Now you are given three points: point1: [0, 0, 0], point2: [0, 1, 0], point3: [3, 2, 0]. You need to draw three vectors as shown in the following picture.
vector1 and vector3 are in the right location, because they both start from the origin. vector2 is pointing at the right location but it doesn't start from point2. Remember, the vector has only magnitude and direction. We could add the start point in our plotter.
A Polygon represents an ordered collection of points in space connected by straight line segments forming a closed boundary around the interior space. It has a closed boundary that separates its interior from the exterior.
from compas.geometry import Polygonfrom compas_plotters import GeometryPlotter# Construct a regular polygon from a number of sides and a radius.my_polygon = Polygon.from_sides_and_radius_xy(5, 2.0)plotter =GeometryPlotter(show_axes=True)plotter.add(my_polygon, edgecolor=(0, 0, 1), facecolor=(0.7, 0.7, 1.0))plotter.zoom_extents()plotter.show()
from compas.geometry import Plane, Circlefrom compas_plotters import GeometryPlottermy_plane =Plane([2, 1, 0], [0, 0, 1])# center and normalmy_circle =Circle(my_plane, 2)plotter =GeometryPlotter(show_axes=True)plotter.add(my_circle, edgecolor=(0, 0, 1), facecolor=(0, 1, 1), linewidth=3)plotter.zoom_extents()plotter.show()
6. PointCloud
from random import randomfrom compas.geometry import Pointcloudfrom compas.utilities import i_to_rgbfrom compas_plotters import GeometryPlotter# ==============================================================================# Pointcloud Geometry# ==============================================================================pcl = Pointcloud.from_bounds(10, 5, 0, 100)# ==============================================================================# Visualize Plottor# ==============================================================================plotter =GeometryPlotter(show_axes=True)for point in pcl.points: plotter.add(point, facecolor=i_to_rgb(random(), normalize=True))plotter.zoom_extents()plotter.show()
7. Transformation
Transformation means changes in geometric shape, which is represented by a 4x4 transformation matrix. Here we will learn 3 typical transformations in COMPAS: translation, rotation and scale.
A COMPAS geometry object can be transformed by calling the method .transform() or .transformed(). The former modifies the object in place, whereas the latter returns a new object.
7.a: Translation
Translation is to move the geometry, one of the most basic transformation types. The shape, size and orientation of the geometry remain the same.
Rotation is to rotate the geometry by a certain degree.
R = Rotation.from_axis_and_angle([0, 0, 1], math.radians(15))my_polygon.transform(R)
7.c: Scale
Scale is to enlarge or shrink the geometry by a scale factor.
S = Scale.from_factors([0.5, 1, 1])# scale factors along X, Y, Zmy_polygon.transform(S)
7.d: Frame
Frame plays an important role in transformation, especially in 3D. A frame defines a local coordinate system and transformation can be created between different coordinate systems represented by frames.