-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdummy_scaffold_sketch.py
233 lines (167 loc) · 6.45 KB
/
dummy_scaffold_sketch.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from __future__ import print_function, division
import numpy as np
import fit_line
import fit_point
import fit_curve
'''
This is just a dummpy scaffold sketch.
It will just return the raw data.
'''
def make_new_program_state():
'''
Returns a dictionary for storing the state of a scaffold sketching
program.
The dictionary has keys value pairs
'construction_lines' : [ list of ndarray as construction lines ]
'shape_curves' : [ list of points ]
'raw_construction_lines' : [ list of tuple as raw points]
'raw_shape_curves' : [ list of ndarray as raw shape points ]
'line_points': [ np.array([x,y,z]) ] points attach to lines
'line_directions' : [ np.array([0, 1, 0]) ] # start with vertical direction
'line_lengths': [ float as line_length ]
'points_info': [ points_idx: [ a list of ndarray as direction at that point] ]
'''
state = { 'construction_lines' : [],
'shape_curves' : [],
'raw_construction_lines': [], # for debug
'raw_shape_curves' : [], # for debug
'line_points' : [],
'line_directions' : [ np.array([0, 1, 0]) ],
'line_lengths' : [],
'points_info' : {}
}
return state
def line_or_point(state, data ):
'''
Given:
state: a state as returned by `make_new_program_state`
data: from html side
'scale' : scale vector
'pts': pts
Returns:
use this function to decide whether it's a line or point
'''
### Use the scale to set up thresholds
scale = data['scale']
pts = data['pts']
# print('scale', scale)
# print('data', data)
### 1 Get startpoint and endpoint of line
### 2 Depend on how many constraints, how long the line is, let it be a point or line
startpoint, endpoint = fit_line.endpoints_line( pts )
startpoint = np.asarray( startpoint )
endpoint = np.asarray( endpoint )
if np.linalg.norm( startpoint - endpoint ) < 0.03: # less than 0.03, by no means make it a point?
result = incorporate_new_raw_point( state, pts)
print('point')
else:
result = incorporate_new_raw_construction_line( state, pts )
print('line')
return result
def incorporate_new_raw_point( state, pts ):
'''
Given:
state: a state as returned by `make_new_program_state`
pts: raw points from the GUI as a sequence of (x,y,z?) triplets.
Returns:
point
Modified `state` to add a new point based off
of the raw GUI input `pts`.
Called by line_or_point function.
'''
point = fit_point.point_fitting(state, pts)
return point
def incorporate_new_raw_construction_line( state, pts ):
'''
Given:
state: a state as returned by `make_new_program_state`
pts: startpoint and endpoint of construction line
Returns:
line: a part of points which are the start and end of the new construction line
Modified `state` to add a new construction line based off
of the raw GUI input `pts`.
'''
line = fit_line.endpoints_line( pts )
line = np.asarray(line)
state['construction_lines'].append( line )
return line
def incorporate_new_raw_shape_line( state, data ):
'''
Given:
state: a state as returned by `make_new_program_state`
data: from html side
'scale' : scale vector
'pts': pts
Returns:
curve: curve points
Modified `state` to add a new construction line based off
of the raw GUI input `pts`.
'''
### Use the scale to set up thresholds
scale = data['scale']
pts = data['pts']
### 1 Fit a curve to the points.
### 2 Store all curve points in state.
xi = [ pt['x'] for pt in pts]
yi = [ pt['y'] for pt in pts]
zi = [ pt['z'] for pt in pts]
curve_points = np.zeros([ len(pts) , 3])
curve_points[:, 0] = xi
curve_points[:, 1] = yi
curve_points[:, 2] = zi
# print( curve_points )
points = curve_points.tolist()
state['shape_curves'].append( points )
return points
def prepare_state_for_UI( state ):
"""
construction_lines: list of ndarray
shape_lines: list of ndarray
prepare them to json so it is easier for javascript to parse
"""
current_state = { 'lines' : [], 'points': [], 'curves' : [] }
for construction_line in state['construction_lines']:
current_state['lines'].append( construction_line.tolist() )
for point in state['line_points']:
current_state['points'].append( point.tolist() )
for shape_curve in state['shape_curves']:
current_state['curves'].append( shape_curve )
return current_state
def prepare_state_for_save_all_info( state ):
'''
This is save all info
'''
"""
construction_lines: list of ndarray
shape_lines: list of ndarray
Object of type ndarray is not JSON serializable
convert things to list
"""
current_state = { 'construction_lines' : [], # list of ndarray
'shape_curves' : [], # list of list
'raw_construction_lines': [], # list of ndarray
'raw_shape_curves': [],
'line_points' : [],
'line_directions': [], # list of ndarray
'line_lengths': [],
'points_info': {}
}
for construction_line in state['construction_lines']:
current_state['construction_lines'].append( construction_line.tolist() )
for shape_curve in state['shape_curves']:
current_state['shape_curves'].append( shape_curve )
for raw_construction_line in state['raw_construction_lines']:
current_state['raw_construction_lines'].append( raw_construction_line )
for raw_shape_curve in state['raw_shape_curves']:
current_state['raw_shape_curves'].append( raw_shape_curve.tolist() )
for point in state['line_points']:
current_state['line_points'].append( point.tolist() )
for dir in state['line_directions']:
current_state['line_directions'].append( dir.tolist() )
for length in state['line_lengths']:
current_state['line_lengths'].append( length )
for point, infos in state['points_info'].items():
current_state['points_info'][point] = []
for info in infos:
current_state['points_info'][point].append( info.tolist() )
return current_state