# source:libcfa/src/vector.hfa@0856a96

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

• Property mode set to `100644`
File size: 3.5 KB
Line
1#pragma once
2#include <math.hfa>
3#include <iostream.hfa>
4
5struct vec2 {
6    float x, y;
7};
8
9static inline {
10
11// Constructors
12
13void ?{}(vec2& v, float x, float y) {
14    v.[x, y] = [x, y];
15}
16void ?{}(vec2& vec, zero_t) with (vec) {
17    x = y = 0;
18}
19void ?{}(vec2& vec, float val) with (vec) {
20    x = y = val;
21}
22void ?{}(vec2& vec, const vec2& other) with (vec) {
23    [x,y] = other.[x,y];
24}
25
26// Assignment
27void ?=?(vec2& vec, const vec2& other) with (vec) {
28    [x,y] = other.[x,y];
29}
30void ?=?(vec2& vec, zero_t) with (vec) {
31    [x,y] = [0,0];
32}
33
34// Primitive mathematical operations
35
36// Subtraction
37vec2 ?-?(const vec2& u, const vec2& v) {
38    return [u.x - v.x, u.y - v.y];
39}
40vec2& ?-=?(vec2& u, const vec2& v) {
41    u = u - v;
42    return u;
43}
44vec2 -?(const vec2& v) with (v) {
45    return [-x, -y];
46}
47
49vec2 ?+?(const vec2& u, const vec2& v) {
50    return [u.x + v.x, u.y + v.y];
51}
52vec2& ?+=?(vec2& u, const vec2& v) {
53    u = u + v;
54    return u;
55}
56
57// Scalar Multiplication
58vec2 ?*?(const vec2& v, float scalar) with (v) {
59    return [x * scalar, y * scalar];
60}
61vec2 ?*?(float scalar, const vec2& v) {
62    return v * scalar;
63}
64vec2& ?*=?(vec2& v, float scalar) with (v) {
65    v = v * scalar;
66    return v;
67}
68
69
70// Scalar Division
71vec2 ?/?(const vec2& v, float scalar) with (v) {
72    return [x / scalar, y / scalar];
73}
74vec2& ?/=?(vec2& v, float scalar) with (v) {
75    v = v / scalar;
76    return v;
77}
78
79// Relational Operators
80bool ?==?(const vec2& u, const vec2& v) with (u) {
81    return x == v.x && y == v.y;
82}
83bool ?!=?(const vec2& u, const vec2& v) {
84    return !(u == v);
85}
86
87// Printing the vector (ostream)
88forall( dtype ostype | ostream( ostype ) ) {
89    ostype & ?|?( ostype & os, const vec2& 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& 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& u, const vec2& v) {
103    return u.x * v.x + u.y * v.y;
104}
105
106float length(const vec2& v) {
107   return sqrt(dot(v, v));
108}
109
110float length_squared(const vec2& v) {
111   return dot(v, v);
112}
113
114float distance(const vec2& v1, const vec2& v2) {
115    return length(v1 - v2);
116}
117
118vec2 normalize(const vec2& v) {
119    return v / sqrt(dot(v, v));
120}
121
122// Project vector u onto vector v
123vec2 project(const vec2& u, const vec2& v) {
124    vec2 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 reflect(const vec2& v, const vec2& 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 refract(const vec2& v, const vec2& 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 faceforward(const vec2& n, const vec2& i, const vec2& ng) {
152    return dot(ng, i) < 0 ? n : -n;
153}
154
155}
Note: See TracBrowser for help on using the repository browser.