Changeset 703

Show
Ignore:
Timestamp:
09/16/03 13:20:13 (5 years ago)
Author:
sholloway
Message:

*** empty log message ***

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/RBRapier/RBRapier/Formats/SVG/RapierRenderItems.py

    r692 r703  
    165165        return result 
    166166    fromSVGTransforms = classmethod(fromSVGTransforms) 
    167  
    168     def TransformPoints(self, points): 
    169         # add the homogeneous coordinate 
    170         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. 
    174167 
    175168#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  • trunk/RBRapier/RBRapier/Renderer/AttributeMgr.py

    r688 r703  
    3737    """Encapsulates a single collection of attribute changes""" 
    3838 
    39 class AttributeTrackerBase(DynamicBitmaskChangeTracker): 
    40     pass 
    41  
    42 class AttributeTracker(AttributeTrackerBase): 
     39class AttributeTracker(DynamicBitmaskChangeTracker): 
    4340    _ElementAttributeName = 'AttributeChange' 
    4441 
    45 class ClientAttributeTracker(AttributeTrackerBase): 
    46     _ElementAttributeName = 'ClientAttributeChange' 
     42class AttributeEffector(AttributeTracker): 
     43    def SequenceAdd(self, Sequence): 
     44        DynamicBitmaskChangeTracker.SequenceAdd(self, Sequence) 
     45        Sequence.OnBeginExecute.Add(self.GLSelect) 
     46        Sequence.OnEndExecute.Add(self.GLDeselect) 
    4747 
    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): 
    6049        GL.glPushAttrib(self.Bitmask) 
    6150         
    62     def OnEndExecute(self, context): 
     51    def GLDeselect(self, context): 
    6352        GL.glPopAttrib() 
    6453 
    6554#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     55#~ Client Attributes 
     56#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    6657 
    67 class ClientAttributeEffector(AttributeBaseEffector): 
     58class ClientAttributeChangeElement(BitmaskChangeElement): 
     59    """Encapsulates a single collection of client attribute changes""" 
     60 
     61class DynamicClientAttributeChangeElement(DynamicBitmaskChangeElement): 
     62    """Encapsulates a single collection of client attribute changes""" 
     63 
     64class ClientAttributeTracker(DynamicBitmaskChangeTracker): 
    6865    _ElementAttributeName = 'ClientAttributeChange' 
    6966 
    70     def OnBeginExecute(self, context): 
     67class 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): 
    7174        GL.glPushClientAttrib(self.Bitmask) 
    7275         
    73     def OnEndExecute(self, context): 
     76    def GLDeselect(self, context): 
    7477        GL.glPopClientAttrib() 
    7578 
  • trunk/RBRapier/RBRapier/Renderer/Environment/Selection.py

    r702 r703  
    3030#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    3131 
    32 class GLSelectionBuffer(object): 
     32class SelectionBuffer(object): 
    3333    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    3434    #~ Constants / Variables / Etc.  
    3535    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    3636 
    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 
    3968 
    4069    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    4372 
    4473    def GLSelect(self, context): 
    45         del self.GLSelection 
    46         GL.glGLSelectBuffer(self.GLSelectBufferSize) 
     74        del self.Selection 
     75        GL.glSelectBuffer(self.SelectBufferSize) 
    4776        GL.glRenderMode(GL.GL_SELECT) 
    4877        GL.glInitNames() 
    49         GL.glPushName(0) 
    5078 
    5179    def GLDeselect(self, context): 
    5280        rawselection = GL.glRenderMode(GL.GL_RENDER) 
    53         self._SetRawGLSelection(rawselection) 
     81        self._SetRawSelection(rawselection) 
    5482 
    5583    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    5785    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    5886 
    59     def GetGLSelection(self): 
     87    def GetSelection(self): 
    6088        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): 
    6493        try: del self._selection 
    6594        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(()) 
    70101 
     102    Selection = property(GetSelection, SetSelection, DelSelection) 
     103 
  • trunk/RBRapier/RBRapier/Renderer/StateMgr.py

    r432 r703  
    2626from OpenGL import GL 
    2727from RBFoundation.Aspects import Aspect 
    28 from RBRapier.Renderer.AttributeMgr import AttributeChangeElement 
     28from RBRapier.Renderer.AttributeMgr import AttributeChangeElement, ClientAttributeChangeElement 
    2929 
    3030#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    112112 
    113113class ClientStateManager(StateManagerBase): 
    114     #AttributeChange = AttributeChangeElement(GL.GL_CLIENT_ENABLE_BIT) 
     114    ClientAttributeChange = ClientAttributeChangeElement(GL.GL_CLIENT_VERTEX_ARRAY_BIT) 
    115115 
    116116    def _SetState(self, state, enabled): 
  • trunk/RBRapier/RBRapier/Renderer/View/Transformations.py

    r702 r703  
    2727from RBRapier.Tools import Projections 
    2828from RBRapier.Tools import Quaternion 
     29from RBRapier.Tools import RectangleBase 
    2930 
    3031from Numeric import transpose 
     
    3334#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    3435#~ Definitions  
     36#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     37 
     38def GLExecuteAsMatrix(self, context): 
     39    GL.glMultMatrixd(transpose(self.asArray4x4()).tolist()) 
     40def GLExecuteLoadMatrix(self, context): 
     41    GL.glLoadMatrixd(transpose(self.asArray4x4()).tolist()) 
     42 
    3543#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    3644 
     
    4351    Mode = None 
    4452    Save = 0 
     53    _save_matrix = None 
    4554 
    4655    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    5665 
    5766    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) 
    6275 
    6376    def GLDeselect(self, context): 
     
    6578            if self.Mode: 
    6679                GL.glMatrixMode(self.Mode) 
    67                 GL.glPopMatrix() 
     80                self._RestoreMatrix() 
    6881                GL.glMatrixMode(GL.GL_MODELVIEW) 
    6982            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 
    7198 
    7299#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    84111 
    85112class Matrix(Transformations.Matrix): 
    86     def GLExecute(self, context): 
    87         GL.glMultMatrixd(transpose(self.matrix).tolist()) 
     113    GLExecute = GLExecuteAsMatrix 
    88114 
    89115class MatrixMgd(ManagedTransformationMixin, Matrix): 
     
    91117 
    92118class LoadMatrix(Transformations.Matrix): 
    93     def GLExecute(self, context): 
    94         GL.glLoadMatrixd(transpose(self.matrix).tolist()) 
     119    GLExecute = GLExecuteLoadMatrix 
    95120 
    96121class LoadMatrixMgd(ManagedTransformationMixin, LoadMatrix): 
     
    133158 
    134159class Quaternion(Quaternion.Quaternion): 
    135     def GLExecute(self, context): 
    136         GL.glMultMatrixd(transpose(self.asArray4x4()).tolist()) 
     160    GLExecute = GLExecuteAsMatrix 
    137161 
    138162class QuaternionMgd(ManagedTransformationMixin, Quaternion): 
     
    140164 
    141165class LinearMappingMatrix(Transformations.LinearMappingMatrix): 
    142     def GLExecute(self, context): 
    143         GL.glMultMatrixd(transpose(self.asArray4x4()).tolist()) 
     166    GLExecute = GLExecuteAsMatrix 
    144167 
    145168class LinearMappingMatrixMgd(ManagedTransformationMixin, LinearMappingMatrix): 
     169    pass 
     170 
     171class RectangleMappingMatrix(RectangleBase.RectangleMappingMatrix3dh): 
     172    GLExecute = GLExecuteAsMatrix 
     173 
     174class RectangleMappingMatrixMgd(ManagedTransformationMixin, RectangleMappingMatrix): 
    146175    pass 
    147176 
     
    163192 
    164193class Shear(Transformations.Shear): 
    165     def GLExecute(self, context): 
    166         GL.glMultMatrixd(transpose(self.matrix).tolist()) 
     194    GLExecute = GLExecuteAsMatrix 
    167195 
    168196class ShearMgd(ManagedTransformationMixin, Shear): 
     
    170198 
    171199class Skew(Transformations.Skew): 
    172     def GLExecute(self, context): 
    173         GL.glMultMatrixd(transpose(self.matrix).tolist()) 
     200    GLExecute = GLExecuteAsMatrix 
    174201 
    175202class SkewMgd(ManagedTransformationMixin, Skew): 
     
    180207#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    181208 
    182 class Orthographic(Projections.Orthographic): 
    183     def GLExecute(self, context): 
     209class 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 
     217class Orthographic(Projections.Orthographic, ProjectionModelMixin): 
     218    def GLExecute(self, context): 
     219        self.ApplyProjectionModel() 
    184220        GL.glOrtho(self.Left, self.Right, self.Bottom, self.Top, self.Near, self.Far) 
    185221 
     
    187223    pass 
    188224 
    189 class Frustum(Projections.Frustum): 
    190     def GLExecute(self, context): 
     225class Frustum(Projections.Frustum, ProjectionModelMixin): 
     226    def GLExecute(self, context): 
     227        self.ApplyProjectionModel() 
    191228        GL.glFrustum(self.Left, self.Right, self.Bottom, self.Top, self.Near, self.Far) 
    192229 
     
    194231    pass 
    195232 
    196 class Perspective(Projections.Perspective): 
    197     def GLExecute(self, context): 
     233class Perspective(Projections.Perspective, ProjectionModelMixin): 
     234    def GLExecute(self, context): 
     235        self.ApplyProjectionModel() 
    198236        GL.glFrustum(self.Left, self.Right, self.Bottom, self.Top, self.Near, self.Far) 
    199237 
  • trunk/RBRapier/RBRapier/Tools/Animation/Navigation.py

    r702 r703  
    113113        viewbox = self.GetViewBox().copy() # make a copy so we don't "erode" 
    114114        viewbox.SetAspectRatio(aspectratio) 
    115         viewbox.asProjection(self.projection) 
     115        self.projection.ProjectionModel = viewbox 
    116116        self.viewboxes['displayed'] = viewbox 
    117117 
  • trunk/RBRapier/RBRapier/Tools/Projections.py

    r692 r703  
    124124    def SetAspectRatio(self, value, byWidth=None): 
    125125        self.SetDimensionsAspectRatio(self.Width, self.Height, value) 
    126         #if byWidth is None: 
    127         #    byWidth = value < 1 
    128         #if byWidth: 
    129         #    self.Width = self.Height / float(value) 
    130         #else: 
    131         #    self.Height = self.Width * float(value) 
    132126    AspectRatio = property(GetAspectRatio, SetAspectRatio) 
    133127 
     
    180174            [0., 0., -2./deprojh, zoff], 
    181175            [0., 0., 0., 1.]], 
    182             Numeric.Float32
     176            self.NumericType
    183177        return result 
    184178 
     
    281275            [0., 0., -zoff, ugly], 
    282276            [0., 0., -1., 0.]], 
    283             Numeric.Float32
     277            self.NumericType
    284278        return result 
    285279 
  • trunk/RBRapier/RBRapier/Tools/Quaternion.py

    r658 r703  
    4343 
    4444#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    45 #~ Constants / Variables / Etc.  
    46 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    47  
    48 _NumericType = Numeric.Float 
    49  
    50 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    5145#~ Definitions  
    5246#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    115109    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    116110 
    117     NumericType = _NumericType 
     111    NumericType = Numeric.Float 
    118112     
    119113    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    240234               [ 0.32943134, -0.24949789,  0.92006655,  0.        ], 
    241235               [ 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.        ]]) 
    242241        """ 
    243242        s, a, b, c = self._array 
     
    248247            [0., 0., 0., 0.]], self.NumericType) 
    249248        return result 
     249 
     250    def asInverse4x4(self): 
     251        return self.Inverse().asArray4x4() 
    250252 
    251253    def Magnitude(self, squared=0): 
  • trunk/RBRapier/RBRapier/Tools/RectangleBase.py

    r691 r703  
    2727import Numeric 
    2828from Transformations import TransformPrimitive3dh 
     29from Transformations2d import TransformPrimitive2dh 
     30from Vector import LinearMapping, LinearDimMapping 
    2931 
    3032#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    6264 
    6365    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) 
    6570 
    6671    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    103108    AspectRatio = property(GetAspectRatio) 
    104109 
     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 
    105125    def SetRectangle(self, *args):  
    106126        if len(args) == 1: args = args[0] 
    107127        self.Rectangle[:] = self.Rectangle.__class__(args) 
    108128 
    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 
     155class 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 
     198class RectangleMappingMatrix3dh(RectangleMappingBase, TransformPrimitive3dh): 
    126199    def asArray4x4(self): 
    127         halfwidth = self.Rectangle[2] / 2 
    128         halfheight = self.Rectangle[3] / 2 
    129         xoff = self.Rectangle[0] + halfwidth 
    130         yoff = self.Rectangle[1] + halfheight 
    131         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
    136209        return result 
    137210 
    138211    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 
     225class 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) 
    146246        return result 
    147247 
     
    152252if __name__=='__main__': 
    153253    print "Testing..." 
    154     import doctest, RectangleBase 
    155     doctest.testmod(RectangleBase
     254    import doctest, RectangleBase as _TestMod 
     255    doctest.testmod(_TestMod
    156256    print "Test complete." 
    157257 
  • trunk/RBRapier/RBRapier/Tools/Transformations.py

    r660 r703  
    3434_rad2deg = 180.0/Numeric.pi 
    3535_deg2rad = Numeric.pi/180.0 
    36 _NumericType = Numeric.Float 
    3736 
    3837#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    4140 
    4241class TransformPrimitive3dh(object): 
     42    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     43    #~ Constants / Variables / Etc.  
     44    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     45 
     46    NumericType = Numeric.Float 
     47 
     48    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     49    #~ Public Methods  
     50    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     51 
    4352    def asArray4x4(self): 
    4453        """Returns the transformation in 4x4 Numeric array form. 
     
    6473            return Numeric.dot(other, self.asArray4x4()) 
    6574        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) 
    66106 
    67107#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    90130    >>> n = c.Add(Frustum()) 
    91131    >>> 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 
    102136    """ 
    103137 
     
    162196    def asArray4x4(self): 
    163197        """Returns the transformation in 4x4 Numeric array form""" 
    164         r = Numeric.identity(4, _NumericType) 
     198        r = Numeric.identity(4, self.NumericType) 
    165199        for xform in self.collection: 
    166200            r = Numeric.dot(r, xform.asArray4x4()) 
     
    195229 
    196230    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) 
    199233        assert self.matrix.shape == (4,4) 
    200234 
     
    233267           [ 0.,  0.,  1.,  0.], 
    234268           [ 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.]]) 
    235284    """ 
    236285 
     
    242291 
    243292    def asArray4x4(self): 
    244         return Numeric.identity(4, _NumericType) 
     293        return Numeric.identity(4, self.NumericType) 
    245294 
    246295    def asInverse4x4(self): 
    247         return Numeric.identity(4, _NumericType) 
     296        return Numeric.identity(4, self.NumericType) 
    248297 
    249298    def Inverse(self): 
     
    269318    """ 
    270319 
    271     Direction = Vector3HProperty('Direction', NumericType=_NumericType) 
     320    Direction = Vector3HProperty('Direction', NumericType=TransformPrimitive3dh.NumericType) 
    272321 
    273322    def __init__(self, Direction=(0,0,0)): 
     
    278327 
    279328    def asArray4x4(self): 
    280         result = Numeric.identity(4, _NumericType) 
     329        result = Numeric.identity(4, self.NumericType) 
    281330        result[:, 3] = self.Direction 
    282331        return result 
    283332 
    284333    def asInverse4x4(self): 
    285         result = Numeric.identity(4, _NumericType) 
     334        result = Numeric.identity(4, self.NumericType) 
    286335        result[:, 3] = -self.Direction 
    287336        return result 
     
    309358    """ 
    310359 
    311     Scale = Vector3HProperty('Scale', (1.,1.,1.,1.), NumericType=_NumericType) 
     360    Scale = Vector3HProperty('Scale', (1.,1.,1.,1.), NumericType=TransformPrimitive3dh.NumericType) 
    312361 
    313362    def __init__(self, Scale=1.0): 
     
    323372 
    324373    def asArray4x4(self): 
    325         result = Numeric.identity(4, _NumericType) 
     374        result = Numeric.identity(4, self.NumericType) 
    326375        for idx in range(4-1): result[idx,idx] = self.Scale[idx] 
    327376        return result 
    328377 
    329378    def asInverse4x4(self): 
    330         result = Numeric.identity(4, _NumericType) 
     379        result = Numeric.identity(4, self.NumericType) 
    331380        for idx in range(4-1): result[idx,idx] = 1./self.Scale[idx] 
    332381        return result 
     
    393442    """ 
    394443 
    395     Axis = UnitVector3HProperty('Axis', NumericType=_NumericType) 
     444    Axis = UnitVector3HProperty('Axis', NumericType=TransformPrimitive3dh.NumericType) 
    396445 
    397446    def __init__(self, angle=0.0, axis=(0.0, 0.0, 1.0)): 
     
    411460        u = self.Axis[:-1] 
    412461        uut = Numeric.outerproduct(u, u) 
    413         M = Numeric.identity(3, _NumericType) - uut 
    414         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) 
    415464        radians = self.Radians 
    416465        R = uut + Numeric.cos(radians) * M + Numeric.sin(radians) * S 
    417         result = Numeric.identity(4, _NumericType) 
     466        result = Numeric.identity(4, self.NumericType) 
    418467        result[:3,:3] = R 
    419468        return result 
     
    439488    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    440489 
    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) 
    444493 
    445494    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    466515        U_ = S.Cross3(L) 
    467516 
    468         result = Numeric.identity(4, _NumericType) 
     517        result = Numeric.identity(4, self.NumericType) 
    469518        result[0,:-1] = S 
    470519        result[1,:-1] = U_ 
    471520        result[2,:-1] = -L 
    472         xlate = Numeric.identity(4, _NumericType) 
     521        xlate = Numeric.identity(4, self.NumericType) 
    473522        xlate[:-1,3] = -E 
    474523        return Numeric.dot(result, xlate) 
     
    494543 
    495544    _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) 
    499548 
    500549    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    604653#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    605654 
    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  
    616655class LinearMappingMatrix(Matrix): 
    617656    """ 
     
    647686        return "<LinearMappingMatrix: %s >" % ([(self.fromX, self.toX), (self.fromY, self.toY), (self.fromZ, self.toZ)],) 
    648687 
    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) 
    653700        result = Numeric.asarray([ 
    654701            [Xs, 0, 0, Xt], 
     
    656703            [0, 0, Zs, Zt], 
    657704            [0, 0, 0, 1]],  
    658             _NumericType) 
     705            self.NumericType) 
    659706        return result 
    660707 
    661708    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
    665712        result = Numeric.asarray([ 
    666713            [Xs, 0, 0, Xt], 
     
    668715            [0, 0, Zs, Zt], 
    669716            [0, 0, 0, 1]],  
    670             _NumericType) 
     717            self.NumericType) 
    671718        return result 
    672719 
  • trunk/RBRapier/RBRapier/Tools/Transformations2d.py

    r685 r703  
    3434_rad2deg = 180.0/Numeric.pi 
    3535_deg2rad = Numeric.pi/180.0 
    36 _NumericType = Numeric.Float 
     36 
    3737 
    3838#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    4141 
    4242class TransformPrimitive2dh(object): 
     43    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     44    #~ Constants / Variables / Etc.  
     45    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     46 
     47    NumericType = Numeric.Float 
     48 
     49    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     50    #~ Public Methods  
     51    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     52 
    4353    def asArray3x3(self): 
    4454        """Returns the transformation in 3x3 Numeric array form. 
     
    6474            return Numeric.dot(other, self.asArray3x3()) 
    6575        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):