Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bmo_offset_edgeloops.c « operators « bmesh « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 98a8432cfb1943806dbbabc5e01cc8b777d4b971 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * 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.
 */

/** \file
 * \ingroup bmesh
 *
 * Simple edge offset functionality.
 *
 * \note Actual offset is done by edge-slide.
 * (this only changes topology)
 */

#include "MEM_guardedalloc.h"

#include "BLI_alloca.h"
#include "BLI_math.h"
#include "BLI_utildefines_stack.h"

#include "BKE_customdata.h"

#include "bmesh.h"

#include "intern/bmesh_operators_private.h" /* own include */

#define USE_CAP_OPTION

#define ELE_NEW (1 << 0)

#ifdef USE_CAP_OPTION
#  define ELE_VERT_ENDPOINT (1 << 1)
#endif

/* set for debugging */
#define OFFSET 0.0f

static BMFace *bm_face_split_walk_back(BMesh *bm, BMLoop *l_src, BMLoop **r_l)
{
  float(*cos)[3];
  BMLoop *l_dst;
  BMFace *f;
  int num, i;

  for (l_dst = l_src->prev, num = 0; BM_elem_index_get(l_dst->prev->v) != -1;
       l_dst = l_dst->prev, num++) {
    /* pass */
  }

  BLI_assert(num != 0);

  cos = BLI_array_alloca(cos, num);

  for (l_dst = l_src->prev, i = 0; BM_elem_index_get(l_dst->prev->v) != -1;
       l_dst = l_dst->prev, i++) {
    copy_v3_v3(cos[num - (i + 1)], l_dst->v->co);
  }

  f = BM_face_split_n(bm, l_src->f, l_dst->prev, l_src->next, cos, num, r_l, NULL);

  return f;
}

void bmo_offset_edgeloops_exec(BMesh *bm, BMOperator *op)
{
  const int edges_num = BMO_slot_buffer_count(op->slots_in, "edges");
  BMVert **verts;
  STACK_DECLARE(verts);
  int i;

#ifdef USE_CAP_OPTION
  bool use_cap_endpoint = BMO_slot_bool_get(op->slots_in, "use_cap_endpoint");
  int v_edges_max = 0;
#endif

  BMOIter oiter;

  /* only so we can detect new verts (index == -1) */
  BM_mesh_elem_index_ensure(bm, BM_VERT);

  BM_mesh_elem_hflag_disable_all(bm, BM_VERT | BM_EDGE | BM_FACE, BM_ELEM_TAG, false);

  /* over alloc */
  verts = MEM_mallocN(sizeof(*verts) * (edges_num * 2), __func__);

  STACK_INIT(verts, (edges_num * 2));

  {
    BMEdge *e;
    BMO_ITER (e, &oiter, op->slots_in, "edges", BM_EDGE) {
      int j;

      BM_elem_flag_enable(e, BM_ELEM_TAG);

      for (j = 0; j < 2; j++) {
        BMVert *v_edge = *(&(e->v1) + j);
        if (!BM_elem_flag_test(v_edge, BM_ELEM_TAG)) {
          BM_elem_flag_enable(v_edge, BM_ELEM_TAG);
          STACK_PUSH(verts, v_edge);
        }
      }
    }
  }

  /* -------------------------------------------------------------------- */
  /* Remove verts only used by tagged edges */

  for (i = 0; i < STACK_SIZE(verts); i++) {
    BMIter iter;
    int flag = 0;
    BMEdge *e;

    BM_ITER_ELEM (e, &iter, verts[i], BM_EDGES_OF_VERT) {
      flag |= BM_elem_flag_test(e, BM_ELEM_TAG) ? 1 : 2;
      if (flag == (1 | 2)) {
        break;
      }
    }

    /* only boundary verts are interesting */
    if (flag != (1 | 2)) {
      STACK_REMOVE(verts, i);
    }
  }

  /* possible but unlikely we have no mixed vertices */
  if (UNLIKELY(STACK_SIZE(verts) == 0)) {
    MEM_freeN(verts);
    return;
  }

  /* main loop */
  for (i = 0; i < STACK_SIZE(verts); i++) {
    int v_edges_num = 0;
    int v_edges_num_untag = 0;
    BMVert *v = verts[i];
    BMIter iter;
    BMEdge *e;

    BM_ITER_ELEM (e, &iter, verts[i], BM_EDGES_OF_VERT) {
      if (!BM_elem_flag_test(e, BM_ELEM_TAG)) {
        BMVert *v_other;
        BMIter liter;
        BMLoop *l;

        BM_ITER_ELEM (l, &liter, e, BM_LOOPS_OF_EDGE) {
          BM_elem_flag_enable(l->f, BM_ELEM_TAG);
        }

        v_other = BM_edge_other_vert(e, v);
        BM_edge_split(bm, e, v_other, NULL, 1.0f - OFFSET);
      }
      else {
        v_edges_num_untag += 1;
      }

      v_edges_num += 1;
    }

#ifdef USE_CAP_OPTION
    if (v_edges_num_untag == 1) {
      BMO_vert_flag_enable(bm, v, ELE_VERT_ENDPOINT);
    }

    CLAMP_MIN(v_edges_max, v_edges_num);
#endif
  }

  for (i = 0; i < STACK_SIZE(verts); i++) {
    BMVert *v = verts[i];
    BMIter liter;
    BMLoop *l;

    BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
      if (BM_elem_flag_test(l->f, BM_ELEM_TAG) && (l->f->len != 3)) {
        BMFace *f_cmp = l->f;
        if ((BM_elem_index_get(l->next->v) == -1) && (BM_elem_index_get(l->prev->v) == -1)) {
#ifdef USE_CAP_OPTION
          if (use_cap_endpoint || (BMO_vert_flag_test(bm, v, ELE_VERT_ENDPOINT) == 0))
#endif
          {
            BMLoop *l_new;
            BM_face_split(bm, l->f, l->prev, l->next, &l_new, NULL, true);
            BLI_assert(f_cmp == l->f);
            BLI_assert(f_cmp != l_new->f);
            UNUSED_VARS_NDEBUG(f_cmp);
            BMO_edge_flag_enable(bm, l_new->e, ELE_NEW);
          }
        }
        else if (l->f->len > 4) {
          if (BM_elem_flag_test(l->e, BM_ELEM_TAG) != BM_elem_flag_test(l->prev->e, BM_ELEM_TAG)) {
            if (BM_elem_index_get(l->next->v) == -1) {
              if (BM_elem_index_get(l->prev->prev->v) == -1) {
                BMLoop *l_new;
                BM_face_split(bm, l->f, l->prev->prev, l->next, &l_new, NULL, true);
                BLI_assert(f_cmp == l->f);
                BLI_assert(f_cmp != l_new->f);
                BMO_edge_flag_enable(bm, l_new->e, ELE_NEW);
                BM_elem_flag_disable(l->f, BM_ELEM_TAG);
              }
              else {
                /* walk backwards */
                BMLoop *l_new;
                bm_face_split_walk_back(bm, l, &l_new);
                do {
                  BMO_edge_flag_enable(bm, l_new->e, ELE_NEW);
                  l_new = l_new->next;
                } while (BM_vert_is_edge_pair(l_new->v));
                BM_elem_flag_disable(l->f, BM_ELEM_TAG);
              }
            }

            /* NOTE: instead of duplicate code in alternate direction,
             * we can be sure to hit the other vertex, so the code above runs. */
#if 0
            else if (BM_elem_index_get(l->prev->v) == -1) {
              if (BM_elem_index_get(l->next->next->v) == -1) {
                /* pass */
              }
            }
#endif
          }
        }
      }
    }
  }

#ifdef USE_CAP_OPTION
  if (use_cap_endpoint == false) {
    BMVert **varr = BLI_array_alloca(varr, v_edges_max);
    STACK_DECLARE(varr);
    BMVert *v;

    for (i = 0; i < STACK_SIZE(verts); i++) {
      BMIter iter;
      BMEdge *e;

      v = verts[i];

      STACK_INIT(varr, v_edges_max);

      BM_ITER_ELEM (e, &iter, v, BM_EDGES_OF_VERT) {
        BMVert *v_other;
        v_other = BM_edge_other_vert(e, v);
        if (BM_elem_index_get(v_other) == -1) {
          if (BM_vert_is_edge_pair(v_other)) {
            /* defer bmesh_kernel_join_edge_kill_vert to avoid looping over data we're removing */
            v_other->e = e;
            STACK_PUSH(varr, v_other);
          }
        }
      }

      while ((v = STACK_POP(varr))) {
        bmesh_kernel_join_edge_kill_vert(bm, v->e, v, true, false, false, true);
      }
    }
  }
#endif

  MEM_freeN(verts);

  BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, ELE_NEW);
}