Cleanup: Sculpt return sampled area normal by value
Use C++ types and std::optional to clarify that the functions can fail.
This commit is contained in:
@@ -286,12 +286,9 @@ Vector<PBVHNode *> pbvh_gather_generic(Object *ob, VPaint *wp, Brush *brush)
|
||||
return node_in_sphere(node, ss->cache->location, ss->cache->radius_squared, true);
|
||||
});
|
||||
|
||||
if (use_normal) {
|
||||
SCULPT_pbvh_calc_area_normal(brush, ob, nodes, ss->cache->sculpt_normal_symm);
|
||||
}
|
||||
else {
|
||||
zero_v3(ss->cache->sculpt_normal_symm);
|
||||
}
|
||||
ss->cache->sculpt_normal_symm =
|
||||
use_normal ? SCULPT_pbvh_calc_area_normal(brush, ob, nodes).value_or(float3(0)) :
|
||||
float3(0);
|
||||
}
|
||||
else {
|
||||
const DistRayAABB_Precalc ray_dist_precalc = dist_squared_ray_to_aabb_v3_precalc(
|
||||
@@ -300,12 +297,7 @@ Vector<PBVHNode *> pbvh_gather_generic(Object *ob, VPaint *wp, Brush *brush)
|
||||
return node_in_cylinder(ray_dist_precalc, node, ss->cache->radius_squared, true);
|
||||
});
|
||||
|
||||
if (use_normal) {
|
||||
copy_v3_v3(ss->cache->sculpt_normal_symm, ss->cache->view_normal);
|
||||
}
|
||||
else {
|
||||
zero_v3(ss->cache->sculpt_normal_symm);
|
||||
}
|
||||
ss->cache->sculpt_normal_symm = use_normal ? ss->cache->view_normal : float3(0);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
@@ -2032,16 +2032,15 @@ void SCULPT_calc_area_center(Sculpt *sd, Object *ob, Span<PBVHNode *> nodes, flo
|
||||
}
|
||||
}
|
||||
|
||||
void SCULPT_calc_area_normal(Sculpt *sd, Object *ob, Span<PBVHNode *> nodes, float r_area_no[3])
|
||||
std::optional<float3> SCULPT_calc_area_normal(Sculpt *sd, Object *ob, Span<PBVHNode *> nodes)
|
||||
{
|
||||
const Brush *brush = BKE_paint_brush(&sd->paint);
|
||||
SCULPT_pbvh_calc_area_normal(brush, ob, nodes, r_area_no);
|
||||
return SCULPT_pbvh_calc_area_normal(brush, ob, nodes);
|
||||
}
|
||||
|
||||
bool SCULPT_pbvh_calc_area_normal(const Brush *brush,
|
||||
Object *ob,
|
||||
Span<PBVHNode *> nodes,
|
||||
float r_area_no[3])
|
||||
std::optional<float3> SCULPT_pbvh_calc_area_normal(const Brush *brush,
|
||||
Object *ob,
|
||||
Span<PBVHNode *> nodes)
|
||||
{
|
||||
using namespace blender;
|
||||
using namespace blender::ed::sculpt_paint;
|
||||
@@ -2063,14 +2062,18 @@ bool SCULPT_pbvh_calc_area_normal(const Brush *brush,
|
||||
},
|
||||
calc_area_normal_and_center_reduce);
|
||||
|
||||
/* For area normal. */
|
||||
for (int i = 0; i < ARRAY_SIZE(anctd.area_nos); i++) {
|
||||
if (normalize_v3_v3(r_area_no, anctd.area_nos[i]) != 0.0f) {
|
||||
break;
|
||||
}
|
||||
if (!any_vertex_sampled) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return any_vertex_sampled;
|
||||
/* For area normal. */
|
||||
float3 result;
|
||||
for (int i = 0; i < ARRAY_SIZE(anctd.area_nos); i++) {
|
||||
if (normalize_v3_v3(result, anctd.area_nos[i]) != 0.0f) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void SCULPT_calc_area_normal_and_center(
|
||||
@@ -2621,35 +2624,24 @@ static Vector<PBVHNode *> sculpt_pbvh_gather_texpaint(Object *ob,
|
||||
}
|
||||
|
||||
/* Calculate primary direction of movement for many brushes. */
|
||||
static void calc_sculpt_normal(Sculpt *sd, Object *ob, Span<PBVHNode *> nodes, float r_area_no[3])
|
||||
static float3 calc_sculpt_normal(Sculpt *sd, Object *ob, Span<PBVHNode *> nodes)
|
||||
{
|
||||
const Brush *brush = BKE_paint_brush(&sd->paint);
|
||||
const SculptSession *ss = ob->sculpt;
|
||||
|
||||
switch (brush->sculpt_plane) {
|
||||
case SCULPT_DISP_DIR_VIEW:
|
||||
copy_v3_v3(r_area_no, ss->cache->true_view_normal);
|
||||
break;
|
||||
|
||||
case SCULPT_DISP_DIR_X:
|
||||
ARRAY_SET_ITEMS(r_area_no, 1.0f, 0.0f, 0.0f);
|
||||
break;
|
||||
|
||||
case SCULPT_DISP_DIR_Y:
|
||||
ARRAY_SET_ITEMS(r_area_no, 0.0f, 1.0f, 0.0f);
|
||||
break;
|
||||
|
||||
case SCULPT_DISP_DIR_Z:
|
||||
ARRAY_SET_ITEMS(r_area_no, 0.0f, 0.0f, 1.0f);
|
||||
break;
|
||||
|
||||
case SCULPT_DISP_DIR_AREA:
|
||||
SCULPT_calc_area_normal(sd, ob, nodes, r_area_no);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
return SCULPT_calc_area_normal(sd, ob, nodes).value_or(float3(0));
|
||||
case SCULPT_DISP_DIR_VIEW:
|
||||
return ss->cache->true_view_normal;
|
||||
case SCULPT_DISP_DIR_X:
|
||||
return float3(1, 0, 0);
|
||||
case SCULPT_DISP_DIR_Y:
|
||||
return float3(0, 1, 0);
|
||||
case SCULPT_DISP_DIR_Z:
|
||||
return float3(0, 0, 1);
|
||||
}
|
||||
BLI_assert_unreachable();
|
||||
return {};
|
||||
}
|
||||
|
||||
static void update_sculpt_normal(Sculpt *sd, Object *ob, Span<PBVHNode *> nodes)
|
||||
@@ -2666,7 +2658,7 @@ static void update_sculpt_normal(Sculpt *sd, Object *ob, Span<PBVHNode *> nodes)
|
||||
if (cache->mirror_symmetry_pass == 0 && cache->radial_symmetry_pass == 0 &&
|
||||
(SCULPT_stroke_is_first_brush_step_of_symmetry_pass(cache) || update_normal))
|
||||
{
|
||||
calc_sculpt_normal(sd, ob, nodes, cache->sculpt_normal);
|
||||
cache->sculpt_normal = calc_sculpt_normal(sd, ob, nodes);
|
||||
if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
|
||||
project_plane_v3_v3v3(cache->sculpt_normal, cache->sculpt_normal, cache->view_normal);
|
||||
normalize_v3(cache->sculpt_normal);
|
||||
@@ -4886,8 +4878,7 @@ bool SCULPT_cursor_geometry_info_update(bContext *C,
|
||||
Object *ob;
|
||||
SculptSession *ss;
|
||||
const Brush *brush = BKE_paint_brush(BKE_paint_get_active_from_context(C));
|
||||
float ray_start[3], ray_end[3], ray_normal[3], depth, face_normal[3], sampled_normal[3],
|
||||
mat[3][3];
|
||||
float ray_start[3], ray_end[3], ray_normal[3], depth, face_normal[3], mat[3][3];
|
||||
float viewDir[3] = {0.0f, 0.0f, 1.0f};
|
||||
bool original = false;
|
||||
|
||||
@@ -5000,9 +4991,10 @@ bool SCULPT_cursor_geometry_info_update(bContext *C,
|
||||
}
|
||||
|
||||
/* Calculate the sampled normal. */
|
||||
if (SCULPT_pbvh_calc_area_normal(brush, ob, nodes, sampled_normal)) {
|
||||
copy_v3_v3(out->normal, sampled_normal);
|
||||
copy_v3_v3(ss->cursor_sampled_normal, sampled_normal);
|
||||
if (const std::optional<float3> sampled_normal = SCULPT_pbvh_calc_area_normal(brush, ob, nodes))
|
||||
{
|
||||
copy_v3_v3(out->normal, *sampled_normal);
|
||||
copy_v3_v3(ss->cursor_sampled_normal, *sampled_normal);
|
||||
}
|
||||
else {
|
||||
/* Use face normal when there are no vertices to sample inside the cursor radius. */
|
||||
|
||||
@@ -601,9 +601,9 @@ void SCULPT_do_clay_thumb_brush(Sculpt *sd, Object *ob, Span<PBVHNode *> nodes)
|
||||
Brush *brush = BKE_paint_brush(&sd->paint);
|
||||
|
||||
/* Sampled geometry normal and area center. */
|
||||
float area_no_sp[3];
|
||||
float area_no[3];
|
||||
float area_co_tmp[3];
|
||||
float3 area_no_sp;
|
||||
float3 area_no;
|
||||
float3 area_co_tmp;
|
||||
|
||||
float mat[4][4];
|
||||
float scale[4][4];
|
||||
@@ -612,10 +612,10 @@ void SCULPT_do_clay_thumb_brush(Sculpt *sd, Object *ob, Span<PBVHNode *> nodes)
|
||||
SCULPT_calc_brush_plane(sd, ob, nodes, area_no_sp, area_co_tmp);
|
||||
|
||||
if (brush->sculpt_plane != SCULPT_DISP_DIR_AREA || (brush->flag & BRUSH_ORIGINAL_NORMAL)) {
|
||||
SCULPT_calc_area_normal(sd, ob, nodes, area_no);
|
||||
area_no = SCULPT_calc_area_normal(sd, ob, nodes).value_or(float3(0));
|
||||
}
|
||||
else {
|
||||
copy_v3_v3(area_no, area_no_sp);
|
||||
area_no = area_no_sp;
|
||||
}
|
||||
|
||||
/* Delay the first daub because grab delta is not setup. */
|
||||
@@ -976,11 +976,11 @@ void SCULPT_do_clay_strips_brush(Sculpt *sd, Object *ob, Span<PBVHNode *> nodes)
|
||||
const float displace = radius * (0.18f + offset);
|
||||
|
||||
/* The sculpt-plane normal (whatever its set to). */
|
||||
float area_no_sp[3];
|
||||
float3 area_no_sp;
|
||||
|
||||
/* Geometry normal */
|
||||
float area_no[3];
|
||||
float area_co[3];
|
||||
float3 area_no;
|
||||
float3 area_co;
|
||||
|
||||
float temp[3];
|
||||
float mat[4][4];
|
||||
@@ -991,10 +991,10 @@ void SCULPT_do_clay_strips_brush(Sculpt *sd, Object *ob, Span<PBVHNode *> nodes)
|
||||
SCULPT_tilt_apply_to_normal(area_no_sp, ss->cache, brush->tilt_strength_factor);
|
||||
|
||||
if (brush->sculpt_plane != SCULPT_DISP_DIR_AREA || (brush->flag & BRUSH_ORIGINAL_NORMAL)) {
|
||||
SCULPT_calc_area_normal(sd, ob, nodes, area_no);
|
||||
area_no = SCULPT_calc_area_normal(sd, ob, nodes).value_or(float3(0));
|
||||
}
|
||||
else {
|
||||
copy_v3_v3(area_no, area_no_sp);
|
||||
area_no = area_no_sp;
|
||||
}
|
||||
|
||||
if (is_zero_v3(ss->cache->grab_delta_symmetry)) {
|
||||
|
||||
@@ -176,13 +176,13 @@ void cache_init(bContext *C,
|
||||
return !node_fully_masked_or_hidden(node) && node_in_sphere(node, co, radius_sq, true);
|
||||
});
|
||||
|
||||
if (BKE_paint_brush(&sd->paint) &&
|
||||
SCULPT_pbvh_calc_area_normal(brush, ob, nodes, ss->filter_cache->initial_normal))
|
||||
{
|
||||
copy_v3_v3(ss->last_normal, ss->filter_cache->initial_normal);
|
||||
const std::optional<float3> area_normal = SCULPT_pbvh_calc_area_normal(brush, ob, nodes);
|
||||
if (BKE_paint_brush(&sd->paint) && area_normal) {
|
||||
ss->filter_cache->initial_normal = *area_normal;
|
||||
ss->last_normal = ss->filter_cache->initial_normal;
|
||||
}
|
||||
else {
|
||||
copy_v3_v3(ss->filter_cache->initial_normal, ss->last_normal);
|
||||
ss->filter_cache->initial_normal = ss->last_normal;
|
||||
}
|
||||
|
||||
/* Update last stroke location */
|
||||
|
||||
@@ -1024,10 +1024,9 @@ void SCULPT_calc_brush_plane(Sculpt *sd,
|
||||
float r_area_no[3],
|
||||
float r_area_co[3]);
|
||||
|
||||
void SCULPT_calc_area_normal(Sculpt *sd,
|
||||
Object *ob,
|
||||
blender::Span<PBVHNode *> nodes,
|
||||
float r_area_no[3]);
|
||||
std::optional<blender::float3> SCULPT_calc_area_normal(Sculpt *sd,
|
||||
Object *ob,
|
||||
blender::Span<PBVHNode *> nodes);
|
||||
/**
|
||||
* This calculates flatten center and area normal together,
|
||||
* amortizing the memory bandwidth and loop overhead to calculate both at the same time.
|
||||
@@ -1545,10 +1544,9 @@ void relax_vertex(SculptSession *ss,
|
||||
/**
|
||||
* Expose 'calc_area_normal' externally (just for vertex paint).
|
||||
*/
|
||||
bool SCULPT_pbvh_calc_area_normal(const Brush *brush,
|
||||
Object *ob,
|
||||
blender::Span<PBVHNode *> nodes,
|
||||
float r_area_no[3]);
|
||||
std::optional<blender::float3> SCULPT_pbvh_calc_area_normal(const Brush *brush,
|
||||
Object *ob,
|
||||
blender::Span<PBVHNode *> nodes);
|
||||
|
||||
/**
|
||||
* Flip all the edit-data across the axis/axes specified by \a symm.
|
||||
|
||||
@@ -198,11 +198,11 @@ void SCULPT_do_multiplane_scrape_brush(Sculpt *sd, Object *ob, blender::Span<PBV
|
||||
const float displace = -radius * offset;
|
||||
|
||||
/* The sculpt-plane normal (whatever its set to) */
|
||||
float area_no_sp[3];
|
||||
float3 area_no_sp;
|
||||
|
||||
/* Geometry normal. */
|
||||
float area_no[3];
|
||||
float area_co[3];
|
||||
float3 area_no;
|
||||
float3 area_co;
|
||||
|
||||
float temp[3];
|
||||
float mat[4][4];
|
||||
@@ -210,10 +210,10 @@ void SCULPT_do_multiplane_scrape_brush(Sculpt *sd, Object *ob, blender::Span<PBV
|
||||
SCULPT_calc_brush_plane(sd, ob, nodes, area_no_sp, area_co);
|
||||
|
||||
if (brush->sculpt_plane != SCULPT_DISP_DIR_AREA || (brush->flag & BRUSH_ORIGINAL_NORMAL)) {
|
||||
SCULPT_calc_area_normal(sd, ob, nodes, area_no);
|
||||
area_no = SCULPT_calc_area_normal(sd, ob, nodes).value_or(float3(0));
|
||||
}
|
||||
else {
|
||||
copy_v3_v3(area_no, area_no_sp);
|
||||
area_no = area_no_sp;
|
||||
}
|
||||
|
||||
/* Delay the first daub because grab delta is not setup. */
|
||||
|
||||
Reference in New Issue
Block a user