-
Notifications
You must be signed in to change notification settings - Fork 9
/
fields.clj
3366 lines (2906 loc) · 110 KB
/
fields.clj
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
(ns fastmath.fields
"Vector field functions.
Vector fields are functions R^2->R^2.
Names are taken from fractal flames world where such fields are call `variations`. Most implementations are taken from [JWildfire](http://jwildfire.org/) software.
### Creation
To create vector field call [[field]] multimethod with name of the field as keyword.
Some of the vector fields require additional configuration as a map of parameters as keywords and values. Call [[parametrization]] to create random one or to merge with provided.
Additionally you can provide `amount` parameter which is scaling factor for vector field (default: `1.0`).
### Derived fields
You can use several method to derive new vector field from the other one(s). Possible options are:
* [[derivative]], [[grad-x]], [[grad-y]] - directional derivative of the field
* [[sum]] - sum of two fields
* [[multiply]] - multiplication of the fields
* [[composition]] - composition of the fields
* [[angles]] - angles of the field vectors
### Scalar fields
You can derive scalar fields from given vector field(s):
* [[jacobian]] - determinant of jacobian matrix
* [[divergence]] - divergence of the field
* [[cross]] - cross product of the fields (as a determinant of the 2x2 matrix of vectors)
* [[dot]] - dot product
* [[angle-between]] - angle between vectors from fields.
### Combinations
The other option is to create vector field using some of the above possibilities. Combination is a tree of field operations with parametrizations. Functions:
* [[combine]] - create vector field randomly of from given parametrization.
* [[random-configuration]] - returns random configuration as a map
* [[randomize-configuration]] - change parametrization for given configuration."
{:metadoc/categories {:cr "Create fields"
:sc "Derive scalar field from vector field"
:vf "Derive vector field from other vector field(s)."}}
(:require [fastmath.core :as m]
[fastmath.complex :as c]
[fastmath.random :refer :all]
[fastmath.vector :as v])
(:import [fastmath.vector Vec2]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(m/use-primitive-operators)
(defmulti parametrization
"Return random parametrization map for given field.
Optinally you can pass part of the parametrization. In this case function will add remaining keys with randomly generated values.
If field doesn't have parametrization, empty map will be returned.
See [[field]]."
{:metadoc/categories #{:cr}}
(fn [key & _] key))
(defmethod parametrization :default
([_ _] {})
([_] {}))
(defmulti field
"Return vector field for given name and options: amount (scaling factor) and parametrization.
Default scaling factor is 1.0, default parametrization is random.
Resulting function operates on [[Vec2]] type."
{:metadoc/categories #{:cr}}
(fn [key & _] key))
(defmethod field :default
([_ s _] (fn [v] (v/mult v s)))
([_ s] (fn [v] (v/mult v s)))
([_] identity))
(defmacro ^:private make-config-method
"Add new multimethod for variation parametrization"
[sym m]
(let [k (keyword sym)]
`(defmethod parametrization ~k
([k# p#] (merge ~m p#))
([k#] ~m))))
(defmacro ^:private make-field-method
"Add new multimethod for variation factory function and store variation fn in global list."
[sym t]
(let [k (keyword sym)
m (symbol (str "make-" sym))]
`(do (defmethod field ~k
([k# a# p#] (~m a# (parametrization ~k p#)))
([k# a#] (~m a# (parametrization ~k)))
([k#] (~m 1.0 (parametrization ~k))))
~(if (= t :regular)
`(register-regular-field ~k)
`(register-random-field ~k)))))
;; Locally used random function for some parametrization parameters. Mostly used to avoid `0` value.
(defn- srandom
"Symetric random from [-mx -mn] and [mn mx]"
^double [^double mn ^double mx]
(let [rand (drand mn mx)]
(randval rand (* -1.0 rand))))
;; Local noise generating functions (for various noise implementations)
(defn- make-noise-variation
"Calculate shift by angle taken from noise"
[^double amount scale noise-fn]
(fn [v]
(let [^Vec2 vv (v/mult v scale)
angle (* m/TWO_PI ^double (noise-fn (.x vv) (.y vv)))]
(v/add v (Vec2. (* amount (m/cos angle))
(* amount (m/sin angle)))))))
(defn- make-noise-variation2
"Calculate shift by vector taken from noise"
[^double amount scale noise-fn]
(fn [v]
(let [^Vec2 vv (v/mult v scale)
x1 (- ^double (noise-fn (.x vv) (.y vv)) 0.5)
y1 (- ^double (noise-fn (.y vv) m/E (.x vv)) 0.5)]
(v/add v (Vec2. (* amount x1)
(* amount y1))))))
(def ^:private ^:const unitx (Vec2. 1.0 0.0))
(def ^:private ^:const zerov (Vec2. 0.0 0.0))
;; Two atoms to store variation names. One for non-random functions and second for random.
(def ^:private regular-field (atom [:default]))
(def ^:private random-field (atom []))
(defn- register-field
"Add `name` to the atom `what`"
[what name]
(swap! what conj name))
(def ^:private register-random-field (partial register-field random-field))
(def ^:private register-regular-field (partial register-field regular-field))
;; ## A
;; ### Arch
(defn- make-arch
"Arch"
[^double amount _]
(fn [^Vec2 v]
(let [ang (* amount (drand m/PI))
sinr (m/sin ang)
cosr (m/cos ang)]
(if (zero? cosr) zerov
(Vec2. (* amount sinr)
(* amount (/ (m/sq sinr) cosr)))))))
(make-field-method arch :random)
;; ### Asteria
(make-config-method asteria {:alpha (drand -5 5)})
(defn- make-asteria
"asteria by dark-beam, http://jwildfire.org/forum/viewtopic.php?f=23&t=1464"
[^double amount {:keys [^double alpha]}]
(let [sina (m/sin (* m/PI alpha))
cosa (m/cos (* m/PI alpha))]
(fn [^Vec2 v]
(let [^Vec2 v0 (v/mult v amount)
^double r (v/magsq v0)
xx (m/sq (dec (m/abs (.x v0))))
yy (m/sq (dec (m/abs (.y v0))))
r2 (m/sqrt (+ xx yy))
in (< r 1.0)
in1 (if (and (< r 1.0) (< r2 1.0))
(brand 0.65)
(not in))]
(if in1
v0
(let [xx (- (* cosa (.x v0))
(* sina (.y v0)))
yy (+ (* sina (.x v0))
(* cosa (.y v0)))
nx (* (/ xx (m/sqrt (- 1.0 (* yy yy))))
(- 1.0 (m/sqrt (- 1.0 (m/sq (inc (- (m/abs yy))))))))]
(Vec2. (+ (* cosa nx)
(* sina yy))
(+ (- (* sina nx))
(* cosa yy)))))))))
(make-field-method asteria :random)
;; ### Atan
(make-config-method atan {:mode (lrand 3)
:stretch (srandom 0.01 2.0)})
(defn- make-atan
""
[^double amount {:keys [^long mode ^double stretch]}]
(let [m (m/constrain mode 0 2)
norm (/ 1.0 (* m/M_PI_2 amount))]
(fn [^Vec2 v]
(case m
0 (Vec2. (.x v)
(* norm (m/atan (* stretch (.y v)))))
1 (Vec2. (* norm (m/atan (* stretch (.x v))))
(.y v))
(Vec2. (* norm (m/atan (* stretch (.x v))))
(* norm (m/atan (* stretch (.y v)))))))))
(make-field-method atan :regular)
;; ### Auger
(make-config-method auger {:freq (drand -5 5)
:weight (drand -1 1)
:sym (drand -2 2)
:scale (srandom 0.5 2)})
(defn- make-auger
"Auger by Xyrus02"
[^double amount {:keys [^double freq ^double weight ^double sym ^double scale]}]
(fn [^Vec2 v]
(let [x (.x v)
y (.y v)
s (m/sin (* freq x))
t (m/sin (* freq y))
dy (+ y (* weight (+ (m/abs y) (* 0.5 s scale)) s))
dx (+ x (* weight (+ (m/abs x) (* 0.5 t scale)) t))
xx (* amount (+ x (* sym (- dx x))))
yy (* amount dy)]
(Vec2. xx yy))))
(make-field-method auger :regular)
;; ## B
(defn- bseries-calc
"Common calculations for bSeries "
[^double amount ^double tau ^double sigma]
(let [sinht (m/sinh tau)
cosht (m/cosh tau)
sins (m/sin sigma)
coss (m/cos sigma)
temp (- cosht coss)]
(if (zero? temp)
(Vec2. 0.0 0.0)
(Vec2. (* amount (/ sinht temp))
(* amount (/ sins temp))))))
;; ### bCollide
(make-config-method bcollide {:num (srandom 1 30)
:a (drand 2)})
(defn- make-bcollide
"bCollide by Michael Faber, http://michaelfaber.deviantart.com/art/bSeries-320574477"
[^double amount {:keys [^double num ^double a]}]
(let [bcn-pi (* num m/M_1_PI)
pi-bcn (/ m/PI num)
bca-bcn (/ (* m/PI a) num)]
(fn [^Vec2 v]
(let [v+ (v/add v unitx)
v- (Vec2. (- 1.0 (.x v)) (.y v))
tau (* 0.5 (- (m/log (v/magsq v+))
(m/log (v/magsq v-))))
pre-sigma (- m/PI ^double (v/heading v+) ^double (v/heading v-))
alt (double (int (* pre-sigma bcn-pi)))
sigma (if (even? (int alt))
(+ (* alt pi-bcn) (rem (+ pre-sigma bca-bcn) pi-bcn))
(+ (* alt pi-bcn) (rem (- pre-sigma bca-bcn) pi-bcn)))]
(bseries-calc amount tau sigma)))))
(make-field-method bcollide :regular)
;; ### bMod
(make-config-method bmod {:radius (drand 0.5 2.0)
:distance (drand 2.0)})
(defn- make-bmod
"bMod by Michael Faber, http://michaelfaber.deviantart.com/art/bSeries-320574477"
[^double amount {:keys [^double radius ^double distance]}]
(let [rd (* radius distance)
r2 (+ radius radius)]
(fn [^Vec2 v]
(let [v+ (v/add v unitx)
v- (Vec2. (- 1.0 (.x v)) (.y v))
pre-tau (* 0.5 (- (m/log (v/magsq v+))
(m/log (v/magsq v-))))
sigma (- m/PI ^double (v/heading v+) ^double (v/heading v-))
tau (if (and (< pre-tau radius) (< (- pre-tau) radius))
(- (rem (+ pre-tau radius rd) r2) radius)
pre-tau)]
(bseries-calc amount tau sigma)))))
(make-field-method bmod :regular)
;; ### bSplit
(make-config-method bsplit {:x (drand -2.0 2.0)
:y (drand -2.0 2.0)})
(defn- make-bsplit
"Raykoid666, transcribed and modded by Nic Anderson, chronologicaldot"
[^double amount {:keys [^double x ^double y]}]
(fn [^Vec2 v]
(let [xx (+ x (.x v))]
(Vec2. (* (/ amount (m/tan xx)) (m/cos (+ y (.y v))))
(* (/ amount (m/sin xx)) (- y (.y v)))))))
(make-field-method bsplit :regular)
;; ### bSwirl
(make-config-method bswirl {:in (drand -2.0 2.0)
:out (drand -2.0 2.0)})
(defn- make-bswirl
"bSwirl by Michael Faber, http://michaelfaber.deviantart.com/art/bSeries-320574477"
[^double amount {:keys [^double in ^double out]}]
(fn [^Vec2 v]
(let [v+ (v/add v unitx)
v- (Vec2. (- 1.0 (.x v)) (.y v))
tau (* 0.5 (- (m/log (v/magsq v+))
(m/log (v/magsq v-))))
pre-sigma (- m/PI ^double (v/heading v+) ^double (v/heading v-))
sigma (+ pre-sigma (* tau out) (/ in tau))]
(bseries-calc amount tau sigma))))
(make-field-method bswirl :regular)
;; ### bTransform
(make-config-method btransform {:rotate (drand m/TWO_PI)
:power (randval 0.5 (drand 10.0) (irand 10.0))
:move (drand -2.0 2.0)
:split (drand -2.0 2.0)})
(defn- make-btransform
"bTransform by Michael Faber, http://michaelfaber.deviantart.com/art/bSeries-320574477"
[^double amount {:keys [^double rotate ^double power ^double move ^double split]}]
(let [mp (/ m/TWO_PI power)]
(fn [^Vec2 v]
(let [v+ (v/add v unitx)
v- (Vec2. (- 1.0 (.x v)) (.y v))
pre-tau (+ (/ (* 0.5 (- (m/log (v/magsq v+))
(m/log (v/magsq v-)))) power) move)
pre-sigma (+ (- m/PI ^double (v/heading v+) ^double (v/heading v-)) rotate)
tau (if (neg? (.x v))
(- pre-tau split)
(+ pre-tau split))
sigma (+ (/ pre-sigma power)
(* mp (m/floor (* power (drand)))))]
(bseries-calc amount tau sigma)))))
(make-field-method btransform :random)
;; ### BWraps7
(make-config-method bwraps7 {:cellsize (srandom 0.5 2.0)
:space (drand -1.0 1.0)
:gain (drand -2.0 2.0)
:inner-twist (drand -2.0 2.0)
:outer-twist (drand -2.0 2.0)})
(defn- make-bwraps7
"http://slobo777.deviantart.com/art/Bubble-Wrap-WIP-Plugin-112370125"
[^double amount {:keys [^double cellsize ^double space ^double gain ^double inner-twist ^double outer-twist]}]
(let [radius (* 0.5 (/ cellsize (inc (m/sq space))))
g2 (+ m/EPSILON (* gain gain))
max-bubble- (* g2 radius)
max-bubble (if (> max-bubble- 2.0)
1.0
(* max-bubble- (/ 1.0 (inc (* 0.25 (m/sq max-bubble-))))))
r2 (m/sq radius)
rfactor (/ radius max-bubble)]
(fn [^Vec2 v]
(if (< (m/abs cellsize) m/EPSILON)
(v/mult v amount)
(let [^Vec2 C (-> v
(v/div cellsize)
(v/applyf m/floor)
(v/add (Vec2. 0.5 0.5))
(v/mult cellsize))
L (v/sub v C)]
(if (> ^double (v/magsq L) r2)
(v/mult v amount)
(let [L (v/mult L g2)
r (/ rfactor (inc (* 0.25 ^double (v/magsq L))))
^Vec2 L (v/mult L r)
r (/ ^double (v/magsq L) r2)
theta (+ (* inner-twist (- 1.0 r))
(* outer-twist r))
s (m/sin theta)
c (m/cos theta)
vx (+ (.x C) (* c (.x L)) (* s (.y L)))
vy (+ (.y C) (* -1.0 s (.x L)) (* c (.y L)))]
(v/mult (Vec2. vx vy) amount))))))))
(make-field-method bwraps7 :regular)
;; ### Barycentroid
(make-config-method barycentroid {:a (drand -2.0 2.0)
:b (drand -2.0 2.0)
:c (drand -2.0 2.0)
:d (drand -2.0 2.0)})
(defn- make-barycentroid
"barycentroid from Xyrus02, http://xyrusworx.deviantart.com/art/Barycentroid-Plugin-144832371?q=sort%3Atime+favby%3Amistywisp&qo=0&offset=10"
[^double amount {:keys [^double a ^double b ^double c ^double d]}]
(let [dot00 (+ (* a a) (* b b))
dot01 (+ (* a c) (* b d))
dot11 (+ (* c c) (* d d))
inv-denom (/ 1.0 (- (* dot00 dot11) (* dot01 dot01)))]
(fn [^Vec2 v]
(let [dot02 (+ (* a (.x v)) (* b (.y v)))
dot12 (+ (* c (.x v)) (* d (.y v)))
u (* inv-denom (- (* dot11 dot02) (* dot01 dot12)))
vv (* inv-denom (- (* dot00 dot12) (* dot01 dot02)))
um (* (m/signum u) (m/sqrt (+ (* u u) (m/sq (.x v)))))
vm (* (m/signum vv) (m/sqrt (+ (* vv vv) (m/sq (.y v)))))]
(Vec2. (* amount um)
(* amount vm))))))
(make-field-method barycentroid :regular)
;; ### Bent
(defn- make-bent
"Bent"
[^double amount _]
(fn [^Vec2 v]
(let [nx (if (neg? (.x v)) (+ (.x v) (.x v)) (.x v))
ny (if (neg? (.y v)) (* (.y v) 0.5) (.y v))]
(Vec2. (* amount nx)
(* amount ny)))))
(make-field-method bent :regular)
(make-config-method bent2 {:x (srandom 0.5 2.0)
:y (srandom 0.5 2.0)})
(defn- make-bent2
"Bent2"
[^double amount {:keys [^double x ^double y]}]
(fn [^Vec2 v]
(let [nx (if (neg? (.x v)) (* (.x v) x) (.x v))
ny (if (neg? (.y v)) (* (.y v) y) (.y v))]
(Vec2. (* amount nx)
(* amount ny)))))
(make-field-method bent2 :regular)
;; ### Bipolar
(make-config-method bipolar {:shift (drand -2.0 2.0)})
(defn- make-bipolar
"Bipolar"
[^double amount {:keys [^double shift]}]
(let [ps (* (- m/HALF_PI) shift)]
(fn [^Vec2 v]
(let [^double x2y2 (v/magsq v)
t (inc x2y2)
x2 (+ (.x v) (.x v))
pre-y (+ ps (* 0.5 (m/atan2 (+ (.y v) (.y v))
(dec x2y2))))
y (if (> pre-y m/HALF_PI)
(- (rem (+ pre-y m/HALF_PI) m/PI) m/HALF_PI)
(if (< pre-y (- m/HALF_PI))
(- m/HALF_PI (rem (- m/HALF_PI pre-y) m/PI))
pre-y))
f (+ t x2)
g (- t x2)]
(if (or (zero? g)
(not (pos? (/ f g))))
(Vec2. 0.0 0.0)
(Vec2. (* amount m/M_2_PI 0.25 (m/log (/ f g)))
(* amount m/M_2_PI y)))))))
(make-field-method bipolar :regular)
;; ### Blade
(defn- make-blade
"Blade"
[^double amount _]
(fn [^Vec2 v]
(let [r (* (drand amount) ^double (v/mag v))
sinr (m/sin r)
cosr (m/cos r)]
(Vec2. (* amount (.x v) (+ cosr sinr))
(* amount (.x v) (- cosr sinr))))))
(make-field-method blade :random)
(defn- make-blade2
"Blade2"
[^double amount _]
(fn [^Vec2 v]
(let [r (* (drand amount) ^double (v/mag v))
sinr (m/sin r)
cosr (m/cos r)]
(Vec2. (* amount (.x v) (+ cosr sinr))
(* amount (.y v) (- cosr sinr))))))
(make-field-method blade2 :random)
;; ### Blob
(make-config-method blob {:low (drand -2.0 2.0)
:high (drand -2.0 2.0)
:waves (drand -6.0 6.0)})
(defn- make-blob
"Blob"
[^double amount {:keys [^double low ^double high ^double waves]}]
(let [hl (- high low)]
(fn [^Vec2 v]
(let [^double a (v/heading v)
^double r (v/mag v)
rr (->> (* a waves)
(m/sin)
(* 0.5)
(+ 0.5)
(* hl)
(+ low)
(* r))]
(Vec2. (* amount rr (m/sin a))
(* amount rr (m/cos a)))))))
(make-field-method blob :regular)
;; ### Blocky
(make-config-method blocky {:x (srandom 0.5 1.5)
:y (srandom 0.5 1.5)
:mp (srandom 0.001 6.0)})
(defn- make-blocky
"blocky from FracFx, http://fracfx.deviantart.com/art/FracFx-Plugin-Pack-171806681"
[^double amount {:keys [^double x ^double y ^double mp]}]
(let [vv (/ amount m/HALF_PI)]
(fn [^Vec2 v]
(let [T (inc (/ (+ (m/cos (.x v)) (m/cos (.y v))) mp))
r (/ amount T)
tmp (inc ^double (v/magsq v))
x2 (+ (.x v) (.x v))
y2 (+ (.y v) (.y v))
xmax (* 0.5 (+ (m/sqrt (+ tmp x2)) (m/sqrt (- tmp x2))))
ymax (* 0.5 (+ (m/sqrt (+ tmp y2)) (m/sqrt (- tmp y2))))
ax (/ (.x v) xmax)
bx (m/safe-sqrt (- 1.0 (m/sq ax)))
ay (/ (.y v) ymax)
by (m/safe-sqrt (- 1.0 (m/sq ay)))]
(Vec2. (* vv (m/atan2 ax bx) r x)
(* vv (m/atan2 ay by) r y))))))
(make-field-method blocky :regular)
;; ### Blur Circle
(defn- make-blurcircle
"Blur circle"
[^double amount _]
(fn [^Vec2 v]
(let [x (drand -1.0 1.0)
y (drand -1.0 1.0)
absx (m/abs x)
absy (m/abs y)
^Vec2 ps (if (>= absx absy)
(Vec2. (if (>= x absy)
(+ absx y)
(- (* 5.0 absx) y)) absx)
(Vec2. (if (>= y absx)
(- (* 3.0 absy) x)
(+ (* 7.0 absy) x)) absy))
r (* amount (.y ps))
a (-> m/M_PI_4
(* (.x ps))
(/ (.y ps))
(- m/M_PI_4))
sa (m/sin a)
ca (m/cos a)]
(Vec2. (* r ca) (* r sa)))))
(make-field-method blurcircle :random)
;; ### Blur
(defn- make-blur
"Blur"
[^double amount _]
(fn [^Vec2 v]
(let [r (drand m/TWO_PI)
sr (m/sin r)
cr (m/cos r)
r2 (drand amount)]
(Vec2. (* r2 cr) (* r2 sr)))))
(make-field-method blur :random)
;; ### Blur pixelize
(make-config-method blurpixelize {:size (srandom 0.01 1.2)
:scale (drand -1.2 1.2)})
(defn- make-blurpixelize
"Blur Pixelize from Apo7X15C"
[^double amount {:keys [^double size ^double scale]}]
(let [inv-size (/ 1.0 size)
av (* amount size)
half (Vec2. 0.5 0.5)]
(fn [v]
(-> v
(v/mult inv-size)
(v/applyf m/floor)
(v/add (-> (v/generate-vec2 drand)
(v/sub half)
(v/mult scale)))
(v/add half)
(v/mult av)))))
(make-field-method blurpixelize :random)
;; ### BlurZoom
(make-config-method blurzoom {:length (drand -1.2 1.2)
:x (drand -1.2 1.2)
:y (drand -1.2 1.2)})
(defn- make-blurzoom
"Blur Zoom from Apo7X15C"
[^double amount {:keys [^double length ^double x ^double y]}]
(let [xy (Vec2. x y)
xy- (Vec2. x (- y))]
(fn [v]
(-> v
(v/sub xy)
(v/mult (inc (drand length)))
(v/add xy-)
(v/mult amount)))))
(make-field-method blurzoom :random)
;; ### Boarders
(defn- make-boarders
"Boarders"
[^double amount _]
(fn [^Vec2 v]
(let [roundx (m/rint (.x v))
roundy (m/rint (.y v))
offsetx (- (.x v) roundx)
offsety (- (.y v) roundy)
hoffsetx (* 0.5 offsetx)
hoffsety (* 0.5 offsety)]
(randval 0.75
(Vec2. (* amount (+ roundx hoffsetx))
(* amount (+ roundy hoffsety)))
(if (>= (m/abs offsetx) (m/abs offsety))
(if (>= offsetx 0.0)
(Vec2. (* amount (+ hoffsetx roundx 0.25))
(* amount (+ hoffsety roundy (/ (* 0.25 offsety) offsetx))))
(Vec2. (* amount (- (+ hoffsetx roundx) 0.25))
(* amount (- (+ hoffsety roundy) (/ (* 0.25 offsety) offsetx)))))
(if (>= offsety 0.0)
(Vec2. (* amount (+ hoffsetx roundx (/ (* 0.25 offsetx) offsety)))
(* amount (+ hoffsety roundy 0.25)))
(Vec2. (* amount (- (+ hoffsetx roundx) (/ (* 0.25 offsetx) offsety)))
(* amount (- (+ hoffsety roundy) 0.25)))))))))
(make-field-method boarders :random)
(make-config-method boarders2 {:c (drand -1.2 1.2)
:left (drand -1.2 1.2)
:right (drand -1.2 1.2)})
(defn- make-boarders2
"Boarders"
[^double amount {:keys [^double c ^double left ^double right]}]
(let [cc (m/abs c)
cl (m/abs left)
cr (m/abs right)
cc (if (zero? cc) m/EPSILON cc)
cl (if (zero? cl) m/EPSILON cl)
cr (if (zero? cr) m/EPSILON cr)
cl (* c cl)
cr (+ c (* c cr))]
(fn [^Vec2 v]
(let [roundx (m/rint (.x v))
roundy (m/rint (.y v))
offsetx (- (.x v) roundx)
offsety (- (.y v) roundy)
coffsetx (* c offsetx)
coffsety (* c offsety)]
(randval cr
(Vec2. (* amount (+ roundx coffsetx))
(* amount (+ roundy coffsety)))
(if (>= (m/abs offsetx) (m/abs offsety))
(if (>= offsetx 0.0)
(Vec2. (* amount (+ coffsetx roundx cl))
(* amount (+ coffsety roundy (/ (* cl offsety) offsetx))))
(Vec2. (* amount (- (+ coffsetx roundx) cl))
(* amount (- (+ coffsety roundy) (/ (* cl offsety) offsetx)))))
(if (>= offsety 0.0)
(Vec2. (* amount (+ coffsetx roundx (/ (* cl offsetx) offsety)))
(* amount (+ coffsety roundy cl)))
(Vec2. (* amount (- (+ coffsetx roundx) (/ (* cl offsetx) offsety)))
(* amount (- (+ coffsety roundy) cl))))))))))
(make-field-method boarders2 :random)
;; ### Bubble
(defn- make-bubble
"Bubble"
[^double amount _]
(fn [v]
(v/mult v (/ amount (inc (* 0.25 ^double (v/mag v)))))))
(make-field-method bubble :regular)
;; ### Butterfly
(defn- make-butterfly
"Butterfly"
[^double amount _]
(let [wx (* amount 1.3029400317411197908970256609023)]
(fn [^Vec2 v]
(let [y2 (* 2.0 (.y v))
r (* wx (m/sqrt (/ (m/abs (* (.y v) (.x v)))
(+ m/EPSILON (m/sq (.x v)) (m/sq y2)))))]
(Vec2. (* r (.x v))
(* r y2))))))
(make-field-method butterfly :regular)
;; ### BesselJ
(comment defn- make-besselj
"Bessel"
[^double amount _]
(fn [^Vec2 v]
(Vec2. (* amount ^double (v/mag v) (m/bessel-j (m/abs (.x v)) (m/abs (.y v))))
(* amount ^double (v/heading v)))))
(comment make-field-method besselj :regular)
;; ### Beta
(defn- make-beta
"Beta"
[^double amount _]
(fn [^Vec2 v]
(Vec2. (* amount (m/log-beta (+ m/EPSILON (m/abs (.x v))) (+ m/EPSILON (m/abs (.y v)))))
(* amount ^double (v/heading v)))))
(make-field-method beta :regular)
;; ## C
;; ### CPow3
(make-config-method cpow3 {:r (drand -2 2)
:a (drand -2 2)
:divisor (srandom 0.1 2)
:spread (srandom 0.1 2)
:discrete-spread (srandom 0.1 2)
:spread2 (drand -2 2)
:offset2 (drand -2 2)})
(defn- make-cpow3
"CPow3"
[^double amount {:keys [^double r ^double a ^double divisor ^double spread ^double discrete-spread ^double spread2 ^double offset2]}]
(let [ang (/ m/TWO_PI divisor)
c (/ (* r (m/cos (* m/HALF_PI a))) divisor)
d (/ (* r (m/sin (* m/HALF_PI a))) divisor)
half-c (* 0.5 c)
half-d (* 0.5 d)
inv-spread (/ 0.5 spread)
full-spread (* m/TWO_PI spread)
fac (* c half-d ang)]
(fn [^Vec2 v]
(let [^double ai (v/heading v)
n (drand spread)
n (double (if (>= discrete-spread 1.0) (int n) n))
n (if (neg? ai) (inc n) n)
ai (+ ai (* m/TWO_PI n))
ai (if (< (m/cos (* ai inv-spread)) (drand -1.0 1.0)) (- ai full-spread) ai)
lnr2 (m/log (v/magsq v))
ri (* amount (m/exp (- (* half-c lnr2) (* d ai))))
ang2 (* fac ai lnr2 (+ (* spread2 (drand)) offset2))]
(Vec2. (* ri (m/cos ang2))
(* ri (m/sin ang2)))))))
(make-field-method cpow3 :random)
;; ### CPow
(make-config-method cpow {:r (drand -2 2)
:i (drand -2 2)
:power (srandom 0.1 12)})
(defn- make-cpow
"CPow"
[^double amount {:keys [^double r ^double i ^double power]}]
(let [va (/ m/TWO_PI power)
vc (/ r power)
vd (/ i power)]
(fn [v]
(let [^double a (v/heading v)
lnr (* 0.5 (m/log (v/magsq v)))
ang (+ (* a vc)
(* vd lnr)
(* va (m/floor (drand power))))
m (* amount (m/exp (- (* vc lnr)
(* vd a))))]
(Vec2. (* m (m/cos ang)) (* m (m/sin ang)))))))
(make-field-method cpow :random)
;; ### Cell
(make-config-method cell {:size (srandom 0.1 2.0)})
(defn- make-cell
"Cell"
[^double amount {:keys [^double size]}]
(let [inv-cell-size (/ 1.0 size)
av (Vec2. amount (- amount))]
(fn [v]
(let [^Vec2 xy (-> v
(v/mult inv-cell-size)
(v/applyf m/floor))
dxy (-> v
(v/sub (v/mult xy size)))
newxy (if-not (neg? (.y xy))
(if-not (neg? (.x xy))
(v/mult xy 2.0)
(Vec2. (- (inc (* 2.0 (.x xy))))
(* 2.0 (.y xy))))
(if-not (neg? (.x xy))
(Vec2. (* 2.0 (.x xy))
(- (inc (* 2.0 (.y xy)))))
(Vec2. (- (inc (* 2.0 (.x xy))))
(- (inc (* 2.0 (.y xy)))))))]
(-> newxy
(v/mult size)
(v/add dxy)
(v/emult av))))))
(make-field-method cell :regular)
;; ### Checks
(make-config-method checks {:x (drand -10 10)
:y (drand -10 10)
:size (drand -2.0 2.0)
:rnd (randval 0.0 (srandom 0.1 1))})
(defn- make-checks
"Checks"
[^double amount {:keys [^double x ^double y ^double size ^double rnd]}]
(let [cs (/ 1.0 (+ size m/EPSILON))
ncx (* -1.0 x)
ncy (* -1.0 y)]
(fn [^Vec2 v]
(let [isxy (+ (m/round (* cs (.x v)))
(m/round (* cs (.y v))))
dxy (if (even? isxy)
(Vec2. (+ ncx (drand rnd)) ncy)
(Vec2. x (+ y (drand rnd))))]
(-> v
(v/add dxy)
(v/mult amount))))))
(make-field-method checks :random)
;; ### Chunk
(make-config-method chunk {:a (drand -1.2 1.2)
:b (drand -1.2 1.2)
:c (drand -1.2 1.2)
:d (drand -1.2 1.2)
:e (drand -1.2 1.2)
:f (drand -1.2 1.2)
:mode (brand)})
(defn- make-chunk
"Chunk, by zephyrtronium https://zephyrtronium.deviantart.com/art/Chunk-Apophysis-Plugin-Pack-182375397"
[^double amount {:keys [^double a ^double b ^double c ^double d ^double e ^double f mode]}]
(fn [^Vec2 v]
(let [r (+ (* a (m/sq (.x v)))
(* b (.x v) (.y v))
(* c (m/sq (.y v)))
(* d (.x v))
(* e (.y v))
f)]
(if mode
(if-not (pos? r) v zerov)
(if (pos? r) v zerov)))))
(make-field-method chunk :regular)
;; ### Circle blur
(defn- make-circleblur
"Circle blur"
[^double amount _]
(fn [_]
(let [rad (m/sqrt (drand))
a (drand m/TWO_PI)]
(Vec2. (* amount (m/cos a) rad)
(* amount (m/sin a) rad)))))
(make-field-method circleblur :random)
;; ### CircleRand
(make-config-method circlerand {:Sc (drand 0.1 1.2)
:Dens (drand 1)
:X (drand -20 20)
:Y (drand -20 20)
:Seed (irand Integer/MAX_VALUE)})
(defn- make-circlerand
"Circle Rand http://eralex61.deviantart.com/art/Circles-Plugins-126273412"
[^double amount {:keys [^double Sc ^double Dens ^double X ^double Y ^double Seed]}]
(let [xy (Vec2. X Y)]
(fn [v]
(loop [iter (int 0)]
(let [XY (-> (v/generate-vec2 #(drand -1.0 1.0))
(v/emult xy))
^Vec2 MN (-> XY
(v/mult 0.5)
(v/div Sc)
(v/applyf m/floor))
XY (v/sub XY (-> MN
(v/mult 2.0)
(v/applyf #(inc ^double %))
(v/mult Sc)))]
(if (bool-and (< iter 60)
(bool-or (> (discrete-noise (+ Seed (.x MN)) (.y MN)) Dens)
(> ^double (v/mag XY) (-> (discrete-noise (+ 10.0 (.x MN)) (+ 3.0 (.y MN)))
(* 0.7)
(+ 0.3)
(* Sc)))))
(recur (inc iter))
(-> MN
(v/mult 2.0)
(v/applyf #(inc ^double %))
(v/mult Sc)
(v/add XY)
(v/mult amount))))))))
(make-field-method circlerand :random)
;; ### CircleLinear
(make-config-method circlelinear {:Sc (drand 1)
:K (drand -2 2)
:Dens1 (drand 1)
:Dens2 (drand 1)
:Reverse (drand -1 1)
:X (drand 20)
:Y (drand 20)
:Seed (irand Integer/MAX_VALUE)})
(defn- make-circlelinear
"CircleLinear by eralex, http://eralex61.deviantart.com/art/Circles-Plugins-126273412"
[^double amount {:keys [^double Sc ^double K ^double Dens1 ^double Dens2 ^double Reverse ^double X ^double Y ^double Seed]}]
(let [dd (* Dens1 Dens2)]
(fn [^Vec2 v]
(let [M (->> Sc
(/ (.x v))
(* 0.5)
(m/floor))
N (->> Sc
(/ (.y v))
(* 0.5)
(m/floor))
X (- (.x v) (->> M
(* 2.0)
(inc)
(* Sc)))
Y (- (.y v) (->> N
(* 2.0)
(inc)
(* Sc)))
U (m/hypot X Y)
V (->> (discrete-noise (+ M 10.0) (+ N 3.0))
(* 0.7)
(+ 0.3)
(* Sc))
Z1 (discrete-noise (+ M Seed) N)
^Vec2 XY (if (and (< Z1 Dens1) (< U V))
(if (pos? Reverse)
(if (< Z1 dd)
(Vec2. (* K X) (* K Y))
(let [Z (->> K
(- 1.0)
(* U)
(/ V)
(+ K))]
(Vec2. (* Z X) (* Z Y))))
(if (> Z1 dd)
(Vec2. (* K X) (* K Y))
(let [Z (->> K
(- 1.0)
(* U)
(/ V)
(+ K))]
(Vec2. (* Z X) (* Z Y)))))
(Vec2. X Y))]
(Vec2. (->> 2.0
(* M)
(inc)
(* Sc)
(+ (.x XY))
(* amount))
(->> 2.0
(* N)
(inc)