| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 22 апр. 2020 г. |
| 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/PullParser.h> |
| 25 |
#include <stdarg.h> |
| 26 |
|
| 27 |
namespace lsp |
| 28 |
{ |
| 29 |
using namespace lsp::obj; |
| 30 |
} |
| 31 |
|
| 32 |
UTEST_BEGIN("runtime.fmt.obj", pullparser) |
| 33 |
bool ck_v(const event_t *ev, float x, float y, float z, float w) |
| 34 |
{ |
| 35 |
if (ev == NULL) |
| 36 |
return false; |
| 37 |
return |
| 38 |
float_equals_absolute(x, ev->vertex.x) && |
| 39 |
float_equals_absolute(y, ev->vertex.y) && |
| 40 |
float_equals_absolute(z, ev->vertex.z) && |
| 41 |
float_equals_absolute(w, ev->vertex.w); |
| 42 |
} |
| 43 |
|
| 44 |
bool ck_vn(const event_t *ev, float dx, float dy, float dz, float dw) |
| 45 |
{ |
| 46 |
if (ev == NULL) |
| 47 |
return false; |
| 48 |
return |
| 49 |
float_equals_absolute(dx, ev->normal.dx) && |
| 50 |
float_equals_absolute(dy, ev->normal.dy) && |
| 51 |
float_equals_absolute(dz, ev->normal.dz) && |
| 52 |
float_equals_absolute(dw, ev->normal.dw); |
| 53 |
} |
| 54 |
|
| 55 |
bool ck_vt(const event_t *ev, float u, float v, float w) |
| 56 |
{ |
| 57 |
if (ev == NULL) |
| 58 |
return false; |
| 59 |
return |
| 60 |
float_equals_absolute(u, ev->texcoord.u) && |
| 61 |
float_equals_absolute(v, ev->texcoord.v) && |
| 62 |
float_equals_absolute(w, ev->texcoord.w); |
| 63 |
} |
| 64 |
|
| 65 |
bool ck_iv(const lltl::darray<index_t> *xv, size_t n ...) |
| 66 |
{ |
| 67 |
if ((xv == NULL) || (xv->size() != n)) |
| 68 |
return false; |
| 69 |
|
| 70 |
va_list v; |
| 71 |
va_start(v, n); |
| 72 |
|
| 73 |
bool result = true; |
| 74 |
for (size_t i=0; i<n; ++i) |
| 75 |
{ |
| 76 |
int idx = va_arg(v, int); |
| 77 |
const index_t *v = xv->uget(i); |
| 78 |
if (*v != idx) |
| 79 |
{ |
| 80 |
result = false; |
| 81 |
break; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
va_end(v); |
| 86 |
return result; |
| 87 |
} |
| 88 |
|
| 89 |
void test_simple_obj_file() |
| 90 |
{ |
| 91 |
static const char *contents= |
| 92 |
"# Some comment\n" |
| 93 |
"o Triangle 1\n" |
| 94 |
"v -2 -1 -1 #comment\n" |
| 95 |
"v 2 -1 -1\n" |
| 96 |
"v 0 2 -1\n" |
| 97 |
"vn 0 0 1\n" |
| 98 |
"f 1//1 2//1 3//1\n" |
| 99 |
"\n" |
| 100 |
"o Triangle 2\r\n" |
| 101 |
"v -2.0 -1.0 -2.0\r\n" |
| 102 |
"v 2.0 -1.0 -2.0\r\n" |
| 103 |
"v 0.0 2.0 -2.0\r\n" |
| 104 |
"vt -2.0 -1.0\r\n" |
| 105 |
"vt 2.0 -1.0\r\n" |
| 106 |
"vt 0.0 2.0\r\n" |
| 107 |
"vn 0.0 0.0 1.0\r\n" |
| 108 |
"vn 0.1 0.1 1.0\r\n" |
| 109 |
"f 4/1/2 5/2/2 6/3/3\r\n" |
| 110 |
"f -1/-1/-1 -2/-2/-1 -3/-3/-2\r\n" |
| 111 |
"\r\n" |
| 112 |
"\r\n"; |
| 113 |
|
| 114 |
PullParser p; |
| 115 |
event_t ev; |
| 116 |
const event_t *pev; |
| 117 |
|
| 118 |
UTEST_ASSERT(p.current() == NULL); |
| 119 |
UTEST_ASSERT(p.current(&ev) == STATUS_CLOSED); |
| 120 |
UTEST_ASSERT(p.wrap(contents, "UTF-8") == STATUS_OK); |
| 121 |
UTEST_ASSERT(p.current() == NULL); |
| 122 |
UTEST_ASSERT(p.current(&ev) == STATUS_NO_DATA); |
| 123 |
|
| 124 |
// Object 1 |
| 125 |
UTEST_ASSERT(p.next() == STATUS_OK); |
| 126 |
UTEST_ASSERT((pev = p.current()) != NULL); |
| 127 |
UTEST_ASSERT(pev->type == EV_OBJECT); |
| 128 |
UTEST_ASSERT(pev->name.equals_ascii("Triangle 1")); |
| 129 |
|
| 130 |
UTEST_ASSERT(p.next() == STATUS_OK); |
| 131 |
UTEST_ASSERT((pev = p.current()) != NULL); |
| 132 |
UTEST_ASSERT(pev->type == EV_VERTEX); |
| 133 |
UTEST_ASSERT(ck_v(pev, -2.0f, -1.0f, -1.0f, 1.0f)); |
| 134 |
UTEST_ASSERT(p.next() == STATUS_OK); |
| 135 |
UTEST_ASSERT((pev = p.current()) != NULL); |
| 136 |
UTEST_ASSERT(pev->type == EV_VERTEX); |
| 137 |
UTEST_ASSERT(ck_v(pev, 2.0f, -1.0f, -1.0f, 1.0f)); |
| 138 |
UTEST_ASSERT(p.next() == STATUS_OK); |
| 139 |
UTEST_ASSERT((pev = p.current()) != NULL); |
| 140 |
UTEST_ASSERT(pev->type == EV_VERTEX); |
| 141 |
UTEST_ASSERT(ck_v(pev, 0.0f, 2.0f, -1.0f, 1.0f)); |
| 142 |
|
| 143 |
UTEST_ASSERT(p.next() == STATUS_OK); |
| 144 |
UTEST_ASSERT((pev = p.current()) != NULL); |
| 145 |
UTEST_ASSERT(pev->type == EV_NORMAL); |
| 146 |
UTEST_ASSERT(ck_vn(pev, 0.0f, 0.0f, 1.0f, 0.0f)); |
| 147 |
|
| 148 |
UTEST_ASSERT(p.next() == STATUS_OK); |
| 149 |
UTEST_ASSERT((pev = p.current()) != NULL); |
| 150 |
UTEST_ASSERT(pev->type == EV_FACE); |
| 151 |
UTEST_ASSERT(ck_iv(&pev->ivertex, 3, 0, 1, 2)); |
| 152 |
UTEST_ASSERT(ck_iv(&pev->itexcoord, 3, -1, -1, -1)); |
| 153 |
UTEST_ASSERT(ck_iv(&pev->inormal, 3, 0, 0, 0)); |
| 154 |
|
| 155 |
UTEST_ASSERT(p.current(&ev) == STATUS_OK); |
| 156 |
UTEST_ASSERT(ev.type == EV_FACE); |
| 157 |
UTEST_ASSERT(ck_iv(&ev.ivertex, 3, 0, 1, 2)); |
| 158 |
UTEST_ASSERT(ck_iv(&ev.itexcoord, 3, -1, -1, -1)); |
| 159 |
UTEST_ASSERT(ck_iv(&ev.inormal, 3, 0, 0, 0)); |
| 160 |
|
| 161 |
// Object 2 |
| 162 |
UTEST_ASSERT(p.next(&ev) == STATUS_OK); |
| 163 |
UTEST_ASSERT(ev.type == EV_OBJECT); |
| 164 |
UTEST_ASSERT(ev.name.equals_ascii("Triangle 2")); |
| 165 |
|
| 166 |
UTEST_ASSERT(p.next(&ev) == STATUS_OK); |
| 167 |
UTEST_ASSERT(ev.type == EV_VERTEX); |
| 168 |
UTEST_ASSERT(ck_v(&ev, -2.0f, -1.0f, -2.0f, 1.0f)); |
| 169 |
UTEST_ASSERT(p.next(&ev) == STATUS_OK); |
| 170 |
UTEST_ASSERT(ev.type == EV_VERTEX); |
| 171 |
UTEST_ASSERT(ck_v(&ev, 2.0f, -1.0f, -2.0f, 1.0f)); |
| 172 |
UTEST_ASSERT(p.next(&ev) == STATUS_OK); |
| 173 |
UTEST_ASSERT(ev.type == EV_VERTEX); |
| 174 |
UTEST_ASSERT(ck_v(&ev, 0.0f, 2.0f, -2.0f, 1.0f)); |
| 175 |
|
| 176 |
UTEST_ASSERT(p.next(&ev) == STATUS_OK); |
| 177 |
UTEST_ASSERT(ev.type == EV_TEXCOORD); |
| 178 |
UTEST_ASSERT(ck_vt(&ev, -2.0f, -1.0f, 0.0f)); |
| 179 |
UTEST_ASSERT(p.next(&ev) == STATUS_OK); |
| 180 |
UTEST_ASSERT(ev.type == EV_TEXCOORD); |
| 181 |
UTEST_ASSERT(ck_vt(&ev, 2.0f, -1.0f, 0.0f)); |
| 182 |
UTEST_ASSERT(p.next(&ev) == STATUS_OK); |
| 183 |
UTEST_ASSERT(ev.type == EV_TEXCOORD); |
| 184 |
UTEST_ASSERT(ck_vt(&ev, 0.0f, 2.0f, 0.0f)); |
| 185 |
|
| 186 |
UTEST_ASSERT(p.next(&ev) == STATUS_OK); |
| 187 |
UTEST_ASSERT(ev.type == EV_NORMAL); |
| 188 |
UTEST_ASSERT(ck_vt(&ev, 0.0f, 0.0f, 1.0f)); |
| 189 |
|
| 190 |
UTEST_ASSERT(p.next(&ev) == STATUS_OK); |
| 191 |
UTEST_ASSERT(ev.type == EV_NORMAL); |
| 192 |
UTEST_ASSERT(ck_vt(&ev, 0.1f, 0.1f, 1.0f)); |
| 193 |
|
| 194 |
UTEST_ASSERT(p.next(&ev) == STATUS_OK); |
| 195 |
UTEST_ASSERT(ev.type == EV_FACE); |
| 196 |
UTEST_ASSERT(ck_iv(&ev.ivertex, 3, 3, 4, 5)); |
| 197 |
UTEST_ASSERT(ck_iv(&ev.itexcoord, 3, 0, 1, 2)); |
| 198 |
UTEST_ASSERT(ck_iv(&ev.inormal, 3, 1, 1, 2)); |
| 199 |
|
| 200 |
UTEST_ASSERT(p.next(&ev) == STATUS_OK); |
| 201 |
UTEST_ASSERT(ev.type == EV_FACE); |
| 202 |
UTEST_ASSERT(ck_iv(&ev.ivertex, 3, 5, 4, 3)); |
| 203 |
UTEST_ASSERT(ck_iv(&ev.itexcoord, 3, 2, 1, 0)); |
| 204 |
UTEST_ASSERT(ck_iv(&ev.inormal, 3, 2, 2, 1)); |
| 205 |
|
| 206 |
// End of file |
| 207 |
UTEST_ASSERT(p.next() == STATUS_EOF); |
| 208 |
UTEST_ASSERT(p.next() == STATUS_EOF); |
| 209 |
|
| 210 |
UTEST_ASSERT(p.current() == NULL); |
| 211 |
UTEST_ASSERT(p.current(&ev) == STATUS_NO_DATA); |
| 212 |
|
| 213 |
// Close file |
| 214 |
UTEST_ASSERT(p.close() == STATUS_OK); |
| 215 |
UTEST_ASSERT(p.current() == NULL); |
| 216 |
UTEST_ASSERT(p.current(&ev) == STATUS_CLOSED); |
| 217 |
} |
| 218 |
|
| 219 |
void test_load_obj_file(const char *fname) |
| 220 |
{ |
| 221 |
io::Path path; |
| 222 |
UTEST_ASSERT(path.fmt("%s/%s", resources(), fname) > 0); |
| 223 |
|
| 224 |
printf("Reading file %s\n", path.as_native()); |
| 225 |
PullParser p; |
| 226 |
status_t res; |
| 227 |
|
| 228 |
UTEST_ASSERT(p.open(&path) == STATUS_OK); |
| 229 |
|
| 230 |
while ((res = p.next()) == STATUS_OK) |
| 231 |
{ |
| 232 |
const char *type = NULL; |
| 233 |
UTEST_ASSERT(p.current() != NULL); |
| 234 |
switch (p.current()->type) |
| 235 |
{ |
| 236 |
case EV_VERTEX: type = "vertex"; break; |
| 237 |
case EV_NORMAL: type = "normal"; break; |
| 238 |
case EV_FACE: type = "face"; break; |
| 239 |
case EV_OBJECT: type = "object"; break; |
| 240 |
default: UTEST_FAIL_MSG("Unknown event type: %d", int(p.current()->type)); |
| 241 |
} |
| 242 |
printf(" read event: %s\n", type); |
| 243 |
} |
| 244 |
|
| 245 |
UTEST_ASSERT(res == STATUS_EOF); |
| 246 |
UTEST_ASSERT(p.close() == STATUS_OK); |
| 247 |
} |
| 248 |
|
| 249 |
UTEST_MAIN |
| 250 |
{ |
| 251 |
printf("Testing simple OBJ file...\n"); |
| 252 |
test_simple_obj_file(); |
| 253 |
|
| 254 |
printf("Testing load of complicated OBJ file...\n"); |
| 255 |
test_load_obj_file("fmt/obj/swimming-pool.obj"); |
| 256 |
} |
| 257 |
|
| 258 |
UTEST_END |
| 259 |
|
| 260 |
|