-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathbraid.html
358 lines (316 loc) · 13.8 KB
/
braid.html
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Braid Maker</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"/>
<script type="text/javascript" src="jscripts/braid.js"/>
<script type="text/javascript" src="jscripts/controls.js"/>
<link rel="stylesheet" type="text/css" href="css/screen.css" media="screen" ></link>
</head>
<body>
<h1>Braid Maker    <span>
<button style="background-color: lightgreen" id="but0" onClick="tab(0)">Braid</button>
</span>  <span>
<button id="but1" onClick="tab(1)">Generate graphic (SVG)</button>
</span>  <span>
<button id="but2" onClick="tab(2)">Generate 3D object (OpenSCAD) </button>
</span>  <span>
<button id="but3" onClick="tab(3)">About</button>
</span>
</h1>
<div id="tab0">
<table >
<tr>
<th>Grid</th>
<td> N <input type="text" size="3" id="n" value="7"/>  M <input type="text" size="3" id="m" value="5"/>
</td>
</tr>
<tr>
<th>Hole size </th>
<td>
<input type="text" size="3" id="hole-1-size-x" value="0"/>  <input type="text" size="3" id="hole-1-size-y" value="0"/>
</td>
<th>centre</th>
<td> x:<input type="text" id="hole-1-x" value="" size="3"/> y: <input type="text" id="hole-1-y" value="" size="3"/>
</td>
</tr>
<tr>
<th>Hole size </th>
<td>
<input type="text" size="3" id="hole-2-size-x" value="0"/>  <input type="text" size="3" id="hole-2-size-y" value="0"/>
</td>
<th>centre</th>
<td> x:<input type="text" id="hole-2-x" value="" size="3"/> y: <input type="text" id="hole-2-y" value="" size="3"/>
</td>
</tr>
<tr>
<th>Hole size </th>
<td>
<input type="text" size="3" id="hole-3-size-x" value="0"/>  <input type="text" size="3" id="hole-3-size-y" value="0"/>
</td>
<th>centre</th>
<td> x:<input type="text" id="hole-3-x" value="" size="3"/> y: <input type="text" id="hole-3-y" value="" size="3"/>
</td>
</tr>
<tr>
<th>Hole size </th>
<td>
<input type="text" size="3" id="hole-4-size-x" value="0"/>  <input type="text" size="3" id="hole-41-size-y" value="0"/>
</td>
<th>centre</th>
<td> x:<input type="text" id="hole-4-x" value="" size="3"/> y: <input type="text" id="hole-4-y" value="" size="3"/>
</td>
</tr>
</table>
<div>
<h3>Operation</h3>
<div>
<button onClick="initialize_grid()">Initialize Grid</button>
<button onClick="make_braid()">Braid Grid</button>
<!-- <button onClick="doit()">new SVG page</button>
<button onClick="addit()">incremented SVG page</button>
-->
<button onClick="clear_braid()">Clear Braid</button>
</div>
<h3>Progress</h3>
<div id="info"/>
<h3>Grid</h3>
<div style="page-break-before: always;">
<table id="grid" border="1"/>
</div>
</div>
</div>
<div id="tab1" style="display:none">
<table>
<tr>
<th>Grid Spacing</th>
<td>
<input type="text" size="6" id="step" value="10"/>
</td>
</tr>
<tr>
<th>Braid Width</th>
<td>
<input type="text" size="6" id="width" value="6"/>
</td>
</tr>
<tr>
<th>Rope Colours</th>
<td>
<input type="text" size="60" id="colours" value="red,green,blue,yellow,orange,aqua"/>
</td>
</tr>
<tr>
<th>Border</th>
<td> Width <input type="text" size="6" id="line_width" value="0.3"/> Color
<input type="text" size="15" id="line_colour" value="black"/>
</td>
</tr>
<tr>
<th>Scale</th>
<td>
<input type="text" id="scale" size="4" value="1"/>
</td>
</tr>
<tr>
<th/>
<td>
<button onClick="make_svg()">Generate Image</button> <button onClick="page_svg()">Image Page</button>
</td>
</tr>
</table>
<div id="svgframe" style="page-break-before: always;">
<svg xmlns="http://www.w3.org/2000/svg" id="svgimage" width="1600" height="900">
<g id="canvas" transform="translate(50,20)"/>
</svg>
</div>
</div>
<div id="tab3" style="display:none">
<h3>Purpose</h3>
<div>To construct N x M braids. Holes can
be placed in the grid, either using the(rectangular) hole parameters and/or by checking
cells in the matrix. Coordinates of hole centres and size refer to the underlying 2*N+1 by 2*M+1 grid.</div>
<h3>Colours</h3>
<div>Colour names can taken from the list of <a href="http://www.december.com/html/spec/colorsvg.html">SVG colour</a> or use hex codes #hhhhhh. There are also two palettes provided, p-svg for svg colours and p-greens for all greens</div>
<h3>Outputs</h3>
<div>Output as a graph using SVG or to OpenSCAD for generation of STL file for 3D
printing. </div>
<h3>Method</h3>
<div>Braiding starts at an edge and proceeds over and under, turning left or right when
meeting an edge as required. If after completing one rope there are still cells
uncovered, another starting point is selected, until all possible starting points
have been tried. This process is guaranteed to complete for grids without holes but may fail when there are holes.</div>
<div>
<h3>Development</h3>
This script was developed by <a href="http://kitwallace.co.uk">Kit Wallace</a> using Javascript, JQuery and SVG (and StackOverFlow !).
<h3>Issues</h3>
<ul>
<li>No input checks.</li>
<li>Saving the SVG output doesnt work in Chome - use Firefox.</li>
</ul>
</div>
</div>
<div id="tab2" style="display:none">
<table>
<tr>
<th>Depth of smoothing </th>
<td>
<input type="text" size="3" id="depth" value="2"/>
</td>
</tr>
<tr>
<th># sides of rope </th>
<td>
<input type="text" size="6" id="sides" value="10"/>
</td>
</tr>
<tr>
<th>Rope radius </th>
<td>
<input type="text" size="6" id="radius" value="0.3"/>
</td>
</tr>
<tr>
<th>Vertical scale </th>
<td>
<input type="text" size="6" id="vscale" value="0.3"/>
</td>
</tr>
<tr>
<th>Overall scale</th>
<td>
<input type="text" size="6" id="scale" value="10"/>
</td>
</tr>
<tr>
<th>
<button onClick="make_scad()">Generate OpenSCAD</button>
<button onClick="download_scad()">Download OpenSCAD</button>
</th>
</tr>
</table>
<div>
<pre id="scad" style="page-break-after: always;"/>
<hr/>
<pre id="base" style="display:none;"><![CDATA[
// create tubes as polyhedra
// tube can be open or closed
// polyhedron constructor
function poly(name,vertices,faces,debug=[],partial=false) =
[name,vertices,faces,debug,partial];
function p_name(obj) = obj[0];
function p_vertices(obj) = obj[1];
function p_faces(obj) = obj[2];
module show_solid(obj) {
polyhedron(p_vertices(obj),p_faces(obj),convexity=10);
};
// utility functions
function m_translate(v) = [ [1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[v.x, v.y, v.z, 1 ] ];
function m_rotate(v) = [ [1, 0, 0, 0],
[0, cos(v.x), sin(v.x), 0],
[0, -sin(v.x), cos(v.x), 0],
[0, 0, 0, 1] ]
* [ [ cos(v.y), 0, -sin(v.y), 0],
[0, 1, 0, 0],
[ sin(v.y), 0, cos(v.y), 0],
[0, 0, 0, 1] ]
* [ [ cos(v.z), sin(v.z), 0, 0],
[-sin(v.z), cos(v.z), 0, 0],
[ 0, 0, 1, 0],
[ 0, 0, 0, 1] ];
function vec3(v) = [v.x, v.y, v.z];
function transform(v, m) = vec3([v.x, v.y, v.z, 1] * m);
function orient_to(centre,normal, p) = m_rotate([0, atan2(sqrt(pow(normal.x, 2) + pow(normal.y, 2)), normal.z), 0])
* m_rotate([0, 0, atan2(normal[1], normal[0])])
* m_translate(centre);
// solid from path
function circle_points(r, sides,phase=45) =
let (delta = 360/sides)
[for (i=[0:sides-1]) [r * sin(i*delta + phase), r * cos(i*delta+phase), 0]];
function loop_points(step,min=0,max=360) =
[for (t=[min:step:max-step]) f(t)];
function transform_points(list, matrix, i = 0) =
i < len(list)
? concat([ transform(list[i], matrix) ], transform_points(list, matrix, i + 1))
: [];
function tube_points(loop, circle_points, i = 0) =
(i < len(loop) - 1)
? concat(transform_points(circle_points, orient_to(loop[i], loop[i + 1] - loop[i] )),
tube_points(loop, circle_points, i + 1))
: transform_points(circle_points, orient_to(loop[i], loop[0] - loop[i] )) ;
function flatten(l) = [ for (a = l) for (b = a) b ] ;
function loop_faces(segs, sides, open=false) =
flatten( [for (i=[0:segs])
for (j=[0:sides -1])
[
[ i * sides + j,
i * sides + (j + 1) % sides,
((i + 1) % segs) * sides + (j + 1) % sides]
, [ i * sides + j,
((i + 1) % segs) * sides + (j + 1) % sides,
((i + 1) % segs) * sides + j ]
]
]) ;
// smoothed path by interpolate between points
weight = [-1, 9, 9, -1] / 16;
function interpolate(path,n,i) =
path[(i + n - 1) %n] * weight[0] +
path[i] * weight[1] +
path[(i + 1) %n] * weight[2] +
path[(i + 2) %n] * weight[3] ;
function subdivide(path,i=0) =
i < len(path)
? concat([path[i]],
[interpolate(path,len(path),i)],
subdivide(path, i+1))
: [];
function smooth(path,n) =
n == 0
? path
: smooth(subdivide(path),n-1);
function path_segment(path,start,end) =
let (l = len(path))
let (s = max(floor(start * 360 / l),0),
e = min(ceil(end * 360 / l),l - 1))
[for (i=[s:e]) path[i]];
function scale(path,scale,i=0) =
i < len(path)
? concat(
[[path[i][0]*scale[0],path[i][1]*scale[1],path[i][2]*scale[2]]],
scale(path,scale,i+1)
)
: [];
function curve_length(step,t=0) =
t < 360
? norm(f(t+step) - f(t)) + curve_length(step,t+step)
: 0;
function map(t, min, max) =
min + t* (max-min)/360;
// create a knot from a path
function path_knot(path,r,sides,kscale,phase=45,open=false) =
let(loop_points = scale(path,kscale))
let(circle_points = circle_points(r,sides,phase))
let(tube_points = tube_points(loop_points,circle_points))
let(loop_faces = loop_faces(len(loop_points),sides,open))
poly(name="Knot",
vertices = tube_points,
faces = loop_faces);
module braid (Paths,Depth,Kscale,Phase ) {
for (k =[0:len(Paths) - 1]) {
path = Paths[k];
spath=smooth(path,Depth);
knot= path_knot(spath,R,Sides,Kscale,Phase,false);
solid1=knot; // apply transformations here
color(colours[k])
show_solid(solid1);
}
};
colours = ["red","green","blue","yellow","pink","orange","springgreen","cyan","thistle","silver"];
]]></pre>
</div>
</div>
</body>
</html>