4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
33 #include "BIF_transform.h"
35 /* ************************** Types ***************************** */
48 struct wmOperatorType;
53 typedef struct NDofInput {
60 typedef struct NumInput {
63 short flag; /* Different flags to indicate different behaviors */
64 float val[3]; /* Direct value of the input */
65 int ctrl[3]; /* Control to indicate what to do with the numbers that are typed */
69 The ctrl value has different meaning:
70 0 : No value has been typed
72 otherwise, |value| - 1 is where the cursor is located after the period
73 Positive : number is positive
74 Negative : number is negative
77 typedef struct TransSnap {
85 float dist; // Distance from snapPoint to snapTarget
87 void (*applySnap)(struct TransInfo *, float *);
88 void (*calcSnap)(struct TransInfo *, float *);
89 void (*targetSnap)(struct TransInfo *);
90 float (*distance)(struct TransInfo *, float p1[3], float p2[3]); // Get the transform distance between two points (used by Closest snap)
93 typedef struct TransCon {
94 char text[50]; /* Description of the Constraint for header_print */
95 float mtx[3][3]; /* Matrix of the Constraint space */
96 float imtx[3][3]; /* Inverse Matrix of the Constraint space */
97 float pmtx[3][3]; /* Projection Constraint Matrix (same as imtx with some axis == 0) */
98 float center[3]; /* transformation center to define where to draw the view widget
99 ALWAYS in global space. Unlike the transformation center */
100 short imval[2]; /* initial mouse value for visual calculation */
101 /* the one in TransInfo is not garanty to stay the same (Rotates change it) */
102 int mode; /* Mode flags of the Constraint */
103 void (*drawExtra)(struct TransInfo *);
104 /* For constraints that needs to draw differently from the other
105 uses this instead of the generic draw function */
106 void (*applyVec)(struct TransInfo *, struct TransData *, float *, float *, float *);
107 /* Apply function pointer for linear vectorial transformation */
108 /* The last three parameters are pointers to the in/out/printable vectors */
109 void (*applySize)(struct TransInfo *, struct TransData *, float [3][3]);
110 /* Apply function pointer for size transformation */
111 void (*applyRot)(struct TransInfo *, struct TransData *, float [3], float *);
112 /* Apply function pointer for rotation transformation */
115 typedef struct TransDataIpokey {
116 int flag; /* which keys */
117 float *locx, *locy, *locz; /* channel pointers */
118 float *rotx, *roty, *rotz;
119 float *quatx, *quaty, *quatz, *quatw;
120 float *sizex, *sizey, *sizez;
121 float oldloc[9]; /* storage old values */
127 typedef struct TransDataExtension {
128 float drot[3]; /* Initial object drot */
129 float dsize[3]; /* Initial object dsize */
130 float *rot; /* Rotation of the data to transform (Faculative) */
131 float irot[3]; /* Initial rotation */
132 float *quat; /* Rotation quaternion of the data to transform (Faculative) */
133 float iquat[4]; /* Initial rotation quaternion */
134 float *size; /* Size of the data to transform (Faculative) */
135 float isize[3]; /* Initial size */
136 float obmat[4][4]; /* Object matrix */
137 } TransDataExtension;
139 typedef struct TransData2D {
140 float loc[3]; /* Location of data used to transform (x,y,0) */
141 float *loc2d; /* Pointer to real 2d location of data */
144 /* we need to store 2 handles for each transdata incase the other handle wasnt selected */
145 typedef struct TransDataCurveHandleFlags {
148 } TransDataCurveHandleFlags;
151 typedef struct TransData {
152 float dist; /* Distance needed to affect element (for Proportionnal Editing) */
153 float rdist; /* Distance to the nearest element (for Proportionnal Editing) */
154 float factor; /* Factor of the transformation (for Proportionnal Editing) */
155 float *loc; /* Location of the data to transform */
156 float iloc[3]; /* Initial location */
157 float *val; /* Value pointer for special transforms */
158 float ival; /* Old value*/
159 float center[3]; /* Individual data center */
160 float mtx[3][3]; /* Transformation matrix from data space to global space */
161 float smtx[3][3]; /* Transformation matrix from global space to data space */
162 float axismtx[3][3];/* Axis orientation matrix of the data */
164 struct bConstraint *con; /* for objects/bones, the first constraint in its constraint stack */
165 TransDataExtension *ext; /* for objects, poses. 1 single malloc per TransInfo! */
166 TransDataIpokey *tdi; /* for objects, ipo keys. per transdata a malloc */
167 TransDataCurveHandleFlags *hdata; /* for curves, stores handle flags for modification/cancel */
168 void *extra; /* extra data (mirrored element pointer, in editmode mesh to EditVert) (editbone for roll fixing) (...) */
169 short flag; /* Various flags */
170 short protectflag; /* If set, copy of Object or PoseChannel protection */
171 /*#ifdef WITH_VERSE*/
172 void *verse; /* pointer at verse data struct (VerseVert, etc.) */
176 typedef struct TransInfo {
177 int mode; /* current mode */
178 int flag; /* generic flags for special behaviors */
179 short state; /* current state (running, canceled,...)*/
180 int context; /* current context */
181 float val; /* init value for some transformations (and rotation angle) */
182 float fac; /* factor for distance based transform */
183 int (*transform)(struct TransInfo *, short *);
184 /* transform function pointer */
185 int (*handleEvent)(struct TransInfo *, struct wmEvent *event);
186 /* event handler function pointer RETURN 1 if redraw is needed */
187 int total; /* total number of transformed data */
188 TransData *data; /* transformed data (array) */
189 TransDataExtension *ext; /* transformed data extension (array) */
190 TransData2D *data2d; /* transformed data for 2d (array) */
191 TransCon con; /* transformed constraint */
193 NumInput num; /* numerical input */
194 NDofInput ndof; /* ndof input */
195 char redraw; /* redraw flag */
196 float propsize; /* proportional circle radius */
197 char proptext[20]; /* proportional falloff text */
198 float center[3]; /* center of transformation */
199 int center2d[2]; /* center in screen coordinates */
200 short imval[2]; /* initial mouse position */
201 short shiftmval[2]; /* mouse position when shift was pressed */
202 short idx_max; /* maximum index on the input vector */
203 float snap[3]; /* Snapping Gears */
205 float viewmat[4][4]; /* copy from G.vd, prevents feedback, */
206 float viewinv[4][4]; /* and to make sure we don't have to */
207 float persmat[4][4]; /* access G.vd from other space types */
211 char spacetype; /* spacetype where transforming is */
213 float vec[3]; /* translation, to show for widget */
214 float mat[3][3]; /* rot/rescale, to show for widget */
216 char *undostr; /* if set, uses this string for undo */
217 float spacemtx[3][3]; /* orientation matrix of the current space */
218 char spacename[32]; /* name of the current space */
220 struct Object *poseobj; /* if t->flag & T_POSE, this denotes pose object */
222 void *customData; /* Per Transform custom data */
224 /*************** NEW STUFF *********************/
231 struct wmEvent *event; /* last event, reset at the start of each transformApply and nulled at the transformEnd */
232 short mval[2]; /* current mouse position */
236 /* ******************** Macros & Prototypes *********************** */
239 #define NUM_NULL_ONE 2
240 #define NUM_NO_NEGATIVE 4
241 #define NUM_NO_ZERO 8
242 #define NUM_NO_FRACTION 16
243 #define NUM_AFFECT_ALL 32
245 /* NDOFINPUT FLAGS */
248 /* transinfo->state */
249 #define TRANS_RUNNING 0
250 #define TRANS_CONFIRM 1
251 #define TRANS_CANCEL 2
253 /* transinfo->flag */
254 #define T_OBJECT (1 << 0)
255 #define T_EDIT (1 << 1)
256 #define T_POSE (1 << 2)
257 #define T_TEXTURE (1 << 3)
258 #define T_CAMERA (1 << 4)
259 // when shift pressed, higher resolution transform. cannot rely on G.qual, need event!
260 #define T_SHIFT_MOD (1 << 5)
261 // trans on points, having no rotation/scale
262 #define T_POINTS (1 << 6)
263 // for manipulator exceptions, like scaling using center point, drawing help lines
264 #define T_USES_MANIPULATOR (1 << 7)
266 /* restrictions flags */
267 #define T_ALL_RESTRICTIONS ((1 << 8)|(1 << 9)|(1 << 10))
268 #define T_NO_CONSTRAINT (1 << 8)
269 #define T_NULL_ONE (1 << 9)
270 #define T_NO_ZERO (1 << 10)
272 #define T_PROP_EDIT (1 << 11)
273 #define T_PROP_CONNECTED (1 << 12)
275 /* if MMB is pressed or not */
276 #define T_MMB_PRESSED (1 << 13)
278 #define T_V3D_ALIGN (1 << 14)
279 /* for 2d views like uv or ipo */
280 #define T_2D_EDIT (1 << 15)
281 #define T_CLIP_UV (1 << 16)
283 #define T_FREE_CUSTOMDATA (1 << 17)
285 #define T_AUTOIK (1 << 18)
287 /* ******************************************************************************** */
289 /* transinfo->con->mode */
294 #define CON_SELECT 16
295 #define CON_NOFLIP 32 /* does not reorient vector to face viewport when on */
299 /* transdata->flag */
300 #define TD_SELECTED 1
301 #define TD_ACTIVE (1 << 1)
302 #define TD_NOACTION (1 << 2)
303 #define TD_USEQUAT (1 << 3)
304 #define TD_NOTCONNECTED (1 << 4)
305 #define TD_SINGLESIZE (1 << 5) /* used for scaling of MetaElem->rad */
307 #define TD_VERSE_OBJECT (1 << 6)
308 #define TD_VERSE_VERT (1 << 7)
310 #define TD_TIMEONLY (1 << 8)
311 #define TD_NOCENTER (1 << 9)
312 #define TD_NO_EXT (1 << 10) /* ext abused for particle key timing */
313 #define TD_SKIP (1 << 11) /* don't transform this data */
314 #define TD_BEZTRIPLE (1 << 12) /* if this is a bez triple, we need to restore the handles, if this is set transdata->misc.hdata needs freeing */
315 #define TD_NO_LOC (1 << 13) /* when this is set, don't apply translation changes to this element */
317 /* transsnap->status */
319 #define TARGET_INIT 2
322 /* transsnap->modePoint */
326 /* transsnap->modeTarget */
327 #define SNAP_CLOSEST 0
328 #define SNAP_CENTER 1
329 #define SNAP_MEDIAN 2
330 #define SNAP_ACTIVE 3
333 void TFM_OT_transform(struct wmOperatorType *ot);
335 void initTransform(struct bContext *C, struct TransInfo *t, int mode, int context, struct wmEvent *event);
336 void transformEvent(TransInfo *t, struct wmEvent *event);
337 void transformApply(TransInfo *t);
338 int transformEnd(TransInfo *t);
340 void setTransformViewMatrices(TransInfo *t);
341 void convertViewVec(TransInfo *t, float *vec, short dx, short dy);
342 void projectIntView(TransInfo *t, float *vec, int *adr);
343 void projectFloatView(TransInfo *t, float *vec, float *adr);
345 void convertVecToDisplayNum(float *vec, float *num);
346 void convertDisplayNumToVec(float *num, float *vec);
348 void initWarp(TransInfo *t);
349 int handleEventWarp(TransInfo *t, struct wmEvent *event);
350 int Warp(TransInfo *t, short mval[2]);
352 void initShear(TransInfo *t);
353 int handleEventShear(TransInfo *t, struct wmEvent *event);
354 int Shear(TransInfo *t, short mval[2]);
356 void initResize(TransInfo *t);
357 int Resize(TransInfo *t, short mval[2]);
359 void initTranslation(TransInfo *t);
360 int Translation(TransInfo *t, short mval[2]);
362 void initToSphere(TransInfo *t);
363 int ToSphere(TransInfo *t, short mval[2]);
365 void initRotation(TransInfo *t);
366 int Rotation(TransInfo *t, short mval[2]);
368 void initShrinkFatten(TransInfo *t);
369 int ShrinkFatten(TransInfo *t, short mval[2]);
371 void initTilt(TransInfo *t);
372 int Tilt(TransInfo *t, short mval[2]);
374 void initCurveShrinkFatten(TransInfo *t);
375 int CurveShrinkFatten(TransInfo *t, short mval[2]);
377 void initTrackball(TransInfo *t);
378 int Trackball(TransInfo *t, short mval[2]);
380 void initPushPull(TransInfo *t);
381 int PushPull(TransInfo *t, short mval[2]);
383 void initBevel(TransInfo *t);
384 int handleEventBevel(TransInfo *t, struct wmEvent *event);
385 int Bevel(TransInfo *t, short mval[2]);
387 void initBevelWeight(TransInfo *t);
388 int BevelWeight(TransInfo *t, short mval[2]);
390 void initCrease(TransInfo *t);
391 int Crease(TransInfo *t, short mval[2]);
393 void initBoneSize(TransInfo *t);
394 int BoneSize(TransInfo *t, short mval[2]);
396 void initBoneEnvelope(TransInfo *t);
397 int BoneEnvelope(TransInfo *t, short mval[2]);
399 void initBoneRoll(TransInfo *t);
400 int BoneRoll(TransInfo *t, short mval[2]);
402 void initTimeTranslate(TransInfo *t);
403 int TimeTranslate(TransInfo *t, short mval[2]);
405 void initTimeSlide(TransInfo *t);
406 int TimeSlide(TransInfo *t, short mval[2]);
408 void initTimeScale(TransInfo *t);
409 int TimeScale(TransInfo *t, short mval[2]);
411 void initBakeTime(TransInfo *t);
412 int BakeTime(TransInfo *t, short mval[2]);
414 void initMirror(TransInfo *t);
415 int Mirror(TransInfo *t, short mval[2]);
417 void initAlign(TransInfo *t);
418 int Align(TransInfo *t, short mval[2]);
420 /*********************** transform_conversions.c ********** */
422 void flushTransGPactionData(TransInfo *t);
423 void flushTransIpoData(TransInfo *t);
424 void flushTransUVs(TransInfo *t);
425 void flushTransParticles(TransInfo *t);
426 int clipUVTransform(TransInfo *t, float *vec, int resize);
428 /*********************** exported from transform_manipulator.c ********** */
429 void draw_manipulator_ext(struct ScrArea *sa, int type, char axis, int col, float vec[3], float mat[][3]);
430 int calc_manipulator_stats(struct ScrArea *sa);
431 float get_drawsize(struct View3D *v3d, struct ScrArea *sa, float *co);
433 /*********************** TransData Creation and General Handling *********** */
434 void createTransData(struct bContext *C, TransInfo *t);
435 void sort_trans_data_dist(TransInfo *t);
436 void add_tdi_poin(float *poin, float *old, float delta);
437 void special_aftertrans_update(TransInfo *t);
439 void transform_autoik_update(TransInfo *t, short mode);
441 /* auto-keying stuff used by special_aftertrans_update */
442 short autokeyframe_cfra_can_key(struct Object *ob);
443 void autokeyframe_ob_cb_func(struct Object *ob, int tmode);
444 void autokeyframe_pose_cb_func(struct Object *ob, int tmode, short targetless_ik);
446 /*********************** Constraints *****************************/
448 void getConstraintMatrix(TransInfo *t);
449 void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[]);
450 void setLocalConstraint(TransInfo *t, int mode, const char text[]);
451 void setUserConstraint(TransInfo *t, int mode, const char text[]);
453 void constraintNumInput(TransInfo *t, float vec[3]);
455 void getConstraintMatrix(TransInfo *t);
456 int isLockConstraint(TransInfo *t);
457 int getConstraintSpaceDimension(TransInfo *t);
458 char constraintModeToChar(TransInfo *t);
460 void startConstraint(TransInfo *t);
461 void stopConstraint(TransInfo *t);
463 void initSelectConstraint(TransInfo *t, float mtx[3][3]);
464 void selectConstraint(TransInfo *t);
465 void postSelectConstraint(TransInfo *t);
467 void setNearestAxis(TransInfo *t);
469 /*********************** Snapping ********************************/
477 void snapGrid(TransInfo *t, float *val);
478 void snapGridAction(TransInfo *t, float *val, GearsType action);
480 void initSnapping(struct TransInfo *t);
481 void applySnapping(TransInfo *t, float *vec);
482 void resetSnapping(TransInfo *t);
483 int handleSnapping(TransInfo *t, struct wmEvent *event);
484 void drawSnapping(TransInfo *t);
485 int usingSnappingNormal(TransInfo *t);
486 int validSnappingNormal(TransInfo *t);
488 /*********************** Generics ********************************/
490 void initTransInfo(struct bContext *C, TransInfo *t, struct wmEvent *event);
491 void postTrans (TransInfo *t);
492 void resetTransRestrictions(TransInfo *t);
494 void drawLine(float *center, float *dir, char axis, short options);
496 TransDataCurveHandleFlags *initTransDataCurveHandes(TransData *td, struct BezTriple *bezt);
498 /* DRAWLINE options flags */
503 void applyTransObjects(TransInfo *t);
504 void restoreTransObjects(TransInfo *t);
505 void recalcData(TransInfo *t);
507 void calculateCenter(TransInfo *t);
508 void calculateCenter2D(TransInfo *t);
509 void calculateCenterBound(TransInfo *t);
510 void calculateCenterMedian(TransInfo *t);
511 void calculateCenterCursor(TransInfo *t);
513 void calculateCenterCursor2D(TransInfo *t);
514 void calculatePropRatio(TransInfo *t);
516 void getViewVector(TransInfo *t, float coord[3], float vec[3]);
518 TransInfo * BIF_GetTransInfo(void);
520 /*********************** NumInput ********************************/
522 void initNumInput(NumInput *n);
523 void outputNumInput(NumInput *n, char *str);
524 short hasNumInput(NumInput *n);
525 void applyNumInput(NumInput *n, float *vec);
526 char handleNumInput(NumInput *n, struct wmEvent *event);
528 /*********************** NDofInput ********************************/
530 void initNDofInput(NDofInput *n);
531 int hasNDofInput(NDofInput *n);
532 void applyNDofInput(NDofInput *n, float *vec);
533 int handleNDofInput(NDofInput *n, struct wmEvent *event);
535 /* handleNDofInput return values */
536 #define NDOF_REFRESH 1
537 #define NDOF_NOMOVE 2
538 #define NDOF_CONFIRM 3
539 #define NDOF_CANCEL 4
542 /*********************** TransSpace ******************************/
544 int manageObjectSpace(int confirm, int set);
545 int manageMeshSpace(int confirm, int set);
546 int manageBoneSpace(int confirm, int set);
548 /* Those two fill in mat and return non-zero on success */
549 int createSpaceNormal(float mat[3][3], float normal[3]);
550 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3]);
552 int addMatrixSpace(float mat[3][3], char name[]);
553 int addObjectSpace(struct Object *ob);
554 void applyTransformOrientation(void);
557 #define ORIENTATION_NONE 0
558 #define ORIENTATION_NORMAL 1
559 #define ORIENTATION_VERT 2
560 #define ORIENTATION_EDGE 3
561 #define ORIENTATION_FACE 4
563 int getTransformOrientation(struct bContext *C, float normal[3], float plane[3], int activeOnly);
564 int createSpaceNormal(float mat[3][3], float normal[3]);
565 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3]);