From 79552f87f28de9b9c5947b3a190f6f90fb654aea Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 6 Dec 2023 09:12:02 -0500 Subject: [PATCH] Fix: Protect against access of empty geometry bounds optional --- .../blenkernel/intern/gpencil_geom_legacy.cc | 8 ++++---- source/blender/blenkernel/intern/mesh.cc | 2 +- source/blender/draw/engines/eevee/eevee_shadows.cc | 3 ++- .../draw/engines/eevee_next/eevee_pipeline.cc | 2 +- .../draw/engines/gpencil/gpencil_cache_utils.cc | 13 +++++++------ .../blender/draw/engines/overlay/overlay_extra.cc | 2 +- .../draw/engines/overlay/overlay_outline.cc | 14 ++++++++------ 7 files changed, 24 insertions(+), 20 deletions(-) diff --git a/source/blender/blenkernel/intern/gpencil_geom_legacy.cc b/source/blender/blenkernel/intern/gpencil_geom_legacy.cc index 502ddf58e62..4a46e4b433d 100644 --- a/source/blender/blenkernel/intern/gpencil_geom_legacy.cc +++ b/source/blender/blenkernel/intern/gpencil_geom_legacy.cc @@ -25,6 +25,7 @@ #include "BLI_math_matrix.h" #include "BLI_math_rotation.h" #include "BLI_math_vector.h" +#include "BLI_math_vector.hh" #include "BLI_math_vector_types.hh" #include "BLI_polyfill_2d.h" #include "BLI_span.hh" @@ -115,10 +116,9 @@ std::optional> BKE_gpencil_data_minmax(const bG void BKE_gpencil_centroid_3d(bGPdata *gpd, float r_centroid[3]) { - const blender::Bounds bounds = *BKE_gpencil_data_minmax(gpd); - - const float3 tot = bounds.min + bounds.max; - mul_v3_v3fl(r_centroid, tot, 0.5f); + using namespace blender; + const Bounds bounds = BKE_gpencil_data_minmax(gpd).value_or(Bounds(float3(0))); + copy_v3_v3(r_centroid, math::midpoint(bounds.min, bounds.max)); } void BKE_gpencil_stroke_boundingbox_calc(bGPDstroke *gps) diff --git a/source/blender/blenkernel/intern/mesh.cc b/source/blender/blenkernel/intern/mesh.cc index 06dd1038cdb..a3fe2f6fe62 100644 --- a/source/blender/blenkernel/intern/mesh.cc +++ b/source/blender/blenkernel/intern/mesh.cc @@ -790,7 +790,7 @@ void BKE_mesh_texspace_calc(Mesh *me) using namespace blender; if (me->texspace_flag & ME_TEXSPACE_FLAG_AUTO) { const Bounds bounds = me->bounds_min_max().value_or( - Bounds{float3(-1.0f), float3(1.0f)}); + Bounds(float3(-1.0f), float3(1.0f))); float texspace_location[3], texspace_size[3]; mid_v3_v3v3(texspace_location, bounds.min, bounds.max); diff --git a/source/blender/draw/engines/eevee/eevee_shadows.cc b/source/blender/draw/engines/eevee/eevee_shadows.cc index 7e49df04363..d86c6f04865 100644 --- a/source/blender/draw/engines/eevee/eevee_shadows.cc +++ b/source/blender/draw/engines/eevee/eevee_shadows.cc @@ -114,6 +114,7 @@ void EEVEE_shadows_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata) void EEVEE_shadows_caster_register(EEVEE_ViewLayerData *sldata, Object *ob) { + using namespace blender; EEVEE_LightsInfo *linfo = sldata->lights; EEVEE_ShadowCasterBuffer *backbuffer = linfo->shcaster_backbuffer; EEVEE_ShadowCasterBuffer *frontbuffer = linfo->shcaster_frontbuffer; @@ -155,7 +156,7 @@ void EEVEE_shadows_caster_register(EEVEE_ViewLayerData *sldata, Object *ob) } /* Update World AABB in frontbuffer. */ - const blender::Bounds bounds = *BKE_object_boundbox_get(ob); + const Bounds bounds = BKE_object_boundbox_get(ob).value_or(Bounds(float3(0))); BoundBox bb; BKE_boundbox_init_from_minmax(&bb, bounds.min, bounds.max); float min[3], max[3]; diff --git a/source/blender/draw/engines/eevee_next/eevee_pipeline.cc b/source/blender/draw/engines/eevee_next/eevee_pipeline.cc index b33adb6acde..8976c65018d 100644 --- a/source/blender/draw/engines/eevee_next/eevee_pipeline.cc +++ b/source/blender/draw/engines/eevee_next/eevee_pipeline.cc @@ -949,7 +949,7 @@ GridAABB VolumePipeline::grid_aabb_from_object(Object *ob) return int3(grid_coords * float3(data.tex_size) + 0.5); }; - const Bounds bounds = *BKE_object_boundbox_get(ob); + const Bounds bounds = BKE_object_boundbox_get(ob).value_or(Bounds(float3(0))); int3 min = int3(INT32_MAX); int3 max = int3(INT32_MIN); diff --git a/source/blender/draw/engines/gpencil/gpencil_cache_utils.cc b/source/blender/draw/engines/gpencil/gpencil_cache_utils.cc index c432afb42e2..019c0f1fb8e 100644 --- a/source/blender/draw/engines/gpencil/gpencil_cache_utils.cc +++ b/source/blender/draw/engines/gpencil/gpencil_cache_utils.cc @@ -23,6 +23,7 @@ #include "BLI_hash.h" #include "BLI_link_utils.h" #include "BLI_math_color.h" +#include "BLI_math_vector.hh" #include "BLI_memblock.h" #include "gpencil_engine.h" @@ -37,6 +38,7 @@ GPENCIL_tObject *gpencil_object_cache_add(GPENCIL_PrivateData *pd, Object *ob) { + using namespace blender; bGPdata *gpd = (bGPdata *)ob->data; GPENCIL_tObject *tgp_ob = static_cast(BLI_memblock_alloc(pd->gp_object_pool)); @@ -64,13 +66,12 @@ GPENCIL_tObject *gpencil_object_cache_add(GPENCIL_PrivateData *pd, Object *ob) * strokes not aligned with the object axes. Maybe we could try to * compute the minimum axis of all strokes. But this would be more * computationally heavy and should go into the GPData evaluation. */ - const blender::Bounds bounds = *BKE_gpencil_data_minmax(gpd); - BoundBox bb; - BKE_boundbox_init_from_minmax(&bb, bounds.min, bounds.max); + const std::optional> bounds = BKE_gpencil_data_minmax(gpd).value_or( + Bounds(float3(0))); + float3 size = bounds->max - bounds->min; + float3 center = math::midpoint(bounds->min, bounds->max); /* Convert bbox to matrix */ - float mat[4][4], size[3], center[3]; - BKE_boundbox_calc_size_aabb(&bb, size); - BKE_boundbox_calc_center_aabb(&bb, center); + float mat[4][4]; unit_m4(mat); copy_v3_v3(mat[3], center); /* Avoid division by 0.0 later. */ diff --git a/source/blender/draw/engines/overlay/overlay_extra.cc b/source/blender/draw/engines/overlay/overlay_extra.cc index 66adb9391f0..d067d71a708 100644 --- a/source/blender/draw/engines/overlay/overlay_extra.cc +++ b/source/blender/draw/engines/overlay/overlay_extra.cc @@ -364,7 +364,7 @@ static void OVERLAY_bounds(OVERLAY_ExtraCallBuffers *cb, } const Bounds bounds = BKE_object_boundbox_get(ob).value_or( - Bounds{float3(-1.0f), float3(1.0f)}); + Bounds(float3(-1.0f), float3(1.0f))); float3 size = bounds.max - bounds.min; const float3 center = around_origin ? float3(0) : math::midpoint(bounds.min, bounds.max); diff --git a/source/blender/draw/engines/overlay/overlay_outline.cc b/source/blender/draw/engines/overlay/overlay_outline.cc index 8ac8f92fdea..92b2588e017 100644 --- a/source/blender/draw/engines/overlay/overlay_outline.cc +++ b/source/blender/draw/engines/overlay/overlay_outline.cc @@ -6,6 +6,8 @@ * \ingroup draw_engine */ +#include "BLI_math_vector.hh" + #include "DRW_render.h" #include "BKE_global.h" @@ -22,6 +24,7 @@ /* Returns the normal plane in NDC space. */ static void gpencil_depth_plane(Object *ob, float r_plane[4]) { + using namespace blender; /* TODO: put that into private data. */ float viewinv[4][4]; DRW_view_viewmat_get(nullptr, viewinv, true); @@ -33,13 +36,12 @@ static void gpencil_depth_plane(Object *ob, float r_plane[4]) * strokes not aligned with the object axes. Maybe we could try to * compute the minimum axis of all strokes. But this would be more * computationally heavy and should go into the GPData evaluation. */ - const blender::Bounds bounds = *BKE_object_boundbox_get(ob); - BoundBox bb; - BKE_boundbox_init_from_minmax(&bb, bounds.min, bounds.max); + const std::optional> bounds = BKE_object_boundbox_get(ob).value_or( + Bounds(float3(0))); + float3 size = bounds->max - bounds->min; + float3 center = math::midpoint(bounds->min, bounds->max); /* Convert bbox to matrix */ - float mat[4][4], size[3], center[3]; - BKE_boundbox_calc_size_aabb(&bb, size); - BKE_boundbox_calc_center_aabb(&bb, center); + float mat[4][4]; unit_m4(mat); copy_v3_v3(mat[3], center); /* Avoid division by 0.0 later. */