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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2006 by Nicholas Bishop
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*
* Deals with the first-level data in multires (edge flags, weights, and UVs)
*
* multires.h
*
*/
#include "DNA_customdata_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
//XXX #include "BIF_editmesh.h"
#include "BKE_customdata.h"
#include "BKE_global.h"
#include "BKE_mesh.h"
#include "BKE_multires.h"
#include "BLI_editVert.h"
#include "MEM_guardedalloc.h"
#include <string.h>
MDeformVert *subdivide_dverts(MDeformVert *src, MultiresLevel *lvl);
MTFace *subdivide_mtfaces(MTFace *src, MultiresLevel *lvl);
void multires_update_edge_flags(Mesh *me, EditMesh *em);
void eed_to_medge_flag(EditEdge *eed, short *flag, char *crease);
/*********** Generic ***********/
CustomDataMask cdmask(const int type)
{
if(type == CD_MDEFORMVERT)
return CD_MASK_MDEFORMVERT;
else if(type == CD_MTFACE)
return CD_MASK_MTFACE;
return -1;
}
char type_ok(const int type)
{
return (type == CD_MDEFORMVERT) || (type == CD_MTFACE);
}
/* Copy vdata or fdata from Mesh or EditMesh to Multires. */
void multires_update_customdata(MultiresLevel *lvl1, EditMesh *em, CustomData *src, CustomData *dst, const int type)
{
if(src && dst && type_ok(type)) {
const int tot= (type == CD_MDEFORMVERT ? lvl1->totvert : lvl1->totface);
int i;
CustomData_free(dst, tot);
if(CustomData_has_layer(src, type)) {
if(em) {
EditVert *eve= em->verts.first;
EditFace *efa= em->faces.first;
CustomData_copy(src, dst, cdmask(type), CD_CALLOC, tot);
for(i=0; i<tot; ++i) {
if(type == CD_MDEFORMVERT) {
CustomData_from_em_block(&em->vdata, dst, eve->data, i);
eve= eve->next;
}
else if(type == CD_MTFACE) {
CustomData_from_em_block(&em->fdata, dst, efa->data, i);
efa= efa->next;
}
}
}
else
CustomData_copy(src, dst, cdmask(type), CD_DUPLICATE, tot);
}
}
}
/* Uses subdivide_dverts or subdivide_mtfaces to subdivide src to match lvl_end. Does not free src. */
void *subdivide_customdata_to_level(void *src, MultiresLevel *lvl_start,
MultiresLevel *lvl_end, const int type)
{
if(src && lvl_start && lvl_end && type_ok(type)) {
MultiresLevel *lvl;
void *cr_data= NULL, *pr_data= NULL;
pr_data= src;
for(lvl= lvl_start; lvl && lvl != lvl_end; lvl= lvl->next) {
if(type == CD_MDEFORMVERT)
cr_data= subdivide_dverts(pr_data, lvl);
else if(type == CD_MTFACE)
cr_data= subdivide_mtfaces(pr_data, lvl);
/* Free previous subdivision level's data */
if(lvl != lvl_start) {
if(type == CD_MDEFORMVERT)
free_dverts(pr_data, lvl->totvert);
else if(type == CD_MTFACE)
MEM_freeN(pr_data);
}
pr_data= cr_data;
cr_data= NULL;
}
return pr_data;
}
return NULL;
}
/* Directly copy src into dst (handles both Mesh and EditMesh) */
void customdata_to_mesh(Mesh *me, EditMesh *em, CustomData *src, CustomData *dst, const int tot, const int type)
{
if(me && me->mr && src && dst && type_ok(type)) {
if(em) {
int i;
EditVert *eve= em->verts.first;
EditFace *efa= em->faces.first;
CustomData_copy(src, dst, cdmask(type), CD_CALLOC, 0);
for(i=0; i<tot; ++i) {
if(type == CD_MDEFORMVERT) {
CustomData_to_em_block(src, dst, i, &eve->data);
eve= eve->next;
}
else if(type == CD_MTFACE) {
CustomData_to_em_block(src, dst, i, &efa->data);
efa= efa->next;
}
}
} else {
CustomData_merge(src, dst, cdmask(type), CD_DUPLICATE, tot);
}
}
}
/* Subdivide vdata or fdata from Multires into either Mesh or EditMesh. */
void multires_customdata_to_mesh(Mesh *me, EditMesh *em, MultiresLevel *lvl, CustomData *src,
CustomData *dst, const int type)
{
if(me && me->mr && lvl && src && dst && type_ok(type) &&
CustomData_has_layer(src, type)) {
const int tot= (type == CD_MDEFORMVERT ? lvl->totvert : lvl->totface);
if(lvl == me->mr->levels.first) {
customdata_to_mesh(me, em, src, dst, tot, type);
}
else {
CustomData cdf;
const int count = CustomData_number_of_layers(src, type);
int i;
/* Construct a new CustomData containing the subdivided data */
CustomData_copy(src, &cdf, cdmask(type), CD_ASSIGN, tot);
for(i=0; i<count; ++i) {
void *layer= CustomData_get_layer_n(&cdf, type, i);
CustomData_set_layer_n(&cdf, type, i,
subdivide_customdata_to_level(layer, me->mr->levels.first, lvl, type));
}
customdata_to_mesh(me, em, &cdf, dst, tot, type);
CustomData_free(&cdf, tot);
}
}
}
/* Subdivide the first-level customdata up to cr_lvl, then delete the original data */
void multires_del_lower_customdata(Multires *mr, MultiresLevel *cr_lvl)
{
MultiresLevel *lvl1= mr->levels.first;
MDeformVert *dverts= NULL;
CustomData cdf;
int i;
/* dverts */
dverts= subdivide_customdata_to_level(CustomData_get(&mr->vdata, 0, CD_MDEFORMVERT),
lvl1, cr_lvl, CD_MDEFORMVERT);
if(dverts) {
CustomData_free_layers(&mr->vdata, CD_MDEFORMVERT, lvl1->totvert);
CustomData_add_layer(&mr->vdata, CD_MDEFORMVERT, CD_ASSIGN, dverts, cr_lvl->totvert);
}
/* mtfaces */
CustomData_copy(&mr->fdata, &cdf, CD_MASK_MTFACE, CD_ASSIGN, cr_lvl->totface);
for(i=0; i<CustomData_number_of_layers(&mr->fdata, CD_MTFACE); ++i) {
MTFace *mtfaces=
subdivide_customdata_to_level(CustomData_get_layer_n(&mr->fdata, CD_MTFACE, i),
lvl1, cr_lvl, CD_MTFACE);
if(mtfaces)
CustomData_set_layer_n(&cdf, CD_MTFACE, i, mtfaces);
}
CustomData_free(&mr->fdata, lvl1->totface);
mr->fdata= cdf;
}
/* Update all special first-level data, if the first-level is active */
void multires_update_first_level(Mesh *me, EditMesh *em)
{
if(me && me->mr && me->mr->current == 1) {
multires_update_customdata(me->mr->levels.first, em, em ? &em->vdata : &me->vdata,
&me->mr->vdata, CD_MDEFORMVERT);
multires_update_customdata(me->mr->levels.first, em, em ? &em->fdata : &me->fdata,
&me->mr->fdata, CD_MTFACE);
multires_update_edge_flags(me, em);
}
}
/*********** Multires.edge_flags ***********/
void multires_update_edge_flags(Mesh *me, EditMesh *em)
{
MultiresLevel *lvl= me->mr->levels.first;
EditEdge *eed= NULL;
int i;
if(em) eed= em->edges.first;
for(i=0; i<lvl->totedge; ++i) {
if(em) {
me->mr->edge_flags[i]= 0;
eed_to_medge_flag(eed, &me->mr->edge_flags[i], &me->mr->edge_creases[i]);
eed= eed->next;
}
else {
me->mr->edge_flags[i]= me->medge[i].flag;
me->mr->edge_creases[i]= me->medge[i].crease;
}
}
}
/*********** Multires.vdata ***********/
/* MDeformVert */
/* Add each weight from in to out. Scale each weight by w. */
void multires_add_dvert(MDeformVert *out, const MDeformVert *in, const float w)
{
if(out && in) {
int i, j;
char found;
for(i=0; i<in->totweight; ++i) {
found= 0;
for(j=0; j<out->totweight; ++j) {
if(out->dw[j].def_nr==in->dw[i].def_nr) {
out->dw[j].weight += in->dw[i].weight * w;
found= 1;
}
}
if(!found) {
MDeformWeight *newdw= MEM_callocN(sizeof(MDeformWeight)*(out->totweight+1),
"multires dvert");
if(out->dw) {
memcpy(newdw, out->dw, sizeof(MDeformWeight)*out->totweight);
MEM_freeN(out->dw);
}
out->dw= newdw;
out->dw[out->totweight].weight= in->dw[i].weight * w;
out->dw[out->totweight].def_nr= in->dw[i].def_nr;
++out->totweight;
}
}
}
}
/* Takes an input array of dverts and subdivides them (linear) using the topology of lvl */
MDeformVert *subdivide_dverts(MDeformVert *src, MultiresLevel *lvl)
{
if(lvl && lvl->next) {
MDeformVert *out = MEM_callocN(sizeof(MDeformVert)*lvl->next->totvert, "dvert prop array");
int i, j;
/* Copy lower level */
for(i=0; i<lvl->totvert; ++i)
multires_add_dvert(&out[i], &src[i], 1);
/* Edge verts */
for(i=0; i<lvl->totedge; ++i) {
for(j=0; j<2; ++j)
multires_add_dvert(&out[lvl->totvert+i], &src[lvl->edges[i].v[j]],0.5);
}
/* Face verts */
for(i=0; i<lvl->totface; ++i) {
for(j=0; j<(lvl->faces[i].v[3]?4:3); ++j)
multires_add_dvert(&out[lvl->totvert + lvl->totedge + i],
&src[lvl->faces[i].v[j]],
lvl->faces[i].v[3]?0.25:(1.0f/3.0f));
}
return out;
}
return NULL;
}
/*********** Multires.fdata ***********/
/* MTFace */
void multires_uv_avg2(float out[2], const float a[2], const float b[2])
{
int i;
for(i=0; i<2; ++i)
out[i] = (a[i] + b[i]) / 2.0f;
}
/* Takes an input array of mtfaces and subdivides them (linear) using the topology of lvl */
MTFace *subdivide_mtfaces(MTFace *src, MultiresLevel *lvl)
{
if(lvl && lvl->next) {
MTFace *out= MEM_callocN(sizeof(MultiresColFace)*lvl->next->totface,"Multirescolfaces");
int i, j, curf;
for(i=0, curf=0; i<lvl->totface; ++i) {
const char sides= lvl->faces[i].v[3]?4:3;
float cntr[2]= {0, 0};
/* Find average uv coord of the current face */
for(j=0; j<sides; ++j) {
cntr[0]+= src[i].uv[j][0];
cntr[1]+= src[i].uv[j][1];
}
cntr[0]/= sides;
cntr[1]/= sides;
for(j=0; j<sides; ++j, ++curf) {
out[curf]= src[i];
multires_uv_avg2(out[curf].uv[0], src[i].uv[j], src[i].uv[j==0?sides-1:j-1]);
out[curf].uv[1][0]= src[i].uv[j][0];
out[curf].uv[1][1]= src[i].uv[j][1];
multires_uv_avg2(out[curf].uv[2], src[i].uv[j], src[i].uv[j==sides-1?0:j+1]);
out[curf].uv[3][0]= cntr[0];
out[curf].uv[3][1]= cntr[1];
}
}
return out;
}
return NULL;
}
void multires_delete_layer(Object *ob, CustomData *cd, const int type, int n)
{
Mesh *me= ob->data;
if(me && me->mr && cd) {
MultiresLevel *lvl1= me->mr->levels.first;
multires_update_levels(me, 0);
CustomData_set_layer_active(cd, type, n);
CustomData_free_layer_active(cd, type, lvl1->totface);
multires_level_to_mesh(ob, me, 0);
}
}
void multires_add_layer(Object *ob, CustomData *cd, const int type, const int n)
{
Mesh *me= ob->data;
if(me && me->mr && cd) {
multires_update_levels(me, 0);
if(CustomData_has_layer(cd, type))
CustomData_add_layer(cd, type, CD_DUPLICATE, CustomData_get_layer(cd, type),
current_level(me->mr)->totface);
else
CustomData_add_layer(cd, type, CD_DEFAULT, NULL, current_level(me->mr)->totface);
CustomData_set_layer_active(cd, type, n);
multires_level_to_mesh(ob, me, 0);
}
}
|