# Geometry module¶

Geometry funtions.

## minimal_geometry¶

minimal_geometry.py: A bare minimum geometry library to do
some of the work required by Rowan’s laura2vtk.py
class cfpylib.geom.minimal_geometry.Vector(x=0.0, y=0.0, z=0.0)
__abs__()

Magnitude

__div__(other)

Scalar division

__init__(x=0.0, y=0.0, z=0.0)

Create Vector object.

__mul__(other)

Dot product or scalar multiplication.

__neg__()

Negation

__pos__()

Unary plus

__repr__()

String representation

__rmul__(other)

Dot product or scalar multiplication.

__str__()

String representation

__sub__(other)

Subraction

sum()

Algebraic summation of component values

unit()

Unit vector

cfpylib.geom.minimal_geometry.dot(a, b)

Dot product

cfpylib.geom.minimal_geometry.cross(a, b)

Cross product

Quadrilateral defining unit vectors, area and centroid.

cfpylib.geom.minimal_geometry.tetrahedron_properties(p0, p1, p2, p3)

Tetrahedron centroid and volume

cfpylib.geom.minimal_geometry.wedge_properties(p0, p1, p2, p3, p4, p5)

Wedge centroid and volume

cfpylib.geom.minimal_geometry.hexahedron_properties(p0, p1, p2, p3, p4, p5, p6, p7)

Hexahedron centroid and volume

cfpylib.geom.minimal_geometry.hexahedron_volume(p0, p1, p2, p3, p4, p5, p6, p7)

Hexahedron volume

## svg_render¶

svg_render.py: Render a drawing in Scalable Vector Graphics format.

This module provides a few convenient functions for rendering a 2D drawing in Scalable Vector Graphics format. The main transformation is from a user-space coordinate system with (0,0) at the lower-left corner to the SVG coordinate system with (0,0) in the upper-left corner of the page. Along the way, user-space units are converted to points because Inkscape seems to behave better if everything is specified in points.

Author: P.Jacobs

Version: 23-Oct-2005 first cut to suit scriptit.py in mb_cns

class cfpylib.geom.svg_render.SvgEnvironment(width=120.0, height=120.0, unitLength='mm', title='Untitled', desc='No description')

Somewhere to keep the rendering configuration.

__init__(width=120.0, height=120.0, unitLength='mm', title='Untitled', desc='No description')

Creates a SVG environment with a particular canvas size.

Inserts a comment into the SVG file.

arc(x0, y0, x1, y1, xc, yc, dashed=0)

Render a circular arc from point 0 to pint 1 with centre at c.

bezier3(x0, y0, x1, y1, x2, y2, x3, y3, dashed=0)

Render a thrid-order Bezier curve.

circle(x, y, r, dashed=0)

Render a circle of radius r at centre (x,y).

close()

Finishes off the SVG file and closes it.

dotlabel(x, y, label=None, anchor='middle', dotSize=2.0, fontSize=10, colour='black', fontFamily='sanserif')

Render a dot with a text label.

Parameters: x – x-coordinate in user-space y – y-coordinate in user-space label – label text anchor – anchor location on label dotSize – dot diameter in mm textSize – font size in points colour – of both the label and the dot
getLineStyle(dashed=0)

Assembles a suitable style specification string.

Parameters: dashed – (boolean or int) flag to indicate that the line is dashed style string
line(x1, y1, x2, y2, dashed=0)

Render a line from point 1 to point 2.

open(fileName='drawing.svg')

Opens the SVG file and writes the preamble.

Parameters: self – SvgEnvironment object fileName – file to save the rendering. None
polyline(xlist, ylist, dashed=0)

Render a polyline from lists of x and y coordinates.

setDashArray(dashLength=2.0, gapLength=2.0)

Sets length of dashes and gaps.

Parameters: dashLength – (float or int) in mm gapLength – (float or int) in mm None
setLineWidth(w)

Sets line width.

Parameters: w – (float or int) line-width in mm. None
text(x, y, textString, angle=0.0, fontSize=10, anchor='start', colour='black', fontFamily='sanserif')

Render the textString at point (x,y).

Parameters: x (float or int) – x-coordinate of anchor in user-space y (float or int) – y-coordinate of anchor in user-space textString – string of characters to render angle (float or int) – angle (in degrees) of text line wrt x-axis (counterclockwise is positive) fontSize (int or float) – size of font in points anchor (string) – one of ‘start’, ‘middle’ or ‘end’ colour (string) – of the text
toPointsX(x)

Transforms x-coordinate from user-space to SVG space.

Parameters: x – (float or int) x-coordinate in units (with the origin in the lower-left corner) points for SVG
toPointsY(y)

Transforms y-coordinate from user-space to SVG space.

Parameters: y – (float or int) y-coordinate in units (with the origin in the lower-left corner) points in SVG coordinate system (with the origin in the upper left)

## transform_pyfunc¶

transform_pyfunc.py: Apply a matrix transformation to a Python function.

The functions provided by this module are used to manipulate a python function prior to using the function to create a path with libprep3’s PyFunctionPath. Available transformations are rotation and translation.

cfpylib.geom.transform_pyfunc.rotate_pyfunc(original_pyfunc, axis, angle)

Rotate a python function.

Return a function that is the “original_pyfunc” function rotated about an axis “axis” through an angle of “angle”.

Parameters: original_pyfunc – (function) The name of the python function to be rotated. This python function must return a tuple of the 3 co-ordinates. axis – (str) The axis of rotation [“x”, “y” or “z”]. angle – (float) Counter-clockwise (right hand rule) angle of rotation [radians]. (function) Rotated version of the original python function.
cfpylib.geom.transform_pyfunc.translate_pyfunc(original_pyfunc, new_origin)

Translate a python function.

Return a function that is the “original_pyfunc” function with it’s origin tranlated to “new_origin”.

Parameters: original_pyfunc – (function) The name of the python function to be rotated. This python function must return a tuple of the 3 co-ordinates. new_origin – (tuple) The new origin of original_pyfunc. (function) Translated version of the original python function.