vtkPlotSurface.cxx 8.97 KB
Newer Older
Zack Galbreath's avatar
Zack Galbreath committed
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkPlotSurface.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 "vtkChartXYZ.h"
#include "vtkContext2D.h"
#include "vtkContext3D.h"
#include "vtkLookupTable.h"
#include "vtkObjectFactory.h"
#include "vtkNew.h"
#include "vtkPen.h"
#include "vtkPlotSurface.h"
#include "vtkTable.h"
#include "vtkUnsignedCharArray.h"

//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkPlotSurface)

//-----------------------------------------------------------------------------
vtkPlotSurface::vtkPlotSurface()
{
  this->NumberOfRows = 0;
  this->NumberOfColumns = 0;
  this->NumberOfVertices = 0;
  this->ColorComponents = 0;
  this->XAxisLabel = "X";
  this->YAxisLabel = "Y";
  this->ZAxisLabel = "Z";
  this->XMinimum = this->XMaximum = this->YMinimum = this->YMaximum = 0.0;
  this->DataHasBeenRescaled = true;
}

//-----------------------------------------------------------------------------
vtkPlotSurface::~vtkPlotSurface()
{
}

//-----------------------------------------------------------------------------
void vtkPlotSurface::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

//-----------------------------------------------------------------------------
bool vtkPlotSurface::Paint(vtkContext2D *painter)
{
  if (!this->Visible)
    {
    return false;
    }

  if (!this->DataHasBeenRescaled)
    {
    this->RescaleData();
    }

  // Get the 3D context.
  vtkContext3D *context = painter->GetContext3D();

  if (!context)
    {
    return false;
    }

  context->ApplyPen(this->Pen.GetPointer());

  // draw the surface
  if (this->Surface.size() > 0)
    {
81 82 83 84
    context->DrawTriangleMesh(this->Surface[0].GetData(),
                              static_cast<int>(this->Surface.size()),
                              this->Colors->GetPointer(0),
                              this->ColorComponents);
Zack Galbreath's avatar
Zack Galbreath committed
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
    }

  return true;
}

//-----------------------------------------------------------------------------
void vtkPlotSurface::SetInputData(vtkTable *input)
{
  this->InputTable = input;
  this->NumberOfRows = input->GetNumberOfRows();
  this->NumberOfColumns = input->GetNumberOfColumns();
  this->NumberOfVertices =
    (this->NumberOfRows - 1) * (this->NumberOfColumns - 1) * 6;

  // initialize data ranges to row and column indices if they are not
  // already set.
  if (this->XMinimum == 0 && this->XMaximum == 0)
    {
    this->XMaximum = this->NumberOfColumns - 1;
    }
  if (this->YMinimum == 0 && this->YMaximum == 0)
    {
    this->YMaximum = this->NumberOfRows - 1;
    }

  this->Points.clear();
  this->Points.resize(this->NumberOfRows * this->NumberOfColumns);
  float *data = this->Points[0].GetData();
  int pos = 0;
  float surfaceMin = VTK_FLOAT_MAX;
  float surfaceMax = VTK_FLOAT_MIN;
  for (int i = 0; i < this->NumberOfRows; ++i)
    {
    for (int j = 0; j < this->NumberOfColumns; ++j)
      {
      // X (columns)
      data[pos] = this->ColumnToX(j);
      ++pos;

      // Y (rows)
      data[pos] = this->RowToY(i);
      ++pos;

      // Z (cell value)
      float k = input->GetValue(i, j).ToFloat();
      data[pos] = k;
      ++pos;

      if (k < surfaceMin)
        {
        surfaceMin = k;
        }
      if (k > surfaceMax)
        {
        surfaceMax = k;
        }
      }
    }

  if (this->Chart)
    {
    this->Chart->RecalculateBounds();
    }
  this->ComputeDataBounds();

  // setup lookup table
  this->LookupTable->SetNumberOfTableValues(256);
  this->LookupTable->SetRange(surfaceMin, surfaceMax);
  this->LookupTable->Build();
  this->ColorComponents = 3;

  // generate the surface that is used for rendering
157
  this->GenerateSurface();
Zack Galbreath's avatar
Zack Galbreath committed
158 159 160 161

  this->DataHasBeenRescaled = true;
}

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
//-----------------------------------------------------------------------------
void vtkPlotSurface::SetInputData(vtkTable *input,
                          const vtkStdString& vtkNotUsed(xName),
                          const vtkStdString& vtkNotUsed(yName),
                          const vtkStdString& vtkNotUsed(zName))
{
  vtkWarningMacro("Warning: parameters beyond vtkTable are ignored");
  this->SetInputData(input);
}

//-----------------------------------------------------------------------------
void vtkPlotSurface::SetInputData(vtkTable *input,
                          const vtkStdString& vtkNotUsed(xName),
                          const vtkStdString& vtkNotUsed(yName),
                          const vtkStdString& vtkNotUsed(zName),
                          const vtkStdString& vtkNotUsed(colorName))
{
  vtkWarningMacro("Warning: parameters beyond vtkTable are ignored");
  this->SetInputData(input);
}

//-----------------------------------------------------------------------------
void vtkPlotSurface::SetInputData(vtkTable *input,
                                  vtkIdType vtkNotUsed(xColumn),
                                  vtkIdType vtkNotUsed(yColumn),
                                  vtkIdType vtkNotUsed(zColumn))
{
  vtkWarningMacro("Warning: parameters beyond vtkTable are ignored");
  this->SetInputData(input);
}

Zack Galbreath's avatar
Zack Galbreath committed
193
//-----------------------------------------------------------------------------
194
void vtkPlotSurface::GenerateSurface()
Zack Galbreath's avatar
Zack Galbreath committed
195 196 197
{
  // clear out and initialize our surface & colors
  this->Surface.clear();
198
  this->Surface.resize(this->NumberOfVertices);
Zack Galbreath's avatar
Zack Galbreath committed
199 200 201
  this->Colors->Reset();
  this->Colors->Allocate(this->NumberOfVertices * 3);

202
  // collect vertices of triangles
Zack Galbreath's avatar
Zack Galbreath committed
203 204 205 206 207 208 209
  float *data = this->Surface[0].GetData();
  int pos = 0;
  for (int i = 0; i < this->NumberOfRows - 1; ++i)
    {
    for (int j = 0; j < this->NumberOfColumns - 1; ++j)
      {
      float value1 = this->InputTable->GetValue(i, j).ToFloat();
210
      float value2 = this->InputTable->GetValue(i, j + 1).ToFloat();
Zack Galbreath's avatar
Zack Galbreath committed
211
      float value3 = this->InputTable->GetValue(i + 1, j + 1).ToFloat();
212
      float value4 = this->InputTable->GetValue(i + 1, j).ToFloat();
Zack Galbreath's avatar
Zack Galbreath committed
213

214 215 216 217
      // bottom right triangle
      this->InsertSurfaceVertex(data, value1, i, j, pos);
      this->InsertSurfaceVertex(data, value2, i, j + 1, pos);
      this->InsertSurfaceVertex(data, value3, i + 1, j + 1, pos);
Zack Galbreath's avatar
Zack Galbreath committed
218

219 220 221 222
      // upper left triangle
      this->InsertSurfaceVertex(data, value1, i, j, pos);
      this->InsertSurfaceVertex(data, value3, i + 1, j + 1, pos);
      this->InsertSurfaceVertex(data, value4, i + 1, j, pos);
Zack Galbreath's avatar
Zack Galbreath committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
      }
    }
}

//-----------------------------------------------------------------------------
void vtkPlotSurface::InsertSurfaceVertex(float *data, float value, int i,
                                         int j, int &pos)
{
    data[pos] = this->ColumnToX(j);
    ++pos;
    data[pos] = this->RowToY(i);
    ++pos;
    data[pos] = value;
    ++pos;

    unsigned char *rgb = this->LookupTable->MapValue(data[pos-1]);
    const unsigned char constRGB[3] = { rgb[0], rgb[1], rgb[2] };
David C. Lonie's avatar
David C. Lonie committed
240 241 242
    this->Colors->InsertNextTypedTuple(&constRGB[0]);
    this->Colors->InsertNextTypedTuple(&constRGB[1]);
    this->Colors->InsertNextTypedTuple(&constRGB[2]);
Zack Galbreath's avatar
Zack Galbreath committed
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
}

//-----------------------------------------------------------------------------
void vtkPlotSurface::SetXRange(float min, float max)
{
  this->XMinimum = min;
  this->XMaximum = max;
  this->DataHasBeenRescaled = false;
}

//-----------------------------------------------------------------------------
void vtkPlotSurface::SetYRange(float min, float max)
{
  this->YMinimum = min;
  this->YMaximum = max;
  this->DataHasBeenRescaled = false;
}

//-----------------------------------------------------------------------------
void vtkPlotSurface::RescaleData()
{
  float *data = this->Points[0].GetData();

  // rescale Points (used by ChartXYZ to generate axes scales).
  int pos = 0;
  for (int i = 0; i < this->NumberOfRows; ++i)
    {
    for (int j = 0; j < this->NumberOfColumns; ++j)
      {
      // X (columns)
      data[pos] = this->ColumnToX(j);
      ++pos;

      // Y (rows)
      data[pos] = this->RowToY(i);
      ++pos;

      // Z value doesn't change
      ++pos;
      }
    }
  this->Chart->RecalculateBounds();
  this->ComputeDataBounds();
  this->DataHasBeenRescaled = true;
}

//-----------------------------------------------------------------------------
float vtkPlotSurface::ColumnToX(int columnIndex)
{
  float newRange = this->XMaximum - this->XMinimum;
  return static_cast<float>(columnIndex) * (newRange / this->NumberOfColumns) +
    this->XMinimum;
}

//-----------------------------------------------------------------------------
float vtkPlotSurface::RowToY(int rowIndex)
{
  float newRange = this->YMaximum - this->YMinimum;
  return static_cast<float>(rowIndex) * (newRange / this->NumberOfRows) +
    this->YMinimum;
}