Changeset 1641

Show
Ignore:
Timestamp:
07/16/07 09:57:46 (1 year ago)
Author:
wrobe..@pld-linux.org
Message:

- make line constraint non-juggling constraint
- allow to use projection not only for Constraint.solve_for method

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphas/branches/hw/gaphas/constraint.py

    r1638 r1641  
    425425 
    426426        if self.balance is None: 
    427             b1, b2 = self.band 
    428             w = b2 - b1 
    429             if w != 0: 
    430                 self.balance = (v - b1) / w 
    431             else: 
    432                 self.balance = 0 
     427            self.update_balance() 
     428 
     429 
     430    def update_balance(self): 
     431        b1, b2 = self.band 
     432        w = b2 - b1 
     433        if w != 0: 
     434            self.balance = (self.v - b1) / w 
     435        else: 
     436            self.balance = 0 
    433437 
    434438 
     
    456460        self._point = point 
    457461 
    458         self.update_ratio() 
    459  
    460462 
    461463    def update_ratio(self): 
     
    465467        >>> point = (Variable(15), Variable(4)) 
    466468        >>> lc = LineConstraint(line=line, point=point) 
     469        >>> lc.update_ratio() 
    467470        >>> lc.ratio_x, lc.ratio_y 
    468471        (0.5, 0.20000000000000001) 
     
    499502        >>> point = (Variable(15), Variable(4)) 
    500503        >>> lc = LineConstraint(line=line, point=point) 
     504        >>> lc.update_ratio() 
    501505        >>> lc.solve_for(point[0]) 
    502506        >>> point 
     
    515519        y = sy + (ey - sy) * self.ratio_y 
    516520 
    517         px.value, py.value = x, y 
     521        if px.value != x: 
     522            px.value = x 
     523        if py.value != y: 
     524            py.value = y 
    518525 
    519526 
     
    539546    >>> bc = BalanceConstraint(band=(zero, r.width), v=v.x, balance=0.25) 
    540547    >>> eq = EqualsConstraint(a=r.height, b=v.y) 
    541     >>> proj(bc, {v.x: v.x0, r.width: r.x0, zero: r.x0}) 
    542     >>> proj(eq, {v.y: v.y0, r.height: r.y0}) 
     548    >>> proj(bc, data={v.x: v.x0, r.width: r.x0, zero: r.x0}) 
     549    >>> proj(eq, data={v.y: v.y0, r.height: r.y0}) 
    543550     
    544551    Let's change rectangle dimensions 
     
    552559    >>> v.x, v.y 
    553560    (Variable(6, 20), Variable(-20, 20)) 
     561 
     562    It is also possible to use projection in case of other constraint's 
     563    methods than 'solve_for' 
     564    >>> r = Rectangle(5, 5, 20, 20) 
     565    >>> v = Vector(10, 50, 5, -25) 
     566    >>> zero = Variable(0) 
     567    >>> bc = BalanceConstraint(band=(zero, r.width), v=v.x) 
     568    >>> proj(bc, data={v.x: v.x0, r.width: r.x0, zero: r.x0}, f=bc.update_balance) 
     569    >>> bc.update_balance() 
     570    >>> bc.balance 
     571    0.5 
    554572    """ 
    555573    def _cproj(self): 
     
    569587    def __call__(self, c, *args, **kw): 
    570588        """ 
    571         Decorator for Constraint.solve_for method to perform 
    572         projection to common space before variable solving and later 
    573         project to internal variable space. 
    574         """ 
    575         f = c.solve_for 
    576         def wrapper(var): 
     589        Decorator for Constraint.solve_for method to perform projection to 
     590        common space before variable solving and later project to internal 
     591        variable space. 
     592        """ 
     593        if 'f' in kw: 
     594            f = kw['f'] 
     595            fn = f.__name__ 
     596        else: 
     597            f = c.solve_for 
     598            fn = 'solve_for' 
     599 
     600        def wrapper(*fargs): 
    577601            self._cproj(c, *args, **kw) 
    578             f(var
     602            f(*fargs
    579603            self._iproj(c, *args, **kw) 
    580         c.solve_for = wrapper 
     604 
     605        setattr(c, fn, wrapper) 
    581606 
    582607