Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
ZEDCAMPointCloudTest.hpp File Reference

Example file that demonstrates getting and viewing the point cloud of the ZEDCam. More...

Include dependency graph for ZEDCAMPointCloudTest.hpp:

Go to the source code of this file.

Functions

void DepthMouseCallback (int nEvent, int nX, int nY, int nFlags, void *pUserData)
 Mouse callback function for depth image clicks.
void PointCloudMouseCallback (int nEvent, int nX, int nY, int nFlags, void *pUserData)
 Mouse callback function for point cloud clicks.
void SuppressPCLLogging ()
 Suppresses PCL logging messages.
void RunExample ()
 Main example function.

Variables

const float VOXEL_GRID_LEAF_SIZE = 0.05f

Detailed Description

Example file that demonstrates getting and viewing the point cloud of the ZEDCam.

Author
clayjay3
Date
2023-09-16

Function Documentation

◆ DepthMouseCallback()

void DepthMouseCallback ( int nEvent,
int nX,
int nY,
int nFlags,
void * pUserData )

Mouse callback function for depth image clicks.

Parameters
nEvent- The type of mouse event (e.g., left button click).
nX- The x-coordinate of the mouse event.
nY- The y-coordinate of the mouse event.
nFlags- The flags associated with the mouse event.
pUserData- Pointer to user data (in this case, the depth image).
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-05-01
53{
54 (void) nFlags; // Unused parameter
55 if (nEvent != cv::EVENT_LBUTTONDOWN)
56 return;
57
58 cv::Mat* cvDepthImage = static_cast<cv::Mat*>(pUserData);
59 if (nX >= 0 && nY >= 0 && nX < cvDepthImage->cols && nY < cvDepthImage->rows)
60 {
61 // Handle different depth image types
62 float fDepthValue = 0.0f;
63
64 // Check the depth image type and retrieve the depth value accordingly
65 fDepthValue = cvDepthImage->at<float>(nY, nX);
66 std::cout << "Depth at (" << nX << ", " << nY << "): " << fDepthValue << " m" << std::endl;
67 }
68}
_Tp & at(int i0=0)
int rows
int cols
EVENT_LBUTTONDOWN
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PointCloudMouseCallback()

void PointCloudMouseCallback ( int nEvent,
int nX,
int nY,
int nFlags,
void * pUserData )

Mouse callback function for point cloud clicks.

Parameters
nEvent- The type of mouse event (e.g., left button click).
nX- The x-coordinate of the mouse event.
nY- The y-coordinate of the mouse event.
nFlags- The flags associated with the mouse event.
pUserData- Pointer to user data (in this case, the point cloud).
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-05-03
83{
84 (void) nFlags; // Unused parameter
85 if (nEvent != cv::EVENT_LBUTTONDOWN)
86 return;
87
88 cv::Mat* cvPointCloud = static_cast<cv::Mat*>(pUserData);
89 if (nX >= 0 && nY >= 0 && nX < cvPointCloud->cols && nY < cvPointCloud->rows)
90 {
91 // Handle different point cloud types
92 cv::Vec4f cvPoint = cvPointCloud->at<cv::Vec4f>(nY, nX);
93 std::cout << "Point at (" << nX << ", " << nY << "): "
94 << "X: " << cvPoint[0] << ", "
95 << "Y: " << cvPoint[1] << ", "
96 << "Z: " << cvPoint[2] << std::endl;
97 }
98}
Vec< float, 4 > Vec4f
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SuppressPCLLogging()

void SuppressPCLLogging ( )

Suppresses PCL logging messages.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-05-01
107{
108 pcl::console::setVerbosityLevel(pcl::console::L_ALWAYS);
109}
Here is the caller graph for this function:

◆ RunExample()

void RunExample ( )

Main example function.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-03-05
119{
120 // Suppress logging messages
122
123 // Initialize and start handlers.
124 globals::g_pCameraHandler = new CameraHandler();
125
126 // Get pointer to camera.
127 std::shared_ptr<ZEDCamera> pExampleZEDCam1 = globals::g_pCameraHandler->GetZED(CameraHandler::ZEDCamName::eHeadMainCam);
128 // Start ZED cam.
129 pExampleZEDCam1->Start();
130
131 // Declare mats to store images in.
132 cv::Mat cvNormalFrame1;
133 cv::Mat cvDepthFrame1;
134 cv::Mat cvPointCloud1;
135 cv::Mat cvPointCloudColor1;
136 cv::cuda::GpuMat cvGPUNormalFrame1;
137 cv::cuda::GpuMat cvGPUDepthFrame1;
138 cv::cuda::GpuMat cvGPUPointCloud1;
139 // Declare other data types to store data in.
140 ZEDCam::Pose stPose;
141
142 // Declare FPS counter.
143 IPS FPS = IPS();
144
145 // Create a depth display window and set up mouse callback
146 cv::namedWindow("Depth Frame", cv::WINDOW_AUTOSIZE);
147
148 // Loop forever, or until user hits ESC.
149 while (true)
150 {
151 // Create instance variables.
152 std::future<bool> fuDepthCopyStatus;
153 std::future<bool> fuPointCloudCopyStatus;
154
155 // Check if the camera is setup to use CPU or GPU mats.
156 if (pExampleZEDCam1->GetUsingGPUMem())
157 {
158 // Grab frames from camera.
159 fuDepthCopyStatus = pExampleZEDCam1->RequestDepthCopy(cvGPUDepthFrame1);
160 fuPointCloudCopyStatus = pExampleZEDCam1->RequestPointCloudCopy(cvGPUPointCloud1);
161 }
162 else
163 {
164 // Grab frames from camera.
165 fuDepthCopyStatus = pExampleZEDCam1->RequestDepthCopy(cvDepthFrame1);
166 fuPointCloudCopyStatus = pExampleZEDCam1->RequestPointCloudCopy(cvPointCloud1);
167 }
168
169 // Wait for the frames to be copied.
170 if (fuDepthCopyStatus.get() && fuPointCloudCopyStatus.get())
171 {
172 if (pExampleZEDCam1->GetUsingGPUMem())
173 {
174 // Download data from GPU matrices.
175 cvGPUDepthFrame1.download(cvDepthFrame1);
176 cvGPUPointCloud1.download(cvPointCloud1);
177 }
178
179 // Display the depth frame and set up mouse callback
180 if (!cvDepthFrame1.empty())
181 {
182 // Normalize the depth frame for display (depth is stored as 16-bit unsigned int)
183 cv::Mat cvDepthDisplay;
184 cv::normalize(cvDepthFrame1, cvDepthDisplay, 0, 255, cv::NORM_MINMAX, CV_8U);
185 cv::applyColorMap(cvDepthDisplay, cvDepthDisplay, cv::COLORMAP_JET);
186
187 // Show the depth frame
188 cv::imshow("Depth Frame", cvDepthDisplay);
189
190 // Set the mouse callback for the depth window
191 cv::setMouseCallback("Depth Frame", DepthMouseCallback, &cvDepthFrame1);
192 cv::setMouseCallback("Depth Frame", PointCloudMouseCallback, &cvPointCloud1);
193 }
194
195 // Use PCL to visualize the point cloud.
196 static pcl::PointCloud<pcl::PointXYZ>::Ptr pclCloud(new pcl::PointCloud<pcl::PointXYZ>());
197 static pcl::PointCloud<pcl::PointXYZ>::Ptr pclFilteredPointCloud(new pcl::PointCloud<pcl::PointXYZ>);
198 static pcl::PointCloud<pcl::PointXYZ>::Ptr pclDownsampledPointCloud(new pcl::PointCloud<pcl::PointXYZ>);
199 static pcl::visualization::PCLVisualizer::Ptr pclViewer;
200
201 // Initialize the PCL viewer on the first run
202 if (!pclViewer)
203 {
204 pclViewer.reset(new pcl::visualization::PCLVisualizer("Depth Point Cloud"));
205 pclViewer->setBackgroundColor(0, 0, 0);
206 pclViewer->addPointCloud<pcl::PointXYZ>(pclDownsampledPointCloud, "depth_cloud");
207 pclViewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "depth_cloud");
208 pclViewer->addCoordinateSystem(20.0);
209 pclViewer->initCameraParameters();
210 }
211
212 // Convert the OpenCV point cloud to PCL format.
213 pclCloud->width = cvPointCloud1.cols;
214 pclCloud->height = cvPointCloud1.rows;
215 pclCloud->is_dense = false;
216 pclCloud->points.resize(pclCloud->width * pclCloud->height);
217
218 // Copy points from OpenCV format to PCL format.
219 for (int nY = 0; nY < cvPointCloud1.rows; ++nY)
220 {
221 for (int nX = 0; nX < cvPointCloud1.cols; ++nX)
222 {
223 cv::Vec4f cvPoint = cvPointCloud1.at<cv::Vec4f>(nY, nX);
224 size_t siIDx = nY * cvPointCloud1.cols + nX;
225
226 // Only add points with valid depth.
227 if (cvPoint[2] > 0)
228 {
229 pclCloud->points[siIDx].x = cvPoint[0];
230 pclCloud->points[siIDx].y = cvPoint[1];
231 pclCloud->points[siIDx].z = cvPoint[2];
232 }
233 else
234 {
235 // For invalid points, set to NaN.
236 pclCloud->points[siIDx].x = std::numeric_limits<float>::quiet_NaN();
237 pclCloud->points[siIDx].y = std::numeric_limits<float>::quiet_NaN();
238 pclCloud->points[siIDx].z = std::numeric_limits<float>::quiet_NaN();
239 }
240 }
241 }
242
243 // Remove NaN points to clean up the cloud
244 std::vector<int> vIndices;
245 pcl::removeNaNFromPointCloud(*pclCloud, *pclFilteredPointCloud, vIndices);
246 // Downsample the point cloud for better visualization performance
247 pcl::VoxelGrid<pcl::PointXYZ> pclVoxelGrid;
248 pclVoxelGrid.setInputCloud(pclFilteredPointCloud);
249 pclVoxelGrid.setLeafSize(VOXEL_GRID_LEAF_SIZE, VOXEL_GRID_LEAF_SIZE, VOXEL_GRID_LEAF_SIZE);
250 pclVoxelGrid.filter(*pclDownsampledPointCloud);
251 // Update the point cloud in the viewer
252 pclViewer->updatePointCloud(pclDownsampledPointCloud, "depth_cloud");
253 pclViewer->spinOnce(10);
254 }
255
256 // Tick FPS counter.
257 FPS.Tick();
258
259 char chKey = cv::waitKey(1);
260 if (chKey == 27) // Press 'Esc' key to exit
261 break;
262 }
263
264 // Close all OpenCV windows.
266
268 // Cleanup.
270 // Stop RoveComm quill logging or quill will segfault if trying to output logs to RoveComm.
271 network::g_bRoveCommUDPStatus = false;
272 network::g_bRoveCommTCPStatus = false;
273
274 // Stop camera threads.
275 globals::g_pCameraHandler->StopAllCameras();
276}
void DepthMouseCallback(int nEvent, int nX, int nY, int nFlags, void *pUserData)
Mouse callback function for depth image clicks.
Definition ZEDCAMPointCloudTest.hpp:52
void SuppressPCLLogging()
Suppresses PCL logging messages.
Definition ZEDCAMPointCloudTest.hpp:106
void PointCloudMouseCallback(int nEvent, int nX, int nY, int nFlags, void *pUserData)
Mouse callback function for point cloud clicks.
Definition ZEDCAMPointCloudTest.hpp:82
The CameraHandler class is responsible for managing all of the camera feeds that Autonomy_Software us...
Definition CameraHandler.h:34
This util class provides an easy way to keep track of iterations per second for any body of code.
Definition IPS.hpp:30
void Tick()
This method is used to update the iterations per second counter and recalculate all of the IPS metric...
Definition IPS.hpp:138
bool empty() const
void download(OutputArray dst) const
NORM_MINMAX
static Vec< _Tp, cn > normalize(const Vec< _Tp, cn > &v)
#define CV_8U
WINDOW_AUTOSIZE
void imshow(const String &winname, InputArray mat)
int waitKey(int delay=0)
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
void destroyAllWindows()
void setMouseCallback(const String &winname, MouseCallback onMouse, void *userdata=0)
void applyColorMap(InputArray src, OutputArray dst, int colormap)
COLORMAP_JET
This struct is used within the ZEDCam class to store the camera pose with high precision....
Definition ZEDCamera.hpp:77
Here is the call graph for this function: