#!/usr/bin/env python from numpy import * from math import cos, sin, radians import sys def usage(): print """Usage: transform.py option x y z alpha beta gama [sx sy sz] option: -w - world transformation(default mode) -c - camera transformation x, y, z - object translation alpha - rotation about the x-axis in degree beta - rotation about the y-axis in degree gama - rotation about the z-axis in degree sx, sy, sz - object scale(only available in world transform) """ def world_transform(x, y, z, alpha, beta, gama, sx="1", sy="1", sz="1"): alpha = radians(float(alpha)) beta = radians(float(beta)) gama = radians(float(gama)) trans = matrix("1 0 0 %s; 0 1 0 %s; 0 0 1 %s; 0 0 0 1" % (x, y, z)) rx = matrix([ [1, 0, 0, 0], [0, cos(alpha), -sin(alpha), 0], [0, sin(alpha), cos(alpha), 0], [0, 0, 0, 1] ]) ry = matrix([ [cos(beta), 0, sin(beta), 0], [0, 1, 0, 0], [-sin(beta), 0, cos(beta), 0], [0, 0, 0, 1] ]) rz = matrix([ [cos(gama), -sin(gama), 0, 0], [sin(gama), cos(gama), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1] ]) scale = matrix("%s 0 0 0; 0 %s 0 0; 0 0 %s 0; 0 0 0 1" % (sx, sy, sz)) return trans * rx * ry * rz * scale def camera_transform(x, y, z, alpha, beta, gama): alpha = radians(float(alpha)) beta = radians(float(beta)) gama = radians(float(gama)) trans = matrix("1 0 0 -%s; 0 1 0 -%s; 0 0 1 -%s; 0 0 0 1" % (x, y, z)) rx = matrix([ [1, 0, 0, 0], [0, cos(alpha), sin(alpha), 0], [0, -sin(alpha), cos(alpha), 0], [0, 0, 0, 1] ]) ry = matrix([ [cos(beta), 0, -sin(beta), 0], [0, 1, 0, 0], [sin(beta), 0, cos(beta), 0], [0, 0, 0, 1] ]) rz = matrix([ [cos(gama), sin(gama), 0, 0], [-sin(gama), cos(gama), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1] ]) return trans * rx * ry * rz def main(): import getopt import pdb try : opts, args = getopt.getopt(sys.argv[1:], "wc") except getopt.GetoptError: pdb.set_trace() usage() sys.exit(1) for o, a in opts: if o == "-w": trans = world_transform(*args) if o == "-c": trans = camera_transform(*args) for x in sys.stdin.xreadlines(): if x[0].isdigit(): z = trans * matrix(x + " 1").transpose() print "\t".join( ["%f" % x for x in z.transpose().tolist()[0] ]) else: print x if __name__ == "__main__": main()