Skip to content

Commit

Permalink
Merge pull request #1 from Meakk/add_camera_shortcuts
Browse files Browse the repository at this point in the history
Camera orientation Z-up fix
  • Loading branch information
lknknm authored Jul 5, 2023
2 parents 27cc279 + 58f6f99 commit 470c5e8
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions library/src/interactor_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class interactor_impl::internals
//----------------------------------------------------------------------------
// Method defined to normalize the Z axis so all models are treated temporarily
// as Z-up axis models.
void zUpTransforms(vtkMatrix3x3* to, vtkMatrix3x3* from)
void ToEnvironmentSpace(vtkMatrix3x3* transform)
{
vtkRenderer* renderer =
this->VTKInteractor->GetRenderWindow()->GetRenderers()->GetFirstRenderer();
Expand All @@ -91,9 +91,7 @@ class interactor_impl::internals
fwd[0], fwd[1], fwd[2], //
up[0], up[1], up[2], //
};
to->DeepCopy(m);
from->DeepCopy(m);
from->Transpose();
transform->DeepCopy(m);
}

//----------------------------------------------------------------------------
Expand All @@ -107,53 +105,46 @@ class interactor_impl::internals
};
static void SetViewOrbit(ViewType view, internals* self)
{
vtkNew<vtkMatrix3x3> toZup, fromZup;
self->zUpTransforms(toZup, fromZup);
vtkNew<vtkMatrix3x3> transform;
self->ToEnvironmentSpace(transform);
camera& cam = self->Window.getCamera();
vector3_t up = { 0, 0, 1 };
point3_t pos = cam.getPosition();
point3_t foc = cam.getFocalPoint();
point3_t axis, newPos;

/* convert coords to +Z up */
toZup->MultiplyPoint(pos.data(), pos.data());
toZup->MultiplyPoint(foc.data(), foc.data());

const double dx = foc[0] - pos[0];
const double dy = foc[1] - pos[1];
const double dz = foc[2] - pos[2];
double radius = sqrt(dx * dx + dy * dy + dz * dz);
switch (view)
{
case ViewType::VT_FRONT:
axis = { 0, 0, +1 };
axis = { 0, +1, 0 };
break;
case ViewType::VT_RIGHT:
axis = { +1, 0, 0 };
break;
case ViewType::VT_TOP:
axis = { 0, +1, 0 };
axis = { 0, 0, +1 };
up = { 0, -1, 0 };
break;
case ViewType::VT_ISOMETRIC:
axis = { -1, +1, +1 };
double oneThirdRoot = sqrt(1.0 / 3.0);
axis = { -oneThirdRoot, +oneThirdRoot, +oneThirdRoot };
break;
}
fromZup->MultiplyPoint(up.data(), up.data());
fromZup->MultiplyPoint(axis.data(), axis.data());

transform->MultiplyPoint(up.data(), up.data());
transform->MultiplyPoint(axis.data(), axis.data());

newPos[0] = foc[0] + radius * axis[0];
newPos[1] = foc[1] + radius * axis[1];
newPos[2] = foc[2] + radius * axis[2];

/* convert coordinates back to whatever up is according to model/options */
fromZup->MultiplyPoint(newPos.data(), newPos.data());
fromZup->MultiplyPoint(foc.data(), foc.data());

/* set camera coordinates back */
cam.setPosition(newPos);
cam.setViewUp(up);
cam.resetToBounds(0.9);
}

static void OnKeyPress(vtkObject*, unsigned long, void* clientData, void*)
Expand Down

0 comments on commit 470c5e8

Please sign in to comment.