star-2d

Contour structuring for efficient 2D geometric queries
git clone git://git.meso-star.fr/star-2d.git
Log | Files | Refs | README | LICENSE

commit 837098cb30ce4afcc2ad8eeb2d876001f485453f
parent 7901a05be4507db4a7260e45e5467720d39ceee0
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 22 Jun 2016 10:57:15 +0200

Begin the implementation of the s2d_scene API

Implement the backend geometry data structure. Implement and test the
s2d_scene_<create|ref_get|ref_put> functions.

Diffstat:
Mcmake/CMakeLists.txt | 5+++++
Asrc/s2d_geometry.c | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/s2d_geometry.h | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/s2d_scene.c | 188+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/s2d_scene_c.h | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test_s2d_scene.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 497 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -57,13 +57,17 @@ set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) set(S2D_FILES_SRC s2d_device.c + s2d_geometry.c s2d_line_segments.c + s2d_scene.c s2d_shape.c) set(S2D_FILES_INC_API s2d.h) set(S2D_FILES_INC s2d_buffer.h s2d_device_c.h + s2d_geometry.h s2d_line_segments.h + s2d_scene_c.h s2d_shape_c.h) set(S2D_FILES_DOC COPYING.fr COPYING.en README.md) @@ -122,6 +126,7 @@ if(NOT NO_TEST) endfunction() new_test(test_s2d_device) + new_test(test_s2d_scene) new_test(test_s2d_shape) endif(NOT NO_TEST) diff --git a/src/s2d_geometry.c b/src/s2d_geometry.c @@ -0,0 +1,100 @@ +/* Copyright (C) |Meso|Star> 2016 (contact@meso-star.com) + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. */ + +#include "s2d_device_c.h" +#include "s2d_geometry.h" +#include "s2d_line_segments.h" + +#include <rsys/mem_allocator.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +geometry_release(ref_T* ref) +{ + struct geometry* geom; + struct s2d_device* dev; + + geom = CONTAINER_OF(ref, struct geometry, ref); + dev = geom->dev; + if(geom->lines) line_segments_ref_put(geom->lines); + MEM_RM(dev->allocator, geom); + S2D(device_ref_put(dev)); +} + +/******************************************************************************* + * Non exported functions + ******************************************************************************/ +res_T +geometry_create(struct s2d_device* dev, struct geometry** out_geom) +{ + struct geometry* geom = NULL; + res_T res = RES_OK; + ASSERT(dev && out_geom); + + geom = (struct geometry*)MEM_CALLOC + (dev->allocator, 1, sizeof(struct geometry)); + if(!geom) { + res = RES_MEM_ERR; + goto error; + } + ref_init(&geom->ref); + S2D(device_ref_get(dev)); + geom->dev = dev; + geom->name = S2D_INVALID_ID; + geom->irtc = RTC_INVALID_GEOMETRY_ID; + geom->flip_contour = 0; + geom->is_enabled = 1; + geom->lines = NULL; + +exit: + *out_geom = geom; + return res; +error: + if(geom) { + geometry_ref_put(geom); + geom = NULL; + } + goto exit; +} + +void +geometry_ref_get(struct geometry* geom) +{ + ASSERT(geom); + ref_get(&geom->ref); +} + +void +geometry_ref_put(struct geometry* geom) +{ + ASSERT(geom); + ref_put(&geom->ref, geometry_release); +} + diff --git a/src/s2d_geometry.h b/src/s2d_geometry.h @@ -0,0 +1,64 @@ +/* Copyright (C) |Meso|Star> 2016 (contact@meso-star.com) + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. */ + +#ifndef S2D_GEOMETRY_H +#define S2D_GEOMETRY_H + +#include "s2d.h" +#include "s2d_backend.h" +#include <rsys/ref_count.h> + +/* Backend geometry */ +struct geometry { + unsigned name; /* Client side identifier */ + unsigned irtc; /* Backend identifier */ + unsigned scene_prim_id_offset; /* Offset from local to scene prim_id */ + char flip_contour; /* Is the geometry contour flipped? */ + char is_enabled; /* Is the geometry enabled? */ + + struct line_segments* lines; /* Reference toward the client side data */ + + struct s2d_device* dev; + ref_T ref; +}; + +extern LOCAL_SYM res_T +geometry_create + (struct s2d_device* dev, + struct geometry** geom); + +extern LOCAL_SYM void +geometry_ref_get + (struct geometry* geometry); + +extern LOCAL_SYM void +geometry_ref_put + (struct geometry* geometry); + +#endif /* S2D_GEOMETRY_H */ + diff --git a/src/s2d_scene.c b/src/s2d_scene.c @@ -0,0 +1,188 @@ +/* Copyright (C) |Meso|Star> 2016 (contact@meso-star.com) + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. */ + +#include "s2d.h" +#include "s2d_device_c.h" +#include "s2d_geometry.h" +#include "s2d_scene_c.h" +#include "s2d_shape_c.h" + +#include <rsys/logger.h> +#include <rsys/float2.h> +#include <rsys/mem_allocator.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +scene_session_clear(struct s2d_scene* scn) +{ + struct geometry** pgeoms; + size_t ngeoms, i; + ASSERT(scn); + + ngeoms = darray_geom_size_get(&scn->embree2geoms); + pgeoms = darray_geom_data_get(&scn->embree2geoms); + FOR_EACH(i, 0, ngeoms) { + if(!pgeoms[i]) continue; + geometry_ref_put(pgeoms[i]); + } + darray_geom_clear(&scn->embree2geoms); + scn->session_mask = 0; +} + +static void +scene_detach_shape(struct s2d_scene* scn, struct s2d_shape* shape) +{ + struct geometry** pgeom; + ASSERT(scn && shape && !is_list_empty(&shape->scene_attachment)); + ASSERT(scn->session_mask == 0); + + pgeom = htable_geom_find(&scn->cached_geoms, &shape); + if(pgeom) { /* Remove the cached geometry */ + struct geometry* geom = *pgeom; + if(geom->irtc != RTC_INVALID_GEOMETRY_ID) { + rtcDeleteGeometry(scn->rtc_scn, geom->irtc); + geom->irtc = RTC_INVALID_GEOMETRY_ID; + scn->is_rtc_scn_outdated = 1; + } + geometry_ref_put(geom); + htable_geom_erase(&scn->cached_geoms, &shape); + } + list_del(&shape->scene_attachment); + + S2D(shape_ref_put(shape)); +} + +static void +scene_release(ref_T* ref) +{ + struct s2d_scene* scn; + struct s2d_device* dev; + ASSERT(ref); + scn = CONTAINER_OF(ref, struct s2d_scene, ref); + S2D(scene_clear(scn)); + dev = scn->dev; + scene_session_clear(scn); + if(scn->rtc_scn) rtcDeleteScene(scn->rtc_scn); + htable_geom_release(&scn->cached_geoms); + darray_geom_release(&scn->embree2geoms); + MEM_RM(dev->allocator, scn); + S2D(device_ref_put(dev)); +} + +/******************************************************************************* + * Exported s2d_scene functions + ******************************************************************************/ +res_T +s2d_scene_create(struct s2d_device* dev, struct s2d_scene** out_scn) +{ + struct s2d_scene* scn = NULL; + const RTCSceneFlags rtc_scene_mask = + RTC_SCENE_DYNAMIC + | RTC_SCENE_INCOHERENT + | RTC_SCENE_ROBUST; + const RTCAlgorithmFlags rtc_intersect_mask = + RTC_INTERSECT1 + | RTC_INTERSECT4; + res_T res = RES_OK; + + if(!dev || !out_scn) { + res = RES_BAD_ARG; + goto error; + } + + scn = (struct s2d_scene*)MEM_CALLOC + (dev->allocator, 1, sizeof(struct s2d_scene)); + if(!scn) { + res = RES_MEM_ERR; + goto error; + } + list_init(&scn->shapes); + htable_geom_init(dev->allocator, &scn->cached_geoms); + darray_geom_init(dev->allocator, &scn->embree2geoms); + ref_init(&scn->ref); + S2D(device_ref_get(dev)); + scn->dev = dev; + scn->session_mask = 0; + scn->rtc_scn = rtcDeviceNewScene(dev->rtc, rtc_scene_mask, rtc_intersect_mask); + if(!scn->rtc_scn) { + res = RES_MEM_ERR; + goto error; + } + f2_splat(scn->lower, FLT_MAX); + f2_splat(scn->upper,-FLT_MAX); + +exit: + if(out_scn) *out_scn = scn; + return res; +error: + if(scn) { + S2D(scene_ref_put(scn)); + scn = NULL; + } + goto exit; +} + +res_T +s2d_scene_ref_get(struct s2d_scene* scn) +{ + if(!scn) return RES_BAD_ARG; + ref_get(&scn->ref); + return RES_OK; +} + +res_T +s2d_scene_ref_put(struct s2d_scene* scn) +{ + if(!scn) return RES_BAD_ARG; + ref_put(&scn->ref, scene_release); + return RES_OK; +} + +res_T +s2d_scene_clear(struct s2d_scene* scn) +{ + struct list_node* node, *tmp; + if(!scn) return RES_BAD_ARG; + if(scn->session_mask != 0) { + if(scn->dev->verbose) { + logger_print(scn->dev->logger, LOG_ERROR, + "%s: Invalid operation. A session is active onto the scene.\n", + __FUNCTION__); + } + return RES_BAD_OP; + } + LIST_FOR_EACH_SAFE(node, tmp, &scn->shapes) { + struct s2d_shape* shape = CONTAINER_OF + (node, struct s2d_shape, scene_attachment); + scene_detach_shape(scn, shape); + } + return RES_OK; +} + diff --git a/src/s2d_scene_c.h b/src/s2d_scene_c.h @@ -0,0 +1,79 @@ +/* Copyright (C) |Meso|Star> 2016 (contact@meso-star.com) + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. */ + +#ifndef S2D_SCENE_C_H +#define S2D_SCENE_C_H + +#include "s2d_backend.h" + +#include <rsys/dynamic_array.h> +#include <rsys/hash_table.h> +#include <rsys/list.h> +#include <rsys/ref_count.h> + +/* + * The geometry pointer must be initialized to NULL in order to define which + * pointers are valid or not. + */ +static FINLINE void +geom_ptr_init__(struct mem_allocator* alloc, struct geometry** geom) +{ + (void)alloc; + *geom = NULL; +} + +/* Generate the darray_geom data type */ +#define DARRAY_NAME geom +#define DARRAY_DATA struct geometry* +#define DARRAY_FUNCTOR_INIT geom_ptr_init__ +#include <rsys/dynamic_array.h> + +/* Generate the htable_geom hash table */ +#define HTABLE_NAME geom +#define HTABLE_DATA struct geometry* +#define HTABLE_KEY struct s2d_shape* +#include <rsys/hash_table.h> + +struct s2d_scene { + struct list_node shapes; /* List of attached shapes */ + struct htable_geom cached_geoms; /* Cached shape geometries */ + struct darray_geom embree2geoms; /* Shape geometries indexed by embree id */ + + float lower[2], upper[2]; /* AABB of the scene */ + + RTCScene rtc_scn; /* Embree scene */ + char is_rtc_scn_outdated; /* Must the embree scene rebuild */ + + int session_mask; /* Combination of enum s2d_session_flag */ + struct s2d_device* dev; + + ref_T ref; +}; + +#endif /* S2D_SCENE_C_H */ + diff --git a/src/test_s2d_scene.c b/src/test_s2d_scene.c @@ -0,0 +1,61 @@ +/* Copyright (C) |Meso|Star> 2016 (contact@meso-star.com) + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. */ + +#include "s2d.h" +#include "test_s2d_utils.h" + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct s2d_device* dev; + struct s2d_scene* scn; + (void)argc, (void)argv; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + + CHECK(s2d_device_create(NULL, &allocator, 1, &dev), RES_OK); + + CHECK(s2d_scene_create(NULL, NULL), RES_BAD_ARG); + CHECK(s2d_scene_create(dev, NULL), RES_BAD_ARG); + CHECK(s2d_scene_create(NULL, &scn), RES_BAD_ARG); + CHECK(s2d_scene_create(dev, &scn), RES_OK); + + CHECK(s2d_scene_ref_get(NULL), RES_BAD_ARG); + CHECK(s2d_scene_ref_get(scn), RES_OK); + CHECK(s2d_scene_ref_put(NULL), RES_BAD_ARG); + CHECK(s2d_scene_ref_put(scn), RES_OK); + CHECK(s2d_scene_ref_put(scn), RES_OK); + + CHECK(s2d_device_ref_put(dev), RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHECK(mem_allocated_size(), 0); + return 0; +}