star-vx

Structuring voxels for ray-tracing
git clone git://git.meso-star.fr/star-vx.git
Log | Files | Refs | README | LICENSE

commit 985c112d6b0876f0f8f6f32b24060a64d7979b50
parent eae934e11339dc57596ea4f902d6dde1275461d2
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed,  4 Apr 2018 13:45:23 +0200

Move the dump_data function to the utils header of the tests

Diffstat:
Msrc/test_svx_octree.c | 77+----------------------------------------------------------------------------
Msrc/test_svx_octree_trace_ray.c | 17++++++++++++++++-
Msrc/test_svx_utils.h | 104+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 121 insertions(+), 77 deletions(-)

diff --git a/src/test_svx_octree.c b/src/test_svx_octree.c @@ -143,49 +143,6 @@ check_leaves } static void -write_points - (const struct svx_voxel* leaf, - const size_t ileaf, - void* context) -{ - FILE* stream = context; - (void)ileaf; - CHK(stream != NULL); - CHK(leaf != NULL); - fprintf(stream, - "%g %g %g\n%g %g %g\n%g %g %g\n%g %g %g\n" - "%g %g %g\n%g %g %g\n%g %g %g\n%g %g %g\n", - leaf->lower[0], leaf->lower[1], leaf->lower[2], - leaf->upper[0], leaf->lower[1], leaf->lower[2], - leaf->lower[0], leaf->upper[1], leaf->lower[2], - leaf->upper[0], leaf->upper[1], leaf->lower[2], - leaf->lower[0], leaf->lower[1], leaf->upper[2], - leaf->upper[0], leaf->lower[1], leaf->upper[2], - leaf->lower[0], leaf->upper[1], leaf->upper[2], - leaf->upper[0], leaf->upper[1], leaf->upper[2]); -} - -static void -write_cells - (const struct svx_voxel* leaf, - const size_t ileaf, - void* context) -{ - FILE* stream = context; - CHK(stream != NULL); - CHK(leaf != NULL); - fprintf(stream, "8 %lu %lu %lu %lu %lu %lu %lu %lu\n", - (unsigned long)(ileaf*8 + 0), - (unsigned long)(ileaf*8 + 1), - (unsigned long)(ileaf*8 + 2), - (unsigned long)(ileaf*8 + 3), - (unsigned long)(ileaf*8 + 4), - (unsigned long)(ileaf*8 + 5), - (unsigned long)(ileaf*8 + 6), - (unsigned long)(ileaf*8 + 7)); -} - -static void write_scalars (const struct svx_voxel* leaf, const size_t ileaf, @@ -198,38 +155,6 @@ write_scalars fprintf(stream, "%g\n", *(double*)leaf->data); } -static void -dump_data(FILE* stream, struct svx_octree* oct) -{ - struct svx_octree_desc desc; - size_t ileaf; - - CHK(stream != NULL); - CHK(oct != NULL); - - fprintf(stream, "# vtk DataFile Version 2.0\n"); - fprintf(stream, "Volume\n"); - fprintf(stream, "ASCII\n"); - fprintf(stream, "DATASET UNSTRUCTURED_GRID\n"); - - CHK(svx_octree_get_desc(oct, &desc) == RES_OK); - fprintf(stream, "POINTS %lu float\n", (unsigned long)(desc.nleaves* 8)); - CHK(svx_octree_for_each_leaf(oct, write_points, stream) == RES_OK); - - fprintf(stream, "CELLS %lu %lu\n", - (unsigned long)desc.nleaves, - (unsigned long)(desc.nleaves*(8/*#verts per leaf*/ + 1/*1st field of a cell*/))); - CHK(svx_octree_for_each_leaf(oct, write_cells, stream) == RES_OK); - - fprintf(stream, "CELL_TYPES %lu\n", (unsigned long)desc.nleaves); - FOR_EACH(ileaf, 0, desc.nleaves) fprintf(stream, "11\n"); - - fprintf(stream, "CELL_DATA %lu\n", (unsigned long)desc.nleaves); - fprintf(stream, "SCALARS K float 1\n"); - fprintf(stream, "LOOKUP_TABLE default\n"); - CHK(svx_octree_for_each_leaf(oct, write_scalars, stream) == RES_OK); -} - static int max_lod (const struct svx_voxel* vox, @@ -437,7 +362,7 @@ main(int argc, char** argv) CHK(octdesc.nvoxels == (octdesc.nleaves*8 - 1) / 7); CHK(octdesc.depth == (size_t)log2i((int)(nvxls[0]/2))+1); - dump_data(stdout, oct); + dump_data(stdout, oct, FLOAT, 1, write_scalars); #undef NEW_SCN diff --git a/src/test_svx_octree_trace_ray.c b/src/test_svx_octree_trace_ray.c @@ -81,7 +81,7 @@ voxels_merge(void* dst, const void* voxels[], const size_t nvoxels, void* ctx) CHK(dst && voxels && nvoxels && ctx); - for(ivoxel=0; tmp && ivoxel<nvoxels; ++ivoxel) { + for(ivoxel=0; !tmp && ivoxel<nvoxels; ++ivoxel) { const char* voxel_data = voxels[ivoxel]; tmp = *voxel_data; } @@ -106,6 +106,19 @@ voxels_challenge_merge(const void* voxels[], const size_t nvoxels, void* ctx) return merge; } +static void +write_scalars + (const struct svx_voxel* leaf, + const size_t ileaf, + void* context) +{ + FILE* stream = context; + (void)ileaf; + CHK(stream != NULL); + CHK(leaf != NULL); + fprintf(stream, "%d\n", *(char*)leaf->data); +} + int main(int argc, char** argv) { @@ -144,6 +157,8 @@ main(int argc, char** argv) CHK(svx_octree_create(dev, lower, upper, def, &voxel_desc, &oct) == RES_OK); + dump_data(stdout, oct, CHAR, 1, write_scalars); + CHK(svx_octree_ref_put(oct) == RES_OK); CHK(svx_device_ref_put(dev) == RES_OK); CHK(mem_allocated_size() == 0); diff --git a/src/test_svx_utils.h b/src/test_svx_utils.h @@ -19,6 +19,110 @@ #include <rsys/mem_allocator.h> #include <stdio.h> +enum data_type { + CHAR, + INT, + LONG, + FLOAT, + DOUBLE +}; + +static INLINE const char* +data_type_to_string(const enum data_type type) +{ + switch(type) { + case CHAR: return "char"; + case INT: return "int"; + case LONG: return "long"; + case FLOAT: return "float"; + case DOUBLE: return "double"; + default: FATAL("Unreachable code.\n"); + } +} + +static INLINE void +write_leaf_vertices + (const struct svx_voxel* leaf, + const size_t ileaf, + void* context) +{ + FILE* stream = context; + (void)ileaf; + CHK(stream != NULL); + CHK(leaf != NULL); + fprintf(stream, + "%g %g %g\n%g %g %g\n%g %g %g\n%g %g %g\n" + "%g %g %g\n%g %g %g\n%g %g %g\n%g %g %g\n", + leaf->lower[0], leaf->lower[1], leaf->lower[2], + leaf->upper[0], leaf->lower[1], leaf->lower[2], + leaf->lower[0], leaf->upper[1], leaf->lower[2], + leaf->upper[0], leaf->upper[1], leaf->lower[2], + leaf->lower[0], leaf->lower[1], leaf->upper[2], + leaf->upper[0], leaf->lower[1], leaf->upper[2], + leaf->lower[0], leaf->upper[1], leaf->upper[2], + leaf->upper[0], leaf->upper[1], leaf->upper[2]); +} + +static INLINE void +write_leaf_cells + (const struct svx_voxel* leaf, + const size_t ileaf, + void* context) +{ + FILE* stream = context; + CHK(stream != NULL); + CHK(leaf != NULL); + fprintf(stream, "8 %lu %lu %lu %lu %lu %lu %lu %lu\n", + (unsigned long)(ileaf*8 + 0), + (unsigned long)(ileaf*8 + 1), + (unsigned long)(ileaf*8 + 2), + (unsigned long)(ileaf*8 + 3), + (unsigned long)(ileaf*8 + 4), + (unsigned long)(ileaf*8 + 5), + (unsigned long)(ileaf*8 + 6), + (unsigned long)(ileaf*8 + 7)); +} + +static INLINE void +dump_data + (FILE* stream, + struct svx_octree* oct, + const enum data_type type, + const size_t numcomps, + void (*write_leaf_data) + (const struct svx_voxel* leaf, const size_t ileaf, void* ctx)) +{ + struct svx_octree_desc desc; + size_t ileaf; + + CHK(stream != NULL); + CHK(oct != NULL); + CHK(write_leaf_data); + + fprintf(stream, "# vtk DataFile Version 2.0\n"); + fprintf(stream, "Volume\n"); + fprintf(stream, "ASCII\n"); + fprintf(stream, "DATASET UNSTRUCTURED_GRID\n"); + + CHK(svx_octree_get_desc(oct, &desc) == RES_OK); + fprintf(stream, "POINTS %lu float\n", (unsigned long)(desc.nleaves* 8)); + CHK(svx_octree_for_each_leaf(oct, write_leaf_vertices, stream) == RES_OK); + + fprintf(stream, "CELLS %lu %lu\n", + (unsigned long)desc.nleaves, + (unsigned long)(desc.nleaves*(8/*#verts per leaf*/ + 1/*1st field of a cell*/))); + CHK(svx_octree_for_each_leaf(oct, write_leaf_cells, stream) == RES_OK); + + fprintf(stream, "CELL_TYPES %lu\n", (unsigned long)desc.nleaves); + FOR_EACH(ileaf, 0, desc.nleaves) fprintf(stream, "11\n"); + + fprintf(stream, "CELL_DATA %lu\n", (unsigned long)desc.nleaves); + fprintf(stream, "SCALARS K %s %lu\n", data_type_to_string(type), numcomps); + fprintf(stream, "LOOKUP_TABLE default\n"); + CHK(svx_octree_for_each_leaf(oct, write_leaf_data, stream) == RES_OK); +} + + static INLINE void check_memory_allocator(struct mem_allocator* allocator) {