Skip to content

Commit 026ad91

Browse files
rubenwardyparamat
authored andcommittedDec 26, 2017
Fix rounding error in g/set_node caused by truncation to float
1 parent 0bcc2f3 commit 026ad91

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed
 

‎src/irr_v3d.h

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424
#include <vector3d.h>
2525

2626
typedef core::vector3df v3f;
27+
typedef core::vector3d<double> v3d;
2728
typedef core::vector3d<s16> v3s16;
2829
typedef core::vector3d<u16> v3u16;
2930
typedef core::vector3d<s32> v3s32;

‎src/script/common/c_converter.cpp

+42-4
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,44 @@ v3f check_v3f(lua_State *L, int index)
225225
return pos;
226226
}
227227

228+
v3d read_v3d(lua_State *L, int index)
229+
{
230+
v3d pos;
231+
CHECK_POS_TAB(index);
232+
lua_getfield(L, index, "x");
233+
pos.X = lua_tonumber(L, -1);
234+
lua_pop(L, 1);
235+
lua_getfield(L, index, "y");
236+
pos.Y = lua_tonumber(L, -1);
237+
lua_pop(L, 1);
238+
lua_getfield(L, index, "z");
239+
pos.Z = lua_tonumber(L, -1);
240+
lua_pop(L, 1);
241+
return pos;
242+
}
243+
244+
v3d check_v3d(lua_State *L, int index)
245+
{
246+
v3d pos;
247+
CHECK_POS_TAB(index);
248+
lua_getfield(L, index, "x");
249+
CHECK_POS_COORD("x");
250+
pos.X = lua_tonumber(L, -1);
251+
CHECK_FLOAT_RANGE(pos.X, "x")
252+
lua_pop(L, 1);
253+
lua_getfield(L, index, "y");
254+
CHECK_POS_COORD("y");
255+
pos.Y = lua_tonumber(L, -1);
256+
CHECK_FLOAT_RANGE(pos.Y, "y")
257+
lua_pop(L, 1);
258+
lua_getfield(L, index, "z");
259+
CHECK_POS_COORD("z");
260+
pos.Z = lua_tonumber(L, -1);
261+
CHECK_FLOAT_RANGE(pos.Z, "z")
262+
lua_pop(L, 1);
263+
return pos;
264+
}
265+
228266
void push_ARGB8(lua_State *L, video::SColor color)
229267
{
230268
lua_newtable(L);
@@ -263,15 +301,15 @@ void push_v3s16(lua_State *L, v3s16 p)
263301
v3s16 read_v3s16(lua_State *L, int index)
264302
{
265303
// Correct rounding at <0
266-
v3f pf = read_v3f(L, index);
267-
return floatToInt(pf, 1.0);
304+
v3d pf = read_v3d(L, index);
305+
return doubleToInt(pf, 1.0);
268306
}
269307

270308
v3s16 check_v3s16(lua_State *L, int index)
271309
{
272310
// Correct rounding at <0
273-
v3f pf = check_v3f(L, index);
274-
return floatToInt(pf, 1.0);
311+
v3d pf = check_v3d(L, index);
312+
return doubleToInt(pf, 1.0);
275313
}
276314

277315
bool read_color(lua_State *L, int index, video::SColor *color)

‎src/util/numeric.h

+11
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,17 @@ inline v3s16 floatToInt(v3f p, f32 d)
254254
(p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
255255
}
256256

257+
/*
258+
Returns integer position of node in given double precision position
259+
*/
260+
inline v3s16 doubleToInt(v3d p, double d)
261+
{
262+
return v3s16(
263+
(p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
264+
(p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
265+
(p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
266+
}
267+
257268
/*
258269
Returns floating point position of node in given integer position
259270
*/

0 commit comments

Comments
 (0)
Please sign in to comment.