source: libcfa/src/vector.hfa@ 44f41997

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 44f41997 was 44f41997, checked in by Dmitry Kobets <dkobets@…>, 6 years ago

Add vector normalization

  • Property mode set to 100644
File size: 1.6 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
8
9struct vec2 {
10 float x, y;
11};
12
13void ?{}( vec2 & v, float x, float y) {
14 v.[x, y] = [x, y];
15}
16
17forall( dtype ostype | ostream( ostype ) ) {
18 ostype & ?|?( ostype & os, const vec2& v) with (v) {
19 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
20 fmt( os, "<%g,%g>", x, y);
21 return os;
22 }
23 void ?|?( ostype & os, const vec2 v ) {
24 (ostype &)(os | v); ends( os );
25 }
26}
27
28vec2 ?-?(const vec2& u, const vec2& v) {
29 return [u.x - v.x, u.y - v.y];
30}
31vec2 ?*?(const vec2& v, float scalar) with (v) {
32 return [x * scalar, y * scalar];
33}
34vec2 ?/?(const vec2& v, float scalar) with (v) {
35 return [x / scalar, y / scalar];
36}
37
38/* //---------------------- Geometric Functions ---------------------- */
39/* // These functions implement the Geometric Functions section of GLSL */
40
41static inline float dot(const vec2& u, const vec2& v) {
42 return u.x * v.x + u.y * v.y;
43}
44
45static inline float length(const vec2& v) {
46 return sqrt(dot(v, v));
47}
48
49// Returns the distance betwwen v1 and v2, i.e., length(p0 - p1).
50static inline float distance(const vec2& v1, const vec2& v2) {
51 return length(v1 - v2);
52}
53
54static inline vec2 normalize(const vec2& v) {
55 // TODO(dkobets) -- show them inversesqrt
56 // https://github.com/g-truc/glm/blob/269ae641283426f7f84116f2fe333472b9c914c9/glm/detail/func_exponential.inl
57 /* return v * inversesqrt(dot(v, v)); */
58 return v / sqrt(dot(v, v));
59}
Note: See TracBrowser for help on using the repository browser.