~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/editors/object/object_lattice.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
#include "DNA_scene_types.h"
47
47
 
48
48
#include "RNA_access.h"
 
49
#include "RNA_define.h"
49
50
 
50
51
#include "BKE_context.h"
51
52
#include "BKE_depsgraph.h"
52
53
#include "BKE_key.h"
53
54
#include "BKE_lattice.h"
54
 
#include "BKE_mesh.h"
 
55
#include "BKE_deform.h"
55
56
 
56
57
#include "ED_lattice.h"
57
58
#include "ED_object.h"
68
69
 
69
70
void free_editLatt(Object *ob)
70
71
{
71
 
        Lattice *lt= ob->data;
 
72
        Lattice *lt = ob->data;
72
73
        
73
74
        if (lt->editlatt) {
74
 
                Lattice *editlt= lt->editlatt->latt;
 
75
                Lattice *editlt = lt->editlatt->latt;
75
76
 
76
77
                if (editlt->def)
77
78
                        MEM_freeN(editlt->def);
78
79
                if (editlt->dvert)
79
 
                        free_dverts(editlt->dvert, editlt->pntsu*editlt->pntsv*editlt->pntsw);
 
80
                        BKE_defvert_array_free(editlt->dvert, editlt->pntsu * editlt->pntsv * editlt->pntsw);
80
81
 
81
82
                MEM_freeN(editlt);
82
83
                MEM_freeN(lt->editlatt);
83
84
 
84
 
                lt->editlatt= NULL;
 
85
                lt->editlatt = NULL;
85
86
        }
86
87
}
87
88
 
88
89
void make_editLatt(Object *obedit)
89
90
{
90
 
        Lattice *lt= obedit->data;
 
91
        Lattice *lt = obedit->data;
91
92
        KeyBlock *actkey;
92
93
 
93
94
        free_editLatt(obedit);
94
95
 
95
 
        actkey= ob_get_keyblock(obedit);
 
96
        actkey = BKE_keyblock_from_object(obedit);
96
97
        if (actkey)
97
 
                key_to_latt(actkey, lt);
 
98
                BKE_key_convert_to_lattice(actkey, lt);
98
99
 
99
 
        lt->editlatt= MEM_callocN(sizeof(EditLatt), "editlatt");
100
 
        lt->editlatt->latt= MEM_dupallocN(lt);
101
 
        lt->editlatt->latt->def= MEM_dupallocN(lt->def);
 
100
        lt->editlatt = MEM_callocN(sizeof(EditLatt), "editlatt");
 
101
        lt->editlatt->latt = MEM_dupallocN(lt);
 
102
        lt->editlatt->latt->def = MEM_dupallocN(lt->def);
102
103
 
103
104
        if (lt->dvert) {
104
 
                int tot= lt->pntsu*lt->pntsv*lt->pntsw;
105
 
                lt->editlatt->latt->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert");
106
 
                copy_dverts(lt->editlatt->latt->dvert, lt->dvert, tot);
 
105
                int tot = lt->pntsu * lt->pntsv * lt->pntsw;
 
106
                lt->editlatt->latt->dvert = MEM_mallocN(sizeof(MDeformVert) * tot, "Lattice MDeformVert");
 
107
                BKE_defvert_array_copy(lt->editlatt->latt->dvert, lt->dvert, tot);
107
108
        }
108
109
 
109
 
        if (lt->key) lt->editlatt->shapenr= obedit->shapenr;
 
110
        if (lt->key) lt->editlatt->shapenr = obedit->shapenr;
110
111
}
111
112
 
112
113
void load_editLatt(Object *obedit)
117
118
        float *fp;
118
119
        int tot;
119
120
 
120
 
        lt= obedit->data;
121
 
        editlt= lt->editlatt->latt;
 
121
        lt = obedit->data;
 
122
        editlt = lt->editlatt->latt;
122
123
 
123
124
        if (lt->editlatt->shapenr) {
124
 
                actkey= BLI_findlink(&lt->key->block, lt->editlatt->shapenr-1);
 
125
                actkey = BLI_findlink(&lt->key->block, lt->editlatt->shapenr - 1);
125
126
 
126
127
                /* active key: vertices */
127
 
                tot= editlt->pntsu*editlt->pntsv*editlt->pntsw;
 
128
                tot = editlt->pntsu * editlt->pntsv * editlt->pntsw;
128
129
                
129
130
                if (actkey->data) MEM_freeN(actkey->data);
130
131
                
131
 
                fp=actkey->data= MEM_callocN(lt->key->elemsize*tot, "actkey->data");
132
 
                actkey->totelem= tot;
 
132
                fp = actkey->data = MEM_callocN(lt->key->elemsize * tot, "actkey->data");
 
133
                actkey->totelem = tot;
133
134
 
134
 
                bp= editlt->def;
 
135
                bp = editlt->def;
135
136
                while (tot--) {
136
137
                        copy_v3_v3(fp, bp->vec);
137
 
                        fp+= 3;
 
138
                        fp += 3;
138
139
                        bp++;
139
140
                }
140
141
        }
141
142
        else {
142
143
                MEM_freeN(lt->def);
143
144
 
144
 
                lt->def= MEM_dupallocN(editlt->def);
145
 
 
146
 
                lt->flag= editlt->flag;
147
 
 
148
 
                lt->pntsu= editlt->pntsu;
149
 
                lt->pntsv= editlt->pntsv;
150
 
                lt->pntsw= editlt->pntsw;
 
145
                lt->def = MEM_dupallocN(editlt->def);
 
146
 
 
147
                lt->flag = editlt->flag;
 
148
 
 
149
                lt->pntsu = editlt->pntsu;
 
150
                lt->pntsv = editlt->pntsv;
 
151
                lt->pntsw = editlt->pntsw;
151
152
                
152
 
                lt->typeu= editlt->typeu;
153
 
                lt->typev= editlt->typev;
154
 
                lt->typew= editlt->typew;
 
153
                lt->typeu = editlt->typeu;
 
154
                lt->typev = editlt->typev;
 
155
                lt->typew = editlt->typew;
155
156
        }
156
157
 
157
158
        if (lt->dvert) {
158
 
                free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
159
 
                lt->dvert= NULL;
 
159
                BKE_defvert_array_free(lt->dvert, lt->pntsu * lt->pntsv * lt->pntsw);
 
160
                lt->dvert = NULL;
160
161
        }
161
162
 
162
163
        if (editlt->dvert) {
163
 
                tot= lt->pntsu*lt->pntsv*lt->pntsw;
 
164
                tot = lt->pntsu * lt->pntsv * lt->pntsw;
164
165
 
165
 
                lt->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert");
166
 
                copy_dverts(lt->dvert, editlt->dvert, tot);
 
166
                lt->dvert = MEM_mallocN(sizeof(MDeformVert) * tot, "Lattice MDeformVert");
 
167
                BKE_defvert_array_copy(lt->dvert, editlt->dvert, tot);
167
168
        }
168
169
}
169
170
 
170
 
/************************** Operators *************************/
 
171
/************************** Select All Operator *************************/
171
172
 
172
173
void ED_setflagsLatt(Object *obedit, int flag)
173
174
{
174
 
        Lattice *lt= obedit->data;
 
175
        Lattice *lt = obedit->data;
175
176
        BPoint *bp;
176
177
        int a;
177
178
        
178
 
        bp= lt->editlatt->latt->def;
 
179
        bp = lt->editlatt->latt->def;
179
180
        
180
 
        a= lt->editlatt->latt->pntsu*lt->editlatt->latt->pntsv*lt->editlatt->latt->pntsw;
 
181
        a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
181
182
        
182
183
        while (a--) {
183
 
                if (bp->hide==0) {
184
 
                        bp->f1= flag;
 
184
                if (bp->hide == 0) {
 
185
                        bp->f1 = flag;
185
186
                }
186
187
                bp++;
187
188
        }
189
190
 
190
191
static int lattice_select_all_exec(bContext *C, wmOperator *op)
191
192
{
192
 
        Object *obedit= CTX_data_edit_object(C);
193
 
        Lattice *lt= obedit->data;
 
193
        Object *obedit = CTX_data_edit_object(C);
 
194
        Lattice *lt = obedit->data;
194
195
        BPoint *bp;
195
196
        int a;
196
197
        int action = RNA_enum_get(op->ptr, "action");
198
199
        if (action == SEL_TOGGLE) {
199
200
                action = SEL_SELECT;
200
201
 
201
 
                bp= lt->editlatt->latt->def;
202
 
                a= lt->editlatt->latt->pntsu*lt->editlatt->latt->pntsv*lt->editlatt->latt->pntsw;
 
202
                bp = lt->editlatt->latt->def;
 
203
                a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
203
204
 
204
205
                while (a--) {
205
 
                        if (bp->hide==0) {
206
 
                                if (bp->f1) {
 
206
                        if (bp->hide == 0) {
 
207
                                if (bp->f1 & SELECT) {
207
208
                                        action = SEL_DESELECT;
208
209
                                        break;
209
210
                                }
213
214
        }
214
215
 
215
216
        switch (action) {
216
 
        case SEL_SELECT:
217
 
                ED_setflagsLatt(obedit, 1);
218
 
                break;
219
 
        case SEL_DESELECT:
220
 
                ED_setflagsLatt(obedit, 0);
221
 
                break;
222
 
        case SEL_INVERT:
223
 
                bp= lt->editlatt->latt->def;
224
 
                a= lt->editlatt->latt->pntsu*lt->editlatt->latt->pntsv*lt->editlatt->latt->pntsw;
 
217
                case SEL_SELECT:
 
218
                        ED_setflagsLatt(obedit, 1);
 
219
                        break;
 
220
                case SEL_DESELECT:
 
221
                        ED_setflagsLatt(obedit, 0);
 
222
                        break;
 
223
                case SEL_INVERT:
 
224
                        bp = lt->editlatt->latt->def;
 
225
                        a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
225
226
 
226
 
                while (a--) {
227
 
                        if (bp->hide==0) {
228
 
                                bp->f1 ^= 1;
 
227
                        while (a--) {
 
228
                                if (bp->hide == 0) {
 
229
                                        bp->f1 ^= SELECT;
 
230
                                }
 
231
                                bp++;
229
232
                        }
230
 
                        bp++;
231
 
                }
232
 
                break;
 
233
                        break;
233
234
        }
234
235
 
235
 
        WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
 
236
        WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
236
237
 
237
238
        return OPERATOR_FINISHED;
238
239
}
249
250
        ot->poll = ED_operator_editlattice;
250
251
        
251
252
        /* flags */
252
 
        ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 
253
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
253
254
 
254
255
        WM_operator_properties_select_all(ot);
255
256
}
256
257
 
 
258
/************************** Make Regular Operator *************************/
 
259
 
257
260
static int make_regular_poll(bContext *C)
258
261
{
259
262
        Object *ob;
260
263
 
261
264
        if (ED_operator_editlattice(C)) return 1;
262
265
 
263
 
        ob= CTX_data_active_object(C);
264
 
        return (ob && ob->type==OB_LATTICE);
 
266
        ob = CTX_data_active_object(C);
 
267
        return (ob && ob->type == OB_LATTICE);
265
268
}
266
269
 
267
270
static int make_regular_exec(bContext *C, wmOperator *UNUSED(op))
268
271
{
269
 
        Object *ob= CTX_data_edit_object(C);
 
272
        Object *ob = CTX_data_edit_object(C);
270
273
        Lattice *lt;
271
274
        
272
275
        if (ob) {
273
 
                lt= ob->data;
274
 
                resizelattice(lt->editlatt->latt, lt->pntsu, lt->pntsv, lt->pntsw, NULL);
 
276
                lt = ob->data;
 
277
                BKE_lattice_resize(lt->editlatt->latt, lt->pntsu, lt->pntsv, lt->pntsw, NULL);
275
278
        }
276
279
        else {
277
 
                ob= CTX_data_active_object(C);
278
 
                lt= ob->data;
279
 
                resizelattice(lt, lt->pntsu, lt->pntsv, lt->pntsw, NULL);
 
280
                ob = CTX_data_active_object(C);
 
281
                lt = ob->data;
 
282
                BKE_lattice_resize(lt, lt->pntsu, lt->pntsv, lt->pntsw, NULL);
280
283
        }
281
284
        
282
285
        DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
283
 
        WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 
286
        WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
284
287
 
285
288
        return OPERATOR_FINISHED;
286
289
}
297
300
        ot->poll = make_regular_poll;
298
301
        
299
302
        /* flags */
300
 
        ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 
303
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 
304
}
 
305
 
 
306
/************************** Flip Verts Operator *************************/
 
307
 
 
308
/* flipping options */
 
309
typedef enum eLattice_FlipAxes {
 
310
        LATTICE_FLIP_U = 0,
 
311
        LATTICE_FLIP_V = 1,
 
312
        LATTICE_FLIP_W = 2
 
313
} eLattice_FlipAxes;
 
314
 
 
315
/* Helper macro for accessing item at index (u, v, w) 
 
316
 * < lt: (Lattice)
 
317
 * < U: (int) u-axis coordinate of point
 
318
 * < V: (int) v-axis coordinate of point
 
319
 * < W: (int) w-axis coordinate of point
 
320
 * < dimU: (int) number of points per row or number of columns (U-Axis)
 
321
 * < dimV: (int) number of rows (V-Axis)
 
322
 * > returns: (BPoint *) pointer to BPoint at this index
 
323
 */
 
324
#define LATTICE_PT(lt, U, V, W, dimU, dimV)       \
 
325
        ( (lt)->def               +                   \
 
326
          ((dimU) * (dimV)) * (W) +                   \
 
327
          (dimU) * (V)            +                   \
 
328
          (U)                                         \
 
329
        )
 
330
        
 
331
/* Flip midpoint value so that relative distances between midpoint and neighbour-pair is maintained
 
332
 * ! Assumes that uvw <=> xyz (i.e. axis-aligned index-axes with coordinate-axes)
 
333
 * - Helper for lattice_flip_exec()
 
334
 */
 
335
static void lattice_flip_point_value(Lattice *lt, int u, int v, int w, float mid, eLattice_FlipAxes axis)
 
336
{
 
337
        BPoint *bp;
 
338
        float diff;
 
339
        
 
340
        /* just the point in the middle (unpaired) */
 
341
        bp = LATTICE_PT(lt, u, v, w, lt->pntsu, lt->pntsv);
 
342
        
 
343
        /* flip over axis */
 
344
        diff = mid - bp->vec[axis];
 
345
        bp->vec[axis] = mid + diff;
 
346
}
 
347
 
 
348
/* Swap pairs of lattice points along a specified axis
 
349
 * - Helper for lattice_flip_exec()
 
350
 */
 
351
static void lattice_swap_point_pairs(Lattice *lt, int u, int v, int w, float mid, eLattice_FlipAxes axis)
 
352
{
 
353
        BPoint *bpA, *bpB;
 
354
        
 
355
        int numU = lt->pntsu;
 
356
        int numV = lt->pntsv;
 
357
        int numW = lt->pntsw;
 
358
        
 
359
        int u0 = u, u1 = u;
 
360
        int v0 = v, v1 = v;
 
361
        int w0 = w, w1 = w;
 
362
        
 
363
        /* get pair index by just overriding the relevant pair-value
 
364
         * - "-1" else buffer overflow
 
365
         */
 
366
        switch (axis) {
 
367
                case LATTICE_FLIP_U:
 
368
                        u1 = numU - u - 1;
 
369
                        break;
 
370
                case LATTICE_FLIP_V:
 
371
                        v1 = numV - v - 1;
 
372
                        break;
 
373
                case LATTICE_FLIP_W:
 
374
                        w1 = numW - w - 1;
 
375
                        break;
 
376
        }
 
377
        
 
378
        /* get points to operate on */
 
379
        bpA = LATTICE_PT(lt, u0, v0, w0, numU, numV);
 
380
        bpB = LATTICE_PT(lt, u1, v1, w1, numU, numV);
 
381
        
 
382
        /* Swap all coordinates, so that flipped coordinates belong to
 
383
         * the indices on the correct side of the lattice.
 
384
         *
 
385
         *   Coords:  (-2 4) |0| (3 4)   --> (3 4) |0| (-2 4) 
 
386
         *   Indices:  (0,L)     (1,R)   --> (0,L)     (1,R)
 
387
         */
 
388
        swap_v3_v3(bpA->vec, bpB->vec);
 
389
        
 
390
        /* However, we need to mirror the coordinate values on the axis we're dealing with,
 
391
         * otherwise we'd have effectively only rotated the points around. If we don't do this,
 
392
         * we'd just be reimplementing the naive mirroring algorithm, which causes unwanted deforms
 
393
         * such as flipped normals, etc.
 
394
         *
 
395
         *   Coords:  (3 4) |0| (-2 4)  --\   
 
396
         *                                 \-> (-3 4) |0| (2 4)
 
397
         *   Indices: (0,L)     (1,R)   -->     (0,L)     (1,R)
 
398
         */
 
399
        lattice_flip_point_value(lt, u0, v0, w0, mid, axis);
 
400
        lattice_flip_point_value(lt, u1, v1, w1, mid, axis);
 
401
}
 
402
        
 
403
static int lattice_flip_exec(bContext *C, wmOperator *op)
 
404
{
 
405
        Object *obedit = CTX_data_edit_object(C);
 
406
        Lattice *lt;
 
407
        
 
408
        eLattice_FlipAxes axis = RNA_enum_get(op->ptr, "axis");
 
409
        int numU, numV, numW;
 
410
        int totP;
 
411
        
 
412
        float mid = 0.0f;
 
413
        short isOdd = 0;
 
414
        
 
415
        /* get lattice - we need the "edit lattice" from the lattice... confusing... */
 
416
        lt = (Lattice *)obedit->data;
 
417
        lt = lt->editlatt->latt;
 
418
        
 
419
        numU = lt->pntsu;
 
420
        numV = lt->pntsv;
 
421
        numW = lt->pntsw;
 
422
        totP = numU * numV * numW;
 
423
        
 
424
        /* First Pass: determine midpoint - used for flipping center verts if there are odd number of points on axis */
 
425
        switch (axis) {
 
426
                case LATTICE_FLIP_U:
 
427
                        isOdd = numU & 1;
 
428
                        break;
 
429
                case LATTICE_FLIP_V:
 
430
                        isOdd = numV & 1;
 
431
                        break;
 
432
                case LATTICE_FLIP_W:
 
433
                        isOdd = numW & 1;
 
434
                        break;
 
435
                        
 
436
                default:
 
437
                        printf("lattice_flip(): Unknown flipping axis (%d)\n", axis);
 
438
                        return OPERATOR_CANCELLED;
 
439
        }
 
440
        
 
441
        if (isOdd) {
 
442
                BPoint *bp;
 
443
                float avgInv = 1.0f / (float)totP;
 
444
                int i;
 
445
                
 
446
                /* midpoint calculation - assuming that u/v/w are axis-aligned */
 
447
                for (i = 0, bp = lt->def; i < totP; i++, bp++) {
 
448
                        mid += bp->vec[axis] * avgInv;
 
449
                }
 
450
        }
 
451
        
 
452
        /* Second Pass: swap pairs of vertices per axis, assuming they are all sorted */
 
453
        switch (axis) {
 
454
                case LATTICE_FLIP_U:
 
455
                {
 
456
                        int u, v, w;
 
457
                        
 
458
                        /* v/w strips - front to back, top to bottom */
 
459
                        for (w = 0; w < numW; w++) {
 
460
                                for (v = 0; v < numV; v++) {
 
461
                                        /* swap coordinates of pairs of vertices on u */
 
462
                                        for (u = 0; u < (numU / 2); u++) {
 
463
                                                lattice_swap_point_pairs(lt, u, v, w, mid, axis);
 
464
                                        }
 
465
                                        
 
466
                                        /* flip u-coordinate of midpoint (i.e. unpaired point on u) */
 
467
                                        if (isOdd) {
 
468
                                                u = (numU / 2);
 
469
                                                lattice_flip_point_value(lt, u, v, w, mid, axis);
 
470
                                        }
 
471
                                }
 
472
                        }
 
473
                }
 
474
                break;
 
475
                case LATTICE_FLIP_V:
 
476
                {
 
477
                        int u, v, w;
 
478
                        
 
479
                        /* u/w strips - front to back, left to right */
 
480
                        for (w = 0; w < numW; w++) {
 
481
                                for (u = 0; u < numU; u++) {
 
482
                                        /* swap coordinates of pairs of vertices on v */
 
483
                                        for (v = 0; v < (numV / 2); v++) {
 
484
                                                lattice_swap_point_pairs(lt, u, v, w, mid, axis);
 
485
                                        }
 
486
                                        
 
487
                                        /* flip v-coordinate of midpoint (i.e. unpaired point on v) */
 
488
                                        if (isOdd) {
 
489
                                                v = (numV / 2);
 
490
                                                lattice_flip_point_value(lt, u, v, w, mid, axis);
 
491
                                        }
 
492
                                }
 
493
                        }
 
494
                }
 
495
                break;
 
496
                case LATTICE_FLIP_W:
 
497
                {
 
498
                        int u, v, w;
 
499
                        
 
500
                        for (v = 0; v < numV; v++) {
 
501
                                for (u = 0; u < numU; u++) {
 
502
                                        /* swap coordinates of pairs of vertices on w */
 
503
                                        for (w = 0; w < (numW / 2); w++) {
 
504
                                                lattice_swap_point_pairs(lt, u, v, w, mid, axis);
 
505
                                        }
 
506
                                        
 
507
                                        /* flip w-coordinate of midpoint (i.e. unpaired point on w) */
 
508
                                        if (isOdd) {
 
509
                                                w = (numW / 2);
 
510
                                                lattice_flip_point_value(lt, u, v, w, mid, axis);
 
511
                                        }
 
512
                                }
 
513
                        }
 
514
                }
 
515
                break;
 
516
                
 
517
                default: /* shouldn't happen, but just in case */
 
518
                        break;
 
519
        }
 
520
        
 
521
        /* updates */
 
522
        DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
 
523
        WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
 
524
        
 
525
        return OPERATOR_FINISHED;
 
526
}
 
527
 
 
528
void LATTICE_OT_flip(wmOperatorType *ot)
 
529
{
 
530
        static EnumPropertyItem flip_items[] = {
 
531
                {LATTICE_FLIP_U, "U", 0, "U (X) Axis", ""},
 
532
                {LATTICE_FLIP_V, "V", 0, "V (Y) Axis", ""},
 
533
                {LATTICE_FLIP_W, "W", 0, "W (Z) Axis", ""},
 
534
                {0, NULL, 0, NULL, NULL}};
 
535
        
 
536
        /* identifiers */
 
537
        ot->name = "Flip (Distortion Free)";
 
538
        ot->description = "Mirror all control points without inverting the lattice deform";
 
539
        ot->idname = "LATTICE_OT_flip";
 
540
        
 
541
        /* api callbacks */
 
542
        ot->poll = ED_operator_editlattice;
 
543
        ot->invoke = WM_menu_invoke;
 
544
        ot->exec = lattice_flip_exec;
 
545
        
 
546
        /* flags */
 
547
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 
548
        
 
549
        /* properties */
 
550
        ot->prop = RNA_def_enum(ot->srna, "axis", flip_items, LATTICE_FLIP_U, "Flip Axis", "Coordinates along this axis get flipped");
301
551
}
302
552
 
303
553
/****************************** Mouse Selection *************************/
304
554
 
305
 
static void findnearestLattvert__doClosest(void *userData, BPoint *bp, int x, int y)
 
555
static void findnearestLattvert__doClosest(void *userData, BPoint *bp, const float screen_co[2])
306
556
{
307
 
        struct { BPoint *bp; short dist, select; int mval[2]; } *data = userData;
308
 
        float temp = abs(data->mval[0]-x) + abs(data->mval[1]-y);
 
557
        struct { BPoint *bp; float dist; int select; float mval_fl[2]; } *data = userData;
 
558
        float dist_test = len_manhattan_v2v2(data->mval_fl, screen_co);
309
559
        
310
 
        if ((bp->f1 & SELECT)==data->select)
311
 
                temp += 5;
 
560
        if ((bp->f1 & SELECT) && data->select)
 
561
                dist_test += 5.0f;
312
562
 
313
 
        if (temp<data->dist) {
314
 
                data->dist = temp;
 
563
        if (dist_test < data->dist) {
 
564
                data->dist = dist_test;
315
565
 
316
566
                data->bp = bp;
317
567
        }
319
569
 
320
570
static BPoint *findnearestLattvert(ViewContext *vc, const int mval[2], int sel)
321
571
{
322
 
                /* sel==1: selected gets a disadvantage */
323
 
                /* in nurb and bezt or bp the nearest is written */
324
 
                /* return 0 1 2: handlepunt */
325
 
        struct { BPoint *bp; short dist, select; int mval[2]; } data = {NULL};
 
572
        /* (sel == 1): selected gets a disadvantage */
 
573
        /* in nurb and bezt or bp the nearest is written */
 
574
        /* return 0 1 2: handlepunt */
 
575
        struct { BPoint *bp; float dist; int select; float mval_fl[2]; } data = {NULL};
326
576
 
327
577
        data.dist = 100;
328
578
        data.select = sel;
329
 
        data.mval[0]= mval[0];
330
 
        data.mval[1]= mval[1];
 
579
        data.mval_fl[0] = mval[0];
 
580
        data.mval_fl[1] = mval[1];
331
581
 
332
582
        ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
333
 
        lattice_foreachScreenVert(vc, findnearestLattvert__doClosest, &data);
 
583
        lattice_foreachScreenVert(vc, findnearestLattvert__doClosest, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
334
584
 
335
585
        return data.bp;
336
586
}
337
587
 
338
 
int mouse_lattice(bContext *C, const int mval[2], int extend)
 
588
int mouse_lattice(bContext *C, const int mval[2], int extend, int deselect, int toggle)
339
589
{
340
590
        ViewContext vc;
341
 
        BPoint *bp= NULL;
 
591
        BPoint *bp = NULL;
342
592
 
343
593
        view3d_set_viewcontext(C, &vc);
344
 
        bp= findnearestLattvert(&vc, mval, 1);
 
594
        bp = findnearestLattvert(&vc, mval, TRUE);
345
595
 
346
596
        if (bp) {
347
 
                if (extend==0) {
 
597
                if (extend) {
 
598
                        bp->f1 |= SELECT;
 
599
                }
 
600
                else if (deselect) {
 
601
                        bp->f1 &= ~SELECT;
 
602
                }
 
603
                else if (toggle) {
 
604
                        bp->f1 ^= SELECT;  /* swap */
 
605
                }
 
606
                else {
348
607
                        ED_setflagsLatt(vc.obedit, 0);
349
608
                        bp->f1 |= SELECT;
350
609
                }
351
 
                else
352
 
                        bp->f1 ^= SELECT; /* swap */
353
610
 
354
 
                WM_event_add_notifier(C, NC_GEOM|ND_SELECT, vc.obedit->data);
 
611
                WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
355
612
 
356
613
                return 1;
357
614
        }
368
625
 
369
626
static void undoLatt_to_editLatt(void *data, void *edata, void *UNUSED(obdata))
370
627
{
371
 
        UndoLattice *ult= (UndoLattice*)data;
372
 
        EditLatt *editlatt= (EditLatt *)edata;
373
 
        int a= editlatt->latt->pntsu*editlatt->latt->pntsv*editlatt->latt->pntsw;
 
628
        UndoLattice *ult = (UndoLattice *)data;
 
629
        EditLatt *editlatt = (EditLatt *)edata;
 
630
        int a = editlatt->latt->pntsu * editlatt->latt->pntsv * editlatt->latt->pntsw;
374
631
 
375
 
        memcpy(editlatt->latt->def, ult->def, a*sizeof(BPoint));
 
632
        memcpy(editlatt->latt->def, ult->def, a * sizeof(BPoint));
376
633
}
377
634
 
378
635
static void *editLatt_to_undoLatt(void *edata, void *UNUSED(obdata))
379
636
{
380
 
        UndoLattice *ult= MEM_callocN(sizeof(UndoLattice), "UndoLattice");
381
 
        EditLatt *editlatt= (EditLatt *)edata;
 
637
        UndoLattice *ult = MEM_callocN(sizeof(UndoLattice), "UndoLattice");
 
638
        EditLatt *editlatt = (EditLatt *)edata;
382
639
        
383
 
        ult->def= MEM_dupallocN(editlatt->latt->def);
384
 
        ult->pntsu= editlatt->latt->pntsu;
385
 
        ult->pntsv= editlatt->latt->pntsv;
386
 
        ult->pntsw= editlatt->latt->pntsw;
 
640
        ult->def = MEM_dupallocN(editlatt->latt->def);
 
641
        ult->pntsu = editlatt->latt->pntsu;
 
642
        ult->pntsv = editlatt->latt->pntsv;
 
643
        ult->pntsw = editlatt->latt->pntsw;
387
644
        
388
645
        return ult;
389
646
}
390
647
 
391
648
static void free_undoLatt(void *data)
392
649
{
393
 
        UndoLattice *ult= (UndoLattice*)data;
 
650
        UndoLattice *ult = (UndoLattice *)data;
394
651
 
395
652
        if (ult->def) MEM_freeN(ult->def);
396
653
        MEM_freeN(ult);
398
655
 
399
656
static int validate_undoLatt(void *data, void *edata)
400
657
{
401
 
        UndoLattice *ult= (UndoLattice*)data;
402
 
        EditLatt *editlatt= (EditLatt *)edata;
 
658
        UndoLattice *ult = (UndoLattice *)data;
 
659
        EditLatt *editlatt = (EditLatt *)edata;
403
660
 
404
661
        return (ult->pntsu == editlatt->latt->pntsu &&
405
 
                        ult->pntsv == editlatt->latt->pntsv &&
406
 
                        ult->pntsw == editlatt->latt->pntsw);
 
662
                ult->pntsv == editlatt->latt->pntsv &&
 
663
                ult->pntsw == editlatt->latt->pntsw);
407
664
}
408
665
 
409
666
static void *get_editlatt(bContext *C)
410
667
{
411
 
        Object *obedit= CTX_data_edit_object(C);
 
668
        Object *obedit = CTX_data_edit_object(C);
412
669
 
413
 
        if (obedit && obedit->type==OB_LATTICE) {
414
 
                Lattice *lt= obedit->data;
 
670
        if (obedit && obedit->type == OB_LATTICE) {
 
671
                Lattice *lt = obedit->data;
415
672
                return lt->editlatt;
416
673
        }
417
674