67 const bool check_axis[3],
68 bool extract_wire_edges)
74 if (extract_wire_edges ?
e->l !=
nullptr :
e->l ==
nullptr) {
78 int selected_face_count = 0;
83 selected_face_count++;
84 if (selected_face_count > 1) {
90 if (selected_face_count > 1) {
97 for (
int i = 0; i < 3; i++) {
112 const bool check_axis[3],
113 bool extract_wire_edges)
115 ListBaseT<BMEdgeLoopStore> eloops = {
nullptr};
117 .use_vert_junction =
true,
129 chain.
is_wire = extract_wire_edges;
136 if (extract_wire_edges) {
137 bool touches_face_region =
false;
143 touches_face_region =
true;
147 if (touches_face_region) {
151 if (touches_face_region) {
156 if (chain.
verts.size() >= 3) {
157 r_chains.
append(std::move(chain));
181 for (
BMVert *curr : chain) {
210 const std::optional<float2> &fixed_center,
216 if (fixed_center.has_value()) {
217 r_center = *fixed_center;
222 radius /= verts.
size();
229 float initial_radius = 1.0f;
230 bool converged =
false;
237 const float2 d_vec = initial_center - cv.co_2d;
243 const float residual = initial_radius -
distance;
245 for (
int row = 0; row < 3; row++) {
246 for (
int col = 0; col < 3; col++) {
247 normal_matrix[col][row] += j_row[row] * j_row[col];
249 jacobian_transpose_residual[row] += j_row[row] * residual;
258 float3 delta = inverse_normal_matrix * jacobian_transpose_residual;
260 initial_center.x += delta.x;
261 initial_center.y += delta.y;
262 initial_radius += delta.z;
277 initial_center =
float2(0.0f);
278 initial_radius = 0.0f;
282 initial_radius /= verts.size();
285 r_center = initial_center;
286 *r_radius = initial_radius;
290 const std::optional<float2> &fixed_center,
295 if (fixed_center.has_value()) {
296 center = *fixed_center;
299 float total_edge_length = 0.0f;
304 const float2 &curr_co = cv.co_2d;
306 center += (prev_co + curr_co) * edge_length;
307 total_edge_length += edge_length;
310 if (total_edge_length != 0.0f) {
311 center *= (0.5f / total_edge_length);
318 radius_sq = std::min(radius_sq, dist_sq);
328 const bool is_regular,
329 const bool is_closed,
330 const float rotation_angle)
333 float start_angle = 0.0f;
336 float total_angle = 2.0f * std::numbers::pi_v<float>;
337 int divisions = verts.
size();
343 if (!is_closed && divisions > 1) {
345 divisions = verts.
size() - 1;
347 float2 vec_prev = verts[0].co_2d - center;
352 float2 vec_curr = verts[i].co_2d - center;
359 const float max_angle = 2.0f * std::numbers::pi_v<float>;
360 total_angle = std::clamp(total_angle, -max_angle, max_angle);
363 step = total_angle / divisions;
364 float sum_sin = 0.0f;
365 float sum_cos = 0.0f;
376 float2 vec = verts[i].co_2d - center;
377 const float angle_diff =
atan2f(vec.y, vec.x) - (
step * i);
378 sum_sin +=
sinf(angle_diff);
379 sum_cos +=
cosf(angle_diff);
381 start_angle =
atan2f(sum_sin, sum_cos);
388 angle = start_angle +
step * i - rotation_angle;
391 float2 vec = verts[i].co_2d - center;
395 verts[i].target_2d.x = center.x +
cosf(
angle) * radius;
396 verts[i].target_2d.y = center.y +
sinf(
angle) * radius;
408 const std::array<BMLoop *, 3> <ri =
data->looptris[
index];
413 if (dist_sq < nearest->dist_sq) {
446 float3 p2 = center_pos + normal;
454 float3 hit_pos = center_pos + normal * lambda;
456 if (dist_sq < best_dist_sq) {
457 best_dist_sq = dist_sq;
475 test_tri_fn(v1,
v2, v3);
479 test_tri_fn(v1, v3, v4);
484 const int tottri = f->
len - 2;
488 f,
false, loops.
data(),
reinterpret_cast<uint(*)[3]
>(
index.data()));
490 for (
int i = 0; i < tottri; i++) {
491 tris[i] = {loops[
index[i][0]]->v, loops[
index[i][1]]->v, loops[
index[i][2]]->v};
495 for (
const std::array<BMVert *, 3> &tri : cached) {
496 test_tri_fn(tri[0], tri[1], tri[2]);
513 if (dist_sq < best_dist_sq) {
514 best_dist_sq = dist_sq;
530 if (nearest.
index != -1) {
548 const bool check_axis[3] = {
566 for (
const bool extract_wire_edges : {
false,
true}) {
567 if (!extract_wire_edges &&
bm->
totface == 0) {
585 if ((flatten < 1.0f) && (
bm->
totface > 0)) {
592 const std::array<BMLoop *, 3> <ri = looptris[i];
617 std::reverse(chain.
begin(), chain.
end());
625 bool is_mirrored =
false;
626 int mirror_axis = -1;
628 if (!chain_data.is_closed) {
632 for (
int i = 0; i < 3; i++) {
661 std::optional<float2> fixed_center = std::nullopt;
663 fixed_center =
float2(0.0f);
673 if (custom_radius > 0.0f) {
674 radius = custom_radius;
678 circle_verts, circle_center_2d, radius, regular, chain_data.is_closed,
angle);
681 const float3 target_local(cv.target_2d.x, cv.target_2d.y, 0.0f);
682 float3 final_pos = center_3d + mat * target_local;
686 if (flatten < 1.0f && !chain_data.is_wire) {
689 bvh_tree, &bvh_data, cv.v, final_pos, mat.
z_axis(), projected_pos, tess_cache);
699 if (cv.v == chain.
first() || cv.v == chain.
last()) {
700 final_pos[mirror_axis] = 0.0f;
705 if (lock_x || lock_y || lock_z) {
706 const float *orig = cv.v->co;
708 final_pos.x = orig[0];
711 final_pos.y = orig[1];
714 final_pos.z = orig[2];
#define BM_DEFAULT_NGON_STACK_SIZE
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
bool closest(btVector3 &v)
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
constexpr int64_t size() const
constexpr IndexRange index_range() const
constexpr int64_t size() const
constexpr const T & last(const int64_t n=0) const
void append(const T &value)
const T & last(const int64_t n=0) const
IndexRange index_range() const
void reserve(const int64_t min_capacity)
void reinitialize(const int64_t new_size)
constexpr IndexRange drop_front(int64_t n) const
constexpr int64_t size() const
float distance(float, float) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
MatT from_up_axis(const VectorT up)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
QuaternionBase< T > normalize_and_get_length(const QuaternionBase< T > &q, T &out_length)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
CartesianBasis invert(const CartesianBasis &basis)
T midpoint(const T &a, const T &b)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
void BM_face_calc_tessellation(const BMFace *f, const bool use_fixed_quad, BMLoop **r_loops, uint(*r_index)[3])
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ListBaseT< LinkData > * BM_edgeloop_verts_get(BMEdgeLoopStore *el_store)
int BM_mesh_edgeloops_find(BMesh *bm, ListBaseT< BMEdgeLoopStore > *r_eloops, FunctionRef< bool(BMEdge *)> test_fn, const BMEdgeLoopFind_Params *params)
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
static bool is_valid_boundary_edge(BMEdge *e, const char hflag, const bool check_axis[3], bool extract_wire_edges)
float line_point_factor_v3(const float p[3], const float l1[3], const float l2[3])
int BMO_slot_int_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
MINLINE void copy_v3_v3(float r[3], const float a[3])
int BLI_bvhtree_find_nearest(const BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
void BMO_slot_buffer_hflag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, char hflag, bool do_flush)
BMO_FLAG_BUFFER.
bool isect_line_tri_v3(const float p1[3], const float p2[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
void BM_mesh_edgeloops_free(ListBaseT< BMEdgeLoopStore > *eloops)
float BMO_slot_float_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void BLI_bvhtree_free(BVHTree *tree)
@ FIT_METHOD_LEAST_SQUARE
void BLI_bvhtree_balance(BVHTree *tree)
float angle_signed_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static void bm_vert_chain_extract_from_boundary_edges(BMesh *bm, Vector< VertChain > &r_chains, const char hflag, const bool check_axis[3], bool extract_wire_edges)
bool BM_edgeloop_is_closed(BMEdgeLoopStore *el_store)
Map< BMFace *, Array< std::array< BMVert *, 3 > > > FaceTessellationCache
void bmo_circularize_exec(BMesh *bm, BMOperator *op)
VecBase< float, 2 > float2
BMesh const char void * data
BVHTree * BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis)
static void project_on_mesh(BVHTree *bvh_tree, NearestTriUserData *bvh_data, BMVert *v, const float3 ¢er_pos, const float3 &normal, float3 &r_pos, FaceTessellationCache &tess_cache)
static bool bm_vert_chain_orientation_matrix_calc(Span< BMVert * > chain, float3 &r_center, float3x3 &r_mat)
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
void BM_mesh_calc_tessellation(BMesh *bm, MutableSpan< std::array< BMLoop *, 3 > > looptris)
static void calculate_circle_best_fit(Span< CircleVert > verts, const std::optional< float2 > &fixed_center, float2 &r_center, float *r_radius)
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
MatBase< float, 3, 3 > float3x3
constexpr float CIRCULARIZE_EPSILON
constexpr float MIRROR_LIMIT
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE void add_newell_cross_v3_v3v3(float n[3], const float v_prev[3], const float v_curr[3])
float closest_to_line_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
constexpr int NON_LINEAR_LEAST_SQUARES_MAX_ITERATIONS
void closest_on_tri_to_point_v3(float r[3], const float p[3], const float v1[3], const float v2[3], const float v3[3])
void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints)
static void nearest_tri_cb(void *userdata, int index, const float co[3], BVHTreeNearest *nearest)
static void project_chain_to_2d(Span< BMVert * > chain, const float3 ¢er, const float3x3 &mat, Vector< CircleVert > &r_2d_verts)
void BMO_error_raise(BMesh *bm, BMOperator *owner, eBMOpErrorLevel level, const char *msg) ATTR_NONNULL(1
VecBase< float, 3 > float3
static void calculate_circle_inside_fit(Span< CircleVert > verts, const std::optional< float2 > &fixed_center, float2 &r_center, float *r_radius)
static void calculate_target_locations(MutableSpan< CircleVert > verts, const float2 ¢er, const float radius, const bool is_regular, const bool is_closed, const float rotation_angle)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
Span< std::array< BMLoop *, 3 > > looptris