[Bf-blender-cvs] [ca116153fa4] master: Cleanup: ViewOpsData struct members

Campbell Barton noreply at git.blender.org
Mon Jan 29 04:43:28 CET 2018


Commit: ca116153fa460365ad4f9122fa048d039e83e4b2
Author: Campbell Barton
Date:   Mon Jan 29 14:36:40 2018 +1100
Branches: master
https://developer.blender.org/rBca116153fa460365ad4f9122fa048d039e83e4b2

Cleanup: ViewOpsData struct members

- Group initial/previous/current members
  Was using terms old/prev/last/orig in confusing way.
- Replace x,y variables with vectors.
- Remove unused members.

===================================================================

M	source/blender/editors/space_view3d/view3d_edit.c

===================================================================

diff --git a/source/blender/editors/space_view3d/view3d_edit.c b/source/blender/editors/space_view3d/view3d_edit.c
index 11d5d2d44f2..7bd4e5ff996 100644
--- a/source/blender/editors/space_view3d/view3d_edit.c
+++ b/source/blender/editors/space_view3d/view3d_edit.c
@@ -125,48 +125,70 @@ static void view3d_operator_properties_common(wmOperatorType *ot, const enum eV3
  * \{ */
 
 typedef struct ViewOpsData {
-	/* context pointers (assigned by viewops_data_alloc) */
+	/** Context pointers (assigned by #viewops_data_alloc). */
 	Scene *scene;
 	ScrArea *sa;
 	ARegion *ar;
 	View3D *v3d;
 	RegionView3D *rv3d;
 
-	/* needed for continuous zoom */
+	/** Needed for continuous zoom. */
 	wmTimer *timer;
-	double timer_lastdraw;
 
-	float oldquat[4];
-	float viewquat[4]; /* working copy of rv3d->viewquat */
-	float trackvec[3];
-	float mousevec[3]; /* dolly only */
+	/** Viewport state on initialization, don't change afterwards. */
+	struct {
+		float dist;
+		float camzoom;
+		float quat[4];
+		/** #wmEvent.x, y. */
+		int event_xy[2];
+		/** #wmEvent.type that triggered the operator. */
+		int event_type;
+		float ofs[3];
+		/** Initial distance to 'ofs'. */
+		float zfac;
+
+		/** Trackball rotation only. */
+		float trackvec[3];
+		/** Dolly only. */
+		float mousevec[3];
+	} init;
+
+	/** Previous state (previous modal event handled). */
+	struct {
+		int event_xy[2];
+		/** For operators that use time-steps (continuous zoom). */
+		double time;
+	} prev;
+
+	/** Current state. */
+	struct {
+		/** Working copy of #RegionView3D.viewquat, needed for rotation calculation
+		 * so we can apply snap to the view-port while keeping the unsnapped rotation
+		 * here to use when snap is disabled and for continued calculation. */
+		float viewquat[4];
+	} curr;
+
 	float reverse;
-	float dist_prev, camzoom_prev;
-	float grid, far;
 	bool axis_snap;  /* view rotate only */
-	float zfac;
 
-	/* use for orbit selection and auto-dist */
-	float ofs[3], dyn_ofs[3];
+	/** Use for orbit selection and auto-dist. */
+	float dyn_ofs[3];
 	bool use_dyn_ofs;
-
-	int origx, origy, oldx, oldy;
-	int origkey; /* the key that triggered the operator */
-
 } ViewOpsData;
 
 #define TRACKBALLSIZE  (1.1f)
 
-static void calctrackballvec(const rcti *rect, int mx, int my, float vec[3])
+static void calctrackballvec(const rcti *rect, const int event_xy[2], float vec[3])
 {
 	const float radius = TRACKBALLSIZE;
 	const float t = radius / (float)M_SQRT2;
 	float x, y, z, d;
 
 	/* normalize x and y */
-	x = BLI_rcti_cent_x(rect) - mx;
+	x = BLI_rcti_cent_x(rect) - event_xy[0];
 	x /= (float)(BLI_rcti_size_x(rect) / 4);
-	y = BLI_rcti_cent_y(rect) - my;
+	y = BLI_rcti_cent_y(rect) - event_xy[1];
 	y /= (float)(BLI_rcti_size_y(rect) / 2);
 	d = sqrtf(x * x + y * y);
 	if (d < t) { /* Inside sphere */
@@ -198,7 +220,7 @@ static void viewops_data_alloc(bContext *C, wmOperator *op)
 }
 
 void view3d_orbit_apply_dyn_ofs(
-        float r_ofs[3], const float ofs_old[3], const float viewquat_old[4],
+        float r_ofs[3], const float ofs_init[3], const float viewquat_old[4],
         const float viewquat_new[4], const float dyn_ofs[3])
 {
 	float q[4];
@@ -207,7 +229,7 @@ void view3d_orbit_apply_dyn_ofs(
 
 	invert_qt_normalized(q);
 
-	sub_v3_v3v3(r_ofs, ofs_old, dyn_ofs);
+	sub_v3_v3v3(r_ofs, ofs_init, dyn_ofs);
 	mul_qt_v3(q, r_ofs);
 	add_v3_v3(r_ofs, dyn_ofs);
 }
@@ -372,14 +394,15 @@ static void viewops_data_create_ex(
 	 * we may want to make this optional but for now its needed always */
 	ED_view3d_camera_lock_init(vod->v3d, vod->rv3d);
 
-	vod->dist_prev = rv3d->dist;
-	vod->camzoom_prev = rv3d->camzoom;
-	copy_qt_qt(vod->viewquat, rv3d->viewquat);
-	copy_qt_qt(vod->oldquat, rv3d->viewquat);
-	vod->origx = vod->oldx = event->x;
-	vod->origy = vod->oldy = event->y;
-	vod->origkey = event->type; /* the key that triggered the operator.  */
-	copy_v3_v3(vod->ofs, rv3d->ofs);
+	vod->init.dist = rv3d->dist;
+	vod->init.camzoom = rv3d->camzoom;
+	copy_qt_qt(vod->init.quat, rv3d->viewquat);
+	vod->init.event_xy[0] = vod->prev.event_xy[0] = event->x;
+	vod->init.event_xy[1] = vod->prev.event_xy[1] = event->y;
+	vod->init.event_type = event->type;
+	copy_v3_v3(vod->init.ofs, rv3d->ofs);
+
+	copy_qt_qt(vod->curr.viewquat, rv3d->viewquat);
 
 	if (orbit_mode & VIEWOPS_ORBIT_SELECT) {
 		float ofs[3];
@@ -417,7 +440,7 @@ static void viewops_data_create_ex(
 
 				/* find a new ofs value that is along the view axis (rather than the mouse location) */
 				closest_to_line_v3(dvec, vod->dyn_ofs, my_pivot, my_origin);
-				vod->dist_prev = rv3d->dist = len_v3v3(my_pivot, dvec);
+				vod->init.dist = rv3d->dist = len_v3v3(my_pivot, dvec);
 
 				negate_v3_v3(rv3d->ofs, dvec);
 			}
@@ -430,28 +453,20 @@ static void viewops_data_create_ex(
 				negate_v3(rv3d->ofs);
 			}
 			negate_v3(vod->dyn_ofs);
-			copy_v3_v3(vod->ofs, rv3d->ofs);
+			copy_v3_v3(vod->init.ofs, rv3d->ofs);
 		}
 	}
 
-	{
-		/* for dolly */
-		const float mval_f[2] = {(float)event->mval[0],
-		                         (float)event->mval[1]};
-		ED_view3d_win_to_vector(vod->ar, mval_f, vod->mousevec);
-	}
-
-	/* lookup, we don't pass on v3d to prevent confusement */
-	vod->grid = vod->v3d->grid;
-	vod->far = vod->v3d->far;
+	/* for dolly */
+	ED_view3d_win_to_vector(vod->ar, (const float[2]){UNPACK2(event->mval)}, vod->init.mousevec);
 
 	/* TODO: use_mouse_init support */
-	calctrackballvec(&vod->ar->winrct, event->x, event->y, vod->trackvec);
+	calctrackballvec(&vod->ar->winrct, &event->x, vod->init.trackvec);
 
 	{
 		float tvec[3];
 		negate_v3_v3(tvec, rv3d->ofs);
-		vod->zfac = ED_view3d_calc_zfac(rv3d, tvec, NULL);
+		vod->init.zfac = ED_view3d_calc_zfac(rv3d, tvec, NULL);
 	}
 
 	vod->reverse = 1.0f;
@@ -560,7 +575,7 @@ static void viewrotate_apply_dyn_ofs(ViewOpsData *vod, const float viewquat_new[
 {
 	if (vod->use_dyn_ofs) {
 		RegionView3D *rv3d = vod->rv3d;
-		view3d_orbit_apply_dyn_ofs(rv3d->ofs, vod->ofs, vod->oldquat, viewquat_new, vod->dyn_ofs);
+		view3d_orbit_apply_dyn_ofs(rv3d->ofs, vod->init.ofs, vod->init.quat, viewquat_new, vod->dyn_ofs);
 	}
 }
 
@@ -576,7 +591,7 @@ static void viewrotate_apply_snap(ViewOpsData *vod)
 	int x, y, z;
 	bool found = false;
 
-	invert_qt_qt_normalized(viewquat_inv, vod->viewquat);
+	invert_qt_qt_normalized(viewquat_inv, vod->curr.viewquat);
 
 	mul_qt_v3(viewquat_inv, zaxis);
 	normalize_v3(zaxis);
@@ -612,7 +627,7 @@ static void viewrotate_apply_snap(ViewOpsData *vod)
 		 * for testing roll */
 		rotation_between_vecs_to_quat(viewquat_align, zaxis_best, zaxis);
 		normalize_qt(viewquat_align);
-		mul_qt_qtqt(viewquat_align, vod->viewquat, viewquat_align);
+		mul_qt_qtqt(viewquat_align, vod->curr.viewquat, viewquat_align);
 		normalize_qt(viewquat_align);
 		invert_qt_qt_normalized(viewquat_align_inv, viewquat_align);
 
@@ -666,7 +681,7 @@ static void viewrotate_apply_snap(ViewOpsData *vod)
 	}
 }
 
-static void viewrotate_apply(ViewOpsData *vod, int x, int y)
+static void viewrotate_apply(ViewOpsData *vod, const int event_xy[2])
 {
 	RegionView3D *rv3d = vod->rv3d;
 
@@ -676,9 +691,9 @@ static void viewrotate_apply(ViewOpsData *vod, int x, int y)
 		float axis[3], q1[4], dvec[3], newvec[3];
 		float angle;
 
-		calctrackballvec(&vod->ar->winrct, x, y, newvec);
+		calctrackballvec(&vod->ar->winrct, event_xy, newvec);
 
-		sub_v3_v3v3(dvec, newvec, vod->trackvec);
+		sub_v3_v3v3(dvec, newvec, vod->init.trackvec);
 
 		angle = (len_v3(dvec) / (2.0f * TRACKBALLSIZE)) * (float)M_PI;
 
@@ -689,12 +704,12 @@ static void viewrotate_apply(ViewOpsData *vod, int x, int y)
 		 * so that the angle of rotation is linearly proportional to
 		 * the distance that the mouse is dragged. */
 
-		cross_v3_v3v3(axis, vod->trackvec, newvec);
+		cross_v3_v3v3(axis, vod->init.trackvec, newvec);
 		axis_angle_to_quat(q1, axis, angle);
 
-		mul_qt_qtqt(vod->viewquat, q1, vod->oldquat);
+		mul_qt_qtqt(vod->curr.viewquat, q1, vod->init.quat);
 
-		viewrotate_apply_dyn_ofs(vod, vod->viewquat);
+		viewrotate_apply_dyn_ofs(vod, vod->curr.viewquat);
 	}
 	else {
 		/* New turntable view code by John Aughey */
@@ -711,7 +726,7 @@ static void viewrotate_apply(ViewOpsData *vod, int x, int y)
 		const float sensitivity = 0.007f;
 
 		/* Get the 3x3 matrix and its inverse from the quaternion */
-		quat_to_mat3(m, vod->viewquat);
+		quat_to_mat3(m, vod->curr.viewquat);
 		invert_m3_m3(m_inv, m);
 
 		/* avoid gimble lock */
@@ -738,30 +753,30 @@ static void viewrotate_apply(ViewOpsData *vod, int x, int y)
 		/* This can likely be computed directly from the quaternion. */
 
 		/* Perform the up/down rotation */
-		axis_angle_to_quat(quat_local_x, xaxis, sensitivity * -(y - vod->oldy));
-		mul_qt_qtqt(quat_local_x, vod->viewquat, quat_local_x);
+		axis_angle_to_quat(quat_local_x, xaxis, sensitivity * -(event_xy[1] - vod->prev.event_xy[1]));
+		mul_qt_qtqt(quat_local_x, vod->curr.viewquat, quat_local_x);
 
 		/* Perform the orbital rotation */
-		axis_angle_to_quat_single(quat_global_z, 'Z', sensitivity * vod->reverse * (x - vod->oldx));
-		mul_qt_qtqt(vod->viewquat, quat_local_x, quat_global_z);
+		axis_angle_to_quat_single(quat_global_z, 'Z', sensitivity * vod->reverse * (event_xy[0] - vod->prev.event_xy[0]));
+		mul_qt_qtqt(vod->curr.viewquat, quat_local_x, quat_global_z);
 
-		viewrotate_apply_dyn_ofs(vod, vod->viewquat);
+		viewrotate_apply_dyn_ofs(vod, vod->curr.viewquat);
 	}
 
 	/* avoid precision loss over time */
-	normalize_qt(vod->viewquat);
+	normalize_qt(vod->curr.viewquat);
 
 	/* use a working copy so view rotation locking doesnt overwrite the locked
 	 * rotation back into the view we calculate with */
-	copy_qt_qt(rv3d->viewquat, vod->viewquat);
+	copy_qt_qt(rv3d->viewquat, vod->curr.viewquat);
 
 	/* check for view snap,
 	 * note: don't apply snap to vod->viewquat so the view wont jam up */
 	if (vod->axis_snap) {
 		viewrotate_apply_snap(vod);
 	}
-	vod->oldx = x;
-	vod->oldy = y;
+	vod->prev.event_xy[0] = event_xy[0];
+	vod->prev.event_xy[1] = event_xy[1];
 
 	ED_view3d_camera_lock_sync(vod->v3d, rv3d);
 
@@ -802,12 +817,12 @@ static int viewrotate_modal(bContext *C, wmOperator *op, const wmEvent *event)
 				break;
 		}
 	}
-	else if (event->type == vod->origkey && event->val == KM_RELEASE) 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list