Blender  V2.93
editlattice.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "MEM_guardedalloc.h"
22 
23 #include "DNA_curve_types.h"
24 #include "DNA_key_types.h"
25 #include "DNA_lattice_types.h"
26 #include "DNA_listBase.h"
27 #include "DNA_meshdata_types.h"
28 #include "DNA_object_types.h"
29 
30 #include "BLI_listbase.h"
31 #include "BLI_math_vector.h"
32 
33 #include "BKE_deform.h"
34 #include "BKE_key.h"
35 
36 #include "BKE_editlattice.h" /* own include */
37 
39 {
40  Lattice *lt = ob->data;
41 
42  if (lt->editlatt) {
43  Lattice *editlt = lt->editlatt->latt;
44 
45  if (editlt->def) {
46  MEM_freeN(editlt->def);
47  }
48  if (editlt->dvert) {
49  BKE_defvert_array_free(editlt->dvert, editlt->pntsu * editlt->pntsv * editlt->pntsw);
50  }
51  MEM_freeN(editlt);
52  MEM_freeN(lt->editlatt);
53 
54  lt->editlatt = NULL;
55  }
56 }
57 
59 {
60  Lattice *lt = obedit->data;
61  KeyBlock *actkey;
62 
63  BKE_editlattice_free(obedit);
64 
65  actkey = BKE_keyblock_from_object(obedit);
66  if (actkey) {
68  }
69  lt->editlatt = MEM_callocN(sizeof(EditLatt), "editlatt");
70  lt->editlatt->latt = MEM_dupallocN(lt);
71  lt->editlatt->latt->def = MEM_dupallocN(lt->def);
72 
73  if (lt->dvert) {
74  int tot = lt->pntsu * lt->pntsv * lt->pntsw;
75  lt->editlatt->latt->dvert = MEM_mallocN(sizeof(MDeformVert) * tot, "Lattice MDeformVert");
77  }
78 
79  if (lt->key) {
80  lt->editlatt->shapenr = obedit->shapenr;
81  }
82 }
83 
85 {
86  Lattice *lt, *editlt;
87  KeyBlock *actkey;
88  BPoint *bp;
89  float *fp;
90  int tot;
91 
92  lt = obedit->data;
93  editlt = lt->editlatt->latt;
94 
95  MEM_freeN(lt->def);
96 
97  lt->def = MEM_dupallocN(editlt->def);
98 
99  lt->flag = editlt->flag;
100 
101  lt->pntsu = editlt->pntsu;
102  lt->pntsv = editlt->pntsv;
103  lt->pntsw = editlt->pntsw;
104 
105  lt->typeu = editlt->typeu;
106  lt->typev = editlt->typev;
107  lt->typew = editlt->typew;
108  lt->actbp = editlt->actbp;
109 
110  lt->fu = editlt->fu;
111  lt->fv = editlt->fv;
112  lt->fw = editlt->fw;
113  lt->du = editlt->du;
114  lt->dv = editlt->dv;
115  lt->dw = editlt->dw;
116 
117  if (lt->editlatt->shapenr) {
118  actkey = BLI_findlink(&lt->key->block, lt->editlatt->shapenr - 1);
119 
120  /* active key: vertices */
121  tot = editlt->pntsu * editlt->pntsv * editlt->pntsw;
122 
123  if (actkey->data) {
124  MEM_freeN(actkey->data);
125  }
126 
127  fp = actkey->data = MEM_callocN(lt->key->elemsize * tot, "actkey->data");
128  actkey->totelem = tot;
129 
130  bp = editlt->def;
131  while (tot--) {
132  copy_v3_v3(fp, bp->vec);
133  fp += 3;
134  bp++;
135  }
136  }
137 
138  if (lt->dvert) {
139  BKE_defvert_array_free(lt->dvert, lt->pntsu * lt->pntsv * lt->pntsw);
140  lt->dvert = NULL;
141  }
142 
143  if (editlt->dvert) {
144  tot = lt->pntsu * lt->pntsv * lt->pntsw;
145 
146  lt->dvert = MEM_mallocN(sizeof(MDeformVert) * tot, "Lattice MDeformVert");
147  BKE_defvert_array_copy(lt->dvert, editlt->dvert, tot);
148  }
149 }
support for deformation groups and hooks.
void BKE_defvert_array_free(struct MDeformVert *dvert, int totvert)
Definition: deform.c:977
void BKE_defvert_array_copy(struct MDeformVert *dst, const struct MDeformVert *src, int totvert)
void BKE_keyblock_convert_to_lattice(struct KeyBlock *kb, struct Lattice *lt)
Definition: key.c:2025
struct KeyBlock * BKE_keyblock_from_object(struct Object *ob)
Definition: key.c:1902
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
MINLINE void copy_v3_v3(float r[3], const float a[3])
These structs are the foundation for all linked lists in the library system.
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
void BKE_editlattice_make(Object *obedit)
Definition: editlattice.c:58
void BKE_editlattice_free(Object *ob)
Definition: editlattice.c:38
void BKE_editlattice_load(Object *obedit)
Definition: editlattice.c:84
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
float vec[4]
struct Lattice * latt
void * data
Definition: DNA_key_types.h:66
int elemsize
Definition: DNA_key_types.h:96
ListBase block
struct Key * key
struct MDeformVert * dvert
struct EditLatt * editlatt
struct BPoint * def
short shapenr
void * data