79 const float surface_collision_distance)
85 const int max_collisions = 5;
89 for (
const int curve_i : segment) {
90 const IndexRange points = points_by_curve[curve_i];
95 bool revert_curve =
false;
96 for (
const int point_i : points.
drop_front(1)) {
97 const float goal_segment_length_cu = segment_lengths_cu[point_i - 1];
98 const float3 &prev_pos_cu = positions_cu[point_i - 1];
99 const float3 &start_pos_cu = start_positions_cu[point_i];
101 int used_iterations = 0;
102 for ([[maybe_unused]]
const int iteration :
IndexRange(max_collisions)) {
104 const float3 &old_pos_cu = positions_cu[point_i];
105 if (start_pos_cu == old_pos_cu) {
115 const float3 pos_diff_su = old_pos_su - start_pos_su;
116 float max_ray_length_su;
121 start_pos_su, ray_direction_su, max_ray_length_su + surface_collision_distance);
123 const std::optional<bke::bvh::RayHit> hit = surface_bvh.
ray_intersect(ray);
129 if (
math::dot(hit_normal_su, ray_direction_su) > 0.0f) {
143 const float3 plane_pos_cu = hit_pos_cu + hit_normal_cu * surface_collision_distance;
144 const float3 plane_normal_cu = hit_normal_cu;
148 const float3 collided_segment_cu = plane_pos_cu - prev_pos_cu;
149 const float3 slide_normal_cu = plane_normal_cu *
150 math::dot(collided_segment_cu, plane_normal_cu);
151 const float3 slide_direction_cu = collided_segment_cu - slide_normal_cu;
153 float slide_direction_length_cu;
155 slide_direction_cu, slide_direction_length_cu);
158 if (
pow2f(goal_segment_length_cu) > slide_normal_length_sq_cu) {
160 const float slide_distance_cu = std::sqrt(
pow2f(goal_segment_length_cu) -
161 slide_normal_length_sq_cu) -
162 slide_direction_length_cu;
163 positions_cu[point_i] = plane_pos_cu +
164 normalized_slide_direction_cu * slide_distance_cu;
170 positions_cu[point_i] = prev_pos_cu +
math::normalize(old_pos_su - prev_pos_cu) *
171 goal_segment_length_cu;
174 if (used_iterations == max_collisions) {
void solve_length_and_collision_constraints(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, Span< float > segment_lengths, Span< float3 > start_positions, const Mesh &surface, const bke::CurvesSurfaceTransforms &transforms, MutableSpan< float3 > positions, const float surface_collision_distance)