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

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

#include "vtkContext2D.h"
#include "vtkPen.h"
20
#include "vtkRect.h"
21 22
#include "vtkPoints2D.h"
#include "vtkIdTypeArray.h"
23 24 25 26 27 28 29 30 31

#include "vtkObjectFactory.h"

//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkPlotLine);

//-----------------------------------------------------------------------------
vtkPlotLine::vtkPlotLine()
{
32
  this->MarkerStyle = vtkPlotPoints::NONE;
33
  this->PolyLine = true;
34 35 36
}

//-----------------------------------------------------------------------------
37
vtkPlotLine::~vtkPlotLine() = default;
38 39 40 41 42 43 44

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

45
  if (!this->Visible || !this->Points)
46
  {
47
    return false;
48
  }
49

50 51
  // Draw the line between the points
  painter->ApplyPen(this->Pen);
52 53

  if (this->BadPoints && this->BadPoints->GetNumberOfTuples() > 0)
54
  {
55 56
    // draw lines skipping bad points
    float *points = static_cast<float *>(this->Points->GetVoidPointer(0));
57
    const int pointSize = 2;
58
    vtkIdType lastGood = 0;
59 60 61 62
    vtkIdType bpIdx = 0;
    vtkIdType lineIncrement = this->PolyLine ? 1 : 2;
    vtkIdType nPoints = this->Points->GetNumberOfPoints();
    vtkIdType nBadPoints = this->BadPoints->GetNumberOfTuples();
63

64
    while (lastGood < nPoints)
65
    {
66 67 68 69 70 71 72 73
      vtkIdType id = bpIdx < nBadPoints ?
        this->BadPoints->GetValue(bpIdx) : this->Points->GetNumberOfPoints();

      // With non polyline, we discard a line if any of its points are bad
      if (!this->PolyLine && id % 2 == 1)
      {
        id--;
      }
74 75

      // render from last good point to one before this bad point
76
      if (id - lastGood > 1)
77
      {
78
        int start = lastGood;
79 80
        int numberOfPoints = id - start;
        if (this->PolyLine)
81
        {
82
          painter->DrawPoly(points + pointSize * start, numberOfPoints);
83
        }
84
        else
85
        {
86
          painter->DrawLines(points + pointSize * start, numberOfPoints);
87
        }
88
      }
89 90
      lastGood = id + lineIncrement;
      bpIdx++;
91
    }
92
  }
93
  else
94
  {
95
    // draw lines between all points
96
    if (this->PolyLine)
97
    {
98
      painter->DrawPoly(this->Points);
99
    }
100
    else
101
    {
102
      painter->DrawLines(this->Points);
103
    }
104
  }
105

106
  return this->vtkPlotPoints::Paint(painter);
107 108
}

109
//-----------------------------------------------------------------------------
110
bool vtkPlotLine::PaintLegend(vtkContext2D *painter, const vtkRectf& rect, int)
111 112 113 114
{
  painter->ApplyPen(this->Pen);
  painter->DrawLine(rect[0], rect[1]+0.5*rect[3],
                    rect[0]+rect[2], rect[1]+0.5*rect[3]);
115
  this->Superclass::PaintLegend(painter, rect, 0);
116 117 118
  return true;
}

119 120 121 122 123
//-----------------------------------------------------------------------------
void vtkPlotLine::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}