-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.lua
1749 lines (1587 loc) · 55.5 KB
/
render.lua
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local facade = require"facade"
local image = require"image"
local chronos = require"chronos"
local filter = require"filter"
local unpack = table.unpack
local floor = math.floor
local ceil = math.ceil
local _M = facade.driver()
setmetatable(_ENV, { __index = _M } )
local background = _M.solid_color(_M.color.white)
local error_lim = 0.01
------------------------------------
-- Print functions --
------------------------------------
local function stderr(...)
io.stderr:write(string.format(...))
end
local function print_mat(matrix) --display matrix
for i=1, #matrix do
temp = ""
for j=1, #matrix[1] do
temp = string.format("%s%f ", temp, matrix[i][j])
end
print(temp)
end
end
local function print_vec(vector) --display vector
temp = ""
for i=1, #vector do
temp = string.format("%s%f ", temp, vector[i])
end
print(temp)
end
local print_shape = {
[shape_type.triangle] = function(shape)
local tdata = shape:get_triangle_data()
print("\tp1", tdata:get_x1(), tdata:get_y1())
print("\tp2", tdata:get_x2(), tdata:get_y2())
print("\tp3", tdata:get_x3(), tdata:get_y3())
end,
[shape_type.circle] = function(shape)
local cdata = shape:get_circle_data()
print("\tc", cdata:get_cx(), cdata:get_cy())
print("\tr", cdata:get_r())
end,
[shape_type.polygon] = function(shape)
local pdata = shape:get_polygon_data()
local coords = pdata:get_coordinates()
for i = 2, #coords, 2 do
local xi, yi = coords[i-1], coords[i]
print("", i//2, xi, yi)
end
end,
[shape_type.rect] = function(shape)
local rdata = shape:get_rect_data()
print("", rdata:get_x(), rdata:get_y())
print("", rdata:get_width(), rdata:get_height())
end,
[shape_type.path] = function(shape)
local pdata = shape:get_path_data()
pdata:iterate(
filter.make_input_path_f_find_monotonic_parameters(
filter.make_input_path_f_find_cubic_parameters({
root_dx_parameter = function(self, t)
print("", "root_dx_parameter", t)
end,
root_dy_parameter = function(self, t)
print("", "root_dy_parameter", t)
end,
inflection_parameter = function(self, t)
print("", "inflection_parameter", t)
end,
double_point_parameter = function(self, t)
print("", "double_point_parameter", t)
end,
begin_contour = function(self, x0, y0)
print("", "begin_contour", x0, y0)
end,
end_open_contour = function(self, x0, y0)
print("", "end_open_contour", x0, y0)
end,
end_closed_contour = function(self, x0, y0)
print("", "end_closed_contour", x0, y0)
end,
linear_segment = function(self, x0, y0, x1, y1)
print("", "linear_segment", x0, y0, x1, y1)
end,
quadratic_segment = function(self, x0, y0, x1, y1, x2, y2)
print("", "quadratic_segment", x0, y0, x1, y1, x2, y2)
end,
cubic_segment = function(self, x0, y0, x1, y1, x2, y2, x3, y3)
print("", "cubic_segment", x0, y0, x1, y1, x2, y2, x3, y3)
end,
rational_quadratic_segment = function(self, x0, y0, x1, y1, w1,
x2, y2)
print("", "rational_quadratic_segment", x0, y0, x1, y1, w1,
x2, y2)
end,
})))
end,
}
local function print_color_ramp(ramp)
print("", ramp:get_spread())
for i, stop in ipairs(ramp:get_color_stops()) do
print("", stop:get_offset(), "->", table.unpack(stop:get_color()))
end
end
local print_paint = {
[paint_type.solid_color] = function(paint)
local color = paint:get_solid_color()
print("", table.unpack(color))
local opacity = paint:get_opacity()
print("", "opacity", opacity)
end,
[paint_type.linear_gradient] = function(paint)
local lg = paint:get_linear_gradient_data()
print("", "p1", lg:get_x1(), lg:get_y1())
print("", "p2", lg:get_x2(), lg:get_y2())
print_color_ramp(lg:get_color_ramp())
local opacity = paint:get_opacity()
print("", "opacity", opacity)
end,
[paint_type.radial_gradient] = function(paint)
local lg = paint:get_radial_gradient_data()
print("", "c", lg:get_cx(), lg:get_cy())
print("", "f", lg:get_fx(), lg:get_fy())
print("", "r", lg:get_r())
print_color_ramp(lg:get_color_ramp())
local opacity = paint:get_opacity()
print("", "opacity", opacity)
end
}
------------------------------------
-- Math functions --
------------------------------------
local function copy_matrix(t)
local temp = {}
for i=1, #t do
temp[i] = {}
for j=1, #t[1] do
temp[i][j] = t[i][j]
end
end
return temp
end
local function transpose(matrix)
local temp = {}
for i = 1, #matrix[1] do
temp[i] = {}
for j = 1, #matrix do
temp[i][j] = matrix[j][i]
end
end
return temp
end
local function matmul(a, b) --matrix multiplication
dim_a = {#a, #a[1]}
dim_b = {#b, #b[1]}
if dim_a[2] ~= dim_b[1] then
print(string.format("a: [%d, %d] \nb: [%d, %d]", dim_a[1], dim_a[2], dim_b[1], dim_b[2]))
error("matrix size incompatible.")
end
result = {}
for i=1, dim_a[1] do
result[i] = {}
for j=1, dim_b[2] do
temp = 0
for k=1, dim_a[2] do
temp = temp + a[i][k] * b[k][j]
end
result[i][j] = temp
end
end
return result
end
local function transform(matrix, x, y) --affine transform
local p = {x, y, 1}
local r = {0, 0}
for i=1, 3 do
r[1] = r[1]+p[i]*matrix[1][i]
r[2] = r[2]+p[i]*matrix[2][i]
end
return r[1], r[2]
end
local function gauss_jordan(matrix, s) --Gaussian elimination
function swap(x, r1, r2) --swap rows r1 and r2 in the matrix x or elements r1 and r2 in the vector x
temp = x[r1]
x[r1] = x[r2]
x[r2] = temp
return x
end
n_row = #matrix
n_col = #matrix[1]
local MAX = min(n_row, n_col)
pivot = 1
for j=1, MAX do
if matrix[pivot][j]==0 then --search for the next pivot
for t=j+1, n_row do
if matrix[t][j]~=0 then
matrix = swap(matrix, t, j)
s = swap(s, t, j)
break
end
end
end
if matrix[pivot][j]~=0 then
if matrix[pivot][j]~=1 then --if the pivot isn't 1, scale the line to turn intro 1
for n=1, #s[1] do
s[pivot][n] = s[pivot][n]/matrix[pivot][j]
end
for n=j+1, n_col do
matrix[pivot][n] = matrix[pivot][n]/matrix[pivot][j]
end
matrix[pivot][j] = 1
end
for i=1, n_row do
if matrix[i][j]~=0 and i~=pivot then
for n=1, #s[1] do
s[i][n] = s[i][n]-matrix[i][j]*s[pivot][n]
end
for n=j+1, n_col do
matrix[i][n] = matrix[i][n]-matrix[i][j]*matrix[pivot][n]
end
matrix[i][j] = 0
end
end
pivot = pivot + 1
end
end
return matrix, s
end
local function get_t_vec(t, n)
local result = {{1}}
for p=2, n+1 do
result[1][p] = result[1][p-1]*t
end
return result
end
local function b_search(index, k) --binary search
if k>=index[#index] then
return #index+1
end
local t_min = 1
local t_max = #index
local t = 0
while t_min<t_max do
t = floor((t_min+t_max)/2)
if index[t]>k then
t_max = t
else
t_min = t+1
end
end
return t_min
end
local change_of_basis = {
[3]={{ 1, 0, 0},
{-2, 2, 0},
{ 1,-2, 1}},
[4]={{ 1, 0, 0, 0},
{-3, 3, 0, 0},
{ 3,-6, 3, 0},
{-1, 3,-3, 1}}
}
local function get_plane(a, b) --cross product
return {a[2]*b[3]-a[3]*b[2],
a[3]*b[1]-a[1]*b[3],
a[1]*b[2]-a[2]*b[1]}
end
local function planes_intersection(p1, p2)
local matrix = copy_matrix({p1, p2, {0, 0, 1}})
local _, p = gauss_jordan(matrix, {{0}, {0}, {1}})
p = transpose(p)[1]
return p
end
local function is_colinear(p1, p2, p3)
local v1 = {}
local v2 = {}
for i=1, 3 do
v1[i] = p2[i]-p1[i]
v2[i] = p3[i]-p1[i]
end
local prod = get_plane(v1, v2) --cross product
if prod[1]==0 and prod[2]==0 and prod[3]==0 then
return true
else
return false
end
end
local derivative_mat = {
[3] = {{0, 1, 0},
{0, 0, 2},
{0, 0, 0}},
[4] = {{0, 1, 0, 0},
{0, 0, 2, 0},
{0, 0, 0, 3},
{0, 0, 0, 0}}
}
local function derivate(C)
return matmul(derivative_mat[#C], C)
end
local function det3(M)
local result = 0
for i=0, 2 do
result = result + M[i+1][1]*M[(i+1)%3+1][2]*M[(i+2)%3+1][3] - M[i+1][3]*M[(i+1)%3+1][2]*M[(i+2)%3+1][1]
end
return result
end
local function project(p)
if not p[3] then
return p
end
return {p[1]/p[3], p[2]/p[3]}
end
local function cubicBspline(nr)
local k, l, m = 1/(6*nr), 1/(2*nr), 1/nr
local function calc(b)
x = {b, b^2, b^3}
return {x[3]*k,
(-x[3]+x[2]+x[1]+1/3)*l,
(x[3]/2-x[2]+2/3)*m,
((-x[3]+1)/3+x[2]-x[1])*l}
end
return calc
end
------------------------------------
-- Classes --
------------------------------------
local TransformStack = {}
TransformStack.__index = TransformStack
setmetatable(TransformStack, {
__call = function (cls, ...)
return cls.create(...)
end,
})
function TransformStack.create()
local self = setmetatable({}, TransformStack)
self.values = {[0]=_M.identity():toxform()}
self.len = 0
return self
end
function TransformStack:push(xf)
xf = self.values[self.len]*xf
self.len = self.len + 1
self.values[self.len] = xf
end
function TransformStack:pop()
if self.len == 0 then
error("stack is already empty")
end
temp = self.values[self.len]
self.values[self.len] = nil
self.len = self.len - 1
return temp
end
function TransformStack:top()
if self.len < 0 then
error("stack is empty")
end
return self.values[self.len]
end
local PixelColor= {}
PixelColor.__index = PixelColor
setmetatable(PixelColor, {
__call = function (cls, ...)
return cls.create(...)
end,
})
function PixelColor.create()
local self = setmetatable({}, PixelColor)
self.rgb = {0, 0, 0}
self.alpha = 0
return self
end
function PixelColor:blend(bottom_color)
local i_alpha = 1-self.alpha
for c=1, 3 do
self.rgb[c] = self.rgb[c]+i_alpha*bottom_color[c]
end
self.alpha = self.alpha + i_alpha*bottom_color[4]
if self.alpha>0.95 then
return true
else
return false
end
end
local gamma = 2.2
local igamma = 1/gamma
function PixelColor:unpack()
return self.rgb[1]^gamma, self.rgb[2]^gamma, self.rgb[3]^gamma, self.alpha^gamma
end
function apply_gamma(c, w)
return (c[1]/w)^igamma, (c[2]/w)^igamma, (c[3]/w)^igamma, (c[4]/w)^igamma
end
local winding_buffer= {}
winding_buffer.__index = winding_buffer
setmetatable(winding_buffer, {
__call = function (cls, ...)
return cls.create(...)
end,
})
function winding_buffer.create()
local self = setmetatable({}, winding_buffer)
self.buff = {}
self.sorted = false
return self
end
function winding_buffer:add(tbuff)
self.sorted = false
for i, values in pairs(tbuff) do
if self.buff[i] == nil then
self.buff[i] = {values}
else
table.insert(self.buff[i], values)
end
end
end
function winding_buffer:get()
if true then --not self.sorted then
self.sorted = true
for i, values in pairs(self.buff) do
table.sort(values, function(a, b) return a[1]<b[1] end)
end
end
return self.buff
end
------------------------------------
-- Paths --
------------------------------------
--## Verify if the monotone is increasing or decreasing
local function get_monotone_type(p1, p2)
local dy = 0
if p1[2]==p2[2] then
return nil
elseif p1[1]<p2[1] then
dy = p2[2]-p1[2]
else
dy = p1[2]-p2[2]
end
if dy>0 then
return 1
else
return -1
end
end
--## Get the min/max values of x and y in a list of points
local function get_box_lim(p)
local x_lim = {}
local y_lim = {}
for i=1, #p do
local temp = project(p[i])
x_lim[i] = temp[1]
y_lim[i] = temp[2]
end
table.sort(x_lim)
table.sort(y_lim)
local x_inf = x_lim[1]
local x_sup = x_lim[#x_lim]
local y_inf = y_lim[1]
local y_sup = y_lim[#y_lim]
return x_inf, x_sup, y_inf, y_sup
end
--## Generate a function to verify if the point is on the left of the line ##--
local function left_line(rp1, rp2, rt)
local p1 = project(rp1)
local p2 = project(rp2)
local a = p2[2]-p1[2]
local mon_type = get_monotone_type(p1, p2)
local x_min, x_max, y_min, y_max = get_box_lim({p1, p2})
local signal = 1
local left = {}
if a~=0 then --if the line isn't horizontal
local b = -(p1[1]-p2[1])/a
local c = p1[1]-p1[2]*b
if a<0 then
signal = -1
end
if rt then
local t = project(rt)
if t[1]<=b*t[2]+c then
return true
else
return false
end
end
left = function (x, y)
if y<y_min or y>=y_max then return 0
elseif x<x_min then return signal
elseif x>=x_max then return 0
elseif x<=b*y+c then return signal
else return 0 end
end
else
left = function(x, y) --if the line is horizontal
return 0
end
end
return left, {x_min, x_max, y_min, y_max, {mon_type, signal}}
end
--## Generate a function that calculate the value at the point ##--
local calc_implicit = {
[2] = function (iC)
return function (x, y)
local temp = matmul({{x, y, 1}}, iC)[1]
return temp[1]^2-(temp[2]*temp[3])
end
end,
[3] = function (iC)
if #iC == 3 then
return function (x, y)
local temp = matmul({{x, y, 1}}, iC)[1]
return temp[1]*(temp[1]^2-(temp[2]*temp[3]))
end
else
local a, b, c, d, e, f, g, h, i, xd, yd = unpack(iC)
return function (x, y)
local tx = x - xd
local ty = y - yd
return ty*(a + ty*(b + ty*c)) + tx*(d + ty*(e + ty*f) + tx*(g + ty*h + tx*i))
end
end
end
}
--## Calculate the gradient of the implicit function ##--
local calc_implicit_gradx = {
[2] = function(p, iC)
local temp = matmul({p}, iC)[1]
return 2*temp[1]*iC[1][1]-(iC[1][2]*temp[3]+iC[1][3]*temp[2])
end,
[3] = function(p, iC)
if #iC == 3 then
local temp = matmul({p}, iC)[1]
return 3*(temp[1]^2)*iC[1][1]-(iC[1][2]*temp[1]+iC[1][1]*temp[2])
else
local a, b, c, d, e, f, g, h, i, xd, yd = unpack(iC)
local tx = p[1] - xd
local ty = p[2] - yd
return (d + ty*(e + ty*f) + 2*tx*(g + ty*h) + 3*(tx^2)*i)
end
end
}
--## Calculate gradient or second gradient of the parametric function ##--
local function get_grad(t, dC)
local d = matmul(get_t_vec(t, #dC-1), dC)[1]
if d[1]==0 and d[2]==0 and d[3]==0 then
d = matmul(get_t_vec(t, #dC-1), derivate(dC))[1]
end
return d
end
--## get homogeneous plane that is the intersection of 2 tangent planes ##--
local function get_tg_intersection(dC, e1, e2)
--## get the gradient in the point ##--
local d1 = get_grad(e1[1], dC)
local d2 = get_grad(e2[1], dC)
local iC1 = get_plane(d1, e1[2])
local iC2 = get_plane(d2, e2[2])
return planes_intersection(iC1, iC2)
end
--## Make the monotone sample function ##--
local function monotone(dC, e1, e2, iC, hp, parsed)
--## Compute the triangle box limits ##--
local p1, p2 = e1[2], e2[2]
local pp1, pp2 = project(p1), project(p2)
local p3 = get_tg_intersection(dC, e1, e2)
local y_lim = p3[2]
local signal = 1
if (pp2[2]-pp1[2])<0 then
signal = -1
end
local L1, L2, L3 = left_line(p1, p2), {}, {}
if signal == 1 then
L2 = left_line(p1, p3)
L3 = left_line(p3, p2)
else
L2 = left_line(p3, p2)
L3 = left_line(p1, p3)
end
--## Compute additional parameters ##--
local gradx = calc_implicit_gradx[#dC-1](hp, iC)
local side = left_line(p1, p2, p3)
local mon_type = get_monotone_type(pp1, pp2)
if parsed.display["s"] then
print("\n## Monotone ##")
print("", "t ->", e1[1], e2[1])
print("", "sig", signal, "side", side)
print("", "p1", unpack(pp1))
print("", "p2", unpack(pp2))
print("", "p3", unpack(project(p3)))
print("", "gradx", gradx)
print("", "type", mon_type)
end
--## Prepare the implicit function ##--
local implicit = calc_implicit[#dC-1](iC)
local left = {}
if gradx>0 then
left = function(x, y)
if implicit(x, y)<0 then return signal
else return 0
end
end
else
left = function(x, y)
if implicit(x, y)>0 then return signal
else return 0
end
end
end
--## Fuse all ##--
local triangle_bound = {}
if side then
triangle_bound = function(x, y)
if L1(x, y)==0 then
return 0
else
if y<y_lim then
tst = L2(x, y)~=0
else
tst = L3(x, y)~=0
end
if tst then return signal
else return left(x, y)
end
end
end
else
triangle_bound = function(x, y)
if L1(x, y)~=0 then
return signal
else
if y<y_lim then
tst = L2(x, y)==0
else
tst = L3(x, y)==0
end
if tst then return 0
else return left(x, y)
end
end
end
end
return triangle_bound, {mon_type, signal}
end
--## Find linear function root ##--
local function lin(b, a, parsed)
if a == 0 then
if parsed.display["s"] then
print("", "a igual a 0")
end
else
local t = -b/a
if parsed.display["s"] then
print("", "t:", t)
end
return t
end
end
--## Find quadratic function roots ##--
local function root(c, b, a, parsed)
if a==0 then
if parsed.display["s"] then
print("", "a==0")
end
return lin(c, b, parsed)
end
local delta = b^2-4*a*c
if parsed.display["s"] then
print("", "delta:", delta)
end
if math.abs(delta)<error_lim then
local t = -b/(2*a)
if parsed.display["s"] then
print("", "t:", t)
end
return {t1}
elseif delta > 0 then
delta = math.sqrt(delta)
local t1 = (-b+delta)/(2*a)
local t2 = (-b-delta)/(2*a)
if parsed.display["s"] then
print("", "t1:", t1, "t2:", t2)
end
return {t1, t2}
else
if parsed.display["s"] then
print("", "raiz complexa")
end
end
end
--## Calculate the implicit function ##--
local function quad_get_implicit(points, C, dC, calc, parsed)
local p1 = points[1]
local p2 = points[3]
--## get the gradient in the point ##--
local d1 = get_grad(0, dC)
local d2 = get_grad(1, dC)
--## get vectors perpendicular to planes (project the lines in the w=1) ##--
local iC = {}
iC[1] = get_plane(p1, p2)
iC[2] = get_plane(d1, p1)
iC[3] = get_plane(d2, p2)
--## makes the iC adjustment ##--
iC = transpose(iC)
local temp = matmul({calc(0.5)}, iC)[1]
local k2 = temp[1]^2
local lm = temp[2]*temp[3]
local a = k2/lm
for i=1, 3 do
iC[i][2] = iC[i][2]*a
end
if parsed.display["s"] then
print("\n## Quadratic ##")
print("", "d1", unpack(d1))
print("", "d2", unpack(d2))
print_mat(iC)
end
return iC
end
--## Calculate the implicit function ##--
local function cubic_get_implicit(points, C, dC, calc, parsed)
--## Verify if it's a quadratic bezier ##--
local tC = transpose(C)
local kd, kc, kb, ka = unpack(tC[1])
local ld, lc, lb, la = unpack(tC[2])
local d = {0}
for i=2, 4 do
local temp = {}
aa = {}
for j=4, 1, -1 do
if j~=i then
temp[#temp+1] = C[j]
end
end
d[i] = det3(temp)*(-1)^(i+1)
end
if parsed.display["s"] then
print("\n## Cubic ##")
print("d2:", d[2], "d3:", d[3], "d4:", d[4])
end
if math.abs(d[2])<error_lim and math.abs(d[3])<error_lim then
if parsed.display["s"] then
print("is a quadratic bezier")
end
return quad_get_implicit(points, C, dC, calc, parsed)
end
--## Display cubic type ##--
if parsed.display["s"] then
local delta = {}
delta[1] = -d[2]^2
delta[2] = d[2]*d[3]
delta[3] = d[2]*d[4]-d[3]^2
local discriminant = 4*delta[1]*delta[3]-delta[2]^2
print("delta1:", delta[1], "delta2:", delta[2], "delta3:", delta[3])
print("must be all 0: ", unpack(matmul({d}, C)[1]))
print("", "discriminante", discriminant)
local t = {}
if discriminant < 0 then
if parsed.display["s"] then
print("", "Loop")
end
t = root(delta[3], delta[2], delta[1], parsed)
else
if parsed.display["s"] then
if discriminant < 0 then
print("", "Cusp")
else
print("", "Serpentine")
end
local a = -6*ka*lb + 6*kb*la
local b = -6*ka*lc + 6*kc*la
local c = -2*kb*lc + 2*kc*lb
t = root(c, b, a, parsed)
print("", "t ->", unpack(t))
end
end
end
--## Calculate the implicit function ##--
local xd = points[1][1]
local yd = points[1][2]
local temp = {}
for i=2, 4 do
temp[#temp+1] = points[i][1]-xd
temp[#temp+1] = points[i][2]-yd
end
local x1, y1, x2, y2, x3, y3 = unpack(temp)
local a = -27*x1*x3^2*y1^2+81*x1*x2*x3*y1*y2-81*x1^2*x3*y2^2-81*x1*x2^2*y1*y3+54*x1^2*x3*y1*y3+81*x1^2*x2*y2*y3-27*x1^3*y3^2
local b = 81*x1*x2^2*y1-54*x1^2*x3*y1-81*x1*x2*x3*y1+54*x1*x3^2*y1-9*x2*x3^2*y1-81*x1^2*x2*y2+162*x1^2*x3*y2-81*x1*x2*x3*y2+27*x2^2*x3*y2-18*x1*x3^2*y2+54*x1^3*y3-81*x1^2*x2*y3+81*x1*x2^2*y3-27*x2^3*y3-54*x1^2*x3*y3+27*x1*x2*x3*y3
local c = -27*x1^3+81*x1^2*x2-81*x1*x2^2+27*x2^3-27*x1^2*x3+54*x1*x2*x3-27*x2^2*x3-9*x1*x3^2+9*x2*x3^2-x3^3
local d = 27*x3^2*y1^3-81*x2*x3*y1^2*y2+81*x1*x3*y1*y2^2+81*x2^2*y1^2*y3-54*x1*x3*y1^2*y3-81*x1*x2*y1*y2*y3+27*x1^2*y1*y3^2
local e = -81*x2^2*y1^2+108*x1*x3*y1^2+81*x2*x3*y1^2-54*x3^2*y1^2-243*x1*x3*y1*y2+81*x2*x3*y1*y2+27*x3^2*y1*y2+81*x1^2*y2^2+81*x1*x3*y2^2-54*x2*x3*y2^2-108*x1^2*y1*y3+243*x1*x2*y1*y3-81*x2^2*y1*y3-9*x2*x3*y1*y3-81*x1^2*y2*y3-81*x1*x2*y2*y3+54*x2^2*y2*y3+9*x1*x3*y2*y3+54*x1^2*y3^2-27*x1*x2*y3^2
local f = 81*x1^2*y1-162*x1*x2*y1+81*x2^2*y1+54*x1*x3*y1-54*x2*x3*y1+9*x3^2*y1-81*x1^2*y2+162*x1*x2*y2-81*x2^2*y2-54*x1*x3*y2+54*x2*x3*y2-9*x3^2*y2+27*x1^2*y3-54*x1*x2*y3+27*x2^2*y3+18*x1*x3*y3-18*x2*x3*y3+3*x3^2*y3
local g = -54*x3*y1^3+81*x2*y1^2*y2+81*x3*y1^2*y2-81*x1*y1*y2^2-81*x3*y1*y2^2+27*x3*y2^3+54*x1*y1^2*y3-162*x2*y1^2*y3+54*x3*y1^2*y3+81*x1*y1*y2*y3+81*x2*y1*y2*y3-27*x3*y1*y2*y3-27*x2*y2^2*y3-54*x1*y1*y3^2+18*x2*y1*y3^2+9*x1*y2*y3^2
local h = -81*x1*y1^2+81*x2*y1^2-27*x3*y1^2+162*x1*y1*y2-162*x2*y1*y2+54*x3*y1*y2-81*x1*y2^2+81*x2*y2^2-27*x3*y2^2-54*x1*y1*y3+54*x2*y1*y3-18*x3*y1*y3+54*x1*y2*y3-54*x2*y2*y3+18*x3*y2*y3-9*x1*y3^2+9*x2*y3^2-3*x3*y3^2
local i = 27*y1^3-81*y1^2*y2+81*y1*y2^2-27*y2^3+27*y1^2*y3-54*y1*y2*y3+27*y2^2*y3+9*y1*y3^2-9*y2*y3^2+y3^3
local iC = {a, b, c, d, e, f, g, h, i, xd, yd}
--## Display things ##--
if parsed.display["s"] then
print_vec(iC)
end
return iC
end
--## Redirect implicit commands ##--
local get_implicit = {
[2] = quad_get_implicit,
[3] = cubic_get_implicit
}
--## Process bezier curves ##--
local function left_bezier(points, n, raw_divs, parsed, segments, lims)
--## generate parametric and the derivative matrix ##--
local C = matmul(change_of_basis[n+1], points)
calc = function(t)
return matmul(get_t_vec(t, n), C)[1]
end
local dC = derivate(C)
--## process the monotone divisions ##--
local divs = {}
for i=1, #raw_divs do
divs[i] = raw_divs[i][1]
end
--## get implicit function ##--
local iC = get_implicit[n](points, C, dC, calc, parsed)
--## organize extremas ##--
table.sort(divs)
local extremas = {{0, points[1]}}
for i = 1, #divs do
local p = calc(divs[i])
extremas[#extremas+1] = {divs[i], p}
end
extremas[#extremas+1] = {1, points[#points]}
table.sort(extremas, function(a, b) return a[1]<b[1] end)
--## generate monotones ##--
local monotones = {}
local hp = {}
local mon_lim = {}
local temp = 0
for i=2, #extremas do
if extremas[i-1][1]~=extremas[i][1] then
hp = calc((extremas[i-1][1]+extremas[i][1])/2)
temp = {get_box_lim({extremas[i-1][2], extremas[i][2]})}
monotones[#monotones+1], temp[#temp+1] = monotone(dC, extremas[i-1],
extremas[i], iC, hp, parsed)
mon_lim[#mon_lim+1] = temp
end
end
--## bounding function ##--
for i=1, #monotones do
segments[#segments+1] = monotones[i]
lims[#lims+1] = mon_lim[i]
end
end
--## Redirect commands ##--
local get_path_segments = {
line = function(segments, command, parsed, lims)
if command[1][2]~=command[2][2] then
segments[#segments+1], lims[#lims+1] = left_line(command[1], command[2])
end
end,
quadratic = function(segments, command, parsed, lims)
left_bezier({command[1], command[2], command[3]},
2, command["div"], parsed, segments, lims)
end,
cubic = function(segments, command, parsed, lims)
left_bezier({command[1], command[2],
command[3], command[4]},
3, command["div"], parsed, segments, lims)
end,
}
--## Make the monotone rasterisation ##--
local function get_segment_buffer(segment, y_lim, x_lim, index, info, nr)
-- initialize variables and precompute
local monotone_type, signal = unpack(info)
local i, j = 0, 0
local j_lim = floor((y_lim[2]-y_lim[1])*nr+0.5)
local x, y = x_lim[1], y_lim[1]
local condition = function(r) return r<=j_lim end
if monotone_type==-1 then
condition = function(r) return r>=0 end
j = j_lim
y = y_lim[1]+j/nr
end
local buffer = {}
-- rasterize
while(condition(j)) do
if segment(x, y)==0 then
buffer[index[2]+j] = {index[1]+i, signal}
j = j+monotone_type
y = y_lim[1]+j/nr
else
i = i+1
x = x_lim[1]+i/nr
end
end
return buffer
end
--## Main path ##--
local function get_path_acc(shape, scene_proj, parsed, nr, calc_index)
-- path processing
local transf = scene_proj*shape:get_xf()
local begin = {}
local segments = {}
local lims = {}