ModelState.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
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 */
17 
18 #ifndef GAZEBO_PHYSICS_MODELSTATE_HH_
19 #define GAZEBO_PHYSICS_MODELSTATE_HH_
20 
21 #include <vector>
22 #include <string>
23 #include <boost/regex.hpp>
24 
25 #include <ignition/math/Pose3.hh>
26 #include <ignition/math/Vector3.hh>
27 #include "gazebo/math/Pose.hh"
28 
29 #include "gazebo/physics/State.hh"
32 #include "gazebo/util/system.hh"
33 
34 namespace gazebo
35 {
36  namespace physics
37  {
40 
49  class GZ_PHYSICS_VISIBLE ModelState : public State
50  {
52  public: ModelState();
53 
62  public: ModelState(const ModelPtr _model, const common::Time &_realTime,
63  const common::Time &_simTime, const uint64_t _iterations);
64 
70  public: explicit ModelState(const ModelPtr _model);
71 
76  public: explicit ModelState(const sdf::ElementPtr _sdf);
77 
79  public: virtual ~ModelState();
80 
89  public: void Load(const ModelPtr _model, const common::Time &_realTime,
90  const common::Time &_simTime, const uint64_t _iterations);
91 
96  public: virtual void Load(const sdf::ElementPtr _elem);
97 
101  public: const math::Pose GetPose() const GAZEBO_DEPRECATED(8.0);
102 
105  public: const ignition::math::Pose3d &Pose() const;
106 
109  public: const ignition::math::Vector3d &Scale() const;
110 
113  public: bool IsZero() const;
114 
119  public: unsigned int GetLinkStateCount() const;
120 
125  public: LinkState_M GetLinkStates(const boost::regex &_regex) const;
126 
131  public: JointState_M GetJointStates(const boost::regex &_regex) const;
132 
140  public: LinkState GetLinkState(const std::string &_linkName) const;
141 
145  public: bool HasLinkState(const std::string &_linkName) const;
146 
149  public: const LinkState_M &GetLinkStates() const;
150 
155  public: unsigned int GetJointStateCount() const;
156 
164  public: JointState GetJointState(unsigned int _index) const;
165 
173  public: JointState GetJointState(const std::string &_jointName) const;
174 
177  public: const JointState_M &GetJointStates() const;
178 
182  public: bool HasJointState(const std::string &_jointName) const;
183 
188  public: unsigned int NestedModelStateCount() const;
189 
197  public: ModelState NestedModelState(const std::string &_modelName) const;
198 
202  public: bool HasNestedModelState(const std::string &_modelName) const;
203 
206  public: const ModelState_M &NestedModelStates() const;
207 
210  public: void FillSDF(sdf::ElementPtr _sdf);
211 
215  public: virtual void SetWallTime(const common::Time &_time);
216 
219  public: virtual void SetRealTime(const common::Time &_time);
220 
223  public: virtual void SetSimTime(const common::Time &_time);
224 
228  public: virtual void SetIterations(const uint64_t _iterations);
229 
233  public: ModelState &operator=(const ModelState &_state);
234 
238  public: ModelState operator-(const ModelState &_state) const;
239 
243  public: ModelState operator+(const ModelState &_state) const;
244 
249  public: inline friend std::ostream &operator<<(std::ostream &_out,
250  const gazebo::physics::ModelState &_state)
251  {
252  ignition::math::Vector3d euler(_state.pose.Rot().Euler());
253  _out.unsetf(std::ios_base::floatfield);
254  _out << std::setprecision(3)
255  << "<model name='" << _state.GetName() << "'>"
256  << "<pose>"
257  << ignition::math::precision(_state.pose.Pos().X(), 4) << " "
258  << ignition::math::precision(_state.pose.Pos().Y(), 4) << " "
259  << ignition::math::precision(_state.pose.Pos().Z(), 4) << " "
260  << ignition::math::precision(euler.X(), 4) << " "
261  << ignition::math::precision(euler.Y(), 4) << " "
262  << ignition::math::precision(euler.Z(), 4) << " "
263  << "</pose>";
264 
265  // Only record scale if it is not the default value of [1, 1, 1].
266  if (_state.scale != ignition::math::Vector3d::One)
267  _out << "<scale>" << _state.scale << "</scale>";
268 
269  for (LinkState_M::const_iterator iter =
270  _state.linkStates.begin(); iter != _state.linkStates.end();
271  ++iter)
272  {
273  _out << iter->second;
274  }
275 
276  for (const auto &ms : _state.modelStates)
277  {
278  _out << ms.second;
279  }
280 
281  // Output the joint information
282  // for (JointState_M::const_iterator iter =
283  // _state.jointStates.begin(); iter != _state.jointStates.end();
284  // ++iter)
285  // {
286  // _out << iter->second;
287  // }
288 
289  _out << "</model>";
290 
291  return _out;
292  }
293 
295  private: ignition::math::Pose3d pose;
296 
298  private: ignition::math::Vector3d scale;
299 
301  private: LinkState_M linkStates;
302 
304  private: JointState_M jointStates;
305 
307  private: ModelState_M modelStates;
308  };
310  }
311 }
312 #endif
std::map< std::string, ModelState > ModelState_M
Definition: PhysicsTypes.hh:233
std::string GetName() const
Get the name associated with this State.
Encapsulates a position and rotation in three space.
Definition: Pose.hh:42
std::map< std::string, JointState > JointState_M
Definition: PhysicsTypes.hh:245
State of an entity.
Definition: State.hh:49
keeps track of state of a physics::Joint
Definition: JointState.hh:43
#define GAZEBO_DEPRECATED(version)
Definition: system.hh:302
friend std::ostream & operator<<(std::ostream &_out, const gazebo::physics::ModelState &_state)
Stream insertion operator.
Definition: ModelState.hh:249
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:93
std::map< std::string, LinkState > LinkState_M
Definition: PhysicsTypes.hh:241
Store state information of a physics::Model object.
Definition: ModelState.hh:49
Store state information of a physics::Link object.
Definition: LinkState.hh:47
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:44