after some discussion, this is the replacement for the old loopcut tool: edge ring...
authorJoseph Eagar <joeedh@gmail.com>
Sun, 13 Sep 2009 16:15:26 +0000 (16:15 +0000)
committerJoseph Eagar <joeedh@gmail.com>
Sun, 13 Sep 2009 16:15:26 +0000 (16:15 +0000)
source/blender/blenkernel/BKE_utildefines.h
source/blender/editors/mesh/editmesh_loop.c
source/blender/editors/mesh/loopcut.c [new file with mode: 0644]
source/blender/editors/mesh/mesh_intern.h
source/blender/editors/mesh/mesh_ops.c

index 4d43518901eab55307032b310e3657f4882e70fc..7d8cb41db825e07710281e1f5c867b7cc0edef43 100644 (file)
 #define SET_INT_IN_POINTER(i) ((void*)(intptr_t)(i))
 #define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i))
 
+/*little array macro library.  example of usage:
+
+int *arr = NULL;
+V_DECLARE(arr);
+int i;
+
+for (i=0; i<10; i++) {
+       V_GROW(arr);
+       arr[i] = something;
+}
+V_FREE(arr);
+
+arrays are buffered, using double-buffering (so on each reallocation,
+the array size is doubled).  supposedly this should give good Big Oh
+behaviour, though it may not be the best in practice.
+*/
+
+#define V_DECLARE(vec) int _##vec##_count=0; void *_##vec##_tmp
+
+/*in the future, I plan on having V_DECLARE allocate stack memory it'll
+  use at first, and switch over to heap when it needs more.  that'll mess
+  up cases where you'd want to use this API to build a dynamic list for
+  non-local use, so all such cases should use this macro.*/
+#define V_DYNDECLARE(vec) V_DECLARE(vec)
+
+/*this returns the entire size of the array, including any buffering.*/
+#define V_SIZE(vec) ((signed int)((vec)==NULL ? 0 : MEM_allocN_len(vec) / sizeof(*vec)))
+
+/*this returns the logical size of the array, not including buffering.*/
+#define V_COUNT(vec) _##vec##_count
+
+/*grow the array by one.  zeroes the new elements.*/
+#define V_GROW(vec) \
+       V_SIZE(vec) > _##vec##_count ? _##vec##_count++ : \
+       ((_##vec##_tmp = MEM_callocN(sizeof(*vec)*(_##vec##_count*2+2), #vec " " __FILE__ " ")),\
+       (vec && memcpy(_##vec##_tmp, vec, sizeof(*vec) * _##vec##_count)),\
+       (vec && (MEM_freeN(vec),1)),\
+       (vec = _##vec##_tmp),\
+       _##vec##_count++)
+
+#define V_FREE(vec) if (vec) MEM_freeN(vec);
+
+/*resets the logical size of an array to zero, but doesn't
+  free the memory.*/
+#define V_RESET(vec) _##vec##_count=0
+
+/*set the count of the array*/
+#define V_SETCOUNT(vec, count) _##vec##_count = (count)
+
 #endif
 
index 4c3e76f2285894bf4616d746f82e6afa1859b9d3..c98b387d28a4a68d2df665d8659e9b09e295729a 100644 (file)
@@ -199,6 +199,7 @@ static void edgering_sel(EditMesh *em, EditEdge *startedge, int select, int prev
                }
        }
 }
+
 void CutEdgeloop(Object *obedit, wmOperator *op, EditMesh *em, int numcuts)
 {
        ViewContext vc; // XXX
diff --git a/source/blender/editors/mesh/loopcut.c b/source/blender/editors/mesh/loopcut.c
new file mode 100644 (file)
index 0000000..c7e10d5
--- /dev/null
@@ -0,0 +1,400 @@
+/**
+ * $Id:
+ *
+ * ***** 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2007 Blender Foundation.
+ * All rights reserved.
+ *
+ * 
+ * Contributor(s): Joseph Eagar, Joshua Leung
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include <float.h>
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdio.h>
+
+#include "DNA_ID.h"
+#include "DNA_screen_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_userdef_types.h"
+#include "DNA_windowmanager_types.h"
+#include "DNA_object_types.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "PIL_time.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_dynstr.h" /*for WM_operator_pystring */
+#include "BLI_editVert.h"
+
+#include "BKE_blender.h"
+#include "BKE_context.h"
+#include "BKE_scene.h"
+#include "BKE_utildefines.h"
+#include "BKE_mesh.h"
+
+#include "BIF_gl.h"
+#include "BIF_glutil.h" /* for paint cursor */
+
+#include "IMB_imbuf_types.h"
+
+#include "ED_screen.h"
+#include "ED_util.h"
+#include "ED_space_api.h"
+#include "ED_view3d.h"
+#include "ED_mesh.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h"
+
+#include "UI_interface.h"
+#include "UI_resources.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include "mesh_intern.h"
+
+/* ringsel operator */
+
+/* struct for properties used while drawing */
+typedef struct tringselOpData {
+       ARegion *ar;            /* region that ringsel was activated in */
+       void *draw_handle;      /* for drawing preview loop */
+       
+       float (*edges)[2][3];
+       int totedge;
+
+       ViewContext vc;
+
+       Object *ob;
+       EditMesh *em;
+       EditEdge *eed;
+
+       int extend;
+} tringselOpData;
+
+/* modal loop selection drawing callback */
+static void ringsel_draw(const bContext *C, ARegion *ar, void *arg)
+{
+       int i;
+       tringselOpData *lcd = arg;
+       
+       glDisable(GL_DEPTH_TEST);
+
+       glPushMatrix();
+       glMultMatrixf(lcd->ob->obmat);
+
+       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();
+
+       glPopMatrix();
+       glEnable(GL_DEPTH_TEST);
+}
+
+static void edgering_sel(tringselOpData *lcd, int previewlines, int select)
+{
+       EditMesh *em = lcd->em;
+       EditEdge *startedge = lcd->eed;
+       EditEdge *eed;
+       EditFace *efa;
+       EditVert *v[2][2];
+       float (*edges)[2][3] = NULL;
+       V_DYNDECLARE(edges);
+       float co[2][3];
+       int looking=1, i, j=0, tot=0;
+       
+       if (!startedge)
+               return;
+
+       if (lcd->edges) {
+               MEM_freeN(lcd->edges);
+               lcd->edges = NULL;
+               lcd->totedge = 0;
+       }
+
+       if (!lcd->extend) {
+               EM_clear_flag_all(lcd->em, SELECT);
+       }
+
+       /* in eed->f1 we put the valence (amount of faces in edge) */
+       /* in eed->f2 we put tagged flag as correct loop */
+       /* in efa->f1 we put tagged flag as correct to select */
+
+       for(eed= em->edges.first; eed; eed= eed->next) {
+               eed->f1= 0;
+               eed->f2= 0;
+       }
+
+       for(efa= em->faces.first; efa; efa= efa->next) {
+               efa->f1= 0;
+               if(efa->h==0) {
+                       efa->e1->f1++;
+                       efa->e2->f1++;
+                       efa->e3->f1++;
+                       if(efa->e4) efa->e4->f1++;
+               }
+       }
+       
+       // tag startedge OK
+       startedge->f2= 1;
+       
+       while(looking) {
+               looking= 0;
+               
+               for(efa= em->faces.first; efa; efa= efa->next) {
+                       if(efa->e4 && efa->f1==0 && efa->h == 0) {      // not done quad
+                               if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
+
+                                       // if edge tagged, select opposing edge and mark face ok
+                                       if(efa->e1->f2) {
+                                               efa->e3->f2= 1;
+                                               efa->f1= 1;
+                                               looking= 1;
+                                       }
+                                       else if(efa->e2->f2) {
+                                               efa->e4->f2= 1;
+                                               efa->f1= 1;
+                                               looking= 1;
+                                       }
+                                       if(efa->e3->f2) {
+                                               efa->e1->f2= 1;
+                                               efa->f1= 1;
+                                               looking= 1;
+                                       }
+                                       if(efa->e4->f2) {
+                                               efa->e2->f2= 1;
+                                               efa->f1= 1;
+                                               looking= 1;
+                                       }
+                               }
+                       }
+               }
+       }
+       
+       if(previewlines > 0 && !select){
+                       for(efa= em->faces.first; efa; efa= efa->next) {
+                               if(efa->v4 == NULL) {  continue; }
+                               if(efa->h == 0){
+                                       if(efa->e1->f2 == 1){
+                                               if(efa->e1->h == 1 || efa->e3->h == 1 )
+                                                       continue;
+                                               
+                                               v[0][0] = efa->v1;
+                                               v[0][1] = efa->v2;
+                                               v[1][0] = efa->v4;
+                                               v[1][1] = efa->v3;
+                                       } else if(efa->e2->f2 == 1){
+                                               if(efa->e2->h == 1 || efa->e4->h == 1)
+                                                       continue;
+                                               v[0][0] = efa->v2;
+                                               v[0][1] = efa->v3;
+                                               v[1][0] = efa->v1;
+                                               v[1][1] = efa->v4;                                      
+                                       } else { continue; }
+                                                                                 
+                                       for(i=1;i<=previewlines;i++){
+                                               co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0])*(i/((float)previewlines+1))+v[0][0]->co[0];
+                                               co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1])*(i/((float)previewlines+1))+v[0][0]->co[1];
+                                               co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2])*(i/((float)previewlines+1))+v[0][0]->co[2];
+
+                                               co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0])*(i/((float)previewlines+1))+v[1][0]->co[0];
+                                               co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1])*(i/((float)previewlines+1))+v[1][0]->co[1];
+                                               co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2])*(i/((float)previewlines+1))+v[1][0]->co[2];                                        
+                                               
+                                               V_GROW(edges);
+                                               VECCOPY(edges[tot][0], co[0]);
+                                               VECCOPY(edges[tot][1], co[1]);
+                                               tot++;
+                                       }
+                               }
+                       }
+       } else {
+               select = (startedge->f & SELECT) == 0;
+
+               /* select the edges */
+               for(eed= em->edges.first; eed; eed= eed->next) {
+                       if(eed->f2) EM_select_edge(eed, select);
+               }
+       }
+
+       lcd->edges = edges;
+       lcd->totedge = tot;
+}
+
+static void ringsel_find_edge(tringselOpData *lcd, const bContext *C, ARegion *ar)
+{
+       if (lcd->eed)
+               edgering_sel(lcd, 1, 0);
+}
+
+static void ringsel_finish(bContext *C, wmOperator *op)
+{
+       tringselOpData *lcd= op->customdata;
+
+       if (lcd->eed);
+               edgering_sel(lcd, 0, 1);
+}
+
+/* called when modal loop selection is done... */
+static void ringsel_exit (bContext *C, wmOperator *op)
+{
+       tringselOpData *lcd= op->customdata;
+
+       /* 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;
+}
+
+/* called when modal loop selection gets set up... */
+static int ringsel_init (bContext *C, wmOperator *op)
+{
+       tringselOpData *lcd;
+       
+       /* alloc new customdata */
+       lcd= op->customdata= MEM_callocN(sizeof(tringselOpData), "ringsel Modal Op Data");
+       
+       /* 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);
+       lcd->ob = CTX_data_edit_object(C);
+       lcd->em= BKE_mesh_get_editmesh((Mesh *)lcd->ob->data);
+       lcd->extend = RNA_boolean_get(op->ptr, "extend");
+       em_setup_viewcontext(C, &lcd->vc);
+
+       ED_region_tag_redraw(lcd->ar);
+
+       return 1;
+}
+
+static int ringsel_cancel (bContext *C, wmOperator *op)
+{
+       /* this is just a wrapper around exit() */
+       ringsel_exit(C, op);
+       return OPERATOR_CANCELLED;
+}
+
+static int ringsel_invoke (bContext *C, wmOperator *op, wmEvent *evt)
+{
+       ScrArea *sa = CTX_wm_area(C);
+       tringselOpData *lcd;
+       EditEdge *edge;
+       int dist = 75;
+
+       view3d_operator_needs_opengl(C);
+
+       if (!ringsel_init(C, op))
+               return OPERATOR_CANCELLED;
+       
+       /* add a modal handler for this operator - handles loop selection */
+       WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
+
+       lcd = op->customdata;
+       lcd->vc.mval[0] = evt->mval[0];
+       lcd->vc.mval[1] = evt->mval[1];
+       
+       edge = findnearestedge(&lcd->vc, &dist);
+       if (edge != lcd->eed) {
+               lcd->eed = edge;
+               ringsel_find_edge(lcd, C, lcd->ar);
+       }
+
+       return OPERATOR_RUNNING_MODAL;
+}
+
+static int ringsel_modal (bContext *C, wmOperator *op, wmEvent *event)
+{
+       tringselOpData *lcd= op->customdata;
+
+       view3d_operator_needs_opengl(C);
+
+       switch (event->type) {
+               case RIGHTMOUSE:
+               case LEFTMOUSE: /* confirm */ // XXX hardcoded
+                       if (event->val == 0) {
+                               /* finish */
+                               ED_region_tag_redraw(lcd->ar);
+
+                               ringsel_finish(C, op);
+                               ringsel_exit(C, op);
+                               
+                               return OPERATOR_FINISHED;
+                       }
+
+                       ED_region_tag_redraw(lcd->ar);
+                       break;
+               case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
+                       int dist = 75;
+                       EditEdge *edge;
+
+                       lcd->vc.mval[0] = event->mval[0];
+                       lcd->vc.mval[1] = event->mval[1];
+                       edge = findnearestedge(&lcd->vc, &dist);
+
+                       if (edge != lcd->eed) {
+                               lcd->eed = edge;
+                               ringsel_find_edge(lcd, C, lcd->ar);
+                       }
+
+                       ED_region_tag_redraw(lcd->ar);
+                       break;
+               }
+       }
+       
+       /* keep going until the user confirms */
+       return OPERATOR_RUNNING_MODAL;
+}
+
+// naming is whatever this should use...
+void MESH_OT_edgering_select (wmOperatorType *ot)
+{
+       /* description */
+       ot->name= "Loop Cut";
+       ot->idname= "MESH_OT_edgering_select";
+       ot->description= "Add a new loop between existing loops.";
+       
+       /* callbacks */
+       ot->invoke= ringsel_invoke;
+       ot->modal= ringsel_modal;
+       ot->cancel= ringsel_cancel;
+       ot->poll= ED_operator_editmesh;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
+
+       RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
+}
index 905a51a1bb0f963aa6260a15bb81f095e62f3c0b..887474414b316447d5fcc35c6d811ba7e49fdda8 100644 (file)
@@ -242,5 +242,7 @@ void MESH_OT_vertex_color_remove(struct wmOperatorType *ot);
 void MESH_OT_sticky_add(struct wmOperatorType *ot);
 void MESH_OT_sticky_remove(struct wmOperatorType *ot);
 
+void MESH_OT_edgering_select(struct wmOperatorType *ot);
+
 #endif // MESH_INTERN_H
 
index c545f7d70d6d22dd25fdc61bd44f9b75af938f67..3cdf1d4781737860b74756a702a15ce6f5480ccd 100644 (file)
@@ -320,6 +320,8 @@ void ED_operatortypes_mesh(void)
        WM_operatortype_append(MESH_OT_face_specials);
        WM_operatortype_append(MESH_OT_specials);
        
+       WM_operatortype_append(MESH_OT_edgering_select);
+
        /* macros */
        ot= WM_operatortype_append_macro("MESH_OT_duplicate_move", "Add Duplicate", OPTYPE_UNDO|OPTYPE_REGISTER);
        WM_operatortype_macro_define(ot, "MESH_OT_duplicate");
@@ -332,7 +334,7 @@ void ED_operatortypes_mesh(void)
        ot= WM_operatortype_append_macro("MESH_OT_extrude_move", "Extrude", OPTYPE_UNDO|OPTYPE_REGISTER);
        WM_operatortype_macro_define(ot, "MESH_OT_extrude");
        WM_operatortype_macro_define(ot, "TFM_OT_translate");
-       
+
 }
 
 /* note mesh keymap also for other space? */
@@ -346,11 +348,10 @@ void ED_keymap_mesh(wmWindowManager *wm)
        WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_ALT, 0);
        kmi= WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_ALT, 0);
        RNA_boolean_set(kmi->ptr, "extend", 1);
-       kmi= WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_ALT|KM_CTRL, 0);
-       RNA_boolean_set(kmi->ptr, "ring", 1);
-       kmi= WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_ALT|KM_CTRL, 0);
+
+       kmi= WM_keymap_add_item(keymap, "MESH_OT_edgering_select", SELECTMOUSE, KM_PRESS, KM_ALT|KM_CTRL, 0);
+       kmi= WM_keymap_add_item(keymap, "MESH_OT_edgering_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_ALT|KM_CTRL, 0);
        RNA_boolean_set(kmi->ptr, "extend", 1);
-       RNA_boolean_set(kmi->ptr, "ring", 1);
 
        WM_keymap_add_item(keymap, "MESH_OT_select_shortest_path", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);