[Bf-blender-cvs] [5ae95277709] geometry-nodes: Geometry Nodes: Support pointcloud in transform node

Hans Goudey noreply at git.blender.org
Tue Oct 27 19:00:05 CET 2020


Commit: 5ae952777098cfbcd3c93fb77a240c5da27b8c1f
Author: Hans Goudey
Date:   Tue Oct 27 12:59:58 2020 -0500
Branches: geometry-nodes
https://developer.blender.org/rB5ae952777098cfbcd3c93fb77a240c5da27b8c1f

Geometry Nodes: Support pointcloud in transform node

This is likely not the final implementation of the transform node, but
it's  a good trivial case for supporting multiple geometry data types.

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

M	source/blender/nodes/geometry/nodes/node_geo_transform.cc

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

diff --git a/source/blender/nodes/geometry/nodes/node_geo_transform.cc b/source/blender/nodes/geometry/nodes/node_geo_transform.cc
index d0ac41b3641..fe74cd84281 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_transform.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_transform.cc
@@ -14,9 +14,12 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#include "BKE_mesh.h"
 #include "BLI_math_matrix.h"
 
+#include "DNA_pointcloud_types.h"
+
+#include "BKE_mesh.h"
+
 #include "node_geometry_util.hh"
 
 static bNodeSocketTemplate geo_node_transform_in[] = {
@@ -32,32 +35,80 @@ static bNodeSocketTemplate geo_node_transform_out[] = {
     {-1, ""},
 };
 
+using blender::float3;
+
+static bool use_translate(const float3 rotation, const float3 scale)
+{
+  if (compare_ff(rotation.length_squared(), 0.0f, 1e-9f) != 1) {
+    return false;
+  }
+  if (compare_ff(scale.x, 1.0f, 1e-9f) != 1 || compare_ff(scale.y, 1.0f, 1e-9f) != 1 ||
+      compare_ff(scale.z, 1.0f, 1e-9f) != 1) {
+    return false;
+  }
+  return true;
+}
+
+static void transform_mesh(Mesh *mesh,
+                           const float3 translation,
+                           const float3 rotation,
+                           const float3 scale)
+{
+  /* Use only translation if rotation and scale are zero. */
+  if (use_translate(rotation, scale)) {
+    BKE_mesh_translate(mesh, translation, true);
+  }
+  else {
+    float mat[4][4];
+    loc_eul_size_to_mat4(mat, translation, rotation, scale);
+    BKE_mesh_transform(mesh, mat, true);
+  }
+}
+
+static void transform_pointcloud(PointCloud *pointcloud,
+                                 const float3 translation,
+                                 const float3 rotation,
+                                 const float3 scale)
+{
+  /* Use only translation if rotation and scale don't apply. */
+  if (use_translate(rotation, scale)) {
+    for (int i = 0; i < pointcloud->totpoint; i++) {
+      add_v3_v3(pointcloud->co[i], translation);
+    }
+  }
+  else {
+    float mat[4][4];
+    loc_eul_size_to_mat4(mat, translation, rotation, scale);
+    for (int i = 0; i < pointcloud->totpoint; i++) {
+      mul_m4_v3(mat, pointcloud->co[i]);
+    }
+  }
+}
+
 namespace blender::nodes {
 static void geo_transform_exec(bNode *UNUSED(node), GValueByName &inputs, GValueByName &outputs)
 {
   GeometryPtr geometry = inputs.extract<GeometryPtr>("Geometry");
 
-  if (!geometry.has_value() || !geometry->mesh_available()) {
+  if (!geometry.has_value()) {
     outputs.move_in("Geometry", std::move(geometry));
     return;
   }
 
-  bke::make_geometry_mutable(geometry);
-
-  Mesh *mesh = geometry->mesh_get_for_write();
-
   const float3 translation = inputs.extract<float3>("Translation");
   const float3 rotation = inputs.extract<float3>("Rotation");
   const float3 scale = inputs.extract<float3>("Scale");
 
-  /* Use only translation if rotation and scale are zero. */
-  if (translation.length() > 0.0f && rotation.length() == 0.0f && scale.length() == 0.0f) {
-    BKE_mesh_translate(mesh, translation, true);
+  bke::make_geometry_mutable(geometry);
+
+  if (geometry->mesh_available()) {
+    Mesh *mesh = geometry->mesh_get_for_write();
+    transform_mesh(mesh, translation, rotation, scale);
   }
-  else {
-    float mat[4][4];
-    loc_eul_size_to_mat4(mat, translation, rotation, scale);
-    BKE_mesh_transform(mesh, mat, true);
+
+  if (geometry->pointcloud_available()) {
+    PointCloud *pointcloud = geometry->pointcloud_get_for_write();
+    transform_pointcloud(pointcloud, translation, rotation, scale);
   }
 
   outputs.move_in("Geometry", std::move(geometry));



More information about the Bf-blender-cvs mailing list