CMS 3D CMS Logo

Public Member Functions | Public Attributes | Static Public Attributes

svgfig::Poly Class Reference

List of all members.

Public Member Functions

def __init__
def __repr__
def Path
def SVG

Public Attributes

 attr
 d
 loop
 mode

Static Public Attributes

dictionary defaults = {}

Detailed Description

Draws a curve specified by a sequence of points. The curve may be
piecewise linear, like a polygon, or a Bezier curve.

Poly(d, mode, loop, attribute=value)

d                       required        list of tuples representing points
                                        and possibly control points
mode                    default="L"   "lines", "bezier", "velocity",
                                        "foreback", "smooth", or an abbreviation
loop                    default=False   if True, connect the first and last
                                        point, closing the loop
attribute=value pairs   keyword list    SVG attributes

The format of the tuples in d depends on the mode.

"lines"/"L"         d=[(x,y), (x,y), ...]
                                        piecewise-linear segments joining the (x,y) points
"bezier"/"B"        d=[(x, y, c1x, c1y, c2x, c2y), ...]
                                        Bezier curve with two control points (control points
                                        preceed (x,y), as in SVG paths). If (c1x,c1y) and
                                        (c2x,c2y) both equal (x,y), you get a linear
                                        interpolation ("lines")
"velocity"/"V"      d=[(x, y, vx, vy), ...]
                                        curve that passes through (x,y) with velocity (vx,vy)
                                        (one unit of arclength per unit time); in other words,
                                        (vx,vy) is the tangent vector at (x,y). If (vx,vy) is
                                        (0,0), you get a linear interpolation ("lines").
"foreback"/"F"      d=[(x, y, bx, by, fx, fy), ...]
                                        like "velocity" except that there is a left derivative
                                        (bx,by) and a right derivative (fx,fy). If (bx,by)
                                        equals (fx,fy) (with no minus sign), you get a
                                        "velocity" curve
"smooth"/"S"        d=[(x,y), (x,y), ...]
                                        a "velocity" interpolation with (vx,vy)[i] equal to
                                        ((x,y)[i+1] - (x,y)[i-1])/2: the minimal derivative

Definition at line 1679 of file svgfig.py.


Constructor & Destructor Documentation

def svgfig::Poly::__init__ (   self,
  d = [],
  mode = "L",
  loop = False,
  attr 
)

Definition at line 1721 of file svgfig.py.

01722                                                         :
01723     self.d = list(d)
01724     self.mode = mode
01725     self.loop = loop
01726 
01727     self.attr = dict(self.defaults)
01728     self.attr.update(attr)


Member Function Documentation

def svgfig::Poly::__repr__ (   self)

Definition at line 1718 of file svgfig.py.

01719                     :
01720     return "<Poly (%d nodes) mode=%s loop=%s %s>" % (len(self.d), self.mode, repr(self.loop), self.attr)

def svgfig::Poly::Path (   self,
  trans = None,
  local = False 
)
Apply the transformation "trans" and return a Path object in
global coordinates.  If local=True, return a Path in local coordinates
(which must be transformed again).

Definition at line 1733 of file svgfig.py.

01734                                          :
01735     """Apply the transformation "trans" and return a Path object in
01736     global coordinates.  If local=True, return a Path in local coordinates
01737     (which must be transformed again)."""
01738     if isinstance(trans, basestring): trans = totrans(trans)
01739 
01740     if self.mode[0] == "L" or self.mode[0] == "l": mode = "L"
01741     elif self.mode[0] == "B" or self.mode[0] == "b": mode = "B"
01742     elif self.mode[0] == "V" or self.mode[0] == "v": mode = "V"
01743     elif self.mode[0] == "F" or self.mode[0] == "f": mode = "F"
01744     elif self.mode[0] == "S" or self.mode[0] == "s":
01745       mode = "S"
01746 
01747       vx, vy = [0.]*len(self.d), [0.]*len(self.d)
01748       for i in xrange(len(self.d)):
01749         inext = (i+1) % len(self.d)
01750         iprev = (i-1) % len(self.d)
01751 
01752         vx[i] = (self.d[inext][0] - self.d[iprev][0])/2.
01753         vy[i] = (self.d[inext][1] - self.d[iprev][1])/2.
01754         if not self.loop and (i == 0 or i == len(self.d)-1):
01755           vx[i], vy[i] = 0., 0.
01756 
01757     else:
01758       raise ValueError, "mode must be \"lines\", \"bezier\", \"velocity\", \"foreback\", \"smooth\", or an abbreviation"
01759 
01760     d = []
01761     indexes = range(len(self.d))
01762     if self.loop and len(self.d) > 0: indexes.append(0)
01763 
01764     for i in indexes:
01765       inext = (i+1) % len(self.d)
01766       iprev = (i-1) % len(self.d)
01767 
01768       x, y = self.d[i][0], self.d[i][1]
01769 
01770       if trans == None: X, Y = x, y
01771       else: X, Y = trans(x, y)
01772 
01773       if d == []:
01774         if local: d.append(("M", x, y, False))
01775         else: d.append(("M", X, Y, True))
01776 
01777       elif mode == "L":
01778         if local: d.append(("L", x, y, False))
01779         else: d.append(("L", X, Y, True))
01780 
01781       elif mode == "B":
01782         c1x, c1y = self.d[i][2], self.d[i][3]
01783         if trans == None: C1X, C1Y = c1x, c1y
01784         else: C1X, C1Y = trans(c1x, c1y)
01785 
01786         c2x, c2y = self.d[i][4], self.d[i][5]
01787         if trans == None: C2X, C2Y = c2x, c2y
01788         else: C2X, C2Y = trans(c2x, c2y)
01789 
01790         if local: d.append(("C", c1x, c1y, False, c2x, c2y, False, x, y, False))
01791         else: d.append(("C", C1X, C1Y, True, C2X, C2Y, True, X, Y, True))
01792 
01793       elif mode == "V":
01794         c1x, c1y = self.d[iprev][2]/3. + self.d[iprev][0], self.d[iprev][3]/3. + self.d[iprev][1]
01795         c2x, c2y = self.d[i][2]/-3. + x, self.d[i][3]/-3. + y
01796 
01797         if trans == None: C1X, C1Y = c1x, c1y
01798         else: C1X, C1Y = trans(c1x, c1y)
01799         if trans == None: C2X, C2Y = c2x, c2y
01800         else: C2X, C2Y = trans(c2x, c2y)
01801 
01802         if local: d.append(("C", c1x, c1y, False, c2x, c2y, False, x, y, False))
01803         else: d.append(("C", C1X, C1Y, True, C2X, C2Y, True, X, Y, True))
01804 
01805       elif mode == "F":
01806         c1x, c1y = self.d[iprev][4]/3. + self.d[iprev][0], self.d[iprev][5]/3. + self.d[iprev][1]
01807         c2x, c2y = self.d[i][2]/-3. + x, self.d[i][3]/-3. + y
01808 
01809         if trans == None: C1X, C1Y = c1x, c1y
01810         else: C1X, C1Y = trans(c1x, c1y)
01811         if trans == None: C2X, C2Y = c2x, c2y
01812         else: C2X, C2Y = trans(c2x, c2y)
01813 
01814         if local: d.append(("C", c1x, c1y, False, c2x, c2y, False, x, y, False))
01815         else: d.append(("C", C1X, C1Y, True, C2X, C2Y, True, X, Y, True))
01816 
01817       elif mode == "S":
01818         c1x, c1y = vx[iprev]/3. + self.d[iprev][0], vy[iprev]/3. + self.d[iprev][1]
01819         c2x, c2y = vx[i]/-3. + x, vy[i]/-3. + y
01820 
01821         if trans == None: C1X, C1Y = c1x, c1y
01822         else: C1X, C1Y = trans(c1x, c1y)
01823         if trans == None: C2X, C2Y = c2x, c2y
01824         else: C2X, C2Y = trans(c2x, c2y)
01825 
01826         if local: d.append(("C", c1x, c1y, False, c2x, c2y, False, x, y, False))
01827         else: d.append(("C", C1X, C1Y, True, C2X, C2Y, True, X, Y, True))
01828 
01829     if self.loop and len(self.d) > 0: d.append(("Z",))
01830 
01831     return Path(d, **self.attr)

def svgfig::Poly::SVG (   self,
  trans = None 
)
Apply the transformation "trans" and return an SVG object.

Definition at line 1729 of file svgfig.py.

01730                            :
01731     """Apply the transformation "trans" and return an SVG object."""
01732     return self.Path(trans).SVG()


Member Data Documentation

Definition at line 1721 of file svgfig.py.

Definition at line 1721 of file svgfig.py.

dictionary svgfig::Poly::defaults = {} [static]

Definition at line 1716 of file svgfig.py.

Definition at line 1721 of file svgfig.py.

Definition at line 1721 of file svgfig.py.