Blender V5.3
node_geo_deform_curves_on_surface.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6#include "BKE_curves.hh"
7#include "BKE_editmesh.hh"
8#include "BKE_lib_id.hh"
9#include "BKE_mesh.hh"
10#include "BKE_mesh_wrapper.hh"
11#include "BKE_modifier.hh"
12
13#include "BLI_math_matrix.hh"
14#include "BLI_task.hh"
15
17
19
20#include "node_geometry_util.hh"
21
22#include <fmt/format.h>
23
25
29
31
33{
34 b.use_custom_socket_order();
35 b.allow_any_socket_order();
36 b.add_input<decl::Geometry>("Curves"_ustr)
37 .supported_type(GeometryComponent::Type::Curve)
38 .description("Curves to deform");
39 b.add_output<decl::Geometry>("Curves"_ustr).propagate_all_geometry().align_with_previous();
40}
41
43 const Mesh &surface_mesh_old,
44 const Mesh &surface_mesh_new,
45 const Span<float2> curve_attachment_uvs,
46 const ReverseUVSampler &reverse_uv_sampler_old,
47 const ReverseUVSampler &reverse_uv_sampler_new,
48 const Span<float3> corner_normals_old,
49 const Span<float3> corner_normals_new,
50 const Span<float3> rest_positions,
51 const float4x4 &surface_to_curves,
52 MutableSpan<float3> r_positions,
53 MutableSpan<float3x3> r_rotations,
54 std::atomic<int> &r_invalid_uv_count)
55{
56 /* Find attachment points on old and new mesh. */
57 const int curves_num = curves.curves_num();
58 Array<ReverseUVSampler::Result> surface_samples_old(curves_num);
59 Array<ReverseUVSampler::Result> surface_samples_new(curves_num);
61 1024 < curves_num,
62 [&]() { reverse_uv_sampler_old.sample_many(curve_attachment_uvs, surface_samples_old); },
63 [&]() { reverse_uv_sampler_new.sample_many(curve_attachment_uvs, surface_samples_new); });
64
65 const float4x4 curves_to_surface = math::invert(surface_to_curves);
66
67 const Span<float3> surface_positions_old = surface_mesh_old.vert_positions();
68 const Span<int> surface_corner_verts_old = surface_mesh_old.corner_verts();
69 const Span<int3> surface_corner_tris_old = surface_mesh_old.corner_tris();
70
71 const Span<float3> surface_positions_new = surface_mesh_new.vert_positions();
72 const Span<int> surface_corner_verts_new = surface_mesh_new.corner_verts();
73 const Span<int3> surface_corner_tris_new = surface_mesh_new.corner_tris();
74
75 const OffsetIndices points_by_curve = curves.points_by_curve();
76
77 threading::parallel_for(curves.curves_range(), 256, [&](const IndexRange range) {
78 for (const int curve_i : range) {
79 const ReverseUVSampler::Result &surface_sample_old = surface_samples_old[curve_i];
80 if (surface_sample_old.type != ReverseUVSampler::ResultType::Ok) {
81 r_invalid_uv_count++;
82 continue;
83 }
84 const ReverseUVSampler::Result &surface_sample_new = surface_samples_new[curve_i];
85 if (surface_sample_new.type != ReverseUVSampler::ResultType::Ok) {
86 r_invalid_uv_count++;
87 continue;
88 }
89
90 const int3 &tri_old = surface_corner_tris_old[surface_sample_old.tri_index];
91 const int3 &tri_new = surface_corner_tris_new[surface_sample_new.tri_index];
92 const float3 &bary_weights_old = surface_sample_old.bary_weights;
93 const float3 &bary_weights_new = surface_sample_new.bary_weights;
94
95 const int corner_0_old = tri_old[0];
96 const int corner_1_old = tri_old[1];
97 const int corner_2_old = tri_old[2];
98
99 const int corner_0_new = tri_new[0];
100 const int corner_1_new = tri_new[1];
101 const int corner_2_new = tri_new[2];
102
103 const int vert_0_old = surface_corner_verts_old[corner_0_old];
104 const int vert_1_old = surface_corner_verts_old[corner_1_old];
105 const int vert_2_old = surface_corner_verts_old[corner_2_old];
106
107 const int vert_0_new = surface_corner_verts_new[corner_0_new];
108 const int vert_1_new = surface_corner_verts_new[corner_1_new];
109 const int vert_2_new = surface_corner_verts_new[corner_2_new];
110
111 const float3 &normal_0_old = corner_normals_old[corner_0_old];
112 const float3 &normal_1_old = corner_normals_old[corner_1_old];
113 const float3 &normal_2_old = corner_normals_old[corner_2_old];
114 const float3 normal_old = math::normalize(
115 mix3(bary_weights_old, normal_0_old, normal_1_old, normal_2_old));
116
117 const float3 &normal_0_new = corner_normals_new[corner_0_new];
118 const float3 &normal_1_new = corner_normals_new[corner_1_new];
119 const float3 &normal_2_new = corner_normals_new[corner_2_new];
120 const float3 normal_new = math::normalize(
121 mix3(bary_weights_new, normal_0_new, normal_1_new, normal_2_new));
122
123 const float3 &pos_0_old = surface_positions_old[vert_0_old];
124 const float3 &pos_1_old = surface_positions_old[vert_1_old];
125 const float3 &pos_2_old = surface_positions_old[vert_2_old];
126 const float3 pos_old = mix3(bary_weights_old, pos_0_old, pos_1_old, pos_2_old);
127
128 const float3 &pos_0_new = surface_positions_new[vert_0_new];
129 const float3 &pos_1_new = surface_positions_new[vert_1_new];
130 const float3 &pos_2_new = surface_positions_new[vert_2_new];
131 const float3 pos_new = mix3(bary_weights_new, pos_0_new, pos_1_new, pos_2_new);
132
133 /* The translation is just the difference between the old and new position on the surface. */
134 const float3 translation = pos_new - pos_old;
135
136 const float3 &rest_pos_0 = rest_positions[vert_0_new];
137 const float3 &rest_pos_1 = rest_positions[vert_1_new];
138
139 /* The tangent reference direction is used to determine the rotation of the surface point
140 * around its normal axis. It's important that the old and new tangent reference are computed
141 * in a consistent way. If the surface has not been rotated, the old and new tangent
142 * reference have to have the same direction. For that reason, the old tangent reference is
143 * computed based on the rest position attribute instead of positions on the old mesh. This
144 * way the old and new tangent reference use the same topology.
145 *
146 * TODO: Figure out if this can be smoothly interpolated across the surface as well.
147 * Currently, this is a source of discontinuity in the deformation, because the vector
148 * changes instantly from one triangle to the next. */
149 const float3 tangent_reference_dir_old = rest_pos_1 - rest_pos_0;
150 const float3 tangent_reference_dir_new = pos_1_new - pos_0_new;
151
152 /* Compute first local tangent based on the (potentially smoothed) normal and the tangent
153 * reference. */
154 const float3 tangent_x_old = math::normalize(
155 math::cross(normal_old, tangent_reference_dir_old));
156 const float3 tangent_x_new = math::normalize(
157 math::cross(normal_new, tangent_reference_dir_new));
158
159 /* The second tangent defined by the normal and first tangent. */
160 const float3 tangent_y_old = math::normalize(math::cross(normal_old, tangent_x_old));
161 const float3 tangent_y_new = math::normalize(math::cross(normal_new, tangent_x_new));
162
163 /* Construct rotation matrix that encodes the orientation of the old surface position. */
164 float3x3 rotation_old(tangent_x_old, tangent_y_old, normal_old);
165
166 /* Construct rotation matrix that encodes the orientation of the new surface position. */
167 float3x3 rotation_new(tangent_x_new, tangent_y_new, normal_new);
168
169 /* Can use transpose instead of inverse because the matrix is orthonormal. In the case of
170 * zero-area triangles, the matrix would not be orthonormal, but in this case, none of this
171 * works anyway. */
172 const float3x3 rotation_old_inv = math::transpose(rotation_old);
173
174 /* Compute a rotation matrix that rotates points from the old to the new surface
175 * orientation. */
176 const float3x3 rotation = rotation_new * rotation_old_inv;
177
178 /* Construction transformation matrix for this surface position that includes rotation and
179 * translation. */
180 /* Subtract and add #pos_old, so that the rotation origin is the position on the surface. */
181 float4x4 surface_transform = math::from_origin_transform<float4x4>(float4x4(rotation),
182 pos_old);
183 surface_transform.location() += translation;
184
185 /* Change the basis of the transformation so to that it can be applied in the local space of
186 * the curves. */
187 const float4x4 curve_transform = surface_to_curves * surface_transform * curves_to_surface;
188
189 /* Actually transform all points. */
190 const IndexRange points = points_by_curve[curve_i];
191 for (const int point_i : points) {
192 const float3 old_point_pos = r_positions[point_i];
193 const float3 new_point_pos = math::transform_point(curve_transform, old_point_pos);
194 r_positions[point_i] = new_point_pos;
195 }
196
197 if (!r_rotations.is_empty()) {
198 for (const int point_i : points) {
199 r_rotations[point_i] = rotation * r_rotations[point_i];
200 }
201 }
202 }
203 });
204}
205
207{
208 GeometrySet curves_geometry = params.extract_input<GeometrySet>("Curves"_ustr);
209
210 Mesh *surface_mesh_orig = nullptr;
211 bool free_suface_mesh_orig = false;
212 BLI_SCOPED_DEFER([&]() {
213 if (free_suface_mesh_orig) {
214 BKE_id_free(nullptr, surface_mesh_orig);
215 }
216 });
217
218 auto pass_through_input = [&]() {
219 params.set_output("Curves"_ustr, std::move(curves_geometry));
220 };
221
222 const Object *self_ob_eval = params.self_object();
223 if (self_ob_eval == nullptr || self_ob_eval->type != OB_CURVES) {
224 pass_through_input();
225 params.error_message_add(NodeWarningType::Error, TIP_("Node only works for curves objects"));
226 return;
227 }
228 const Curves *self_curves_eval = id_cast<const Curves *>(self_ob_eval->data);
229 if (self_curves_eval->surface_uv_map == nullptr || self_curves_eval->surface_uv_map[0] == '\0') {
230 pass_through_input();
231 params.error_message_add(NodeWarningType::Error, TIP_("Surface UV map not defined"));
232 return;
233 }
234 /* Take surface information from self-object. */
235 Object *surface_ob_eval = self_curves_eval->surface;
236 const StringRefNull uv_map_name = self_curves_eval->surface_uv_map;
237 const StringRefNull rest_position_name = "rest_position";
238
239 if (!curves_geometry.has_curves()) {
240 pass_through_input();
241 return;
242 }
243 if (surface_ob_eval == nullptr || surface_ob_eval->type != OB_MESH) {
244 pass_through_input();
245 params.error_message_add(NodeWarningType::Error, TIP_("Curves not attached to a surface"));
246 return;
247 }
248 Object *surface_ob_orig = DEG_get_original(surface_ob_eval);
249 Mesh &surface_object_data = *id_cast<Mesh *>(surface_ob_orig->data);
250
251 if (BMesh *bm = BKE_editmesh_bmesh_get_for_write(&surface_object_data)) {
252 surface_mesh_orig = BKE_mesh_from_bmesh_for_eval_nomain(bm, nullptr, &surface_object_data);
253 free_suface_mesh_orig = true;
254 }
255 else {
256 surface_mesh_orig = &surface_object_data;
257 }
258 Mesh *surface_mesh_eval = BKE_modifier_get_evaluated_mesh_from_evaluated_object(surface_ob_eval);
259 if (surface_mesh_eval == nullptr) {
260 pass_through_input();
261 params.error_message_add(NodeWarningType::Error, TIP_("Surface has no mesh"));
262 return;
263 }
264
265 BKE_mesh_wrapper_ensure_mdata(surface_mesh_eval);
266
267 const AttributeAccessor mesh_attributes_eval = surface_mesh_eval->attributes();
268 const AttributeAccessor mesh_attributes_orig = surface_mesh_orig->attributes();
269
270 Curves &curves_id = *curves_geometry.get_curves_for_write();
271 CurvesGeometry &curves = curves_id.geometry.wrap();
272
273 if (!mesh_attributes_eval.contains(uv_map_name)) {
274 pass_through_input();
275 const std::string message = fmt::format(
276 fmt::runtime(TIP_("Evaluated surface missing UV map: \"{}\"")), uv_map_name);
278 return;
279 }
280 if (!mesh_attributes_orig.contains(uv_map_name)) {
281 pass_through_input();
282 const std::string message = fmt::format(
283 fmt::runtime(TIP_("Original surface missing UV map: \"{}\"")), uv_map_name);
285 return;
286 }
287 if (!mesh_attributes_eval.contains(rest_position_name)) {
288 pass_through_input();
290 TIP_("Evaluated surface missing attribute: \"rest_position\""));
291 return;
292 }
293 if (!curves.surface_uv_coords() && curves.curves_num() > 0) {
294 pass_through_input();
296 TIP_("Curves are not attached to any UV map"));
297 return;
298 }
299 const VArraySpan uv_map_orig = *mesh_attributes_orig.lookup<float2>(uv_map_name,
300 AttrDomain::Corner);
301 const VArraySpan uv_map_eval = *mesh_attributes_eval.lookup<float2>(uv_map_name,
302 AttrDomain::Corner);
303 const VArraySpan rest_positions = *mesh_attributes_eval.lookup<float3>(rest_position_name,
304 AttrDomain::Point);
305 const VArraySpan surface_uv_coords = *curves.attributes().lookup_or_default<float2>(
306 "surface_uv_coordinate", AttrDomain::Curve, float2(0));
307
308 const Span<int3> corner_tris_orig = surface_mesh_orig->corner_tris();
309 const Span<int3> corner_tris_eval = surface_mesh_eval->corner_tris();
310 const ReverseUVSampler reverse_uv_sampler_orig{uv_map_orig, corner_tris_orig};
311 const ReverseUVSampler reverse_uv_sampler_eval{uv_map_eval, corner_tris_eval};
312
313 /* Retrieve face corner normals from each mesh. It's necessary to use face corner normals
314 * because face normals or vertex normals may lose information (custom normals, auto smooth) in
315 * some cases. */
316 const Span<float3> corner_normals_orig = surface_mesh_orig->corner_normals();
317 const Span<float3> corner_normals_eval = surface_mesh_eval->corner_normals();
318
319 std::atomic<int> invalid_uv_count = 0;
320
321 const bke::CurvesSurfaceTransforms transforms{*self_ob_eval, surface_ob_eval};
322
323 bke::CurvesEditHints *edit_hints = curves_geometry.get_curve_edit_hints_for_write();
324 MutableSpan<float3> edit_hint_positions;
325 MutableSpan<float3x3> edit_hint_rotations;
326 MutableSpan<float3x3> edit_hint_rotations_for_curves;
327 if (edit_hints != nullptr) {
328 if (const std::optional<MutableSpan<float3>> positions = edit_hints->positions_for_write()) {
329 edit_hint_positions = *positions;
330 }
331 if (!edit_hints->deform_mats.has_value()) {
332 edit_hints->deform_mats.emplace(edit_hints->curves_id_orig.geometry.point_num,
334 edit_hints->deform_mats->fill(float3x3::identity());
335 }
336 edit_hint_rotations = *edit_hints->deform_mats;
337 /* These matrices correspond to original point indices, so they cannot be used when the
338 * evaluated geometry has a different number of points. */
339 if (curves.points_num() == edit_hints->curves_id_orig.geometry.point_num) {
340 edit_hint_rotations_for_curves = edit_hint_rotations;
341 }
342 }
343
344 if (edit_hint_positions.is_empty()) {
346 *surface_mesh_orig,
347 *surface_mesh_eval,
348 surface_uv_coords,
349 reverse_uv_sampler_orig,
350 reverse_uv_sampler_eval,
351 corner_normals_orig,
352 corner_normals_eval,
353 rest_positions,
354 transforms.surface_to_curves,
355 curves.positions_for_write(),
356 edit_hint_rotations_for_curves,
357 invalid_uv_count);
358 }
359 else {
360 /* First deform the actual curves in the input geometry. */
362 *surface_mesh_orig,
363 *surface_mesh_eval,
364 surface_uv_coords,
365 reverse_uv_sampler_orig,
366 reverse_uv_sampler_eval,
367 corner_normals_orig,
368 corner_normals_eval,
369 rest_positions,
370 transforms.surface_to_curves,
371 curves.positions_for_write(),
372 {},
373 invalid_uv_count);
374 /* Then also deform edit curve information for use in sculpt mode. */
375 const CurvesGeometry &curves_orig = edit_hints->curves_id_orig.geometry.wrap();
376 const VArraySpan<float2> surface_uv_coords_orig = *curves_orig.attributes().lookup_or_default(
377 "surface_uv_coordinate", AttrDomain::Curve, float2(0));
378 if (!surface_uv_coords_orig.is_empty()) {
379 deform_curves(curves_orig,
380 *surface_mesh_orig,
381 *surface_mesh_eval,
382 surface_uv_coords_orig,
383 reverse_uv_sampler_orig,
384 reverse_uv_sampler_eval,
385 corner_normals_orig,
386 corner_normals_eval,
387 rest_positions,
388 transforms.surface_to_curves,
389 edit_hint_positions,
390 edit_hint_rotations,
391 invalid_uv_count);
392 }
393 }
394
395 curves.tag_positions_changed();
396
397 if (invalid_uv_count) {
398 const std::string message = fmt::format(fmt::runtime(TIP_("Invalid surface UVs on {} curves")),
399 invalid_uv_count.load());
401 }
402
403 params.set_output("Curves"_ustr, curves_geometry);
404}
405
406static void node_register()
407{
408 static bke::bNodeType ntype;
410 &ntype, "GeometryNodeDeformCurvesOnSurface"_ustr, GEO_NODE_DEFORM_CURVES_ON_SURFACE);
411 ntype.ui_name = "Deform Curves on Surface";
412 ntype.ui_description =
413 "Translate and rotate curves based on changes between the object's original and evaluated "
414 "surface mesh";
415 ntype.enum_name_legacy = "DEFORM_CURVES_ON_SURFACE";
418 ntype.declare = node_declare;
421}
422NOD_REGISTER_NODE(node_register)
423
424} // namespace blender::nodes::node_geo_deform_curves_on_surface_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_DEFORM_CURVES_ON_SURFACE
#define BLI_SCOPED_DEFER(function_to_defer)
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr bool is_empty() const
Definition BLI_span.hh:515
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
bool contains(StringRef name) const
GAttributeReader lookup(const StringRef name) const
std::optional< MutableSpan< float3 > > positions_for_write()
std::optional< Array< float3x3 > > deform_mats
AttributeAccessor attributes() const
constexpr bool is_empty() const
void sample_many(Span< float2 > query_uvs, MutableSpan< Result > r_results) const
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
T mix3(const float3 &weights, const T &v0, const T &v1, const T &v2)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
CartesianBasis invert(const CartesianBasis &basis)
static void deform_curves(const CurvesGeometry &curves, const Mesh &surface_mesh_old, const Mesh &surface_mesh_new, const Span< float2 > curve_attachment_uvs, const ReverseUVSampler &reverse_uv_sampler_old, const ReverseUVSampler &reverse_uv_sampler_new, const Span< float3 > corner_normals_old, const Span< float3 > corner_normals_new, const Span< float3 > rest_positions, const float4x4 &surface_to_curves, MutableSpan< float3 > r_positions, MutableSpan< float3x3 > r_rotations, std::atomic< int > &r_invalid_uv_count)
void parallel_invoke(Functions &&...functions)
Definition BLI_task.hh:197
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
void BKE_mesh_wrapper_ensure_mdata(Mesh *mesh)
MatBase< float, 4, 4 > float4x4
Mesh * BKE_modifier_get_evaluated_mesh_from_evaluated_object(Object *ob_eval)
VecBase< float, 2 > float2
void BKE_id_free(Main *bmain, void *idv)
T * DEG_get_original(T *id)
Mesh * BKE_mesh_from_bmesh_for_eval_nomain(BMesh *bm, const CustomData_MeshMasks *cd_mask_extra, const Mesh *me_settings)
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
CurvesEditHints * get_curve_edit_hints_for_write()
static constexpr int _180
Definition BKE_node.hh:247
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393