1use crate::prelude::*;
2
3use colors_transform::Color;
4use rhai::{Engine, EvalAltResult};
5
6use std::ops::Add;
7use std::ops::Sub;
8use std::ops::Mul;
9use std::ops::Div;
10use std::ops::AddAssign;
11use std::ops::SubAssign;
12use std::ops::Neg;
13
14use std::iter::once;
15use rhai::FuncArgs;
16
17#[derive(PartialEq, Debug, Copy, Clone)]
19pub struct F2 {
20 pub x : F,
21 pub y : F,
22}
23
24impl F2 {
25
26 pub fn from(v: F2) -> Self {
27 Self {
28 x : v.x,
29 y : v.y,
30 }
31 }
32
33 pub fn zeros() -> Self {
34 Self {
35 x : 0.0,
36 y : 0.0,
37 }
38 }
39
40 pub fn new_x(x: F) -> Self {
41 Self {
42 x : x,
43 y : x,
44 }
45 }
46
47 pub fn new(x: F, y: F) -> Self {
48 Self {
49 x,
50 y,
51 }
52 }
53
54 pub fn get_x(&mut self) -> F {
55 self.x
56 }
57
58 pub fn set_x(&mut self, new_val: F) {
59 self.x = new_val;
60 }
61
62 pub fn get_y(&mut self) -> F {
63 self.y
64 }
65
66 pub fn set_y(&mut self, new_val: F) {
67 self.y = new_val;
68 }
69
70 pub fn copy(&mut self) -> F2 {
72 self.clone()
73 }
74
75 pub fn normalize(&mut self) {
77 let l = self.length();
78 self.x /= l;
79 self.y /= l;
80 }
81
82 pub fn length(&self) -> F {
84 (self.x * self.x + self.y * self.y).sqrt()
85 }
86
87 pub fn abs(&self) -> F2 {
89 F2::new(self.x.abs(), self.y.abs())
90 }
91
92 pub fn dot(&self, other: &F2) -> F {
93 self.x * other.x + self.y * other.y
94 }
95
96 pub fn mult_f(&self, other: &F) -> F2 {
97 F2::new(self.x * other,
98 self.y * other
99 )
100 }
101
102 pub fn max_f(&self, other: &F) -> F2 {
103 F2::new(self.x.max(*other), self.y.max(*other))
104 }
105
106 pub fn xyy(&self) -> F3 {
108 F3::new(self.x, self.y, self.y)
109 }
110
111 pub fn yyx(&self) -> F3 {
112 F3::new(self.y, self.y, self.x)
113 }
114
115 pub fn yxy(&self) -> F3 {
116 F3::new(self.y, self.x, self.y)
117 }
118
119 pub fn xxx(&self) -> F3 {
120 F3::new(self.x, self.x, self.x)
121 }
122
123 pub fn register(engine: &mut Engine) {
125 engine.register_type_with_name::<F2>("F2")
126 .register_fn("F2", F2::zeros)
127 .register_fn("F2", F2::new)
128 .register_fn("F2", F3::from)
129 .register_fn("normalize", F2::normalize)
130 .register_fn("length", F2::length)
131 .register_fn("copy", F2::clone)
132 .register_get_set("x", F2::get_x, F2::set_x)
133 .register_get_set("y", F2::get_y, F2::set_y);
134
135 engine.register_fn("+", |a: F2, b: F2| -> F2 {
136 F2::new(a.x + b.x, a.y + b.y)
137 });
138
139 engine.register_fn("-", |a: F2, b: F2| -> F2 {
140 F2::new(a.x - b.x, a.y - b.y)
141 });
142
143 engine.register_fn("*", |a: F2, b: F2| -> F2 {
144 F2::new(a.x * b.x, a.y * b.y)
145 });
146
147 engine.register_fn("*", |a: F, b: F2| -> F2 {
148 F2::new(a * b.x, a * b.y)
149 });
150
151 engine.register_fn("*", |a: F2, b: F| -> F2 {
152 F2::new(a.x * b, a.y * b)
153 });
154
155 engine.register_fn("/", |a: F2, b: F2| -> F2 {
156 F2::new(a.x / b.x, a.y / b.y)
157 });
158
159 engine.register_fn("/", |a: F, b: F2| -> F2 {
160 F2::new(a / b.x, a / b.y)
161 });
162
163 engine.register_fn("/", |a: F2, b: F| -> F2 {
164 F2::new(a.x / b, a.y / b)
165 });
166 }
167}
168
169impl FuncArgs for F2 {
170 fn parse<C: Extend<rhai::Dynamic>>(self, container: &mut C) {
171 container.extend(once(rhai::Dynamic::from(self)));
172 }
173}
174
175impl Add for F2 {
176 type Output = F2;
177
178 fn add(self, other: F2) -> F2 {
179 F2::new( self.x + other.x, self.y + other.y)
180 }
181}
182
183impl Sub for F2 {
184 type Output = F2;
185
186 fn sub(self, other: F2) -> F2 {
187 F2::new( self.x - other.x, self.y - other.y )
188 }
189}
190
191impl Mul for F2 {
192 type Output = F2;
193
194 fn mul(self, other: F2) -> F2 {
195 F2::new( self.x * other.x, self.y * other.y )
196 }
197}
198
199impl Div for F2 {
200 type Output = F2;
201
202 fn div(self, other: F2) -> F2 {
203 F2::new( self.x / other.x, self.y / other.y )
204 }
205}
206
207#[derive(PartialEq, Debug, Copy, Clone)]
209pub struct F3 {
210 pub x : F,
211 pub y : F,
212 pub z : F,
213}
214
215impl F3 {
216
217 pub fn from(v: F3) -> Self {
218 Self {
219 x : v.x,
220 y : v.y,
221 z : v.z,
222 }
223 }
224
225 pub fn zeros() -> Self {
226 Self {
227 x : 0.0,
228 y : 0.0,
229 z : 0.0,
230 }
231 }
232
233 pub fn new_x(x: F) -> Self {
234 Self {
235 x : x,
236 y : x,
237 z : x,
238 }
239 }
240
241 pub fn new(x: F, y: F, z: F) -> Self {
242 Self {
243 x : x,
244 y : y,
245 z : z,
246 }
247 }
248
249 pub fn color(mut color: String) -> Self {
250
251 if color.starts_with('#') {
252 let mut chars = color.chars();
254 chars.next();
255 color = chars.as_str().to_string();
256 }
257
258 use colors_transform::{Rgb};
259
260 let mut x = 0.0;
261 let mut y = 0.0;
262 let mut z = 0.0;
263
264 if let Some(rgb) = Rgb::from_hex_str(color.as_str()).ok() {
265 x = rgb.get_red() as F / 255.0;
266 y = rgb.get_green() as F / 255.0;
267 z = rgb.get_blue() as F / 255.0;
268 }
269
270 Self {
271 x,
272 y,
273 z
274 }
275 }
276
277 pub fn get_x(&mut self) -> F {
278 self.x
279 }
280
281 pub fn set_x(&mut self, new_val: F) {
282 self.x = new_val;
283 }
284
285 pub fn get_y(&mut self) -> F {
286 self.y
287 }
288
289 pub fn set_y(&mut self, new_val: F) {
290 self.y = new_val;
291 }
292
293 pub fn get_z(&mut self) -> F {
294 self.z
295 }
296
297 pub fn set_z(&mut self, new_val: F) {
298 self.z = new_val;
299 }
300
301 pub fn copy(&mut self) -> F3 {
303 self.clone()
304 }
305
306 pub fn normalize(&self) -> F3 {
308 let l = self.length();
309 F3::new(
310 self.x / l,
311 self.y / l,
312 self.z / l)
313 }
314
315 pub fn abs(&self) -> F3 {
317 F3::new(self.x.abs(), self.y.abs(), self.z.abs())
318 }
319
320 pub fn floor(&self) -> F3 {
322 F3::new(self.x.floor(), self.y.floor(), self.z.floor())
323 }
324
325 pub fn fract(&self) -> F3 {
327 F3::new(self.x.fract(), self.y.fract(), self.z.fract())
328 }
329
330 pub fn length(&self) -> F {
332 (self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
333 }
334
335 pub fn dot(&self, other: &F3) -> F {
336 self.x * other.x + self.y * other.y + self.z * other.z
337 }
338
339 pub fn cross(&self, other: &F3) -> F3 {
340 F3::new(self.y * other.z - self.z * other.y,
341 self.z * other.x - self.x * other.z,
342 self.x * other.y - self.y * other.x
343 )
344 }
345
346 pub fn mult_f(&self, other: &F) -> F3 {
347 F3::new(self.x * other,
348 self.y * other,
349 self.z * other
350 )
351 }
352
353 pub fn div_f(&self, other: &F) -> F3 {
354 F3::new(self.x / other,
355 self.y / other,
356 self.z / other
357 )
358 }
359
360 pub fn max_f(&self, other: &F) -> F3 {
361 F3::new(self.x.max(*other), self.y.max(*other), self.z.max(*other))
362 }
363
364 pub fn to_linear(&mut self) -> F3 {
365 F3::new(self.x.powf(2.2), self.y.powf(2.2), self.z.powf(2.2))
366 }
367
368 pub fn to_gamma(&mut self) -> F3 {
369 F3::new(self.x.powf(1.0/2.2), self.y.powf(1.0/2.2), self.z.powf(1.0/2.2))
370 }
371
372 pub fn register(engine: &mut Engine) {
374 engine.register_type_with_name::<F3>("F3")
375 .register_fn("F3", F3::zeros)
376 .register_fn("F3", F3::new)
377 .register_fn("F3", F3::new_x)
378 .register_fn("F3", F3::from)
379 .register_fn("F3", F3::color)
380 .register_fn("normalize", F3::normalize)
381 .register_fn("floor", F3::floor)
382 .register_fn("fract", F3::fract)
383 .register_fn("length", F3::length)
384 .register_fn("copy", F3::clone)
385 .register_fn("to_linear", F3::to_linear)
386 .register_fn("to_gamma", F3::to_gamma)
387 .register_get_set("x", F3::get_x, F3::set_x)
388 .register_get_set("y", F3::get_y, F3::set_y)
389 .register_get_set("z", F3::get_z, F3::set_z);
390
391 engine.register_fn("+", |a: F3, b: F3| -> F3 {
392 F3::new(a.x + b.x, a.y + b.y, a.z + b.z)
393 });
394
395 engine.register_fn("-", |a: F3, b: F3| -> F3 {
396 F3::new(a.x - b.x, a.y - b.y, a.z - b.z)
397 });
398
399 engine.register_fn("*", |a: F3, b: F3| -> F3 {
400 F3::new(a.x * b.x, a.y * b.y, a.z * b.z)
401 });
402
403 engine.register_fn("*", |a: F, b: F3| -> F3 {
404 F3::new(a * b.x, a * b.y, a * b.z)
405 });
406
407 engine.register_fn("*", |a: F3, b: F| -> F3 {
408 F3::new(a.x * b, a.y * b, a.z * b)
409 });
410
411 engine.register_indexer_get(|o: &mut F3, prop: &str| -> Result<F2, Box<EvalAltResult>> {
413 match prop {
414 "xz" => {
415 Ok(F2::new(o.x, o.z))
416 },
417 _ => {
418 Err("F3: Property not found".into())
419 }
420 }
421 });
422 }
435}
436
437impl Add for F3 {
438 type Output = F3;
439
440 fn add(self, other: F3) -> F3 {
441 F3::new( self.x + other.x, self.y + other.y, self.z + other.z )
442 }
443}
444
445impl AddAssign for F3 {
446 fn add_assign(&mut self, other: F3) {
447 self.x += other.x;
448 self.y += other.y;
449 self.z += other.z;
450 }
451}
452
453impl Sub for F3 {
454 type Output = F3;
455
456 fn sub(self, other: F3) -> F3 {
457 F3::new( self.x - other.x, self.y - other.y, self.z - other.z )
458 }
459}
460
461impl SubAssign for F3 {
462 fn sub_assign(&mut self, other: F3) {
463 self.x -= other.x;
464 self.y -= other.y;
465 self.z -= other.z;
466 }
467}
468
469impl Mul for F3 {
470 type Output = F3;
471
472 fn mul(self, other: F3) -> F3 {
473 F3::new( self.x * other.x, self.y * other.y, self.z * other.z )
474 }
475}
476
477impl std::ops::Mul<F3> for f32 {
478 type Output = F3;
479
480 fn mul(self, other: F3) -> F3 {
481 F3::new(self * other.x, self * other.y, self * other.z)
482 }
483}
484
485impl Div for F3 {
486 type Output = F3;
487
488 fn div(self, other: F3) -> F3 {
489 F3::new( self.x / other.x, self.y / other.y, self.z / other.z )
490 }
491}
492
493impl std::ops::DivAssign for F3 {
494 fn div_assign(&mut self, other: F3) {
495 self.x /= other.x;
496 self.y /= other.y;
497 self.z /= other.z;
498 }
499}
500
501impl Div<F3> for f32 {
502 type Output = F3;
503
504 fn div(self, other: F3) -> F3 {
505 F3::new(self / other.x, self / other.y, self / other.z)
506 }
507}
508
509impl Neg for F3 {
510 type Output = Self;
511
512 fn neg(self) -> Self::Output {
513 F3::new(-self.x, -self.y, -self.z)
514 }
515}
516
517#[derive(PartialEq, Debug, Copy, Clone)]
519pub struct B3 {
520 pub x : bool,
521 pub y : bool,
522 pub z : bool,
523}
524
525impl B3 {
526
527 pub fn from(v: B3) -> Self {
528 Self {
529 x : v.x,
530 y : v.y,
531 z : v.z,
532 }
533 }
534
535 pub fn falsed() -> Self {
536 Self {
537 x : false,
538 y : false,
539 z : false,
540 }
541 }
542
543 pub fn new_x(x: bool) -> Self {
544 Self {
545 x : x,
546 y : x,
547 z : x,
548 }
549 }
550
551 pub fn new(x: bool, y: bool, z: bool) -> Self {
552 Self {
553 x : x,
554 y : y,
555 z : z,
556 }
557 }
558
559 pub fn get_x(&mut self) -> bool {
560 self.x
561 }
562
563 pub fn set_x(&mut self, new_val: bool) {
564 self.x = new_val;
565 }
566
567 pub fn get_y(&mut self) -> bool {
568 self.y
569 }
570
571 pub fn set_y(&mut self, new_val: bool) {
572 self.y = new_val;
573 }
574
575 pub fn get_z(&mut self) -> bool {
576 self.z
577 }
578
579 pub fn set_z(&mut self, new_val: bool) {
580 self.z = new_val;
581 }
582
583 pub fn register(engine: &mut Engine) {
585 engine.register_type_with_name::<B3>("B3")
586 .register_fn("B3", B3::falsed)
587 .register_fn("B3", B3::new)
588 .register_fn("B3", B3::from)
589 .register_get_set("x", B3::get_x, B3::set_x)
590 .register_get_set("y", B3::get_y, B3::set_y)
591 .register_get_set("z", B3::get_z, B3::set_z);
592 }
593}