diff --git a/docs/src/gui/vismach.adoc b/docs/src/gui/vismach.adoc index f67066b08a7..848733ddff0 100644 --- a/docs/src/gui/vismach.adoc +++ b/docs/src/gui/vismach.adoc @@ -83,8 +83,12 @@ or raw data: possible to assemble the model in the CAD package. Alternatively parts can be created inside the model script from a range -of *shape primitives*. Many shapes are created at the origin and need to -be moved to the required location after creation: +of *shape primitives*. All primitives accept static numeric arguments or +an optional leading HAL component (`comp`) followed by HAL pin names +for dynamic dimensions. Many shapes are created at the origin and need +to be moved to the required location +after creation: + * `cylinder = CylinderX(x1, r1, x2, r2) + cylinder = CylinderY(y1, r1, y2, r2) + @@ -108,6 +112,37 @@ be moved to the required location after creation: Creates an xw by yw by zw box centred on the origin. * `box = BoxCenteredXY(xw, yw, z)` + Creates a box of width xw / yw and height z. +* `line = Line([comp,] x1, y1, z1, x2, y2, z2, stretch=1, r=5)` + + Creates a cylindrical line segment starting at `(x1, y1, z1)` along + vector `(x2, y2, z2)` scaled by `stretch` with radius `r`. +* `grid = Grid([comp,] s=500) + + grid = Grid([comp,] x, y, z, vx, vy, vz, s=500) + + grid = Grid([comp,] ox, oy, oz, xx, xy, xz, zx, zy, zz, s=500)` + + Creates a reference grid of half-size `s` on the XY plane at the + origin, perpendicular to normal vector `(vx, vy, vz)` at `(x, y, z)`, + or aligned to origin `(ox, oy, oz)`, X-direction `(xx, xy, xz)`, and + Z-normal `(zx, zy, zz)`. +* `plane = Plane([comp,] s=500) + + plane = Plane([comp,] x, y, z, vx, vy, vz, s=500) + + plane = Plane([comp,] ox, oy, oz, xx, xy, xz, zx, zy, zz, s=500)` + + Creates a solid reference plane of half-size `s` at the origin, + aligned to a normal vector, or aligned to an origin, X-direction + vector, and Z-normal vector. +* `coords = Coords([comp,] stretch=1, r=5) + + coords = Coords([comp,] ox, oy, oz, zx, zy, zz, stretch=1, r=5) + + coords = Coords([comp,] ox, oy, oz, xx, xy, xz, zx, zy, zz, stretch=1, r=5)` + + Creates a 3-axis RGB coordinate indicator (X=Red, Y=Green, Z=Blue) of + base length `50 * stretch` and radius `r` at the origin, aligned to a + Z-normal vector, or aligned to an origin, X-direction vector, and + Z-normal vector. + +[NOTE] +==== +Legacy `Hal*` aliases (such as `HalLine`, `HalGrid`, +`HalGridFromNormal`, `HalGridFromNormalAndDirection`, `HalPlaneFromNormal`, +and `HalCoordsFromNormalAndDirection`) remain supported for backwards +compatibility, though they may be deprecated in the future. +==== Composite parts may be created by *assembling* these primitives either at creation time or subsequently using `Collection()`: @@ -131,22 +166,39 @@ rotation axis is created at the origin (but moves with the Part): * `part1 = Rotate([part1], theta, x, y, z)` + Rotate the part by angle theta about an axis between the origin and x, y, z. +* `part1 = Scale([part1], x, y, z)` + + Scale the part by factors `x`, `y`, and `z` along the respective axes. +* `part1 = RotateEuler([part1], th1, th2, th3, order=123)` + + Apply three successive Euler rotations (`th1`, `th2`, `th3` in degrees) + about the axes specified by `order` (default `123` for X-Y-Z). +* `part1 = Show([part1], const, var, scaleby_true=1, scaleby_false=0)` + + Toggle part visibility/scale between `scaleby_true` and `scaleby_false` + depending on whether `const == var`. + +[NOTE] +==== +Legacy `Hal*` aliases (such as `HalTranslate`, `HalVectorTranslate`, +`HalRotate`, `HalVectorRotate`, `HalScale`, `HalRotateEuler`, and +`HalShow`) remain supported for backwards compatibility, though they may +be deprecated in the future. +==== == Animating Parts -To animate the model (controlled by the values of HAL pins) there are -two functions 'HalTranslate' and 'HalRotate'. For parts to move inside -an assembly they need to have their HAL motions defined before being -assembled with the "Collection" command. The rotation axis and -translation vector move with the part as it is moved by the vismach -script during model assembly, or as it moves in response to the HAL pins -as the model is animated: +To animate the model (controlled by the values of HAL pins), `Translate`, +`Rotate`, `Scale`, `RotateEuler`, and `Show` accept a HAL component and +HAL pin names. For parts to move inside an assembly they need to have +their motions defined before being assembled with the "Collection" command. +The rotation axis and translation vector move with the part as it is moved +by the vismach script during model assembly, or as it moves in response to +the HAL pins as the model is animated: + -* `part = HalTranslate([part], comp, "hal_pin", xs, ys, zs)` + +* `part = Translate([part], comp, "hal_pin", xs, ys, zs)` + The function arguments are: ** first a _collection/part_ which can be pre-created earlier in the script, or could be created at this point if preferred eg - `part1 = HalTranslate([Box(....)], ...)`. + `part1 = Translate([Box(....)], ...)`. ** The _HAL component_ is the next argument, i.e. the object returned by the `comp = hal.component(...)` command. After that is the name of the HAL in that will animate the motion, this needs to match an @@ -158,8 +210,8 @@ as the model is animated: However if the STL file happened to be in cm and the machine was in inches, this could be fixed at this point by using 0.3937 (1cm /2.54in) as the scale. -* `part = HalRotate([part], comp, "hal_pin", angle_scale, x, y, z)` + - This command is similar in its operation to HalTranslate except that +* `part = Rotate([part], comp, "hal_pin", angle_scale, x, y, z)` + + This command is similar in its operation to Translate except that it is typically necessary to move the part to the origin first to define the axis. ** The _axis of rotation_ is from the origin point to the point defined @@ -195,21 +247,21 @@ of drive dogs: #Drive dogs dogs = Box(-6,-3,94,6,3,100) dogs = Color([1,1,1,1],[dogs]) -dogs = HalRotate([dogs],c,"spindle",360,0,0,1) +dogs = Rotate([dogs],c,"spindle",360,0,0,1) dogs = Translate([dogs],-1,49,0) #Drawbar draw = CylinderZ(120,3,125,3) draw = Color([1,0,.5,1],[draw]) draw = Translate([draw],-1,49,0) -draw = HalTranslate([draw],c,"drawbar",0,0,1) +draw = Translate([draw],c,"drawbar",0,0,1) # head/spindle head = AsciiSTL(filename="./head.stl") head = Color([0.3,0.3,0.3,1],[head]) head = Translate([head],0,0,4) head = Collection([head, tool, dogs, draw]) -head = HalTranslate([head],c,"Z",0,0,0.1) +head = Translate([head],c,"Z",0,0,0.1) # base base = AsciiSTL(filename="./base.stl") @@ -295,7 +347,7 @@ tooltip = Capture() #Build and assemble the model part1 = Collection([Box(-6,-3,94,6,3,100)]) part1 = Color([1,1,1,1],[part1]) -part1 = HalRotate([part1],comp,"pin_name",360,0,0,1) +part1 = Rotate([part1],comp,"pin_name",360,0,0,1) part1 = Translate([dogs],-1,49,0) ... #create a top-level model diff --git a/lib/python/vismach.py b/lib/python/vismach.py index 95648aefab6..788cfaf9c36 100644 --- a/lib/python/vismach.py +++ b/lib/python/vismach.py @@ -22,7 +22,6 @@ from OpenGL.GLU import * from math import * import glnav -import hal class Collection(object): def __init__(self, parts): @@ -56,159 +55,223 @@ def volume(self): def set_volume(self,vol): self.vol = vol; -class Translate(Collection): - def __init__(self, parts, x, y, z): - self.parts = parts - self.where = x, y, z - def apply(self): - glPushMatrix() - glTranslatef(*self.where) +def _split_comp(args): + """Detects an optional leading hal.component instance in *args.""" + if args and isinstance(args[0], hal.component): + return args[0], args[1:] + return None, args - def unapply(self): - glPopMatrix() -class Scale(Collection): - def __init__(self, parts, x, y, z): +def _cross3(a, b): + return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]] + + +def _polar3(v, stretch=1.0): + length = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) * stretch + axis = (1, 0, 0) if hypot(v[0], v[1]) < 0.001 else _cross3(v, (0, 0, 1)) + angle = -atan2(hypot(v[0], v[1]), v[2]) * 180.0 / pi + return (length, angle, axis) + + +class ArgsBase(object): + """Resolves static numbers or HAL pin strings against an optional hal.component.""" + def __init__(self, comp, *args): + self.comp = comp + self._args = args + + def coords(self): + return [self.comp[v] if isinstance(v, str) else v for v in self._args] + + def cross(self, a, b): + return _cross3(a, b) + + def polar(self, v, stretch=1.0): + return _polar3(v, stretch) + + +class Translate(Collection, ArgsBase): + """ + Unifies legacy classes: + - Translate(parts, x, y, z) + - HalTranslate(parts, comp, var, x, y, z) + - HalVectorTranslate(parts, comp, xvar, yvar, zvar, scale=1) + """ + def __init__(self, parts, *args, **kwargs): self.parts = parts - self.scaleby = x, y, z + comp, rest = _split_comp(args) + if ( + comp is not None + and len(rest) == 4 + and isinstance(rest[0], str) + and not any(isinstance(v, str) for v in rest[1:]) + ): + # HalTranslate(parts, comp, var, x, y, z) + self._mode = "scalar_axis" + var, x, y, z = rest + ArgsBase.__init__(self, comp, var, x, y, z) + + else: + # Translate(parts, x, y, z) or HalVectorTranslate(parts, comp, x, y, z, scale=1) + self._mode = "vector" + if len(rest) == 3: + x, y, z = rest + sc = kwargs.get("scale", 1) + elif len(rest) == 4: + x, y, z, sc = rest + else: + raise TypeError("Translate expects (parts, [comp,] x, y, z[, scale]) or (parts, comp, var, x, y, z)") + ArgsBase.__init__(self, comp, x, y, z, sc) def apply(self): glPushMatrix() - glScalef(*self.scaleby) + if self._mode == "scalar_axis": + v, x, y, z = self.coords() + glTranslatef(x * v, y * v, z * v) + else: + x, y, z, sc = self.coords() + glTranslatef(sc * x, sc * y, sc * z) def unapply(self): glPopMatrix() -class HalTranslate(Collection): - def __init__(self, parts, comp, var, x, y, z): +HalTranslate = Translate +HalVectorTranslate = Translate + + +class Scale(Collection, ArgsBase): + """ + Unifies legacy classes: + - Scale(parts, x, y, z) + - HalScale(parts, comp, x, y, z, var) + """ + def __init__(self, parts, *args): self.parts = parts - self.where = x, y, z - self.comp = comp - self.var = var + comp, rest = _split_comp(args) + if len(rest) == 3: + x, y, z = rest + factor = 1 + elif len(rest) == 4: + x, y, z, factor = rest + else: + raise TypeError("Scale expects (parts, [comp,] x, y, z[, var])") + ArgsBase.__init__(self, comp, x, y, z, factor) def apply(self): - x, y, z = self.where - v = self.comp[self.var] - + x, y, z, factor = self.coords() glPushMatrix() - glTranslatef(x*v, y*v, z*v) + glScalef(x * factor, y * factor, z * factor) def unapply(self): glPopMatrix() +HalScale = Scale -class HalRotate(Collection): - def __init__(self, parts, comp, var, th, x, y, z): + +class Rotate(Collection, ArgsBase): + """ + Unifies legacy classes: + - Rotate(parts, th, x, y, z) + - HalRotate(parts, comp, var, th, x, y, z) + - HalVectorRotate(parts, comp, var, th, x, y, z) + """ + def __init__(self, parts, *args): self.parts = parts - self.where = th, x, y, z - self.comp = comp - self.var = var + comp, rest = _split_comp(args) + if comp is not None and len(rest) == 5: + var, th, x, y, z = rest + ArgsBase.__init__(self, comp, var, th, x, y, z) + elif len(rest) == 4: + th, x, y, z = rest + ArgsBase.__init__(self, comp, 1, th, x, y, z) + else: + raise TypeError("Rotate expects (parts, [comp, var,] th, x, y, z)") def apply(self): - th, x, y, z = self.where + if hasattr(self, "get_values"): + # Legacy subclass pattern (e.g. HalRotateNutation) overriding get_values() + c_vals = self.coords() + var = c_vals[0] if isinstance(c_vals, (list, tuple)) else c_vals + th, x, y, z = self.get_values() + else: + var, th, x, y, z = self.coords() glPushMatrix() - glRotatef(th * self.comp[self.var], x, y, z) + glRotatef(th * var, x, y, z) def unapply(self): glPopMatrix() +HalRotate = Rotate +HalVectorRotate = Rotate + -class Rotate(Collection): - def __init__(self, parts, th, x, y, z): +class RotateEuler(Collection, ArgsBase): + """Replaces legacy class HalRotateEuler(parts, [comp,] th1, th2, th3, order=123).""" + def __init__(self, parts, *args, order=123): self.parts = parts - self.where = th, x, y, z + comp, rest = _split_comp(args) + if len(rest) == 4: + th1, th2, th3, order = rest + elif len(rest) == 3: + th1, th2, th3 = rest + else: + raise TypeError("RotateEuler expects (parts, [comp,] th1, th2, th3[, order])") + ArgsBase.__init__(self, comp, order, th1, th2, th3) def apply(self): - th, x, y, z = self.where + order, th1, th2, th3 = self.coords() + order = int(order) glPushMatrix() - glRotatef(th, x, y, z) + axes = { + 131: ((1, 0, 0), (0, 0, 1), (1, 0, 0)), + 121: ((1, 0, 0), (0, 1, 0), (1, 0, 0)), + 212: ((0, 1, 0), (1, 0, 0), (0, 1, 0)), + 232: ((0, 1, 0), (0, 0, 1), (0, 1, 0)), + 323: ((0, 0, 1), (0, 1, 0), (0, 0, 1)), + 313: ((0, 0, 1), (1, 0, 0), (0, 0, 1)), + 123: ((1, 0, 0), (0, 1, 0), (0, 0, 1)), + 132: ((1, 0, 0), (0, 0, 1), (0, 1, 0)), + 213: ((0, 1, 0), (1, 0, 0), (0, 0, 1)), + 231: ((0, 1, 0), (0, 0, 1), (1, 0, 0)), + 321: ((0, 0, 1), (0, 1, 0), (1, 0, 0)), + 312: ((0, 0, 1), (1, 0, 0), (0, 1, 0)), + } + if order in axes: + a1, a2, a3 = axes[order] + glRotatef(th1, *a1) + glRotatef(th2, *a2) + glRotatef(th3, *a3) def unapply(self): glPopMatrix() +HalRotateEuler = RotateEuler -class HalRotateEuler(Collection): - def __init__(self, parts, comp, th1, th2, th3, order=123): - self.parts = parts - self.comp = comp - self.order = order - self.th1 = th1 - self.th2 = th2 - self.th3 = th3 - def apply(self): - # check whether pins or values have been passed - if isinstance(self.order, str): - order = int(self.comp[self.order]) - else: - order = self.order - if isinstance(self.th1, str): - th1 = self.comp[self.th1] - else: - th1 = self.th1 - if isinstance(self.th2, str): - th2 = self.comp[self.th2] - else: - th2 = self.th2 - if isinstance(self.th3, str): - th3 = self.comp[self.th3] +class Show(Collection, ArgsBase): + """Replaces legacy class HalShow(parts, [comp,] const, var, scaleby_true=1, scaleby_false=0).""" + def __init__(self, parts, *args, scaleby_true=1, scaleby_false=0): + self.parts = parts + comp, rest = _split_comp(args) + if len(rest) == 4: + const, var, scaleby_true, scaleby_false = rest + elif len(rest) == 2: + const, var = rest else: - th3 = self.th3 + raise TypeError("Show expects (parts, [comp,] const, var[, scaleby_true, scaleby_false])") + ArgsBase.__init__(self, comp, const, var, scaleby_true, scaleby_false) + def apply(self): + const, var, s_t, s_f = self.coords() + s = s_t if const == var else s_f glPushMatrix() - if order == 131: - glRotatef(th1, 1, 0, 0) - glRotatef(th2, 0, 0, 1) - glRotatef(th3, 1, 0, 0) - elif order == 121: - glRotatef(th1, 1, 0, 0) - glRotatef(th2, 0, 1, 0) - glRotatef(th3, 1, 0, 0) - elif order == 212: - glRotatef(th1, 0, 1, 0) - glRotatef(th2, 1, 0, 0) - glRotatef(th3, 0, 1, 0) - elif order == 232: - glRotatef(th1, 0, 1, 0) - glRotatef(th2, 0, 0, 1) - glRotatef(th3, 0, 1, 0) - elif order == 323: - glRotatef(th1, 0, 0, 1) - glRotatef(th2, 0, 1, 0) - glRotatef(th3, 0, 0, 1) - elif order == 313: - glRotatef(th1, 0, 0, 1) - glRotatef(th2, 1, 0, 0) - glRotatef(th3, 0, 0, 1) - elif order == 123: - glRotatef(th1, 1, 0, 0) - glRotatef(th2, 0, 1, 0) - glRotatef(th3, 0, 0, 1) - elif order == 132: - glRotatef(th1, 1, 0, 0) - glRotatef(th2, 0, 0, 1) - glRotatef(th3, 0, 1, 0) - elif order == 213: - glRotatef(th1, 0, 1, 0) - glRotatef(th2, 1, 0, 0) - glRotatef(th3, 0, 0, 1) - elif order == 231: - glRotatef(th1, 0, 1, 0) - glRotatef(th2, 0, 0, 1) - glRotatef(th3, 1, 0, 0) - elif order == 321: - glRotatef(th1, 0, 0, 1) - glRotatef(th2, 0, 1, 0) - glRotatef(th3, 1, 0, 0) - elif order == 312: - glRotatef(th1, 0, 0, 1) - glRotatef(th2, 1, 0, 0) - glRotatef(th3, 0, 1, 0) + glScalef(s, s, s) def unapply(self): glPopMatrix() +HalShow = Show + class Track(Collection): '''move and rotate an object to point from one capture()'d @@ -261,520 +324,290 @@ def apply(self): def unapply(self): glPopMatrix() -# scales an object by the value of a halpin -class HalScale(Collection): - def __init__(self, parts, comp, x, y, z, var): - self.parts = parts - self.comp = comp - self.var = var - self.xyz = x, y, z - - def apply(self): - x, y, z = self.xyz - factor = self.comp[self.var] - glPushMatrix() - glScalef(x*factor,y*factor,z*factor) - - def unapply(self): - glPopMatrix() - -# shows an object if const=var and hides it otherwise, behavior can be changed -# using the optional arguments for scalefactors when true or false -class HalShow(Collection): - def __init__(self, parts, comp, const, var, scaleby_true=1, scaleby_false=0): - self.parts = parts - self.comp = comp - self.const = const - self.var = var - self.scaleby_true = scaleby_true - self.scaleby_false = scaleby_false - - def apply(self): - s_t = self.scaleby_true - s_f = self.scaleby_false - glPushMatrix() - if self.const == self.comp[self.var]: - glScalef(s_t,s_t,s_t) +class CoordsBase(ArgsBase): + def __init__(self, *args): + if args and (args[0] is None or isinstance(args[0], hal.component)): + comp, rest = args[0], args[1:] else: - glScalef(s_f,s_f,s_f) - - def unapply(self): - glPopMatrix() + comp, rest = _split_comp(args) + ArgsBase.__init__(self, comp, *rest) + self.q = gluNewQuadric() -# translates an object using a variable translation vector -# use scale=-1 to change direction -class HalVectorTranslate(Collection): - def __init__(self, parts, comp, xvar, yvar, zvar, scale=1): - self.parts = parts - self.comp = comp - self.xvar = xvar - self.yvar = yvar - self.zvar = zvar - self.sc = scale +class Line(CoordsBase): + def __init__(self, *args, **kwargs): + comp, rest = _split_comp(args) + if len(rest) < 6 or len(rest) > 8: + raise TypeError("Line expects ([comp,] x1, y1, z1, x2, y2, z2, stretch=1, r=5)") + x1, y1, z1, x2, y2, z2 = rest[:6] + stretch = rest[6] if len(rest) >= 7 else kwargs.get("stretch", 1) + r = rest[7] if len(rest) == 8 else kwargs.get("r", 5) + CoordsBase.__init__(self, comp, x1, y1, z1, x2, y2, z2, stretch, r) - def apply(self): - # check for zero vector components - xvar = 0 if self.xvar == 0 else self.comp[self.xvar] - yvar = 0 if self.yvar == 0 else self.comp[self.yvar] - zvar = 0 if self.zvar == 0 else self.comp[self.zvar] - sc = self.sc + def draw(self): + x1, y1, z1, x2, y2, z2, stretch, r = self.coords() + length, angle, axis = self.polar([x2, y2, z2], stretch) glPushMatrix() - glTranslatef(sc*xvar, sc*yvar, sc*zvar) + glTranslate(x1, y1, z1) + glRotate(angle, *axis) + gluCylinder(self.q, r, r, length, 32, 1) def unapply(self): glPopMatrix() +HalLine = Line -class HalVectorRotate(Collection): - def __init__(self, parts, comp, var, th, x, y, z): - self.parts = parts - self.comp = comp - self.var = var - self.values = th, x, y, z - - def get_values(self): - return self.values - def apply(self): - th, x, y, z = self.get_values() +def _apply_normal_or_basis(origin, vec_z, vec_x=None): + """Pushes a matrix aligned to an origin + Z-normal (and optional X-direction).""" + ox, oy, oz = origin + if vec_x is None: + _, angle, axis = _polar3(vec_z) glPushMatrix() - glRotatef(th * self.comp[self.var], x, y, z) - - def unapply(self): - glPopMatrix() - + glTranslate(ox, oy, oz) + glRotate(angle, *axis) + else: + xx, xy, xz = vec_x + zx, zy, zz = vec_z + yx, yy, yz = _cross3(vec_z, vec_x) + m_t = [ + [xx, xy, xz, 0], + [yx, yy, yz, 0], + [zx, zy, zz, 0], + [ox, oy, oz, 1], + ] + glPushMatrix() + glMultMatrixf(m_t) -class CoordsBase(object): - def __init__(self, *args): - if args and isinstance(args[0], hal.component): - self.comp = args[0] - args = args[1:] +def _draw_grid_lines(s): + s_int = int(s) + glBegin(GL_LINES) + for i in range(-s_int, s_int + 10, 10): + glVertex3f(-s_int, i, 0) + glVertex3f(s_int, i, 0) + glVertex3f(i, -s_int, 0) + glVertex3f(i, s_int, 0) + glEnd() + + +class Grid(CoordsBase): + """ + Unifies legacy classes: + - HalGrid([comp,] s=500) + - HalGridFromNormal([comp,] x, y, z, vx, vy, vz, s=500) + - HalGridFromNormalAndDirection([comp,] ox, oy, oz, xx, xy, xz, zx, zy, zz, s=500) + """ + def __init__(self, *args, **kwargs): + comp, rest = _split_comp(args) + n = len(rest) + + if n in (0, 1): + self._mode = "origin" + s = rest[0] if n == 1 else kwargs.get("s", 500) + CoordsBase.__init__(self, comp, s) + elif n in (6, 7): + self._mode = "normal" + x, y, z, vx, vy, vz = rest[:6] + s = rest[6] if n == 7 else kwargs.get("s", 500) + CoordsBase.__init__(self, comp, x, y, z, vx, vy, vz, s) + elif n in (9, 10): + self._mode = "normal_dir" + ox, oy, oz, xx, xy, xz, zx, zy, zz = rest[:9] + s = rest[9] if n == 10 else kwargs.get("s", 500) + CoordsBase.__init__(self, comp, ox, oy, oz, xx, xy, xz, zx, zy, zz, s) else: - self.comp = None - self._coords = args - self.q = gluNewQuadric() - - def coords(self): - return list(map(self._coord, self._coords)) - - def _coord(self, v): - if isinstance(v, str): return self.comp[v] - return v - - -# draw an open cylinder from point_1 to point_2, radius is optional (defaults to 5) -class HalLine(): - def __init__(self, comp, x1var, y1var, z1var, x2var, y2var, z2var, stretch, r=5): - self.comp = comp - self.x1var = x1var - self.y1var = y1var - self.z1var = z1var - self.x2var = x2var - self.y2var = y2var - self.z2var = z2var - self.stretch = stretch - self.r = r - self.q = gluNewQuadric() - - def cross(self, a, b): - return [a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]] - - # calculate polar coordinates in degrees - # a rotates around the x-axis; b rotates around the y axis - def polar(self, v): - length = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]) * self.stretch - axis = (1, 0, 0) if hypot(v[0], v[1]) < 0.001 else self.cross(v, (0, 0, 1)) - angle = -atan2(hypot(v[0], v[1]), v[2])*180/pi - return (length, angle, axis) + raise TypeError( + "Grid expects 0-1, 6-7 (origin + normal), or " + "9-10 (origin + X-dir + normal) arguments after optional comp" + ) def draw(self): - x1 = 0 if self.x1var == 0 else self.comp[self.x1var] - y1 = 0 if self.y1var == 0 else self.comp[self.y1var] - z1 = 0 if self.z1var == 0 else self.comp[self.z1var] - x2 = 0 if self.x2var == 0 else self.comp[self.x2var] - y2 = 0 if self.y2var == 0 else self.comp[self.y2var] - z2 = 0 if self.z2var == 0 else self.comp[self.z2var] - r = self.r - v = [x2,y2,z2] - length, angle, axis = self.polar(v) - glPushMatrix() - glTranslate(x1, y1, z1) - glRotate(angle,*axis) - gluCylinder(self.q, r, r, length, 32, 1) - - def unapply(self): - glPopMatrix() + if self._mode == "origin": + (s,) = self.coords() + elif self._mode == "normal": + x, y, z, vx, vy, vz, s = self.coords() + _apply_normal_or_basis((x, y, z), (vx, vy, vz)) + else: + ox, oy, oz, xx, xy, xz, zx, zy, zz, s = self.coords() + _apply_normal_or_basis((ox, oy, oz), (zx, zy, zz), (xx, xy, xz)) -# draw a plane defined by it's normal vector(vx,vy,vz) origin at (x,y,z) -class HalPlaneFromNormal(): - def __init__(self, comp, x, y, z, vx, vy, vz, s=500): - self.comp = comp - self.x = x - self.y = y - self.z = z - self.vx = vx - self.vy = vy - self.vz = vz - self.s = s - self.q = gluNewQuadric() + _draw_grid_lines(s) - def cross(self, a, b): - return [a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]] + def unapply(self): + if self._mode != "origin": + glPopMatrix() + + +HalGrid = Grid +HalGridFromNormal = Grid +HalGridFromNormalAndDirection = Grid + + +class Plane(CoordsBase): + """ + Replaces legacy class: + - HalPlaneFromNormal([comp,] x, y, z, vx, vy, vz, s=500) + and adds: + - Plane([comp,] s=500) + - Plane([comp,] ox, oy, oz, xx, xy, xz, zx, zy, zz, s=500) + """ + def __init__(self, *args, **kwargs): + comp, rest = _split_comp(args) + n = len(rest) + + if n in (0, 1): + self._mode = "origin" + s = rest[0] if n == 1 else kwargs.get("s", 500) + CoordsBase.__init__(self, comp, s) + elif n in (6, 7): + self._mode = "normal" + x, y, z, vx, vy, vz = rest[:6] + s = rest[6] if n == 7 else kwargs.get("s", 500) + CoordsBase.__init__(self, comp, x, y, z, vx, vy, vz, s) + elif n in (9, 10): + self._mode = "normal_dir" + ox, oy, oz, xx, xy, xz, zx, zy, zz = rest[:9] + s = rest[9] if n == 10 else kwargs.get("s", 500) + CoordsBase.__init__(self, comp, ox, oy, oz, xx, xy, xz, zx, zy, zz, s) + else: + raise TypeError("Plane expects 0-1, 6-7, or 9-10 arguments after optional comp") - # calculate polar coordinates in degrees - # a rotates around the x-axis; b rotates around the y axis - def polar(self, v): - length = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]) - axis = (1, 0, 0) if hypot(v[0], v[1]) < 0.001 else self.cross(v, (0, 0, 1)) - angle = -atan2(hypot(v[0], v[1]), v[2])*180/pi - return (length, angle, axis) + def draw(self): + if self._mode == "origin": + (s,) = self.coords() + n = (0, 0, 1) + elif self._mode == "normal": + x, y, z, vx, vy, vz, s = self.coords() + n = (vx, vy, vz) + _apply_normal_or_basis((x, y, z), n) + else: + ox, oy, oz, xx, xy, xz, zx, zy, zz, s = self.coords() + n = (zx, zy, zz) + _apply_normal_or_basis((ox, oy, oz), n, (xx, xy, xz)) - def square(self,n, s): glBegin(GL_QUADS) - glNormal3f(n[0],n[1],n[2]) - glVertex3f( s, s, 0) - glVertex3f(-s, s, 0) + glNormal3f(*n) + glVertex3f(s, s, 0) + glVertex3f(-s, s, 0) glVertex3f(-s, -s, 0) - glVertex3f( s, -s, 0) + glVertex3f(s, -s, 0) glEnd() - def draw(self): - # check for zero values in the arguments - x = 0 if self.x == 0 else self.comp[self.x] - y = 0 if self.y == 0 else self.comp[self.y] - z = 0 if self.z == 0 else self.comp[self.z] - vx = 0 if self.vx == 0 else self.comp[self.vx] - vy = 0 if self.vy == 0 else self.comp[self.vy] - vz = 0 if self.vz == 0 else self.comp[self.vz] - s = self.s - v = [vx, vy, vz] - length, angle, axis = self.polar(v) - glPushMatrix() - glTranslate(x,y,z) - glRotate(angle,*axis) - self.square(v,s) - def unapply(self): - glPopMatrix() - - -# draw a coordinate system defined by it's normal vector(zx,zy,zz) and x-direction vector(xx, xy, xz) -# optional r to define the thickness of the cylinders -class HalCoordsFromNormalAndDirection(): - def __init__(self, comp, ox, oy, oz, xx, xy, xz, zx, zy, zz, stretch, r=5): - self.comp = comp - self.ox = ox - self.oy = oy - self.oz = oz - self.xx = xx - self.xy = xy - self.xz = xz - self.zx = zx - self.zy = zy - self.zz = zz - self.stretch = stretch - self.r = r - self.q = gluNewQuadric() - + if self._mode != "origin": + glPopMatrix() + + +HalPlaneFromNormal = Plane + + +class Coords(CoordsBase): + """ + Replaces legacy class: + - HalCoordsFromNormalAndDirection ([comp,] ox, oy, oz, xx, xy, xz, zx, zy, zz, stretch=1, r=5) + and adds: + - Coords([comp,] stretch=1, r=5) + - Coords([comp,] ox, oy, oz, zx, zy, zz, stretch=1, r=5) + """ + def __init__(self, *args, **kwargs): + comp, rest = _split_comp(args) + n = len(rest) + + if n <= 2: + self._mode = "origin" + stretch = rest[0] if n >= 1 else kwargs.get("stretch", 1) + r = rest[1] if n == 2 else kwargs.get("r", 5) + CoordsBase.__init__(self, comp, stretch, r) + elif n in (6, 7, 8): + self._mode = "normal" + ox, oy, oz, zx, zy, zz = rest[:6] + stretch = rest[6] if n >= 7 else kwargs.get("stretch", 1) + r = rest[7] if n == 8 else kwargs.get("r", 5) + CoordsBase.__init__(self, comp, ox, oy, oz, zx, zy, zz, stretch, r) + elif n in (9, 10, 11): + self._mode = "normal_dir" + ox, oy, oz, xx, xy, xz, zx, zy, zz = rest[:9] + stretch = rest[9] if n >= 10 else kwargs.get("stretch", 1) + r = rest[10] if n == 11 else kwargs.get("r", 5) + CoordsBase.__init__(self, comp, ox, oy, oz, xx, xy, xz, zx, zy, zz, stretch, r) + else: + raise TypeError("Coords expects 0-2, 6-8, or 9-11 arguments after optional comp") - def draw_vector(self, color): - gluCylinder(self.q, self.r, self.r, 50*self.stretch, 32, 1) + def draw_vector(self, r, stretch, color): glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color) - - def cross(self, a, b): - return [a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]] + gluCylinder(self.q, r, r, 50 * stretch, 32, 1) def draw(self): - # check for zero values in the arguments - ox = 0 if self.ox == 0 else self.comp[self.ox] - oy = 0 if self.oy == 0 else self.comp[self.oy] - oz = 0 if self.oz == 0 else self.comp[self.oz] - xx = 0 if self.xx == 0 else self.comp[self.xx] - xy = 0 if self.xy == 0 else self.comp[self.xy] - xz = 0 if self.xz == 0 else self.comp[self.xz] - zx = 0 if self.zx == 0 else self.comp[self.zx] - zy = 0 if self.zy == 0 else self.comp[self.zy] - zz = 0 if self.zz == 0 else self.comp[self.zz] - r = self.r - vo = [ox, oy, oz] - vx = [xx, xy, xz] - vz = [zx, zy, zz] - # calculate the missing y vector - vy = [yx, yy, yz] = self.cross(vz,vx) - # for some reason we need to rotate in the opposite (transpose) sense - m_t=[[ xx, xy, xz, 0], - [ yx, yy, yz, 0], - [ zx, zy, zz, 0], - [ ox, oy, oz, 1]] + if self._mode == "origin": + stretch, r = self.coords() + glPushMatrix() + elif self._mode == "normal": + ox, oy, oz, zx, zy, zz, stretch, r = self.coords() + _apply_normal_or_basis((ox, oy, oz), (zx, zy, zz)) + else: + ox, oy, oz, xx, xy, xz, zx, zy, zz, stretch, r = self.coords() + _apply_normal_or_basis((ox, oy, oz), (zx, zy, zz), (xx, xy, xz)) - glPushMatrix() - glMultMatrixf(m_t) - self.draw_vector([1,0,0,1]) - glRotate(90,0,1,0) - self.draw_vector([0,1,0,1]) - glRotate(-90,1,0,0) - self.draw_vector([0,0,1,1]) + glPushAttrib(GL_LIGHTING_BIT) + self.draw_vector(r, stretch, [1, 0, 0, 1]) + glRotate(90, 0, 1, 0) + self.draw_vector(r, stretch, [0, 1, 0, 1]) + glRotate(-90, 1, 0, 0) + self.draw_vector(r, stretch, [0, 0, 1, 1]) + glPopAttrib() def unapply(self): glPopMatrix() -# draw a grid defined by it's normal vector(zx,zy,zz) and x-direction vector(xx, xy, xz) -# optional s to define the half-width from the origin (ox,oy,oz) -class HalGridFromNormalAndDirection(): - def __init__(self, comp, ox, oy, oz, xx, xy, xz, zx, zy, zz, s=500): - self.comp = comp - self.ox = ox - self.oy = oy - self.oz = oz - self.xx = xx - self.xy = xy - self.xz = xz - self.zx = zx - self.zy = zy - self.zz = zz - self.s = s - self.q = gluNewQuadric() +HalCoordsFromNormalAndDirection = Coords - def square(self, s): - glBegin(GL_LINES); - for i in range (-s,s+10,10): - # line 1 in x direction - glVertex3f(-s, i, 0) - glVertex3f( s, i, 0) - # line 1 in y direction - glVertex3f( i,-s, 0) - glVertex3f( i, s, 0) - glEnd() - def cross(self, a, b): - return [a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]] +class _CylinderAxis(CoordsBase): + """Shared base class for axis-aligned capped cylinders (X, Y, or Z).""" + _axis_rotation = None # (angle_deg, x, y, z) or None for Z-axis def draw(self): - # check for zero values in the arguments - ox = 0 if self.ox == 0 else self.comp[self.ox] - oy = 0 if self.oy == 0 else self.comp[self.oy] - oz = 0 if self.oz == 0 else self.comp[self.oz] - xx = 0 if self.xx == 0 else self.comp[self.xx] - xy = 0 if self.xy == 0 else self.comp[self.xy] - xz = 0 if self.xz == 0 else self.comp[self.xz] - zx = 0 if self.zx == 0 else self.comp[self.zx] - zy = 0 if self.zy == 0 else self.comp[self.zy] - zz = 0 if self.zz == 0 else self.comp[self.zz] - s = self.s - vo = [ox, oy, oz] - vx = [xx, xy, xz] - vz = [zx, zy, zz] - # calculate the missing y vector - vy = [yx, yy, yz] = self.cross(vz,vx) - # for some reason we need to rotate in the opposite (transpose) sense - m_t=[[ xx, xy, xz, 0], - [ yx, yy, yz, 0], - [ zx, zy, zz, 0], - [ ox, oy, oz, 1]] - - glPushMatrix() - glMultMatrixf(m_t) - self.square(s) - - def unapply(self): - glPopMatrix() - -# draw a grid defined by it's normal vector(vx,vy,vz) origin at (x,y,z) -class HalGridFromNormal(): - def __init__(self, comp, x, y, z, vx, vy, vz, s=500): - self.comp = comp - self.x = x - self.y = y - self.z = z - self.vx = vx - self.vy = vy - self.vz = vz - self.s = s - self.q = gluNewQuadric() - - def cross(self, a, b): - return [a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]] - - # calculate polar coordinates in degrees - # a rotates around the x-axis; b rotates around the y axis - def polar(self, v): - length = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]) - axis = (1, 0, 0) if hypot(v[0], v[1]) < 0.001 else self.cross(v, (0, 0, 1)) - angle = -atan2(hypot(v[0], v[1]), v[2])*180/pi - return (length, angle, axis) - - def square(self,n, s): - glBegin(GL_LINES); - for i in range (-s,s+10,10): - # line 1 in x direction - glVertex3f(-s, i, 0) - glVertex3f( s, i, 0) - # line 1 in y direction - glVertex3f( i,-s, 0) - glVertex3f( i, s, 0) - glEnd() + p1, r1, p2, r2 = self.coords() + if p1 > p2: + p1, p2 = p2, p1 + r1, r2 = r2, r1 - def draw(self): - # check for zero values in the arguments - x = 0 if self.x == 0 else self.comp[self.x] - y = 0 if self.y == 0 else self.comp[self.y] - z = 0 if self.z == 0 else self.comp[self.z] - vx = 0 if self.vx == 0 else self.comp[self.vx] - vy = 0 if self.vy == 0 else self.comp[self.vy] - vz = 0 if self.vz == 0 else self.comp[self.vz] - s = self.s - v = [vx, vy, vz] - length, angle, axis = self.polar(v) glPushMatrix() - glTranslate(x,y,z) - glRotate(angle,*axis) - self.square(v,s) - - def unapply(self): - glPopMatrix() - -# draw a grid defined by it's normal vector(vx,vy,vz) origin at (x,y,z) -class HalGrid(): - def __init__(self, comp, s=500): - self.comp = comp - self.s = s - self.q = gluNewQuadric() - - def grid(self, s): - glBegin(GL_LINES); - for i in range (-s,s+10,10): - # line 1 in x direction - glVertex3f(-s, i, 0) - glVertex3f( s, i, 0) - # line 1 in y direction - glVertex3f( i,-s, 0) - glVertex3f( i, s, 0) - glEnd() - - def draw(self): - s = self.s - self.grid(s) + if self._axis_rotation is not None: + glRotatef(*self._axis_rotation) + glTranslatef(0, 0, p1) + gluCylinder(self.q, r1, r2, p2 - p1, 32, 1) - -# give endpoint X values and radii -# resulting cylinder is on the X axis -class CylinderX(CoordsBase): - def draw(self): - x1, r1, x2, r2 = self.coords() - if x1 > x2: - tmp = x1 - x1 = x2 - x2 = tmp - tmp = r1 - r1 = r2 - r2 = tmp - glPushMatrix() - # GL creates cylinders along Z, so need to rotate - z1 = x1 - z2 = x2 - glRotatef(90,0,1,0) - # need to translate the whole thing to z1 - glTranslatef(0,0,z1) - # the cylinder starts out at Z=0 - gluCylinder(self.q, r1, r2, z2-z1, 32, 1) - # bottom cap - glRotatef(180,1,0,0) + # Bottom cap + glRotatef(180, 1, 0, 0) gluDisk(self.q, 0, r1, 32, 1) - glRotatef(180,1,0,0) - # the top cap needs flipped and translated + glRotatef(180, 1, 0, 0) + + # Top cap glPushMatrix() - glTranslatef(0,0,z2-z1) + glTranslatef(0, 0, p2 - p1) gluDisk(self.q, 0, r2, 32, 1) glPopMatrix() glPopMatrix() def volume(self): - x1, r1, x2, r2 = self.coords() - # actually a frustum of a cone - vol = 3.1415927/3.0 * abs(x1-x2)*(r1*r1+r1*r2+r2*r2) - #print "CylinderX.volume", vol - return vol + p1, r1, p2, r2 = self.coords() + return (pi / 3.0) * abs(p1 - p2) * (r1 * r1 + r1 * r2 + r2 * r2) -# give endpoint Y values and radii -# resulting cylinder is on the Y axis -class CylinderY(CoordsBase): - def __init__(self, y1, r1, y2, r2): - self._coords = y1, r1, y2, r2 - self.q = gluNewQuadric() +class CylinderX(_CylinderAxis): + _axis_rotation = (90, 0, 1, 0) - def draw(self): - y1, r1, y2, r2 = self.coords() - if y1 > y2: - tmp = y1 - y1 = y2 - y2 = tmp - tmp = r1 - r1 = r2 - r2 = tmp - glPushMatrix() - # GL creates cylinders along Z, so need to rotate - z1 = y1 - z2 = y2 - glRotatef(-90,1,0,0) - # need to translate the whole thing to z1 - glTranslatef(0,0,z1) - # the cylinder starts out at Z=0 - gluCylinder(self.q, r1, r2, z2-z1, 32, 1) - # bottom cap - glRotatef(180,1,0,0) - gluDisk(self.q, 0, r1, 32, 1) - glRotatef(180,1,0,0) - # the top cap needs flipped and translated - glPushMatrix() - glTranslatef(0,0,z2-z1) - gluDisk(self.q, 0, r2, 32, 1) - glPopMatrix() - glPopMatrix() - def volume(self): - y1, r1, y2, r2 = self.coords() - # actually a frustum of a cone - vol = 3.1415927/3.0 * abs(y1-y2)*(r1*r1+r1*r2+r2*r2) - #print "CylinderY.volume", vol - return vol +class CylinderY(_CylinderAxis): + _axis_rotation = (-90, 1, 0, 0) -class CylinderZ(CoordsBase): - def draw(self): - z1, r1, z2, r2 = self.coords() - if z1 > z2: - tmp = z1 - z1 = z2 - z2 = tmp - tmp = r1 - r1 = r2 - r2 = tmp - # need to translate the whole thing to z1 - glPushMatrix() - glTranslatef(0,0,z1) - # the cylinder starts out at Z=0 - gluCylinder(self.q, r1, r2, z2-z1, 32, 1) - # bottom cap - glRotatef(180,1,0,0) - gluDisk(self.q, 0, r1, 32, 1) - glRotatef(180,1,0,0) - # the top cap needs flipped and translated - glPushMatrix() - glTranslatef(0,0,z2-z1) - gluDisk(self.q, 0, r2, 32, 1) - glPopMatrix() - glPopMatrix() +class CylinderZ(_CylinderAxis): + _axis_rotation = None - def volume(self): - z1, r1, z2, r2 = self.coords() - # actually a frustum of a cone - vol = 3.1415927/3.0 * abs(z1-z2)*(r1*r1+r1*r2+r2*r2) - #print "CylinderZ.volume", vol - return vol # give center and radius class Sphere(CoordsBase):