Welcome to mirror list, hosted at ThFree Co, Russian Federation.

shaders.py « freestyle « modules « freestyle « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 127db3fcd4b273f9332b43b8a814b9cf5e06a85a (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

#  Filename : shaders.py
#  Authors  : Fredo Durand, Stephane Grabli, Francois Sillion, Emmanuel Turquin
#  Date     : 11/08/2005
#  Purpose  : Stroke shaders to be used for creation of stylized strokes

"""
This module contains stroke shaders used for creation of stylized
strokes.  It is also intended to be a collection of examples for
shader definition in Python.

User-defined stroke shaders inherit the
:class:`freestyle.types.StrokeShader` class.
"""

__all__ = (
    "BackboneStretcherShader",
    "BezierCurveShader",
    "BlenderTextureShader",
    "CalligraphicShader",
    "ColorNoiseShader",
    "ConstantColorShader",
    "ConstantThicknessShader",
    "ConstrainedIncreasingThicknessShader",
    "GuidingLinesShader",
    "IncreasingColorShader",
    "IncreasingThicknessShader",
    "PolygonalizationShader",
    "RoundCapShader",
    "SamplingShader",
    "SmoothingShader",
    "SpatialNoiseShader",
    "SquareCapShader",
    "StrokeTextureStepShader",
    "ThicknessNoiseShader",
    "TipRemoverShader",
    "py2DCurvatureColorShader",
    "pyBackboneStretcherNoCuspShader",
    "pyBackboneStretcherShader",
    "pyBluePrintCirclesShader",
    "pyBluePrintDirectedSquaresShader",
    "pyBluePrintEllipsesShader",
    "pyBluePrintSquaresShader",
    "pyConstantColorShader",
    "pyConstantThicknessShader",
    "pyConstrainedIncreasingThicknessShader",
    "pyDecreasingThicknessShader",
    "pyDepthDiscontinuityThicknessShader",
    "pyDiffusion2Shader",
    "pyFXSVaryingThicknessWithDensityShader",
    "pyGuidingLineShader",
    "pyHLRShader",
    "pyImportance2DThicknessShader",
    "pyImportance3DThicknessShader",
    "pyIncreasingColorShader",
    "pyIncreasingThicknessShader",
    "pyInterpolateColorShader",
    "pyLengthDependingBackboneStretcherShader",
    "pyMaterialColorShader",
    "pyModulateAlphaShader",
    "pyNonLinearVaryingThicknessShader",
    "pyPerlinNoise1DShader",
    "pyPerlinNoise2DShader",
    "pyRandomColorShader",
    "pySLERPThicknessShader",
    "pySamplingShader",
    "pySinusDisplacementShader",
    "pyTVertexRemoverShader",
    "pyTVertexThickenerShader",
    "pyTimeColorShader",
    "pyTipRemoverShader",
    "pyZDependingThicknessShader",
    )


# module members
from _freestyle import (
    BackboneStretcherShader,
    BezierCurveShader,
    BlenderTextureShader,
    CalligraphicShader,
    ColorNoiseShader,
    ConstantColorShader,
    ConstantThicknessShader,
    ConstrainedIncreasingThicknessShader,
    GuidingLinesShader,
    IncreasingColorShader,
    IncreasingThicknessShader,
    PolygonalizationShader,
    SamplingShader,
    SmoothingShader,
    SpatialNoiseShader,
    StrokeTextureStepShader,
    ThicknessNoiseShader,
    TipRemoverShader,
    )

# constructs for shader definition in Python
from freestyle.types import (
    Interface0DIterator,
    Nature,
    Noise,
    StrokeAttribute,
    StrokeShader,
    StrokeVertexIterator,
    StrokeVertex,
    )
from freestyle.functions import (
    Curvature2DAngleF0D,
    DensityF0D,
    GetProjectedZF0D,
    MaterialF0D,
    Normal2DF0D,
    Orientation2DF1D,
    ZDiscontinuityF0D,
    )
from freestyle.predicates import (
    pyVertexNatureUP0D,
    pyUEqualsUP0D,
    )

from freestyle.utils import (
    bound,
    BoundingBox,
    phase_to_direction,
    )

from freestyle.utils import ContextFunctions as CF

import bpy
import random

from math import atan, cos, pi, sin, sinh, sqrt
from mathutils import Vector
from random import randint


# -- Thickness Stroke Shaders -- #


class pyDepthDiscontinuityThicknessShader(StrokeShader):
    """
    Assigns a thickness to the stroke based on the stroke's distance
    to the camera (Z-value).
    """
    def __init__(self, min, max):
        StrokeShader.__init__(self)
        self.a = max - min
        self.b = min
        self.func = ZDiscontinuityF0D()

    def shade(self, stroke):
        it = Interface0DIterator(stroke)
        for svert in it:
            z = self.func(it)
            thickness = self.a * z + self.b
            svert.attribute.thickness = (thickness, thickness)


class pyConstantThicknessShader(StrokeShader):
    """
    Assigns a constant thickness along the stroke.
    """
    def __init__(self, thickness):
        StrokeShader.__init__(self)
        self._thickness = thickness / 2.0

    def shade(self, stroke):
        for svert in stroke:
            svert.attribute.thickness = (self._thickness, self._thickness)


class pyFXSVaryingThicknessWithDensityShader(StrokeShader):
    """
    Assigns thickness to a stroke based on the density of the diffuse map.
    """
    def __init__(self, wsize, threshold_min, threshold_max, thicknessMin, thicknessMax):
        StrokeShader.__init__(self)
        self._func = DensityF0D(wsize)
        self.threshold_min = threshold_min
        self.threshold_max = threshold_max
        self._thicknessMin = thicknessMin
        self._thicknessMax = thicknessMax

    def shade(self, stroke):
        it = Interface0DIterator(stroke)
        delta_threshold = self.threshold_max - self.threshold_min
        delta_thickness = self._thicknessMax - self._thicknessMin

        for svert in it:
            c = self._func(it)
            c = bound(self.threshold_min, c, self.threshold_max)
            t = (self.threshold_max - c) / delta_threshold * delta_thickness + self._thicknessMin
            svert.attribute.thickness = (t / 2.0, t / 2.0)


class pyIncreasingThicknessShader(StrokeShader):
    """
    Increasingly thickens the stroke.
    """
    def __init__(self, thicknessMin, thicknessMax):
        StrokeShader.__init__(self)
        self._thicknessMin = thicknessMin
        self._thicknessMax = thicknessMax

    def shade(self, stroke):
        n = len(stroke)
        for i, svert in enumerate(stroke):
            c = i / n
            if i < (n * 0.5):
                t = (1.0 - c) * self._thicknessMin + c * self._thicknessMax
            else:
                t = (1.0 - c) * self._thicknessMax + c * self._thicknessMin
            svert.attribute.thickness = (t / 2.0, t / 2.0)


class pyConstrainedIncreasingThicknessShader(StrokeShader):
    """
    Increasingly thickens the stroke, constrained by a ratio of the
    stroke's length.
    """
    def __init__(self, thicknessMin, thicknessMax, ratio):
        StrokeShader.__init__(self)
        self._thicknessMin = thicknessMin
        self._thicknessMax = thicknessMax
        self._ratio = ratio

    def shade(self, stroke):
        n = len(stroke)
        maxT = min(self._ratio * stroke.length_2d, self._thicknessMax)

        for i, svert in enumerate(stroke):
            c = i / n
            if i < (n * 0.5):
                t = (1.0 - c) * self._thicknessMin + c * maxT
            else:
                t = (1.0 - c) * maxT + c * self._thicknessMin

            if i == (n - 1):
                svert.attribute.thickness = (self._thicknessMin / 2.0, self._thicknessMin / 2.0)
            else:
                svert.attribute.thickness = (t / 2.0, t / 2.0)


class pyDecreasingThicknessShader(StrokeShader):
    """
    Inverse of pyIncreasingThicknessShader, decreasingly thickens the stroke.
    """
    def __init__(self, thicknessMin, thicknessMax):
        StrokeShader.__init__(self)
        self._thicknessMin = thicknessMin
        self._thicknessMax = thicknessMax

    def shade(self, stroke):
        l = stroke.length_2d
        n = len(stroke)
        tMax = min(self._thicknessMax, 0.33 * l)
        tMin = min(self._thicknessMin, 0.10 * l)

        for i, svert in enumerate(stroke):
            c = i / n
            t = (1.0 - c) * tMax + c * tMin
            svert.attribute.thickness = (t / 2.0, t / 2.0)


class pyNonLinearVaryingThicknessShader(StrokeShader):
    """
    Assigns thickness to a stroke based on an exponential function.
    """
    def __init__(self, thicknessExtremity, thicknessMiddle, exponent):
        self._thicknessMin = thicknessMiddle
        self._thicknessMax = thicknessExtremity
        self._exp = exponent
        StrokeShader.__init__(self)

    def shade(self, stroke):
        n = len(stroke)
        for i, svert in enumerate(stroke):
            c = (i / n) if (i < n / 2.0) else ((n - i) / n)
            c = pow(c, self._exp) * pow(2.0, self._exp)
            t = (1.0 - c) * self._thicknessMax + c * self._thicknessMin
            svert.attribute.thickness = (t / 2.0, t / 2.0)


class pySLERPThicknessShader(StrokeShader):
    """
    Assigns thickness to a stroke based on spherical linear interpolation.
    """
    def __init__(self, thicknessMin, thicknessMax, omega=1.2):
        StrokeShader.__init__(self)
        self._thicknessMin = thicknessMin
        self._thicknessMax = thicknessMax
        self.omega = omega

    def shade(self, stroke):
        n = len(stroke)
        maxT = min(self._thicknessMax, 0.33 * stroke.length_2d)
        omega = self.omega
        sinhyp = sinh(omega)
        for i, svert in enumerate(stroke):
            c = i / n
            if i < (n * 0.5):
                t = sin((1-c) * omega) / sinhyp * self._thicknessMin + sin(c * omega) / sinhyp * maxT
            else:
                t = sin((1-c) * omega) / sinhyp * maxT + sin(c * omega) / sinhyp * self._thicknessMin
            svert.attribute.thickness = (t / 2.0, t / 2.0)


class pyTVertexThickenerShader(StrokeShader):
    """
    Thickens TVertices (visual intersections between two edges).
    """
    def __init__(self, a=1.5, n=3):
        StrokeShader.__init__(self)
        self._a = a
        self._n = n

    def shade(self, stroke):
        n = self._n
        a = self._a

        term = (a - 1.0) / (n - 1.0)

        if (stroke[0].nature & Nature.T_VERTEX):
            for count, svert in zip(range(n), stroke):
                r = term * (n / (count + 1.0) - 1.0) + 1.0
                (tr, tl) = svert.attribute.thickness
                svert.attribute.thickness = (r * tr, r * tl)

        if (stroke[-1].nature & Nature.T_VERTEX):
            for count, svert in zip(range(n), reversed(stroke)):
                r = term * (n / (count + 1.0) - 1.0) + 1.0
                (tr, tl) = svert.attribute.thickness
                svert.attribute.thickness = (r * tr, r * tl)


class pyImportance2DThicknessShader(StrokeShader):
    """
    Assigns thickness based on distance to a given point in 2D space.
    the thickness is inverted, so the vertices closest to the
    specified point have the lowest thickness.
    """
    def __init__(self, x, y, w, kmin, kmax):
        StrokeShader.__init__(self)
        self._origin = Vector((x, y))
        self._w = w
        self._kmin, self._kmax = kmin, kmax

    def shade(self, stroke):
        for svert in stroke:
            d = (svert.point_2d - self._origin).length
            k = (self._kmin if (d > self._w) else
                (self._kmax * (self._w-d) + self._kmin * d) / self._w)

            (tr, tl) = svert.attribute.thickness
            svert.attribute.thickness = (k*tr/2.0, k*tl/2.0)


class pyImportance3DThicknessShader(StrokeShader):
    """
    Assigns thickness based on distance to a given point in 3D space.
    """
    def __init__(self, x, y, z, w, kmin, kmax):
        StrokeShader.__init__(self)
        self._origin = Vector((x, y, z))
        self._w = w
        self._kmin, self._kmax = kmin, kmax

    def shade(self, stroke):
        for svert in stroke:
            d = (svert.point_3d - self._origin).length
            k = (self._kmin if (d > self._w) else
                (self._kmax * (self._w-d) + self._kmin * d) / self._w)

            (tr, tl) = svert.attribute.thickness
            svert.attribute.thickness = (k*tr/2.0, k*tl/2.0)


class pyZDependingThicknessShader(StrokeShader):
    """
    Assigns thickness based on an object's local Z depth (point
    closest to camera is 1, point furthest from camera is zero).
    """
    def __init__(self, min, max):
        StrokeShader.__init__(self)
        self.__min = min
        self.__max = max
        self.func = GetProjectedZF0D()

    def shade(self, stroke):
        it = Interface0DIterator(stroke)
        z_indices = tuple(self.func(it) for _ in it)
        z_min, z_max = min(1, *z_indices), max(0, *z_indices)
        z_diff = 1 / (z_max - z_min)

        for svert, z_index in zip(stroke, z_indices):
            z = (z_index - z_min) * z_diff
            thickness = (1 - z) * self.__max + z * self.__min
            svert.attribute.thickness = (thickness, thickness)


# -- Color & Alpha Stroke Shaders -- #


class pyConstantColorShader(StrokeShader):
    """
    Assigns a constant color to the stroke.
    """
    def __init__(self,r,g,b, a = 1):
        StrokeShader.__init__(self)
        self._color = (r, g, b)
        self._a = a
    def shade(self, stroke):
        for svert in stroke:
            svert.attribute.color = self._color
            svert.attribute.alpha = self._a


class pyIncreasingColorShader(StrokeShader):
    """
    Fades from one color to another along the stroke.
    """
    def __init__(self,r1,g1,b1,a1, r2,g2,b2,a2):
        StrokeShader.__init__(self)
        # use 4d vector to simplify math
        self._c1 = Vector((r1, g1 ,b1, a1))
        self._c2 = Vector((r2, g2, b2, a2))

    def shade(self, stroke):
        n = len(stroke) - 1

        for i, svert in enumerate(stroke):
            c = i / n
            color = (1 - c) * self._c1 + c * self._c2
            svert.attribute.color = color[:3]
            svert.attribute.alpha = color[3]


class pyInterpolateColorShader(StrokeShader):
    """
    Fades from one color to another and back.
    """
    def __init__(self,r1,g1,b1,a1, r2,g2,b2,a2):
        StrokeShader.__init__(self)
        # use 4d vector to simplify math
        self._c1 = Vector((r1, g1 ,b1, a1))
        self._c2 = Vector((r2, g2, b2, a2))

    def shade(self, stroke):
        n = len(stroke) - 1
        for i, svert in enumerate(stroke):
            c = 1.0 - 2.0 * abs((i / n) - 0.5)
            color = (1.0 - c) * self._c1 + c * self._c2
            svert.attribute.color = color[:3]
            svert.attribute.alpha = color[3]


class pyModulateAlphaShader(StrokeShader):
    """
    Limits the stroke's alpha between a min and max value.
    """
    def __init__(self, min=0, max=1):
        StrokeShader.__init__(self)
        self.__min = min
        self.__max = max
    def shade(self, stroke):
        for svert in stroke:
            alpha = svert.attribute.alpha
            alpha = bound(self.__min, alpha * svert.point.y * 0.0025, self.__max)
            svert.attribute.alpha = alpha


class pyMaterialColorShader(StrokeShader):
    """
    Assigns the color of the underlying material to the stroke.
    """
    def __init__(self, threshold=50):
        StrokeShader.__init__(self)
        self._threshold = threshold
        self._func = MaterialF0D()

    def shade(self, stroke):
        xn = 0.312713
        yn = 0.329016
        Yn = 1.0
        un = 4.0 * xn / (-2.0 * xn + 12.0 * yn + 3.0)
        vn = 9.0 * yn / (-2.0 * xn + 12.0 * yn + 3.0)

        it = Interface0DIterator(stroke)
        for svert in it:
            mat = self._func(it)

            r, g, b, *_ = mat.diffuse

            X = 0.412453 * r + 0.35758 * g + 0.180423 * b
            Y = 0.212671 * r + 0.71516 * g + 0.072169 * b
            Z = 0.019334 * r + 0.11919 * g + 0.950227 * b

            if not any((X, Y, Z)):
                X = Y = Z = 0.01

            u = 4.0 * X / (X + 15.0 * Y + 3.0 * Z)
            v = 9.0 * Y / (X + 15.0 * Y + 3.0 * Z)

            L= 116. * pow((Y/Yn),(1./3.)) - 16
            U = 13. * L * (u - un)
            V = 13. * L * (v - vn)

            if L > self._threshold:
                L /= 1.3
                U += 10.
            else:
                L = L + 2.5 * (100-L) * 0.2
                U /= 3.0
                V /= 3.0

            u = U / (13.0 * L) + un
            v = V / (13.0 * L) + vn

            Y = Yn * pow(((L+16.)/116.), 3.)
            X = -9. * Y * u / ((u - 4.)* v - u * v)
            Z = (9. * Y - 15*v*Y - v*X) /( 3. * v)

            r = 3.240479 * X - 1.53715 * Y - 0.498535 * Z
            g = -0.969256 * X + 1.875991 * Y + 0.041556 * Z
            b = 0.055648 * X - 0.204043 * Y + 1.057311 * Z

            r = max(0, r)
            g = max(0, g)
            b = max(0, b)

            svert.attribute.color = (r, g, b)


class pyRandomColorShader(StrokeShader):
    """
    Assigns a color to the stroke based on given seed.
    """
    def __init__(self, s=1):
        StrokeShader.__init__(self)
        random.seed = s

    def shade(self, stroke):
        c = (random.uniform(15, 75) * 0.01,
             random.uniform(15, 75) * 0.01,
             random.uniform(15, 75) * 0.01)
        for svert in stroke:
            svert.attribute.color = c


class py2DCurvatureColorShader(StrokeShader):
    """
    Assigns a color (greyscale) to the stroke based on the curvature.
    A higher curvature will yield a brighter color.
    """
    def shade(self, stroke):
        func = Curvature2DAngleF0D()
        it = Interface0DIterator(stroke)
        for svert in it:
            c = func(it)
            if c < 0 and bpy.app.debug_freestyle:
                print("py2DCurvatureColorShader: negative 2D curvature")
            color = 10.0 * c / pi
            svert.attribute.color = (color, color, color)


class pyTimeColorShader(StrokeShader):
    """
    Assigns a greyscale value that increases for every vertex.
    The brightness will increase along the stroke.
    """
    def __init__(self, step=0.01):
        StrokeShader.__init__(self)
        self._step = step
    def shade(self, stroke):
        for i, svert in enumerate(stroke):
            c = i * self._step
            svert.attribute.color = (c, c, c)


# -- Geometry Stroke Shaders -- #


class pySamplingShader(StrokeShader):
    """
    Resamples the stroke, which gives the stroke the amount of
    vertices specified.
    """
    def __init__(self, sampling):
        StrokeShader.__init__(self)
        self._sampling = sampling

    def shade(self, stroke):
        stroke.resample(float(self._sampling))
        stroke.update_length()


class pyBackboneStretcherShader(StrokeShader):
    """
    Stretches the stroke's backbone by a given length (in pixels).
    """
    def __init__(self, l):
        StrokeShader.__init__(self)
        self._l = l

    def shade(self, stroke):
        # get start and end points
        v0, vn = stroke[0], stroke[-1]
        p0, pn = v0.point, vn.point
        # get the direction
        d1 = (p0 - stroke[ 1].point).normalized()
        dn = (pn - stroke[-2].point).normalized()
        v0.point += d1 * self._l
        vn.point += dn * self._l
        stroke.update_length()


class pyLengthDependingBackboneStretcherShader(StrokeShader):
    """
    Stretches the stroke's backbone proportional to the stroke's length
    NOTE: you'll probably want an l somewhere between (0.5 - 0). A value that
    is too high may yield unexpected results.
    """
    def __init__(self, l):
        StrokeShader.__init__(self)
        self._l = l
    def shade(self, stroke):
        # get start and end points
        v0, vn = stroke[0], stroke[-1]
        p0, pn = v0.point, vn.point
        # get the direction
        d1 = (p0 - stroke[ 1].point).normalized()
        dn = (pn - stroke[-2].point).normalized()
        v0.point += d1 * self._l * stroke.length_2d
        vn.point += dn * self._l * stroke.length_2d
        stroke.update_length()


class pyGuidingLineShader(StrokeShader):
    def shade(self, stroke):
        # get the tangent direction
        t = stroke[-1].point - stroke[0].point
        # look for the stroke middle vertex
        itmiddle = iter(stroke)
        while itmiddle.object.u < 0.5:
            itmiddle.increment()
        center_vertex = itmiddle.object
        # position all the vertices along the tangent for the right part
        it = StrokeVertexIterator(itmiddle)
        for svert in it:
            svert.point = center_vertex.point + t * (svert.u - center_vertex.u)
        # position all the vertices along the tangent for the left part
        it = StrokeVertexIterator(itmiddle).reversed()
        for svert in it:
            svert.point = center_vertex.point - t * (center_vertex.u - svert.u)
        stroke.update_length()


class pyBackboneStretcherNoCuspShader(StrokeShader):
    """
    Stretches the stroke's backbone, excluding cusp vertices (end junctions).
    """
    def __init__(self, l):
        StrokeShader.__init__(self)
        self._l = l

    def shade(self, stroke):

        v0, v1 = stroke[0], stroke[1]
        vn, vn_1 = stroke[-1], stroke[-2]

        if not (v0.nature & v1.nature & Nature.CUSP):
            d1 = (v0.point - v1.point).normalized()
            v0.point += d1 * self._l

        if not (vn.nature & vn_1.nature & Nature.CUSP):
            dn = (vn.point - vn_1.point).normalized()
            vn.point += dn * self._l

        stroke.update_length()


class pyDiffusion2Shader(StrokeShader):
    """
    Iteratively adds an offset to the position of each stroke vertex
    in the direction perpendicular to the stroke direction at the
    point. The offset is scaled by the 2D curvature (i.e. how quickly
    the stroke curve is) at the point.
    """
    def __init__(self, lambda1, nbIter):
        StrokeShader.__init__(self)
        self._lambda = lambda1
        self._nbIter = nbIter
        self._normalInfo = Normal2DF0D()
        self._curvatureInfo = Curvature2DAngleF0D()

    def shade(self, stroke):
        for i in range (1, self._nbIter):
            it = Interface0DIterator(stroke)
            for svert in it:
                svert.point += self._normalInfo(it) * self._lambda * self._curvatureInfo(it)
        stroke.update_length()


class pyTipRemoverShader(StrokeShader):
    """
    Removes the tips of the stroke.
    """
    def __init__(self, l):
        StrokeShader.__init__(self)
        self._l = l

    @staticmethod
    def check_vertex(v, length):
        # Returns True if the given strokevertex is less than self._l away
        # from the stroke's tip and therefore should be removed.
        return (v.curvilinear_abscissa < length or v.stroke_length-v.curvilinear_abscissa < length)

    def shade(self, stroke):
        n = len(stroke)
        if n < 4:
            return

        verticesToRemove = tuple(svert for svert in stroke if self.check_vertex(svert, self._l))
        # explicit conversion to StrokeAttribute is needed
        oldAttributes = (StrokeAttribute(svert.attribute) for svert in stroke)

        if n - len(verticesToRemove) < 2:
            return

        for sv in verticesToRemove:
            stroke.remove_vertex(sv)

        stroke.update_length()
        stroke.resample(n)
        if len(stroke) != n and bpy.app.debug_freestyle:
            print("pyTipRemover: Warning: resampling problem")

        for svert, a in zip(stroke, oldAttributes):
            svert.attribute = a
        stroke.update_length()


class pyTVertexRemoverShader(StrokeShader):
    """
    Removes t-vertices from the stroke.
    """
    def shade(self, stroke):
        if len(stroke) < 4:
            return

        v0, vn = stroke[0], stroke[-1]
        if (v0.nature & Nature.T_VERTEX):
            stroke.remove_vertex(v0)
        if (vn.nature & Nature.T_VERTEX):
            stroke.remove_vertex(vn)
        stroke.update_length()


class pyHLRShader(StrokeShader):
    """
    Controls visibility based upon the quantitative invisibility (QI)
    based on hidden line removal (HLR).
    """
    def shade(self, stroke):
        if len(stroke) < 4:
            return

        it = iter(stroke)
        for v1, v2 in zip(it, it.incremented()):
            if (v1.nature & Nature.VIEW_VERTEX):
                visible = (v1.get_fedge(v2).viewedge.qi != 0)
            v1.attribute.visible = not visible


class pySinusDisplacementShader(StrokeShader):
    """
    Displaces the stroke in the shape of a sine wave.
    """
    def __init__(self, f, a):
        StrokeShader.__init__(self)
        self._f = f
        self._a = a
        self._getNormal = Normal2DF0D()

    def shade(self, stroke):
        it = Interface0DIterator(stroke)
        for svert in it:
            normal = self._getNormal(it)
            a = self._a * (1 - 2 * (abs(svert.u - 0.5)))
            n = normal * a * cos(self._f * svert.u * 6.28)
            svert.point += n
        stroke.update_length()


class pyPerlinNoise1DShader(StrokeShader):
    """
    Displaces the stroke using the curvilinear abscissa.  This means
    that lines with the same length and sampling interval will be
    identically distorded.
    """
    def __init__(self, freq=10, amp=10, oct=4, seed=-1):
        StrokeShader.__init__(self)
        self.__noise = Noise(seed)
        self.__freq = freq
        self.__amp = amp
        self.__oct = oct

    def shade(self, stroke):
        for svert in stroke:
            s = svert.projected_x + svert.projected_y
            nres = self.__noise.turbulence1(s, self.__freq, self.__amp, self.__oct)
            svert.point = (svert.projected_x + nres, svert.projected_y + nres)
        stroke.update_length()


class pyPerlinNoise2DShader(StrokeShader):
    """
    Displaces the stroke using the strokes coordinates.  This means
    that in a scene no strokes will be distorded identically.

    More information on the noise shaders can be found at:
    freestyleintegration.wordpress.com/2011/09/25/development-updates-on-september-25/
    """
    def __init__(self, freq=10, amp=10, oct=4, seed=-1):
        StrokeShader.__init__(self)
        self.__noise = Noise(seed)
        self.__freq = freq
        self.__amp = amp
        self.__oct = oct

    def shade(self, stroke):
        for svert in stroke:
            nres = self.__noise.turbulence2(svert.point_2d, self.__freq, self.__amp, self.__oct)
            svert.point = (svert.projected_x + nres, svert.projected_y + nres)
        stroke.update_length()


class pyBluePrintCirclesShader(StrokeShader):
    """
    Draws the silhouette of the object as a circle.
    """
    def __init__(self, turns=1, random_radius=3, random_center=5):
        StrokeShader.__init__(self)
        self.__turns = turns
        self.__random_center = random_center
        self.__random_radius = random_radius

    def shade(self, stroke):
        # get minimum and maximum coordinates
        p_min, p_max = BoundingBox.from_sequence(svert.point for svert in stroke).corners

        stroke.resample(32 * self.__turns)
        sv_nb = len(stroke) // self.__turns
        center = (p_min + p_max) / 2
        radius = (center.x - p_min.x + center.y - p_min.y) / 2
        R = self.__random_radius
        C = self.__random_center

        # The directions (and phases) are calculated using a separate
        # function decorated with an lru-cache. This guarantees that
        # the directions (involving sin and cos) are calculated as few
        # times as possible.
        #
        # This works because the phases and directions are only
        # dependant on the stroke length, and the chance that
        # stroke.resample() above produces strokes of the same length
        # is quite high.
        #
        # In tests, the amount of calls to sin() and cos() went from
        # over 21000 to just 32 times, yielding a speedup of over 100%
        directions = phase_to_direction(sv_nb)

        it = iter(stroke)

        for j in range(self.__turns):
            prev_radius = radius
            prev_center = center
            radius += randint(-R, R)
            center += Vector((randint(-C, C), randint(-C, C)))

            for (phase, direction), svert in zip(directions, it):
                r = prev_radius + (radius - prev_radius) * phase
                c = prev_center + (center - prev_center) * phase
                svert.point = c + r * direction

        if not it.is_end:
            it.increment()
            for sv in tuple(it):
                stroke.remove_vertex(sv)

        stroke.update_length()


class pyBluePrintEllipsesShader(StrokeShader):
    def __init__(self, turns=1, random_radius=3, random_center=5):
        StrokeShader.__init__(self)
        self.__turns = turns
        self.__random_center = random_center
        self.__random_radius = random_radius

    def shade(self, stroke):
        p_min, p_max = BoundingBox.from_sequence(svert.point for svert in stroke).corners

        stroke.resample(32 * self.__turns)
        sv_nb = len(stroke) // self.__turns

        center = (p_min + p_max) / 2
        radius = center - p_min

        R = self.__random_radius
        C = self.__random_center

        # for description of the line below, see pyBluePrintCirclesShader
        directions = phase_to_direction(sv_nb)
        it = iter(stroke)
        for j in range(self.__turns):
            prev_radius = radius
            prev_center = center
            radius = radius + Vector((randint(-R, R), randint(-R, R)))
            center = center + Vector((randint(-C, C), randint(-C, C)))

            for (phase, direction), svert in zip(directions, it):
                r = prev_radius + (radius - prev_radius) * phase
                c = prev_center + (center - prev_center) * phase
                svert.point = (c.x + r.x * direction.x, c.y + r.y * direction.y)

        # remove excess vertices
        if not it.is_end:
            it.increment()
            for sv in tuple(it):
                stroke.remove_vertex(sv)

        stroke.update_length()


class pyBluePrintSquaresShader(StrokeShader):
    def __init__(self, turns=1, bb_len=10, bb_rand=0):
        StrokeShader.__init__(self)
        self.__turns = turns # does not have any effect atm
        self.__bb_len = bb_len
        self.__bb_rand = bb_rand

    def shade(self, stroke):
        # this condition will lead to errors later, end now
        if len(stroke) < 1:
            return

        # get minimum and maximum coordinates
        p_min, p_max = BoundingBox.from_sequence(svert.point for svert in stroke).corners

        stroke.resample(32 * self.__turns)
        num_segments = len(stroke) // self.__turns
        f = num_segments // 4
        # indices of the vertices that will form corners
        first, second, third, fourth = (f, f * 2, f * 3, num_segments)

        # construct points of the backbone
        bb_len = self.__bb_len
        points = (
            Vector((p_min.x - bb_len, p_min.y)),
            Vector((p_max.x + bb_len, p_min.y)),
            Vector((p_max.x, p_min.y - bb_len)),
            Vector((p_max.x, p_max.y + bb_len)),
            Vector((p_max.x + bb_len, p_max.y)),
            Vector((p_min.x - bb_len, p_max.y)),
            Vector((p_min.x, p_max.y + bb_len)),
            Vector((p_min.x, p_min.y - bb_len)),
            )

        # add randomization to the points (if needed)
        if self.__bb_rand:
            R, r = self.__bb_rand, self.__bb_rand // 2

            randomization_mat = (
                Vector((randint(-R, R), randint(-r, r))),
                Vector((randint(-R, R), randint(-r, r))),
                Vector((randint(-r, r), randint(-R, R))),
                Vector((randint(-r, r), randint(-R, R))),
                Vector((randint(-R, R), randint(-r, r))),
                Vector((randint(-R, R), randint(-r, r))),
                Vector((randint(-r, r), randint(-R, R))),
                Vector((randint(-r, r), randint(-R, R))),
                )

            # combine both tuples
            points = tuple(p + rand for (p, rand) in zip(points, randomization_mat))


        # subtract even from uneven; result is length four tuple of vectors
        it = iter(points)
        old_vecs = tuple(next(it) - current for current in it)

        it = iter(stroke)
        verticesToRemove = list()
        for j in range(self.__turns):
            for i, svert in zip(range(num_segments), it):
                if i < first:
                    svert.point = points[0] + old_vecs[0] * i / (first - 1)
                    svert.attribute.visible = (i != first - 1)
                elif i < second:
                    svert.point = points[2] + old_vecs[1] * (i - first) / (second - first - 1)
                    svert.attribute.visible = (i != second - 1)
                elif i < third:
                    svert.point = points[4] + old_vecs[2] * (i - second) / (third - second - 1)
                    svert.attribute.visible = (i != third - 1)
                elif i < fourth:
                    svert.point = points[6] + old_vecs[3] * (i - third) / (fourth - third - 1)
                    svert.attribute.visible = (i != fourth - 1)
                else:
                    # special case; remove these vertices
                    verticesToRemove.append(svert)

        # remove excess vertices (if any)
        if not it.is_end:
            it.increment()
            verticesToRemove += [svert for svert in it]
            for sv in verticesToRemove:
                stroke.remove_vertex(sv)
        stroke.update_length()


class pyBluePrintDirectedSquaresShader(StrokeShader):
    """
    Replaces the stroke with a directed square.
    """
    def __init__(self, turns=1, bb_len=10, mult=1):
        StrokeShader.__init__(self)
        self.__mult = mult
        self.__turns = turns
        self.__bb_len = 1 + bb_len * 0.01

    def shade(self, stroke):
        stroke.resample(32 * self.__turns)
        n = len(stroke)

        p_mean = (1 / n) * sum((svert.point for svert in stroke), Vector((0.0, 0.0)))
        p_var = Vector((0, 0))
        p_var_xy = 0.0
        for d in (svert.point - p_mean for svert in stroke):
            p_var += Vector((d.x ** 2, d.y ** 2))
            p_var_xy += d.x * d.y

        # divide by number of vertices
        p_var /= n
        p_var_xy /= n
        trace = p_var.x + p_var.y
        det = p_var.x * p_var.y - pow(p_var_xy, 2)

        sqrt_coeff = sqrt(trace * trace - 4 * det)
        lambda1, lambda2 = (trace + sqrt_coeff) / 2, (trace - sqrt_coeff) / 2
        # make sure those numbers aren't to small, if they are, rooting them will yield complex numbers
        lambda1, lambda2 = max(1e-12, lambda1), max(1e-12, lambda2)
        theta = atan(2 * p_var_xy / (p_var.x - p_var.y)) / 2

        if p_var.y > p_var.x:
            e1 = Vector((cos(theta + pi / 2), sin(theta + pi / 2))) * sqrt(lambda1) * self.__mult
            e2 = Vector((cos(theta + pi    ), sin(theta + pi    ))) * sqrt(lambda2) * self.__mult
        else:
            e1 = Vector((cos(theta), sin(theta)))                   * sqrt(lambda1) * self.__mult
            e2 = Vector((cos(theta + pi / 2), sin(theta + pi / 2))) * sqrt(lambda2) * self.__mult

        # partition the stroke
        num_segments = len(stroke) // self.__turns
        f = num_segments // 4
        # indices of the vertices that will form corners
        first, second, third, fourth = (f, f * 2, f * 3, num_segments)

        bb_len1 = self.__bb_len
        bb_len2 = 1 + (bb_len1 - 1) * sqrt(lambda1 / lambda2)
        points = (
            p_mean - e1 - e2 * bb_len2,
            p_mean - e1 * bb_len1 + e2,
            p_mean + e1 + e2 * bb_len2,
            p_mean + e1 * bb_len1 - e2,
            )

        old_vecs = (
            e2 * bb_len2 * 2,
            e1 * bb_len1 * 2,
           -e2 * bb_len2 * 2,
           -e1 * bb_len1 * 2,
            )

        it = iter(stroke)
        verticesToRemove = list()
        for j in range(self.__turns):
            for i, svert in zip(range(num_segments), it):
                if i < first:
                    svert.point = points[0] + old_vecs[0] * i / (first - 1)
                    svert.attribute.visible = (i != first - 1)
                elif i < second:
                    svert.point = points[1] + old_vecs[1] * (i - first) / (second - first - 1)
                    svert.attribute.visible = (i != second - 1)
                elif i < third:
                    svert.point = points[2] + old_vecs[2] * (i - second) / (third - second - 1)
                    svert.attribute.visible = (i != third - 1)
                elif i < fourth:
                    svert.point = points[3] + old_vecs[3] * (i - third) / (fourth - third - 1)
                    svert.attribute.visible = (i != fourth - 1)
                else:
                    # special case; remove these vertices
                    verticesToRemove.append(svert)

        # remove excess vertices
        if not it.is_end:
            it.increment()
            verticesToRemove += [svert for svert in it]
            for sv in verticesToRemove:
                stroke.remove_vertex(sv)
        stroke.update_length()


# -- various (used in the parameter editor) -- #


class RoundCapShader(StrokeShader):
    def round_cap_thickness(self, x):
        x = max(0.0, min(x, 1.0))
        return pow(1.0 - (x ** 2.0), 0.5)

    def shade(self, stroke):
        # save the location and attribute of stroke vertices
        buffer = tuple((Vector(sv.point), StrokeAttribute(sv.attribute)) for sv in stroke)
        nverts = len(buffer)
        if nverts < 2:
            return
        # calculate the number of additional vertices to form caps
        thickness_beg = sum(stroke[0].attribute.thickness)
        caplen_beg = thickness_beg / 2.0
        nverts_beg = max(5, int(thickness_beg))

        thickness_end = sum(stroke[-1].attribute.thickness)
        caplen_end = (thickness_end) / 2.0
        nverts_end = max(5, int(thickness_end))

        # adjust the total number of stroke vertices
        stroke.resample(nverts + nverts_beg + nverts_end)
        # restore the location and attribute of the original vertices
        for i, (p, attr) in enumerate(buffer):
            stroke[nverts_beg + i].point = p
            stroke[nverts_beg + i].attribute = attr
        # reshape the cap at the beginning of the stroke
        q, attr = buffer[1]
        p, attr = buffer[0]
        direction = (p - q).normalized() * caplen_beg
        n = 1.0 / nverts_beg
        R, L = attr.thickness
        for t, svert in zip(range(nverts_beg, 0, -1), stroke):
            r = self.round_cap_thickness((t + 1) * n)
            svert.point = p + direction * t * n
            svert.attribute = attr
            svert.attribute.thickness = (R * r, L * r)
        # reshape the cap at the end of the stroke
        q, attr = buffer[-2]
        p, attr = buffer[-1]
        direction = (p - q).normalized() * caplen_beg
        n = 1.0 / nverts_end
        R, L = attr.thickness
        for t, svert in zip(range(nverts_end, 0, -1), reversed(stroke)):
            r = self.round_cap_thickness((t + 1) * n)
            svert.point = p + direction * t * n
            svert.attribute = attr
            svert.attribute.thickness = (R * r, L * r)
        # update the curvilinear 2D length of each vertex
        stroke.update_length()


class SquareCapShader(StrokeShader):
    def shade(self, stroke):
        # save the location and attribute of stroke vertices
        buffer = tuple((Vector(sv.point), StrokeAttribute(sv.attribute)) for sv in stroke)
        nverts = len(buffer)
        if nverts < 2:
            return
        # calculate the number of additional vertices to form caps
        caplen_beg = sum(stroke[0].attribute.thickness) / 2.0
        nverts_beg = 1

        caplen_end = sum(stroke[-1].attribute.thickness) / 2.0
        nverts_end = 1
        # adjust the total number of stroke vertices
        stroke.resample(nverts + nverts_beg + nverts_end)
        # restore the location and attribute of the original vertices
        for i, (p, attr) in zip(range(nverts), buffer):
            stroke[nverts_beg + i].point = p
            stroke[nverts_beg + i].attribute = attr
        # reshape the cap at the beginning of the stroke
        q, attr = buffer[1]
        p, attr = buffer[0]
        stroke[0].point += (p - q).normalized() * caplen_beg
        stroke[0].attribute = attr
        # reshape the cap at the end of the stroke
        q, attr = buffer[-2]
        p, attr = buffer[-1]
        stroke[-1].point += (p - q).normalized() * caplen_end
        stroke[-1].attribute = attr
        # update the curvilinear 2D length of each vertex
        stroke.update_length()