Back to SDS/2 Parametric Scripts

 

# point.py

# class Point

# a Point instance has 'x', 'y', and 'z' atributes

# a Point instance has '__add__', '__sub__', '__mul__', 'dot_product', and 'uv' methods

# a Point instance repr: Point(2.50000000, 3.50000000, 0.00000000)

# a Point instance prints: (2.5000, 3.5000, 0.0000)

 

import sys

 

if 'C:\\SDS2_7.0\\macro' not in sys.path:

    sys.path.append('C:\\SDS2_7.0\\macro')

 

from macrolib.PrintPtList import formatPtList_

 

class Point(object):

    def __init__(self, x=0.0, y=0.0, z=0.0):

        self.x = float(x)

        self.y = float(y)

        self.z = float(z)

       

    def __str__(self):

        return '(%0.4f, %0.4f, %0.4f)' % (self.x, self.y, self.z)

   

    def __add__(self, other):

        return Point(self.x + other.x, self.y + other.y, self.z + other.z)

   

    def __sub__(self, other):

        return Point(self.x - other.x, self.y - other.y, self.z - other.z)

   

    def __mul__(self, f):

        return Point(self.x * f, self.y * f, self.z * f)

   

    def __repr__(self):

        return 'Point(%0.8f, %0.8f, %0.8f)' % (self.x, self.y, self.z)

   

    def dot_prod(self, other):

        return self.x*other.x + self.y*other.y + self.z*other.z

   

    def uv(self):

        return Point(self.x/self.magnitude(), self.y/self.magnitude(), self.z/self.magnitude())

   

    def cp(self, other):

        return Point(self.y*other.z - self.z*other.y, self.z*other.x - self.x*other.z, self.x*other.y - self.y*other.x)

   

    def magnitude(self):

        return (self.x**2 + self.y**2 + self.z**2)**0.5

   

    def test(cls):

        print 'You can access a class method without creating an instance.'

    test = classmethod(test)

   

    def test2():

        print 'I have never used a static method.'

    test2 = staticmethod(test2)

 

p1 = Point(2.5, 3.5)

print "p1 =", p1

print "repr(p1) =", repr(p1)

print "eval(repr(p1)) =", eval(repr(p1))

p2 =  p1 + (Point(2.5, 3.5, 7.7))

print "p2 =", p2

p3 = p2 - p1

print "p3 =", p3

p4 = p2 * 6

print "p4 =", p4

d34 = p3.uv().dot_prod(p4.uv())

print "p3_uv dot p4_uv = d34 =", d34

print "p4 unit vector =", p4.uv()

print "p4 magnitude =", p4.magnitude()

print 'p3 cross product p4 =', p3.cp(p4)

 

'''

>>> def cp(p1, p2):

... return (p1[1]*p2[2] - p1[2]*p2[1], p1[2]*p2[0] - p1[0]*p2[2], p1[0]*p2[1] - p1[1]*p2[0])

...

>>> p3 = (2.5000, 3.5000, 7.7000)

>>> p4 = (30.0000, 42.0000, 46.2000)

>>> cp(p3, p4)

(-161.70000000000002, 115.5, 0.0)

>>>

'''

 

def sortPoints(_ptlist, key='x'):

    try:

        if key.lower() in ['x', 'y', 'z']:

            def cmpItems(a,b):

                return cmp(eval('a.'+key.lower()), eval('b.'+key.lower()))

            _ptlist.sort(cmpItems)

        else:

            raise AttributeError, 'Invalid attribute was passed to sortPoints()'

    except:

        raise TypeError, 'Invalid point list'

 

ptList = [Point(1.0,2.0,3.0), Point(0.5,1.0,7.0), Point(12.6,-12.9,-15.6), Point(-99, 12.6, 1700),\

          Point(29.6, 44.4, 19.1), Point(22.8, 12.2, 4.54)]

 

print formatPtList_("Unsorted points list:", ptList)

 

sKey = "Y"

sortPoints(ptList, sKey)

 

print formatPtList_("Points list sorted on '%s' attribute:" % (sKey), ptList)

 

"""

>>> p1 = (2.5000, 3.5000, 0.0000)

repr(p1) = Point(2.50000000, 3.50000000, 0.00000000)

eval(repr(p1)) = (2.5000, 3.5000, 0.0000)

p2 = (5.0000, 7.0000, 7.7000)

p3 = (2.5000, 3.5000, 7.7000)

p4 = (30.0000, 42.0000, 46.2000)

p3_uv dot p4_uv = d34 = 0.94562842201

p4 unit vector = (0.4331, 0.6063, 0.6669)

p4 magnitude = 69.2707730576

p3 cross product p4 = (-161.7000, 115.5000, 0.0000)

Unsorted points list:

X attribute         Y attribute         Z attribute        

============================================================

1                   2                   3                  

1/2                 1                   7                  

1'-0 5/8            -1'-0 7/8           -1'-3 5/8          

-8'-3               1'-0 5/8            141'-8             

2'-5 5/8            3'-8 3/8            1'-7 1/8           

1'-10 13/16         1'-0 3/16           4 9/16             

 

Points list sorted on 'Y' attribute:

X attribute         Y attribute         Z attribute        

============================================================

1'-0 5/8            -1'-0 7/8           -1'-3 5/8          

1/2                 1                   7                  

1                   2                   3                  

1'-10 13/16         1'-0 3/16           4 9/16             

-8'-3               1'-0 5/8            141'-8             

2'-5 5/8            3'-8 3/8            1'-7 1/8           

 

>>>

"""

 

'''

>>> pointList = [[1.2, 5.0, 9.1999999999999993], [1.2, 5.0, 10], [5, 0.59999999999999998, 9.8000000000000007], [10.1, 5.2999999999999998, 8.0], [10.5, 1.3, 9.5]]

>>> xList = [p[0] for p in pointList]

>>> yList = [p[1] for p in pointList]

>>> zList = [p[2] for p in pointList]

>>> map(max, [xList, yList, zList])

[10.5, 5.2999999999999998, 10]

>>> map(min, [xList, yList, zList])

[1.2, 0.59999999999999998, 8.0]

>>>

'''