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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkColorLegend.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 "vtkColorLegend.h"
#include "vtkAxis.h"
#include "vtkBrush.h"
#include "vtkCallbackCommand.h"
#include "vtkContext2D.h"
#include "vtkContextScene.h"
#include "vtkFloatArray.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPoints2D.h"
#include "vtkSmartPointer.h"
#include "vtkTransform2D.h"
#include "vtkScalarsToColors.h"
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkColorLegend);
//-----------------------------------------------------------------------------
vtkColorLegend::vtkColorLegend()
{
this->Interpolate = true;
this->Axis = vtkSmartPointer<vtkAxis>::New();
this->Axis->SetPosition(vtkAxis::RIGHT);
this->AddItem(this->Axis);
this->SetInline(false);
this->SetHorizontalAlignment(vtkChartLegend::RIGHT);
this->SetVerticalAlignment(vtkChartLegend::BOTTOM);
this->Callback = vtkSmartPointer<vtkCallbackCommand>::New();
this->Callback->SetClientData(this);
this->Callback->SetCallback(vtkColorLegend::OnScalarsToColorsModified);
this->TransferFunction = NULL;
this->Orientation = vtkColorLegend::VERTICAL;
this->Position.Set(0.0, 0.0, 0.0, 0.0);
this->CustomPositionSet = false;
this->DrawBorder = false;
}
//-----------------------------------------------------------------------------
vtkColorLegend::~vtkColorLegend()
{
}
//-----------------------------------------------------------------------------
void vtkColorLegend::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Interpolate: " << this->Interpolate << endl;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::GetBounds(double bounds[4])
{
if (this->TransferFunction)
{
bounds[0] = this->TransferFunction->GetRange()[0];
bounds[1] = this->TransferFunction->GetRange()[1];
}
else
{
bounds[0] = 0.0;
bounds[1] = 1.0;
}
bounds[2] = 0.0;
bounds[3] = 1.0;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::Update()
{
if (this->ImageData == 0 ||
this->ImageData->GetMTime() < this->GetMTime())
{
this->ComputeTexture();
}
// check if the range of our TransferFunction changed
double bounds[4];
this->GetBounds(bounds);
if (bounds[0] == bounds[1])
{
vtkWarningMacro(<< "The color transfer function seems to be empty.");
this->Axis->Update();
return;
}
double axisBounds[2];
this->Axis->GetUnscaledRange(axisBounds);
if (bounds[0] != axisBounds[0] || bounds[1] != axisBounds[1])
{
this->Axis->SetUnscaledRange(bounds[0], bounds[1]);
}
this->Axis->Update();
}
//-----------------------------------------------------------------------------
bool vtkColorLegend::Paint(vtkContext2D* painter)
{
if (this->TransferFunction == NULL)
{
return true;
}
this->GetBoundingRect(painter);
if (this->DrawBorder)
{
// Draw a box around the legend.
painter->ApplyPen(this->Pen.GetPointer());
painter->ApplyBrush(this->Brush.GetPointer());
painter->DrawRect(this->Rect.GetX(), this->Rect.GetY(),
this->Rect.GetWidth(), this->Rect.GetHeight());
}
painter->DrawImage(this->Position, this->ImageData);
this->Axis->Paint(painter);
return true;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::SetTransferFunction(vtkScalarsToColors* transfer)
{
this->TransferFunction = transfer;
}
//-----------------------------------------------------------------------------
vtkScalarsToColors * vtkColorLegend::GetTransferFunction()
{
return this->TransferFunction;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::SetPoint(float x, float y)
{
this->Superclass::SetPoint(x, y);
this->CustomPositionSet = false;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::SetTextureSize(float w, float h)
{
this->Position.SetWidth(w);
this->Position.SetHeight(h);
this->CustomPositionSet = false;
this->Modified();
}
//-----------------------------------------------------------------------------
void vtkColorLegend::SetPosition(const vtkRectf& pos)
{
this->Position = pos;
this->SetPoint(pos[0], pos[1]);
this->UpdateAxisPosition();
this->CustomPositionSet = true;
}
//-----------------------------------------------------------------------------
vtkRectf vtkColorLegend::GetPosition()
{
return this->Position;
}
//-----------------------------------------------------------------------------
vtkRectf vtkColorLegend::GetBoundingRect(vtkContext2D *painter)
{
if (this->CacheBounds && this->RectTime > this->GetMTime() &&
this->RectTime > this->PlotTime &&
this->RectTime > this->Axis->GetMTime())
{
return this->Rect;
}
if (!this->CustomPositionSet)
{
// if the Position ivar was not explicitly set, we compute the
// location of the lower left point of the legend here.
float posX = floor(this->Point[0]);
float posY = floor(this->Point[1]);
float posW = this->Position.GetWidth();
float posH = this->Position.GetHeight();
if (this->Orientation == vtkColorLegend::VERTICAL)
{
// For vertical orientation, we need to move our anchor point
// further to the left to accommodate the width of the axis.
// To do this, we query our axis to get its preliminary bounds.
// Even though its position has not yet been set, its width &
// height should still be accurate.
this->UpdateAxisPosition();
this->Axis->Update();
vtkRectf axisRect = this->Axis->GetBoundingRect(painter);
posX -= axisRect.GetWidth();
}
// Compute bottom left point based on current alignment.
if (this->HorizontalAlignment == vtkChartLegend::CENTER)
{
posX -= posW / 2.0;
}
else if (this->HorizontalAlignment == vtkChartLegend::RIGHT)
{
posX -= posW;
}
if (this->VerticalAlignment == vtkChartLegend::CENTER)
{
posY -= posH / 2.0;
}
else if (this->VerticalAlignment == vtkChartLegend::TOP)
{
posY -= posH;
}
this->Position.SetX(posX);
this->Position.SetY(posY);
this->UpdateAxisPosition();
}
this->Axis->Update();
vtkRectf axisRect = this->Axis->GetBoundingRect(painter);
if (this->Orientation == vtkColorLegend::HORIZONTAL)
{
// "+ 1" so the texture doesn't obscure the border
this->Rect = vtkRectf(this->Position.GetX(),
this->Position.GetY() - axisRect.GetHeight() + 1,
this->Position.GetWidth() + 1,
this->Position.GetHeight() + axisRect.GetHeight());
}
else
{
this->Rect = vtkRectf(this->Position.GetX(),
this->Position.GetY(),
this->Position.GetWidth() + axisRect.GetWidth(),
this->Position.GetHeight());
}
this->RectTime.Modified();
return this->Rect;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::ComputeTexture()
{
if (this->TransferFunction == NULL)
{
return;
}
if (!this->ImageData)
{
this->ImageData = vtkSmartPointer<vtkImageData>::New();
}
double bounds[4];
this->GetBounds(bounds);
if (bounds[0] == bounds[1])
{
vtkWarningMacro(<< "The color transfer function seems to be empty.");
return;
}
// Set the axis up
this->Axis->SetUnscaledRange(bounds[0], bounds[1]);
//this->Axis->AutoScale();
// Could depend on the screen resolution
const int dimension = 256;
double* values = new double[dimension];
// Texture 1D
if (this->Orientation == vtkColorLegend::VERTICAL)
{
this->ImageData->SetExtent(0, 0,
0, dimension-1,
0, 0);
}
else
{
this->ImageData->SetExtent(0, dimension-1,
0, 0,
0, 0);
}
this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
for (int i = 0; i < dimension; ++i)
{
values[i] = bounds[0] + i * (bounds[1] - bounds[0]) / (dimension - 1);
}
unsigned char* ptr =
reinterpret_cast<unsigned char*>(this->ImageData->GetScalarPointer());
this->TransferFunction->MapScalarsThroughTable2(
values, ptr, VTK_DOUBLE, dimension, 1, 3);
delete [] values;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::OnScalarsToColorsModified(vtkObject* caller,
unsigned long eid,
void *clientdata,
void* calldata)
{
vtkColorLegend* self =
reinterpret_cast<vtkColorLegend*>(clientdata);
self->ScalarsToColorsModified(caller, eid, calldata);
}
//-----------------------------------------------------------------------------
void vtkColorLegend::ScalarsToColorsModified(vtkObject* vtkNotUsed(object),
unsigned long vtkNotUsed(eid),
void* vtkNotUsed(calldata))
{
this->Modified();
}
//-----------------------------------------------------------------------------
void vtkColorLegend::SetOrientation(int orientation)
{
if (orientation < 0 || orientation > 1)
{
vtkErrorMacro("Error, invalid orientation value supplied: " << orientation)
return;
}
this->Orientation = orientation;
if (this->Orientation == vtkColorLegend::HORIZONTAL)
{
this->Axis->SetPosition(vtkAxis::BOTTOM);
}
}
//-----------------------------------------------------------------------------
void vtkColorLegend::SetTitle(const vtkStdString &title)
{
this->Axis->SetTitle(title);
}
//-----------------------------------------------------------------------------
vtkStdString vtkColorLegend::GetTitle()
{
return this->Axis->GetTitle();
}
//-----------------------------------------------------------------------------
void vtkColorLegend::UpdateAxisPosition()
{
if (this->Orientation == vtkColorLegend::VERTICAL)
{
this->Axis->SetPoint1(
vtkVector2f(this->Position.GetX() + this->Position.GetWidth(),
this->Position.GetY()));
this->Axis->SetPoint2(
vtkVector2f(this->Position.GetX() + this->Position.GetWidth(),
this->Position.GetY() + this->Position.GetHeight()));
}
else
{
this->Axis->SetPoint1(
vtkVector2f(this->Position.GetX(), this->Position.GetY()));
this->Axis->SetPoint2(
vtkVector2f(this->Position.GetX() + this->Position.GetWidth(),
this->Position.GetY()));
}
}
//-----------------------------------------------------------------------------
bool vtkColorLegend::MouseMoveEvent(const vtkContextMouseEvent &mouse)
{
bool retval = this->Superclass::MouseMoveEvent(mouse);
this->Position[0] = this->Point[0];
this->Position[1] = this->Point[1];
this->UpdateAxisPosition();
return retval;
}
|