property_factory.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Bielefeld University
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Bielefeld University nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Robert Haschke */
36 
37 #include "property_factory.h"
38 
39 #include <boost/functional/factory.hpp>
41 #include <moveit/task_constructor/properties.h>
42 
46 
47 namespace mtc = ::moveit::task_constructor;
48 
49 namespace moveit_rviz_plugin {
50 
51 static rviz::StringProperty* stringFactory(const QString& name, mtc::Property& mtc_prop,
52  const planning_scene::PlanningScene* /*unused*/,
53  rviz::DisplayContext* /*unused*/) {
54  std::string value;
55  if (!mtc_prop.value().empty())
56  value = boost::any_cast<std::string>(mtc_prop.value());
57  rviz::StringProperty* rviz_prop =
58  new rviz::StringProperty(name, QString::fromStdString(value), QString::fromStdString(mtc_prop.description()));
59  QObject::connect(rviz_prop, &rviz::StringProperty::changed,
60  [rviz_prop, &mtc_prop]() { mtc_prop.setValue(rviz_prop->getStdString()); });
61  return rviz_prop;
62 }
63 template <typename T>
64 static rviz::FloatProperty* floatFactory(const QString& name, mtc::Property& mtc_prop,
65  const planning_scene::PlanningScene* /*unused*/,
66  rviz::DisplayContext* /*unused*/) {
67  T value = !mtc_prop.value().empty() ? boost::any_cast<T>(mtc_prop.value()) : T();
68  rviz::FloatProperty* rviz_prop =
69  new rviz::FloatProperty(name, value, QString::fromStdString(mtc_prop.description()));
70  QObject::connect(rviz_prop, &rviz::FloatProperty::changed,
71  [rviz_prop, &mtc_prop]() { mtc_prop.setValue(T(rviz_prop->getFloat())); });
72  return rviz_prop;
73 }
74 
76  // register some standard types
77  registerType<float>(&floatFactory<float>);
78  registerType<double>(&floatFactory<double>);
79  registerType<std::string>(&stringFactory);
80 }
81 
82 PropertyFactory& PropertyFactory::instance() {
84  return instance;
85 }
86 
87 void PropertyFactory::registerType(const std::string& type_name, const PropertyFactoryFunction& f) {
88  if (type_name.empty())
89  return;
90  property_registry_.insert(std::make_pair(type_name, f));
91 }
92 
93 void PropertyFactory::registerStage(const std::type_index& type_index, const PropertyFactory::TreeFactoryFunction& f) {
94  stage_registry_.insert(std::make_pair(type_index, f));
95 }
96 
97 rviz::Property* PropertyFactory::create(const std::string& prop_name, mtc::Property& prop,
98  const planning_scene::PlanningScene* scene,
99  rviz::DisplayContext* display_context) const {
100  auto it = property_registry_.find(prop.typeName());
101  if (it == property_registry_.end())
102  return createDefault(prop_name, prop.typeName(), prop.description(), prop.serialize());
103  return it->second(QString::fromStdString(prop_name), prop, scene, display_context);
104 }
105 
108  rviz::DisplayContext* display_context) {
109  auto it = stage_registry_.find(typeid(stage));
110  if (it == stage_registry_.end())
111  return defaultPropertyTreeModel(stage.properties(), scene, display_context);
112  return it->second(stage.properties(), scene, display_context);
113 }
114 
116  const planning_scene::PlanningScene* scene,
117  rviz::DisplayContext* display_context) {
118  auto root = new rviz::Property();
119  addRemainingProperties(root, properties, scene, display_context);
120  return new rviz::PropertyTreeModel(root, nullptr);
121 }
122 
123 static bool hasChild(rviz::Property* root, const QString& name) {
124  for (int i = 0, end = root->numChildren(); i != end; ++i) {
125  if (root->childAt(i)->getName() == name)
126  return true;
127  }
128  return false;
129 }
130 
132  const planning_scene::PlanningScene* scene,
133  rviz::DisplayContext* display_context) {
134  for (auto& prop : properties) {
135  const QString& name = QString::fromStdString(prop.first);
136  if (hasChild(root, name))
137  continue;
138 
139  rviz::Property* rviz_prop = create(prop.first, prop.second, scene, display_context);
140  if (!rviz_prop)
141  rviz_prop = new rviz::Property(name);
142  root->addChild(rviz_prop);
143  }
144 
145  // just to see something, when no properties are defined
146  if (root->numChildren() == 0)
147  new rviz::Property("no properties", QVariant(), QString(), root);
148 }
149 
150 #ifndef HAVE_YAML
151 rviz::Property* PropertyFactory::createDefault(const std::string& name, const std::string& /*type*/,
152  const std::string& description, const std::string& value,
153  rviz::Property* old) {
154  if (old) { // reuse existing Property?
155  assert(old->getNameStd() == name);
156  old->setDescription(QString::fromStdString(description));
157  old->setValue(QString::fromStdString(value));
158  return old;
159  } else { // create new Property?
160  rviz::Property* result = new rviz::StringProperty(QString::fromStdString(name), QString::fromStdString(value),
161  QString::fromStdString(description));
162  result->setReadOnly(true);
163  return result;
164  }
165 }
166 #endif
167 } // namespace moveit_rviz_plugin
moveit_rviz_plugin::PropertyFactory::create
rviz::Property * create(const std::string &prop_name, moveit::task_constructor::Property &prop, const planning_scene::PlanningScene *scene, rviz::DisplayContext *display_context) const
create rviz::Property for given MTC Property
Definition: property_factory.cpp:129
moveit_rviz_plugin::PropertyFactory::stage_registry_
std::map< std::type_index, TreeFactoryFunction > stage_registry_
Definition: property_factory.h:121
moveit_rviz_plugin::PropertyFactory::addRemainingProperties
void addRemainingProperties(rviz::Property *root, moveit::task_constructor::PropertyMap &properties, const planning_scene::PlanningScene *scene, rviz::DisplayContext *display_context)
add all properties from map that are not yet in root
Definition: property_factory.cpp:163
rviz::PropertyTreeModel
moveit_rviz_plugin::PropertyFactory::instance
static PropertyFactory & instance()
Definition: property_factory.cpp:114
planning_scene::PlanningScene
rviz::Property::getNameStd
std::string getNameStd() const
moveit::task_constructor::Property::value
boost::any & value()
moveit_rviz_plugin::stringFactory
static rviz::StringProperty * stringFactory(const QString &name, mtc::Property &mtc_prop, const planning_scene::PlanningScene *, rviz::DisplayContext *)
Definition: property_factory.cpp:83
moveit::task_constructor::Stage::properties
PropertyMap & properties()
moveit::task_constructor::Property::serialize
std::string serialize() const
property_factory.h
moveit::task_constructor::Stage
float_property.h
moveit_rviz_plugin::PropertyFactory::PropertyFactory
PropertyFactory()
class is singleton
Definition: property_factory.cpp:107
moveit_rviz_plugin::PropertyFactory::TreeFactoryFunction
std::function< rviz::PropertyTreeModel *(moveit::task_constructor::PropertyMap &, const planning_scene::PlanningScene *, rviz::DisplayContext *)> TreeFactoryFunction
Definition: property_factory.h:82
rviz::FloatProperty
moveit_rviz_plugin::PropertyFactory::createPropertyTreeModel
rviz::PropertyTreeModel * createPropertyTreeModel(moveit::task_constructor::Stage &stage, const planning_scene::PlanningScene *scene, rviz::DisplayContext *display_context)
create PropertyTreeModel for given Stage
Definition: property_factory.cpp:138
rviz::Property
moveit::task_constructor::PropertyMap
moveit_rviz_plugin::PropertyFactory::defaultPropertyTreeModel
rviz::PropertyTreeModel * defaultPropertyTreeModel(moveit::task_constructor::PropertyMap &properties, const planning_scene::PlanningScene *scene, rviz::DisplayContext *display_context)
turn a PropertyMap into an rviz::PropertyTreeModel
Definition: property_factory.cpp:147
rviz::FloatProperty::getFloat
virtual float getFloat() const
rviz::Property::setValue
virtual bool setValue(const QVariant &new_value)
property_tree_model.h
moveit::task_constructor
rviz::StringProperty
rviz::StringProperty::getStdString
std::string getStdString()
moveit_rviz_plugin::hasChild
static bool hasChild(rviz::Property *root, const QString &name)
Definition: property_factory.cpp:155
stage.h
value
float value
moveit::task_constructor::Property::setValue
void setValue(const boost::any &value)
rviz::DisplayContext
name
name
moveit_rviz_plugin::PropertyFactory
Definition: property_factory.h:73
moveit_rviz_plugin::PropertyFactory::property_registry_
std::map< std::string, PropertyFactoryFunction > property_registry_
Definition: property_factory.h:120
moveit_rviz_plugin
rviz::Property::setDescription
virtual void setDescription(const QString &description)
f
f
moveit_rviz_plugin::floatFactory
static rviz::FloatProperty * floatFactory(const QString &name, mtc::Property &mtc_prop, const planning_scene::PlanningScene *, rviz::DisplayContext *)
Definition: property_factory.cpp:96
rviz::Property::setReadOnly
virtual void setReadOnly(bool read_only)
moveit::task_constructor::Property::typeName
std::string typeName() const
moveit_rviz_plugin::PropertyFactory::registerType
void registerType(const PropertyFactoryFunction &f)
register a factory function for type T
Definition: property_factory.h:86
moveit::task_constructor::Property::description
const std::string & description() const
moveit::task_constructor::Property
rviz::Property::changed
void changed()
moveit_rviz_plugin::PropertyFactory::registerStage
void registerStage(const TreeFactoryFunction &f)
register a factory function for stage T
Definition: property_factory.h:93
root
root
string_property.h
moveit_rviz_plugin::PropertyFactory::createDefault
static rviz::Property * createDefault(const std::string &name, const std::string &type, const std::string &description, const std::string &value, rviz::Property *old=nullptr)
create rviz::Property for property of given name, type, description, and value
Definition: property_factory.cpp:183


visualization
Author(s): Robert Haschke
autogenerated on Thu Feb 27 2025 03:39:51