vtk-m/vtkm/rendering/Canvas.h

188 lines
5.7 KiB
C
Raw Normal View History

//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt 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.
//
// Copyright 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_rendering_Canvas_h
#define vtk_m_rendering_Canvas_h
#include <vtkm/Types.h>
2016-06-02 19:04:01 +00:00
#include <vtkm/rendering/Camera.h>
2016-02-10 22:06:38 +00:00
#include <vtkm/rendering/Color.h>
2016-05-18 19:29:50 +00:00
#include <vtkm/rendering/ColorTable.h>
#include <vtkm/rendering/WorldAnnotator.h>
2016-02-10 22:06:38 +00:00
#include <iostream>
#include <fstream>
namespace vtkm {
namespace rendering {
class Canvas
{
public:
2016-06-01 17:37:03 +00:00
VTKM_CONT_EXPORT
Canvas(vtkm::Id width=1024,
vtkm::Id height=1024,
const vtkm::rendering::Color &color =
vtkm::rendering::Color(0.0f,0.0f,0.0f,1.0f))
: Width(0), Height(0), BackgroundColor(color)
2016-06-01 17:37:03 +00:00
{
this->ResizeBuffers(width, height);
2016-06-01 17:37:03 +00:00
}
virtual ~Canvas() { }
2016-06-01 17:37:03 +00:00
VTKM_CONT_EXPORT
virtual void Initialize() = 0;
2016-06-01 17:37:03 +00:00
VTKM_CONT_EXPORT
virtual void Activate() = 0;
2016-06-01 17:37:03 +00:00
VTKM_CONT_EXPORT
virtual void Clear() = 0;
2016-06-01 17:37:03 +00:00
VTKM_CONT_EXPORT
virtual void Finish() = 0;
2016-06-05 05:31:10 +00:00
typedef vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::Float32,4> > ColorBufferType;
typedef vtkm::cont::ArrayHandle<vtkm::Float32> DepthBufferType;
VTKM_CONT_EXPORT
vtkm::Id GetWidth() const { return this->Width; }
VTKM_CONT_EXPORT
vtkm::Id GetHeight() const { return this->Height; }
VTKM_CONT_EXPORT
const ColorBufferType &GetColorBuffer() const { return this->ColorBuffer; }
VTKM_CONT_EXPORT
ColorBufferType &GetColorBuffer() { return this->ColorBuffer; }
VTKM_CONT_EXPORT
const DepthBufferType &GetDepthBuffer() const { return this->DepthBuffer; }
VTKM_CONT_EXPORT
DepthBufferType &GetDepthBuffer() { return this->DepthBuffer; }
VTKM_CONT_EXPORT
void ResizeBuffers(vtkm::Id width, vtkm::Id height)
{
VTKM_ASSERT(width >= 0);
VTKM_ASSERT(height >= 0);
vtkm::Id numPixels = width*height;
if (this->ColorBuffer.GetNumberOfValues() != numPixels)
{
this->ColorBuffer.Allocate(numPixels);
}
if (this->DepthBuffer.GetNumberOfValues() != numPixels)
{
this->DepthBuffer.Allocate(numPixels);
}
this->Width = width;
this->Height = height;
}
2016-06-05 05:31:10 +00:00
VTKM_CONT_EXPORT
vtkm::rendering::Color GetBackgroundColor() const
{
return this->BackgroundColor;
}
VTKM_CONT_EXPORT
void SetBackgroundColor(const vtkm::rendering::Color &color)
{
this->BackgroundColor = color;
}
// If a subclass uses a system that renderers to different buffers, then
// these should be overridden to copy the data to the buffers.
VTKM_CONT_EXPORT
virtual void RefreshColorBuffer() { }
VTKM_CONT_EXPORT
virtual void RefreshDepthBuffer() { }
2016-06-01 17:37:03 +00:00
VTKM_CONT_EXPORT
virtual void SetViewToWorldSpace(const vtkm::rendering::Camera &, bool) {}
2016-06-01 17:37:03 +00:00
VTKM_CONT_EXPORT
virtual void SetViewToScreenSpace(const vtkm::rendering::Camera &, bool) {}
2016-06-01 17:37:03 +00:00
VTKM_CONT_EXPORT
void SetViewportClipping(const vtkm::rendering::Camera &, bool) {}
2016-02-10 22:06:38 +00:00
2016-06-01 17:37:03 +00:00
VTKM_CONT_EXPORT
virtual void SaveAs(const std::string &fileName)
{
this->RefreshColorBuffer();
std::ofstream of(fileName.c_str());
of<<"P6"<<std::endl<<this->Width<<" "<<this->Height<<std::endl<<255<<std::endl;
ColorBufferType::PortalConstControl colorPortal =
this->ColorBuffer.GetPortalConstControl();
for (vtkm::Id yIndex=this->Height-1; yIndex>=0; yIndex--)
{
for (vtkm::Id xIndex=0; xIndex < this->Width; xIndex++)
{
vtkm::Vec<vtkm::Float32,4> tuple =
colorPortal.Get(yIndex*this->Width + xIndex);
of<<(unsigned char)(tuple[0]*255);
of<<(unsigned char)(tuple[1]*255);
of<<(unsigned char)(tuple[2]*255);
}
}
of.close();
}
2016-06-01 17:37:03 +00:00
virtual void AddLine(vtkm::Float64, vtkm::Float64,
vtkm::Float64, vtkm::Float64,
vtkm::Float32,
const vtkm::rendering::Color &) const {}
2016-06-01 17:37:03 +00:00
virtual void AddColorBar(vtkm::Float32, vtkm::Float32,
2016-06-02 19:04:01 +00:00
vtkm::Float32, vtkm::Float32,
2016-06-01 17:37:03 +00:00
const vtkm::rendering::ColorTable &,
bool) const {}
2016-06-01 17:37:03 +00:00
virtual void AddText(vtkm::Float32, vtkm::Float32,
vtkm::Float32,
vtkm::Float32,
vtkm::Float32,
vtkm::Float32, vtkm::Float32,
Color,
std::string) const {}
/// Creates a WorldAnnotator of a type that is paired with this Canvas. Other
/// types of world annotators might work, but this provides a default.
///
/// The WorldAnnotator is created with the C++ new keyword (so it should be
/// deleted with delete later). A pointer to the created WorldAnnotator is
/// returned.
///
VTKM_CONT_EXPORT
virtual vtkm::rendering::WorldAnnotator *CreateWorldAnnotator() const
{
return new vtkm::rendering::WorldAnnotator;
}
2016-06-05 05:31:10 +00:00
private:
vtkm::Id Width;
vtkm::Id Height;
2016-06-01 17:37:03 +00:00
vtkm::rendering::Color BackgroundColor;
ColorBufferType ColorBuffer;
DepthBufferType DepthBuffer;
};
}} //namespace vtkm::rendering
#endif //vtk_m_rendering_Canvas_h