Changeset 703
- Timestamp:
- 09/16/03 13:20:13 (5 years ago)
- Files:
-
- trunk/RBRapier/RBRapier/Formats/SVG/RapierRenderItems.py (modified) (1 diff)
- trunk/RBRapier/RBRapier/Renderer/AttributeMgr.py (modified) (1 diff)
- trunk/RBRapier/RBRapier/Renderer/Environment/Selection.py (modified) (3 diffs)
- trunk/RBRapier/RBRapier/Renderer/StateMgr.py (modified) (2 diffs)
- trunk/RBRapier/RBRapier/Renderer/View/Transformations.py (modified) (14 diffs)
- trunk/RBRapier/RBRapier/Tools/Animation/Navigation.py (modified) (1 diff)
- trunk/RBRapier/RBRapier/Tools/Projections.py (modified) (3 diffs)
- trunk/RBRapier/RBRapier/Tools/Quaternion.py (modified) (4 diffs)
- trunk/RBRapier/RBRapier/Tools/RectangleBase.py (modified) (4 diffs)
- trunk/RBRapier/RBRapier/Tools/Transformations.py (modified) (21 diffs)
- trunk/RBRapier/RBRapier/Tools/Transformations2d.py (modified) (13 diffs)
- trunk/RBRapier/RBRapier/Tools/Vector.py (modified) (1 diff)
- trunk/RBRapier/RBRapier/Tools/ViewBox.py (modified) (5 diffs)
- trunk/RBRapier/demo/Cube/cubescene.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/RBRapier/RBRapier/Formats/SVG/RapierRenderItems.py
r692 r703 165 165 return result 166 166 fromSVGTransforms = classmethod(fromSVGTransforms) 167 168 def TransformPoints(self, points):169 # add the homogeneous coordinate170 matrix = self.asArray3x3()171 points = Numeric.concatenate((points, Numeric.ones((len(points), 1), _NumericType)), 1)172 points = Numeric.transpose(Numeric.dot(matrix, Numeric.transpose(points)))173 return points[:,:-1] # trim the homogenous coordinate -- assumes that they are all still 1.174 167 175 168 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ trunk/RBRapier/RBRapier/Renderer/AttributeMgr.py
r688 r703 37 37 """Encapsulates a single collection of attribute changes""" 38 38 39 class AttributeTrackerBase(DynamicBitmaskChangeTracker): 40 pass 41 42 class AttributeTracker(AttributeTrackerBase): 39 class AttributeTracker(DynamicBitmaskChangeTracker): 43 40 _ElementAttributeName = 'AttributeChange' 44 41 45 class ClientAttributeTracker(AttributeTrackerBase): 46 _ElementAttributeName = 'ClientAttributeChange' 42 class AttributeEffector(AttributeTracker): 43 def SequenceAdd(self, Sequence): 44 DynamicBitmaskChangeTracker.SequenceAdd(self, Sequence) 45 Sequence.OnBeginExecute.Add(self.GLSelect) 46 Sequence.OnEndExecute.Add(self.GLDeselect) 47 47 48 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 49 50 class AttributeBaseEffector(AttributeTrackerBase): 51 def SequenceAdd(self, Sequence): 52 AttributeTrackerBase.SequenceAdd(self, Sequence) 53 Sequence.OnBeginExecute.Add(self.OnBeginExecute) 54 Sequence.OnEndExecute.Add(self.OnEndExecute) 55 56 class AttributeEffector(AttributeBaseEffector): 57 _ElementAttributeName = 'AttributeChange' 58 59 def OnBeginExecute(self, context): 48 def GLSelect(self, context): 60 49 GL.glPushAttrib(self.Bitmask) 61 50 62 def OnEndExecute(self, context):51 def GLDeselect(self, context): 63 52 GL.glPopAttrib() 64 53 65 54 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 55 #~ Client Attributes 56 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 66 57 67 class ClientAttributeEffector(AttributeBaseEffector): 58 class ClientAttributeChangeElement(BitmaskChangeElement): 59 """Encapsulates a single collection of client attribute changes""" 60 61 class DynamicClientAttributeChangeElement(DynamicBitmaskChangeElement): 62 """Encapsulates a single collection of client attribute changes""" 63 64 class ClientAttributeTracker(DynamicBitmaskChangeTracker): 68 65 _ElementAttributeName = 'ClientAttributeChange' 69 66 70 def OnBeginExecute(self, context): 67 class ClientAttributeEffector(ClientAttributeTracker): 68 def SequenceAdd(self, Sequence): 69 DynamicBitmaskChangeTracker.SequenceAdd(self, Sequence) 70 Sequence.OnBeginExecute.Add(self.GLSelect) 71 Sequence.OnEndExecute.Add(self.GLDeselect) 72 73 def GLSelect(self, context): 71 74 GL.glPushClientAttrib(self.Bitmask) 72 75 73 def OnEndExecute(self, context):76 def GLDeselect(self, context): 74 77 GL.glPopClientAttrib() 75 78 trunk/RBRapier/RBRapier/Renderer/Environment/Selection.py
r702 r703 30 30 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 31 31 32 class GLSelectionBuffer(object):32 class SelectionBuffer(object): 33 33 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 34 34 #~ Constants / Variables / Etc. 35 35 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 36 36 37 GLSelectBufferSize = 127 38 _selection = None 37 SelectBufferSize = 127 38 _selection = [] 39 40 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 41 #~ Definitions 42 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 43 44 class SelectionEntry(object): 45 __slots__ = '_z0', '_z1', 'namepath' 46 47 def __init__(self, z0, z1, namepath=()): 48 self._z0 = z0 49 self._z1 = z1 50 self.namepath = namepath 51 52 def __repr__(self): 53 return "<Selection z=%s names=%s>" % (self.GetZRange(), self.namepath) 54 55 def GetZRange(self): 56 maxvalue = 0xffffffffL 57 return float(self._z0)/maxvalue, float(self._z1)/maxvalue 58 59 def Resolve(self, resolver): 60 result = resolver 61 namepath = self.namepath[:] 62 try: 63 for step in namepath: 64 result = result[step] 65 except LookupError: 66 pass 67 return result 39 68 40 69 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 43 72 44 73 def GLSelect(self, context): 45 del self. GLSelection46 GL.gl GLSelectBuffer(self.GLSelectBufferSize)74 del self.Selection 75 GL.glSelectBuffer(self.SelectBufferSize) 47 76 GL.glRenderMode(GL.GL_SELECT) 48 77 GL.glInitNames() 49 GL.glPushName(0)50 78 51 79 def GLDeselect(self, context): 52 80 rawselection = GL.glRenderMode(GL.GL_RENDER) 53 self._SetRaw GLSelection(rawselection)81 self._SetRawSelection(rawselection) 54 82 55 83 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 57 85 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 58 86 59 def Get GLSelection(self):87 def GetSelection(self): 60 88 return self._selection 61 def SetGLSelection(self, value): 62 self._selection = value 63 def DelGLSelection(self): 89 def SetSelection(self, value=()): 90 self._selection = list(value) 91 92 def DelSelection(self): 64 93 try: del self._selection 65 94 except AttributeError: pass 66 def _SetRawGLSelection(self, rawselection): 67 print "TODO: Parse selection buffer" 68 print "_SetRawGLSelection:", self.GLSelectionBuffer 69 GLSelection = property(GetGLSelection, SetGLSelection, DelGLSelection) 95 def _SetRawSelection(self, rawselection): 96 if rawselection: 97 from itertools import starmap 98 self.SetSelection(starmap(self.SelectionEntry, rawselection)) 99 else: 100 self.SetSelection(()) 70 101 102 Selection = property(GetSelection, SetSelection, DelSelection) 103 trunk/RBRapier/RBRapier/Renderer/StateMgr.py
r432 r703 26 26 from OpenGL import GL 27 27 from RBFoundation.Aspects import Aspect 28 from RBRapier.Renderer.AttributeMgr import AttributeChangeElement 28 from RBRapier.Renderer.AttributeMgr import AttributeChangeElement, ClientAttributeChangeElement 29 29 30 30 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 112 112 113 113 class ClientStateManager(StateManagerBase): 114 #AttributeChange = AttributeChangeElement(GL.GL_CLIENT_ENABLE_BIT)114 ClientAttributeChange = ClientAttributeChangeElement(GL.GL_CLIENT_VERTEX_ARRAY_BIT) 115 115 116 116 def _SetState(self, state, enabled): trunk/RBRapier/RBRapier/Renderer/View/Transformations.py
r702 r703 27 27 from RBRapier.Tools import Projections 28 28 from RBRapier.Tools import Quaternion 29 from RBRapier.Tools import RectangleBase 29 30 30 31 from Numeric import transpose … … 33 34 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 34 35 #~ Definitions 36 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 37 38 def GLExecuteAsMatrix(self, context): 39 GL.glMultMatrixd(transpose(self.asArray4x4()).tolist()) 40 def GLExecuteLoadMatrix(self, context): 41 GL.glLoadMatrixd(transpose(self.asArray4x4()).tolist()) 42 35 43 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 36 44 … … 43 51 Mode = None 44 52 Save = 0 53 _save_matrix = None 45 54 46 55 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 56 65 57 66 def GLSelect(self, context): 58 if self.Mode: GL.glMatrixMode(self.Mode) 59 if self.Save: GL.glPushMatrix() 60 self.GLExecute(context) 61 if self.Mode: GL.glMatrixMode(GL.GL_MODELVIEW) 67 if self.Mode: 68 GL.glMatrixMode(self.Mode) 69 self._SaveMatrix() 70 self.GLExecute(context) 71 GL.glMatrixMode(GL.GL_MODELVIEW) 72 else: 73 self._SaveMatrix() 74 self.GLExecute(context) 62 75 63 76 def GLDeselect(self, context): … … 65 78 if self.Mode: 66 79 GL.glMatrixMode(self.Mode) 67 GL.glPopMatrix()80 self._RestoreMatrix() 68 81 GL.glMatrixMode(GL.GL_MODELVIEW) 69 82 else: 70 GL.glPopMatrix() 83 self._RestoreMatrix() 84 85 def _SaveMatrix(self): 86 try: 87 GL.glPushMatrix() 88 except GL.GLerror: 89 self._save_matrix = GL.glGetDouble(Gl.GL_MODELVIEW_MATRIX) 90 91 def _RestoreMatrix(self): 92 oldmatrix = self._save_matrix 93 if oldmatrix is None: 94 GL.glPopMatrix() 95 else: 96 GL.glLoadMatrixd(oldmatrix) 97 del self._save_matrix 71 98 72 99 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 84 111 85 112 class Matrix(Transformations.Matrix): 86 def GLExecute(self, context): 87 GL.glMultMatrixd(transpose(self.matrix).tolist()) 113 GLExecute = GLExecuteAsMatrix 88 114 89 115 class MatrixMgd(ManagedTransformationMixin, Matrix): … … 91 117 92 118 class LoadMatrix(Transformations.Matrix): 93 def GLExecute(self, context): 94 GL.glLoadMatrixd(transpose(self.matrix).tolist()) 119 GLExecute = GLExecuteLoadMatrix 95 120 96 121 class LoadMatrixMgd(ManagedTransformationMixin, LoadMatrix): … … 133 158 134 159 class Quaternion(Quaternion.Quaternion): 135 def GLExecute(self, context): 136 GL.glMultMatrixd(transpose(self.asArray4x4()).tolist()) 160 GLExecute = GLExecuteAsMatrix 137 161 138 162 class QuaternionMgd(ManagedTransformationMixin, Quaternion): … … 140 164 141 165 class LinearMappingMatrix(Transformations.LinearMappingMatrix): 142 def GLExecute(self, context): 143 GL.glMultMatrixd(transpose(self.asArray4x4()).tolist()) 166 GLExecute = GLExecuteAsMatrix 144 167 145 168 class LinearMappingMatrixMgd(ManagedTransformationMixin, LinearMappingMatrix): 169 pass 170 171 class RectangleMappingMatrix(RectangleBase.RectangleMappingMatrix3dh): 172 GLExecute = GLExecuteAsMatrix 173 174 class RectangleMappingMatrixMgd(ManagedTransformationMixin, RectangleMappingMatrix): 146 175 pass 147 176 … … 163 192 164 193 class Shear(Transformations.Shear): 165 def GLExecute(self, context): 166 GL.glMultMatrixd(transpose(self.matrix).tolist()) 194 GLExecute = GLExecuteAsMatrix 167 195 168 196 class ShearMgd(ManagedTransformationMixin, Shear): … … 170 198 171 199 class Skew(Transformations.Skew): 172 def GLExecute(self, context): 173 GL.glMultMatrixd(transpose(self.matrix).tolist()) 200 GLExecute = GLExecuteAsMatrix 174 201 175 202 class SkewMgd(ManagedTransformationMixin, Skew): … … 180 207 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 181 208 182 class Orthographic(Projections.Orthographic): 183 def GLExecute(self, context): 209 class ProjectionModelMixin(object): 210 ProjectionModel = None 211 212 def ApplyProjectionModel(self): 213 projmodel = self.ProjectionModel 214 if projmodel is not None: 215 projmodel.UpdateProjection(self) 216 217 class Orthographic(Projections.Orthographic, ProjectionModelMixin): 218 def GLExecute(self, context): 219 self.ApplyProjectionModel() 184 220 GL.glOrtho(self.Left, self.Right, self.Bottom, self.Top, self.Near, self.Far) 185 221 … … 187 223 pass 188 224 189 class Frustum(Projections.Frustum): 190 def GLExecute(self, context): 225 class Frustum(Projections.Frustum, ProjectionModelMixin): 226 def GLExecute(self, context): 227 self.ApplyProjectionModel() 191 228 GL.glFrustum(self.Left, self.Right, self.Bottom, self.Top, self.Near, self.Far) 192 229 … … 194 231 pass 195 232 196 class Perspective(Projections.Perspective): 197 def GLExecute(self, context): 233 class Perspective(Projections.Perspective, ProjectionModelMixin): 234 def GLExecute(self, context): 235 self.ApplyProjectionModel() 198 236 GL.glFrustum(self.Left, self.Right, self.Bottom, self.Top, self.Near, self.Far) 199 237 trunk/RBRapier/RBRapier/Tools/Animation/Navigation.py
r702 r703 113 113 viewbox = self.GetViewBox().copy() # make a copy so we don't "erode" 114 114 viewbox.SetAspectRatio(aspectratio) 115 viewbox.asProjection(self.projection)115 self.projection.ProjectionModel = viewbox 116 116 self.viewboxes['displayed'] = viewbox 117 117 trunk/RBRapier/RBRapier/Tools/Projections.py
r692 r703 124 124 def SetAspectRatio(self, value, byWidth=None): 125 125 self.SetDimensionsAspectRatio(self.Width, self.Height, value) 126 #if byWidth is None:127 # byWidth = value < 1128 #if byWidth:129 # self.Width = self.Height / float(value)130 #else:131 # self.Height = self.Width * float(value)132 126 AspectRatio = property(GetAspectRatio, SetAspectRatio) 133 127 … … 180 174 [0., 0., -2./deprojh, zoff], 181 175 [0., 0., 0., 1.]], 182 Numeric.Float32)176 self.NumericType) 183 177 return result 184 178 … … 281 275 [0., 0., -zoff, ugly], 282 276 [0., 0., -1., 0.]], 283 Numeric.Float32)277 self.NumericType) 284 278 return result 285 279 trunk/RBRapier/RBRapier/Tools/Quaternion.py
r658 r703 43 43 44 44 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 45 #~ Constants / Variables / Etc.46 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~47 48 _NumericType = Numeric.Float49 50 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~51 45 #~ Definitions 52 46 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 115 109 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 116 110 117 NumericType = _NumericType111 NumericType = Numeric.Float 118 112 119 113 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 240 234 [ 0.32943134, -0.24949789, 0.92006655, 0. ], 241 235 [ 0. , 0. , 0. , 1. ]]) 236 >>> a2.asInverse4x4() 237 array([[ 0.92006655, 0.32943134, -0.24949789, 0. ], 238 [-0.24949789, 0.92006655, 0.32943134, 0. ], 239 [ 0.32943134, -0.24949789, 0.92006655, 0. ], 240 [ 0. , 0. , 0. , 1. ]]) 242 241 """ 243 242 s, a, b, c = self._array … … 248 247 [0., 0., 0., 0.]], self.NumericType) 249 248 return result 249 250 def asInverse4x4(self): 251 return self.Inverse().asArray4x4() 250 252 251 253 def Magnitude(self, squared=0): trunk/RBRapier/RBRapier/Tools/RectangleBase.py
r691 r703 27 27 import Numeric 28 28 from Transformations import TransformPrimitive3dh 29 from Transformations2d import TransformPrimitive2dh 30 from Vector import LinearMapping, LinearDimMapping 29 31 30 32 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 62 64 63 65 def __init__(self, Rectangle=None): 64 self.Rectangle = Rectangle or [0, 0, 1, 1] 66 self.Rectangle = Rectangle or [-1, -1, 2, 2] 67 68 def __repr__(self): 69 return "<x:%s y:%s w:%s h:%s>" % tuple(self.Rectangle) 65 70 66 71 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 103 108 AspectRatio = property(GetAspectRatio) 104 109 110 def GetBox(self): 111 x, y, w, h = self.Rectangle() 112 return (x, y), (x+w, y+h) 113 def SetBox(self, box): 114 (x0, y0), (x1, y1) = box 115 w, h = x1-x0, y1-y0 116 return self.SetRectangle(x0,y0,w,h) 117 118 def GetPts(self): 119 return self.GetBox() 120 def SetPts(self, pos0, pos1): 121 return self.SetBox((pos0, pos1)) 122 123 def GetRectangle(self): 124 return self.Rectangle 105 125 def SetRectangle(self, *args): 106 126 if len(args) == 1: args = args[0] 107 127 self.Rectangle[:] = self.Rectangle.__class__(args) 108 128 109 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 110 111 class RectangleArrayBase(TransformPrimitive3dh, RectangleBase): 112 """ 113 >>> r = RectangleArrayBase([0, 0, 10, 10]) 114 >>> r.asArray4x4() 115 array([[5, 0, 0, 5], 116 [0, 5, 0, 5], 117 [0, 0, 1, 0], 118 [0, 0, 0, 1]]) 119 >>> r.asInverse4x4() 120 array([[ 0.2, 0. , 0. , -1. ], 121 [ 0. , 0.2, 0. , -1. ], 122 [ 0. , 0. , 1. , 0. ], 123 [ 0. , 0. , 0. , 1. ]]) 124 """ 125 129 def MapPointTo(self, point, *args, **kw): 130 return self.MapPoints([point], *args, **kw) 131 132 def MapPointsTo(self, points, xspan=(-1., 1.), yspan=None, flipx=False, flipy=False): 133 xspan, yspan = xspan or yspan, yspan or xspan 134 if flipx: xspan = xspan[1], xspan[0] 135 if flipy: yspan = yspan[1], yspan[0] 136 x0, y0, w, h = self.Rectangle 137 xfn = LinearMapping((x0, x0+w), xspan or yspan) 138 yfn = LinearMapping((y0, y0+h), yspan or xspan) 139 return [(xfn(x),yfn(y)) for x,y in points] 140 141 def MapPointsFrom(self, point, *args, **kw): 142 return self.MapPointsFrom([point], *args, **kw) 143 144 def MapPointsFrom(self, points, xspan=(-1., 1.), yspan=None, flipx=False, flipy=False): 145 xspan, yspan = xspan or yspan, yspan or xspan 146 if flipx: xspan = xspan[1], xspan[0] 147 if flipy: yspan = yspan[1], yspan[0] 148 x0, y0, w, h = self.Rectangle 149 xfn = LinearMapping(xspan, (x0, x0+w)) 150 yfn = LinearMapping(yspan, (y0, y0+h)) 151 return [(xfn(x),yfn(y)) for x,y in points] 152 153 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 154 155 class RectangleMappingBase(object): 156 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 157 #~ Constants / Variables / Etc. 158 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 159 160 _fromrect = RectangleBase() 161 _torect = RectangleBase() 162 163 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 164 #~ Public Methods 165 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 166 167 def __init__(self, fromrect=None, torect=None): 168 if isinstance(fromrect, (list, tuple)): 169 self._fromrect = RectangleBase(fromrect) 170 elif fromrect is not None: 171 self._fromrect = fromrect 172 if isinstance(torect, (list, tuple)): 173 self._torect = RectangleBase(torect) 174 elif torect is not None: 175 self._torect = torect 176 177 def __repr__(self): 178 return "<%s from:%r to:%r>" % (self.__class__.__name__, self.FromRect, self.ToRect) 179 180 def GetToRect(self): 181 return self._torect 182 def SetToRect(self, torect): 183 self._torect = torect 184 def DelToRect(self): 185 del self._torect 186 ToRect = property(GetToRect, SetToRect, DelToRect) 187 188 def GetFromRect(self): 189 return self._fromrect 190 def SetFromRect(self, fromrect): 191 self._fromrect = fromrect 192 def DelFromRect(self): 193 del self._fromrect 194 FromRect = property(GetFromRect, SetFromRect, DelFromRect) 195 196 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 197 198 class RectangleMappingMatrix3dh(RectangleMappingBase, TransformPrimitive3dh): 126 199 def asArray4x4(self): 127 halfwidth = self.Rectangle[2] / 2128 halfheight = self.Rectangle[3] / 2129 xoff = self.Rectangle[0] + halfwidth130 yoff = self.Rectangle[1] + halfheight131 result = Numeric.asarray([ 132 [ halfwidth, 0, 0, xoff],133 [ 0, halfheight, 0, yoff],134 [ 0, 0, 1,0],135 [ 0, 0, 0, 1]])200 x0f, y0f, wf, hf = map(float, self.GetFromRect().GetRectangle()) 201 x0t, y0t, wt, ht = map(float, self.GetToRect().GetRectangle()) 202 sx, tx = LinearDimMapping((x0f, wf), (x0t, wt), False) 203 sy, ty = LinearDimMapping((y0f, hf), (y0t, ht), False) 204 result = Numeric.asarray([ 205 [sx, 0, 0, tx], 206 [ 0, sy, 0, ty], 207 [ 0, 0, 1, 0], 208 [ 0, 0, 0, 1]], self.NumericType) 136 209 return result 137 210 138 211 def asInverse4x4_(self): 139 halfwidth = 2. / self.Rectangle[2] 140 halfheight = 2. / self.Rectangle[3] 141 result = Numeric.asarray([ 142 [halfwidth, 0, 0, -1], 143 [0, halfheight, 0, -1], 144 [0, 0, 1, 0], 145 [0, 0, 0, 1]]) 212 x0f, y0f, wf, hf = map(float, self.GetFromRect().GetRectangle()) 213 x0t, y0t, wt, ht = map(float, self.GetToRect().GetRectangle()) 214 sx, tx = LinearDimMapping((x0t, wt),(x0f, wf), False) 215 sy, ty = LinearDimMapping((y0t, ht),(y0f, hf), False) 216 result = Numeric.asarray([ 217 [sx, 0, 0, tx], 218 [ 0, sy, 0, ty], 219 [ 0, 0, 1, 0], 220 [ 0, 0, 0, 1]], self.NumericType) 221 return result 222 223 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 224 225 class RectangleMappingMatrix2dh(RectangleMappingBase, TransformPrimitive2dh): 226 def asArray4x4(self): 227 x0f, y0f, wf, hf = map(float, self.GetFromRect().GetRectangle()) 228 x0t, y0t, wt, ht = map(float, self.GetToRect().GetRectangle()) 229 sx, tx = LinearDimMapping((x0f, wf), (x0t, wt), False) 230 sy, ty = LinearDimMapping((y0f, hf), (y0t, ht), False) 231 result = Numeric.asarray([ 232 [sx, 0, tx], 233 [ 0, sy, ty], 234 [ 0, 0, 1]], self.NumericType) 235 return result 236 237 def asInverse4x4_(self): 238 x0f, y0f, wf, hf = map(float, self.GetFromRect().GetRectangle()) 239 x0t, y0t, wt, ht = map(float, self.GetToRect().GetRectangle()) 240 sx, tx = LinearDimMapping((x0t, wt),(x0f, wf), False) 241 sy, ty = LinearDimMapping((y0t, ht),(y0f, hf), False) 242 result = Numeric.asarray([ 243 [sx, 0, tx], 244 [ 0, sy, ty], 245 [ 0, 0, 1]], self.NumericType) 146 246 return result 147 247 … … 152 252 if __name__=='__main__': 153 253 print "Testing..." 154 import doctest, RectangleBase 155 doctest.testmod( RectangleBase)254 import doctest, RectangleBase as _TestMod 255 doctest.testmod(_TestMod) 156 256 print "Test complete." 157 257 trunk/RBRapier/RBRapier/Tools/Transformations.py
r660 r703 34 34 _rad2deg = 180.0/Numeric.pi 35 35 _deg2rad = Numeric.pi/180.0 36 _NumericType = Numeric.Float37 36 38 37 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 41 40 42 41 class TransformPrimitive3dh(object): 42 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 43 #~ Constants / Variables / Etc. 44 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 45 46 NumericType = Numeric.Float 47 48 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 49 #~ Public Methods 50 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 51 43 52 def asArray4x4(self): 44 53 """Returns the transformation in 4x4 Numeric array form. … … 64 73 return Numeric.dot(other, self.asArray4x4()) 65 74 else: return other * self.asArray4x4() 75 76 def asPoints(self, points, includeDimRestore=False): 77 dims = len(points[0]) 78 if dims not in (2,3,4): 79 raise ValueError, "Points are not of right dimension -- must be 2, 3 or 4, but found %d" % dims 80 if dims == 2: 81 ones = Numeric.ones((len(points), 1), self.NumericType) 82 zeros = Numeric.zeros((len(points), 1), self.NumericType) 83 points = Numeric.concatenate((points, zeros, ones), 1) 84 correctdims = lambda pts: pts[:,:-2] # trim the z and homogenous coordinates -- assumes that they are all still 1. 85 elif dims == 3: 86 # add the homogeneous coordinate 87 ones = Numeric.ones((len(points), 1), self.NumericType) 88 points = Numeric.concatenate((points, ones), 1) 89 correctdims = lambda pts: pts[:,:-1] # trim the homogenous coordinate -- assumes that they are all still 1. 90 else: 91 correctdims = lambda pts: pts 92 93 if includeDimRestore: 94 return points, correctdims 95 else: return points 96 97 def TransformPoints(self, points, bInverse=False): 98 if bInverse: 99 matrix = self.asInverse4x4() 100 else: 101 matrix = self.asArray4x4() 102 103 points, correctdims = self.asPoints(points, True) 104 result = Numeric.transpose(Numeric.dot(matrix, Numeric.transpose(points))) 105 return correctdims(result) 66 106 67 107 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 90 130 >>> n = c.Add(Frustum()) 91 131 >>> n = c.Add(Perspective()) 92 >>> c.asArray4x4() 93 array([[ 44.29636998, 125.91233332, -57.83760035, -29.67837068], 94 [ 201.84505811, 24.85095591, -347.25496638, -180.08553265], 95 [ 549.72683721, 552.42084495, -107.81043034, -54.78269031], 96 [ 0. , 0. , 1.22222221, 1.11111116]]) 97 >>> c.asInverse4x4() 98 array([[-1.01736204e-002, 9.93235414e-004, 2.27417457e-003, 1.36462590e-003], 99 [ 9.06907225e-003, -1.47085536e-003, -1.90716711e-004, -5.55521140e-003], 100 [-1.22560268e-002, -5.60517231e-003, 3.04564689e-003, -1.08567050e+000], 101 [ 1.34816287e-002, 6.16568918e-003, -3.35021138e-003, 2.09423744e+000]]) 132 >>> c.asArray4x4()[0][0] 133 44.29636824137652 134 >>> c.asInverse4x4()[0][0] 135 -0.010173620849422499 102 136 """ 103 137 … … 162 196 def asArray4x4(self): 163 197 """Returns the transformation in 4x4 Numeric array form""" 164 r = Numeric.identity(4, _NumericType)198 r = Numeric.identity(4, self.NumericType) 165 199 for xform in self.collection: 166 200 r = Numeric.dot(r, xform.asArray4x4()) … … 195 229 196 230 def __init__(self, matrix=None): 197 if matrix: self.matrix = Numeric.asarray(matrix, _NumericType)198 else: self.matrix = Numeric.identity(4, _NumericType)231 if matrix: self.matrix = Numeric.asarray(matrix, self.NumericType) 232 else: self.matrix = Numeric.identity(4, self.NumericType) 199 233 assert self.matrix.shape == (4,4) 200 234 … … 233 267 [ 0., 0., 1., 0.], 234 268 [ 0., 0., 0., 1.]]) 269 270 >>> i.TransformPoints([(2., 3., 4., 1.)]) 271 array([ [ 2., 3., 4., 1.]]) 272 >>> i.TransformPoints([(2., 3., 4., 1.)], True) 273 array([ [ 2., 3., 4., 1.]]) 274 275 >>> i.TransformPoints([(2., 3., 4.)]) 276 array([ [ 2., 3., 4.]]) 277 >>> i.TransformPoints([(2., 3., 4.)], True) 278 array([ [ 2., 3., 4.]]) 279 280 >>> i.TransformPoints([(2., 3.)]) 281 array([ [ 2., 3.]]) 282 >>> i.TransformPoints([(2., 3.)], True) 283 array([ [ 2., 3.]]) 235 284 """ 236 285 … … 242 291 243 292 def asArray4x4(self): 244 return Numeric.identity(4, _NumericType)293 return Numeric.identity(4, self.NumericType) 245 294 246 295 def asInverse4x4(self): 247 return Numeric.identity(4, _NumericType)296 return Numeric.identity(4, self.NumericType) 248 297 249 298 def Inverse(self): … … 269 318 """ 270 319 271 Direction = Vector3HProperty('Direction', NumericType= _NumericType)320 Direction = Vector3HProperty('Direction', NumericType=TransformPrimitive3dh.NumericType) 272 321 273 322 def __init__(self, Direction=(0,0,0)): … … 278 327 279 328 def asArray4x4(self): 280 result = Numeric.identity(4, _NumericType)329 result = Numeric.identity(4, self.NumericType) 281 330 result[:, 3] = self.Direction 282 331 return result 283 332 284 333 def asInverse4x4(self): 285 result = Numeric.identity(4, _NumericType)334 result = Numeric.identity(4, self.NumericType) 286 335 result[:, 3] = -self.Direction 287 336 return result … … 309 358 """ 310 359 311 Scale = Vector3HProperty('Scale', (1.,1.,1.,1.), NumericType= _NumericType)360 Scale = Vector3HProperty('Scale', (1.,1.,1.,1.), NumericType=TransformPrimitive3dh.NumericType) 312 361 313 362 def __init__(self, Scale=1.0): … … 323 372 324 373 def asArray4x4(self): 325 result = Numeric.identity(4, _NumericType)374 result = Numeric.identity(4, self.NumericType) 326 375 for idx in range(4-1): result[idx,idx] = self.Scale[idx] 327 376 return result 328 377 329 378 def asInverse4x4(self): 330 result = Numeric.identity(4, _NumericType)379 result = Numeric.identity(4, self.NumericType) 331 380 for idx in range(4-1): result[idx,idx] = 1./self.Scale[idx] 332 381 return result … … 393 442 """ 394 443 395 Axis = UnitVector3HProperty('Axis', NumericType= _NumericType)444 Axis = UnitVector3HProperty('Axis', NumericType=TransformPrimitive3dh.NumericType) 396 445 397 446 def __init__(self, angle=0.0, axis=(0.0, 0.0, 1.0)): … … 411 460 u = self.Axis[:-1] 412 461 uut = Numeric.outerproduct(u, u) 413 M = Numeric.identity(3, _NumericType) - uut414 S = Numeric.asarray([[0., -u[2], u[1]], [u[2], 0., -u[0]], [-u[1], u[0], 0.]], _NumericType)462 M = Numeric.identity(3, self.NumericType) - uut 463 S = Numeric.asarray([[0., -u[2], u[1]], [u[2], 0., -u[0]], [-u[1], u[0], 0.]], self.NumericType) 415 464 radians = self.Radians 416 465 R = uut + Numeric.cos(radians) * M + Numeric.sin(radians) * S 417 result = Numeric.identity(4, _NumericType)466 result = Numeric.identity(4, self.NumericType) 418 467 result[:3,:3] = R 419 468 return result … … 439 488 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 440 489 441 Eye = Vector3Property('Eye', (0.,0.,1.), NumericType= _NumericType)442 Center = Vector3Property('Center', (0.,0.,0.), NumericType= _NumericType)443 Up = UnitVector3Property('Up', (0.,1.,0.), NumericType= _NumericType)490 Eye = Vector3Property('Eye', (0.,0.,1.), NumericType=TransformPrimitive3dh.NumericType) 491 Center = Vector3Property('Center', (0.,0.,0.), NumericType=TransformPrimitive3dh.NumericType) 492 Up = UnitVector3Property('Up', (0.,1.,0.), NumericType=TransformPrimitive3dh.NumericType) 444 493 445 494 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 466 515 U_ = S.Cross3(L) 467 516 468 result = Numeric.identity(4, _NumericType)517 result = Numeric.identity(4, self.NumericType) 469 518 result[0,:-1] = S 470 519 result[1,:-1] = U_ 471 520 result[2,:-1] = -L 472 xlate = Numeric.identity(4, _NumericType)521 xlate = Numeric.identity(4, self.NumericType) 473 522 xlate[:-1,3] = -E 474 523 return Numeric.dot(result, xlate) … … 494 543 495 544 _RhoThetaPhi = 1., 0., 90 496 Center = Vector3Property('Center', (0.,0.,0.), NumericType= _NumericType)497 Spherical = Vector3Property('Spherical', (0.,0.,1.), NumericType= _NumericType)498 Up = UnitVector3Property('Up', (0.,1.,0.), NumericType= _NumericType)545 Center = Vector3Property('Center', (0.,0.,0.), NumericType=TransformPrimitive3dh.NumericType) 546 Spherical = Vector3Property('Spherical', (0.,0.,1.), NumericType=TransformPrimitive3dh.NumericType) 547 Up = UnitVector3Property('Up', (0.,1.,0.), NumericType=TransformPrimitive3dh.NumericType) 499 548 500 549 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 604 653 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 605 654 606 def LinearMapping((low_from, hi_from), (low_to, hi_to)):607 """608 >>> LinearMapping((10.,20.), (-1.,1.))609 (0.20000000000000001, -3.0)610 >>> LinearMapping((-1.,1.), (10.,20.))611 (5.0, 15.0)612 """613 wr = (hi_to - low_to)/(hi_from - low_from)614 return (wr, low_to - low_from*wr)615 616 655 class LinearMappingMatrix(Matrix): 617 656 """ … … 647 686 return "<LinearMappingMatrix: %s >" % ([(self.fromX, self.toX), (self.fromY, self.toY), (self.fromZ, self.toZ)],) 648 687 649 def asArray4x4(self): 650 Xs,Xt = LinearMapping(self.fromX, self.toX) 651 Ys,Yt = LinearMapping(self.fromY, self.toY) 652 Zs,Zt = LinearMapping(self.fromZ, self.toZ) 688 def SetFromRect(self, rect): 689 self.fromX = [rect[0], rect[0] + rect[2]] 690 self.fromY = [rect[1], rect[1] + rect[3]] 691 692 def SetToRect(self, rect): 693 self.toX = [rect[0], rect[0] + rect[2]] 694 self.toY = [rect[1], rect[1] + rect[3]] 695 696 def asArray4x4(self): 697 Xs,Xt = LinearMapping(self.fromX, self.toX, False) 698 Ys,Yt = LinearMapping(self.fromY, self.toY, False) 699 Zs,Zt = LinearMapping(self.fromZ, self.toZ, False) 653 700 result = Numeric.asarray([ 654 701 [Xs, 0, 0, Xt], … … 656 703 [0, 0, Zs, Zt], 657 704 [0, 0, 0, 1]], 658 _NumericType)705 self.NumericType) 659 706 return result 660 707 661 708 def asInverse4x4(self): 662 Xs,Xt = LinearMapping(self.toX, self.fromX )663 Ys,Yt = LinearMapping(self.toY, self.fromY )664 Zs,Zt = LinearMapping(self.toZ, self.fromZ )709 Xs,Xt = LinearMapping(self.toX, self.fromX, False) 710 Ys,Yt = LinearMapping(self.toY, self.fromY, False) 711 Zs,Zt = LinearMapping(self.toZ, self.fromZ, False) 665 712 result = Numeric.asarray([ 666 713 [Xs, 0, 0, Xt], … … 668 715 [0, 0, Zs, Zt], 669 716 [0, 0, 0, 1]], 670 _NumericType)717 self.NumericType) 671 718 return result 672 719 trunk/RBRapier/RBRapier/Tools/Transformations2d.py
r685 r703 34 34 _rad2deg = 180.0/Numeric.pi 35 35 _deg2rad = Numeric.pi/180.0 36 _NumericType = Numeric.Float 36 37 37 38 38 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 41 41 42 42 class TransformPrimitive2dh(object): 43 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 44 #~ Constants / Variables / Etc. 45 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 46 47 NumericType = Numeric.Float 48 49 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 50 #~ Public Methods 51 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 52 43 53 def asArray3x3(self): 44 54 """Returns the transformation in 3x3 Numeric array form. … … 64 74 return Numeric.dot(other, self.asArray3x3()) 65 75 else: return other * self.asArray3x3() 76 77 def asPoints(self, points, includeDimRestore=False): 78 dims = len(points[0]) 79 if dims not in (2,3):
