-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_palette.hpp
More file actions
152 lines (150 loc) · 6.97 KB
/
block_palette.hpp
File metadata and controls
152 lines (150 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// block_palette.hpp
#pragma once
#include <array>
#include <vector>
#include <cmath>
#include <cstdint>
#include <limits>
#include <thread>
namespace block_palette
{
using Point3 = std::array<double, 3>;
// forward-declare OpenCL mapping entry (implemented in opencl_map.cpp when USE_OPENCL=1)
bool opencl_map(const std::vector<Point3> &pts, std::vector<int> &out_indices);
struct RGB
{
int r, g, b;
};
struct Block
{
const char *name;
RGB c;
};
static const Block COLOR_MAP[] = {
{"white_concrete", {207, 213, 214}}, {"orange_concrete", {224, 97, 0}}, {"magenta_concrete", {169, 48, 159}}, {"light_blue_concrete", {36, 137, 199}}, {"yellow_concrete", {241, 175, 21}}, {"lime_concrete", {94, 168, 24}}, {"pink_concrete", {214, 101, 143}}, {"gray_concrete", {54, 57, 61}}, {"light_gray_concrete", {125, 125, 115}}, {"cyan_concrete", {21, 119, 136}}, {"purple_concrete", {100, 32, 156}}, {"blue_concrete", {45, 47, 143}}, {"brown_concrete", {96, 60, 32}}, {"green_concrete", {73, 91, 36}}, {"red_concrete", {142, 33, 33}}, {"black_concrete", {8, 10, 15}}, {"white_wool", {233, 236, 236}}, {"orange_wool", {240, 118, 19}}, {"magenta_wool", {190, 68, 201}}, {"light_blue_wool", {58, 175, 217}}, {"yellow_wool", {249, 199, 35}}, {"lime_wool", {112, 185, 25}}, {"pink_wool", {237, 141, 172}}, {"gray_wool", {62, 68, 71}}, {"light_gray_wool", {142, 142, 134}}, {"cyan_wool", {21, 137, 145}}, {"purple_wool", {122, 42, 172}}, {"blue_wool", {53, 57, 157}}, {"brown_wool", {114, 71, 40}}, {"green_wool", {85, 110, 27}}, {"red_wool", {162, 34, 35}}, {"black_wool", {21, 21, 26}}, {"white_terracotta", {209, 178, 161}}, {"orange_terracotta", {161, 83, 37}}, {"magenta_terracotta", {150, 88, 109}}, {"light_blue_terracotta", {113, 108, 137}}, {"yellow_terracotta", {186, 133, 35}}, {"lime_terracotta", {103, 117, 52}}, {"pink_terracotta", {160, 77, 78}}, {"gray_terracotta", {57, 42, 35}}, {"light_gray_terracotta", {135, 107, 98}}, {"cyan_terracotta", {87, 91, 91}}, {"purple_terracotta", {118, 70, 86}}, {"blue_terracotta", {74, 59, 91}}, {"brown_terracotta", {77, 51, 36}}, {"green_terracotta", {76, 82, 42}}, {"red_terracotta", {143, 61, 47}}, {"black_terracotta", {37, 23, 16}}};
static const int N_BLOCKS = sizeof(COLOR_MAP) / sizeof(COLOR_MAP[0]);
inline int color_distance2(const RGB &a, int r, int g, int b)
{
int dr = a.r - r;
int dg = a.g - g;
int db = a.b - b;
return dr * dr + dg * dg + db * db;
}
inline int nearest_block_cpu(int r, int g, int b)
{
int best = 0;
int best_dist = std::numeric_limits<int>::max();
for (int i = 0; i < N_BLOCKS; ++i)
{
int d = color_distance2(COLOR_MAP[i].c, r, g, b);
if (d < best_dist)
{
best_dist = d;
best = i;
}
}
return best;
}
inline void cpu_map(const std::vector<Point3> &pts, std::vector<int> &out_indices)
{
out_indices.resize(pts.size());
for (size_t i = 0; i < pts.size(); ++i)
{
int r = static_cast<int>(std::fmod(std::abs(pts[i][0]), 256.0));
int g = static_cast<int>(std::fmod(std::abs(pts[i][1]), 256.0));
int b = static_cast<int>(std::fmod(std::abs(pts[i][2]), 256.0));
out_indices[i] = nearest_block_cpu(r, g, b);
}
}
// Multithreaded CPU mapper: safe partitioning using std::thread
inline void cpu_map_mt(const std::vector<Point3> &pts, std::vector<int> &out_indices, unsigned threads = 0)
{
size_t n = pts.size();
out_indices.resize(n);
if (n == 0) return;
if (threads == 0) threads = std::max<unsigned>(1, std::thread::hardware_concurrency());
size_t base = n / threads; size_t rem = n % threads;
std::vector<std::thread> ths; ths.reserve(threads);
size_t start = 0;
for (unsigned t = 0; t < threads; ++t)
{
size_t cnt = base + (t < rem ? 1 : 0);
size_t s = start;
if (cnt == 0) break;
ths.emplace_back([s, cnt, &pts, &out_indices]() {
for (size_t i = 0; i < cnt; ++i)
{
size_t idx = s + i;
int r = static_cast<int>(std::fmod(std::abs(pts[idx][0]), 256.0));
int g = static_cast<int>(std::fmod(std::abs(pts[idx][1]), 256.0));
int b = static_cast<int>(std::fmod(std::abs(pts[idx][2]), 256.0));
out_indices[idx] = nearest_block_cpu(r, g, b);
}
});
start += cnt;
}
for (auto &th : ths) if (th.joinable()) th.join();
}
#ifdef ENABLE_CUDA
// CUDA mapping implementation (defined in cuda_kernel.cu) uses C linkage
extern "C" bool cuda_map_colors(const int *R, const int *G, const int *B, int n, int *out_block_index);
inline bool cuda_map(const std::vector<Point3> &pts, std::vector<int> &out_indices)
{
if (pts.empty())
{
out_indices.clear();
return true;
}
std::vector<int> R(pts.size()), G(pts.size()), B(pts.size());
for (size_t i = 0; i < pts.size(); ++i)
{
R[i] = static_cast<int>(std::fmod(std::abs(pts[i][0]), 256.0));
G[i] = static_cast<int>(std::fmod(std::abs(pts[i][1]), 256.0));
B[i] = static_cast<int>(std::fmod(std::abs(pts[i][2]), 256.0));
}
out_indices.resize(pts.size());
return cuda_map_colors(R.data(), G.data(), B.data(), static_cast<int>(pts.size()), out_indices.data());
}
#endif
// palette data accessor (C++ linkage, callable as block_palette::get_palette_data())
inline const unsigned char *get_palette_data()
{
static std::vector<unsigned char> palette_data;
if (palette_data.empty())
{
palette_data.reserve(N_BLOCKS * 3);
for (int i = 0; i < N_BLOCKS; ++i)
{
palette_data.push_back(static_cast<unsigned char>(COLOR_MAP[i].c.r));
palette_data.push_back(static_cast<unsigned char>(COLOR_MAP[i].c.g));
palette_data.push_back(static_cast<unsigned char>(COLOR_MAP[i].c.b));
}
}
return palette_data.data();
}
inline void map_blocks(const std::vector<Point3> &pts, std::vector<int> &out_indices, bool prefer_cuda)
{
#ifdef ENABLE_CUDA
if (prefer_cuda)
{
if (cuda_map(pts, out_indices))
return;
}
#else
(void)prefer_cuda;
#endif
// Try OpenCL first if available (and CUDA not used/failed)
#ifdef USE_OPENCL
if (opencl_map(pts, out_indices)) return;
#endif
// use multithreaded CPU mapping for better performance
unsigned hw = std::max<unsigned>(1, std::thread::hardware_concurrency());
cpu_map_mt(pts, out_indices, hw);
}
inline const char *block_name(int index)
{
if (index < 0 || index >= N_BLOCKS)
return "minecraft:stone";
return COLOR_MAP[index].name;
}
}