-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
160 lines (136 loc) · 5.23 KB
/
solver.py
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
'''
Takes layer data and uses translational/rotation parameters
to tranform the 2D points into three dimensional space
'''
from layer import Layer
# from vectors import Point, Vector
from svgpathtools import paths2svg
from geosolver.geometric import GeometricProblem, GeometricSolver, DistanceConstraint,AngleConstraint, FixConstraint, CoincidenceConstraint, RigidConstraint
from geosolver.vector import vector
from geosolver.diagnostic import diag_select, diag_print
from geosolver.intersections import distance_2p, angle_3p
from geosolver.configuration import Configuration
from geosolver.geometric import Point
class Solver(object):
def __init__(self, joints, fixed, layers):
self.joints = joints
self.layers = layers
self.fixed = fixed
self.problem = GeometricProblem(dimension=3)
# self.problem = GeometricProblem(dimension=3, use_prototype=False)
def solve(self):
joints = self.joints
layers = self.layers
# print(joints)
# print(layers)
self.solve_shape(layers)
self.solve_fixed(self.fixed, layers)
self.solve_joints(joints, layers)
self.test(self.problem)
def solve_shape(self, layers):
for layer in layers:
print("Defining shape ", layer.id)
conf = {}
for point in layer.named_pairs:
print("With point ", point)
# Add initial points
self.problem.add_variable(Point(point[0]), vector([point[1], point[2],0]))
# Lock them together
conf[Point(point[0])] = vector([point[1], point[2],0])
self.problem.add_constraint(RigidConstraint(Configuration(conf)))
def solve_fixed(self, fixed, layers):
for i, layer in enumerate(layers):
print("Solving fixed ", layer.id)
i = 0
for point in layer.named_pairs:
if point[0] in fixed:
self.problem.add_constraint(FixConstraint(Point(point[0]), vector([point[1], point[2],0])))
i += 1
# layers[i].volume = self.solve_layer(layer)
def solve_joints(self, joints, layers):
print("joints ", joints)
for jointid in joints:
joint = joints[jointid]
i = 1
while i < len(joint):
print("Solving joint ", joint[0], joint[i])
self.problem.add_constraint(CoincidenceConstraint(Point(joint[0]), Point(joint[i])))
# self.problem.add_constraint(DistanceConstraint(joint[0], joint[i], 0.0))
i += 1
return layers
def test(self, problem):
"""Test solver on a given problem"""
#diag_select(".*")
print("problem:")
print(problem)
print("Solving...")
solver = GeometricSolver(problem)
print("...done")
print("drplan:")
print(solver.dr)
print("top-level rigids:",list(solver.dr.top_level()))
result = solver.get_result()
print("result:")
print(result)
print("result is",result.flag, "with", len(result.solutions),"solutions")
check = True
if len(result.solutions) == 0:
check = False
diag_select("(GeometricProblem.verify)|(satisfied)")
for sol in result.solutions:
print("solution:",sol)
check = check and problem.verify(sol)
if check:
print("all solutions valid")
else:
print("INVALID")
def solve_layer(self, layer):
origin = self.find_origin(layer)
translate_x = layer.translate.x
translate_y = layer.translate.y
translate_z = layer.translate.z
solved = []
for i, point in enumerate(layer.straight_pairs):
x = (point[0] - origin[0])# + translate_x
y = (point[1] - origin[1])# + translate_y
z = 0#translate_z
print("point: ", point, x, y)
# solved.append(self.point_transform(x, y, z, 0))
return solved
def point_transform(self, x, y, z, axis):
#Transform from rotation axis
base = Vector(0,1,0)
pnt = Vector(x,y,z)
# an = base.angle(pnt)
# print('a - ', base)
# print('b - ', pnt)
# print('c - ', an)
return pnt
def find_origin(self, layer):
#Bounding box to find path origin and translate to global origin
# xmin, xmax, ymin, ymax = paths2svg.big_bounding_box(path)
# origin = [(xmax + xmin) / 2, (ymax + ymin) / 2]
# return origin
max_x = 0
max_y = 0
min_x = 0
min_y = 0
first_run = True
for point in layer.straight_pairs:
if first_run == True:
max_x = point[0]
max_y = point[1]
min_x = point[0]
min_y = point[1]
first_run = False
else:
if point[0] > max_x:
max_x = point[0]
elif point[0] < min_x:
min_x = point[0]
if point[1] > max_y:
max_y = point[1]
elif point[1] < min_y:
min_y = point[1]
# print(point)
return [(max_x + min_x) / 2, (max_y + min_y) / 2]