summaryrefslogtreecommitdiff
path: root/trunk/users/vasile/brlcad/primitive.py
blob: 10801ae1792773410693d8f591fd33d7fe712c7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
## Copyright (C) 2008 James Vasile <james@hackervisions.org>'

## This is a freed work; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## any later version.

## This is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
## License for more details.  You should have received a copy of the
## GNU General Public License along with the work; if not, write
## to the Free Software Foundation, Inc., 51 Franklin Street, 5th
## Floor, Boston, MA 02110-1301 USA

from brlcad import *

class Primitive(Shape):
    "Primitive shapes executed in one or more statements."
    isprimitive=True
    def __init__(self, shape, args=[], **kwargs):
        self.name = build_name(shape, 's', **kwargs)
        self.statements = []

        self.guess_vertex(args)

        Shape.__init__(self, ( Kill(self.name), 
                               Statement('in %s %s' % (self.name, shape),args),), 
                       name=self.name, **kwargs)

    def guess_vertex(self, args):
        ## Guess at the vertex.  If this won't find the right vertex,
        ## you'll have to set it manually.
        i = 0
        while not hasattr(self, 'vertex') and i < len(args):
            if type(args[i]) == tuple and len(args[i]) == 3:
                self.vertex = args[i]
            i += 1

    def rotate(self, rotation, vertex=None):
        if vertex == None:
            vertex = self.vertex

        xrot, yrot, zrot = rotation
        xvert, yvert, zvert = vertex

        self.statements.append('sed %s\nkeypoint %f %f %f\nrot %f %f %f\naccept\n' % (
                self.name, xvert, yvert, zvert, xrot, yrot, zrot))

    def translate(self, translate, vertex=None, **kwargs):
        '''Translate shape to coordinates translate.

        kwargs['absolute'] = True will move the shape relative to the
        origin.  Default is to move the shape relative to its current
        position.

        Untested function.  Works in theory ;)'''

        tx, ty, tz = translate
        sx, sy, sz = self.vertex

        if vertex == None:  ## will vertex ever really be None?
            vx, vy, vz = self.vertex
        else:
            vx, vy, vz = vertex

        if 'absolute' in kwargs:
            self.statements.append('sed %s\ntra %f %f %f\naccept\n' % (
                    self.name, tx-vx, ty-vy, tz-vz))
        else:
            self.statements.append('sed %s\ntra %f %f %f\naccept\n' % (
                    self.name, tx, ty, tz))

class Box(Primitive):
    def __init__(self, xmin, xmax, ymin, ymax, zmin, zmax, **kwargs):
        self.vertex = (xmin, ymin, zmin)
        if not 'basename' in kwargs:
            kwargs['basename'] = 'box'
        Primitive.__init__(self, "rpp", (xmin, xmax, ymin, ymax, zmin, zmax), **kwargs)
class Ellipsoid_foci(Primitive):
    def __init__(self, focus_1, focus_2, chord_length, **kwargs):
        'TODO: test chord_length must be > distance between foci'
        Primitive.__init__(self, "ellg", (focus_1, focus_2, chord_length), **kwargs)

## Implement some primitive shapes
from string import Template
primitives = {
'Arb4':['arb4', 'v1, v2, v3, v4'],
'Arb5':['arb5', 'v1, v2, v3, v4, v5'],
'Arb6':['arb6', 'v1, v2, v3, v4, v5, v'],
'Arb7':['arb7', 'v1, v2, v3, v4, v5, v6, v7'],
'Arb8':['arb8', 'v1, v2, v3, v4, v5, v6, v7, v8'],
'Cone':['trc', 'vertex, height_vector, base_radius, top_radius'],
'Cone_elliptical':['tec', 'vertex, height_vector, major_axis, minor_axis, ratio'],
'Cone_general':['tgc', 'vertex, height_vector, avector, bvector, cscalar, dscalar'],
'Cylinder':['rcc', 'vertex, height_vector, radius'],
'Cylinder_elliptical':['rec', 'vertex, height_vector, major_axis, minor_axis'],
'Cylinder_hyperbolic':['rhc','vertex, height_vector, bvector, half_width, apex_to_asymptote'],
'Cylinder_parabolic':['rpc', 'vertex, height_vector, bvector, half_width'],
'Ellipsoid':['ell', 'vertex, avector, bvector, cvector'],
'Hyperboloid_elliptical': ['ehy', 'vertex, height_vector, avector, bscalar, apex_to_asymptote'],
'Paraboloid_elliptical': ['epa', 'vertex, height_vector, avector, bscalar'],
'Ellipsoid_radius':['ell1', 'vertex, radius'],
'Particle':['part', 'vertex, height_vector, radius_at_v_end, radius_at_h_end'],
'Sphere':['sph', 'vertex, radius'],
'Torus':['tor', 'vertex, normal, radius_1, radius_2'],
'Torus_elliptical':['eto', 'vertex, normal_vector, radius, cvector, axis'],
}
for p in primitives:
    exec Template('''class $classname(Primitive):
    def __init__(self, $args, **kwargs):
        Primitive.__init__(self, "$mged", ($args), **kwargs)'''). \
            substitute(classname = p, mged = primitives[p][0], args = primitives[p][1])