/
MeasurementTool.cpp
189 lines (151 loc) · 4.15 KB
/
MeasurementTool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "MeasurementTool.h"
#include "i18n.h"
#include "iclipper.h"
#include "icolourscheme.h"
#include "math/Matrix4.h"
#include "XYMouseToolEvent.h"
#include "selectionlib.h"
#include "string/convert.h"
#include <fmt/format.h>
namespace ui
{
MeasurementTool::MeasurementTool() :
_points(GL_POINTS),
_lines(GL_LINE_STRIP)
{}
const std::string& MeasurementTool::getName()
{
static std::string name("MeasurementTool");
return name;
}
const std::string& MeasurementTool::getDisplayName()
{
static std::string displayName(_("Measure"));
return displayName;
}
MouseTool::Result MeasurementTool::onMouseDown(Event& ev)
{
try
{
if (GlobalClipper().clipMode())
{
return Result::Ignored; // no measurement in clip mode
}
// We only operate on XY view events, so attempt to cast
XYMouseToolEvent& xyEvent = dynamic_cast<XYMouseToolEvent&>(ev);
Vector3 clickPos = xyEvent.getWorldPos();
xyEvent.getView().snapToGrid(clickPos);
assert(_lines.size() == _points.size());
VertexCb clickVertex(clickPos, Colour4b());
if (_points.empty())
{
// If we just started, allocate two points
_points.push_back(clickVertex);
_points.push_back(clickVertex);
// Copy the vertices to the lines vector
_lines.push_back(clickVertex);
_lines.push_back(clickVertex);
}
else
{
// Store the click position and add a new vertex
_points[_points.size() - 1].vertex = clickVertex.vertex;
_lines[_lines.size() - 1].vertex = clickVertex.vertex;
// Add one additional point to the chain
_points.push_back(clickVertex);
_lines.push_back(clickVertex);
}
return Result::Activated;
}
catch (std::bad_cast&)
{
}
return Result::Ignored; // not handled
}
MouseTool::Result MeasurementTool::onMouseMove(Event& ev)
{
try
{
// We only operate on XY view events, so attempt to cast
XYMouseToolEvent& xyEvent = dynamic_cast<XYMouseToolEvent&>(ev);
Vector3 endPos = xyEvent.getWorldPos();
xyEvent.getView().snapToGrid(endPos);
assert(!_points.empty());
_points[_points.size() - 1].vertex = endPos;
_points.setColour(Colour4b(0, 0, 0, 0));
_lines[_lines.size() -1] = _points[_points.size() - 1];
}
catch (std::bad_cast&)
{
return Result::Ignored;
}
return Result::Continued;
}
MouseTool::Result MeasurementTool::onMouseUp(Event& ev)
{
try
{
// We only operate on XY view events, so attempt to cast
dynamic_cast<XYMouseToolEvent&>(ev).getScale();
// We continue until the user hits ESC
return Result::Continued;
}
catch (std::bad_cast&)
{
return Result::Ignored;
}
}
MeasurementTool::Result MeasurementTool::onCancel(IInteractiveView& view)
{
_points.clear();
_lines.clear();
return Result::Finished;
}
void MeasurementTool::onMouseCaptureLost(IInteractiveView& view)
{
onCancel(view);
}
unsigned int MeasurementTool::getPointerMode()
{
return PointerMode::Normal;
}
unsigned int MeasurementTool::getRefreshMode()
{
return RefreshMode::Force | RefreshMode::ActiveView;
}
void MeasurementTool::ensureShaders(RenderSystem& renderSystem)
{
if (!_wireShader)
{
Vector3 colour = GlobalColourSchemeManager().getColour("drag_selection");
_colour.x() = colour.x();
_colour.y() = colour.y();
_colour.z() = colour.z();
_colour.w() = 1;
_wireShader = renderSystem.capture(ColourShaderType::OrthoviewSolid, _colour);
}
if (!_pointShader)
{
_pointShader = renderSystem.capture(BuiltInShaderType::Point);
}
}
void MeasurementTool::render(RenderSystem& renderSystem, IRenderableCollector& collector, const VolumeTest& volume)
{
ensureShaders(renderSystem);
#if 0
// Render lines
collector.addRenderable(*_wireShader, _lines, Matrix4::getIdentity());
// Render points
collector.addRenderable(*_pointShader, _points, Matrix4::getIdentity());
#endif
// Render distance string
for (std::size_t i = 1; i < _points.size(); ++i)
{
const Vector3& a = _points[i-1].vertex;
const Vector3& b = _points[i].vertex;
glColor4fv(_colour);
glRasterPos3dv((a+b)*0.5);
GlobalOpenGL().drawString(string::to_string((a-b).getLength()));
}
}
} // namespace