Monday, June 24, 2013

Default value field

Within the addon a function is defined which can produce a standard value field. This field is used in the example animations to color Suzanne.

Code

The code of this function is quite straightforward:


def fillLat():
    maxValue = 3.5
    lat = Lattice((30,30,30),(-1.0,-1.0,-1.0),(3.0,3.0,3.0))
    for z in range(lat.dim[2]) :
        for y in range(lat.dim[1]) :
            for x in range(lat.dim[0]) :
                wp = lat.discreteToWorld((x,y,z))
                d = math.sqrt(wp[0]*wp[0] + wp[1]*wp[1] + wp[2]*wp[2])
                lat.setValue(maxValue - d,(x,y,z))
    lat.min = 0.0
    lat.max = maxValue
    return lat

 Here a couple of things happen
  1. maxValue is defined to be 3.5
  2. We create a Lattice defined by three tuples
    1. Number of voxels in each direction
    2. Position of first corner of the total data field in the world coordinates (global coordinate system)
    3. Extent of the total data field
  3. Loop over all dimensions, to handle every voxel
    1. Get the position of the (center of the) current voxel in the world coordinates (wp)
    2. Calculate the distance (d) of the current voxel relative to (0,0,0)
    3. Set the value of the current voxel to the defined maximum minus the distance
  4. Set the minimum value to 0.0; this value can be used to initialize the colormap
  5. Set the maximum value to maxValue; this value can be used to initialize the colormap
  6. Return the create lattice
This code can be found in 
radth-packages/radthlib/lattice.py
When another default value field is requested, this function must be re-implemented, or such a lattice can be read from a pickle file.

No comments:

Post a Comment