adding finer grain control over color bar and scalar field label

This commit is contained in:
Matt Larsen 2018-01-18 13:21:39 -08:00
parent 3de1717ba8
commit 80081f2a1b
4 changed files with 72 additions and 5 deletions

@ -19,6 +19,7 @@
//============================================================================
#include <vtkm/rendering/ColorBarAnnotation.h>
#include <vtkm/rendering/TextAnnotationScreen.h>
namespace vtkm
{
@ -27,12 +28,32 @@ namespace rendering
ColorBarAnnotation::ColorBarAnnotation()
{
vtkm::Bounds bounds(vtkm::Range(-0.88, +0.88), vtkm::Range(+0.87, +0.92), vtkm::Range(0, 0));
Position = bounds;
Horizontal = true;
FieldName = "";
}
ColorBarAnnotation::~ColorBarAnnotation()
{
}
void ColorBarAnnotation::SetFieldName(const std::string& fieldName)
{
FieldName = fieldName;
}
void ColorBarAnnotation::SetPosition(const vtkm::Bounds& position)
{
Position = position;
vtkm::Float64 x = Position.X.Length();
vtkm::Float64 y = Position.Y.Length();
if (x > y)
Horizontal = true;
else
Horizontal = false;
}
void ColorBarAnnotation::SetRange(const vtkm::Range& range, vtkm::IdComponent numTicks)
{
std::vector<vtkm::Float64> positions, proportions;
@ -52,17 +73,50 @@ void ColorBarAnnotation::Render(const vtkm::rendering::Camera& camera,
const vtkm::rendering::WorldAnnotator& worldAnnotator,
vtkm::rendering::Canvas& canvas)
{
vtkm::Bounds bounds(vtkm::Range(-0.88, +0.88), vtkm::Range(+0.87, +0.92), vtkm::Range(0, 0));
canvas.AddColorBar(bounds, this->ColorTable, true);
canvas.AddColorBar(Position, this->ColorTable, Horizontal);
this->Axis.SetColor(canvas.GetForegroundColor());
this->Axis.SetLineWidth(1);
this->Axis.SetScreenPosition(bounds.X.Min, bounds.Y.Min, bounds.X.Max, bounds.Y.Min);
this->Axis.SetMajorTickSize(0, .02, 1.0);
if (Horizontal)
{
this->Axis.SetScreenPosition(Position.X.Min, Position.Y.Min, Position.X.Max, Position.Y.Min);
this->Axis.SetLabelAlignment(TextAnnotation::HCenter, TextAnnotation::Top);
this->Axis.SetMajorTickSize(0, .02, 1.0);
}
else
{
this->Axis.SetScreenPosition(Position.X.Min, Position.Y.Min, Position.X.Min, Position.Y.Max);
this->Axis.SetLabelAlignment(TextAnnotation::Right, TextAnnotation::VCenter);
this->Axis.SetMajorTickSize(.02, 0.0, 1.0);
}
this->Axis.SetMinorTickSize(0, 0, 0); // no minor ticks
this->Axis.SetLabelAlignment(TextAnnotation::HCenter, TextAnnotation::Top);
this->Axis.Render(camera, worldAnnotator, canvas);
if (FieldName != "")
{
vtkm::Vec<vtkm::Float32, 2> labelPos;
if (Horizontal)
{
labelPos[0] = vtkm::Float32(Position.X.Min);
labelPos[1] = vtkm::Float32(Position.Y.Max);
}
else
{
labelPos[0] = vtkm::Float32(Position.X.Min - 0.07);
labelPos[1] = vtkm::Float32(Position.Y.Max + 0.03);
}
vtkm::rendering::TextAnnotationScreen var(FieldName,
canvas.GetForegroundColor(),
.045f, // font scale
labelPos,
0.f); // rotation
var.Render(camera, worldAnnotator, canvas);
}
}
}
} // namespace vtkm::rendering

@ -38,6 +38,9 @@ class VTKM_RENDERING_EXPORT ColorBarAnnotation
protected:
vtkm::rendering::ColorTable ColorTable;
vtkm::rendering::AxisAnnotation2D Axis;
vtkm::Bounds Position;
bool Horizontal;
std::string FieldName;
public:
ColorBarAnnotation();
@ -50,14 +53,22 @@ public:
this->ColorTable = colorTable;
}
VTKM_CONT
void SetRange(const vtkm::Range& range, vtkm::IdComponent numTicks);
VTKM_CONT
void SetFieldName(const std::string& fieldName);
VTKM_CONT
void SetRange(vtkm::Float64 l, vtkm::Float64 h, vtkm::IdComponent numTicks)
{
this->SetRange(vtkm::Range(l, h), numTicks);
}
VTKM_CONT
void SetPosition(const vtkm::Bounds& position);
virtual void Render(const vtkm::rendering::Camera& camera,
const vtkm::rendering::WorldAnnotator& worldAnnotator,
vtkm::rendering::Canvas& canvas);

@ -102,6 +102,7 @@ void View2D::RenderScreenAnnotations()
if (scene.GetNumberOfActors() > 0)
{
//this->ColorBarAnnotation.SetAxisColor(vtkm::rendering::Color(1,1,1));
this->ColorBarAnnotation.SetFieldName(scene.GetActor(0).GetScalarField().GetName());
this->ColorBarAnnotation.SetRange(
scene.GetActor(0).GetScalarRange().Min, scene.GetActor(0).GetScalarRange().Max, 5);
this->ColorBarAnnotation.SetColorTable(scene.GetActor(0).GetColorTable());

@ -69,6 +69,7 @@ void View3D::RenderScreenAnnotations()
if (this->GetScene().GetNumberOfActors() > 0)
{
//this->ColorBarAnnotation.SetAxisColor(vtkm::rendering::Color(1,1,1));
this->ColorBarAnnotation.SetFieldName(this->GetScene().GetActor(0).GetScalarField().GetName());
this->ColorBarAnnotation.SetRange(this->GetScene().GetActor(0).GetScalarRange(), 5);
this->ColorBarAnnotation.SetColorTable(this->GetScene().GetActor(0).GetColorTable());
this->ColorBarAnnotation.Render(