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

mesh_iterators.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b6afc1f47a8f7bb8d66846b4b86770c82888821 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * 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 bke
 *
 * Functions for iterating mesh features.
 */

#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"

#include "BKE_customdata.h"
#include "BKE_editmesh.h"
#include "BKE_editmesh_cache.h"
#include "BKE_mesh.h"
#include "BKE_mesh_iterators.h"

#include "BLI_bitmap.h"
#include "BLI_math.h"

#include "MEM_guardedalloc.h"

void BKE_mesh_foreach_mapped_vert(Mesh *mesh,
                                  void (*func)(void *userData,
                                               int index,
                                               const float co[3],
                                               const float no_f[3],
                                               const short no_s[3]),
                                  void *userData,
                                  MeshForeachFlag flag)
{
  if (mesh->edit_mesh != NULL) {
    BMEditMesh *em = mesh->edit_mesh;
    BMesh *bm = em->bm;
    BMIter iter;
    BMVert *eve;
    int i;
    if (mesh->runtime.edit_data->vertexCos != NULL) {
      const float(*vertexCos)[3] = mesh->runtime.edit_data->vertexCos;
      const float(*vertexNos)[3];
      if (flag & MESH_FOREACH_USE_NORMAL) {
        BKE_editmesh_cache_ensure_vert_normals(em, mesh->runtime.edit_data);
        vertexNos = mesh->runtime.edit_data->vertexNos;
      }
      else {
        vertexNos = NULL;
      }
      BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
        const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? vertexNos[i] : NULL;
        func(userData, i, vertexCos[i], no, NULL);
      }
    }
    else {
      BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
        const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? eve->no : NULL;
        func(userData, i, eve->co, no, NULL);
      }
    }
  }
  else {
    const MVert *mv = mesh->mvert;
    const int *index = CustomData_get_layer(&mesh->vdata, CD_ORIGINDEX);

    if (index) {
      for (int i = 0; i < mesh->totvert; i++, mv++) {
        const short *no = (flag & MESH_FOREACH_USE_NORMAL) ? mv->no : NULL;
        const int orig = *index++;
        if (orig == ORIGINDEX_NONE) {
          continue;
        }
        func(userData, orig, mv->co, NULL, no);
      }
    }
    else {
      for (int i = 0; i < mesh->totvert; i++, mv++) {
        const short *no = (flag & MESH_FOREACH_USE_NORMAL) ? mv->no : NULL;
        func(userData, i, mv->co, NULL, no);
      }
    }
  }
}

void BKE_mesh_foreach_mapped_edge(
    Mesh *mesh,
    const int tot_edges,
    void (*func)(void *userData, int index, const float v0co[3], const float v1co[3]),
    void *userData)
{
  if (mesh->edit_mesh != NULL) {
    BMEditMesh *em = mesh->edit_mesh;
    BMesh *bm = em->bm;
    BMIter iter;
    BMEdge *eed;
    int i;
    if (mesh->runtime.edit_data->vertexCos != NULL) {
      const float(*vertexCos)[3] = mesh->runtime.edit_data->vertexCos;
      BM_mesh_elem_index_ensure(bm, BM_VERT);

      BM_ITER_MESH_INDEX (eed, &iter, bm, BM_EDGES_OF_MESH, i) {
        func(userData,
             i,
             vertexCos[BM_elem_index_get(eed->v1)],
             vertexCos[BM_elem_index_get(eed->v2)]);
      }
    }
    else {
      BM_ITER_MESH_INDEX (eed, &iter, bm, BM_EDGES_OF_MESH, i) {
        func(userData, i, eed->v1->co, eed->v2->co);
      }
    }
  }
  else {
    const MVert *mv = mesh->mvert;
    const MEdge *med = mesh->medge;
    const int *index = CustomData_get_layer(&mesh->edata, CD_ORIGINDEX);

    if (index) {
      for (int i = 0; i < mesh->totedge; i++, med++) {
        const int orig = *index++;
        if (orig == ORIGINDEX_NONE) {
          continue;
        }
        func(userData, orig, mv[med->v1].co, mv[med->v2].co);
      }
    }
    else if (mesh->totedge == tot_edges) {
      for (int i = 0; i < mesh->totedge; i++, med++) {
        func(userData, i, mv[med->v1].co, mv[med->v2].co);
      }
    }
  }
}

void BKE_mesh_foreach_mapped_loop(Mesh *mesh,
                                  void (*func)(void *userData,
                                               int vertex_index,
                                               int face_index,
                                               const float co[3],
                                               const float no[3]),
                                  void *userData,
                                  MeshForeachFlag flag)
{

  /* We can't use dm->getLoopDataLayout(dm) here,
   * we want to always access dm->loopData, EditDerivedBMesh would
   * return loop data from bmesh itself. */
  if (mesh->edit_mesh != NULL) {
    BMEditMesh *em = mesh->edit_mesh;
    BMesh *bm = em->bm;
    BMIter iter;
    BMFace *efa;

    const float(*vertexCos)[3] = mesh->runtime.edit_data->vertexCos;

    /* XXX: investigate using EditMesh data. */
    const float(*lnors)[3] = (flag & MESH_FOREACH_USE_NORMAL) ?
                                 CustomData_get_layer(&mesh->ldata, CD_NORMAL) :
                                 NULL;

    int f_idx;

    BM_mesh_elem_index_ensure(bm, BM_VERT);

    BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, f_idx) {
      BMLoop *l_iter, *l_first;

      l_iter = l_first = BM_FACE_FIRST_LOOP(efa);
      do {
        const BMVert *eve = l_iter->v;
        const int v_idx = BM_elem_index_get(eve);
        const float *no = lnors ? *lnors++ : NULL;
        func(userData, v_idx, f_idx, vertexCos ? vertexCos[v_idx] : eve->co, no);
      } while ((l_iter = l_iter->next) != l_first);
    }
  }
  else {
    const float(*lnors)[3] = (flag & MESH_FOREACH_USE_NORMAL) ?
                                 CustomData_get_layer(&mesh->ldata, CD_NORMAL) :
                                 NULL;

    const MVert *mv = mesh->mvert;
    const MLoop *ml = mesh->mloop;
    const MPoly *mp = mesh->mpoly;
    const int *v_index = CustomData_get_layer(&mesh->vdata, CD_ORIGINDEX);
    const int *f_index = CustomData_get_layer(&mesh->pdata, CD_ORIGINDEX);
    int p_idx, i;

    if (v_index || f_index) {
      for (p_idx = 0; p_idx < mesh->totpoly; p_idx++, mp++) {
        for (i = 0; i < mp->totloop; i++, ml++) {
          const int v_idx = v_index ? v_index[ml->v] : ml->v;
          const int f_idx = f_index ? f_index[p_idx] : p_idx;
          const float *no = lnors ? *lnors++ : NULL;
          if (ELEM(ORIGINDEX_NONE, v_idx, f_idx)) {
            continue;
          }
          func(userData, v_idx, f_idx, mv[ml->v].co, no);
        }
      }
    }
    else {
      for (p_idx = 0; p_idx < mesh->totpoly; p_idx++, mp++) {
        for (i = 0; i < mp->totloop; i++, ml++) {
          const int v_idx = ml->v;
          const int f_idx = p_idx;
          const float *no = lnors ? *lnors++ : NULL;
          func(userData, v_idx, f_idx, mv[ml->v].co, no);
        }
      }
    }
  }
}

void BKE_mesh_foreach_mapped_face_center(
    Mesh *mesh,
    void (*func)(void *userData, int index, const float cent[3], const float no[3]),
    void *userData,
    MeshForeachFlag flag)
{
  if (mesh->edit_mesh != NULL) {
    BMEditMesh *em = mesh->edit_mesh;
    BMesh *bm = em->bm;
    const float(*polyCos)[3];
    const float(*polyNos)[3];
    BMFace *efa;
    BMIter iter;
    int i;

    BKE_editmesh_cache_ensure_poly_centers(em, mesh->runtime.edit_data);
    polyCos = mesh->runtime.edit_data->polyCos; /* always set */

    if (flag & MESH_FOREACH_USE_NORMAL) {
      BKE_editmesh_cache_ensure_poly_normals(em, mesh->runtime.edit_data);
      polyNos = mesh->runtime.edit_data->polyNos; /* maybe NULL */
    }
    else {
      polyNos = NULL;
    }

    if (polyNos) {
      BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, i) {
        const float *no = polyNos[i];
        func(userData, i, polyCos[i], no);
      }
    }
    else {
      BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, i) {
        const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? efa->no : NULL;
        func(userData, i, polyCos[i], no);
      }
    }
  }
  else {
    const MVert *mvert = mesh->mvert;
    const MPoly *mp = mesh->mpoly;
    const MLoop *ml;
    float _no_buf[3];
    float *no = (flag & MESH_FOREACH_USE_NORMAL) ? _no_buf : NULL;
    const int *index = CustomData_get_layer(&mesh->pdata, CD_ORIGINDEX);

    if (index) {
      for (int i = 0; i < mesh->totpoly; i++, mp++) {
        const int orig = *index++;
        if (orig == ORIGINDEX_NONE) {
          continue;
        }
        float cent[3];
        ml = &mesh->mloop[mp->loopstart];
        BKE_mesh_calc_poly_center(mp, ml, mvert, cent);
        if (flag & MESH_FOREACH_USE_NORMAL) {
          BKE_mesh_calc_poly_normal(mp, ml, mvert, no);
        }
        func(userData, orig, cent, no);
      }
    }
    else {
      for (int i = 0; i < mesh->totpoly; i++, mp++) {
        float cent[3];
        ml = &mesh->mloop[mp->loopstart];
        BKE_mesh_calc_poly_center(mp, ml, mvert, cent);
        if (flag & MESH_FOREACH_USE_NORMAL) {
          BKE_mesh_calc_poly_normal(mp, ml, mvert, no);
        }
        func(userData, i, cent, no);
      }
    }
  }
}

void BKE_mesh_foreach_mapped_subdiv_face_center(
    Mesh *mesh,
    void (*func)(void *userData, int index, const float cent[3], const float no[3]),
    void *userData,
    MeshForeachFlag flag)
{
  const MPoly *mp = mesh->mpoly;
  const MLoop *ml;
  const MVert *mv;
  float _no_buf[3];
  float *no = (flag & MESH_FOREACH_USE_NORMAL) ? _no_buf : NULL;
  const int *index = CustomData_get_layer(&mesh->pdata, CD_ORIGINDEX);

  if (index) {
    for (int i = 0; i < mesh->totpoly; i++, mp++) {
      const int orig = *index++;
      if (orig == ORIGINDEX_NONE) {
        continue;
      }
      ml = &mesh->mloop[mp->loopstart];
      for (int j = 0; j < mp->totloop; j++, ml++) {
        mv = &mesh->mvert[ml->v];
        if (mv->flag & ME_VERT_FACEDOT) {
          if (flag & MESH_FOREACH_USE_NORMAL) {
            normal_short_to_float_v3(no, mv->no);
          }
          func(userData, orig, mv->co, no);
        }
      }
    }
  }
  else {
    for (int i = 0; i < mesh->totpoly; i++, mp++) {
      ml = &mesh->mloop[mp->loopstart];
      for (int j = 0; j < mp->totloop; j++, ml++) {
        mv = &mesh->mvert[ml->v];
        if (mv->flag & ME_VERT_FACEDOT) {
          if (flag & MESH_FOREACH_USE_NORMAL) {
            normal_short_to_float_v3(no, mv->no);
          }
          func(userData, i, mv->co, no);
        }
      }
    }
  }
}

/* Helpers based on above foreach loopers> */

typedef struct MappedVCosData {
  float (*vertexcos)[3];
  BLI_bitmap *vertex_visit;
} MappedVCosData;

static void get_vertexcos__mapFunc(void *user_data,
                                   int index,
                                   const float co[3],
                                   const float UNUSED(no_f[3]),
                                   const short UNUSED(no_s[3]))
{
  MappedVCosData *mapped_vcos_data = (MappedVCosData *)user_data;

  if (BLI_BITMAP_TEST(mapped_vcos_data->vertex_visit, index) == 0) {
    /* We need coord from prototype vertex, not from copies,
     * we assume they stored in the beginning of vertex array stored in evaluated mesh
     * (mirror modifier for eg does this). */
    copy_v3_v3(mapped_vcos_data->vertexcos[index], co);
    BLI_BITMAP_ENABLE(mapped_vcos_data->vertex_visit, index);
  }
}

void BKE_mesh_foreach_mapped_vert_coords_get(Mesh *me_eval, float (*r_cos)[3], const int totcos)
{
  MappedVCosData user_data;
  memset(r_cos, 0, sizeof(*r_cos) * totcos);
  user_data.vertexcos = r_cos;
  user_data.vertex_visit = BLI_BITMAP_NEW(totcos, __func__);
  BKE_mesh_foreach_mapped_vert(me_eval, get_vertexcos__mapFunc, &user_data, MESH_FOREACH_NOP);
  MEM_freeN(user_data.vertex_visit);
}