-
Notifications
You must be signed in to change notification settings - Fork 51
/
Utils.cc
226 lines (197 loc) · 7.56 KB
/
Utils.cc
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright (C) 2019 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifdef __linux__
#include <X11/Xlib.h>
#include <X11/Xresource.h>
#endif
#include "ignition/rendering/Utils.hh"
namespace ignition
{
namespace rendering
{
inline namespace IGNITION_RENDERING_VERSION_NAMESPACE {
//
/////////////////////////////////////////////////
float screenScalingFactor()
{
// todo(anyone) set device pixel ratio for high dpi displays on Windows
float ratio = 1.0;
#ifdef __linux__
auto closeDisplay = [](Display * display)
{
if (display)
XCloseDisplay(display);
};
auto display =
std::unique_ptr<Display, decltype(closeDisplay)>(
XOpenDisplay(nullptr), closeDisplay);
char *resourceString = XResourceManagerString(display.get());
if (resourceString)
{
char *type = nullptr;
float dpiDesktop = 0.0;
// Need to initialize the DB before calling Xrm* functions
XrmInitialize();
XrmValue value;
XrmDatabase db = XrmGetStringDatabase(resourceString);
// Debug:
// printf("Entire DB:\n%s\n", resourceString);
if (XrmGetResource(db, "Xft.dpi", "String", &type, &value) == True)
{
if (value.addr)
dpiDesktop = atof(value.addr);
}
// To get the ratio we need the DPI as reported by the Xrmdatabase,
// which takes into account desktop scaling, and the DPI computed by the
// actual display resolution.
//
// dpiRes = N pixels / (M millimeters / (25.4 millimeters / 1 inch))
// = N pixels / (M inch / 25.4)
// = (N * 25.4 pixels) / M inch
//
// We can use either the width or height in the following line. The zero
// values in DisplayHeight and DisplayHeightMM is the screen number. A
// value of zero uses the default screen.
float yDpiRes = (DisplayHeight(display.get(), 0) * 25.4) /
DisplayHeightMM(display.get(), 0);
if (!math::equal(dpiDesktop, 0.0f) && !math::equal(yDpiRes, 0.0f))
ratio = dpiDesktop / yDpiRes;
// Debug:
// printf("DPI Desktop: %f, DPI XY: [%f, %f], Ratio XY: [%f, %f]\n",
// dpiDesktop, xDpiRes, yDpiRes, xRatio, yRatio);
}
#endif
return ratio;
}
/////////////////////////////////////////////////
void screenScalingFactor(float &_xScale, float &_yScale)
{
// todo(anyone) set device pixel ratio for high dpi displays on Windows
#ifdef __linux__
auto closeDisplay = [](Display * display)
{
if (display)
XCloseDisplay(display);
};
auto display =
std::unique_ptr<Display, decltype(closeDisplay)>(
XOpenDisplay(nullptr), closeDisplay);
char *resourceString = XResourceManagerString(display.get());
if (resourceString)
{
char *type = nullptr;
float dpiDesktop = 0.0;
// Need to initialize the DB before calling Xrm* functions
XrmInitialize();
XrmValue value;
XrmDatabase db = XrmGetStringDatabase(resourceString);
// Debug:
// printf("Entire DB:\n%s\n", resourceString);
if (XrmGetResource(db, "Xft.dpi", "String", &type, &value) == True)
{
if (value.addr)
dpiDesktop = atof(value.addr);
}
// To get the ratio we need the DPI as reported by the Xrmdatabase,
// which takes into account desktop scaling, and the DPI computed by the
// actual display resolution.
//
// dpiRes = N pixels / (M millimeters / (25.4 millimeters / 1 inch))
// = N pixels / (M inch / 25.4)
// = (N * 25.4 pixels) / M inch
//
// We can use either the width or height in the following line. The zero
// values in DisplayHeight and DisplayHeightMM is the screen number. A
// value of zero uses the default screen.
float xDpiRes = (DisplayWidth(display.get(), 0) * 25.4) /
DisplayWidthMM(display.get(), 0);
float yDpiRes = (DisplayHeight(display.get(), 0) * 25.4) /
DisplayHeightMM(display.get(), 0);
if (!math::equal(dpiDesktop, 0.0f) && !math::equal(yDpiRes, 0.0f))
_yScale = yDpiRes / dpiDesktop;
if (_yScale < 1) _yScale = 1;
if (!math::equal(dpiDesktop, 0.0f) && !math::equal(xDpiRes, 0.0f))
_xScale = xDpiRes / dpiDesktop;
if (_xScale < 1) _xScale = 1;
// Debug:
// printf("DPI Desktop: %f, DPI XY: [%f, %f], Scale XY: [%f, %f]\n",
// dpiDesktop, xDpiRes, yDpiRes, _xScale, _yScale);
}
#endif
}
/////////////////////////////////////////////////
ignition::math::AxisAlignedBox transformAxisAlignedBox(
const ignition::math::AxisAlignedBox &_bbox,
const ignition::math::Pose3d &_pose)
{
auto center = _bbox.Center();
// Get the 8 corners of the bounding box.
std::vector<ignition::math::Vector3d> vertices;
vertices.push_back(center + ignition::math::Vector3d(-_bbox.XLength()/2.0,
_bbox.YLength()/2.0,
_bbox.ZLength()/2.0));
vertices.push_back(center + ignition::math::Vector3d(_bbox.XLength()/2.0,
_bbox.YLength()/2.0,
_bbox.ZLength()/2.0));
vertices.push_back(center + ignition::math::Vector3d(-_bbox.XLength()/2.0,
-_bbox.YLength()/2.0,
_bbox.ZLength()/2.0));
vertices.push_back(center + ignition::math::Vector3d(_bbox.XLength()/2.0,
-_bbox.YLength()/2.0,
_bbox.ZLength()/2.0));
vertices.push_back(center + ignition::math::Vector3d(-_bbox.XLength()/2.0,
_bbox.YLength()/2.0,
-_bbox.ZLength()/2.0));
vertices.push_back(center + ignition::math::Vector3d(_bbox.XLength()/2.0,
_bbox.YLength()/2.0,
-_bbox.ZLength()/2.0));
vertices.push_back(center + ignition::math::Vector3d(-_bbox.XLength()/2.0,
-_bbox.YLength()/2.0,
-_bbox.ZLength()/2.0));
vertices.push_back(center + ignition::math::Vector3d(_bbox.XLength()/2.0,
-_bbox.YLength()/2.0,
-_bbox.ZLength()/2.0));
// Transform corners.
for (unsigned int i = 0; i < vertices.size(); ++i)
{
auto &v = vertices[i];
v = _pose.Rot() * v + _pose.Pos();
}
ignition::math::Vector3d min = vertices[0];
ignition::math::Vector3d max = vertices[0];
// find min / max of vertices
for (unsigned int i = 1; i < vertices.size(); ++i)
{
auto &v = vertices[i];
if (min.X() > v.X())
min.X() = v.X();
if (max.X() < v.X())
max.X() = v.X();
if (min.Y() > v.Y())
min.Y() = v.Y();
if (max.Y() < v.Y())
max.Y() = v.Y();
if (min.Z() > v.Z())
min.Z() = v.Z();
if (max.Z() < v.Z())
max.Z() = v.Z();
}
return ignition::math::AxisAlignedBox(min, max);
}
}
}
}