solstice-solver

Solver library of the solstice app
git clone git://git.meso-star.com/solstice-solver.git
Log | Files | Refs | README | LICENSE

commit 968f6f8b78c22e07e5cecf08fedb43ba8a9f0600
parent 0b379ec5b9d30d20aaf65e941cab2a93f468b9ac
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Wed, 29 Jun 2016 16:59:22 +0200

First try to object instances.

Diffstat:
Mcmake/CMakeLists.txt | 3+++
Asrc/ssol_object_instance.c | 157+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/ssol_object_instance_c.h | 33+++++++++++++++++++++++++++++++++
Asrc/test_ssol_object_instance.c | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 292 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -51,6 +51,7 @@ set(SSOL_FILES_SRC ssol_image.c ssol_material.c ssol_object.c + ssol_object_instance.c ssol_shape.c) set(SSOL_FILES_INC_API @@ -61,6 +62,7 @@ set(SSOL_FILES_INC ssol_image_c.h ssol_material_c.h ssol_object_c.h + ssol_object_instance_c.h ssol_shape_c.h) set(SSOL_FILES_DOC COPYING README.md) @@ -114,6 +116,7 @@ if(NOT NO_TEST) new_test(test_ssol_image) new_test(test_ssol_material) new_test(test_ssol_object) + new_test(test_ssol_object_instance) new_test(test_ssol_shape) endif(NOT NO_TEST) diff --git a/src/ssol_object_instance.c b/src/ssol_object_instance.c @@ -0,0 +1,156 @@ +/* Copyright (C) CNRS 2016 +* +* 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 "ssol.h" +#include "ssol_object_c.h" +#include "ssol_shape_c.h" +#include "ssol_object_instance_c.h" +#include "ssol_device_c.h" + +#include <rsys\rsys.h> +#include <rsys\mem_allocator.h> +#include <rsys\ref_count.h> + +/******************************************************************************* +* Helper functions +******************************************************************************/ + +static void +object_instance_release(ref_T* ref) +{ + struct ssol_object_instance* instance; + ASSERT(ref); + instance = CONTAINER_OF(ref, struct ssol_object_instance, ref); + + ASSERT(instance->dev && instance->dev->allocator); + + if (instance->object) + SSOL(object_ref_put(instance->object)); + if (instance->image) + SSOL(image_ref_put(instance->image)); + SSOL(device_ref_put(instance->dev)); + MEM_RM(instance->dev->allocator, instance); +} + +/******************************************************************************* +* Local functions +******************************************************************************/ + +/******************************************************************************* +* Exported ssol_object_instance functions +******************************************************************************/ + +res_T +ssol_object_instantiate + (struct ssol_object* object, + struct ssol_object_instance** out_instance) +{ + struct ssol_object_instance* instance = NULL; + struct ssol_device* dev; + res_T res = RES_OK; + if (!object || !object->dev || !out_instance) { + return RES_BAD_ARG; + } + + dev = object->dev; + ASSERT(dev && dev->allocator); + instance = (struct ssol_object_instance*)MEM_CALLOC + (dev->allocator, 1, sizeof(struct ssol_object_instance)); + if (!instance) { + res = RES_MEM_ERR; + goto error; + } + + list_init(&instance->scene_attachment); + instance->dev = dev; + SSOL(device_ref_get(dev)); + ref_init(&instance->ref); + +exit: + if (out_instance) *out_instance = instance; + return res; +error: + if (instance) { + SSOL(object_instance_ref_put(instance)); + instance = NULL; + } + goto exit; +} + +res_T +ssol_object_instance_ref_get + (struct ssol_object_instance* instance) +{ + if (!instance) + return RES_BAD_ARG; + ref_get(&instance->ref); + return RES_OK; +} + +res_T +ssol_object_instance_ref_put + (struct ssol_object_instance* instance) +{ + if (!instance) + return RES_BAD_ARG; + ref_put(&instance->ref, object_instance_release); + return RES_OK; +} + +res_T +ssol_object_instance_set_transform + (struct ssol_object_instance* instance, + const double transform []) +{ + if (!instance || !transform) + return RES_BAD_ARG; + + /* keep transform for later use */ + memcpy(instance->transform, transform, sizeof(instance->transform)); + + return RES_OK; +} + +res_T +ssol_object_instance_set_receiver_image + (struct ssol_object_instance* instance, + struct ssol_image* image, + const enum ssol_parametrization_type type) +{ + if (!instance + || !image + || (type != SSOL_PARAMETRIZATION_TEXCOORD + && type != SSOL_PARAMETRIZATION_PRIMITIVE_ID)) + return RES_BAD_ARG; + + if (instance->image) SSOL(image_ref_put(instance->image)); + SSOL(image_ref_get(image)); + instance->image = image; + instance->param_type = type; + + return RES_OK; +} + +res_T +ssol_object_instance_is_attached + (struct ssol_object_instance* instance, + char* is_attached) +{ + if (!instance || !is_attached) + return RES_BAD_ARG; + *is_attached = !is_list_empty(&instance->scene_attachment); + + return RES_OK; +} +\ No newline at end of file diff --git a/src/ssol_object_instance_c.h b/src/ssol_object_instance_c.h @@ -0,0 +1,33 @@ +/* Copyright (C) CNRS 2016 +* +* 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/>. */ + +#ifndef SSOL_OBJECT_INSTANCE_C_H +#define SSOL_OBJECT_INSTANCE_C_H + +#include <rsys/ref_count.h> +#include <rsys/list.h> + +struct ssol_object_instance { + struct ssol_object* object; + double transform[12]; + struct ssol_image* image; + enum ssol_parametrization_type param_type; + struct list_node scene_attachment; + + struct ssol_device* dev; + ref_T ref; +}; + +#endif /* SSOL_OBJECT_INSTANCE_C_H */ diff --git a/src/test_ssol_object_instance.c b/src/test_ssol_object_instance.c @@ -0,0 +1,98 @@ +/* Copyright (C) CNRS 2016 +* +* 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 "ssol.h" +#include "test_ssol_utils.h" + +#include <rsys/logger.h> + +/******************************************************************************* +* test main program +******************************************************************************/ +int +main(int argc, char** argv) +{ + struct logger logger; + struct mem_allocator allocator; + struct ssol_device* dev; + struct ssol_carving carving; + struct ssol_quadric quadric; + struct ssol_shape* shape; + struct ssol_punched_surface punched_surface; + struct ssol_material* material; + struct ssol_object* object; + struct ssol_image* image; + struct ssol_object_instance* instance; + double transform[12]; + (void) argc, (void) argv; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + + CHECK(logger_init(&allocator, &logger), RES_OK); + logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL); + logger_set_stream(&logger, LOG_ERROR, log_stream, NULL); + logger_set_stream(&logger, LOG_WARNING, log_stream, NULL); + + CHECK(ssol_device_create(&logger, &allocator, SSOL_NTHREADS_DEFAULT, 0, &dev), RES_OK); + + CHECK(ssol_material_create_virtual(dev, &material), RES_OK); + + carving.type = SSOL_CARVING_CIRCLE; + carving.internal = 0; + carving.data.circle.center[0] = 0; + carving.data.circle.center[1] = 0; + carving.data.circle.radius = 1; + quadric.type = SSOL_QUADRIC_PLANE; + punched_surface.nb_carvings = 1; + punched_surface.quadric = &quadric; + punched_surface.carvings = &carving; + CHECK(ssol_shape_create_punched_surface(dev, &shape), RES_OK); + CHECK(ssol_object_create(dev, shape, material, &object), RES_OK); + + CHECK(ssol_object_instantiate(object, &instance), RES_OK); + + CHECK(ssol_object_instance_ref_get(NULL), RES_BAD_ARG); + CHECK(ssol_object_instance_ref_get(instance), RES_OK); + + CHECK(ssol_object_instance_ref_put(NULL), RES_BAD_ARG); + CHECK(ssol_object_instance_ref_put(instance), RES_OK); + + CHECK(ssol_object_instance_set_transform(NULL, transform), RES_BAD_ARG); + CHECK(ssol_object_instance_set_transform(instance, NULL), RES_BAD_ARG); + CHECK(ssol_object_instance_set_transform(instance, transform), RES_OK); + + CHECK(ssol_image_create(dev, &image), RES_OK); + CHECK(ssol_object_instance_set_receiver_image(NULL, image, SSOL_PARAMETRIZATION_PRIMITIVE_ID), RES_BAD_ARG); + CHECK(ssol_object_instance_set_receiver_image(instance, NULL, SSOL_PARAMETRIZATION_PRIMITIVE_ID), RES_BAD_ARG); + CHECK(ssol_object_instance_set_receiver_image(instance, image, (enum ssol_parametrization_type)999), RES_BAD_ARG); + CHECK(ssol_object_instance_set_receiver_image(instance, image, SSOL_PARAMETRIZATION_PRIMITIVE_ID), RES_OK); + + CHECK(ssol_object_instance_ref_put(instance), RES_OK); + + CHECK(ssol_image_ref_put(image), RES_OK); + CHECK(ssol_object_ref_put(object), RES_OK); + CHECK(ssol_shape_ref_put(shape), RES_OK); + CHECK(ssol_material_ref_put(material), RES_OK); + + CHECK(ssol_device_ref_put(dev), RES_OK); + + logger_release(&logger); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHECK(mem_allocated_size(), 0); + + return 0; +} +\ No newline at end of file