Commit 2ae2e104 authored by Julien Finet's avatar Julien Finet
Browse files

Add vtkControlPointsItem::BlockUpdates

To reduce the number of updates when modifying the transfer functions, we
abort computePoints() when doing batch changes on the transfer functions.

Change-Id: Ie8360edfeefbe262c6fb8e2e0739bef8b65cb74b
parent e0571ebd
......@@ -67,6 +67,24 @@ void vtkColorTransferControlPointsItem::PrintSelf(ostream &os, vtkIndent indent)
}
}
//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::StartChanges()
{
if (this->ColorTransferFunction)
{
this->ColorTransferFunction->InvokeEvent(vtkCommand::StartEvent);
}
}
//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::EndChanges()
{
if (this->ColorTransferFunction)
{
this->ColorTransferFunction->InvokeEvent(vtkCommand::EndEvent);
}
}
//-----------------------------------------------------------------------------
unsigned long int vtkColorTransferControlPointsItem::GetControlPointsMTime()
{
......@@ -80,6 +98,10 @@ unsigned long int vtkColorTransferControlPointsItem::GetControlPointsMTime()
//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::SetColorTransferFunction(vtkColorTransferFunction* t)
{
if (t == this->ColorTransferFunction)
{
return;
}
if (this->ColorTransferFunction)
{
this->ColorTransferFunction->RemoveObserver(this->Callback);
......@@ -87,7 +109,9 @@ void vtkColorTransferControlPointsItem::SetColorTransferFunction(vtkColorTransfe
vtkSetObjectBodyMacro(ColorTransferFunction, vtkColorTransferFunction, t);
if (this->ColorTransferFunction)
{
this->ColorTransferFunction->AddObserver(vtkCommand::StartEvent, this->Callback);
this->ColorTransferFunction->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
this->ColorTransferFunction->AddObserver(vtkCommand::EndEvent, this->Callback);
}
this->ResetBounds();
this->ComputePoints();
......
......@@ -85,6 +85,9 @@ protected:
vtkColorTransferControlPointsItem();
virtual ~vtkColorTransferControlPointsItem();
virtual void StartChanges();
virtual void EndChanges();
virtual unsigned long int GetControlPointsMTime();
virtual void DrawPoint(vtkContext2D* painter, vtkIdType index);
......
......@@ -40,7 +40,6 @@ vtkCompositeControlPointsItem::vtkCompositeControlPointsItem()
{
this->PointsFunction = ColorAndOpacityPointsFunction;
this->OpacityFunction = 0;
this->Updating = false;
this->ColorFill = true;
}
......@@ -70,6 +69,26 @@ void vtkCompositeControlPointsItem::PrintSelf(ostream &os, vtkIndent indent)
}
}
//-----------------------------------------------------------------------------
void vtkCompositeControlPointsItem::StartChanges()
{
if (this->OpacityFunction)
{
this->OpacityFunction->InvokeEvent(vtkCommand::StartEvent);
}
this->Superclass::StartChanges();
}
//-----------------------------------------------------------------------------
void vtkCompositeControlPointsItem::EndChanges()
{
if (this->OpacityFunction)
{
this->OpacityFunction->InvokeEvent(vtkCommand::EndEvent);
}
this->Superclass::EndChanges();
}
//-----------------------------------------------------------------------------
unsigned long int vtkCompositeControlPointsItem::GetControlPointsMTime()
{
......@@ -84,6 +103,10 @@ unsigned long int vtkCompositeControlPointsItem::GetControlPointsMTime()
//-----------------------------------------------------------------------------
void vtkCompositeControlPointsItem::SetOpacityFunction(vtkPiecewiseFunction* function)
{
if (function == this->OpacityFunction)
{
return;
}
if (this->OpacityFunction)
{
this->OpacityFunction->RemoveObserver(this->Callback);
......@@ -95,7 +118,9 @@ void vtkCompositeControlPointsItem::SetOpacityFunction(vtkPiecewiseFunction* fun
}
if (this->OpacityFunction)
{
this->OpacityFunction->AddObserver(vtkCommand::StartEvent, this->Callback);
this->OpacityFunction->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
this->OpacityFunction->AddObserver(vtkCommand::EndEvent, this->Callback);
}
this->ResetBounds();
this->ComputePoints();
......@@ -104,6 +129,10 @@ void vtkCompositeControlPointsItem::SetOpacityFunction(vtkPiecewiseFunction* fun
//-----------------------------------------------------------------------------
void vtkCompositeControlPointsItem::SetColorTransferFunction(vtkColorTransferFunction* c)
{
if (c == this->ColorTransferFunction)
{
return;
}
// Observation will be set Superclass::SetColorTransferFunction
if (this->ColorTransferFunction)
{
......@@ -119,15 +148,6 @@ void vtkCompositeControlPointsItem::SetColorTransferFunction(vtkColorTransferFun
}
this->Superclass::SetColorTransferFunction(c);
}
//-----------------------------------------------------------------------------
void vtkCompositeControlPointsItem::ComputePoints()
{
if (this->Updating)
{
return;
}
this->Superclass::ComputePoints();
}
//-----------------------------------------------------------------------------
void vtkCompositeControlPointsItem::DrawPoint(vtkContext2D* painter, vtkIdType index)
......@@ -234,28 +254,23 @@ void vtkCompositeControlPointsItem::EditPoint(float tX, float tY)
vtkIdType vtkCompositeControlPointsItem::AddPoint(double* newPos)
{
vtkIdType addedPoint = -1;
this->StartChanges();
if (this->OpacityFunction &&
(this->PointsFunction == OpacityPointsFunction ||
this->PointsFunction == ColorAndOpacityPointsFunction))
{
bool oldUpdating = this->Updating;
if (this->PointsFunction == ColorAndOpacityPointsFunction)
{
this->Updating = true;
}
addedPoint = this->OpacityFunction->AddPoint(newPos[0], newPos[1]);
if (this->PointsFunction == OpacityPointsFunction)
{
this->vtkControlPointsItem::AddPointId(addedPoint);
}
this->Updating = oldUpdating;
}
if (this->PointsFunction == ColorPointsFunction ||
this->PointsFunction == ColorAndOpacityPointsFunction)
{
addedPoint = this->Superclass::AddPoint(newPos);
}
this->EndChanges();
return addedPoint;
}
......@@ -263,16 +278,11 @@ vtkIdType vtkCompositeControlPointsItem::AddPoint(double* newPos)
vtkIdType vtkCompositeControlPointsItem::RemovePoint(double* currentPoint)
{
vtkIdType removedPoint = -1;
this->StartChanges();
if (this->PointsFunction == ColorPointsFunction ||
this->PointsFunction == ColorAndOpacityPointsFunction)
{
bool oldUpdating = this->Updating;
if (this->PointsFunction == ColorAndOpacityPointsFunction)
{
this->Updating = true;
}
removedPoint = this->Superclass::RemovePoint(currentPoint);
this->Updating = oldUpdating;
}
if (this->OpacityFunction &&
(this->PointsFunction == OpacityPointsFunction ||
......@@ -280,6 +290,7 @@ vtkIdType vtkCompositeControlPointsItem::RemovePoint(double* currentPoint)
{
removedPoint = this->OpacityFunction->RemovePoint(currentPoint[0]);
}
this->EndChanges();
return removedPoint;
}
......@@ -318,7 +329,7 @@ void vtkCompositeControlPointsItem::MergeTransferFunctions()
//-----------------------------------------------------------------------------
void vtkCompositeControlPointsItem::SilentMergeTransferFunctions()
{
bool wasUpdating = this->Updating;
this->StartChanges();
this->MergeTransferFunctions();
this->Updating = wasUpdating;
this->EndChanges();
}
......@@ -50,7 +50,7 @@ public:
void SetOpacityFunction(vtkPiecewiseFunction* opacity);
vtkGetObjectMacro(OpacityFunction, vtkPiecewiseFunction);
enum PointsFunction{
enum PointsFunctionType{
ColorPointsFunction = 1,
OpacityPointsFunction = 2,
ColorAndOpacityPointsFunction = 3
......@@ -85,8 +85,10 @@ protected:
vtkCompositeControlPointsItem();
virtual ~vtkCompositeControlPointsItem();
virtual void StartChanges();
virtual void EndChanges();
virtual unsigned long int GetControlPointsMTime();
virtual void ComputePoints();
virtual vtkIdType GetNumberOfPoints()const;
virtual void DrawPoint(vtkContext2D* painter, vtkIdType index);
......@@ -99,7 +101,6 @@ protected:
int PointsFunction;
vtkPiecewiseFunction* OpacityFunction;
bool Updating;
private:
vtkCompositeControlPointsItem(const vtkCompositeControlPointsItem &); // Not implemented.
......
......@@ -51,6 +51,8 @@ vtkControlPointsItem::vtkControlPointsItem()
this->Selection = vtkIdTypeArray::New();
this->CurrentPoint = -1;
this->BlockUpdates = 0;
this->Callback = vtkCallbackCommand::New();
this->Callback->SetClientData(this);
this->Callback->SetCallback(
......@@ -208,17 +210,39 @@ bool vtkControlPointsItem::Paint(vtkContext2D* painter)
//-----------------------------------------------------------------------------
void vtkControlPointsItem::CallComputePoints(
vtkObject* vtkNotUsed(sender), unsigned long vtkNotUsed(event),
vtkObject* vtkNotUsed(sender), unsigned long event,
void* receiver, void* vtkNotUsed(params))
{
vtkControlPointsItem* item =
reinterpret_cast<vtkControlPointsItem*>(receiver);
item->ComputePoints();
switch(event)
{
case vtkCommand::StartEvent:
++item->BlockUpdates;
break;
case vtkCommand::EndEvent:
--item->BlockUpdates;
if (item->BlockUpdates == 0)
{
item->ComputePoints();
}
break;
case vtkCommand::ModifiedEvent:
item->ComputePoints();
break;
default:
break;
}
}
//-----------------------------------------------------------------------------
void vtkControlPointsItem::ComputePoints()
{
if (this->BlockUpdates > 0)
{
return;
}
if (this->GetNumberOfPoints() == 0)
{
this->Selection->SetNumberOfTuples(0);
......@@ -992,6 +1016,7 @@ vtkIdType vtkControlPointsItem::MovePoint(vtkIdType pointId, const vtkVector2f&
void vtkControlPointsItem::MovePoints(const vtkVector2f& translation, vtkIdTypeArray* pointIds)
{
assert(pointIds);
this->StartChanges();
// don't support 'switch' mode yet
//vtkIdTypeArray* addedSelection = vtkIdTypeArray::New();
bool oldSwitchPoints = this->SwitchPointsMode;
......@@ -1027,9 +1052,9 @@ void vtkControlPointsItem::MovePoints(const vtkVector2f& translation, vtkIdTypeA
//this->SelectPoints(addedSelection);
this->SwitchPointsMode = oldSwitchPoints;
// end "don't support 'switch' mode yet"
this->EndChanges();
}
//-----------------------------------------------------------------------------
void vtkControlPointsItem::SpreadPoints(float factor, vtkIdTypeArray* pointIds)
{
......@@ -1038,6 +1063,8 @@ void vtkControlPointsItem::SpreadPoints(float factor, vtkIdTypeArray* pointIds)
{
return;
}
this->StartChanges();
double min[2], max[2], center[2];
double point[4];
vtkIdType minPointId = pointIds->GetValue(0);
......@@ -1116,6 +1143,7 @@ void vtkControlPointsItem::SpreadPoints(float factor, vtkIdTypeArray* pointIds)
vtkVector2f newPos(std::max(point[0] + tX, center[0]), point[1]);
this->SetPointPos(pointId, newPos);
}
this->EndChanges();
}
//-----------------------------------------------------------------------------
......
......@@ -226,6 +226,9 @@ protected:
vtkControlPointsItem();
virtual ~vtkControlPointsItem();
virtual void StartChanges()=0;
virtual void EndChanges()=0;
static void CallComputePoints(vtkObject* sender, unsigned long event, void* receiver, void* params);
// Description:
......@@ -274,10 +277,11 @@ protected:
virtual bool MouseButtonReleaseEvent(const vtkContextMouseEvent &mouse);
void AddPointId(vtkIdType addedPointId);
vtkCallbackCommand* Callback;
vtkPen* SelectedPointPen;
vtkBrush* SelectedPointBrush;
int BlockUpdates;
vtkIdType CurrentPoint;
double Bounds[4];
......
......@@ -66,6 +66,24 @@ void vtkPiecewiseControlPointsItem::PrintSelf(ostream &os, vtkIndent indent)
}
}
//-----------------------------------------------------------------------------
void vtkPiecewiseControlPointsItem::StartChanges()
{
if (this->PiecewiseFunction)
{
this->PiecewiseFunction->InvokeEvent(vtkCommand::StartEvent);
}
}
//-----------------------------------------------------------------------------
void vtkPiecewiseControlPointsItem::EndChanges()
{
if (this->PiecewiseFunction)
{
this->PiecewiseFunction->InvokeEvent(vtkCommand::EndEvent);
}
}
//-----------------------------------------------------------------------------
unsigned long int vtkPiecewiseControlPointsItem::GetControlPointsMTime()
{
......@@ -79,10 +97,16 @@ unsigned long int vtkPiecewiseControlPointsItem::GetControlPointsMTime()
//-----------------------------------------------------------------------------
void vtkPiecewiseControlPointsItem::SetPiecewiseFunction(vtkPiecewiseFunction* t)
{
if (t == this->PiecewiseFunction)
{
return;
}
vtkSetObjectBodyMacro(PiecewiseFunction, vtkPiecewiseFunction, t);
if (this->PiecewiseFunction)
{
this->PiecewiseFunction->AddObserver(vtkCommand::StartEvent, this->Callback);
this->PiecewiseFunction->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
this->PiecewiseFunction->AddObserver(vtkCommand::EndEvent, this->Callback);
}
this->ResetBounds();
this->ComputePoints();
......
......@@ -68,6 +68,9 @@ protected:
vtkPiecewiseControlPointsItem();
virtual ~vtkPiecewiseControlPointsItem();
virtual void StartChanges();
virtual void EndChanges();
virtual unsigned long int GetControlPointsMTime();
virtual vtkIdType GetNumberOfPoints()const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment