forked from joao-borrego/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualUtils.hh
137 lines (112 loc) · 4.35 KB
/
VisualUtils.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (C) 2018 João Borrego
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*!
\file visual_utils/VisualUtils.hh
\brief Visual Utils plugin
A custom gazebo plugin that provides an interface to programatically
change the visual properties of an object.
\author João Borrego : jsbruglie
\author Rui Figueiredo : ruipimentelfigueiredo
*/
// Gazebo
#include <gazebo/common/Events.hh>
#include "gazebo/common/Plugin.hh"
#include "gazebo/common/SystemPaths.hh"
#include <gazebo/msgs/msgs.hh>
#include "gazebo/rendering/RenderEngine.hh"
#include <gazebo/rendering/Visual.hh>
#include <gazebo/transport/Node.hh>
// Boost - for convenient string split
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
// Mutex
#include <mutex>
// Shuffle vector
#include <algorithm>
#include <random>
#include <chrono>
// Custom messages
#include "visual_utils_request.pb.h"
#include "visual_utils_response.pb.h"
namespace VisualUtils {
/// Topic monitored for incoming commands
#define REQUEST_TOPIC "~/gap/visual_utils"
/// Topic for publishing replies
#define RESPONSE_TOPIC "~/gap/visual_utils/response"
/// Request update
#define UPDATE gap::msgs::VisualUtilsRequest::UPDATE
/// Set default pose
#define DEFAULT_POSE gap::msgs::VisualUtilsRequest::DEFAULT_POSE
/// TODO
#define MATERIAL gap::msgs::VisualUtilsRequest::MATERIAL_PREFIX
/// Visual updated response
#define UPDATED gap::msgs::VisualUtilsResponse::UPDATED
// Default parameters
/// Default unique name
#define DEFAULT_NAME "default"
}
namespace gazebo{
/// Shared pointer declaration for request message type
typedef const boost::shared_ptr<const gap::msgs::VisualUtilsRequest>
VisualUtilsRequestPtr;
/// Shared pointer declaration for response message type
typedef const boost::shared_ptr<const gap::msgs::VisualUtilsResponse>
VisualUtilsResponsePtr;
// Forward declaration of private data class
class VisualUtilsPrivate;
/// \brief A custom gazebo plugin that provides an interface to programatically
/// alter visuals during simulation.
///
/// Materials are assumed to be loaded and name [pattern][index]
/// See the example usage below:
///
/// \code{.xml}
/// <plugin name="visual_utils" filename="libVisualUtils.so">
/// <!-- Unique name identifier -->
/// <uid>box_1</uid>
/// <!-- Prefix patterns for material names, separated by whitespace -->
/// <patterns>Plugin/flat_ Plugin/gradient_ ... </patterns>
/// <!-- Number of variants per prefix pattern -->
/// <variants>100</variants>
/// </plugin>
/// \endcode
///
/// See worlds/visual.world for a complete example.
class VisualUtils : public VisualPlugin {
/// \brief Constructs the object
public: VisualUtils();
/// \brief Destroys the object
public: virtual ~VisualUtils();
/// \brief Loads the plugin
/// \param _visual The visual to which the plugin is attached
/// \param _sdf The SDF element with plugin parameters
public: virtual void Load(
rendering::VisualPtr _visual,
sdf::ElementPtr _sdf);
/// \brief Update once per simulation iteration.
public: void Update();
/// \brief Callback function for handling incoming requests
/// \param _msg The message
public: void onRequest(VisualUtilsRequestPtr & _msg);
/// \brief Private data pointer
private: std::unique_ptr<VisualUtilsPrivate> dataPtr;
/// \brief Loads names of available materials.
private: void loadResources();
/// \brief Randomly generates a new material name.
/// \param name Output random material name
private: void randomMaterialName(std::string & name);
};
}