vtkColorTransferControlPointsItem.cxx 7.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkColorTransferControlPointsItem.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtkBrush.h"
#include "vtkCallbackCommand.h"
#include "vtkContext2D.h"
#include "vtkIdTypeArray.h"
#include "vtkColorTransferFunction.h"
#include "vtkColorTransferControlPointsItem.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPoints2D.h"
#include "vtkContextScene.h"

// to handle mouse.GetButton
#include "vtkContextMouseEvent.h"

#include <cassert>
#include <limits>
#include <algorithm>

//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkColorTransferControlPointsItem);

//-----------------------------------------------------------------------------
vtkColorTransferControlPointsItem::vtkColorTransferControlPointsItem()
{
40
  this->ColorTransferFunction = nullptr;
41 42 43 44 45 46 47
  this->ColorFill = false;
}

//-----------------------------------------------------------------------------
vtkColorTransferControlPointsItem::~vtkColorTransferControlPointsItem()
{
  if (this->ColorTransferFunction)
48
  {
49
    this->ColorTransferFunction->RemoveObserver(this->Callback);
50
    this->ColorTransferFunction->Delete();
51
    this->ColorTransferFunction = nullptr;
52
  }
53 54 55 56 57 58 59 60
}

//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "ColorTransferFunction: ";
  if (this->ColorTransferFunction)
61
  {
62 63
    os << endl;
    this->ColorTransferFunction->PrintSelf(os, indent.GetNextIndent());
64
  }
65
  else
66
  {
67
    os << "(none)" << endl;
68
  }
69 70
}

71
//-----------------------------------------------------------------------------
72
void vtkColorTransferControlPointsItem::emitEvent(unsigned long event, void* params)
73 74
{
  if (this->ColorTransferFunction)
75
  {
76
    this->ColorTransferFunction->InvokeEvent(event, params);
77
  }
78 79
}

80
//-----------------------------------------------------------------------------
81
vtkMTimeType vtkColorTransferControlPointsItem::GetControlPointsMTime()
82 83
{
  if (this->ColorTransferFunction)
84
  {
85
    return this->ColorTransferFunction->GetMTime();
86
  }
87 88 89 90 91 92
  return this->GetMTime();
}

//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::SetColorTransferFunction(vtkColorTransferFunction* t)
{
93
  if (t == this->ColorTransferFunction)
94
  {
95
    return;
96
  }
97
  if (this->ColorTransferFunction)
98
  {
99
    this->ColorTransferFunction->RemoveObserver(this->Callback);
100
  }
101 102
  vtkSetObjectBodyMacro(ColorTransferFunction, vtkColorTransferFunction, t);
  if (this->ColorTransferFunction)
103
  {
104
    this->ColorTransferFunction->AddObserver(vtkCommand::StartEvent, this->Callback);
105
    this->ColorTransferFunction->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
106
    this->ColorTransferFunction->AddObserver(vtkCommand::EndEvent, this->Callback);
107
  }
108
  this->ResetBounds();
109 110 111 112 113 114
  this->ComputePoints();
}

//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::DrawPoint(vtkContext2D* painter, vtkIdType index)
{
115
  assert(index != -1);
116
  if (this->ColorFill)
117
  {
118 119 120
    double xrgbms[6];
    this->ColorTransferFunction->GetNodeValue(index, xrgbms);
    painter->GetBrush()->SetColorF(xrgbms[1], xrgbms[2], xrgbms[3], 0.55);
121
  }
122 123 124 125
  this->vtkControlPointsItem::DrawPoint(painter, index);
}

//-----------------------------------------------------------------------------
126
vtkIdType vtkColorTransferControlPointsItem::GetNumberOfPoints()const
127
{
128 129
  return this->ColorTransferFunction ?
    static_cast<vtkIdType>(this->ColorTransferFunction->GetSize()) : 0;
130 131 132
}

//-----------------------------------------------------------------------------
133
void vtkColorTransferControlPointsItem::GetControlPoint(vtkIdType index, double* pos)const
134 135
{
  double xrgbms[6];
136 137
  vtkColorTransferFunction* thisTF = const_cast<vtkColorTransferFunction*>(
    this->ColorTransferFunction);
138 139

  if (thisTF)
140
  {
141 142 143 144 145
    thisTF->GetNodeValue(index, xrgbms);
    pos[0] = xrgbms[0];
    pos[1] = 0.5;
    pos[2] = xrgbms[4];
    pos[3] = xrgbms[5];
146
  }
147 148 149 150 151 152 153 154 155 156
}

//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::SetControlPoint(vtkIdType index, double* newPos)
{
  double xrgbms[6];
  this->ColorTransferFunction->GetNodeValue(index, xrgbms);
  if (newPos[0] != xrgbms[0] ||
      newPos[2] != xrgbms[1] ||
      newPos[3] != xrgbms[2])
157
  {
158 159 160
    xrgbms[0] = newPos[0];
    xrgbms[4] = newPos[2];
    xrgbms[5] = newPos[3];
161
    this->StartChanges();
162
    this->ColorTransferFunction->SetNodeValue(index, xrgbms);
163
    this->EndChanges();
164
  }
165 166 167 168 169 170
}

//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::EditPoint(float tX, float tY)
{
  if (!this->ColorTransferFunction)
171
  {
172
    return;
173
  }
174 175 176

  this->StartChanges();

177 178 179 180 181 182
  double xrgbms[6];
  this->ColorTransferFunction->GetNodeValue(this->CurrentPoint, xrgbms);
  xrgbms[4] += tX;
  xrgbms[5] += tY;
  this->ColorTransferFunction->SetNodeValue(this->CurrentPoint, xrgbms);
  if (this->CurrentPoint > 0)
183
  {
184 185 186 187
    this->ColorTransferFunction->GetNodeValue(this->CurrentPoint - 1, xrgbms);
    xrgbms[4] += tX;
    xrgbms[5] += tY;
    this->ColorTransferFunction->SetNodeValue(this->CurrentPoint - 1, xrgbms);
188
  }
189 190

  this->EndChanges();
191 192 193 194 195 196
}

//-----------------------------------------------------------------------------
vtkIdType vtkColorTransferControlPointsItem::AddPoint(double* newPos)
{
  if (!this->ColorTransferFunction)
197
  {
198
    return -1;
199
  }
200

201 202
  this->StartChanges();

203
  double posX = newPos[0];
204
  double rgb[3] = {0., 0., 0.};
205
  this->ColorTransferFunction->GetColor(posX, rgb);
206
  vtkIdType addedPoint =
207
    this->ColorTransferFunction->AddRGBPoint(posX, rgb[0], rgb[1], rgb[2]);
208
  this->vtkControlPointsItem::AddPointId(addedPoint);
209 210

  this->EndChanges();
211 212 213 214 215 216
  return addedPoint;
}

//-----------------------------------------------------------------------------
vtkIdType vtkColorTransferControlPointsItem::RemovePoint(double* currentPoint)
{
217 218
  if (!this->ColorTransferFunction || !this->IsPointRemovable(
    this->GetControlPointId(currentPoint)))
219
  {
220
    return -1;
221
  }
222 223 224

  this->StartChanges();

225
#ifndef NDEBUG
226
  vtkIdType expectedPoint =
227
#endif
228 229 230
    this->vtkControlPointsItem::RemovePoint(currentPoint);
  vtkIdType removedPoint =
    this->ColorTransferFunction->RemovePoint(currentPoint[0]);
231
  assert(removedPoint == expectedPoint);
232 233

  this->EndChanges();
234 235
  return removedPoint;
}
236 237 238 239 240

//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::ComputeBounds(double* bounds)
{
  if (this->ColorTransferFunction)
241
  {
242
    this->ColorTransferFunction->GetRange(bounds);
243 244
    bounds[2] = 0.5;
    bounds[3] = 0.5;
245 246 247

    this->TransformDataToScreen(bounds[0], bounds[2], bounds[0], bounds[2]);
    this->TransformDataToScreen(bounds[1], bounds[3], bounds[1], bounds[3]);
248
  }
249
  else
250
  {
251
    this->Superclass::ComputeBounds(bounds);
252
  }
253
}