source: libcfa/src/vec/vec2.hfa @ 9ec35db

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

Add vec3_float tests

  • Property mode set to 100644
File size: 4.5 KB
Line 
1#pragma once
2#include <math.hfa>
3#include <iostream.hfa>
4
5trait vec2_t(otype T) {
6    void ?{}(T&, int);
7    T ?=?(T&, zero_t);
8    T ?-?(T, T);
9    T -?(T);
10    T ?+?(T, T);
11    T ?*?(T, T);
12    T ?/?(T, T);
13    int ?==?(T, T);
14    int ?<?(T, T);
15    T sqrt(T);
16};
17
18static inline {
19// int
20int ?=?(int& n, zero_t) { return n = 0.f; }
21int sqrt(int a) { return sqrt((float)a); }
22/* float */
23void ?{}(float& a, int b) { a = b; }
24float ?=?(float& n, zero_t) { return n = 0.f; }
25/* double */
26void ?{}(double& a, int b) { a = b; }
27double ?=?(double& n, zero_t) { return n = 0L; }
28// long double
29void ?{}(long double& a, int b) { a = b; }
30long double ?=?(long double& n, zero_t) { return n = 0L; }
31}
32
33forall(otype T | vec2_t(T)) {
34    struct vec2 {
35        T x, y;
36    };
37}
38
39/* static inline { */
40forall(otype T | vec2_t(T)) {
41    static inline {
42
43    // Constructors
44
45    void ?{}(vec2(T)& v, T x, T y) {
46        v.[x, y] = [x, y];
47    }
48    void ?{}(vec2(T)& vec, zero_t) with (vec) {
49        x = y = 0;
50    }
51    void ?{}(vec2(T)& vec, T val) with (vec) {
52        x = y = val;
53    }
54    void ?{}(vec2(T)& vec, vec2(T) other) with (vec) {
55        [x,y] = other.[x,y];
56    }
57
58    // Assignment
59    void ?=?(vec2(T)& vec, vec2(T) other) with (vec) {
60        [x,y] = other.[x,y];
61    }
62    void ?=?(vec2(T)& vec, zero_t) with (vec) {
63        x = y = 0;
64    }
65
66    // Primitive mathematical operations
67
68    // Subtraction
69    vec2(T) ?-?(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref )
70        return [u.x - v.x, u.y - v.y];
71    }
72    vec2(T)& ?-=?(vec2(T)& u, vec2(T) v) {
73        u = u - v;
74        return u;
75    }
76    vec2(T) -?(vec2(T)& v) with (v) {
77        return [-x, -y];
78    }
79
80    // Addition
81    vec2(T) ?+?(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref )
82        return [u.x + v.x, u.y + v.y];
83    }
84    vec2(T)& ?+=?(vec2(T)& u, vec2(T) v) {
85        u = u + v;
86        return u;
87    }
88
89    // Scalar Multiplication
90    vec2(T) ?*?(vec2(T) v, T scalar) with (v) { // TODO (can't make this const ref)
91        return [x * scalar, y * scalar];
92    }
93    vec2(T) ?*?(T scalar, vec2(T) v) { // TODO (can't make this const ref)
94        return v * scalar;
95    }
96    vec2(T)& ?*=?(vec2(T)& v, T scalar) {
97        v = v * scalar;
98        return v;
99    }
100
101
102    // Scalar Division
103    vec2(T) ?/?(vec2(T) v, T scalar) with (v) {
104        return [x / scalar, y / scalar];
105    }
106    vec2(T)& ?/=?(vec2(T)& v, T scalar) with (v) {
107        v = v / scalar;
108        return v;
109    }
110    // Relational Operators
111    bool ?==?(vec2(T) u, vec2(T) v) with (u) {
112        return x == v.x && y == v.y;
113    }
114    bool ?!=?(vec2(T) u, vec2(T) v) {
115        return !(u == v);
116    }
117
118    T dot(vec2(T) u, vec2(T) v) {
119        return u.x * v.x + u.y * v.y;
120    }
121
122    T length(vec2(T) v) {
123       return sqrt(dot(v, v));
124    }
125
126    T length_squared(vec2(T) v) {
127       return dot(v, v);
128    }
129
130    T distance(vec2(T) v1, vec2(T) v2) {
131        return length(v1 - v2);
132    }
133
134    vec2(T) normalize(vec2(T) v) {
135        return v / sqrt(dot(v, v));
136    }
137
138    // Project vector u onto vector v
139    vec2(T) project(vec2(T) u, vec2(T) v) {
140        vec2(T) v_norm = normalize(v);
141        return v_norm * dot(u, v_norm);
142    }
143
144    // Reflect incident vector v with respect to surface with normal n
145    vec2(T) reflect(vec2(T) v, vec2(T) n) {
146        return v - (T){2} * project(v, n);
147    }
148
149    // Refract incident vector v with respect to surface with normal n
150    // eta is the ratio of indices of refraction between starting material and
151    // entering material (i.e., from air to water, eta = 1/1.33)
152    // v and n must already be normalized
153    vec2(T) refract(vec2(T) v, vec2(T) n, T eta) {
154        T dotValue = dot(n, v);
155        T k = (T){1} - eta * eta * ((T){1} - dotValue * dotValue);
156        if (k < (T){0}) {
157            return 0;
158        }
159        return eta * v - (eta * dotValue + sqrt(k)) * n;
160    }
161
162    // Given a perturbed normal and a geometric normal,
163    // flip the perturbed normal if the geometric normal is pointing away
164    // from the observer.
165    // n is the perturbed vector that we want to align
166    // i is the incident vector
167    // ng is the geometric normal of the surface
168    vec2(T) faceforward(vec2(T) n, vec2(T) i, vec2(T) ng) {
169        return dot(ng, i) < (T){0} ? n : -n;
170    }
171    }
172}
173
174forall(dtype ostype, otype T | writeable(T, ostype) | vec2_t(T)) {
175    ostype & ?|?( ostype & os, vec2(T) v) with (v) {
176        return os | '<' | x | ',' | y | '>';
177    }
178    void ?|?( ostype & os, vec2(T) v ) with (v) {
179        (ostype &)(os | v); ends(os);
180    }
181}
Note: See TracBrowser for help on using the repository browser.