-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOperators.h
1947 lines (1878 loc) · 75.2 KB
/
Operators.h
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
//
// Operators.h
// texsyn
//
// Created by Craig Reynolds on 12/31/19.
// Copyright © 2019 Craig Reynolds. All rights reserved.
//
#pragma once
#include "Texture.h"
#include "Disk.h"
#include "COTS.h"
#include "TwoPointTransform.h"
#include "PerlinNoise.h"
#include "RandomSequence.h"
// Minimal texture, a uniform color everywhere on the texture plane. Its single
// parameter is that color. As a convenience for hand written code, also can be
// constructed from three RGB floats, or a single luminance (gray level) float.
class Uniform : public Texture
{
public:
Uniform(Color _color) : color(_color) {};
Uniform(float red, float green, float blue) : color(red, green, blue) {};
Uniform(float _gray_value) : color(Color::gray(_gray_value)) {};
Color getColor(Vec2 position) const override { return color; }
private:
const Color color;
};
// A circular spot, centered at a given point, with two radii. Within an inner
// radius it is colored according to one input texture. Beyond an outer radius
// it is uniformly colored according a second input texture. Between the two
// radii, there is a sinusoid transition from one texture to the other.
// (Reverses radii if they are out of order.)
class Spot : public Texture
{
public:
Spot(Vec2 center_,
float inner_radius_, const Texture& inner_texture_,
float outer_radius_, const Texture& outer_texture_)
: center(center_),
inner_radius(std::max(0.0f, std::min(inner_radius_, outer_radius_))),
inner_texture(inner_texture_),
outer_radius(std::max(0.0f, std::max(inner_radius_, outer_radius_))),
outer_texture(outer_texture_) {}
Color getColor(Vec2 position) const override
{
// Distance from sample position to spot center.
float d = (position - center).length();
// Fraction for interpolation: 0 inside, 1 outside, ramp between.
float f = remapIntervalClip(d, inner_radius, outer_radius, 0, 1);
// Sinusoidal interpolation between inner and outer colors.
return interpolatePointOnTextures(sinusoid(f), position, position,
inner_texture, outer_texture);
}
// BACKWARD_COMPATIBILITY for version before inherent matting.
Spot(Vec2 a, float b, Color c, float d, Color e)
: Spot(a, b, disposableUniform(c), d, disposableUniform(e)){}
private:
const Vec2 center;
const float inner_radius;
const Texture& inner_texture;
const float outer_radius;
const Texture& outer_texture;
};
// Gradation between two textures with arbitrary position, width, and
// orientation. The arguments are two points, defining a line segment, and a
// texture for each end. The gradation occurs along the line segment, a given
// location on the texture is projected onto that line to determine its mix of
// the two input textures.
class Gradation : public Texture
{
public:
Gradation(Vec2 point_0, const Texture& texture_0,
Vec2 point_1, const Texture& texture_1)
: transform(point_0, point_1),
texture0(texture_0),
texture1(texture_1) {}
Color getColor(Vec2 position) const override
{
// Transform so vector from (0, 0) to (1, 0) spans transition region.
Vec2 inside = transform.localize(position);
return interpolatePointOnTextures((transform.scale() == 0 ? 0.5 :
sinusoid(clip01(inside.x()))),
position, position,
texture0, texture1);
}
// BACKWARD_COMPATIBILITY for version before inherent matting.
Gradation(Vec2 a, Color b, Vec2 c, Color d)
: Gradation(a, disposableUniform(b), c, disposableUniform(d)){}
private:
const TwoPointTransform transform;
const Texture& texture0;
const Texture& texture1;
};
// A grating of two alternating stripes each colored according to two given
// input textures. Spacing and orientation is defined by two points. Stripes are
// perpendicular to the segment between these two points. That segment's length
// is the width (wavelength) of the stripe. The softness parameter varies from a
// square wave at 0 and a sinusoid at 1. The duty_cycle parameter controls
// relative width of sub-stripes, it is the ratio of the first color's stripes
// to the stripe pair's total width.
class Grating : public Texture
{
public:
Grating(Vec2 point_0, const Texture& texture_0,
Vec2 point_1, const Texture& texture_1,
float softness_,
float duty_cycle_) :
transform(point_0, point_1),
texture0(texture_0),
texture1(texture_1),
softness(clip(softness_, 0, 1)),
duty_cycle(clip(duty_cycle_, 0, 1)) {}
Color getColor(Vec2 position) const override
{
// Transform so vector from (0, 0) to (1, 0) exactly spans one stripe.
Vec2 inside = transform.localize(position);
// unit_modulo is normalized "cross stripe coordinate" on [0, 1]
float unit_modulo = fmod_floor(inside.x(), 1);
// Blend alpha is unit_modulo remapped by soft_square_wave().
float alpha = soft_square_wave(unit_modulo, softness, duty_cycle);
if (transform.scale() == 0) alpha = 0.5;
return interpolatePointOnTextures(alpha,
position, position,
texture0, texture1);
}
// BACKWARD_COMPATIBILITY with version before duty_cycle, inherent matting.
Grating(Vec2 a, Color b, Vec2 c, Color d, float e)
: Grating(a, disposableUniform(b), c, disposableUniform(d), e, 0.5) {}
Grating(Vec2 a, Color b, Vec2 c, Color d, float e, float f)
: Grating(a, disposableUniform(b), c, disposableUniform(d), e, f) {}
private:
const TwoPointTransform transform;
const Texture& texture0;
const Texture& texture1;
const float softness;
const float duty_cycle;
};
class SoftMatte : public Texture
{
public:
SoftMatte(const Texture& _matte,
const Texture& _texture0,
const Texture& _texture1)
: matte(_matte), texture0(_texture0), texture1(_texture1) {}
Color getColor(Vec2 position) const override
{
return interpolatePointOnTextures(matte.getColor(position).luminance(),
position, position,
texture0, texture1);
}
private:
const Texture& matte;
const Texture& texture0;
const Texture& texture1;
};
// Add two textures.
class Add : public Texture
{
public:
Add(const Texture& _texture0, const Texture& _texture1)
: texture0(_texture0), texture1(_texture1) {}
Color getColor(Vec2 position) const override
{
return texture0.getColor(position) + texture1.getColor(position);
}
private:
const Texture& texture0;
const Texture& texture1;
};
// Subtract two textures. (texture0 - texture1)
class Subtract : public Texture
{
public:
Subtract(const Texture& _texture0, const Texture& _texture1)
: texture0(_texture0), texture1(_texture1) {}
Color getColor(Vec2 position) const override
{
return texture0.getColor(position) - texture1.getColor(position);
}
private:
const Texture& texture0;
const Texture& texture1;
};
// Multiply two textures: for each color sample, multiply component-wise the
// corresponding color samples of the two textures.
class Multiply : public Texture
{
public:
Multiply(const Texture& _texture0, const Texture& _texture1)
: texture0(_texture0), texture1(_texture1) {}
Color getColor(Vec2 position) const override
{
return texture0.getColor(position) * texture1.getColor(position);
}
private:
const Texture& texture0;
const Texture& texture1;
};
// Select between two textures, sample by sample, by taking the one whose
// luminance is greater.
class Max : public Texture
{
public:
Max(const Texture& _texture0, const Texture& _texture1)
: texture0(_texture0), texture1(_texture1) {}
Color getColor(Vec2 position) const override
{
Color c0 = texture0.getColor(position);
Color c1 = texture1.getColor(position);
return (c0.luminance() > c1.luminance()) ? c0 : c1;
}
private:
const Texture& texture0;
const Texture& texture1;
};
// Select between two textures, sample by sample, by taking the one whose
// luminance is less.
class Min : public Texture
{
public:
Min(const Texture& _texture0, const Texture& _texture1)
: texture0(_texture0), texture1(_texture1) {}
Color getColor(Vec2 position) const override
{
Color c0 = texture0.getColor(position);
Color c1 = texture1.getColor(position);
return (c0.luminance() < c1.luminance()) ? c0 : c1;
}
private:
const Texture& texture0;
const Texture& texture1;
};
// Absolute value of the difference of two textures. abs(texture0 - texture1)
// (TODO Names AbsDiff Distance, Norm?)
class AbsDiff : public Texture
{
public:
AbsDiff(const Texture& _texture0, const Texture& _texture1)
: texture0(_texture0), texture1(_texture1) {}
Color getColor(Vec2 position) const override
{
Color diff = texture0.getColor(position) - texture1.getColor(position);
// TODO define overload of std::abs() for Color?
return Color(std::abs(diff.r()),
std::abs(diff.g()),
std::abs(diff.b()));
}
private:
const Texture& texture0;
const Texture& texture1;
};
// Like a binary version of AbsDiff, also to be used in Texture::diff(), texture
// is black everywhere the two input textures have exactly equal RGB values, and
// white where they are not equal.
class NotEqual : public Texture
{
public:
NotEqual(const Texture& _texture0, const Texture& _texture1)
: texture0(_texture0), texture1(_texture1) {}
Color getColor(Vec2 position) const override
{
Color black(0);
Color white(1);
Color diff = texture0.getColor(position) - texture1.getColor(position);
return ((diff == black) ? black : white);
}
private:
const Texture& texture0;
const Texture& texture1;
};
// Ken Perlin's 2002 "Improved Noise": http://mrl.nyu.edu/~perlin/noise/
// This and other noise textures below use PerlinNoise package in Utilities.h
class Noise : public Texture
{
public:
Noise(Vec2 point_0,
Vec2 point_1,
const Texture& texture_0,
const Texture& texture_1)
: transform(point_0, point_1),
texture0(texture_0),
texture1(texture_1) {}
Color getColor(Vec2 position) const override
{
float blend = getScalerNoise(transformIntoNoiseSpace(position));
return interpolatePointOnTextures(transform.scale() == 0 ? 0.5 : blend,
position, position,
texture0, texture1);
}
// Get scalar noise fraction on [0, 1] for the given transformed position.
// Overridden by other noise-based textures to customize basic behavior.
virtual float getScalerNoise(Vec2 transformed_position) const
{
return PerlinNoise::unitNoise2d(transformed_position);
}
// Transform a point from texture space into noise space.
Vec2 transformIntoNoiseSpace(Vec2 position) const
{
return transform.localize(position);
}
// BACKWARD_COMPATIBILITY with version before "two point" specification.
Noise(float a, Vec2 b, const Texture& c, const Texture& d)
: Noise(b, b + Vec2(a, 0), c, d) {};
// BACKWARD_COMPATIBILITY with version before inherent matting.
Noise(float a, Vec2 b, Color c, Color d)
: Noise(a, b, disposableUniform(c), disposableUniform(d)) {}
private:
const TwoPointTransform transform;
const Texture& texture0;
const Texture& texture1;
};
// Brownian Noise -- multi octave fractal 1/f Perlin Noise
class Brownian : public Noise
{
public:
Brownian(Vec2 point_0,
Vec2 point_1,
const Texture& texture_0,
const Texture& texture_1)
: Noise(point_0, point_1, texture_0, texture_1) {};
float getScalerNoise(Vec2 transformed_position) const override
{
return PerlinNoise::brownian2d(transformed_position);
}
// BACKWARD_COMPATIBILITY with version before "two point" specification.
Brownian(float a, Vec2 b, const Texture& c, const Texture& d)
: Brownian(b, b + Vec2(a, 0), c, d) {};
// BACKWARD_COMPATIBILITY with version before inherent matting.
Brownian(float a, Vec2 b, Color c, Color d)
: Brownian(a, b, disposableUniform(c), disposableUniform(d)) {};
};
// Classic Perlin turbulence.
class Turbulence : public Noise
{
public:
Turbulence(Vec2 point_0,
Vec2 point_1,
const Texture& texture_0,
const Texture& texture_1)
: Noise(point_0, point_1, texture_0, texture_1) {};
float getScalerNoise(Vec2 transformed_position) const override
{
return PerlinNoise::turbulence2d(transformed_position);
}
// BACKWARD_COMPATIBILITY with version before "two point" specification.
Turbulence(float a, Vec2 b, const Texture& c, const Texture& d)
: Turbulence(b, b + Vec2(a, 0), c, d) {};
// BACKWARD_COMPATIBILITY with version before inherent matting.
Turbulence(float a, Vec2 b, Color c, Color d)
: Turbulence(a, b, disposableUniform(c), disposableUniform(d)) {};
};
// Furbulence: two "fold" version of Turbulence producing sharp features at
// both low and high ends of the output range.
class Furbulence : public Noise
{
public:
Furbulence(Vec2 point_0,
Vec2 point_1,
const Texture& texture_0,
const Texture& texture_1)
: Noise(point_0, point_1, texture_0, texture_1) {};
float getScalerNoise(Vec2 transformed_position) const override
{
return PerlinNoise::furbulence2d(transformed_position);
}
// BACKWARD_COMPATIBILITY with version before "two point" specification.
Furbulence(float a, Vec2 b, const Texture& c, const Texture& d)
: Furbulence(b, b + Vec2(a, 0), c, d) {};
// BACKWARD_COMPATIBILITY with version before inherent matting.
Furbulence(float a, Vec2 b, Color c, Color d)
: Furbulence(a, b, disposableUniform(c), disposableUniform(d)) {};
};
// Wrapulence: another variation on turbulence(). noise() is scaled up in value,
// then wrapped modulo [0, 1]. It has hard edge discontinuities at all scales.
class Wrapulence : public Noise
{
public:
Wrapulence(Vec2 point_0,
Vec2 point_1,
const Texture& texture_0,
const Texture& texture_1)
: Noise(point_0, point_1, texture_0, texture_1) {};
float getScalerNoise(Vec2 transformed_position) const override
{
return PerlinNoise::wrapulence2d(transformed_position);
}
// BACKWARD_COMPATIBILITY with version before "two point" specification.
Wrapulence(float a, Vec2 b, const Texture& c, const Texture& d)
: Wrapulence(b, b + Vec2(a, 0), c, d) {};
// BACKWARD_COMPATIBILITY with version before inherent matting.
Wrapulence(float a, Vec2 b, Color c, Color d)
: Wrapulence(a, b, disposableUniform(c), disposableUniform(d)) {};
};
// MultiNoise: combines five noise generators (Noise, Brownian, Turbulence,
// Furbulence, Wrapulence) into one, with an extra float argument (on [0, 1])
// that selects between them. (This is to allow all the variations without
// "overwhelming" the space of generators when randomly selecting for GP. An
// alternative would be to specify a "likeliness" weighting in the GP defs for
// making the random selections.)
class MultiNoise : public Noise
{
public:
MultiNoise(Vec2 point_0,
Vec2 point_1,
const Texture& texture_0,
const Texture& texture_1,
float _which)
: Noise(point_0, point_1, texture_0, texture_1),
which(_which) {};
float getScalerNoise(Vec2 transformed_position) const override
{
return PerlinNoise::multiNoise2d(transformed_position, which);
}
// BACKWARD_COMPATIBILITY with version before "two point" specification.
MultiNoise(float a, Vec2 b, const Texture& c, const Texture& d, float e)
: MultiNoise(b, b + Vec2(a, 0), c, d, e) {};
// BACKWARD_COMPATIBILITY with version before inherent matting.
MultiNoise(float a, Vec2 b, Color c, Color d, float e)
: MultiNoise(a, b, disposableUniform(c), disposableUniform(d), e) {};
const float which;
};
// Color Noise -- RGB Perlin Noise
class ColorNoise : public MultiNoise
{
public:
ColorNoise(Vec2 point_0, Vec2 point_1, float _which)
: MultiNoise(point_0, point_1, *this, *this, _which),
offset1(Vec2(1, 0)),
offset2(offset1.rotate(pi * 2 / 3)),
offset3(offset2.rotate(pi * 2 / 3)) {};
Color getColor(Vec2 position) const override
{
Vec2 tp1 = transformIntoNoiseSpace(position + offset1).rotate(0.3);
Vec2 tp2 = transformIntoNoiseSpace(position + offset2).rotate(0.6);
Vec2 tp3 = transformIntoNoiseSpace(position + offset3).rotate(0.9);
return Color(PerlinNoise::multiNoise2d(tp1, which),
PerlinNoise::multiNoise2d(tp2, which),
PerlinNoise::multiNoise2d(tp3, which));
}
// BACKWARD_COMPATIBILITY with version before "two point" specification.
ColorNoise(float a, Vec2 b, float c) : ColorNoise(b, b + Vec2(a, 0), c) {};
private:
const Vec2 offset1;
const Vec2 offset2;
const Vec2 offset3;
};
// Maps the brightness of a sample of the given Texture to a pure hue (full
// brightness, full saturation). The hue transform is offset by a given phase.
class BrightnessToHue : public Texture
{
public:
BrightnessToHue (float _huePhase, const Texture& _texture)
: huePhase (_huePhase), texture (_texture) {}
Color getColor(Vec2 position) const override
{
float luminance = texture.getColor(position).luminance();
return HSV(luminance + huePhase, 1, 1);
}
private:
const float huePhase;
const Texture& texture;
};
// Wrap a given portion of a half-plane radially around a given center point,
// keeping the texture along a given ray unchanged. Related to a rectangular-
// (Cartesian)-to-polar transform.
//
// The parameters are the "width" of a vertical slice of the half-plane which
// will be wrapped, the "center" of rotation, a "fixed_ray" vector indicating
// the orientation of the fixed point ray extending from the center, and the
// source "texture".
//
// Consider the "fixed_ray" to be the +Y axis of a space with "center" at its
// origin. For any point on the X axis from [-width, width] consider the ray
// parallel to +Y. The X coordinate is mapped to an angle of rotation from +Y.
//
class Wrap : public Texture
{
public:
Wrap(float _width, Vec2 _center, Vec2 _fixed_ray, const Texture& _texture)
: width(_width),
center(_center),
fixed_ray(_fixed_ray),
texture(_texture) {}
Color getColor(Vec2 position) const override
{
// Position relative to "center".
Vec2 p = position - center;
// X and Y basis vectors of transformed space.
Vec2 new_y = fixed_ray.normalize();
Vec2 new_x = new_y.rotate90degCW();
// Measure angle (0 parallel to new_y axis, pi/2 parallel to new_x).
float angle = Vec2(p.dot(new_x), p.dot(new_y)).atan2();
// Distance from "center".
float radius = p.length();
Vec2 new_point = (center +
new_x * remapInterval(angle, -pi, pi, -width, width) +
new_y * radius);
return texture.getColor(new_point);
}
private:
const float width;
const Vec2 center;
const Vec2 fixed_ray;
const Texture& texture;
};
// Modifies the given texture within a disk of "radius" around "center", doing a
// "fisheye" expansion of the center of the disk (when center_magnification > 1)
// or a contraction (when center_magnification < 1). Rewritten on July 2, 2020.
class StretchSpot : public Texture
{
public:
StretchSpot(float _center_magnification,
float _spot_radius,
Vec2 _center,
const Texture& _texture) :
center_magnification(std::max(_center_magnification, 0.0f)),
spot_radius(std::max(_spot_radius, 0.0f)),
center(_center),
texture(_texture) {}
Color getColor(Vec2 position) const override
{
Vec2 offset = position - center;
float r = offset.length(); // radius from center
float rr = clip01(r / spot_radius); // "relative radius" on [0, 1]
float taper = interpolate(std::pow(rr, 5), rr, sinusoid(rr));
float scale = interpolate(taper, center_magnification, 1.0f);
return texture.getColor((offset / scale) + center);
}
private:
const float center_magnification;
const float spot_radius;
const Vec2 center;
const Texture& texture;
};
// Stretch (scale in one dimension) the given input Texture. The stretching is
// defined by the first Vec2 argument "stretch". Its magnitude is the scale
// factor, and its direction is the axis scaling. The "center" argument is the
// origin of the scaling. It is unchanged by the transform, as is the line
// through it perpendicular to the "stretch" vector.
class Stretch : public Texture
{
public:
Stretch(Vec2 _stretch, Vec2 _center, const Texture& _texture)
: scale(_stretch.length()),
main_basis(_stretch / scale),
perp_basis(main_basis.rotate90degCW()),
center(_center),
texture(_texture) {}
Color getColor(Vec2 position) const override
{
Vec2 offset = position - center;
float main_distance = offset.dot(main_basis);
float perp_distance = offset.dot(perp_basis);
float stretched = main_distance / scale;
return texture.getColor(center +
main_basis * stretched +
perp_basis * perp_distance);
}
private:
const float scale;
const Vec2 main_basis;
const Vec2 perp_basis;
const Vec2 center;
const Texture& texture;
};
// Creates a Color grating by taking a slice(/transit/1d texture/waveform) from
// a given Texture, then sweeping that pattern in a perpendicular direction. The
// slice is defined by a tangent vector and a center. The tangent basis vector
// give the direction of the slice and its length is a scaling factor along the
// slice. (Nearly identical to a very large first arg to Stretch.)
class SliceGrating : public Texture
{
public:
SliceGrating(Vec2 _slice_tangent, Vec2 _center, const Texture& _texture)
: slice_tangent(_slice_tangent),
center(_center),
texture(_texture) {}
Color getColor(Vec2 position) const override
{
Vec2 offset = position - center;
float projection = offset.dot(slice_tangent);
return texture.getColor(center + (slice_tangent * projection));
}
private:
const Vec2 slice_tangent;
const Vec2 center;
const Texture& texture;
};
// Creates a radial color pattern based on a slice(/transit/1d texture/waveform)
// from another Texture. The slice is defined by a "center" point on it and a
// tangent vector indicating its orientation and scaling. The radial pattern
// consists of rays of colors read from a portion of the slice, addressed by
// angle relative to the tangent direction.
class SliceToRadial : public Texture
{
public:
SliceToRadial(Vec2 _slice_tangent, Vec2 _center, const Texture& _texture)
: slice_tangent(_slice_tangent),
perpendicular(slice_tangent.rotate90degCW()),
center(_center),
texture(_texture) {}
Color getColor(Vec2 position) const override
{
Vec2 offset = position - center;
float angle = std::atan2(offset.dot(perpendicular),
offset.dot(slice_tangent));
return texture.getColor(center + (slice_tangent * angle));
}
private:
const Vec2 slice_tangent;
const Vec2 perpendicular;
const Vec2 center;
const Texture& texture;
};
// Shear one Texture, along a given axis, by an amount based on luminance values
// from a slice of another Texture. Parameters are the two Textures, along with
// an axis for each, defined as a center point and tangent vector.
class SliceShear : public Texture
{
public:
SliceShear(Vec2 _slice_tangent,
Vec2 _slice_center,
const Texture& _texture_for_slice,
Vec2 _shear_tangent,
Vec2 _shear_center,
const Texture& _texture_to_shear)
: slice_tangent(_slice_tangent),
slice_center(_slice_center),
texture_for_slice(_texture_for_slice),
shear_tangent(_shear_tangent),
shear_center(_shear_center),
texture_to_shear(_texture_to_shear),
perpendicular(shear_tangent.rotate90degCCW()) {}
Color getColor(Vec2 position) const override
{
// Find point on texture_to_shear: decompose into x,y in shear space,
// offset x by luminince from slice sample, recombine to new position.
Vec2 shear_offset = position - shear_center;
float local_x = shear_offset.dot(shear_tangent);
float local_y = shear_offset.dot(perpendicular);
// Use "local_y" to select a color along the slice.
Vec2 slice_sample_position = slice_center + slice_tangent * local_y;
// Sample color from "texture_for_slice" then take its luminance.
Color slice_color = texture_for_slice.getColor(slice_sample_position);
float luminance = slice_color.luminance();
// Reconstruct sample location with its "x" coord offset by "luminance".
return texture_to_shear.getColor(shear_center +
shear_tangent * (local_x + luminance) +
perpendicular * local_y);
}
private:
const Vec2 slice_tangent;
const Vec2 slice_center;
const Texture& texture_for_slice;
const Vec2 shear_tangent;
const Vec2 shear_center;
const Texture& texture_to_shear;
const Vec2 perpendicular;
};
// Colorize one texture by mapping its luminance to the sequence of colors
// along a slice(/transit/1d texture/waveform) of another texture.
class Colorize : public Texture
{
public:
Colorize(Vec2 _slice_tangent,
Vec2 _center,
const Texture& _texture_for_slice,
const Texture& _texture_to_color)
: slice_tangent(_slice_tangent),
center(_center),
texture_for_slice(_texture_for_slice),
texture_to_color(_texture_to_color) {}
Color getColor(Vec2 position) const override
{
// Look up color to be tinted/colorized an get its luminance.
Color original = texture_to_color.getColor(position);
float luminance = original.luminance();
// Look up color by luminance along slice in texture_for_slice.
Vec2 on_slice = center + (slice_tangent * luminance);
return texture_for_slice.getColor(on_slice);
}
private:
const Vec2 slice_tangent;
const Vec2 center;
const Texture& texture_for_slice;
const Texture& texture_to_color;
};
// Use the Möbius transformation of the complex plane as a Texture Operator by
// considering it isomorphic to the Cartesian Texture plane. The transformation
// is parameterized by four "points" (aka complex numbers). The Wikipedia
// article (https://en.wikipedia.org/wiki/Möbius_transformation) says the four
// points should satisfy: ad − bc ≠ 0.
// See Tim Hutton cool app: http://timhutton.github.io/mobius-transforms/
class MobiusTransform : public Texture
{
public:
MobiusTransform(Vec2 _a, Vec2 _b, Vec2 _c, Vec2 _d, const Texture& _texture)
: a(Vec2ToComplex(_a)),
b(Vec2ToComplex(_b)),
c(Vec2ToComplex(_c)),
d(Vec2ToComplex(_d)),
texture(_texture) {}
Color getColor(Vec2 position) const override
{
Complex z = Vec2ToComplex(position);
Complex imt = inverse_mobius_transform(z, a, b, c, d);
return texture.getColor(ComplexToVec2(imt));
}
static Vec2 ComplexToVec2(Complex z) { return {z.real(), z.imag()}; }
static Complex Vec2ToComplex(Vec2 v) { return {v.x(), v.y()}; }
private:
const Complex a;
const Complex b;
const Complex c;
const Complex d;
const Texture& texture;
};
// Scale rigid geometric transformation. Mostly for hand-written code.
// TODO should this have a "center" parameter? For GP it would make sense,
// but if this is just for hand-written code it can be handled by ordering.
class Scale : public Texture
{
public:
Scale(float _scale, const Texture& _texture)
: scale(_scale), texture(_texture) {}
Color getColor(Vec2 position) const override
{
return texture.getColor(position / scale);
}
private:
const float scale;
const Texture& texture;
};
// Rotate rigid geometric transformation. Mostly for hand-written code.
// TODO should this have a "center" parameter? For GP it would make sense,
// but if this is just for hand-written code it can be handled by ordering.
class Rotate : public Texture
{
public:
Rotate(float _angle, const Texture& _texture)
: angle(_angle), texture(_texture) {}
Color getColor(Vec2 position) const override
{
return texture.getColor(position.rotate(-angle));
}
private:
const float angle;
const Texture& texture;
};
// Translate rigid geometric transformation. Mostly for hand-written code.
// TODO should this have a "center" parameter? For GP it would make sense,
// but if this is just for hand-written code it can be handled by ordering.
class Translate : public Texture
{
public:
Translate(Vec2 _translation, const Texture& _texture)
: translation(_translation), texture(_texture) {}
Color getColor(Vec2 position) const override
{
return texture.getColor(position - translation);
}
private:
const Vec2 translation;
const Texture& texture;
};
// Uses a "distributed sampling" approach. Takes subsamples on a jiggled NxN
// grid spanning the bounding square of a cosinusoidal kernel with the given
// diameter. N can be statically adjusted with Blur::sqrt_of_subsample_count.
// For each subsample, it looks up a color in the input texture, adjusts it
// by the kernel weight, and averages the result.
class Blur : public Texture
{
public:
Blur(const float _width, const Texture& _texture)
: width(_width), texture(_texture) {}
Color getColor(Vec2 position) const override
{
return single(position, width, texture);
}
static Color single(Vec2 position, float width, const Texture& texture)
{
yield(); // To better support multi-threading.
float radius = width / 2;
std::vector<Vec2> offsets;
RandomSequence rs(position.hash());
int n = tooExpensiveToNest() ? 1 : sqrt_of_subsample_count;
jittered_grid_NxN_in_square(n, width, rs, offsets);
Color sum_of_weighted_colors(0, 0, 0);
float sum_of_weights = 0;
incrementExpensiveToNest();
for (Vec2 offset : offsets)
{
float length = offset.length();
if (length <= radius)
{
float weight = 1 - sinusoid(length / radius);
Color color_at_offset = texture.getColor(position + offset);
sum_of_weighted_colors += color_at_offset * weight;
sum_of_weights += weight;
}
}
decrementExpensiveToNest();
return sum_of_weighted_colors / sum_of_weights;
}
// Each Blur::getColor() uses an NxN jiggled grid of subsamples, where N is:
static inline int sqrt_of_subsample_count = 7;
private:
const float width;
const Texture& texture;
};
// Given two intensity thresholds and an input texture, remap the texture colors
// between those thresholds to "full intensity range". Converts the texture to
// hue-saturation-value color space. Areas darker than the lower threshold get
// value=0, areas brighter than the upper threshold get value=1, areas between
// the two thresholds are mapped to values on [0, 1]. The hue and saturation
// components remain unchanged.
class SoftThreshold : public Texture
{
public:
SoftThreshold(float _intensity0, float _intensity1, const Texture& _texture)
: intensity0(std::min(_intensity0, _intensity1)),
intensity1(std::max(_intensity0, _intensity1)),
texture(_texture) {}
Color getColor(Vec2 position) const override
{
Color color = texture.getColor(position);
HSV hsv(color);
return hsv.newV(remapIntervalClip(hsv.v(), intensity0, intensity1, 0, 1));
}
private:
const float intensity0;
const float intensity1;
const Texture& texture;
};
// Finds (colored) edges based on Blur and "unsharp masking" -- subtracting the
// blurred texture from the original.
//
// TODO this is a "modernized" version of the old August 2009 version. It finds
// the edges, a signal which is symmetric around zero brightness, then biases
// that up by adding middle gray. That last part seems questionable, but
// definitely produces a result more likely to be visable.
class EdgeDetect : public Texture
{
public:
EdgeDetect(const float width_, const Texture& texture_)
: width(width_), texture(texture_) {}
Color getColor(Vec2 position) const override
{
Color original = texture.getColor(position);
Color blurred = Blur::single(position, width, texture);
return (original - blurred) + Color::gray(0.5);
}
private:
const float width;
const Texture& texture;
};
// Enhances the edges (emphasize the high frequencies) of a given texture. Based
// on EdgeDetect, which is based on Blur via the technique of “unsharp masking”.
// Parameters include a filter (kernel) width, and a scale factor controlling
// the strength of the edge enhancement.
class EdgeEnhance : public Texture
{
public:
EdgeEnhance(const float _width,
const float _strength,
const Texture& _texture)
: width(_width), strength(_strength), texture(_texture) {}
Color getColor(Vec2 position) const override
{
Color original = texture.getColor(position);
Color blurred = Blur::single(position, width, texture);
return original + ((original - blurred) * strength);
}
private:
const float width;
const float strength;
const Texture& texture;
};
// In HSV space, rotates the hue by the given "offset". "H" is on [0, 1] so
// H+offset is taken "f-modulo 1.0". Only the fractional part of offset is
// meaningful.
class AdjustHue : public Texture
{
public:
AdjustHue(float _offset, const Texture& _texture)
: offset(_offset),
texture(_texture) {}
Color getColor(Vec2 position) const override
{
Color color = texture.getColor(position);
HSV hsv(color);
return hsv.newH(std::fmod(hsv.h() + offset, 1));
}
private:
const float offset;
const Texture& texture;
};
// Adjust saturation: in HSV space, scale (and clip) saturation.
class AdjustSaturation : public Texture
{
public:
AdjustSaturation (float _factor, const Texture& _texture)
: factor(_factor),
texture(_texture) {}
Color getColor(Vec2 position) const override
{
Color color = texture.getColor(position);
HSV hsv(color);
return hsv.newS(clip(hsv.s() * factor, 0, 1));
}
private:
const float factor;
const Texture& texture;
};
// Adjust brightness: scale all colors by a given factor. RGB components are
// multiplied by the factor. See also Multiply which forms the product of two
// textures. In the previous version of this library there was a Tint operator
// that multiplied a Texture by a Color.
class AdjustBrightness : public Texture
{
public:
AdjustBrightness (float _factor, const Texture& _texture)
: factor(_factor),
texture(_texture) {}
Color getColor(Vec2 position) const override
{
return texture.getColor(position) * factor;
}
private:
const float factor;
const Texture& texture;
};
// Twist an input texture around a given "center". The twist has infinite
// extent but falls off as 1/r. This creates a spiral tightly curved near
// "center" and asymptotically approaching zero curvature for increasing radius.
// The Twist is parameterized by an "angle_scale" (bigger values mean more
// twisting) and a "radius_scale" which adjusts the rate of falloff (bigger
// values pull the twisting closed to "center"). The twist angle is:
// angle = angle_scale / ((radius * radius_scale) + 1)
class Twist : public Texture
{
public:
Twist (float _angle_scale,
float _radius_scale,
const Vec2 _center,
const Texture&
_texture)
: angle_scale(_angle_scale),
radius_scale(_radius_scale),
center(_center),
texture(_texture) {}
Color getColor(Vec2 position) const override
{
Vec2 offset = position - center;
float radius = offset.length();
float angle = angle_scale / ((radius * radius_scale) + 1);
Vec2 rotated_offset = offset.rotate(angle);
return texture.getColor(center + rotated_offset);