vtk-m2/vtkm/rendering/AxisAnnotation3D.h

244 lines
7.6 KiB
C
Raw Normal View History

2016-04-19 21:04:00 +00:00
//============================================================================
// 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.
//============================================================================
#ifndef vtk_m_rendering_AxisAnnotation3D_h
#define vtk_m_rendering_AxisAnnotation3D_h
#include <vtkm/cont/DataSet.h>
2016-06-02 19:04:01 +00:00
#include <vtkm/rendering/Camera.h>
2016-04-19 21:04:00 +00:00
#include <vtkm/rendering/Color.h>
#include <vtkm/rendering/Scene.h>
#include <vtkm/rendering/WorldAnnotator.h>
#include <vtkm/rendering/AxisAnnotation.h>
#include <vtkm/rendering/TextAnnotation.h>
2016-04-19 21:04:00 +00:00
#include <sstream>
2016-04-19 21:04:00 +00:00
namespace vtkm {
namespace rendering {
class AxisAnnotation3D : public AxisAnnotation
{
private:
protected:
vtkm::Float64 maj_size, maj_toff;
vtkm::Float64 min_size, min_toff;
2016-04-19 21:04:00 +00:00
int axis;
vtkm::Float32 invertx, inverty, invertz;
vtkm::Float64 x0, y0, z0, x1, y1, z1;
vtkm::Float64 lower, upper;
vtkm::Float64 fontscale;
vtkm::Float32 fontoffset;
vtkm::Float32 linewidth;
vtkm::rendering::Color color;
std::vector<BillboardTextAnnotation*> labels;
2016-04-19 21:04:00 +00:00
int moreOrLessTickAdjustment;
public:
AxisAnnotation3D() : AxisAnnotation()
{
axis = 0;
color = Color(1,1,1);
fontoffset = 0.1f; // world space offset from axis
fontscale = 0.05; // screen space font size
linewidth = 1.0;
color = Color(1,1,1);
moreOrLessTickAdjustment = 0;
2016-04-19 21:04:00 +00:00
}
virtual ~AxisAnnotation3D()
{
}
void SetMoreOrLessTickAdjustment(int offset)
{
moreOrLessTickAdjustment = offset;
}
void SetColor(vtkm::rendering::Color c)
2016-04-19 21:04:00 +00:00
{
color = c;
}
void SetAxis(int a)
{
axis = a;
}
void SetTickInvert(bool x, bool y, bool z)
{
invertx = x ? +1.0f : -1.0f;
inverty = y ? +1.0f : -1.0f;
invertz = z ? +1.0f : -1.0f;
2016-04-19 21:04:00 +00:00
}
void SetMajorTickSize(vtkm::Float64 size, vtkm::Float64 offset)
2016-04-19 21:04:00 +00:00
{
/// offset of 0 means the tick is inside the frame
/// offset of 1 means the tick is outside the frame
/// offset of 0.5 means the tick is centered on the frame
maj_size = size;
maj_toff = offset;
}
void SetMinorTickSize(vtkm::Float64 size, vtkm::Float64 offset)
2016-04-19 21:04:00 +00:00
{
min_size = size;
min_toff = offset;
}
void SetWorldPosition(vtkm::Float64 x0_, vtkm::Float64 y0_, vtkm::Float64 z0_,
vtkm::Float64 x1_, vtkm::Float64 y1_, vtkm::Float64 z1_)
2016-04-19 21:04:00 +00:00
{
x0 = x0_;
y0 = y0_;
z0 = z0_;
x1 = x1_;
y1 = y1_;
z1 = z1_;
}
void SetLabelFontScale(vtkm::Float64 s)
2016-04-19 21:04:00 +00:00
{
fontscale = s;
for (unsigned int i=0; i<labels.size(); i++)
labels[i]->SetScale(vtkm::Float32(s));
}
void SetLabelFontOffset(vtkm::Float32 off)
{
fontoffset = off;
2016-04-19 21:04:00 +00:00
}
void SetRange(vtkm::Float64 l, vtkm::Float64 u)
2016-04-19 21:04:00 +00:00
{
lower = l;
upper = u;
}
virtual void Render(const vtkm::rendering::Camera &camera,
const vtkm::rendering::WorldAnnotator &worldAnnotator,
vtkm::rendering::Canvas &canvas)
2016-04-19 21:04:00 +00:00
{
bool infront = true;
worldAnnotator.AddLine(x0,y0,z0,
2016-04-19 21:04:00 +00:00
x1,y1,z1,
linewidth, color, infront);
std::vector<vtkm::Float64> positions;
std::vector<vtkm::Float64> proportions;
2016-04-19 21:04:00 +00:00
// major ticks
CalculateTicks(lower, upper, false, positions, proportions, moreOrLessTickAdjustment);
unsigned int nmajor = (unsigned int)proportions.size();
while (labels.size() < nmajor)
2016-04-19 21:04:00 +00:00
{
labels.push_back(new BillboardTextAnnotation("test",
color,
vtkm::Float32(fontscale),
0,0,0,
0));
2016-04-19 21:04:00 +00:00
}
std::stringstream numberToString;
2016-04-19 21:04:00 +00:00
for (unsigned int i=0; i<nmajor; ++i)
{
vtkm::Float64 xc = x0 + (x1-x0) * proportions[i];
vtkm::Float64 yc = y0 + (y1-y0) * proportions[i];
vtkm::Float64 zc = z0 + (z1-z0) * proportions[i];
2016-04-19 21:04:00 +00:00
for (int pass=0; pass<=1; pass++)
{
vtkm::Float64 tx=0, ty=0, tz=0;
2016-04-19 21:04:00 +00:00
switch (axis)
{
case 0: if (pass==0) ty=maj_size; else tz=maj_size; break;
case 1: if (pass==0) tx=maj_size; else tz=maj_size; break;
case 2: if (pass==0) tx=maj_size; else ty=maj_size; break;
}
tx *= invertx;
ty *= inverty;
tz *= invertz;
vtkm::Float64 xs = xc - tx*maj_toff;
vtkm::Float64 xe = xc + tx*(1. - maj_toff);
vtkm::Float64 ys = yc - ty*maj_toff;
vtkm::Float64 ye = yc + ty*(1. - maj_toff);
vtkm::Float64 zs = zc - tz*maj_toff;
vtkm::Float64 ze = zc + tz*(1. - maj_toff);
2016-04-19 21:04:00 +00:00
worldAnnotator.AddLine(xs,ys,zs,
xe,ye,ze,
linewidth, color, infront);
2016-04-19 21:04:00 +00:00
}
vtkm::Float32 tx=0, ty=0, tz=0;
const vtkm::Float32 s = 0.4f;
2016-04-19 21:04:00 +00:00
switch (axis)
{
case 0: ty=s*fontoffset; tz=s*fontoffset; break;
case 1: tx=s*fontoffset; tz=s*fontoffset; break;
case 2: tx=s*fontoffset; ty=s*fontoffset; break;
2016-04-19 21:04:00 +00:00
}
tx *= invertx;
ty *= inverty;
tz *= invertz;
numberToString.str("");
numberToString << positions[i];
labels[i]->SetText(numberToString.str());
2016-04-19 21:04:00 +00:00
//if (fabs(positions[i]) < 1e-10)
// labels[i]->SetText("0");
labels[i]->SetPosition(vtkm::Float32(xc - tx),
vtkm::Float32(yc - ty),
vtkm::Float32(zc - tz));
2016-04-19 21:04:00 +00:00
labels[i]->SetAlignment(TextAnnotation::HCenter,
TextAnnotation::VCenter);
}
// minor ticks
CalculateTicks(lower, upper, true, positions, proportions, moreOrLessTickAdjustment);
unsigned int nminor = (unsigned int)proportions.size();
for (unsigned int i=0; i<nminor; ++i)
{
vtkm::Float64 xc = x0 + (x1-x0) * proportions[i];
vtkm::Float64 yc = y0 + (y1-y0) * proportions[i];
vtkm::Float64 zc = z0 + (z1-z0) * proportions[i];
2016-04-19 21:04:00 +00:00
for (int pass=0; pass<=1; pass++)
{
vtkm::Float64 tx=0, ty=0, tz=0;
2016-04-19 21:04:00 +00:00
switch (axis)
{
case 0: if (pass==0) ty=min_size; else tz=min_size; break;
case 1: if (pass==0) tx=min_size; else tz=min_size; break;
case 2: if (pass==0) tx=min_size; else ty=min_size; break;
}
tx *= invertx;
ty *= inverty;
tz *= invertz;
vtkm::Float64 xs = xc - tx*min_toff;
vtkm::Float64 xe = xc + tx*(1. - min_toff);
vtkm::Float64 ys = yc - ty*min_toff;
vtkm::Float64 ye = yc + ty*(1. - min_toff);
vtkm::Float64 zs = zc - tz*min_toff;
vtkm::Float64 ze = zc + tz*(1. - min_toff);
2016-04-19 21:04:00 +00:00
worldAnnotator.AddLine(xs,ys,zs,
2016-04-19 21:04:00 +00:00
xe,ye,ze,
linewidth, color, infront);
}
}
for (unsigned int i=0; i<nmajor; ++i)
{
labels[i]->Render(camera, worldAnnotator, canvas);
2016-04-19 21:04:00 +00:00
}
}
};
}} //namespace vtkm::rendering
#endif // vtk_m_rendering_AxisAnnotation3D_h