Changeset 665
- Timestamp:
- 08/19/03 21:33:25 (5 years ago)
- Files:
-
- trunk/RBRapier/RBRapier/Formats/Attic/SVG.old/Renderers/RapierObjects.py (modified) (2 diffs)
- trunk/RBRapier/RBRapier/Formats/SVG/RapierRenderItems.py (modified) (20 diffs)
- trunk/RBRapier/RBRapier/Formats/SVG/SVGSkin/SVGItems/PathBuilder.py (modified) (1 diff)
- trunk/RBRapier/RBRapier/Tools/Geometry/Curves.py (modified) (1 diff)
- trunk/RBRapier/RBRapier/Tools/Geometry/gluPolygonTesselation.py (modified) (1 diff)
- trunk/RBRapier/RBRapier/Tools/Transformations2d.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/RBRapier/RBRapier/Formats/Attic/SVG.old/Renderers/RapierObjects.py
r662 r665 187 187 188 188 class RapierPathFactory(Abstract.AbstractPathFactory): 189 steps = 64189 steps = 32 190 190 pos = (0,0) 191 191 controlpoint = None … … 298 298 def ellipticarc_rel(self, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y): 299 299 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) 301 301 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) 305 311 306 312 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ trunk/RBRapier/RBRapier/Formats/SVG/RapierRenderItems.py
r662 r665 24 24 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 25 25 26 from __future__ import generators 26 27 from OpenGL import GL 28 import Polygon as PolygonModule 27 29 import Numeric 28 30 29 31 from RBRapier.Tools import Transformations2d 30 32 from RBRapier.Tools.Geometry import Curves 31 from RBRapier.Tools.Geometry.gluPolygonTesselation import gluPolygonTesselator33 from RBRapier.Tools.Geometry.gluPolygonTesselation import PolygonTesselator as _PolygonTesselator 32 34 33 35 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 42 44 43 45 class Style(object): 44 def __init__(self, *styles ):45 self._style = {}46 def __init__(self, *styles, **kwstyle): 47 self._style = kwstyle 46 48 map(self._style.update, styles) 47 49 … … 49 51 result = self.__class__() 50 52 result._style = self._style.copy() 53 return result 51 54 52 55 def __iadd__(self, other): … … 81 84 result = self.__class__() 82 85 result.collection = self.collection[:] 86 return result 83 87 84 88 def translate(self, x=0.0, y=0.0): … … 105 109 self.Add(Transformations2d.Matrix([[xx, xy, xh], [yx, yy, yh], [hx, hy, hh]])) 106 110 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 107 119 def AddSVGTransforms(self, transforms): 108 120 def AddTransforms(name, *args): … … 119 131 fromSVGTransforms = classmethod(fromSVGTransforms) 120 132 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 142 class SVGTesselator(_PolygonTesselator): 143 def _OnVertex(self, vertexdata): 144 self.vertexdata.append(vertexdata[:2]) 122 145 123 146 class PathConnector(object): 124 147 pos = (0,0) 125 148 controlpoint = None 126 steps = 64149 steps = 32 127 150 128 151 command_table = { … … 153 176 def AddSVGPath(self, path): 154 177 def AddPathElement(commandidx, *args): 155 name = self.command_table.get( name, '')178 name = self.command_table.get(commandidx, '') 156 179 transformfn = getattr(self, name, None) 157 180 if transformfn is not None: … … 164 187 return result 165 188 fromSVGPath = classmethod(fromSVGPath) 189 190 def GetContours(self): 191 return [map(tuple, contour) for contour in filter(None, self.pathlist)] 166 192 167 193 def move_rel(self, x, y): … … 238 264 def ellipticarc_rel(self, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y): 239 265 dx, dy = self.pos 240 return self.ellipticarc_rel(rx, ry, xrotation, largeArcFlag, sweep flag, x+dx, y+dy)266 return self.ellipticarc_rel(rx, ry, xrotation, largeArcFlag, sweepFlag, x+dx, y+dy) 241 267 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) 243 271 self.pathlist[-1].extend(list(ellipse)) 244 272 self._savepos((x,y)) … … 261 289 262 290 class 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 263 305 def SetPoints(self, points, transform=None): 264 points = Numeric.asarray(points, _NumericType)265 if transform is not None:266 # add the homogeneous coordinate267 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.270 306 self.points = points 271 307 … … 276 312 def SetColors(self, colors): 277 313 self.colors = Numeric.asarray(colors, _NumericType) 314 315 def Commit(self): 316 raise NotImplementedError 278 317 279 318 class GLStroke(GLGeometry): 280 319 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): 281 329 pass 282 330 283 def AddLineStrip(self, indexes): 331 def Commit(self): 332 raise NotImplementedError 333 334 class 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): 284 345 pass 285 346 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 292 349 293 350 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 301 358 302 359 transform = None 360 default_transform = Transform() 303 361 style = None 362 default_style = Style(stroke=(0,0,0)) 304 363 305 364 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 307 366 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 308 367 309 def Compile(self, style=None, transform=None): 368 def Compile(self, style, transform): 369 #style, transform = self._GetStyleAndTransform(style, transform) 310 370 pass 311 #style, transform = self._GetStyleAndTransform(style, transform)312 371 313 372 #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 331 390 def _GetStyleAndTransform(self, style=None, transform=None): 332 391 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() 334 396 elif self.style is not None: 335 397 style += self.style 336 398 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() 338 403 elif self.transform is not None: 339 404 transform *= self.transform … … 345 410 children = () 346 411 347 def Compile(self, style , transform):412 def Compile(self, style=None, transform=None): 348 413 style, transform = self._GetStyleAndTransform(style, transform) 349 414 for child in self.children: 350 child.Compile(style, transform) 415 for geometry in child.Compile(style, transform): 416 yield geometry 351 417 352 418 def SetChildren(self, children): … … 373 439 def Compile(self, style, transform): 374 440 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 378 448 379 449 #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 385 455 386 456 class 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 387 478 #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 388 479 … … 400 491 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 401 492 402 class Polyline(RenderItem):403 #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~404 405 def SetPoints(self, points):406 self.points = points407 408 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~409 410 class Polygon(Polyline):411 pass # Note: what the heck is the difference anyway?412 413 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~414 415 493 class 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 417 513 #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 418 514 … … 434 530 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 435 531 532 class 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 564 class 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 436 596 class 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 437 634 #~ SVG Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 438 635 … … 473 670 'line': Line, 474 671 'rect': Rect, 475 'image': Image,672 #'image': Image, 476 673 'path': Path, 477 674 'polyline': Polyline, trunk/RBRapier/RBRapier/Formats/SVG/SVGSkin/SVGItems/PathBuilder.py
r662 r665 150 150 151 151 def RestoreToEx(self, onbegin=lambda xformcmd:None, onadd=lambda name, *args:None, onend=lambda xformcmd:None): 152 onbegin(self. transformcmd)152 onbegin(self.pathcmd) 153 153 for args in self.path: 154 154 onadd(*args) 155 onend(self. transformcmd)155 onend(self.pathcmd) 156 156 157 157 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ trunk/RBRapier/RBRapier/Tools/Geometry/Curves.py
r664 r665 178 178 179 179 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. 183 187 184 188 if xrotation: trunk/RBRapier/RBRapier/Tools/Geometry/gluPolygonTesselation.py
r633 r665 152 152 GL.glEnd() 153 153 154 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 155 156 class 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 128 128 def asArray3x3(self): 129 129 """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) 134 138 135 139 def Inverse(self):
