BSPParser
Simple and modern library for parsing the Valve BSP format
Loading...
Searching...
No Matches
vector-maths.hpp
1#pragma once
2
3#include "../structs/common.hpp"
4#include <cmath>
5
6namespace BspParser::Internal {
7 inline Structs::Vector xyz(const Structs::Vector4& v) {
8 return {.x = v.x, .y = v.y, .z = v.z};
9 }
10
11 inline Structs::Vector2 add(const Structs::Vector2& a, const Structs::Vector2& b) {
12 return Structs::Vector2{
13 .x = a.x + b.x,
14 .y = a.y + b.y,
15 };
16 }
17
18 template <typename... T>
19 Structs::Vector add(const T&... args)
20 requires(... && std::same_as<Structs::Vector, T>)
21 {
22 return Structs::Vector{
23 .x = (args.x + ...),
24 .y = (args.y + ...),
25 .z = (args.z + ...),
26 };
27 }
28
29 inline Structs::Vector2 sub(const Structs::Vector2& a, const Structs::Vector2& b) {
30 return Structs::Vector2{
31 .x = a.x - b.x,
32 .y = a.y - b.y,
33 };
34 }
35
36 inline Structs::Vector sub(const Structs::Vector& a, const Structs::Vector& b) {
37 return Structs::Vector{
38 .x = a.x - b.x,
39 .y = a.y - b.y,
40 .z = a.z - b.z,
41 };
42 }
43
44 inline Structs::Vector2 mul(const Structs::Vector2& a, const float b) {
45 return Structs::Vector2{
46 .x = a.x * b,
47 .y = a.y * b,
48 };
49 }
50
51 inline Structs::Vector mul(const Structs::Vector& a, const float b) {
52 return Structs::Vector{
53 .x = a.x * b,
54 .y = a.y * b,
55 .z = a.z * b,
56 };
57 }
58
59 inline Structs::Vector div(const Structs::Vector& a, const float b) {
60 return Structs::Vector{
61 .x = a.x / b,
62 .y = a.y / b,
63 .z = a.z / b,
64 };
65 }
66
67 inline float dot(const Structs::Vector& a, const Structs::Vector& b) {
68 return a.x * b.x + a.y * b.y + a.z * b.z;
69 }
70
71 inline Structs::Vector cross(const Structs::Vector& a, const Structs::Vector& b) {
72 return Structs::Vector{
73 .x = a.y * b.z - a.z * b.y,
74 .y = a.z * b.x - a.x * b.z,
75 .z = a.x * b.y - a.y * b.x,
76 };
77 }
78
79 inline float length(const Structs::Vector& v) {
80 return std::sqrt(dot(v, v));
81 }
82
83 inline Structs::Vector normalise(const Structs::Vector& v) {
84 return div(v, length(v));
85 }
86
87 inline Structs::Vector lerp(const Structs::Vector& a, const Structs::Vector& b, const float t) {
88 return Structs::Vector{
89 .x = a.x + (b.x - a.x) * t,
90 .y = a.y + (b.y - a.y) * t,
91 .z = a.z + (b.z - a.z) * t,
92 };
93 }
94}