This file is indexed.

/usr/share/pyshared/pyscript/path.py is in python-pyscript 0.6.1-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# Copyright (C) 2002-2006  Alexei Gilchrist and Paul Cochrane
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# $Id: path.py,v 1.25 2006/05/16 05:38:31 aalexei Exp $

"""
The Path module
"""

__revision__ = '$Revision: 1.25 $'

#from pyscript.defaults import defaults
from math import sqrt, pi, sin, cos
from pyscript.vectors import P, Bbox, U, Identity, R
from pyscript.base import Color
from pyscript.objects import AffineObj
from pyscript.arrowheads import ArrowHead
import cStringIO

# -------------------------------------------------------------------------
# Pathlettes ... components of path, not used by themselves
# -------------------------------------------------------------------------

class _line(object):
    '''
    A line pathlette
    '''
    s = None
    e = None

    def __init__(self, s, e):
        object.__init__(self)
        self.s = s
        self.e = e

    def _get_start(self):
        """
        return start point
        """
        return self.s
    start = property(_get_start)

    def _get_end(self):
        """
        return end point
        """
        return self.e
    end = property(_get_end)

    def _get_length(self):
        """
        Get the length of the pathlette
        """
        return (self.e-self.s).length
    length = property(_get_length)

    def P(self, f):
        '''
        return point at fraction f of length
        '''
        return (self.s+(self.e-self.s)*f)

    def tangent(self, f):
        '''
        return angle of tangent of curve at fraction f of length
        '''
        return (self.e-self.s).arg


    def body(self):
        """
        Return the postscript body
        """
        return '%s lineto\n' % self.e

    def bbox(self, itoe = Identity):
        """
        Return the bounding box
        """

        p0 = itoe(self.s)
        p1 = itoe(self.e)

        x0 = min(p0[0], p1[0])
        x1 = max(p0[0], p1[0])
        y0 = min(p0[1], p1[1])
        y1 = max(p0[1], p1[1])

        return Bbox(sw = P(x0, y0), width = x1-x0,
                    height = y1-y0)

# -------------------------------------------------------------------------

class _bezier(object):
    '''
    A Bezier pathlette
    '''
    s = None
    e = None
    cs = None
    ce = None
    length = None
    
    TOL = None #tolerance for linearising

    def __init__(self, s, cs, ce, e, TOL = 2e-3, temporary = False):
        object.__init__(self)
        self.s = s # start
        self.e = e # end
        self.cs = cs # start control
        self.ce = ce # end control
        self.TOL = TOL
        
        # for efficiency don't do this unless we intend to
        # keep this pathlette
        if not temporary:
            self._points = self.straighten()
            self.set_length()

    def _is_straight(self):
        '''
        is this curve straight?
        '''

        L1 = (self.cs-self.s).length+\
          (self.ce-self.cs).length+\
          (self.e-self.ce).length
        L2 = (self.e-self.s).length
        
        if abs(L1-L2)/float(L1) <= self.TOL:
            return True
        else:
            return False

    def straighten(self):
        """
        Straighten the bezier curve
        """
        
        if self._is_straight():
            return (self.s, self.e)
        else: 
            c1, c2 = self._bisect(temporary = True)
            
            return (c1.straighten()+c2.straighten())
        
    def _bisect(self, t = .5, temporary = False):
        '''
        Divide this bezier into two
        '''
        p01   = self.s * (1-t) + self.cs * t
        p12   = self.cs * (1-t) + self.ce * t
        p23   = self.ce * (1-t) + self.e * t
        p012  = p01  * (1-t) + p12  * t
        p123  = p12  * (1-t) + p23  * t
        p0123 = p012 * (1-t) + p123 * t

        return (_bezier(self.s.copy(), 
            p01, p012, p0123, temporary = temporary), 
            _bezier(p0123.copy(), 
                p123, p23, self.e.copy(), temporary = temporary)) 

    
    def set_length(self):
        """
        Set the length of the bezier curve
        """
        L = 0
        p0 = self.s
        for p in self._points:
            L += (p-p0).length
            p0 = p
        self.length = L

    def body(self):
        """
        Return the postscript body of the object
        """
        return '%s %s %s curveto\n' % (self.cs, self.ce, self.e)


    def _t(self, t):
        '''
        Return point on curve parametrised by t [0-1]
        This is exact
        '''
        a1 = 3*(self.cs-self.s)
        a2 = 3*(self.s-2*self.cs+self.ce)
        a3 = -self.s+3*self.cs-3*self.ce+self.e
        
        return a3*t**3+a2*t**2+a1*t+self.s

    def _get_start(self):
        """
        return start point
        """
        return self.s
    start = property(_get_start)

    def _get_end(self):
        """
        return end point
        """
        return self.e
    end = property(_get_end)

    def P(self, f):
        '''
        return point on curve at fraction f of length
        '''
        assert 0 <= f <= 1

        #if self.length is None:
        #    self._cache()
        
        if f == 0:
            return self.s
        elif f == 1:
            return self.e
        
        Lf = self.length*f

        L = 0
        p0 = self.s
        for p in self._points:
            l = (p-p0).length
            if L+l >= Lf: 
                break
            L += l
            p0 = p

        # XXX Add a correction here so it's actually on the curve!
        # Newton Rapson?

        return (p-p0).U*(Lf-L) +p0

    def tangent(self, f):
        '''
        return angle of tangent of curve at fraction f of length
        '''
        assert 0 <= f <= 1

        if f == 0:
            return (self.cs-self.s).arg
        elif f == 1:
            return (self.e-self.ce).arg
        
        Lf = self.length*f

        L = 0
        p0 = self.s
        for p in self._points:
            l = (p-p0).length
            if L+l >= Lf: 
                break
            L += l
            p0 = p

        # XXX Add a correction here so it's actually on the curve!
        # Newton Rapson?

        return (p-p0).arg

    def bbox(self, itoe = Identity):
        """
        Return the bounding box of the object
        """
        # run through the list of points to get the bounding box
        
        #if self.length is None:
        #    self._cache()

        p0 = itoe(self.s)
        x0, y0 = p0
        x1, y1 = p0

        for p in self._points:
        
            p1 = itoe(p)

            x0 = min(x0, p1[0])
            x1 = max(x1, p1[0])
            y0 = min(y0, p1[1])
            y1 = max(y1, p1[1])

        return Bbox(sw = P(x0, y0), width = x1-x0,
                    height = y1-y0)

# -------------------------------------------------------------------------
# Curve specifier
# -------------------------------------------------------------------------

class C(object):
    """
    Specifier and generator for curves
    """

    # these params control the natural bezier
    # (they are set to the MetaPost defaults)
    _a = sqrt(2)
    _b = 1/16.
    _c = (3-sqrt(5))/2.
    
    # user parameters for curve:
    c0 = None
    c1 = None
    t0 = 1
    t1 = 1
    #curl = 1

    # this for specifing an arc
    arc = None
    
    def __init__(self, *args, **options):
        '''
        store curve parameters
        '''

        if len(args) == 1:
            raise ValueError, "C takes two arguments"
            #self.c0 = args[0]
            #self.c1 = args[0]
        
        elif len(args) == 2: 
            self.c0 = args[0]
            self.c1 = args[1]
        
        # anything supplied in keywords will override
        # the above points eg C(P(0, 0), c1=45)
        object.__init__(self)
        self(**options)

    def __call__(self, **options):
        '''
        Set a whole lot of attributes in one go
        
        eg::
          obj.set(bg=Color(.3), linewidth=2)

        @return: self 
        @rtype: self
        '''

        # first do non-property ones
        # this will raise an exception if class doesn't have attribute
        # I think this is good.
        prop = []
        for key, value in options.items():
            if isinstance(eval('self.__class__.%s'%key), property):
                prop.append((key, value))
            else:
                self.__class__.__setattr__(self, key, value)

        # now the property ones
        # (which are functions of the non-property ones)
        for key, value in prop:
            self.__class__.__setattr__(self, key, value)
                

        # for convenience return a reference to us
        return self

    def _get_fullyspecified(self):
        '''
        Is this curve fully specified (all control points)
        '''
        if self.arc is not None:
            # an arc is already fully specified
            return 1
        elif isinstance(self.c0, P) and isinstance(self.c1, P):
            # both points set
            return 1
        else:
            return 0

    fullyspecified = property(_get_fullyspecified, None)
        
    def curve(self, p0, p1 = None):
        '''
        return pathlette object corresponding to curve
        '''

        if self.arc is not None:
            # an arc
            return self.create_arc(p0)
        else:
            # a bezier
            if not self.fullyspecified:
                # fit natural curve...
                self.fit_curve(p0, p1)
            return self.create_bezier(p0, p1)
        
    def fit_curve(self, p0, p1):
        '''
        fit a natural looking spline to end slopes
        '''

        # first get the angles ...
        if type(self.c0) in [type(10), type(10.0)]:
            # turn this into a unit vector in that direction
            w0 = U(self.c0)
        elif isinstance(self.c0, R):
            # already have unit vectior
            w0 = self.c0
        elif isinstance(self.c0, P):
            # non-unit vector giving direction
            w0 = (self.c0-p0)
        else:
            raise ValueError, "Unknown control type c0"

        if type(self.c1) in [type(10), type(10.0)]:
            # turn this into a unit vector in that direction
            w1 = U(self.c1)
        elif isinstance(self.c1, R):
            # already have unit vectior
            w1 = self.c1
        elif isinstance(self.c1, P):
            # non-unit vector giving direction
            w1 = (self.c1-p1)
        else:
            raise ValueError, "Unknown control type c1"

        t = ((p1-p0).arg-w0.arg)*pi/180.
        p = -((-p1+p0).arg-w1.arg)*pi/180.

        a = self._a
        b = self._b
        c = self._c

        alpha = a*(sin(t)-b*sin(p))*(sin(p)-b*sin(t))*(cos(t)-cos(p))

        rho   = (2+alpha)/(1+(1-c)*cos(t)+c*cos(p))
        sigma = (2-alpha)/(1+(1-c)*cos(p)+c*cos(t))

        c0 = P( p0.x + rho*( (p1.x-p0.x)*cos(t)-(p1.y-p0.y)*sin(t))/(3*self.t0) ,
            p0.y + rho*( (p1.y-p0.y)*cos(t)+(p1.x-p0.x)*sin(t))/(3*self.t0) )

        c1 = P( 
                p0.x + (p1.x-p0.x)*(1-sigma*cos(p)/(3*self.t1)) -
                (p1.y-p0.y)*sigma*sin(p)/(3*self.t1) , 
                p0.y + (p1.y-p0.y)*(1-sigma*cos(p)/(3*self.t1)) + 
                (p1.x-p0.x)*sigma*sin(p)/(3*self.t1) 
                )

        # only change if we were given an angle
        if type(self.c0) in [type(10), type(10.0)]:
            self.c0 = c0
        if type(self.c1) in [type(10), type(10.0)]:
            self.c1 = c1

    def create_arc(self, centre):
        """
        Create an arc
        """
        return None

    def create_bezier(self, p0, p1):
        """
        Create a bezier curve
        """
        c0 = self.c0
        c1 = self.c1
    
        # fix up relative points:
        if isinstance(c0, R):
            c0 = p0+c0
        if isinstance(c1, R):
            c1 = p1+c1
    
        return _bezier(p0, c0, c1, p1)

# -------------------------------------------------------------------------
# Path object
# -------------------------------------------------------------------------

class Path(AffineObj):
    """
    A Path
    """
    
    fg = Color(0)
    bg = None
    linewidth = None
    linecap = None
    linejoin = None
    miterlimit = None
    dash = None
    closed = 0

    #ArrowHead instances:
    heads = []

    #_pathlettes=[]

    def __init__(self, *path, **options):

        self._pathlettes = []

        AffineObj.__init__(self, **options)

        path = list(path) # so we can use pop
        
        # first point must be, well a point
        assert isinstance(path[0], P)

        # if the last point of a closed path has been
        # skipped, add it now
        if not isinstance(path[-1], P) and self.closed:
            path.append(path[0])

        cp = path.pop(0) # current point

        while 1:
            if len(path) == 0: 
                break

            p = path.pop(0)
            if isinstance(p, R):
                p = cp+p
                self._pathlettes.append(_line(cp, p))
                cp = p
            elif isinstance(p, P):
                self._pathlettes.append(_line(cp, p))
                cp = p
            elif isinstance(p, C):
                c = p
                # Get the next point
                p = path.pop(0)
                if isinstance(p, R):
                    p = cp+p
                self._pathlettes.append(c.curve(cp, p))
                cp = p
                
            else:
                raise ValueError, "Unknown path control"

        # now add arrowheads
        heads = []
        for head in self.heads:
        
            # make a copy so this class has it's own instance 
                
            h=head.copy()

            # line colors overide arrow they blend
            # (how would a user overide this?)
            if options.has_key('fg'):
                h(fg=options['fg'])
                h(bg=options['fg'])
            
            # position it appropriately:
            h.__init__(tip=self.P(head.pos), angle=self.tangent(head.pos).arg)
    
            heads.append(h)
        self.heads = heads 
           

    def bbox(self):
        """
        Return the bounding box of the Path
        """
        b = Bbox()
        for pl in self._pathlettes:
            b.union(pl.bbox(self.itoe))

        # take into account extent of arrowheads
        for ar in self.heads:
            b.union(ar.bbox())

        return b

    def _get_start(self):
        """
        return start point
        """
        return self.itoe(self._pathlettes[0].start)
    start = property(_get_start)

    def _get_end(self):
        """
        return end point
        """
        return self.itoe(self._pathlettes[-1].end)
    end = property(_get_end)

    def _get_length(self):
        """
        Get the length of the path
        """

        l = 0
        for pl in self._pathlettes:
            l += pl.length
        return l
    length = property(_get_length)
        
    def P(self, f):
        '''
        Return the point at fraction f along the path
        '''

        assert 0 <= f <= 1

        Lf = self.length*f

        L = 0
        for pl in self._pathlettes:
            l = pl.length
            if L+l >= Lf: 
                break
            L += l
        return self.itoe(pl.P((Lf-L)/float(l)))

    def tangent(self, f):
        '''
        return tangent (unit vector) of curve at fraction f of length
        '''

        assert 0 <= f <= 1

        Lf = self.length*f

        L = 0
        for pl in self._pathlettes:
            l = pl.length
            if L+l >= Lf: 
                break
            L += l
        return U(self.itoe(U(pl.tangent((Lf-L)/float(l)))).arg)

    def body(self):
        """
        Return the postscript body of the Path
        """

        out = cStringIO.StringIO()

        if self.linewidth is not None:
            out.write("%g setlinewidth "%self.linewidth)

        if self.linecap is not None:
            out.write("%d setlinecap "%self.linecap)
            
        if self.linejoin is not None:
            out.write("%d setlinejoin "%self.linejoin)

        if self.miterlimit is not None:
            out.write("%f setmiterlimit "%self.miterlimit)

        if self.dash is not None:
            out.write(str(self.dash))

        out.write('newpath %s moveto\n'%self._pathlettes[0].start)

        for pl in self._pathlettes:
            out.write(pl.body())

        if self.closed:
            out.write(' closepath ')
        
        if self.bg is not None:
            out.write("gsave %s fill grestore\n"%self.bg)
        
        if self.fg is not None:
            out.write("%s stroke\n"%self.fg)

        for head in self.heads:
            out.write(str(head))

        return out.getvalue()

# -------------------------------------------------------------------------
# Arrow objects
# -------------------------------------------------------------------------

class Arrow(Path):
    '''
    Path object with arrow at end ... just for convenience
    '''
    #heads = [defaults.arrowhead(pos=1)]
    heads = [ArrowHead(1)]

class DoubleArrow(Path):
    """
    Path object with arrow at both ends ... just for convenience
    """
    #heads = [defaults.arrowhead(pos=1),defaults.arrowhead(pos=1,reverse=1)]
    heads = [ArrowHead(1), ArrowHead(0, reverse=1)]

# vim: expandtab shiftwidth=4: