[Bf-blender-cvs] [899ec8b6b80] master: Curves: use uv coordinates to attach curves to mesh

Jacques Lucke noreply at git.blender.org
Sun Jun 5 12:15:04 CEST 2022


Commit: 899ec8b6b8035bde44d49d228c30698499a87080
Author: Jacques Lucke
Date:   Sun Jun 5 12:14:13 2022 +0200
Branches: master
https://developer.blender.org/rB899ec8b6b8035bde44d49d228c30698499a87080

Curves: use uv coordinates to attach curves to mesh

This implements the new way to attach curves to a mesh surface using
a uv map (based on the recent discussion in T95776).

The curves data block now not only stores a reference to the surface object
but also a name of a uv map on that object. Having a uv map is optional
for most operations, but it will be required later for animation (when the
curves are supposed to be deformed based on deformation of the surface).

The "Empty Hair" operator in the Add menu sets the uv map name automatically
if possible. It's possible to start working without a uv map and to attach the
curves to a uv map later on. It's also possible to reattach the curves to a new
uv map using the "Curves > Snap to Nearest Surface" operator in curves sculpt
mode.

Note, the implementation to do the reverse lookup from uv to a position on the
surface is trivial and inefficient now. A more efficient data structure will be
implemented separately soon.

Differential Revision: https://developer.blender.org/D15125

===================================================================

M	source/blender/blenkernel/BKE_curves.hh
M	source/blender/blenkernel/intern/curves_geometry.cc
M	source/blender/blenlib/BLI_virtual_array.hh
M	source/blender/editors/curves/CMakeLists.txt
M	source/blender/editors/curves/intern/curves_ops.cc
M	source/blender/editors/object/object_add.cc
M	source/blender/editors/sculpt_paint/CMakeLists.txt
M	source/blender/editors/sculpt_paint/curves_sculpt_add.cc
M	source/blender/geometry/CMakeLists.txt
A	source/blender/geometry/GEO_reverse_uv_sampler.hh
A	source/blender/geometry/intern/reverse_uv_sampler.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_curves.hh b/source/blender/blenkernel/BKE_curves.hh
index 168b17bad30..dc67f1e7403 100644
--- a/source/blender/blenkernel/BKE_curves.hh
+++ b/source/blender/blenkernel/BKE_curves.hh
@@ -264,22 +264,10 @@ class CurvesGeometry : public ::CurvesGeometry {
   MutableSpan<float> nurbs_weights_for_write();
 
   /**
-   * The index of a triangle (#MLoopTri) that a curve is attached to.
-   * The index is -1, if the curve is not attached.
+   * UV coordinate for each curve that encodes where the curve is attached to the surface mesh.
    */
-  VArray<int> surface_triangle_indices() const;
-  MutableSpan<int> surface_triangle_indices_for_write();
-
-  /**
-   * Barycentric coordinates of the attachment point within a triangle.
-   * Only the first two coordinates are stored. The third coordinate can be derived because the sum
-   * of the three coordinates is 1.
-   *
-   * When the triangle index is -1, this coordinate should be ignored.
-   * The span can be empty, when all triangle indices are -1.
-   */
-  Span<float2> surface_triangle_coords() const;
-  MutableSpan<float2> surface_triangle_coords_for_write();
+  Span<float2> surface_uv_coords() const;
+  MutableSpan<float2> surface_uv_coords_for_write();
 
   VArray<float> selection_point_float() const;
   MutableSpan<float> selection_point_float_for_write();
diff --git a/source/blender/blenkernel/intern/curves_geometry.cc b/source/blender/blenkernel/intern/curves_geometry.cc
index c2e67d853c9..2fa31bd4100 100644
--- a/source/blender/blenkernel/intern/curves_geometry.cc
+++ b/source/blender/blenkernel/intern/curves_geometry.cc
@@ -35,10 +35,9 @@ static const std::string ATTR_HANDLE_POSITION_RIGHT = "handle_right";
 static const std::string ATTR_NURBS_ORDER = "nurbs_order";
 static const std::string ATTR_NURBS_WEIGHT = "nurbs_weight";
 static const std::string ATTR_NURBS_KNOTS_MODE = "knots_mode";
-static const std::string ATTR_SURFACE_TRIANGLE_INDEX = "surface_triangle_index";
-static const std::string ATTR_SURFACE_TRIANGLE_COORDINATE = "surface_triangle_coordinate";
 static const std::string ATTR_SELECTION_POINT_FLOAT = ".selection_point_float";
 static const std::string ATTR_SELECTION_CURVE_FLOAT = ".selection_curve_float";
+static const std::string ATTR_SURFACE_UV_COORDINATE = "surface_uv_coordinate";
 
 /* -------------------------------------------------------------------- */
 /** \name Constructors/Destructor
@@ -419,24 +418,14 @@ MutableSpan<int8_t> CurvesGeometry::nurbs_knots_modes_for_write()
   return get_mutable_attribute<int8_t>(*this, ATTR_DOMAIN_CURVE, ATTR_NURBS_KNOTS_MODE, 0);
 }
 
-VArray<int> CurvesGeometry::surface_triangle_indices() const
+Span<float2> CurvesGeometry::surface_uv_coords() const
 {
-  return get_varray_attribute<int>(*this, ATTR_DOMAIN_CURVE, ATTR_SURFACE_TRIANGLE_INDEX, -1);
+  return get_span_attribute<float2>(*this, ATTR_DOMAIN_CURVE, ATTR_SURFACE_UV_COORDINATE);
 }
 
-MutableSpan<int> CurvesGeometry::surface_triangle_indices_for_write()
+MutableSpan<float2> CurvesGeometry::surface_uv_coords_for_write()
 {
-  return get_mutable_attribute<int>(*this, ATTR_DOMAIN_CURVE, ATTR_SURFACE_TRIANGLE_INDEX, -1);
-}
-
-Span<float2> CurvesGeometry::surface_triangle_coords() const
-{
-  return get_span_attribute<float2>(*this, ATTR_DOMAIN_CURVE, ATTR_SURFACE_TRIANGLE_COORDINATE);
-}
-
-MutableSpan<float2> CurvesGeometry::surface_triangle_coords_for_write()
-{
-  return get_mutable_attribute<float2>(*this, ATTR_DOMAIN_CURVE, ATTR_SURFACE_TRIANGLE_COORDINATE);
+  return get_mutable_attribute<float2>(*this, ATTR_DOMAIN_CURVE, ATTR_SURFACE_UV_COORDINATE);
 }
 
 VArray<float> CurvesGeometry::selection_point_float() const
diff --git a/source/blender/blenlib/BLI_virtual_array.hh b/source/blender/blenlib/BLI_virtual_array.hh
index ab4ca185ddb..0705d423f01 100644
--- a/source/blender/blenlib/BLI_virtual_array.hh
+++ b/source/blender/blenlib/BLI_virtual_array.hh
@@ -1138,6 +1138,8 @@ template<typename T> class VArray_Span final : public Span<T> {
   Array<T> owned_data_;
 
  public:
+  VArray_Span() = default;
+
   VArray_Span(VArray<T> varray) : Span<T>(), varray_(std::move(varray))
   {
     this->size_ = varray_.size();
diff --git a/source/blender/editors/curves/CMakeLists.txt b/source/blender/editors/curves/CMakeLists.txt
index a5d8390e7f2..3c31e8014ff 100644
--- a/source/blender/editors/curves/CMakeLists.txt
+++ b/source/blender/editors/curves/CMakeLists.txt
@@ -7,6 +7,7 @@ set(INC
   ../../blentranslation
   ../../depsgraph
   ../../functions
+  ../../geometry
   ../../makesdna
   ../../makesrna
   ../../windowmanager
diff --git a/source/blender/editors/curves/intern/curves_ops.cc b/source/blender/editors/curves/intern/curves_ops.cc
index 85214e1608d..54f0a0208fb 100644
--- a/source/blender/editors/curves/intern/curves_ops.cc
+++ b/source/blender/editors/curves/intern/curves_ops.cc
@@ -17,6 +17,7 @@
 
 #include "WM_api.h"
 
+#include "BKE_attribute_math.hh"
 #include "BKE_bvhutils.h"
 #include "BKE_context.h"
 #include "BKE_curves.hh"
@@ -45,6 +46,8 @@
 #include "RNA_enum_types.h"
 #include "RNA_prototypes.h"
 
+#include "GEO_reverse_uv_sampler.hh"
+
 /**
  * The code below uses a suffix naming convention to indicate the coordinate space:
  * `cu`: Local space of the curves object that is being edited.
@@ -184,25 +187,20 @@ static void try_convert_single_object(Object &curves_ob,
   }
   Mesh &surface_me = *static_cast<Mesh *>(surface_ob.data);
 
+  BVHTreeFromMesh surface_bvh;
+  BKE_bvhtree_from_mesh_get(&surface_bvh, &surface_me, BVHTREE_FROM_LOOPTRI, 2);
+  BLI_SCOPED_DEFER([&]() { free_bvhtree_from_mesh(&surface_bvh); });
+
   const Span<float3> positions_cu = curves.positions();
-  const VArray<int> looptri_indices = curves.surface_triangle_indices();
   const Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(&surface_me),
                                 BKE_mesh_runtime_looptri_len(&surface_me)};
 
-  /* Find indices of curves that can be transferred to the old hair system. */
-  Vector<int> curves_indices_to_transfer;
-  for (const int curve_i : curves.curves_range()) {
-    const int looptri_i = looptri_indices[curve_i];
-    if (looptri_i >= 0 && looptri_i < looptris.size()) {
-      curves_indices_to_transfer.append(curve_i);
-    }
-    else {
-      *r_could_not_convert_some_curves = true;
-    }
+  if (looptris.is_empty()) {
+    *r_could_not_convert_some_curves = true;
   }
 
-  const int hairs_num = curves_indices_to_transfer.size();
-  if (hairs_num == 0) {
+  const int hair_num = curves.curves_num();
+  if (hair_num == 0) {
     return;
   }
 
@@ -228,8 +226,8 @@ static void try_convert_single_object(Object &curves_ob,
   psys_changed_type(&surface_ob, particle_system);
 
   MutableSpan<ParticleData> particles{
-      static_cast<ParticleData *>(MEM_calloc_arrayN(hairs_num, sizeof(ParticleData), __func__)),
-      hairs_num};
+      static_cast<ParticleData *>(MEM_calloc_arrayN(hair_num, sizeof(ParticleData), __func__)),
+      hair_num};
 
   /* The old hair system still uses #MFace, so make sure those are available on the mesh. */
   BKE_mesh_tessface_calc(&surface_me);
@@ -250,17 +248,23 @@ static void try_convert_single_object(Object &curves_ob,
   const float4x4 world_to_surface_mat = surface_to_world_mat.inverted();
   const float4x4 curves_to_surface_mat = world_to_surface_mat * curves_to_world_mat;
 
-  for (const int new_hair_i : curves_indices_to_transfer.index_range()) {
-    const int curve_i = curves_indices_to_transfer[new_hair_i];
+  for (const int new_hair_i : IndexRange(hair_num)) {
+    const int curve_i = new_hair_i;
     const IndexRange points = curves.points_for_curve(curve_i);
 
-    const int looptri_i = looptri_indices[curve_i];
-    const MLoopTri &looptri = looptris[looptri_i];
-    const int poly_i = looptri.poly;
-
     const float3 &root_pos_cu = positions_cu[points.first()];
     const float3 root_pos_su = curves_to_surface_mat * root_pos_cu;
 
+    BVHTreeNearest nearest;
+    nearest.dist_sq = FLT_MAX;
+    BLI_bvhtree_find_nearest(
+        surface_bvh.tree, root_pos_su, &nearest, surface_bvh.nearest_callback, &surface_bvh);
+    BLI_assert(nearest.index >= 0);
+
+    const int looptri_i = nearest.index;
+    const MLoopTri &looptri = looptris[looptri_i];
+    const int poly_i = looptri.poly;
+
     const int mface_i = find_mface_for_root_position(
         surface_me, poly_to_mface_map[poly_i], root_pos_su);
     const MFace &mface = surface_me.mface[mface_i];
@@ -520,7 +524,7 @@ static int snap_curves_to_surface_exec(bContext *C, wmOperator *op)
 {
   const AttachMode attach_mode = static_cast<AttachMode>(RNA_enum_get(op->ptr, "attach_mode"));
 
-  std::atomic<bool> found_invalid_looptri_index = false;
+  std::atomic<bool> found_invalid_uv = false;
 
   CTX_DATA_BEGIN (C, Object *, curves_ob, selected_objects) {
     if (curves_ob->type != OB_CURVES) {
@@ -537,9 +541,19 @@ static int snap_curves_to_surface_exec(bContext *C, wmOperator *op)
     }
     Mesh &surface_mesh = *static_cast<Mesh *>(surface_ob.data);
 
+    MeshComponent surface_mesh_component;
+    surface_mesh_component.replace(&surface_mesh, GeometryOwnershipType::ReadOnly);
+
+    VArray_Span<float2> surface_uv_map;
+    if (curves_id.surface_uv_map != nullptr) {
+      surface_uv_map = surface_mesh_component
+                           .attribute_try_get_for_read(
+                               curves_id.surface_uv_map, ATTR_DOMAIN_CORNER, CD_PROP_FLOAT2)
+                           .typed<float2>();
+    }
+
     MutableSpan<float3> positions_cu = curves.positions_for_write();
-    MutableSpan<int> surface_triangle_indices = curves.surface_triangle_indices_for_write();
-    MutableSpan<float2> surface_triangle_coords = curves.surface_triangle_coords_for_write();
+    MutableSpan<float2> surface_uv_coords = curves.surface_uv_coords_for_write();
 
     const Span<MLoopTri> surface_looptris = {BKE_mesh_runtime_looptri_ensure(&surface_mesh),
                                              BKE_mesh_runtime_looptri_len(&surface_mesh)};
@@ -585,36 +599,50 @@ static int snap_curves_to_surface_exec(bContext *C, wmOperator *op)
               pos_cu += pos_diff_cu;
             }
 
-            surface_triangle_indices[cu

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list