CMS 3D CMS Logo

List of all members | Classes | Public Member Functions | Public Attributes | Static Public Attributes
svgfig.Curve Class Reference
Inheritance diagram for svgfig.Curve:
svgfig.CurveAxis svgfig.Ellipse svgfig.Line svgfig.Rect svgfig.HLine svgfig.LineAxis svgfig.VLine svgfig.XAxis svgfig.YAxis

Classes

class  Sample
 nested class Sample More...
 
class  Samples
 end Sample More...
 

Public Member Functions

def __init__ (self, f, low, high, loop=False, attr)
 
def __repr__ (self)
 
def Path (self, trans=None, local=False)
 
def sample (self, trans=None)
 end nested class More...
 
def subsample (self, left, right, depth, trans=None)
 
def SVG (self, trans=None)
 

Public Attributes

 attr
 
 f
 
 high
 
 last_samples
 
 loop
 
 low
 

Static Public Attributes

dictionary defaults = {}
 
int discontinuity_limit = 5
 
float linearity_limit = 0.05
 
bool random_sampling = True
 
int recursion_limit = 15
 

Detailed Description

Draws a parametric function as a path.

Curve(f, low, high, loop, attribute=value)

f                      required         a Python callable or string in
                                        the form "f(t), g(t)"
low, high              required         left and right endpoints
loop                   default=False    if True, connect the endpoints
attribute=value pairs  keyword list     SVG attributes

Definition at line 1498 of file svgfig.py.

Constructor & Destructor Documentation

def svgfig.Curve.__init__ (   self,
  f,
  low,
  high,
  loop = False,
  attr 
)

Definition at line 1518 of file svgfig.py.

1518  def __init__(self, f, low, high, loop=False, **attr):
1519  self.f = f
1520  self.low = low
1521  self.high = high
1522  self.loop = loop
1523 
1524  self.attr = dict(self.defaults)
1525  self.attr.update(attr)
1526 
def __init__(self, f, low, high, loop=False, attr)
Definition: svgfig.py:1518
dictionary defaults
Definition: svgfig.py:1509

Member Function Documentation

def svgfig.Curve.__repr__ (   self)

Definition at line 1515 of file svgfig.py.

References svgfig.SVG.attr, svgfig.Path.attr, svgfig.Curve.attr, svgfig.Curve.f, svgfig.Curve.high, and svgfig.Curve.low.

Referenced by data_sources.json_file.__str__().

1515  def __repr__(self):
1516  return "<Curve %s [%s, %s] %s>" % (self.f, self.low, self.high, self.attr)
1517 
def __repr__(self)
Definition: svgfig.py:1515
def svgfig.Curve.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 1653 of file svgfig.py.

References svgfig.SVG.attr, svgfig.Path.attr, svgfig.Curve.attr, svgfig.Curve.f, svgfig.funcRtoR2(), svgfig.Curve.last_samples, svgfig.Curve.loop, CastorLedAnalysis.sample, HcalLedAnalysis.sample, CastorPedestalAnalysis.sample, HcalPedestalAnalysis.sample, svgfig.Curve.sample(), and svgfig.totrans().

Referenced by svgfig.Curve.SVG(), svgfig.Poly.SVG(), svgfig.Line.SVG(), svgfig.Rect.SVG(), and svgfig.Ellipse.SVG().

1653  def Path(self, trans=None, local=False):
1654  """Apply the transformation "trans" and return a Path object in
1655  global coordinates. If local=True, return a Path in local coordinates
1656  (which must be transformed again)."""
1657 
1658  if isinstance(trans, basestring): trans = totrans(trans)
1659  if isinstance(self.f, basestring): self.f = funcRtoR2(self.f)
1660 
1661  self.sample(trans)
1662 
1663  output = []
1664  for s in self.last_samples:
1665  if s.X != None and s.Y != None:
1666  if s.left == None or s.left.Y == None:
1667  command = "M"
1668  else:
1669  command = "L"
1670 
1671  if local: output.append((command, s.x, s.y, False))
1672  else: output.append((command, s.X, s.Y, True))
1673 
1674  if self.loop: output.append(("Z",))
1675  return Path(output, **self.attr)
1676 
def funcRtoR2(expr, var="t", globals=None, locals=None)
Definition: svgfig.py:1468
def Path(self, trans=None, local=False)
Definition: svgfig.py:1653
def sample(self, trans=None)
end nested class
Definition: svgfig.py:1575
def totrans(expr, vars=("x","y"), globals=None, locals=None)
Definition: svgfig.py:597
def svgfig.Curve.sample (   self,
  trans = None 
)

end nested class

Adaptive-sampling algorithm that chooses the best sample points
for a parametric curve between two endpoints and detects
discontinuities.  Called by SVG().

Definition at line 1575 of file svgfig.py.

References funct.abs(), svgfig.Curve.f, objects.autophobj.float, svgfig.Curve.high, svgfig.Curve.linearity_limit, svgfig.Curve.low, svgfig.Curve.recursion_limit, and svgfig.Curve.subsample().

Referenced by svgfig.Curve.Path().

1575  def sample(self, trans=None):
1576  """Adaptive-sampling algorithm that chooses the best sample points
1577  for a parametric curve between two endpoints and detects
1578  discontinuities. Called by SVG()."""
1579  oldrecursionlimit = sys.getrecursionlimit()
1580  sys.setrecursionlimit(self.recursion_limit + 100)
1581  try:
1582  # the best way to keep all the information while sampling is to make a linked list
1583  if not (self.low < self.high): raise ValueError("low must be less than high")
1584  low, high = self.Sample(float(self.low)), self.Sample(float(self.high))
1585  low.link(None, high)
1586  high.link(low, None)
1587 
1588  low.evaluate(self.f, trans)
1589  high.evaluate(self.f, trans)
1590 
1591  # adaptive sampling between the low and high points
1592  self.subsample(low, high, 0, trans)
1593 
1594  # Prune excess points where the curve is nearly linear
1595  left = low
1596  while left.right != None:
1597  # increment mid and right
1598  mid = left.right
1599  right = mid.right
1600  if right != None and left.X != None and left.Y != None and mid.X != None and mid.Y != None and right.X != None and right.Y != None:
1601  numer = left.X*(right.Y - mid.Y) + mid.X*(left.Y - right.Y) + right.X*(mid.Y - left.Y)
1602  denom = math.sqrt((left.X - right.X)**2 + (left.Y - right.Y)**2)
1603  if denom != 0. and abs(numer/denom) < self.linearity_limit:
1604  # drop mid (the garbage collector will get it)
1605  left.right = right
1606  right.left = left
1607  else:
1608  # increment left
1609  left = left.right
1610  else:
1611  left = left.right
1612 
1613  self.last_samples = self.Samples(low, high)
1614 
1615  finally:
1616  sys.setrecursionlimit(oldrecursionlimit)
1617 
def subsample(self, left, right, depth, trans=None)
Definition: svgfig.py:1618
end Sample
Definition: svgfig.py:1551
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def sample(self, trans=None)
end nested class
Definition: svgfig.py:1575
int recursion_limit
Definition: svgfig.py:1511
float linearity_limit
Definition: svgfig.py:1512
nested class Sample
Definition: svgfig.py:1528
def svgfig.Curve.subsample (   self,
  left,
  right,
  depth,
  trans = None 
)
Part of the adaptive-sampling algorithm that chooses the best
sample points.  Called by sample().

Definition at line 1618 of file svgfig.py.

References funct.abs(), svgfig.Curve.discontinuity_limit, svgfig.Curve.f, svgfig.Curve.linearity_limit, svgfig.Curve.random_sampling, svgfig.Curve.recursion_limit, and svgfig.Curve.subsample().

Referenced by svgfig.Curve.sample(), and svgfig.Curve.subsample().

1618  def subsample(self, left, right, depth, trans=None):
1619  """Part of the adaptive-sampling algorithm that chooses the best
1620  sample points. Called by sample()."""
1621 
1622  if self.random_sampling:
1623  mid = self.Sample(left.t + random.uniform(0.3, 0.7) * (right.t - left.t))
1624  else:
1625  mid = self.Sample(left.t + 0.5 * (right.t - left.t))
1626 
1627  left.right = mid
1628  right.left = mid
1629  mid.link(left, right)
1630  mid.evaluate(self.f, trans)
1631 
1632  # calculate the distance of closest approach of mid to the line between left and right
1633  numer = left.X*(right.Y - mid.Y) + mid.X*(left.Y - right.Y) + right.X*(mid.Y - left.Y)
1634  denom = math.sqrt((left.X - right.X)**2 + (left.Y - right.Y)**2)
1635 
1636  # if we haven't sampled enough or left fails to be close enough to right, or mid fails to be linear enough...
1637  if depth < 3 or (denom == 0 and left.t != right.t) or denom > self.discontinuity_limit or (denom != 0. and abs(numer/denom) > self.linearity_limit):
1638 
1639  # and we haven't sampled too many points
1640  if depth < self.recursion_limit:
1641  self.subsample(left, mid, depth+1, trans)
1642  self.subsample(mid, right, depth+1, trans)
1643 
1644  else:
1645  # We've sampled many points and yet it's still not a small linear gap.
1646  # Break the line: it's a discontinuity
1647  mid.y = mid.Y = None
1648 
def subsample(self, left, right, depth, trans=None)
Definition: svgfig.py:1618
bool random_sampling
Definition: svgfig.py:1510
int discontinuity_limit
Definition: svgfig.py:1513
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int recursion_limit
Definition: svgfig.py:1511
float linearity_limit
Definition: svgfig.py:1512
nested class Sample
Definition: svgfig.py:1528
def svgfig.Curve.SVG (   self,
  trans = None 
)
Apply the transformation "trans" and return an SVG object.

Definition at line 1649 of file svgfig.py.

References SiStripHistoPlotter::PlotParameter.Path, Json::PathArgument.Path, and svgfig.Curve.Path().

1649  def SVG(self, trans=None):
1650  """Apply the transformation "trans" and return an SVG object."""
1651  return self.Path(trans).SVG()
1652 
def SVG(self, trans=None)
Definition: svgfig.py:1649
def Path(self, trans=None, local=False)
Definition: svgfig.py:1653

Member Data Documentation

svgfig.Curve.attr
dictionary svgfig.Curve.defaults = {}
static

Definition at line 1509 of file svgfig.py.

Referenced by tree.Tree.reset(), and tree.Tree.var().

int svgfig.Curve.discontinuity_limit = 5
static

Definition at line 1513 of file svgfig.py.

Referenced by svgfig.Curve.subsample().

svgfig.Curve.f
svgfig.Curve.high
svgfig.Curve.last_samples

Definition at line 1613 of file svgfig.py.

Referenced by svgfig.Curve.Path().

float svgfig.Curve.linearity_limit = 0.05
static

Definition at line 1512 of file svgfig.py.

Referenced by svgfig.Curve.sample(), and svgfig.Curve.subsample().

svgfig.Curve.loop

Definition at line 1522 of file svgfig.py.

Referenced by svgfig.Poly.__repr__(), svgfig.Curve.Path(), and svgfig.Poly.Path().

svgfig.Curve.low
bool svgfig.Curve.random_sampling = True
static

Definition at line 1510 of file svgfig.py.

Referenced by svgfig.Curve.subsample().

int svgfig.Curve.recursion_limit = 15
static

Definition at line 1511 of file svgfig.py.

Referenced by svgfig.Curve.sample(), and svgfig.Curve.subsample().