fix [#30715] bmesh: select linked not ignoring hidden verts/edges/faces
[blender.git] / source / blender / blenkernel / intern / deform.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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Reevan McKay
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/deform.c
29  *  \ingroup bke
30  */
31
32
33 #include <string.h>
34 #include <math.h>
35 #include "ctype.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_meshdata_types.h"
40 #include "DNA_object_types.h"
41
42 #include "BKE_deform.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_utildefines.h"
46
47
48 void defgroup_copy_list(ListBase *outbase, ListBase *inbase)
49 {
50         bDeformGroup *defgroup, *defgroupn;
51
52         outbase->first = outbase->last = NULL;
53
54         for (defgroup = inbase->first; defgroup; defgroup = defgroup->next) {
55                 defgroupn = defgroup_duplicate(defgroup);
56                 BLI_addtail(outbase, defgroupn);
57         }
58 }
59
60 bDeformGroup *defgroup_duplicate(bDeformGroup *ingroup)
61 {
62         bDeformGroup *outgroup;
63
64         if (!ingroup)
65                 return NULL;
66
67         outgroup = MEM_callocN(sizeof(bDeformGroup), "copy deformGroup");
68
69         /* For now, just copy everything over. */
70         memcpy(outgroup, ingroup, sizeof(bDeformGroup));
71
72         outgroup->next = outgroup->prev = NULL;
73
74         return outgroup;
75 }
76
77 /* copy & overwrite weights */
78 void defvert_copy(MDeformVert *dvert_dst, const MDeformVert *dvert_src)
79 {
80         if (dvert_dst->totweight == dvert_src->totweight) {
81                 if (dvert_src->totweight)
82                         memcpy(dvert_dst->dw, dvert_src->dw, dvert_src->totweight * sizeof(MDeformWeight));
83         }
84         else {
85                 if (dvert_dst->dw)
86                         MEM_freeN(dvert_dst->dw);
87
88                 if (dvert_src->totweight)
89                         dvert_dst->dw = MEM_dupallocN(dvert_src->dw);
90                 else
91                         dvert_dst->dw = NULL;
92
93                 dvert_dst->totweight = dvert_src->totweight;
94         }
95 }
96
97 /* copy an index from one dvert to another
98  * - do nothing if neither are set.
99  * - add destination weight if needed.
100  */
101 void defvert_copy_index(MDeformVert *dvert_dst, const MDeformVert *dvert_src, const int defgroup)
102 {
103         MDeformWeight *dw_src, *dw_dst;
104
105         dw_src = defvert_find_index(dvert_src, defgroup);
106
107         if (dw_src) {
108                 /* source is valid, verify destination */
109                 dw_dst = defvert_verify_index(dvert_dst, defgroup);
110                 dw_dst->weight = dw_src->weight;
111         }
112         else {
113                 /* source was NULL, assign zero, could also remove */
114                 dw_dst = defvert_find_index(dvert_dst, defgroup);
115
116                 if (dw_dst) {
117                         dw_dst->weight = 0.0f;
118                 }
119         }
120 }
121
122 /* only sync over matching weights, don't add or remove groups
123  * warning, loop within loop.
124  */
125 void defvert_sync(MDeformVert *dvert_dst, const MDeformVert *dvert_src, int use_verify)
126 {
127         if (dvert_src->totweight && dvert_dst->totweight) {
128                 int i;
129                 MDeformWeight *dw_src;
130                 for (i = 0, dw_src = dvert_src->dw; i < dvert_src->totweight; i++, dw_src++) {
131                         MDeformWeight *dw_dst;
132                         if (use_verify) dw_dst = defvert_verify_index(dvert_dst, dw_src->def_nr);
133                         else            dw_dst = defvert_find_index(dvert_dst, dw_src->def_nr);
134
135                         if (dw_dst) {
136                                 dw_dst->weight = dw_src->weight;
137                         }
138                 }
139         }
140 }
141
142 /* be sure all flip_map values are valid */
143 void defvert_sync_mapped(MDeformVert *dvert_dst, const MDeformVert *dvert_src,
144                          const int *flip_map, const int flip_map_len, const int use_verify)
145 {
146         if (dvert_src->totweight && dvert_dst->totweight) {
147                 int i;
148                 MDeformWeight *dw_src;
149                 for (i = 0, dw_src = dvert_src->dw; i < dvert_src->totweight; i++, dw_src++) {
150                         if (dw_src->def_nr < flip_map_len) {
151                                 MDeformWeight *dw_dst;
152                                 if (use_verify) dw_dst = defvert_verify_index(dvert_dst, flip_map[dw_src->def_nr]);
153                                 else            dw_dst = defvert_find_index(dvert_dst, flip_map[dw_src->def_nr]);
154
155                                 if (dw_dst) {
156                                         dw_dst->weight = dw_src->weight;
157                                 }
158                         }
159                 }
160         }
161 }
162
163 /* be sure all flip_map values are valid */
164 void defvert_remap(MDeformVert *dvert, int *map, const int map_len)
165 {
166         MDeformWeight *dw = dvert->dw;
167         unsigned int i;
168         for (i = dvert->totweight; i != 0; i--, dw++) {
169                 if (dw->def_nr < map_len) {
170                         dw->def_nr = map[dw->def_nr];
171
172                         /* just in case */
173                         BLI_assert(dw->def_nr >= 0);
174                 }
175         }
176 }
177
178 void defvert_normalize(MDeformVert *dvert)
179 {
180         if (dvert->totweight <= 0) {
181                 /* nothing */
182         }
183         else if (dvert->totweight == 1) {
184                 dvert->dw[0].weight = 1.0f;
185         }
186         else {
187                 MDeformWeight *dw;
188                 unsigned int i;
189                 float tot_weight = 0.0f;
190
191                 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
192                         tot_weight += dw->weight;
193                 }
194
195                 if (tot_weight > 0.0f) {
196                         float scalar = 1.0f / tot_weight;
197                         for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
198                                 dw->weight *= scalar;
199
200                                 /* in case of division errors with very low weights */
201                                 CLAMP(dw->weight, 0.0f, 1.0f);
202                         }
203                 }
204         }
205 }
206
207 void defvert_normalize_lock(MDeformVert *dvert, const int def_nr_lock)
208 {
209         if (dvert->totweight <= 0) {
210                 /* nothing */
211         }
212         else if (dvert->totweight == 1) {
213                 dvert->dw[0].weight = 1.0f;
214         }
215         else {
216                 MDeformWeight *dw_lock = NULL;
217                 MDeformWeight *dw;
218                 unsigned int i;
219                 float tot_weight = 0.0f;
220                 float lock_iweight = 1.0f;
221
222                 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
223                         if (dw->def_nr != def_nr_lock) {
224                                 tot_weight += dw->weight;
225                         }
226                         else {
227                                 dw_lock = dw;
228                                 lock_iweight = (1.0f - dw_lock->weight);
229                                 CLAMP(lock_iweight, 0.0f, 1.0f);
230                         }
231                 }
232
233                 if (tot_weight > 0.0f) {
234                         /* paranoid, should be 1.0 but in case of float error clamp anyway */
235
236                         float scalar = (1.0f / tot_weight) * lock_iweight;
237                         for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
238                                 if (dw != dw_lock) {
239                                         dw->weight *= scalar;
240
241                                         /* in case of division errors with very low weights */
242                                         CLAMP(dw->weight, 0.0f, 1.0f);
243                                 }
244                         }
245                 }
246         }
247 }
248
249 void defvert_flip(MDeformVert *dvert, const int *flip_map, const int flip_map_len)
250 {
251         MDeformWeight *dw;
252         int i;
253
254         for (dw = dvert->dw, i = 0; i<dvert->totweight; dw++, i++) {
255                 if (dw->def_nr < flip_map_len) {
256                         if (flip_map[dw->def_nr] >= 0) {
257                                 dw->def_nr = flip_map[dw->def_nr];
258                         }
259                 }
260         }
261 }
262
263 void defvert_flip_merged(MDeformVert *dvert, const int *flip_map, const int flip_map_len)
264 {
265         MDeformWeight *dw, *dw_cpy;
266         float weight;
267         int i, totweight = dvert->totweight;
268
269         /* copy weights */
270         for (dw = dvert->dw, i = 0; i < totweight; dw++, i++) {
271                 if (dw->def_nr < flip_map_len) {
272                         if (flip_map[dw->def_nr] >= 0) {
273                                 /* error checkers complain of this but we'll never get NULL return */
274                                 dw_cpy = defvert_verify_index(dvert, flip_map[dw->def_nr]);
275                                 dw = &dvert->dw[i]; /* in case array got realloced */
276
277                                 /* distribute weights: if only one of the vertex groups was
278                                  * assigned this will halve the weights, otherwise it gets
279                                  * evened out. this keeps it proportional to other groups */
280                                 weight = 0.5f * (dw_cpy->weight + dw->weight);
281                                 dw_cpy->weight = weight;
282                                 dw->weight = weight;
283                         }
284                 }
285         }
286 }
287
288 bDeformGroup *defgroup_find_name(Object *ob, const char *name)
289 {
290         /* return a pointer to the deform group with this name
291          * or return NULL otherwise.
292          */
293         bDeformGroup *curdef;
294
295         for (curdef = ob->defbase.first; curdef; curdef = curdef->next) {
296                 if (!strcmp(curdef->name, name)) {
297                         return curdef;
298                 }
299         }
300         return NULL;
301 }
302
303 int defgroup_name_index(Object *ob, const char *name)
304 {
305         /* Return the location of the named deform group within the list of
306          * deform groups. This function is a combination of BLI_findlink and
307          * defgroup_find_name. The other two could be called instead, but that
308          * require looping over the vertexgroups twice.
309          */
310         bDeformGroup *curdef;
311         int def_nr;
312
313         if (name && name[0] != '\0') {
314                 for (curdef = ob->defbase.first, def_nr = 0; curdef; curdef = curdef->next, def_nr++) {
315                         if (!strcmp(curdef->name, name))
316                                 return def_nr;
317                 }
318         }
319
320         return -1;
321 }
322
323 /* note, must be freed */
324 int *defgroup_flip_map(Object *ob, int *flip_map_len, int use_default)
325 {
326         int defbase_tot = *flip_map_len = BLI_countlist(&ob->defbase);
327
328         if (defbase_tot == 0) {
329                 return NULL;
330         }
331         else {
332                 bDeformGroup *dg;
333                 char name[sizeof(dg->name)];
334                 int i, flip_num, *map = MEM_mallocN(defbase_tot * sizeof(int), __func__);
335
336                 for (i = 0; i < defbase_tot; i++) {
337                         map[i] = -1;
338                 }
339
340                 for (dg = ob->defbase.first, i = 0; dg; dg = dg->next, i++) {
341                         if (map[i] == -1) { /* may be calculated previously */
342
343                                 /* in case no valid value is found, use this */
344                                 if (use_default)
345                                         map[i] = i;
346
347                                 flip_side_name(name, dg->name, FALSE);
348                                 if (strcmp(name, dg->name)) {
349                                         flip_num = defgroup_name_index(ob, name);
350                                         if (flip_num >= 0) {
351                                                 map[i] = flip_num;
352                                                 map[flip_num] = i; /* save an extra lookup */
353                                         }
354                                 }
355                         }
356                 }
357                 return map;
358         }
359 }
360
361 /* note, must be freed */
362 int *defgroup_flip_map_single(Object *ob, int *flip_map_len, int use_default, int defgroup)
363 {
364         int defbase_tot = *flip_map_len = BLI_countlist(&ob->defbase);
365
366         if (defbase_tot == 0) {
367                 return NULL;
368         }
369         else {
370                 bDeformGroup *dg;
371                 char name[sizeof(dg->name)];
372                 int i, flip_num, *map = MEM_mallocN(defbase_tot * sizeof(int), __func__);
373
374                 for (i = 0; i < defbase_tot; i++) {
375                         map[i] = use_default ? i : -1;
376                 }
377
378                 dg = BLI_findlink(&ob->defbase, defgroup);
379
380                 flip_side_name(name, dg->name, FALSE);
381                 if (strcmp(name, dg->name)) {
382                         flip_num = defgroup_name_index(ob, name);
383
384                         if (flip_num >= 0) {
385                                 map[defgroup] = flip_num;
386                                 map[flip_num] = defgroup;
387                         }
388                 }
389
390                 return map;
391         }
392 }
393
394 int defgroup_flip_index(Object *ob, int index, int use_default)
395 {
396         bDeformGroup *dg = BLI_findlink(&ob->defbase, index);
397         int flip_index = -1;
398
399         if (dg) {
400                 char name[sizeof(dg->name)];
401                 flip_side_name(name, dg->name, 0);
402
403                 if (strcmp(name, dg->name))
404                         flip_index = defgroup_name_index(ob, name);
405         }
406
407         return (flip_index == -1 && use_default) ? index : flip_index;
408 }
409
410 static int defgroup_find_name_dupe(const char *name, bDeformGroup *dg, Object *ob)
411 {
412         bDeformGroup *curdef;
413
414         for (curdef = ob->defbase.first; curdef; curdef = curdef->next) {
415                 if (dg != curdef) {
416                         if (!strcmp(curdef->name, name)) {
417                                 return 1;
418                         }
419                 }
420         }
421
422         return 0;
423 }
424
425 static int defgroup_unique_check(void *arg, const char *name)
426 {
427         struct {Object *ob; void *dg;} *data = arg;
428         return defgroup_find_name_dupe(name, data->dg, data->ob);
429 }
430
431 void defgroup_unique_name(bDeformGroup *dg, Object *ob)
432 {
433         struct {Object *ob; void *dg;} data;
434         data.ob = ob;
435         data.dg = dg;
436
437         BLI_uniquename_cb(defgroup_unique_check, &data, "Group", '.', dg->name, sizeof(dg->name));
438 }
439
440 BLI_INLINE int is_char_sep(const char c)
441 {
442         return ELEM4(c, '.', ' ', '-', '_');
443 }
444
445 /* finds the best possible flipped name. For renaming; check for unique names afterwards */
446 /* if strip_number: removes number extensions
447  * note: don't use sizeof() for 'name' or 'from_name' */
448 void flip_side_name(char name[MAX_VGROUP_NAME], const char from_name[MAX_VGROUP_NAME], int strip_number)
449 {
450         int     len;
451         char    prefix[MAX_VGROUP_NAME]  = "";   /* The part before the facing */
452         char    suffix[MAX_VGROUP_NAME]  = "";   /* The part after the facing */
453         char    replace[MAX_VGROUP_NAME] = "";   /* The replacement string */
454         char    number[MAX_VGROUP_NAME]  = "";   /* The number extension string */
455         char    *index = NULL;
456
457         /* always copy the name, since this can be called with an uninitialized string */
458         BLI_strncpy(name, from_name, MAX_VGROUP_NAME);
459
460         len = BLI_strnlen(from_name, MAX_VGROUP_NAME);
461         if (len < 3) {
462                 /* we don't do names like .R or .L */
463                 return;
464         }
465
466         /* We first check the case with a .### extension, let's find the last period */
467         if (isdigit(name[len - 1])) {
468                 index = strrchr(name, '.'); // last occurrence
469                 if (index && isdigit(index[1]) ) { // doesnt handle case bone.1abc2 correct..., whatever!
470                         if (strip_number == 0) {
471                                 BLI_strncpy(number, index, sizeof(number));
472                         }
473                         *index = 0;
474                         len = BLI_strnlen(name, MAX_VGROUP_NAME);
475                 }
476         }
477
478         BLI_strncpy(prefix, name, sizeof(prefix));
479
480         /* first case; separator . - _ with extensions r R l L  */
481         if (is_char_sep(name[len - 2]) ) {
482                 switch(name[len - 1]) {
483                         case 'l':
484                                 prefix[len - 1] = 0;
485                                 strcpy(replace, "r");
486                                 break;
487                         case 'r':
488                                 prefix[len - 1] = 0;
489                                 strcpy(replace, "l");
490                                 break;
491                         case 'L':
492                                 prefix[len - 1] = 0;
493                                 strcpy(replace, "R");
494                                 break;
495                         case 'R':
496                                 prefix[len - 1] = 0;
497                                 strcpy(replace, "L");
498                                 break;
499                 }
500         }
501         /* case; beginning with r R l L , with separator after it */
502         else if (is_char_sep(name[1]) ) {
503                 switch(name[0]) {
504                         case 'l':
505                                 strcpy(replace, "r");
506                                 BLI_strncpy(suffix, name + 1, sizeof(suffix));
507                                 prefix[0] = 0;
508                                 break;
509                         case 'r':
510                                 strcpy(replace, "l");
511                                 BLI_strncpy(suffix, name + 1, sizeof(suffix));
512                                 prefix[0] = 0;
513                                 break;
514                         case 'L':
515                                 strcpy(replace, "R");
516                                 BLI_strncpy(suffix, name + 1, sizeof(suffix));
517                                 prefix[0] = 0;
518                                 break;
519                         case 'R':
520                                 strcpy(replace, "L");
521                                 BLI_strncpy(suffix, name + 1, sizeof(suffix));
522                                 prefix[0] = 0;
523                                 break;
524                 }
525         }
526         else if (len > 5) {
527                 /* hrms, why test for a separator? lets do the rule 'ultimate left or right' */
528                 index = BLI_strcasestr(prefix, "right");
529                 if (index == prefix || index == prefix + len - 5) {
530                         if (index[0] == 'r')
531                                 strcpy(replace, "left");
532                         else {
533                                 if (index[1] == 'I')
534                                         strcpy(replace, "LEFT");
535                                 else
536                                         strcpy(replace, "Left");
537                         }
538                         *index = 0;
539                         BLI_strncpy(suffix, index + 5, sizeof(suffix));
540                 }
541                 else {
542                         index = BLI_strcasestr(prefix, "left");
543                         if (index == prefix || index == prefix + len - 4) {
544                                 if (index[0] == 'l')
545                                         strcpy(replace, "right");
546                                 else {
547                                         if (index[1] == 'E')
548                                                 strcpy(replace, "RIGHT");
549                                         else
550                                                 strcpy(replace, "Right");
551                                 }
552                                 *index = 0;
553                                 BLI_strncpy(suffix, index + 4, sizeof(suffix));
554                         }
555                 }
556         }
557
558         BLI_snprintf (name, MAX_VGROUP_NAME, "%s%s%s%s", prefix, replace, suffix, number);
559 }
560
561 float defvert_find_weight(const struct MDeformVert *dvert, const int defgroup)
562 {
563         MDeformWeight *dw = defvert_find_index(dvert, defgroup);
564         return dw ? dw->weight : 0.0f;
565 }
566
567 /* take care with this the rationale is:
568  * - if the object has no vertex group. act like vertex group isn't set and return 1.0,
569  * - if the vertex group exists but the 'defgroup' isn't found on this vertex, _still_ return 0.0
570  *
571  * This is a bit confusing, just saves some checks from the caller.
572  */
573 float defvert_array_find_weight_safe(const struct MDeformVert *dvert, const int index, const int defgroup)
574 {
575         if (defgroup == -1 || dvert == NULL)
576                 return 1.0f;
577
578         return defvert_find_weight(dvert + index, defgroup);
579 }
580
581
582 MDeformWeight *defvert_find_index(const MDeformVert *dvert, const int defgroup)
583 {
584         if (dvert && defgroup >= 0) {
585                 MDeformWeight *dw = dvert->dw;
586                 unsigned int i;
587
588                 for (i = dvert->totweight; i != 0; i--, dw++) {
589                         if (dw->def_nr == defgroup) {
590                                 return dw;
591                         }
592                 }
593         }
594
595         return NULL;
596 }
597
598 /* Ensures that mv has a deform weight entry for the specified defweight group */
599 /* Note this function is mirrored in editmesh_tools.c, for use for editvertices */
600 MDeformWeight *defvert_verify_index(MDeformVert *dvert, const int defgroup)
601 {
602         MDeformWeight *dw_new;
603
604         /* do this check always, this function is used to check for it */
605         if (!dvert || defgroup < 0)
606                 return NULL;
607
608         dw_new = defvert_find_index(dvert, defgroup);
609         if (dw_new)
610                 return dw_new;
611
612         dw_new = MEM_callocN(sizeof(MDeformWeight) * (dvert->totweight + 1), "deformWeight");
613         if (dvert->dw) {
614                 memcpy(dw_new, dvert->dw, sizeof(MDeformWeight) * dvert->totweight);
615                 MEM_freeN(dvert->dw);
616         }
617         dvert->dw = dw_new;
618         dw_new += dvert->totweight;
619         dw_new->weight = 0.0f;
620         dw_new->def_nr = defgroup;
621         /* Group index */
622
623         dvert->totweight++;
624
625         return dw_new;
626 }
627
628 /* TODO. merge with code above! */
629
630 /* Adds the given vertex to the specified vertex group, with given weight.
631  * warning, this does NOT check for existing, assume caller already knows its not there */
632 void defvert_add_index_notest(MDeformVert *dvert, int defgroup, const float weight)
633 {
634         MDeformWeight *dw_new;
635
636         /* do this check always, this function is used to check for it */
637         if (!dvert || defgroup < 0)
638                 return;
639
640         dw_new = MEM_callocN(sizeof(MDeformWeight) * (dvert->totweight + 1), "defvert_add_to group, new deformWeight");
641         if (dvert->dw) {
642                 memcpy(dw_new, dvert->dw, sizeof(MDeformWeight) * dvert->totweight);
643                 MEM_freeN(dvert->dw);
644         }
645         dvert->dw = dw_new;
646         dw_new += dvert->totweight;
647         dw_new->weight = weight;
648         dw_new->def_nr = defgroup;
649         dvert->totweight++;
650 }
651
652
653 /* Removes the given vertex from the vertex group.
654  * WARNING: This function frees the given MDeformWeight, do not use it afterward! */
655 void defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
656 {
657         if (dvert && dw) {
658                 MDeformWeight *dw_new;
659                 int i = dw - dvert->dw;
660
661                 /* Security check! */
662                 if (i < 0 || i >= dvert->totweight) {
663                         return;
664                 }
665
666                 dvert->totweight--;
667                 /* If there are still other deform weights attached to this vert then remove
668                  * this deform weight, and reshuffle the others.
669                  */
670                 if (dvert->totweight) {
671                         dw_new = MEM_mallocN(sizeof(MDeformWeight) * (dvert->totweight), __func__);
672                         if (dvert->dw) {
673 #if 1                   /* since we don't care about order, swap this with the last, save a memcpy */
674                                 if (i != dvert->totweight) {
675                                         dvert->dw[i] = dvert->dw[dvert->totweight];
676                                 }
677                                 memcpy(dw_new, dvert->dw, sizeof(MDeformWeight) * dvert->totweight);
678 #else
679                                 memcpy(dw_new, dvert->dw, sizeof(MDeformWeight) * i);
680                                 memcpy(dw_new + i, dvert->dw + i + 1, sizeof(MDeformWeight) * (dvert->totweight - i));
681 #endif
682                                 MEM_freeN(dvert->dw);
683                         }
684                         dvert->dw = dw_new;
685                 }
686                 else {
687                         /* If there are no other deform weights left then just remove this one. */
688                         MEM_freeN(dvert->dw);
689                         dvert->dw = NULL;
690                 }
691         }
692 }
693
694 void defvert_clear(MDeformVert *dvert)
695 {
696         if (dvert->dw) {
697                 MEM_freeN(dvert->dw);
698                 dvert->dw = NULL;
699         }
700
701         dvert->totweight = 0;
702 }