1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10
11 #include <vtkm/rendering/ColorBarAnnotation.h>
12 #include <vtkm/rendering/TextAnnotationScreen.h>
13
14 namespace vtkm
15 {
16 namespace rendering
17 {
18
ColorBarAnnotation()19 ColorBarAnnotation::ColorBarAnnotation()
20 : ColorTable(vtkm::ColorSpace::Lab)
21 , Position(vtkm::Range(-0.88, +0.88), vtkm::Range(+0.87, +0.92), vtkm::Range(0, 0))
22 , Horizontal(true)
23 , FieldName("")
24 {
25 }
26
~ColorBarAnnotation()27 ColorBarAnnotation::~ColorBarAnnotation() {}
28
SetFieldName(const std::string & fieldName)29 void ColorBarAnnotation::SetFieldName(const std::string& fieldName)
30 {
31 FieldName = fieldName;
32 }
33
SetPosition(const vtkm::Bounds & position)34 void ColorBarAnnotation::SetPosition(const vtkm::Bounds& position)
35 {
36 Position = position;
37 vtkm::Float64 x = Position.X.Length();
38 vtkm::Float64 y = Position.Y.Length();
39 if (x > y)
40 Horizontal = true;
41 else
42 Horizontal = false;
43 }
44
SetRange(const vtkm::Range & range,vtkm::IdComponent numTicks)45 void ColorBarAnnotation::SetRange(const vtkm::Range& range, vtkm::IdComponent numTicks)
46 {
47 std::vector<vtkm::Float64> positions, proportions;
48 this->Axis.SetMinorTicks(positions, proportions); // clear any minor ticks
49
50 for (vtkm::IdComponent i = 0; i < numTicks; ++i)
51 {
52 vtkm::Float64 prop = static_cast<vtkm::Float64>(i) / static_cast<vtkm::Float64>(numTicks - 1);
53 vtkm::Float64 pos = range.Min + prop * range.Length();
54 positions.push_back(pos);
55 proportions.push_back(prop);
56 }
57 this->Axis.SetMajorTicks(positions, proportions);
58 }
59
Render(const vtkm::rendering::Camera & camera,const vtkm::rendering::WorldAnnotator & worldAnnotator,vtkm::rendering::Canvas & canvas)60 void ColorBarAnnotation::Render(const vtkm::rendering::Camera& camera,
61 const vtkm::rendering::WorldAnnotator& worldAnnotator,
62 vtkm::rendering::Canvas& canvas)
63 {
64
65 canvas.AddColorBar(Position, this->ColorTable, Horizontal);
66
67 this->Axis.SetColor(canvas.GetForegroundColor());
68 this->Axis.SetLineWidth(1);
69
70 if (Horizontal)
71 {
72 this->Axis.SetScreenPosition(Position.X.Min, Position.Y.Min, Position.X.Max, Position.Y.Min);
73 this->Axis.SetLabelAlignment(TextAnnotation::HCenter, TextAnnotation::Top);
74 this->Axis.SetMajorTickSize(0, .02, 1.0);
75 }
76 else
77 {
78 this->Axis.SetScreenPosition(Position.X.Min, Position.Y.Min, Position.X.Min, Position.Y.Max);
79 this->Axis.SetLabelAlignment(TextAnnotation::Right, TextAnnotation::VCenter);
80 this->Axis.SetMajorTickSize(.02, 0.0, 1.0);
81 }
82
83 this->Axis.SetMinorTickSize(0, 0, 0); // no minor ticks
84 this->Axis.Render(camera, worldAnnotator, canvas);
85
86 if (FieldName != "")
87 {
88 vtkm::Vec2f_32 labelPos;
89 if (Horizontal)
90 {
91 labelPos[0] = vtkm::Float32(Position.X.Min);
92 labelPos[1] = vtkm::Float32(Position.Y.Max);
93 }
94 else
95 {
96 labelPos[0] = vtkm::Float32(Position.X.Min - 0.07);
97 labelPos[1] = vtkm::Float32(Position.Y.Max + 0.03);
98 }
99
100 vtkm::rendering::TextAnnotationScreen var(FieldName,
101 canvas.GetForegroundColor(),
102 .045f, // font scale
103 labelPos,
104 0.f); // rotation
105
106 var.Render(camera, worldAnnotator, canvas);
107 }
108 }
109 }
110 } // namespace vtkm::rendering
111