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/editmesh_loopcut.c

623 lines
15 KiB
C
Raw Normal View History

/*
2009-09-16 22:27:22 +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,
2010-02-12 13:34:04 +00:00
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2009-09-16 22:27:22 +00:00
*
* The Original Code is Copyright (C) 2007 Blender Foundation.
* All rights reserved.
*
*
* Contributor(s): Joseph Eagar, Joshua Leung
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/editors/mesh/editmesh_loopcut.c
2011-02-27 20:29:51 +00:00
* \ingroup edmesh
*/
#include "DNA_object_types.h"
2009-09-16 22:27:22 +00:00
#include "MEM_guardedalloc.h"
#include "BLI_array.h"
#include "BLI_string.h"
2012-01-17 18:11:17 +00:00
#include "BLI_math.h"
2009-09-16 22:27:22 +00:00
#include "BLF_translation.h"
2009-09-16 22:27:22 +00:00
#include "BKE_context.h"
#include "BKE_modifier.h"
#include "BKE_report.h"
#include "BKE_editmesh.h"
2009-09-16 22:27:22 +00:00
#include "BIF_gl.h"
#include "ED_screen.h"
#include "ED_space_api.h"
#include "ED_view3d.h"
#include "ED_mesh.h"
#include "ED_numinput.h"
2009-09-16 22:27:22 +00:00
#include "RNA_access.h"
#include "RNA_define.h"
#include "WM_api.h"
#include "WM_types.h"
#include "mesh_intern.h" /* own include */
2009-09-16 22:27:22 +00:00
/* ringsel operator */
/* struct for properties used while drawing */
typedef struct RingSelOpData {
ARegion *ar; /* region that ringsel was activated in */
void *draw_handle; /* for drawing preview loop */
2009-09-16 22:27:22 +00:00
float (*edges)[2][3];
int totedge;
ViewContext vc;
Object *ob;
BMEditMesh *em;
BMEdge *eed;
NumInput num;
2009-09-16 22:27:22 +00:00
bool extend;
bool do_cut;
} RingSelOpData;
2009-09-16 22:27:22 +00:00
/* modal loop selection drawing callback */
static void ringsel_draw(const bContext *C, ARegion *UNUSED(ar), void *arg)
2009-09-16 22:27:22 +00:00
{
View3D *v3d = CTX_wm_view3d(C);
RingSelOpData *lcd = arg;
int i;
2009-09-16 22:27:22 +00:00
if (lcd->totedge > 0) {
if (v3d && v3d->zbuf)
glDisable(GL_DEPTH_TEST);
2009-09-16 22:27:22 +00:00
glPushMatrix();
glMultMatrixf(lcd->ob->obmat);
2009-09-16 22:27:22 +00:00
glColor3ub(255, 0, 255);
glBegin(GL_LINES);
for (i = 0; i < lcd->totedge; i++) {
glVertex3fv(lcd->edges[i][0]);
glVertex3fv(lcd->edges[i][1]);
}
glEnd();
2009-09-16 22:27:22 +00:00
glPopMatrix();
if (v3d && v3d->zbuf)
glEnable(GL_DEPTH_TEST);
}
2009-09-16 22:27:22 +00:00
}
/* given two opposite edges in a face, finds the ordering of their vertices so
* that cut preview lines won't cross each other */
static void edgering_find_order(BMEdge *lasteed, BMEdge *eed,
BMVert *lastv1, BMVert *v[2][2])
{
BMIter liter;
BMLoop *l, *l2;
int rev;
l = eed->l;
/* find correct order for v[1] */
if (!(BM_edge_in_face(l->f, eed) && BM_edge_in_face(l->f, lasteed))) {
BM_ITER_ELEM (l, &liter, l, BM_LOOPS_OF_LOOP) {
if (BM_edge_in_face(l->f, eed) && BM_edge_in_face(l->f, lasteed))
break;
}
}
/* this should never happen */
if (!l) {
v[0][0] = eed->v1;
v[0][1] = eed->v2;
v[1][0] = lasteed->v1;
v[1][1] = lasteed->v2;
return;
}
l2 = BM_loop_other_edge_loop(l, eed->v1);
2011-09-12 15:10:59 +00:00
rev = (l2 == l->prev);
while (l2->v != lasteed->v1 && l2->v != lasteed->v2) {
2011-09-12 15:10:59 +00:00
l2 = rev ? l2->prev : l2->next;
}
if (l2->v == lastv1) {
v[0][0] = eed->v1;
v[0][1] = eed->v2;
}
else {
v[0][0] = eed->v2;
v[0][1] = eed->v1;
}
}
static void edgering_sel(RingSelOpData *lcd, int previewlines, int select)
2009-09-16 22:27:22 +00:00
{
BMEditMesh *em = lcd->em;
BMEdge *eed_start = lcd->eed;
BMEdge *eed, *eed_last;
BMVert *v[2][2], *v_last;
2009-09-16 22:27:22 +00:00
BMWalker walker;
float (*edges)[2][3] = NULL;
BLI_array_declare(edges);
int i, tot = 0;
2009-09-16 22:27:22 +00:00
memset(v, 0, sizeof(v));
if (!eed_start)
2009-09-16 22:27:22 +00:00
return;
if (lcd->edges) {
MEM_freeN(lcd->edges);
lcd->edges = NULL;
lcd->totedge = 0;
}
if (!lcd->extend) {
EDBM_flag_disable_all(lcd->em, BM_ELEM_SELECT);
2009-09-16 22:27:22 +00:00
}
if (select) {
BMW_init(&walker, em->bm, BMW_EDGERING,
BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
BMW_FLAG_TEST_HIDDEN,
BMW_NIL_LAY);
for (eed = BMW_begin(&walker, eed_start); eed; eed = BMW_step(&walker)) {
BM_edge_select_set(em->bm, eed, true);
2009-09-16 22:27:22 +00:00
}
BMW_end(&walker);
2009-09-16 22:27:22 +00:00
return;
}
BMW_init(&walker, em->bm, BMW_EDGERING,
BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
BMW_FLAG_TEST_HIDDEN,
BMW_NIL_LAY);
v_last = NULL;
eed_last = NULL;
for (eed = eed_start = BMW_begin(&walker, eed_start); eed; eed = BMW_step(&walker)) {
if (eed_last) {
if (v_last) {
v[1][0] = v[0][0];
v[1][1] = v[0][1];
}
else {
v[1][0] = eed_last->v1;
v[1][1] = eed_last->v2;
v_last = eed_last->v1;
2009-09-16 22:27:22 +00:00
}
edgering_find_order(eed_last, eed, v_last, v);
v_last = v[0][0];
BLI_array_grow_items(edges, previewlines);
for (i = 1; i <= previewlines; i++) {
2012-12-19 04:59:47 +00:00
const float fac = (i / ((float)previewlines + 1));
interp_v3_v3v3(edges[tot][0], v[0][0]->co, v[0][1]->co, fac);
interp_v3_v3v3(edges[tot][1], v[1][0]->co, v[1][1]->co, fac);
2009-09-16 22:27:22 +00:00
tot++;
}
}
eed_last = eed;
2009-09-16 22:27:22 +00:00
}
#ifdef BMW_EDGERING_NGON
if (lasteed != startedge && BM_edge_share_face_check(lasteed, startedge)) {
#else
if (eed_last != eed_start && BM_edge_share_quad_check(eed_last, eed_start)) {
#endif
v[1][0] = v[0][0];
v[1][1] = v[0][1];
edgering_find_order(eed_last, eed_start, v_last, v);
BLI_array_grow_items(edges, previewlines);
for (i = 1; i <= previewlines; i++) {
2012-12-19 04:59:47 +00:00
const float fac = (i / ((float)previewlines + 1));
if (!v[0][0] || !v[0][1] || !v[1][0] || !v[1][1]) {
continue;
2012-12-19 04:59:47 +00:00
}
interp_v3_v3v3(edges[tot][0], v[0][0]->co, v[0][1]->co, fac);
interp_v3_v3v3(edges[tot][1], v[1][0]->co, v[1][1]->co, fac);
2009-09-16 22:27:22 +00:00
tot++;
}
}
BMW_end(&walker);
2009-09-16 22:27:22 +00:00
lcd->edges = edges;
lcd->totedge = tot;
}
static void ringsel_find_edge(RingSelOpData *lcd, int cuts)
2009-09-16 22:27:22 +00:00
{
if (lcd->eed) {
edgering_sel(lcd, cuts, 0);
}
else if (lcd->edges) {
MEM_freeN(lcd->edges);
lcd->edges = NULL;
lcd->totedge = 0;
}
2009-09-16 22:27:22 +00:00
}
static void ringsel_finish(bContext *C, wmOperator *op)
{
RingSelOpData *lcd = op->customdata;
const int cuts = RNA_int_get(op->ptr, "number_cuts");
const float smoothness = 0.292f * RNA_float_get(op->ptr, "smoothness");
#ifdef BMW_EDGERING_NGON
const bool use_only_quads = false;
#else
const bool use_only_quads = false;
#endif
2009-09-16 22:27:22 +00:00
if (lcd->eed) {
2011-02-27 06:19:40 +00:00
BMEditMesh *em = lcd->em;
edgering_sel(lcd, cuts, 1);
2009-09-16 22:27:22 +00:00
if (lcd->do_cut) {
/* Enable gridfill, so that intersecting loopcut works as one would expect.
* Note though that it will break edgeslide in this specific case.
* See [#31939]. */
BM_mesh_esubdivide(em->bm, BM_ELEM_SELECT,
smoothness, 0.0f, 0.0f,
cuts,
SUBDIV_SELECT_LOOPCUT, SUBD_PATH, 0, true,
use_only_quads, 0);
2012-12-12 15:01:27 +00:00
/* tessface is already re-recalculated */
EDBM_update_generic(em, false, true);
2012-12-12 15:01:27 +00:00
/* we cant slide multiple edges in vertex select mode */
if ((cuts > 1) && (em->selectmode & SCE_SELECT_VERTEX)) {
/* dont flush vertex selection when we have multiple cuts, otherwise we can't slide */
}
/* force edge slide to edge select mode in in face select mode */
else if (EDBM_selectmode_disable(lcd->vc.scene, em, SCE_SELECT_FACE, SCE_SELECT_EDGE)) {
/* pass, the change will flush selection */
}
2012-12-12 15:01:27 +00:00
else {
/* else flush explicitly */
EDBM_selectmode_flush(lcd->em);
2012-12-12 15:01:27 +00:00
}
}
else {
/* XXX Is this piece of code ever used now? Simple loop select is now
* in editmesh_select.c (around line 1000)... */
/* sets as active, useful for other tools */
if (em->selectmode & SCE_SELECT_VERTEX)
BM_select_history_store(em->bm, lcd->eed->v1); /* low priority TODO, get vertrex close to mouse */
if (em->selectmode & SCE_SELECT_EDGE)
BM_select_history_store(em->bm, lcd->eed);
EDBM_selectmode_flush(lcd->em);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, lcd->ob->data);
2009-09-16 22:27:22 +00:00
}
}
}
/* called when modal loop selection is done... */
static void ringsel_exit(bContext *UNUSED(C), wmOperator *op)
2009-09-16 22:27:22 +00:00
{
RingSelOpData *lcd = op->customdata;
2009-09-16 22:27:22 +00:00
/* deactivate the extra drawing stuff in 3D-View */
ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
if (lcd->edges)
MEM_freeN(lcd->edges);
ED_region_tag_redraw(lcd->ar);
/* free the custom data */
MEM_freeN(lcd);
op->customdata = NULL;
2009-09-16 22:27:22 +00:00
}
2009-09-16 22:27:22 +00:00
/* called when modal loop selection gets set up... */
static int ringsel_init(bContext *C, wmOperator *op, bool do_cut)
2009-09-16 22:27:22 +00:00
{
RingSelOpData *lcd;
2009-09-16 22:27:22 +00:00
/* alloc new customdata */
lcd = op->customdata = MEM_callocN(sizeof(RingSelOpData), "ringsel Modal Op Data");
2009-09-16 22:27:22 +00:00
/* assign the drawing handle for drawing preview line... */
lcd->ar = CTX_wm_region(C);
lcd->draw_handle = ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
2009-09-16 22:27:22 +00:00
lcd->ob = CTX_data_edit_object(C);
lcd->em = BKE_editmesh_from_object(lcd->ob);
2009-09-16 22:27:22 +00:00
lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
lcd->do_cut = do_cut;
initNumInput(&lcd->num);
lcd->num.idx_max = 0;
lcd->num.flag |= NUM_NO_NEGATIVE | NUM_NO_FRACTION;
/* XXX, temp, workaround for [# ] */
EDBM_mesh_ensure_valid_dm_hack(CTX_data_scene(C), lcd->em);
2009-09-16 22:27:22 +00:00
em_setup_viewcontext(C, &lcd->vc);
ED_region_tag_redraw(lcd->ar);
return 1;
}
static int ringcut_cancel(bContext *C, wmOperator *op)
2009-09-16 22:27:22 +00:00
{
/* this is just a wrapper around exit() */
ringsel_exit(C, op);
return OPERATOR_CANCELLED;
}
/* called by both init() and exec() */
static int loopcut_init(bContext *C, wmOperator *op, const bool is_interactive)
2009-09-16 22:27:22 +00:00
{
Object *obedit = CTX_data_edit_object(C);
RingSelOpData *lcd;
2009-09-16 22:27:22 +00:00
BMEdge *edge;
float dist = 75.0f;
2009-09-16 22:27:22 +00:00
if (modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
BKE_report(op->reports, RPT_WARNING, "Loop cut does not work well on deformed edit mesh display");
2009-09-16 22:27:22 +00:00
view3d_operator_needs_opengl(C);
if (!ringsel_init(C, op, 1))
return OPERATOR_CANCELLED;
2009-09-16 22:27:22 +00:00
/* add a modal handler for this operator - handles loop selection */
if (is_interactive) {
WM_event_add_modal_handler(C, op);
}
2009-09-16 22:27:22 +00:00
lcd = op->customdata;
RNA_int_get_array(op->ptr, "location", lcd->vc.mval);
edge = EDBM_edge_find_nearest(&lcd->vc, &dist);
2009-09-16 22:27:22 +00:00
if (edge != lcd->eed) {
lcd->eed = edge;
ringsel_find_edge(lcd, 1);
2009-09-16 22:27:22 +00:00
}
if (is_interactive) {
ScrArea *sa = CTX_wm_area(C);
ED_area_headerprint(sa, IFACE_("Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts, "
"hold Alt for smooth"));
return OPERATOR_RUNNING_MODAL;
}
else {
ringsel_finish(C, op);
ringsel_exit(C, op);
return OPERATOR_FINISHED;
}
}
static int ringcut_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
RNA_int_set_array(op->ptr, "location", event->mval);
return loopcut_init(C, op, true);
}
static int loopcut_exec(bContext *C, wmOperator *op)
{
return loopcut_init(C, op, false);
2009-09-16 22:27:22 +00:00
}
static int loopcut_modal(bContext *C, wmOperator *op, const wmEvent *event)
2009-09-16 22:27:22 +00:00
{
float smoothness = RNA_float_get(op->ptr, "smoothness");
int cuts = RNA_int_get(op->ptr, "number_cuts");
RingSelOpData *lcd = op->customdata;
bool show_cuts = false;
2009-09-16 22:27:22 +00:00
view3d_operator_needs_opengl(C);
switch (event->type) {
case RETKEY:
case PADENTER:
case LEFTMOUSE: /* confirm */ // XXX hardcoded
if (event->val == KM_PRESS) {
/* finish */
ED_region_tag_redraw(lcd->ar);
ringsel_finish(C, op);
ringsel_exit(C, op);
ED_area_headerprint(CTX_wm_area(C), NULL);
return OPERATOR_FINISHED;
}
ED_region_tag_redraw(lcd->ar);
break;
case RIGHTMOUSE: /* abort */ // XXX hardcoded
ED_region_tag_redraw(lcd->ar);
ringsel_exit(C, op);
ED_area_headerprint(CTX_wm_area(C), NULL);
return OPERATOR_FINISHED;
case ESCKEY:
if (event->val == KM_RELEASE) {
/* cancel */
ED_region_tag_redraw(lcd->ar);
ED_area_headerprint(CTX_wm_area(C), NULL);
2011-02-27 06:19:40 +00:00
return ringcut_cancel(C, op);
}
ED_region_tag_redraw(lcd->ar);
break;
case PADPLUSKEY:
case PAGEUPKEY:
case WHEELUPMOUSE: /* change number of cuts */
if (event->val == KM_RELEASE)
break;
if (event->alt == 0) {
cuts++;
RNA_int_set(op->ptr, "number_cuts", cuts);
ringsel_find_edge(lcd, cuts);
show_cuts = true;
}
else {
smoothness = min_ff(smoothness + 0.05f, 4.0f);
RNA_float_set(op->ptr, "smoothness", smoothness);
show_cuts = true;
}
ED_region_tag_redraw(lcd->ar);
break;
case PADMINUS:
case PAGEDOWNKEY:
case WHEELDOWNMOUSE: /* change number of cuts */
if (event->val == KM_RELEASE)
break;
if (event->alt == 0) {
cuts = max_ii(cuts - 1, 0);
RNA_int_set(op->ptr, "number_cuts", cuts);
ringsel_find_edge(lcd, cuts);
show_cuts = true;
}
else {
smoothness = max_ff(smoothness - 0.05f, 0.0f);
RNA_float_set(op->ptr, "smoothness", smoothness);
show_cuts = true;
}
2009-09-16 22:27:22 +00:00
ED_region_tag_redraw(lcd->ar);
break;
2013-01-15 23:45:41 +00:00
case MOUSEMOVE: /* mouse moved somewhere to select another loop */
{
float dist = 75.0f;
2009-09-16 22:27:22 +00:00
BMEdge *edge;
lcd->vc.mval[0] = event->mval[0];
lcd->vc.mval[1] = event->mval[1];
edge = EDBM_edge_find_nearest(&lcd->vc, &dist);
2009-09-16 22:27:22 +00:00
if (edge != lcd->eed) {
lcd->eed = edge;
ringsel_find_edge(lcd, cuts);
2009-09-16 22:27:22 +00:00
}
ED_region_tag_redraw(lcd->ar);
break;
}
2009-09-16 22:27:22 +00:00
}
/* using the keyboard to input the number of cuts */
if (event->val == KM_PRESS) {
/* init as zero so backspace clears */
if (handleNumInput(&lcd->num, event)) {
float value = RNA_int_get(op->ptr, "number_cuts");
applyNumInput(&lcd->num, &value);
/* allow zero so you can backspace and type in a value
* otherwise 1 as minimum would make more sense */
cuts = CLAMPIS(value, 0, 130);
RNA_int_set(op->ptr, "number_cuts", cuts);
ringsel_find_edge(lcd, cuts);
show_cuts = true;
ED_region_tag_redraw(lcd->ar);
}
}
if (show_cuts) {
char buf[64];
BLI_snprintf(buf, sizeof(buf), IFACE_("Number of Cuts: %d, Smooth: %.2f (Alt)"), cuts, smoothness);
ED_area_headerprint(CTX_wm_area(C), buf);
}
2009-09-16 22:27:22 +00:00
/* keep going until the user confirms */
return OPERATOR_RUNNING_MODAL;
}
/* for bmesh this tool is in bmesh_select.c */
#if 0
void MESH_OT_edgering_select(wmOperatorType *ot)
{
/* description */
ot->name = "Edge Ring Select";
ot->idname = "MESH_OT_edgering_select";
ot->description = "Select an edge ring";
/* callbacks */
ot->invoke = ringsel_invoke;
ot->poll = ED_operator_editmesh_region_view3d;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
}
#endif
void MESH_OT_loopcut(wmOperatorType *ot)
2009-09-16 22:27:22 +00:00
{
PropertyRNA *prop;
2009-09-16 22:27:22 +00:00
/* description */
ot->name = "Loop Cut";
ot->idname = "MESH_OT_loopcut";
ot->description = "Add a new loop between existing loops";
2009-09-16 22:27:22 +00:00
/* callbacks */
ot->invoke = ringcut_invoke;
ot->exec = loopcut_exec;
ot->modal = loopcut_modal;
ot->cancel = ringcut_cancel;
ot->poll = ED_operator_editmesh_region_view3d;
2009-09-16 22:27:22 +00:00
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_BLOCKING;
/* properties */
prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
/* avoid re-using last var because it can cause _very_ high poly meshes and annoy users (or worse crash) */
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
prop = RNA_def_float(ot->srna, "smoothness", 0.0f, 0.0f, FLT_MAX, "Smoothness", "Smoothness factor", 0.0f, 4.0f);
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
RNA_def_int_vector(ot->srna, "location", 2, NULL, 0, INT_MAX, "Location", "", 0, 16384);
2009-09-16 22:27:22 +00:00
}