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;
54 typedef struct NDofInput {
61 typedef struct NumInput {
64 short flag; /* Different flags to indicate different behaviors */
65 float val[3]; /* Direct value of the input */
66 int ctrl[3]; /* Control to indicate what to do with the numbers that are typed */
70 The ctrl value has different meaning:
71 0 : No value has been typed
73 otherwise, |value| - 1 is where the cursor is located after the period
74 Positive : number is positive
75 Negative : number is negative
78 typedef struct TransSnap {
87 float dist; // Distance from snapPoint to snapTarget
89 void (*applySnap)(struct TransInfo *, float *);
90 void (*calcSnap)(struct TransInfo *, float *);
91 void (*targetSnap)(struct TransInfo *);
92 float (*distance)(struct TransInfo *, float p1[3], float p2[3]); // Get the transform distance between two points (used by Closest snap)
95 typedef struct TransCon {
96 char text[50]; /* Description of the Constraint for header_print */
97 float mtx[3][3]; /* Matrix of the Constraint space */
98 float imtx[3][3]; /* Inverse Matrix of the Constraint space */
99 float pmtx[3][3]; /* Projection Constraint Matrix (same as imtx with some axis == 0) */
100 float center[3]; /* transformation center to define where to draw the view widget
101 ALWAYS in global space. Unlike the transformation center */
102 short imval[2]; /* initial mouse value for visual calculation */
103 /* the one in TransInfo is not garanty to stay the same (Rotates change it) */
104 int mode; /* Mode flags of the Constraint */
105 void (*drawExtra)(struct TransInfo *);
106 /* For constraints that needs to draw differently from the other
107 uses this instead of the generic draw function */
108 void (*applyVec)(struct TransInfo *, struct TransData *, float *, float *, float *);
109 /* Apply function pointer for linear vectorial transformation */
110 /* The last three parameters are pointers to the in/out/printable vectors */
111 void (*applySize)(struct TransInfo *, struct TransData *, float [3][3]);
112 /* Apply function pointer for size transformation */
113 void (*applyRot)(struct TransInfo *, struct TransData *, float [3], float *);
114 /* Apply function pointer for rotation transformation */
117 typedef struct TransDataIpokey {
118 int flag; /* which keys */
119 float *locx, *locy, *locz; /* channel pointers */
120 float *rotx, *roty, *rotz;
121 float *quatx, *quaty, *quatz, *quatw;
122 float *sizex, *sizey, *sizez;
123 float oldloc[9]; /* storage old values */
129 typedef struct TransDataExtension {
130 float drot[3]; /* Initial object drot */
131 float dsize[3]; /* Initial object dsize */
132 float *rot; /* Rotation of the data to transform (Faculative) */
133 float irot[3]; /* Initial rotation */
134 float *quat; /* Rotation quaternion of the data to transform (Faculative) */
135 float iquat[4]; /* Initial rotation quaternion */
136 float *size; /* Size of the data to transform (Faculative) */
137 float isize[3]; /* Initial size */
138 float obmat[4][4]; /* Object matrix */
139 } TransDataExtension;
141 typedef struct TransData2D {
142 float loc[3]; /* Location of data used to transform (x,y,0) */
143 float *loc2d; /* Pointer to real 2d location of data */
146 /* we need to store 2 handles for each transdata incase the other handle wasnt selected */
147 typedef struct TransDataCurveHandleFlags {
150 } TransDataCurveHandleFlags;
153 typedef struct TransData {
154 float dist; /* Distance needed to affect element (for Proportionnal Editing) */
155 float rdist; /* Distance to the nearest element (for Proportionnal Editing) */
156 float factor; /* Factor of the transformation (for Proportionnal Editing) */
157 float *loc; /* Location of the data to transform */
158 float iloc[3]; /* Initial location */
159 float *val; /* Value pointer for special transforms */
160 float ival; /* Old value*/
161 float center[3]; /* Individual data center */
162 float mtx[3][3]; /* Transformation matrix from data space to global space */
163 float smtx[3][3]; /* Transformation matrix from global space to data space */
164 float axismtx[3][3];/* Axis orientation matrix of the data */
166 struct bConstraint *con; /* for objects/bones, the first constraint in its constraint stack */
167 TransDataExtension *ext; /* for objects, poses. 1 single malloc per TransInfo! */
168 TransDataIpokey *tdi; /* for objects, ipo keys. per transdata a malloc */
169 TransDataCurveHandleFlags *hdata; /* for curves, stores handle flags for modification/cancel */
170 void *extra; /* extra data (mirrored element pointer, in editmode mesh to EditVert) (editbone for roll fixing) (...) */
171 short flag; /* Various flags */
172 short protectflag; /* If set, copy of Object or PoseChannel protection */
173 /*#ifdef WITH_VERSE*/
174 void *verse; /* pointer at verse data struct (VerseVert, etc.) */
178 typedef struct MouseInput {
179 void (*apply)(struct TransInfo *, struct MouseInput *, short [2], float [3]);
181 short imval[2]; /* initial mouse position */
183 short precision_mval[2]; /* mouse position when precision key was pressed */
188 typedef struct TransInfo {
189 int mode; /* current mode */
190 int flag; /* generic flags for special behaviors */
191 int modifiers; /* special modifiers, by function, not key */
192 short state; /* current state (running, canceled,...)*/
193 int options; /* current context/options for transform */
194 float val; /* init value for some transformations (and rotation angle) */
195 float fac; /* factor for distance based transform */
196 int (*transform)(struct TransInfo *, short *);
197 /* transform function pointer */
198 int (*handleEvent)(struct TransInfo *, struct wmEvent *);
199 /* event handler function pointer RETURN 1 if redraw is needed */
200 int total; /* total number of transformed data */
201 TransData *data; /* transformed data (array) */
202 TransDataExtension *ext; /* transformed data extension (array) */
203 TransData2D *data2d; /* transformed data for 2d (array) */
204 TransCon con; /* transformed constraint */
206 NumInput num; /* numerical input */
207 NDofInput ndof; /* ndof input */
208 MouseInput mouse; /* mouse input */
209 char redraw; /* redraw flag */
210 float propsize; /* proportional circle radius */
211 char proptext[20]; /* proportional falloff text */
212 float center[3]; /* center of transformation */
213 int center2d[2]; /* center in screen coordinates */
214 short imval[2]; /* initial mouse position */
215 short idx_max; /* maximum index on the input vector */
216 float snap[3]; /* Snapping Gears */
218 float viewmat[4][4]; /* copy from G.vd, prevents feedback, */
219 float viewinv[4][4]; /* and to make sure we don't have to */
220 float persmat[4][4]; /* access G.vd from other space types */
224 char spacetype; /* spacetype where transforming is */
226 float vec[3]; /* translation, to show for widget */
227 float mat[3][3]; /* rot/rescale, to show for widget */
229 char *undostr; /* if set, uses this string for undo */
230 float spacemtx[3][3]; /* orientation matrix of the current space */
231 char spacename[32]; /* name of the current space */
233 struct Object *poseobj; /* if t->flag & T_POSE, this denotes pose object */
235 void *customData; /* Per Transform custom data */
237 /*************** NEW STUFF *********************/
244 short mval[2]; /* current mouse position */
245 struct Object *obedit;
249 /* ******************** Macros & Prototypes *********************** */
252 #define NUM_NULL_ONE 2
253 #define NUM_NO_NEGATIVE 4
254 #define NUM_NO_ZERO 8
255 #define NUM_NO_FRACTION 16
256 #define NUM_AFFECT_ALL 32
258 /* NDOFINPUT FLAGS */
261 /* transinfo->state */
262 #define TRANS_RUNNING 0
263 #define TRANS_CONFIRM 1
264 #define TRANS_CANCEL 2
266 /* transinfo->flag */
267 #define T_OBJECT (1 << 0)
268 #define T_EDIT (1 << 1)
269 #define T_POSE (1 << 2)
270 #define T_TEXTURE (1 << 3)
271 #define T_CAMERA (1 << 4)
272 // trans on points, having no rotation/scale
273 #define T_POINTS (1 << 6)
274 // for manipulator exceptions, like scaling using center point, drawing help lines
275 #define T_USES_MANIPULATOR (1 << 7)
277 /* restrictions flags */
278 #define T_ALL_RESTRICTIONS ((1 << 8)|(1 << 9)|(1 << 10))
279 #define T_NO_CONSTRAINT (1 << 8)
280 #define T_NULL_ONE (1 << 9)
281 #define T_NO_ZERO (1 << 10)
283 #define T_PROP_EDIT (1 << 11)
284 #define T_PROP_CONNECTED (1 << 12)
286 #define T_V3D_ALIGN (1 << 14)
287 /* for 2d views like uv or ipo */
288 #define T_2D_EDIT (1 << 15)
289 #define T_CLIP_UV (1 << 16)
291 #define T_FREE_CUSTOMDATA (1 << 17)
293 #define T_AUTOIK (1 << 18)
295 /* TransInfo->modifiers */
296 #define MOD_CONSTRAINT_SELECT 0x01
297 #define MOD_PRECISION 0x02
298 #define MOD_SNAP_GEARS 0x04
299 #define MOD_CONSTRAINT_PLANE 0x08
302 /* ******************************************************************************** */
304 /* transinfo->con->mode */
309 #define CON_SELECT 16
310 #define CON_NOFLIP 32 /* does not reorient vector to face viewport when on */
314 /* transdata->flag */
315 #define TD_SELECTED 1
316 #define TD_ACTIVE (1 << 1)
317 #define TD_NOACTION (1 << 2)
318 #define TD_USEQUAT (1 << 3)
319 #define TD_NOTCONNECTED (1 << 4)
320 #define TD_SINGLESIZE (1 << 5) /* used for scaling of MetaElem->rad */
322 #define TD_VERSE_OBJECT (1 << 6)
323 #define TD_VERSE_VERT (1 << 7)
325 #define TD_TIMEONLY (1 << 8)
326 #define TD_NOCENTER (1 << 9)
327 #define TD_NO_EXT (1 << 10) /* ext abused for particle key timing */
328 #define TD_SKIP (1 << 11) /* don't transform this data */
329 #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 */
330 #define TD_NO_LOC (1 << 13) /* when this is set, don't apply translation changes to this element */
332 /* transsnap->status */
334 #define TARGET_INIT 2
337 /* transsnap->modePoint */
341 /* transsnap->modeTarget */
342 #define SNAP_CLOSEST 0
343 #define SNAP_CENTER 1
344 #define SNAP_MEDIAN 2
345 #define SNAP_ACTIVE 3
348 void TFM_OT_transform(struct wmOperatorType *ot);
350 void initTransform(struct bContext *C, struct TransInfo *t, struct wmOperator *op, struct wmEvent *event);
351 void saveTransform(struct bContext *C, struct TransInfo *t, struct wmOperator *op);
352 void transformEvent(TransInfo *t, struct wmEvent *event);
353 void transformApply(TransInfo *t);
354 int transformEnd(struct bContext *C, TransInfo *t);
356 void setTransformViewMatrices(TransInfo *t);
357 void convertViewVec(TransInfo *t, float *vec, short dx, short dy);
358 void projectIntView(TransInfo *t, float *vec, int *adr);
359 void projectFloatView(TransInfo *t, float *vec, float *adr);
361 void convertVecToDisplayNum(float *vec, float *num);
362 void convertDisplayNumToVec(float *num, float *vec);
364 void initWarp(TransInfo *t);
365 int handleEventWarp(TransInfo *t, struct wmEvent *event);
366 int Warp(TransInfo *t, short mval[2]);
368 void initShear(TransInfo *t);
369 int handleEventShear(TransInfo *t, struct wmEvent *event);
370 int Shear(TransInfo *t, short mval[2]);
372 void initResize(TransInfo *t);
373 int Resize(TransInfo *t, short mval[2]);
375 void initTranslation(TransInfo *t);
376 int Translation(TransInfo *t, short mval[2]);
378 void initToSphere(TransInfo *t);
379 int ToSphere(TransInfo *t, short mval[2]);
381 void initRotation(TransInfo *t);
382 int Rotation(TransInfo *t, short mval[2]);
384 void initShrinkFatten(TransInfo *t);
385 int ShrinkFatten(TransInfo *t, short mval[2]);
387 void initTilt(TransInfo *t);
388 int Tilt(TransInfo *t, short mval[2]);
390 void initCurveShrinkFatten(TransInfo *t);
391 int CurveShrinkFatten(TransInfo *t, short mval[2]);
393 void initTrackball(TransInfo *t);
394 int Trackball(TransInfo *t, short mval[2]);
396 void initPushPull(TransInfo *t);
397 int PushPull(TransInfo *t, short mval[2]);
399 void initBevel(TransInfo *t);
400 int handleEventBevel(TransInfo *t, struct wmEvent *event);
401 int Bevel(TransInfo *t, short mval[2]);
403 void initBevelWeight(TransInfo *t);
404 int BevelWeight(TransInfo *t, short mval[2]);
406 void initCrease(TransInfo *t);
407 int Crease(TransInfo *t, short mval[2]);
409 void initBoneSize(TransInfo *t);
410 int BoneSize(TransInfo *t, short mval[2]);
412 void initBoneEnvelope(TransInfo *t);
413 int BoneEnvelope(TransInfo *t, short mval[2]);
415 void initBoneRoll(TransInfo *t);
416 int BoneRoll(TransInfo *t, short mval[2]);
418 void initTimeTranslate(TransInfo *t);
419 int TimeTranslate(TransInfo *t, short mval[2]);
421 void initTimeSlide(TransInfo *t);
422 int TimeSlide(TransInfo *t, short mval[2]);
424 void initTimeScale(TransInfo *t);
425 int TimeScale(TransInfo *t, short mval[2]);
427 void initBakeTime(TransInfo *t);
428 int BakeTime(TransInfo *t, short mval[2]);
430 void initMirror(TransInfo *t);
431 int Mirror(TransInfo *t, short mval[2]);
433 void initAlign(TransInfo *t);
434 int Align(TransInfo *t, short mval[2]);
436 /*********************** transform_conversions.c ********** */
438 void flushTransGPactionData(TransInfo *t);
439 void flushTransIpoData(TransInfo *t);
440 void flushTransUVs(TransInfo *t);
441 void flushTransParticles(TransInfo *t);
442 int clipUVTransform(TransInfo *t, float *vec, int resize);
443 void flushTransNodes(TransInfo *t);
445 /*********************** exported from transform_manipulator.c ********** */
446 void draw_manipulator_ext(struct ScrArea *sa, int type, char axis, int col, float vec[3], float mat[][3]);
447 int calc_manipulator_stats(struct ScrArea *sa);
448 float get_drawsize(struct View3D *v3d, struct ScrArea *sa, float *co);
450 /*********************** TransData Creation and General Handling *********** */
451 void createTransData(struct bContext *C, TransInfo *t);
452 void sort_trans_data_dist(TransInfo *t);
453 void add_tdi_poin(float *poin, float *old, float delta);
454 void special_aftertrans_update(TransInfo *t);
456 void transform_autoik_update(TransInfo *t, short mode);
458 /* auto-keying stuff used by special_aftertrans_update */
459 short autokeyframe_cfra_can_key(struct Object *ob);
460 void autokeyframe_ob_cb_func(struct Object *ob, int tmode);
461 void autokeyframe_pose_cb_func(struct Object *ob, int tmode, short targetless_ik);
463 /*********************** Constraints *****************************/
465 void getConstraintMatrix(TransInfo *t);
466 void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[]);
467 void setLocalConstraint(TransInfo *t, int mode, const char text[]);
468 void setUserConstraint(TransInfo *t, int mode, const char text[]);
470 void constraintNumInput(TransInfo *t, float vec[3]);
472 void getConstraintMatrix(TransInfo *t);
473 int isLockConstraint(TransInfo *t);
474 int getConstraintSpaceDimension(TransInfo *t);
475 char constraintModeToChar(TransInfo *t);
477 void startConstraint(TransInfo *t);
478 void stopConstraint(TransInfo *t);
480 void initSelectConstraint(TransInfo *t, float mtx[3][3]);
481 void selectConstraint(TransInfo *t);
482 void postSelectConstraint(TransInfo *t);
484 void setNearestAxis(TransInfo *t);
486 /*********************** Snapping ********************************/
494 void snapGrid(TransInfo *t, float *val);
495 void snapGridAction(TransInfo *t, float *val, GearsType action);
497 void initSnapping(struct TransInfo *t);
498 void applySnapping(TransInfo *t, float *vec);
499 void resetSnapping(TransInfo *t);
500 int handleSnapping(TransInfo *t, struct wmEvent *event);
501 void drawSnapping(TransInfo *t);
502 int usingSnappingNormal(TransInfo *t);
503 int validSnappingNormal(TransInfo *t);
505 /********************** Mouse Input ******************************/
514 INPUT_HORIZONTAL_RATIO,
515 INPUT_HORIZONTAL_ABSOLUTE,
516 INPUT_VERTICAL_RATIO,
517 INPUT_VERTICAL_ABSOLUTE
520 void initMouseInput(TransInfo *t, MouseInput *mi, int center[2], short mval[2]);
521 void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode);
522 int handleMouseInput(struct TransInfo *t, struct MouseInput *mi, struct wmEvent *event);
523 void applyMouseInput(struct TransInfo *t, struct MouseInput *mi, short mval[2], float output[3]);
525 /*********************** Generics ********************************/
527 void initTransInfo(struct bContext *C, TransInfo *t, struct wmEvent *event);
528 void postTrans (TransInfo *t);
529 void resetTransRestrictions(TransInfo *t);
531 void drawLine(float *center, float *dir, char axis, short options);
533 TransDataCurveHandleFlags *initTransDataCurveHandes(TransData *td, struct BezTriple *bezt);
535 /* DRAWLINE options flags */
540 void applyTransObjects(TransInfo *t);
541 void restoreTransObjects(TransInfo *t);
542 void restoreTransNodes(TransInfo *t);
543 void recalcData(TransInfo *t);
545 void calculateCenter(TransInfo *t);
546 void calculateCenter2D(TransInfo *t);
547 void calculateCenterBound(TransInfo *t);
548 void calculateCenterMedian(TransInfo *t);
549 void calculateCenterCursor(TransInfo *t);
551 void calculateCenterCursor2D(TransInfo *t);
552 void calculatePropRatio(TransInfo *t);
554 void getViewVector(TransInfo *t, float coord[3], float vec[3]);
556 TransInfo * BIF_GetTransInfo(void);
558 /*********************** NumInput ********************************/
560 void initNumInput(NumInput *n);
561 void outputNumInput(NumInput *n, char *str);
562 short hasNumInput(NumInput *n);
563 void applyNumInput(NumInput *n, float *vec);
564 char handleNumInput(NumInput *n, struct wmEvent *event);
566 /*********************** NDofInput ********************************/
568 void initNDofInput(NDofInput *n);
569 int hasNDofInput(NDofInput *n);
570 void applyNDofInput(NDofInput *n, float *vec);
571 int handleNDofInput(NDofInput *n, struct wmEvent *event);
573 /* handleNDofInput return values */
574 #define NDOF_REFRESH 1
575 #define NDOF_NOMOVE 2
576 #define NDOF_CONFIRM 3
577 #define NDOF_CANCEL 4
580 /*********************** TransSpace ******************************/
582 int manageObjectSpace(int confirm, int set);
583 int manageMeshSpace(int confirm, int set);
584 int manageBoneSpace(int confirm, int set);
586 /* Those two fill in mat and return non-zero on success */
587 int createSpaceNormal(float mat[3][3], float normal[3]);
588 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3]);
590 int addMatrixSpace(float mat[3][3], char name[]);
591 int addObjectSpace(struct Object *ob);
592 void applyTransformOrientation(void);
595 #define ORIENTATION_NONE 0
596 #define ORIENTATION_NORMAL 1
597 #define ORIENTATION_VERT 2
598 #define ORIENTATION_EDGE 3
599 #define ORIENTATION_FACE 4
601 int getTransformOrientation(struct bContext *C, float normal[3], float plane[3], int activeOnly);
602 int createSpaceNormal(float mat[3][3], float normal[3]);
603 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3]);