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
22namespace
23{
24 void renderText(const LabelText& labelText);
25 void renderLabel(const Label& label);
26 void glDrawString(const SbVec2f& screenPosition, const LabelText& labelText);
28 const LabelText& labelText);
29 void fillScreenBoundingBox(Label& label);
30
31 void setupRenderState();
32 void resetRenderState();
33
36
37 GLfloat getScreenTextWidth(const LabelText& labelText);
38 GLfloat getScreenTextAscender(const LabelText& labelText);
39
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 {
51
55
57 }
58
59 void resetRenderState()
60 {
62
63 glFlush();
64
66
70 }
71
73 {
78
80
83
84 ml::OpenGL::disableAllClipPlanes();
85
88 GLint viewport[4];
90
91 glOrtho(0, viewport[2], 0, viewport[3], 0.f, 1.f);
92
94
97 }
98
100 {
101 glPopAttrib();
102
104 glPopMatrix();
106 glPopMatrix();
107 }
108
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;
130
131 if (isValidPosition) {
136 const auto screenZ = currentRasterPosition[2];
137
139
140 glTranslatef(0, 0, -screenZ);
141
142 const auto textWidth = getScreenTextWidth(labelText);
143 const auto textAscender = getScreenTextAscender(labelText);
144
146
148
149 glDrawString(screenPosition, labelText);
150
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
168 {
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
180 const auto screenZ = currentRasterPosition[2];
181
183
184 glTranslatef(0, 0, -screenZ);
185
186 glDrawString(label.screenBoundingBox[0], labelText);
187
189 }
190 }
191
192 void fillScreenBoundingBox(Label& label)
193 {
195
196 const auto labelText = label.labelText;
197
198 glRasterPos3f(labelText.textPosition[0], labelText.textPosition[1], labelText.textPosition[2]);
199
200 GLboolean isValidPosition;
202
203 label.isOnScreen = isValidPosition;
204
205 if (isValidPosition) {
210 const auto screenZ = currentRasterPosition[2];
211
213
214 glTranslatef(0, 0, -screenZ);
215
216 const auto textWidth = getScreenTextWidth(labelText);
217 const auto textAscender = getScreenTextAscender(labelText);
218
220
221 label.screenBoundingBox[0].setValue(screenX, screenY);
223
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
249 {
252
255
256 GLint viewport[4];
258
261 &screenX, &screenY, &screenZ);
262
263 return SbVec3f(static_cast<float>(screenX), static_cast<float>(screenY), static_cast<float>(screenZ));
264 }
265} // namespace
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
unsigned __int64 abs(unsigned __int64 i)
SbVec2f textShiftFactorScreen
std::string text
SbColor textShadowColor
SbVec3f tickDirection
void initializeScreenBoundingBox()
LabelText labelText
SbColor tickColor
SbVec3f tickPosition
SbVec2f screenBoundingBox[2]