MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
SoCoordinateSystemUtils.h
Go to the documentation of this file.
1
/*************************************************************************************
2
**
3
** Copyright 2021, MeVis Medical Solutions AG
4
**
5
** The user may use this file in accordance with the license agreement provided with
6
** the Software or, alternatively, in accordance with the terms contained in a
7
** written agreement between the user and MeVis Medical Solutions AG.
8
**
9
** For further information use the contact form at https://www.mevislab.de/contact
10
**
11
**************************************************************************************/
12
13
#pragma once
14
15
#include <
mlOpenGL.h
>
16
#include <Inventor/So.h>
17
#include <
mlGL2DFont.h
>
18
#include <
mlGLHiDPI.h
>
19
20
#include <
SoCoordinateSystem.h
>
21
22
namespace
23
{
24
void
renderText
(
const
LabelText
& labelText);
25
void
renderLabel
(
const
Label
& label);
26
void
glDrawString
(
const
SbVec2f
&
screenPosition
,
const
LabelText
& labelText);
27
void
applyLabelOffset
(
GLfloat
&
screenX
,
GLfloat
&
screenY
,
const
GLfloat
&
textWidth
,
const
GLfloat
&
textAscender
,
28
const
LabelText
& labelText);
29
void
fillScreenBoundingBox
(
Label
& label);
30
31
void
setupRenderState
();
32
void
resetRenderState
();
33
34
void
setupRasterPositionStateScreen
();
35
void
resetRasterPositionState
();
36
37
GLfloat
getScreenTextWidth
(
const
LabelText
& labelText);
38
GLfloat
getScreenTextAscender
(
const
LabelText
& labelText);
39
40
SbVec3f
getScreenCoordinate
(
const
SbVec3f
&
worldCoordinate
);
41
SbVec4f
getColorWithAlpha
(
const
SbColor
& color,
float
alpha)
42
{
43
return
SbVec4f
(color[0], color[1], color[2], alpha);
44
}
45
46
void
setupRenderState
()
47
{
48
glPushMatrix
();
49
glPushAttrib
(
GL_ALL_ATTRIB_BITS
);
50
glDisable
(
GL_LIGHTING
);
51
52
glEnable
(
GL_BLEND
);
53
glBlendFunc
(
GL_SRC_ALPHA
,
GL_ONE_MINUS_SRC_ALPHA
);
54
glDepthFunc
(
GL_LEQUAL
);
55
56
glPushAttrib
(
GL_LINE_BIT
);
57
}
58
59
void
resetRenderState
()
60
{
61
glPopAttrib
();
62
63
glFlush
();
64
65
glDisable
(
GL_BLEND
);
66
67
glEnable
(
GL_LIGHTING
);
68
glPopAttrib
();
69
glPopMatrix
();
70
}
71
72
void
setupRasterPositionStateScreen
()
73
{
74
glMatrixMode
(
GL_MODELVIEW
);
75
glPushMatrix
();
76
glLoadIdentity
();
77
glMatrixMode
(
GL_PROJECTION
);
78
79
glPushAttrib
(
GL_ALL_ATTRIB_BITS
);
80
81
glDisable
(
GL_LIGHTING
);
82
glDisable
(
GL_CULL_FACE
);
83
84
ml::OpenGL::disableAllClipPlanes();
85
86
glPushMatrix
();
87
glLoadIdentity
();
88
GLint
viewport
[4];
89
glGetIntegerv
(
GL_VIEWPORT
,
viewport
);
90
91
glOrtho
(0,
viewport
[2], 0,
viewport
[3], 0.f, 1.f);
92
93
glMatrixMode
(
GL_MODELVIEW
);
94
95
glEnable
(
GL_BLEND
);
96
glBlendFunc
(
GL_SRC_ALPHA
,
GL_ONE_MINUS_SRC_ALPHA
);
97
}
98
99
void
resetRasterPositionState
()
100
{
101
glPopAttrib
();
102
103
glMatrixMode
(
GL_PROJECTION
);
104
glPopMatrix
();
105
glMatrixMode
(
GL_MODELVIEW
);
106
glPopMatrix
();
107
}
108
109
void
applyLabelOffset
(
GLfloat
&
screenX
,
GLfloat
&
screenY
,
const
GLfloat
&
textWidth
,
const
GLfloat
&
textAscender
,
110
const
LabelText
& labelText)
111
{
112
screenX
-= (
textWidth
* .5f);
113
screenY
-= (
textAscender
* .5f);
114
115
// only use text height for distance from axis:
116
screenX
+= (
textAscender
* labelText.
textShiftFactorScreen
[0]);
117
screenY
+= (
textAscender
* labelText.
textShiftFactorScreen
[1]);
118
// only left/right-justify if displacement along x-axis is greater than along y-axis
119
if
(
abs
(labelText.
textShiftFactorScreen
[0]) >
abs
(labelText.
textShiftFactorScreen
[1])) {
120
screenX
+= (
textWidth
* .5f *
sign
(labelText.
textShiftFactorScreen
[0]));
121
}
122
}
123
124
void
renderText
(
const
LabelText
& labelText)
125
{
126
glRasterPos3f
(labelText.
textPosition
[0], labelText.
textPosition
[1], labelText.
textPosition
[2]);
127
128
GLboolean
isValidPosition;
129
glGetBooleanv
(
GL_CURRENT_RASTER_POSITION_VALID
, &isValidPosition);
130
131
if
(isValidPosition) {
132
GLfloat
currentRasterPosition
[4];
133
glGetFloatv
(
GL_CURRENT_RASTER_POSITION
,
currentRasterPosition
);
134
auto
screenX
=
currentRasterPosition
[0];
135
auto
screenY
=
currentRasterPosition
[1];
136
const
auto
screenZ
=
currentRasterPosition
[2];
137
138
setupRasterPositionStateScreen
();
139
140
glTranslatef
(0, 0, -
screenZ
);
141
142
const
auto
textWidth
=
getScreenTextWidth
(labelText);
143
const
auto
textAscender
=
getScreenTextAscender
(labelText);
144
145
applyLabelOffset
(
screenX
,
screenY
,
textWidth
,
textAscender
, labelText);
146
147
const
SbVec2f
screenPosition
(
screenX
,
screenY
);
148
149
glDrawString
(
screenPosition
, labelText);
150
151
resetRasterPositionState
();
152
}
153
}
154
155
void
renderLabel
(
const
Label
& label)
156
{
157
auto
offset = .25f;
158
if
(label.
isVisible
) {
159
offset = .5f;
160
}
161
const
auto
tickStart
= label.
tickPosition
+ label.
tickDirection
* offset;
162
const
auto
tickEnd
= label.
tickPosition
- label.
tickDirection
* offset;
163
164
const
auto
tickColor =
getColorWithAlpha
(label.
tickColor
, label.
tickAlpha
);
165
glColor4fv
(tickColor.getValue());
166
167
glBegin
(
GL_LINE_STRIP
);
168
{
169
glVertex3f
(
tickStart
[0],
tickStart
[1],
tickStart
[2]);
170
glVertex3f
(
tickEnd
[0],
tickEnd
[1],
tickEnd
[2]);
171
}
172
glEnd
();
173
174
if
(label.
isVisible
) {
175
const
auto
labelText = label.
labelText
;
176
glRasterPos3f
(labelText.
textPosition
[0], labelText.
textPosition
[1], labelText.
textPosition
[2]);
177
178
GLfloat
currentRasterPosition
[4];
179
glGetFloatv
(
GL_CURRENT_RASTER_POSITION
,
currentRasterPosition
);
180
const
auto
screenZ
=
currentRasterPosition
[2];
181
182
setupRasterPositionStateScreen
();
183
184
glTranslatef
(0, 0, -
screenZ
);
185
186
glDrawString
(label.
screenBoundingBox
[0], labelText);
187
188
resetRasterPositionState
();
189
}
190
}
191
192
void
fillScreenBoundingBox
(
Label
& label)
193
{
194
label.
initializeScreenBoundingBox
();
195
196
const
auto
labelText = label.
labelText
;
197
198
glRasterPos3f
(labelText.
textPosition
[0], labelText.
textPosition
[1], labelText.
textPosition
[2]);
199
200
GLboolean
isValidPosition;
201
glGetBooleanv
(
GL_CURRENT_RASTER_POSITION_VALID
, &isValidPosition);
202
203
label.
isOnScreen
= isValidPosition;
204
205
if
(isValidPosition) {
206
GLfloat
currentRasterPosition
[4];
207
glGetFloatv
(
GL_CURRENT_RASTER_POSITION
,
currentRasterPosition
);
208
auto
screenX
=
currentRasterPosition
[0];
209
auto
screenY
=
currentRasterPosition
[1];
210
const
auto
screenZ
=
currentRasterPosition
[2];
211
212
setupRasterPositionStateScreen
();
213
214
glTranslatef
(0, 0, -
screenZ
);
215
216
const
auto
textWidth
=
getScreenTextWidth
(labelText);
217
const
auto
textAscender
=
getScreenTextAscender
(labelText);
218
219
applyLabelOffset
(
screenX
,
screenY
,
textWidth
,
textAscender
, labelText);
220
221
label.
screenBoundingBox
[0].setValue(
screenX
,
screenY
);
222
label.
screenBoundingBox
[1].setValue(
screenX
+
textWidth
,
screenY
+
textAscender
);
223
224
resetRasterPositionState
();
225
}
226
}
227
228
void
glDrawString
(
const
SbVec2f
&
screenPosition
,
const
LabelText
& labelText)
229
{
230
const
auto
font
= ml::GL2DFont::getDefaultSystemFont();
231
const
auto
textColor =
getColorWithAlpha
(labelText.
textColor
, labelText.
textAlpha
);
232
font
->drawString(
screenPosition
[0],
screenPosition
[1], labelText.
fontSize
, textColor.getValue(),
233
labelText.
text
.c_str(), 0,
false
, labelText.
useTextShadow
, labelText.
textShadowColor
.getValue());
234
}
235
236
GLfloat
getScreenTextWidth
(
const
LabelText
& labelText)
237
{
238
const
auto
font
= ml::GL2DFont::getDefaultSystemFont();
239
return
static_cast<
GLfloat
>
(
font
->getStringWidth(labelText.
fontSize
, labelText.
text
.c_str()));
240
}
241
242
GLfloat
getScreenTextAscender
(
const
LabelText
& labelText)
243
{
244
const
auto
font
= ml::GL2DFont::getDefaultSystemFont();
245
return
static_cast<
GLfloat
>
(
font
->getFontAscender(labelText.
fontSize
));
246
}
247
248
SbVec3f
getScreenCoordinate
(
const
SbVec3f
&
worldCoordinate
)
249
{
250
GLdouble
modelViewMatrix
[16];
251
glGetDoublev
(
GL_MODELVIEW_MATRIX
,
modelViewMatrix
);
252
253
GLdouble
projectionMatrix
[16];
254
glGetDoublev
(
GL_PROJECTION_MATRIX
,
projectionMatrix
);
255
256
GLint
viewport
[4];
257
glGetIntegerv
(
GL_VIEWPORT
,
viewport
);
258
259
GLdouble
screenX
,
screenY
,
screenZ
;
260
gluProject
(
worldCoordinate
[0],
worldCoordinate
[1],
worldCoordinate
[2],
modelViewMatrix
,
projectionMatrix
,
viewport
,
261
&
screenX
, &
screenY
, &
screenZ
);
262
263
return
SbVec3f
(
static_cast<
float
>
(
screenX
),
static_cast<
float
>
(
screenY
),
static_cast<
float
>
(
screenZ
));
264
}
265
}
// namespace
SoCoordinateSystem.h
mlGL2DFont.h
mlGLHiDPI.h
mlOpenGL.h
mlrange_cast
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
Definition
mlRangeCasts.h:305
abs
unsigned __int64 abs(unsigned __int64 i)
Definition
mlUtilsSystem.h:154
LabelText
Definition
SoCoordinateSystem.h:95
LabelText::textShiftFactorScreen
SbVec2f textShiftFactorScreen
Definition
SoCoordinateSystem.h:108
LabelText::text
std::string text
Definition
SoCoordinateSystem.h:105
LabelText::useTextShadow
bool useTextShadow
Definition
SoCoordinateSystem.h:115
LabelText::textPosition
SbVec3f textPosition
Definition
SoCoordinateSystem.h:107
LabelText::textShadowColor
SbColor textShadowColor
Definition
SoCoordinateSystem.h:116
LabelText::textAlpha
float textAlpha
Definition
SoCoordinateSystem.h:113
LabelText::fontSize
float fontSize
Definition
SoCoordinateSystem.h:110
LabelText::textColor
SbColor textColor
Definition
SoCoordinateSystem.h:112
Label
Definition
SoCoordinateSystem.h:120
Label::tickAlpha
float tickAlpha
Definition
SoCoordinateSystem.h:161
Label::isOnScreen
bool isOnScreen
Definition
SoCoordinateSystem.h:165
Label::tickDirection
SbVec3f tickDirection
Definition
SoCoordinateSystem.h:159
Label::initializeScreenBoundingBox
void initializeScreenBoundingBox()
Definition
SoCoordinateSystem.h:126
Label::labelText
LabelText labelText
Definition
SoCoordinateSystem.h:167
Label::tickColor
SbColor tickColor
Definition
SoCoordinateSystem.h:160
Label::isVisible
bool isVisible
Definition
SoCoordinateSystem.h:164
Label::tickPosition
SbVec3f tickPosition
Definition
SoCoordinateSystem.h:158
Label::screenBoundingBox
SbVec2f screenBoundingBox[2]
Definition
SoCoordinateSystem.h:163
MeVisLab
Standard
Sources
Inventor
SoUtils
SoCoordinateSystemUtils.h
Generated by
1.10.0