Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / editlattice.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  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/blenkernel/intern/editlattice.c
22  *  \ingroup bke
23  */
24
25 #include "MEM_guardedalloc.h"
26
27 #include "DNA_listBase.h"
28 #include "DNA_object_types.h"
29 #include "DNA_key_types.h"
30 #include "DNA_lattice_types.h"
31 #include "DNA_meshdata_types.h"
32 #include "DNA_curve_types.h"
33
34 #include "BLI_math_vector.h"
35 #include "BLI_listbase.h"
36
37 #include "BKE_deform.h"
38 #include "BKE_key.h"
39
40 #include "BKE_editlattice.h" /* own include */
41
42 void BKE_editlattice_free(Object *ob)
43 {
44         Lattice *lt = ob->data;
45
46         if (lt->editlatt) {
47                 Lattice *editlt = lt->editlatt->latt;
48
49                 if (editlt->def) {
50                         MEM_freeN(editlt->def);
51                 }
52                 if (editlt->dvert) {
53                         BKE_defvert_array_free(editlt->dvert, editlt->pntsu * editlt->pntsv * editlt->pntsw);
54                 }
55                 MEM_freeN(editlt);
56                 MEM_freeN(lt->editlatt);
57
58                 lt->editlatt = NULL;
59         }
60 }
61
62 void BKE_editlattice_make(Object *obedit)
63 {
64         Lattice *lt = obedit->data;
65         KeyBlock *actkey;
66
67         BKE_editlattice_free(obedit);
68
69         actkey = BKE_keyblock_from_object(obedit);
70         if (actkey) {
71                 BKE_keyblock_convert_to_lattice(actkey, lt);
72         }
73         lt->editlatt = MEM_callocN(sizeof(EditLatt), "editlatt");
74         lt->editlatt->latt = MEM_dupallocN(lt);
75         lt->editlatt->latt->def = MEM_dupallocN(lt->def);
76
77         if (lt->dvert) {
78                 int tot = lt->pntsu * lt->pntsv * lt->pntsw;
79                 lt->editlatt->latt->dvert = MEM_mallocN(sizeof(MDeformVert) * tot, "Lattice MDeformVert");
80                 BKE_defvert_array_copy(lt->editlatt->latt->dvert, lt->dvert, tot);
81         }
82
83         if (lt->key) {
84                 lt->editlatt->shapenr = obedit->shapenr;
85         }
86 }
87
88 void BKE_editlattice_load(Object *obedit)
89 {
90         Lattice *lt, *editlt;
91         KeyBlock *actkey;
92         BPoint *bp;
93         float *fp;
94         int tot;
95
96         lt = obedit->data;
97         editlt = lt->editlatt->latt;
98
99         if (lt->editlatt->shapenr) {
100                 actkey = BLI_findlink(&lt->key->block, lt->editlatt->shapenr - 1);
101
102                 /* active key: vertices */
103                 tot = editlt->pntsu * editlt->pntsv * editlt->pntsw;
104
105                 if (actkey->data) {
106                         MEM_freeN(actkey->data);
107                 }
108
109                 fp = actkey->data = MEM_callocN(lt->key->elemsize * tot, "actkey->data");
110                 actkey->totelem = tot;
111
112                 bp = editlt->def;
113                 while (tot--) {
114                         copy_v3_v3(fp, bp->vec);
115                         fp += 3;
116                         bp++;
117                 }
118         }
119         else {
120                 MEM_freeN(lt->def);
121
122                 lt->def = MEM_dupallocN(editlt->def);
123
124                 lt->flag = editlt->flag;
125
126                 lt->pntsu = editlt->pntsu;
127                 lt->pntsv = editlt->pntsv;
128                 lt->pntsw = editlt->pntsw;
129
130                 lt->typeu = editlt->typeu;
131                 lt->typev = editlt->typev;
132                 lt->typew = editlt->typew;
133                 lt->actbp = editlt->actbp;
134         }
135
136         if (lt->dvert) {
137                 BKE_defvert_array_free(lt->dvert, lt->pntsu * lt->pntsv * lt->pntsw);
138                 lt->dvert = NULL;
139         }
140
141         if (editlt->dvert) {
142                 tot = lt->pntsu * lt->pntsv * lt->pntsw;
143
144                 lt->dvert = MEM_mallocN(sizeof(MDeformVert) * tot, "Lattice MDeformVert");
145                 BKE_defvert_array_copy(lt->dvert, editlt->dvert, tot);
146         }
147 }