Commit 107ff4dc authored by sumpfralle's avatar sumpfralle

fixed code-style issues


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@491 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 93ba4797
......@@ -23,7 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
try:
import OpenGL.GL as GL
GL_enabled = True
except:
except ImportError:
GL_enabled = False
......@@ -31,16 +31,20 @@ import math
class Line:
id=0
def __init__(self,p1,p2):
id = 0
def __init__(self, p1, p2):
self.id = Line.id
Line.id += 1
self.p1 = p1
self.p2 = p2
self._dir = None
self._len = None
def __repr__(self):
return "Line<%g,%g,%g>-<%g,%g,%g>" % (self.p1.x,self.p1.y,self.p1.z,
self.p2.x,self.p2.y,self.p2.z)
return "Line<%g,%g,%g>-<%g,%g,%g>" % (self.p1.x, self.p1.y, self.p1.z,
self.p2.x, self.p2.y, self.p2.z)
def __cmp__(self, other):
""" Two lines are equal if both pairs of points are at the same
locations.
......@@ -58,13 +62,13 @@ class Line:
return cmp(str(self), str(other))
def dir(self):
if not hasattr(self,"_dir"):
if self._dir is None:
self._dir = self.p2.sub(self.p1)
self._dir.normalize()
return self._dir
def len(self):
if not hasattr(self,"_len"):
if self._len is None:
self._len = self.p2.sub(self.p1).norm()
return self._len
......@@ -73,14 +77,14 @@ class Line:
def closest_point(self, p):
v = self.dir()
l = self.p1.dot(v)-p.dot(v)
l = self.p1.dot(v) - p.dot(v)
return self.p1.sub(v.mul(l))
def dist_to_point_sq(self, p):
return p.sub(self.closest_point(p)).normsq()
def dist_to_point(self, p):
return sqrt(self.dist_to_point_sq(p))
return math.sqrt(self.dist_to_point_sq(p))
def minx(self):
return min(self.p1.x, self.p2.x)
......@@ -117,10 +121,14 @@ class Line:
line_size = math.sqrt((line[0] ** 2) + (line[1] ** 2))
ortho_size = math.sqrt((ortho[0] ** 2) + (ortho[1] ** 2))
ortho_dest_size = line_size / 10.0
ortho = (ortho[0] * ortho_dest_size / ortho_size, ortho[1] * ortho_dest_size / ortho_size)
line_back = (-line[0] * ortho_dest_size / line_size, -line[1] * ortho_dest_size / line_size)
p3 = (self.p2.x + ortho[0] + line_back[0], self.p2.y + ortho[1] + line_back[1], self.p2.z)
p4 = (self.p2.x - ortho[0] + line_back[0], self.p2.y - ortho[1] + line_back[1], self.p2.z)
ortho = (ortho[0] * ortho_dest_size / ortho_size,
ortho[1] * ortho_dest_size / ortho_size)
line_back = (-line[0] * ortho_dest_size / line_size,
-line[1] * ortho_dest_size / line_size)
p3 = (self.p2.x + ortho[0] + line_back[0],
self.p2.y + ortho[1] + line_back[1], self.p2.z)
p4 = (self.p2.x - ortho[0] + line_back[0],
self.p2.y - ortho[1] + line_back[1], self.p2.z)
GL.glVertex3f(p3[0], p3[1], p3[2])
GL.glVertex3f(self.p2.x, self.p2.y, self.p2.z)
GL.glVertex3f(p4[0], p4[1], p4[2])
......
......@@ -20,13 +20,13 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
"""
various matrix related functions for PyCAM
"""
# various matrix related functions for PyCAM
from pycam.Geometry.Point import Point
import math
def get_dot_product(a, b):
""" calculate the dot product of two 3d vectors
......@@ -37,7 +37,7 @@ def get_dot_product(a, b):
@rtype: float
@return: the dot product is (a0*b0 + a1*b1 + a2*b2)
"""
return sum(map(lambda l1, l2: l1 * l2, a, b))
return sum(l1 * l2 for l1, l2 in zip(a, b))
def get_cross_product(a, b):
""" calculate the cross product of two 3d vectors
......@@ -103,7 +103,8 @@ def get_rotation_matrix_from_to(v_orig, v_dest):
arcsin = -1.0
rot_angle = math.asin(arcsin)
# calculate the rotation axis
# the rotation axis is equal to the cross product of the original and destination vectors
# The rotation axis is equal to the cross product of the original and
# destination vectors.
rot_axis = Point(v_orig[1] * v_dest[2] - v_orig[2] * v_dest[1],
v_orig[2] * v_dest[0] - v_orig[0] * v_dest[2],
v_orig[0] * v_dest[1] - v_orig[1] * v_dest[0])
......@@ -124,7 +125,7 @@ def get_rotation_matrix_from_to(v_orig, v_dest):
t * rot_axis.z * rot_axis.z + c)
def get_rotation_matrix_axis_angle(rot_axis, rot_angle):
""" calculate rotation matrix for a normalized "rot_axis" vector and an angle
""" calculate rotation matrix for a normalized vector and an angle
see http://mathworld.wolfram.com/RotationMatrix.html
@type rot_axis: tuple(float)
......
......@@ -25,13 +25,7 @@ import pycam.Exporters.STLExporter
from pycam.Geometry import Triangle, Line, Point
from pycam.Geometry.TriangleKdtree import TriangleKdtree
from pycam.Toolpath import Bounds
from utils import INFINITE
try:
import OpenGL.GL as GL
GL_enabled = True
except:
GL_enabled = False
from pycam.Geometry.utils import INFINITE
MODEL_TRANSFORMATIONS = {
......@@ -146,9 +140,12 @@ class BaseModel(object):
for point in item.get_points():
if not point.id in processed:
processed.append(point.id)
x = point.x * matrix[0][0] + point.y * matrix[0][1] + point.z * matrix[0][2] + matrix[0][3]
y = point.x * matrix[1][0] + point.y * matrix[1][1] + point.z * matrix[1][2] + matrix[1][3]
z = point.x * matrix[2][0] + point.y * matrix[2][1] + point.z * matrix[2][2] + matrix[2][3]
x = point.x * matrix[0][0] + point.y * matrix[0][1] \
+ point.z * matrix[0][2] + matrix[0][3]
y = point.x * matrix[1][0] + point.y * matrix[1][1] \
+ point.z * matrix[1][2] + matrix[1][3]
z = point.x * matrix[2][0] + point.y * matrix[2][1] \
+ point.z * matrix[2][2] + matrix[2][3]
point.x = x
point.y = y
point.z = z
......@@ -188,6 +185,7 @@ class Model(BaseModel):
self._kdtree_dirty = True
# enable/disable kdtree
self._use_kdtree = use_kdtree
self._t_kdtree = None
def append(self, item):
super(Model, self).append(item)
......@@ -313,7 +311,8 @@ class ContourModel(BaseModel):
finished = False
while not finished:
if len(new_group) > 1:
# calculate new intersections for each pair of adjacent lines
# Calculate new intersections for each pair of adjacent
# lines.
for index in range(len(new_group)):
if (index == 0) and (not closed_group):
# skip the first line if the group is not closed
......@@ -324,7 +323,8 @@ class ContourModel(BaseModel):
do_lines_intersection(l1, l2)
# Remove all lines that were marked as obsolete during
# intersection calculation.
clean_group = [line for line in new_group if not line.p1 is None]
clean_group = [line for line in new_group
if not line.p1 is None]
finished = len(new_group) == len(clean_group)
if (len(clean_group) == 1) and closed_group:
new_group = []
......
......@@ -26,6 +26,7 @@ don't really need complete "Point" instances that consume a lot of memory.
Since python 2.6 the "namedtuple" factory is available.
This reduces the memory consumption of a toolpath down to 1/3.
"""
try:
# this works for python 2.6 or above (saves memory)
import collections.namedtuple
......@@ -43,7 +44,7 @@ class Path:
Path.id += 1
self.top_join = None
self.bot_join = None
self.winding=0
self.winding = 0
self.points = []
def __repr__(self):
......@@ -54,7 +55,7 @@ class Path:
if first:
first = False
else:
s +="-"
s += "-"
s += "%d(%g,%g,%g)" % (p.id, p.x, p.y, p.z)
return s
......
......@@ -20,7 +20,6 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
from Point import *
class Plane:
id = 0
......@@ -31,5 +30,5 @@ class Plane:
self.n = n
def __repr__(self):
return "Plane<%s,%s>" % (self.p,self.n)
return "Plane<%s,%s>" % (self.p, self.n)
......@@ -27,17 +27,19 @@ def _is_near(x, y):
class Point:
id=0
id = 0
def __init__(self,x,y,z):
def __init__(self, x, y, z):
self.id = Point.id
Point.id += 1
self.x = float(x)
self.y = float(y)
self.z = float(z)
self._norm = None
self._normsq = None
def __repr__(self):
return "Point%d<%g,%g,%g>" % (self.id,self.x,self.y,self.z)
return "Point%d<%g,%g,%g>" % (self.id, self.x, self.y, self.z)
def __cmp__(self, other):
""" Two points are equal if all dimensions are identical.
......@@ -57,30 +59,31 @@ class Point:
return cmp(str(self), str(other))
def mul(self, c):
return Point(self.x*c,self.y*c,self.z*c)
return Point(self.x * c, self.y * c, self.z * c)
def div(self, c):
return Point(self.x/c,self.y/c,self.z/c)
return Point(self.x / c, self.y / c, self.z / c)
def add(p1, p2):
return Point(p1.x+p2.x,p1.y+p2.y,p1.z+p2.z)
def add(self, p):
return Point(self.x + p.x, self.y + p.y, self.z + p.z)
def sub(p1, p2):
return Point(p1.x-p2.x,p1.y-p2.y,p1.z-p2.z)
def sub(self, p):
return Point(self.x - p.x, self.y - p.y, self.z - p.z)
def dot(p1, p2):
return p1.x*p2.x + p1.y*p2.y + p1.z*p2.z
def dot(self, p):
return self.x * p.x + self.y * p.y + self.z * p.z
def cross(p1, p2):
return Point(p1.y*p2.z-p2.y*p1.z, p2.x*p1.z-p1.x*p2.z, p1.x*p2.y-p2.x*p1.y)
def cross(self, p):
return Point(self.y * p.z - p.y * self.z, p.x * self.z - self.x * p.z,
self.x * p.y - p.x * self.y)
def normsq(self):
if not hasattr(self, "_normsq"):
if self._normsq is None:
self._normsq = self.dot(self)
return self._normsq
def norm(self):
if not hasattr(self, "_norm"):
if self._norm is None:
self._norm = math.sqrt(self.normsq())
return self._norm
......
......@@ -20,21 +20,21 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import math
from Point import *
from Line import *
from Triangle import *
from kdtree import *
from pycam.Geometry.utils import epsilon
from pycam.Geometry.Point import Point
from pycam.Geometry.kdtree import Node, kdtree
class PointKdtree(kdtree):
def __init__(self, points=[],cutoff=5, cutoff_distance=0.5, tolerance=0.001):
def __init__(self, points=None, cutoff=5, cutoff_distance=0.5,
tolerance=epsilon):
if points is None:
points = []
self._n = None
self.tolerance=tolerance
self.tolerance = tolerance
nodes = []
for p in points:
n = Node();
n = Node()
n.point = p
n.bound = []
n.bound.append(p.x)
......@@ -54,19 +54,18 @@ class PointKdtree(kdtree):
if self._n:
n = self._n
else:
n = Node();
n = Node()
n.bound = []
n.bound.append(x)
n.bound.append(y)
n.bound.append(z)
(nn,dist) = self.nearest_neighbor(n, self.dist)
if nn and dist<self.tolerance:
(nn, dist) = self.nearest_neighbor(n, self.dist)
if nn and (dist < self.tolerance):
self._n = n
return nn.p
else:
n.p = Point(x,y,z)
n.p = Point(x, y, z)
self._n = None
self.insert(n)
return n.p
This diff is collapsed.
This diff is collapsed.
......@@ -20,73 +20,73 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import math
from pycam.Geometry.kdtree import kdtree, Node
from Point import *
from Line import *
from Triangle import *
from kdtree import *
overlaptest = True
overlaptest=True
def SearchKdtree2d(kdtree, minx, maxx, miny, maxy):
if kdtree.bucket:
def SearchKdtree2d(tree, minx, maxx, miny, maxy):
if tree.bucket:
triangles = []
for n in kdtree.nodes:
global tests, hits, overlaptest
for n in tree.nodes:
if not overlaptest:
triangles.append(n.triangle)
else:
if not (n.bound[0]>maxx
or n.bound[1]<minx
or n.bound[2]>maxy
or n.bound[3]<miny):
if not (n.bound[0] > maxx
or n.bound[1] < minx
or n.bound[2] > maxy
or n.bound[3] < miny):
triangles.append(n.triangle)
return triangles
else:
if kdtree.cutdim==0:
if maxx<kdtree.minval:
if tree.cutdim == 0:
if maxx < tree.minval:
return []
elif maxx<kdtree.cutval:
return SearchKdtree2d(kdtree.lo, minx, maxx, miny, maxy)
elif maxx < tree.cutval:
return SearchKdtree2d(tree.lo, minx, maxx, miny, maxy)
else:
return SearchKdtree2d(kdtree.lo, minx, maxx, miny, maxy)+SearchKdtree2d(kdtree.hi, minx, maxx, miny, maxy)
elif kdtree.cutdim==1:
if minx>kdtree.maxval:
return SearchKdtree2d(tree.lo, minx, maxx, miny, maxy) \
+ SearchKdtree2d(tree.hi, minx, maxx, miny, maxy)
elif tree.cutdim == 1:
if minx > tree.maxval:
return []
elif minx>kdtree.cutval:
return SearchKdtree2d(kdtree.hi, minx, maxx, miny, maxy)
elif minx > tree.cutval:
return SearchKdtree2d(tree.hi, minx, maxx, miny, maxy)
else:
return SearchKdtree2d(kdtree.lo, minx, maxx, miny, maxy)+SearchKdtree2d(kdtree.hi, minx, maxx, miny, maxy)
elif kdtree.cutdim==2:
if maxy<kdtree.minval:
return SearchKdtree2d(tree.lo, minx, maxx, miny, maxy) \
+ SearchKdtree2d(tree.hi, minx, maxx, miny, maxy)
elif tree.cutdim == 2:
if maxy < tree.minval:
return []
elif maxy<kdtree.cutval:
return SearchKdtree2d(kdtree.lo, minx, maxx, miny, maxy)
elif maxy < tree.cutval:
return SearchKdtree2d(tree.lo, minx, maxx, miny, maxy)
else:
return SearchKdtree2d(kdtree.lo, minx, maxx, miny, maxy)+SearchKdtree2d(kdtree.hi, minx, maxx, miny, maxy)
elif kdtree.cutdim==3:
if miny>kdtree.maxval:
return SearchKdtree2d(tree.lo, minx, maxx, miny, maxy) \
+ SearchKdtree2d(tree.hi, minx, maxx, miny, maxy)
elif tree.cutdim == 3:
if miny > tree.maxval:
return []
elif miny>kdtree.cutval:
return SearchKdtree2d(kdtree.hi, minx, maxx, miny, maxy)
elif miny > tree.cutval:
return SearchKdtree2d(tree.hi, minx, maxx, miny, maxy)
else:
return SearchKdtree2d(kdtree.lo, minx, maxx, miny, maxy)+SearchKdtree2d(kdtree.hi, minx, maxx, miny, maxy)
return SearchKdtree2d(tree.lo, minx, maxx, miny, maxy) \
+ SearchKdtree2d(tree.hi, minx, maxx, miny, maxy)
class TriangleKdtree(kdtree):
def __init__(self, triangles, cutoff=3, cutoff_distance=1.0):
nodes = []
for t in triangles:
n = Node();
n = Node()
n.triangle = t
n.bound = []
n.bound.append(min(min(t.p1.x,t.p2.x),t.p3.x))
n.bound.append(max(max(t.p1.x,t.p2.x),t.p3.x))
n.bound.append(min(min(t.p1.y,t.p2.y),t.p3.y))
n.bound.append(max(max(t.p1.y,t.p2.y),t.p3.y))
n.bound.append(min(t.p1.x, t.p2.x, t.p3.x))
n.bound.append(max(t.p1.x, t.p2.x, t.p3.x))
n.bound.append(min(t.p1.y, t.p2.y, t.p3.y))
n.bound.append(max(t.p1.y, t.p2.y, t.p3.y))
nodes.append(n)
kdtree.__init__(self, nodes, cutoff, cutoff_distance)
super(TriangleKdtree, self).__init__(nodes, cutoff, cutoff_distance)
def Search(self, minx, maxx, miny, maxy):
return SearchKdtree2d(self, minx, maxx, miny, maxy)
......@@ -25,10 +25,9 @@ __all__ = ["utils", "Line", "Model", "Path", "Plane", "Point", "Triangle",
"PolygonExtractor", "TriangleKdtree", "intersection", "kdtree",
"Matrix"]
from Point import Point
from Line import Line
from Triangle import Triangle
from Path import Path
from Plane import Plane
from utils import *
from PolygonExtractor import PolygonExtractor
from pycam.Geometry.Point import Point
from pycam.Geometry.Line import Line
from pycam.Geometry.Triangle import Triangle
from pycam.Geometry.Path import Path
from pycam.Geometry.Plane import Plane
from pycam.Geometry.PolygonExtractor import PolygonExtractor
This diff is collapsed.
This diff is collapsed.
......@@ -26,24 +26,3 @@ epsilon = 0.0001
def sqr(x):
return x*x
def min3(x,y,z):
if x<y:
xy = x
else:
xy = y
if xy<z:
return xy
else:
return z
def max3(x,y,z):
if x>y:
xy = x
else:
xy = y
if xy>z:
return xy
else:
return z
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment