1use cgmath::num_traits::{Float, FromPrimitive};
2
3pub trait HashGen<S> {
5 fn hash1(gen: Self) -> S;
7 fn hash2(gen: Self) -> [S; 2];
9 fn hash3(gen: Self) -> [S; 3];
11 fn hash4(gen: Self) -> [S; 4];
13}
14
15impl<S: Float + FromPrimitive> HashGen<S> for S {
16 fn hash1(s: S) -> S {
17 let a = S::from_f64(61.909685033545934).unwrap();
18 let b = S::from_f64(8.436303256302796).unwrap();
19 let c = S::from_f64(220.6786200836378).unwrap();
20 let x = S::sin(s * a + b) * c;
21 x - S::floor(x)
22 }
23
24 fn hash2(s: S) -> [S; 2] {
25 let a = S::from_f64(61.909685033545934).unwrap();
26 let b = S::from_f64(8.436303256302796).unwrap();
27 let c = S::from_f64(220.6786200836378).unwrap();
28 let x = S::sin(s * a + b) * c;
29 let a = S::from_f64(92.38848345286779).unwrap();
30 let b = S::from_f64(2.7771476700831443).unwrap();
31 let c = S::from_f64(573.3044309816089).unwrap();
32 let y = S::sin(s * a + b) * c;
33 [x - S::floor(x), y - S::floor(y)]
34 }
35
36 fn hash3(s: S) -> [S; 3] {
37 let a = S::from_f64(61.909685033545934).unwrap();
38 let b = S::from_f64(8.436303256302796).unwrap();
39 let c = S::from_f64(220.6786200836378).unwrap();
40 let x = S::sin(s * a + b) * c;
41 let a = S::from_f64(92.38848345286779).unwrap();
42 let b = S::from_f64(2.7771476700831443).unwrap();
43 let c = S::from_f64(573.3044309816089).unwrap();
44 let y = S::sin(s * a + b) * c;
45 let a = S::from_f64(69.61119030137992).unwrap();
46 let b = S::from_f64(0.8814422748956363).unwrap();
47 let c = S::from_f64(176.56179040382136).unwrap();
48 let z = S::sin(s * a + b) * c;
49 [x - S::floor(x), y - S::floor(y), z - S::floor(z)]
50 }
51
52 fn hash4(s: S) -> [S; 4] {
53 let a = S::from_f64(61.909685033545934).unwrap();
54 let b = S::from_f64(8.436303256302796).unwrap();
55 let c = S::from_f64(220.6786200836378).unwrap();
56 let x = S::sin(s * a + b) * c;
57 let a = S::from_f64(92.38848345286779).unwrap();
58 let b = S::from_f64(2.7771476700831443).unwrap();
59 let c = S::from_f64(573.3044309816089).unwrap();
60 let y = S::sin(s * a + b) * c;
61 let a = S::from_f64(69.61119030137992).unwrap();
62 let b = S::from_f64(0.8814422748956363).unwrap();
63 let c = S::from_f64(176.56179040382136).unwrap();
64 let z = S::sin(s * a + b) * c;
65 let a = S::from_f64(21.423754555191877).unwrap();
66 let b = S::from_f64(0.2164601136047869).unwrap();
67 let c = S::from_f64(871.0649084120648).unwrap();
68 let w = S::sin(s * a + b) * c;
69 [
70 x - S::floor(x),
71 y - S::floor(y),
72 z - S::floor(z),
73 w - S::floor(w),
74 ]
75 }
76}
77
78impl<S: Float + FromPrimitive> HashGen<S> for [S; 2] {
79 fn hash1(v: [S; 2]) -> S {
80 let a = HashGen::hash1(v[0]);
81 let b = HashGen::hash1(v[1]);
82 let c = S::from_f64(9.784225605373198).unwrap();
83 let d = S::from_f64(68.94807014710901).unwrap();
84 let e = S::from_f64(81.49907289737997).unwrap();
85 let x = S::sin(a * c + b * d) * e;
86 x - S::floor(x)
87 }
88
89 fn hash2(v: [S; 2]) -> [S; 2] {
90 let a = HashGen::hash1(v[0]);
91 let b = HashGen::hash1(v[1]);
92 let c = S::from_f64(9.784225605373198).unwrap();
93 let d = S::from_f64(68.94807014710901).unwrap();
94 let e = S::from_f64(81.49907289737997).unwrap();
95 let x = S::sin(a * c + b * d) * e;
96 let c = S::from_f64(80.84673652708462).unwrap();
97 let d = S::from_f64(47.747983481580206).unwrap();
98 let e = S::from_f64(967.6242851986622).unwrap();
99 let y = S::sin(a * c + b * d) * e;
100 [x - S::floor(x), y - S::floor(y)]
101 }
102
103 fn hash3(v: [S; 2]) -> [S; 3] {
104 let a = HashGen::hash1(v[0]);
105 let b = HashGen::hash1(v[1]);
106 let c = S::from_f64(9.784225605373198).unwrap();
107 let d = S::from_f64(68.94807014710901).unwrap();
108 let e = S::from_f64(81.49907289737997).unwrap();
109 let x = S::sin(a * c + b * d) * e;
110 let c = S::from_f64(80.84673652708462).unwrap();
111 let d = S::from_f64(47.747983481580206).unwrap();
112 let e = S::from_f64(967.6242851986622).unwrap();
113 let y = S::sin(a * c + b * d) * e;
114 let c = S::from_f64(75.65061185374819).unwrap();
115 let d = S::from_f64(0.7529162434507297).unwrap();
116 let e = S::from_f64(825.2394180776313).unwrap();
117 let z = S::sin(a * c + b * d) * e;
118 [x - S::floor(x), y - S::floor(y), z - S::floor(z)]
119 }
120
121 fn hash4(v: [S; 2]) -> [S; 4] {
122 let a = HashGen::hash1(v[0]);
123 let b = HashGen::hash1(v[1]);
124 let c = S::from_f64(9.784225605373198).unwrap();
125 let d = S::from_f64(68.94807014710901).unwrap();
126 let e = S::from_f64(81.49907289737997).unwrap();
127 let x = S::sin(a * c + b * d) * e;
128 let c = S::from_f64(80.84673652708462).unwrap();
129 let d = S::from_f64(47.747983481580206).unwrap();
130 let e = S::from_f64(967.6242851986622).unwrap();
131 let y = S::sin(a * c + b * d) * e;
132 let c = S::from_f64(75.65061185374819).unwrap();
133 let d = S::from_f64(0.7529162434507297).unwrap();
134 let e = S::from_f64(825.2394180776313).unwrap();
135 let z = S::sin(a * c + b * d) * e;
136 let c = S::from_f64(97.2707869579049).unwrap();
137 let d = S::from_f64(85.3104588821598).unwrap();
138 let e = S::from_f64(329.18836800713547).unwrap();
139 let w = S::sin(a * c + b * d) * e;
140 [
141 x - S::floor(x),
142 y - S::floor(y),
143 z - S::floor(z),
144 w - S::floor(w),
145 ]
146 }
147}
148
149impl<S: Float + FromPrimitive> HashGen<S> for [S; 3] {
150 fn hash1(v: [S; 3]) -> S {
151 let a = HashGen::hash1([v[1], v[2]]);
152 let b = HashGen::hash1([v[2], v[0]]);
153 let c = HashGen::hash1([v[0], v[1]]);
154 let d = S::from_f64(98.54090184430027).unwrap();
155 let e = S::from_f64(67.78141684128485).unwrap();
156 let f = S::from_f64(2.0948068876946224).unwrap();
157 let g = S::from_f64(280.4659654793662).unwrap();
158 let x = S::sin(a * d + b * e + c * f) * g;
159 x - S::floor(x)
160 }
161
162 fn hash2(v: [S; 3]) -> [S; 2] {
163 let a = HashGen::hash1([v[1], v[2]]);
164 let b = HashGen::hash1([v[2], v[0]]);
165 let c = HashGen::hash1([v[0], v[1]]);
166 let d = S::from_f64(98.54090184430027).unwrap();
167 let e = S::from_f64(67.78141684128485).unwrap();
168 let f = S::from_f64(2.0948068876946224).unwrap();
169 let g = S::from_f64(280.4659654793662).unwrap();
170 let x = S::sin(a * d + b * e + c * f) * g;
171 let d = S::from_f64(31.298218789251166).unwrap();
172 let e = S::from_f64(20.629869323489913).unwrap();
173 let f = S::from_f64(49.45405453340679).unwrap();
174 let g = S::from_f64(65.41804793662531).unwrap();
175 let y = S::sin(a * d + b * e + c * f) * g;
176 [x - S::floor(x), y - S::floor(y)]
177 }
178
179 fn hash3(v: [S; 3]) -> [S; 3] {
180 let a = HashGen::hash1([v[1], v[2]]);
181 let b = HashGen::hash1([v[2], v[0]]);
182 let c = HashGen::hash1([v[0], v[1]]);
183 let d = S::from_f64(98.54090184430027).unwrap();
184 let e = S::from_f64(67.78141684128485).unwrap();
185 let f = S::from_f64(2.0948068876946224).unwrap();
186 let g = S::from_f64(280.4659654793662).unwrap();
187 let x = S::sin(a * d + b * e + c * f) * g;
188 let d = S::from_f64(31.298218789251166).unwrap();
189 let e = S::from_f64(20.629869323489913).unwrap();
190 let f = S::from_f64(49.45405453340679).unwrap();
191 let g = S::from_f64(65.41804793662531).unwrap();
192 let y = S::sin(a * d + b * e + c * f) * g;
193 let d = S::from_f64(24.301021506408738).unwrap();
194 let e = S::from_f64(2.287647662619474).unwrap();
195 let f = S::from_f64(97.795539177359).unwrap();
196 let g = S::from_f64(738.3773753517622).unwrap();
197 let z = S::sin(a * d + b * e + c * f) * g;
198 [x - S::floor(x), y - S::floor(y), z - S::floor(z)]
199 }
200
201 fn hash4(v: [S; 3]) -> [S; 4] {
202 let a = HashGen::hash1([v[1], v[2]]);
203 let b = HashGen::hash1([v[2], v[0]]);
204 let c = HashGen::hash1([v[0], v[1]]);
205 let d = S::from_f64(98.54090184430027).unwrap();
206 let e = S::from_f64(67.78141684128485).unwrap();
207 let f = S::from_f64(2.0948068876946224).unwrap();
208 let g = S::from_f64(280.4659654793662).unwrap();
209 let x = S::sin(a * d + b * e + c * f) * g;
210 let d = S::from_f64(31.298218789251166).unwrap();
211 let e = S::from_f64(20.629869323489913).unwrap();
212 let f = S::from_f64(49.45405453340679).unwrap();
213 let g = S::from_f64(65.41804793662531).unwrap();
214 let y = S::sin(a * d + b * e + c * f) * g;
215 let d = S::from_f64(24.301021506408738).unwrap();
216 let e = S::from_f64(2.287647662619474).unwrap();
217 let f = S::from_f64(97.795539177359).unwrap();
218 let g = S::from_f64(738.3773753517622).unwrap();
219 let z = S::sin(a * d + b * e + c * f) * g;
220 let d = S::from_f64(13.335891984218563).unwrap();
221 let e = S::from_f64(77.76549475370358).unwrap();
222 let f = S::from_f64(78.43332391527221).unwrap();
223 let g = S::from_f64(582.2181553770123).unwrap();
224 let w = S::sin(a * d + b * e + c * f) * g;
225 [
226 x - S::floor(x),
227 y - S::floor(y),
228 z - S::floor(z),
229 w - S::floor(w),
230 ]
231 }
232}
233
234impl<S: Float + FromPrimitive> HashGen<S> for [S; 4] {
235 fn hash1(v: [S; 4]) -> S {
236 let a = HashGen::hash1([v[1], v[2], v[3]]);
237 let b = HashGen::hash1([v[2], v[3], v[0]]);
238 let c = HashGen::hash1([v[3], v[0], v[1]]);
239 let d = HashGen::hash1([v[0], v[1], v[2]]);
240 let e = S::from_f64(45.48741506500266).unwrap();
241 let f = S::from_f64(61.983961049580714).unwrap();
242 let g = S::from_f64(71.68710748047283).unwrap();
243 let h = S::from_f64(17.018593362218127).unwrap();
244 let i = S::from_f64(233.18759725099315).unwrap();
245 let x = S::sin(a * e + b * f + c * g + d * h) * i;
246 x - S::floor(x)
247 }
248
249 fn hash2(v: [S; 4]) -> [S; 2] {
250 let a = HashGen::hash1([v[1], v[2], v[3]]);
251 let b = HashGen::hash1([v[2], v[3], v[0]]);
252 let c = HashGen::hash1([v[3], v[0], v[1]]);
253 let d = HashGen::hash1([v[0], v[1], v[2]]);
254 let e = S::from_f64(45.48741506500266).unwrap();
255 let f = S::from_f64(61.983961049580714).unwrap();
256 let g = S::from_f64(71.68710748047283).unwrap();
257 let h = S::from_f64(17.018593362218127).unwrap();
258 let i = S::from_f64(233.18759725099315).unwrap();
259 let x = S::sin(a * e + b * f + c * g + d * h) * i;
260 let e = S::from_f64(94.89956622422271).unwrap();
261 let f = S::from_f64(67.23354063041712).unwrap();
262 let g = S::from_f64(1.7039983839709838).unwrap();
263 let h = S::from_f64(32.280513717302526).unwrap();
264 let i = S::from_f64(939.8840745127328).unwrap();
265 let y = S::sin(a * e + b * f + c * g + d * h) * i;
266 [x - S::floor(x), y - S::floor(y)]
267 }
268
269 fn hash3(v: [S; 4]) -> [S; 3] {
270 let a = HashGen::hash1([v[1], v[2], v[3]]);
271 let b = HashGen::hash1([v[2], v[3], v[0]]);
272 let c = HashGen::hash1([v[3], v[0], v[1]]);
273 let d = HashGen::hash1([v[0], v[1], v[2]]);
274 let e = S::from_f64(45.48741506500266).unwrap();
275 let f = S::from_f64(61.983961049580714).unwrap();
276 let g = S::from_f64(71.68710748047283).unwrap();
277 let h = S::from_f64(17.018593362218127).unwrap();
278 let i = S::from_f64(233.18759725099315).unwrap();
279 let x = S::sin(a * e + b * f + c * g + d * h) * i;
280 let e = S::from_f64(94.89956622422271).unwrap();
281 let f = S::from_f64(67.23354063041712).unwrap();
282 let g = S::from_f64(1.7039983839709838).unwrap();
283 let h = S::from_f64(32.280513717302526).unwrap();
284 let i = S::from_f64(939.8840745127328).unwrap();
285 let y = S::sin(a * e + b * f + c * g + d * h) * i;
286 let e = S::from_f64(8.536774960497063).unwrap();
287 let f = S::from_f64(88.78943466077406).unwrap();
288 let g = S::from_f64(57.999707672724874).unwrap();
289 let h = S::from_f64(93.95289146038817).unwrap();
290 let i = S::from_f64(310.70624415491886).unwrap();
291 let z = S::sin(a * e + b * f + c * g + d * h) * i;
292 [x - S::floor(x), y - S::floor(y), z - S::floor(z)]
293 }
294
295 fn hash4(v: [S; 4]) -> [S; 4] {
296 let a = HashGen::hash1([v[1], v[2], v[3]]);
297 let b = HashGen::hash1([v[2], v[3], v[0]]);
298 let c = HashGen::hash1([v[3], v[0], v[1]]);
299 let d = HashGen::hash1([v[0], v[1], v[2]]);
300 let e = S::from_f64(45.48741506500266).unwrap();
301 let f = S::from_f64(61.983961049580714).unwrap();
302 let g = S::from_f64(71.68710748047283).unwrap();
303 let h = S::from_f64(17.018593362218127).unwrap();
304 let i = S::from_f64(233.18759725099315).unwrap();
305 let x = S::sin(a * e + b * f + c * g + d * h) * i;
306 let e = S::from_f64(94.89956622422271).unwrap();
307 let f = S::from_f64(67.23354063041712).unwrap();
308 let g = S::from_f64(1.7039983839709838).unwrap();
309 let h = S::from_f64(32.280513717302526).unwrap();
310 let i = S::from_f64(939.8840745127328).unwrap();
311 let y = S::sin(a * e + b * f + c * g + d * h) * i;
312 let e = S::from_f64(8.536774960497063).unwrap();
313 let f = S::from_f64(88.78943466077406).unwrap();
314 let g = S::from_f64(57.999707672724874).unwrap();
315 let h = S::from_f64(93.95289146038817).unwrap();
316 let i = S::from_f64(310.70624415491886).unwrap();
317 let z = S::sin(a * e + b * f + c * g + d * h) * i;
318 let e = S::from_f64(46.900144129289025).unwrap();
319 let f = S::from_f64(38.3329178217256).unwrap();
320 let g = S::from_f64(95.10984147120259).unwrap();
321 let h = S::from_f64(38.51337731313087).unwrap();
322 let i = S::from_f64(843.5063860117572).unwrap();
323 let w = S::sin(a * e + b * f + c * g + d * h) * i;
324 [
325 x - S::floor(x),
326 y - S::floor(y),
327 z - S::floor(z),
328 w - S::floor(w),
329 ]
330 }
331}
332
333impl<S: Float + FromPrimitive> HashGen<S> for [S; 1] {
334 fn hash1(gen: Self) -> S { S::hash1(gen[0]) }
335 fn hash2(gen: Self) -> [S; 2] { S::hash2(gen[0]) }
336 fn hash3(gen: Self) -> [S; 3] { S::hash3(gen[0]) }
337 fn hash4(gen: Self) -> [S; 4] { S::hash4(gen[0]) }
338}
339
340macro_rules! derive_hashgen {
341 ($from: ty, $into: ty) => {
342 impl<S: Float + FromPrimitive> HashGen<S> for $from {
343 fn hash1(gen: Self) -> S { <$into>::hash1(gen.into()) }
344 fn hash2(gen: Self) -> [S; 2] { <$into>::hash2(gen.into()) }
345 fn hash3(gen: Self) -> [S; 3] { <$into>::hash3(gen.into()) }
346 fn hash4(gen: Self) -> [S; 4] { <$into>::hash4(gen.into()) }
347 }
348 };
349}
350
351use cgmath::*;
352derive_hashgen!(Point1<S>, [S; 1]);
353derive_hashgen!(Point2<S>, [S; 2]);
354derive_hashgen!(Point3<S>, [S; 3]);
355derive_hashgen!(Vector1<S>, [S; 1]);
356derive_hashgen!(Vector2<S>, [S; 2]);
357derive_hashgen!(Vector3<S>, [S; 3]);
358derive_hashgen!(Vector4<S>, [S; 4]);
359
360pub fn take_one_unit<G: HashGen<f64>>(gen: G) -> Vector3<f64> {
362 let u = HashGen::hash2(gen);
363 let theta = 2.0 * std::f64::consts::PI * u[0];
364 let z = 2.0 * u[1] - 1.0;
365 let r = f64::sqrt(1.0 - z * z);
366 Vector3::new(r * f64::cos(theta), r * f64::sin(theta), z)
367}