[Bf-blender-cvs] [417695e4a8b] master: Curve Profile: Miscellaneous cleanup
Hans Goudey
noreply at git.blender.org
Fri Sep 4 17:57:46 CEST 2020
Commit: 417695e4a8bff7927a936797ca01c6461ce0eaf7
Author: Hans Goudey
Date: Fri Sep 4 10:56:56 2020 -0500
Branches: master
https://developer.blender.org/rB417695e4a8bff7927a936797ca01c6461ce0eaf7
Curve Profile: Miscellaneous cleanup
- Declare variables where they are initialized
- Use consistent variable and static function names
- Use helper functions more for common operations
- Remove use of BezTriple struct, use CurveProfilePoint instead
- Apply small simplifications to code in some situations
===================================================================
M source/blender/blenkernel/BKE_curveprofile.h
M source/blender/blenkernel/intern/curveprofile.c
M source/blender/editors/interface/interface_handlers.c
===================================================================
diff --git a/source/blender/blenkernel/BKE_curveprofile.h b/source/blender/blenkernel/BKE_curveprofile.h
index 9c72a866fa9..08d3da7123c 100644
--- a/source/blender/blenkernel/BKE_curveprofile.h
+++ b/source/blender/blenkernel/BKE_curveprofile.h
@@ -23,6 +23,8 @@
* \ingroup bke
*/
+#include "DNA_curveprofile_types.h"
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -34,7 +36,7 @@ struct CurveProfilePoint;
void BKE_curveprofile_set_defaults(struct CurveProfile *profile);
-struct CurveProfile *BKE_curveprofile_add(int preset);
+struct CurveProfile *BKE_curveprofile_add(eCurveProfilePresets preset);
void BKE_curveprofile_free_data(struct CurveProfile *profile);
diff --git a/source/blender/blenkernel/intern/curveprofile.c b/source/blender/blenkernel/intern/curveprofile.c
index 068f8845e64..f52619f8c3a 100644
--- a/source/blender/blenkernel/intern/curveprofile.c
+++ b/source/blender/blenkernel/intern/curveprofile.c
@@ -143,6 +143,14 @@ bool BKE_curveprofile_move_point(struct CurveProfile *profile,
const bool snap,
const float delta[2])
{
+ /* Don't move the final point. */
+ if (point == &profile->path[profile->path_len - 1]) {
+ return false;
+ }
+ /* Don't move the first point. */
+ if (point == profile->path) {
+ return false;
+ }
float origx = point->x;
float origy = point->y;
@@ -183,8 +191,6 @@ bool BKE_curveprofile_move_point(struct CurveProfile *profile,
*/
bool BKE_curveprofile_remove_point(CurveProfile *profile, CurveProfilePoint *point)
{
- CurveProfilePoint *pts;
-
/* Must have 2 points minimum. */
if (profile->path_len <= 2) {
return false;
@@ -195,18 +201,20 @@ bool BKE_curveprofile_remove_point(CurveProfile *profile, CurveProfilePoint *poi
return false;
}
- pts = MEM_mallocN(sizeof(CurveProfilePoint) * profile->path_len, "path points");
+ CurveProfilePoint *new_path = MEM_mallocN(sizeof(CurveProfilePoint) * profile->path_len,
+ "profile path");
- uint i_delete = (uint)(point - profile->path);
+ int i_delete = (int)(point - profile->path);
+ BLI_assert(i_delete > 0);
/* Copy the before and after the deleted point. */
- memcpy(pts, profile->path, sizeof(CurveProfilePoint) * i_delete);
- memcpy(pts + i_delete,
+ memcpy(new_path, profile->path, sizeof(CurveProfilePoint) * i_delete);
+ memcpy(new_path + i_delete,
profile->path + i_delete + 1,
sizeof(CurveProfilePoint) * (profile->path_len - i_delete - 1));
MEM_freeN(profile->path);
- profile->path = pts;
+ profile->path = new_path;
profile->path_len -= 1;
return true;
}
@@ -220,30 +228,43 @@ bool BKE_curveprofile_remove_point(CurveProfile *profile, CurveProfilePoint *poi
*/
void BKE_curveprofile_remove_by_flag(CurveProfile *profile, const short flag)
{
- int i_old, i_new, n_removed = 0;
-
/* Copy every point without the flag into the new path. */
- CurveProfilePoint *new_pts = MEM_mallocN(sizeof(CurveProfilePoint) * profile->path_len,
- "profile path");
+ CurveProfilePoint *new_path = MEM_mallocN(sizeof(CurveProfilePoint) * profile->path_len,
+ "profile path");
/* Build the new list without any of the points with the flag. Keep the first and last points. */
- new_pts[0] = profile->path[0];
- for (i_old = 1, i_new = 1; i_old < profile->path_len - 1; i_old++) {
+ int i_new = 1;
+ int i_old = 1;
+ int n_removed = 0;
+ new_path[0] = profile->path[0];
+ for (; i_old < profile->path_len - 1; i_old++) {
if (!(profile->path[i_old].flag & flag)) {
- new_pts[i_new] = profile->path[i_old];
+ new_path[i_new] = profile->path[i_old];
i_new++;
}
else {
n_removed++;
}
}
- new_pts[i_new] = profile->path[i_old];
+ new_path[i_new] = profile->path[i_old];
MEM_freeN(profile->path);
- profile->path = new_pts;
+ profile->path = new_path;
profile->path_len -= n_removed;
}
+/**
+ * Shorthand helper function for setting location and interpolation of a point.
+ */
+static void point_init(CurveProfilePoint *point, float x, float y, short flag, char h1, char h2)
+{
+ point->x = x;
+ point->y = y;
+ point->flag = flag;
+ point->h1 = h1;
+ point->h2 = h2;
+}
+
/**
* Adds a new point at the specified location. The choice for which points to place the new vertex
* between is made by checking which control point line segment is closest to the new point and
@@ -253,7 +274,6 @@ void BKE_curveprofile_remove_by_flag(CurveProfile *profile, const short flag)
*/
CurveProfilePoint *BKE_curveprofile_insert(CurveProfile *profile, float x, float y)
{
- CurveProfilePoint *new_pt = NULL;
const float new_loc[2] = {x, y};
/* Don't add more control points than the maximum size of the higher resolution table. */
@@ -262,14 +282,13 @@ CurveProfilePoint *BKE_curveprofile_insert(CurveProfile *profile, float x, float
}
/* Find the index at the line segment that's closest to the new position. */
- float distance;
float min_distance = FLT_MAX;
int i_insert = 0;
for (int i = 0; i < profile->path_len - 1; i++) {
const float loc1[2] = {profile->path[i].x, profile->path[i].y};
const float loc2[2] = {profile->path[i + 1].x, profile->path[i + 1].y};
- distance = dist_squared_to_line_segment_v2(new_loc, loc1, loc2);
+ float distance = dist_squared_to_line_segment_v2(new_loc, loc1, loc2);
if (distance < min_distance) {
min_distance = distance;
i_insert = i + 1;
@@ -278,28 +297,25 @@ CurveProfilePoint *BKE_curveprofile_insert(CurveProfile *profile, float x, float
/* Insert the new point at the location we found and copy all of the old points in as well. */
profile->path_len++;
- CurveProfilePoint *new_pts = MEM_mallocN(sizeof(CurveProfilePoint) * profile->path_len,
- "profile path");
+ CurveProfilePoint *new_path = MEM_mallocN(sizeof(CurveProfilePoint) * profile->path_len,
+ "profile path");
+ CurveProfilePoint *new_pt = NULL;
for (int i_new = 0, i_old = 0; i_new < profile->path_len; i_new++) {
if (i_new != i_insert) {
/* Insert old points. */
- memcpy(&new_pts[i_new], &profile->path[i_old], sizeof(CurveProfilePoint));
- new_pts[i_new].flag &= ~PROF_SELECT; /* Deselect old points. */
+ new_path[i_new] = profile->path[i_old];
+ new_path[i_new].flag &= ~PROF_SELECT; /* Deselect old points. */
i_old++;
}
else {
/* Insert new point. */
- new_pts[i_new].x = x;
- new_pts[i_new].y = y;
- new_pts[i_new].flag = PROF_SELECT;
- new_pt = &new_pts[i_new];
/* Set handles of new point based on its neighbors. */
- if (new_pts[i_new - 1].h2 == HD_VECT && profile->path[i_insert].h1 == HD_VECT) {
- new_pt->h1 = new_pt->h2 = HD_VECT;
- }
- else {
- new_pt->h1 = new_pt->h2 = HD_AUTO;
- }
+ char new_handle_type = (new_path[i_new - 1].h2 == HD_VECT &&
+ profile->path[i_insert].h1 == HD_VECT) ?
+ HD_VECT :
+ HD_AUTO;
+ point_init(&new_path[i_new], x, y, PROF_SELECT, new_handle_type, new_handle_type);
+ new_pt = &new_path[i_new];
/* Give new point a reference to the profile. */
new_pt->profile = profile;
}
@@ -307,7 +323,7 @@ CurveProfilePoint *BKE_curveprofile_insert(CurveProfile *profile, float x, float
/* Free the old path and use the new one. */
MEM_freeN(profile->path);
- profile->path = new_pts;
+ profile->path = new_path;
return new_pt;
}
@@ -331,6 +347,13 @@ void BKE_curveprofile_selected_handle_set(CurveProfile *profile, int type_1, int
}
}
+static CurveProfilePoint mirror_point(const CurveProfilePoint *point)
+{
+ CurveProfilePoint new_point = *point;
+ point_init(&new_point, point->y, point->x, point->flag, point->h2, point->h1);
+ return new_point;
+}
+
/**
* Flips the profile across the diagonal so that its orientation is reversed.
*
@@ -342,123 +365,76 @@ void BKE_curveprofile_reverse(CurveProfile *profile)
if (profile->path_len == 2) {
return;
}
- CurveProfilePoint *new_pts = MEM_mallocN(sizeof(CurveProfilePoint) * profile->path_len,
- "profile path");
+ CurveProfilePoint *new_path = MEM_mallocN(sizeof(CurveProfilePoint) * profile->path_len,
+ "profile path");
/* Mirror the new points across the y = x line */
for (int i = 0; i < profile->path_len; i++) {
int i_reversed = profile->path_len - i - 1;
BLI_assert(i_reversed >= 0);
- new_pts[i_reversed].x = profile->path[i].y;
- new_pts[i_reversed].y = profile->path[i].x;
- new_pts[i_reversed].flag = profile->path[i].flag;
- new_pts[i_reversed].h1 = profile->path[i].h2;
- new_pts[i_reversed].h2 = profile->path[i].h1;
- new_pts[i_reversed].profile = profile;
+ new_path[i_reversed] = mirror_point(&profile->path[i]);
+ new_path[i_reversed].profile = profile;
/* Mirror free handles, they can't be recalculated. */
if (ELEM(profile->path[i].h1, HD_FREE, HD_ALIGN)) {
- new_pts[i_reversed].h1_loc[0] = profile->path[i].h2_loc[1];
- new_pts[i_reversed].h1_loc[1] = profile->path[i].h2_loc[0];
+ new_path[i_reversed].h1_loc[0] = profile->path[i].h2_loc[1];
+ new_path[i_reversed].h1_loc[1] = profile->path[i].h2_loc[0];
}
if (ELEM(profile->path[i].h2, HD_FREE, HD_ALIGN)) {
- new_pts[i_reversed].h2_loc[0] = profile->path[i].h1_loc[1];
- new_pts[i_reversed].h2_loc[1] = profile->path[i].h1_loc[0];
+ new_path[i_reversed].h2_loc[0] = profile->path[i].h1_loc[1];
+ new_path[i_reversed].h2_loc[1] = profile->path[i].h1_loc[0];
}
}
/* Free the old points and use the new ones */
MEM_freeN(profile->path);
- profile->path = new_pts;
+ profile->path = new_path;
}
/**
* Builds a quarter circle profile with space on each side for 'support loops.'
*/
-static void CurveProfile_build_supports(CurveProfile
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list