vtkPlotPoints.cxx 27.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkPlotPoints.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 "vtkPlotPoints.h"

18
#include "vtkNew.h"
19 20
#include "vtkContext2D.h"
#include "vtkPen.h"
21
#include "vtkBrush.h"
22
#include "vtkAxis.h"
23 24 25
#include "vtkContextMapper2D.h"
#include "vtkPoints2D.h"
#include "vtkTable.h"
26
#include "vtkFloatArray.h"
27
#include "vtkIdTypeArray.h"
28 29
#include "vtkImageData.h"
#include "vtkMath.h"
30
#include "vtkObjectFactory.h"
31
#include "vtkCharArray.h"
32 33
#include "vtkUnsignedCharArray.h"
#include "vtkLookupTable.h"
34

35 36
#include <vector>
#include <algorithm>
37
#include <limits>
38
#include <set>
39 40

// PIMPL for STL vector...
41 42 43 44 45 46 47
struct vtkIndexedVector2f
{
  size_t index;
  vtkVector2f pos;
};

class vtkPlotPoints::VectorPIMPL : public std::vector<vtkIndexedVector2f>
48 49
{
public:
50 51
  VectorPIMPL(vtkVector2f* array, size_t n)
    : std::vector<vtkIndexedVector2f>()
52
  {
53 54 55 56 57 58 59 60
    this->reserve(n);
    for (size_t i = 0; i < n; ++i)
      {
      vtkIndexedVector2f tmp;
      tmp.index = i;
      tmp.pos = array[i];
      this->push_back(tmp);
      }
61 62
  }
};
63 64

//-----------------------------------------------------------------------------
65
vtkStandardNewMacro(vtkPlotPoints)
66 67 68 69

//-----------------------------------------------------------------------------
vtkPlotPoints::vtkPlotPoints()
{
70 71 72
  this->Points = NULL;
  this->Sorted = NULL;
  this->BadPoints = NULL;
73
  this->ValidPointMask = NULL;
74
  this->MarkerStyle = vtkPlotPoints::CIRCLE;
75
  this->MarkerSize = -1.0;
76 77
  this->LogX = false;
  this->LogY = false;
78 79 80 81

  this->LookupTable = 0;
  this->Colors = 0;
  this->ScalarVisibility = 0;
82 83 84

  this->UnscaledInputBounds[0] = this->UnscaledInputBounds[2] = vtkMath::Inf();
  this->UnscaledInputBounds[1] = this->UnscaledInputBounds[3] = -vtkMath::Inf();
85 86 87 88 89
}

//-----------------------------------------------------------------------------
vtkPlotPoints::~vtkPlotPoints()
{
90 91 92 93 94 95 96 97 98 99 100
  if (this->Points)
    {
    this->Points->Delete();
    this->Points = NULL;
    }
  delete this->Sorted;
  if (this->BadPoints)
    {
    this->BadPoints->Delete();
    this->BadPoints = NULL;
    }
101 102 103 104 105 106 107 108
  if (this->LookupTable)
    {
    this->LookupTable->UnRegister(this);
    }
  if ( this->Colors != 0 )
    {
    this->Colors->UnRegister(this);
    }
109 110 111 112 113 114 115 116 117 118 119
}

//-----------------------------------------------------------------------------
void vtkPlotPoints::Update()
{
  if (!this->Visible)
    {
    return;
    }
  // Check if we have an input
  vtkTable *table = this->Data->GetInput();
120 121 122 123

  if (table && !this->ValidPointMaskName.empty() &&
      table->GetColumnByName(this->ValidPointMaskName))
    {
124
    this->ValidPointMask = vtkArrayDownCast<vtkCharArray>(
125 126 127 128 129 130 131
      table->GetColumnByName(this->ValidPointMaskName));
    }
  else
    {
    this->ValidPointMask = 0;
    }

132 133 134 135 136 137 138
  if (!table)
    {
    vtkDebugMacro(<< "Update event called with no input table set.");
    return;
    }
  else if(this->Data->GetMTime() > this->BuildTime ||
          table->GetMTime() > this->BuildTime ||
139
          (this->LookupTable && this->LookupTable->GetMTime() > this->BuildTime) ||
140 141 142 143 144
          this->MTime > this->BuildTime)
    {
    vtkDebugMacro(<< "Updating cached values.");
    this->UpdateTableCache(table);
    }
145 146 147
  else if (this->XAxis && this->YAxis &&
           ((this->XAxis->GetMTime() > this->BuildTime) ||
            (this->YAxis->GetMTime() > this->BuildTime)))
148
    {
149 150
    if ((this->LogX != this->XAxis->GetLogScale()) ||
        (this->LogY != this->YAxis->GetLogScale()))
151 152 153 154
      {
      this->UpdateTableCache(table);
      }
    }
155 156 157 158 159 160 161 162
}

//-----------------------------------------------------------------------------
bool vtkPlotPoints::Paint(vtkContext2D *painter)
{
  // This is where everything should be drawn, or dispatched to other methods.
  vtkDebugMacro(<< "Paint event called in vtkPlotPoints.");

163
  if (!this->Visible || !this->Points || this->Points->GetNumberOfPoints() == 0)
164 165 166
    {
    return false;
    }
167

168 169 170
  // Maintain legacy behavior (using pen width) if MarkerSize was not set
  float width = this->MarkerSize;
  if (width < 0.0f)
171
    {
172 173 174 175 176
    width = this->Pen->GetWidth() * 2.3;
    if (width < 8.0)
      {
      width = 8.0;
      }
177 178
    }

179
  // If there is a marker style, then draw the marker for each point too
180
  if (this->MarkerStyle != VTK_MARKER_NONE)
181 182 183 184
    {
    painter->ApplyPen(this->Pen);
    painter->ApplyBrush(this->Brush);
    painter->GetPen()->SetWidth(width);
185 186 187 188

    float *points = static_cast<float *>(this->Points->GetVoidPointer(0));
    unsigned char *colors = 0;
    int nColorComponents = 0;
189 190
    if (this->ScalarVisibility && this->Colors)
      {
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
      colors = this->Colors->GetPointer(0);
      nColorComponents = static_cast<int>(this->Colors->GetNumberOfComponents());
      }

    if (this->BadPoints && this->BadPoints->GetNumberOfTuples() > 0)
      {
      vtkIdType lastGood = 0;

      for (vtkIdType i = 0; i < this->BadPoints->GetNumberOfTuples(); i++)
        {
        vtkIdType id = this->BadPoints->GetValue(i);

        // render from last good point to one before this bad point
        if (id - lastGood > 2)
          {
          painter->DrawMarkers(this->MarkerStyle, false,
                               points + 2 * (lastGood + 1),
                               id - lastGood - 1,
                               colors ? colors + 4 * (lastGood + 1) : 0,
                               nColorComponents);
          }

        lastGood = id;
        }
215 216 217 218 219 220 221 222 223 224

        // render any trailing good points
        if (this->Points->GetNumberOfPoints() - lastGood > 2)
          {
          painter->DrawMarkers(this->MarkerStyle, false,
                               points + 2 * (lastGood + 1),
                               this->Points->GetNumberOfPoints() - lastGood - 1,
                               colors ? colors + 4 * (lastGood + 1) : 0,
                               nColorComponents);
          }
225 226 227
      }
    else
      {
228 229 230 231
      // draw all of the points
      painter->DrawMarkers(this->MarkerStyle, false,
                           points, this->Points->GetNumberOfPoints(),
                           colors, nColorComponents);
232 233 234
      }
    }

235
  // Now add some decorations for our selected points...
236
  if (this->Selection && this->Selection->GetNumberOfTuples())
237
    {
238 239
    if (this->Selection->GetMTime() > this->SelectedPoints->GetMTime() ||
        this->GetMTime() > this->SelectedPoints->GetMTime())
240
      {
241
      float *f = vtkArrayDownCast<vtkFloatArray>(
242
            this->Points->GetData())->GetPointer(0);
243 244 245 246 247
      int nSelected(static_cast<int>(this->Selection->GetNumberOfTuples()));
      this->SelectedPoints->SetNumberOfComponents(2);
      this->SelectedPoints->SetNumberOfTuples(nSelected);
      float *selectedPtr = static_cast<float *>(this->SelectedPoints->GetVoidPointer(0));
      for (int i = 0; i < nSelected; ++i)
248
        {
249 250
        *(selectedPtr++) = f[2 * this->Selection->GetValue(i)];
        *(selectedPtr++) = f[2 * this->Selection->GetValue(i) + 1];
251 252
        }
      }
253
    vtkDebugMacro(<<"Selection set " << this->Selection->GetNumberOfTuples());
254
    painter->GetPen()->SetColor(this->SelectionPen->GetColor());
255
    painter->GetPen()->SetOpacity(this->SelectionPen->GetOpacity());
256 257
    painter->GetPen()->SetWidth(width + 2.7);

258 259 260 261 262 263 264 265 266 267 268 269 270 271
    if (this->MarkerStyle == VTK_MARKER_NONE)
      {
      painter->DrawMarkers(VTK_MARKER_PLUS, false,
                           static_cast<float *>(
                             this->SelectedPoints->GetVoidPointer(0)),
                           this->SelectedPoints->GetNumberOfTuples());
      }
    else
      {
      painter->DrawMarkers(this->MarkerStyle, true,
                           static_cast<float *>(
                             this->SelectedPoints->GetVoidPointer(0)),
                           this->SelectedPoints->GetNumberOfTuples());
      }
272 273 274 275 276
    }

  return true;
}

277
//-----------------------------------------------------------------------------
278 279
bool vtkPlotPoints::PaintLegend(vtkContext2D *painter, const vtkRectf& rect,
                                int)
280 281 282 283 284 285 286 287 288 289 290 291
{
  if (this->MarkerStyle)
    {
    float width = this->Pen->GetWidth() * 2.3;
    if (width < 8.0)
      {
      width = 8.0;
      }
    painter->ApplyPen(this->Pen);
    painter->ApplyBrush(this->Brush);
    painter->GetPen()->SetWidth(width);

292
    float point[] = { rect[0]+ 0.5f * rect[2], rect[1] + 0.5f * rect[3] };
293
    painter->DrawMarkers(this->MarkerStyle, false, point, 1);
294 295 296 297
    }
  return true;
}

298 299 300 301 302
//-----------------------------------------------------------------------------
void vtkPlotPoints::GetBounds(double bounds[4])
{
  if (this->Points)
    {
303 304
    // There are bad points in the series - need to do this ourselves.
    this->CalculateBounds(bounds);
305 306 307 308 309
    }
  vtkDebugMacro(<< "Bounds: " << bounds[0] << "\t" << bounds[1] << "\t"
                << bounds[2] << "\t" << bounds[3]);
}

310 311 312
//-----------------------------------------------------------------------------
void vtkPlotPoints::GetUnscaledInputBounds(double bounds[4])
{
313
  this->CalculateUnscaledInputBounds();
314 315 316 317 318 319 320 321 322
  for (int i = 0; i < 4; ++i)
    {
    bounds[i] = this->UnscaledInputBounds[i];
    }
  vtkDebugMacro(
    << "Bounds: " << bounds[0] << "\t" << bounds[1] << "\t"
    << bounds[2] << "\t" << bounds[3]);
}

323 324 325
namespace
{

326 327
bool compVector3fX(const vtkIndexedVector2f& v1,
                   const vtkIndexedVector2f& v2)
328
{
329
  if (v1.pos.GetX() < v2.pos.GetX())
330 331 332 333 334 335 336 337 338 339 340 341 342
    {
    return true;
    }
  else
    {
    return false;
    }
}

// See if the point is within tolerance.
bool inRange(const vtkVector2f& point, const vtkVector2f& tol,
             const vtkVector2f& current)
{
343 344
  if (current.GetX() > point.GetX() - tol.GetX() && current.GetX() < point.GetX() + tol.GetX() &&
      current.GetY() > point.GetY() - tol.GetY() && current.GetY() < point.GetY() + tol.GetY())
345 346 347 348 349 350 351 352 353 354 355 356
    {
    return true;
    }
  else
    {
    return false;
    }
}

}

//-----------------------------------------------------------------------------
357
void vtkPlotPoints::CreateSortedPoints()
358 359 360 361
{
  // Sort the data if it has not been done already...
  if (!this->Sorted)
    {
362
    vtkIdType n = this->Points->GetNumberOfPoints();
363 364
    vtkVector2f* data =
        static_cast<vtkVector2f*>(this->Points->GetVoidPointer(0));
365 366 367 368 369
    this->Sorted = new VectorPIMPL(data, n);
    std::sort(this->Sorted->begin(), this->Sorted->end(), compVector3fX);
    }
}

370
//-----------------------------------------------------------------------------
371 372 373
vtkIdType vtkPlotPoints::GetNearestPoint(const vtkVector2f& point,
                                         const vtkVector2f& tol,
                                         vtkVector2f* location)
374
{
375
  // Right now doing a simple bisector search of the array.
376 377
  if (!this->Points)
    {
378
    return -1;
379
    }
380
  this->CreateSortedPoints();
381 382 383 384 385 386

  // Set up our search array, use the STL lower_bound algorithm
  VectorPIMPL::iterator low;
  VectorPIMPL &v = *this->Sorted;

  // Get the lowest point we might hit within the supplied tolerance
387
  vtkIndexedVector2f lowPoint;
388
  lowPoint.index = 0;
389
  lowPoint.pos = vtkVector2f(point.GetX()-tol.GetX(), 0.0f);
390
  low = std::lower_bound(v.begin(), v.end(), lowPoint, compVector3fX);
391 392

  // Now consider the y axis
393
  float highX = point.GetX() + tol.GetX();
394 395
  while (low != v.end())
    {
396
    if (inRange(point, tol, (*low).pos))
397
      {
398
      *location = (*low).pos;
399
      return static_cast<int>((*low).index);
400
      }
401
    else if (low->pos.GetX() > highX)
402 403 404 405 406
      {
      break;
      }
    ++low;
    }
407
  return -1;
408 409
}

410
//-----------------------------------------------------------------------------
411 412 413 414 415 416
bool vtkPlotPoints::SelectPoints(const vtkVector2f& min, const vtkVector2f& max)
{
  if (!this->Points)
    {
    return false;
    }
417
  this->CreateSortedPoints();
418 419 420 421 422 423 424

  if (!this->Selection)
    {
    this->Selection = vtkIdTypeArray::New();
    }
  this->Selection->SetNumberOfTuples(0);

425 426 427
  // Set up our search array, use the STL lower_bound algorithm
  VectorPIMPL::iterator low;
  VectorPIMPL &v = *this->Sorted;
428

429 430 431 432 433 434
  // Get the lowest point we might hit within the supplied tolerance
  vtkIndexedVector2f lowPoint;
  lowPoint.index = 0;
  lowPoint.pos = min;
  low = std::lower_bound(v.begin(), v.end(), lowPoint, compVector3fX);

435 436
  // Output a sorted selection list too.
  std::vector<vtkIdType> selected;
437 438
  // Iterate until we are out of range in X
  while (low != v.end())
439
    {
440 441
      if (low->pos.GetX() >= min.GetX() && low->pos.GetX() <= max.GetX() &&
          low->pos.GetY() >= min.GetY() && low->pos.GetY() <= max.GetY())
442
        {
443
        selected.push_back(low->index);
444
        }
445
      else if (low->pos.GetX() > max.GetX())
446 447 448 449
        {
        break;
        }
      ++low;
450
    }
451 452 453 454 455 456
  this->Selection->SetNumberOfTuples(selected.size());
  vtkIdType *ptr = static_cast<vtkIdType *>(this->Selection->GetVoidPointer(0));
  for (size_t i = 0; i < selected.size(); ++i)
    {
    ptr[i] = selected[i];
    }
457
  std::sort(ptr, ptr + selected.size());
458
  this->Selection->Modified();
459 460 461
  return this->Selection->GetNumberOfTuples() > 0;
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
//-----------------------------------------------------------------------------
bool vtkPlotPoints::SelectPointsInPolygon(const vtkContextPolygon &polygon)
{
  if (!this->Points)
    {
    // nothing to select
    return false;
    }

  if (!this->Selection)
    {
    // create selection object
    this->Selection = vtkIdTypeArray::New();
    }
  else
    {
    // clear previous selection
    this->Selection->SetNumberOfValues(0);
    }

  for(vtkIdType pointId = 0;
      pointId < this->Points->GetNumberOfPoints();
      pointId++)
    {
    // get point location
    double point[3];
    this->Points->GetPoint(pointId, point);

490
    if (polygon.Contains(vtkVector2f(point[0], point[1])))
491
      {
492
      this->Selection->InsertNextValue(pointId);
493 494 495 496 497 498 499
      }
    }

  // return true if we selected any points
  return this->Selection->GetNumberOfTuples() > 0;
}

500 501 502
//-----------------------------------------------------------------------------
namespace {

503 504 505 506 507 508 509 510 511 512 513 514 515
// Find any bad points in the supplied array.
template<typename T>
void SetBadPoints(T *data, vtkIdType n, std::set<vtkIdType> &bad)
{
  for (vtkIdType i = 0; i < n; ++i)
    {
    if (vtkMath::IsInf(data[i]) || vtkMath::IsNan(data[i]))
      {
      bad.insert(i);
      }
    }
}

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
// Calculate the bounds from the original data.
template<typename A>
void ComputeBounds(A *a, int n, double bounds[2])
{
  bounds[0] =  std::numeric_limits<double>::max();
  bounds[1] = -std::numeric_limits<double>::max();
  for (int i = 0; i < n; ++a, ++i)
    {
    bounds[0] = bounds[0] < *a ? bounds[0] : *a;
    bounds[1] = bounds[1] > *a ? bounds[1] : *a;
    }
}

template<typename A>
void ComputeBounds(A *a, int n, vtkIdTypeArray *bad, double bounds[2])
{
  // If possible, use the simpler code without any bad points.
  if (!bad || bad->GetNumberOfTuples() == 0)
    {
    ComputeBounds(a, n, bounds);
    return;
    }

  // Initialize the first range of points.
  vtkIdType start = 0;
  vtkIdType end = 0;
  vtkIdType i = 0;
  vtkIdType nBad = bad->GetNumberOfTuples();
  if (bad->GetValue(i) == 0)
    {
    while (i < nBad && i == bad->GetValue(i))
      {
      start = bad->GetValue(i++) + 1;
      }
    if (start < n)
      {
      end = n;
      }
    else
      {
      // They are all bad points, return early.
      return;
      }
    }
  if (i < nBad)
    {
    end = bad->GetValue(i++);
    }
  else
    {
    end = n;
    }

  bounds[0] =  std::numeric_limits<double>::max();
  bounds[1] = -std::numeric_limits<double>::max();
  while (start < n)
    {
    // Calculate the min/max in this range.
    while (start < end)
      {
      bounds[0] = bounds[0] < a[start] ? bounds[0] : a[start];
      bounds[1] = bounds[1] > a[start] ? bounds[1] : a[start];
      ++start;
      }
    // Now figure out the next range to be evaluated.
    start = end + 1;
    while (i < nBad && start == bad->GetValue(i))
      {
      start = bad->GetValue(i++) + 1;
      }
586
    if (i < nBad)
587
      {
588
      end = bad->GetValue(i++);
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
      }
    else
      {
      end = n;
      }
    }
}

// Dispatch this call off to the right function.
template<typename A>
void ComputeBounds(A *a, vtkDataArray *b, int n, vtkIdTypeArray *bad,
                   double bounds[4])
{
  ComputeBounds(a, n, bad, bounds);
  switch(b->GetDataType())
    {
    vtkTemplateMacro(
      ComputeBounds(static_cast<VTK_TT*>(b->GetVoidPointer(0)), n, bad,
                    &bounds[2]));
    }
}

611
// Copy the two arrays into the points array
612
template<typename A, typename B>
613
void CopyToPoints(vtkPoints2D *points, A *a, B *b, int n, const vtkRectd &ss)
614 615
{
  points->SetNumberOfPoints(n);
616
  float* data = static_cast<float*>(points->GetVoidPointer(0));
617 618
  for (int i = 0; i < n; ++i)
    {
619 620
    data[2 * i]     = static_cast<float>((a[i] + ss[0]) * ss[2]);
    data[2 * i + 1] = static_cast<float>((b[i] + ss[1]) * ss[3]);
621 622 623
    }
}

624
// Copy one array into the points array, use the index of that array as x
625
template<typename A>
626
void CopyToPoints(vtkPoints2D *points, A *a, int n, const vtkRectd &ss)
627 628 629 630 631
{
  points->SetNumberOfPoints(n);
  float* data = static_cast<float*>(points->GetVoidPointer(0));
  for (int i = 0; i < n; ++i)
    {
632 633
    data[2 * i]     = static_cast<float>((  i  + ss[0]) * ss[2]);
    data[2 * i + 1] = static_cast<float>((a[i] + ss[1]) * ss[3]);
634 635 636
    }
}

637
// Copy the two arrays into the points array
638
template<typename A>
639 640
void CopyToPointsSwitch(vtkPoints2D *points, A *a, vtkDataArray *b, int n,
                        const vtkRectd &ss)
641 642 643 644
{
  switch(b->GetDataType())
    {
    vtkTemplateMacro(
645
      CopyToPoints(
646
        points, a, static_cast<VTK_TT*>(b->GetVoidPointer(0)), n, ss));
647 648 649
    }
}

650 651
}

652
//-----------------------------------------------------------------------------
653
bool vtkPlotPoints::GetDataArrays(vtkTable *table, vtkDataArray *array[2])
654
{
655 656 657 658 659
  if (!table)
    {
    return false;
    }

660
  // Get the x and y arrays (index 0 and 1 respectively)
661 662 663
  array[0] = this->UseIndexForXSeries ?
        0 : this->Data->GetInputArrayToProcess(0, table);
  array[1] = this->Data->GetInputArrayToProcess(1, table);
664

665
  if (!array[0] && !this->UseIndexForXSeries)
666 667 668 669
    {
    vtkErrorMacro(<< "No X column is set (index 0).");
    return false;
    }
670
  else if (!array[1])
671 672 673 674
    {
    vtkErrorMacro(<< "No Y column is set (index 1).");
    return false;
    }
675
  else if (!this->UseIndexForXSeries &&
676
           array[0]->GetNumberOfTuples() != array[1]->GetNumberOfTuples())
677
    {
678
    vtkErrorMacro("The x and y columns must have the same number of elements. "
679 680 681 682 683 684 685 686 687 688 689 690 691
                  << array[0]->GetNumberOfTuples() << ", "
                  << array[1]->GetNumberOfTuples());
    return false;
    }
  return true;
}

//-----------------------------------------------------------------------------
bool vtkPlotPoints::UpdateTableCache(vtkTable *table)
{
  vtkDataArray *array[2] = { 0, 0 };
  if (!this->GetDataArrays(table, array))
    {
692
    this->BuildTime.Modified();
693 694 695 696 697 698 699
    return false;
    }

  if (!this->Points)
    {
    this->Points = vtkPoints2D::New();
    }
700 701
  vtkDataArray *x = array[0];
  vtkDataArray *y = array[1];
702

703 704
  // Now copy the components into their new columns
  if (this->UseIndexForXSeries)
705
    {
706 707
    switch(y->GetDataType())
      {
708 709 710
      vtkTemplateMacro(
        CopyToPoints(
          this->Points, static_cast<VTK_TT*>(y->GetVoidPointer(0)),
711
          y->GetNumberOfTuples(), this->ShiftScale));
712
      }
713
    }
714
  else
715
    {
716 717 718
    switch(x->GetDataType())
      {
      vtkTemplateMacro(
719 720
        CopyToPointsSwitch(
          this->Points, static_cast<VTK_TT*>(x->GetVoidPointer(0)),
721
          y, x->GetNumberOfTuples(), this->ShiftScale));
722
      }
723
    }
724 725 726
  this->CalculateLogSeries();
  this->FindBadPoints();
  this->Points->Modified();
727 728
  delete this->Sorted;
  this->Sorted = 0;
729 730

  // Additions for color mapping
731
  if (this->ScalarVisibility && !this->ColorArrayName.empty())
732 733
    {
    vtkDataArray* c =
734
      vtkArrayDownCast<vtkDataArray>(table->GetColumnByName(this->ColorArrayName));
735 736 737 738 739 740 741
    // TODO: Should add support for categorical coloring & try enum lookup
    if (c)
      {
      if (!this->LookupTable)
        {
        this->CreateDefaultLookupTable();
        }
742 743 744 745
      if (this->Colors)
        {
        this->Colors->UnRegister(this);
        }
746 747 748 749 750 751 752 753 754 755 756 757
      this->Colors = this->LookupTable->MapScalars(c, VTK_COLOR_MODE_MAP_SCALARS, -1);
      // Consistent register and unregisters
      this->Colors->Register(this);
      this->Colors->Delete();
      }
    else
      {
      this->Colors->UnRegister(this);
      this->Colors = 0;
      }
    }

758
  this->BuildTime.Modified();
759

760
  return true;
761 762
}

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
//-----------------------------------------------------------------------------
void vtkPlotPoints::CalculateUnscaledInputBounds()
{
  vtkTable *table = this->Data->GetInput();
  vtkDataArray *array[2] = { 0, 0 };
  if (!this->GetDataArrays(table, array))
    {
    return;
    }
  // Now copy the components into their new columns
  if (this->UseIndexForXSeries)
    {
    this->UnscaledInputBounds[0] = 0.0;
    this->UnscaledInputBounds[1] = array[1]->GetNumberOfTuples() - 1;
    switch(array[1]->GetDataType())
      {
      vtkTemplateMacro(
        ComputeBounds(static_cast<VTK_TT*>(array[1]->GetVoidPointer(0)),
            array[1]->GetNumberOfTuples(), this->BadPoints,
            &this->UnscaledInputBounds[2]));
      }
    }
  else
    {
    switch(array[0]->GetDataType())
      {
      vtkTemplateMacro(
        ComputeBounds(static_cast<VTK_TT*>(array[0]->GetVoidPointer(0)),
            array[1], array[0]->GetNumberOfTuples(), this->BadPoints,
            this->UnscaledInputBounds));
      }
    }
}

797
//-----------------------------------------------------------------------------
798
void vtkPlotPoints::CalculateLogSeries()
799 800 801 802 803
{
  if (!this->XAxis || !this->YAxis)
    {
    return;
    }
804 805
  this->LogX = this->XAxis->GetLogScaleActive();
  this->LogY = this->YAxis->GetLogScaleActive();
806 807 808 809
  float* data = static_cast<float*>(this->Points->GetVoidPointer(0));
  vtkIdType n = this->Points->GetNumberOfPoints();
  if (this->LogX)
    {
810
    if (this->XAxis->GetUnscaledMinimum() < 0.)
811
      {
812 813 814 815 816 817 818 819 820 821 822
      for (vtkIdType i = 0; i < n; ++i)
        {
        data[2*i] = log10(fabs(data[2*i]));
        }
      }
    else
      {
      for (vtkIdType i = 0; i < n; ++i)
        {
        data[2*i] = log10(data[2*i]);
        }
823 824 825 826
      }
    }
  if (this->LogY)
    {
827 828 829 830 831 832 833 834 835 836 837 838 839 840
    if (this->YAxis->GetUnscaledMinimum() < 0.)
      {
      for (vtkIdType i = 0; i < n; ++i)
        {
        data[2*i+1] = log10(fabs(data[2*i+1]));
        }
      }
    else
      {
      for (vtkIdType i = 0; i < n; ++i)
        {
        data[2*i+1] = log10(data[2*i+1]);
        }
      }
841 842 843 844
  }
}

//-----------------------------------------------------------------------------
845
void vtkPlotPoints::FindBadPoints()
846 847 848 849 850
{
  // This should be run after CalculateLogSeries as a final step.
  vtkIdType n = this->Points->GetNumberOfPoints();

  // Scan through and find any bad points.
851 852 853
  vtkTable *table = this->Data->GetInput();
  vtkDataArray *array[2] = { 0, 0 };
  if (!this->GetDataArrays(table, array))
854
    {
855 856 857 858 859 860
    return;
    }
  std::set<vtkIdType> bad;
  if (!this->UseIndexForXSeries)
    {
    switch(array[0]->GetDataType())
861
      {
862 863
      vtkTemplateMacro(
        SetBadPoints(static_cast<VTK_TT*>(array[0]->GetVoidPointer(0)), n, bad));
864 865
      }
    }
866 867 868 869 870
  switch(array[1]->GetDataType())
    {
    vtkTemplateMacro(
      SetBadPoints(static_cast<VTK_TT*>(array[1]->GetVoidPointer(0)), n, bad));
    }
871

872 873 874 875 876 877 878
  // add points from the ValidPointMask
  if (this->ValidPointMask)
    {
    for (vtkIdType i = 0; i < n; i++)
      {
      if (this->ValidPointMask->GetValue(i) == 0)
        {
879
        bad.insert(i);
880 881 882 883
        }
      }
    }

884
  // If there are bad points copy them, if not ensure the pointer is null.
885
  if (!bad.empty())
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
    {
    if (!this->BadPoints)
      {
      this->BadPoints = vtkIdTypeArray::New();
      }
    else
      {
      this->BadPoints->SetNumberOfTuples(0);
      }
    for (std::set<vtkIdType>::const_iterator it = bad.begin();
         it != bad.end(); ++it)
      {
      this->BadPoints->InsertNextValue(*it);
      }
    }
  else if (this->BadPoints)
902 903 904 905 906 907 908
    {
    this->BadPoints->Delete();
    this->BadPoints = NULL;
    }
}

//-----------------------------------------------------------------------------
909
void vtkPlotPoints::CalculateBounds(double bounds[4])
910 911
{
  // We can use the BadPoints array to skip the bad points
912
  if (!this->Points)
913 914 915
    {
    return;
    }
916 917
  this->CalculateUnscaledInputBounds();
  for (int i = 0; i < 4; ++i)
918
    {
919
    bounds[i] = this->UnscaledInputBounds[i];
920
    }
921
  if (this->LogX)
922
    {
923 924
    bounds[0] = log10(bounds[0]);
    bounds[1] = log10(bounds[1]);
925
    }
926
  if (this->LogY)
927
    {
928 929
    bounds[2] = log10(bounds[2]);
    bounds[3] = log10(bounds[3]);
930 931 932
    }
}

933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
//-----------------------------------------------------------------------------
void vtkPlotPoints::SetLookupTable(vtkScalarsToColors *lut)
{
  if ( this->LookupTable != lut )
    {
    if ( this->LookupTable)
      {
      this->LookupTable->UnRegister(this);
      }
    this->LookupTable = lut;
    if (lut)
      {
      lut->Register(this);
      }
    this->Modified();
    }
}

//-----------------------------------------------------------------------------
vtkScalarsToColors *vtkPlotPoints::GetLookupTable()
{
  if ( this->LookupTable == 0 )
    {
    this->CreateDefaultLookupTable();
    }
  return this->LookupTable;
}

//-----------------------------------------------------------------------------
void vtkPlotPoints::CreateDefaultLookupTable()
{
  if ( this->LookupTable)
    {
    this->LookupTable->UnRegister(this);
    }
  this->LookupTable = vtkLookupTable::New();
  // Consistent Register/UnRegisters.
  this->LookupTable->Register(this);
  this->LookupTable->Delete();
}

//-----------------------------------------------------------------------------
975
void vtkPlotPoints::SelectColorArray(const vtkStdString& arrayName)
976 977 978 979 980 981 982
{
  vtkTable *table = this->Data->GetInput();
  if (!table)
    {
    vtkDebugMacro(<< "SelectColorArray called with no input table set.");
    return;
    }
983
  if (this->ColorArrayName == arrayName)
984 985 986 987 988
    {
    return;
    }
  for (vtkIdType c = 0; c < table->GetNumberOfColumns(); ++c)
    {
989
    if (arrayName == table->GetColumnName(c))
990
      {
991
      this->ColorArrayName = arrayName;
992 993 994 995 996
      this->Modified();
      return;
      }
    }
  vtkDebugMacro(<< "SelectColorArray called with invalid column name.");
997
  this->ColorArrayName = "";
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
  this->Modified();
  return;
}

//-----------------------------------------------------------------------------
void vtkPlotPoints::SelectColorArray(vtkIdType arrayNum)
{
  vtkTable *table = this->Data->GetInput();
  if (!table)
    {
    vtkDebugMacro(<< "SelectColorArray called with no input table set.");
    return;
    }
1011
  vtkDataArray *col = vtkArrayDownCast<vtkDataArray>(table->GetColumn(arrayNum));
1012 1013 1014 1015 1016 1017 1018 1019 1020
  // TODO: Should add support for categorical coloring & try enum lookup
  if (!col)
    {
    vtkDebugMacro(<< "SelectColorArray called with invalid column index");
    return;
    }
  else
    {
    const char *arrayName = table->GetColumnName(arrayNum);
1021
    if (this->ColorArrayName == arrayName || arrayName == 0)
1022 1023 1024 1025 1026
      {
      return;
      }
    else
      {
1027
      this->ColorArrayName = arrayName;
1028 1029 1030 1031 1032
      this->Modified();
      }
    }
}

1033
//-----------------------------------------------------------------------------
1034
vtkStdString vtkPlotPoints::GetColorArrayName()
1035 1036 1037
{
  return this->ColorArrayName;
}
1038

1039 1040 1041 1042 1043
//-----------------------------------------------------------------------------
void vtkPlotPoints::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}