Cleanup: simplify Add Curves brush

This commit is contained in:
Jacques Lucke 2022-06-17 16:33:30 +02:00
parent 52b93c423d
commit 23662a9a84
1 changed files with 36 additions and 59 deletions

View File

@ -87,25 +87,16 @@ struct AddOperationExecutor {
Object *surface_ob_ = nullptr;
Mesh *surface_ = nullptr;
Span<MLoopTri> surface_looptris_;
Span<float3> corner_normals_su_;
VArray_Span<float2> surface_uv_map_;
const CurvesSculpt *curves_sculpt_ = nullptr;
const Brush *brush_ = nullptr;
const BrushCurvesSculptSettings *brush_settings_ = nullptr;
int add_amount_;
bool use_front_face_;
float brush_radius_re_;
float2 brush_pos_re_;
bool use_front_face_;
bool interpolate_length_;
bool interpolate_shape_;
bool interpolate_point_count_;
bool use_interpolation_;
float new_curve_length_;
int add_amount_;
int constant_points_per_curve_;
/** Various matrices to convert between coordinate spaces. */
float4x4 curves_to_world_mat_;
float4x4 curves_to_surface_mat_;
@ -117,24 +108,12 @@ struct AddOperationExecutor {
BVHTreeFromMesh surface_bvh_;
int tot_old_curves_;
int tot_old_points_;
struct AddedPoints {
Vector<float3> positions_cu;
Vector<float3> bary_coords;
Vector<int> looptri_indices;
};
struct NeighborInfo {
/* Curve index of the neighbor. */
int index;
/* The weights of all neighbors of a new curve add up to 1. */
float weight;
};
static constexpr int max_neighbors = 5;
using NeighborsVector = Vector<NeighborInfo, max_neighbors>;
AddOperationExecutor(const bContext &C) : ctx_(C)
{
}
@ -162,13 +141,6 @@ struct AddOperationExecutor {
surface_to_curves_normal_mat_ = surface_to_curves_mat_.inverted().transposed();
curves_to_surface_mat_ = world_to_surface_mat_ * curves_to_world_mat_;
if (!CustomData_has_layer(&surface_->ldata, CD_NORMAL)) {
BKE_mesh_calc_normals_split(surface_);
}
corner_normals_su_ = {
reinterpret_cast<const float3 *>(CustomData_get_layer(&surface_->ldata, CD_NORMAL)),
surface_->totloop};
curves_sculpt_ = ctx_.scene->toolsettings->curves_sculpt;
brush_ = BKE_paint_brush_for_read(&curves_sculpt_->paint);
brush_settings_ = brush_->curves_sculpt_settings;
@ -179,16 +151,6 @@ struct AddOperationExecutor {
const eBrushFalloffShape falloff_shape = static_cast<eBrushFalloffShape>(
brush_->falloff_shape);
add_amount_ = std::max(0, brush_settings_->add_amount);
constant_points_per_curve_ = std::max(2, brush_settings_->points_per_curve);
interpolate_length_ = brush_settings_->flag & BRUSH_CURVES_SCULPT_FLAG_INTERPOLATE_LENGTH;
interpolate_shape_ = brush_settings_->flag & BRUSH_CURVES_SCULPT_FLAG_INTERPOLATE_SHAPE;
interpolate_point_count_ = brush_settings_->flag &
BRUSH_CURVES_SCULPT_FLAG_INTERPOLATE_POINT_COUNT;
use_interpolation_ = interpolate_length_ || interpolate_shape_ || interpolate_point_count_;
new_curve_length_ = brush_settings_->curve_length;
tot_old_curves_ = curves_->curves_num();
tot_old_points_ = curves_->points_num();
if (add_amount_ == 0) {
return;
@ -204,15 +166,6 @@ struct AddOperationExecutor {
surface_looptris_ = {BKE_mesh_runtime_looptri_ensure(surface_),
BKE_mesh_runtime_looptri_len(surface_)};
if (curves_id_->surface_uv_map != nullptr) {
MeshComponent surface_component;
surface_component.replace(surface_, GeometryOwnershipType::ReadOnly);
surface_uv_map_ = surface_component
.attribute_try_get_for_read(curves_id_->surface_uv_map,
ATTR_DOMAIN_CORNER)
.typed<float2>();
}
/* Sample points on the surface using one of multiple strategies. */
AddedPoints added_points;
if (add_amount_ == 1) {
@ -233,27 +186,51 @@ struct AddOperationExecutor {
return;
}
if (use_interpolation_) {
this->ensure_curve_roots_kdtree();
/* Find UV map. */
VArray_Span<float2> surface_uv_map;
if (curves_id_->surface_uv_map != nullptr) {
MeshComponent surface_component;
surface_component.replace(surface_, GeometryOwnershipType::ReadOnly);
surface_uv_map = surface_component
.attribute_try_get_for_read(curves_id_->surface_uv_map,
ATTR_DOMAIN_CORNER)
.typed<float2>();
}
/* Find normals. */
if (!CustomData_has_layer(&surface_->ldata, CD_NORMAL)) {
BKE_mesh_calc_normals_split(surface_);
}
const Span<float3> corner_normals_su = {
reinterpret_cast<const float3 *>(CustomData_get_layer(&surface_->ldata, CD_NORMAL)),
surface_->totloop};
geometry::AddCurvesOnMeshInputs add_inputs;
add_inputs.root_positions_cu = added_points.positions_cu;
add_inputs.bary_coords = added_points.bary_coords;
add_inputs.looptri_indices = added_points.looptri_indices;
add_inputs.interpolate_length = interpolate_length_;
add_inputs.interpolate_shape = interpolate_shape_;
add_inputs.interpolate_point_count = interpolate_point_count_;
add_inputs.fallback_curve_length = new_curve_length_;
add_inputs.fallback_point_count = constant_points_per_curve_;
add_inputs.interpolate_length = brush_settings_->flag &
BRUSH_CURVES_SCULPT_FLAG_INTERPOLATE_LENGTH;
add_inputs.interpolate_shape = brush_settings_->flag &
BRUSH_CURVES_SCULPT_FLAG_INTERPOLATE_SHAPE;
add_inputs.interpolate_point_count = brush_settings_->flag &
BRUSH_CURVES_SCULPT_FLAG_INTERPOLATE_POINT_COUNT;
add_inputs.fallback_curve_length = brush_settings_->curve_length;
add_inputs.fallback_point_count = std::max(2, brush_settings_->points_per_curve);
add_inputs.surface = surface_;
add_inputs.surface_bvh = &surface_bvh_;
add_inputs.surface_looptris = surface_looptris_;
add_inputs.surface_uv_map = surface_uv_map_;
add_inputs.corner_normals_su = corner_normals_su_;
add_inputs.surface_uv_map = surface_uv_map;
add_inputs.corner_normals_su = corner_normals_su;
add_inputs.curves_to_surface_mat = curves_to_surface_mat_;
add_inputs.surface_to_curves_normal_mat = surface_to_curves_normal_mat_;
add_inputs.old_roots_kdtree = self_->curve_roots_kdtree_;
if (add_inputs.interpolate_length || add_inputs.interpolate_shape ||
add_inputs.interpolate_point_count) {
this->ensure_curve_roots_kdtree();
add_inputs.old_roots_kdtree = self_->curve_roots_kdtree_;
}
geometry::add_curves_on_mesh(*curves_, add_inputs);
DEG_id_tag_update(&curves_id_->id, ID_RECALC_GEOMETRY);