mirror of
https://gitlab.kitware.com/vtk/vtk-m
synced 2024-09-16 17:22:55 +00:00
Extract canvas's line rendering to LineRenderer
This allows for the line rendering to be shared for other use cases like `WorldAnnotator`
This commit is contained in:
parent
3051d88de0
commit
3e5878394e
@ -35,6 +35,7 @@ set(headers
|
||||
ColorTable.h
|
||||
ConnectivityProxy.h
|
||||
DecodePNG.h
|
||||
LineRenderer.h
|
||||
MatrixHelpers.h
|
||||
Scene.h
|
||||
Mapper.h
|
||||
@ -71,6 +72,7 @@ set(sources
|
||||
ColorTable.cxx
|
||||
ConnectivityProxy.cxx
|
||||
DecodePNG.cxx
|
||||
LineRenderer.cxx
|
||||
Mapper.cxx
|
||||
MapperRayTracer.cxx
|
||||
MapperVolume.cxx
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include <vtkm/cont/TryExecute.h>
|
||||
#include <vtkm/rendering/BitmapFontFactory.h>
|
||||
#include <vtkm/rendering/DecodePNG.h>
|
||||
#include <vtkm/rendering/LineRenderer.h>
|
||||
#include <vtkm/rendering/TextRenderer.h>
|
||||
#include <vtkm/worklet/DispatcherMapField.h>
|
||||
#include <vtkm/worklet/WorkletMapField.h>
|
||||
@ -225,6 +226,7 @@ Canvas::Canvas(vtkm::Id width, vtkm::Id height)
|
||||
: Width(0)
|
||||
, Height(0)
|
||||
{
|
||||
vtkm::MatrixIdentity(Transform);
|
||||
this->ResizeBuffers(width, height);
|
||||
}
|
||||
|
||||
@ -291,41 +293,12 @@ void Canvas::AddColorSwatch(const vtkm::Float64 x0,
|
||||
|
||||
void Canvas::AddLine(const vtkm::Vec<vtkm::Float64, 2>& point0,
|
||||
const vtkm::Vec<vtkm::Float64, 2>& point1,
|
||||
vtkm::Float32 vtkmNotUsed(linewidth),
|
||||
vtkm::Float32 linewidth,
|
||||
const vtkm::rendering::Color& color) const
|
||||
{
|
||||
const vtkm::Float32 width = static_cast<vtkm::Float32>(this->Width);
|
||||
const vtkm::Float32 height = static_cast<vtkm::Float32>(this->Height);
|
||||
vtkm::Id x0 = static_cast<vtkm::Id>(vtkm::Round((point0[0] * 0.5f + 0.5f) * width));
|
||||
vtkm::Id y0 = static_cast<vtkm::Id>(vtkm::Round((point0[1] * 0.5f + 0.5f) * height));
|
||||
vtkm::Id x1 = static_cast<vtkm::Id>(vtkm::Round((point1[0] * 0.5f + 0.5f) * width));
|
||||
vtkm::Id y1 = static_cast<vtkm::Id>(vtkm::Round((point1[1] * 0.5f + 0.5f) * height));
|
||||
vtkm::Id dx = vtkm::Abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
|
||||
vtkm::Id dy = -vtkm::Abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
|
||||
vtkm::Id err = dx + dy, err2 = 0;
|
||||
ColorBufferType::PortalControl colorPortal =
|
||||
ColorBufferType(this->ColorBuffer).GetPortalControl();
|
||||
|
||||
while (true)
|
||||
{
|
||||
vtkm::Id index = y0 * this->Width + x0;
|
||||
colorPortal.Set(index, color.Components);
|
||||
if (x0 == x1 && y0 == y1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
err2 = err * 2;
|
||||
if (err2 >= dy)
|
||||
{
|
||||
err += dy;
|
||||
x0 += sx;
|
||||
}
|
||||
if (err2 <= dx)
|
||||
{
|
||||
err += dx;
|
||||
y0 += sy;
|
||||
}
|
||||
}
|
||||
vtkm::rendering::Canvas* self = const_cast<vtkm::rendering::Canvas*>(this);
|
||||
LineRenderer renderer(self, Transform);
|
||||
renderer.RenderLine(point0, point1, linewidth, color);
|
||||
}
|
||||
|
||||
void Canvas::AddLine(vtkm::Float64 x0,
|
||||
@ -456,6 +429,20 @@ bool Canvas::LoadFont() const
|
||||
return true;
|
||||
}
|
||||
|
||||
void Canvas::SetViewToWorldSpace(const vtkm::rendering::Camera& camera, bool clip)
|
||||
{
|
||||
Transform = vtkm::MatrixMultiply(camera.CreateProjectionMatrix(this->Width, this->Height),
|
||||
camera.CreateViewMatrix());
|
||||
}
|
||||
|
||||
void Canvas::SetViewToScreenSpace(const vtkm::rendering::Camera& camera, bool clip)
|
||||
{
|
||||
vtkm::Matrix<vtkm::Float32, 4, 4> projection;
|
||||
vtkm::MatrixIdentity(projection);
|
||||
projection[2][2] = -1.0f;
|
||||
Transform = projection;
|
||||
}
|
||||
|
||||
void Canvas::SaveAs(const std::string& fileName) const
|
||||
{
|
||||
this->RefreshColorBuffer();
|
||||
|
@ -22,6 +22,7 @@
|
||||
|
||||
#include <vtkm/rendering/vtkm_rendering_export.h>
|
||||
|
||||
#include <vtkm/Matrix.h>
|
||||
#include <vtkm/Types.h>
|
||||
#include <vtkm/rendering/BitmapFont.h>
|
||||
#include <vtkm/rendering/Camera.h>
|
||||
@ -112,8 +113,8 @@ public:
|
||||
virtual void RefreshColorBuffer() const {}
|
||||
virtual void RefreshDepthBuffer() const {}
|
||||
|
||||
virtual void SetViewToWorldSpace(const vtkm::rendering::Camera&, bool) {}
|
||||
virtual void SetViewToScreenSpace(const vtkm::rendering::Camera&, bool) {}
|
||||
virtual void SetViewToWorldSpace(const vtkm::rendering::Camera& camera, bool clip);
|
||||
virtual void SetViewToScreenSpace(const vtkm::rendering::Camera& camera, bool clip);
|
||||
virtual void SetViewportClipping(const vtkm::rendering::Camera&, bool) {}
|
||||
|
||||
virtual void SaveAs(const std::string& fileName) const;
|
||||
@ -206,6 +207,7 @@ private:
|
||||
DepthBufferType DepthBuffer;
|
||||
mutable vtkm::rendering::BitmapFont Font;
|
||||
mutable FontTextureType FontTexture;
|
||||
vtkm::Matrix<vtkm::Float32, 4, 4> Transform;
|
||||
};
|
||||
}
|
||||
} //namespace vtkm::rendering
|
||||
|
119
vtkm/rendering/LineRenderer.cxx
Normal file
119
vtkm/rendering/LineRenderer.cxx
Normal file
@ -0,0 +1,119 @@
|
||||
//============================================================================
|
||||
// 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 2016 Sandia Corporation.
|
||||
// Copyright 2016 UT-Battelle, LLC.
|
||||
// Copyright 2016 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.
|
||||
//============================================================================
|
||||
|
||||
#include <vtkm/rendering/LineRenderer.h>
|
||||
|
||||
#include <vtkm/Transform3D.h>
|
||||
#include <vtkm/cont/TryExecute.h>
|
||||
#include <vtkm/worklet/DispatcherMapField.h>
|
||||
#include <vtkm/worklet/WorkletMapField.h>
|
||||
|
||||
namespace vtkm
|
||||
{
|
||||
namespace rendering
|
||||
{
|
||||
|
||||
LineRenderer::LineRenderer(vtkm::rendering::Canvas* canvas,
|
||||
vtkm::Matrix<vtkm::Float32, 4, 4> transform)
|
||||
: Canvas(canvas)
|
||||
, Transform(transform)
|
||||
{
|
||||
}
|
||||
|
||||
void LineRenderer::RenderLine(const vtkm::Vec<vtkm::Float64, 2>& point0,
|
||||
const vtkm::Vec<vtkm::Float64, 2>& point1,
|
||||
vtkm::Float32 lineWidth,
|
||||
const vtkm::rendering::Color& color)
|
||||
{
|
||||
RenderLine(vtkm::make_Vec(point0[0], point0[1], 0.0),
|
||||
vtkm::make_Vec(point1[0], point1[1], 0.0),
|
||||
lineWidth,
|
||||
color);
|
||||
}
|
||||
|
||||
void LineRenderer::RenderLine(const vtkm::Vec<vtkm::Float64, 3>& point0,
|
||||
const vtkm::Vec<vtkm::Float64, 3>& point1,
|
||||
vtkm::Float32 vtkmNotUsed(lineWidth),
|
||||
const vtkm::rendering::Color& color)
|
||||
{
|
||||
vtkm::Vec<vtkm::Float32, 3> p0 = TransformPoint(point0);
|
||||
vtkm::Vec<vtkm::Float32, 3> p1 = TransformPoint(point1);
|
||||
|
||||
vtkm::Id x0 = static_cast<vtkm::Id>(vtkm::Round(p0[0]));
|
||||
vtkm::Id y0 = static_cast<vtkm::Id>(vtkm::Round(p0[1]));
|
||||
vtkm::Float32 z0 = static_cast<vtkm::Float32>(p0[2]);
|
||||
vtkm::Id x1 = static_cast<vtkm::Id>(vtkm::Round(p1[0]));
|
||||
vtkm::Id y1 = static_cast<vtkm::Id>(vtkm::Round(p1[1]));
|
||||
vtkm::Float32 z1 = static_cast<vtkm::Float32>(p1[2]);
|
||||
vtkm::Id dx = vtkm::Abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
|
||||
vtkm::Id dy = -vtkm::Abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
|
||||
vtkm::Id err = dx + dy, err2 = 0;
|
||||
vtkm::rendering::Canvas::ColorBufferType::PortalControl colorPortal =
|
||||
Canvas->GetColorBuffer().GetPortalControl();
|
||||
vtkm::rendering::Canvas::DepthBufferType::PortalControl depthPortal =
|
||||
Canvas->GetDepthBuffer().GetPortalControl();
|
||||
vtkm::Vec<vtkm::Float32, 4> colorC = color.Components;
|
||||
while (true)
|
||||
{
|
||||
vtkm::Float32 t = (dx == 0) ? 1.0f : (static_cast<vtkm::Float32>(x0) - p0[0]) / (p1[0] - p0[0]);
|
||||
vtkm::Float32 z = vtkm::Lerp(z0, z1, t);
|
||||
vtkm::Id index = y0 * Canvas->GetWidth() + x0;
|
||||
if (depthPortal.Get(index) >= z)
|
||||
{
|
||||
depthPortal.Set(index, z);
|
||||
colorPortal.Set(index, colorC);
|
||||
}
|
||||
if (x0 == x1 && y0 == y1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
err2 = err * 2;
|
||||
if (err2 >= dy)
|
||||
{
|
||||
err += dy;
|
||||
x0 += sx;
|
||||
}
|
||||
if (err2 <= dx)
|
||||
{
|
||||
err += dx;
|
||||
y0 += sy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vtkm::Vec<vtkm::Float32, 3> LineRenderer::TransformPoint(
|
||||
const vtkm::Vec<vtkm::Float64, 3>& point) const
|
||||
{
|
||||
vtkm::Vec<vtkm::Float32, 4> temp(static_cast<vtkm::Float32>(point[0]),
|
||||
static_cast<vtkm::Float32>(point[1]),
|
||||
static_cast<vtkm::Float32>(point[2]),
|
||||
1.0f);
|
||||
temp = vtkm::MatrixMultiply(Transform, temp);
|
||||
vtkm::Vec<vtkm::Float32, 3> p;
|
||||
for (vtkm::IdComponent i = 0; i < 3; ++i)
|
||||
{
|
||||
p[i] = static_cast<vtkm::Float32>(temp[i] / temp[3]);
|
||||
}
|
||||
p[0] = (p[0] * 0.5f + 0.5f) * static_cast<vtkm::Float32>(Canvas->GetWidth());
|
||||
p[1] = (p[1] * 0.5f + 0.5f) * static_cast<vtkm::Float32>(Canvas->GetHeight());
|
||||
return p;
|
||||
}
|
||||
}
|
||||
} // namespace vtkm::rendering
|
62
vtkm/rendering/LineRenderer.h
Normal file
62
vtkm/rendering/LineRenderer.h
Normal file
@ -0,0 +1,62 @@
|
||||
//============================================================================
|
||||
// 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 2017 Sandia Corporation.
|
||||
// Copyright 2017 UT-Battelle, LLC.
|
||||
// Copyright 2017 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_LineRenderer_h
|
||||
#define vtk_m_rendering_LineRenderer_h
|
||||
|
||||
#include <vtkm/Matrix.h>
|
||||
#include <vtkm/rendering/Canvas.h>
|
||||
#include <vtkm/rendering/Color.h>
|
||||
#include <vtkm/rendering/vtkm_rendering_export.h>
|
||||
|
||||
namespace vtkm
|
||||
{
|
||||
namespace rendering
|
||||
{
|
||||
|
||||
class VTKM_RENDERING_EXPORT LineRenderer
|
||||
{
|
||||
public:
|
||||
VTKM_CONT
|
||||
LineRenderer(vtkm::rendering::Canvas* canvas, vtkm::Matrix<vtkm::Float32, 4, 4> transform);
|
||||
|
||||
VTKM_CONT
|
||||
void RenderLine(const vtkm::Vec<vtkm::Float64, 2>& point0,
|
||||
const vtkm::Vec<vtkm::Float64, 2>& point1,
|
||||
vtkm::Float32 lineWidth,
|
||||
const vtkm::rendering::Color& color);
|
||||
|
||||
VTKM_CONT
|
||||
void RenderLine(const vtkm::Vec<vtkm::Float64, 3>& point0,
|
||||
const vtkm::Vec<vtkm::Float64, 3>& point1,
|
||||
vtkm::Float32 lineWidth,
|
||||
const vtkm::rendering::Color& color);
|
||||
|
||||
private:
|
||||
VTKM_CONT
|
||||
vtkm::Vec<vtkm::Float32, 3> TransformPoint(const vtkm::Vec<vtkm::Float64, 3>& point) const;
|
||||
|
||||
vtkm::rendering::Canvas* Canvas;
|
||||
vtkm::Matrix<vtkm::Float32, 4, 4> Transform;
|
||||
}; // class LineRenderer
|
||||
}
|
||||
} // namespace vtkm::rendering
|
||||
|
||||
#endif // vtk_m_rendering_LineRenderer_h
|
Loading…
Reference in New Issue
Block a user