doxygen: add newline after \file
[blender.git] / source / blender / blenkernel / intern / editlattice.c
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
17 /** \file
18  * \ingroup bke
19  */
20
21 #include "MEM_guardedalloc.h"
22
23 #include "DNA_listBase.h"
24 #include "DNA_object_types.h"
25 #include "DNA_key_types.h"
26 #include "DNA_lattice_types.h"
27 #include "DNA_meshdata_types.h"
28 #include "DNA_curve_types.h"
29
30 #include "BLI_math_vector.h"
31 #include "BLI_listbase.h"
32
33 #include "BKE_deform.h"
34 #include "BKE_key.h"
35
36 #include "BKE_editlattice.h" /* own include */
37
38 void BKE_editlattice_free(Object *ob)
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
58 void BKE_editlattice_make(Object *obedit)
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) {
67                 BKE_keyblock_convert_to_lattice(actkey, lt);
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");
76                 BKE_defvert_array_copy(lt->editlatt->latt->dvert, lt->dvert, tot);
77         }
78
79         if (lt->key) {
80                 lt->editlatt->shapenr = obedit->shapenr;
81         }
82 }
83
84 void BKE_editlattice_load(Object *obedit)
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         if (lt->editlatt->shapenr) {
96                 actkey = BLI_findlink(&lt->key->block, lt->editlatt->shapenr - 1);
97
98                 /* active key: vertices */
99                 tot = editlt->pntsu * editlt->pntsv * editlt->pntsw;
100
101                 if (actkey->data) {
102                         MEM_freeN(actkey->data);
103                 }
104
105                 fp = actkey->data = MEM_callocN(lt->key->elemsize * tot, "actkey->data");
106                 actkey->totelem = tot;
107
108                 bp = editlt->def;
109                 while (tot--) {
110                         copy_v3_v3(fp, bp->vec);
111                         fp += 3;
112                         bp++;
113                 }
114         }
115         else {
116                 MEM_freeN(lt->def);
117
118                 lt->def = MEM_dupallocN(editlt->def);
119
120                 lt->flag = editlt->flag;
121
122                 lt->pntsu = editlt->pntsu;
123                 lt->pntsv = editlt->pntsv;
124                 lt->pntsw = editlt->pntsw;
125
126                 lt->typeu = editlt->typeu;
127                 lt->typev = editlt->typev;
128                 lt->typew = editlt->typew;
129                 lt->actbp = editlt->actbp;
130         }
131
132         if (lt->dvert) {
133                 BKE_defvert_array_free(lt->dvert, lt->pntsu * lt->pntsv * lt->pntsw);
134                 lt->dvert = NULL;
135         }
136
137         if (editlt->dvert) {
138                 tot = lt->pntsu * lt->pntsv * lt->pntsw;
139
140                 lt->dvert = MEM_mallocN(sizeof(MDeformVert) * tot, "Lattice MDeformVert");
141                 BKE_defvert_array_copy(lt->dvert, editlt->dvert, tot);
142         }
143 }