vtkControlPointsItem.cxx 54.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkControlPointsItem.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.

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

16
#include "vtkAxis.h"
17 18 19
#include "vtkBrush.h"
#include "vtkCallbackCommand.h"
#include "vtkContext2D.h"
20
#include "vtkContextDevice2D.h"
21
#include "vtkContextKeyEvent.h"
22
#include "vtkContextMouseEvent.h"
23
#include "vtkContextScene.h"
24
#include "vtkControlPointsItem.h"
25
#include "vtkIdTypeArray.h"
26
#include "vtkNew.h"
27 28 29
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPoints2D.h"
30
#include "vtkRenderWindowInteractor.h"
31 32
#include "vtkSmartPointer.h"
#include "vtkTransform2D.h"
33
#include "vtkVectorOperators.h"
34

35
#include <algorithm>
36
#include <cassert>
37
#include <limits>
38 39 40 41

//-----------------------------------------------------------------------------
vtkControlPointsItem::vtkControlPointsItem()
{
42
  this->Pen->SetLineType(vtkPen::SOLID_LINE);
43 44 45 46 47 48 49 50 51 52 53
  this->Pen->SetWidth(2.);
  this->Pen->SetColor(140, 144, 125, 200);
  this->Brush->SetColor(125, 135, 144, 200);

  this->SelectedPointPen = vtkPen::New();
  this->SelectedPointPen->SetWidth(2.);
  // 98, 140, 178
  this->SelectedPointPen->SetColor(63, 90, 115, 200);
  //this->SelectedPointPen->SetColor(98, 140, 178, 200);
  this->SelectedPointBrush = vtkBrush::New();
  this->SelectedPointBrush->SetColor(58, 121, 178, 200);
54

55
  this->Selection = vtkIdTypeArray::New();
56 57
  this->CurrentPoint = -1;

58
  this->BlockUpdates = 0;
59
  this->StartedInteractions = 0;
60
  this->StartedChanges = 0;
61

62 63 64 65
  this->Callback = vtkCallbackCommand::New();
  this->Callback->SetClientData(this);
  this->Callback->SetCallback(
    vtkControlPointsItem::CallComputePoints);
66 67 68 69 70

  this->Bounds[0] = this->Bounds[2] = 0.;
  this->Bounds[1] = this->Bounds[3] = -1.;
  this->UserBounds[0] = this->UserBounds[2] = 0.;
  this->UserBounds[1] = this->UserBounds[3] = -1.;
71 72
  this->ValidBounds[0] = this->ValidBounds[2] = 0.;
  this->ValidBounds[1] = this->ValidBounds[3] = -1.;
73

74
  this->ScreenPointRadius = 6.f;
75 76 77 78 79 80 81 82 83 84
  this->Transform = vtkTransform2D::New();

  this->StrokeMode = false;
  this->SwitchPointsMode = false;
  this->MouseMoved = false;
  this->EnforceValidFunction = true;
  this->PointToDelete = -1;
  this->PointAboutToBeDeleted = false;
  this->PointToToggle = -1;
  this->PointAboutToBeToggled = false;
85
  this->InvertShadow = false;
86 87
  this->EndPointsXMovable = true;
  this->EndPointsYMovable = true;
88
  this->EndPointsRemovable = true;
89 90 91
  this->ShowLabels = false;
  this->LabelFormat = NULL;
  this->SetLabelFormat("%.3f, %.3f");
92 93 94 95 96
}

//-----------------------------------------------------------------------------
vtkControlPointsItem::~vtkControlPointsItem()
{
97
  this->SetLabelFormat(NULL);
98 99 100 101 102
  if (this->Callback)
    {
    this->Callback->Delete();
    this->Callback = 0;
    }
103 104 105 106 107 108 109 110 111 112
  if (this->SelectedPointPen)
    {
    this->SelectedPointPen->Delete();
    this->SelectedPointPen = 0;
    }
  if (this->SelectedPointBrush)
    {
    this->SelectedPointBrush->Delete();
    this->SelectedPointBrush = 0;
    }
113 114 115 116 117
  if (this->Transform)
    {
    this->Transform->Delete();
    this->Transform = 0;
    }
118 119 120 121 122 123
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
124 125 126 127

  os << indent << "EndPointsXMovable: " << this->EndPointsXMovable << endl;
  os << indent << "EndPointsYMovable: " << this->EndPointsYMovable << endl;
  os << indent << "EndPointsRemovable: " << this->EndPointsRemovable << endl;
128
  os << indent << "ShowLabels: " << this->ShowLabels << endl;
129 130
}

131 132 133
//-----------------------------------------------------------------------------
void vtkControlPointsItem::GetBounds(double bounds[4])
{
134
  // valid user bounds ? use them
135 136
  if (this->UserBounds[0] <= this->UserBounds[1] &&
      this->UserBounds[2] <= this->UserBounds[3])
137
    {
138 139 140 141 142 143
    bounds[0] = this->UserBounds[0];
    bounds[1] = this->UserBounds[1];
    bounds[2] = this->UserBounds[2];
    bounds[3] = this->UserBounds[3];
    return;
    }
144 145 146
  // invalid bounds ? compute them
  if (!(this->Bounds[0] <= this->Bounds[1] &&
        this->Bounds[2] > this->Bounds[3]))
147 148
    {
    this->ComputeBounds();
149 150 151 152 153
    }
  bounds[0] = this->Bounds[0];
  bounds[1] = this->Bounds[1];
  bounds[2] = this->Bounds[2];
  bounds[3] = this->Bounds[3];
154 155
}

156 157 158 159 160 161 162 163 164 165 166 167
//-----------------------------------------------------------------------------
void vtkControlPointsItem::ResetBounds()
{
  this->Bounds[0] = 0.;
  this->Bounds[1] = -1.;
  this->Bounds[2] = 0.;
  this->Bounds[3] = -1.;
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::ComputeBounds()
{
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  double oldBounds[4];
  oldBounds[0] = this->Bounds[0];
  oldBounds[1] = this->Bounds[1];
  oldBounds[2] = this->Bounds[2];
  oldBounds[3] = this->Bounds[3];

  this->ComputeBounds(this->Bounds);

  if (this->Bounds[0] != oldBounds[0] ||
      this->Bounds[1] != oldBounds[1] ||
      this->Bounds[2] != oldBounds[2] ||
      this->Bounds[3] != oldBounds[3])
    {
    this->Modified();
    }
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::ComputeBounds( double* bounds)
{
  bounds[0] = bounds[2] =  VTK_DOUBLE_MAX;
  bounds[1] = bounds[3] = -VTK_DOUBLE_MAX;
190 191 192 193
  for (vtkIdType i=0; i < this->GetNumberOfPoints(); ++i)
    {
    double point[4];
    this->GetControlPoint(i, point);
194 195 196 197
    bounds[0] = std::min(bounds[0], point[0]);
    bounds[1] = std::max(bounds[1], point[0]);
    bounds[2] = std::min(bounds[2], point[1]);
    bounds[3] = std::max(bounds[3], point[1]);
198 199 200
    }
}

201 202 203
//-----------------------------------------------------------------------------
bool vtkControlPointsItem::Paint(vtkContext2D* painter)
{
204
  painter->GetDevice()->EnableClipping(false);
205 206
  painter->ApplyPen(this->Pen);
  painter->ApplyBrush(this->Brush);
207
  this->InvertShadow = false;
208 209 210
  this->DrawUnselectedPoints(painter);

  painter->GetPen()->SetLineType(vtkPen::SOLID_LINE);
211 212 213 214 215 216
  //painter->GetPen()->SetColorF(0.87, 0.87, 1.);
  //painter->GetBrush()->SetColorF(0.75, 0.75, 0.95, 0.65);
  //float oldPenWidth = painter->GetPen()->GetWidth();
  painter->ApplyPen(this->SelectedPointPen);
  painter->ApplyBrush(this->SelectedPointBrush);
  this->InvertShadow = true;
217
  float oldScreenPointRadius = this->ScreenPointRadius;
218
  if (this->Selection && this->Selection->GetNumberOfTuples())
219
    {
220 221
    //painter->GetPen()->SetWidth(oldPenWidth * 1.4);
    //this->ScreenPointRadius = oldScreenPointRadius * 1.1;
222 223 224 225 226
    this->DrawSelectedPoints(painter);
    }
  this->ScreenPointRadius = oldScreenPointRadius;
  this->Transform->SetMatrix(painter->GetTransform()->GetMatrix());

227
  painter->GetDevice()->EnableClipping(true);
228
  this->PaintChildren(painter);
229
  return true;
230 231
}

232 233 234
//-----------------------------------------------------------------------------
void vtkControlPointsItem::StartChanges()
{
235 236 237 238 239 240
  ++this->StartedChanges;
  if (this->StartedChanges == 1)
    {
    this->InvokeEvent(vtkCommand::StartEvent);
    this->emitEvent(vtkCommand::StartEvent);
    }
241 242 243 244 245
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::EndChanges()
{
246 247 248 249 250 251 252
  --this->StartedChanges;
  assert(this->StartedChanges >=0);
  if (this->StartedChanges == 0)
    {
    this->emitEvent(vtkCommand::EndEvent);
    this->InvokeEvent(vtkCommand::EndEvent);
    }
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
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::StartInteraction()
{
  ++this->StartedInteractions;
  this->emitEvent(vtkCommand::StartInteractionEvent);
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::StartInteractionIfNotStarted()
{
  if (this->GetInteractionsCount() == 0)
    {
    this->StartInteraction();
    }
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::Interaction()
{
  assert(this->StartedInteractions > 0);
  this->emitEvent(vtkCommand::InteractionEvent);
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::EndInteraction()
{
  --this->StartedInteractions;
  assert(this->StartedInteractions >= 0);
  this->emitEvent(vtkCommand::EndInteractionEvent);
}

//-----------------------------------------------------------------------------
int vtkControlPointsItem::GetInteractionsCount()const
{
  return this->StartedInteractions;
}

//-----------------------------------------------------------------------------
//void vtkControlPointsItem::emitEvent(unsigned long event, void* params);

295 296
//-----------------------------------------------------------------------------
void vtkControlPointsItem::CallComputePoints(
297
  vtkObject* vtkNotUsed(sender), unsigned long event,
298 299 300 301
  void* receiver, void* vtkNotUsed(params))
{
  vtkControlPointsItem* item =
    reinterpret_cast<vtkControlPointsItem*>(receiver);
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  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;
    }
320 321 322 323 324
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::ComputePoints()
{
325 326 327 328 329
  if (this->BlockUpdates > 0)
    {
    return;
    }

330
  if (this->Selection && this->GetNumberOfPoints() == 0)
331 332 333 334
    {
    this->Selection->SetNumberOfTuples(0);
    }

335 336
  const int selectedPointCount = this->Selection ?
        this->Selection->GetNumberOfTuples() : 0;
337 338 339 340 341 342
  if (selectedPointCount)
    {
    vtkIdTypeArray* oldSelection = this->Selection;
    this->Selection = vtkIdTypeArray::New();
    for (vtkIdType i = 0; i < selectedPointCount; ++i)
      {
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
343 344 345 346
      if (oldSelection->GetValue(i) < this->GetNumberOfPoints())
        {
        this->SelectPoint(oldSelection->GetValue(i));
        }
347 348 349 350
      }
    oldSelection->Delete();
    }

351 352 353 354
  if (this->GetScene())
    {
    this->GetScene()->SetDirty(true);
    }
355
  this->Modified();
356 357
}

358 359 360 361
//-----------------------------------------------------------------------------
void vtkControlPointsItem::TransformScreenToData(const vtkVector2f& in, vtkVector2f& out)
{
  out = in;
362 363 364 365 366 367

  // inverse shift/scale from screen space.
  const vtkRectd& ss = this->ShiftScale;
  out.SetX(static_cast<float>((out.GetX() / ss[2]) - ss[0]));
  out.SetY(static_cast<float>((out.GetY() / ss[3]) - ss[1]));

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 394 395 396
  if (this->UsingLogScale())
    {
    // using log scale.
    double bounds[4];
    this->ComputeBounds(bounds);

    double posX = in.GetX();
    double normVal = (posX - bounds[0])/(bounds[1] - bounds[0]);
    double lval = log10(bounds[0]) + normVal*(log10(bounds[1]) - log10(bounds[0]));
    posX = pow(10.0, lval);
    out.SetX(posX);
    }
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::TransformDataToScreen(const vtkVector2f& in, vtkVector2f& out)
{
  out = in;
  if (this->UsingLogScale())
    {
    double bounds[4];
    this->ComputeBounds(bounds);

    double posX = in.GetX();
    double lnormVal = (log10(posX) - log10(bounds[0])) /
                      (log10(bounds[1]) - log10(bounds[0]));
    posX = bounds[0] + lnormVal * (bounds[1] - bounds[0]);
    out.SetX(posX);
    }
397 398 399 400 401

  // now, shift/scale to screen space.
  const vtkRectd& ss = this->ShiftScale;
  out.SetX(static_cast<float>((out.GetX() + ss[0]) * ss[2]));
  out.SetY(static_cast<float>((out.GetY() + ss[1]) * ss[3]));
402 403
}

404
//-----------------------------------------------------------------------------
405
bool vtkControlPointsItem::Hit(const vtkContextMouseEvent &mouse)
406
{
407 408
  vtkVector2f vpos = mouse.GetPos();
  this->TransformScreenToData(vpos, vpos);
409
  double pos[2];
410 411
  pos[0] = vpos.GetX();
  pos[1] = vpos.GetY();
412 413 414
  double bounds[4];
  this->GetBounds(bounds);
  bool clamped = this->ClampPos(pos, bounds);
415 416 417 418 419 420
  if (!clamped)
    {
    return true;
    }
  // maybe the cursor is over the first or last point (which could be outside
  // the bounds because of the screen point size).
421 422
  pos[0] = vpos.GetX();
  pos[1] = vpos.GetY();
423
  for (int i = 0; i < this->GetNumberOfPoints(); ++i)
424
    {
425 426 427 428
    if (this->IsOverPoint(pos, i))
      {
      return true;
      }
429 430 431 432 433
    }
  return false;
}

//-----------------------------------------------------------------------------
434
bool vtkControlPointsItem::ClampPos(double pos[2], double bounds[4])
435
{
436 437 438 439 440
  if (bounds[1] < bounds[0] || bounds[3] < bounds[2])
    {
    // bounds are not valid. Don't clamp.
    return false;
    }
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
  bool clamped = false;
  if (pos[0] < bounds[0])
    {
    pos[0] = bounds[0];
    clamped = true;
    }
  if (pos[0] > bounds[1])
    {
    pos[0] = bounds[1];
    clamped = true;
    }
  if (pos[1] < 0.)
    {
    pos[1] = 0.;
    clamped = true;
    }
  if (pos[1] > 1.)
    {
    pos[1] = 1.;
    clamped = true;
    }
  return clamped;
463
}
464

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
//-----------------------------------------------------------------------------
bool vtkControlPointsItem::ClampValidPos(double pos[2])
{
  double validBounds[4];
  this->GetValidBounds(validBounds);
  if (validBounds[0] > this->ValidBounds[1] ||
      validBounds[2] > this->ValidBounds[3])
    {
    double bounds[4];
    this->GetBounds(bounds);
    return this->ClampPos(pos, bounds);
    }
  return this->ClampPos(pos, validBounds);
}

480 481 482 483
//-----------------------------------------------------------------------------
void vtkControlPointsItem::DrawUnselectedPoints(vtkContext2D* painter)
{
  const int count = this->GetNumberOfPoints();
484
  for (vtkIdType i = 0; i < count; ++i)
485
    {
486
    vtkIdType idx = this->Selection ? this->Selection->LookupValue(i) : -1;
487 488 489 490
    if (idx != -1)
      {
      continue;
      }
491 492 493 494 495 496 497 498 499 500 501
    this->DrawPoint(painter, i);
    }
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::DrawSelectedPoints(vtkContext2D* painter)
{
  const int count = this->Selection ? this->Selection->GetNumberOfTuples() : 0;
  for (vtkIdType i = 0; i < count; ++i)
    {
    vtkIdType index = this->Selection->GetValue(i);
502
    assert(index != -1);
503
    this->DrawPoint(painter, index);
504 505
    }
}
506

507 508 509
//-----------------------------------------------------------------------------
void vtkControlPointsItem::DrawPoint(vtkContext2D* painter, vtkIdType index)
{
510
  assert(index != -1);
511 512 513
  double point[4];
  this->GetControlPoint(index, point);

514 515 516 517 518
  vtkVector2f vpoint(point[0], point[1]);
  this->TransformDataToScreen(vpoint, vpoint);
  point[0] = vpoint.GetX();
  point[1] = vpoint.GetY();

519 520 521 522 523 524 525 526 527 528
  double pointInScene[2];
  vtkTransform2D* sceneTransform = painter->GetTransform();
  sceneTransform->TransformPoints(point, pointInScene, 1);

  vtkSmartPointer<vtkTransform2D> translation =
    vtkSmartPointer<vtkTransform2D>::New();
  translation->Translate(pointInScene[0], pointInScene[1]);

  painter->PushMatrix();
  painter->SetTransform(translation);
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

  unsigned char brushOpacity = painter->GetBrush()->GetOpacity();
  unsigned char penColor[3];
  painter->GetPen()->GetColor(penColor);
  unsigned char penOpacity = painter->GetPen()->GetOpacity();
  //float width = painter->GetPen()->GetWidth();

  float radius = this->ScreenPointRadius;
  bool invertShadow = this->InvertShadow;
  unsigned char color[3] = {penColor[0], penColor[1], penColor[2]};

  if (this->PointToToggle == index && this->PointAboutToBeToggled)
    {
    invertShadow = !invertShadow;
    }
  if (this->PointToDelete == index && this->PointAboutToBeDeleted)
    {
    invertShadow = !invertShadow;
    color[0] = 255;
    color[1] = 0;
    color[2] = 0;
    }
  if (this->CurrentPoint == index)
552
    {
553
    radius = this->ScreenPointRadius * 1.3;
554 555 556
    color[0] = 255;
    color[1] = 0;
    color[2] = 255;
557
    }
558 559 560 561 562 563 564 565 566 567 568 569

  painter->GetPen()->SetColor(color);
  painter->DrawArc(0.f, 0.f, radius, 0.f, 360.f);

  painter->GetBrush()->SetOpacity(0);
  //painter->GetPen()->SetWidth(2.0f);

  unsigned char lightPenColor[4];
  lightPenColor[0] = std::min(color[0] + 100, 255);
  lightPenColor[1] = std::min(color[1] + 100, 255);
  lightPenColor[2] = std::min(color[2] + 100, 255);
  lightPenColor[3] = penOpacity;
570

571 572 573 574 575 576 577 578 579 580
  unsigned char darkPenColor[4];
  darkPenColor[0] = std::max(color[0] - 50, 0);
  darkPenColor[1] = std::max(color[1] - 50, 0);
  darkPenColor[2] = std::max(color[2] - 50, 0);
  darkPenColor[3] = penOpacity;

  painter->GetPen()->SetColor(invertShadow ? lightPenColor : darkPenColor);
  painter->DrawArc(0.f, 0.f, radius - 1.0, 200.f, 380.f);
  painter->GetPen()->SetColor(invertShadow ? darkPenColor : lightPenColor);
  painter->DrawArc(0.f, 0.f, radius - 1.0, 20.f, 200.f);
581

582 583 584 585 586 587 588 589
  painter->GetPen()->SetColor(color);
  if (this->PointToDelete == index && this->PointAboutToBeDeleted)
    {
    painter->DrawLine(-radius, -radius, radius, radius);
    painter->DrawLine(-radius, radius, radius, -radius);
    }

  painter->GetPen()->SetColor(penColor);
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

  if (this->ShowLabels && (/*index == 0 ||
                           index == this->GetNumberOfPoints()-1 || */
                           this->GetCurrentPoint() == index))
    {
    translation->Translate(0, radius+5);
    painter->SetTransform(translation);
    vtkStdString label = this->GetControlPointLabel(index);

    vtkVector2f bounds[2];
    painter->ComputeStringBounds(label, bounds[0].GetData());
    if (bounds[1].GetX() != 0.0f && bounds[1].GetY() != 0.0f)
      {
      float scale[2];
      float position[2];
      painter->GetTransform()->GetScale(scale);
      painter->GetTransform()->GetPosition(position);

      double brushColor[4];
      painter->GetBrush()->GetColorF(brushColor);
      painter->GetBrush()->SetColorF(1, 1, 1, 1);
      painter->GetBrush()->SetOpacityF(0.75);
      painter->GetPen()->SetOpacity(0);
      bounds[0] = vtkVector2f(-5/scale[0], -3/scale[1]);
      bounds[1] = vtkVector2f(bounds[1].GetX()+10/scale[0],
        bounds[1].GetY()+10/scale[1]);

      // Pull the tooltip back in if it will go off the edge of the screen.
      float maxX = (this->Scene->GetViewWidth() - position[0])/scale[0];
      if (bounds[0].GetX() >= maxX - bounds[1].GetX())
        {
        bounds[0].SetX(maxX - bounds[1].GetX());
        }
      // Pull the tooltip down in if it will go off the edge of the screen.
      float maxY = (this->Scene->GetViewHeight() - position[1])/scale[1];
      if (bounds[0].GetY() >= maxY - bounds[1].GetY())
        {
        bounds[0].SetY(maxY - bounds[1].GetY());
        }
      painter->DrawRect(bounds[0].GetX(), bounds[0].GetY(), bounds[1].GetX(), bounds[1].GetY());
      painter->DrawString(bounds[0].GetX()+5/scale[0], bounds[0].GetY()+3/scale[1], label);
      painter->GetBrush()->SetColorF(brushColor);
      }
    }

635 636 637
  painter->GetPen()->SetOpacity(penOpacity);
  //painter->GetPen()->SetWidth(width);
  painter->GetBrush()->SetOpacity(brushOpacity);
638

639 640 641
  painter->PopMatrix();
}

642
//-----------------------------------------------------------------------------
643 644
void vtkControlPointsItem::SelectPoint(double* currentPoint)
{
645
  vtkIdType pointId = this->FindPoint(currentPoint);
646 647 648 649 650 651 652 653 654 655 656
  if (pointId == -1)
    {
    vtkErrorMacro( << "try to select a point that doesn't exist");
    return;
    }
  this->SelectPoint(pointId);
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::SelectPoint(vtkIdType pointId)
{
657
  if (!this->Selection || this->Selection->LookupValue(pointId) != -1)
658 659 660
    {
    return;
    }
661
  this->Selection->InsertNextValue(pointId);
662
  this->GetScene()->SetDirty(true);
663 664 665
}

//-----------------------------------------------------------------------------
666 667 668 669 670 671 672 673 674 675 676 677
void vtkControlPointsItem::SelectAllPoints()
{
  this->DeselectAllPoints();
  const int count = this->GetNumberOfPoints();
  for (vtkIdType i = 0; i < count; ++i)
    {
    this->SelectPoint(i);
    }
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::DeselectPoint(double* point)
678 679
{
  // make sure the point belongs to the list of points
680
  vtkIdType pointId = this->FindPoint(point);
681 682 683 684 685 686 687 688 689 690 691
  if (pointId == -1)
    {
    vtkErrorMacro( << "try to deselect a point that doesn't exist");
    return;
    }
  this->DeselectPoint(pointId);
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::DeselectPoint(vtkIdType pointId)
{
692 693
  vtkIdType selectionPointId = this->Selection
      ? this->Selection->LookupValue(pointId) : -1;
694 695
  if (selectionPointId == -1)
    {
696
    //vtkErrorMacro(<< "Point:" << pointId << " was not selected");
697 698 699
    return;
    }
  this->Selection->RemoveTuple(selectionPointId);
700
  this->GetScene()->SetDirty(true);
701 702 703 704 705
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::DeselectAllPoints()
{
706 707 708 709
  if (this->GetNumberOfSelectedPoints() == 0)
    {
    return;
    }
710
  this->Selection->SetNumberOfTuples(0);
711
  this->GetScene()->SetDirty(true);
712 713 714 715 716 717
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::ToggleSelectPoint(double* currentPoint)
{
  // make sure the point belongs to the list of points
718
  vtkIdType pointId = this->FindPoint(currentPoint);
719 720 721 722 723 724 725 726 727 728 729
  if (pointId == -1)
    {
    vtkErrorMacro( << "try to toggle a point that doesn't exist");
    return;
    }
  this->ToggleSelectPoint(pointId);
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::ToggleSelectPoint(vtkIdType pointId)
{
730 731
  vtkIdType selectionId = this->Selection
      ? this->Selection->LookupValue(pointId) : -1;
732 733 734 735 736 737 738 739 740
  if (selectionId != -1)
    {
    this->DeselectPoint(pointId);
    return;
    }
  this->SelectPoint(pointId);
}

//-----------------------------------------------------------------------------
741 742 743 744 745 746 747 748
bool vtkControlPointsItem::SelectPoints(const vtkVector2f& min, const vtkVector2f& max)
{
  bool atLeast1PointSelected = false;
  const int numberOfPoints = this->GetNumberOfPoints();
  for(vtkIdType i = 0; i < numberOfPoints; ++i)
    {
    double point[4];
    this->GetControlPoint(i, point);
749 750
    if (point[0] >= min.GetX() && point[0] <= max.GetX() &&
        point[1] >= min.GetY() && point[1] <= max.GetY())
751 752 753 754 755 756 757 758 759 760 761 762
      {
      this->SelectPoint(i);
      atLeast1PointSelected = true;
      }
    else
      {
      this->DeselectPoint(i);
      }
    }
  return atLeast1PointSelected;
}

763 764 765
//-----------------------------------------------------------------------------
vtkIdType vtkControlPointsItem::GetNumberOfSelectedPoints()const
{
766
  return this->Selection ? this->Selection->GetNumberOfTuples() : 0;
767 768
}

769 770
//-----------------------------------------------------------------------------
vtkIdType vtkControlPointsItem::GetCurrentPoint()const
771
{
772 773 774 775 776 777 778
  return this->CurrentPoint;
}
/*
//-----------------------------------------------------------------------------
vtkIdType vtkControlPointsItem::FindPoint(double* pos, double tolerance)
{
  if (tolerance < std::numeric_limits<float>::epsilon())
779
    {
780
    tolerance = std::numeric_limits<float>::epsilon();
781 782 783 784
    }
  // make sure the point belongs to the list of points
  vtkIdType pointId = -1;
  double minDist = VTK_DOUBLE_MAX;
785
  const int numberOfPoints = this->GetNumberOfPoints();
786
  for(vtkIdType i = 0; i < numberOfPoints; ++i)
787
    {
788 789
    double point[4];
    this->GetControlPoint(i, point);
790 791
    double distance = (point[0] - pos[0]) * (point[0] - pos[0]) +
      (point[1] - pos[1]) * (point[1] - pos[1]);
792
    if (distance <= tolerance)
793 794 795 796 797 798
      {
      if (distance == 0.)
        {// we found the best match ever
        return i;
        }
      else if (distance < minDist)
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
        {// we found something not too bad, we maybe we can find closer
        pointId = i;
        minDist = distance;
        }
      }
    // don't search any further if the x is already too large
    //if (point[0] > pos[0] + this->ItemPointRadius2)
    //  {
    //  break;
    //  }
    }
  return pointId;
}
*/

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
//-----------------------------------------------------------------------------
bool vtkControlPointsItem::IsOverPoint(double* pos, vtkIdType pointId)
{
  if (pointId < 0 || pointId >= this->GetNumberOfPoints())
    {
    return false;
    }

  double screenPos[2];
  this->Transform->TransformPoints(pos, screenPos, 1);

  double point[4];
  this->GetControlPoint(pointId, point);
  double screenPoint[2];
  this->Transform->TransformPoints(point, screenPoint, 1);

  double distance2 =
    (screenPoint[0] - screenPos[0]) * (screenPoint[0] - screenPos[0]) +
    (screenPoint[1] - screenPos[1]) * (screenPoint[1] - screenPos[1]);
  double tolerance = 1.3;
  double radius2 = this->ScreenPointRadius * this->ScreenPointRadius
    * tolerance * tolerance;
  return distance2 <= radius2;
}

839
//-----------------------------------------------------------------------------
840
vtkIdType vtkControlPointsItem::FindPoint(double* _pos)
841
{
842 843 844 845
  vtkVector2f vpos(_pos[0], _pos[1]);
  this->TransformDataToScreen(vpos, vpos);
  double pos[2] = {vpos.GetX(), vpos.GetY()};

846
  double tolerance = 1.3;
847 848
  double radius2 = this->ScreenPointRadius * this->ScreenPointRadius
    * tolerance * tolerance;
849

850 851 852 853 854 855 856 857 858
  double screenPos[2];
  this->Transform->TransformPoints(pos, screenPos, 1);
  vtkIdType pointId = -1;
  double minDist = VTK_DOUBLE_MAX;
  const int numberOfPoints = this->GetNumberOfPoints();
  for(vtkIdType i = 0; i < numberOfPoints; ++i)
    {
    double point[4];
    this->GetControlPoint(i, point);
859 860 861 862 863
    vtkVector2f vpos1(point[0], point[1]);
    this->TransformDataToScreen(vpos1, vpos1);
    point[0] = vpos1.GetX();
    point[1] = vpos1.GetY();

864 865
    double screenPoint[2];
    this->Transform->TransformPoints(point, screenPoint, 1);
866
    double distance2 =
867 868
      (screenPoint[0] - screenPos[0]) * (screenPoint[0] - screenPos[0]) +
      (screenPoint[1] - screenPos[1]) * (screenPoint[1] - screenPos[1]);
869
    if (distance2 <= radius2)
870
      {
871
      if (distance2 == 0.)
872 873 874
        {// we found the best match ever
        return i;
        }
875
      else if (distance2 < minDist)
876
        {// we found something not too bad, maybe we can find closer
877
        pointId = i;
878
        minDist = distance2;
879 880 881
        }
      }
    // don't search any further if the x is already too large
882
    if (screenPoint[0] > (screenPos[0] + this->ScreenPointRadius * tolerance))
883
      {
884
      break;
885 886
      }
    }
887
  return pointId;
888
}
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

//-----------------------------------------------------------------------------
vtkIdType vtkControlPointsItem::GetControlPointId(double* point)
{
  const int numberOfPoints = this->GetNumberOfPoints();
  for(vtkIdType i = 0; i < numberOfPoints; ++i)
    {
    double controlPoint[4];
    this->GetControlPoint(i, controlPoint);
    if (controlPoint[0] == point[0])
      {
      return i;
      }
    }
  return -1;
}

906
//-----------------------------------------------------------------------------
907 908
void vtkControlPointsItem
::GetControlPointsIds(vtkIdTypeArray* points, bool excludeFirstAndLast)const
909
{
910
  assert(points != 0);
911 912 913 914 915
  int numberOfPoints = this->GetNumberOfPoints();
  if (excludeFirstAndLast)
    {
    numberOfPoints -= 2;
    }
916
  numberOfPoints = std::max(numberOfPoints, 0);
917
  points->SetNumberOfTuples(numberOfPoints);
918
  vtkIdType pointId = excludeFirstAndLast ? 1 : 0;
919 920
  for(vtkIdType i = 0; i < numberOfPoints; ++i)
    {
921
    points->SetValue(i, pointId++);
922 923
    }
}
924
//-----------------------------------------------------------------------------
925
void vtkControlPointsItem::AddPointId(vtkIdType addedPointId)
926
{
927
  assert(addedPointId != -1);
928
  // offset all the point ids
929
  const int selectionCount = this->GetNumberOfSelectedPoints();
930 931 932
  for (vtkIdType i = 0; i < selectionCount; ++i)
    {
    vtkIdType pointId = this->Selection->GetValue(i);
933
    if (pointId >= addedPointId)
934 935 936 937
      {
      this->Selection->SetValue(i, ++pointId);
      }
    }
938
  if (this->CurrentPoint >= addedPointId)
939 940 941 942 943 944 945 946 947 948 949
    {
    this->SetCurrentPoint(this->CurrentPoint + 1);
    }
}

//-----------------------------------------------------------------------------
vtkIdType vtkControlPointsItem::RemovePoint(double* point)
{
  return this->RemovePointId(this->GetControlPointId(point));
}

950 951 952 953 954 955 956 957
//-----------------------------------------------------------------------------
vtkIdType vtkControlPointsItem::RemovePoint(vtkIdType pointId)
{
  double point[4];
  this->GetControlPoint(pointId, point);
  return this->RemovePoint(point);
}

958 959 960
//-----------------------------------------------------------------------------
vtkIdType vtkControlPointsItem::RemovePointId(vtkIdType pointId)
{
961 962 963 964
  if(!this->IsPointRemovable(pointId))
    {
    return pointId;
    }
965

966 967
  this->StartChanges();

968
  assert(pointId != -1);
969 970 971
  // Useless to remove the point here as it will be removed anyway in ComputePoints
  this->DeselectPoint(pointId);

972
  const vtkIdType selectionCount = this->GetNumberOfSelectedPoints();
973 974 975 976 977 978 979 980 981
  for (vtkIdType i = 0; i < selectionCount; ++i)
    {
    vtkIdType selectedPointId = this->Selection->GetValue(i);
    if (selectedPointId > pointId)
      {
      this->Selection->SetValue(i, --selectedPointId);
      }
    }

982 983
  if (this->CurrentPoint > pointId ||
      this->CurrentPoint == this->GetNumberOfPoints() - 1)
984 985 986
    {
    this->SetCurrentPoint(this->CurrentPoint - 1);
    }
987 988

  this->EndChanges();
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
  return pointId;
}

//-----------------------------------------------------------------------------
void vtkControlPointsItem::SetCurrentPoint(vtkIdType index)
{
  if (index == this->CurrentPoint)
    {
    return;
    }
  this->CurrentPoint = index;
  this->InvokeEvent(vtkControlPointsItem::CurrentPointChangedEvent,
                    reinterpret_cast<void *>(this->CurrentPoint));
  this->GetScene()->SetDirty(true);
}

//-----------------------------------------------------------------------------
bool vtkControlPointsItem::MouseButtonPressEvent(const vtkContextMouseEvent &mouse)
{
  this->MouseMoved = false;
  this->PointToToggle = -1;
  this->PointToDelete = -1;
1011 1012 1013

  vtkVector2f vpos = mouse.GetPos();
  this->TransformScreenToData(vpos, vpos);
1014
  double pos[2];
1015 1016
  pos[0] = vpos.GetX();
  pos[1] = vpos.GetY();
1017 1018
  vtkIdType pointUnderMouse = this->FindPoint(pos);

1019
  if (mouse.GetButton() == vtkContextMouseEvent::LEFT_BUTTON)
1020 1021 1022 1023 1024 1025 1026
    {
    if (pointUnderMouse != -1)
      {
      this->SetCurrentPoint(pointUnderMouse);
      return true;
      }
    else if (pointUnderMouse == -1
1027
             && this->GetNumberOfSelectedPoints() <= 1
1028 1029
             && !this->StrokeMode)
      {
1030
      this->ClampValidPos(pos);
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
      vtkIdType addedPoint = this->AddPoint(pos);
      this->SetCurrentPoint(addedPoint);
      return true;
      }
    else
      {
      this->SetCurrentPoint(-1);
      }
    return true;
    }

1042
  if (mouse.GetButton() == vtkContextMouseEvent::RIGHT_BUTTON
1043 1044 1045 1046 1047 1048 1049 1050
      && pointUnderMouse != -1)
    {
    this->PointToToggle = pointUnderMouse;
    this->PointAboutToBeToggled = true;
    this->GetScene()->SetDirty(true);
    return true;
    }

1051
  if (mouse.GetButton() == vtkContextMouseEvent::MIDDLE_BUTTON
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
      && pointUnderMouse != -1)
    {
    this->PointToDelete = pointUnderMouse;
    this->PointAboutToBeDeleted = true;
    this->GetScene()->SetDirty(true);
    return true;
    }

  return false;
}

//-----------------------------------------------------------------------------
1064
bool vtkControlPointsItem::MouseDoubleClickEvent(const vtkContextMouseEvent &mouse)
1065
{
1066
  if (mouse.GetButton() == vtkContextMouseEvent::RIGHT_BUTTON)
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    {
    if (this->Selection->GetNumberOfTuples())
      {
      this->DeselectAllPoints();
      }
    else
      {
      this->SelectAllPoints();
      }
    return true;
    }
  bool res = this->MouseButtonPressEvent(mouse);
1079 1080
  if (mouse.GetButton() == vtkContextMouseEvent::LEFT_BUTTON
      && this->CurrentPoint != -1)
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
    {
    this->InvokeEvent(vtkControlPointsItem::CurrentPointEditEvent,
                      reinterpret_cast<void *>(this->CurrentPoint));
    }
  return res;
}

//-----------------------------------------------------------------------------
bool vtkControlPointsItem::MouseMoveEvent(const vtkContextMouseEvent &mouse)
{
1091 1092 1093
  vtkVector2f mousePos = mouse.GetPos();
  this->TransformScreenToData(mousePos, mousePos);

1094
  if (mouse.GetButton() == vtkContextMouseEvent::LEFT_BUTTON)
1095 1096 1097
    {
    if (this->StrokeMode)
      {
1098 1099
      this->StartInteractionIfNotStarted();

1100
      this->Stroke(mousePos);
1101 1102

      this->Interaction();
1103
      }
1104
    else if (this->CurrentPoint == -1 && this->GetNumberOfSelectedPoints() > 1)
1105
      {
1106
      vtkVector2f deltaPos = mouse.GetPos() - mouse.GetLastPos();
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
      if(this->IsEndPointPicked())
        {
        if(!this->GetEndPointsMovable())
          {
          return false;
          }
        else if(this->GetEndPointsXMovable())
          {
          deltaPos.SetY(0);
          }
        else if(this->GetEndPointsYMovable())
          {
          deltaPos.SetX(0);
          }
        }

1123 1124
      this->StartInteractionIfNotStarted();

1125 1126 1127 1128 1129 1130 1131
      if (vtkIdTypeArray* points = this->GetSelection())
        {
        // must stay valid after each individual point move
        points->Register(this);
        this->MovePoints(deltaPos, points);
        points->UnRegister(this);
        }
1132 1133

      this->Interaction();
1134 1135 1136
      }
    else if (this->CurrentPoint != -1)
      {
1137
      vtkVector2f curPos(mousePos);
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
      if(this->IsEndPointPicked())
        {
        double currentPoint[4] = {0.0, 0.0, 0.0, 0.0};
        this->GetControlPoint(this->CurrentPoint, currentPoint);
        if(!this->GetEndPointsMovable())
          {
          return false;
          }
        else if(this->GetEndPointsXMovable())
          {
          curPos.SetY(currentPoint[1]);
          }
        else if(this->GetEndPointsYMovable())
          {
          curPos.SetX(currentPoint[0]);
          }
        }
1155 1156
      this->StartInteractionIfNotStarted();

1157
      this->SetCurrentPointPos(curPos);
1158 1159

      this->Interaction();
1160 1161
      }
    }
1162
  if (mouse.GetButton() == vtkContextMouseEvent::RIGHT_BUTTON)
1163 1164 1165 1166 1167 1168
    {
    if (this->PointToToggle == -1)
      {
      return false;
      }
    double pos[2];
1169 1170
    pos[0] = mousePos[0];
    pos[1] = mousePos[1];
1171 1172 1173 1174 1175 1176 1177 1178
    vtkIdType pointUnderCursor = this->FindPoint(pos);
    if ((pointUnderCursor == this->PointToToggle) != this->PointAboutToBeToggled)
      {
      this->PointAboutToBeToggled = !this->PointAboutToBeToggled;
      this->GetScene()->SetDirty(true);
      }
    }
  this->MouseMoved = true;
1179
  if (mouse.GetButton() == vtkContextMouseEvent::MIDDLE_BUTTON)
1180 1181 1182 1183 1184 1185 1186
    {
    if (this->PointToDelete == -1)
      {
      // allow chart ruber band to work
      return false;
      }
    double pos[2];
1187 1188
    pos[0] = mousePos[0];
    pos[1] = mousePos[1];
1189 1190 1191 1192 1193 1194 1195 1196 1197
    vtkIdType pointUnderCursor = this->FindPoint(pos);
    if ((pointUnderCursor == this->PointToDelete) != this->PointAboutToBeDeleted)
      {
      this->PointAboutToBeDeleted = !this->PointAboutToBeDeleted;
      this->GetScene()->SetDirty(true);
      }
    return true;
    }

1198
  if (mouse.GetButton() == vtkContextMouseEvent::RIGHT_BUTTON
1199 1200 1201 1202
      && this->CurrentPoint == -1)
    {
    return false;
    }
1203 1204 1205 1206
  if (mouse.GetButton() == vtkContextMouseEvent::NO_BUTTON)
    {
    return false;
    }
1207 1208 1209 1210
  return true;
}

//-----------------------------------------------------------------------------
1211
void vtkControlPointsItem::SetCurrentPointPos(const vtkVector2f& newPos)
1212
{
1213
  vtkIdType movedPoint = this->SetPointPos(this->CurrentPoint, newPos);
1214 1215 1216 1217 1218
  // If the moved point was not CurrentPoint then make it current.
  this->SetCurrentPoint(movedPoint);
}

//-----------------------------------------------------------------------------
1219
vtkIdType vtkControlPointsItem::SetPointPos(vtkIdType point, const vtkVector2f& newPos)
1220 1221 1222 1223 1224 1225 1226 1227
{
  if (point == -1)
    {
    return point;
    }

  // Make sure the new point is inside the boundaries of the function
  double boundedPos[2];
1228 1229
  boundedPos[0] = newPos[0];
  boundedPos[1] = newPos[1];
1230
  this->ClampValidPos(boundedPos);
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

  if (!this->SwitchPointsMode)
    {
    // Stop mode.
    // You can't move a point past another point.
    if (point > 0)
      {
      double previousPoint[4] = {0.0, 0.0, 0.0, 0.0};
      this->GetControlPoint(point - 1, previousPoint);
      boundedPos[0] = std::max(previousPoint[0], boundedPos[0]);
      }
    if (point < this->GetNumberOfPoints() - 1)
      {
      double nextPoint[4] = {0.0, 0.0, 0.0, 0.0};
      this->GetControlPoint(point + 1, nextPoint);
      boundedPos[0] = std::min(boundedPos[0], nextPoint[0]);
      }
    }
  else
    {
    // Switch mode.
    // Moving a point to the right of the next one, makes it current.
    // and moving a point to the left of the previous one makes it current.
    if (point > 0)
      {
      double previousPoint[4] = {0.0, 0.0, 0.0, 0.0};
      this->GetControlPoint(point - 1, previousPoint);
      while (boundedPos[0] < previousPoint[0])
        {
        point = point - 1;
        if (point == 0)
          {
          break;
          }
        // maybe the move is that important that it went over multiple points
        this->GetControlPoint(point - 1, previousPoint);
        }
      }
    if (point < this->GetNumberOfPoints() - 1)
      {
      double nextPoint[4] = {0.0, 0.0, 0.0, 0.0};
      this->GetControlPoint(point + 1, nextPoint);
      while (boundedPos[0] > nextPoint[0])
        {
        point = point + 1;
        if (point == this->GetNumberOfPoints() - 1)
          {
          break;
          }
        this->GetControlPoint(point + 1, nextPoint);
        }
      }
    }
  double currentPoint[4] = {0.0, 0.0, 0.0, 0.0};
  this->GetControlPoint(point, currentPoint);
  currentPoint[0] = boundedPos[0];
  currentPoint[1] = boundedPos[1];
1288 1289 1290

  // SetControlPoint will call StartChanges/EndChanges correctly, so we don't
  // need to call it here.
1291 1292 1293 1294 1295
  this->SetControlPoint(point, currentPoint);
  return point;
}

//-----------------------------------------------------------------------------
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
void vtkControlPointsItem::MoveCurrentPoint(const vtkVector2f& translation)
{
  this->MovePoint(this->CurrentPoint, translation);
}

//-----------------------------------------------------------------------------
vtkIdType vtkControlPointsItem::MovePoint(vtkIdType pointId, const vtkVector2f& translation)
{
  double point[4];
  this->GetControlPoint(pointId, point);
  return this->SetPointPos(pointId, vtkVector2f(point[0] + translation.GetX(), point[1] + translation.GetY()));
}

//-----------------------------------------------------------------------------
1310
void vtkControlPointsItem::MovePoints(const vtkVector2f& translation, vtkIdTypeArray* pointIds)
1311
{
1312
  assert(pointIds);
1313
  this->StartChanges();
1314 1315 1316 1317 1318
  // don't support 'switch' mode yet
  //vtkIdTypeArray* addedSelection = vtkIdTypeArray::New();
  bool oldSwitchPoints = this->SwitchPointsMode;
  this->SwitchPointsMode = false;
  // end "don't support 'switch' mode yet"
1319
  const int count = pointIds->GetNumberOfTuples();
1320 1321
  float tX = translation.GetX();
  float tY = translation.GetY();
1322 1323 1324
  int start = tX < 0.f ? 0 : count - 1;
  int end = tX < 0.f ? count : -1;
  int step = tX < 0.f ? 1 : -1;
1325 1326
  for (vtkIdType i = start; i != end; i+=step)
    {
1327
    vtkIdType pointId = pointIds->GetValue(i);
1328
    double currentPoint[4] = {0.0, 0.0, 0.0, 0.0};
1329
    this->GetControlPoint(pointId, currentPoint);
1330
    vtkVector2f newPos(currentPoint[0] + tX, currentPoint[1] + tY);
1331
    //vtkIdType newIdx =
1332
      this->SetPointPos(pointId, newPos);
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
    // don't support 'switch' mode yet
    //if (newIdx != point)
    //  {
    //  int next = (newIdx > point) ? 1 : -1;
    //  for (int i = point + next; i != newIdx; i+=next)
    //    {
    //    addedSelection->InsertNextValue(i);
    //    }
    //  addedSelection->InsertNextValue(newIdx);
    //  }
    // end "don't support 'switch' mode yet"
    }
  // don't support 'switch' mode yet
  //this->SelectPoints(addedSelection);
  this->SwitchPointsMode = oldSwitchPoints;
  // end "don't support 'switch' mode yet"
1349
  this->EndChanges();
1350 1351
}

1352
//-----------------------------------------------------------------------------
1353 1354
void vtkControlPointsItem::MovePoints(const vtkVector2f& translation,
                                      bool dontMoveFirstAndLast)
1355
{
1356 1357 1358
  vtkNew<vtkIdTypeArray> points;
  this->GetControlPointsIds(points.GetPointer(), dontMoveFirstAndLast);
  this->MovePoints(translation, points.GetPointer());
1359 1360
}

1361
//-----------------------------------------------------------------------------
1362
void vtkControlPointsItem::SpreadPoints(float factor, vtkIdTypeArray* pointIds)
1363
{
1364 1365
  assert(pointIds);
  if (pointIds->GetNumberOfTuples() == 0)
1366 1367 1368
    {
    return;
    }
1369 1370
  this->StartChanges();

1371 1372
  double min[2], max[2], center[2];
  double point[4];
1373
  vtkIdType minPointId = pointIds->GetValue(0);
1374 1375 1376
  this->GetControlPoint(minPointId, point);
  min[0] = point[0];
  min[1] = point[1];
1377
  vtkIdType maxPointId = pointIds->GetValue(pointIds->GetNumberOfTuples() - 1);
1378 1379 1380 1381 1382 1383 1384 1385
  this->GetControlPoint(maxPointId, point);
  max[0] = point[0];
  max[1] = point[1];
  center[0] = (min[0] + max[0]) / 2.;
  center[1] = (min[1] + max[1]) / 2.;

  // Left part
  vtkIdType start = 0;
1386
  vtkIdType end = pointIds->GetNumberOfTuples();
1387 1388 1389 1390 1391 1392 1393
  vtkIdType step = 1;
  vtkIdType median = -1; // not needed when factor >= 0
  if (factor < 0.f)
    {
    // search median
    for (vtkIdType i = 0; i < end; ++i)
      {
1394
      vtkIdType pointId = pointIds->GetValue(i);
1395 1396 1397 1398 1399 1400 1401 1402 1403
      this->GetControlPoint(pointId, point);
      if (point[0] > center[0])
        {
        median = i;
        break;
        }
      }
    if (median == -1)
      {
1404
      median = pointIds->GetNumberOfTuples() - 1;
1405 1406 1407 1408 1409 1410 1411 1412
      }
    start = median - 1;
    end = -1;
    step = -1;
    }
  vtkIdType i;
  for (i = start; i != end; i+=step)
    {
1413
    vtkIdType pointId = pointIds->GetValue(i);
1414 1415 1416