Skip to content

Commit

Permalink
Update vtkBoneWidget only when necessary
Browse files Browse the repository at this point in the history
Previously, there was still some set...() methods that did not check
 (or improperly) if the new values changed, causing unecessary
computations and ModifiedEvent() to be fired.

Issue #13656
  • Loading branch information
vovythevov committed Nov 5, 2012
1 parent cdb5c84 commit ed70333
Showing 1 changed file with 90 additions and 14 deletions.
104 changes: 90 additions & 14 deletions Libs/VTK/Widgets/vtkBoneWidget.cxx
Expand Up @@ -82,6 +82,14 @@ bool CompareVector3(const double* v1, const double* v2)
return false;
}

bool CompareQuaternion(vtkQuaterniond q1, double q2[4])
{
vtkQuaterniond quad2;
quad2.Set(q2);

return q1.Compare(quad2, 1e-6);
}

}// End namespace

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -356,6 +364,7 @@ void vtkBoneWidget::CreateDefaultRepresentation()
this->InstantiateParenthoodLink();

this->UpdateRepresentation();
this->Modified();
}

//----------------------------------------------------------------------------
Expand All @@ -371,10 +380,12 @@ void vtkBoneWidget::SetProcessEvents(int pe)
void vtkBoneWidget::SetWidgetState(int state)
{
state = std::min(3, std::max(state, 0));
if (state == vtkBoneWidget::PlaceHead || state == vtkBoneWidget::PlaceTail)
if (state == this->WidgetState
|| state == vtkBoneWidget::PlaceHead || state == vtkBoneWidget::PlaceTail)
{
return;
}

this->BoneSelected = vtkBoneWidget::NotSelected;
this->WidgetState = state;

Expand Down Expand Up @@ -458,21 +469,46 @@ void vtkBoneWidget
::SetWorldToParentRestRotationAndTranslation(double quat[4],
double translate[3])
{
this->WorldToParentRestRotation.Set(quat);
CopyVector3(translate, this->WorldToParentRestTranslation);
this->UpdateRestMode();
bool changeRotation =
! CompareQuaternion(this->WorldToParentRestRotation, quat);
bool changeTranslation =
! CompareVector3(this->WorldToParentRestTranslation, translate);

if (changeRotation)
{
this->WorldToParentRestRotation.Set(quat);
}
if (changeTranslation)
{
CopyVector3(translate, this->WorldToParentRestTranslation);
}

if (changeRotation || changeTranslation)
{
this->UpdateRestMode();
}
}

//----------------------------------------------------------------------------
void vtkBoneWidget::SetWorldToParentRestRotation(double quat[4])
{
if (CompareQuaternion(this->WorldToParentRestRotation, quat))
{
return;
}

this->WorldToParentRestRotation.Set(quat);
this->UpdateRestMode();
}

//----------------------------------------------------------------------------
void vtkBoneWidget::SetWorldToParentRestTranslation(double translate[3])
{
if (CompareVector3(this->WorldToParentRestTranslation, translate))
{
return;
}

CopyVector3(translate, this->WorldToParentRestTranslation);
this->UpdateRestMode(); //probably recomputing rotations for nothing.
}
Expand Down Expand Up @@ -573,13 +609,35 @@ void vtkBoneWidget
::SetWorldToParentPoseRotationAndTranslation(double quat[4],
double translate[3])
{
this->WorldToParentPoseRotation.Set(quat);
this->SetWorldToParentPoseTranslation(translate);
bool changeRotation =
! CompareQuaternion(this->WorldToParentPoseRotation, quat);
bool changeTranslation =
! CompareVector3(this->WorldToParentPoseTranslation, translate);

if (changeRotation)
{
this->WorldToParentPoseRotation.Set(quat);
}
if (changeTranslation)
{
CopyVector3(translate, this->WorldToParentPoseTranslation);
}

if (changeRotation || changeTranslation)
{
this->UpdateWorldPosePositions();
this->UpdatePoseMode();
}
}

//----------------------------------------------------------------------------
void vtkBoneWidget::SetWorldToParentPoseRotation(double quat[4])
{
if (CompareQuaternion(this->WorldToParentPoseRotation, quat))
{
return;
}

this->WorldToParentPoseRotation.Set(quat);
this->UpdateWorldPosePositions();
this->UpdatePoseMode();
Expand All @@ -588,6 +646,11 @@ void vtkBoneWidget::SetWorldToParentPoseRotation(double quat[4])
///----------------------------------------------------------------------------
void vtkBoneWidget::SetWorldToParentPoseTranslation(double translate[3])
{
if (CompareVector3(this->WorldToParentPoseTranslation, translate))
{
return;
}

CopyVector3(translate, this->WorldToParentPoseTranslation);
this->UpdateWorldPosePositions();
this->UpdatePoseMode();
Expand Down Expand Up @@ -686,17 +749,22 @@ vtkSmartPointer<vtkTransform>
//----------------------------------------------------------------------------
void vtkBoneWidget::SetWorldHeadAndTailRest(double head[3], double tail[3])
{
if (! CompareVector3(this->WorldHeadRest, head))
bool changeHead = ! CompareVector3(this->WorldHeadRest, head);
bool changeTail = ! CompareVector3(this->WorldTailRest, tail);

if (changeHead)
{
CopyVector3(head, this->WorldHeadRest);
}

if (! CompareVector3(this->WorldTailRest, tail))
if (changeTail)
{
CopyVector3(tail, this->WorldTailRest);
}

this->UpdateRestMode();
if (changeHead || changeTail)
{
this->UpdateRestMode();
}
}

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -801,17 +869,22 @@ void vtkBoneWidget::SetDisplayTailRestPosition(double displayTail[3])
//----------------------------------------------------------------------------
void vtkBoneWidget::SetLocalHeadAndTailRest(double head[3], double tail[3])
{
if (! CompareVector3(this->LocalHeadRest, head))
bool changeHead = ! CompareVector3(this->LocalHeadRest, head);
bool changeTail = ! CompareVector3(this->LocalTailRest, tail);

if (changeHead)
{
CopyVector3(head, this->LocalHeadRest);
}

if (! CompareVector3(this->LocalTailRest, tail))
if (changeTail)
{
CopyVector3(tail, this->LocalTailRest);
}

this->UpdateRestMode();
if (changeHead || changeTail)
{
this->UpdateRestMode();
}
}

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -997,6 +1070,7 @@ void vtkBoneWidget::StartSelectAction(vtkAbstractWidget *w)

self->SetDisplayTailRestPosition(e);
CopyVector3(self->WorldTailRest, self->WorldTailPose);
self->Modified();
}

else if ( self->WidgetState == vtkBoneWidget::Rest
Expand Down Expand Up @@ -1875,6 +1949,8 @@ void vtkBoneWidget::SetWidgetSelectedState(int selectionState)
{
this->GetBoneRepresentation()->Highlight(0);
}

this->Modified();
}

//----------------------------------------------------------------------------
Expand Down

0 comments on commit ed70333

Please sign in to comment.