Big i18n commit: add "reports" from bmesh/readfile/tracking/dynapaint (and a few...
[blender.git] / source / blender / bmesh / operators / bmo_slide.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Francisco De La Cruz
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/bmesh/operators/bmo_slide.c
24  *  \ingroup bmesh
25  */
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BKE_global.h"
30
31 #include "BLI_math.h"
32
33 #include "bmesh.h"
34 #include "intern/bmesh_operators_private.h" /* own include */
35
36 #define EDGE_MARK 1
37 #define VERT_MARK 2
38
39 /*
40  * Slides a vertex along a connected edge
41  *
42  */
43 void bmo_slide_vert_exec(BMesh *bm, BMOperator *op)
44 {
45         BMOIter oiter;
46         BMIter iter;
47         BMHeader *h;
48         BMVert *vertex;
49         BMEdge *edge;
50         BMEdge *slide_edge;
51
52         /* Selection counts */
53         int selected_edges = 0;
54
55         /* Get slide amount */
56         const float distance_t = BMO_slot_float_get(op, "distance_t");
57
58         /* Get start vertex */
59         vertex = BMO_iter_new(&oiter, bm, op, "vert", BM_VERT);
60
61
62         if (!vertex) {
63                 if (G.debug & G_DEBUG) {
64                         fprintf(stderr, "slide_vert: No vertex selected...");
65                 }
66                 BMO_error_raise(bm, op, BMERR_INVALID_SELECTION, "Vertex Slide error: invalid selection");
67                 return;
68         }
69
70         /* Count selected edges */
71         BMO_ITER (h, &oiter, bm, op, "edge", BM_VERT | BM_EDGE) {
72                 switch (h->htype) {
73                         case BM_EDGE:
74                                 selected_edges++;
75                                 /* Mark all selected edges (cast BMHeader->BMEdge) */
76                                 BMO_elem_flag_enable(bm, (BMElemF *)h, EDGE_MARK);
77                                 break;
78                 }
79         }
80
81         /* Only allow sliding if an edge is selected */
82         if (selected_edges == 0) {
83                 if (G.debug & G_DEBUG) {
84                         fprintf(stderr, "slide_vert: select a single edge\n");
85                 }
86                 BMO_error_raise(bm, op, BMERR_INVALID_SELECTION, "Vertex Slide error: invalid selection");
87                 return;
88         }
89
90         /* Make sure we get the correct edge. */
91         slide_edge = NULL;
92         BM_ITER_ELEM (edge, &iter, vertex, BM_EDGES_OF_VERT) {
93                 if (BMO_elem_flag_test(bm, edge, EDGE_MARK) && BM_vert_in_edge(edge, vertex)) {
94                         slide_edge = edge;
95                         break;
96                 }
97         }
98
99         /* Found edge */
100         if (slide_edge) {
101                 BMVert *other = BM_edge_other_vert(slide_edge, vertex);
102
103                 /* mark */
104                 BMO_elem_flag_enable(bm, vertex, VERT_MARK);
105
106                 /* Interpolate */
107                 interp_v3_v3v3(vertex->co, vertex->co, other->co, distance_t);
108         }
109
110         /* Return the new edge. The same previously marked with VERT_MARK */
111         BMO_slot_buffer_from_enabled_flag(bm, op, "vertout", BM_VERT, VERT_MARK);
112         return;
113 }
114
115 #undef EDGE_MARK
116 #undef VERT_MARK