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

object_facemap.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c44ec2b510ee7cf4c2a27797376f1a85582fd436 (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
/*
 * 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.
 *
 * The Original Code is Copyright (C) 2008 Blender Foundation.
 * All rights reserved.
 */

/** \file
 * \ingroup bke
 */

#include <string.h>

#include "DNA_mesh_types.h"
#include "DNA_object_types.h"

#include "BLI_listbase.h"
#include "BLI_string.h"
#include "BLI_string_utils.h"
#include "BLI_utildefines.h"

#include "BKE_customdata.h"
#include "BKE_editmesh.h"
#include "BKE_object.h"
#include "BKE_object_deform.h"
#include "BKE_object_facemap.h" /* own include */

#include "BLT_translation.h"

#include "MEM_guardedalloc.h"

#include "RNA_access.h"
#include "RNA_define.h"

static bool fmap_unique_check(void *arg, const char *name)
{
  struct {
    Object *ob;
    void *fm;
  } *data = arg;

  bFaceMap *fmap;

  for (fmap = data->ob->fmaps.first; fmap; fmap = fmap->next) {
    if (data->fm != fmap) {
      if (STREQ(fmap->name, name)) {
        return true;
      }
    }
  }

  return false;
}

static bFaceMap *fmap_duplicate(bFaceMap *infmap)
{
  bFaceMap *outfmap;

  if (!infmap) {
    return NULL;
  }

  outfmap = MEM_callocN(sizeof(bFaceMap), "copy facemap");

  /* For now, just copy everything over. */
  memcpy(outfmap, infmap, sizeof(bFaceMap));

  outfmap->next = outfmap->prev = NULL;

  return outfmap;
}

void BKE_object_facemap_copy_list(ListBase *outbase, const ListBase *inbase)
{
  bFaceMap *fmap, *fmapn;

  BLI_listbase_clear(outbase);

  for (fmap = inbase->first; fmap; fmap = fmap->next) {
    fmapn = fmap_duplicate(fmap);
    BLI_addtail(outbase, fmapn);
  }
}

void BKE_object_facemap_unique_name(Object *ob, bFaceMap *fmap)
{
  struct {
    Object *ob;
    void *fmap;
  } data;
  data.ob = ob;
  data.fmap = fmap;

  BLI_uniquename_cb(fmap_unique_check, &data, DATA_("Group"), '.', fmap->name, sizeof(fmap->name));
}

bFaceMap *BKE_object_facemap_add_name(Object *ob, const char *name)
{
  bFaceMap *fmap;

  if (!ob || ob->type != OB_MESH) {
    return NULL;
  }

  fmap = MEM_callocN(sizeof(bFaceMap), __func__);

  BLI_strncpy(fmap->name, name, sizeof(fmap->name));

  BLI_addtail(&ob->fmaps, fmap);

  ob->actfmap = BLI_listbase_count(&ob->fmaps);

  BKE_object_facemap_unique_name(ob, fmap);

  return fmap;
}

bFaceMap *BKE_object_facemap_add(Object *ob)
{
  return BKE_object_facemap_add_name(ob, DATA_("FaceMap"));
}

static void object_fmap_remove_edit_mode(Object *ob, bFaceMap *fmap, bool do_selected, bool purge)
{
  const int fmap_nr = BLI_findindex(&ob->fmaps, fmap);

  if (ob->type == OB_MESH) {
    Mesh *me = ob->data;

    if (me->edit_mesh) {
      BMEditMesh *em = me->edit_mesh;
      const int cd_fmap_offset = CustomData_get_offset(&em->bm->pdata, CD_FACEMAP);

      if (cd_fmap_offset != -1) {
        BMFace *efa;
        BMIter iter;
        int *map;

        if (purge) {
          BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
            map = BM_ELEM_CD_GET_VOID_P(efa, cd_fmap_offset);

            if (map) {
              if (*map == fmap_nr) {
                *map = -1;
              }
              else if (*map > fmap_nr) {
                *map -= 1;
              }
            }
          }
        }
        else {
          BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
            map = BM_ELEM_CD_GET_VOID_P(efa, cd_fmap_offset);

            if (map && *map == fmap_nr &&
                (!do_selected || BM_elem_flag_test(efa, BM_ELEM_SELECT))) {
              *map = -1;
            }
          }
        }
      }

      if (ob->actfmap == BLI_listbase_count(&ob->fmaps)) {
        ob->actfmap--;
      }

      BLI_remlink(&ob->fmaps, fmap);
      MEM_freeN(fmap);
    }
  }
}

static void object_fmap_remove_object_mode(Object *ob, bFaceMap *fmap, bool purge)
{
  const int fmap_nr = BLI_findindex(&ob->fmaps, fmap);

  if (ob->type == OB_MESH) {
    Mesh *me = ob->data;

    if (CustomData_has_layer(&me->pdata, CD_FACEMAP)) {
      int *map = CustomData_get_layer(&me->pdata, CD_FACEMAP);
      int i;

      if (map) {
        for (i = 0; i < me->totpoly; i++) {
          if (map[i] == fmap_nr) {
            map[i] = -1;
          }
          else if (purge && map[i] > fmap_nr) {
            map[i]--;
          }
        }
      }
    }

    if (ob->actfmap == BLI_listbase_count(&ob->fmaps)) {
      ob->actfmap--;
    }

    BLI_remlink(&ob->fmaps, fmap);
    MEM_freeN(fmap);
  }
}

static void fmap_remove_exec(Object *ob, bFaceMap *fmap, const bool is_edit_mode, const bool purge)
{
  if (is_edit_mode) {
    object_fmap_remove_edit_mode(ob, fmap, false, purge);
  }
  else {
    object_fmap_remove_object_mode(ob, fmap, purge);
  }
}

void BKE_object_facemap_remove(Object *ob, bFaceMap *fmap)
{
  fmap_remove_exec(ob, fmap, BKE_object_is_in_editmode(ob), true);
}

void BKE_object_facemap_clear(Object *ob)
{
  bFaceMap *fmap = (bFaceMap *)ob->fmaps.first;

  if (fmap) {
    const bool edit_mode = BKE_object_is_in_editmode_vgroup(ob);

    while (fmap) {
      bFaceMap *next_fmap = fmap->next;
      fmap_remove_exec(ob, fmap, edit_mode, false);
      fmap = next_fmap;
    }
  }
  /* remove all face-maps */
  if (ob->type == OB_MESH) {
    Mesh *me = ob->data;
    CustomData_free_layer(&me->pdata, CD_FACEMAP, me->totpoly, 0);
  }
  ob->actfmap = 0;
}

int BKE_object_facemap_name_index(Object *ob, const char *name)
{
  return (name) ? BLI_findstringindex(&ob->fmaps, name, offsetof(bFaceMap, name)) : -1;
}

bFaceMap *BKE_object_facemap_find_name(Object *ob, const char *name)
{
  return BLI_findstring(&ob->fmaps, name, offsetof(bFaceMap, name));
}

int *BKE_object_facemap_index_map_create(Object *ob_src, Object *ob_dst, int *r_map_len)
{
  /* Build src to merged mapping of facemap indices. */
  if (BLI_listbase_is_empty(&ob_src->fmaps) || BLI_listbase_is_empty(&ob_dst->fmaps)) {
    *r_map_len = 0;
    return NULL;
  }

  *r_map_len = BLI_listbase_count(&ob_src->fmaps);
  int *fmap_index_map = MEM_malloc_arrayN(
      *r_map_len, sizeof(*fmap_index_map), "defgroup index map create");
  bool is_fmap_remap_needed = false;

  int i = 0;
  for (bFaceMap *fmap_src = ob_src->fmaps.first; fmap_src; fmap_src = fmap_src->next, i++) {
    fmap_index_map[i] = BKE_object_facemap_name_index(ob_dst, fmap_src->name);
    is_fmap_remap_needed = is_fmap_remap_needed || (fmap_index_map[i] != i);
  }

  if (!is_fmap_remap_needed) {
    MEM_freeN(fmap_index_map);
    fmap_index_map = NULL;
    *r_map_len = 0;
  }

  return fmap_index_map;
}

void BKE_object_facemap_index_map_apply(int *fmap, int fmap_len, const int *map, int map_len)
{
  if (map == NULL || map_len == 0) {
    return;
  }
  for (int i = 0; i < fmap_len; i++, fmap++) {
    *fmap = (*fmap < map_len && *fmap != -1) ? map[*fmap] : -1;
  }
}