Nebula
Loading...
Searching...
No Matches
debugfloat.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
38#include "math/vec4.h"
39#include "math/mat4.h"
40#include "math/quat.h"
41#include "math/plane.h"
42#include "math/scalar.h"
43#include "core/types.h"
44#include "util/typepunning.h"
45
46//------------------------------------------------------------------------------
47namespace Debug
48{
50{
51public:
53 static void printHex(float val, const char *msg = NULL);
55 static void printHex(const Math::vec4 &v, const char *msg = NULL);
57 static void printHex(const Math::mat4 &m, const char *msg = NULL);
59 static void printHex(const Math::quat &q, const char *msg = NULL);
61 static void printHex(const Math::plane &p, const char *msg = NULL);
62
63 // restore a float's bit pattern
64 static void restoreHex(float &v, int val);
65 // restore a vec4's bit pattern
66 static void restoreHex(Math::vec4 &v, int x, int y, int z, int w);
67 // restore a matrix's bit pattern
68 static void restoreHex(Math::mat4 &m,
69 int m0, int m1, int m2, int m3,
70 int m4, int m5, int m6, int m7,
71 int m8, int m9, int m10, int m11,
72 int m12, int m13, int m14, int m15);
73 // restore a quaternion's bit pattern
74 static void restoreHex(Math::quat &q, int x, int y, int z, int w);
75 // restore a plane's bit pattern
76 static void restoreHex(Math::plane &p, int x, int y, int z, int w);
77
79 static void print(const float &v, const char *msg = NULL);
81 static void print(const Math::vec4 &v, const char *msg = NULL);
83 static void print(const Math::mat4 &m, const char *msg = NULL);
85 static void print(const Math::quat &q, const char *msg = NULL);
87 static void print(const Math::plane &p, const char *msg = NULL);
88};
89
90//------------------------------------------------------------------------------
93inline
94void
95DebugFloat::restoreHex(float &v, int val)
96{
98}
99
100//------------------------------------------------------------------------------
103inline
104void
105DebugFloat::restoreHex(Math::vec4 &v, int x, int y, int z, int w)
106{
107 const int vRaw[4] = {x, y, z, w};
108 float *p = (float*)&v;
109 for(int index = 0; index < 4; ++index)
110 {
111 *p++ = Util::TypePunning<float, int>(vRaw[index]);
112 }
113}
114
115//------------------------------------------------------------------------------
118inline
119void
121 int m0, int m1, int m2, int m3,
122 int m4, int m5, int m6, int m7,
123 int m8, int m9, int m10, int m11,
124 int m12, int m13, int m14, int m15)
125{
126 const int mRaw[4][4] = {{m0, m1, m2, m3},
127 {m4, m5, m6, m7},
128 {m8, m9, m10, m11},
129 {m12, m13, m14, m15}};
130 float *p = (float*)&m.row0;
131 for(int index = 0; index < 16; ++index)
132 {
133 *p++ = Util::TypePunning<float, int>(mRaw[index/4][index%4]);
134 }
135}
136
137//------------------------------------------------------------------------------
140inline
141void
142DebugFloat::restoreHex(Math::quat &q, int x, int y, int z, int w)
143{
144 const int vRaw[4] = {x, y, z, w};
145 float *p = (float*)&q;
146 for(int index = 0; index < 4; ++index)
147 {
148 *p++ = Util::TypePunning<float, int>(vRaw[index]);
149 }
150}
151
152//------------------------------------------------------------------------------
155inline
156void
157DebugFloat::restoreHex(Math::plane &pl, int x, int y, int z, int w)
158{
159 const int vRaw[4] = {x, y, z, w};
160 float *p = (float*)&pl;
161 for(int index = 0; index < 4; ++index)
162 {
163 *p++ = Util::TypePunning<float, int>(vRaw[index]);
164 }
165}
166
167//------------------------------------------------------------------------------
170inline
171void
172DebugFloat::printHex(const Math::mat4 &m, const char *msg)
173{
174 if(msg) n_printf("%s: ", msg);
175 printHex(m.row0);
176 n_printf(", ");
177 printHex(m.row1);
178 n_printf(", ");
179 printHex(m.row2);
180 n_printf(", ");
181 printHex(m.row3);
182 if(msg) n_printf("\n");
183}
184
185//------------------------------------------------------------------------------
188inline
189void
190DebugFloat::printHex(const Math::vec4 &v, const char *msg)
191{
192 if(msg) n_printf("%s: ", msg);
193 const float *p = (const float*)&v;
194 printHex(p[0]);
195 n_printf(", ");
196 printHex(p[1]);
197 n_printf(", ");
198 printHex(p[2]);
199 n_printf(", ");
200 printHex(p[3]);
201 if(msg) n_printf("\n");
202}
203
204//------------------------------------------------------------------------------
207inline
208void
209DebugFloat::printHex(const Math::quat &q, const char *msg)
210{
211 if(msg) n_printf("%s: ", msg);
212 const float *p = (const float*)&q;
213 printHex(p[0]);
214 n_printf(", ");
215 printHex(p[1]);
216 n_printf(", ");
217 printHex(p[2]);
218 n_printf(", ");
219 printHex(p[3]);
220 if(msg) n_printf("\n");
221}
222
223//------------------------------------------------------------------------------
226inline
227void
228DebugFloat::printHex(const Math::plane &pl, const char *msg)
229{
230 if(msg) n_printf("%s: ", msg);
231 const float *p = (const float*)&pl;
232 printHex(p[0]);
233 n_printf(", ");
234 printHex(p[1]);
235 n_printf(", ");
236 printHex(p[2]);
237 n_printf(", ");
238 printHex(p[3]);
239 if(msg) n_printf("\n");
240}
241
242//------------------------------------------------------------------------------
245inline
246void
247DebugFloat::printHex(float val, const char *msg)
248{
249 if(msg) n_printf("%s: ", msg);
250 const int bits = Util::TypePunning<int, float>(val);
251 unsigned char tmp;
252 char buffer[(sizeof(int)*2)+3];
253 char *p = buffer;
254 *p++ = '0';
255 *p++ = 'x';
256 for( int i = sizeof(int)*2-1; i > -1; --i )
257 {
258 tmp = ( bits >> (i*4) ) & 0xF;
259 if( tmp >= 10 )
260 {
261 *p++ = tmp - 10 + 'A';
262 }
263 else
264 {
265 *p++ = tmp + '0';
266 }
267 }
268 *p = '\0';
269 n_printf("%s", buffer );
270 if(msg) n_printf("\n");
271}
272
273//------------------------------------------------------------------------------
276inline
277void
278DebugFloat::print(const Math::vec4 &v, const char *msg)
279{
280 if(msg) n_printf("%s: ", msg);
281 n_printf("%10.4f %10.4f %10.4f %10.4f", v.x, v.y, v.z, v.w);
282 if(msg) n_printf("\n");
283}
284
285//------------------------------------------------------------------------------
288inline
289void
290DebugFloat::print(const float &v, const char *msg)
291{
292 if(msg) n_printf("%s:\n", msg);
293 n_printf("%10.4f", v);
294 if(msg) n_printf("\n");
295}
296
297//------------------------------------------------------------------------------
300inline
301void
302DebugFloat::print(const Math::mat4 &m, const char *msg)
303{
304 if(msg) n_printf("%s:\n", msg);
305 // yes, looks weird, rows and cols seem to be mixed, but its not
306 print(Math::vec4(m.row0.x, m.row1.x, m.row2.x, m.row3.x), NULL);
307 n_printf("\n");
308 print(Math::vec4(m.row0.y, m.row1.y, m.row2.y, m.row3.y), NULL);
309 n_printf("\n");
310 print(Math::vec4(m.row0.z, m.row1.z, m.row2.z, m.row3.z), NULL);
311 n_printf("\n");
312 print(Math::vec4(m.row0.w, m.row1.w, m.row2.w, m.row3.w), NULL);
313 n_printf("\n");
314}
315
316//------------------------------------------------------------------------------
319inline
320void
321DebugFloat::print(const Math::quat &q, const char *msg)
322{
323 if(msg) n_printf("%s:\n", msg);
324 if(Math::nearequal(length(q), 1.0f, 0.00001f))
325 {
326 const Math::scalar angle = Math::acos(q.w) * 2.0f;
327 n_printf(" axis : %f %f %f\n"
328 " w : %f\n"
329 " magnitude: %f rad: %f deg: %f\n",
330 q.x, q.y, q.z, q.w, length(q), angle, Math::rad2deg(angle));
331 }
332 else
333 {
334 n_printf(" axis : %f %f %f\n"
335 " w : %f\n"
336 " magnitude: %f\n",
337 q.x, q.y, q.z, q.w, length(q));
338 }
339 if(msg) n_printf("\n");
340}
341
342} // namespace Debug
343//------------------------------------------------------------------------------
This class is supposed to make it easier, to restore the exact value of floating- point-based types,...
Definition debugfloat.h:50
static void printHex(float val, const char *msg=NULL)
print float's bit pattern as hex to stdout
Definition debugfloat.h:247
static void print(const float &v, const char *msg=NULL)
print float's values plain to stdout
Definition debugfloat.h:290
static void print(const Math::plane &p, const char *msg=NULL)
print plane's values plain to stdout
static void restoreHex(float &v, int val)
Definition debugfloat.h:95
void __cdecl n_printf(const char *msg,...)
Nebula's printf replacement.
Definition debug.cc:209
Definition corepagehandler.cc:13
int bits
Definition globalconstants.cc:28
__forceinline constexpr scalar rad2deg(scalar r)
Definition scalar.h:477
__forceinline scalar angle(const vec3 &v0, const vec3 &v1)
Definition vec3.h:508
__forceinline scalar length(const quat &q)
Definition quat.h:259
float scalar
Definition scalar.h:45
__forceinline scalar acos(scalar x)
Definition scalar.h:218
__forceinline bool nearequal(const point &v0, const point &v1, float epsilon)
Definition point.h:485
A & TypePunning(B &v)
Definition typepunning.h:21
Nebula's scalar datatype.
A 4x4 single point precision float matrix.
Definition mat4.h:49
vec4 row3
Definition mat4.h:138
vec4 row1
Definition mat4.h:136
vec4 row0
Definition mat4.h:135
vec4 row2
Definition mat4.h:137
A mathematical plane represented by a normal and a distance from origin.
Definition plane.h:22
A quaternion is usually used to represent an orientation in 3D space.
Definition quat.h:30
float y
Definition quat.h:78
float z
Definition quat.h:78
float w
Definition quat.h:78
float x
Definition quat.h:78
A 4D vector.
Definition vec4.h:24
float y
Definition vec4.h:93
float z
Definition vec4.h:93
float w
Definition vec4.h:93
float x
Definition vec4.h:93