Commit bdef19e4 authored by Lars Kruse's avatar Lars Kruse

replaced "pnorm(psub(...))" with "pdist(...)"

parent 685ec9f9
......@@ -157,10 +157,10 @@ class Line(IDGenerator, TransformableContainer):
return psub(self.p1, pmul(v, l))
def dist_to_point_sq(self, p):
return pnormsq(psub(p, self.closes_point(p)))
return pdist_sq(p, self.closes_point(p))
def dist_to_point(self, p):
return sqrt(self.dist_to_point_sq(p))
return pdist(p, self.closes_point(p))
def is_point_inside(self, p):
if (p == self.p1) or (p == self.p2):
......@@ -214,7 +214,7 @@ class Line(IDGenerator, TransformableContainer):
if self.is_point_inside(x3):
candidates.append((x3, pnorm(c) / pnorm(a)))
elif self.is_point_inside(x4):
candidates.append((x4, pnorm(psub(line.p2, self.p1)) / pnorm(a)))
candidates.append((x4, pdist(line.p2, self.p1) / pnorm(a)))
elif line.is_point_inside(x1):
candidates.append((x1, 0))
elif line.is_point_inside(x2):
......
......@@ -1068,7 +1068,7 @@ class Rectangle(IDGenerator, TransformableContainer):
orders = ((p1, p2, p3, p4), (p1, p2, p4, p3), (p1, p3, p2, p4),
(p1, p3, p4, p2), (p1, p4, p2, p3), (p1, p4, p3, p2))
for order in orders:
if abs(pnorm(psub(order[0], order[2])) - pnorm(psub(order[1], order[3]))) < epsilon:
if abs(pdist(order[0], order[2]) - pdist(order[1], order[3])) < epsilon:
t1 = Triangle(order[0], order[1], order[2])
t2 = Triangle(order[2], order[3], order[0])
if t1.normal == t2.normal == normal:
......@@ -1132,7 +1132,7 @@ class Rectangle(IDGenerator, TransformableContainer):
if len(unique_vertices) != 2:
log.error("Invalid number of vertices: %s" % unique_vertices)
return None
if abs(pnorm(psub(unique_verticies[0], unique_verticies[1])) - pnorm(psub(shared_vertices[0], shared_vertices[1]))) < epsilon:
if abs(pdist(unique_verticies[0], unique_verticies[1]) - pdist(shared_vertices[0], shared_vertices[1])) < epsilon:
try:
return Rectangle(unique_vertices[0], unique_vertices[1],
shared_vertices[0], shared_vertices[1],
......
......@@ -33,9 +33,12 @@ def pnormsq(a):
return pdot(a,a)
def pdist(a, b, axes=None):
return sqrt(pdist_sq(a, b, axes=axes))
def pdist_sq(a, b, axes=None):
if axes is None:
axes = (0, 1, 2)
return sqrt(sum([(a[index] - b[index]) ** 2 for index in axes]))
return sum([(a[index] - b[index]) ** 2 for index in axes])
def pcmp(a,b):
""" Two points are equal if all dimensions are identical.
......
......@@ -71,7 +71,7 @@ class PolygonInTree(IDGenerator):
pass
def get_cost(self, other):
return pnorm(psub(other.start, self.end))
return pdist(other.start, self.end)
class PolygonPositionSorter(object):
......@@ -417,9 +417,9 @@ class Polygon(TransformableContainer):
def get_lengths(self):
result = []
for index in range(len(self._points) - 1):
result.append(pnorm(psub(self._points[index + 1], self._points[index])))
result.append(pdist(self._points[index + 1], self._points[index]))
if self.is_closed:
result.append(pnorm(psub(self._points[0], self._points[-1])))
result.append(pdist(self._points[0], self._points[-1]))
return result
def get_max_inside_distance(self):
......@@ -427,12 +427,12 @@ class Polygon(TransformableContainer):
"""
if len(self._points) < 2:
return None
distance = pnorm(psub(self._points[1], self._points[0]))
distance = pdist(self._points[1], self._points[0])
for p1 in self._points:
for p2 in self._points:
if p1 is p2:
continue
distance = max(distance, pnorm(psub(p2, p1)))
distance = max(distance, pdist(p2, p1))
return distance
def is_outer(self):
......@@ -601,7 +601,7 @@ class Polygon(TransformableContainer):
max_dist = 1000 * epsilon
def test_point_near(p, others):
for o in others:
if pnorm(psub(p, o)) < max_dist:
if pdist(p, o) < max_dist:
return True
return False
reverse_lines = []
......@@ -645,7 +645,7 @@ class Polygon(TransformableContainer):
# no lines are left
print "out 2"
return []
if pnorm(psub(prev_line.p2, next_line.p1)) > max_dist:
if pdist(prev_line.p2, next_line.p1) > max_dist:
cp, dist = prev_line.get_intersection(next_line)
else:
cp = prev_line.p2
......@@ -692,8 +692,8 @@ class Polygon(TransformableContainer):
# maybe we have been here before
if not cp in split_points:
split_points.append(cp)
elif (pnorm(psub(cp, line.p1)) < max_dist) or (pnorm(psub(cp, line.p2)) < max_dist):
if pnorm(psub(cp, lines.p1)) < pnorm(psub(cp, line.p2)):
elif (pdist(cp, line.p1) < max_dist) or (pdist(cp, line.p2) < max_dist):
if pdist(cp, lines.p1) < pdist(cp, line.p2):
non_reversed[index] = Line(cp, line.p2)
else:
non_reversed[index] = Line(line.p1, cp)
......@@ -881,7 +881,7 @@ class Polygon(TransformableContainer):
line1 = new_group[index1]
line2 = new_group[index2]
intersection, factor = line1.get_intersection(line2)
if intersection and (pnorm(psub(intersection, line1.p1)) > epsilon) and (pnorm(psub(intersection, line1.p2)) > epsilon):
if intersection and (pdist(intersection, line1.p1) > epsilon) and (pdist(intersection, line1.p2) > epsilon):
del new_group[index1]
new_group.insert(index1,
Line(line1.p1, intersection))
......@@ -895,7 +895,7 @@ class Polygon(TransformableContainer):
if not index1 + 1 in group_starts:
group_starts.append(index1 + 1)
# don't update index2 -> maybe there are other hits
elif intersection and (pnorm(psub(intersection, line1.p1)) < epsilon):
elif intersection and (pdist(intersection, line1.p1) < epsilon):
if not index1 in group_starts:
group_starts.append(index1)
index2 += 1
......@@ -1290,7 +1290,7 @@ class Polygon(TransformableContainer):
for index in range(len(collisions) - 1):
p1 = collisions[index][0]
p2 = collisions[index + 1][0]
if pnorm(psub(p1, p2)) < epsilon:
if pdist(p1, p2) < epsilon:
# ignore zero-length lines
continue
# Use the middle between p1 and p2 to check the
......
......@@ -71,12 +71,12 @@ class Triangle(IDGenerator, TransformableContainer):
self.plane = Plane(self.center, self.normal)
# calculate circumcircle (resulting in radius and middle)
denom = pnorm(pcross(psub(self.p2, self.p1), psub(self.p3, self.p2)))
self.radius = (pnorm(psub(self.p2, self.p1)) * pnorm(psub(self.p3, self.p2)) * pnorm(psub(self.p3, self.p1))) / (2 * denom)
self.radius = (pdist(self.p2, self.p1) * pdist(self.p3, self.p2) * pdist(self.p3, self.p1)) / (2 * denom)
self.radiussq = self.radius ** 2
denom2 = 2 * denom * denom
alpha = pnormsq(psub(self.p3, self.p2)) * pdot(psub(self.p1, self.p2), psub(self.p1, self.p3)) / denom2
beta = pnormsq(psub(self.p1, self.p3)) * pdot(psub(self.p2, self.p1), psub(self.p2, self.p3)) / denom2
gamma = pnormsq(psub(self.p1, self.p2)) * pdot(psub(self.p3, self.p1), psub(self.p3, self.p2)) / denom2
alpha = pdist_sq(self.p3, self.p2) * pdot(psub(self.p1, self.p2), psub(self.p1, self.p3)) / denom2
beta = pdist_sq(self.p1, self.p3) * pdot(psub(self.p2, self.p1), psub(self.p2, self.p3)) / denom2
gamma = pdist_sq(self.p1, self.p2) * pdot(psub(self.p3, self.p1), psub(self.p3, self.p2)) / denom2
self.middle = (self.p1[0] * alpha + self.p2[0] * beta + self.p3[0] * gamma,
self.p1[1] * alpha + self.p2[1] * beta + self.p3[1] * gamma,
self.p1[2] * alpha + self.p2[2] * beta + self.p3[2] * gamma)
......
......@@ -158,7 +158,7 @@ def get_bezier_lines(points_with_bulge, segments=32):
# point to the end point; a bulge of 0 indicates a straight segment,
# and a bulge of 1 is a semicircle.
alpha = 2 * (abs(bulge1) + abs(bulge2))
dist = pnorm(psub(p2, p1))
dist = pdist(p2, p1)
# calculate the radius of the circumcircle - avoiding divide-by-zero
if (abs(alpha) < epsilon) or (abs(math.pi - alpha) < epsilon):
radius = dist / 2.0
......
......@@ -142,8 +142,8 @@ class DXFParser(object):
current_group = []
groups.append(current_group)
def get_distance_between_groups(group1, group2):
forward = pnorm(psub(group1[-1].p2, group2[0].p1))
backward = pnorm(psub(group2[-1].p2, group1[0].p1))
forward = pdist(group1[-1].p2, group2[0].p1)
backward = pdist(group2[-1].p2, group1[0].p1)
return min(forward, backward)
remaining_groups = groups[:]
ordered_groups = []
......
......@@ -144,7 +144,7 @@ class PushCutter(object):
for line in layer_grid:
p1, p2 = line
# calculate the required calculation depth (recursion)
distance = pnorm(psub(p2, p1))
distance = pdist(p2, p1)
# TODO: accessing cutter.radius here is slightly ugly
depth = math.log(accuracy * distance / cutter.radius) / math.log(2)
depth = min(max(ceil(depth), 4), max_depth)
......
......@@ -66,7 +66,7 @@ def get_free_paths_triangles(models, cutter, p1, p2, return_triangles=False):
backward = pnormalized(psub(p1, p2))
forward = pnormalized(psub(p2, p1))
xyz_dist = pnorm(psub(p2, p1))
xyz_dist = pdist(p2, p1)
minx = min(p1[0], p2[0])
maxx = max(p1[0], p2[0])
......@@ -252,7 +252,7 @@ def get_max_height_triangles(model, cutter, x, y, minz, maxz):
def _check_deviance_of_adjacent_points(p1, p2, p3, min_distance):
straight = psub(p3, p1)
added = pnorm(psub(p2, p1)) + pnorm(psub(p3, p2))
added = pdist(p2, p1) + pdist(p3, p2)
# compare only the x/y distance of p1 and p3 with min_distance
if straight[0] ** 2 + straight[1] ** 2 < min_distance ** 2:
# the points are too close together
......
......@@ -272,7 +272,7 @@ def _get_edge_bridges(polygon, z_plane, min_bridges, average_distance,
avoid_distance):
def is_near_list(point_list, point, distance):
for p in point_list:
if pnorm(psub(p, point)) <= distance:
if pdist(p, point) <= distance:
return True
return False
lines = polygon.get_lines()
......
......@@ -176,7 +176,7 @@ class Toolpath(object):
self.last_pos = new_position
return True
else:
distance = pnorm(psub(new_position, self.last_pos))
distance = pdist(new_position, self.last_pos)
if self.moved_distance + distance > self.max_movement:
partial = (self.max_movement - self.moved_distance) / \
distance
......@@ -207,7 +207,7 @@ class Toolpath(object):
if ((abs(p_last[0] - p_next[0]) > epsilon) or (abs(p_last[1] - p_next[1]) > epsilon)):
# Draw the connection between the last and the next path.
# Respect the safety height.
if (abs(p_last[2] - p_next[2]) > epsilon) or (pnorm(psub(p_last, p_next)) > self._max_safe_distance + epsilon):
if (abs(p_last[2] - p_next[2]) > epsilon) or (pdist(p_last, p_next) > self._max_safe_distance + epsilon):
# The distance between these two points is too far.
# This condition helps to prevent moves up/down for
# adjacent lines.
......@@ -324,7 +324,7 @@ class Toolpath(object):
lastp = outpaths[-1][0][-1]
working_path.append((path[0][0], path[0][1], safety_height))
if ((abs(lastp[0] - path[0][0]) > epsilon) or (abs(lastp[1] - path[0][1]) > epsilon)):
if (abs(lastp[2] - path[0][2]) > epsilon) or (pnorm(psub(lastp, path[0])) > self._max_safe_distance + epsilon):
if (abs(lastp[2] - path[0][2]) > epsilon) or (pdist(lastp, path[0]) > self._max_safe_distance + epsilon):
outpaths.append((tuple([x[0] for x in groupby(working_path)]), True))
else:
working_path.append((0,0,0))
......@@ -373,7 +373,7 @@ class Toolpath(object):
for move_type, args in self.get_basic_moves():
if move_type in (MOVE_STRAIGHT, MOVE_STRAIGHT_RAPID):
if not current_position is None:
result += pnorm(psub(args, current_position))
result += pdist(args, current_position)
current_position = args
return result
def get_basic_moves(self, reset_cache=False):
......
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