From 0a8b08db18d30b04756a093cb0fa7e010c09be8d Mon Sep 17 00:00:00 2001 From: korenkonder Date: Sun, 21 Jul 2024 16:28:56 +0300 Subject: [PATCH] Don't try curve fitting if value is constant --- src/CRE/Glitter/curve.cpp | 90 ++++++++++++++++++++--------------- src/KKdLib/interpolation.cpp | 92 +++++++++++++++++++----------------- src/KKdLib/mot.cpp | 92 +++++++++++++++++++----------------- src/ReDIVA/x_pv_game.cpp | 57 ---------------------- 4 files changed, 150 insertions(+), 181 deletions(-) diff --git a/src/CRE/Glitter/curve.cpp b/src/CRE/Glitter/curve.cpp index 3a1b49d1..85068009 100644 --- a/src/CRE/Glitter/curve.cpp +++ b/src/CRE/Glitter/curve.cpp @@ -379,52 +379,66 @@ namespace Glitter { int32_t c = 0; size_t i; for (i = reverse_min_count - 1; i < left_count; i++) { - double_t t1_accum = 0.0; - double_t t2_accum = 0.0; - for (size_t j = 1; j < i - 1; j++) { - vec2d t = vec2d( - (double_t)((j + 0) * step), - (double_t)((j + 1) * step) - ) / (double_t)(i * step); - vec2d t_1 = t - 1.0f; + bool constant = true; + for (size_t j = 1; j <= i; j++) + if (memcmp(&a[0], &a[j], sizeof(double_t)) + || memcmp(&b[0], &b[j], sizeof(double_t))) { + constant = false; + break; + } - vec2d t1_t2 = (*(vec2d*)&a[j + 0] + *(vec2d*)&b[j + 0]) - (a[0] + b[0]) - - (t * 2.0 - 3.0) * (t * t) * ((a[0] + b[0]) - (a[i] + b[i])); - t1_t2 /= t_1 * t; - - double_t t1 = -t1_t2.x * t.y + t1_t2.y * t.x; - double_t t2 = t1_t2.x * t_1.y - t1_t2.y * t_1.x; - - t1_accum += t1; - t2_accum += t2; - } - t1 = t1_accum / (double_t)(i - 2); - t2 = t2_accum / (double_t)(i - 2); - - constant = true; + t1 = 0.0; + t2 = 0.0; has_error = false; has_error_lerp = false; has_error_hermite = false; - for (size_t j = 1; j < i; j++) { - double_t val = InterpolateHermite(a[0] + b[0], (a[i] + b[i]) - (a[0] + b[0]), t1, t2, - 0.0, (double_t)(i * step), (double_t)(j * step)); - double_t val_lerp = InterpolateLinear(a[0] + b[0], a[i] + b[i], - 0.0, (double_t)(i * step), (double_t)(j * step)); - if (fabs(val - (a[0] + b[j])) > reverse_bias[0]) { - has_error = true; - constant = false; - if (fabs(val_lerp - (a[j] + b[j])) > reverse_bias[1]) { - has_error_lerp = true; - if (fabs(val - (a[j] + b[j])) > reverse_bias[2]) { - has_error_hermite = true; - break; + + if (!constant) { + double_t t1_accum = 0.0; + double_t t2_accum = 0.0; + for (size_t j = 1; j < i - 1; j++) { + vec2d t = vec2d( + (double_t)((j + 0) * step), + (double_t)((j + 1) * step) + ) / (double_t)(i * step); + vec2d t_1 = t - 1.0f; + + vec2d t1_t2 = (*(vec2d*)&a[j + 0] + *(vec2d*)&b[j + 0]) - (a[0] + b[0]) + - (t * 2.0 - 3.0) * (t * t) * ((a[0] + b[0]) - (a[i] + b[i])); + t1_t2 /= t_1 * t; + + double_t t1 = -t1_t2.x * t.y + t1_t2.y * t.x; + double_t t2 = t1_t2.x * t_1.y - t1_t2.y * t_1.x; + + t1_accum += t1; + t2_accum += t2; + } + t1 = t1_accum / (double_t)(i - 2); + t2 = t2_accum / (double_t)(i - 2); + + has_error = false; + has_error_lerp = false; + has_error_hermite = false; + for (size_t j = 1; j < i; j++) { + double_t val = InterpolateHermite(a[0] + b[0], (a[i] + b[i]) - (a[0] + b[0]), t1, t2, + 0.0, (double_t)(i * step), (double_t)(j * step)); + double_t val_lerp = InterpolateLinear(a[0] + b[0], a[i] + b[i], + 0.0, (double_t)(i * step), (double_t)(j * step)); + if (fabs(val - (a[0] + b[j])) > reverse_bias[0]) { + has_error = true; + if (fabs(val_lerp - (a[j] + b[j])) > reverse_bias[1]) { + has_error_lerp = true; + if (fabs(val - (a[j] + b[j])) > reverse_bias[2]) { + has_error_hermite = true; + break; + } } } } - } - if (fabs(t1) > 0.5 || fabs(t2) > 0.5) - has_error_hermite = true; + if (fabs(t1) > 0.5 || fabs(t2) > 0.5) + has_error_hermite = true; + } if (!has_error_hermite) { t1_prev = t1; diff --git a/src/KKdLib/interpolation.cpp b/src/KKdLib/interpolation.cpp index 59d7fd9c..8ba1f426 100644 --- a/src/KKdLib/interpolation.cpp +++ b/src/KKdLib/interpolation.cpp @@ -214,51 +214,57 @@ int32_t interpolate_chs_reverse_sequence( break; } - if (!fast) { - double_t t1_accum = 0.0; - double_t t2_accum = 0.0; - - size_t j = 1; - for (; j < i - 1 && j + 3 <= i - 1; j += 3) { - float_t t1a = 0.0f; - float_t t2a = 0.0f; - float_t t1b = 0.0f; - float_t t2b = 0.0f; - float_t t1c = 0.0f; - float_t t2c = 0.0f; - interpolate_chs_reverse_value(a, left_count, t1a, t2a, t1b, t2b, t1c, t2c, 0, i, j); - t1_accum += t1a; - t2_accum += t2a; - t1_accum += t1b; - t2_accum += t2b; - t1_accum += t1c; - t2_accum += t2c; - } - - for (; j < i - 1; j++) { - float_t t1 = 0.0f; - float_t t2 = 0.0f; - interpolate_chs_reverse_value(a, left_count, t1, t2, 0, i, j); - t1_accum += t1; - t2_accum += t2; - } - t1 = (float_t)(t1_accum / (double_t)(i - 2)); - t2 = (float_t)(t2_accum / (double_t)(i - 2)); - } - else - interpolate_chs_reverse_value(a, left_count, t1, t2, 0, i, 1); - + t1 = 0.0f; + t2 = 0.0f; has_error = false; - for (size_t j = 1; j < i; j++) { - float_t val = interpolate_chs_value(a[0], a[i], t1, t2, 0.0f, (float_t)i, (float_t)j); - if (fabsf(val - a[j]) > reverse_bias) { - has_error = true; - break; - } - } - if (fabsf(t1) > 0.5f || fabsf(t2) > 0.5f) - has_error = true; + if (!constant) { + if (!fast) { + double_t t1_accum = 0.0; + double_t t2_accum = 0.0; + + size_t j = 1; + for (; j < i - 1 && j + 3 <= i - 1; j += 3) { + float_t t1a = 0.0f; + float_t t2a = 0.0f; + float_t t1b = 0.0f; + float_t t2b = 0.0f; + float_t t1c = 0.0f; + float_t t2c = 0.0f; + interpolate_chs_reverse_value(a, left_count, t1a, t2a, t1b, t2b, t1c, t2c, 0, i, j); + t1_accum += t1a; + t2_accum += t2a; + t1_accum += t1b; + t2_accum += t2b; + t1_accum += t1c; + t2_accum += t2c; + } + + for (; j < i - 1; j++) { + float_t t1 = 0.0f; + float_t t2 = 0.0f; + interpolate_chs_reverse_value(a, left_count, t1, t2, 0, i, j); + t1_accum += t1; + t2_accum += t2; + } + t1 = (float_t)(t1_accum / (double_t)(i - 2)); + t2 = (float_t)(t2_accum / (double_t)(i - 2)); + } + else + interpolate_chs_reverse_value(a, left_count, t1, t2, 0, i, 1); + + has_error = false; + for (size_t j = 1; j < i; j++) { + float_t val = interpolate_chs_value(a[0], a[i], t1, t2, 0.0f, (float_t)i, (float_t)j); + if (fabsf(val - a[j]) > reverse_bias) { + has_error = true; + break; + } + } + + if (fabsf(t1) > 0.5f || fabsf(t2) > 0.5f) + has_error = true; + } if (!has_error) { i_prev = i; diff --git a/src/KKdLib/mot.cpp b/src/KKdLib/mot.cpp index 361aadb2..4045b3bc 100644 --- a/src/KKdLib/mot.cpp +++ b/src/KKdLib/mot.cpp @@ -255,51 +255,57 @@ mot_key_set_type mot_set::fit_keys_into_curve(std::vector& values_src, break; } - if (!fast) { - double_t t1_accum = 0.0; - double_t t2_accum = 0.0; - - size_t j = 1; - for (; j < i - 1 && j + 3 <= i - 1; j += 3) { - float_t t1a = 0.0f; - float_t t2a = 0.0f; - float_t t1b = 0.0f; - float_t t2b = 0.0f; - float_t t1c = 0.0f; - float_t t2c = 0.0f; - interpolate_mot_reverse_value(a, left_count, t1a, t2a, t1b, t2b, t1c, t2c, 0, i, j); - t1_accum += t1a; - t2_accum += t2a; - t1_accum += t1b; - t2_accum += t2b; - t1_accum += t1c; - t2_accum += t2c; - } - - for (; j < i - 1; j++) { - float_t t1 = 0.0f; - float_t t2 = 0.0f; - interpolate_mot_reverse_value(a, left_count, t1, t2, 0, i, j); - t1_accum += t1; - t2_accum += t2; - } - t1 = (float_t)(t1_accum / (double_t)(i - 2)); - t2 = (float_t)(t2_accum / (double_t)(i - 2)); - } - else - interpolate_mot_reverse_value(a, left_count, t1, t2, 0, i, 1); - + t1 = 0.0f; + t2 = 0.0f; has_error = false; - for (size_t j = 1; j < i; j++) { - float_t val = interpolate_mot_value(a[0], a[i], t1, t2, 0.0f, (float_t)i, (float_t)j); - if (fabsf(val - a[j]) > reverse_bias) { - has_error = true; - break; - } - } - if (fabsf(t1) > 0.5f || fabsf(t2) > 0.5f) - has_error = true; + if (!constant) { + if (!fast) { + double_t t1_accum = 0.0; + double_t t2_accum = 0.0; + + size_t j = 1; + for (; j < i - 1 && j + 3 <= i - 1; j += 3) { + float_t t1a = 0.0f; + float_t t2a = 0.0f; + float_t t1b = 0.0f; + float_t t2b = 0.0f; + float_t t1c = 0.0f; + float_t t2c = 0.0f; + interpolate_mot_reverse_value(a, left_count, t1a, t2a, t1b, t2b, t1c, t2c, 0, i, j); + t1_accum += t1a; + t2_accum += t2a; + t1_accum += t1b; + t2_accum += t2b; + t1_accum += t1c; + t2_accum += t2c; + } + + for (; j < i - 1; j++) { + float_t t1 = 0.0f; + float_t t2 = 0.0f; + interpolate_mot_reverse_value(a, left_count, t1, t2, 0, i, j); + t1_accum += t1; + t2_accum += t2; + } + t1 = (float_t)(t1_accum / (double_t)(i - 2)); + t2 = (float_t)(t2_accum / (double_t)(i - 2)); + } + else + interpolate_mot_reverse_value(a, left_count, t1, t2, 0, i, 1); + + has_error = false; + for (size_t j = 1; j < i; j++) { + float_t val = interpolate_mot_value(a[0], a[i], t1, t2, 0.0f, (float_t)i, (float_t)j); + if (fabsf(val - a[j]) > reverse_bias) { + has_error = true; + break; + } + } + + if (fabsf(t1) > 0.5f || fabsf(t2) > 0.5f) + has_error = true; + } if (!has_error) { i_prev = i; diff --git a/src/ReDIVA/x_pv_game.cpp b/src/ReDIVA/x_pv_game.cpp index 9c817b6f..7c7e2217 100644 --- a/src/ReDIVA/x_pv_game.cpp +++ b/src/ReDIVA/x_pv_game.cpp @@ -9565,63 +9565,6 @@ static int x_pv_game_auth_3d_hrc_obj_bone_compare_func(void const* src1, void co return bone1->dst_name.compare(bone2->dst_name); } -static void auth_3d_key_rev(auth_3d_key& k, std::vector& values_src) { - std::vector values; - int32_t type = interpolate_chs_reverse_sequence(values_src, values); - - k = {}; - switch (type) { - case A3DA_KEY_NONE: - k.type = AUTH_3D_KEY_NONE; - k.value = 0.0f; - return; - case A3DA_KEY_STATIC: - k.type = values[0].value != 0.0f ? AUTH_3D_KEY_STATIC : AUTH_3D_KEY_NONE; - k.value = values[0].value; - return; - case A3DA_KEY_LINEAR: - k.type = AUTH_3D_KEY_LINEAR; - break; - case A3DA_KEY_HERMITE: - default: - k.type = AUTH_3D_KEY_HERMITE; - break; - case A3DA_KEY_HOLD: - k.type = AUTH_3D_KEY_HOLD; - break; - } - - k.max_frame = (float_t)(values_src.size() + 1); - k.frame_delta = k.max_frame; - k.value_delta = 0.0f; - - size_t length = values.size(); - if (length > 1) { - k.keys_vec.assign(values.begin(), values.end()); - k.length = length; - k.keys = k.keys_vec.data(); - - kft3* first_key = &k.keys[0]; - kft3* last_key = &k.keys[length - 1]; - if (first_key->frame < last_key->frame - && last_key->frame > 0.0f && k.max_frame > first_key->frame) { - k.ep_type_pre = AUTH_3D_EP_NONE; - k.ep_type_post = AUTH_3D_EP_NONE; - k.frame_delta = last_key->frame - first_key->frame; - k.value_delta = last_key->value - first_key->value; - } - } - else if (length == 1) { - float_t value = values.front().value; - k.type = value != 0.0f ? AUTH_3D_KEY_STATIC : AUTH_3D_KEY_NONE; - k.value = value; - } - else { - k.type = AUTH_3D_KEY_NONE; - k.value = 0.0f; - } -} - #if BAKE_X_PACK static void x_pv_game_update_object_set(ObjsetInfo* info) { prj::shared_ptr old_alloc = info->alloc_handler;