source: libcfa/src/vector.hfa @ b545cad

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since b545cad was b545cad, checked in by Dmitry Kobets <dkobets@…>, 5 years ago

Added refraction

  • Property mode set to 100644
File size: 2.5 KB
Line 
1#pragma once
2#include <math.hfa>
3#include <iostream.hfa>
4
5//---------------------- Vector Types ----------------------
6// TODO: make generic, as per glm
7
8struct vec2 {
9    float x, y;
10};
11
12void ?{}( vec2 & v, float x, float y) {
13    v.[x, y] = [x, y];
14}
15
16forall( dtype ostype | ostream( ostype ) ) {
17    ostype & ?|?( ostype & os, const vec2& v) with (v) {
18        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
19        fmt( os, "<%g,%g>", x, y);
20        return os;
21    }
22    void ?|?( ostype & os, const vec2& v ) {
23        (ostype &)(os | v); ends( os );
24    }
25}
26
27void ?{}(vec2& vec, zero_t) with (vec) {
28    x = y = 0;
29}
30void ?{}(vec2& vec, vec2& other) with (vec) {
31    [x,y] = other.[x,y];
32}
33
34vec2 ?-?(const vec2& u, const vec2& v) {
35    return [u.x - v.x, u.y - v.y];
36}
37vec2 ?*?(const vec2& v, float scalar) with (v) {
38    return [x * scalar, y * scalar];
39}
40vec2 ?*?(float scalar, const vec2& v) {
41    return v * scalar;
42}
43vec2 ?/?(const vec2& v, float scalar) with (v) {
44    return [x / scalar, y / scalar];
45}
46
47/* //---------------------- Geometric Functions ---------------------- */
48/* // These functions implement the Geometric Functions section of GLSL */
49
50static inline float dot(const vec2& u, const vec2& v) {
51    return u.x * v.x + u.y * v.y;
52}
53
54static inline float length(const vec2& v) {
55   return sqrt(dot(v, v));
56}
57
58// Returns the distance betwwen v1 and v2, i.e., length(p0 - p1).
59static inline float distance(const vec2& v1, const vec2& v2) {
60    return length(v1 - v2);
61}
62
63static inline vec2 normalize(const vec2& v) {
64    // TODO(dkobets) -- show them inversesqrt
65    // https://github.com/g-truc/glm/blob/269ae641283426f7f84116f2fe333472b9c914c9/glm/detail/func_exponential.inl
66    /* return v * inversesqrt(dot(v, v)); */
67    return v / sqrt(dot(v, v));
68}
69
70// project vector u onto vector v
71static inline vec2 project(const vec2& u, const vec2& v) {
72    vec2 v_norm = normalize(v);
73    return v_norm * dot(u, v_norm);
74}
75
76/* returns the reflection direction : v - 2.0 * project(v, n)
77 * for incident vector v and surface normal n
78 */
79static inline vec2 reflect(const vec2& v, const vec2& n) {
80    return v - 2 * project(v, n);
81}
82
83// incident vector v, surface normal n
84// eta = ratio of indices of refraction between starting material and
85// entering material (i.e., from air to water, eta = 1/1.33)
86static inline vec2 refract(const vec2& v, const vec2& n, float eta) {
87    float dotValue = dot(n, v);
88    float k = 1 - eta * eta * (1 - dotValue * dotValue);
89    if (k < 0) {
90        return 0;
91    }
92    return eta * v - (eta * dotValue + sqrt(k)) * n;
93}
Note: See TracBrowser for help on using the repository browser.