Skip to content

Commit f32f4a9

Browse files
authored
Merge pull request #6 from korarei/refactor/geo
Refactor: Add constexpr and const qualifiers to GeoMap class
2 parents 53e5b4a + 94d69f4 commit f32f4a9

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

dll_src/geo_utils.hpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstddef>
55
#include <cstdint>
66
#include <map>
7+
#include <mutex>
78
#include <unordered_map>
89
#include <vector>
910

@@ -12,10 +13,10 @@
1213
template <std::size_t N>
1314
class GeoMap {
1415
public:
15-
GeoMap() noexcept = default;
16-
~GeoMap() noexcept = default;
16+
constexpr GeoMap() noexcept = default;
17+
constexpr ~GeoMap() noexcept = default;
1718

18-
void resize(std::size_t hash, std::size_t idx, std::size_t num, std::uint32_t mode) noexcept {
19+
constexpr void resize(std::size_t hash, std::size_t idx, std::size_t num, std::uint32_t mode) noexcept {
1920
if (mode == 0u) {
2021
clear(hash);
2122
return;
@@ -42,7 +43,7 @@ class GeoMap {
4243
return;
4344
}
4445

45-
void write(std::size_t hash, std::size_t idx, std::size_t pos, const Geo &geo) noexcept {
46+
constexpr void write(std::size_t hash, std::size_t idx, std::size_t pos, const Geo &geo) noexcept {
4647
const auto [id, offset] = calc_block_pos(pos);
4748

4849
auto it_hash = map.find(hash);
@@ -58,7 +59,7 @@ class GeoMap {
5859
return;
5960
}
6061

61-
[[nodiscard]] Geo *read(std::size_t hash, std::size_t idx, std::size_t pos) noexcept {
62+
[[nodiscard]] constexpr const Geo *read(std::size_t hash, std::size_t idx, std::size_t pos) const noexcept {
6263
const auto [id, offset] = calc_block_pos(pos);
6364

6465
if (auto block = get_block(hash, idx, id); block && (*block)[offset].get_flag())
@@ -67,8 +68,9 @@ class GeoMap {
6768
return nullptr;
6869
}
6970

70-
void clear() noexcept { MapData{}.swap(map); }
71-
void clear(std::size_t hash) noexcept {
71+
constexpr void clear() noexcept { MapData{}.swap(map); }
72+
73+
constexpr void clear(std::size_t hash) noexcept {
7274
auto it_hash = map.find(hash);
7375
if (it_hash == map.end())
7476
return;
@@ -80,13 +82,16 @@ class GeoMap {
8082
}
8183

8284
private:
83-
using BlockMap = std::map<std::size_t, std::array<Geo, N>>;
85+
using Block = std::array<Geo, N>;
86+
using BlockMap = std::map<std::size_t, Block>;
8487
using MapData = std::unordered_map<std::size_t, std::vector<BlockMap>>;
8588
MapData map{};
8689

87-
std::array<std::size_t, 2> calc_block_pos(std::size_t v) const { return {v / N, v % N}; }
90+
[[nodiscard]] constexpr std::array<std::size_t, 2> calc_block_pos(std::size_t v) const noexcept {
91+
return {v / N, v % N};
92+
}
8893

89-
std::array<Geo, N> *get_block(std::size_t hash, std::size_t idx, std::size_t id) {
94+
[[nodiscard]] constexpr const Block *get_block(std::size_t hash, std::size_t idx, std::size_t id) const noexcept {
9095
auto it_hash = map.find(hash);
9196
if (it_hash == map.end())
9297
return nullptr;

dll_src/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ cleanup_geo(const Param &param, const Input &input) {
7676
case 1:
7777
if (input.is_last[1])
7878
geo_map.clear(input.obj_id);
79-
break;
79+
return;
8080
case 2:
8181
geo_map.clear();
82-
break;
82+
return;
8383
case 3:
8484
geo_map.clear(input.obj_id);
85-
break;
85+
return;
8686
default:
87-
break;
87+
return;
8888
}
8989
}
9090

dll_src/structs.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ struct CParam {
2222
struct CInput {
2323
float w, h;
2424
float px, py;
25-
int obj_id, obj_idx, obj_num;
26-
int frame, total_frame;
25+
size_t obj_id, obj_idx, obj_num;
26+
size_t frame, total_frame;
2727
float curr[6];
2828
float prev[6];
2929
float geo[6];
@@ -79,10 +79,10 @@ struct Input {
7979
constexpr Input(const CInput &c_input) noexcept :
8080
res(c_input.w, c_input.h),
8181
pivot(c_input.px, c_input.py),
82-
obj_id(static_cast<std::size_t>(std::max(c_input.obj_id, 0))),
83-
obj_idx(static_cast<std::size_t>(c_input.obj_idx)),
84-
obj_num(static_cast<std::size_t>(c_input.obj_num)),
85-
frame(static_cast<std::size_t>(c_input.frame)),
82+
obj_id(c_input.obj_id),
83+
obj_idx(c_input.obj_idx),
84+
obj_num(c_input.obj_num),
85+
frame(c_input.frame),
8686
is_last(c_input.obj_idx == c_input.obj_num - 1, c_input.frame == c_input.total_frame - 1),
8787
tf_curr(std::to_array(c_input.curr)),
8888
tf_prev(std::to_array(c_input.prev)),

scripts/ObjectMotionBlur_LK_template.anm2

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ if (not pcall(obj.getvalue, "cx")) then
2020
return
2121
end
2222

23+
if (obj.num == 0) then
24+
return
25+
end
26+
2327
local function tobool(v, d)
2428
if (type(v) == "boolean") then
2529
return v
@@ -38,7 +42,7 @@ local ext = tonumber(_0.extrapolation) or s0 s0 = nil
3842
local resize = tobool(_0.resize, c1 ~= 0) c1 = nil
3943
local geo_cache = tonumber(_0.geo_cache or s1) s1 = nil
4044
local geo_ctrl = tonumber(_0.geo_ctrl or s2) s2 = nil
41-
local obj_id = tonumber(_0.object_id) or t6 t6 = nil -- beta 11a
45+
local obj_id = math.max(tonumber(_0.object_id) or t6, 0) t6 = nil -- beta 11a
4246
local mix = math.min(math.max(tonumber(_0.mix) or t5, 0.0), 100.0) * 0.01 t5 = nil
4347
local print_info = tobool(_0.print_info, c2 ~= 0) c2 = nil
4448
_0 = nil
@@ -57,8 +61,8 @@ pcall(ffi.cdef, [[
5761
typedef struct {
5862
float w, h;
5963
float px, py;
60-
int obj_id, obj_idx, obj_num;
61-
int frame, total_frame;
64+
size_t obj_id, obj_idx, obj_num;
65+
size_t frame, total_frame;
6266
float curr[6];
6367
float prev[6];
6468
float geo[6];

0 commit comments

Comments
 (0)