+# ---------------------------------------------------------------------
+#
+## Projections classes
+#
+# ---------------------------------------------------------------------
+
+class Projector:
+ """Calculate the projection of an object given the camera.
+
+ A projector is useful to so some per-object transformation to obtain the
+ projection of an object given the camera.
+
+ The main method is #doProjection# see the method description for the
+ parameter list.
+ """
+
+ def __init__(self, cameraObj, obMesh, canvasSize):
+ """Calculate the projection matrix.
+
+ The projection matrix depends, in this case, on the camera settings,
+ and also on object transformation matrix.
+ """
+
+ self.size = canvasSize
+
+ camera = cameraObj.getData()
+
+ aspect = float(canvasSize[0])/float(canvasSize[1])
+ near = camera.clipStart
+ far = camera.clipEnd
+
+ fovy = atan(0.5/aspect/(camera.lens/32))
+ fovy = fovy * 360/pi
+
+ # What projection do we want?
+ if camera.type:
+ m2 = self._calcOrthoMatrix(fovy, aspect, near, far, 17) #camera.scale)
+ else:
+ m2 = self._calcPerspectiveMatrix(fovy, aspect, near, far)
+
+ m1 = Matrix()
+ mP = Matrix()
+
+ # View transformation
+ cam = cameraObj.getInverseMatrix()
+ cam.transpose()
+
+ m1 = obMesh.getMatrix()
+ m1.transpose()
+
+ mP = cam * m1
+ mP = m2 * mP
+
+ self.projectionMatrix = mP
+
+ ##
+ # Public methods
+ #
+
+ def doProjection(self, v):
+ """Project the point on the view plane.
+
+ Given a vertex calculate the projection using the current projection
+ matrix.
+ """
+
+ # Note that we need the vertex expressed using homogeneous coordinates
+ p = self.projectionMatrix * Vector([v[0], v[1], v[2], 1.0])
+
+ mW = self.size[0]/2
+ mH = self.size[1]/2
+
+ if p[3]<=0:
+ p[0] = int(p[0]*mW)+mW
+ p[1] = int(p[1]*mH)+mH
+ else:
+ p[0] = int((p[0]/p[3])*mW)+mW
+ p[1] = int((p[1]/p[3])*mH)+mH
+
+ # For now we want (0,0) in the top-left corner of the canvas
+ # Mirror and translate along y
+ p[1] *= -1
+ p[1] += self.size[1]
+
+ return p
+
+ ##
+ # Private methods
+ #
+
+ def _calcPerspectiveMatrix(self, fovy, aspect, near, far):
+ """Return a perspective projection matrix."""
+
+ top = near * tan(fovy * pi / 360.0)
+ bottom = -top
+ left = bottom*aspect
+ right= top*aspect
+ x = (2.0 * near) / (right-left)
+ y = (2.0 * near) / (top-bottom)
+ a = (right+left) / (right-left)
+ b = (top+bottom) / (top - bottom)
+ c = - ((far+near) / (far-near))
+ d = - ((2*far*near)/(far-near))
+
+ m = Matrix(
+ [x, 0.0, a, 0.0],
+ [0.0, y, b, 0.0],
+ [0.0, 0.0, c, d],
+ [0.0, 0.0, -1.0, 0.0])
+
+ return m
+
+ def _calcOrthoMatrix(self, fovy, aspect , near, far, scale):
+ """Return an orthogonal projection matrix."""
+
+ top = near * tan(fovy * pi / 360.0) * (scale * 10)
+ bottom = -top
+ left = bottom * aspect
+ right= top * aspect
+ rl = right-left
+ tb = top-bottom
+ fn = near-far
+ tx = -((right+left)/rl)
+ ty = -((top+bottom)/tb)
+ tz = ((far+near)/fn)
+
+ m = Matrix(
+ [2.0/rl, 0.0, 0.0, tx],
+ [0.0, 2.0/tb, 0.0, ty],
+ [0.0, 0.0, 2.0/fn, tz],
+ [0.0, 0.0, 0.0, 1.0])
+
+ return m
+
+
+# ---------------------------------------------------------------------
+#
+## Mesh representation class
+#
+# ---------------------------------------------------------------------
+
+# TODO: a class to represent the needed properties of a 2D vector image
+# Just use a NMesh structure?
+
+
+# ---------------------------------------------------------------------
+#
+## Vector Drawing Classes
+#
+# ---------------------------------------------------------------------
+
+## A generic Writer
+
+class VectorWriter:
+ """
+ A class for printing output in a vectorial format.
+
+ Given a 2D representation of the 3D scene the class is responsible to
+ write it is a vector format.
+
+ Every subclasses of VectorWriter must have at last the following public
+ methods:
+ - printCanvas(mesh) --- where mesh is as specified before.
+ """
+
+ def __init__(self, fileName, canvasSize):
+ """Open the file named #fileName# and set the canvas size."""
+
+ self.file = open(fileName, "w")
+ print "Outputting to: ", fileName
+
+ self.canvasSize = canvasSize
+
+
+ ##
+ # Public Methods
+ #
+
+ def printCanvas(mesh):
+ return
+
+ ##
+ # Private Methods
+ #