| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 23 июл. 2025 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/test-fw/utest.h> |
| 23 |
#include <lsp-plug.in/test-fw/helpers.h> |
| 24 |
#include <lsp-plug.in/fmt/obj/Compressor.h> |
| 25 |
#include <lsp-plug.in/fmt/obj/Decompressor.h> |
| 26 |
#include <lsp-plug.in/fmt/obj/PushParser.h> |
| 27 |
#include <lsp-plug.in/lltl/parray.h> |
| 28 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 29 |
|
| 30 |
namespace lsp |
| 31 |
{ |
| 32 |
namespace |
| 33 |
{ |
| 34 |
enum call_type_t |
| 35 |
{ |
| 36 |
EV_BEGIN_OBJECT, |
| 37 |
EV_END_OBJECT, |
| 38 |
EV_END_OF_DATA, |
| 39 |
EV_ADD_VERTEX, |
| 40 |
EV_ADD_PARAM_VERTEX, |
| 41 |
EV_ADD_NORMAL, |
| 42 |
EV_ADD_TEXTURE_VERTEX, |
| 43 |
EV_ADD_FACE, |
| 44 |
EV_ADD_POINTS, |
| 45 |
EV_ADD_LINE |
| 46 |
}; |
| 47 |
|
| 48 |
typedef struct call_t |
| 49 |
{ |
| 50 |
call_type_t type; |
| 51 |
union |
| 52 |
{ |
| 53 |
struct { float x, y, z, w; } vertex; |
| 54 |
struct { float dx, dy, dz, dw; } normal; |
| 55 |
struct { float u, v, w; } texcoord; |
| 56 |
char *name; |
| 57 |
struct { |
| 58 |
size_t count; |
| 59 |
obj::index_t *v; |
| 60 |
obj::index_t *t; |
| 61 |
obj::index_t *n; |
| 62 |
} indices; |
| 63 |
}; |
| 64 |
} call_t; |
| 65 |
|
| 66 |
class ObjRecorder: public obj::IObjHandler |
| 67 |
{ |
| 68 |
protected: |
| 69 |
lltl::parray<call_t> vCalls; |
| 70 |
|
| 71 |
protected: |
| 72 |
call_t *add_call(call_type_t type, size_t bytes) |
| 73 |
{ |
| 74 |
call_t *call = reinterpret_cast<call_t *>(malloc(sizeof(call_t) + bytes)); |
| 75 |
if (call == NULL) |
| 76 |
return NULL; |
| 77 |
if (!vCalls.add(call)) |
| 78 |
{ |
| 79 |
free(call); |
| 80 |
return NULL; |
| 81 |
} |
| 82 |
|
| 83 |
bzero(call, sizeof(call_t)); |
| 84 |
call->type = type; |
| 85 |
|
| 86 |
switch (type) |
| 87 |
{ |
| 88 |
case EV_BEGIN_OBJECT: |
| 89 |
call->name = reinterpret_cast<char *>(&call[1]); |
| 90 |
break; |
| 91 |
case EV_ADD_FACE: |
| 92 |
call->indices.v = reinterpret_cast<obj::index_t *>(&call[1]); |
| 93 |
call->indices.t = &call->indices.v[bytes / (3 * sizeof(obj::index_t))]; |
| 94 |
call->indices.n = &call->indices.t[bytes / (3 * sizeof(obj::index_t))]; |
| 95 |
break; |
| 96 |
|
| 97 |
case EV_ADD_POINTS: |
| 98 |
call->indices.v = reinterpret_cast<obj::index_t *>(&call[1]); |
| 99 |
break; |
| 100 |
|
| 101 |
case EV_ADD_LINE: |
| 102 |
call->indices.v = reinterpret_cast<obj::index_t *>(&call[1]); |
| 103 |
call->indices.t = &call->indices.v[bytes / (2 * sizeof(obj::index_t))]; |
| 104 |
break; |
| 105 |
|
| 106 |
default: |
| 107 |
break; |
| 108 |
} |
| 109 |
|
| 110 |
return call; |
| 111 |
} |
| 112 |
|
| 113 |
public: |
| 114 |
ObjRecorder() |
| 115 |
{ |
| 116 |
} |
| 117 |
|
| 118 |
virtual ~ObjRecorder() override |
| 119 |
{ |
| 120 |
for (size_t i=0, n = vCalls.size(); i<n; ++i) |
| 121 |
{ |
| 122 |
call_t *call = vCalls.uget(i); |
| 123 |
free(call); |
| 124 |
} |
| 125 |
vCalls.flush(); |
| 126 |
} |
| 127 |
|
| 128 |
public: |
| 129 |
virtual status_t begin_object(const char *name) override |
| 130 |
{ |
| 131 |
const size_t bytes = strlen(name) + 1; |
| 132 |
call_t *call = add_call(EV_BEGIN_OBJECT, bytes); |
| 133 |
if (call == NULL) |
| 134 |
return STATUS_NO_MEM; |
| 135 |
memcpy(call->name, name, bytes); |
| 136 |
return STATUS_OK; |
| 137 |
} |
| 138 |
|
| 139 |
virtual status_t begin_object(const LSPString *name) override |
| 140 |
{ |
| 141 |
return begin_object(name->get_utf8()); |
| 142 |
} |
| 143 |
|
| 144 |
virtual status_t end_object() override |
| 145 |
{ |
| 146 |
return (add_call(EV_END_OBJECT, 0) != NULL) ? STATUS_OK : STATUS_NO_MEM; |
| 147 |
} |
| 148 |
|
| 149 |
virtual status_t end_of_data() override |
| 150 |
{ |
| 151 |
return (add_call(EV_END_OF_DATA, 0) != NULL) ? STATUS_OK : STATUS_NO_MEM; |
| 152 |
} |
| 153 |
|
| 154 |
virtual ssize_t add_vertex(float x, float y, float z, float w) override |
| 155 |
{ |
| 156 |
call_t *call = add_call(EV_ADD_VERTEX, 0); |
| 157 |
if (call == NULL) |
| 158 |
return STATUS_NO_MEM; |
| 159 |
|
| 160 |
call->vertex.x = x; |
| 161 |
call->vertex.y = y; |
| 162 |
call->vertex.z = z; |
| 163 |
call->vertex.w = w; |
| 164 |
|
| 165 |
return STATUS_OK; |
| 166 |
} |
| 167 |
|
| 168 |
virtual ssize_t add_param_vertex(float x, float y, float z, float w) override |
| 169 |
{ |
| 170 |
call_t *call = add_call(EV_ADD_PARAM_VERTEX, 0); |
| 171 |
if (call == NULL) |
| 172 |
return STATUS_NO_MEM; |
| 173 |
|
| 174 |
call->vertex.x = x; |
| 175 |
call->vertex.y = y; |
| 176 |
call->vertex.z = z; |
| 177 |
call->vertex.w = w; |
| 178 |
|
| 179 |
return STATUS_OK; |
| 180 |
} |
| 181 |
|
| 182 |
virtual ssize_t add_normal(float nx, float ny, float nz, float nw) override |
| 183 |
{ |
| 184 |
call_t *call = add_call(EV_ADD_NORMAL, 0); |
| 185 |
if (call == NULL) |
| 186 |
return STATUS_NO_MEM; |
| 187 |
|
| 188 |
call->normal.dx = nx; |
| 189 |
call->normal.dy = ny; |
| 190 |
call->normal.dz = nz; |
| 191 |
call->normal.dw = nw; |
| 192 |
|
| 193 |
return STATUS_OK; |
| 194 |
} |
| 195 |
|
| 196 |
virtual ssize_t add_texture_vertex(float u, float v, float w) override |
| 197 |
{ |
| 198 |
call_t *call = add_call(EV_ADD_TEXTURE_VERTEX, 0); |
| 199 |
if (call == NULL) |
| 200 |
return STATUS_NO_MEM; |
| 201 |
|
| 202 |
call->texcoord.u = u; |
| 203 |
call->texcoord.v = v; |
| 204 |
call->texcoord.w = w; |
| 205 |
|
| 206 |
return STATUS_OK; |
| 207 |
} |
| 208 |
|
| 209 |
virtual ssize_t add_face(const obj::index_t *vv, const obj::index_t *vn, const obj::index_t *vt, size_t n) override |
| 210 |
{ |
| 211 |
call_t *call = add_call(EV_ADD_FACE, n * sizeof(obj::index_t) * 3); |
| 212 |
if (call == NULL) |
| 213 |
return STATUS_NO_MEM; |
| 214 |
|
| 215 |
call->indices.count = n; |
| 216 |
memcpy(call->indices.v, vv, n * sizeof(obj::index_t)); |
| 217 |
memcpy(call->indices.t, vt, n * sizeof(obj::index_t)); |
| 218 |
memcpy(call->indices.n, vn, n * sizeof(obj::index_t)); |
| 219 |
|
| 220 |
return STATUS_OK; |
| 221 |
} |
| 222 |
|
| 223 |
virtual ssize_t add_points(const obj::index_t *vv, size_t n) override |
| 224 |
{ |
| 225 |
call_t *call = add_call(EV_ADD_POINTS, n * sizeof(obj::index_t)); |
| 226 |
if (call == NULL) |
| 227 |
return STATUS_NO_MEM; |
| 228 |
|
| 229 |
call->indices.count = n; |
| 230 |
memcpy(call->indices.v, vv, n * sizeof(obj::index_t)); |
| 231 |
|
| 232 |
return STATUS_OK; |
| 233 |
} |
| 234 |
|
| 235 |
virtual ssize_t add_line(const obj::index_t *vv, const obj::index_t *vt, size_t n) override |
| 236 |
{ |
| 237 |
call_t *call = add_call(EV_ADD_LINE, n * sizeof(obj::index_t) * 2); |
| 238 |
if (call == NULL) |
| 239 |
return STATUS_NO_MEM; |
| 240 |
|
| 241 |
call->indices.count = n; |
| 242 |
memcpy(call->indices.v, vv, n * sizeof(obj::index_t)); |
| 243 |
memcpy(call->indices.t, vt, n * sizeof(obj::index_t)); |
| 244 |
|
| 245 |
return STATUS_OK; |
| 246 |
} |
| 247 |
|
| 248 |
public: |
| 249 |
inline const call_t *get(size_t i) const |
| 250 |
{ |
| 251 |
return vCalls.get(i); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
}; |
| 256 |
} |
| 257 |
} /* namespace lsp */ |
| 258 |
|
| 259 |
UTEST_BEGIN("runtime.fmt.obj", compressor) |
| 260 |
|
| 261 |
status_t read_obj_file(ObjRecorder *data, const io::Path *path) |
| 262 |
{ |
| 263 |
obj::PushParser parser; |
| 264 |
return parser.parse_file(data, path); |
| 265 |
} |
| 266 |
|
| 267 |
status_t read_compressed_obj_file(ObjRecorder *data, const io::Path *path) |
| 268 |
{ |
| 269 |
obj::Decompressor parser; |
| 270 |
return parser.parse_file(data, path); |
| 271 |
} |
| 272 |
|
| 273 |
ssize_t compare(const ObjRecorder *a, const ObjRecorder *b) |
| 274 |
{ |
| 275 |
for (size_t i=0; ; ++i) |
| 276 |
{ |
| 277 |
const call_t *ca = a->get(i); |
| 278 |
const call_t *cb = b->get(i); |
| 279 |
|
| 280 |
if (ca == NULL) |
| 281 |
{ |
| 282 |
if (cb == NULL) |
| 283 |
return -1; |
| 284 |
printf("Missing record #%d for second file\n", int(i)); |
| 285 |
return i; |
| 286 |
} |
| 287 |
else if (cb == NULL) |
| 288 |
{ |
| 289 |
printf("Missing record #%d for first file\n", int(i)); |
| 290 |
return i; |
| 291 |
} |
| 292 |
|
| 293 |
if (ca->type != cb->type) |
| 294 |
{ |
| 295 |
printf("Record #%d type %d does not match record type %d\n", |
| 296 |
int(i), int(ca->type), int(cb->type)); |
| 297 |
return i; |
| 298 |
} |
| 299 |
|
| 300 |
switch (ca->type) |
| 301 |
{ |
| 302 |
case EV_BEGIN_OBJECT: |
| 303 |
if (strcmp(ca->name, cb->name) != 0) |
| 304 |
{ |
| 305 |
printf("Record #%d object name '%s' does not match object name '%s'\n", |
| 306 |
int(i), ca->name, cb->name); |
| 307 |
return i; |
| 308 |
} |
| 309 |
break; |
| 310 |
|
| 311 |
case EV_ADD_VERTEX: |
| 312 |
if ((ca->vertex.x != cb->vertex.x) || |
| 313 |
(ca->vertex.y != cb->vertex.y) || |
| 314 |
(ca->vertex.z != cb->vertex.z) || |
| 315 |
(ca->vertex.w != cb->vertex.w)) |
| 316 |
{ |
| 317 |
printf("Record #%d vertex {%f, %f, %f, %f} does not match vertex {%f, %f, %f, %f}\n", |
| 318 |
int(i), |
| 319 |
ca->vertex.x, ca->vertex.y, ca->vertex.z, ca->vertex.w, |
| 320 |
cb->vertex.x, cb->vertex.y, cb->vertex.z, cb->vertex.w); |
| 321 |
return i; |
| 322 |
} |
| 323 |
break; |
| 324 |
|
| 325 |
case EV_ADD_PARAM_VERTEX: |
| 326 |
if ((ca->vertex.x != cb->vertex.x) || |
| 327 |
(ca->vertex.y != cb->vertex.y) || |
| 328 |
(ca->vertex.z != cb->vertex.z) || |
| 329 |
(ca->vertex.w != cb->vertex.w)) |
| 330 |
{ |
| 331 |
printf("Record #%d parameter vertex {%f, %f, %f, %f} does not match parameter vertex {%f, %f, %f, %f}\n", |
| 332 |
int(i), |
| 333 |
ca->vertex.x, ca->vertex.y, ca->vertex.z, ca->vertex.w, |
| 334 |
cb->vertex.x, cb->vertex.y, cb->vertex.z, cb->vertex.w); |
| 335 |
return i; |
| 336 |
} |
| 337 |
break; |
| 338 |
|
| 339 |
case EV_ADD_NORMAL: |
| 340 |
if ((ca->normal.dx != cb->normal.dx) || |
| 341 |
(ca->normal.dy != cb->normal.dy) || |
| 342 |
(ca->normal.dz != cb->normal.dz) || |
| 343 |
(ca->normal.dw != cb->normal.dw)) |
| 344 |
{ |
| 345 |
printf("Record #%d normal {%f, %f, %f, %f} does not match normal {%f, %f, %f, %f}\n", |
| 346 |
int(i), |
| 347 |
ca->normal.dx, ca->normal.dy, ca->normal.dz, ca->normal.dw, |
| 348 |
cb->normal.dx, cb->normal.dy, cb->normal.dz, cb->normal.dw); |
| 349 |
return i; |
| 350 |
} |
| 351 |
break; |
| 352 |
|
| 353 |
case EV_ADD_TEXTURE_VERTEX: |
| 354 |
if ((ca->texcoord.u != cb->texcoord.u) || |
| 355 |
(ca->texcoord.v != cb->texcoord.v) || |
| 356 |
(ca->texcoord.w != cb->texcoord.w)) |
| 357 |
{ |
| 358 |
printf("Record #%d normal {%f, %f, %f} does not match normal {%f, %f, %f}\n", |
| 359 |
int(i), |
| 360 |
ca->texcoord.u, ca->texcoord.v, ca->texcoord.w, |
| 361 |
cb->texcoord.u, cb->texcoord.v, cb->texcoord.w); |
| 362 |
return i; |
| 363 |
} |
| 364 |
break; |
| 365 |
|
| 366 |
case EV_ADD_FACE: |
| 367 |
if (ca->indices.count != cb->indices.count) |
| 368 |
{ |
| 369 |
printf("Record #%d number of face indices %d does not match %d\n", |
| 370 |
int(i), int(ca->indices.count), int(cb->indices.count)); |
| 371 |
return i; |
| 372 |
} |
| 373 |
for (size_t j=0; j<ca->indices.count; ++j) |
| 374 |
{ |
| 375 |
if (ca->indices.v[j] != cb->indices.v[j]) |
| 376 |
{ |
| 377 |
printf("Record #%d face vertex index #%d value %d differs to %d\n", |
| 378 |
int(i), int(j), int(ca->indices.v[j]), int(cb->indices.v[j])); |
| 379 |
return i; |
| 380 |
} |
| 381 |
} |
| 382 |
for (size_t j=0; j<ca->indices.count; ++j) |
| 383 |
{ |
| 384 |
if (ca->indices.t[j] != cb->indices.t[j]) |
| 385 |
{ |
| 386 |
printf("Record #%d face texture index #%d value %d differs to %d\n", |
| 387 |
int(i), int(j), int(ca->indices.t[j]), int(cb->indices.t[j])); |
| 388 |
return i; |
| 389 |
} |
| 390 |
} |
| 391 |
for (size_t j=0; j<ca->indices.count; ++j) |
| 392 |
{ |
| 393 |
if (ca->indices.n[j] != cb->indices.n[j]) |
| 394 |
{ |
| 395 |
printf("Record #%d face normal index #%d value %d differs to %d\n", |
| 396 |
int(i), int(j), int(ca->indices.n[j]), int(cb->indices.n[j])); |
| 397 |
return i; |
| 398 |
} |
| 399 |
} |
| 400 |
break; |
| 401 |
|
| 402 |
case EV_ADD_POINTS: |
| 403 |
if (ca->indices.count != cb->indices.count) |
| 404 |
{ |
| 405 |
printf("Record #%d number of point indices %d does not match %d\n", |
| 406 |
int(i), int(ca->indices.count), int(cb->indices.count)); |
| 407 |
return i; |
| 408 |
} |
| 409 |
for (size_t j=0; j<ca->indices.count; ++j) |
| 410 |
{ |
| 411 |
if (ca->indices.v[j] != cb->indices.v[j]) |
| 412 |
{ |
| 413 |
printf("Record #%d point vertex index #%d value %d differs to %d\n", |
| 414 |
int(i), int(j), int(ca->indices.v[j]), int(cb->indices.v[j])); |
| 415 |
return i; |
| 416 |
} |
| 417 |
} |
| 418 |
break; |
| 419 |
|
| 420 |
case EV_ADD_LINE: |
| 421 |
if (ca->indices.count != cb->indices.count) |
| 422 |
{ |
| 423 |
printf("Record #%d number of line indices %d does not match %d\n", |
| 424 |
int(i), int(ca->indices.count), int(cb->indices.count)); |
| 425 |
return i; |
| 426 |
} |
| 427 |
|
| 428 |
for (size_t j=0; j<ca->indices.count; ++j) |
| 429 |
{ |
| 430 |
if (ca->indices.v[j] != cb->indices.v[j]) |
| 431 |
{ |
| 432 |
printf("Record #%d line vertex index #%d value %d differs to %d\n", |
| 433 |
int(i), int(j), int(ca->indices.v[j]), int(cb->indices.v[j])); |
| 434 |
return i; |
| 435 |
} |
| 436 |
} |
| 437 |
for (size_t j=0; j<ca->indices.count; ++j) |
| 438 |
{ |
| 439 |
if (ca->indices.t[j] != cb->indices.t[j]) |
| 440 |
{ |
| 441 |
printf("Record #%d line texture index #%d value %d differs to %d\n", |
| 442 |
int(i), int(j), int(ca->indices.t[j]), int(cb->indices.t[j])); |
| 443 |
return i; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
break; |
| 448 |
|
| 449 |
case EV_END_OF_DATA: |
| 450 |
case EV_END_OBJECT: |
| 451 |
default: |
| 452 |
break; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
return -1; |
| 457 |
} |
| 458 |
|
| 459 |
void test_compress_obj_file(const char *fname, const char *postfix) |
| 460 |
{ |
| 461 |
io::Path src, dst; |
| 462 |
UTEST_ASSERT(src.fmt("%s/%s", resources(), fname) > 0); |
| 463 |
UTEST_ASSERT(dst.fmt("%s/utest-%s-%s.cobj", tempdir(), full_name(), postfix) > 0); |
| 464 |
|
| 465 |
printf("Compressing %s -> %s\n", src.as_native(), dst.as_native()); |
| 466 |
|
| 467 |
obj::Compressor c; |
| 468 |
obj::PushParser p; |
| 469 |
|
| 470 |
UTEST_ASSERT(c.set_buffer_size(7) == STATUS_OK); |
| 471 |
UTEST_ASSERT(c.open(&dst, io::File::FM_WRITE_NEW) == STATUS_OK); |
| 472 |
UTEST_ASSERT(p.parse_file(&c, &src) == STATUS_OK); |
| 473 |
UTEST_ASSERT(c.close() == STATUS_OK); |
| 474 |
|
| 475 |
const wssize_t src_size = src.size(); |
| 476 |
UTEST_ASSERT(src_size >= 0); |
| 477 |
const wssize_t dst_size = dst.size(); |
| 478 |
UTEST_ASSERT(dst_size >= 0); |
| 479 |
|
| 480 |
printf("Stats: uncompressed size = %d, compressed size = %d, ratio: %.3f\n", |
| 481 |
int(src_size), int(dst_size), |
| 482 |
double(src_size) / double(dst_size)); |
| 483 |
|
| 484 |
printf("Validating result...\n"); |
| 485 |
ObjRecorder osrc, odst; |
| 486 |
|
| 487 |
status_t res = read_obj_file(&osrc, &src); |
| 488 |
UTEST_ASSERT_MSG(res == STATUS_OK, "read_obj_file failed with code=%d", int(res)); |
| 489 |
res = read_compressed_obj_file(&odst, &dst); |
| 490 |
UTEST_ASSERT_MSG(res == STATUS_OK, "read_compressed_obj_file failed with code=%d", int(res)); |
| 491 |
ssize_t diff = compare(&osrc, &odst); |
| 492 |
UTEST_ASSERT_MSG(diff < 0, "Source and destination records #%d differ", int(diff)); |
| 493 |
} |
| 494 |
|
| 495 |
UTEST_MAIN |
| 496 |
{ |
| 497 |
printf("Testing compression of complicated OBJ files...\n"); |
| 498 |
test_compress_obj_file("fmt/obj/parking.obj", "parking"); |
| 499 |
test_compress_obj_file("fmt/obj/coliseum.obj", "coliseum"); |
| 500 |
test_compress_obj_file("fmt/obj/forest.obj", "forest"); |
| 501 |
test_compress_obj_file("fmt/obj/cooling-tower.obj", "cooling-tower"); |
| 502 |
test_compress_obj_file("fmt/obj/church.obj", "church"); |
| 503 |
} |
| 504 |
|
| 505 |
UTEST_END |
| 506 |
|
| 507 |
|