Files
test/source/blender/bmesh/operators/bmo_edgenet.cc
Campbell Barton e955c94ed3 License Headers: Set copyright to "Blender Authors", add AUTHORS
Listing the "Blender Foundation" as copyright holder implied the Blender
Foundation holds copyright to files which may include work from many
developers.

While keeping copyright on headers makes sense for isolated libraries,
Blender's own code may be refactored or moved between files in a way
that makes the per file copyright holders less meaningful.

Copyright references to the "Blender Foundation" have been replaced with
"Blender Authors", with the exception of `./extern/` since these this
contains libraries which are more isolated, any changed to license
headers there can be handled on a case-by-case basis.

Some directories in `./intern/` have also been excluded:

- `./intern/cycles/` it's own `AUTHORS` file is planned.
- `./intern/opensubdiv/`.

An "AUTHORS" file has been added, using the chromium projects authors
file as a template.

Design task: #110784

Ref !110783.
2023-08-16 00:20:26 +10:00

266 lines
6.8 KiB
C++

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup bmesh
*
* Edge-Net for filling in open edge-loops.
*/
#include "MEM_guardedalloc.h"
#include "BLI_array.h"
#include "BLI_math_vector.h"
#include "bmesh.h"
#include "bmesh_tools.h"
#include "intern/bmesh_operators_private.h" /* own include */
#define EDGE_MARK 1
#define EDGE_VIS 2
#define ELE_NEW 1
void bmo_edgenet_fill_exec(BMesh *bm, BMOperator *op)
{
BMOperator op_attr;
BMOIter siter;
BMFace *f;
const short mat_nr = BMO_slot_int_get(op->slots_in, "mat_nr");
const bool use_smooth = BMO_slot_bool_get(op->slots_in, "use_smooth");
// const int sides = BMO_slot_int_get(op->slots_in, "sides");
if (!bm->totvert || !bm->totedge) {
return;
}
BM_mesh_elem_hflag_disable_all(bm, BM_EDGE, BM_ELEM_TAG, false);
BMO_slot_buffer_hflag_enable(bm, op->slots_in, "edges", BM_EDGE, BM_ELEM_TAG, false);
BM_mesh_elem_hflag_disable_all(bm, BM_FACE, BM_ELEM_TAG, false);
BM_mesh_edgenet(bm, true, true); /* TODO: sides. */
BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "faces.out", BM_FACE, BM_ELEM_TAG);
BMO_ITER (f, &siter, op->slots_out, "faces.out", BM_FACE) {
f->mat_nr = mat_nr;
if (use_smooth) {
BM_elem_flag_enable(f, BM_ELEM_SMOOTH);
}
/* normals are zero'd */
BM_face_normal_update(f);
}
/* --- Attribute Fill --- */
/* may as well since we have the faces already in a buffer */
BMO_op_initf(bm,
&op_attr,
op->flag,
"face_attribute_fill faces=%S use_normals=%b use_data=%b",
op,
"faces.out",
true,
true);
BMO_op_exec(bm, &op_attr);
/* check if some faces couldn't be touched */
if (BMO_slot_buffer_len(op_attr.slots_out, "faces_fail.out")) {
BMO_op_callf(bm, op->flag, "recalc_face_normals faces=%S", &op_attr, "faces_fail.out");
}
BMO_op_finish(bm, &op_attr);
}
static BMEdge *edge_next(BMesh *bm, BMEdge *e)
{
BMIter iter;
BMEdge *e2;
int i;
for (i = 0; i < 2; i++) {
BM_ITER_ELEM (e2, &iter, i ? e->v2 : e->v1, BM_EDGES_OF_VERT) {
if (BMO_edge_flag_test(bm, e2, EDGE_MARK) &&
(BMO_edge_flag_test(bm, e2, EDGE_VIS) == false) && (e2 != e))
{
return e2;
}
}
}
return nullptr;
}
void bmo_edgenet_prepare_exec(BMesh *bm, BMOperator *op)
{
BMOIter siter;
BMEdge *e;
BMEdge **edges1 = nullptr, **edges2 = nullptr, **edges;
BLI_array_declare(edges1);
BLI_array_declare(edges2);
BLI_array_declare(edges);
bool ok = true;
int i, count;
BMO_slot_buffer_flag_enable(bm, op->slots_in, "edges", BM_EDGE, EDGE_MARK);
/* validate that each edge has at most one other tagged edge in the
* disk cycle around each of its vertices */
BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
for (i = 0; i < 2; i++) {
count = BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, (i ? e->v2 : e->v1), EDGE_MARK, true);
if (count > 2) {
ok = false;
break;
}
}
if (!ok) {
break;
}
}
/* we don't have valid edge layouts, return */
if (!ok) {
return;
}
/* find connected loops within the input edge */
count = 0;
while (true) {
BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
if (!BMO_edge_flag_test(bm, e, EDGE_VIS)) {
if (BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, e->v1, EDGE_MARK, true) == 1 ||
BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, e->v2, EDGE_MARK, true) == 1)
{
break;
}
}
}
if (!e) {
break;
}
if (!count) {
edges = edges1;
}
else if (count == 1) {
edges = edges2;
}
else {
break;
}
i = 0;
while (e) {
BMO_edge_flag_enable(bm, e, EDGE_VIS);
BLI_array_grow_one(edges);
edges[i] = e;
e = edge_next(bm, e);
i++;
}
if (!count) {
edges1 = edges;
BLI_array_len_set(edges1, BLI_array_len(edges));
}
else {
edges2 = edges;
BLI_array_len_set(edges2, BLI_array_len(edges));
}
BLI_array_clear(edges);
count++;
}
if (edges1 && BLI_array_len(edges1) > 2 &&
BM_edge_share_vert_check(edges1[0], edges1[BLI_array_len(edges1) - 1]))
{
if (edges2 && BLI_array_len(edges2) > 2 &&
BM_edge_share_vert_check(edges2[0], edges2[BLI_array_len(edges2) - 1]))
{
BLI_array_free(edges1);
BLI_array_free(edges2);
return;
}
edges1 = edges2;
edges2 = nullptr;
}
if (edges2 && BLI_array_len(edges2) > 2 &&
BM_edge_share_vert_check(edges2[0], edges2[BLI_array_len(edges2) - 1]))
{
edges2 = nullptr;
}
/* two unconnected loops, connect the */
if (edges1 && edges2) {
BMVert *v1, *v2, *v3, *v4;
float dvec1[3];
float dvec2[3];
if (BLI_array_len(edges1) == 1) {
v1 = edges1[0]->v1;
v2 = edges1[0]->v2;
}
else {
v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
i = BLI_array_len(edges1) - 1;
v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
}
if (BLI_array_len(edges2) == 1) {
v3 = edges2[0]->v1;
v4 = edges2[0]->v2;
}
else {
v3 = BM_vert_in_edge(edges2[1], edges2[0]->v1) ? edges2[0]->v2 : edges2[0]->v1;
i = BLI_array_len(edges2) - 1;
v4 = BM_vert_in_edge(edges2[i - 1], edges2[i]->v1) ? edges2[i]->v2 : edges2[i]->v1;
}
/* if there is ever bow-tie quads between two edges the problem is here! #30367. */
#if 0
normal_tri_v3(dvec1, v1->co, v2->co, v4->co);
normal_tri_v3(dvec2, v1->co, v4->co, v3->co);
#else
{
/* Save some CPU cycles and skip the `sqrt` and 1 subtraction. */
float a1[3], a2[3], a3[3];
sub_v3_v3v3(a1, v1->co, v2->co);
sub_v3_v3v3(a2, v1->co, v4->co);
sub_v3_v3v3(a3, v1->co, v3->co);
cross_v3_v3v3(dvec1, a1, a2);
cross_v3_v3v3(dvec2, a2, a3);
}
#endif
if (dot_v3v3(dvec1, dvec2) < 0.0f) {
SWAP(BMVert *, v3, v4);
}
e = BM_edge_create(bm, v1, v3, nullptr, BM_CREATE_NO_DOUBLE);
BMO_edge_flag_enable(bm, e, ELE_NEW);
e = BM_edge_create(bm, v2, v4, nullptr, BM_CREATE_NO_DOUBLE);
BMO_edge_flag_enable(bm, e, ELE_NEW);
}
else if (edges1) {
BMVert *v1, *v2;
if (BLI_array_len(edges1) > 1) {
v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
i = BLI_array_len(edges1) - 1;
v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
e = BM_edge_create(bm, v1, v2, nullptr, BM_CREATE_NO_DOUBLE);
BMO_edge_flag_enable(bm, e, ELE_NEW);
}
}
BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, ELE_NEW);
BLI_array_free(edges1);
BLI_array_free(edges2);
}