Shapes

Shapes are basic geometric elements used for constructive area geometry trees.

class pcsg.shape.Shape

Informational base class of Shape objects. This empty class is implemented for type matching purposes.

dimensions

Number of dimensions is always 2 for Shapes.

Circle

../_images/b26285d7b502.png

Circle created with diameter = 1

../_images/9c2c35f0fec3.png

Circle created with radius = 1

Example: Circle created with diameter = 1

../_images/827cf2c774fc.png
from pcsg import shape

item = shape.Circle (diameter = 1)

Example: Circle created with radius = 1

../_images/7e4937edc6b2.png
from pcsg import shape

item = shape.Circle (radius = 1)
class pcsg.shape.Circle(radius: Optional[float] = None, diameter: Optional[float] = None, name: Optional[str] = None, attributes: Optional[pcsg.attributes.Attributes] = None)

Bases: pcsg.tree.Item, pcsg.shape.Shape

A Circle is a basic Shape class.

The Circle can be definied with the parameter radius or diameter. Only the radius will be stored. The Circles center is at coordinate (0, 0).

radius

Radius of the circle

Square

../_images/1d4651300f4d.png

Square created with size = (1, 2)

../_images/d4386058aaa2.png

Square created with size = (2, 1)

../_images/85667df49579.png

Square created with size = 2

Example: Square created with size = (1, 2)

../_images/cad321b3732a.png
from pcsg import shape

item = shape.Square (size = (1, 2))

Example: Square created with size = (2, 1)

../_images/ad49378baccf.png
from pcsg import shape

item = shape.Square (size = (2, 1))

Example: Square created with size = 2

../_images/b710b3876de5.png
from pcsg import shape

item = shape.Square (size = 2)
class pcsg.shape.Square(size: tuple = (1, 1), name: Optional[str] = None, attributes: Optional[pcsg.attributes.Attributes] = None)

Bases: pcsg.tree.Item, pcsg.shape.Shape

A Square is a basic Shape class.

The Square is defined by a two dimensional size tuple. The Squares center is at coordinate (0, 0).

size

Size of Square as tuple containing (width: float, height: float).

Polygon

../_images/d448c01766ca.png

Simple Polygon

../_images/49211d89c902.png

Polygon with a hole

../_images/5470c8539b9c.png

Polygon with a multiple holes

../_images/8999cec3c4d2.png

Polygons can be created by rasterizing any Shape class

../_images/d0a651f6f380.png
../_images/8ca1678092c5.png

Rasterizing a shape retruns a group of Polygons when the geometry renders to disjunct areas

../_images/94147e14945f.png

Rasterizing a shape retruns Empty if it containes no areas

Example: Simple Polygon

../_images/2d3eaeeef319.png
from pcsg import shape

item = shape.Polygon (
    points = (
                (-1, 0),
                (-1, 1),
                (1, 1)
             )
)

Example: Polygon with a hole

../_images/e3771f28a663.png
from pcsg import shape

item = shape.Polygon (
    points =  (
                # points for outer shape:
                (-1, 0),
                (-1, 1),
                (1, 1),
                # points for cutout:
                (-0.8, 0.2),
                (-0.8, 0.8),
                (-0.3, 0.8)
              ),
    outer =   (0, 1, 2),
    cutouts = ((3, 4, 5),)
)

Example: Polygon with a multiple holes

../_images/679a6982a9b7.png
from pcsg import shape

item = shape.Polygon (
    points =  (
                # points for outer shape:
                (-1, 0),
                (-1, 1),
                (1, 1),
                # points for first cutout:
                (-0.8, 0.2),
                (-0.8, 0.8),
                (-0.3, 0.8),
                # points for second cutout:
                (0, 0.8),
                (0.5, 0.8),
                (0, 0.6)
              ),
    outer =   (0, 1, 2),
    cutouts = ((3, 4, 5), (6, 7, 8))
)

Example: Polygons can be created by rasterizing any Shape class

../_images/32fec83b5104.png
import pcsg

# create outline
outline = pcsg.transform.Rotate (
    pcsg.shape.Square (size = 1),
    rz = 5
)

# rasterize outline to Polygon
item = pcsg.shape.Polygon.fromShape (
    outline,
    attributes
)

Example: Rasterizing a shape retruns a group of Polygons when the geometry renders to disjunct areas

../_images/4cb80c95c664.png
../_images/7ab433fcb3b5.png
import pcsg

# create a composed solid
volume = pcsg.tree.Group (
    children = (
        pcsg.transform.Translate (
            pcsg.solid.Sphere (1.1),
            x = -0.7,
            z = -0.8
        ),
        pcsg.transform.Translate (
            pcsg.solid.Sphere (0.9),
            x = 0.7,
            z = -0.8
        )
    )
)

# project solid to x/y plane
projection = pcsg.shape.Projection (
    volume,
    cut = True
)

# get polygons from projection
polygons = pcsg.shape.Polygon.fromShape (
    projection,
    attributes
)

# result will be a group node.
assert isinstance (polygons, pcsg.tree.Group)

# colorize polygons inside group
polygons = pcsg.algorithms.distributeColors (polygons)

Example: Rasterizing a shape retruns Empty if it containes no areas

../_images/015016ab4145.png
import pcsg

# create empty projection
solid = pcsg.shape.Projection (
    pcsg.transform.Translate (
        pcsg.solid.Sphere (1.1),
        z = -2
    ),
    cut = True
)

# get empty polygon
item = pcsg.shape.Polygon.fromShape (
    solid,
    attributes
)

# result will be an empty node.
assert isinstance (item, pcsg.tree.Empty)
class pcsg.shape.Polygon(points: Optional[object] = None, outer: Optional[list] = None, cutouts: Optional[list] = None, interpolation: pcsg.shape.PolygonInterpolation = <PolygonInterpolation.Linear: 1>, name: Optional[str] = None, attributes: Optional[pcsg.attributes.Attributes] = None)

Bases: pcsg.tree.Item, pcsg.shape.Shape

A Polygon is a basic Shape class.

The Polygon is defined by a list of point forming a closed shape.

cutouts

Point array of tuples containing point indices used for cut out shapes.

static fromShape(shape: pcsg.tree.Item, rasterizingAttributes: Optional[pcsg.attributes.Attributes] = None, name: Optional[str] = None, attributes: Optional[pcsg.attributes.Attributes] = None)

Create a Polygon by rasterizing a two dimensional csg tree Item. The accuracy for rasterizing is calculated from rasterizingAttributes. When the shape has disjunct areas, a Group of Polygons will be returned. When the shape has no renderable items, Empty will be returned.

interpolation

Interpolation type used for rendering.

outer

Point indices used for outer shape.

points

Tuple of two dimensional vectors describing all points of the Polygon.

Interpolation types for Polygons:

class pcsg.shape.PolygonInterpolation(value)
Cubic = 3

The polygon will be redered by a cubic interpolation. For calculating the interpolated curve the preceeding and following point will be taken into account.

Linear = 1

The polygon will be redered by connecting all points by straight lines.

Quadratic = 2

The polygon will be redered by a quadratic interpolation. For calculating the interpolated curve the following point will be taken into account.

Bezier

../_images/85278e8222e6.png

Shape from bezier curves

Example: Shape from bezier curves

../_images/66f672f1227c.png
from pcsg import shape

item = shape.Bezier (
    curves = (
                ((-2, -1.3), (-2, 0), (-2, 0), (0, 1.3)  ),
                ((0, 1.3),   (1, 2),  (2, 1),  (2, -1.3) ),
                ((2, -1.3),  (0, -1), (0, -1), (-2, -1.3))
             )
)
class pcsg.shape.Bezier(curves: Optional[object] = None, name: Optional[str] = None, attributes: Optional[pcsg.attributes.Attributes] = None)

Bases: pcsg.tree.Item, pcsg.shape.Shape

A Bezier shape is a basic Shape class.

The Bezier shape is defined by a list of bezier curves forming a closed shape. Each bezier curve is a tuple containing the start point, two control points and the end point of the segment.

curves

List containing a tuple of 4 points for each bezier segment. A segment consists of four 2-dimensional points: a start point, two control points and an end point.

isClosed()

Returns True when the Bezier shape has a closed outline, False otherwise.

Projection

../_images/241bcf30743e.png
../_images/77432fdee107.png

Orthogonal projection of solid

../_images/d0a651f6f380.png
../_images/5e41b45b17eb.png

Intersection of solid with x/y plane

Example: Orthogonal projection of solid

../_images/e095917576fe.png
../_images/e2fc9f25c00e.png
import pcsg

# create solid
volume = pcsg.transform.Rotate (
    pcsg.solid.Cylinder (height = 3, radius1 = 2, radius2 = 0),
    rx = -45,
    ry = 10
)

# orthogonal projection to x/y plane
item = pcsg.shape.Projection (
    volume,
    cut = False
)

Example: Intersection of solid with x/y plane

../_images/4cb80c95c664.png
../_images/b6d755e6ecbb.png
import pcsg

# create solid
solid = pcsg.boolean.Union (
    children = (
        pcsg.transform.Translate (
            pcsg.solid.Sphere (1.1),
            x = -0.7,
            z = -0.8
        ),
        pcsg.transform.Translate (
            pcsg.solid.Sphere (0.9),
            x = 0.7,
            z = -0.8
        )
    )
)

# projection by cutting with x/y plane
item = pcsg.shape.Projection (
    solid,
    cut = True
)
class pcsg.shape.Projection(children: Optional[object] = None, cut: bool = True, name: Optional[str] = None, attributes: Optional[pcsg.attributes.Attributes] = None)

Bases: pcsg.tree.Node, pcsg.shape.Shape

Create a Shape by projection of a Solid on the x/y plane.

children

Tuple containing a single child. The child must be a three dimensional csg tree item.

cut

When cut is True, the intersection of the child object with the x/y plane with z=0 will be calculated. When cut is False, an orthogonal projection will be calculated.

Complex

class pcsg.shape.Complex(name: Optional[str] = None, attributes: Optional[pcsg.attributes.Attributes] = None)

Bases: pcsg.tree.Item, pcsg.shape.Shape

Base class for implementing complex Shapes.

_toBezier(rasterizingAttributes: pcsg.attributes.Attributes)

To be implemented by child class optionally: convert a Complex to a Bezier shape. This function may return None when a conversion to a Bezier shape is not possible.

_toPolygon(rasterizingAttributes: pcsg.attributes.Attributes)

To be implemented by child class: convert a Complex to a Polygon.

rasterize(rasterizingAttributes: pcsg.attributes.Attributes, tryAsBezierShape=False)

When tryAsBezierShape is True, the Complex is rendered as Bezier curve if possible, otherwise it will be rendered as a Polygon.

Polar

../_images/a54f4f6f2a8e.png

Shape defined by polar curve function

Example: Shape defined by polar curve function

../_images/754b7f17edee.png
import math
from pcsg import shape

def curveFunction (phi):
    c1 = (1 + 0.9 * math.cos (8 * phi))
    c2 = (1 + 0.1 * math.cos (24 * phi))
    c3 = (0.9 + 0.1 * math.cos (200 * phi))
    c4 = (1 + math.sin (phi))
    return (1 + 5 * c1 * c2 * c3 * c4) * 0.1

item = shape.Polar (
    curveFunction
)
class pcsg.shape.Polar(func=None, name: Optional[str] = None, attributes: Optional[pcsg.attributes.Attributes] = None)

Bases: pcsg.shape.Complex

A Shape defined by a polar curve function.

func

Function mapping a parameter phi in range 0 to 2*pi to a a radius.