[Bf-blender-cvs] [adaf92b4abe] master: Cleanup: knife tool

Campbell Barton noreply at git.blender.org
Wed May 25 09:50:06 CEST 2022


Commit: adaf92b4abe54dfc5c920bfa721c1ffc30413676
Author: Campbell Barton
Date:   Wed May 25 17:47:27 2022 +1000
Branches: master
https://developer.blender.org/rBadaf92b4abe54dfc5c920bfa721c1ffc30413676

Cleanup: knife tool

- Use early return and continue to reduce right-shift.
- Rename `lv` to `tri_cos` for storing triangle coordinates.
- Reduce variable scope.

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

M	source/blender/editors/mesh/editmesh_knife.c

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

diff --git a/source/blender/editors/mesh/editmesh_knife.c b/source/blender/editors/mesh/editmesh_knife.c
index 39d104cdcce..7972459e457 100644
--- a/source/blender/editors/mesh/editmesh_knife.c
+++ b/source/blender/editors/mesh/editmesh_knife.c
@@ -377,45 +377,45 @@ static void knifetool_raycast_planes(const KnifeTool_OpData *kcd, float r_v1[3],
       kcd->vc.rv3d->persmat, planes[2], planes[0], planes[1], planes[3], NULL, NULL);
 
   /* Ray-cast all planes. */
+  float ray_dir[3];
+  float ray_hit_best[2][3] = {{UNPACK3(kcd->prev.cage)}, {UNPACK3(kcd->curr.cage)}};
+  float lambda_best[2] = {-FLT_MAX, FLT_MAX};
+  int i;
+
   {
-    float ray_dir[3];
-    float ray_hit_best[2][3] = {{UNPACK3(kcd->prev.cage)}, {UNPACK3(kcd->curr.cage)}};
-    float lambda_best[2] = {-FLT_MAX, FLT_MAX};
-    int i;
+    float curr_cage_adjust[3];
+    float co_depth[3];
 
-    {
-      float curr_cage_adjust[3];
-      float co_depth[3];
+    copy_v3_v3(co_depth, kcd->prev.cage);
+    ED_view3d_win_to_3d(kcd->vc.v3d, kcd->region, co_depth, kcd->curr.mval, curr_cage_adjust);
 
-      copy_v3_v3(co_depth, kcd->prev.cage);
-      ED_view3d_win_to_3d(kcd->vc.v3d, kcd->region, co_depth, kcd->curr.mval, curr_cage_adjust);
+    sub_v3_v3v3(ray_dir, curr_cage_adjust, kcd->prev.cage);
+  }
 
-      sub_v3_v3v3(ray_dir, curr_cage_adjust, kcd->prev.cage);
+  for (i = 0; i < 4; i++) {
+    float ray_hit[3];
+    float lambda_test;
+    if (!isect_ray_plane_v3(kcd->prev.cage, ray_dir, planes[i], &lambda_test, false)) {
+      continue;
     }
 
-    for (i = 0; i < 4; i++) {
-      float ray_hit[3];
-      float lambda_test;
-      if (isect_ray_plane_v3(kcd->prev.cage, ray_dir, planes[i], &lambda_test, false)) {
-        madd_v3_v3v3fl(ray_hit, kcd->prev.cage, ray_dir, lambda_test);
-        if (lambda_test < 0.0f) {
-          if (lambda_test > lambda_best[0]) {
-            copy_v3_v3(ray_hit_best[0], ray_hit);
-            lambda_best[0] = lambda_test;
-          }
-        }
-        else {
-          if (lambda_test < lambda_best[1]) {
-            copy_v3_v3(ray_hit_best[1], ray_hit);
-            lambda_best[1] = lambda_test;
-          }
-        }
+    madd_v3_v3v3fl(ray_hit, kcd->prev.cage, ray_dir, lambda_test);
+    if (lambda_test < 0.0f) {
+      if (lambda_test > lambda_best[0]) {
+        copy_v3_v3(ray_hit_best[0], ray_hit);
+        lambda_best[0] = lambda_test;
+      }
+    }
+    else {
+      if (lambda_test < lambda_best[1]) {
+        copy_v3_v3(ray_hit_best[1], ray_hit);
+        lambda_best[1] = lambda_test;
       }
     }
-
-    copy_v3_v3(r_v1, ray_hit_best[0]);
-    copy_v3_v3(r_v2, ray_hit_best[1]);
   }
+
+  copy_v3_v3(r_v1, ray_hit_best[0]);
+  copy_v3_v3(r_v2, ray_hit_best[1]);
 }
 
 static void knifetool_draw_angle_snapping(const KnifeTool_OpData *kcd)
@@ -440,43 +440,45 @@ static void knifetool_draw_angle_snapping(const KnifeTool_OpData *kcd)
 
 static void knifetool_draw_orientation_locking(const KnifeTool_OpData *kcd)
 {
-  if (!compare_v3v3(kcd->prev.cage, kcd->curr.cage, KNIFE_FLT_EPSBIG)) {
-    float v1[3], v2[3];
+  if (compare_v3v3(kcd->prev.cage, kcd->curr.cage, KNIFE_FLT_EPSBIG)) {
+    return;
+  }
 
-    /* This is causing buggy behavior when `prev.cage` and `curr.cage` are too close together. */
-    knifetool_raycast_planes(kcd, v1, v2);
+  float v1[3], v2[3];
 
-    uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
-    immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
+  /* This is causing buggy behavior when `prev.cage` and `curr.cage` are too close together. */
+  knifetool_raycast_planes(kcd, v1, v2);
 
-    switch (kcd->constrain_axis) {
-      case KNF_CONSTRAIN_AXIS_X: {
-        immUniformColor3ubv(kcd->colors.xaxis);
-        break;
-      }
-      case KNF_CONSTRAIN_AXIS_Y: {
-        immUniformColor3ubv(kcd->colors.yaxis);
-        break;
-      }
-      case KNF_CONSTRAIN_AXIS_Z: {
-        immUniformColor3ubv(kcd->colors.zaxis);
-        break;
-      }
-      default: {
-        immUniformColor3ubv(kcd->colors.axis_extra);
-        break;
-      }
+  uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
+  immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
+
+  switch (kcd->constrain_axis) {
+    case KNF_CONSTRAIN_AXIS_X: {
+      immUniformColor3ubv(kcd->colors.xaxis);
+      break;
+    }
+    case KNF_CONSTRAIN_AXIS_Y: {
+      immUniformColor3ubv(kcd->colors.yaxis);
+      break;
+    }
+    case KNF_CONSTRAIN_AXIS_Z: {
+      immUniformColor3ubv(kcd->colors.zaxis);
+      break;
     }
+    default: {
+      immUniformColor3ubv(kcd->colors.axis_extra);
+      break;
+    }
+  }
 
-    GPU_line_width(2.0);
+  GPU_line_width(2.0);
 
-    immBegin(GPU_PRIM_LINES, 2);
-    immVertex3fv(pos, v1);
-    immVertex3fv(pos, v2);
-    immEnd();
+  immBegin(GPU_PRIM_LINES, 2);
+  immVertex3fv(pos, v1);
+  immVertex3fv(pos, v2);
+  immEnd();
 
-    immUnbindProgram();
-  }
+  immUnbindProgram();
 }
 
 static void knifetool_draw_visible_distances(const KnifeTool_OpData *kcd)
@@ -1226,7 +1228,6 @@ static void knife_bvh_init(KnifeTool_OpData *kcd)
   }
 
   /* Construct BVH Tree. */
-  float cos[3][3];
   const float epsilon = FLT_EPSILON * 2.0f;
   int tottri = 0;
   int ob_tottri = 0;
@@ -1283,8 +1284,10 @@ static void knife_bvh_init(KnifeTool_OpData *kcd)
       if (!test_fn_ret) {
         continue;
       }
-      knife_bm_tri_cagecos_get_worldspace(kcd, b, i, cos);
-      BLI_bvhtree_insert(kcd->bvh.tree, i + tottri, (float *)cos, 3);
+
+      float tri_cos[3][3];
+      knife_bm_tri_cagecos_get_worldspace(kcd, b, i, tri_cos);
+      BLI_bvhtree_insert(kcd->bvh.tree, i + tottri, &tri_cos[0][0], 3);
     }
 
     tottri += em->tottri;
@@ -1307,6 +1310,10 @@ static void knife_bvh_raycast_cb(void *userdata,
                                  const BVHTreeRay *ray,
                                  BVHTreeRayHit *hit)
 {
+  if (index != -1) {
+    return;
+  }
+
   KnifeTool_OpData *kcd = userdata;
   BMLoop **ltri;
   Object *ob;
@@ -1315,60 +1322,49 @@ static void knife_bvh_raycast_cb(void *userdata,
   float dist, uv[2];
   bool isect;
   int tottri;
-  float tri_cos[3][3];
 
-  if (index != -1) {
-    tottri = 0;
-    uint b = 0;
-    for (; b < kcd->objects_len; b++) {
-      index -= tottri;
-      ob = kcd->objects[b];
-      em = BKE_editmesh_from_object(ob);
-      tottri = em->tottri;
-      if (index < tottri) {
-        ltri = em->looptris[index];
-        break;
-      }
+  tottri = 0;
+  uint b = 0;
+  for (; b < kcd->objects_len; b++) {
+    index -= tottri;
+    ob = kcd->objects[b];
+    em = BKE_editmesh_from_object(ob);
+    tottri = em->tottri;
+    if (index < tottri) {
+      ltri = em->looptris[index];
+      break;
     }
+  }
 
-    if (kcd->bvh.filter_cb) {
-      if (!kcd->bvh.filter_cb(ltri[0]->f, kcd->bvh.filter_data)) {
-        return;
-      }
+  if (kcd->bvh.filter_cb) {
+    if (!kcd->bvh.filter_cb(ltri[0]->f, kcd->bvh.filter_data)) {
+      return;
     }
+  }
 
-    knife_bm_tri_cagecos_get_worldspace(kcd, b, index, tri_cos);
-
-    isect =
-        (ray->radius > 0.0f ?
-             isect_ray_tri_epsilon_v3(ray->origin,
-                                      ray->direction,
-                                      tri_cos[0],
-                                      tri_cos[1],
-                                      tri_cos[2],
-                                      &dist,
-                                      uv,
-                                      ray->radius) :
+  float tri_cos[3][3];
+  knife_bm_tri_cagecos_get_worldspace(kcd, b, index, tri_cos);
+  isect = (ray->radius > 0.0f ?
+               isect_ray_tri_epsilon_v3(
+                   ray->origin, ray->direction, UNPACK3(tri_cos), &dist, uv, ray->radius) :
 #ifdef USE_KDOPBVH_WATERTIGHT
-             isect_ray_tri_watertight_v3(
-                 ray->origin, ray->isect_precalc, tri_cos[0], tri_cos[1], tri_cos[2], &dist, uv));
+               isect_ray_tri_watertight_v3(
+                   ray->origin, ray->isect_precalc, UNPACK3(tri_cos), &dist, uv));
 #else
-             isect_ray_tri_v3(
-                 ray->origin, ray->direction, tri_cos[0], tri_cos[1], tri_cos[2], &dist, uv));
+                 isect_ray_tri_v3(ray->origin, ray->direction, UNPACK3(tri_cos), &dist, uv);
 #endif
 
-    if (isect && dist < hit->dist) {
-      hit->dist = dist;
-      hit->index = index;
+  if (isect && dist < hit->dist) {
+    hit->dist = dist;
+    hit->index = index;
 
-      copy_v3_v3(hit->no, ltri[0]->f->no);
+    copy_v3_v3(hit->no, ltri[0]->f->no);
 
-      madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
+    madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
 
-      kcd->bvh.looptris = em->looptris;
-      copy_v2_v2(kcd->bvh.uv, uv);
-      kcd->bvh.base_index = b;
-    }
+    kcd->bvh.looptris = em->looptris;
+    copy_v2_v2(kcd->bvh.uv, uv);
+    kcd->bvh.base_index = b;
   }
 }
 
@@ -1978,29 +1974,30 @@ static void prepare_linehits_for_cut(KnifeTool_OpData *kcd)
    * Also remove all but one of a series of vertex hits for the same vertex. */
   for (int i = 0; i < n; i++) {
     KnifeLineHit *lhi = &linehits[i];
-    if (lhi->v) {
-      for (int j = i - 1; j >= 0; j--) {
-        KnifeLineHit *lhj = &linehits[j];
-        if (!lhj->kfe || fabsf(lhi->l - lhj->l) > KNIFE_FLT_EPSBIG ||
-            fabsf(lhi->m - lhj->m) > KNIFE_FLT_EPSBIG) {
-          break;
-        }
+    if (lhi->v == NULL) {
+      continue;
+    }
 
-        if (lhi->kfe == lhj->kfe) {
-          lhj->l = -1.0f;
-          is_double = true;
-        }
+    for (int j = i - 1; j >= 0; j--) {
+      KnifeLineHit *lhj = &linehits[j];
+      if (!lhj->kfe || fabsf(lhi->l - lhj->l) > KNIFE_FLT_EPSBIG ||
+          fabsf(lhi->m - lhj->m) > KNIFE_FLT_EPSBIG) {
+        break;
       }
-      for (int j = i + 1; j < n; j++) {
-        KnifeLineHit *lhj = &linehits[j];
-        if (fabsf(lhi->l - lhj->l) > KNIFE_FLT_EPSBIG ||
-            fabsf(lhi->m - lhj->m) > KNIFE_FLT_EPSBIG) {
-          break;
-        }
-        if ((lhj->kfe && (lhi->kfe == lhj->kfe)) || (lhi->v == lhj->v)) {
-          lhj->l = -1.0f;
-          is_double = true;
-      

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list