CameraUtils.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 
28 // Gazebo
29 #include "gazebo/common/Plugin.hh"
30 #include <gazebo/msgs/msgs.hh>
31 #include "gazebo/sensors/CameraSensor.hh"
32 #include "gazebo/rendering/Camera.hh"
33 #include "gazebo/util/system.hh"
34 // Custom messages
35 #include "camera_utils_request.pb.h"
36 #include "camera_utils_response.pb.h"
37 // Strings
38 #include <string>
39 // To create directories
40 #include <boost/filesystem.hpp>
41 // Malloc and memcpy
42 #include <stdio.h>
43 #include <string.h>
44 
45 namespace CameraUtils {
46 
48 #define REQUEST_TOPIC "~/gap/camera_utils"
49 #define RESPONSE_TOPIC "~/gap/camera_utils/response"
51 
53 #define MOVE_REQUEST gap::msgs::CameraUtilsRequest::MOVE
54 #define MOVE_RESPONSE gap::msgs::CameraUtilsResponse::MOVE
56 #define CAPTURE_REQUEST gap::msgs::CameraUtilsRequest::CAPTURE
58 #define CAPTURE_RESPONSE gap::msgs::CameraUtilsResponse::CAPTURE
60 #define OPTIONS_REQUEST gap::msgs::CameraUtilsRequest::OPTIONS
62 #define PROJECTION_REQUEST gap::msgs::CameraUtilsRequest::PROJECTION
64 #define PROJECTION_RESPONSE gap::msgs::CameraUtilsResponse::PROJECTION
66 
67 // Default parameters
68 
70 #define DEFAULT_OUTPUT_DIR (const std::string) "/tmp/camera_utils_output/"
71 #define DEFAULT_EXTENSION (const std::string) ".png"
73 
74 }
75 
76 namespace gazebo{
77 
79  typedef const boost::shared_ptr<const gap::msgs::CameraUtilsRequest>
82  typedef const boost::shared_ptr<const gap::msgs::CameraUtilsResponse>
84 
85  // Forward declaration of private data class
86  class CameraUtilsPrivate;
87 
105  class CameraUtils : public SensorPlugin {
106 
107  // Private attributes
108 
110  private: std::unique_ptr<CameraUtilsPrivate> dataPtr;
112  private: std::string output_dir;
114  private: int saved_counter {0};
116  private: std::string next_file_name;
118  private: bool save_on_update {false};
120  private: event::ConnectionPtr newFrameConnection;
121 
122  // Protected attributes
123 
125  protected: sensors::CameraSensorPtr parentSensor;
127  protected: rendering::CameraPtr camera;
129  protected: unsigned int width;
131  protected: unsigned int height;
133  protected: unsigned int depth;
135  protected: std::string format;
137  protected: std::string extension;
138 
139  // Public methods
140 
142  public: CameraUtils();
143 
145  public: virtual ~CameraUtils();
146 
150  public: virtual void Load(sensors::SensorPtr _sensor, sdf::ElementPtr _sdf);
151 
158  public: void OnNewFrame(const unsigned char *_image,
159  unsigned int _width, unsigned int _height,
160  unsigned int _depth, const std::string &_format);
161 
162  // Private methods
163 
166  private: void onRequest(CameraUtilsRequestPtr &_msg);
167  };
168 }
Definition: CameraUtils.cc:30
Class for private camera utils plugin data.
Definition: CameraUtils.cc:34
const boost::shared_ptr< const gap::msgs::CameraUtilsResponse > CameraUtilsReplyPtr
Shared pointer declaration for response message type.
Definition: CameraUtils.hh:83
event::ConnectionPtr newFrameConnection
Connects to new frame rendered event.
Definition: CameraUtils.hh:120
std::string format
Image format.
Definition: CameraUtils.hh:135
std::string extension
Exported image extension.
Definition: CameraUtils.hh:137
const boost::shared_ptr< const gap::msgs::CameraUtilsRequest > CameraUtilsRequestPtr
Shared pointer declaration for request message type.
Definition: CameraUtils.hh:80
std::string output_dir
Directory for saving output.
Definition: CameraUtils.hh:112
unsigned int depth
Image depth.
Definition: CameraUtils.hh:133
unsigned int width
Image width.
Definition: CameraUtils.hh:129
std::unique_ptr< CameraUtilsPrivate > dataPtr
Class with private attributes.
Definition: CameraUtils.hh:110
std::string next_file_name
File name for next capture.
Definition: CameraUtils.hh:116
sensors::CameraSensorPtr parentSensor
Pointer to camera sensor.
Definition: CameraUtils.hh:125
Definition: CameraUtils.hh:45
rendering::CameraPtr camera
Pointer to camera object.
Definition: CameraUtils.hh:127
unsigned int height
Image height.
Definition: CameraUtils.hh:131