Mech-Eye API 2.5.1
API reference documentation for Mech-Eye Industrial 3D Camera
Loading...
Searching...
No Matches
api_util.h
Go to the documentation of this file.
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 <set>
36#include <utility>
37#include <regex>
38#ifdef WIN32
39#include <windows.h>
40#endif
41#include "CameraProperties.h"
42#include "ErrorStatus.h"
43#include "CommonTypes.h"
44#include "Camera.h"
45
46inline void printCameraInfo(const mmind::eye::CameraInfo& cameraInfo)
47{
48 std::cout << "............................." << std::endl;
49 std::cout << "Model: " << cameraInfo.model << std::endl;
50#ifdef WIN32
51 const auto consoleCP = GetConsoleOutputCP();
52 SetConsoleOutputCP(CP_UTF8);
53#endif
54 std::cout << "Device name: " << cameraInfo.deviceName << std::endl;
55#ifdef WIN32
56 SetConsoleOutputCP(consoleCP);
57#endif
58 std::cout << "Serial number: " << cameraInfo.serialNumber << std::endl;
59 std::cout << "IP address: " << cameraInfo.ipAddress << std::endl;
60 std::cout << "Subnet mask: " << cameraInfo.subnetMask << std::endl;
61 std::cout << "IP address assignment method: "
62 << mmind::eye::ipAssignmentMethodToString(cameraInfo.ipAssignmentMethod) << std::endl;
63 std::cout << "Hardware version: "
64 << "V" << cameraInfo.hardwareVersion.toString() << std::endl;
65 std::cout << "Firmware version: "
66 << "V" << cameraInfo.firmwareVersion.toString() << std::endl;
67 std::cout << "............................." << std::endl;
68 std::cout << std::endl;
69}
70
71inline void printCameraStatus(const mmind::eye::CameraStatus& cameraStatus)
72{
73 std::cout << ".....Camera temperatures....." << std::endl;
74 std::cout << "CPU: " << cameraStatus.temperature.cpuTemperature << "°C" << std::endl;
75 std::cout << "Projector: " << cameraStatus.temperature.projectorTemperature << "°C"
76 << std::endl;
77 std::cout << "............................" << std::endl;
78
79 std::cout << std::endl;
80}
81
82inline void printCameraResolutions(const mmind::eye::CameraResolutions& cameraResolutions)
83
84{
85 std::cout << ".....Image resolutions....." << std::endl;
86 std::cout << "2D image (texture): " << cameraResolutions.texture.width << " (width) × "
87 << cameraResolutions.texture.height << " (height)" << std::endl;
88 std::cout << "Depth map: " << cameraResolutions.depth.width << " (width) × "
89 << cameraResolutions.depth.height << " (height)" << std::endl;
90}
91
92inline void printCameraMatrix(const std::string& title,
93 const mmind::eye::CameraMatrix& cameraMatrix)
94{
95 std::cout << title << ": " << std::endl
96 << " [" << cameraMatrix.fx << ", " << 0 << ", " << cameraMatrix.cx << "]"
97
98 << std::endl
99 << " [" << 0 << ", " << cameraMatrix.fy << ", " << cameraMatrix.cy << "]"
100
101 << std::endl
102 << " [" << 0 << ", " << 0 << ", " << 1 << "]" << std::endl;
103 std::cout << std::endl;
104}
105
106inline void printCameraDistCoeffs(const std::string& title,
107 const mmind::eye::CameraDistortion& distCoeffs)
108{
109 std::cout << title << ": " << std::endl
110 << " k1: " << distCoeffs.k1 << ", k2: " << distCoeffs.k2
111 << ", p1: " << distCoeffs.p1 << ", p2: " << distCoeffs.p2 << ", k3: " << distCoeffs.k3
112 << std::endl;
113 std::cout << std::endl;
114}
115
116inline void printTransform(const std::string& title, const mmind::eye::Transformation& transform)
117{
118 std::cout << "Rotation: " << title << ": " << std::endl;
119 for (int i = 0; i < 3; i++) {
120 std::cout << " [";
121 for (int j = 0; j < 3; j++) {
122 std::cout << transform.rotation[i][j];
123 if (j != 2)
124 std::cout << ", ";
125 }
126 std::cout << "]" << std::endl;
127 }
128 std::cout << std::endl;
129 std::cout << "Translation " << title << ": " << std::endl;
130 std::cout << " X: " << transform.translation[0] << "mm, Y: " << transform.translation[1]
131 << "mm, Z: " << transform.translation[2] << "mm" << std::endl;
132 std::cout << std::endl;
133}
134
136{
137 printCameraMatrix("Texture 2D camera matrix", intrinsics.texture.cameraMatrix);
138 printCameraDistCoeffs("Texture 2D camera distortion coefficients",
139 intrinsics.texture.cameraDistortion);
140
141 printCameraMatrix("Depth 2D camera matrix", intrinsics.depth.cameraMatrix);
142 printCameraDistCoeffs("Depth 2D camera distortion coefficients",
143 intrinsics.depth.cameraDistortion);
144
145 printTransform("Transformation from depth 2D camera to texture 2D camera",
146 intrinsics.depthToTexture);
147}
148
150{
151 std::cout << "Looking for available cameras..." << std::endl;
152 std::vector<mmind::eye::CameraInfo> deviceInfoList = mmind::eye::Camera::discoverCameras();
153
154 if (deviceInfoList.empty()) {
155 std::cout << "No cameras are available." << std::endl;
156 return false;
157 }
158
159 for (int i = 0; i < deviceInfoList.size(); i++) {
160 std::cout << "Mech-Eye device index: " << i << std::endl;
161 printCameraInfo(deviceInfoList[i]);
162 }
163
164 std::cout << "Enter the index of the device to which you want to connect: ";
165 unsigned inputIndex = 0;
166
167 while (true) {
168 std::string str;
169 std::cin >> str;
170 if (std::regex_match(str.begin(), str.end(), std::regex{"[0-9]+"}) &&
171 atoi(str.c_str()) < deviceInfoList.size()) {
172 inputIndex = atoi(str.c_str());
173 break;
174 }
175 std::cout << "The entered index is invalid. Please enter the device index again: ";
176 }
177
179 status = device.connect(deviceInfoList[inputIndex]);
180
181 if (!status.isOK()) {
182 showError(status);
183 return false;
184 }
185
186 std::cout << "Successfully connected to the camera." << std::endl;
187 return true;
188}
189
190inline std::vector<mmind::eye::Camera> findAndConnectMultiCamera()
191{
192 std::cout << "Looking for available cameras..." << std::endl;
193 std::vector<mmind::eye::CameraInfo> cameraInfoList = mmind::eye::Camera::discoverCameras();
194
195 if (cameraInfoList.empty()) {
196 std::cout << "No cameras are available." << std::endl;
197 return {};
198 }
199
200 for (int i = 0; i < cameraInfoList.size(); i++) {
201 std::cout << "Mech-Eye device index: " << i << std::endl;
202 printCameraInfo(cameraInfoList[i]);
203 }
204
205 std::string str;
206 std::set<unsigned> indices;
207
208 while (true) {
209 std::cout << "Enter the indices of the devices to which you want to connect: " << std::endl;
210 std::cout << "Enter the character \"c\" at the end of all the indices" << std::endl;
211
212 std::cin >> str;
213 if (str == "c")
214 break;
215 if (std::regex_match(str.begin(), str.end(), std::regex{"[0-9]+"}) &&
216 atoi(str.c_str()) < cameraInfoList.size())
217 indices.insert(atoi(str.c_str()));
218 else
219 std::cout << "The entered indices are invalid. Please enter the device indices again: ";
220 }
221
222 std::vector<mmind::eye::Camera> cameraList{};
223
224 auto iter = indices.cbegin();
225 for (int i = 0; i < indices.size(); ++i, ++iter) {
226 mmind::eye::Camera camera;
227 auto status = camera.connect(cameraInfoList[*iter]);
228 if (status.isOK())
229 cameraList.push_back(camera);
230 else
231 showError(status);
232 }
233
234 return cameraList;
235}
236
237inline bool confirmCapture3D()
238{
239 std::cout
240 << "Do you want the camera to capture 3D data? Enter \"y\" to confirm or \"n\" to cancel: "
241 << std::endl;
242 while (true) {
243 std::string confirmStr;
244 std::cin >> confirmStr;
245 if (confirmStr == "y") {
246 return true;
247 } else if (confirmStr == "n") {
248 std::cout << "The capture command was canceled." << std::endl;
249 return false;
250 } else {
251 std::cout << "The entered character was invalid. Please enter \"y\" to confirm or "
252 "\"n\" to cancel:"
253 << std::endl;
254 }
255 }
256}
void showError(const mmind::api::ErrorStatus &status)
Definition SampleUtil.h:40
void printCameraMatrix(const std::string &title, const mmind::eye::CameraMatrix &cameraMatrix)
Definition api_util.h:92
bool findAndConnect(mmind::eye::Camera &device)
Definition api_util.h:149
std::vector< mmind::eye::Camera > findAndConnectMultiCamera()
Definition api_util.h:190
void printCameraInfo(const mmind::eye::CameraInfo &cameraInfo)
Definition api_util.h:46
bool confirmCapture3D()
Definition api_util.h:237
void printCameraStatus(const mmind::eye::CameraStatus &cameraStatus)
Definition api_util.h:71
void printCameraDistCoeffs(const std::string &title, const mmind::eye::CameraDistortion &distCoeffs)
Definition api_util.h:106
void printCameraIntrinsics(const mmind::eye::CameraIntrinsics &intrinsics)
Definition api_util.h:135
void printCameraResolutions(const mmind::eye::CameraResolutions &cameraResolutions)
Definition api_util.h:82
void printTransform(const std::string &title, const mmind::eye::Transformation &transform)
Definition api_util.h:116
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
Describes the distortion parameters.
Definition CameraProperties.h:94
double k3
Radial distortion coefficients.
Definition CameraProperties.h:99
double k2
Radial distortion coefficients.
Definition CameraProperties.h:96
double k1
Radial distortion coefficients.
Definition CameraProperties.h:95
double p2
Tangential distortion coefficients.
Definition CameraProperties.h:98
double p1
Tangential distortion coefficients.
Definition CameraProperties.h:97
Defines the camera information.
Definition CameraProperties.h:49
IpAssignmentMethod ipAssignmentMethod
The IP address assignment method of the device.
Definition CameraProperties.h:58
std::string serialNumber
The serial number of the device.
Definition CameraProperties.h:52
std::string model
The device model, such as Mech-Eye NANO.
Definition CameraProperties.h:50
Version firmwareVersion
The version of the firmware (upgradable).
Definition CameraProperties.h:55
std::string subnetMask
The subnet mask of the device.
Definition CameraProperties.h:57
Version hardwareVersion
The version of the hardware (pre-determined in the factory).
Definition CameraProperties.h:54
std::string deviceName
The device name (UTF-8 encoded).
Definition CameraProperties.h:51
std::string ipAddress
The IP address of the device.
Definition CameraProperties.h:56
Defines the 3D camera intrinsic parameters, including the intrinsic parameters of the texture 2D came...
Definition CameraProperties.h:139
Transformation depthToTexture
Definition CameraProperties.h:145
Intrinsics2DCamera depth
The intrinsic parameters of the depth 2D camera(s) for capturing the depth map.
Definition CameraProperties.h:143
Intrinsics2DCamera texture
Definition CameraProperties.h:140
Describes the camera intrinsic parameter matrix.
Definition CameraProperties.h:83
double fy
Focal lengths.
Definition CameraProperties.h:85
double cy
Principal point.
Definition CameraProperties.h:87
double fx
Focal lengths.
Definition CameraProperties.h:84
double cx
Principal point.
Definition CameraProperties.h:86
Defines the camera image resolutions, including the resolutions of the 2D image (texture) and depth m...
Definition CameraProperties.h:128
Size depth
Definition CameraProperties.h:130
Size texture
Definition CameraProperties.h:129
Describes the camera's statuses.
Definition CameraProperties.h:75
DeviceTemperature temperature
Definition CameraProperties.h:76
float projectorTemperature
The temperature (in °C) of the camera projector.
Definition CameraProperties.h:68
float cpuTemperature
The temperature (in °C) of the camera CPU.
Definition CameraProperties.h:67
Describes the types of errors.
Definition ErrorStatus.h:12
bool isOK() const
Returns true if the operation succeeded.
Definition ErrorStatus.h:85
CameraDistortion cameraDistortion
Definition CameraProperties.h:108
CameraMatrix cameraMatrix
Definition CameraProperties.h:109
size_t height
Definition CommonTypes.h:46
size_t width
Definition CommonTypes.h:45
Defines the rigid body transformations, including rotation matrix and translation vector.
Definition CameraProperties.h:118
double translation[3]
3*1 translation vector in [x(mm), y(mm), z(mm)].
Definition CameraProperties.h:120
double rotation[3][3]
3*3 rotation matrix.
Definition CameraProperties.h:119