Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
debugfloat.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
37
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
//------------------------------------------------------------------------------
47
namespace
Debug
48
{
49
class
DebugFloat
50
{
51
public
:
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
//------------------------------------------------------------------------------
93
inline
94
void
95
DebugFloat::restoreHex
(
float
&v,
int
val)
96
{
97
v =
Util::TypePunning<float, int>
(val);
98
}
99
100
//------------------------------------------------------------------------------
103
inline
104
void
105
DebugFloat::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
//------------------------------------------------------------------------------
118
inline
119
void
120
DebugFloat::restoreHex
(
Math::mat4
&m,
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
//------------------------------------------------------------------------------
140
inline
141
void
142
DebugFloat::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
//------------------------------------------------------------------------------
155
inline
156
void
157
DebugFloat::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
//------------------------------------------------------------------------------
170
inline
171
void
172
DebugFloat::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
//------------------------------------------------------------------------------
188
inline
189
void
190
DebugFloat::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
//------------------------------------------------------------------------------
207
inline
208
void
209
DebugFloat::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
//------------------------------------------------------------------------------
226
inline
227
void
228
DebugFloat::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
//------------------------------------------------------------------------------
245
inline
246
void
247
DebugFloat::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
//------------------------------------------------------------------------------
276
inline
277
void
278
DebugFloat::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
//------------------------------------------------------------------------------
288
inline
289
void
290
DebugFloat::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
//------------------------------------------------------------------------------
300
inline
301
void
302
DebugFloat::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
//------------------------------------------------------------------------------
319
inline
320
void
321
DebugFloat::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
//------------------------------------------------------------------------------
Debug::DebugFloat
This class is supposed to make it easier, to restore the exact value of floating- point-based types,...
Definition
debugfloat.h:50
Debug::DebugFloat::printHex
static void printHex(float val, const char *msg=NULL)
print float's bit pattern as hex to stdout
Definition
debugfloat.h:247
Debug::DebugFloat::print
static void print(const float &v, const char *msg=NULL)
print float's values plain to stdout
Definition
debugfloat.h:290
Debug::DebugFloat::print
static void print(const Math::plane &p, const char *msg=NULL)
print plane's values plain to stdout
Debug::DebugFloat::restoreHex
static void restoreHex(float &v, int val)
Definition
debugfloat.h:95
n_printf
void __cdecl n_printf(const char *msg,...)
Nebula's printf replacement.
Definition
debug.cc:209
mat4.h
Debug
Definition
corepagehandler.cc:13
Graphics::bits
int bits
Definition
globalconstants.cc:28
Math::rad2deg
__forceinline constexpr scalar rad2deg(scalar r)
Definition
scalar.h:496
Math::angle
__forceinline scalar angle(const vec3 &v0, const vec3 &v1)
Definition
vec3.h:514
Math::length
__forceinline scalar length(const quat &q)
Definition
quat.h:260
Math::scalar
float scalar
Definition
scalar.h:45
Math::acos
__forceinline scalar acos(scalar x)
Definition
scalar.h:218
Math::nearequal
__forceinline bool nearequal(const point &v0, const point &v1, float epsilon)
Definition
point.h:485
Util::TypePunning
A & TypePunning(B &v)
Definition
typepunning.h:21
plane.h
quat.h
scalar.h
Nebula's scalar datatype.
Math::mat4
A 4x4 single point precision float matrix.
Definition
mat4.h:49
Math::mat4::row3
vec4 row3
Definition
mat4.h:143
Math::mat4::row1
vec4 row1
Definition
mat4.h:141
Math::mat4::row0
vec4 row0
Definition
mat4.h:140
Math::mat4::row2
vec4 row2
Definition
mat4.h:142
Math::plane
A mathematical plane represented by a normal and a distance from origin.
Definition
plane.h:23
Math::quat
A quaternion is usually used to represent an orientation in 3D space.
Definition
quat.h:30
Math::quat::y
float y
Definition
quat.h:78
Math::quat::z
float z
Definition
quat.h:78
Math::quat::w
float w
Definition
quat.h:78
Math::quat::x
float x
Definition
quat.h:78
Math::vec4
A 4D vector.
Definition
vec4.h:24
Math::vec4::y
float y
Definition
vec4.h:95
Math::vec4::z
float z
Definition
vec4.h:95
Math::vec4::w
float w
Definition
vec4.h:95
Math::vec4::x
float x
Definition
vec4.h:95
typepunning.h
types.h
vec4.h
code
foundation
debug
debugfloat.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.