[Bf-blender-cvs] [eca2804a860] temp-T96710-pbvh-pixels: Barycentric weights refactoring.

Jeroen Bakker noreply at git.blender.org
Mon Apr 4 13:01:26 CEST 2022


Commit: eca2804a8601036f0da4b5f91826f1f75dcd8d71
Author: Jeroen Bakker
Date:   Mon Apr 4 13:01:21 2022 +0200
Branches: temp-T96710-pbvh-pixels
https://developer.blender.org/rBeca2804a8601036f0da4b5f91826f1f75dcd8d71

Barycentric weights refactoring.

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

M	source/blender/blenkernel/BKE_pbvh.hh
M	source/blender/blenkernel/intern/pbvh_pixels.cc
M	source/blender/editors/sculpt_paint/sculpt_paint_image.cc

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

diff --git a/source/blender/blenkernel/BKE_pbvh.hh b/source/blender/blenkernel/BKE_pbvh.hh
index 39acf17973d..85980b9cba4 100644
--- a/source/blender/blenkernel/BKE_pbvh.hh
+++ b/source/blender/blenkernel/BKE_pbvh.hh
@@ -2,6 +2,7 @@
 
 #pragma once
 
+#include "BLI_math.h"
 #include "BLI_math_vec_types.hh"
 #include "BLI_rect.h"
 #include "BLI_vector.hh"
@@ -16,20 +17,60 @@
 namespace blender::bke::pbvh::pixels {
 
 /* TODO(jbakker): move encoders to blenlib. */
+class EncodedBarycentricCoord;
 
 /* Stores a barycentric coordinate in a float2. */
-struct EncodedBarycentricCoord {
+class BarycentricWeights {
+ private:
+  float3 weights;
+
+ public:
+  explicit BarycentricWeights(const float2 v1, const float2 v2, const float2 v3, const float2 co)
+  {
+    barycentric_weights_v2(v1, v2, v3, co, weights);
+  }
+
+  explicit BarycentricWeights(const float3 weights) : weights(weights)
+  {
+  }
+
+  const bool is_inside_triangle() const
+  {
+    return barycentric_inside_triangle_v2(weights);
+  }
+
+  float3 operator-(const BarycentricWeights &rhs) const
+  {
+    return weights - rhs.weights;
+  }
+
+  BarycentricWeights operator+(const float3 &rhs) const
+  {
+    return BarycentricWeights(weights + rhs);
+  }
+
+  operator const float *() const
+  {
+    return weights;
+  }
+
+  friend class EncodedBarycentricCoord;
+};
+
+class EncodedBarycentricCoord {
+ private:
   float2 encoded;
 
-  EncodedBarycentricCoord &operator=(const float3 decoded)
+ public:
+  EncodedBarycentricCoord &operator=(const BarycentricWeights decoded)
   {
-    encoded = float2(decoded.x, decoded.y);
+    encoded = float2(decoded.weights.x, decoded.weights.y);
     return *this;
   }
 
-  float3 decode() const
+  const BarycentricWeights decode() const
   {
-    return float3(encoded.x, encoded.y, 1.0 - encoded.x - encoded.y);
+    return BarycentricWeights(float3(encoded.x, encoded.y, 1.0 - encoded.x - encoded.y));
   }
 };
 
diff --git a/source/blender/blenkernel/intern/pbvh_pixels.cc b/source/blender/blenkernel/intern/pbvh_pixels.cc
index 006f78c45ce..a22cfb88f9a 100644
--- a/source/blender/blenkernel/intern/pbvh_pixels.cc
+++ b/source/blender/blenkernel/intern/pbvh_pixels.cc
@@ -22,21 +22,6 @@
 
 namespace blender::bke::pbvh::pixels::extractor {
 
-static float3 barycentric_weights(const float2 v1,
-                                  const float2 v2,
-                                  const float2 v3,
-                                  const float2 co)
-{
-  float3 weights;
-  barycentric_weights_v2(v1, v2, v3, co, weights);
-  return weights;
-}
-
-static bool is_inside_triangle(const float3 barycentric_weights)
-{
-  return barycentric_inside_triangle_v2(barycentric_weights);
-}
-
 /**
  * Keep track of visited polygons.
  *
@@ -56,7 +41,7 @@ class VisitedPolygons : std::vector<bool> {
   bool tag_visited(const int poly_index)
   {
     bool visited = (*this)[poly_index];
-    this[poly_index] = true;
+    (*this)[poly_index] = true;
     return visited;
   }
 };
@@ -71,8 +56,8 @@ static float3 calc_barycentric_delta(const ImBuf *image_buffer,
 {
   const float2 start_uv(float(x) / image_buffer->x, float(y) / image_buffer->y);
   const float2 end_uv(float(x + 1) / image_buffer->x, float(y) / image_buffer->y);
-  const float3 start_barycentric = barycentric_weights(uvs[0], uvs[1], uvs[2], start_uv);
-  const float3 end_barycentric = barycentric_weights(uvs[0], uvs[1], uvs[2], end_uv);
+  const BarycentricWeights start_barycentric(uvs[0], uvs[1], uvs[2], start_uv);
+  const BarycentricWeights end_barycentric(uvs[0], uvs[1], uvs[2], end_uv);
   const float3 delta_barycentric = end_barycentric - start_barycentric;
   return delta_barycentric;
 }
@@ -88,7 +73,6 @@ static void extract_barycentric_pixels(TileData &tile_data,
 {
   for (int y = miny; y < maxy; y++) {
     bool start_detected = false;
-    float3 barycentric;
     PixelsPackage package;
     package.triangle_index = triangle_index;
     package.num_pixels = 0;
@@ -96,8 +80,8 @@ static void extract_barycentric_pixels(TileData &tile_data,
 
     for (x = minx; x < maxx; x++) {
       float2 uv(float(x) / image_buffer->x, float(y) / image_buffer->y);
-      barycentric = barycentric_weights(uvs[0], uvs[1], uvs[2], uv);
-      const bool is_inside = is_inside_triangle(barycentric);
+      const BarycentricWeights barycentric(uvs[0], uvs[1], uvs[2], uv);
+      const bool is_inside = barycentric.is_inside_triangle();
       if (!start_detected && is_inside) {
         start_detected = true;
         package.start_image_coordinate = ushort2(x, y);
diff --git a/source/blender/editors/sculpt_paint/sculpt_paint_image.cc b/source/blender/editors/sculpt_paint/sculpt_paint_image.cc
index e6cb63891e2..def65e56b9d 100644
--- a/source/blender/editors/sculpt_paint/sculpt_paint_image.cc
+++ b/source/blender/editors/sculpt_paint/sculpt_paint_image.cc
@@ -260,7 +260,7 @@ template<typename ImagePixelAccessor> class PaintingKernel {
     return result - start_pixel;
   }
 
-  Pixel init_pixel(const TrianglePaintInput &triangle, const float3 weights) const
+  Pixel init_pixel(const TrianglePaintInput &triangle, const BarycentricWeights &weights) const
   {
     const int3 &vert_indices = triangle.vert_indices;
     Pixel result;



More information about the Bf-blender-cvs mailing list