s2d_geometry.c (2384B)
1 /* Copyright (C) 2016-2021, 2023 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "s2d_device_c.h" 17 #include "s2d_geometry.h" 18 #include "s2d_line_segments.h" 19 20 #include <rsys/mem_allocator.h> 21 22 /******************************************************************************* 23 * Helper functions 24 ******************************************************************************/ 25 static void 26 geometry_release(ref_T* ref) 27 { 28 struct geometry* geom; 29 struct s2d_device* dev; 30 31 geom = CONTAINER_OF(ref, struct geometry, ref); 32 dev = geom->dev; 33 if(geom->lines) line_segments_ref_put(geom->lines); 34 MEM_RM(dev->allocator, geom); 35 S2D(device_ref_put(dev)); 36 } 37 38 /******************************************************************************* 39 * Non exported functions 40 ******************************************************************************/ 41 res_T 42 geometry_create(struct s2d_device* dev, struct geometry** out_geom) 43 { 44 struct geometry* geom = NULL; 45 res_T res = RES_OK; 46 ASSERT(dev && out_geom); 47 48 geom = (struct geometry*)MEM_CALLOC 49 (dev->allocator, 1, sizeof(struct geometry)); 50 if(!geom) { 51 res = RES_MEM_ERR; 52 goto error; 53 } 54 ref_init(&geom->ref); 55 S2D(device_ref_get(dev)); 56 geom->dev = dev; 57 geom->name = S2D_INVALID_ID; 58 geom->rtc = NULL; 59 geom->rtc_id = RTC_INVALID_GEOMETRY_ID; 60 geom->embree_outdated_mask = 0; 61 geom->flip_contour = 0; 62 geom->is_enabled = 1; 63 geom->lines = NULL; 64 65 exit: 66 *out_geom = geom; 67 return res; 68 error: 69 if(geom) { 70 geometry_ref_put(geom); 71 geom = NULL; 72 } 73 goto exit; 74 } 75 76 void 77 geometry_ref_get(struct geometry* geom) 78 { 79 ASSERT(geom); 80 ref_get(&geom->ref); 81 } 82 83 void 84 geometry_ref_put(struct geometry* geom) 85 { 86 ASSERT(geom); 87 ref_put(&geom->ref, geometry_release); 88 } 89