source: libcfa/src/vec/vec.hfa @ ae3db00

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

Add support for unsigned integer vectors + tests

  • Property mode set to 100644
File size: 3.0 KB
Line 
1#pragma once
2
3#include <math.hfa>
4
5trait fromint(otype T) {
6    void ?{}(T&, int);
7};
8trait zeroinit(otype T) {
9    void ?{}(T&, zero_t);
10};
11trait zero_assign(otype T) {
12    T ?=?(T&, zero_t);
13};
14trait subtract(otype T) {
15    T ?-?(T, T);
16};
17trait negate(otype T) {
18    T -?(T);
19};
20trait add(otype T) {
21    T ?+?(T, T);
22};
23trait multiply(otype T) {
24    T ?*?(T, T);
25};
26trait divide(otype T) {
27    T ?/?(T, T);
28};
29trait lessthan(otype T) {
30    int ?<?(T, T);
31};
32trait equality(otype T) {
33    int ?==?(T, T);
34};
35trait sqrt(otype T) {
36    T sqrt(T);
37};
38
39static inline {
40// int
41int ?=?(int& n, zero_t) { return n = 0.f; }
42// unsigned int
43int ?=?(unsigned int& n, zero_t) { return n = 0.f; }
44/* float */
45void ?{}(float& a, int b) { a = b; }
46float ?=?(float& n, zero_t) { return n = 0.f; }
47/* double */
48void ?{}(double& a, int b) { a = b; }
49double ?=?(double& n, zero_t) { return n = 0L; }
50// long double
51void ?{}(long double& a, int b) { a = b; }
52long double ?=?(long double& n, zero_t) { return n = 0L; }
53}
54
55trait dottable(otype V, otype T) {
56    T dot(V, V);
57};
58
59static inline {
60
61forall(otype T | sqrt(T), otype V | dottable(V, T))
62T length(V v) {
63   return sqrt(dot(v, v));
64}
65
66forall(otype T, otype V | dottable(V, T))
67T length_squared(V v) {
68   return dot(v, v);
69}
70
71forall(otype T, otype V | { T length(V); } | subtract(V))
72T distance(V v1, V v2) {
73    return length(v1 - v2);
74}
75
76
77forall(otype T, otype V | { T length(V); V ?/?(V, T); })
78V normalize(V v) {
79    return v / length(v);
80}
81
82// Project vector u onto vector v
83forall(otype T, otype V | dottable(V, T) | { V normalize(V); V ?*?(V, T); })
84V project(V u, V v) {
85    V v_norm = normalize(v);
86    return v_norm * dot(u, v_norm);
87}
88
89// Reflect incident vector v with respect to surface with normal n
90forall(otype T | fromint(T), otype V | { V project(V, V); V ?*?(T, V); V ?-?(V,V); })
91V reflect(V v, V n) {
92    return v - (T){2} * project(v, n);
93}
94
95// Refract incident vector v with respect to surface with normal n
96// eta is the ratio of indices of refraction between starting material and
97// entering material (i.e., from air to water, eta = 1/1.33)
98// v and n must already be normalized
99forall(otype T | fromint(T) | subtract(T) | multiply(T) | add(T) | lessthan(T) | sqrt(T),
100       otype V | dottable(V, T) | { V ?*?(T, V); V ?-?(V,V); void ?{}(V&, zero_t); })
101V refract(V v, V n, T eta) {
102    T dotValue = dot(n, v);
103    T k = (T){1} - eta * eta * ((T){1} - dotValue * dotValue);
104    if (k < (T){0}) {
105        return 0;
106    }
107    return eta * v - (eta * dotValue + sqrt(k)) * n;
108}
109
110// Given a perturbed normal and a geometric normal,
111// flip the perturbed normal if the geometric normal is pointing away
112// from the observer.
113// n is the perturbed vector that we want to align
114// i is the incident vector
115// ng is the geometric normal of the surface
116/* forall(| add(T) | multiply(T) | lessthan(T) | fromint(T) | subtract(T)) */
117forall(otype T | lessthan(T) | zeroinit(T), otype V | dottable(V, T) | negate(V))
118V faceforward(V n, V i, V ng) {
119    return dot(ng, i) < (T){0} ? n : -n;
120}
121
122} // inline
Note: See TracBrowser for help on using the repository browser.