2.3.7 Changing the Formex

Until now, we've only created simple Formices. The strength of pyFormex however is that it is very easy to generate large geometrical models by a sequence of mathematical transformations. After initiating a basic Formex, it's possible to transform it by using copies, translations, rotations, projections,...

There are many transformations available, but this is not the right place to describe them all. This is what the reference manual in chapter 8 is for. A summary of all possible transformations and functions can be found there.

To illustrate some of these transformations and the recommended way of writing a script, we will analyse some of the examples. More of these interesting examples are found in installdir/examples. Let's begin with the example Spiral.py.


#!/usr/bin/env pyformex
# $Id$
##
## This file is part of pyFormex 0.3 Release Mon Feb 20 21:04:03 2006
## pyFormex is a python implementation of Formex algebra
## Homepage: http://pyformex.berlios.de/
## Distributed under the GNU General Public License, see file COPYING
## Copyright (C) Benedict Verhegghe except where stated otherwise 
##
#
"""Spiral"""

m = 36 # number of cells along torus big circle
n = 10 # number of cells along torus small circle

def drawit(F,view='front'):
    clear()
    draw(F,view)
    
F = Formex(pattern("164"),[1,2,3]); drawit(F)
F = F.replic(m,1,0); drawit(F)
F = F.replic(n,1,1); drawit(F)
F = F.translate1(2,1); drawit(F,'iso')
F = F.cylindrical([2,1,0],[1.,360./n,1.]); drawit(F,'iso')
F = F.replic(5,m,2); drawit(F,'iso')
F = F.rotate(-10,0); drawit(F,'iso')
F = F.translate1(0,5); drawit(F,'iso')
F = F.cylindrical([0,2,1],[1.,360./m,1.]); drawit(F,'iso')
drawit(F,'right')

During this first read-through, you will have noticed that every step is drawn. Of course, this is not necessary, but it can be useful. And above all, it is very educational for use in a tutorial...

The next important thing is that parameters were used. It's recommended to always do this, especially when you want to do a parametric study of course, but it can also be very convenient if at some point you want to change the geometry (for example when you want to re-use the script for another application).

A simple function drawit() is defined for use in this script only. This function only provides a shorter way of drawing Formices, since it combines clear() and draw.

Now, let's dissect the script.

def drawit(F,view='front'):
    clear()
    draw(F,view)
This is a small function that is only defined in this script. It clears the screen and draws the Formex at the same time.

m = 36 # number of cells along torus big circle
n = 10 # number of cells along torus small circle
These are the parameters. They can easily be changed, and a whole new spiral will be created without any extra effort. The first step is to create a basic Formex. In this case, it's a triangle which has a different property number for every edge.
F = Formex(pattern("164"),[1,2,3]); drawit(F)
Figure 2.9: The basic Formex
 
spiral-000.png

This basic Formex is copied 'm' times in the 0-direction with a translation step of '1' (the length of an edge of the triangle). After that, the new Formex is copied 'n' times in the 1-direction with a translation step of '1'. Because of the recursive definition (F=F.replic), the original Formex F is overwritten by the transformed one.

F = F.replic(m,1,0); drawit(F)
F = F.replic(n,1,1); drawit(F)

Now a copy of this last Formex is translated in direction '2' with a translation step of '1'. This necessary for the transformation into a cilinder. The result of all previous steps is a rectangular pattern with the desired dimensions, in a plane z=1.

F = F.translate(2,1); drawit(F,'iso')
Figure 2.10: The rectangular pattern
 
spiral-003.png

This pattern is rolled up into a cilinder around the 2-axis.

F = F.cylindrical([2,1,0],[1.,360./n,1.]); drawit(F,'iso')
Figure 2.11: The cylinder
 
spiral-004.png

This cilinder is copied 5 times in the 2-direction with a translation step of 'm' (the lenght of the cilinder).

F = F.replic(5,m,2); drawit(F,'iso')

The next step is to rotate this cilinder -10 degrees around the 0-axis. This will determine the pitch angle of the spiral.

F = F.rotate(-10,0); drawit(F,'iso')
Figure 2.12: The new cylinder
 
spiral-006.png

This last Formex is now translated in direction '0' with a translation step of '5'.

F = F.translate(0,5); drawit(F,'iso')

Finally, the Formex is rolled up, but around a different axis then before. Due to the pitch angle, a spiral is created. If the pitch angle would be 0 (no rotation of -10 degrees around the 0-axis), the resulting Formex would be a torus.

F = F.cylindrical([0,2,1],[1.,360./m,1.]); drawit(F,'iso')
drawit(F,'right')

Figure 2.13: The spiral
 
spiral-007.png spiral-008.png