VOXL OpenVINS Server 1.0
Visual Inertial Odometry Server for VOXL Platform
Loading...
Searching...
No Matches
VoxlCommon.h
Go to the documentation of this file.
1/**
2 * @file VoxlCommon.h
3 * @brief Common definitions and utilities for the VOXL OpenVINS server
4 * @author Zauberflote
5 * @date 2025
6 * @version 1.0
7 *
8 * This header file contains common definitions, data structures, and utility functions
9 * used throughout the VOXL OpenVINS server. It provides the foundation for camera
10 * management, IMU handling, and general system utilities.
11 *
12 * The file includes:
13 * - Camera mode enumerations and conversion functions
14 * - Image data structures for tracking
15 * - Camera information structures
16 * - Utility functions for timing and memory management
17 * - Mathematical helper functions
18 */
19
20#ifndef VOXL_COMMON_H
21#define VOXL_COMMON_H
22#pragma once
23
24// Standard includes
25#include <stdint.h>
26#include <vector>
27#include <opencv2/opencv.hpp>
28#include <functional>
29#include <Eigen/Core>
30#include <Eigen/Dense>
31#include <modal_pipe.h>
32#include <unistd.h>
33#include <time.h>
34#include <math.h>
35#include <string.h>
36#include <iomanip>
37#include <iostream>
38#include <fstream>
39
40// ============================================================================
41// CONSTANTS AND MACROS
42// ============================================================================
43
44/**
45 * @brief Arbitrary string buffer size for names and identifiers
46 *
47 * Used for camera names, tracking names, and other string identifiers
48 * throughout the system.
49 */
50#define CM_CHAR_BUF_SIZE 64
51
52/**
53 * @brief Convert degrees to radians
54 *
55 * Macro for converting angle measurements from degrees to radians.
56 */
57#ifndef DEG_TO_RAD
58#define DEG_TO_RAD (M_PI / 180.0)
59#endif
60
61/**
62 * @brief Convert radians to degrees
63 *
64 * Macro for converting angle measurements from radians to degrees.
65 */
66#ifndef RAD_TO_DEG
67#define RAD_TO_DEG (180.0 / M_PI)
68#endif
69
70/**
71 * @brief Maximum size for image data buffer
72 *
73 * Calculated for 1280x800 resolution with 3 channels and 2 bytes per pixel
74 */
75constexpr size_t MAX_IMAGE_SIZE = 1280 * 800 * 3 * 2;
76
77// ============================================================================
78// ENUMERATIONS
79// ============================================================================
80
81/**
82 * @enum camera_mode
83 * @brief Camera operation modes for the VIO system
84 *
85 * This enumeration defines the different camera modes supported by the system.
86 * The mode determines how multiple cameras are used in the visual-inertial
87 * odometry pipeline.
88 */
89typedef enum camera_mode
90{
91 UNKNOWN = -1, ///< Unknown or invalid camera mode
92 MONO = 0, ///< Single camera mode
93 STEREO = 1, ///< Stereo camera mode (both cameras active)
94 STEREO_LEFT_ONLY = 2, ///< Stereo setup with only left camera active
95 STEREO_RIGHT_ONLY = 3 ///< Stereo setup with only right camera active
97
98// ============================================================================
99// UTILITY FUNCTIONS
100// ============================================================================
101
102/**
103 * @brief Convert camera mode enumeration to string representation
104 *
105 * This function converts a camera_mode enumeration value to its corresponding
106 * string representation for logging and debugging purposes.
107 *
108 * @param cm Camera mode enumeration value
109 * @return String representation of the camera mode
110 */
111static std::string camera_mode_as_string(camera_mode cm)
112{
113 if (cm == MONO)
114 {
115 return "MONO";
116 }
117 else if (cm == STEREO)
118 {
119 return "STEREO";
120 }
121 else if (cm == STEREO_LEFT_ONLY)
122 {
123 return "STEREO_LEFT_ONLY";
124 }
125 else if (cm == STEREO_RIGHT_ONLY)
126 {
127 return "STEREO_RIGHT_ONLY";
128 }
129 else
130 return "UNKNOWN";
131}
132
133/**
134 * @brief Convert string representation to camera mode enumeration
135 *
136 * This function converts a string representation of a camera mode to its
137 * corresponding enumeration value. Used for parsing configuration files
138 * and command-line arguments.
139 *
140 * @param str_cm String representation of camera mode
141 * @return Camera mode enumeration value
142 */
143static camera_mode string_camera_mode_to_enum(const char *str_cm)
144{
145 if (!strncmp(str_cm, "MONO", sizeof("MONO")))
146 {
147 return MONO;
148 }
149 if (!strncmp(str_cm, "STEREO", sizeof("STEREO")))
150 {
151 return STEREO;
152 }
153 if (!strncmp(str_cm, "STEREO_LEFT_ONLY", sizeof("STEREO_LEFT_ONLY")))
154 {
155 return STEREO_LEFT_ONLY;
156 }
157 if (!strncmp(str_cm, "STEREO_RIGHT_ONLY", sizeof("STEREO_RIGHT_ONLY")))
158 {
159 return STEREO_RIGHT_ONLY;
160 }
161 else
162 return UNKNOWN;
163}
164
165// ============================================================================
166// DATA STRUCTURES
167// ============================================================================
168
169/**
170 * @struct image_data
171 * @brief Base packet structure for image data fed to trackers
172 *
173 * This structure contains all the necessary information for processing
174 * images in the VIO system, including timestamps, tracker identifiers,
175 * image data, and masking information.
176 *
177 * The structure is used as the primary data container for all image
178 * processing operations in the tracking pipeline.
179 */
180typedef struct image_data
181{
182 int64_t timestamp_ns; ///< Timestamp of image in nanoseconds
183 std::vector<size_t> tracker_ids; ///< Vector of tracker IDs per camera, matching order of images + masks
184 std::vector<cv::Mat> images; ///< Vector of images to track across, in order matching ids vector
185 std::vector<cv::Mat> masks; ///< Vector of masks denoting regions of non-interest, in order matching ids vector
186 ///< Mask regions with val == 255 will be ignored in tracking process
187} image_data;
188
189/**
190 * @struct cam_info
191 * @brief Camera information and calibration data
192 *
193 * This structure contains all the configuration and calibration information
194 * for a single camera in the VIO system, including intrinsic parameters,
195 * extrinsic parameters, and operational settings.
196 */
197typedef struct cam_info
198{
199 char name[128]; ///< Camera name identifier
200 char tracking_name[128]; ///< Name used for tracking operations
201 char preview_name[128]; ///< Name used for preview/display
202 camera_mode mode; ///< Camera operation mode
203 Eigen::Matrix<double, 7, 1> cam_wrt_imu; ///< Camera pose relative to IMU (quaternion + position)
204 Eigen::Matrix<double, 8, 1> cam_calib_intrinsic; ///< Camera intrinsic calibration parameters
205 int width; ///< Image width in pixels
206 int height; ///< Image height in pixels
207 bool is_fisheye; ///< Flag indicating if camera uses fisheye lens
208 bool is_occluded_on_takeoff; ///< Flag indicating if camera is occluded on takeoff
209 size_t cam_id; ///< Unique camera identifier
210} cam_info;
211
212// ============================================================================
213// TIMING AND SYSTEM UTILITIES
214// ============================================================================
215
216/**
217 * @brief Get monotonic time in nanoseconds
218 *
219 * This function provides a high-precision monotonic timestamp for timing
220 * measurements and synchronization. It uses the CLOCK_MONOTONIC clock
221 * which is not affected by system time changes.
222 *
223 * Used across main projects for consistent timing measurements.
224 *
225 * @return Monotonic time in nanoseconds, or -1 on error
226 */
227static int64_t _apps_time_monotonic_ns()
228{
229 struct timespec ts;
230 if (clock_gettime(CLOCK_MONOTONIC, &ts))
231 {
232 fprintf(stderr, "ERROR calling clock_gettime\n");
233 return -1;
234 }
235 return (int64_t)ts.tv_sec * 1000000000 + (int64_t)ts.tv_nsec;
236}
237
238/**
239 * @brief Mathematical sign function
240 *
241 * Returns the sign of a double value: 1.0 for positive, -1.0 for negative,
242 * and 0.0 for zero.
243 *
244 * @param x Input value
245 * @return Sign of the input value
246 */
247static double sign(double x)
248{
249 return x > 0 ? 1.0 : (x < 0 ? -1.0 : 0.0);
250}
251
252// ============================================================================
253// MEMORY MANAGEMENT UTILITIES
254// ============================================================================
255
256/**
257 * @brief Extract memory usage value from /proc/self/status
258 *
259 * This function reads the /proc/self/status file to extract specific
260 * memory usage statistics for the current process.
261 *
262 * @param fieldName Name of the field to extract (e.g., "VmSize:", "VmRSS:")
263 * @return Memory value in bytes, or 0 if not found
264 */
265static long getValueFromStatus(const std::string &fieldName)
266{
267 std::ifstream status("/proc/self/status");
268 std::string line;
269 while (std::getline(status, line))
270 {
271 if (line.find(fieldName) == 0)
272 {
273 long value = 0;
274 char unit[4] = {0}; // Initialize buffer to zeros for safety
275 // Security: Use field width limit to prevent buffer overflow
276 // %3s reads at most 3 chars + null terminator (4 bytes total)
277 sscanf(line.c_str(), "%*s %ld %3s", &value, unit);
278 // Ensure null termination (defensive programming)
279 unit[sizeof(unit) - 1] = '\0';
280 // Convert to bytes based on unit
281 if (strcmp(unit, "kB") == 0)
282 value *= 1024;
283 return value;
284 }
285 }
286 return 0;
287}
288
289/**
290 * @brief Print comprehensive memory usage statistics
291 *
292 * This function displays detailed memory usage information for the current
293 * process, including virtual memory size, resident set size, data segment
294 * size, and stack size. Useful for debugging and performance monitoring.
295 *
296 * @param label Label to identify the memory usage report
297 */
298static void printMemoryUsage(const std::string &label)
299{
300 const double mb = 1024.0 * 1024.0;
301
302 long vmSize = getValueFromStatus("VmSize:");
303 long vmRSS = getValueFromStatus("VmRSS:");
304 long vmData = getValueFromStatus("VmData:");
305 long vmStk = getValueFromStatus("VmStk:");
306
307 std::cout << "\n=== " << label << " ===\n"
308 << std::fixed << std::setprecision(2)
309 << "Virtual Memory Size: " << vmSize / mb << " MB\n"
310 << "Resident Set Size: " << vmRSS / mb << " MB\n"
311 << "Data Segment Size: " << vmData / mb << " MB\n"
312 << "Stack Size: " << vmStk / mb << " MB\n"
313 << std::endl;
314}
315
316#endif // VOXL_COMMON_H
constexpr size_t MAX_IMAGE_SIZE
Maximum size for image data buffer.
Definition VoxlCommon.h:75
camera_mode
Camera operation modes for the VIO system.
Definition VoxlCommon.h:90
@ STEREO_LEFT_ONLY
Stereo setup with only left camera active.
Definition VoxlCommon.h:94
@ MONO
Single camera mode.
Definition VoxlCommon.h:92
@ UNKNOWN
Unknown or invalid camera mode.
Definition VoxlCommon.h:91
@ STEREO_RIGHT_ONLY
Stereo setup with only right camera active.
Definition VoxlCommon.h:95
@ STEREO
Stereo camera mode (both cameras active)
Definition VoxlCommon.h:93
Camera information and calibration data.
Definition VoxlCommon.h:198
char name[128]
Camera name identifier.
Definition VoxlCommon.h:199
bool is_occluded_on_takeoff
Flag indicating if camera is occluded on takeoff.
Definition VoxlCommon.h:208
int width
Image width in pixels.
Definition VoxlCommon.h:205
char tracking_name[128]
Name used for tracking operations.
Definition VoxlCommon.h:200
char preview_name[128]
Name used for preview/display.
Definition VoxlCommon.h:201
int height
Image height in pixels.
Definition VoxlCommon.h:206
camera_mode mode
Camera operation mode.
Definition VoxlCommon.h:202
size_t cam_id
Unique camera identifier.
Definition VoxlCommon.h:209
bool is_fisheye
Flag indicating if camera uses fisheye lens.
Definition VoxlCommon.h:207
Eigen::Matrix< double, 8, 1 > cam_calib_intrinsic
Camera intrinsic calibration parameters.
Definition VoxlCommon.h:204
Eigen::Matrix< double, 7, 1 > cam_wrt_imu
Camera pose relative to IMU (quaternion + position)
Definition VoxlCommon.h:203
Base packet structure for image data fed to trackers.
Definition VoxlCommon.h:181
std::vector< size_t > tracker_ids
Vector of tracker IDs per camera, matching order of images + masks.
Definition VoxlCommon.h:183
int64_t timestamp_ns
Timestamp of image in nanoseconds.
Definition VoxlCommon.h:182
std::vector< cv::Mat > masks
Vector of masks denoting regions of non-interest, in order matching ids vector Mask regions with val ...
Definition VoxlCommon.h:185
std::vector< cv::Mat > images
Vector of images to track across, in order matching ids vector.
Definition VoxlCommon.h:184