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 
28 #include "gazebo/physics/State.hh"
31 #include "gazebo/util/system.hh"
32 
33 namespace gazebo
34 {
35  namespace physics
36  {
39 
48  class GZ_PHYSICS_VISIBLE ModelState : public State
49  {
51  public: ModelState();
52 
61  public: ModelState(const ModelPtr _model, const common::Time &_realTime,
62  const common::Time &_simTime, const uint64_t _iterations);
63 
69  public: explicit ModelState(const ModelPtr _model);
70 
75  public: explicit ModelState(const sdf::ElementPtr _sdf);
76 
78  public: virtual ~ModelState();
79 
88  public: void Load(const ModelPtr _model, const common::Time &_realTime,
89  const common::Time &_simTime, const uint64_t _iterations);
90 
95  public: virtual void Load(const sdf::ElementPtr _elem);
96 
99  public: const ignition::math::Pose3d &Pose() const;
100 
103  public: const ignition::math::Vector3d &Scale() const;
104 
107  public: bool IsZero() const;
108 
113  public: unsigned int GetLinkStateCount() const;
114 
119  public: LinkState_M GetLinkStates(const boost::regex &_regex) const;
120 
125  public: JointState_M GetJointStates(const boost::regex &_regex) const;
126 
134  public: LinkState GetLinkState(const std::string &_linkName) const;
135 
139  public: bool HasLinkState(const std::string &_linkName) const;
140 
143  public: const LinkState_M &GetLinkStates() const;
144 
149  public: unsigned int GetJointStateCount() const;
150 
158  public: JointState GetJointState(unsigned int _index) const;
159 
167  public: JointState GetJointState(const std::string &_jointName) const;
168 
171  public: const JointState_M &GetJointStates() const;
172 
176  public: bool HasJointState(const std::string &_jointName) const;
177 
182  public: unsigned int NestedModelStateCount() const;
183 
191  public: ModelState NestedModelState(const std::string &_modelName) const;
192 
196  public: bool HasNestedModelState(const std::string &_modelName) const;
197 
200  public: const ModelState_M &NestedModelStates() const;
201 
204  public: void FillSDF(sdf::ElementPtr _sdf);
205 
209  public: virtual void SetWallTime(const common::Time &_time);
210 
213  public: virtual void SetRealTime(const common::Time &_time);
214 
217  public: virtual void SetSimTime(const common::Time &_time);
218 
222  public: virtual void SetIterations(const uint64_t _iterations);
223 
227  public: ModelState &operator=(const ModelState &_state);
228 
232  public: ModelState operator-(const ModelState &_state) const;
233 
237  public: ModelState operator+(const ModelState &_state) const;
238 
243  public: inline friend std::ostream &operator<<(std::ostream &_out,
244  const gazebo::physics::ModelState &_state)
245  {
246  ignition::math::Vector3d euler(_state.pose.Rot().Euler());
247  _out.unsetf(std::ios_base::floatfield);
248  _out << std::setprecision(3)
249  << "<model name='" << _state.GetName() << "'>"
250  << "<pose>"
251  << ignition::math::precision(_state.pose.Pos().X(), 4) << " "
252  << ignition::math::precision(_state.pose.Pos().Y(), 4) << " "
253  << ignition::math::precision(_state.pose.Pos().Z(), 4) << " "
254  << ignition::math::precision(euler.X(), 4) << " "
255  << ignition::math::precision(euler.Y(), 4) << " "
256  << ignition::math::precision(euler.Z(), 4) << " "
257  << "</pose>";
258 
259  // Only record scale if it is not the default value of [1, 1, 1].
260  if (_state.scale != ignition::math::Vector3d::One)
261  _out << "<scale>" << _state.scale << "</scale>";
262 
263  for (LinkState_M::const_iterator iter =
264  _state.linkStates.begin(); iter != _state.linkStates.end();
265  ++iter)
266  {
267  _out << iter->second;
268  }
269 
270  for (const auto &ms : _state.modelStates)
271  {
272  _out << ms.second;
273  }
274 
275  // Output the joint information
276  // for (JointState_M::const_iterator iter =
277  // _state.jointStates.begin(); iter != _state.jointStates.end();
278  // ++iter)
279  // {
280  // _out << iter->second;
281  // }
282 
283  _out << "</model>";
284 
285  return _out;
286  }
287 
289  private: ignition::math::Pose3d pose;
290 
292  private: ignition::math::Vector3d scale;
293 
295  private: LinkState_M linkStates;
296 
298  private: JointState_M jointStates;
299 
301  private: ModelState_M modelStates;
302  };
304  }
305 }
306 #endif
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:48
keeps track of state of a physics::Joint
Definition: JointState.hh:43
Store state information of a physics::Link object.
Definition: LinkState.hh:48
Store state information of a physics::Model object.
Definition: ModelState.hh:49
LinkState_M GetLinkStates(const boost::regex &_regex) const
Get link states based on a regular expression.
virtual void SetRealTime(const common::Time &_time)
Set the real time when this state was generated.
virtual void SetSimTime(const common::Time &_time)
Set the sim time when this state was generated.
bool HasLinkState(const std::string &_linkName) const
Return true if there is a link with the specified name.
ModelState(const ModelPtr _model)
Constructor.
ModelState()
Default constructor.
const LinkState_M & GetLinkStates() const
Get the link states.
virtual void SetWallTime(const common::Time &_time)
Set the wall time when this state was generated.
const JointState_M & GetJointStates() const
Get the joint states.
LinkState GetLinkState(const std::string &_linkName) const
Get a link state by Link name.
ModelState & operator=(const ModelState &_state)
Assignment operator.
friend std::ostream & operator<<(std::ostream &_out, const gazebo::physics::ModelState &_state)
Stream insertion operator.
Definition: ModelState.hh:243
ModelState(const sdf::ElementPtr _sdf)
Constructor.
bool HasNestedModelState(const std::string &_modelName) const
Return true if there is a nested model with the specified name.
JointState_M GetJointStates(const boost::regex &_regex) const
Get joint states based on a regular expression.
const ModelState_M & NestedModelStates() const
Get the nested model states.
ModelState operator+(const ModelState &_state) const
Addition operator.
ModelState(const ModelPtr _model, const common::Time &_realTime, const common::Time &_simTime, const uint64_t _iterations)
Constructor.
bool IsZero() const
Return true if the values in the state are zero.
bool HasJointState(const std::string &_jointName) const
Return true if there is a joint with the specified name.
unsigned int GetJointStateCount() const
Get the number of joint states.
JointState GetJointState(const std::string &_jointName) const
Get a Joint state by Joint name.
unsigned int NestedModelStateCount() const
Get the number of model states.
unsigned int GetLinkStateCount() const
Get the number of link states.
const ignition::math::Pose3d & Pose() const
Get the stored model pose.
JointState GetJointState(unsigned int _index) const
Get a Joint state.
void Load(const ModelPtr _model, const common::Time &_realTime, const common::Time &_simTime, const uint64_t _iterations)
Load state from Model pointer.
virtual ~ModelState()
Destructor.
void FillSDF(sdf::ElementPtr _sdf)
Populate a state SDF element with data from the object.
virtual void Load(const sdf::ElementPtr _elem)
Load state from SDF element.
virtual void SetIterations(const uint64_t _iterations)
Set the simulation iterations when this state was generated.
const ignition::math::Vector3d & Scale() const
Get the stored model scale.
ModelState operator-(const ModelState &_state) const
Subtraction operator.
ModelState NestedModelState(const std::string &_modelName) const
Get a model state by model name.
State of an entity.
Definition: State.hh:50
std::string GetName() const
Get the name associated with this State.
std::map< std::string, ModelState > ModelState_M
Definition: PhysicsTypes.hh:233
std::map< std::string, LinkState > LinkState_M
Definition: PhysicsTypes.hh:241
std::map< std::string, JointState > JointState_M
Definition: PhysicsTypes.hh:245
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:93
Forward declarations for the common classes.
Definition: Animation.hh:27