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