Mech-Eye API 2.5.2
API reference documentation for Mech-Eye Industrial 3D Camera
Loading...
Searching...
No Matches
api_util.h
1/*******************************************************************************
2 *BSD 3-Clause License
3 *
4 *Copyright (c) 2016-2025, Mech-Mind Robotics Technologies Co., Ltd.
5 *All rights reserved.
6 *
7 *Redistribution and use in source and binary forms, with or without
8 *modification, are permitted provided that the following conditions are met:
9 *
10 *1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 *2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 *3. Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 *THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 *AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 *IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 *DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 *DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 *SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 *CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 *OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 *OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************************************************************/
32
33#pragma once
34#include <iostream>
35#include <iomanip>
36#include <set>
37#include <utility>
38#include <regex>
39#ifdef WIN32
40#include <windows.h>
41#endif
42#include "CameraProperties.h"
43#include "ErrorStatus.h"
44#include "CommonTypes.h"
45#include "Camera.h"
46
47inline void printCameraInfo(const mmind::eye::CameraInfo& cameraInfo)
48{
49 std::cout << "............................." << std::endl;
50 std::cout << "Model: " << cameraInfo.model << std::endl;
51#ifdef WIN32
52 const auto consoleCP = GetConsoleOutputCP();
53 SetConsoleOutputCP(CP_UTF8);
54#endif
55 std::cout << "Device name: " << cameraInfo.deviceName << std::endl;
56#ifdef WIN32
57 SetConsoleOutputCP(consoleCP);
58#endif
59 std::cout << "Serial number: " << cameraInfo.serialNumber << std::endl;
60 std::cout << "IP address: " << cameraInfo.ipAddress << std::endl;
61 std::cout << "Subnet mask: " << cameraInfo.subnetMask << std::endl;
62 std::cout << "IP address assignment method: "
63 << mmind::eye::ipAssignmentMethodToString(cameraInfo.ipAssignmentMethod) << std::endl;
64 std::cout << "Hardware version: "
65 << "V" << cameraInfo.hardwareVersion.toString() << std::endl;
66 std::cout << "Firmware version: "
67 << "V" << cameraInfo.firmwareVersion.toString() << std::endl;
68 std::cout << "Support status: "
69 << (cameraInfo.supported ? "Supported" : "Unsupported") << std::endl;
70 if (!cameraInfo.lastSupportedVersion.isEmpty()) {
71 std::cout << "Last supported version: "
72 << "V" << cameraInfo.lastSupportedVersion.toString() << std::endl;
73 }
74 std::cout << "............................." << std::endl;
75 std::cout << std::endl;
76}
77
78inline void printCameraStatus(const mmind::eye::CameraStatus& cameraStatus)
79{
80 std::cout << ".....Camera temperatures....." << std::endl;
81 std::cout << std::fixed << std::setprecision(1);
82 std::cout << "CPU: " << cameraStatus.temperature.cpuTemperature << "°C" << std::endl;
83 std::cout << "Projector: " << cameraStatus.temperature.projectorTemperature << "°C"
84 << std::endl;
85 std::cout << "............................" << std::endl;
86 std::cout << std::defaultfloat << std::setprecision(6);
87 std::cout << std::endl;
88}
89
90inline void printCameraResolutions(const mmind::eye::CameraResolutions& cameraResolutions)
91
92{
93 std::cout << ".....Image resolutions....." << std::endl;
94 std::cout << "2D image (texture): " << cameraResolutions.texture.width << " (width) × "
95 << cameraResolutions.texture.height << " (height)" << std::endl;
96 std::cout << "Depth map: " << cameraResolutions.depth.width << " (width) × "
97 << cameraResolutions.depth.height << " (height)" << std::endl;
98}
99
100inline void printCameraMatrix(const std::string& title,
101 const mmind::eye::CameraMatrix& cameraMatrix)
102{
103 std::cout << title << ": " << std::endl
104 << " [" << cameraMatrix.fx << ", " << 0 << ", " << cameraMatrix.cx << "]"
105
106 << std::endl
107 << " [" << 0 << ", " << cameraMatrix.fy << ", " << cameraMatrix.cy << "]"
108
109 << std::endl
110 << " [" << 0 << ", " << 0 << ", " << 1 << "]" << std::endl;
111 std::cout << std::endl;
112}
113
114inline void printCameraDistCoeffs(const std::string& title,
115 const mmind::eye::CameraDistortion& distCoeffs)
116{
117 std::cout << title << ": " << std::endl
118 << " k1: " << distCoeffs.k1 << ", k2: " << distCoeffs.k2
119 << ", p1: " << distCoeffs.p1 << ", p2: " << distCoeffs.p2 << ", k3: " << distCoeffs.k3
120 << std::endl;
121 std::cout << std::endl;
122}
123
124inline void printTransform(const std::string& title, const mmind::eye::Transformation& transform)
125{
126 std::cout << "Rotation: " << title << ": " << std::endl;
127 for (int i = 0; i < 3; i++) {
128 std::cout << " [";
129 for (int j = 0; j < 3; j++) {
130 std::cout << transform.rotation[i][j];
131 if (j != 2)
132 std::cout << ", ";
133 }
134 std::cout << "]" << std::endl;
135 }
136 std::cout << std::endl;
137 std::cout << "Translation " << title << ": " << std::endl;
138 std::cout << " X: " << transform.translation[0] << "mm, Y: " << transform.translation[1]
139 << "mm, Z: " << transform.translation[2] << "mm" << std::endl;
140 std::cout << std::endl;
141}
142
143inline void printCameraIntrinsics(const mmind::eye::CameraIntrinsics& intrinsics)
144{
145 printCameraMatrix("Texture 2D camera matrix", intrinsics.texture.cameraMatrix);
146 printCameraDistCoeffs("Texture 2D camera distortion coefficients",
147 intrinsics.texture.cameraDistortion);
148
149 printCameraMatrix("Depth 2D camera matrix", intrinsics.depth.cameraMatrix);
150 printCameraDistCoeffs("Depth 2D camera distortion coefficients",
151 intrinsics.depth.cameraDistortion);
152
153 printTransform("Transformation from depth 2D camera to texture 2D camera",
154 intrinsics.depthToTexture);
155}
156
157inline bool findAndConnect(mmind::eye::Camera& device)
158{
159 std::cout << "Looking for available cameras..." << std::endl;
160 std::vector<mmind::eye::CameraInfo> deviceInfoList = mmind::eye::Camera::discoverCameras();
161
162 if (deviceInfoList.empty()) {
163 std::cout << "No cameras are available." << std::endl;
164 return false;
165 }
166
167 for (size_t i = 0; i < deviceInfoList.size(); i++) {
168 std::cout << "Mech-Eye device index: " << i << std::endl;
169 printCameraInfo(deviceInfoList[i]);
170 }
171
172 std::cout << "Enter the index of the device to which you want to connect: ";
173 unsigned inputIndex = 0;
174
175 while (true) {
176 std::string str;
177 std::cin >> str;
178 if (std::regex_match(str.begin(), str.end(), std::regex{"[0-9]+"}) &&
179 atoi(str.c_str()) < static_cast<int>(deviceInfoList.size())) {
180 inputIndex = atoi(str.c_str());
181 break;
182 }
183 std::cout << "The entered index is invalid. Please enter the device index again: ";
184 }
185
187 status = device.connect(deviceInfoList[inputIndex]);
188
189 if (!status.isOK()) {
190 showError(status);
191 return false;
192 }
193
194 std::cout << "Successfully connected to the camera." << std::endl;
195 return true;
196}
197
198inline std::vector<mmind::eye::Camera> findAndConnectMultiCamera()
199{
200 std::cout << "Looking for available cameras..." << std::endl;
201 std::vector<mmind::eye::CameraInfo> cameraInfoList = mmind::eye::Camera::discoverCameras();
202
203 if (cameraInfoList.empty()) {
204 std::cout << "No cameras are available." << std::endl;
205 return {};
206 }
207
208 for (size_t i = 0; i < cameraInfoList.size(); i++) {
209 std::cout << "Mech-Eye device index: " << i << std::endl;
210 printCameraInfo(cameraInfoList[i]);
211 }
212
213 std::string str;
214 std::set<unsigned> indices;
215
216 while (true) {
217 std::cout << "Enter the indices of the devices to which you want to connect: " << std::endl;
218 std::cout << "Enter the character \"c\" at the end of all the indices" << std::endl;
219
220 std::cin >> str;
221 if (str == "c")
222 break;
223 if (std::regex_match(str.begin(), str.end(), std::regex{"[0-9]+"}) &&
224 atoi(str.c_str()) < static_cast<int>(cameraInfoList.size()))
225 indices.insert(atoi(str.c_str()));
226 else
227 std::cout << "The entered indices are invalid. Please enter the device indices again: ";
228 }
229
230 std::vector<mmind::eye::Camera> cameraList{};
231
232 for (const auto index : indices) {
233 mmind::eye::Camera camera;
234 auto status = camera.connect(cameraInfoList[index]);
235 if (status.isOK())
236 cameraList.push_back(camera);
237 else
238 showError(status);
239 }
240
241 return cameraList;
242}
243
244inline bool confirmCapture3D()
245{
246 std::cout
247 << "Do you want the camera to capture 3D data? Enter \"y\" to confirm or \"n\" to cancel: "
248 << std::endl;
249 while (true) {
250 std::string confirmStr;
251 std::cin >> confirmStr;
252 if (confirmStr == "y") {
253 return true;
254 } else if (confirmStr == "n") {
255 std::cout << "The capture command was canceled." << std::endl;
256 return false;
257 } else {
258 std::cout << "The entered character was invalid. Please enter \"y\" to confirm or "
259 "\"n\" to cancel:"
260 << std::endl;
261 }
262 }
263}
Operates the camera. Use Camera::connect to connect an available camera, and then call the correspond...
Definition Camera.h:55
static std::vector< CameraInfo > discoverCameras(unsigned int timeoutMs=5000)
Discovers all available cameras and returns the list of information of all available cameras....
ErrorStatus connect(const CameraInfo &info, unsigned int timeoutMs=5000)
Connects to a camera using CameraInfo.
std::string toString() const
Converts a Version object to a string.
Definition Version.h:71
bool isEmpty() const
Checks if a Version object is empty.
Definition Version.h:95
Describes the distortion parameters.
double k1
Radial distortion coefficients.
double k3
Radial distortion coefficients.
double p2
Tangential distortion coefficients.
double k2
Radial distortion coefficients.
double p1
Tangential distortion coefficients.
Defines the camera information.
IpAssignmentMethod ipAssignmentMethod
The IP address assignment method of the device.
std::string serialNumber
The serial number of the device.
Version hardwareVersion
The version of the hardware (pre-determined in the factory).
bool supported
The support status of the device.
std::string deviceName
The device name (UTF-8 encoded).
std::string model
The device model, such as Mech-Eye NANO.
Version firmwareVersion
The version of the firmware (upgradable).
std::string subnetMask
The subnet mask of the device.
Version lastSupportedVersion
The last supported version of the device.
std::string ipAddress
The IP address of the device.
Defines the 3D camera intrinsic parameters, including the intrinsic parameters of the texture 2D came...
Intrinsics2DCamera depth
The intrinsic parameters of the depth 2D camera(s) for capturing the depth map.
Describes the camera intrinsic parameter matrix.
double cx
Principal point.
double fy
Focal lengths.
double cy
Principal point.
double fx
Focal lengths.
Defines the camera image resolutions, including the resolutions of the 2D image (texture) and depth m...
Describes the camera's statuses.
float projectorTemperature
The temperature (in °C) of the camera projector.
float cpuTemperature
The temperature (in °C) of the camera CPU.
Describes the types of errors.
Definition ErrorStatus.h:12
bool isOK() const
Returns true if the operation succeeded.
Definition ErrorStatus.h:85
Defines the rigid body transformations, including rotation matrix and translation vector.
double rotation[3][3]
3*3 rotation matrix.
double translation[3]
3*1 translation vector in [x(mm), y(mm), z(mm)].