commit 885b0d3698d20224bd88a1bbbea9658265c5f57f
parent ba56897138cad5caf3b0816d48d990511f665751
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 4 May 2018 10:51:29 +0200
Test and fix the bintree creation
Diffstat:
4 files changed, 150 insertions(+), 5 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -89,6 +89,7 @@ if(NOT NO_TEST)
add_test(${_name} ${_name})
endfunction()
+ new_test(test_svx_bintree)
new_test(test_svx_device)
new_test(test_svx_octree)
new_test(test_svx_octree_trace_ray m)
diff --git a/src/svx_bintree.c b/src/svx_bintree.c
@@ -45,8 +45,7 @@ svx_bintree_create
size_t ivox;
res_T res = RES_OK;
- if(!dev || !lower || !upper || !nvoxels || !check_svx_voxel_desc(desc)
- || !bintree || axis < 0 || axis > 2) {
+ if(!dev || !check_svx_voxel_desc(desc) || !out_bintree || axis<0 || axis>2) {
res = RES_BAD_ARG;
goto error;
}
@@ -85,7 +84,7 @@ svx_bintree_create
FOR_EACH(ivox, 0, bintree->definition) {
size_t xyz[3] = {0, 0, 0};
- if(ivox > nvoxels) continue; /* Out of bound voxels */
+ if(ivox >= nvoxels) continue; /* Out of bound voxels */
/* Retrieve the voxel data from the caller */
xyz[axis] = ivox;
diff --git a/src/svx_tree_builder.h b/src/svx_tree_builder.h
@@ -328,7 +328,7 @@ XD(builder_add_voxel)
mcode_xor = bldr->mcode ^ vox->mcode;
/* The next voxel is not in the current node */
- if(mcode_xor >= 8) {
+ if(mcode_xor >= NCHILDREN) {
size_t ilvl;
inode = (bldr->mcode >> TREE_DIMENSION) & (NCHILDREN-1);
@@ -397,7 +397,7 @@ XD(builder_finalize)
struct buffer_index* root_id,
void* root_data)
{
- const void* data[8];
+ const void* data[NCHILDREN];
size_t inode;
size_t nleaves;
int ilvl;
diff --git a/src/test_svx_bintree.c b/src/test_svx_bintree.c
@@ -0,0 +1,145 @@
+/* Copyright (C) 2018 Université Paul Sabatier, |Meso|Star>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "svx.h"
+
+#include "test_svx_utils.h"
+#include <rsys/math.h>
+
+static void
+get(const size_t xyz[3], void* dst, void* ctx)
+{
+ double* val = dst;
+ CHK(xyz != NULL);
+ CHK(val != NULL);
+ CHK((intptr_t)ctx == 0xDECAFBAD);
+ *val = (double)xyz[SVX_AXIS_Y];
+}
+
+static int
+no_merge(const void* voxels[], const size_t nvoxels, void* ctx)
+{
+ CHK(voxels != NULL);
+ CHK(nvoxels != 0);
+ CHK((intptr_t)ctx == 0xDECAFBAD);
+ return 0; /* Merge nothing */
+}
+
+static void
+keep_max(void* dst, const void* voxels[], const size_t nvoxels, void* ctx)
+{
+ double* vox_dst = dst;
+ double max_val = -DBL_MAX;
+ size_t i;
+
+ CHK(dst != NULL);
+ CHK(voxels != NULL);
+ CHK(nvoxels != 0);
+ CHK(ctx != NULL);
+
+ FOR_EACH(i, 0, nvoxels) {
+ const double* val = voxels[i];
+ max_val = MMAX(max_val, *val);
+ }
+ *vox_dst = max_val;
+}
+
+int
+main(int argc, char** argv)
+{
+ struct svx_device* dev = NULL;
+ struct svx_tree* tree = NULL;
+ struct mem_allocator allocator;
+ struct svx_voxel_desc vox_desc = SVX_VOXEL_DESC_NULL;
+ struct svx_tree_desc tree_desc = SVX_TREE_DESC_NULL;
+ enum svx_axis axis;
+ double low, upp;
+ size_t nvxls;
+ void* ptr = (void*)0xDECAFBAD;
+ (void)argc, (void)argv;
+
+ CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK);
+ CHK(svx_device_create(NULL, &allocator, 1, &dev) == RES_OK);
+
+ low = 0.0;
+ upp = 1.0;
+ axis = SVX_AXIS_Y;
+ nvxls = 5;
+
+ vox_desc.get = get;
+ vox_desc.merge = keep_max;
+ vox_desc.challenge_merge = no_merge;
+ vox_desc.context = ptr;
+ vox_desc.size = sizeof(double);
+
+ #define NEW_TREE svx_bintree_create
+
+ CHK(NEW_TREE(dev, low, upp, nvxls, axis, &vox_desc, &tree) == RES_OK);
+ CHK(svx_tree_ref_put(tree) == RES_OK);
+
+ upp = low;
+ CHK(NEW_TREE(dev, low, upp, nvxls, axis, &vox_desc, &tree) == RES_BAD_ARG);
+ upp = 1.0;
+
+ nvxls = 0;
+ CHK(NEW_TREE(dev, low, upp, nvxls, axis, &vox_desc, &tree) == RES_BAD_ARG);
+ nvxls = 5;
+
+ vox_desc.get = NULL;
+ CHK(NEW_TREE(dev, low, upp, nvxls, axis, &vox_desc, &tree) == RES_BAD_ARG);
+ vox_desc.get = get;
+
+ vox_desc.merge = NULL;
+ CHK(NEW_TREE(dev, low, upp, nvxls, axis, &vox_desc, &tree) == RES_BAD_ARG);
+ vox_desc.merge = keep_max;
+
+ vox_desc.challenge_merge = NULL;
+ CHK(NEW_TREE(dev, low, upp, nvxls, axis, &vox_desc, &tree) == RES_BAD_ARG);
+ vox_desc.challenge_merge = no_merge;
+
+ vox_desc.size = 0;
+ CHK(NEW_TREE(dev, low, upp, nvxls, axis, &vox_desc, &tree) == RES_BAD_ARG);
+ vox_desc.size = sizeof(double);
+
+ axis = SVX_AXIS_NONE__;
+ CHK(NEW_TREE(dev, low, upp, nvxls, axis, &vox_desc, &tree) == RES_BAD_ARG);
+ axis = SVX_AXIS_Y;
+
+ CHK(NEW_TREE(NULL, low, upp, nvxls, axis, &vox_desc, &tree) == RES_BAD_ARG);
+ CHK(NEW_TREE(dev, low, upp, nvxls, axis, NULL, &tree) == RES_BAD_ARG);
+ CHK(NEW_TREE(dev, low, upp, nvxls, axis, &vox_desc, NULL) == RES_BAD_ARG);
+ CHK(NEW_TREE(dev, low, upp, nvxls, axis, &vox_desc, &tree) == RES_OK);
+
+ CHK(svx_tree_get_desc(NULL, &tree_desc) == RES_BAD_ARG);
+ CHK(svx_tree_get_desc(tree, NULL) == RES_BAD_ARG);
+ CHK(svx_tree_get_desc(tree, &tree_desc) == RES_OK);
+ CHK(tree_desc.nleaves == 5);
+ CHK(tree_desc.nvoxels == tree_desc.nleaves + 6/*#parents*/);
+ CHK(tree_desc.depth == 4);
+ CHK(tree_desc.type == SVX_BINTREE);
+ CHK(tree_desc.lower[axis] == low);
+ CHK(tree_desc.upper[axis] == upp);
+
+ #undef NEW_SCN
+
+ CHK(svx_tree_ref_put(tree) == RES_OK);
+ CHK(svx_device_ref_put(dev) == RES_OK);
+
+ check_memory_allocator(&allocator);
+ mem_shutdown_proxy_allocator(&allocator);
+ CHK(mem_allocated_size() == 0);
+ return 0;
+}
+