vtkColorTransferControlPointsItem.cxx 7.62 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 40 41 42 43 44 45 46 47 48
/*=========================================================================

  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()
{
  this->ColorTransferFunction = 0;
  this->ColorFill = false;
}

//-----------------------------------------------------------------------------
vtkColorTransferControlPointsItem::~vtkColorTransferControlPointsItem()
{
  if (this->ColorTransferFunction)
    {
49
    this->ColorTransferFunction->RemoveObserver(this->Callback);
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    this->ColorTransferFunction->Delete();
    this->ColorTransferFunction = 0;
    }
}

//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "ColorTransferFunction: ";
  if (this->ColorTransferFunction)
    {
    os << endl;
    this->ColorTransferFunction->PrintSelf(os, indent.GetNextIndent());
    }
  else
    {
    os << "(none)" << endl;
    }
}

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

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

//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::SetColorTransferFunction(vtkColorTransferFunction* t)
{
93 94 95 96
  if (t == this->ColorTransferFunction)
    {
    return;
    }
97 98 99 100
  if (this->ColorTransferFunction)
    {
    this->ColorTransferFunction->RemoveObserver(this->Callback);
    }
101 102 103
  vtkSetObjectBodyMacro(ColorTransferFunction, vtkColorTransferFunction, t);
  if (this->ColorTransferFunction)
    {
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 117 118 119 120 121 122 123 124 125
  if (this->ColorFill)
    {
    double xrgbms[6];
    this->ColorTransferFunction->GetNodeValue(index, xrgbms);
    painter->GetBrush()->SetColorF(xrgbms[1], xrgbms[2], xrgbms[3], 0.55);
    }
  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 146
    {
    thisTF->GetNodeValue(index, xrgbms);
    pos[0] = xrgbms[0];
    pos[1] = 0.5;
    pos[2] = xrgbms[4];
    pos[3] = xrgbms[5];
    }
147 148 149 150 151 152 153 154 155 156 157 158 159 160
}

//-----------------------------------------------------------------------------
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])
    {
    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 171 172 173
    }
}

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

  this->StartChanges();

177 178 179 180 181 182 183 184 185 186 187 188
  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)
    {
    this->ColorTransferFunction->GetNodeValue(this->CurrentPoint - 1, xrgbms);
    xrgbms[4] += tX;
    xrgbms[5] += tY;
    this->ColorTransferFunction->SetNodeValue(this->CurrentPoint - 1, xrgbms);
    }
189 190

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

//-----------------------------------------------------------------------------
vtkIdType vtkColorTransferControlPointsItem::AddPoint(double* newPos)
{
  if (!this->ColorTransferFunction)
    {
    return -1;
    }
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 221
    {
    return -1;
    }
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

//-----------------------------------------------------------------------------
void vtkColorTransferControlPointsItem::ComputeBounds(double* bounds)
{
  if (this->ColorTransferFunction)
    {
    const double* range = this->ColorTransferFunction->GetRange();
    bounds[0] = range[0];
    bounds[1] = range[1];
    bounds[2] = 0.5;
    bounds[3] = 0.5;
    }
  else
    {
    this->Superclass::ComputeBounds(bounds);
    }
}

//-----------------------------------------------------------------------------
bool vtkColorTransferControlPointsItem::UsingLogScale()
{
  return (this->ColorTransferFunction?
    (this->ColorTransferFunction->UsingLogScale() != 0) : false);
}