-
Notifications
You must be signed in to change notification settings - Fork 2
/
piecewise-cubic.html
410 lines (369 loc) · 12.7 KB
/
piecewise-cubic.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
<html>
<head>
<script>
let ln = function(x) { return Math.log(x) }
let log2 = function(x) { return Math.log(x) / Math.log(2); }
let exp2 = function(x) { return Math.exp(x * Math.log(2)); }
let pow = function(x, y) { return Math.pow(x, y); }
let clamp = function(x, a, b) {
return Math.min(Math.max(x, a), b);
}
let max = function(x, y) { return Math.max(x, y); }
let vec2_dist = function(a, b) {
return (a.x - b.x)*(a.x - b.x) +
(a.y - b.y)*(a.y - b.y);
}
let vec2_copy = function(a) {
return {x:a.x, y:a.y};
}
let vec2_madd = function(s, a, b) {
return {x:s*a.x + b.x, y:s*a.y + b.y};
}
let vec2_add = function(a, b) {
return {x:a.x + b.x, y:a.y + b.y};
}
let vec2_sub = function(a, b) {
return {x:a.x - b.x, y:a.y - b.y};
}
let mat2_mul = function(A, p) {
return {
x:A.xx * p.x + A.xy * p.y,
y:A.yx * p.x + A.yy * p.y
}
}
let mat2_inv = function(A) {
let det = 1 / (A.xx * A.yy - A.xy * A.yx);
return {
xx: A.yy * det,
xy:-A.xy * det,
yx:-A.yx * det,
yy: A.xx * det
}
}
function PiecewiseCubic() {
this.control_points = [{x:1, y:1, m:0.0083},
{x:1.25, y:1.044, m:0.2915},
{x:1.5, y:1.1294, m:0.374},
{x:2, y:1.3237, m:0.3916},
{x:3, y:1.6943, m:0.3476},
{x:5, y:2.3219, m:0.2887}];
this.getMaxX = function() {
return this.control_points[this.control_points.length-1].x;
}
this.insert = function(x) {
let p = this.evaluate(x);
let i = 0;
while (this.control_points.length > i &&
this.control_points[i].x < p.x) {
++i;
}
this.control_points.splice(i, 0, p);
}
this.remove = function(index) {
if (index == null) {
return;
}
this.control_points.splice(index, 1);
}
this.evaluate = function(x) {
let result = {x:x};
const n = this.control_points.length;
if (x < this.control_points[0].x) {
result.y = this.control_points[0].y;
result.m = 0;
return result;
}
if (x > this.control_points[n-1].x) {
result.y = this.control_points[n-1].y;
result.m = 0;
return result;
}
for (let i = 0; i < n - 1; ++i) {
if (x <= this.control_points[i+1].x) {
let x0 = this.control_points[i].x; let x1 = this.control_points[i+1].x;
let y0 = this.control_points[i].y; let y1 = this.control_points[i+1].y;
let m0 = this.control_points[i].m; let m1 = this.control_points[i+1].m;
// Normalize to the unit interval
const t = (x - x0) / (x1 - x0);
m0 *= (x1 - x0);
m1 *= (x1 - x0);
// Compute cubic coefficients and evaluate.
const c3 = (2.0*y0 + m0 - 2.0*y1 + m1);
const c2 = (-3.0*y0 + 3.0*y1 - 2.0*m0 - m1);
const c1 = m0;
const c0 = y0;
result.y = c0 + t*(c1 + t*(c2 + t*c3));
result.m = (c1 + 2*c2*t + 3*c3*t*t) / (x1 - x0);
return result;
}
}
}
}
function CurveViewer() {
this.curve = new PiecewiseCubic();
this.canvas = document.createElement('canvas');
this.canvas.width = '640';
this.canvas.height = '480';
this.canvas.style = style="border:1px solid black;";
this.canvas.addEventListener('mousedown', function(e) { viewer.mouseDown(e); });
this.canvas.addEventListener('mouseup', function(e) { viewer.mouseUpOrLeave(); });
this.canvas.addEventListener('mouseleave', function(e) { viewer.mouseUpOrLeave(); });
this.canvas.addEventListener('mousemove', function(e) { viewer.mouseMove(e); });
this.context = this.canvas.getContext("2d");
this.view_offset = {x:10, y:this.canvas.height - 10};
this.view_scale = {x:100, y:-100};
this.inputModes = [];
this.addRadioButton = function(parent, group_name, label) {
let inputElement = document.createElement('input');
inputElement.type = 'radio';
inputElement.name = group_name;
inputElement.value = label;
inputElement.id = group_name + '_' + label;
let labelElement = document.createElement('label');
labelElement.for = inputElement.id;
labelElement.textContent = label
parent.appendChild(inputElement);
parent.appendChild(labelElement);
return inputElement;
}
this.inputModes = document.createElement('p');
this.inputModeMoveXYM = this.addRadioButton(this.inputModes, "input_mode", "Move XY, Shift-M");
this.inputModeMoveYM = this.addRadioButton(this.inputModes, "input_mode", "Move Y, Shift-M");
this.inputModeMoveM = this.addRadioButton(this.inputModes, "input_mode", "Slope");
this.inputModeAdd = this.addRadioButton(this.inputModes, "input_mode", "Add");
this.inputModeDelete = this.addRadioButton(this.inputModes, "input_mode", "Remove");
this.inputModePanZoom = this.addRadioButton(this.inputModes, "input_mode", "Zoom, Shift-Pan");
this.inputModeMoveXYM.checked = true;
this.div = document.createElement('div');
this.div.appendChild(this.inputModes);
this.div.appendChild(this.canvas);
//////////////////////////////////////////////////////////////////////////////
// Conversion from the model (gain this.curve) to the view (tone map this.curve).
this.modelToViewGrad = function(p) {
let f_x_dx = this.view_scale.x;
let f_x_dy = 0;
let f_y_dx = this.view_scale.y * exp2(-p.y);
let f_y_dy = this.view_scale.y * (-ln(2) * p.x) / exp2(p.y);
return {xx:f_x_dx, xy:f_x_dy, yx:f_y_dx, yy:f_y_dy};
}
this.modelToView = function(p) {
let view_x = this.view_offset.x + this.view_scale.x * p.x;
let view_y = this.view_offset.y + this.view_scale.y * p.x * exp2(-p.y);
if (`m` in p) {
let d_xy = mat2_mul(this.modelToViewGrad(p), {x:1, y:p.m})
return {x:view_x, y:view_y, m:d_xy.y / d_xy.x};
}
return {x:view_x, y:view_y}
}
this.viewToModel = function(p_view, p_model_guess) {
// The x coordinate may be computed analytically.
let p_model = {x:(p_view.x - this.view_offset.x) / this.view_scale.x};
// Use Newton iteration to solve for the Y coordinate.
if (`y` in p_view) {
p_model.y = p_model_guess ? p_model_guess.y : 0;
for (let i = 0; i < 25; ++i) {
let p_view_guess = this.modelToView(p_model);
let p_view_error = vec2_sub(p_view, p_view_guess);
let grad = this.modelToViewGrad(p_model);
let grad_inv = mat2_inv(grad);
let step = mat2_mul(grad_inv, p_view_error);
step.x = 0;
p_model = vec2_madd(1.0, step, p_model);
}
}
// Solve the slope directly.
if (`m` in p_view) {
let grad = this.modelToViewGrad(p_model);
let grad_inv = mat2_inv(grad);
let d_xy_view = {x:1, y:p_view.m};
let d_xy_model = mat2_mul(grad_inv, d_xy_view);
p_model.m = d_xy_model.y / d_xy_model.x;
}
return p_model;
}
//////////////////////////////////////////////////////////////////////////////
// Drawing functions.
this.drawGrid = function() {
for (let i = 0; i <= 50; ++i) {
let xy0 = {x:this.view_offset.x, y:this.view_offset.y};
if (i == 0) {
this.context.lineWidth = 2;
this.context.strokeStyle = '#000';
this.context.setLineDash([]);
xy0.x = 0;
xy0.y = this.canvas.height;
} else {
this.context.lineWidth = 1;
this.context.strokeStyle = '#0008';
this.context.setLineDash([1, 3]);
}
if (i == 1 || i == 2 || i == 4 || i == 8 || i == 16 || i == 32) {
this.context.setLineDash([]);
}
this.context.beginPath();
this.context.moveTo(this.view_offset.x + this.view_scale.x * i, xy0.y);
this.context.lineTo(this.view_offset.x + this.view_scale.x * i, 0);
this.context.stroke();
this.context.beginPath();
this.context.moveTo(xy0.x, this.view_offset.y + this.view_scale.y * i);
this.context.lineTo(this.canvas.width, this.view_offset.y + this.view_scale.y * i);
this.context.stroke();
}
this.context.setLineDash([]);
this.context.lineWidth = 1;
this.context.strokeStyle = '#0008';
this.context.beginPath();
this.context.moveTo(this.view_offset.x, this.view_offset.y);
this.context.lineTo(this.view_offset.x + this.view_scale.x * 50, this.view_offset.y + this.view_scale.y * 50);
this.context.stroke();
}
this.drawControlPoint = function(xym, color) {
this.context.beginPath();
this.context.arc(xym.x, xym.y, 8, 0, 2 * Math.PI);
this.context.fillStyle = color;
this.context.fill();
this.context.lineWidth = 0;
this.context.strokeStyle = color;
this.context.stroke();
this.context.beginPath();
this.context.lineWidth = 4;
this.context.moveTo(xym.x - 25, xym.y - 25*xym.m);
this.context.lineTo(xym.x + 25, xym.y + 25*xym.m);
this.context.stroke();
}
this.draw = function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.drawGrid();
this.context.beginPath();
this.context.lineWidth = 2;
this.context.strokeStyle = 'black';
let y = 0;
for (let x = 0; x < 1.1 * this.curve.getMaxX(); x += 0.001) {
y = this.curve.evaluate(x).y;
p = this.modelToView({x:x, y:y});
if (x == 0) {
this.context.moveTo(p.x, p.y);
} else {
this.context.lineTo(p.x, p.y);
}
}
this.context.stroke();
for (let i = 0; i < this.curve.control_points.length; ++i) {
xym = this.modelToView(this.curve.control_points[i])
this.drawControlPoint(xym, 'rgb(' + i + ', 128, 128)');
}
}
//////////////////////////////////////////////////////////////////////////////
// Event handling
this.getViewPoint = function(e) {
const rect = this.canvas.getBoundingClientRect()
return {x:event.clientX - rect.left, y:event.clientY - rect.top};
}
this.getControlPointIndex = function(view_point) {
let best_dist = null;
let best_index = null;
for (let i = 0; i < this.curve.control_points.length; ++i) {
let view_point_i = this.modelToView(this.curve.control_points[i]);
let dist = vec2_dist(view_point_i, view_point);
if (best_dist == null || dist < best_dist) {
best_index = i;
best_dist = dist;
}
}
if (best_dist > 12*12) {
return null;
}
return best_index;
}
this.mouseDown = function(e) {
let view_point = this.getViewPoint(e);
let index = this.getControlPointIndex(view_point);
if (this.inputModeDelete.checked) {
this.curve.remove(index);
}
if (this.inputModeAdd.checked) {
let model_point = this.viewToModel({x:view_point.x});
this.curve.insert(model_point.x);
}
if (this.inputModeMoveXYM.checked ||
this.inputModeMoveYM.checked ||
this.inputModeMoveM.checked) {
this.drag_index = index;
this.drag_view_point = view_point;
return;
}
this.draw();
}
this.mouseMove = function(e) {
let view_delta = null;
{
let view_old = this.drag_view_point;
this.drag_view_point = this.getViewPoint(e);
if (!view_old) {
return;
}
view_delta = vec2_sub(this.drag_view_point, view_old);
}
if (e.buttons != 1) {
return;
}
if (this.inputModePanZoom.checked) {
console.log('pan-zoom');
if (!e.shiftKey) {
this.view_scale.x *= exp2( 0.01 * view_delta.x) *
exp2(-0.01 * view_delta.y);
const rect = this.canvas.getBoundingClientRect()
this.view_scale.y = -this.view_scale.x;
} else {
this.view_offset.x += this.view_delta.x;
this.view_offset.y += this.view_delta.y;
}
this.draw();
}
if (this.inputModeMoveXYM.checked ||
this.inputModeMoveYM.checked ||
this.inputModeMoveM.checked) {
if (this.drag_index == null) {
return;
}
let model_old = this.curve.control_points[this.drag_index];
let view_old = this.modelToView(model_old);
let view_new = view_old;
if (this.inputModeMoveM.checked ||
(this.inputModeMoveYM.checked && e.shiftKey) ||
(this.inputModeMoveXYM.checked && e.shiftKey)) {
view_new = structuredClone(view_old);
view_new.m += 0.1 * view_delta.y;
} else {
view_new = vec2_add(view_old, view_delta);
if (this.inputModeMoveYM.checked) {
view_new.x = view_old.x;
}
view_new.m = view_old.m;
}
if (view_new != view_old) {
let model_new = this.viewToModel(view_new, model_old);
this.curve.control_points[this.drag_index] = model_new;
this.draw();
}
}
}
this.mouseUpOrLeave = function() {
this.drag_index = null;
}
}
let viewer = null;
let main = function() {
viewer = new CurveViewer();
console.log(viewer);
viewer.draw();
document.body.appendChild(viewer.div);
}
</script>
<html>
<body onload="main()">
<div id="CurveDiv">
<p id="Controls"> </p>
</div>
</body>
</html>