diff options
author | qonnop <qonnop@users.noreply.github.com> | 2018-10-07 15:40:50 +0200 |
---|---|---|
committer | qonnop <qonnop@users.noreply.github.com> | 2018-10-07 15:43:51 +0200 |
commit | 50fae2b1a7a7a5f3029c50f9884ccdde0762c8a3 (patch) | |
tree | f80280c49c4983f891d2fba7bf2f27159ef5e308 | |
parent | 5676632a3bb8562b3346a3afba4069501365b35f (diff) |
Optimize interpolation algorithms
Interpolation in the form of va + (vb - va) * c is faster and prevents floating point issues for int/string
Followup to https://github.com/godotengine/godot/pull/22786
-rw-r--r-- | core/variant_op.cpp | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/core/variant_op.cpp b/core/variant_op.cpp index f230f12b5d..d193858966 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -3521,7 +3521,7 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant & //not as efficient but.. real_t va = a; real_t vb = b; - r_dst = (1.0 - c) * va + vb * c; + r_dst = va + (vb - va) * c; } else { r_dst = a; @@ -3542,16 +3542,13 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant & case INT: { int64_t va = a._data._int; int64_t vb = b._data._int; - if (va != vb) - r_dst = int((1.0 - c) * va + vb * c); - else //avoid int casting issues - r_dst = a; + r_dst = int(va + (vb - va) * c); } return; case REAL: { real_t va = a._data._real; real_t vb = b._data._real; - r_dst = (1.0 - c) * va + vb * c; + r_dst = va + (vb - va) * c; } return; case STRING: { @@ -3559,7 +3556,9 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant & String sa = *reinterpret_cast<const String *>(a._data._mem); String sb = *reinterpret_cast<const String *>(b._data._mem); String dst; - int csize = sb.length() * c + sa.length() * (1.0 - c); + int sa_len = sa.length(); + int sb_len = sb.length(); + int csize = sa_len + (sb_len - sa_len) * c; if (csize == 0) { r_dst = ""; return; |