29#define PROF_TABLE_MAX 512
49 MEM_SAFE_DELETE(profile->
path);
50 MEM_SAFE_DELETE(profile->
table);
66 target->
path = MEM_dupalloc(profile->
path);
71 for (
int i = 0; i < target->
path_len; i++) {
99 profile->
table =
nullptr;
103 for (
int i = 0; i < profile->
path_len; i++) {
119 const float delta[2])
121 short handle_type = (handle_1) ? point->
h1 : point->
h2;
122 float *handle_location = (handle_1) ? &point->
h1_loc[0] : &point->
h2_loc[0];
124 float start_position[2];
133 handle_location[0] += delta ? delta[0] : 0.0f;
134 handle_location[1] += delta ? delta[1] : 0.0f;
136 handle_location[0] = 0.125f *
roundf(8.0f * handle_location[0]);
137 handle_location[1] = 0.125f *
roundf(8.0f * handle_location[1]);
142 short other_handle_type = (handle_1) ? point->
h2 : point->
h1;
143 if (other_handle_type ==
HD_ALIGN) {
144 float *other_handle_location = (handle_1) ? &point->
h2_loc[0] : &point->
h1_loc[0];
145 other_handle_location[0] = 2.0f * point->
x - handle_location[0];
146 other_handle_location[1] = 2.0f * point->
y - handle_location[1];
150 if (!
equals_v2v2(handle_location, start_position)) {
159 const float delta[2])
166 if (point == profile->
path) {
169 float origx = point->
x;
170 float origy = point->
y;
172 point->
x += delta[0];
173 point->
y += delta[1];
175 point->
x = 0.125f *
roundf(8.0f * point->
x);
176 point->
y = 0.125f *
roundf(8.0f * point->
y);
189 point->
h1_loc[0] += point->
x - origx;
190 point->
h1_loc[1] += point->
y - origy;
193 point->
h2_loc[0] += point->
x - origx;
194 point->
h2_loc[1] += point->
y - origy;
197 if (point->
x != origx || point->
y != origy) {
205 for (
int i = 0; i < profile->
path_len; i++) {
207 float delta[2] = {offset.x, offset.y};
235 if (!(point > profile->
path && point < profile->path + profile->
path_len)) {
242 int i_delete = int(point - profile->
path);
247 memcpy(new_path + i_delete,
248 profile->
path + i_delete + 1,
251 MEM_delete(profile->
path);
252 profile->
path = new_path;
267 new_path[0] = profile->
path[0];
268 for (; i_old < profile->
path_len - 1; i_old++) {
269 if (!(profile->
path[i_old].
flag & flag)) {
270 new_path[i_new] = profile->
path[i_old];
277 new_path[i_new] = profile->
path[i_old];
279 MEM_delete(profile->
path);
280 profile->
path = new_path;
299 const float new_loc[2] = {
x,
y};
309 for (
int i = 0; i < profile->
path_len - 1; i++) {
310 const float loc1[2] = {profile->
path[i].
x, profile->
path[i].
y};
311 const float loc2[2] = {profile->
path[i + 1].
x, profile->
path[i + 1].
y};
325 for (
int i_new = 0, i_old = 0; i_new < profile->
path_len; i_new++) {
326 if (i_new != i_insert) {
328 new_path[i_new] = profile->
path[i_old];
336 const char new_handle_type = (new_path[i_new - 1].
h2 ==
HD_VECT &&
341 new_pt = &new_path[i_new];
348 MEM_delete(profile->
path);
349 profile->
path = new_path;
355 for (
int i = 0; i < profile->
path_len; i++) {
394 for (
int i = 0; i < profile->
path_len; i++) {
395 int i_reversed = profile->
path_len - i - 1;
398 new_path[i_reversed].
profile = profile;
412 MEM_delete(profile->
path);
413 profile->
path = new_path;
425 for (
int i = 1; i < n - 2; i++) {
426 const float x = 1.0f - (0.5f * (1.0f -
cosf(
float(i /
float(n - 3)) *
M_PI_2)));
427 const float y = 0.5f + 0.5f *
sinf(
float((i /
float(n - 3)) *
M_PI_2));
449 float n_steps_x = (n % 2 == 0) ? n : (n - 1);
450 float n_steps_y = (n % 2 == 0) ? (n - 2) : (n - 1);
452 for (
int i = 0; i < n; i++) {
453 int step_x = (i + 1) / 2;
455 const float x = 1.0f - (
float(2 * step_x) / n_steps_x);
456 const float y =
float(2 * step_y) / n_steps_y;
468 MEM_SAFE_DELETE(profile->
path);
503 profile->
path = MEM_new_array<CurveProfilePoint>(profile->
path_len, __func__);
549 for (
int i = 0; i < profile->
path_len; i++) {
553 MEM_SAFE_DELETE(profile->
table);
554 profile->
table =
nullptr;
560 for (
int i = 1;; i++) {
562 int idx = (i & 1) ? (i_last - k) : (i_last + k);
564 if (idx < 0 || idx >= profile->
path_len) {
565 if (i_last - k < 0 && i_last + k >= profile->
path_len) {
595 const int resolution = 16;
622 float *point_loc = &point->
x;
625 const float *prev_loc, *next_loc;
626 if (prev ==
nullptr) {
628 pt[0] = 2.0f * point_loc[0] - next_loc[0];
629 pt[1] = 2.0f * point_loc[1] - next_loc[1];
636 if (next ==
nullptr) {
638 pt[0] = 2.0f * point_loc[0] - prev_loc[0];
639 pt[1] = 2.0f * point_loc[1] - prev_loc[1];
646 float dvec_a[2], dvec_b[2];
650 float len_a =
len_v2(dvec_a);
651 float len_b =
len_v2(dvec_b);
661 tvec[0] = dvec_b[0] / len_b + dvec_a[0] / len_a;
662 tvec[1] = dvec_b[1] / len_b + dvec_a[1] / len_a;
688 for (
int i = 1; i < path_len - 1; i++) {
703 float start_handle_direction[2], end_handle_direction[2];
705 sub_v2_v2v2(start_handle_direction, path[i_edge].h2_loc, &path[i_edge].
x);
707 sub_v2_v2v2(end_handle_direction, &path[i_edge + 1].
x, path[i_edge + 1].h1_loc);
709 return angle_v2v2(start_handle_direction, end_handle_direction);
750 bool sample_straight_edges,
757 int totedges = totpoints - 1;
763 for (
int i = 0; i < totedges; i++) {
774 if (n_segments >= totedges) {
775 if (sample_straight_edges) {
778 int n_common = n_segments / totedges;
779 n_left = n_segments % totedges;
784 for (
int i = 0; i < totedges; i++) {
785 n_samples[i] = n_common;
792 int n_curved_edges = 0;
793 for (
int i = 0; i < totedges; i++) {
799 n_curved_edges = (n_curved_edges == 0) ? totedges : n_curved_edges;
802 n_left = n_segments - (totedges - n_curved_edges);
803 int n_common = n_left / n_curved_edges;
805 for (
int i = 0; i < totedges; i++) {
809 n_samples[i] += n_common;
818 n_left -= n_common * n_curved_edges;
827 for (
int i = 0; i < n_left; i++) {
837 for (
int i_sample = 0, i = 0; i < totedges; i++) {
838 if (n_samples[i] > 0) {
840 r_samples[i_sample].
h1 = path[i].
h1;
841 r_samples[i_sample].
h2 = path[i].
h2;
843 for (
int j = i_sample + 1; j < i_sample + n_samples[i]; j++) {
853 path[i + 1].h1_loc[0],
855 &r_samples[i_sample].
x,
860 path[i + 1].h1_loc[1],
862 &r_samples[i_sample].
y,
866 i_sample += n_samples[i];
870 MEM_delete(curve_sorted);
871 MEM_delete(n_samples);
882 profile->
path = MEM_new_array<CurveProfilePoint>(2, __func__);
884 profile->
path[0].
x = 1.0f;
885 profile->
path[0].
y = 0.0f;
887 profile->
path[1].
x = 1.0f;
888 profile->
path[1].
y = 1.0f;
925 float total_length = 0;
945 const float segment_length = total_length / n_segments;
947 float distance_to_previous_table_point = 0.0f;
951 r_samples[0].
x = profile->
table[0].
x;
952 r_samples[0].
y = profile->
table[0].
y;
955 float segment_left = segment_length;
956 for (
int i = 1; i < n_segments; i++) {
958 while (distance_to_next_table_point < segment_left) {
959 segment_left -= distance_to_next_table_point;
962 distance_to_previous_table_point = 0.0f;
965 float factor = (distance_to_previous_table_point + segment_left) /
966 (distance_to_previous_table_point + distance_to_next_table_point);
970#ifdef DEBUG_CURVEPROFILE_EVALUATE
971 printf(
"segment_left: %.3f\n", segment_left);
972 printf(
"i_table: %d\n", i_table);
973 printf(
"distance_to_previous_table_point: %.3f\n", distance_to_previous_table_point);
974 printf(
"distance_to_next_table_point: %.3f\n", distance_to_next_table_point);
975 printf(
"Interpolating with factor %.3f from (%.3f, %.3f) to (%.3f, %.3f)\n\n",
977 profile->
table[i_table].
x,
978 profile->
table[i_table].
y,
979 profile->
table[i_table + 1].
x,
980 profile->
table[i_table + 1].
y);
984 distance_to_next_table_point -= segment_left;
985 distance_to_previous_table_point += segment_left;
986 segment_left = segment_length;
997 CurveProfilePoint *new_table = MEM_new_array<CurveProfilePoint>((n_samples + 1), __func__);
1004 new_table[n_samples - 1].
x = 0.0f;
1005 new_table[n_samples - 1].
y = 1.0f;
1007 MEM_SAFE_DELETE(profile->
table);
1008 profile->
table = new_table;
1018 if (n_samples <= 0) {
1021 CurveProfilePoint *new_table = MEM_new_array<CurveProfilePoint>((n_samples + 1), __func__);
1031 MEM_SAFE_DELETE(profile->
segments);
1046 for (
int i = 0; i < profile->
path_len; i++) {
1068 for (
int i = 0; i < profile->
path_len - 1; i++) {
1095 for (
int i = 0; i < profile->
path_len; i++) {
1113 return &point->
h1_loc[0];
1116 return &point->
h2_loc[0];
1123 return &point->
h1_loc[0];
1126 return &point->
h2_loc[0];
1135 float length_portion,
1140 const float requested_length = length_portion * total_length;
1144 float length_traveled = 0.0f;
1145 while (length_traveled < requested_length) {
1151 if (length_traveled + new_length >= requested_length) {
1154 length_traveled += new_length;
1161 float lerp_factor = (requested_length - length_traveled) / distance_to_next_point;
1163#ifdef DEBUG_CURVEPROFILE_EVALUATE
1164 printf(
"CURVEPROFILE EVALUATE\n");
1165 printf(
" length portion input: %f\n",
double(length_portion));
1166 printf(
" requested path length: %f\n",
double(requested_length));
1167 printf(
" distance to next point: %f\n",
double(distance_to_next_point));
1168 printf(
" length traveled: %f\n",
double(length_traveled));
1169 printf(
" lerp-factor: %f\n",
double(lerp_factor));
1170 printf(
" ith point (%f, %f)\n",
double(profile->
path[i].
x),
double(profile->
path[i].
y));
1171 printf(
" next point(%f, %f)\n",
double(profile->
path[i + 1].
x),
double(profile->
path[i + 1].
y));
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
float distance(float, float) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
VecBase< float, 2 > float2
MINLINE float min_ff(float a, float b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_curveprofile_init(struct CurveProfile *profile, short segments_len)
static void curveprofile_make_table(CurveProfile *profile)
void BKE_curveprofile_translate_selection(struct CurveProfile *profile, const blender::float2 &offset)
MINLINE void copy_v2_v2(float r[2], const float a[2])
static void point_init(CurveProfilePoint *point, float x, float y, eCurveProfilePoint_Flag flag, char h1, char h2)
static float curveprofile_distance_to_next_table_point(const CurveProfile *profile, int i)
void BKE_curveprofile_evaluate_length_portion(const struct CurveProfile *profile, float length_portion, float *x_out, float *y_out)
static void create_samples(CurveProfile *profile, int n_segments, bool sample_straight_edges, CurveProfilePoint *r_samples)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
static bool is_curved_edge(CurveProfilePoint *path, int i)
static float curveprofile_total_length(const CurveProfile *profile)
void BKE_curveprofile_remove_by_flag(struct CurveProfile *profile, short flag)
void BKE_curveprofile_copy_data(struct CurveProfile *target, const struct CurveProfile *profile)
@ PROF_SAMPLE_STRAIGHT_EDGES
@ PROF_SAMPLE_EVEN_LENGTHS
MINLINE float pow2f(float x)
float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
static CurveProfilePoint mirror_point(const CurveProfilePoint *point)
void BKE_curveprofile_reset(struct CurveProfile *profile)
static void curveprofile_build_steps(CurveProfile *profile)
bool BKE_curveprofile_move_handle(struct CurveProfilePoint *point, bool handle_1, bool snap, const float delta[2])
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
static void create_samples_even_spacing(CurveProfile *profile, int n_segments, CurveProfilePoint *r_samples)
void BKE_curveprofile_reset_view(struct CurveProfile *profile)
@ PROF_UPDATE_REMOVE_DOUBLES
static void calculate_path_handles(CurveProfilePoint *path, int path_len)
void BKE_curveprofile_blend_write(struct BlendWriter *writer, const struct CurveProfile *profile)
MINLINE bool equals_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
struct CurveProfilePoint * BKE_curveprofile_insert(struct CurveProfile *profile, float x, float y)
int BKE_curveprofile_table_size(const struct CurveProfile *profile)
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
void BKE_curveprofile_free(struct CurveProfile *profile)
static void curveprofile_make_segments_table(CurveProfile *profile)
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
MINLINE float max_ff(float a, float b)
static float bezt_edge_handle_angle(const CurveProfilePoint *path, int i_edge)
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
float * BKE_curveprofile_active_location_get(struct CurveProfilePoint *point)
struct CurveProfile * BKE_curveprofile_add(eCurveProfilePresets preset)
float angle_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float interpf(float target, float origin, float t)
void BKE_curveprofile_selected_handle_set(struct CurveProfile *profile, int type_1, int type_2)
bool BKE_curveprofile_move_point(struct CurveProfile *profile, struct CurveProfilePoint *point, bool snap, const float delta[2])
static void curveprofile_build_supports(CurveProfile *profile)
struct CurveProfile * BKE_curveprofile_copy(const struct CurveProfile *profile)
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
static void point_calculate_handle(CurveProfilePoint *point, const CurveProfilePoint *prev, const CurveProfilePoint *next)
void BKE_curveprofile_blend_read(struct BlendDataReader *reader, struct CurveProfile *profile)
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
void BKE_curveprofile_update(struct CurveProfile *profile, int update_flags)
CurveProfilePoint * BKE_curveprofile_active_get(CurveProfile *profile)
void BKE_curveprofile_free_data(struct CurveProfile *profile)
void BKE_curveprofile_activate_nearest_point(struct CurveProfile *profile, const int i_last)
bool BKE_curveprofile_remove_point(struct CurveProfile *profile, struct CurveProfilePoint *point)
void BKE_curveprofile_reverse(struct CurveProfile *profile)
static int sort_points_curvature(const void *in_a, const void *in_b)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BLI_rctf_init(struct rctf *rect, float xmin, float xmax, float ymin, float ymax)
MINLINE void madd_v2_v2v2fl(float r[2], const float a[2], const float b[2], float f)
MINLINE float clamp_f(float value, float min, float max)
void BKE_curveprofile_set_defaults(struct CurveProfile *profile)
vector snap(vector a, vector b)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
eCurveProfilePoint_Flag flag
struct CurveProfile * profile
CurveProfilePoint * table
CurveProfilePoint * segments
eCurveProfilePresets preset