• Whitham D. Reeve II's avatar
    Replace Point class with a 3 position tuple and a collection of helper functions. · 3ca79c3c
    Whitham D. Reeve II authored
    PyCam has been spending about 75% of it's time creating point objects. I decided to replace
    the pycam Point object with a 3 position tuple, and the pycam Vector object with a 4 position tuple.
    
    All of the point object methods were rewritten to accept and emit tuples. The transform_by_matrix method
    was rewritten to accept both 3 and 4 position tuples with behavior dependent on that size. The
    normalized method is the only method required to emit the same kind of object that it is called on, and
    this has been taken care of with the tuple version.
    
    What does this mean?
    All instances of Point(x,y,z) have been converted into (x,y,z)
    All instances of Vector(x,y,z) have been converted into (x,y,z,'v')
    The notation to access the x,y,z of the Point objects has been been changed
    	from p.x,p.y,p.z to p[0],p[1],p[2]
    The notation for the point math functions has been completely changed.
    Instead of p1.sub(p2) it has been converted to psub(p1,p2)
    Instead of p1.sub(p2).mul(0.5) it has been converted to pmul(psub(p1,p2), 0.5)
    
    It is very important to point out that the tuple is an immutable type.
    You can not change it once you create it.
    t[0] = calculated_x_value # syntax error
    You have to replace the reference (t) to the old tuple with a new reference to a new tuple.
    t = (calculated_x_value, t[1], t[2]) # works
    
    There was a particularly hairy mutable/immutable barrier in the next() generator present in each class that
    inherits TransformableContainer. The TransformableContainer.transform_by_matrix function uses these
    generators to collapse polygons and lines and triangles into points where a shift is performed on each.
    Now that point is immutable, you can not change the value emitted by the generator.
    Geometry/__init__.py transform_by_marix and the associated next() generator in each sub class has been
    rewritten to transfer the attr used to store the reference to the tuple.
    Geometry/__init__.py transform_by_matrix now sets these attributes which effectively gets around this
    limitation.
    
    There could be some old point object code in any of the files not contained in this commit.
    It is impossible to know without running the code path and/or careful analysis of each file.
    
    Some list comprehensions that convert a list of point objects into a 3 position tuple have been removed.
    
    Whitham D. Reeve II
    3ca79c3c
__init__.py 1.77 KB
# -*- coding: utf-8 -*-
"""
$Id$

Copyright 2008 Lode Leroy

This file is part of PyCAM.

PyCAM 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 3 of the License, or
(at your option) any later version.

PyCAM 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 PyCAM.  If not, see <http://www.gnu.org/licenses/>.
"""

__all__ = ["PathAccumulator", "SimpleCutter", "ZigZagCutter", "PolygonCutter",
        "ContourCutter", "BasePathProcessor"]


class BasePathProcessor(object):

    def __init__(self):
        self.paths = []

    def new_direction(self, direction):
        pass

    def end_direction(self):
        pass

    def finish(self):
        pass

    def sort_layered(self, upper_first=True):
        if upper_first:
            compare_height = lambda path1, path2: \
                    path1.points[0][2] < path2.points[0][2]
        else:
            compare_height = lambda path1, path2: \
                    path1.points[0][2] > path2.points[0][2]
        finished = False
        while not finished:
            index = 0
            finished = True
            while index < len(self.paths) - 1:
                current_path = self.paths[index]
                next_path = self.paths[index + 1]
                if compare_height(current_path, next_path):
                    del self.paths[index]
                    self.paths.insert(index + 1, current_path)
                    finished = False
                index += 1