Made wmNDOFMotionData use a vector rather then xyz members, makes it nicer to use with math functions.

ndof_to_angle_axis and ndof_to_quat now use math functions.
This commit is contained in:
Campbell Barton 2011-08-02 07:08:22 +00:00
parent 2e860a3e85
commit 70b4758ff8
7 changed files with 39 additions and 64 deletions

@ -271,7 +271,7 @@ class InputKeyMapPanel:
row.prop(kmi, "type", text="", full_event=True) row.prop(kmi, "type", text="", full_event=True)
elif map_type == 'MOUSE': elif map_type == 'MOUSE':
row.prop(kmi, "type", text="", full_event=True) row.prop(kmi, "type", text="", full_event=True)
if map_type == 'NDOF': elif map_type == 'NDOF':
row.prop(kmi, "type", text="", full_event=True) row.prop(kmi, "type", text="", full_event=True)
elif map_type == 'TWEAK': elif map_type == 'TWEAK':
subrow = row.row() subrow = row.row()
@ -308,7 +308,7 @@ class InputKeyMapPanel:
sub = split.column() sub = split.column()
subrow = sub.row(align=True) subrow = sub.row(align=True)
if map_type in ('KEYBOARD', 'NDOF'): if map_type in {'KEYBOARD', 'NDOF'}:
subrow.prop(kmi, "type", text="", event=True) subrow.prop(kmi, "type", text="", event=True)
subrow.prop(kmi, "value", text="") subrow.prop(kmi, "value", text="")
elif map_type == 'MOUSE': elif map_type == 'MOUSE':

@ -461,8 +461,8 @@ static int view_ndof_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
const float zoom_sensitivity = 0.5f; const float zoom_sensitivity = 0.5f;
const float pan_sensitivity = 300.f; const float pan_sensitivity = 300.f;
float pan_x = pan_sensitivity * dt * ndof->tx / sima->zoom; float pan_x = pan_sensitivity * dt * ndof->tvec[0] / sima->zoom;
float pan_y = pan_sensitivity * dt * ndof->ty / sima->zoom; float pan_y = pan_sensitivity * dt * ndof->tvec[1] / sima->zoom;
/* "mouse zoom" factor = 1 + (dx + dy) / 300 /* "mouse zoom" factor = 1 + (dx + dy) / 300
* what about "ndof zoom" factor? should behave like this: * what about "ndof zoom" factor? should behave like this:
@ -470,7 +470,7 @@ static int view_ndof_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
* move forward -> factor > 1 * move forward -> factor > 1
* move backward -> factor < 1 * move backward -> factor < 1
*/ */
float zoom_factor = 1.f + zoom_sensitivity * dt * -ndof->tz; float zoom_factor = 1.f + zoom_sensitivity * dt * -ndof->tvec[2];
sima_zoom_set_factor(sima, ar, zoom_factor); sima_zoom_set_factor(sima, ar, zoom_factor);
sima->xof += pan_x; sima->xof += pan_x;

@ -931,42 +931,18 @@ void VIEW3D_OT_rotate(wmOperatorType *ot)
// NDOF utility functions // NDOF utility functions
// (should these functions live in this file?) // (should these functions live in this file?)
float ndof_to_angle_axis(struct wmNDOFMotionData* ndof, float axis[3]) float ndof_to_angle_axis(struct wmNDOFMotionData* ndof, float axis[3])
{ {
const float x = ndof->rx; return ndof->dt * normalize_v3_v3(axis, ndof->rvec);
const float y = ndof->ry; }
const float z = ndof->rz;
float angular_velocity = sqrtf(x*x + y*y + z*z);
float angle = ndof->dt * angular_velocity;
float scale = 1.f / angular_velocity;
// normalize
axis[0] = scale * x;
axis[1] = scale * y;
axis[2] = scale * z;
return angle;
}
void ndof_to_quat(struct wmNDOFMotionData* ndof, float q[4]) void ndof_to_quat(struct wmNDOFMotionData* ndof, float q[4])
{ {
const float x = ndof->rx; float axis[3];
const float y = ndof->ry; float angle;
const float z = ndof->rz;
float angular_velocity = sqrtf(x*x + y*y + z*z); angle= ndof_to_angle_axis(ndof, axis);
float angle = ndof->dt * angular_velocity; axis_angle_to_quat(q, axis, angle);
}
// combined scaling factor -- normalize axis while converting to quaternion
float scale = sin(0.5f * angle) / angular_velocity;
// convert axis-angle to quaternion
q[0] = cos(0.5f * angle);
q[1] = scale * x;
q[2] = scale * y;
q[3] = scale * z;
}
static int ndof_orbit_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event) static int ndof_orbit_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
// -- "orbit" navigation (trackball/turntable) // -- "orbit" navigation (trackball/turntable)
@ -987,7 +963,7 @@ static int ndof_orbit_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event
const float pan_sensitivity = 1.f; const float pan_sensitivity = 1.f;
// rather have bool, but... // rather have bool, but...
int has_rotation = rv3d->viewlock != RV3D_LOCKED && (ndof->rx || ndof->ry || ndof->rz); int has_rotation = rv3d->viewlock != RV3D_LOCKED && !is_zero_v3(ndof->rvec);
float view_inv[4]; float view_inv[4];
invert_qt_qt(view_inv, rv3d->viewquat); invert_qt_qt(view_inv, rv3d->viewquat);
@ -998,19 +974,19 @@ static int ndof_orbit_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event
ndof->tx, ndof->ty, ndof->tz, ndof->rx, ndof->ry, ndof->rz, ndof->dt); ndof->tx, ndof->ty, ndof->tz, ndof->rx, ndof->ry, ndof->rz, ndof->dt);
#endif #endif
if (ndof->tz) { if (ndof->tvec[2]) {
// Zoom! // Zoom!
// velocity should be proportional to the linear velocity attained by rotational motion of same strength // velocity should be proportional to the linear velocity attained by rotational motion of same strength
// [got that?] // [got that?]
// proportional to arclength = radius * angle // proportional to arclength = radius * angle
float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tz; float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tvec[2];
rv3d->dist += zoom_distance; rv3d->dist += zoom_distance;
} }
if (rv3d->viewlock == RV3D_LOCKED) { if (rv3d->viewlock == RV3D_LOCKED) {
/* rotation not allowed -- explore panning options instead */ /* rotation not allowed -- explore panning options instead */
float pan_vec[3] = {ndof->tx, ndof->ty, 0}; float pan_vec[3] = {ndof->tvec[0], ndof->tvec[1], 0.0f};
mul_v3_fl(pan_vec, pan_sensitivity * rv3d->dist * dt); mul_v3_fl(pan_vec, pan_sensitivity * rv3d->dist * dt);
/* transform motion from view to world coordinates */ /* transform motion from view to world coordinates */
@ -1072,7 +1048,7 @@ static int ndof_orbit_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event
mul_qt_v3(view_inv, xvec); mul_qt_v3(view_inv, xvec);
/* Perform the up/down rotation */ /* Perform the up/down rotation */
angle = rot_sensitivity * dt * ndof->rx; angle = rot_sensitivity * dt * ndof->rvec[0];
if (invert) if (invert)
angle = -angle; angle = -angle;
rot[0] = cos(angle); rot[0] = cos(angle);
@ -1080,7 +1056,7 @@ static int ndof_orbit_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event
mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot); mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
/* Perform the orbital rotation */ /* Perform the orbital rotation */
angle = rot_sensitivity * dt * ndof->ry; angle = rot_sensitivity * dt * ndof->rvec[1];
if (invert) if (invert)
angle = -angle; angle = -angle;
@ -1155,11 +1131,10 @@ static int ndof_pan_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
const float vertical_sensitivity = 0.4f; const float vertical_sensitivity = 0.4f;
const float lateral_sensitivity = 0.6f; const float lateral_sensitivity = 0.6f;
float pan_vec[3] = { float pan_vec[3] = {lateral_sensitivity * ndof->tvec[0],
lateral_sensitivity * ndof->tx, vertical_sensitivity * ndof->tvec[1],
vertical_sensitivity * ndof->ty, forward_sensitivity * ndof->tvec[2]
forward_sensitivity * ndof->tz };
};
mul_v3_fl(pan_vec, speed * dt); mul_v3_fl(pan_vec, speed * dt);
#endif #endif

@ -953,9 +953,9 @@ static int flyApply_ndof(bContext *C, FlyInfo *fly)
// ^^ this is ok for default cube scene, but should scale with.. something // ^^ this is ok for default cube scene, but should scale with.. something
float trans[3] = { float trans[3] = {
lateral_sensitivity * ndof->tx, lateral_sensitivity * ndof->tvec[0],
vertical_sensitivity * ndof->ty, vertical_sensitivity * ndof->tvec[1],
forward_sensitivity * ndof->tz forward_sensitivity * ndof->tvec[2]
}; };
if (fly->use_precision) if (fly->use_precision)
@ -969,7 +969,7 @@ static int flyApply_ndof(bContext *C, FlyInfo *fly)
if (flag & NDOF_FLY_HELICOPTER) if (flag & NDOF_FLY_HELICOPTER)
{ {
// replace world z component with device y (yes it makes sense) // replace world z component with device y (yes it makes sense)
trans[2] = speed * dt * vertical_sensitivity * ndof->ty; trans[2] = speed * dt * vertical_sensitivity * ndof->tvec[1];
} }
if (rv3d->persp==RV3D_CAMOB) { if (rv3d->persp==RV3D_CAMOB) {

@ -361,10 +361,10 @@ static int transform_modal(bContext *C, wmOperator *op, wmEvent *event)
TransInfo *t = op->customdata; TransInfo *t = op->customdata;
if (event->type == NDOF_MOTION) if (event->type == NDOF_MOTION)
{ {
// puts("transform_modal: passing through NDOF_MOTION"); /* puts("transform_modal: passing through NDOF_MOTION"); */
return OPERATOR_PASS_THROUGH; return OPERATOR_PASS_THROUGH;
} }
/* XXX insert keys are called here, and require context */ /* XXX insert keys are called here, and require context */
t->context= C; t->context= C;

@ -389,8 +389,8 @@ typedef struct wmNDOFMotionData {
/* awfully similar to GHOST_TEventNDOFMotionData... */ /* awfully similar to GHOST_TEventNDOFMotionData... */
// Each component normally ranges from -1 to +1, but can exceed that. // Each component normally ranges from -1 to +1, but can exceed that.
// These use blender standard view coordinates, with positive rotations being CCW about the axis. // These use blender standard view coordinates, with positive rotations being CCW about the axis.
float tx, ty, tz; // translation float tvec[3]; // translation
float rx, ry, rz; // rotation: float rvec[3]; // rotation:
// axis = (rx,ry,rz).normalized // axis = (rx,ry,rz).normalized
// amount = (rx,ry,rz).magnitude [in revolutions, 1.0 = 360 deg] // amount = (rx,ry,rz).magnitude [in revolutions, 1.0 = 360 deg]
float dt; // time since previous NDOF Motion event float dt; // time since previous NDOF Motion event

@ -2323,13 +2323,13 @@ static void attach_ndof_data(wmEvent* event, const GHOST_TEventNDOFMotionData* g
const float s = U.ndof_sensitivity; const float s = U.ndof_sensitivity;
data->tx = s * ghost->tx; data->tvec[0]= s * ghost->tx;
data->ty = s * ghost->ty; data->tvec[1]= s * ghost->ty;
data->tz = s * ghost->tz; data->tvec[2]= s * ghost->tz;
data->rx = s * ghost->rx; data->rvec[0]= s * ghost->rx;
data->ry = s * ghost->ry; data->rvec[1]= s * ghost->ry;
data->rz = s * ghost->rz; data->rvec[2]= s * ghost->rz;
data->dt = ghost->dt; data->dt = ghost->dt;