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