forked from nraynaud/webgcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_raphael.html
429 lines (419 loc) · 17.4 KB
/
test_raphael.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<!doctype html>
<html>
<head>
<title>Raphael Play</title>
<script src="webapp/libs/jquery.min.js"></script>
<script src="webapp/libs/handlebars-1.0.0-rc.4.js"></script>
<script src="webapp/libs/ember-1.0.0-rc.5.js"></script>
<script src="webapp/libs/ember-data-0.13.min.js"></script>
<script src="webapp/libs/Three.js"></script>
<script src="webapp/libs/TrackballControls.js"></script>
<script src="webapp/libs/csg.js"></script>
<script src="webapp/libs/ThreeCSG.js"></script>
<script src="webapp/libs/svg.js"></script>
<script src="webapp/cnc/elliptic.js"></script>
<script src="http://mrdoob.github.io/three.js/examples/js/shaders/CopyShader.js"></script>
<script src="http://f.z.nf/three.js/examples/js/ShaderToon.js"></script>
<script src="http://mrdoob.github.io/three.js/examples/js/shaders/DotScreenShader.js"></script>
<script src="http://mrdoob.github.io/three.js/examples/js/shaders/RGBShiftShader.js"></script>
<script src="http://mrdoob.github.io/three.js/examples/js/postprocessing/EffectComposer.js"></script>
<script src="http://mrdoob.github.io/three.js/examples/js/postprocessing/RenderPass.js"></script>
<script src="http://mrdoob.github.io/three.js/examples/js/postprocessing/MaskPass.js"></script>
<script src="http://mrdoob.github.io/three.js/examples/js/postprocessing/ShaderPass.js"></script>
<script src="http://f.z.nf/three.js/examples/js/ShaderToon.js"></script>
<style>
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
th {
text-align: right;
}
.threeDView {
width: 50%;
height: 400px;
float: right;
border: solid
}
</style>
</head>
<body>
<script type="text/x-handlebars" data-template-name="slot">
<h2>Slot</h2>
<table>
<tr>
<th><label for="length">length: </label></th>
<td>{{view TestApp.NumberField id="length" placeholder="length"
numericValueBinding="length" min="0"}}
</td>
<tr>
<th><label for="width">width: </label></th>
<td>{{view TestApp.NumberField id="width" placeholder="width"
numericValueBinding="width" min="0"}}
</td>
</tr>
<tr>
<th><label for="depth">depth: </label></th>
<td>{{view TestApp.NumberField id="depth" placeholder="depth"
numericValueBinding="depth" min="0"}}
</td>
</tr>
<tr>
<th><label for="volume">volume (mm<sup>3</sup>): </label></th>
<td>{{volume}}
</td>
</tr>
</table>
</script>
<script type="text/x-handlebars" data-template-name="tool">
<h2>Tool</h2>
<table>
<tr>
<th><label for="diameter">diameter: </label></th>
<td>{{view TestApp.NumberField id="diameter" placeholder="diameter"
numericValueBinding="diameter" min="0"}}
</td>
</tr>
<tr>
<th><label for="flutes">flutes: </label></th>
<td>{{view TestApp.NumberField id="flutes" placeholder="flutes"
numericValueBinding="flutes" min="0" step="1"}}
</td>
</tr>
</table>
</script>
<script type="text/x-handlebars" data-template-name="operation">
<h2>Operation</h2>
<table>
<tr>
<th><label for="increment">increment(a<sub>e</sub>): </label></th>
<td>{{view TestApp.NumberField id="increment" placeholder="increment" numericValueBinding="increment"
min="0"}}
</td>
</tr>
<tr>
<th><label for="stepOver">step over(%): </label></th>
<td>{{view TestApp.NumberField id="stepOver"
placeholder="step over"
numericValueBinding="stepOver" min="0" max="100"}}
</td>
</tr>
<tr>
<th><label for="rotationFrequency">rotation frequency(rpm): </label></th>
<td>{{view TestApp.NumberField id="rotationFrequency"
placeholder="step over"
numericValueBinding="rotationFrequency" min="0"}}
</td>
</tr>
<tr>
<th><label for="feedRate">feed rate(mm/min): </label></th>
<td>{{view TestApp.NumberField id="feedRate"
numericValueBinding="feedRate" min="0"}}
</td>
</tr>
<tr>
<th>chip load(mm):</th>
<td>{{chipLoad}}</td>
</tr>
<tr>
<th>h<sub>max</sub>:</th>
<td>{{hMax}}</td>
</tr>
<tr>
<th>surface speed(m/min):</th>
<td>{{surfaceSpeed}}</td>
</tr>
<tr>
<th>path length(mm):</th>
<td>{{actualPathLength}}</td>
</tr>
<tr>
<th>duration(hh:min:ss):</th>
<td>{{durationText}}</td>
</tr>
<tr>
<th>material removal rate (cm<sup>3</sup>/min):</th>
<td>{{materialRemovalRate}}</td>
</tr>
</table>
</script>
<script type="text/x-handlebars" data-template-name="testApp">
<h1>Trochoidal slot milling</h1>
{{render 'tool' tool}}
{{render 'slot' slot}}
{{render 'operation' operation}}
<div>
<div style="border:solid; width:50%; float:left;">
{{view TestApp.RaphaelView}}
</div>
{{view TestApp.ThreeJSView classNames="threeDView"}}
</div>
</script>
<script>
function secondsToHms(d) {
d = Number(d);
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
var s = Math.floor(d % 3600 % 60);
return ((h > 0 ? h + ":" : "") + (m > 0 ? (h > 0 && m < 10 ? "0" : "") + m + ":" : "0:") + (s < 10 ? "0" : "") + s);
}
window.TestApp = Ember.Application.create();
TestApp.Router.map(function () {
this.resource('testApp', { path: '/' });
});
TestApp.Store = DS.Store.extend({
revision: 13,
adapter: 'DS.FixtureAdapter'
});
TestApp.Document = DS.Model.extend({
tool: DS.belongsTo('TestApp.Tool'),
slot: DS.belongsTo('TestApp.Slot'),
operation: DS.belongsTo('TestApp.Operation')
});
TestApp.Tool = DS.Model.extend({
document: DS.belongsTo('TestApp.Document'),
diameter: DS.attr('number'),
flutes: DS.attr('number')
});
TestApp.Slot = DS.Model.extend({
document: DS.belongsTo('TestApp.Document'),
depth: DS.attr('number'),
length: DS.attr('number'),
width: DS.attr('number'),
volume: function () {
return this.get('depth') * this.get('length') * this.get('width');
}.property('depth', 'length', 'width')
});
TestApp.Operation = DS.Model.extend({
document: DS.belongsTo('TestApp.Document'),
increment: DS.attr('number'),
rotationFrequency: DS.attr('number'),
feedRate: DS.attr('number'),
stepOver: function (key, v) {
var diameter = this.get('document.tool.diameter');
var increment = this.get('increment');
if (arguments.length === 1)
return increment / diameter * 100;
else
this.set('increment', diameter * v / 100);
}.property('increment', 'document.tool.diameter'),
chipLoad: function () {
return this.get('feedRate') / ( this.get('rotationFrequency') * this.get('document.tool.flutes'));
}.property('feedRate', 'rotationFrequency', 'document.tool.flutes'),
hMax: function () {
var diameter = this.get('document.tool.diameter');
var increment = this.get('increment');
var fz = this.get('feedRate') / (this.get('document.tool.flutes') * this.get('rotationFrequency'));
return 2 * fz * Math.sqrt(increment / diameter * (1 - increment / diameter));
}.property('increment', 'feedRate', 'rotationFrequency', 'document.tool.diameter', 'document.tool.flutes'),
surfaceSpeed: function () {
return Math.PI * this.get('document.tool.diameter') * this.get('rotationFrequency') / 1000;
}.property('rotationFrequency', 'document.tool.diameter'),
trochoidalRadius: function () {
var w = this.get('document.slot.width');
var toolD = this.get('document.tool.diameter');
return (w - toolD) / 2;
}.property('document.slot.width', 'document.tool.diameter'),
pathStartY: function () {
return -this.get('document.slot.length') / 2 - this.get('trochoidalRadius');
}.property('trochoidalRadius', 'document.slot.length'),
pathGeneratorLength: function () {
var l = this.get('document.slot.length') + this.get('trochoidalRadius');
var increment = this.get('increment');
return (Math.ceil(l / increment) + 0.5) * increment;
}.property('document.slot.length', 'trochoidalRadius', 'increment'),
actualPathLength: function () {
var trochoR = this.get('trochoidalRadius');
var increment = this.get('increment');
var a = increment / (2 * Math.PI);
var m = -4 * a * trochoR / Math.pow(a - trochoR, 2);
var t = this.get('pathGeneratorLength') / increment * 2 * Math.PI - Math.PI / 2;
return 2 * Math.abs(a - trochoR) * incompleteEllipticIntegralSecondKind(t / 2, m);
}.property('trochoidalRadius', 'pathGeneratorLength', 'increment'),
duration: function () {
return this.get('actualPathLength') / this.get('feedRate');
}.property('actualPathLength', 'feedRate'),
materialRemovalRate: function () {
return this.get('document.slot.volume') / this.get('duration') / 1000;
}.property('document.slot.volume', 'duration')
});
TestApp.TestAppRoute = Ember.Route.extend({
model: function () {
return TestApp.Document.find(1);
}
});
TestApp.OperationController = Ember.ObjectController.extend({
durationText: function () {
return secondsToHms(this.get('model.duration') * 60);
}.property('model.duration')
});
TestApp.NumberField = Ember.TextField.extend({
type: 'number',
attributeBindings: ['min', 'max', 'step'],
numericValue: function (key, v) {
if (arguments.length === 1) {
var val = parseFloat(this.get('value'));
return isNaN(val) ? null : val;
} else
this.set('value', v + '');
}.property('value')
});
TestApp.ThreeJSView = Ember.View.extend({
didInsertElement: function () {
this._super();
var element = $(this.get('element'));
element.attr('style', "");
var renderer = new THREE.WebGLRenderer({antialias: true});
element.append(renderer.domElement);
var camera = new THREE.OrthographicCamera(element.width() / -2, element.width() / 2, element.height() / 2, element.height() / -2, 0.01, 20000);
camera.up = new THREE.Vector3(0, 0, 1);
var scene = new THREE.Scene();
var light = new THREE.PointLight(0xffffff, 1, 3000000);
light.position.set(0, 200000, 200000);
scene.add(light);
scene.add(new THREE.AmbientLight(0x404040));
camera.position.z = 300;
camera.position.y = 300;
camera.position.x = 300;
scene.add(camera);
var controls = new THREE.TrackballControls(camera, element[0]);
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.minDistance = 0.01;
controls.keys = [ 65, 83, 68 ];
controls.addEventListener('change', function () {
renderer.render(scene, camera);
});
function animate() {
requestAnimationFrame(animate);
controls.update();
}
var planeGeometry = new THREE.PlaneGeometry(100, 100, 5, 5);
scene.add(new THREE.Mesh(planeGeometry, new THREE.MeshBasicMaterial({wireframe: true, color: 0x00CC00})));
var partMesh = null;
this.updateMesh = function (newMesh) {
if (partMesh)
scene.remove(partMesh);
partMesh = newMesh;
if (newMesh) {
scene.add(newMesh);
}
renderer.render(scene, camera);
};
renderer.render(scene, camera);
function resizeEveryBody() {
renderer.setSize(element.width(), element.height());
camera.aspect = element.width() / element.height();
camera.updateProjectionMatrix();
renderer.render(scene, camera);
}
$(window).resize(resizeEveryBody);
resizeEveryBody();
animate();
},
partChanged: function () {
var width = this.get('controller.slot.width');
var length = this.get('controller.slot.length');
var depth = this.get('controller.slot.depth');
var stockThickness = Math.max(30, depth + 5);
var stockGeometry = new THREE.CubeGeometry(Math.max(300, width + 60), length, stockThickness, 1, 1, 1);
stockGeometry.applyMatrix(new THREE.Matrix4().makeTranslation(0, 0, -stockThickness / 2));
stockGeometry.computeFaceNormals();
if (width == null || length == null || depth == null)
return;
var slotGeometry = new THREE.CubeGeometry(width, length, depth, 1, 1, 1);
slotGeometry.applyMatrix(new THREE.Matrix4().makeTranslation(0, 0, -depth / 2));
slotGeometry.computeFaceNormals();
var geom = THREE.CSG.fromCSG(THREE.CSG.toCSG(stockGeometry).subtract(THREE.CSG.toCSG(slotGeometry)));
var partMesh = new THREE.Mesh(geom, new THREE.MeshLambertMaterial({color: 0xCC0000}));
var partMeshWireFrame = new THREE.Mesh(geom, new THREE.MeshBasicMaterial({wireframe: true, depthTest: true, color: 0xF0F0F0, transparent: true, opacity: 0.2}));
var obj = new THREE.Object3D();
obj.add(partMesh);
obj.add(partMeshWireFrame);
this.updateMesh(obj);
}.observes('controller.slot.length', 'controller.slot.width', 'controller.slot.depth')
});
TestApp.RaphaelView = Ember.View.extend({
didInsertElement: function () {
this._super();
var paper = SVG(this.get('element'));
this.set('slotDrawing', paper.rect(1, 1).attr({'vector-effect': 'non-scaling-stroke', fill: '#99D9FC', 'stroke-width': 2}));
this.set('toolDrawing', paper.circle(0).attr({'vector-effect': 'non-scaling-stroke', fill: '#8DAABC'}));
this.set('operationAxis', paper.path('M320,240', true).attr({'vector-effect': 'non-scaling-stroke', 'stroke-dasharray': '--.', "stroke-width": 1}));
this.set('operationPath', paper.path('M320,240', true).attr({'vector-effect': 'non-scaling-stroke', fill: 'none', stroke: 'grey', 'stroke-width': 1}));
var paperX = 0;
var paperY = 0;
$(this.get('element')).bind('mousewheel', function (event) {
});
},
changeToolDiameter: function () {
var diameter = this.get('controller.tool.diameter');
if (diameter != null)
this.get('toolDrawing').attr({
r: diameter / 2
});
}.observes('controller.tool.diameter'),
slotLengthWidthChanged: function () {
var w = this.get('controller.slot.width');
var l = this.get('controller.slot.length');
if (w != null && l != null)
this.get('slotDrawing').attr({x: 320 - w / 2, y: 240 - l / 2, width: w, height: l});
}.observes('controller.slot.length', 'controller.slot.width'),
operationIncrementChanged: function () {
var l = this.get('controller.slot.length');
var inc = this.get('controller.operation.increment');
var operationPath = this.get('operationPath');
if (l == null || inc == null || inc == 0) {
operationPath.attr({'path': ''});
return;
}
var trochoR = this.get('controller.operation.trochoidalRadius');
var startX = 320;
var startY = 240 + this.get('controller.operation.pathStartY');
this.get('operationAxis').attr({d: 'M 320 ' + (startY - 10) + 'L 320 ' + (240 + l / 2 + 10) });
var pathLength = this.get('controller.operation.pathGeneratorLength');
var tInc = pathLength / inc;
var segments = tInc * 20;
segments = Math.min(segments, 10000);
var path = '';
var dirX = 0;
var dirY = 1;
var a = inc / (2 * Math.PI);
for (var i = 0; i < segments; i++) {
var ratio = (i / segments);
var t = ratio * tInc * 2 * Math.PI - Math.PI / 2;
var x = startX + dirX * t + trochoR * Math.cos(t);
var y = startY + dirY * a * t + trochoR * Math.sin(t);
if (i == 0)
path = 'M ' + x + ' ' + y + ' L ' + x + ' ' + y;
else
path += ' ' + x + ' ' + y;
}
operationPath.attr({'d': path});
console.log(operationPath.node.getTotalLength());
console.log('trocho', this.get('controller.operation.actualPathLength'));
var start = operationPath.node.getPointAtLength(0);
this.get('toolDrawing').attr({cx: start.x, cy: start.y});
}.observes('controller.operation.increment', 'controller.slot.length', 'controller.slot.width', 'controller.tool.diameter')
});
TestApp.Document.FIXTURES = [
{ id: 1, tool: 1, slot: 1, operation: 1}
];
TestApp.Tool.FIXTURES = [
{ id: 1, document: 1, diameter: 30, flutes: 2}
];
TestApp.Slot.FIXTURES = [
{ id: 1, document: 1, depth: 10, length: 300, width: 100}
];
TestApp.Operation.FIXTURES = [
{ id: 1, document: 1, increment: 10, rotationFrequency: 1000, feedRate: 150}
];
</script>
</body>
</html>