Arty
float3.h
1 #ifndef FLOAT3_H
2 #define FLOAT3_H
3 
4 #include <cmath>
5 #include "common.h"
6 #include "float2.h"
7 
8 struct float4;
9 
10 struct float3 {
11  union {
12  struct { float x, y, z; };
13  float values[3];
14  };
15 
16  float3() {}
17  explicit float3(float x) : x(x), y(x), z(x) {}
18  explicit float3(const float4& xyz);
19  float3(float x, float y, float z) : x(x), y(y), z(z) {}
20  float3(const float2& xy, float z) : x(xy.x), y(xy.y), z(z) {}
21  float3(float x, const float2& yz) : x(x), y(yz.x), z(yz.y) {}
22 
23  bool operator == (const float3& other) const {
24  return x == other.x && y == other.y && z == other.z;
25  }
26 
27  bool operator != (const float3& other) const {
28  return x != other.x || y != other.y || z != other.z;
29  }
30 
31  float operator [] (size_t i) const { return values[i]; }
32  float& operator [] (size_t i) { return values[i]; }
33 
34  float3& operator += (const float3& a) {
35  x += a.x; y += a.y; z += a.z;
36  return *this;
37  }
38 
39  float3& operator -= (const float3& a) {
40  x -= a.x; y -= a.y; z -= a.z;
41  return *this;
42  }
43 
44  float3& operator *= (float a) {
45  x *= a; y *= a; z *= a;
46  return *this;
47  }
48 
49  float3& operator *= (const float3& a) {
50  x *= a.x; y *= a.y; z *= a.z;
51  return *this;
52  }
53 };
54 
55 inline float2::float2(const float3& xy)
56  : x(xy.x), y(xy.y)
57 {}
58 
59 inline float3 operator * (float a, const float3& b) {
60  return float3(a * b.x, a * b.y, a * b.z);
61 }
62 
63 inline float3 operator * (const float3& a, float b) {
64  return float3(a.x * b, a.y * b, a.z * b);
65 }
66 
67 inline float3 operator / (const float3& a, float b) {
68  return a * (1.0f / b);
69 }
70 
71 inline float3 operator - (const float3& a, const float3& b) {
72  return float3(a.x - b.x, a.y - b.y, a.z - b.z);
73 }
74 
75 inline float3 operator - (const float3& a) {
76  return float3(-a.x, -a.y, -a.z);
77 }
78 
79 inline float3 operator + (const float3& a, const float3& b) {
80  return float3(a.x + b.x, a.y + b.y, a.z + b.z);
81 }
82 
83 inline float3 operator * (const float3& a, const float3& b) {
84  return float3(a.x * b.x, a.y * b.y, a.z * b.z);
85 }
86 
87 inline float3 operator / (const float3& a, const float3& b) {
88  return float3(a.x / b.x, a.y / b.y, a.z / b.z);
89 }
90 
91 inline float3 cross(const float3& a, const float3& b) {
92  return float3(a.y * b.z - a.z * b.y,
93  a.z * b.x - a.x * b.z,
94  a.x * b.y - a.y * b.x);
95 }
96 
97 inline float3 rotate(const float3& v, const float3& axis, float angle) {
98  float q[4];
99  q[0] = axis.x * sinf(angle / 2);
100  q[1] = axis.y * sinf(angle / 2);
101  q[2] = axis.z * sinf(angle / 2);
102  q[3] = std::cos(angle / 2);
103 
104  float p[4];
105  p[0] = q[3] * v.x + q[1] * v.z - q[2] * v.y;
106  p[1] = q[3] * v.y - q[0] * v.z + q[2] * v.x;
107  p[2] = q[3] * v.z + q[0] * v.y - q[1] * v.x;
108  p[3] = -(q[0] * v.x + q[1] * v.y + q[2] * v.z);
109 
110  return float3(p[3] * -q[0] + p[0] * q[3] + p[1] * -q[2] - p[2] * -q[1],
111  p[3] * -q[1] - p[0] * -q[2] + p[1] * q[3] + p[2] * -q[0],
112  p[3] * -q[2] + p[0] * -q[1] - p[1] * -q[0] + p[2] * q[3]);
113 }
114 
115 inline float3 min(const float3& a, const float3& b) {
116  return float3(a.x < b.x ? a.x : b.x,
117  a.y < b.y ? a.y : b.y,
118  a.z < b.z ? a.z : b.z);
119 }
120 
121 inline float3 max(const float3& a, const float3& b) {
122  return float3(a.x > b.x ? a.x : b.x,
123  a.y > b.y ? a.y : b.y,
124  a.z > b.z ? a.z : b.z);
125 }
126 
127 inline float dot(const float3& a, const float3& b) {
128  return a.x * b.x + a.y * b.y + a.z * b.z;
129 }
130 
131 inline float lensqr(const float3& a) {
132  return dot(a, a);
133 }
134 
135 inline float length(const float3& a) {
136  return std::sqrt(dot(a, a));
137 }
138 
139 inline float3 normalize(const float3& a) {
140  return a * (1.0f / length(a));
141 }
142 
143 #endif // FLOAT3_H
Definition: float2.h:10
Definition: float3.h:10
Definition: float4.h:9