Changeset 665

Show
Ignore:
Timestamp:
08/19/03 21:33:25 (5 years ago)
Author:
sholloway
Message:

*** empty log message ***

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/RBRapier/RBRapier/Formats/Attic/SVG.old/Renderers/RapierObjects.py

    r662 r665  
    187187 
    188188class RapierPathFactory(Abstract.AbstractPathFactory): 
    189     steps = 64 
     189    steps = 32 
    190190    pos = (0,0) 
    191191    controlpoint = None 
     
    298298    def ellipticarc_rel(self, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y):  
    299299        dx, dy = self.pos 
    300         return self.ellipticarc_rel(rx, ry, xrotation, largeArcFlag, sweepflag, x+dx, y+dy) 
     300        return self.ellipticarc(rx, ry, xrotation, largeArcFlag, sweepFlag, x+dx, y+dy) 
    301301    def ellipticarc(self, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y):  
    302         ellipse = Curves.Ellipse.fromArc(self.pos, (x,y), rx, ry, xrotation, largeArcFlag, sweepFlag, inDegrees=True, steps=self.steps) 
    303         self.pathlist[-1].extend(list(ellipse)) 
    304         self._savepos((x,y)) 
     302        startpt, endpt = self.pos, (x,y) 
     303        if startpt == endpt: return 
     304        if rx and ry: 
     305            ellipse = Curves.Ellipse.fromArc(startpt, endpt, abs(rx), abs(ry), xrotation % 360, bool(largeArcFlag), bool(sweepFlag), inDegrees=True, steps=self.steps) 
     306            self.pathlist[-1].extend(list(ellipse)) 
     307        else: 
     308            # If rX = 0 or rY  = 0, then treat this as a straight line from (x1, y1) to (x2, y2) and stop 
     309            self.pathlist[-1].append(endpt) 
     310        self._savepos(endpt) 
    305311 
    306312    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  • trunk/RBRapier/RBRapier/Formats/SVG/RapierRenderItems.py

    r662 r665  
    2424#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    2525 
     26from __future__ import generators 
    2627from OpenGL import GL 
     28import Polygon as PolygonModule 
    2729import Numeric 
    2830 
    2931from RBRapier.Tools import Transformations2d 
    3032from RBRapier.Tools.Geometry import Curves 
    31 from RBRapier.Tools.Geometry.gluPolygonTesselation import gluPolygonTesselator 
     33from RBRapier.Tools.Geometry.gluPolygonTesselation import PolygonTesselator as _PolygonTesselator 
    3234 
    3335#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    4244 
    4345class Style(object): 
    44     def __init__(self, *styles): 
    45         self._style = {} 
     46    def __init__(self, *styles, **kwstyle): 
     47        self._style = kwstyle 
    4648        map(self._style.update, styles) 
    4749 
     
    4951        result = self.__class__() 
    5052        result._style = self._style.copy() 
     53        return result 
    5154 
    5255    def __iadd__(self, other): 
     
    8184        result = self.__class__() 
    8285        result.collection = self.collection[:] 
     86        return result 
    8387 
    8488    def translate(self, x=0.0, y=0.0):  
     
    105109        self.Add(Transformations2d.Matrix([[xx, xy, xh], [yx, yy, yh], [hx, hy, hh]])) 
    106110 
     111    def asArray3x3(self): 
     112        try:  
     113            return self._cached_matrix 
     114        except AttributeError: 
     115            matrix = Transformations2d.Composite.asArray3x3(self) 
     116            self._cached_matrix = matrix 
     117            return matrix 
     118 
    107119    def AddSVGTransforms(self, transforms): 
    108120        def AddTransforms(name, *args): 
     
    119131    fromSVGTransforms = classmethod(fromSVGTransforms) 
    120132 
    121 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     133    def TransformPoints(self, points): 
     134        # add the homogeneous coordinate 
     135        matrix = self.asArray3x3() 
     136        points = Numeric.concatenate((points, Numeric.ones((len(points), 1), _NumericType)), 1) 
     137        points = Numeric.transpose(Numeric.dot(matrix, Numeric.transpose(points))) 
     138        return points[:,:-1] # trim the homogenous coordinate -- assumes that they are all still 1. 
     139 
     140#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     141 
     142class SVGTesselator(_PolygonTesselator): 
     143    def _OnVertex(self, vertexdata): 
     144        self.vertexdata.append(vertexdata[:2]) 
    122145 
    123146class PathConnector(object): 
    124147    pos = (0,0) 
    125148    controlpoint = None 
    126     steps = 64 
     149    steps = 32 
    127150 
    128151    command_table = { 
     
    153176    def AddSVGPath(self, path): 
    154177        def AddPathElement(commandidx, *args): 
    155             name = self.command_table.get(name, '') 
     178            name = self.command_table.get(commandidx, '') 
    156179            transformfn = getattr(self, name, None) 
    157180            if transformfn is not None: 
     
    164187        return result 
    165188    fromSVGPath = classmethod(fromSVGPath) 
     189 
     190    def GetContours(self): 
     191        return [map(tuple, contour) for contour in filter(None, self.pathlist)] 
    166192 
    167193    def move_rel(self, x, y):  
     
    238264    def ellipticarc_rel(self, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y):  
    239265        dx, dy = self.pos 
    240         return self.ellipticarc_rel(rx, ry, xrotation, largeArcFlag, sweepflag, x+dx, y+dy) 
     266        return self.ellipticarc_rel(rx, ry, xrotation, largeArcFlag, sweepFlag, x+dx, y+dy) 
    241267    def ellipticarc(self, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y):  
    242         ellipse = Curves.Ellipse.fromArc(self.pos, (x,y), rx, ry, xrotation, largeArcFlag, sweepFlag, inDegrees=True, steps=self.steps) 
     268        startpt, endpt = self.pos, (x,y) 
     269        if startpt == endpt: return 
     270        ellipse = Curves.Ellipse.fromArc(startpt, endpt, abs(rx), abs(ry), xrotation % 360, bool(largeArcFlag), bool(sweepFlag), inDegrees=True, steps=self.steps) 
    243271        self.pathlist[-1].extend(list(ellipse)) 
    244272        self._savepos((x,y)) 
     
    261289 
    262290class GLGeometry(object): 
     291    points = () 
     292    colors = () 
     293 
     294    def __init__(self, points=None, singlecolor=None, colors=None): 
     295        if points is not None: 
     296            self.SetPoints(points) 
     297 
     298        if singlecolor is not None: 
     299            assert colors is None 
     300            self.SetSingleColor(singlecolor) 
     301        elif colors is not None: 
     302            assert singlecolor is None 
     303            self.SetColors(colors) 
     304 
    263305    def SetPoints(self, points, transform=None): 
    264         points = Numeric.asarray(points, _NumericType) 
    265         if transform is not None: 
    266             # add the homogeneous coordinate 
    267             points = Numeric.concatenate((points, Numeric.ones((len(points), 1), _NumericType)), 1) 
    268             points = Numeric.transpose(Numeric.dot(transform.asArray3x3(), Numeric.transpose(points))) 
    269             points = result[:,:-1] # trim the homogenous coordinate -- assumes that they are all still 1. 
    270306        self.points = points 
    271307 
     
    276312    def SetColors(self, colors): 
    277313        self.colors = Numeric.asarray(colors, _NumericType) 
     314     
     315    def Commit(self): 
     316        raise NotImplementedError 
    278317 
    279318class GLStroke(GLGeometry): 
    280319    def AddLines(self, indexes): 
     320        self.Add(GL.GL_LINES, indexes) 
     321 
     322    def AddLineStrip(self, indexes): 
     323        self.Add(GL.GL_LINE_STRIP, indexes) 
     324 
     325    def AddLineLoop(self, indexes): 
     326        self.Add(GL.GL_LINE_LOOP, indexes) 
     327 
     328    def Add(self, glmode, indexes): 
    281329        pass 
    282330 
    283     def AddLineStrip(self, indexes): 
     331    def Commit(self): 
     332        raise NotImplementedError 
     333 
     334class GLFill(GLGeometry): 
     335    def AddTriangles(self, indexes): 
     336        self.Add(GL.GL_TRIANGLES, indexes) 
     337 
     338    def AddTriangleStrip(self, indexes): 
     339        self.Add(GL.GL_TRIANGLE_STRIP, indexes) 
     340     
     341    def AddTriangleFan(self, indexes): 
     342        self.Add(GL.GL_TRIANGLE_FAN, indexes) 
     343 
     344    def Add(self, glmode, indexes): 
    284345        pass 
    285346 
    286 class GLFill(GLGeometry): 
    287     def AddTriles(self, indexes): 
    288         pass 
    289  
    290     def AddTriStrip(self, indexes): 
    291         pass 
     347    def Commit(self): 
     348        raise NotImplementedError 
    292349 
    293350#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    301358 
    302359    transform = None 
     360    default_transform = Transform() 
    303361    style = None 
     362    default_style = Style(stroke=(0,0,0)) 
    304363 
    305364    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    307366    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    308367 
    309     def Compile(self, style=None, transform=None): 
     368    def Compile(self, style, transform): 
     369        #style, transform = self._GetStyleAndTransform(style, transform) 
    310370        pass 
    311         #style, transform = self._GetStyleAndTransform(style, transform) 
    312371 
    313372    #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    331390    def _GetStyleAndTransform(self, style=None, transform=None): 
    332391        if style is None: 
    333             style = self.style.copy() 
     392            if self.style is None: 
     393                style = self.default_style.copy() 
     394            else: 
     395                style = self.style.copy() 
    334396        elif self.style is not None: 
    335397            style += self.style 
    336398        if transform is None: 
    337             transform = self.transform.copy() 
     399            if self.transform is None: 
     400                transform = self.default_transform.copy() 
     401            else: 
     402                transform = self.transform.copy() 
    338403        elif self.transform is not None: 
    339404            transform *= self.transform 
     
    345410    children = () 
    346411 
    347     def Compile(self, style, transform): 
     412    def Compile(self, style=None, transform=None): 
    348413        style, transform = self._GetStyleAndTransform(style, transform) 
    349414        for child in self.children: 
    350             child.Compile(style, transform) 
     415            for geometry in child.Compile(style, transform): 
     416                yield geometry 
    351417 
    352418    def SetChildren(self, children): 
     
    373439    def Compile(self, style, transform): 
    374440        style, transform = self._GetStyleAndTransform(style, transform) 
    375         self._r_strokecolor = style.GetStrokeColor() 
    376         self._r_points = TransformPoints(self.points, transform) 
    377         return [self] 
     441 
     442        points = transform.TransformPoints(self.points) 
     443        strokecolor = style.GetStrokeColor() 
     444        if strokecolor is not None: 
     445            stroke = GLStroke(points, strokecolor) 
     446            stroke.AddLines(range(2)) 
     447            yield stroke 
    378448 
    379449    #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    385455 
    386456class Rect(RenderItem): 
     457    def Compile(self, style, transform): 
     458        style, transform = self._GetStyleAndTransform(style, transform) 
     459        x,y = self.position 
     460        w,h = self.dimensions 
     461 
     462        # TODO: compute rounded rectangles 
     463        points = [(x,y), (x+w, y), (x+w, y+h), (x, y+h)] 
     464        points = transform.TransformPoints(points) 
     465 
     466        fillcolor = style.GetFillColor() 
     467        if fillcolor is not None: 
     468            fill = GLFill(points, fillcolor) 
     469            fill.AddTriangleStrip(range(4)) 
     470            yield fill 
     471 
     472        strokecolor = style.GetStrokeColor() 
     473        if strokecolor is not None: 
     474            stroke = GLStroke(points, strokecolor) 
     475            stroke.AddLineLoop(range(4)) 
     476            yield stroke 
     477 
    387478    #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    388479 
     
    400491#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    401492 
    402 class Polyline(RenderItem): 
    403     #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    404  
    405     def SetPoints(self, points): 
    406         self.points = points 
    407  
    408 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    409  
    410 class Polygon(Polyline): 
    411     pass # Note: what the heck is the difference anyway? 
    412  
    413 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    414  
    415493class Ellipse(RenderItem): 
    416     Curves.Ellipse 
     494    def Compile(self, style, transform): 
     495        style, transform = self._GetStyleAndTransform(style, transform) 
     496 
     497        ellipse = Curves.Ellipse(self.center, self.radiusx, self.radiusy)#, 0., 360., xrotation=self.xrotation) 
     498        points = transform.TransformPoints(ellipse.asCurve()) 
     499 
     500        fillcolor = style.GetFillColor() 
     501        if fillcolor is not None: 
     502            fillpoints = Numeric.concatenate((transform.TransformPoints([self.center]), points)) 
     503            fill = GLFill(fillpoints, fillcolor) 
     504            fill.AddTriangleFan(range(len(points))) 
     505            yield fill 
     506 
     507        strokecolor = style.GetStrokeColor() 
     508        if strokecolor is not None: 
     509            stroke = GLStroke(points, strokecolor) 
     510            stroke.AddLineLoop(range(len(points))) 
     511            yield stroke 
     512     
    417513    #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    418514 
     
    434530#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    435531 
     532class Polyline(RenderItem): 
     533    def Compile(self, style, transform): 
     534        style, transform = self._GetStyleAndTransform(style, transform) 
     535 
     536        fillcolor = style.GetFillColor() 
     537        if fillcolor is not None and len(self.points) >= 3: 
     538            fillpoints = self.points 
     539            if fillpoints[0] != fillpoints[-1]: 
     540                # close the polyline so we can fill it 
     541                fillpoints = fillpoints + fillpoints[0:1] 
     542 
     543            glmodedatalist = SVGTesselator([fillpoints]).results 
     544            for glmode, fillpoints in glmodedatalist: 
     545                fillpoints = transform.TransformPoints(fillpoints) 
     546                fill = GLFill(fillpoints, fillcolor) 
     547                fill.Add(glmode, range(len(fillpoints))) 
     548                yield fill 
     549 
     550        strokecolor = style.GetStrokeColor() 
     551        if strokecolor is not None: 
     552            points = transform.TransformPoints(self.points) 
     553            stroke = GLStroke(points, strokecolor) 
     554            stroke.AddLineStrip(range(len(points))) 
     555            yield stroke 
     556 
     557    #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     558 
     559    def SetPoints(self, points): 
     560        self.points = points 
     561 
     562#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     563 
     564class Polygon(Polyline): 
     565    def Compile(self, style, transform): 
     566        style, transform = self._GetStyleAndTransform(style, transform) 
     567 
     568        fillcolor = style.GetFillColor() 
     569        if fillcolor is not None: 
     570            glmodedatalist = SVGTesselator([self.points]).results 
     571            for glmode, fillpoints in glmodedatalist: 
     572                fillpoints = transform.TransformPoints(fillpoints) 
     573                fill = GLFill(fillpoints, fillcolor) 
     574                fill.Add(glmode, range(len(fillpoints))) 
     575                yield fill 
     576 
     577        strokecolor = style.GetStrokeColor() 
     578        if strokecolor is not None: 
     579            glmodedatalist = [] 
     580            glmodedatalist = SVGTesselator([self.points], boundary=True).results 
     581            for glmode, strokepoints in glmodedatalist: 
     582                strokepoints = transform.TransformPoints(strokepoints) 
     583                stroke = GLStroke(strokepoints, strokecolor) 
     584                stroke.Add(glmode, range(len(strokepoints))) 
     585                yield stroke 
     586 
     587    def SetPoints(self, points): 
     588        if points[0] != points[-1]: 
     589            self.points = points + [points[0]] 
     590        else: 
     591            self.points = points 
     592        assert len(self.points) >= 3 
     593 
     594#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     595 
    436596class Path(RenderItem): 
     597    def Compile(self, style, transform): 
     598        style, transform = self._GetStyleAndTransform(style, transform) 
     599 
     600        path = PathConnector.fromSVGPath(self.path) 
     601 
     602        fillcolor = style.GetFillColor() 
     603        if fillcolor is not None: 
     604            # make contour list 
     605            contourlist = [] 
     606            for contour in path.GetContours(): 
     607                if len(contour) <= 2: 
     608                    continue 
     609                if contour[0] != contour[-1]: 
     610                    # close the contour so we can fill it 
     611                    contour += contour[0:1] 
     612                contourlist.append(contour) 
     613 
     614            if contourlist: 
     615                poly = PolygonModule.Polygon() 
     616                map(poly.addContour, contourlist) 
     617                poly.triStrip() 
     618 
     619                #glmodedatalist = [] #SVGTesselator(contourlist).results 
     620                #for glmode, fillpoints in glmodedatalist: 
     621                #    fillpoints = transform.TransformPoints(fillpoints) 
     622                #    fill = GLFill(fillpoints, fillcolor) 
     623                #    fill.Add(glmode, range(len(fillpoints))) 
     624                #    yield fill 
     625 
     626        strokecolor = style.GetStrokeColor() 
     627        if strokecolor is not None: 
     628            for contour in path.GetContours(): 
     629                points = transform.TransformPoints(contour) 
     630                stroke = GLStroke(points, strokecolor) 
     631                stroke.AddLineStrip(range(len(points))) 
     632                yield stroke 
     633 
    437634    #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    438635 
     
    473670        'line': Line, 
    474671        'rect': Rect, 
    475         'image': Image, 
     672        #'image': Image, 
    476673        'path': Path, 
    477674        'polyline': Polyline, 
  • trunk/RBRapier/RBRapier/Formats/SVG/SVGSkin/SVGItems/PathBuilder.py

    r662 r665  
    150150 
    151151    def RestoreToEx(self, onbegin=lambda xformcmd:None, onadd=lambda name, *args:None, onend=lambda xformcmd:None): 
    152         onbegin(self.transformcmd) 
     152        onbegin(self.pathcmd) 
    153153        for args in self.path: 
    154154            onadd(*args) 
    155         onend(self.transformcmd) 
     155        onend(self.pathcmd) 
    156156 
    157157#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  • trunk/RBRapier/RBRapier/Tools/Geometry/Curves.py

    r664 r665  
    178178 
    179179        tmp = rx2*y2 + ry2*x2 
    180         sign = (largeArcFlag != sweepFlag) and 1 or -1 
    181         scale = sign * Numeric.sqrt((rx2*ry2-tmp)/tmp) 
    182         cxP, cyP = scale*rx*y/ry, scale*-ry*x/rx 
     180        radicand = (rx2*ry2-tmp)/tmp 
     181        if radicand > 0: 
     182            sign = (largeArcFlag != sweepFlag) and 1 or -1 
     183            scale = sign * Numeric.sqrt(radicand) 
     184            cxP, cyP = scale*y*rx/ry, scale*x*-ry/rx 
     185        else: 
     186            cxP, cyP = 0., 0. 
    183187 
    184188        if xrotation: 
  • trunk/RBRapier/RBRapier/Tools/Geometry/gluPolygonTesselation.py

    r633 r665  
    152152        GL.glEnd() 
    153153 
     154#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     155 
     156class PolygonTesselator(gluPolygonTesselator): 
     157    def tessellate(self, *args, **kw): 
     158        self.results = [] 
     159        gluPolygonTesselator.tessellate(self, *args, **kw) 
     160        return self.results 
     161 
     162    def _OnBegin(self, mode): 
     163        self.mode = mode 
     164        self.vertexdata = [] 
     165    def _OnVertex(self, vertexdata): 
     166        self.vertexdata.append(vertexdata) 
     167    def _OnEnd(self): 
     168        self.results.append((self.mode, self.vertexdata)) 
     169        del self.mode, self.vertexdata 
     170 
  • trunk/RBRapier/RBRapier/Tools/Transformations2d.py

    r660 r665  
    128128    def asArray3x3(self): 
    129129        """Returns the transformation in 3x3 Numeric array form""" 
    130         r = Numeric.identity(3, _NumericType) 
    131         for xform in self.collection: 
    132             r = Numeric.dot(r, xform.asArray3x3()) 
    133         return r 
     130        coll = self.collection 
     131        if coll: 
     132            r = coll[0].asArray3x3() 
     133            for xform in coll[1:]: 
     134                r = Numeric.dot(r, xform.asArray3x3()) 
     135            return r 
     136        else: 
     137            return Numeric.identity(3, _NumericType) 
    134138 
    135139    def Inverse(self):