This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/editors/mesh/editface.c

863 lines
20 KiB
C
Raw Normal View History

/*
* ***** 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,
2012-02-11 04:16:17 +00:00
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Blender Foundation, Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****
*/
2011-02-27 20:29:51 +00:00
/** \file blender/editors/mesh/editface.c
* \ingroup edmesh
*/
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_bitmap.h"
#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "BKE_DerivedMesh.h"
#include "BKE_global.h"
#include "BKE_mesh.h"
#include "BKE_context.h"
#include "BKE_editmesh.h"
#include "BIF_gl.h"
#include "ED_mesh.h"
#include "ED_screen.h"
#include "ED_view3d.h"
#include "WM_api.h"
#include "WM_types.h"
#include "GPU_draw.h"
#include "GPU_buffers.h"
/* own include */
/* copy the face flags, most importantly selection from the mesh to the final derived mesh,
* use in object mode when selecting faces (while painting) */
void paintface_flush_flags(Object *ob, short flag)
{
Mesh *me = BKE_mesh_from_object(ob);
2011-10-14 09:05:20 +00:00
DerivedMesh *dm = ob->derivedFinal;
MPoly *polys, *mp_orig;
const int *index_array = NULL;
int totpoly;
int i;
BLI_assert((flag & ~(SELECT | ME_HIDE)) == 0);
if (me == NULL)
return;
/* note, call #BKE_mesh_flush_hidden_from_verts_ex first when changing hidden flags */
/* we could call this directly in all areas that change selection,
* since this could become slow for realtime updates (circle-select for eg) */
if (flag & SELECT) {
BKE_mesh_flush_select_from_polys(me);
}
if (dm == NULL)
return;
2011-10-14 09:05:20 +00:00
/* Mesh polys => Final derived polys */
2011-10-17 03:06:20 +00:00
if ((index_array = CustomData_get_layer(&dm->polyData, CD_ORIGINDEX))) {
2011-10-14 09:05:20 +00:00
polys = dm->getPolyArray(dm);
totpoly = dm->getNumPolys(dm);
2011-10-14 09:05:20 +00:00
/* loop over final derived polys */
for (i = 0; i < totpoly; i++) {
if (index_array[i] != ORIGINDEX_NONE) {
/* Copy flags onto the final derived poly from the original mesh poly */
mp_orig = me->mpoly + index_array[i];
polys[i].flag = mp_orig->flag;
}
2011-10-14 09:05:20 +00:00
}
}
if (flag & ME_HIDE) {
/* draw-object caches hidden faces, force re-generation T46867 */
GPU_drawobject_free(dm);
}
}
void paintface_hide(Object *ob, const bool unselected)
{
Mesh *me;
MPoly *mpoly;
int a;
me = BKE_mesh_from_object(ob);
if (me == NULL || me->totpoly == 0) return;
mpoly = me->mpoly;
a = me->totpoly;
while (a--) {
if ((mpoly->flag & ME_HIDE) == 0) {
if (((mpoly->flag & ME_FACE_SEL) == 0) == unselected) {
mpoly->flag |= ME_HIDE;
}
}
if (mpoly->flag & ME_HIDE) {
mpoly->flag &= ~ME_FACE_SEL;
}
mpoly++;
}
BKE_mesh_flush_hidden_from_polys(me);
paintface_flush_flags(ob, SELECT | ME_HIDE);
}
void paintface_reveal(Object *ob, const bool select)
{
Mesh *me;
MPoly *mpoly;
int a;
me = BKE_mesh_from_object(ob);
if (me == NULL || me->totpoly == 0) return;
mpoly = me->mpoly;
a = me->totpoly;
while (a--) {
if (mpoly->flag & ME_HIDE) {
SET_FLAG_FROM_TEST(mpoly->flag, select, ME_FACE_SEL);
mpoly->flag &= ~ME_HIDE;
}
mpoly++;
}
BKE_mesh_flush_hidden_from_polys(me);
paintface_flush_flags(ob, SELECT | ME_HIDE);
}
/* Set tface seams based on edge data, uses hash table to find seam edges. */
static void select_linked_tfaces_with_seams(Mesh *me, const unsigned int index, const bool select)
{
MPoly *mp;
MLoop *ml;
int a, b;
bool do_it = true;
bool mark = false;
BLI_bitmap *edge_tag = BLI_BITMAP_NEW(me->totedge, __func__);
BLI_bitmap *poly_tag = BLI_BITMAP_NEW(me->totpoly, __func__);
if (index != (unsigned int)-1) {
/* only put face under cursor in array */
mp = &me->mpoly[index];
BKE_mesh_poly_edgebitmap_insert(edge_tag, mp, me->mloop + mp->loopstart);
BLI_BITMAP_ENABLE(poly_tag, index);
}
else {
/* fill array by selection */
mp = me->mpoly;
for (a = 0; a < me->totpoly; a++, mp++) {
2012-10-07 09:48:59 +00:00
if (mp->flag & ME_HIDE) {
/* pass */
}
else if (mp->flag & ME_FACE_SEL) {
BKE_mesh_poly_edgebitmap_insert(edge_tag, mp, me->mloop + mp->loopstart);
BLI_BITMAP_ENABLE(poly_tag, a);
}
}
}
while (do_it) {
do_it = false;
/* expand selection */
mp = me->mpoly;
for (a = 0; a < me->totpoly; a++, mp++) {
if (mp->flag & ME_HIDE)
continue;
if (!BLI_BITMAP_TEST(poly_tag, a)) {
mark = false;
ml = me->mloop + mp->loopstart;
for (b = 0; b < mp->totloop; b++, ml++) {
if ((me->medge[ml->e].flag & ME_SEAM) == 0) {
if (BLI_BITMAP_TEST(edge_tag, ml->e)) {
mark = true;
break;
}
}
}
if (mark) {
BLI_BITMAP_ENABLE(poly_tag, a);
BKE_mesh_poly_edgebitmap_insert(edge_tag, mp, me->mloop + mp->loopstart);
do_it = true;
}
}
}
}
MEM_freeN(edge_tag);
for (a = 0, mp = me->mpoly; a < me->totpoly; a++, mp++) {
if (BLI_BITMAP_TEST(poly_tag, a)) {
SET_FLAG_FROM_TEST(mp->flag, select, ME_FACE_SEL);
}
}
MEM_freeN(poly_tag);
}
void paintface_select_linked(bContext *C, Object *ob, const int mval[2], const bool select)
{
Mesh *me;
unsigned int index = (unsigned int)-1;
me = BKE_mesh_from_object(ob);
if (me == NULL || me->totpoly == 0) return;
if (mval) {
if (!ED_mesh_pick_face(C, ob, mval, &index, ED_MESH_PICK_DEFAULT_FACE_SIZE)) {
return;
}
}
select_linked_tfaces_with_seams(me, index, select);
paintface_flush_flags(ob, SELECT);
}
void paintface_deselect_all_visible(Object *ob, int action, bool flush_flags)
{
Mesh *me;
MPoly *mpoly;
int a;
me = BKE_mesh_from_object(ob);
if (me == NULL) return;
2013-08-03 16:55:49 +00:00
if (action == SEL_TOGGLE) {
action = SEL_SELECT;
mpoly = me->mpoly;
a = me->totpoly;
while (a--) {
2013-08-03 16:55:49 +00:00
if ((mpoly->flag & ME_HIDE) == 0 && mpoly->flag & ME_FACE_SEL) {
action = SEL_DESELECT;
break;
}
mpoly++;
}
}
2013-08-03 16:55:49 +00:00
mpoly = me->mpoly;
a = me->totpoly;
while (a--) {
if ((mpoly->flag & ME_HIDE) == 0) {
switch (action) {
case SEL_SELECT:
mpoly->flag |= ME_FACE_SEL;
break;
case SEL_DESELECT:
mpoly->flag &= ~ME_FACE_SEL;
break;
case SEL_INVERT:
mpoly->flag ^= ME_FACE_SEL;
break;
}
}
2013-08-03 16:55:49 +00:00
mpoly++;
}
if (flush_flags) {
paintface_flush_flags(ob, SELECT);
}
}
bool paintface_minmax(Object *ob, float r_min[3], float r_max[3])
{
const Mesh *me;
const MPoly *mp;
const MLoop *ml;
const MVert *mvert;
int a, b;
bool ok = false;
float vec[3], bmat[3][3];
me = BKE_mesh_from_object(ob);
if (!me || !me->mloopuv) {
return ok;
}
copy_m3_m4(bmat, ob->obmat);
mvert = me->mvert;
mp = me->mpoly;
for (a = me->totpoly; a > 0; a--, mp++) {
if (mp->flag & ME_HIDE || !(mp->flag & ME_FACE_SEL))
continue;
ml = me->mloop + mp->totloop;
for (b = 0; b < mp->totloop; b++, ml++) {
mul_v3_m3v3(vec, bmat, mvert[ml->v].co);
add_v3_v3v3(vec, vec, ob->obmat[3]);
minmax_v3v3_v3(r_min, r_max, vec);
}
ok = true;
}
return ok;
}
bool paintface_mouse_select(struct bContext *C, Object *ob, const int mval[2], bool extend, bool deselect, bool toggle)
{
Mesh *me;
MPoly *mpoly, *mpoly_sel;
unsigned int a, index;
/* Get the face under the cursor */
me = BKE_mesh_from_object(ob);
if (!ED_mesh_pick_face(C, ob, mval, &index, ED_MESH_PICK_DEFAULT_FACE_SIZE))
return false;
2012-08-11 21:35:24 +00:00
if (index >= me->totpoly)
return false;
mpoly_sel = me->mpoly + index;
if (mpoly_sel->flag & ME_HIDE) return false;
/* clear flags */
mpoly = me->mpoly;
a = me->totpoly;
if (!extend && !deselect && !toggle) {
while (a--) {
mpoly->flag &= ~ME_FACE_SEL;
mpoly++;
}
}
me->act_face = (int)index;
if (extend) {
mpoly_sel->flag |= ME_FACE_SEL;
}
else if (deselect) {
mpoly_sel->flag &= ~ME_FACE_SEL;
}
else if (toggle) {
if (mpoly_sel->flag & ME_FACE_SEL)
mpoly_sel->flag &= ~ME_FACE_SEL;
else
mpoly_sel->flag |= ME_FACE_SEL;
}
else {
mpoly_sel->flag |= ME_FACE_SEL;
}
/* image window redraw */
paintface_flush_flags(ob, SELECT);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
return true;
}
int do_paintface_box_select(ViewContext *vc, rcti *rect, bool select, bool extend)
{
2011-02-27 06:19:40 +00:00
Object *ob = vc->obact;
Mesh *me;
MPoly *mpoly;
struct ImBuf *ibuf;
unsigned int *rt;
char *selar;
2011-02-27 06:19:40 +00:00
int a, index;
const int size[2] = {
BLI_rcti_size_x(rect) + 1,
BLI_rcti_size_y(rect) + 1};
me = BKE_mesh_from_object(ob);
if ((me == NULL) || (me->totpoly == 0) || (size[0] * size[1] <= 0)) {
return OPERATOR_CANCELLED;
}
selar = MEM_callocN(me->totpoly + 1, "selar");
if (extend == false && select) {
paintface_deselect_all_visible(vc->obact, SEL_DESELECT, false);
mpoly = me->mpoly;
for (a = 1; a <= me->totpoly; a++, mpoly++) {
if ((mpoly->flag & ME_HIDE) == 0)
mpoly->flag &= ~ME_FACE_SEL;
}
}
ED_view3d_backbuf_validate(vc);
ibuf = IMB_allocImBuf(size[0], size[1], 32, IB_rect);
rt = ibuf->rect;
view3d_opengl_read_pixels(vc->ar, rect->xmin, rect->ymin, size[0], size[1], GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
if (ENDIAN_ORDER == B_ENDIAN) {
IMB_convert_rgba_to_abgr(ibuf);
}
GPU_select_to_index_array(ibuf->rect, size[0] * size[1]);
a = size[0] * size[1];
while (a--) {
if (*rt) {
index = *rt;
if (index <= me->totpoly) {
selar[index] = 1;
}
}
rt++;
}
mpoly = me->mpoly;
for (a = 1; a <= me->totpoly; a++, mpoly++) {
if (selar[a]) {
2012-10-07 09:48:59 +00:00
if (mpoly->flag & ME_HIDE) {
/* pass */
}
else {
if (select) mpoly->flag |= ME_FACE_SEL;
else mpoly->flag &= ~ME_FACE_SEL;
}
}
}
IMB_freeImBuf(ibuf);
MEM_freeN(selar);
#ifdef __APPLE__
glReadBuffer(GL_BACK);
#endif
paintface_flush_flags(vc->obact, SELECT);
return OPERATOR_FINISHED;
}
/* (similar to void paintface_flush_flags(Object *ob))
* copy the vertex flags, most importantly selection from the mesh to the final derived mesh,
* use in object mode when selecting vertices (while painting) */
void paintvert_flush_flags(Object *ob)
{
Mesh *me = BKE_mesh_from_object(ob);
DerivedMesh *dm = ob->derivedFinal;
MVert *dm_mvert, *dm_mv;
const int *index_array = NULL;
int totvert;
int i;
if (me == NULL)
return;
/* we could call this directly in all areas that change selection,
* since this could become slow for realtime updates (circle-select for eg) */
BKE_mesh_flush_select_from_verts(me);
if (dm == NULL)
return;
index_array = dm->getVertDataArray(dm, CD_ORIGINDEX);
dm_mvert = dm->getVertArray(dm);
totvert = dm->getNumVerts(dm);
dm_mv = dm_mvert;
if (index_array) {
int orig_index;
for (i = 0; i < totvert; i++, dm_mv++) {
orig_index = index_array[i];
if (orig_index != ORIGINDEX_NONE) {
dm_mv->flag = me->mvert[index_array[i]].flag;
}
}
}
else {
for (i = 0; i < totvert; i++, dm_mv++) {
dm_mv->flag = me->mvert[i].flag;
}
}
}
/* note: if the caller passes false to flush_flags, then they will need to run paintvert_flush_flags(ob) themselves */
void paintvert_deselect_all_visible(Object *ob, int action, bool flush_flags)
{
Mesh *me;
MVert *mvert;
int a;
me = BKE_mesh_from_object(ob);
if (me == NULL) return;
2013-08-03 16:55:49 +00:00
if (action == SEL_TOGGLE) {
action = SEL_SELECT;
mvert = me->mvert;
a = me->totvert;
while (a--) {
2013-08-03 16:55:49 +00:00
if ((mvert->flag & ME_HIDE) == 0 && mvert->flag & SELECT) {
action = SEL_DESELECT;
break;
}
mvert++;
}
}
2013-08-03 16:55:49 +00:00
mvert = me->mvert;
a = me->totvert;
while (a--) {
if ((mvert->flag & ME_HIDE) == 0) {
switch (action) {
case SEL_SELECT:
mvert->flag |= SELECT;
break;
case SEL_DESELECT:
mvert->flag &= ~SELECT;
break;
case SEL_INVERT:
mvert->flag ^= SELECT;
break;
}
}
2013-08-03 16:55:49 +00:00
mvert++;
}
/* handle mselect */
if (action == SEL_SELECT) {
/* pass */
}
else if (ELEM(action, SEL_DESELECT, SEL_INVERT)) {
BKE_mesh_mselect_clear(me);
}
else {
BKE_mesh_mselect_validate(me);
}
if (flush_flags) {
paintvert_flush_flags(ob);
}
}
void paintvert_select_ungrouped(Object *ob, bool extend, bool flush_flags)
{
Mesh *me = BKE_mesh_from_object(ob);
MVert *mv;
MDeformVert *dv;
int a, tot;
if (me == NULL || me->dvert == NULL) {
return;
}
if (!extend) {
paintvert_deselect_all_visible(ob, SEL_DESELECT, false);
}
dv = me->dvert;
tot = me->totvert;
for (a = 0, mv = me->mvert; a < tot; a++, mv++, dv++) {
if ((mv->flag & ME_HIDE) == 0) {
if (dv->dw == NULL) {
/* if null weight then not grouped */
mv->flag |= SELECT;
}
}
}
if (flush_flags) {
paintvert_flush_flags(ob);
}
}
/* ********************* MESH VERTEX MIRR TOPO LOOKUP *************** */
/* note, this is not the best place for the function to be but moved
* here for the purpose of syncing with bmesh */
typedef unsigned int MirrTopoHash_t;
typedef struct MirrTopoVert_t {
MirrTopoHash_t hash;
int v_index;
} MirrTopoVert_t;
static int mirrtopo_hash_sort(const void *l1, const void *l2)
{
2012-04-21 12:51:47 +00:00
if ((MirrTopoHash_t)(intptr_t)l1 > (MirrTopoHash_t)(intptr_t)l2) return 1;
else if ((MirrTopoHash_t)(intptr_t)l1 < (MirrTopoHash_t)(intptr_t)l2) return -1;
return 0;
}
static int mirrtopo_vert_sort(const void *v1, const void *v2)
{
if (((MirrTopoVert_t *)v1)->hash > ((MirrTopoVert_t *)v2)->hash) return 1;
else if (((MirrTopoVert_t *)v1)->hash < ((MirrTopoVert_t *)v2)->hash) return -1;
return 0;
}
Fix T47038: Particles in Particle Edit Mode get added in completely wrong location. It also fixes another issue (crash) related to symmetric editing. Quite involved, we (try to!) fix complete broken logic of parts of particle code, which would use poly index as tessface one (or vice-versa). Issue most probably goes back to BMesh integration time... This patch mostly fixes particle editing mode: - Adding/removing particles when using generative modifiers (like subsurf) should now work. - Adding/removing particles with a non-tessellated mesh (i.e. one having ngons) should also mostly work. - X-axis-mirror-editing particles over ngons does not really work, not sure why currently. - All this in both 'modes' (with or without using modifier stack for particles). Tech side: - Store a deformed-only DM in particle modifier data. - Rename existing DM to make it clear it's a final one. - Use deformed-only DM's tessface2poly mapping to 'solve' poly/tessface mismatches. - Make (part of) mirror-editing code able to use a DM instead of raw mesh, so that we can mirror based on final DM when editing particles using modifier stack (mandatory, since there is no way currently to find orig tessface from an final DM tessface index). Note that this patch is not really nice and clean (current particles are beyond hope on this side anyway), it's more like some urgency bandage. Whole crap needs complete rewrite anyway, BMesh's polygons make it really hard to work with current system (and looptri would not help much here). Also, did not test everything possibly affected by those changes, so it needs some users' testing & validation too. Reviewers: psy-fi Subscribers: dfelinto, eyecandy Maniphest Tasks: T47038 Differential Revision: https://developer.blender.org/D1685
2016-01-04 12:19:45 +01:00
bool ED_mesh_mirrtopo_recalc_check(Mesh *me, DerivedMesh *dm, const int ob_mode, MirrTopoStore_t *mesh_topo_store)
{
int totvert;
int totedge;
Fix T47038: Particles in Particle Edit Mode get added in completely wrong location. It also fixes another issue (crash) related to symmetric editing. Quite involved, we (try to!) fix complete broken logic of parts of particle code, which would use poly index as tessface one (or vice-versa). Issue most probably goes back to BMesh integration time... This patch mostly fixes particle editing mode: - Adding/removing particles when using generative modifiers (like subsurf) should now work. - Adding/removing particles with a non-tessellated mesh (i.e. one having ngons) should also mostly work. - X-axis-mirror-editing particles over ngons does not really work, not sure why currently. - All this in both 'modes' (with or without using modifier stack for particles). Tech side: - Store a deformed-only DM in particle modifier data. - Rename existing DM to make it clear it's a final one. - Use deformed-only DM's tessface2poly mapping to 'solve' poly/tessface mismatches. - Make (part of) mirror-editing code able to use a DM instead of raw mesh, so that we can mirror based on final DM when editing particles using modifier stack (mandatory, since there is no way currently to find orig tessface from an final DM tessface index). Note that this patch is not really nice and clean (current particles are beyond hope on this side anyway), it's more like some urgency bandage. Whole crap needs complete rewrite anyway, BMesh's polygons make it really hard to work with current system (and looptri would not help much here). Also, did not test everything possibly affected by those changes, so it needs some users' testing & validation too. Reviewers: psy-fi Subscribers: dfelinto, eyecandy Maniphest Tasks: T47038 Differential Revision: https://developer.blender.org/D1685
2016-01-04 12:19:45 +01:00
if (dm) {
totvert = dm->getNumVerts(dm);
totedge = dm->getNumEdges(dm);
}
else if (me->edit_btmesh) {
totvert = me->edit_btmesh->bm->totvert;
totedge = me->edit_btmesh->bm->totedge;
}
else {
totvert = me->totvert;
totedge = me->totedge;
}
if ((mesh_topo_store->index_lookup == NULL) ||
(mesh_topo_store->prev_ob_mode != ob_mode) ||
(totvert != mesh_topo_store->prev_vert_tot) ||
(totedge != mesh_topo_store->prev_edge_tot))
{
return true;
}
else {
return false;
}
}
Fix T47038: Particles in Particle Edit Mode get added in completely wrong location. It also fixes another issue (crash) related to symmetric editing. Quite involved, we (try to!) fix complete broken logic of parts of particle code, which would use poly index as tessface one (or vice-versa). Issue most probably goes back to BMesh integration time... This patch mostly fixes particle editing mode: - Adding/removing particles when using generative modifiers (like subsurf) should now work. - Adding/removing particles with a non-tessellated mesh (i.e. one having ngons) should also mostly work. - X-axis-mirror-editing particles over ngons does not really work, not sure why currently. - All this in both 'modes' (with or without using modifier stack for particles). Tech side: - Store a deformed-only DM in particle modifier data. - Rename existing DM to make it clear it's a final one. - Use deformed-only DM's tessface2poly mapping to 'solve' poly/tessface mismatches. - Make (part of) mirror-editing code able to use a DM instead of raw mesh, so that we can mirror based on final DM when editing particles using modifier stack (mandatory, since there is no way currently to find orig tessface from an final DM tessface index). Note that this patch is not really nice and clean (current particles are beyond hope on this side anyway), it's more like some urgency bandage. Whole crap needs complete rewrite anyway, BMesh's polygons make it really hard to work with current system (and looptri would not help much here). Also, did not test everything possibly affected by those changes, so it needs some users' testing & validation too. Reviewers: psy-fi Subscribers: dfelinto, eyecandy Maniphest Tasks: T47038 Differential Revision: https://developer.blender.org/D1685
2016-01-04 12:19:45 +01:00
void ED_mesh_mirrtopo_init(Mesh *me, DerivedMesh *dm, const int ob_mode, MirrTopoStore_t *mesh_topo_store,
const bool skip_em_vert_array_init)
{
Fix T47038: Particles in Particle Edit Mode get added in completely wrong location. It also fixes another issue (crash) related to symmetric editing. Quite involved, we (try to!) fix complete broken logic of parts of particle code, which would use poly index as tessface one (or vice-versa). Issue most probably goes back to BMesh integration time... This patch mostly fixes particle editing mode: - Adding/removing particles when using generative modifiers (like subsurf) should now work. - Adding/removing particles with a non-tessellated mesh (i.e. one having ngons) should also mostly work. - X-axis-mirror-editing particles over ngons does not really work, not sure why currently. - All this in both 'modes' (with or without using modifier stack for particles). Tech side: - Store a deformed-only DM in particle modifier data. - Rename existing DM to make it clear it's a final one. - Use deformed-only DM's tessface2poly mapping to 'solve' poly/tessface mismatches. - Make (part of) mirror-editing code able to use a DM instead of raw mesh, so that we can mirror based on final DM when editing particles using modifier stack (mandatory, since there is no way currently to find orig tessface from an final DM tessface index). Note that this patch is not really nice and clean (current particles are beyond hope on this side anyway), it's more like some urgency bandage. Whole crap needs complete rewrite anyway, BMesh's polygons make it really hard to work with current system (and looptri would not help much here). Also, did not test everything possibly affected by those changes, so it needs some users' testing & validation too. Reviewers: psy-fi Subscribers: dfelinto, eyecandy Maniphest Tasks: T47038 Differential Revision: https://developer.blender.org/D1685
2016-01-04 12:19:45 +01:00
MEdge *medge = NULL, *med;
BMEditMesh *em = dm ? NULL : me->edit_btmesh;
/* editmode*/
BMEdge *eed;
BMIter iter;
int a, last;
int totvert, totedge;
int tot_unique = -1, tot_unique_prev = -1;
int tot_unique_edges = 0, tot_unique_edges_prev;
MirrTopoHash_t *topo_hash = NULL;
MirrTopoHash_t *topo_hash_prev = NULL;
MirrTopoVert_t *topo_pairs;
MirrTopoHash_t topo_pass = 1;
intptr_t *index_lookup; /* direct access to mesh_topo_store->index_lookup */
/* reallocate if needed */
ED_mesh_mirrtopo_free(mesh_topo_store);
mesh_topo_store->prev_ob_mode = ob_mode;
if (em) {
BM_mesh_elem_index_ensure(em->bm, BM_VERT);
totvert = em->bm->totvert;
}
else {
Fix T47038: Particles in Particle Edit Mode get added in completely wrong location. It also fixes another issue (crash) related to symmetric editing. Quite involved, we (try to!) fix complete broken logic of parts of particle code, which would use poly index as tessface one (or vice-versa). Issue most probably goes back to BMesh integration time... This patch mostly fixes particle editing mode: - Adding/removing particles when using generative modifiers (like subsurf) should now work. - Adding/removing particles with a non-tessellated mesh (i.e. one having ngons) should also mostly work. - X-axis-mirror-editing particles over ngons does not really work, not sure why currently. - All this in both 'modes' (with or without using modifier stack for particles). Tech side: - Store a deformed-only DM in particle modifier data. - Rename existing DM to make it clear it's a final one. - Use deformed-only DM's tessface2poly mapping to 'solve' poly/tessface mismatches. - Make (part of) mirror-editing code able to use a DM instead of raw mesh, so that we can mirror based on final DM when editing particles using modifier stack (mandatory, since there is no way currently to find orig tessface from an final DM tessface index). Note that this patch is not really nice and clean (current particles are beyond hope on this side anyway), it's more like some urgency bandage. Whole crap needs complete rewrite anyway, BMesh's polygons make it really hard to work with current system (and looptri would not help much here). Also, did not test everything possibly affected by those changes, so it needs some users' testing & validation too. Reviewers: psy-fi Subscribers: dfelinto, eyecandy Maniphest Tasks: T47038 Differential Revision: https://developer.blender.org/D1685
2016-01-04 12:19:45 +01:00
totvert = dm ? dm->getNumVerts(dm) : me->totvert;
}
topo_hash = MEM_callocN(totvert * sizeof(MirrTopoHash_t), "TopoMirr");
/* Initialize the vert-edge-user counts used to detect unique topology */
if (em) {
totedge = me->edit_btmesh->bm->totedge;
BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
const int i1 = BM_elem_index_get(eed->v1), i2 = BM_elem_index_get(eed->v2);
topo_hash[i1]++;
topo_hash[i2]++;
}
}
else {
Fix T47038: Particles in Particle Edit Mode get added in completely wrong location. It also fixes another issue (crash) related to symmetric editing. Quite involved, we (try to!) fix complete broken logic of parts of particle code, which would use poly index as tessface one (or vice-versa). Issue most probably goes back to BMesh integration time... This patch mostly fixes particle editing mode: - Adding/removing particles when using generative modifiers (like subsurf) should now work. - Adding/removing particles with a non-tessellated mesh (i.e. one having ngons) should also mostly work. - X-axis-mirror-editing particles over ngons does not really work, not sure why currently. - All this in both 'modes' (with or without using modifier stack for particles). Tech side: - Store a deformed-only DM in particle modifier data. - Rename existing DM to make it clear it's a final one. - Use deformed-only DM's tessface2poly mapping to 'solve' poly/tessface mismatches. - Make (part of) mirror-editing code able to use a DM instead of raw mesh, so that we can mirror based on final DM when editing particles using modifier stack (mandatory, since there is no way currently to find orig tessface from an final DM tessface index). Note that this patch is not really nice and clean (current particles are beyond hope on this side anyway), it's more like some urgency bandage. Whole crap needs complete rewrite anyway, BMesh's polygons make it really hard to work with current system (and looptri would not help much here). Also, did not test everything possibly affected by those changes, so it needs some users' testing & validation too. Reviewers: psy-fi Subscribers: dfelinto, eyecandy Maniphest Tasks: T47038 Differential Revision: https://developer.blender.org/D1685
2016-01-04 12:19:45 +01:00
totedge = dm ? dm->getNumEdges(dm) : me->totedge;
medge = dm ? dm->getEdgeArray(dm) : me->medge;
Fix T47038: Particles in Particle Edit Mode get added in completely wrong location. It also fixes another issue (crash) related to symmetric editing. Quite involved, we (try to!) fix complete broken logic of parts of particle code, which would use poly index as tessface one (or vice-versa). Issue most probably goes back to BMesh integration time... This patch mostly fixes particle editing mode: - Adding/removing particles when using generative modifiers (like subsurf) should now work. - Adding/removing particles with a non-tessellated mesh (i.e. one having ngons) should also mostly work. - X-axis-mirror-editing particles over ngons does not really work, not sure why currently. - All this in both 'modes' (with or without using modifier stack for particles). Tech side: - Store a deformed-only DM in particle modifier data. - Rename existing DM to make it clear it's a final one. - Use deformed-only DM's tessface2poly mapping to 'solve' poly/tessface mismatches. - Make (part of) mirror-editing code able to use a DM instead of raw mesh, so that we can mirror based on final DM when editing particles using modifier stack (mandatory, since there is no way currently to find orig tessface from an final DM tessface index). Note that this patch is not really nice and clean (current particles are beyond hope on this side anyway), it's more like some urgency bandage. Whole crap needs complete rewrite anyway, BMesh's polygons make it really hard to work with current system (and looptri would not help much here). Also, did not test everything possibly affected by those changes, so it needs some users' testing & validation too. Reviewers: psy-fi Subscribers: dfelinto, eyecandy Maniphest Tasks: T47038 Differential Revision: https://developer.blender.org/D1685
2016-01-04 12:19:45 +01:00
for (a = 0, med = medge; a < totedge; a++, med++) {
const unsigned int i1 = med->v1, i2 = med->v2;
topo_hash[i1]++;
topo_hash[i2]++;
}
}
topo_hash_prev = MEM_dupallocN(topo_hash);
tot_unique_prev = -1;
tot_unique_edges_prev = -1;
while (1) {
/* use the number of edges per vert to give verts unique topology IDs */
tot_unique_edges = 0;
/* This can make really big numbers, wrapping around here is fine */
if (em) {
BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
const int i1 = BM_elem_index_get(eed->v1), i2 = BM_elem_index_get(eed->v2);
topo_hash[i1] += topo_hash_prev[i2] * topo_pass;
topo_hash[i2] += topo_hash_prev[i1] * topo_pass;
tot_unique_edges += (topo_hash[i1] != topo_hash[i2]);
}
}
else {
Fix T47038: Particles in Particle Edit Mode get added in completely wrong location. It also fixes another issue (crash) related to symmetric editing. Quite involved, we (try to!) fix complete broken logic of parts of particle code, which would use poly index as tessface one (or vice-versa). Issue most probably goes back to BMesh integration time... This patch mostly fixes particle editing mode: - Adding/removing particles when using generative modifiers (like subsurf) should now work. - Adding/removing particles with a non-tessellated mesh (i.e. one having ngons) should also mostly work. - X-axis-mirror-editing particles over ngons does not really work, not sure why currently. - All this in both 'modes' (with or without using modifier stack for particles). Tech side: - Store a deformed-only DM in particle modifier data. - Rename existing DM to make it clear it's a final one. - Use deformed-only DM's tessface2poly mapping to 'solve' poly/tessface mismatches. - Make (part of) mirror-editing code able to use a DM instead of raw mesh, so that we can mirror based on final DM when editing particles using modifier stack (mandatory, since there is no way currently to find orig tessface from an final DM tessface index). Note that this patch is not really nice and clean (current particles are beyond hope on this side anyway), it's more like some urgency bandage. Whole crap needs complete rewrite anyway, BMesh's polygons make it really hard to work with current system (and looptri would not help much here). Also, did not test everything possibly affected by those changes, so it needs some users' testing & validation too. Reviewers: psy-fi Subscribers: dfelinto, eyecandy Maniphest Tasks: T47038 Differential Revision: https://developer.blender.org/D1685
2016-01-04 12:19:45 +01:00
for (a = 0, med = medge; a < totedge; a++, med++) {
const unsigned int i1 = med->v1, i2 = med->v2;
topo_hash[i1] += topo_hash_prev[i2] * topo_pass;
topo_hash[i2] += topo_hash_prev[i1] * topo_pass;
tot_unique_edges += (topo_hash[i1] != topo_hash[i2]);
}
}
memcpy(topo_hash_prev, topo_hash, sizeof(MirrTopoHash_t) * totvert);
/* sort so we can count unique values */
qsort(topo_hash_prev, totvert, sizeof(MirrTopoHash_t), mirrtopo_hash_sort);
tot_unique = 1; /* account for skiping the first value */
for (a = 1; a < totvert; a++) {
if (topo_hash_prev[a - 1] != topo_hash_prev[a]) {
tot_unique++;
}
}
if ((tot_unique <= tot_unique_prev) && (tot_unique_edges <= tot_unique_edges_prev)) {
2012-05-20 21:23:26 +00:00
/* Finish searching for unique values when 1 loop dosnt give a
* higher number of unique values compared to the previous loop */
break;
}
else {
tot_unique_prev = tot_unique;
tot_unique_edges_prev = tot_unique_edges;
}
/* Copy the hash calculated this iter, so we can use them next time */
memcpy(topo_hash_prev, topo_hash, sizeof(MirrTopoHash_t) * totvert);
topo_pass++;
}
/* Hash/Index pairs are needed for sorting to find index pairs */
topo_pairs = MEM_callocN(sizeof(MirrTopoVert_t) * totvert, "MirrTopoPairs");
/* since we are looping through verts, initialize these values here too */
index_lookup = MEM_mallocN(totvert * sizeof(*index_lookup), "mesh_topo_lookup");
if (em) {
if (skip_em_vert_array_init == false) {
BM_mesh_elem_table_ensure(em->bm, BM_VERT);
}
}
for (a = 0; a < totvert; a++) {
topo_pairs[a].hash = topo_hash[a];
topo_pairs[a].v_index = a;
/* initialize lookup */
index_lookup[a] = -1;
}
2011-12-21 00:47:59 +00:00
qsort(topo_pairs, totvert, sizeof(MirrTopoVert_t), mirrtopo_vert_sort);
last = 0;
/* Get the pairs out of the sorted hashes, note, totvert+1 means we can use the previous 2,
* but you cant ever access the last 'a' index of MirrTopoPairs */
if (em) {
BMVert **vtable = em->bm->vtable;
for (a = 1; a <= totvert; a++) {
/* printf("I %d %ld %d\n", (a - last), MirrTopoPairs[a].hash, MirrTopoPairs[a].v_indexs); */
if ((a == totvert) || (topo_pairs[a - 1].hash != topo_pairs[a].hash)) {
const int match_count = a - last;
if (match_count == 2) {
const int j = topo_pairs[a - 1].v_index, k = topo_pairs[a - 2].v_index;
index_lookup[j] = (intptr_t)vtable[k];
index_lookup[k] = (intptr_t)vtable[j];
}
else if (match_count == 1) {
/* Center vertex. */
const int j = topo_pairs[a - 1].v_index;
index_lookup[j] = (intptr_t)vtable[j];
}
last = a;
}
}
}
else {
/* same as above, for mesh */
for (a = 1; a <= totvert; a++) {
if ((a == totvert) || (topo_pairs[a - 1].hash != topo_pairs[a].hash)) {
const int match_count = a - last;
if (match_count == 2) {
const int j = topo_pairs[a - 1].v_index, k = topo_pairs[a - 2].v_index;
index_lookup[j] = k;
index_lookup[k] = j;
}
else if (match_count == 1) {
/* Center vertex. */
const int j = topo_pairs[a - 1].v_index;
index_lookup[j] = j;
}
last = a;
}
}
}
MEM_freeN(topo_pairs);
topo_pairs = NULL;
MEM_freeN(topo_hash);
MEM_freeN(topo_hash_prev);
mesh_topo_store->index_lookup = index_lookup;
mesh_topo_store->prev_vert_tot = totvert;
mesh_topo_store->prev_edge_tot = totedge;
}
void ED_mesh_mirrtopo_free(MirrTopoStore_t *mesh_topo_store)
{
if (mesh_topo_store->index_lookup) {
MEM_freeN(mesh_topo_store->index_lookup);
}
mesh_topo_store->index_lookup = NULL;
mesh_topo_store->prev_vert_tot = -1;
mesh_topo_store->prev_edge_tot = -1;
}