Initial revision
[blender.git] / source / blender / src / editlattice.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * i.t.t. wat de naam doet vermoeden: ook algemene lattice (calc) functies
32  */
33
34 #include <stdlib.h>
35 #include <math.h>
36 #ifdef WIN32
37 #include "BLI_winstuff.h"
38 #endif
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_arithb.h"
43 #include "BLI_editVert.h"
44
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_lattice_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_key_types.h"
50 #include "DNA_view3d_types.h"
51
52 #include "BKE_key.h"
53 #include "BKE_displist.h"
54 #include "BKE_lattice.h"
55 #include "BKE_global.h"
56
57 #include "BIF_toolbox.h"
58 #include "BIF_space.h"
59 #include "BIF_screen.h"
60 #include "BIF_editlattice.h"
61 #include "BIF_editkey.h"
62
63 #include "BSE_edit.h"
64
65 #include "BDR_editobject.h"
66 #include "BDR_drawobject.h"
67
68 #include "blendef.h"
69 #include "mydevice.h"
70
71 #include "render.h"
72
73 #include "BKE_armature.h"
74
75 void apply_lattice(void)
76 {
77         Base *base;
78         Object *par;
79         
80         if(okee("Apply Lattice Deform")==0) return;
81         
82         base= FIRSTBASE;
83         while(base) {
84                 if TESTBASELIB(base) {
85                         if( (par= base->object->parent) ) {
86                                 if(par->type==OB_LATTICE) {
87                                         
88                                         lt_applyflag= 1;
89                                         object_deform(base->object);
90                                         lt_applyflag= 0;
91                                         
92                                         base->object->parent= 0;
93                                 }
94                         }
95                 }
96                 base= base->next;
97         }
98         
99         allqueue(REDRAWVIEW3D, 0);
100 }
101
102 /* ***************************** */
103
104
105
106
107 void free_editLatt(void)
108 {
109         if(editLatt) {
110                 if(editLatt->def) MEM_freeN(editLatt->def);
111                 MEM_freeN(editLatt);
112                 editLatt= 0;
113         }
114 }
115
116
117 static void setflagsLatt(int flag)
118 {
119         BPoint *bp;
120         int a;
121         
122         bp= editLatt->def;
123         
124         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
125         
126         while(a--) {
127                 if(bp->hide==0) {
128                         bp->f1= flag;
129                 }
130                 bp++;
131         }
132 }
133
134
135
136 void make_editLatt(void)
137 {
138         Lattice *lt;
139         KeyBlock *actkey=0;
140         
141         free_editLatt();
142         
143         lt= G.obedit->data;
144
145         /* keys? */
146         if(lt->key) {
147                 actkey= lt->key->block.first;
148                 while(actkey) {
149                         if(actkey->flag & SELECT) break;
150                         actkey= actkey->next;
151                 }
152         }
153
154         if(actkey) {
155                 key_to_latt(actkey, lt);
156         }
157
158         editLatt= MEM_dupallocN(lt);
159         
160         editLatt->def= MEM_dupallocN(lt->def);
161         
162         setflagsLatt(0);
163 }
164
165
166 void load_editLatt(void)
167 {
168         Lattice *lt;
169         KeyBlock *actkey=0;
170         BPoint *bp;
171         float *fp;
172         int tot;
173         
174         lt= G.obedit->data;
175         
176         /* zijn er keys? */
177         if(lt->key) {
178                 actkey= lt->key->block.first;
179                 while(actkey) {
180                         if(actkey->flag & SELECT) break;
181                         actkey= actkey->next;
182                 }
183         }
184
185         if(actkey) {
186                 /* aktieve key: vertices */
187                 tot= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
188                 
189                 if(actkey->data) MEM_freeN(actkey->data);
190                 
191                 fp=actkey->data= MEM_callocN(lt->key->elemsize*tot, "actkey->data");
192                 actkey->totelem= tot;
193         
194                 bp= editLatt->def;
195                 while(tot--) {
196                         VECCOPY(fp, bp->vec);
197                         fp+= 3;
198                         bp++;
199                 }
200                 
201                 if(actkey) do_spec_key(lt->key);
202         }
203         else {
204
205                 MEM_freeN(lt->def);
206         
207                 lt->def= MEM_dupallocN(editLatt->def);
208
209                 lt->flag= editLatt->flag;
210
211                 lt->pntsu= editLatt->pntsu;
212                 lt->pntsv= editLatt->pntsv;
213                 lt->pntsw= editLatt->pntsw;
214                 
215                 lt->typeu= editLatt->typeu;
216                 lt->typev= editLatt->typev;
217                 lt->typew= editLatt->typew;
218         }
219 }
220
221 void remake_editLatt(void)
222 {
223         if(okee("Reload Original data")==0) return;
224         
225         make_editLatt();
226         allqueue(REDRAWVIEW3D, 0);
227         allqueue(REDRAWBUTSEDIT, 0);
228 }
229
230
231 void deselectall_Latt(void)
232 {
233         BPoint *bp;
234         int a;
235         
236         bp= editLatt->def;
237         
238         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
239         
240         allqueue(REDRAWVIEW3D, 0);
241         
242         while(a--) {
243                 if(bp->hide==0) {
244                         if(bp->f1) {
245                                 setflagsLatt(0);
246                                 return;
247                         }
248                 }
249                 bp++;
250         }
251         setflagsLatt(1);
252 }
253
254
255 static BPoint *findnearestLattvert(int sel)
256 {
257         /* sel==1: selected krijgen een nadeel */
258         /* in bp wordt nearest weggeschreven */
259         BPoint *bp1, *bp;
260         short dist= 100, temp, mval[2], a;
261
262         bp= 0;
263
264         /* projektie doen */
265         calc_lattverts_ext();   /* drawobject.c */
266         
267         getmouseco_areawin(mval);
268
269                         
270         bp1= editLatt->def;
271         
272         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
273         
274         while(a--) {
275                 if(bp1->hide==0) {
276                         temp= abs(mval[0]- bp1->s[0])+ abs(mval[1]- bp1->s[1]);
277                         if( (bp1->f1 & 1)==sel) temp+=5;
278                         if(temp<dist) { 
279                                 bp= bp1; 
280                                 dist= temp; 
281                         }
282                 }
283                 bp1++;
284         }
285
286         return bp;
287 }
288
289
290 void mouse_lattice(void)
291 {
292         BPoint *bp=0;
293
294         bp= findnearestLattvert(1);
295
296         if(bp) {
297                 if((G.qual & LR_SHIFTKEY)==0) {
298                 
299                         setflagsLatt(0);
300                         bp->f1 |= 1;
301
302                         allqueue(REDRAWVIEW3D, 0);
303                 }
304                 else {
305                         
306                         if(bp->f1 & 1) bp->f1 &= ~1;
307                         else bp->f1 |= 1;
308
309                         allqueue(REDRAWVIEW3D, 0);
310
311                 }
312
313                 countall();
314         }
315
316         rightmouse_transform();
317         
318 }