typo fix: end of lines ;; --> ;
[blender-staging.git] / source / blender / editors / space_view3d / view3d_edit.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
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.
15  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  *
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_view3d/view3d_edit.c
30  *  \ingroup spview3d
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <math.h>
37 #include <float.h>
38
39 #include "DNA_armature_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_camera_types.h"
43 #include "DNA_lamp_types.h"
44
45 #include "MEM_guardedalloc.h"
46
47 #include "BLI_blenlib.h"
48 #include "BLI_math.h"
49 #include "BLI_rand.h"
50 #include "BLI_utildefines.h"
51
52 #include "BKE_context.h"
53 #include "BKE_image.h"
54 #include "BKE_library.h"
55 #include "BKE_object.h"
56 #include "BKE_paint.h"
57 #include "BKE_report.h"
58 #include "BKE_scene.h"
59 #include "BKE_screen.h"
60 #include "BKE_depsgraph.h" /* for ED_view3d_camera_lock_sync */
61
62
63 #include "BIF_gl.h"
64 #include "BIF_glutil.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "RNA_access.h"
70 #include "RNA_define.h"
71
72 #include "ED_particle.h"
73 #include "ED_screen.h"
74 #include "ED_transform.h"
75 #include "ED_mesh.h"
76 #include "ED_view3d.h"
77
78
79 #include "PIL_time.h" /* smoothview */
80
81 #include "view3d_intern.h"      // own include
82
83 /* ********************** view3d_edit: view manipulations ********************* */
84
85 int ED_view3d_camera_lock_check(View3D *v3d, RegionView3D *rv3d)
86 {
87         return ((v3d->camera) &&
88                 (v3d->camera->id.lib == NULL) &&
89                 (v3d->flag2 & V3D_LOCK_CAMERA) &&
90                 (rv3d->persp==RV3D_CAMOB));
91 }
92
93 void ED_view3d_camera_lock_init(View3D *v3d, RegionView3D *rv3d)
94 {
95         if(ED_view3d_camera_lock_check(v3d, rv3d)) {
96                 ED_view3d_from_object(v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, NULL);
97         }
98 }
99
100 /* return TRUE if the camera is moved */
101 int ED_view3d_camera_lock_sync(View3D *v3d, RegionView3D *rv3d)
102 {
103         if(ED_view3d_camera_lock_check(v3d, rv3d)) {
104                 Object *root_parent;
105
106                 if((U.uiflag & USER_CAM_LOCK_NO_PARENT)==0 && (root_parent= v3d->camera->parent)) {
107                         Object *ob_update;
108                         float view_mat[4][4];
109                         float diff_mat[4][4];
110                         float parent_mat[4][4];
111
112                         while(root_parent->parent) {
113                                 root_parent= root_parent->parent;
114                         }
115
116                         ED_view3d_to_m4(view_mat, rv3d->ofs, rv3d->viewquat, rv3d->dist);
117
118                         invert_m4_m4(v3d->camera->imat, v3d->camera->obmat);
119                         mul_m4_m4m4(diff_mat, v3d->camera->imat, view_mat);
120
121                         mul_m4_m4m4(parent_mat, root_parent->obmat, diff_mat);
122                         object_apply_mat4(root_parent, parent_mat, TRUE, FALSE);
123
124                         ob_update= v3d->camera;
125                         while(ob_update) {
126                                 DAG_id_tag_update(&ob_update->id, OB_RECALC_OB);
127                                 WM_main_add_notifier(NC_OBJECT|ND_TRANSFORM, ob_update);
128                                 ob_update= ob_update->parent;
129                         }
130                 }
131                 else {
132                         ED_view3d_to_object(v3d->camera, rv3d->ofs, rv3d->viewquat, rv3d->dist);
133                         DAG_id_tag_update(&v3d->camera->id, OB_RECALC_OB);
134                         WM_main_add_notifier(NC_OBJECT|ND_TRANSFORM, v3d->camera);
135                 }
136
137                 return TRUE;
138         }
139         else {
140                 return FALSE;
141         }
142 }
143
144
145 /* ********************* box view support ***************** */
146
147 static void view3d_boxview_clip(ScrArea *sa)
148 {
149         ARegion *ar;
150         BoundBox *bb = MEM_callocN(sizeof(BoundBox), "clipbb");
151         float clip[6][4];
152         float x1= 0.0f, y1= 0.0f, z1= 0.0f, ofs[3] = {0.0f, 0.0f, 0.0f};
153         int val;
154
155         /* create bounding box */
156         for(ar= sa->regionbase.first; ar; ar= ar->next) {
157                 if(ar->regiontype==RGN_TYPE_WINDOW) {
158                         RegionView3D *rv3d= ar->regiondata;
159
160                         if(rv3d->viewlock & RV3D_BOXCLIP) {
161                                 if(ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
162                                         if(ar->winx>ar->winy) x1= rv3d->dist;
163                                         else x1= ar->winx*rv3d->dist/ar->winy;
164
165                                         if(ar->winx>ar->winy) y1= ar->winy*rv3d->dist/ar->winx;
166                                         else y1= rv3d->dist;
167                                         copy_v2_v2(ofs, rv3d->ofs);
168                                 }
169                                 else if(ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
170                                         ofs[2]= rv3d->ofs[2];
171
172                                         if(ar->winx>ar->winy) z1= ar->winy*rv3d->dist/ar->winx;
173                                         else z1= rv3d->dist;
174                                 }
175                         }
176                 }
177         }
178
179         for(val=0; val<8; val++) {
180                 if(ELEM4(val, 0, 3, 4, 7))
181                         bb->vec[val][0]= -x1 - ofs[0];
182                 else
183                         bb->vec[val][0]=  x1 - ofs[0];
184
185                 if(ELEM4(val, 0, 1, 4, 5))
186                         bb->vec[val][1]= -y1 - ofs[1];
187                 else
188                         bb->vec[val][1]=  y1 - ofs[1];
189
190                 if(val > 3)
191                         bb->vec[val][2]= -z1 - ofs[2];
192                 else
193                         bb->vec[val][2]=  z1 - ofs[2];
194         }
195
196         /* normals for plane equations */
197         normal_tri_v3( clip[0],bb->vec[0], bb->vec[1], bb->vec[4]);
198         normal_tri_v3( clip[1],bb->vec[1], bb->vec[2], bb->vec[5]);
199         normal_tri_v3( clip[2],bb->vec[2], bb->vec[3], bb->vec[6]);
200         normal_tri_v3( clip[3],bb->vec[3], bb->vec[0], bb->vec[7]);
201         normal_tri_v3( clip[4],bb->vec[4], bb->vec[5], bb->vec[6]);
202         normal_tri_v3( clip[5],bb->vec[0], bb->vec[2], bb->vec[1]);
203
204         /* then plane equations */
205         for(val=0; val<5; val++) {
206                 clip[val][3]= - clip[val][0]*bb->vec[val][0] - clip[val][1]*bb->vec[val][1] - clip[val][2]*bb->vec[val][2];
207         }
208         clip[5][3]= - clip[5][0]*bb->vec[0][0] - clip[5][1]*bb->vec[0][1] - clip[5][2]*bb->vec[0][2];
209
210         /* create bounding box */
211         for(ar= sa->regionbase.first; ar; ar= ar->next) {
212                 if(ar->regiontype==RGN_TYPE_WINDOW) {
213                         RegionView3D *rv3d= ar->regiondata;
214
215                         if(rv3d->viewlock & RV3D_BOXCLIP) {
216                                 rv3d->rflag |= RV3D_CLIPPING;
217                                 memcpy(rv3d->clip, clip, sizeof(clip));
218                                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
219                                 rv3d->clipbb= MEM_dupallocN(bb);
220                         }
221                 }
222         }
223         MEM_freeN(bb);
224 }
225
226 /* sync center/zoom view of region to others, for view transforms */
227 static void view3d_boxview_sync(ScrArea *sa, ARegion *ar)
228 {
229         ARegion *artest;
230         RegionView3D *rv3d= ar->regiondata;
231         short clip= 0;
232
233         for(artest= sa->regionbase.first; artest; artest= artest->next) {
234                 if(artest!=ar && artest->regiontype==RGN_TYPE_WINDOW) {
235                         RegionView3D *rv3dtest= artest->regiondata;
236
237                         if(rv3dtest->viewlock) {
238                                 rv3dtest->dist= rv3d->dist;
239
240                                 if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM) ) {
241                                         if( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
242                                                 rv3dtest->ofs[0]= rv3d->ofs[0];
243                                         else if( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
244                                                 rv3dtest->ofs[1]= rv3d->ofs[1];
245                                 }
246                                 else if( ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK) ) {
247                                         if( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
248                                                 rv3dtest->ofs[0]= rv3d->ofs[0];
249                                         else if( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
250                                                 rv3dtest->ofs[2]= rv3d->ofs[2];
251                                 }
252                                 else if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT) ) {
253                                         if( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
254                                                 rv3dtest->ofs[1]= rv3d->ofs[1];
255                                         if( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
256                                                 rv3dtest->ofs[2]= rv3d->ofs[2];
257                                 }
258
259                                 clip |= rv3dtest->viewlock & RV3D_BOXCLIP;
260
261                                 ED_region_tag_redraw(artest);
262                         }
263                 }
264         }
265
266         if(clip) {
267                 view3d_boxview_clip(sa);
268         }
269 }
270
271 /* for home, center etc */
272 void view3d_boxview_copy(ScrArea *sa, ARegion *ar)
273 {
274         ARegion *artest;
275         RegionView3D *rv3d= ar->regiondata;
276         short clip= 0;
277
278         for(artest= sa->regionbase.first; artest; artest= artest->next) {
279                 if(artest!=ar && artest->regiontype==RGN_TYPE_WINDOW) {
280                         RegionView3D *rv3dtest= artest->regiondata;
281
282                         if(rv3dtest->viewlock) {
283                                 rv3dtest->dist= rv3d->dist;
284                                 copy_v3_v3(rv3dtest->ofs, rv3d->ofs);
285                                 ED_region_tag_redraw(artest);
286
287                                 clip |= rv3dtest->viewlock & RV3D_BOXCLIP;
288                         }
289                 }
290         }
291
292         if(clip) {
293                 view3d_boxview_clip(sa);
294         }
295 }
296
297 /* 'clip' is used to know if our clip setting has changed */
298 void ED_view3d_quadview_update(ScrArea *sa, ARegion *ar, short do_clip)
299 {
300         ARegion *arsync= NULL;
301         RegionView3D *rv3d= ar->regiondata;
302         short viewlock;
303         /* this function copies flags from the first of the 3 other quadview
304            regions to the 2 other, so it assumes this is the region whose
305            properties are always being edited, weak */
306         viewlock= rv3d->viewlock;
307
308         if((viewlock & RV3D_LOCKED)==0)
309                 viewlock= 0;
310         else if((viewlock & RV3D_BOXVIEW)==0) {
311                 viewlock &= ~RV3D_BOXCLIP;
312                 do_clip= TRUE;
313         }
314
315         for(; ar; ar= ar->prev) {
316                 if(ar->alignment==RGN_ALIGN_QSPLIT) {
317                         rv3d= ar->regiondata;
318                         rv3d->viewlock= viewlock;
319
320                         if(do_clip && (viewlock & RV3D_BOXCLIP)==0) {
321                                 rv3d->rflag &= ~RV3D_BOXCLIP;
322                         }
323
324                         /* use arsync so we sync with one of the aligned views below
325                          * else the view jumps on changing view settings like 'clip'
326                          * since it copies from the perspective view */
327                         arsync= ar;
328                 }
329         }
330
331         if(rv3d->viewlock & RV3D_BOXVIEW) {
332                 view3d_boxview_copy(sa, arsync ? arsync : sa->regionbase.last);
333         }
334
335         ED_area_tag_redraw(sa);
336 }
337
338 /* ************************** init for view ops **********************************/
339
340 typedef struct ViewOpsData {
341         ScrArea *sa;
342         ARegion *ar;
343         View3D *v3d;
344         RegionView3D *rv3d;
345
346         /* needed for continuous zoom */
347         wmTimer *timer;
348         double timer_lastdraw;
349
350         float oldquat[4];
351         float trackvec[3];
352         float mousevec[3]; /* dolly only */
353         float reverse, dist0;
354         float grid, far;
355         short axis_snap; /* view rotate only */
356
357         /* use for orbit selection and auto-dist */
358         float ofs[3], dyn_ofs[3];
359         short use_dyn_ofs;
360
361         int origx, origy, oldx, oldy;
362         int origkey; /* the key that triggered the operator */
363
364 } ViewOpsData;
365
366 #define TRACKBALLSIZE  (1.1)
367
368 static void calctrackballvec(rcti *rect, int mx, int my, float *vec)
369 {
370         float x, y, radius, d, z, t;
371
372         radius= TRACKBALLSIZE;
373
374         /* normalize x and y */
375         x= (rect->xmax + rect->xmin)/2 - mx;
376         x/= (float)((rect->xmax - rect->xmin)/4);
377         y= (rect->ymax + rect->ymin)/2 - my;
378         y/= (float)((rect->ymax - rect->ymin)/2);
379
380         d = sqrt(x*x + y*y);
381         if (d < radius * (float)M_SQRT1_2) { /* Inside sphere */
382                 z= sqrt(radius*radius - d*d);
383         }
384         else { /* On hyperbola */
385                 t= radius / (float)M_SQRT2;
386                 z= t*t / d;
387         }
388
389         vec[0]= x;
390         vec[1]= y;
391         vec[2]= -z;             /* yah yah! */
392 }
393
394
395 static void viewops_data_create(bContext *C, wmOperator *op, wmEvent *event)
396 {
397         static float lastofs[3] = {0,0,0};
398         RegionView3D *rv3d;
399         ViewOpsData *vod= MEM_callocN(sizeof(ViewOpsData), "viewops data");
400
401         /* store data */
402         op->customdata= vod;
403         vod->sa= CTX_wm_area(C);
404         vod->ar= CTX_wm_region(C);
405         vod->v3d= vod->sa->spacedata.first;
406         vod->rv3d= rv3d= vod->ar->regiondata;
407
408         /* set the view from the camera, if view locking is enabled.
409          * we may want to make this optional but for now its needed always */
410         ED_view3d_camera_lock_init(vod->v3d, vod->rv3d);
411
412         vod->dist0= rv3d->dist;
413         copy_qt_qt(vod->oldquat, rv3d->viewquat);
414         vod->origx= vod->oldx= event->x;
415         vod->origy= vod->oldy= event->y;
416         vod->origkey= event->type; /* the key that triggered the operator.  */
417         vod->use_dyn_ofs= (U.uiflag & USER_ORBIT_SELECTION) ? 1:0;
418         copy_v3_v3(vod->ofs, rv3d->ofs);
419
420         if (vod->use_dyn_ofs) {
421                 /* If there's no selection, lastofs is unmodified and last value since static */
422                 calculateTransformCenter(C, V3D_CENTROID, lastofs);
423                 negate_v3_v3(vod->dyn_ofs, lastofs);
424         }
425         else if (U.uiflag & USER_ORBIT_ZBUF) {
426
427                 view3d_operator_needs_opengl(C); /* needed for zbuf drawing */
428
429                 if((vod->use_dyn_ofs=ED_view3d_autodist(CTX_data_scene(C), vod->ar, vod->v3d, event->mval, vod->dyn_ofs))) {
430                         if (rv3d->is_persp) {
431                                 float my_origin[3]; /* original G.vd->ofs */
432                                 float my_pivot[3]; /* view */
433                                 float dvec[3];
434
435                                 // locals for dist correction
436                                 float mat[3][3];
437                                 float upvec[3];
438
439                                 negate_v3_v3(my_origin, rv3d->ofs);                             /* ofs is flipped */
440
441                                 /* Set the dist value to be the distance from this 3d point */
442                                 /* this means youll always be able to zoom into it and panning wont go bad when dist was zero */
443
444                                 /* remove dist value */
445                                 upvec[0] = upvec[1] = 0;
446                                 upvec[2] = rv3d->dist;
447                                 copy_m3_m4(mat, rv3d->viewinv);
448
449                                 mul_m3_v3(mat, upvec);
450                                 sub_v3_v3v3(my_pivot, rv3d->ofs, upvec);
451                                 negate_v3(my_pivot);                            /* ofs is flipped */
452
453                                 /* find a new ofs value that is allong the view axis (rather than the mouse location) */
454                                 closest_to_line_v3(dvec, vod->dyn_ofs, my_pivot, my_origin);
455                                 vod->dist0 = rv3d->dist = len_v3v3(my_pivot, dvec);
456
457                                 negate_v3_v3(rv3d->ofs, dvec);
458                         }
459                         negate_v3(vod->dyn_ofs);
460                         copy_v3_v3(vod->ofs, rv3d->ofs);
461                 }
462         }
463
464         {
465                 /* for dolly */
466                 float mval_f[2];
467                 VECCOPY2D(mval_f, event->mval);
468                 ED_view3d_win_to_vector(vod->ar, mval_f, vod->mousevec);
469         }
470
471         /* lookup, we dont pass on v3d to prevent confusement */
472         vod->grid= vod->v3d->grid;
473         vod->far= vod->v3d->far;
474
475         calctrackballvec(&vod->ar->winrct, event->x, event->y, vod->trackvec);
476
477         initgrabz(rv3d, -rv3d->ofs[0], -rv3d->ofs[1], -rv3d->ofs[2]);
478
479         vod->reverse= 1.0f;
480         if (rv3d->persmat[2][1] < 0.0f)
481                 vod->reverse= -1.0f;
482
483         rv3d->rflag |= RV3D_NAVIGATING;
484 }
485
486 static void viewops_data_free(bContext *C, wmOperator *op)
487 {
488         ARegion *ar;
489         Paint *p = paint_get_active(CTX_data_scene(C));
490
491         if(op->customdata) {
492                 ViewOpsData *vod= op->customdata;
493                 ar= vod->ar;
494                 vod->rv3d->rflag &= ~RV3D_NAVIGATING;
495
496                 if(vod->timer)
497                         WM_event_remove_timer(CTX_wm_manager(C), vod->timer->win, vod->timer);
498
499                 MEM_freeN(vod);
500                 op->customdata= NULL;
501         }
502         else {
503                 ar= CTX_wm_region(C);
504         }
505
506         if(p && (p->flags & PAINT_FAST_NAVIGATE))
507                 ED_region_tag_redraw(ar);
508 }
509
510 /* ************************** viewrotate **********************************/
511
512 static const float thres = 0.93f; //cos(20 deg);
513
514 #define COS45 0.70710678118654746
515 #define SIN45 COS45
516
517 static float snapquats[39][5] = {
518         /*{q0, q1, q3, q4, view}*/
519         {COS45, -SIN45, 0.0, 0.0, RV3D_VIEW_FRONT},  //front
520         {0.0, 0.0, -SIN45, -SIN45, RV3D_VIEW_BACK}, //back
521         {1.0, 0.0, 0.0, 0.0, RV3D_VIEW_TOP},       //top
522         {0.0, -1.0, 0.0, 0.0, RV3D_VIEW_BOTTOM},      //bottom
523         {0.5, -0.5, -0.5, -0.5, RV3D_VIEW_RIGHT},    //left
524         {0.5, -0.5, 0.5, 0.5, RV3D_VIEW_LEFT},      //right
525
526         /* some more 45 deg snaps */
527         {0.65328145027160645, -0.65328145027160645, 0.27059805393218994, 0.27059805393218994, 0},
528         {0.92387950420379639, 0.0, 0.0, 0.38268342614173889, 0},
529         {0.0, -0.92387950420379639, 0.38268342614173889, 0.0, 0},
530         {0.35355335474014282, -0.85355335474014282, 0.35355338454246521, 0.14644660055637360, 0},
531         {0.85355335474014282, -0.35355335474014282, 0.14644660055637360, 0.35355338454246521, 0},
532         {0.49999994039535522, -0.49999994039535522, 0.49999997019767761, 0.49999997019767761, 0},
533         {0.27059802412986755, -0.65328145027160645, 0.65328145027160645, 0.27059802412986755, 0},
534         {0.65328145027160645, -0.27059802412986755, 0.27059802412986755, 0.65328145027160645, 0},
535         {0.27059799432754517, -0.27059799432754517, 0.65328139066696167, 0.65328139066696167, 0},
536         {0.38268336653709412, 0.0, 0.0, 0.92387944459915161, 0},
537         {0.0, -0.38268336653709412, 0.92387944459915161, 0.0, 0},
538         {0.14644658565521240, -0.35355335474014282, 0.85355335474014282, 0.35355335474014282, 0},
539         {0.35355335474014282, -0.14644658565521240, 0.35355335474014282, 0.85355335474014282, 0},
540         {0.0, 0.0, 0.92387944459915161, 0.38268336653709412, 0},
541         {-0.0, 0.0, 0.38268336653709412, 0.92387944459915161, 0},
542         {-0.27059802412986755, 0.27059802412986755, 0.65328133106231689, 0.65328133106231689, 0},
543         {-0.38268339633941650, 0.0, 0.0, 0.92387938499450684, 0},
544         {0.0, 0.38268339633941650, 0.92387938499450684, 0.0, 0},
545         {-0.14644658565521240, 0.35355338454246521, 0.85355329513549805, 0.35355332493782043, 0},
546         {-0.35355338454246521, 0.14644658565521240, 0.35355332493782043, 0.85355329513549805, 0},
547         {-0.49999991059303284, 0.49999991059303284, 0.49999985098838806, 0.49999985098838806, 0},
548         {-0.27059799432754517, 0.65328145027160645, 0.65328139066696167, 0.27059799432754517, 0},
549         {-0.65328145027160645, 0.27059799432754517, 0.27059799432754517, 0.65328139066696167, 0},
550         {-0.65328133106231689, 0.65328133106231689, 0.27059793472290039, 0.27059793472290039, 0},
551         {-0.92387932538986206, 0.0, 0.0, 0.38268333673477173, 0},
552         {0.0, 0.92387932538986206, 0.38268333673477173, 0.0, 0},
553         {-0.35355329513549805, 0.85355329513549805, 0.35355329513549805, 0.14644657075405121, 0},
554         {-0.85355329513549805, 0.35355329513549805, 0.14644657075405121, 0.35355329513549805, 0},
555         {-0.38268330693244934, 0.92387938499450684, 0.0, 0.0, 0},
556         {-0.92387938499450684, 0.38268330693244934, 0.0, 0.0, 0},
557         {-COS45, 0.0, 0.0, SIN45, 0},
558         {COS45, 0.0, 0.0, SIN45, 0},
559         {0.0, 0.0, 0.0, 1.0, 0}
560 };
561
562 enum {
563         VIEW_PASS= 0,
564         VIEW_APPLY,
565         VIEW_CONFIRM
566 };
567
568 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
569 #define VIEW_MODAL_CONFIRM                              1 /* used for all view operations */
570 #define VIEWROT_MODAL_AXIS_SNAP_ENABLE  2
571 #define VIEWROT_MODAL_AXIS_SNAP_DISABLE 3
572 #define VIEWROT_MODAL_SWITCH_ZOOM               4
573 #define VIEWROT_MODAL_SWITCH_MOVE               5
574 #define VIEWROT_MODAL_SWITCH_ROTATE             6
575
576 /* called in transform_ops.c, on each regeneration of keymaps  */
577 void viewrotate_modal_keymap(wmKeyConfig *keyconf)
578 {
579         static EnumPropertyItem modal_items[] = {
580         {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Confirm", ""},
581
582         {VIEWROT_MODAL_AXIS_SNAP_ENABLE,        "AXIS_SNAP_ENABLE", 0, "Enable Axis Snap", ""},
583         {VIEWROT_MODAL_AXIS_SNAP_DISABLE,       "AXIS_SNAP_DISABLE", 0, "Disable Axis Snap", ""},
584                 
585         {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
586         {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
587
588         {0, NULL, 0, NULL, NULL}};
589
590         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Rotate Modal");
591
592         /* this function is called for each spacetype, only needs to add map once */
593         if(keymap) return;
594
595         keymap= WM_modalkeymap_add(keyconf, "View3D Rotate Modal", modal_items);
596
597         /* items for modal map */
598         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
599         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
600
601         WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_ENABLE);
602         WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_DISABLE);
603
604         /* disabled mode switching for now, can re-implement better, later on
605         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
606         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
607         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
608         */
609         
610         /* assign map to operators */
611         WM_modalkeymap_assign(keymap, "VIEW3D_OT_rotate");
612
613 }
614
615 static void viewrotate_apply(ViewOpsData *vod, int x, int y)
616 {
617         RegionView3D *rv3d= vod->rv3d;
618
619         rv3d->view= RV3D_VIEW_USER; /* need to reset everytime because of view snapping */
620
621         if (U.flag & USER_TRACKBALL) {
622                 float phi, si, q1[4], dvec[3], newvec[3];
623
624                 calctrackballvec(&vod->ar->winrct, x, y, newvec);
625
626                 sub_v3_v3v3(dvec, newvec, vod->trackvec);
627
628                 si= sqrt(dvec[0]*dvec[0]+ dvec[1]*dvec[1]+ dvec[2]*dvec[2]);
629                 si /= (float)(2.0 * TRACKBALLSIZE);
630
631                 cross_v3_v3v3(q1+1, vod->trackvec, newvec);
632                 normalize_v3(q1+1);
633
634                 /* Allow for rotation beyond the interval
635                         * [-pi, pi] */
636                 while (si > 1.0f)
637                         si -= 2.0f;
638
639                 /* This relation is used instead of
640                         * phi = asin(si) so that the angle
641                         * of rotation is linearly proportional
642                         * to the distance that the mouse is
643                         * dragged. */
644                 phi = si * (float)(M_PI / 2.0);
645
646                 q1[0]= cos(phi);
647                 mul_v3_fl(q1+1, sin(phi));
648                 mul_qt_qtqt(rv3d->viewquat, q1, vod->oldquat);
649
650                 if (vod->use_dyn_ofs) {
651                         /* compute the post multiplication quat, to rotate the offset correctly */
652                         copy_qt_qt(q1, vod->oldquat);
653                         conjugate_qt(q1);
654                         mul_qt_qtqt(q1, q1, rv3d->viewquat);
655
656                         conjugate_qt(q1); /* conj == inv for unit quat */
657                         copy_v3_v3(rv3d->ofs, vod->ofs);
658                         sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
659                         mul_qt_v3(q1, rv3d->ofs);
660                         add_v3_v3(rv3d->ofs, vod->dyn_ofs);
661                 }
662         }
663         else {
664                 /* New turntable view code by John Aughey */
665                 float phi, q1[4];
666                 float m[3][3];
667                 float m_inv[3][3];
668                 float xvec[3] = {1.0f, 0.0f, 0.0f};
669                 /* Sensitivity will control how fast the viewport rotates.  0.0035 was
670                         obtained experimentally by looking at viewport rotation sensitivities
671                         on other modeling programs. */
672                 /* Perhaps this should be a configurable user parameter. */
673                 const float sensitivity = 0.0035f;
674
675                 /* Get the 3x3 matrix and its inverse from the quaternion */
676                 quat_to_mat3( m,rv3d->viewquat);
677                 invert_m3_m3(m_inv,m);
678
679                 /* Determine the direction of the x vector (for rotating up and down) */
680                 /* This can likely be computed directly from the quaternion. */
681                 mul_m3_v3(m_inv,xvec);
682
683                 /* Perform the up/down rotation */
684                 phi = sensitivity * -(y - vod->oldy);
685                 q1[0] = cos(phi);
686                 mul_v3_v3fl(q1+1, xvec, sin(phi));
687                 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
688
689                 if (vod->use_dyn_ofs) {
690                         conjugate_qt(q1); /* conj == inv for unit quat */
691                         sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
692                         mul_qt_v3(q1, rv3d->ofs);
693                         add_v3_v3(rv3d->ofs, vod->dyn_ofs);
694                 }
695
696                 /* Perform the orbital rotation */
697                 phi = sensitivity * vod->reverse * (x - vod->oldx);
698                 q1[0] = cos(phi);
699                 q1[1] = q1[2] = 0.0;
700                 q1[3] = sin(phi);
701                 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
702
703                 if (vod->use_dyn_ofs) {
704                         conjugate_qt(q1);
705                         sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
706                         mul_qt_v3(q1, rv3d->ofs);
707                         add_v3_v3(rv3d->ofs, vod->dyn_ofs);
708                 }
709         }
710
711         /* check for view snap */
712         if (vod->axis_snap){
713                 int i;
714                 float viewquat_inv[4];
715                 float zaxis[3]={0,0,1};
716                 invert_qt_qt(viewquat_inv, rv3d->viewquat);
717
718                 mul_qt_v3(viewquat_inv, zaxis);
719
720                 for (i = 0 ; i < 39; i++){
721
722                         float view = (int)snapquats[i][4];
723                         float viewquat_inv_test[4];
724                         float zaxis_test[3]={0,0,1};
725
726                         invert_qt_qt(viewquat_inv_test, snapquats[i]);
727                         mul_qt_v3(viewquat_inv_test, zaxis_test);
728                         
729                         if(angle_v3v3(zaxis_test, zaxis) < DEG2RADF(45/3)) {
730                                 /* find the best roll */
731                                 float quat_roll[4], quat_final[4], quat_best[4];
732                                 float viewquat_align[4]; /* viewquat aligned to zaxis_test */
733                                 float viewquat_align_inv[4]; /* viewquat aligned to zaxis_test */
734                                 float best_angle = FLT_MAX;
735                                 int j;
736
737                                 /* viewquat_align is the original viewquat aligned to the snapped axis
738                                  * for testing roll */
739                                 rotation_between_vecs_to_quat(viewquat_align, zaxis_test, zaxis);
740                                 normalize_qt(viewquat_align);
741                                 mul_qt_qtqt(viewquat_align, rv3d->viewquat, viewquat_align);
742                                 normalize_qt(viewquat_align);
743                                 invert_qt_qt(viewquat_align_inv, viewquat_align);
744
745                                 /* find best roll */
746                                 for(j= 0; j<8; j++) {
747                                         float angle;
748                                         float xaxis1[3]={1,0,0};
749                                         float xaxis2[3]={1,0,0};
750                                         float quat_final_inv[4];
751
752                                         axis_angle_to_quat(quat_roll, zaxis_test, (float)j * DEG2RADF(45.0f));
753                                         normalize_qt(quat_roll);
754
755                                         mul_qt_qtqt(quat_final, snapquats[i], quat_roll);
756                                         normalize_qt(quat_final);
757                                         
758                                         /* compare 2 vector angles to find the least roll */
759                                         invert_qt_qt(quat_final_inv, quat_final);
760                                         mul_qt_v3(viewquat_align_inv, xaxis1);
761                                         mul_qt_v3(quat_final_inv, xaxis2);
762                                         angle= angle_v3v3(xaxis1, xaxis2);
763
764                                         if(angle <= best_angle) {
765                                                 best_angle= angle;
766                                                 copy_qt_qt(quat_best, quat_final);
767                                                 if(j) view= 0; /* view grid assumes certain up axis */
768                                         }
769                                 }
770
771                                 copy_qt_qt(rv3d->viewquat, quat_best);
772                                 rv3d->view= view; /* if we snap to a rolled camera the grid is invalid */
773
774                                 break;
775                         }
776                 }
777         }
778         vod->oldx= x;
779         vod->oldy= y;
780
781         /* avoid precision loss over time */
782         normalize_qt(rv3d->viewquat);
783
784         ED_view3d_camera_lock_sync(vod->v3d, rv3d);
785
786         ED_region_tag_redraw(vod->ar);
787 }
788
789 static int viewrotate_modal(bContext *C, wmOperator *op, wmEvent *event)
790 {
791         ViewOpsData *vod= op->customdata;
792         short event_code= VIEW_PASS;
793
794         /* execute the events */
795         if(event->type==MOUSEMOVE) {
796                 event_code= VIEW_APPLY;
797         }
798         else if(event->type==EVT_MODAL_MAP) {
799                 switch (event->val) {
800                         case VIEW_MODAL_CONFIRM:
801                                 event_code= VIEW_CONFIRM;
802                                 break;
803                         case VIEWROT_MODAL_AXIS_SNAP_ENABLE:
804                                 vod->axis_snap= TRUE;
805                                 event_code= VIEW_APPLY;
806                                 break;
807                         case VIEWROT_MODAL_AXIS_SNAP_DISABLE:
808                                 vod->axis_snap= FALSE;
809                                 event_code= VIEW_APPLY;
810                                 break;
811                         case VIEWROT_MODAL_SWITCH_ZOOM:
812                                 WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
813                                 event_code= VIEW_CONFIRM;
814                                 break;
815                         case VIEWROT_MODAL_SWITCH_MOVE:
816                                 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
817                                 event_code= VIEW_CONFIRM;
818                                 break;
819                 }
820         }
821         else if(event->type==vod->origkey && event->val==KM_RELEASE) {
822                 event_code= VIEW_CONFIRM;
823         }
824
825         if(event_code==VIEW_APPLY) {
826                 viewrotate_apply(vod, event->x, event->y);
827         }
828         else if (event_code==VIEW_CONFIRM) {
829                 ED_view3d_depth_tag_update(vod->rv3d);
830                 viewops_data_free(C, op);
831
832                 return OPERATOR_FINISHED;
833         }
834
835         return OPERATOR_RUNNING_MODAL;
836 }
837
838 static int viewrotate_invoke(bContext *C, wmOperator *op, wmEvent *event)
839 {
840         ViewOpsData *vod;
841         RegionView3D *rv3d;
842
843         /* makes op->customdata */
844         viewops_data_create(C, op, event);
845         vod= op->customdata;
846         rv3d= vod->rv3d;
847
848         if(rv3d->viewlock) { /* poll should check but in some cases fails, see poll func for details */
849                 viewops_data_free(C, op);
850                 return OPERATOR_PASS_THROUGH;
851         }
852
853         /* switch from camera view when: */
854         if(rv3d->persp != RV3D_PERSP) {
855
856                 if (U.uiflag & USER_AUTOPERSP) {
857                         if(!ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
858                                 rv3d->persp= RV3D_PERSP;
859                         }
860                 }
861                 else if(rv3d->persp==RV3D_CAMOB) {
862
863                         /* changed since 2.4x, use the camera view */
864                         if(vod->v3d->camera) {
865                                 ED_view3d_from_object(vod->v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, NULL);
866                         }
867
868                         if(!ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
869                                 rv3d->persp= rv3d->lpersp;
870                         }
871                 }
872                 ED_region_tag_redraw(vod->ar);
873         }
874         
875         if (event->type == MOUSEPAN) {
876                 viewrotate_apply(vod, event->prevx, event->prevy);
877                 ED_view3d_depth_tag_update(rv3d);
878                 
879                 viewops_data_free(C, op);
880                 
881                 return OPERATOR_FINISHED;
882         }
883         else if (event->type == MOUSEROTATE) {
884                 /* MOUSEROTATE performs orbital rotation, so y axis delta is set to 0 */
885                 viewrotate_apply(vod, event->prevx, event->y);
886                 ED_view3d_depth_tag_update(rv3d);
887                 
888                 viewops_data_free(C, op);
889                 
890                 return OPERATOR_FINISHED;
891         }
892         else {          
893                 /* add temp handler */
894                 WM_event_add_modal_handler(C, op);
895
896                 return OPERATOR_RUNNING_MODAL;
897         }
898 }
899
900 static int view3d_camera_active_poll(bContext *C)
901 {
902         if(ED_operator_view3d_active(C)) {
903                 RegionView3D *rv3d= CTX_wm_region_view3d(C);
904                 if(rv3d && rv3d->persp==RV3D_CAMOB) {
905                         return 1;
906                 }
907         }
908
909         return 0;
910 }
911
912 static int viewrotate_cancel(bContext *C, wmOperator *op)
913 {
914         viewops_data_free(C, op);
915
916         return OPERATOR_CANCELLED;
917 }
918
919 void VIEW3D_OT_rotate(wmOperatorType *ot)
920 {
921
922         /* identifiers */
923         ot->name= "Rotate view";
924         ot->description = "Rotate the view";
925         ot->idname= "VIEW3D_OT_rotate";
926
927         /* api callbacks */
928         ot->invoke= viewrotate_invoke;
929         ot->modal= viewrotate_modal;
930         ot->poll= ED_operator_region_view3d_active;
931         ot->cancel= viewrotate_cancel;
932
933         /* flags */
934         ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
935 }
936
937 // NDOF utility functions
938 // (should these functions live in this file?)
939 float ndof_to_axis_angle(struct wmNDOFMotionData* ndof, float axis[3])
940 {
941         return ndof->dt * normalize_v3_v3(axis, ndof->rvec);
942 }
943
944 void ndof_to_quat(struct wmNDOFMotionData* ndof, float q[4])
945 {
946         float axis[3];
947         float angle;
948
949         angle= ndof_to_axis_angle(ndof, axis);
950         axis_angle_to_quat(q, axis, angle);
951 }
952
953 /* -- "orbit" navigation (trackball/turntable)
954  * -- zooming
955  * -- panning in rotationally-locked views
956  */
957 static int ndof_orbit_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
958 {
959         if (event->type != NDOF_MOTION) {
960                 return OPERATOR_CANCELLED;
961         }
962         else {
963                 View3D *v3d= CTX_wm_view3d(C);
964                 RegionView3D* rv3d = CTX_wm_region_view3d(C);
965                 wmNDOFMotionData* ndof = (wmNDOFMotionData*) event->customdata;
966
967                 ED_view3d_camera_lock_init(v3d, rv3d);
968
969                 rv3d->rot_angle = 0.f; // off by default, until changed later this function
970
971                 if (ndof->progress != P_FINISHING) {
972                         const float dt = ndof->dt;
973                 
974                         // tune these until everything feels right
975                         const float rot_sensitivity = 1.f;
976                         const float zoom_sensitivity = 1.f;
977                         const float pan_sensitivity = 1.f;
978                 
979                         // rather have bool, but...
980                         int has_rotation = rv3d->viewlock != RV3D_LOCKED && !is_zero_v3(ndof->rvec);
981                 
982                         float view_inv[4];
983                         invert_qt_qt(view_inv, rv3d->viewquat);
984                 
985                         //#define DEBUG_NDOF_MOTION
986                         #ifdef DEBUG_NDOF_MOTION
987                         printf("ndof: T=(%.2f,%.2f,%.2f) R=(%.2f,%.2f,%.2f) dt=%.3f delivered to 3D view\n",
988                                 ndof->tx, ndof->ty, ndof->tz, ndof->rx, ndof->ry, ndof->rz, ndof->dt);
989                         #endif
990                 
991                         if (ndof->tvec[2]) {
992                                 // Zoom!
993                                 // velocity should be proportional to the linear velocity attained by rotational motion of same strength
994                                 // [got that?]
995                                 // proportional to arclength = radius * angle
996                 
997                                 float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tvec[2];
998
999                                 if (U.ndof_flag & NDOF_ZOOM_INVERT)
1000                                         zoom_distance = -zoom_distance;
1001
1002                                 rv3d->dist += zoom_distance;
1003                         }
1004                 
1005                         if (rv3d->viewlock == RV3D_LOCKED) {
1006                                 /* rotation not allowed -- explore panning options instead */
1007                                 float pan_vec[3] = {ndof->tvec[0], ndof->tvec[1], 0.0f};
1008                                 mul_v3_fl(pan_vec, pan_sensitivity * rv3d->dist * dt);
1009                 
1010                                 /* transform motion from view to world coordinates */
1011                                 invert_qt_qt(view_inv, rv3d->viewquat);
1012                                 mul_qt_v3(view_inv, pan_vec);
1013                 
1014                                 /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1015                                 sub_v3_v3(rv3d->ofs, pan_vec);
1016                         }
1017                 
1018                         if (has_rotation) {
1019                 
1020                                 const int invert = U.ndof_flag & NDOF_ORBIT_INVERT_AXES;
1021                 
1022                                 rv3d->view = RV3D_VIEW_USER;
1023                 
1024                                 if (U.flag & USER_TRACKBALL) {
1025                                         float rot[4];
1026                                         float axis[3];
1027                                         float angle = rot_sensitivity * ndof_to_axis_angle(ndof, axis);
1028                 
1029                                         if (invert)
1030                                                 angle = -angle;
1031                 
1032                                         // transform rotation axis from view to world coordinates
1033                                         mul_qt_v3(view_inv, axis);
1034                 
1035                                         // update the onscreen doo-dad
1036                                         rv3d->rot_angle = angle;
1037                                         copy_v3_v3(rv3d->rot_axis, axis);
1038                 
1039                                         axis_angle_to_quat(rot, axis, angle);
1040
1041                                         // apply rotation
1042                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1043                                 } else {
1044                                         /* turntable view code by John Aughey, adapted for 3D mouse by [mce] */
1045                                         float angle, rot[4];
1046                                         float xvec[3] = {1,0,0};
1047                 
1048                                         /* Determine the direction of the x vector (for rotating up and down) */
1049                                         mul_qt_v3(view_inv, xvec);
1050                 
1051                                         /* Perform the up/down rotation */
1052                                         angle = rot_sensitivity * dt * ndof->rvec[0];
1053                                         if (invert)
1054                                                 angle = -angle;
1055                                         rot[0] = cos(angle);
1056                                         mul_v3_v3fl(rot+1, xvec, sin(angle));
1057                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1058                 
1059                                         /* Perform the orbital rotation */
1060                                         angle = rot_sensitivity * dt * ndof->rvec[1];
1061                                         if (invert)
1062                                                 angle = -angle;
1063                 
1064                                         // update the onscreen doo-dad
1065                                         rv3d->rot_angle = angle;
1066                                         rv3d->rot_axis[0] = 0;
1067                                         rv3d->rot_axis[1] = 0;
1068                                         rv3d->rot_axis[2] = 1;
1069                 
1070                                         rot[0] = cos(angle);
1071                                         rot[1] = rot[2] = 0.0;
1072                                         rot[3] = sin(angle);
1073                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1074                                 }
1075                         }
1076                 }
1077
1078                 ED_view3d_camera_lock_sync(v3d, rv3d);
1079
1080                 ED_region_tag_redraw(CTX_wm_region(C));
1081
1082                 return OPERATOR_FINISHED;
1083         }
1084 }
1085
1086 void VIEW3D_OT_ndof_orbit(struct wmOperatorType *ot)
1087 {
1088         /* identifiers */
1089         ot->name = "NDOF Orbit View";
1090         ot->description = "Explore every angle of an object using the 3D mouse.";
1091         ot->idname = "VIEW3D_OT_ndof_orbit";
1092
1093         /* api callbacks */
1094         ot->invoke = ndof_orbit_invoke;
1095         ot->poll = ED_operator_view3d_active;
1096
1097         /* flags */
1098         ot->flag = 0;
1099 }
1100
1101 /* -- "pan" navigation
1102  * -- zoom or dolly?
1103  */
1104 static int ndof_pan_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
1105 {
1106         if (event->type != NDOF_MOTION) {
1107                 return OPERATOR_CANCELLED;
1108         }
1109         else {
1110                 View3D *v3d= CTX_wm_view3d(C);
1111                 RegionView3D* rv3d = CTX_wm_region_view3d(C);
1112                 wmNDOFMotionData* ndof = (wmNDOFMotionData*) event->customdata;
1113
1114                 ED_view3d_camera_lock_init(v3d, rv3d);
1115
1116                 rv3d->rot_angle = 0.f; // we're panning here! so erase any leftover rotation from other operators
1117
1118                 if (ndof->progress != P_FINISHING) {
1119                         const float dt = ndof->dt;
1120                         float view_inv[4];
1121 #if 0 // ------------------------------------------- zoom with Z
1122                         // tune these until everything feels right
1123                         const float zoom_sensitivity = 1.f;
1124                         const float pan_sensitivity = 1.f;
1125
1126                         float pan_vec[3] = {
1127                                 ndof->tx, ndof->ty, 0
1128                                 };
1129
1130                         // "zoom in" or "translate"? depends on zoom mode in user settings?
1131                         if (ndof->tz) {
1132                                 float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tz;
1133                                 rv3d->dist += zoom_distance;
1134                         }
1135                 
1136                         mul_v3_fl(pan_vec, pan_sensitivity * rv3d->dist * dt);
1137 #else // ------------------------------------------------------- dolly with Z
1138                         float speed = 10.f; // blender units per second
1139                         // ^^ this is ok for default cube scene, but should scale with.. something
1140
1141                         // tune these until everything feels right
1142                         const float forward_sensitivity = 1.f;
1143                         const float vertical_sensitivity = 0.4f;
1144                         const float lateral_sensitivity = 0.6f;
1145
1146                         float pan_vec[3] = {lateral_sensitivity * ndof->tvec[0],
1147                                                                 vertical_sensitivity * ndof->tvec[1],
1148                                                                 forward_sensitivity * ndof->tvec[2]
1149                                                            };
1150
1151                         mul_v3_fl(pan_vec, speed * dt);
1152 #endif
1153                         /* transform motion from view to world coordinates */
1154                         invert_qt_qt(view_inv, rv3d->viewquat);
1155                         mul_qt_v3(view_inv, pan_vec);
1156
1157                         /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1158                         sub_v3_v3(rv3d->ofs, pan_vec);
1159                 }
1160
1161                 ED_view3d_camera_lock_sync(v3d, rv3d);
1162
1163                 ED_region_tag_redraw(CTX_wm_region(C));
1164
1165                 return OPERATOR_FINISHED;
1166         }
1167 }
1168
1169 void VIEW3D_OT_ndof_pan(struct wmOperatorType *ot)
1170 {
1171         /* identifiers */
1172         ot->name = "NDOF Pan View";
1173         ot->description = "Position your viewpoint with the 3D mouse.";
1174         ot->idname = "VIEW3D_OT_ndof_pan";
1175
1176         /* api callbacks */
1177         ot->invoke = ndof_pan_invoke;
1178         ot->poll = ED_operator_view3d_active;
1179
1180         /* flags */
1181         ot->flag = 0;
1182 }
1183
1184 /* ************************ viewmove ******************************** */
1185
1186
1187 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
1188
1189 /* called in transform_ops.c, on each regeneration of keymaps  */
1190 void viewmove_modal_keymap(wmKeyConfig *keyconf)
1191 {
1192         static EnumPropertyItem modal_items[] = {
1193         {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Confirm", ""},
1194                 
1195         {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
1196         {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1197
1198         {0, NULL, 0, NULL, NULL}};
1199
1200         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Move Modal");
1201
1202         /* this function is called for each spacetype, only needs to add map once */
1203         if(keymap) return;
1204
1205         keymap= WM_modalkeymap_add(keyconf, "View3D Move Modal", modal_items);
1206
1207         /* items for modal map */
1208         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1209         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1210
1211         /* disabled mode switching for now, can re-implement better, later on
1212         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
1213         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
1214         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1215         */
1216         
1217         /* assign map to operators */
1218         WM_modalkeymap_assign(keymap, "VIEW3D_OT_move");
1219 }
1220
1221
1222 static void viewmove_apply(ViewOpsData *vod, int x, int y)
1223 {
1224         if((vod->rv3d->persp==RV3D_CAMOB) && !ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
1225                 const float zoomfac= BKE_screen_view3d_zoom_to_fac((float)vod->rv3d->camzoom) * 2.0f;
1226                 vod->rv3d->camdx += (vod->oldx - x)/(vod->ar->winx * zoomfac);
1227                 vod->rv3d->camdy += (vod->oldy - y)/(vod->ar->winy * zoomfac);
1228                 CLAMP(vod->rv3d->camdx, -1.0f, 1.0f);
1229                 CLAMP(vod->rv3d->camdy, -1.0f, 1.0f);
1230         }
1231         else {
1232                 float dvec[3];
1233                 float mval_f[2];
1234
1235                 mval_f[0]= x - vod->oldx;
1236                 mval_f[1]= y - vod->oldy;
1237                 ED_view3d_win_to_delta(vod->ar, mval_f, dvec);
1238
1239                 add_v3_v3(vod->rv3d->ofs, dvec);
1240
1241                 if(vod->rv3d->viewlock & RV3D_BOXVIEW)
1242                         view3d_boxview_sync(vod->sa, vod->ar);
1243         }
1244
1245         vod->oldx= x;
1246         vod->oldy= y;
1247
1248         ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1249
1250         ED_region_tag_redraw(vod->ar);
1251 }
1252
1253
1254 static int viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
1255 {
1256
1257         ViewOpsData *vod= op->customdata;
1258         short event_code= VIEW_PASS;
1259
1260         /* execute the events */
1261         if(event->type==MOUSEMOVE) {
1262                 event_code= VIEW_APPLY;
1263         }
1264         else if(event->type==EVT_MODAL_MAP) {
1265                 switch (event->val) {
1266                         case VIEW_MODAL_CONFIRM:
1267                                 event_code= VIEW_CONFIRM;
1268                                 break;
1269                         case VIEWROT_MODAL_SWITCH_ZOOM:
1270                                 WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
1271                                 event_code= VIEW_CONFIRM;
1272                                 break;
1273                         case VIEWROT_MODAL_SWITCH_ROTATE:
1274                                 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1275                                 event_code= VIEW_CONFIRM;
1276                                 break;
1277                 }
1278         }
1279         else if(event->type==vod->origkey && event->val==KM_RELEASE) {
1280                 event_code= VIEW_CONFIRM;
1281         }
1282
1283         if(event_code==VIEW_APPLY) {
1284                 viewmove_apply(vod, event->x, event->y);
1285         }
1286         else if (event_code==VIEW_CONFIRM) {
1287                 ED_view3d_depth_tag_update(vod->rv3d);
1288
1289                 viewops_data_free(C, op);
1290
1291                 return OPERATOR_FINISHED;
1292         }
1293
1294         return OPERATOR_RUNNING_MODAL;
1295 }
1296
1297 static int viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
1298 {
1299         ViewOpsData *vod;
1300
1301         /* makes op->customdata */
1302         viewops_data_create(C, op, event);
1303         vod= op->customdata;
1304
1305         if (event->type == MOUSEPAN) {
1306                 viewmove_apply(vod, event->prevx, event->prevy);
1307                 ED_view3d_depth_tag_update(vod->rv3d);
1308                 
1309                 viewops_data_free(C, op);               
1310                 
1311                 return OPERATOR_FINISHED;
1312         }
1313         else {
1314                 /* add temp handler */
1315                 WM_event_add_modal_handler(C, op);
1316
1317                 return OPERATOR_RUNNING_MODAL;
1318         }
1319 }
1320
1321 static int viewmove_cancel(bContext *C, wmOperator *op)
1322 {
1323         viewops_data_free(C, op);
1324
1325         return OPERATOR_CANCELLED;
1326 }
1327
1328 void VIEW3D_OT_move(wmOperatorType *ot)
1329 {
1330
1331         /* identifiers */
1332         ot->name= "Move view";
1333         ot->description = "Move the view";
1334         ot->idname= "VIEW3D_OT_move";
1335
1336         /* api callbacks */
1337         ot->invoke= viewmove_invoke;
1338         ot->modal= viewmove_modal;
1339         ot->poll= ED_operator_view3d_active;
1340         ot->cancel= viewmove_cancel;
1341
1342         /* flags */
1343         ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
1344 }
1345
1346 /* ************************ viewzoom ******************************** */
1347
1348 /* viewdolly_modal_keymap has an exact copy of this, apply fixes to both */
1349 /* called in transform_ops.c, on each regeneration of keymaps  */
1350 void viewzoom_modal_keymap(wmKeyConfig *keyconf)
1351 {
1352         static EnumPropertyItem modal_items[] = {
1353         {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Confirm", ""},
1354                 
1355         {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1356         {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
1357
1358         {0, NULL, 0, NULL, NULL}};
1359
1360         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Zoom Modal");
1361
1362         /* this function is called for each spacetype, only needs to add map once */
1363         if(keymap) return;
1364
1365         keymap= WM_modalkeymap_add(keyconf, "View3D Zoom Modal", modal_items);
1366
1367         /* items for modal map */
1368         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1369         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1370
1371         /* disabled mode switching for now, can re-implement better, later on
1372         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1373         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1374         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
1375          */
1376         
1377         /* assign map to operators */
1378         WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom");
1379 }
1380
1381 static void view_zoom_mouseloc(ARegion *ar, float dfac, int mx, int my)
1382 {
1383         RegionView3D *rv3d= ar->regiondata;
1384
1385         if(U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
1386                 float dvec[3];
1387                 float tvec[3];
1388                 float tpos[3];
1389                 float mval_f[2];
1390                 float new_dist;
1391
1392                 negate_v3_v3(tpos, rv3d->ofs);
1393
1394                 /* Project cursor position into 3D space */
1395                 initgrabz(rv3d, tpos[0], tpos[1], tpos[2]);
1396
1397                 mval_f[0]= (float)(((mx - ar->winrct.xmin) * 2) - ar->winx) / 2.0f;
1398                 mval_f[1]= (float)(((my - ar->winrct.ymin) * 2) - ar->winy) / 2.0f;
1399                 ED_view3d_win_to_delta(ar, mval_f, dvec);
1400
1401                 /* Calculate view target position for dolly */
1402                 add_v3_v3v3(tvec, tpos, dvec);
1403                 negate_v3(tvec);
1404
1405                 /* Offset to target position and dolly */
1406                 new_dist = rv3d->dist * dfac;
1407
1408                 copy_v3_v3(rv3d->ofs, tvec);
1409                 rv3d->dist = new_dist;
1410
1411                 /* Calculate final offset */
1412                 madd_v3_v3v3fl(rv3d->ofs, tvec, dvec, dfac);
1413         } else {
1414                 rv3d->dist *= dfac;
1415         }
1416 }
1417
1418
1419 static void viewzoom_apply(ViewOpsData *vod, int x, int y, const short viewzoom, const short zoom_invert)
1420 {
1421         float zfac=1.0;
1422
1423         if(viewzoom==USER_ZOOM_CONT) {
1424                 double time= PIL_check_seconds_timer();
1425                 float time_step= (float)(time - vod->timer_lastdraw);
1426                 float fac;
1427
1428                 if (U.uiflag & USER_ZOOM_HORIZ) {
1429                         fac= (float)(x - vod->origx);
1430                 }
1431                 else {
1432                         fac= (float)(y - vod->origy);
1433                 }
1434
1435                 if(zoom_invert) {
1436                         fac= -fac;
1437                 }
1438
1439                 // oldstyle zoom
1440                 zfac = 1.0f + ((fac / 20.0f) * time_step);
1441                 vod->timer_lastdraw= time;
1442         }
1443         else if(viewzoom==USER_ZOOM_SCALE) {
1444                 int ctr[2], len1, len2;
1445                 // method which zooms based on how far you move the mouse
1446
1447                 ctr[0] = (vod->ar->winrct.xmax + vod->ar->winrct.xmin)/2;
1448                 ctr[1] = (vod->ar->winrct.ymax + vod->ar->winrct.ymin)/2;
1449
1450                 len1 = (int)sqrt((ctr[0] - x)*(ctr[0] - x) + (ctr[1] - y)*(ctr[1] - y)) + 5;
1451                 len2 = (int)sqrt((ctr[0] - vod->origx)*(ctr[0] - vod->origx) + (ctr[1] - vod->origy)*(ctr[1] - vod->origy)) + 5;
1452
1453                 zfac = vod->dist0 * ((float)len2/len1) / vod->rv3d->dist;
1454         }
1455         else {  /* USER_ZOOM_DOLLY */
1456                 float len1, len2;
1457                 
1458                 if (U.uiflag & USER_ZOOM_HORIZ) {
1459                         len1 = (vod->ar->winrct.xmax - x) + 5;
1460                         len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
1461                 }
1462                 else {
1463                         len1 = (vod->ar->winrct.ymax - y) + 5;
1464                         len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
1465                 }
1466                 if (zoom_invert) {
1467                         SWAP(float, len1, len2);
1468                 }
1469                 
1470                 zfac = vod->dist0 * (2.0f * ((len2/len1)-1.0f) + 1.0f) / vod->rv3d->dist;
1471         }
1472
1473         if(zfac != 1.0f && zfac*vod->rv3d->dist > 0.001f * vod->grid &&
1474                         zfac * vod->rv3d->dist < 10.0f * vod->far)
1475                 view_zoom_mouseloc(vod->ar, zfac, vod->oldx, vod->oldy);
1476
1477         /* these limits were in old code too */
1478         if(vod->rv3d->dist<0.001f * vod->grid) vod->rv3d->dist= 0.001f * vod->grid;
1479         if(vod->rv3d->dist>10.0f * vod->far) vod->rv3d->dist=10.0f * vod->far;
1480
1481         if(vod->rv3d->viewlock & RV3D_BOXVIEW)
1482                 view3d_boxview_sync(vod->sa, vod->ar);
1483
1484         ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1485
1486         ED_region_tag_redraw(vod->ar);
1487 }
1488
1489
1490 static int viewzoom_modal(bContext *C, wmOperator *op, wmEvent *event)
1491 {
1492         ViewOpsData *vod= op->customdata;
1493         short event_code= VIEW_PASS;
1494
1495         /* execute the events */
1496         if (event->type == TIMER && event->customdata == vod->timer) {
1497                 /* continuous zoom */
1498                 event_code= VIEW_APPLY;
1499         }
1500         else if(event->type==MOUSEMOVE) {
1501                 event_code= VIEW_APPLY;
1502         }
1503         else if(event->type==EVT_MODAL_MAP) {
1504                 switch (event->val) {
1505                         case VIEW_MODAL_CONFIRM:
1506                                 event_code= VIEW_CONFIRM;
1507                                 break;
1508                         case VIEWROT_MODAL_SWITCH_MOVE:
1509                                 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
1510                                 event_code= VIEW_CONFIRM;
1511                                 break;
1512                         case VIEWROT_MODAL_SWITCH_ROTATE:
1513                                 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1514                                 event_code= VIEW_CONFIRM;
1515                                 break;
1516                 }
1517         }
1518         else if(event->type==vod->origkey && event->val==KM_RELEASE) {
1519                 event_code= VIEW_CONFIRM;
1520         }
1521
1522         if(event_code==VIEW_APPLY) {
1523                 viewzoom_apply(vod, event->x, event->y, U.viewzoom, (U.uiflag & USER_ZOOM_INVERT) != 0);
1524         }
1525         else if (event_code==VIEW_CONFIRM) {
1526                 ED_view3d_depth_tag_update(vod->rv3d);
1527                 viewops_data_free(C, op);
1528
1529                 return OPERATOR_FINISHED;
1530         }
1531
1532         return OPERATOR_RUNNING_MODAL;
1533 }
1534
1535 static int viewzoom_exec(bContext *C, wmOperator *op)
1536 {
1537         View3D *v3d;
1538         RegionView3D *rv3d;
1539         ScrArea *sa;
1540         ARegion *ar;
1541         short use_cam_zoom;
1542
1543         int delta= RNA_int_get(op->ptr, "delta");
1544         int mx, my;
1545
1546         if(op->customdata) {
1547                 ViewOpsData *vod= op->customdata;
1548
1549                 sa= vod->sa;
1550                 ar= vod->ar;
1551         }
1552         else {
1553                 sa= CTX_wm_area(C);
1554                 ar= CTX_wm_region(C);
1555         }
1556
1557         v3d= sa->spacedata.first;
1558         rv3d= ar->regiondata;
1559
1560         mx= RNA_property_is_set(op->ptr, "mx") ? RNA_int_get(op->ptr, "mx") : ar->winx / 2;
1561         my= RNA_property_is_set(op->ptr, "my") ? RNA_int_get(op->ptr, "my") : ar->winy / 2;
1562
1563         use_cam_zoom= (rv3d->persp==RV3D_CAMOB) && !(rv3d->is_persp && ED_view3d_camera_lock_check(v3d, rv3d));
1564
1565         if(delta < 0) {
1566                 /* this min and max is also in viewmove() */
1567                 if(use_cam_zoom) {
1568                         rv3d->camzoom-= 10;
1569                         if(rv3d->camzoom < RV3D_CAMZOOM_MIN) rv3d->camzoom= RV3D_CAMZOOM_MIN;
1570                 }
1571                 else if(rv3d->dist < 10.0f * v3d->far) {
1572                         view_zoom_mouseloc(ar, 1.2f, mx, my);
1573                 }
1574         }
1575         else {
1576                 if(use_cam_zoom) {
1577                         rv3d->camzoom+= 10;
1578                         if(rv3d->camzoom > RV3D_CAMZOOM_MAX) rv3d->camzoom= RV3D_CAMZOOM_MAX;
1579                 }
1580                 else if(rv3d->dist> 0.001f * v3d->grid) {
1581                         view_zoom_mouseloc(ar, .83333f, mx, my);
1582                 }
1583         }
1584
1585         if(rv3d->viewlock & RV3D_BOXVIEW)
1586                 view3d_boxview_sync(sa, ar);
1587
1588         ED_view3d_depth_tag_update(rv3d);
1589
1590         ED_view3d_camera_lock_sync(v3d, rv3d);
1591
1592         ED_region_tag_redraw(ar);
1593
1594         viewops_data_free(C, op);
1595
1596         return OPERATOR_FINISHED;
1597 }
1598
1599 /* this is an exact copy of viewzoom_modal_keymap */
1600 /* called in transform_ops.c, on each regeneration of keymaps  */
1601 void viewdolly_modal_keymap(wmKeyConfig *keyconf)
1602 {
1603         static EnumPropertyItem modal_items[] = {
1604         {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Confirm", ""},
1605
1606         {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1607         {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
1608
1609         {0, NULL, 0, NULL, NULL}};
1610
1611         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Dolly Modal");
1612
1613         /* this function is called for each spacetype, only needs to add map once */
1614         if(keymap) return;
1615
1616         keymap= WM_modalkeymap_add(keyconf, "View3D Dolly Modal", modal_items);
1617
1618         /* items for modal map */
1619         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1620         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1621
1622         /* disabled mode switching for now, can re-implement better, later on
1623         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1624         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1625         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
1626          */
1627         
1628         /* assign map to operators */
1629         WM_modalkeymap_assign(keymap, "VIEW3D_OT_dolly");
1630 }
1631
1632 /* viewdolly_invoke() copied this function, changes here may apply there */
1633 static int viewzoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
1634 {
1635         ViewOpsData *vod;
1636
1637         /* makes op->customdata */
1638         viewops_data_create(C, op, event);
1639         vod= op->customdata;
1640
1641         /* if one or the other zoom position aren't set, set from event */
1642         if (!RNA_property_is_set(op->ptr, "mx") || !RNA_property_is_set(op->ptr, "my")) {
1643                 RNA_int_set(op->ptr, "mx", event->x);
1644                 RNA_int_set(op->ptr, "my", event->y);
1645         }
1646
1647         if(RNA_property_is_set(op->ptr, "delta")) {
1648                 viewzoom_exec(C, op);
1649         }
1650         else {
1651                 if (event->type == MOUSEZOOM) {
1652                         /* Bypass Zoom invert flag for track pads (pass FALSE always) */
1653
1654                         if (U.uiflag & USER_ZOOM_HORIZ) {
1655                                 vod->origx = vod->oldx = event->x;
1656                                 viewzoom_apply(vod, event->prevx, event->prevy, USER_ZOOM_DOLLY, FALSE);
1657                         }
1658                         else {
1659                                 /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
1660                                 vod->origy = vod->oldy = vod->origy + event->x - event->prevx;
1661                                 viewzoom_apply(vod, event->prevx, event->prevy, USER_ZOOM_DOLLY, FALSE);
1662                         }
1663                         ED_view3d_depth_tag_update(vod->rv3d);
1664                         
1665                         viewops_data_free(C, op);
1666                         return OPERATOR_FINISHED;
1667                 }
1668                 else {
1669                         if(U.viewzoom == USER_ZOOM_CONT) {
1670                                 /* needs a timer to continue redrawing */
1671                                 vod->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
1672                                 vod->timer_lastdraw= PIL_check_seconds_timer();
1673                         }
1674
1675                         /* add temp handler */
1676                         WM_event_add_modal_handler(C, op);
1677
1678                         return OPERATOR_RUNNING_MODAL;
1679                 }
1680         }
1681         return OPERATOR_FINISHED;
1682 }
1683
1684 static int viewzoom_cancel(bContext *C, wmOperator *op)
1685 {
1686         viewops_data_free(C, op);
1687
1688         return OPERATOR_CANCELLED;
1689 }
1690
1691 void VIEW3D_OT_zoom(wmOperatorType *ot)
1692 {
1693         /* identifiers */
1694         ot->name= "Zoom View";
1695         ot->description = "Zoom in/out in the view";
1696         ot->idname= "VIEW3D_OT_zoom";
1697
1698         /* api callbacks */
1699         ot->invoke= viewzoom_invoke;
1700         ot->exec= viewzoom_exec;
1701         ot->modal= viewzoom_modal;
1702         ot->poll= ED_operator_region_view3d_active;
1703         ot->cancel= viewzoom_cancel;
1704
1705         /* flags */
1706         ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
1707
1708         RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
1709         RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
1710         RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
1711 }
1712
1713
1714 /* ************************ viewdolly ******************************** */
1715 static void view_dolly_mouseloc(ARegion *ar, float orig_ofs[3], float dvec[3], float dfac)
1716 {
1717         RegionView3D *rv3d= ar->regiondata;
1718         madd_v3_v3v3fl(rv3d->ofs, orig_ofs, dvec, -(1.0f - dfac));
1719 }
1720
1721 static void viewdolly_apply(ViewOpsData *vod, int x, int y, const short zoom_invert)
1722 {
1723         float zfac=1.0;
1724
1725         {
1726                 float len1, len2;
1727
1728                 if (U.uiflag & USER_ZOOM_HORIZ) {
1729                         len1 = (vod->ar->winrct.xmax - x) + 5;
1730                         len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
1731                 }
1732                 else {
1733                         len1 = (vod->ar->winrct.ymax - y) + 5;
1734                         len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
1735                 }
1736                 if (zoom_invert)
1737                         SWAP(float, len1, len2);
1738
1739                 zfac =  1.0f + ((len2 - len1) * 0.01f * vod->rv3d->dist);
1740         }
1741
1742         if(zfac != 1.0f)
1743                 view_dolly_mouseloc(vod->ar, vod->ofs, vod->mousevec, zfac);
1744
1745         if(vod->rv3d->viewlock & RV3D_BOXVIEW)
1746                 view3d_boxview_sync(vod->sa, vod->ar);
1747
1748         ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1749
1750         ED_region_tag_redraw(vod->ar);
1751 }
1752
1753
1754 static int viewdolly_modal(bContext *C, wmOperator *op, wmEvent *event)
1755 {
1756         ViewOpsData *vod= op->customdata;
1757         short event_code= VIEW_PASS;
1758
1759         /* execute the events */
1760         if(event->type==MOUSEMOVE) {
1761                 event_code= VIEW_APPLY;
1762         }
1763         else if(event->type==EVT_MODAL_MAP) {
1764                 switch (event->val) {
1765                         case VIEW_MODAL_CONFIRM:
1766                                 event_code= VIEW_CONFIRM;
1767                                 break;
1768                         case VIEWROT_MODAL_SWITCH_MOVE:
1769                                 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
1770                                 event_code= VIEW_CONFIRM;
1771                                 break;
1772                         case VIEWROT_MODAL_SWITCH_ROTATE:
1773                                 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1774                                 event_code= VIEW_CONFIRM;
1775                                 break;
1776                 }
1777         }
1778         else if(event->type==vod->origkey && event->val==KM_RELEASE) {
1779                 event_code= VIEW_CONFIRM;
1780         }
1781
1782         if(event_code==VIEW_APPLY) {
1783                 viewdolly_apply(vod, event->x, event->y, (U.uiflag & USER_ZOOM_INVERT) != 0);
1784         }
1785         else if (event_code==VIEW_CONFIRM) {
1786                 ED_view3d_depth_tag_update(vod->rv3d);
1787                 viewops_data_free(C, op);
1788
1789                 return OPERATOR_FINISHED;
1790         }
1791
1792         return OPERATOR_RUNNING_MODAL;
1793 }
1794
1795 static int viewdolly_exec(bContext *C, wmOperator *op)
1796 {
1797         /* View3D *v3d; */
1798         RegionView3D *rv3d;
1799         ScrArea *sa;
1800         ARegion *ar;
1801         float mousevec[3];
1802
1803         int delta= RNA_int_get(op->ptr, "delta");
1804
1805         if(op->customdata) {
1806                 ViewOpsData *vod= op->customdata;
1807
1808                 sa= vod->sa;
1809                 ar= vod->ar;
1810                 copy_v3_v3(mousevec, vod->mousevec);
1811         }
1812         else {
1813                 sa= CTX_wm_area(C);
1814                 ar= CTX_wm_region(C);
1815                 negate_v3_v3(mousevec, ((RegionView3D *)ar->regiondata)->viewinv[2]);
1816                 normalize_v3(mousevec);
1817         }
1818
1819         /* v3d= sa->spacedata.first; */ /* UNUSED */
1820         rv3d= ar->regiondata;
1821
1822         /* overwrite the mouse vector with the view direction (zoom into the center) */
1823         if((U.uiflag & USER_ZOOM_TO_MOUSEPOS) == 0) {
1824                 normalize_v3_v3(mousevec, rv3d->viewinv[2]);
1825         }
1826
1827         if(delta < 0) {
1828                 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, 1.2f);
1829         }
1830         else {
1831                 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, .83333f);
1832         }
1833
1834         if(rv3d->viewlock & RV3D_BOXVIEW)
1835                 view3d_boxview_sync(sa, ar);
1836
1837         ED_view3d_depth_tag_update(rv3d);
1838         ED_region_tag_redraw(ar);
1839
1840         viewops_data_free(C, op);
1841
1842         return OPERATOR_FINISHED;
1843 }
1844
1845 /* copied from viewzoom_invoke(), changes here may apply there */
1846 static int viewdolly_invoke(bContext *C, wmOperator *op, wmEvent *event)
1847 {
1848         ViewOpsData *vod;
1849
1850         /* makes op->customdata */
1851         viewops_data_create(C, op, event);
1852         vod= op->customdata;
1853
1854         /* if one or the other zoom position aren't set, set from event */
1855         if (!RNA_property_is_set(op->ptr, "mx") || !RNA_property_is_set(op->ptr, "my")) {
1856                 RNA_int_set(op->ptr, "mx", event->x);
1857                 RNA_int_set(op->ptr, "my", event->y);
1858         }
1859
1860         if(RNA_property_is_set(op->ptr, "delta")) {
1861                 viewdolly_exec(C, op);
1862         }
1863         else {
1864                 /* overwrite the mouse vector with the view direction (zoom into the center) */
1865                 if((U.uiflag & USER_ZOOM_TO_MOUSEPOS) == 0) {
1866                         negate_v3_v3(vod->mousevec, vod->rv3d->viewinv[2]);
1867                         normalize_v3(vod->mousevec);
1868                 }
1869
1870                 if (event->type == MOUSEZOOM) {
1871                         /* Bypass Zoom invert flag for track pads (pass FALSE always) */
1872
1873                         if (U.uiflag & USER_ZOOM_HORIZ) {
1874                                 vod->origx = vod->oldx = event->x;
1875                                 viewdolly_apply(vod, event->prevx, event->prevy, FALSE);
1876                         }
1877                         else {
1878
1879                                 /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
1880                                 vod->origy = vod->oldy = vod->origy + event->x - event->prevx;
1881                                 viewdolly_apply(vod, event->prevx, event->prevy, FALSE);
1882                         }
1883                         ED_view3d_depth_tag_update(vod->rv3d);
1884
1885                         viewops_data_free(C, op);
1886                         return OPERATOR_FINISHED;
1887                 }
1888                 else {
1889                         /* add temp handler */
1890                         WM_event_add_modal_handler(C, op);
1891
1892                         return OPERATOR_RUNNING_MODAL;
1893                 }
1894         }
1895         return OPERATOR_FINISHED;
1896 }
1897
1898 /* like ED_operator_region_view3d_active but check its not in ortho view */
1899 static int viewdolly_poll(bContext *C)
1900 {
1901         RegionView3D *rv3d= CTX_wm_region_view3d(C);
1902
1903         if(rv3d) {
1904                 if (rv3d->persp == RV3D_PERSP) {
1905                         return 1;
1906                 }
1907                 else {
1908                         View3D *v3d= CTX_wm_view3d(C);
1909                         if (ED_view3d_camera_lock_check(v3d, rv3d)) {
1910                                 return 1;
1911                         }
1912                 }
1913         }
1914         return 0;
1915 }
1916
1917 static int viewdolly_cancel(bContext *C, wmOperator *op)
1918 {
1919         viewops_data_free(C, op);
1920
1921         return OPERATOR_CANCELLED;
1922 }
1923
1924 void VIEW3D_OT_dolly(wmOperatorType *ot)
1925 {
1926         /* identifiers */
1927         ot->name= "Dolly view";
1928         ot->description = "Dolly in/out in the view";
1929         ot->idname= "VIEW3D_OT_dolly";
1930
1931         /* api callbacks */
1932         ot->invoke= viewdolly_invoke;
1933         ot->exec= viewdolly_exec;
1934         ot->modal= viewdolly_modal;
1935         ot->poll= viewdolly_poll;
1936         ot->cancel= viewdolly_cancel;
1937
1938         /* flags */
1939         ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
1940
1941         RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
1942         RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
1943         RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
1944 }
1945
1946
1947
1948 static int view3d_all_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.4x */
1949 {
1950         ARegion *ar= CTX_wm_region(C);
1951         View3D *v3d = CTX_wm_view3d(C);
1952         RegionView3D *rv3d= CTX_wm_region_view3d(C);
1953         Scene *scene= CTX_data_scene(C);
1954         Base *base;
1955         float *curs;
1956         const short skip_camera= ED_view3d_camera_lock_check(v3d, rv3d);
1957
1958         int center= RNA_boolean_get(op->ptr, "center");
1959
1960         float size, min[3], max[3], afm[3];
1961         int ok= 1, onedone=0;
1962
1963         if(center) {
1964                 /* in 2.4x this also move the cursor to (0, 0, 0) (with shift+c). */
1965                 curs= give_cursor(scene, v3d);
1966                 zero_v3(min);
1967                 zero_v3(max);
1968                 zero_v3(curs);
1969         }
1970         else {
1971                 INIT_MINMAX(min, max);
1972         }
1973
1974         for(base= scene->base.first; base; base= base->next) {
1975                 if(BASE_VISIBLE(v3d, base)) {
1976                         onedone= 1;
1977
1978                         if(skip_camera && base->object == v3d->camera) {
1979                                 continue;
1980                         }
1981
1982                         minmax_object(base->object, min, max);
1983                 }
1984         }
1985         if(!onedone) {
1986                 ED_region_tag_redraw(ar);
1987                 /* TODO - should this be cancel?
1988                  * I think no, because we always move the cursor, with or without
1989                  * object, but in this case there is no change in the scene,
1990                  * only the cursor so I choice a ED_region_tag like
1991                  * smooth_view do for the center_cursor.
1992                  * See bug #22640
1993                  */
1994                 return OPERATOR_FINISHED;
1995         }
1996
1997         sub_v3_v3v3(afm, max, min);
1998         size= 0.7f*MAX3(afm[0], afm[1], afm[2]);
1999         if(size == 0.0f) ok= 0;
2000
2001         if(ok) {
2002                 float new_dist;
2003                 float new_ofs[3];
2004
2005                 new_dist = size;
2006                 new_ofs[0]= -(min[0]+max[0])/2.0f;
2007                 new_ofs[1]= -(min[1]+max[1])/2.0f;
2008                 new_ofs[2]= -(min[2]+max[2])/2.0f;
2009
2010                 // correction for window aspect ratio
2011                 if(ar->winy>2 && ar->winx>2) {
2012                         size= (float)ar->winx/(float)ar->winy;
2013                         if(size < 1.0f) size= 1.0f/size;
2014                         new_dist*= size;
2015                 }
2016
2017                 if ((rv3d->persp==RV3D_CAMOB) && !ED_view3d_camera_lock_check(v3d, rv3d)) {
2018                         rv3d->persp= RV3D_PERSP;
2019                         smooth_view(C, v3d, ar, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
2020                 }
2021                 else {
2022                         smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
2023                 }
2024         }
2025 // XXX  BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
2026
2027         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
2028
2029         return OPERATOR_FINISHED;
2030 }
2031
2032
2033 void VIEW3D_OT_view_all(wmOperatorType *ot)
2034 {
2035         /* identifiers */
2036         ot->name= "View All";
2037         ot->description = "View all objects in scene";
2038         ot->idname= "VIEW3D_OT_view_all";
2039
2040         /* api callbacks */
2041         ot->exec= view3d_all_exec;
2042         ot->poll= ED_operator_region_view3d_active;
2043
2044         /* flags */
2045         ot->flag= 0;
2046
2047         RNA_def_boolean(ot->srna, "center", 0, "Center", "");
2048 }
2049
2050
2051 static int viewselected_exec(bContext *C, wmOperator *UNUSED(op)) /* like a localview without local!, was centerview() in 2.4x */
2052 {
2053         ARegion *ar= CTX_wm_region(C);
2054         View3D *v3d = CTX_wm_view3d(C);
2055         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2056         Scene *scene= CTX_data_scene(C);
2057         Object *ob= OBACT;
2058         Object *obedit= CTX_data_edit_object(C);
2059         float size, min[3], max[3], afm[3];
2060         int ok=0, ok_dist=1;
2061         const short skip_camera= ED_view3d_camera_lock_check(v3d, rv3d);
2062
2063         /* SMOOTHVIEW */
2064         float new_ofs[3];
2065         float new_dist;
2066
2067         INIT_MINMAX(min, max);
2068
2069         if (ob && ob->mode & OB_MODE_WEIGHT_PAINT) {
2070                 /* hardcoded exception, we look for the one selected armature */
2071                 /* this is weak code this way, we should make a generic active/selection callback interface once... */
2072                 Base *base;
2073                 for(base=scene->base.first; base; base= base->next) {
2074                         if(TESTBASELIB(v3d, base)) {
2075                                 if(base->object->type==OB_ARMATURE)
2076                                         if(base->object->mode & OB_MODE_POSE)
2077                                                 break;
2078                         }
2079                 }
2080                 if(base)
2081                         ob= base->object;
2082         }
2083
2084
2085         if(obedit) {
2086                 ok = minmax_verts(obedit, min, max);    /* only selected */
2087         }
2088         else if(ob && (ob->mode & OB_MODE_POSE)) {
2089                 if(ob->pose) {
2090                         bArmature *arm= ob->data;
2091                         bPoseChannel *pchan;
2092                         float vec[3];
2093
2094                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2095                                 if(pchan->bone->flag & BONE_SELECTED) {
2096                                         if(pchan->bone->layer & arm->layer) {
2097                                                 bPoseChannel *pchan_tx= pchan->custom_tx ? pchan->custom_tx : pchan;
2098                                                 ok= 1;
2099                                                 mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_head);
2100                                                 DO_MINMAX(vec, min, max);
2101                                                 mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_tail);
2102                                                 DO_MINMAX(vec, min, max);
2103                                         }
2104                                 }
2105                         }
2106                 }
2107         }
2108         else if (paint_facesel_test(ob)) {
2109                 ok= paintface_minmax(ob, min, max);
2110         }
2111         else if (ob && (ob->mode & OB_MODE_PARTICLE_EDIT)) {
2112                 ok= PE_minmax(scene, min, max);
2113         }
2114         else {
2115                 Base *base;
2116                 for(base= FIRSTBASE; base; base = base->next) {
2117                         if(TESTBASE(v3d, base))  {
2118
2119                                 if(skip_camera && base->object == v3d->camera) {
2120                                         continue;
2121                                 }
2122
2123                                 /* account for duplis */
2124                                 if (minmax_object_duplis(scene, base->object, min, max)==0)
2125                                         minmax_object(base->object, min, max); /* use if duplis not found */
2126
2127                                 ok= 1;
2128                         }
2129                 }
2130         }
2131
2132         if(ok==0) return OPERATOR_FINISHED;
2133
2134         sub_v3_v3v3(afm, max, min);
2135         size= MAX3(afm[0], afm[1], afm[2]);
2136
2137         if(!rv3d->is_persp) {
2138                 if(size < 0.0001f) { /* if its a sinble point. dont even re-scale */
2139                         ok_dist= 0;
2140                 }
2141                 else {
2142                         /* perspective should be a bit farther away to look nice */
2143                         size*= 0.7f;
2144                 }
2145         }
2146         else {
2147                 if(size <= v3d->near*1.5f) {
2148                         size= v3d->near*1.5f;
2149                 }
2150         }
2151
2152         add_v3_v3v3(new_ofs, min, max);
2153         mul_v3_fl(new_ofs, -0.5f);
2154
2155         new_dist = size;
2156
2157         /* correction for window aspect ratio */
2158         if(ar->winy>2 && ar->winx>2) {
2159                 size= (float)ar->winx/(float)ar->winy;
2160                 if(size<1.0f) size= 1.0f/size;
2161                 new_dist*= size;
2162         }
2163
2164         if (rv3d->persp==RV3D_CAMOB && !ED_view3d_camera_lock_check(v3d, rv3d)) {
2165                 rv3d->persp= RV3D_PERSP;
2166                 smooth_view(C, v3d, ar, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
2167         }
2168         else {
2169                 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, ok_dist ? &new_dist : NULL, NULL);
2170         }
2171
2172         /* smooth view does viewlock RV3D_BOXVIEW copy */
2173         
2174 // XXX  BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
2175
2176         return OPERATOR_FINISHED;
2177 }
2178
2179 void VIEW3D_OT_view_selected(wmOperatorType *ot)
2180 {
2181
2182         /* identifiers */
2183         ot->name= "View Selected";
2184         ot->description = "Move the view to the selection center";
2185         ot->idname= "VIEW3D_OT_view_selected";
2186
2187         /* api callbacks */
2188         ot->exec= viewselected_exec;
2189         ot->poll= ED_operator_region_view3d_active;
2190
2191         /* flags */
2192         ot->flag= 0;
2193 }
2194
2195 static int viewcenter_cursor_exec(bContext *C, wmOperator *UNUSED(op))
2196 {
2197         View3D *v3d = CTX_wm_view3d(C);
2198         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2199         Scene *scene= CTX_data_scene(C);
2200         
2201         if (rv3d) {
2202                 ARegion *ar= CTX_wm_region(C);
2203
2204                 /* non camera center */
2205                 float new_ofs[3];
2206                 negate_v3_v3(new_ofs, give_cursor(scene, v3d));
2207                 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, NULL, NULL);
2208                 
2209                 /* smooth view does viewlock RV3D_BOXVIEW copy */
2210         }
2211         
2212         return OPERATOR_FINISHED;
2213 }
2214
2215 void VIEW3D_OT_view_center_cursor(wmOperatorType *ot)
2216 {
2217         /* identifiers */
2218         ot->name= "Center View to Cursor";
2219         ot->description= "Centers the view so that the cursor is in the middle of the view";
2220         ot->idname= "VIEW3D_OT_view_center_cursor";
2221         
2222         /* api callbacks */
2223         ot->exec= viewcenter_cursor_exec;
2224         ot->poll= ED_operator_view3d_active;
2225         
2226         /* flags */
2227         ot->flag= 0;
2228 }
2229
2230 static int view3d_center_camera_exec(bContext *C, wmOperator *UNUSED(op)) /* was view3d_home() in 2.4x */
2231 {
2232         ARegion *ar= CTX_wm_region(C);
2233         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2234         Scene *scene= CTX_data_scene(C);
2235         float xfac, yfac;
2236         float size[2];
2237
2238         rv3d->camdx= rv3d->camdy= 0.0f;
2239
2240         view3d_viewborder_size_get(scene, ar, size);
2241
2242         /* 4px is just a little room from the edge of the area */
2243         xfac= (float)ar->winx / (float)(size[0] + 4);
2244         yfac= (float)ar->winy / (float)(size[1] + 4);
2245
2246         rv3d->camzoom= BKE_screen_view3d_zoom_from_fac(MIN2(xfac, yfac));
2247         CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2248
2249         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2250
2251         return OPERATOR_FINISHED;
2252 }
2253
2254 void VIEW3D_OT_view_center_camera(wmOperatorType *ot)
2255 {
2256         /* identifiers */
2257         ot->name= "View Camera Center";
2258         ot->description = "Center the camera view";
2259         ot->idname= "VIEW3D_OT_view_center_camera";
2260
2261         /* api callbacks */
2262         ot->exec= view3d_center_camera_exec;
2263         ot->poll= view3d_camera_active_poll;
2264
2265         /* flags */
2266         ot->flag= 0;
2267 }
2268
2269 /* ********************* Set render border operator ****************** */
2270
2271 static int render_border_exec(bContext *C, wmOperator *op)
2272 {
2273         View3D *v3d = CTX_wm_view3d(C);
2274         ARegion *ar= CTX_wm_region(C);
2275         RegionView3D *rv3d= ED_view3d_context_rv3d(C);
2276         Scene *scene= CTX_data_scene(C);
2277
2278         rcti rect;
2279         rctf vb;
2280
2281         /* get border select values using rna */
2282         rect.xmin= RNA_int_get(op->ptr, "xmin");
2283         rect.ymin= RNA_int_get(op->ptr, "ymin");
2284         rect.xmax= RNA_int_get(op->ptr, "xmax");
2285         rect.ymax= RNA_int_get(op->ptr, "ymax");
2286
2287         /* calculate range */
2288         ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &vb, FALSE);
2289
2290         scene->r.border.xmin= ((float)rect.xmin-vb.xmin)/(vb.xmax-vb.xmin);
2291         scene->r.border.ymin= ((float)rect.ymin-vb.ymin)/(vb.ymax-vb.ymin);
2292         scene->r.border.xmax= ((float)rect.xmax-vb.xmin)/(vb.xmax-vb.xmin);
2293         scene->r.border.ymax= ((float)rect.ymax-vb.ymin)/(vb.ymax-vb.ymin);
2294
2295         /* actually set border */
2296         CLAMP(scene->r.border.xmin, 0.0f, 1.0f);
2297         CLAMP(scene->r.border.ymin, 0.0f, 1.0f);
2298         CLAMP(scene->r.border.xmax, 0.0f, 1.0f);
2299         CLAMP(scene->r.border.ymax, 0.0f, 1.0f);
2300
2301         /* drawing a border surrounding the entire camera view switches off border rendering
2302          * or the border covers no pixels */
2303         if ((scene->r.border.xmin <= 0.0f && scene->r.border.xmax >= 1.0f &&
2304                 scene->r.border.ymin <= 0.0f && scene->r.border.ymax >= 1.0f) ||
2305            (scene->r.border.xmin == scene->r.border.xmax ||
2306                 scene->r.border.ymin == scene->r.border.ymax ))
2307         {
2308                 scene->r.mode &= ~R_BORDER;
2309         } else {
2310                 scene->r.mode |= R_BORDER;
2311         }
2312         
2313         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, NULL);
2314
2315         return OPERATOR_FINISHED;
2316
2317 }
2318
2319 void VIEW3D_OT_render_border(wmOperatorType *ot)
2320 {
2321         /* identifiers */
2322         ot->name= "Set Render Border";
2323         ot->description = "Set the boundaries of the border render and enables border render ";
2324         ot->idname= "VIEW3D_OT_render_border";
2325
2326         /* api callbacks */
2327         ot->invoke= WM_border_select_invoke;
2328         ot->exec= render_border_exec;
2329         ot->modal= WM_border_select_modal;
2330         ot->cancel= WM_border_select_cancel;
2331
2332         ot->poll= view3d_camera_active_poll;
2333
2334         /* flags */
2335         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2336
2337         /* rna */
2338         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
2339         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
2340         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
2341         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
2342
2343 }
2344 /* ********************* Border Zoom operator ****************** */
2345
2346 static int view3d_zoom_border_exec(bContext *C, wmOperator *op)
2347 {
2348         ARegion *ar= CTX_wm_region(C);
2349         View3D *v3d = CTX_wm_view3d(C);
2350         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2351         Scene *scene= CTX_data_scene(C);
2352
2353         /* Zooms in on a border drawn by the user */
2354         rcti rect;
2355         float dvec[3], vb[2], xscale, yscale, scale;
2356
2357         /* SMOOTHVIEW */
2358         float new_dist;
2359         float new_ofs[3];
2360
2361         /* ZBuffer depth vars */
2362         bglMats mats;
2363         float depth_close= FLT_MAX;
2364         double cent[2],  p[3];
2365
2366         /* note; otherwise opengl won't work */
2367         view3d_operator_needs_opengl(C);
2368
2369         /* get border select values using rna */
2370         rect.xmin= RNA_int_get(op->ptr, "xmin");
2371         rect.ymin= RNA_int_get(op->ptr, "ymin");
2372         rect.xmax= RNA_int_get(op->ptr, "xmax");
2373         rect.ymax= RNA_int_get(op->ptr, "ymax");
2374
2375         /* Get Z Depths, needed for perspective, nice for ortho */
2376         bgl_get_mats(&mats);
2377         draw_depth(scene, ar, v3d, NULL);
2378         
2379         {
2380                 /* avoid allocating the whole depth buffer */
2381                 ViewDepths depth_temp= {0};
2382
2383                 /* avoid view3d_update_depths() for speed. */
2384                 view3d_update_depths_rect(ar, &depth_temp, &rect);
2385         
2386                 /* find the closest Z pixel */
2387                 depth_close= view3d_depth_near(&depth_temp);
2388         
2389                 MEM_freeN(depth_temp.depths);
2390         }
2391
2392         cent[0] = (((double)rect.xmin)+((double)rect.xmax)) / 2;
2393         cent[1] = (((double)rect.ymin)+((double)rect.ymax)) / 2;
2394
2395         if (rv3d->is_persp) {
2396                 double p_corner[3];
2397
2398                 /* no depths to use, we cant do anything! */
2399                 if (depth_close==FLT_MAX){
2400                         BKE_report(op->reports, RPT_ERROR, "Depth Too Large");
2401                         return OPERATOR_CANCELLED;
2402                 }
2403                 /* convert border to 3d coordinates */
2404                 if ((   !gluUnProject(cent[0], cent[1], depth_close, mats.modelview, mats.projection, (GLint *)mats.viewport, &p[0], &p[1], &p[2])) ||
2405                         (       !gluUnProject((double)rect.xmin, (double)rect.ymin, depth_close, mats.modelview, mats.projection, (GLint *)mats.viewport, &p_corner[0], &p_corner[1], &p_corner[2])))
2406                         return OPERATOR_CANCELLED;
2407
2408                 dvec[0] = p[0]-p_corner[0];
2409                 dvec[1] = p[1]-p_corner[1];
2410                 dvec[2] = p[2]-p_corner[2];
2411
2412                 new_dist = len_v3(dvec);
2413                 if(new_dist <= v3d->near * 1.5f) new_dist= v3d->near * 1.5f;
2414
2415                 new_ofs[0] = -p[0];
2416                 new_ofs[1] = -p[1];
2417                 new_ofs[2] = -p[2];
2418
2419         } else { /* othographic */
2420                 /* find the current window width and height */
2421                 vb[0] = ar->winx;
2422                 vb[1] = ar->winy;
2423
2424                 new_dist = rv3d->dist;
2425
2426                 /* convert the drawn rectangle into 3d space */
2427                 if (depth_close!=FLT_MAX && gluUnProject(cent[0], cent[1], depth_close, mats.modelview, mats.projection, (GLint *)mats.viewport, &p[0], &p[1], &p[2])) {
2428                         new_ofs[0] = -p[0];
2429                         new_ofs[1] = -p[1];
2430                         new_ofs[2] = -p[2];
2431                 }
2432                 else {
2433                         float mval_f[2];
2434                         /* We cant use the depth, fallback to the old way that dosnt set the center depth */
2435                         copy_v3_v3(new_ofs, rv3d->ofs);
2436
2437                         initgrabz(rv3d, -new_ofs[0], -new_ofs[1], -new_ofs[2]);
2438
2439                         mval_f[0]= (rect.xmin + rect.xmax - vb[0]) / 2.0f;
2440                         mval_f[1]= (rect.ymin + rect.ymax - vb[1]) / 2.0f;
2441                         ED_view3d_win_to_delta(ar, mval_f, dvec);
2442                         /* center the view to the center of the rectangle */
2443                         sub_v3_v3(new_ofs, dvec);
2444                 }
2445
2446                 /* work out the ratios, so that everything selected fits when we zoom */
2447                 xscale = ((rect.xmax-rect.xmin)/vb[0]);
2448                 yscale = ((rect.ymax-rect.ymin)/vb[1]);
2449                 scale = (xscale >= yscale)?xscale:yscale;
2450
2451                 /* zoom in as required, or as far as we can go */
2452                 new_dist = ((new_dist*scale) >= 0.001f * v3d->grid)? new_dist*scale:0.001f * v3d->grid;
2453         }
2454
2455         smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
2456
2457         if(rv3d->viewlock & RV3D_BOXVIEW)
2458                 view3d_boxview_sync(CTX_wm_area(C), ar);
2459
2460         return OPERATOR_FINISHED;
2461 }
2462
2463 static int view3d_zoom_border_invoke(bContext *C, wmOperator *op, wmEvent *event)
2464 {
2465         View3D *v3d= CTX_wm_view3d(C);
2466         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2467
2468         /* if in camera view do not exec the operator so we do not conflict with set render border*/
2469         if ((rv3d->persp != RV3D_CAMOB) || ED_view3d_camera_lock_check(v3d, rv3d))
2470                 return WM_border_select_invoke(C, op, event);
2471         else
2472                 return OPERATOR_PASS_THROUGH;
2473 }
2474
2475 void VIEW3D_OT_zoom_border(wmOperatorType *ot)
2476 {
2477         /* identifiers */
2478         ot->name= "Border Zoom";
2479         ot->description = "Zoom in the view to the nearest object contained in the border";
2480         ot->idname= "VIEW3D_OT_zoom_border";
2481
2482         /* api callbacks */
2483         ot->invoke= view3d_zoom_border_invoke;
2484         ot->exec= view3d_zoom_border_exec;
2485         ot->modal= WM_border_select_modal;
2486         ot->cancel= WM_border_select_cancel;
2487
2488         ot->poll= ED_operator_region_view3d_active;
2489
2490         /* flags */
2491         ot->flag= 0;
2492
2493         /* rna */
2494         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
2495         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
2496         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
2497         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
2498
2499 }
2500
2501 /* sets the view to 1:1 camera/render-pixel */
2502 static void view3d_set_1_to_1_viewborder(Scene *scene, ARegion *ar)
2503 {
2504         RegionView3D *rv3d= ar->regiondata;
2505         float size[2];
2506         int im_width= (scene->r.size*scene->r.xsch)/100;
2507         
2508         view3d_viewborder_size_get(scene, ar, size);
2509
2510         rv3d->camzoom= BKE_screen_view3d_zoom_from_fac((float)im_width/size[0]);
2511         CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2512 }
2513
2514 static int view3d_zoom_1_to_1_camera_exec(bContext *C, wmOperator *UNUSED(op))
2515 {
2516         Scene *scene= CTX_data_scene(C);
2517         ARegion *ar= CTX_wm_region(C);
2518
2519         view3d_set_1_to_1_viewborder(scene, ar);
2520
2521         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2522
2523         return OPERATOR_FINISHED;
2524 }
2525
2526 void VIEW3D_OT_zoom_camera_1_to_1(wmOperatorType *ot)
2527 {
2528         /* identifiers */
2529         ot->name= "Zoom Camera 1:1";
2530         ot->description = "Match the camera to 1:1 to the render output";
2531         ot->idname= "VIEW3D_OT_zoom_camera_1_to_1";
2532
2533         /* api callbacks */
2534         ot->exec= view3d_zoom_1_to_1_camera_exec;
2535         ot->poll= view3d_camera_active_poll;
2536
2537         /* flags */
2538         ot->flag= 0;
2539 }
2540
2541 /* ********************* Changing view operator ****************** */
2542
2543 static EnumPropertyItem prop_view_items[] = {
2544         {RV3D_VIEW_FRONT, "FRONT", 0, "Front", "View From the Front"},
2545         {RV3D_VIEW_BACK, "BACK", 0, "Back", "View From the Back"},
2546         {RV3D_VIEW_LEFT, "LEFT", 0, "Left", "View From the Left"},
2547         {RV3D_VIEW_RIGHT, "RIGHT", 0, "Right", "View From the Right"},
2548         {RV3D_VIEW_TOP, "TOP", 0, "Top", "View From the Top"},
2549         {RV3D_VIEW_BOTTOM, "BOTTOM", 0, "Bottom", "View From the Bottom"},
2550         {RV3D_VIEW_CAMERA, "CAMERA", 0, "Camera", "View From the active amera"},
2551         {0, NULL, 0, NULL, NULL}};
2552
2553
2554 /* would like to make this a generic function - outside of transform */
2555
2556 static void axis_set_view(bContext *C, View3D *v3d, ARegion *ar, float q1, float q2, float q3, float q4, short view, int perspo, int align_active)
2557 {
2558         RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2559         float new_quat[4];
2560
2561         new_quat[0]= q1; new_quat[1]= q2;
2562         new_quat[2]= q3; new_quat[3]= q4;
2563         normalize_qt(new_quat);
2564
2565         if(align_active) {
2566                 /* align to active object */
2567                 Object *obact= CTX_data_active_object(C);
2568                 if (obact==NULL) {
2569                         /* no active object, ignore this option */
2570                         align_active= FALSE;
2571                 }
2572                 else {
2573                         float obact_quat[4];
2574                         float twmat[3][3];
2575
2576                         /* same as transform manipulator when normal is set */
2577                         ED_getTransformOrientationMatrix(C, twmat, FALSE);
2578
2579                         mat3_to_quat( obact_quat,twmat);
2580                         invert_qt(obact_quat);
2581                         mul_qt_qtqt(new_quat, new_quat, obact_quat);
2582
2583                         rv3d->view= view= RV3D_VIEW_USER;
2584                 }
2585         }
2586
2587         if(align_active==FALSE) {
2588                 /* normal operation */
2589                 if(rv3d->viewlock) {
2590                         /* only pass on if */
2591                         if(rv3d->view==RV3D_VIEW_FRONT && view==RV3D_VIEW_BACK);
2592                         else if(rv3d->view==RV3D_VIEW_BACK && view==RV3D_VIEW_FRONT);
2593                         else if(rv3d->view==RV3D_VIEW_RIGHT && view==RV3D_VIEW_LEFT);
2594                         else if(rv3d->view==RV3D_VIEW_LEFT && view==RV3D_VIEW_RIGHT);
2595                         else if(rv3d->view==RV3D_VIEW_BOTTOM && view==RV3D_VIEW_TOP);
2596                         else if(rv3d->view==RV3D_VIEW_TOP && view==RV3D_VIEW_BOTTOM);
2597                         else return;
2598                 }
2599
2600                 rv3d->view= view;
2601         }
2602
2603         if(rv3d->viewlock) {
2604                 ED_region_tag_redraw(ar);
2605                 return;
2606         }
2607
2608         if (rv3d->persp==RV3D_CAMOB && v3d->camera) {
2609
2610                 if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
2611                 else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
2612
2613                 smooth_view(C, v3d, ar, v3d->camera, NULL, rv3d->ofs, new_quat, NULL, NULL);
2614         }
2615         else {
2616
2617                 if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
2618                 else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
2619
2620                 smooth_view(C, v3d, ar, NULL, NULL, NULL, new_quat, NULL, NULL);
2621         }
2622
2623 }
2624
2625 static int viewnumpad_exec(bContext *C, wmOperator *op)
2626 {
2627         View3D *v3d = CTX_wm_view3d(C);
2628         ARegion *ar= ED_view3d_context_region_unlock(C);
2629         RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2630         Scene *scene= CTX_data_scene(C);
2631         static int perspo=RV3D_PERSP;
2632         int viewnum, align_active, nextperspo;
2633
2634         viewnum = RNA_enum_get(op->ptr, "type");
2635         align_active = RNA_boolean_get(op->ptr, "align_active");
2636
2637         /* set this to zero, gets handled in axis_set_view */
2638         if(rv3d->viewlock)
2639                 align_active= 0;
2640
2641         /* Use this to test if we started out with a camera */
2642
2643         if (rv3d->persp == RV3D_CAMOB) {
2644                 nextperspo= rv3d->lpersp;
2645         } else {
2646                 nextperspo= perspo;
2647         }
2648
2649         switch (viewnum) {
2650                 case RV3D_VIEW_BOTTOM :
2651                         axis_set_view(C, v3d, ar, 0.0, -1.0, 0.0, 0.0, viewnum, nextperspo, align_active);
2652                         break;
2653
2654                 case RV3D_VIEW_BACK:
2655                         axis_set_view(C, v3d, ar, 0.0, 0.0, (float)-cos(M_PI/4.0), (float)-cos(M_PI/4.0), viewnum, nextperspo, align_active);
2656                         break;
2657
2658                 case RV3D_VIEW_LEFT:
2659                         axis_set_view(C, v3d, ar, 0.5, -0.5, 0.5, 0.5, viewnum, nextperspo, align_active);
2660                         break;
2661
2662                 case RV3D_VIEW_TOP:
2663                         axis_set_view(C, v3d, ar, 1.0, 0.0, 0.0, 0.0, viewnum, nextperspo, align_active);
2664                         break;
2665
2666                 case RV3D_VIEW_FRONT:
2667                         axis_set_view(C, v3d, ar, (float)cos(M_PI/4.0), (float)-sin(M_PI/4.0), 0.0, 0.0, viewnum, nextperspo, align_active);
2668                         break;
2669
2670                 case RV3D_VIEW_RIGHT:
2671                         axis_set_view(C, v3d, ar, 0.5, -0.5, -0.5, -0.5, viewnum, nextperspo, align_active);
2672                         break;
2673
2674                 case RV3D_VIEW_CAMERA:
2675                         if(rv3d->viewlock==0) {
2676                                 /* lastview -  */
2677
2678                                 if(rv3d->persp != RV3D_CAMOB) {
2679                                         Object *ob= OBACT;
2680
2681                                         if (!rv3d->smooth_timer) {
2682                                                 /* store settings of current view before allowing overwriting with camera view
2683                                                  * only if we're not currently in a view transition */
2684                                                 copy_qt_qt(rv3d->lviewquat, rv3d->viewquat);
2685                                                 rv3d->lview= rv3d->view;
2686                                                 rv3d->lpersp= rv3d->persp;
2687                                         }
2688
2689         #if 0
2690                                         if(G.qual==LR_ALTKEY) {
2691                                                 if(oldcamera && is_an_active_object(oldcamera)) {
2692                                                         v3d->camera= oldcamera;
2693                                                 }
2694                                                 handle_view3d_lock();
2695                                         }
2696         #endif
2697                                         
2698                                         /* first get the default camera for the view lock type */
2699                                         if(v3d->scenelock) {
2700                                                 /* sets the camera view if available */
2701                                                 v3d->camera= scene->camera;                                             
2702                                         }
2703                                         else {
2704                                                 /* use scene camera if one is not set (even though we're unlocked) */
2705                                                 if(v3d->camera==NULL) {
2706                                                         v3d->camera= scene->camera;
2707                                                 }
2708                                         }
2709
2710                                         /* if the camera isnt found, check a number of options */
2711                                         if(v3d->camera==NULL && ob && ob->type==OB_CAMERA)
2712                                                 v3d->camera= ob;
2713                                         
2714                                         if(v3d->camera==NULL)
2715                                                 v3d->camera= scene_find_camera(scene);          
2716
2717                                         /* couldnt find any useful camera, bail out */
2718                                         if(v3d->camera==NULL)
2719                                                 return OPERATOR_CANCELLED;
2720                                         
2721                                         /* important these dont get out of sync for locked scenes */
2722                                         if(v3d->scenelock)
2723                                                 scene->camera= v3d->camera;
2724
2725                                         /* finally do snazzy view zooming */
2726                                         rv3d->persp= RV3D_CAMOB;
2727                                         smooth_view(C, v3d, ar, NULL, v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, &v3d->lens);
2728
2729                                 }
2730                                 else{
2731                                         /* return to settings of last view */
2732                                         /* does smooth_view too */
2733                                         axis_set_view(C, v3d, ar, rv3d->lviewquat[0], rv3d->lviewquat[1], rv3d->lviewquat[2], rv3d->lviewquat[3], rv3d->lview, rv3d->lpersp, 0);
2734                                 }
2735                         }
2736                         break;
2737
2738                 default :
2739                         break;
2740         }
2741
2742         if(rv3d->persp != RV3D_CAMOB) perspo= rv3d->persp;
2743
2744         return OPERATOR_FINISHED;
2745 }
2746
2747
2748 void VIEW3D_OT_viewnumpad(wmOperatorType *ot)
2749 {
2750         /* identifiers */
2751         ot->name= "View numpad";
2752         ot->description = "Set the view";
2753         ot->idname= "VIEW3D_OT_viewnumpad";
2754
2755         /* api callbacks */
2756         ot->exec= viewnumpad_exec;
2757         ot->poll= ED_operator_rv3d_unlock_poll;
2758
2759         /* flags */
2760         ot->flag= 0;
2761
2762         RNA_def_enum(ot->srna, "type", prop_view_items, 0, "View", "The Type of view");
2763         RNA_def_boolean(ot->srna, "align_active", 0, "Align Active", "Align to the active objects axis");
2764 }
2765
2766 static EnumPropertyItem prop_view_orbit_items[] = {
2767         {V3D_VIEW_STEPLEFT, "ORBITLEFT", 0, "Orbit Left", "Orbit the view around to the Left"},
2768         {V3D_VIEW_STEPRIGHT, "ORBITRIGHT", 0, "Orbit Right", "Orbit the view around to the Right"},
2769         {V3D_VIEW_STEPUP, "ORBITUP", 0, "Orbit Up", "Orbit the view Up"},
2770         {V3D_VIEW_STEPDOWN, "ORBITDOWN", 0, "Orbit Down", "Orbit the view Down"},
2771         {0, NULL, 0, NULL, NULL}};
2772
2773 static int vieworbit_exec(bContext *C, wmOperator *op)
2774 {
2775         View3D *v3d= CTX_wm_view3d(C);
2776         ARegion *ar= ED_view3d_context_region_unlock(C);
2777         RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2778         float phi, q1[4], new_quat[4];
2779         int orbitdir;
2780
2781         orbitdir = RNA_enum_get(op->ptr, "type");
2782
2783         if(rv3d->viewlock==0) {
2784                 if((rv3d->persp != RV3D_CAMOB) || ED_view3d_camera_lock_check(v3d, rv3d)) {
2785                         if(orbitdir == V3D_VIEW_STEPLEFT || orbitdir == V3D_VIEW_STEPRIGHT) {
2786                                 float si;
2787                                 /* z-axis */
2788                                 phi= (float)(M_PI/360.0)*U.pad_rot_angle;
2789                                 if(orbitdir == V3D_VIEW_STEPRIGHT) phi= -phi;
2790                                 si= (float)sin(phi);
2791                                 q1[0]= (float)cos(phi);
2792                                 q1[1]= q1[2]= 0.0;
2793                                 q1[3]= si;
2794                                 mul_qt_qtqt(new_quat, rv3d->viewquat, q1);
2795                                 rv3d->view= RV3D_VIEW_USER;
2796                         }
2797                         else if(orbitdir == V3D_VIEW_STEPDOWN || orbitdir == V3D_VIEW_STEPUP) {
2798                                 /* horizontal axis */
2799                                 copy_v3_v3(q1+1, rv3d->viewinv[0]);
2800
2801                                 normalize_v3(q1+1);
2802                                 phi= (float)(M_PI/360.0)*U.pad_rot_angle;
2803                                 if(orbitdir == V3D_VIEW_STEPDOWN) phi= -phi;
2804                                 q1[0]= (float)cos(phi);
2805                                 mul_v3_fl(q1+1, sin(phi));
2806                                 mul_qt_qtqt(new_quat, rv3d->viewquat, q1);
2807                                 rv3d->view= RV3D_VIEW_USER;
2808                         }
2809
2810                         smooth_view(C, CTX_wm_view3d(C), ar, NULL, NULL, NULL, new_quat, NULL, NULL);
2811                 }
2812         }
2813
2814         return OPERATOR_FINISHED;
2815 }
2816
2817 void VIEW3D_OT_view_orbit(wmOperatorType *ot)
2818 {
2819         /* identifiers */
2820         ot->name= "View Orbit";
2821         ot->description = "Orbit the view";
2822         ot->idname= "VIEW3D_OT_view_orbit";
2823
2824         /* api callbacks */
2825         ot->exec= vieworbit_exec;
2826         ot->poll= ED_operator_rv3d_unlock_poll;
2827
2828         /* flags */
2829         ot->flag= 0;
2830         RNA_def_enum(ot->srna, "type", prop_view_orbit_items, 0, "Orbit", "Direction of View Orbit");
2831 }
2832
2833 static EnumPropertyItem prop_view_pan_items[] = {
2834         {V3D_VIEW_PANLEFT, "PANLEFT", 0, "Pan Left", "Pan the view to the Left"},
2835         {V3D_VIEW_PANRIGHT, "PANRIGHT", 0, "Pan Right", "Pan the view to the Right"},
2836         {V3D_VIEW_PANUP, "PANUP", 0, "Pan Up", "Pan the view Up"},
2837         {V3D_VIEW_PANDOWN, "PANDOWN", 0, "Pan Down", "Pan the view Down"},
2838         {0, NULL, 0, NULL, NULL}};
2839
2840 static int viewpan_exec(bContext *C, wmOperator *op)
2841 {
2842         ARegion *ar= CTX_wm_region(C);
2843         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2844         float vec[3];
2845         float mval_f[2]= {0.0f, 0.0f};
2846         int pandir;
2847
2848         pandir = RNA_enum_get(op->ptr, "type");
2849
2850         initgrabz(rv3d, 0.0, 0.0, 0.0);
2851         if(pandir == V3D_VIEW_PANRIGHT)         { mval_f[0]= -32.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2852         else if(pandir == V3D_VIEW_PANLEFT)     { mval_f[0]=  32.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2853         else if(pandir == V3D_VIEW_PANUP)       { mval_f[1]= -25.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2854         else if(pandir == V3D_VIEW_PANDOWN)     { mval_f[1]=  25.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2855         add_v3_v3(rv3d->ofs, vec);
2856
2857         if(rv3d->viewlock & RV3D_BOXVIEW)
2858                 view3d_boxview_sync(CTX_wm_area(C), ar);
2859
2860         ED_region_tag_redraw(ar);
2861
2862         return OPERATOR_FINISHED;
2863 }
2864
2865 void VIEW3D_OT_view_pan(wmOperatorType *ot)
2866 {
2867         /* identifiers */
2868         ot->name= "View Pan";
2869         ot->description = "Pan the view";
2870         ot->idname= "VIEW3D_OT_view_pan";
2871
2872         /* api callbacks */
2873         ot->exec= viewpan_exec;
2874         ot->poll= ED_operator_region_view3d_active;
2875
2876         /* flags */
2877         ot->flag= 0;
2878         RNA_def_enum(ot->srna, "type", prop_view_pan_items, 0, "Pan", "Direction of View Pan");
2879 }
2880
2881 static int viewpersportho_exec(bContext *C, wmOperator *UNUSED(op))
2882 {
2883         ARegion *ar= ED_view3d_context_region_unlock(C);
2884         RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2885
2886         if(rv3d->viewlock==0) {
2887                 if(rv3d->persp!=RV3D_ORTHO)
2888                         rv3d->persp=RV3D_ORTHO;
2889                 else rv3d->persp=RV3D_PERSP;
2890                 ED_region_tag_redraw(ar);
2891         }
2892
2893         return OPERATOR_FINISHED;
2894
2895 }
2896
2897 void VIEW3D_OT_view_persportho(wmOperatorType *ot)
2898 {
2899         /* identifiers */
2900         ot->name= "View Persp/Ortho";
2901         ot->description = "Switch the current view from perspective/orthographic";
2902         ot->idname= "VIEW3D_OT_view_persportho";
2903
2904         /* api callbacks */
2905         ot->exec= viewpersportho_exec;
2906         ot->poll= ED_operator_rv3d_unlock_poll;
2907
2908         /* flags */
2909         ot->flag= 0;
2910 }
2911
2912
2913 /* ******************** add background image operator **************** */
2914
2915 static BGpic *background_image_add(bContext *C)
2916 {
2917         View3D *v3d= CTX_wm_view3d(C);
2918         
2919         BGpic *bgpic= MEM_callocN(sizeof(BGpic), "Background Image");
2920         bgpic->size= 5.0;
2921         bgpic->blend= 0.5;
2922         bgpic->iuser.fie_ima= 2;
2923         bgpic->iuser.ok= 1;
2924         bgpic->view= 0; /* 0 for all */
2925         
2926         BLI_addtail(&v3d->bgpicbase, bgpic);
2927         
2928         return bgpic;
2929 }
2930
2931 static int background_image_add_exec(bContext *C, wmOperator *UNUSED(op))
2932 {
2933         background_image_add(C);
2934
2935         return OPERATOR_FINISHED;
2936 }
2937
2938 static int background_image_add_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2939 {
2940         View3D *v3d= CTX_wm_view3d(C);
2941         Image *ima= NULL;
2942         BGpic *bgpic;
2943         char name[32];
2944         
2945         /* check input variables */
2946         if(RNA_property_is_set(op->ptr, "filepath")) {
2947                 char path[FILE_MAX];
2948                 
2949                 RNA_string_get(op->ptr, "filepath", path);
2950                 ima= BKE_add_image_file(path);
2951         }
2952         else if(RNA_property_is_set(op->ptr, "name")) {
2953                 RNA_string_get(op->ptr, "name", name);
2954                 ima= (Image *)find_id("IM", name);
2955         }
2956         
2957         bgpic = background_image_add(C);
2958         
2959         if (ima) {
2960                 bgpic->ima = ima;
2961                 
2962                 if(ima->id.us==0) id_us_plus(&ima->id);
2963                 else id_lib_extern(&ima->id);
2964                 
2965                 if (!(v3d->flag & V3D_DISPBGPICS))
2966                         v3d->flag |= V3D_DISPBGPICS;
2967         }
2968         
2969         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
2970         
2971         return OPERATOR_FINISHED;
2972 }
2973
2974 void VIEW3D_OT_background_image_add(wmOperatorType *ot)
2975 {
2976         /* identifiers */
2977         ot->name   = "Add Background Image";
2978         ot->description= "Add a new background image";
2979         ot->idname = "VIEW3D_OT_background_image_add";
2980
2981         /* api callbacks */
2982         ot->invoke = background_image_add_invoke;
2983         ot->exec   = background_image_add_exec;
2984         ot->poll   = ED_operator_view3d_active;
2985
2986         /* flags */
2987         ot->flag   = 0;
2988         
2989         /* properties */
2990         RNA_def_string(ot->srna, "name", "Image", 24, "Name", "Image name to assign.");
2991         RNA_def_string(ot->srna, "filepath", "Path", FILE_MAX, "Filepath", "Path to image file");
2992 }
2993
2994
2995 /* ***** remove image operator ******* */
2996 static int background_image_remove_exec(bContext *C, wmOperator *op)
2997 {
2998         View3D *vd = CTX_wm_view3d(C);
2999         int index = RNA_int_get(op->ptr, "index");
3000         BGpic *bgpic_rem= BLI_findlink(&vd->bgpicbase, index);
3001
3002         if(bgpic_rem) {
3003                 BLI_remlink(&vd->bgpicbase, bgpic_rem);
3004                 if(bgpic_rem->ima) bgpic_rem->ima->id.us--;
3005                 MEM_freeN(bgpic_rem);
3006                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, vd);
3007                 return OPERATOR_FINISHED;
3008         }
3009         else {
3010                 return OPERATOR_CANCELLED;
3011         }
3012
3013 }
3014
3015 void VIEW3D_OT_background_image_remove(wmOperatorType *ot)
3016 {
3017         /* identifiers */
3018         ot->name   = "Remove Background Image";
3019         ot->description= "Remove a background image from the 3D view";
3020         ot->idname = "VIEW3D_OT_background_image_remove";
3021
3022         /* api callbacks */
3023         ot->exec   = background_image_remove_exec;
3024         ot->poll   = ED_operator_view3d_active;
3025
3026         /* flags */
3027         ot->flag   = 0;
3028
3029         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Background image index to remove ", 0, INT_MAX);
3030 }
3031
3032 /* ********************* set clipping operator ****************** */
3033
3034 static void calc_clipping_plane(float clip[6][4], BoundBox *clipbb)
3035 {
3036         int val;
3037
3038         for(val=0; val<4; val++) {
3039
3040                 normal_tri_v3( clip[val],clipbb->vec[val], clipbb->vec[val==3?0:val+1], clipbb->vec[val+4]);
3041
3042                 clip[val][3]=
3043                         - clip[val][0]*clipbb->vec[val][0]
3044                         - clip[val][1]*clipbb->vec[val][1]
3045                         - clip[val][2]*clipbb->vec[val][2];
3046         }
3047 }
3048
3049 static void calc_local_clipping(float clip_local[][4], BoundBox *clipbb, float mat[][4])
3050 {
3051         BoundBox clipbb_local;
3052         float imat[4][4];
3053         int i;
3054
3055         invert_m4_m4(imat, mat);
3056
3057         for(i=0; i<8; i++) {
3058                 mul_v3_m4v3(clipbb_local.vec[i], imat, clipbb->vec[i]);
3059         }
3060
3061         calc_clipping_plane(clip_local, &clipbb_local);
3062 }
3063
3064 void ED_view3d_local_clipping(RegionView3D *rv3d, float mat[][4])