scene_example.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 João Borrego
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
27 // Includes
28 
29 // Gazebo
30 #include <gazebo/gazebo_config.h>
31 #include <gazebo/transport/transport.hh>
32 #include <gazebo/msgs/msgs.hh>
33 #include <gazebo/gazebo_client.hh>
34 
35 // Custom messages
36 #include "camera_utils_request.pb.h"
37 #include "camera_utils_response.pb.h"
38 #include "visual_utils_request.pb.h"
39 #include "visual_utils_response.pb.h"
40 #include "world_utils_request.pb.h"
41 #include "world_utils_response.pb.h"
42 
43 // Utilities
44 #include "utils.hh"
45 // Object class
46 #include "ObjectGrid.hh"
47 
48 // I/O streams
49 #include <iostream>
50 // File streams
51 #include <fstream>
52 // Iterating over the contents of a dir
53 #include <boost/filesystem.hpp>
54 // Protecting variables
55 #include <mutex>
56 // Set container
57 #include <set>
58 // Sleep
59 #include <chrono>
60 #include <thread>
61 // Regular expressions
62 #include <regex>
63 // Linear algebra
64 #include <Eigen/Dense>
65 // INT MAX
66 #include <climits>
67 
69 
70 // Macros
71 
72 // Fixed viewpoint
73 #define FIXED_VIEW 0
74 // Moving viewpoint, change in position
75 #define MOVING_VIEW_POS 1
76 // Moving viewpoint, change in position and rotation
77 #define MOVING_VIEW_POS_ROT 2
78 
79 // Do not raise documentation error
81 
83 #define REGEX_XML_MODEL "<model name=(\"([^\"]|\"\")*\")>"
84 #define REGEX_XML_UID "<uid>[\\s\\S]*?<\\/uid>"
86 
88 
90 
91 // Macros for custom messages
92 
93 // Camera utils
94 
96 #define MOVE_REQUEST gap::msgs::CameraUtilsRequest::MOVE
97 #define MOVE_RESPONSE gap::msgs::CameraUtilsResponse::MOVE
99 #define CAPTURE_REQUEST gap::msgs::CameraUtilsRequest::CAPTURE
101 #define CAPTURE_RESPONSE gap::msgs::CameraUtilsResponse::CAPTURE
103 #define PROJECTION_REQUEST gap::msgs::CameraUtilsRequest::PROJECTION
105 #define PROJECTION_RESPONSE gap::msgs::CameraUtilsResponse::PROJECTION
107 #define OPTIONS gap::msgs::CameraUtilsRequest::OPTIONS
109 
110 // Visual utils
111 
113 #define UPDATE gap::msgs::VisualUtilsRequest::UPDATE
114 #define UPDATED gap::msgs::VisualUtilsResponse::UPDATED
116 
117 // World utils
118 
120 #define SPAWN gap::msgs::WorldUtilsRequest::SPAWN
121 #define WORLD_MOVE gap::msgs::WorldUtilsRequest::MOVE
123 #define PHYSICS gap::msgs::WorldUtilsRequest::PHYSICS
125 
127 #define CUSTOM gap::msgs::Object::CUSTOM
128 #define CUSTOM_LIGHT gap::msgs::Object::CUSTOM_LIGHT
130 
132 
133 // API Topics
134 
136 #define CAMERA_UTILS_TOPIC "~/gap/camera_utils"
137 #define CAMERA_UTILS_RESPONSE_TOPIC "~/gap/camera_utils/response"
139 #define VISUAL_UTILS_TOPIC "~/gap/visual_utils"
141 #define VISUAL_UTILS_RESPONSE_TOPIC "~/gap/visual_utils/response"
143 #define WORLD_UTILS_TOPIC "~/gap/world_utils"
145 #define WORLD_UTILS_RESPONSE_TOPIC "~/gap/world_utils/response"
147 
148 // Message pointer typedefs
149 
151 typedef const boost::shared_ptr<const gap::msgs::CameraUtilsResponse>
154 typedef const boost::shared_ptr<const gap::msgs::VisualUtilsResponse>
157 typedef const boost::shared_ptr<const gap::msgs::WorldUtilsResponse>
159 
161 
163 
167 void addModelFromFile(
168  gap::msgs::WorldUtilsRequest & msg,
169  const std::string & file);
170 
173 void addDynamicModels(gap::msgs::WorldUtilsRequest & msg);
174 
176 void updateObjects(gap::msgs::VisualUtilsRequest & msg);
177 
183 void addMoveObject(
184  gap::msgs::WorldUtilsRequest & msg,
185  const std::string & name,
186  const bool is_light,
187  const ignition::math::Pose3d & pose);
188 
191 ignition::math::Pose3d getRandomCameraPose();
192 
195 ignition::math::Pose3d getRandomLightPose();
196 
200 void captureScene(gazebo::transport::PublisherPtr pub, int iteration);
201 
204 bool waitForMove();
205 
208 bool waitForVisuals();
209 
212 bool waitForCamera();
213 
215 bool waitForProjections();
216 
218 void createNameSet();
219 
221 void addProjections(gap::msgs::CameraUtilsRequest & msg);
222 
224 void moveCamera(gazebo::transport::PublisherPtr pub);
225 
229 
233 
237 
241 void setPhysics(gazebo::transport::PublisherPtr pub, bool enable);
242 
245 void visualizeData(const std::string & image_dir, int iteration);
246 
250 void storeAnnotations(
251  const std::string & path,
252  const int iteration);
void onWorldUtilsResponse(WorldUtilsResponsePtr &_msg)
Callback function for WorldUtils response.
Definition: scene_example.cc:506
void addDynamicModels(gap::msgs::WorldUtilsRequest &msg)
Add objects in global grid to WorldUtils spawn request.
Definition: scene_example.cc:257
bool waitForCamera()
Wait for camera to save frame to disk.
Definition: scene_example.cc:471
void onVisualUtilsResponse(VisualUtilsResponsePtr &_msg)
Callback function for VisualUtils response.
Definition: scene_example.cc:493
void moveCamera(gazebo::transport::PublisherPtr pub)
Move camera to global camera pose.
Definition: scene_example.cc:427
ignition::math::Pose3d getRandomCameraPose()
Obtain random camera pose in dome.
Definition: scene_example.cc:333
const boost::shared_ptr< const gap::msgs::VisualUtilsResponse > VisualUtilsResponsePtr
Pointer to VisualUtils request message.
Definition: scene_example.hh:155
void visualizeData(const std::string &image_dir, int iteration)
Debug function to visualise scene Debug function to visualise acquired frame and object bounding boxe...
bool waitForMove()
Wait for camera to move to new pose.
Definition: scene_example.cc:449
bool waitForProjections()
Wait for projected points.
Definition: scene_example.cc:482
ObjectGrid and Object classes.
ignition::math::Pose3d getRandomLightPose()
Obtain random light pose in dome.
Definition: scene_example.cc:373
void captureScene(gazebo::transport::PublisherPtr pub, int iteration)
Send CameraUtils request to capture current scene.
Definition: scene_example.cc:440
bool waitForVisuals()
Wait for visuals to update.
Definition: scene_example.cc:460
void addProjections(gap::msgs::CameraUtilsRequest &msg)
Add 3D points to projection request.
Definition: scene_example.cc:407
void storeAnnotations(const std::string &path, const int iteration)
Store current scene annotations.
Definition: scene_example.cc:575
const boost::shared_ptr< const gap::msgs::WorldUtilsResponse > WorldUtilsResponsePtr
Pointer to WorldUtils request message.
Definition: scene_example.hh:158
void addMoveObject(gap::msgs::WorldUtilsRequest &msg, const std::string &name, const bool is_light, const ignition::math::Pose3d &pose)
Add move object command to WorldUtils request.
Definition: scene_example.cc:315
void updateObjects(gap::msgs::VisualUtilsRequest &msg)
Add objects in global grid to VisualUtils update request.
Definition: scene_example.cc:290
void onCameraUtilsResponse(CameraUtilsResponsePtr &_msg)
Callback function for CameraUtils response.
Definition: scene_example.cc:512
void setPhysics(gazebo::transport::PublisherPtr pub, bool enable)
Enables/disables physics engine.
Definition: scene_example.cc:566
Generic utilities for scene generation example.
const boost::shared_ptr< const gap::msgs::CameraUtilsResponse > CameraUtilsResponsePtr
Pointer to CameraUtils response message.
Definition: scene_example.hh:152
void addModelFromFile(gap::msgs::WorldUtilsRequest &msg, const std::string &file)
Function prototypes.
Definition: scene_example.cc:240
void createNameSet()
Create set with names of existing objects.
Definition: scene_example.cc:390