shapes Package

shapes Package

This package contains the Shape class that underlies everything else, and its composite, MultiShape, along with several factory functions to instantiate these classes, such as Cube. It also contains some algorithms for modifying existing shape instances.

cube Module

Factory functions that return a single Shape instance in shapes similar to or derived from a cube

gloopy.shapes.cube.Cube(radius=None, colors=None)[source]

Return a new Shape, shaped like a cube. One of the platonic solids. Vertices are positioned at radius from the center, meaning the cube edges are sqrt(3*radius^2).

colors may be either an instance of Color, or a sequence of colors, one for each face.

gloopy.shapes.cube.Cuboid(x, y, z, colors=None, source='Cuboid')[source]

Return a new Shape, cuboid of dimensions x, y, z, centered on the origin.

colors may be either an instance of Color, or a sequence of colors, one for each face.

gloopy.shapes.cube.SpaceStation(edge)[source]

Return a new Shape, an space-station from the vintage computer game Elite, a truncated cube with an extra black face for the entrance.

gloopy.shapes.cube.TruncatedCube(edge, truncation=0.67, color1=None, color2=None)[source]

Return a new Shape, a cube with the corners cut off.

truncation varies from 0.0 (a regular cube) to 1.0 (cut off corners start to meet each other at the midpoint of each edge.)

color1’ is for the cube faces. `color2 is for the exposed corners.

cube_groups Module

Factory functions that return a single MultiShape instance formed by composing several cubes

gloopy.shapes.cube_groups.CubeCorners(edge, color1, color2)[source]

Return a new Shape, consisting of a single, large cube, and eight smaller ones at each of its corners.

gloopy.shapes.cube_groups.CubeCross(edge, color1, color2)[source]

Return a new Shape, consisting of a single large cube, and six smaller ones sticking out of each of its faces.

gloopy.shapes.cube_groups.CubeGlob(radius, number, colors)[source]

Return a new Shape consisting of a random glob of cubes arranged in a spherical shell.

gloopy.shapes.cube_groups.RgbCubeCluster(edge, cluster_edge, cube_count, hole=0)[source]

Return a new Shape consisting of a random array of cubes arranged within a large cube-shaped volume. The small cubes are colored by their position in RGB space.

edge: the edge of a small cube

cluster_edge: the edge of the large volume

cube_count: the number of cubes to generate within the volume

hole: if >0, leave an empty hole of this radius in the middle of the
volume.

dodecahedron Module

gloopy.shapes.dodecahedron.Dodecahedron(radius, face_colors=None)[source]

Return a new Shape. One of the platonic solids. Verts are at the given radius.

face_colors may either be a Color which is applied to every face, or a sequence of colors, one for each face.

extrude Module

gloopy.shapes.extrude.extrude(shape, faces=None, offset=0)[source]

Modify the given shape in-place, by extruding the specified faces by offset.

faces is an iterable of integer face indices upon which to operate. If omitted, it defaults to all faces.

Doesn’t work on Multishapes. This should get fixed in a future release.

gloopy.shapes.extrude.extrude_face(shape, face_index, offset)[source]

Modify the given shape in-place, by extruding the specified face ‘offset’.

Doesn’t work on Multishapes. This should get fixed in a future release.

icosahedron Module

gloopy.shapes.icosahedron.Icosahedron(radius, colors)[source]

Return a new Shape. One of the platonic solids. Verters are at the given radius.

face_colors may either be a Color which is applied to every face, or a sequence of colors, one for each face.

multishape Module

class gloopy.shapes.multishape.MultiShape[source]

Bases: object

A composite of multiple Shapes. This allows many shapes to be stuck together and converted into a single Glyph, which means they are rendered as a single call to glDrawElements.

This Multishape provides attributes that make it accessible like a normal shape:

vertices: a sequence of all the vertices in all the child shapes, each one offset and oriented by that child shape’s position and orientation.

faces: a collection of all the Face instances of all the child shapes.

__init__()[source]
add(shape, position=None, orientation=None)[source]

Add shape to this MultiShape’s collection of children. The shape will be offset by position from the center of the MultiShape, and oriented by orientation.

normalize Module

gloopy.shapes.normalize.normalize(shape, length=1)[source]

Modifies the given shape in-place, by normalizing the position of every vertex to lie at length distance from the center. This squishes the shape to make it roughly spherical.

If the shape contains edges which have one face on one side, but more than one face on the other side, then normalizing will result in ugly split seams, through which the interior of the object will be visible.

Doesn’t work on Multishapes. This should get fixed in a future release.

octahedron Module

gloopy.shapes.octahedron.Octahedron(radius, face_colors=None)[source]

Return a new shape. One of the platonic solids. Verts will be at the given radius.

face_colors may either be a Color which is applied to every face, or a sequence of colors, one for each face.

ring Module

gloopy.shapes.ring.Ring(basic_shape, radius, number)[source]

Return a new Shape which is composed of number of basic_shape arranged in a ring of radius.

gloopy.shapes.ring.TriRings(basic_shape, radius, number)[source]

Return a new Shape which is composed of three Rings, one around each of the X, Y and Z axes.

shape Module

class gloopy.shapes.shape.Face(indices, color, shape, source='unknown')[source]

Bases: object

A single flat face that forms part of a Shape. Attributes are the params to the constructor below, plus:

normal: A Vector, perpendicular to the face
__init__(indices, color, shape, source='unknown')[source]

indices: a list of int indices into the parent shape’s vertex list

color: an instance of Color

shape: a reference to the parent Shape

source: a descriptive string. These can be used when writing
algorithms that modify shapes, to select certain faces to operate on.
__getitem__(n)[source]

Return the nth index, as an integer.

__iter__()[source]

Iterate through indices

__len__()[source]

Return the length of indices

centroid[source]

Warning: Not an accurate centroid, just the mean vertex position

get_normal()[source]

Return the unit normal vector at right angles to this face.

Note that the direction of the normal will be reversed if the face’s winding is reversed.

class gloopy.shapes.shape.Shape(vertices, faces, colors, name='unknown')[source]

Bases: object

Defines a polyhedron, a 3D shape with flat faces and straight edges.

__init__(vertices, faces, colors, name='unknown')[source]

vertices: a list of Vector points in 3D space, relative to the shape’s center point.

faces: a list of faces, where each face is a list of integer indices into the vertices list. The referenced vertices of a single face must form a coplanar ring defining the face’s edges. Duplicate indices do not have to be given at the start and end of each face, the closed loop is implied.

colors: a single Color which is applied to every face, or a sequence of colors, one for each face.

name: the ‘source’ attribute to be applied to each face.

See the source for factory functions like Cube() for examples of constructing Shapes.

get_edges()[source]

Return a set of pairs, each pair represents indices that start and end an edge. Contents of each pair is sorted. e.g Tetrahedron: { (0, 1), (1, 2), (0, 2), (0, 3), (1, 3), (2, 3), }

replace_face(index, new_faces)[source]

Replace the face at position ‘index’ in self.faces with the list of Face instances in ‘new_faces’

gloopy.shapes.shape.add_vertex(vertices, new_vert)[source]

Modifies vertices in-place by appending the given new_vert. Returns the index number of the new vertex.

Loads of Shape-modifying algorithms seem to need this function. Can’t make it a method because they often haven’t constructed the shape instance yet.

shape_to_glyph Module

gloopy.shapes.shape_to_glyph.shape_to_glyph(shape)[source]

Return a new Glyph, which contains the geometry of the given shape converted into an indexed vertex array stored in a VBO, ready for rendering in OpenGL as a single draw call.

stellate Module

gloopy.shapes.stellate.stellate(shape, faces=None, height=0)[source]

Modify the given shape in-place. By ‘stellate’ I mean add a new vertex in the middle of the face, raised by ‘height’ out of the plane of the face, and replace the original face by an n-sided pyramid connecting this new vertex to each edge of the original face’s edges.

This isn’t strictly ‘stellation’ as it is technically defined, but is instead a more flexible superset of operations that includes stellation.

By default, all faces are opertated on, but this can be overidden by specifying ‘faces’ as an iterable of integer face indices.

gloopy.shapes.stellate.stellate_face(shape, face_index, height)[source]

Modify shape in-place. Stellate the single face at position ‘face_index’.

subdivide Module

gloopy.shapes.subdivide.subdivide(shape, faces=None, color=None)[source]

Modify the given shape in-place. Subdivides each of its faces into new, smaller faces, by cutting off the corners:

        v0
        /\       
       /  \
mid[0]/----\mid[2]
     / \  / \
    /___\/___\
  v1  mid[1]  v2

By default, all faces are operated on, but this can be overidden by specifying ‘faces’ as an iterable of integer face indices.

gloopy.shapes.subdivide.subdivide_face(shape, face_index, edges, color2=None)[source]

Modify the given shape in-place. Subdivides the single face at position face_index.

edges should be a dictionary, which starts empty, but is filled by subdivide face for it’s own internal bookkeeping. Pass in the same dict object for every call to subdivide_face on the same shape. See subdivide for an example of this.

tetrahedron Module

gloopy.shapes.tetrahedron.DualTetrahedron(radius, color1=None, color2=None)[source]

Return a new Shape, like two interpenetrating Tetrahedrons. Vertices are at radius from the center. One color is used for each of the tetrahedrons.

gloopy.shapes.tetrahedron.Tetrahedron(radius, face_colors=None)[source]

Return a new Shape, a regular triangular-based pyramid, or d4. One of the platonic solids. Vertices are at radius from the center.

colors may be either an instance of Color, or a sequence of colors, one for each face.