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

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since 9ec35db was 9ec35db, checked in by Dmitry Kobets <dkobets@…>, 2 years ago

Add vec3_float tests

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