2012-02-19 18:31:04 +00:00
|
|
|
/*
|
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): Joseph Eagar, Geoffrey Bantle, Campbell Barton
|
|
|
|
|
*
|
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** \file blender/bmesh/intern/bmesh_mods.c
|
|
|
|
|
* \ingroup bmesh
|
|
|
|
|
*
|
|
|
|
|
* This file contains functions for locally modifying
|
|
|
|
|
* the topology of existing mesh data. (split, join, flip etc).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "BLI_math.h"
|
|
|
|
|
#include "BLI_array.h"
|
|
|
|
|
#include "BLI_smallhash.h"
|
|
|
|
|
|
|
|
|
|
#include "BKE_customdata.h"
|
|
|
|
|
|
|
|
|
|
#include "bmesh.h"
|
2012-03-08 03:25:53 +00:00
|
|
|
#include "intern/bmesh_private.h"
|
2012-02-19 18:31:04 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-02-29 06:55:10 +00:00
|
|
|
* \brief Dissolve Vert
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* Turns the face region surrounding a manifold vertex into a single polygon.
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* \par Example:
|
2012-08-13 15:17:15 +00:00
|
|
|
* <pre>
|
2012-03-06 16:17:55 +00:00
|
|
|
* +---------+ +---------+
|
2012-02-29 06:55:10 +00:00
|
|
|
* | \ / | | |
|
2012-03-06 16:17:55 +00:00
|
|
|
* Before: | v | After: | |
|
2012-02-29 06:55:10 +00:00
|
|
|
* | / \ | | |
|
2012-03-06 16:17:55 +00:00
|
|
|
* +---------+ +---------+
|
2012-08-13 15:17:15 +00:00
|
|
|
* </pre>
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-03-06 16:17:55 +00:00
|
|
|
* This function can also collapse edges too
|
|
|
|
|
* in cases when it cant merge into faces.
|
|
|
|
|
*
|
|
|
|
|
* \par Example:
|
2012-08-13 15:17:15 +00:00
|
|
|
* <pre>
|
2012-03-06 16:17:55 +00:00
|
|
|
* Before: +----v----+ After: +---------+
|
2012-08-13 15:17:15 +00:00
|
|
|
* </pre>
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-28 18:28:30 +00:00
|
|
|
* \note dissolves vert, in more situations then BM_disk_dissolve
|
|
|
|
|
* (e.g. if the vert is part of a wire edge, etc).
|
2012-02-19 18:31:04 +00:00
|
|
|
*/
|
|
|
|
|
int BM_vert_dissolve(BMesh *bm, BMVert *v)
|
|
|
|
|
{
|
2012-02-25 23:41:31 +00:00
|
|
|
const int len = BM_vert_edge_count(v);
|
2012-02-19 18:31:04 +00:00
|
|
|
|
|
|
|
|
if (len == 1) {
|
2012-02-25 23:41:31 +00:00
|
|
|
BM_vert_kill(bm, v); /* will kill edges too */
|
2012-02-19 18:31:04 +00:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
2012-03-22 07:53:11 +00:00
|
|
|
else if (!BM_vert_is_manifold(v)) {
|
2012-02-25 23:41:31 +00:00
|
|
|
if (!v->e) {
|
|
|
|
|
BM_vert_kill(bm, v);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2012-02-19 18:31:04 +00:00
|
|
|
else if (!v->e->l) {
|
2012-02-25 23:29:12 +00:00
|
|
|
if (len == 2) {
|
2012-02-26 05:48:12 +00:00
|
|
|
return (BM_vert_collapse_edge(bm, v->e, v, TRUE) != NULL);
|
2012-02-25 23:29:12 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2012-02-26 00:43:47 +00:00
|
|
|
/* used to kill the vertex here, but it may be connected to faces.
|
|
|
|
|
* so better do nothing */
|
|
|
|
|
return FALSE;
|
2012-02-25 23:29:12 +00:00
|
|
|
}
|
2012-02-19 18:31:04 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-26 00:43:47 +00:00
|
|
|
else if (len == 2 && BM_vert_face_count(v) == 1) {
|
2012-03-18 07:38:51 +00:00
|
|
|
/* boundary vertex on a face */
|
2012-02-26 05:48:12 +00:00
|
|
|
return (BM_vert_collapse_edge(bm, v->e, v, TRUE) != NULL);
|
2012-02-26 00:43:47 +00:00
|
|
|
}
|
2012-02-25 23:41:31 +00:00
|
|
|
else {
|
|
|
|
|
return BM_disk_dissolve(bm, v);
|
|
|
|
|
}
|
2012-02-19 18:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
2012-02-28 18:28:30 +00:00
|
|
|
/**
|
|
|
|
|
* dissolves all faces around a vert, and removes it.
|
|
|
|
|
*/
|
2012-02-19 18:31:04 +00:00
|
|
|
int BM_disk_dissolve(BMesh *bm, BMVert *v)
|
|
|
|
|
{
|
|
|
|
|
BMFace *f, *f2;
|
|
|
|
|
BMEdge *e, *keepedge = NULL, *baseedge = NULL;
|
|
|
|
|
int len = 0;
|
|
|
|
|
|
2012-03-22 07:53:11 +00:00
|
|
|
if (!BM_vert_is_manifold(v)) {
|
2012-02-19 18:31:04 +00:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (v->e) {
|
|
|
|
|
/* v->e we keep, what else */
|
|
|
|
|
e = v->e;
|
|
|
|
|
do {
|
2012-02-28 19:30:44 +00:00
|
|
|
e = bmesh_disk_edge_next(e, v);
|
2012-02-26 19:46:12 +00:00
|
|
|
if (!(BM_edge_share_face_count(e, v->e))) {
|
2012-02-19 18:31:04 +00:00
|
|
|
keepedge = e;
|
|
|
|
|
baseedge = v->e;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
len++;
|
|
|
|
|
} while (e != v->e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* this code for handling 2 and 3-valence verts
|
|
|
|
|
* may be totally bad */
|
|
|
|
|
if (keepedge == NULL && len == 3) {
|
|
|
|
|
/* handle specific case for three-valence. solve it by
|
|
|
|
|
* increasing valence to four. this may be hackish. . */
|
|
|
|
|
BMLoop *loop = e->l;
|
|
|
|
|
if (loop->v == v) loop = loop->next;
|
2012-03-06 19:29:05 +00:00
|
|
|
if (!BM_face_split(bm, loop->f, v, loop->v, NULL, NULL, FALSE))
|
2012-02-19 18:31:04 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (!BM_disk_dissolve(bm, v)) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
else if (keepedge == NULL && len == 2) {
|
|
|
|
|
/* collapse the verte */
|
2012-02-26 05:48:12 +00:00
|
|
|
e = BM_vert_collapse_faces(bm, v->e, v, 1.0, TRUE, TRUE);
|
2012-02-19 18:31:04 +00:00
|
|
|
|
|
|
|
|
if (!e) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* handle two-valenc */
|
|
|
|
|
f = e->l->f;
|
|
|
|
|
f2 = e->l->radial_next->f;
|
|
|
|
|
|
2012-04-04 14:48:10 +00:00
|
|
|
if (f != f2 && !BM_faces_join_pair(bm, f, f2, e, TRUE)) {
|
2012-02-19 18:31:04 +00:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (keepedge) {
|
2012-05-19 13:28:19 +00:00
|
|
|
int done = FALSE;
|
2012-02-19 18:31:04 +00:00
|
|
|
|
|
|
|
|
while (!done) {
|
2012-05-19 13:28:19 +00:00
|
|
|
done = TRUE;
|
2012-02-19 18:31:04 +00:00
|
|
|
e = v->e;
|
|
|
|
|
do {
|
|
|
|
|
f = NULL;
|
|
|
|
|
len = bmesh_radial_length(e->l);
|
|
|
|
|
if (len == 2 && (e != baseedge) && (e != keepedge)) {
|
2012-04-04 14:48:10 +00:00
|
|
|
f = BM_faces_join_pair(bm, e->l->f, e->l->radial_next->f, e, TRUE);
|
2012-02-19 18:31:04 +00:00
|
|
|
/* return if couldn't join faces in manifold
|
|
|
|
|
* conditions */
|
2012-08-25 17:42:15 +00:00
|
|
|
/* !disabled for testing why bad things happen */
|
2012-02-19 18:31:04 +00:00
|
|
|
if (!f) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (f) {
|
2012-05-19 13:28:19 +00:00
|
|
|
done = FALSE;
|
2012-02-19 18:31:04 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2012-02-28 19:30:44 +00:00
|
|
|
e = bmesh_disk_edge_next(e, v);
|
2012-02-19 18:31:04 +00:00
|
|
|
} while (e != v->e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* collapse the verte */
|
2012-02-26 05:48:12 +00:00
|
|
|
e = BM_vert_collapse_faces(bm, baseedge, v, 1.0, TRUE, TRUE);
|
2012-02-19 18:31:04 +00:00
|
|
|
|
|
|
|
|
if (!e) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* get remaining two face */
|
|
|
|
|
f = e->l->f;
|
|
|
|
|
f2 = e->l->radial_next->f;
|
|
|
|
|
|
|
|
|
|
if (f != f2) {
|
|
|
|
|
/* join two remaining face */
|
2012-04-04 14:48:10 +00:00
|
|
|
if (!BM_faces_join_pair(bm, f, f2, e, TRUE)) {
|
2012-02-19 18:31:04 +00:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-02-29 06:55:10 +00:00
|
|
|
* \brief Faces Join Pair
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-04-07 12:37:15 +00:00
|
|
|
* Joins two adjacent faces together.
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* Because this method calls to #BM_faces_join to do its work, if a pair
|
|
|
|
|
* of faces share multiple edges, the pair of faces will be joined at
|
|
|
|
|
* every edge (not just edge \a e). This part of the functionality might need
|
|
|
|
|
* to be reconsidered.
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* If the windings do not match the winding of the new face will follow
|
|
|
|
|
* \a f1's winding (i.e. \a f2 will be reversed before the join).
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* \return pointer to the combined face
|
2012-02-19 18:31:04 +00:00
|
|
|
*/
|
2012-04-04 14:48:10 +00:00
|
|
|
BMFace *BM_faces_join_pair(BMesh *bm, BMFace *f1, BMFace *f2, BMEdge *e, const short do_del)
|
2012-02-19 18:31:04 +00:00
|
|
|
{
|
|
|
|
|
BMLoop *l1, *l2;
|
|
|
|
|
BMEdge *jed = NULL;
|
|
|
|
|
BMFace *faces[2] = {f1, f2};
|
|
|
|
|
|
|
|
|
|
jed = e;
|
|
|
|
|
if (!jed) {
|
|
|
|
|
BMLoop *l_first;
|
2012-04-07 12:37:15 +00:00
|
|
|
/* search for an edge that has both these faces in its radial cycle */
|
2012-02-19 18:31:04 +00:00
|
|
|
l1 = l_first = BM_FACE_FIRST_LOOP(f1);
|
|
|
|
|
do {
|
|
|
|
|
if (l1->radial_next->f == f2) {
|
|
|
|
|
jed = l1->e;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} while ((l1 = l1->next) != l_first);
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-26 16:39:21 +00:00
|
|
|
if (UNLIKELY(!jed)) {
|
|
|
|
|
BMESH_ASSERT(0);
|
2012-02-19 18:31:04 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l1 = jed->l;
|
|
|
|
|
|
2012-02-26 16:39:21 +00:00
|
|
|
if (UNLIKELY(!l1)) {
|
|
|
|
|
BMESH_ASSERT(0);
|
2012-02-19 18:31:04 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l2 = l1->radial_next;
|
|
|
|
|
if (l1->v == l2->v) {
|
|
|
|
|
bmesh_loop_reverse(bm, f2);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-04 14:48:10 +00:00
|
|
|
f1 = BM_faces_join(bm, faces, 2, do_del);
|
2012-02-19 18:31:04 +00:00
|
|
|
|
|
|
|
|
return f1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-28 07:42:48 +00:00
|
|
|
/**
|
2012-02-29 06:55:10 +00:00
|
|
|
* \brief Connect Verts, Split Face
|
|
|
|
|
*
|
2012-02-28 07:42:48 +00:00
|
|
|
* connects two verts together, automatically (if very naively) finding the
|
2012-04-07 12:37:15 +00:00
|
|
|
* face they both share (if there is one) and splitting it. Use this at your
|
2012-02-19 18:31:04 +00:00
|
|
|
* own risk, as it doesn't handle the many complex cases it should (like zero-area faces,
|
|
|
|
|
* multiple faces, etc).
|
|
|
|
|
*
|
|
|
|
|
* this is really only meant for cases where you don't know before hand the face
|
|
|
|
|
* the two verts belong to for splitting (e.g. the subdivision operator).
|
2012-02-29 06:55:10 +00:00
|
|
|
*
|
|
|
|
|
* \return The newly created edge.
|
2012-02-19 18:31:04 +00:00
|
|
|
*/
|
2012-02-28 07:42:48 +00:00
|
|
|
BMEdge *BM_verts_connect(BMesh *bm, BMVert *v1, BMVert *v2, BMFace **r_f)
|
2012-02-19 18:31:04 +00:00
|
|
|
{
|
2012-02-28 08:17:53 +00:00
|
|
|
BMIter fiter;
|
|
|
|
|
BMIter viter;
|
|
|
|
|
BMVert *v_iter;
|
|
|
|
|
BMFace *f_iter;
|
2012-02-19 18:31:04 +00:00
|
|
|
|
2012-02-23 09:26:53 +00:00
|
|
|
/* be warned: this can do weird things in some ngon situation, see BM_face_legal_splits */
|
2012-04-19 13:47:58 +00:00
|
|
|
BM_ITER_ELEM (f_iter, &fiter, v1, BM_FACES_OF_VERT) {
|
|
|
|
|
BM_ITER_ELEM (v_iter, &viter, f_iter, BM_FACES_OF_VERT) {
|
2012-02-28 08:17:53 +00:00
|
|
|
if (v_iter == v2) {
|
|
|
|
|
BMLoop *nl;
|
2012-02-19 18:31:04 +00:00
|
|
|
|
2012-03-06 19:29:05 +00:00
|
|
|
f_iter = BM_face_split(bm, f_iter, v1, v2, &nl, NULL, FALSE);
|
2012-02-28 08:17:53 +00:00
|
|
|
|
|
|
|
|
if (r_f) {
|
|
|
|
|
*r_f = f_iter;
|
|
|
|
|
}
|
2012-02-19 18:31:04 +00:00
|
|
|
return nl->e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-29 06:55:10 +00:00
|
|
|
if (r_f) {
|
|
|
|
|
*r_f = NULL;
|
|
|
|
|
}
|
2012-02-19 18:31:04 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-02-29 06:55:10 +00:00
|
|
|
* \brief Face Split
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* Split a face along two vertices. returns the newly made face, and sets
|
|
|
|
|
* the \a r_l member to a loop in the newly created edge.
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* \param bm The bmesh
|
|
|
|
|
* \param f the original face
|
|
|
|
|
* \param v1, v2 vertices which define the split edge, must be different
|
|
|
|
|
* \param r_l pointer which will receive the BMLoop for the split edge in the new face
|
2012-03-06 12:09:35 +00:00
|
|
|
* \param example Edge used for attributes of splitting edge, if non-NULL
|
2012-03-06 19:29:05 +00:00
|
|
|
* \param nodouble Use an existing edge if found
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-28 07:42:48 +00:00
|
|
|
* \return Pointer to the newly created face representing one side of the split
|
|
|
|
|
* if the split is successful (and the original original face will be the
|
|
|
|
|
* other side). NULL if the split fails.
|
2012-02-19 18:31:04 +00:00
|
|
|
*/
|
2012-03-06 19:29:05 +00:00
|
|
|
BMFace *BM_face_split(BMesh *bm, BMFace *f, BMVert *v1, BMVert *v2, BMLoop **r_l,
|
|
|
|
|
BMEdge *example, const short nodouble)
|
2012-02-19 18:31:04 +00:00
|
|
|
{
|
|
|
|
|
const int has_mdisp = CustomData_has_layer(&bm->ldata, CD_MDISPS);
|
|
|
|
|
BMFace *nf, *of;
|
2012-02-23 09:26:53 +00:00
|
|
|
|
|
|
|
|
BLI_assert(v1 != v2);
|
|
|
|
|
|
2012-02-19 18:31:04 +00:00
|
|
|
/* do we have a multires layer */
|
|
|
|
|
if (has_mdisp) {
|
2012-02-23 04:26:24 +00:00
|
|
|
of = BM_face_copy(bm, f, FALSE, FALSE);
|
2012-02-19 18:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef USE_BMESH_HOLES
|
2012-03-06 19:29:05 +00:00
|
|
|
nf = bmesh_sfme(bm, f, v1, v2, r_l, NULL, example, nodouble);
|
2012-02-19 18:31:04 +00:00
|
|
|
#else
|
2012-03-06 19:29:05 +00:00
|
|
|
nf = bmesh_sfme(bm, f, v1, v2, r_l, example, nodouble);
|
2012-02-19 18:31:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (nf) {
|
|
|
|
|
BM_elem_attrs_copy(bm, bm, f, nf);
|
|
|
|
|
copy_v3_v3(nf->no, f->no);
|
|
|
|
|
|
|
|
|
|
/* handle multires update */
|
|
|
|
|
if (has_mdisp && (nf != f)) {
|
|
|
|
|
BMLoop *l_iter;
|
|
|
|
|
BMLoop *l_first;
|
|
|
|
|
|
|
|
|
|
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
|
|
|
|
|
do {
|
2012-06-29 10:12:42 +00:00
|
|
|
BM_loop_interp_multires(bm, l_iter, of);
|
2012-02-19 18:31:04 +00:00
|
|
|
} while ((l_iter = l_iter->next) != l_first);
|
|
|
|
|
|
|
|
|
|
l_iter = l_first = BM_FACE_FIRST_LOOP(nf);
|
|
|
|
|
do {
|
2012-06-29 10:12:42 +00:00
|
|
|
BM_loop_interp_multires(bm, l_iter, of);
|
2012-02-19 18:31:04 +00:00
|
|
|
} while ((l_iter = l_iter->next) != l_first);
|
|
|
|
|
|
|
|
|
|
BM_face_kill(bm, of);
|
|
|
|
|
|
2012-02-21 17:23:32 +00:00
|
|
|
#if 0
|
|
|
|
|
/* BM_face_multires_bounds_smooth doesn't flip displacement correct */
|
2012-02-19 18:31:04 +00:00
|
|
|
BM_face_multires_bounds_smooth(bm, f);
|
|
|
|
|
BM_face_multires_bounds_smooth(bm, nf);
|
2012-02-21 17:23:32 +00:00
|
|
|
#endif
|
2012-02-19 18:31:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nf;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-06 12:09:35 +00:00
|
|
|
/**
|
|
|
|
|
* \brief Face Split with intermediate points
|
|
|
|
|
*
|
|
|
|
|
* Like BM_face_split, but with an edge split by \a n intermediate points with given coordinates.
|
|
|
|
|
*
|
|
|
|
|
* \param bm The bmesh
|
|
|
|
|
* \param f the original face
|
|
|
|
|
* \param v1, v2 vertices which define the split edge, must be different
|
2012-08-17 14:43:20 +00:00
|
|
|
* \param cos Array of coordinates for intermediate points
|
2012-03-06 12:09:35 +00:00
|
|
|
* \param n Length of \a cos (must be > 0)
|
|
|
|
|
* \param r_l pointer which will receive the BMLoop for the first split edge (from \a v1) in the new face
|
|
|
|
|
* \param example Edge used for attributes of splitting edge, if non-NULL
|
|
|
|
|
*
|
|
|
|
|
* \return Pointer to the newly created face representing one side of the split
|
|
|
|
|
* if the split is successful (and the original original face will be the
|
|
|
|
|
* other side). NULL if the split fails.
|
|
|
|
|
*/
|
|
|
|
|
BMFace *BM_face_split_n(BMesh *bm, BMFace *f, BMVert *v1, BMVert *v2, float cos[][3], int n,
|
|
|
|
|
BMLoop **r_l, BMEdge *example)
|
|
|
|
|
{
|
|
|
|
|
BMFace *nf, *of;
|
|
|
|
|
BMLoop *l_dummy;
|
|
|
|
|
BMEdge *e, *newe;
|
|
|
|
|
BMVert *newv;
|
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
|
|
BLI_assert(v1 != v2);
|
|
|
|
|
|
|
|
|
|
of = BM_face_copy(bm, f, TRUE, TRUE);
|
|
|
|
|
|
|
|
|
|
if (!r_l)
|
|
|
|
|
r_l = &l_dummy;
|
|
|
|
|
|
|
|
|
|
#ifdef USE_BMESH_HOLES
|
2012-03-06 19:29:05 +00:00
|
|
|
nf = bmesh_sfme(bm, f, v1, v2, r_l, NULL, example, FALSE);
|
2012-03-06 12:09:35 +00:00
|
|
|
#else
|
2012-03-06 19:29:05 +00:00
|
|
|
nf = bmesh_sfme(bm, f, v1, v2, r_l, example, FALSE);
|
2012-03-06 12:09:35 +00:00
|
|
|
#endif
|
|
|
|
|
/* bmesh_sfme returns in r_l a Loop for nf going from v1 to v2.
|
|
|
|
|
* The radial_next is for f and goes from v2 to v1 */
|
|
|
|
|
|
|
|
|
|
if (nf) {
|
2012-03-07 14:44:43 +00:00
|
|
|
BM_elem_attrs_copy(bm, bm, f, nf);
|
|
|
|
|
copy_v3_v3(nf->no, f->no);
|
|
|
|
|
|
2012-03-06 12:09:35 +00:00
|
|
|
e = (*r_l)->e;
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
|
newv = bmesh_semv(bm, v2, e, &newe);
|
|
|
|
|
BLI_assert(newv != NULL);
|
|
|
|
|
/* bmesh_semv returns in newe the edge going from newv to tv */
|
|
|
|
|
copy_v3_v3(newv->co, cos[i]);
|
|
|
|
|
|
|
|
|
|
/* interpolate the loop data for the loops with v==newv, using orig face */
|
|
|
|
|
for (j = 0; j < 2; j++) {
|
2012-03-21 09:10:08 +00:00
|
|
|
BMEdge *e_iter = (j == 0) ? e : newe;
|
2012-03-06 12:09:35 +00:00
|
|
|
BMLoop *l_iter = e_iter->l;
|
|
|
|
|
do {
|
|
|
|
|
if (l_iter->v == newv) {
|
|
|
|
|
/* this interpolates both loop and vertex data */
|
|
|
|
|
BM_loop_interp_from_face(bm, l_iter, of, TRUE, TRUE);
|
|
|
|
|
}
|
|
|
|
|
} while ((l_iter = l_iter->radial_next) != e_iter->l);
|
|
|
|
|
}
|
|
|
|
|
e = newe;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BM_face_verts_kill(bm, of);
|
|
|
|
|
|
|
|
|
|
return nf;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-19 18:31:04 +00:00
|
|
|
/**
|
2012-02-29 06:55:10 +00:00
|
|
|
* \brief Vert Collapse Faces
|
|
|
|
|
*
|
|
|
|
|
* Collapses vertex \a kv that has only two manifold edges
|
|
|
|
|
* onto a vertex it shares an edge with.
|
|
|
|
|
* \a fac defines the amount of interpolation for Custom Data.
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-28 07:42:48 +00:00
|
|
|
* \note that this is not a general edge collapse function.
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* \note this function is very close to #BM_vert_collapse_edge,
|
|
|
|
|
* both collapse a vertex and return a new edge.
|
|
|
|
|
* Except this takes a factor and merges custom data.
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
|
|
|
|
* BMESH_TODO:
|
|
|
|
|
* Insert error checking for KV valance.
|
|
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* \param bm The bmesh
|
|
|
|
|
* \param ke The edge to collapse
|
|
|
|
|
* \param kv The vertex to collapse into the edge
|
2012-02-28 07:42:48 +00:00
|
|
|
* \param fac The factor along the edge
|
|
|
|
|
* \param join_faces When true the faces around the vertex will be joined
|
2012-02-19 18:31:04 +00:00
|
|
|
* otherwise collapse the vertex by merging the 2 edges this vert touches into one.
|
2012-02-28 07:42:48 +00:00
|
|
|
* \param kill_degenerate_faces Removes faces with less than 3 verts after collapsing.
|
2012-02-29 06:55:10 +00:00
|
|
|
*
|
2012-02-28 07:42:48 +00:00
|
|
|
* \returns The New Edge
|
2012-02-19 18:31:04 +00:00
|
|
|
*/
|
2012-02-26 05:48:12 +00:00
|
|
|
BMEdge *BM_vert_collapse_faces(BMesh *bm, BMEdge *ke, BMVert *kv, float fac,
|
|
|
|
|
const short join_faces, const short kill_degenerate_faces)
|
2012-02-19 18:31:04 +00:00
|
|
|
{
|
|
|
|
|
BMEdge *ne = NULL;
|
2012-02-27 13:47:53 +00:00
|
|
|
BMVert *tv = bmesh_edge_other_vert_get(ke, kv);
|
2012-02-19 18:31:04 +00:00
|
|
|
|
|
|
|
|
BMEdge *e2;
|
|
|
|
|
BMVert *tv2;
|
|
|
|
|
|
|
|
|
|
BMIter iter;
|
|
|
|
|
BMLoop *l_iter = NULL, *kvloop = NULL, *tvloop = NULL;
|
|
|
|
|
|
|
|
|
|
void *src[2];
|
|
|
|
|
float w[2];
|
|
|
|
|
|
|
|
|
|
/* Only intended to be called for 2-valence vertices */
|
|
|
|
|
BLI_assert(bmesh_disk_count(kv) <= 2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* first modify the face loop data */
|
|
|
|
|
w[0] = 1.0f - fac;
|
|
|
|
|
w[1] = fac;
|
|
|
|
|
|
|
|
|
|
if (ke->l) {
|
|
|
|
|
l_iter = ke->l;
|
|
|
|
|
do {
|
|
|
|
|
if (l_iter->v == tv && l_iter->next->v == kv) {
|
|
|
|
|
tvloop = l_iter;
|
|
|
|
|
kvloop = l_iter->next;
|
|
|
|
|
|
|
|
|
|
src[0] = kvloop->head.data;
|
|
|
|
|
src[1] = tvloop->head.data;
|
|
|
|
|
CustomData_bmesh_interp(&bm->ldata, src, w, NULL, 2, kvloop->head.data);
|
|
|
|
|
}
|
|
|
|
|
} while ((l_iter = l_iter->radial_next) != ke->l);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* now interpolate the vertex data */
|
|
|
|
|
BM_data_interp_from_verts(bm, kv, tv, kv, fac);
|
|
|
|
|
|
2012-02-28 19:30:44 +00:00
|
|
|
e2 = bmesh_disk_edge_next(ke, kv);
|
2012-02-19 18:31:04 +00:00
|
|
|
tv2 = BM_edge_other_vert(e2, kv);
|
|
|
|
|
|
|
|
|
|
if (join_faces) {
|
2012-02-28 07:42:48 +00:00
|
|
|
BMFace **faces = NULL;
|
|
|
|
|
BMFace *f;
|
2012-02-19 18:31:04 +00:00
|
|
|
BLI_array_staticdeclare(faces, 8);
|
|
|
|
|
|
2012-04-19 13:47:58 +00:00
|
|
|
BM_ITER_ELEM (f, &iter, kv, BM_FACES_OF_VERT) {
|
2012-02-19 18:31:04 +00:00
|
|
|
BLI_array_append(faces, f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (BLI_array_count(faces) >= 2) {
|
2012-04-04 14:48:10 +00:00
|
|
|
BMFace *f2 = BM_faces_join(bm, faces, BLI_array_count(faces), TRUE);
|
2012-02-19 18:31:04 +00:00
|
|
|
if (f2) {
|
|
|
|
|
BMLoop *nl = NULL;
|
2012-03-06 19:29:05 +00:00
|
|
|
if (BM_face_split(bm, f2, tv, tv2, &nl, NULL, FALSE)) {
|
2012-02-19 18:31:04 +00:00
|
|
|
ne = nl->e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BLI_array_free(faces);
|
|
|
|
|
}
|
2012-02-26 05:48:12 +00:00
|
|
|
else {
|
|
|
|
|
/* single face or no faces */
|
|
|
|
|
/* same as BM_vert_collapse_edge() however we already
|
2012-03-18 07:38:51 +00:00
|
|
|
* have vars to perform this operation so don't call. */
|
2012-02-26 05:48:12 +00:00
|
|
|
ne = bmesh_jekv(bm, ke, kv, TRUE);
|
|
|
|
|
/* ne = BM_edge_exists(tv, tv2); */ /* same as return above */
|
|
|
|
|
|
2012-04-18 08:11:08 +00:00
|
|
|
if (ne && kill_degenerate_faces) {
|
2012-04-18 07:27:15 +00:00
|
|
|
BLI_array_declare(bad_faces);
|
|
|
|
|
BMFace **bad_faces = NULL;
|
|
|
|
|
|
2012-02-26 05:48:12 +00:00
|
|
|
BMIter fiter;
|
|
|
|
|
BMFace *f;
|
|
|
|
|
BMVert *verts[2] = {ne->v1, ne->v2};
|
|
|
|
|
int i;
|
2012-04-18 07:27:15 +00:00
|
|
|
|
2012-02-26 05:48:12 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2012-04-18 07:27:15 +00:00
|
|
|
/* cant kill data we loop on, build a list and remove those */
|
|
|
|
|
BLI_array_empty(bad_faces);
|
2012-04-19 13:47:58 +00:00
|
|
|
BM_ITER_ELEM (f, &fiter, verts[i], BM_FACES_OF_VERT) {
|
2012-02-26 05:48:12 +00:00
|
|
|
if (f->len < 3) {
|
2012-04-18 07:27:15 +00:00
|
|
|
BLI_array_append(bad_faces, f);
|
2012-02-26 05:48:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-04-18 07:27:15 +00:00
|
|
|
while ((f = BLI_array_pop(bad_faces))) {
|
|
|
|
|
BM_face_kill(bm, f);
|
|
|
|
|
}
|
2012-02-26 05:48:12 +00:00
|
|
|
}
|
2012-04-18 07:27:15 +00:00
|
|
|
BLI_array_free(bad_faces);
|
2012-02-26 05:48:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-02-19 18:31:04 +00:00
|
|
|
|
|
|
|
|
return ne;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-02-29 06:55:10 +00:00
|
|
|
* \brief Vert Collapse Faces
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
|
|
|
|
* Collapses a vertex onto another vertex it shares an edge with.
|
|
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* \return The New Edge
|
2012-02-19 18:31:04 +00:00
|
|
|
*/
|
2012-02-26 05:48:12 +00:00
|
|
|
BMEdge *BM_vert_collapse_edge(BMesh *bm, BMEdge *ke, BMVert *kv,
|
|
|
|
|
const short kill_degenerate_faces)
|
2012-02-19 18:31:04 +00:00
|
|
|
{
|
2012-02-25 16:49:59 +00:00
|
|
|
/* nice example implementation but we want loops to have their customdata
|
2012-02-19 18:31:04 +00:00
|
|
|
* accounted for */
|
|
|
|
|
#if 0
|
|
|
|
|
BMEdge *ne = NULL;
|
|
|
|
|
|
|
|
|
|
/* Collapse between 2 edges */
|
|
|
|
|
|
|
|
|
|
/* in this case we want to keep all faces and not join them,
|
2012-03-09 00:41:09 +00:00
|
|
|
* rather just get rid of the vertex - see bug [#28645] */
|
2012-02-27 13:47:53 +00:00
|
|
|
BMVert *tv = bmesh_edge_other_vert_get(ke, kv);
|
2012-02-19 18:31:04 +00:00
|
|
|
if (tv) {
|
2012-02-27 13:47:53 +00:00
|
|
|
BMEdge *e2 = bmesh_disk_edge_next(ke, kv);
|
2012-02-19 18:31:04 +00:00
|
|
|
if (e2) {
|
|
|
|
|
BMVert *tv2 = BM_edge_other_vert(e2, kv);
|
|
|
|
|
if (tv2) {
|
|
|
|
|
/* only action, other calls here only get the edge to return */
|
2012-02-26 04:38:37 +00:00
|
|
|
ne = bmesh_jekv(bm, ke, kv);
|
2012-02-19 18:31:04 +00:00
|
|
|
|
2012-02-26 04:38:37 +00:00
|
|
|
/* ne = BM_edge_exists(tv, tv2); */ /* same as return above */
|
2012-02-19 18:31:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ne;
|
|
|
|
|
#else
|
|
|
|
|
/* with these args faces are never joined, same as above
|
|
|
|
|
* but account for loop customdata */
|
2012-02-26 05:48:12 +00:00
|
|
|
return BM_vert_collapse_faces(bm, ke, kv, 1.0f, FALSE, kill_degenerate_faces);
|
2012-02-19 18:31:04 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#undef DO_V_INTERP
|
|
|
|
|
|
|
|
|
|
/**
|
2012-02-29 06:55:10 +00:00
|
|
|
* \brief Edge Split
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* Splits an edge. \a v should be one of the vertices in \a e and defines
|
2012-03-06 12:09:35 +00:00
|
|
|
* the "from" end of the splitting operation: the new vertex will be
|
|
|
|
|
* \a percent of the way from \a v to the other end.
|
|
|
|
|
* The newly created edge is attached to \a v and is returned in \a r_e.
|
|
|
|
|
* The original edge \a e will be the other half of the split.
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* \return The new vert
|
2012-02-19 18:31:04 +00:00
|
|
|
*/
|
2012-02-28 07:42:48 +00:00
|
|
|
BMVert *BM_edge_split(BMesh *bm, BMEdge *e, BMVert *v, BMEdge **r_e, float percent)
|
2012-02-19 18:31:04 +00:00
|
|
|
{
|
|
|
|
|
BMVert *nv, *v2;
|
|
|
|
|
BMFace **oldfaces = NULL;
|
2012-02-28 07:42:48 +00:00
|
|
|
BMEdge *e_dummy;
|
2012-02-19 18:31:04 +00:00
|
|
|
BLI_array_staticdeclare(oldfaces, 32);
|
|
|
|
|
SmallHash hash;
|
2012-03-03 14:48:50 +00:00
|
|
|
const int do_mdisp = (e->l && CustomData_has_layer(&bm->ldata, CD_MDISPS));
|
2012-02-19 18:31:04 +00:00
|
|
|
|
2012-04-07 12:37:15 +00:00
|
|
|
/* we need this for handling multi-res */
|
2012-02-28 07:42:48 +00:00
|
|
|
if (!r_e) {
|
|
|
|
|
r_e = &e_dummy;
|
|
|
|
|
}
|
2012-02-19 18:31:04 +00:00
|
|
|
|
2012-04-07 12:37:15 +00:00
|
|
|
/* do we have a multi-res layer */
|
2012-03-03 14:48:50 +00:00
|
|
|
if (do_mdisp) {
|
2012-02-19 18:31:04 +00:00
|
|
|
BMLoop *l;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
l = e->l;
|
|
|
|
|
do {
|
|
|
|
|
BLI_array_append(oldfaces, l->f);
|
|
|
|
|
l = l->radial_next;
|
|
|
|
|
} while (l != e->l);
|
|
|
|
|
|
|
|
|
|
/* create a hash so we can differentiate oldfaces from new face */
|
|
|
|
|
BLI_smallhash_init(&hash);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < BLI_array_count(oldfaces); i++) {
|
2012-02-23 04:26:24 +00:00
|
|
|
oldfaces[i] = BM_face_copy(bm, oldfaces[i], TRUE, TRUE);
|
2012-02-19 18:31:04 +00:00
|
|
|
BLI_smallhash_insert(&hash, (intptr_t)oldfaces[i], NULL);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-27 13:47:53 +00:00
|
|
|
v2 = bmesh_edge_other_vert_get(e, v);
|
2012-02-28 07:42:48 +00:00
|
|
|
nv = bmesh_semv(bm, v, e, r_e);
|
2012-03-03 14:48:50 +00:00
|
|
|
|
|
|
|
|
BLI_assert(nv != NULL);
|
2012-02-19 18:31:04 +00:00
|
|
|
|
|
|
|
|
sub_v3_v3v3(nv->co, v2->co, v->co);
|
|
|
|
|
madd_v3_v3v3fl(nv->co, v->co, nv->co, percent);
|
|
|
|
|
|
2012-02-28 07:42:48 +00:00
|
|
|
if (r_e) {
|
|
|
|
|
(*r_e)->head.hflag = e->head.hflag;
|
|
|
|
|
BM_elem_attrs_copy(bm, bm, e, *r_e);
|
2012-02-19 18:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* v->nv->v2 */
|
|
|
|
|
BM_data_interp_face_vert_edge(bm, v2, v, nv, e, percent);
|
|
|
|
|
BM_data_interp_from_verts(bm, v, v2, nv, percent);
|
|
|
|
|
|
2012-03-03 14:48:50 +00:00
|
|
|
if (do_mdisp) {
|
2012-02-19 18:31:04 +00:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
|
|
/* interpolate new/changed loop data from copied old face */
|
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
|
for (i = 0; i < BLI_array_count(oldfaces); i++) {
|
2012-02-28 07:42:48 +00:00
|
|
|
BMEdge *e1 = j ? *r_e : e;
|
2012-02-19 18:31:04 +00:00
|
|
|
BMLoop *l, *l2;
|
|
|
|
|
|
|
|
|
|
l = e1->l;
|
2012-02-26 16:39:21 +00:00
|
|
|
|
|
|
|
|
if (UNLIKELY(!l)) {
|
|
|
|
|
BMESH_ASSERT(0);
|
2012-02-19 18:31:04 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
if (!BLI_smallhash_haskey(&hash, (intptr_t)l->f)) {
|
|
|
|
|
BMLoop *l2_first;
|
|
|
|
|
|
|
|
|
|
l2 = l2_first = BM_FACE_FIRST_LOOP(l->f);
|
|
|
|
|
do {
|
|
|
|
|
BM_loop_interp_multires(bm, l2, oldfaces[i]);
|
|
|
|
|
} while ((l2 = l2->next) != l2_first);
|
|
|
|
|
}
|
|
|
|
|
l = l->radial_next;
|
|
|
|
|
} while (l != e1->l);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* destroy the old face */
|
|
|
|
|
for (i = 0; i < BLI_array_count(oldfaces); i++) {
|
|
|
|
|
BM_face_verts_kill(bm, oldfaces[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-07 12:37:15 +00:00
|
|
|
/* fix boundaries a bit, doesn't work too well quite yet */
|
2012-02-19 18:31:04 +00:00
|
|
|
#if 0
|
|
|
|
|
for (j = 0; j < 2; j++) {
|
2012-02-28 07:42:48 +00:00
|
|
|
BMEdge *e1 = j ? *r_e : e;
|
2012-02-19 18:31:04 +00:00
|
|
|
BMLoop *l, *l2;
|
|
|
|
|
|
|
|
|
|
l = e1->l;
|
2012-02-26 16:39:21 +00:00
|
|
|
if (UNLIKELY(!l)) {
|
|
|
|
|
BMESH_ASSERT(0);
|
2012-02-19 18:31:04 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
BM_face_multires_bounds_smooth(bm, l->f);
|
|
|
|
|
l = l->radial_next;
|
|
|
|
|
} while (l != e1->l);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
BLI_array_free(oldfaces);
|
|
|
|
|
BLI_smallhash_release(&hash);
|
|
|
|
|
}
|
2012-03-03 14:48:50 +00:00
|
|
|
|
2012-02-19 18:31:04 +00:00
|
|
|
return nv;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-06 16:17:55 +00:00
|
|
|
/**
|
|
|
|
|
* \brief Split an edge multiple times evenly
|
|
|
|
|
*/
|
2012-02-19 18:31:04 +00:00
|
|
|
BMVert *BM_edge_split_n(BMesh *bm, BMEdge *e, int numcuts)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
float percent;
|
|
|
|
|
BMVert *nv = NULL;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < numcuts; i++) {
|
|
|
|
|
percent = 1.0f / (float)(numcuts + 1 - i);
|
2012-02-23 05:17:07 +00:00
|
|
|
nv = BM_edge_split(bm, e, e->v2, NULL, percent);
|
2012-02-19 18:31:04 +00:00
|
|
|
}
|
|
|
|
|
return nv;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-06 16:17:55 +00:00
|
|
|
/**
|
|
|
|
|
* Checks if a face is valid in the data structure
|
|
|
|
|
*/
|
2012-04-23 02:17:57 +00:00
|
|
|
int BM_face_validate(BMFace *face, FILE *err)
|
2012-02-19 18:31:04 +00:00
|
|
|
{
|
|
|
|
|
BMIter iter;
|
|
|
|
|
BLI_array_declare(verts);
|
|
|
|
|
BMVert **verts = NULL;
|
|
|
|
|
BMLoop *l;
|
|
|
|
|
int ret = 1, i, j;
|
|
|
|
|
|
|
|
|
|
if (face->len == 2) {
|
|
|
|
|
fprintf(err, "warning: found two-edged face. face ptr: %p\n", face);
|
|
|
|
|
fflush(err);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-28 15:14:16 +00:00
|
|
|
BLI_array_grow_items(verts, face->len);
|
2012-04-23 02:17:57 +00:00
|
|
|
BM_ITER_ELEM_INDEX (l, &iter, face, BM_LOOPS_OF_FACE, i) {
|
|
|
|
|
verts[i] = l->v;
|
2012-02-19 18:31:04 +00:00
|
|
|
if (l->e->v1 == l->e->v2) {
|
|
|
|
|
fprintf(err, "Found bmesh edge with identical verts!\n");
|
|
|
|
|
fprintf(err, " edge ptr: %p, vert: %p\n", l->e, l->e->v1);
|
|
|
|
|
fflush(err);
|
|
|
|
|
ret = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-23 02:17:57 +00:00
|
|
|
for (i = 0; i < face->len; i++) {
|
|
|
|
|
for (j = 0; j < face->len; j++) {
|
2012-02-19 18:31:04 +00:00
|
|
|
if (j == i) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (verts[i] == verts[j]) {
|
|
|
|
|
fprintf(err, "Found duplicate verts in bmesh face!\n");
|
|
|
|
|
fprintf(err, " face ptr: %p, vert: %p\n", face, verts[i]);
|
|
|
|
|
fflush(err);
|
|
|
|
|
ret = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BLI_array_free(verts);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-05 00:50:18 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate the 2 loops which _would_ make up the newly rotated Edge
|
|
|
|
|
* but don't actually change anything.
|
|
|
|
|
*
|
|
|
|
|
* Use this to further inspect if the loops to be connected have issues:
|
|
|
|
|
*
|
|
|
|
|
* Examples:
|
|
|
|
|
* - the newly formed edge already exists
|
2012-03-18 07:38:51 +00:00
|
|
|
* - the new face would be degenerate (zero area / concave / bow-tie)
|
2012-03-05 00:50:18 +00:00
|
|
|
* - may want to measure if the new edge gives improved results topology.
|
|
|
|
|
* over the old one, as with beauty fill.
|
|
|
|
|
*
|
|
|
|
|
* \note #BM_edge_rotate_check must have already run.
|
|
|
|
|
*/
|
2012-04-23 01:19:50 +00:00
|
|
|
void BM_edge_calc_rotate(BMEdge *e, int ccw,
|
2012-03-05 00:50:18 +00:00
|
|
|
BMLoop **r_l1, BMLoop **r_l2)
|
|
|
|
|
{
|
|
|
|
|
BMVert *v1, *v2;
|
|
|
|
|
BMFace *fa, *fb;
|
|
|
|
|
|
|
|
|
|
/* this should have already run */
|
2012-04-19 14:38:09 +00:00
|
|
|
BLI_assert(BM_edge_rotate_check(e) == TRUE);
|
2012-03-05 00:50:18 +00:00
|
|
|
|
|
|
|
|
/* we know this will work */
|
|
|
|
|
BM_edge_face_pair(e, &fa, &fb);
|
|
|
|
|
|
|
|
|
|
/* so we can use ccw variable correctly,
|
2012-04-07 12:37:15 +00:00
|
|
|
* otherwise we could use the edges verts direct */
|
2012-03-05 00:50:18 +00:00
|
|
|
BM_edge_ordered_verts(e, &v1, &v2);
|
|
|
|
|
|
|
|
|
|
/* we could swap the verts _or_ the faces, swapping faces
|
2012-03-18 07:38:51 +00:00
|
|
|
* gives more predictable results since that way the next vert
|
|
|
|
|
* just stitches from face fa / fb */
|
2012-03-05 00:50:18 +00:00
|
|
|
if (ccw) {
|
|
|
|
|
SWAP(BMFace *, fa, fb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*r_l1 = BM_face_other_vert_loop(fb, v2, v1);
|
|
|
|
|
*r_l2 = BM_face_other_vert_loop(fa, v1, v2);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-03 12:35:37 +00:00
|
|
|
/**
|
|
|
|
|
* \brief Check if Rotate Edge is OK
|
|
|
|
|
*
|
|
|
|
|
* Quick check to see if we could rotate the edge,
|
|
|
|
|
* use this to avoid calling exceptions on common cases.
|
|
|
|
|
*/
|
2012-04-19 14:38:09 +00:00
|
|
|
int BM_edge_rotate_check(BMEdge *e)
|
2012-03-03 12:35:37 +00:00
|
|
|
{
|
|
|
|
|
BMFace *fa, *fb;
|
|
|
|
|
if (BM_edge_face_pair(e, &fa, &fb)) {
|
|
|
|
|
BMLoop *la, *lb;
|
|
|
|
|
|
2012-03-04 16:01:02 +00:00
|
|
|
la = BM_face_other_vert_loop(fa, e->v2, e->v1);
|
|
|
|
|
lb = BM_face_other_vert_loop(fb, e->v2, e->v1);
|
2012-03-03 12:35:37 +00:00
|
|
|
|
2012-03-18 07:38:51 +00:00
|
|
|
/* check that the next vert in both faces isn't the same
|
2012-04-07 12:37:15 +00:00
|
|
|
* (ie - the next edge doesn't share the same faces).
|
2012-03-03 12:35:37 +00:00
|
|
|
* since we can't rotate usefully in this case. */
|
|
|
|
|
if (la->v == lb->v) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* mirror of the check above but in the opposite direction */
|
2012-03-04 16:01:02 +00:00
|
|
|
la = BM_face_other_vert_loop(fa, e->v1, e->v2);
|
|
|
|
|
lb = BM_face_other_vert_loop(fb, e->v1, e->v2);
|
2012-03-03 12:35:37 +00:00
|
|
|
|
|
|
|
|
if (la->v == lb->v) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-05 00:50:18 +00:00
|
|
|
/**
|
|
|
|
|
* \brief Check if Edge Rotate Gives Degenerate Faces
|
|
|
|
|
*
|
|
|
|
|
* Check 2 cases
|
|
|
|
|
* 1) does the newly forms edge form a flipped face (compare with previous cross product)
|
2012-03-18 07:38:51 +00:00
|
|
|
* 2) does the newly formed edge cause a zero area corner (or close enough to be almost zero)
|
2012-03-05 00:50:18 +00:00
|
|
|
*
|
2012-08-17 14:43:20 +00:00
|
|
|
* \param e The edge to test rotation.
|
2012-03-05 00:50:18 +00:00
|
|
|
* \param l1,l2 are the loops of the proposed verts to rotate too and should
|
2012-04-23 01:19:50 +00:00
|
|
|
* be the result of calling #BM_edge_calc_rotate
|
2012-03-05 00:50:18 +00:00
|
|
|
*/
|
2012-04-19 14:38:09 +00:00
|
|
|
int BM_edge_rotate_check_degenerate(BMEdge *e, BMLoop *l1, BMLoop *l2)
|
2012-03-05 00:50:18 +00:00
|
|
|
{
|
|
|
|
|
/* note: for these vars 'old' just means initial edge state. */
|
|
|
|
|
|
|
|
|
|
float ed_dir_old[3]; /* edge vector */
|
|
|
|
|
float ed_dir_new[3]; /* edge vector */
|
|
|
|
|
float ed_dir_new_flip[3]; /* edge vector */
|
|
|
|
|
|
|
|
|
|
float ed_dir_v1_old[3];
|
|
|
|
|
float ed_dir_v2_old[3];
|
|
|
|
|
|
|
|
|
|
float ed_dir_v1_new[3];
|
|
|
|
|
float ed_dir_v2_new[3];
|
|
|
|
|
|
|
|
|
|
float cross_old[3];
|
|
|
|
|
float cross_new[3];
|
|
|
|
|
|
|
|
|
|
/* original verts - these will be in the edge 'e' */
|
|
|
|
|
BMVert *v1_old, *v2_old;
|
|
|
|
|
|
|
|
|
|
/* verts from the loops passed */
|
|
|
|
|
|
|
|
|
|
BMVert *v1, *v2;
|
|
|
|
|
/* these are the opposite verts - the verts that _would_ be used if 'ccw' was inverted*/
|
|
|
|
|
BMVert *v1_alt, *v2_alt;
|
|
|
|
|
|
|
|
|
|
/* this should have already run */
|
2012-04-19 14:38:09 +00:00
|
|
|
BLI_assert(BM_edge_rotate_check(e) == TRUE);
|
2012-03-05 00:50:18 +00:00
|
|
|
|
|
|
|
|
BM_edge_ordered_verts(e, &v1_old, &v2_old);
|
|
|
|
|
|
|
|
|
|
v1 = l1->v;
|
|
|
|
|
v2 = l2->v;
|
|
|
|
|
|
|
|
|
|
/* get the next vert along */
|
|
|
|
|
v1_alt = BM_face_other_vert_loop(l1->f, v1_old, v1)->v;
|
|
|
|
|
v2_alt = BM_face_other_vert_loop(l2->f, v2_old, v2)->v;
|
|
|
|
|
|
|
|
|
|
/* normalize all so comparisons are scale independent */
|
|
|
|
|
|
|
|
|
|
BLI_assert(BM_edge_exists(v1_old, v1));
|
|
|
|
|
BLI_assert(BM_edge_exists(v1, v1_alt));
|
|
|
|
|
|
|
|
|
|
BLI_assert(BM_edge_exists(v2_old, v2));
|
|
|
|
|
BLI_assert(BM_edge_exists(v2, v2_alt));
|
|
|
|
|
|
|
|
|
|
/* old and new edge vecs */
|
|
|
|
|
sub_v3_v3v3(ed_dir_old, v1_old->co, v2_old->co);
|
|
|
|
|
sub_v3_v3v3(ed_dir_new, v1->co, v2->co);
|
|
|
|
|
normalize_v3(ed_dir_old);
|
|
|
|
|
normalize_v3(ed_dir_new);
|
|
|
|
|
|
|
|
|
|
/* old edge corner vecs */
|
|
|
|
|
sub_v3_v3v3(ed_dir_v1_old, v1_old->co, v1->co);
|
|
|
|
|
sub_v3_v3v3(ed_dir_v2_old, v2_old->co, v2->co);
|
|
|
|
|
normalize_v3(ed_dir_v1_old);
|
|
|
|
|
normalize_v3(ed_dir_v2_old);
|
|
|
|
|
|
|
|
|
|
/* old edge corner vecs */
|
|
|
|
|
sub_v3_v3v3(ed_dir_v1_new, v1->co, v1_alt->co);
|
|
|
|
|
sub_v3_v3v3(ed_dir_v2_new, v2->co, v2_alt->co);
|
|
|
|
|
normalize_v3(ed_dir_v1_new);
|
|
|
|
|
normalize_v3(ed_dir_v2_new);
|
|
|
|
|
|
|
|
|
|
/* compare */
|
|
|
|
|
cross_v3_v3v3(cross_old, ed_dir_old, ed_dir_v1_old);
|
|
|
|
|
cross_v3_v3v3(cross_new, ed_dir_new, ed_dir_v1_new);
|
|
|
|
|
if (dot_v3v3(cross_old, cross_new) < 0.0f) { /* does this flip? */
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
cross_v3_v3v3(cross_old, ed_dir_old, ed_dir_v2_old);
|
|
|
|
|
cross_v3_v3v3(cross_new, ed_dir_new, ed_dir_v2_new);
|
|
|
|
|
if (dot_v3v3(cross_old, cross_new) < 0.0f) { /* does this flip? */
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
negate_v3_v3(ed_dir_new_flip, ed_dir_new);
|
|
|
|
|
|
|
|
|
|
/* result is zero area corner */
|
|
|
|
|
if ((dot_v3v3(ed_dir_new, ed_dir_v1_new) > 0.999f) ||
|
|
|
|
|
(dot_v3v3(ed_dir_new_flip, ed_dir_v2_new) > 0.999f))
|
|
|
|
|
{
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-19 14:38:09 +00:00
|
|
|
int BM_edge_rotate_check_beauty(BMEdge *e,
|
2012-03-05 00:50:18 +00:00
|
|
|
BMLoop *l1, BMLoop *l2)
|
|
|
|
|
{
|
|
|
|
|
/* Stupid check for now:
|
|
|
|
|
* Could compare angles of surrounding edges
|
|
|
|
|
* before & after, but this is OK.*/
|
|
|
|
|
return (len_squared_v3v3(e->v1->co, e->v2->co) >
|
|
|
|
|
len_squared_v3v3(l1->v->co, l2->v->co));
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-29 06:55:10 +00:00
|
|
|
/**
|
|
|
|
|
* \brief Rotate Edge
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* Spins an edge topologically,
|
|
|
|
|
* either counter-clockwise or clockwise depending on \a ccw.
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* \return The spun edge, NULL on error
|
|
|
|
|
* (e.g., if the edge isn't surrounded by exactly two faces).
|
2012-02-19 18:31:04 +00:00
|
|
|
*
|
2012-02-29 06:55:10 +00:00
|
|
|
* \note This works by dissolving the edge then re-creating it,
|
|
|
|
|
* so the returned edge won't have the same pointer address as the original one.
|
2012-03-05 00:50:18 +00:00
|
|
|
*
|
|
|
|
|
* \see header definition for \a check_flag enum.
|
2012-02-19 18:31:04 +00:00
|
|
|
*/
|
2012-03-05 00:50:18 +00:00
|
|
|
BMEdge *BM_edge_rotate(BMesh *bm, BMEdge *e, const short ccw, const short check_flag)
|
2012-02-19 18:31:04 +00:00
|
|
|
{
|
|
|
|
|
BMVert *v1, *v2;
|
2012-03-06 19:29:05 +00:00
|
|
|
BMLoop *l1, *l2;
|
2012-02-19 18:31:04 +00:00
|
|
|
BMFace *f;
|
2012-03-06 19:29:05 +00:00
|
|
|
BMEdge *e_new = NULL;
|
2012-03-15 23:23:04 +00:00
|
|
|
char f_hflag_prev_1;
|
|
|
|
|
char f_hflag_prev_2;
|
2012-02-19 18:31:04 +00:00
|
|
|
|
2012-04-19 14:38:09 +00:00
|
|
|
if (!BM_edge_rotate_check(e)) {
|
2012-02-19 18:31:04 +00:00
|
|
|
return NULL;
|
2012-03-03 12:35:37 +00:00
|
|
|
}
|
2012-02-19 18:31:04 +00:00
|
|
|
|
2012-04-23 01:19:50 +00:00
|
|
|
BM_edge_calc_rotate(e, ccw, &l1, &l2);
|
2012-02-19 18:31:04 +00:00
|
|
|
|
2012-03-05 00:50:18 +00:00
|
|
|
/* the loops will be freed so assign verts */
|
|
|
|
|
v1 = l1->v;
|
|
|
|
|
v2 = l2->v;
|
2012-02-19 18:31:04 +00:00
|
|
|
|
2012-03-05 00:50:18 +00:00
|
|
|
/* --------------------------------------- */
|
|
|
|
|
/* Checking Code - make sure we can rotate */
|
|
|
|
|
|
|
|
|
|
if (check_flag & BM_EDGEROT_CHECK_BEAUTY) {
|
2012-04-19 14:38:09 +00:00
|
|
|
if (!BM_edge_rotate_check_beauty(e, l1, l2)) {
|
2012-03-05 00:50:18 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2012-02-19 18:31:04 +00:00
|
|
|
}
|
2012-03-05 00:50:18 +00:00
|
|
|
|
|
|
|
|
/* check before applying */
|
2012-03-06 19:29:05 +00:00
|
|
|
if (check_flag & BM_EDGEROT_CHECK_EXISTS) {
|
2012-03-05 00:50:18 +00:00
|
|
|
if (BM_edge_exists(v1, v2)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* slowest, check last */
|
|
|
|
|
if (check_flag & BM_EDGEROT_CHECK_DEGENERATE) {
|
2012-04-19 14:38:09 +00:00
|
|
|
if (!BM_edge_rotate_check_degenerate(e, l1, l2)) {
|
2012-03-05 00:50:18 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Done Checking */
|
|
|
|
|
/* ------------- */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-05 01:53:30 +00:00
|
|
|
/* --------------- */
|
|
|
|
|
/* Rotate The Edge */
|
|
|
|
|
|
2012-03-06 19:29:05 +00:00
|
|
|
/* first create the new edge, this is so we can copy the customdata from the old one
|
|
|
|
|
* if splice if disabled, always add in a new edge even if theres one there. */
|
2012-05-03 19:57:24 +00:00
|
|
|
e_new = BM_edge_create(bm, v1, v2, e, (check_flag & BM_EDGEROT_CHECK_SPLICE) != 0);
|
2012-03-06 19:29:05 +00:00
|
|
|
|
2012-03-15 23:23:04 +00:00
|
|
|
f_hflag_prev_1 = l1->f->head.hflag;
|
|
|
|
|
f_hflag_prev_2 = l2->f->head.hflag;
|
|
|
|
|
|
2012-03-05 01:53:30 +00:00
|
|
|
/* don't delete the edge, manually remove the egde after so we can copy its attributes */
|
2012-04-04 14:48:10 +00:00
|
|
|
f = BM_faces_join_pair(bm, l1->f, l2->f, NULL, TRUE);
|
2012-03-05 00:50:18 +00:00
|
|
|
|
|
|
|
|
if (f == NULL) {
|
|
|
|
|
return NULL;
|
2012-02-19 18:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-05 00:50:18 +00:00
|
|
|
/* note, this assumes joining the faces _didnt_ also remove the verts.
|
|
|
|
|
* the #BM_edge_rotate_check will ensure this, but its possibly corrupt state or future edits
|
|
|
|
|
* break this */
|
2012-03-06 19:29:05 +00:00
|
|
|
if (!BM_face_split(bm, f, v1, v2, NULL, NULL, TRUE)) {
|
2012-02-19 18:31:04 +00:00
|
|
|
return NULL;
|
2012-03-05 00:50:18 +00:00
|
|
|
}
|
2012-03-15 23:23:04 +00:00
|
|
|
else {
|
2012-03-18 07:38:51 +00:00
|
|
|
/* we should really be able to know the faces some other way,
|
2012-03-15 23:23:04 +00:00
|
|
|
* rather then fetching them back from the edge, but this is predictable
|
2012-03-18 07:38:51 +00:00
|
|
|
* where using the return values from face split isn't. - campbell */
|
2012-03-15 23:23:04 +00:00
|
|
|
BMFace *fa, *fb;
|
|
|
|
|
if (BM_edge_face_pair(e_new, &fa, &fb)) {
|
|
|
|
|
fa->head.hflag = f_hflag_prev_1;
|
|
|
|
|
fb->head.hflag = f_hflag_prev_2;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-03-05 00:50:18 +00:00
|
|
|
|
2012-03-06 19:29:05 +00:00
|
|
|
return e_new;
|
2012-02-19 18:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-06 16:17:55 +00:00
|
|
|
/**
|
|
|
|
|
* \brief Rip a single face from a vertex fan
|
|
|
|
|
*/
|
2012-03-09 00:41:09 +00:00
|
|
|
BMVert *BM_face_vert_separate(BMesh *bm, BMFace *sf, BMVert *sv)
|
2012-02-19 18:31:04 +00:00
|
|
|
{
|
|
|
|
|
return bmesh_urmv(bm, sf, sv);
|
|
|
|
|
}
|
2012-03-08 20:00:37 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \brief Rip a single face from a vertex fan
|
|
|
|
|
*
|
2012-03-09 00:41:09 +00:00
|
|
|
* \note same as #BM_face_vert_separate but faster (avoids a loop lookup)
|
2012-03-08 20:00:37 +00:00
|
|
|
*/
|
2012-03-09 00:41:09 +00:00
|
|
|
BMVert *BM_face_loop_separate(BMesh *bm, BMLoop *sl)
|
2012-03-08 20:00:37 +00:00
|
|
|
{
|
|
|
|
|
return bmesh_urmv_loop(bm, sl);
|
|
|
|
|
}
|