modelViewer.cpp
Go to the documentation of this file.
1 
18 //Global includes
19 #include <string>
20 #include <boost/filesystem.hpp>
21 
22 //Pkg includes
23 #include <ros/ros.h>
24 
25 #include <asr_ism_visualizations/ObjectModelVisualizerRVIZ.hpp>
26 #include <asr_ism_visualizations/model_visualizer_rviz.hpp>
27 
28 //ISM includes
29 #include <ISM/common_type/Tracks.hpp>
30 #include <ISM/utility/GeometryHelper.hpp>
31 #include <ISM/utility/TableHelper.hpp>
32 #include <ISM/utility/Util.hpp>
33 
34 
36 {
37  public:
38  ModelViewer(): nh_("~")
39  {
40  std::string db_filename;
41  std::string base_frame;
42  std::string visualization_topic;
43 
44  getNodeParameters(db_filename, base_frame, visualization_topic);
45  table_helper_ = ISM::TableHelperPtr(new ISM::TableHelper(db_filename));
46  if(!table_helper_->recordDataExists())
47  {
48  ISM::printRed("The database \"" + db_filename + "\" doesn't contain any recordings!\n");
49  exit(0);
50  }
51  if(!table_helper_->modelDataExists())
52  {
53  ISM::printRed("The database \"" + db_filename + "\" doesn't contain a model!\n");
54  exit(0);
55  }
56 
58  initVotes();
60 
61 
62  visualization_publisher_ = nh_.advertise<visualization_msgs::MarkerArray>(visualization_topic, 10000);
63  model_visualizer_ = new VIZ::ModelVisualizerRVIZ(visualization_publisher_, base_frame, "", 0);
64 
65  ISM::printGreen("Generate and publish models for " + scene_name_ + "!\n");
66  ISM::printGreen("\t(sub)pattern:\n");
68  for (std::pair<const std::string, std::map<int, std::vector<ISM::VoteSpecifierPtr>>>& track_index_to_votes : pattern_to_track_index_to_votes_)
69  {
70  ISM::printBlue("\t\t" + track_index_to_votes.first + "\n");
71  model_visualizer_->addVisualization(track_index_to_votes.first, tracks_, ism_object_tracks_, track_index_to_votes.second);
72  }
73 
75  }
76 
77  private:
78 
79  void getNodeParameters(std::string& db_filename, std::string& base_frame, std::string& visualization_topic)
80  {
81  if (!nh_.getParam("dbfilename", db_filename))
82  {
83  db_filename = "";
84  }
85  ROS_INFO_STREAM("dbfilename: " << db_filename);
86 
87  if (!nh_.getParam("baseFrame", base_frame))
88  {
89  base_frame = "/map";
90  }
91  ROS_INFO_STREAM("baseFrame: " << base_frame);
92 
93  if (!nh_.getParam("visualization_topic", visualization_topic))
94  {
95  visualization_topic = "";
96  }
97  ROS_INFO_STREAM("visualization_topic: " << visualization_topic);
98 
99  if (!nh_.getParam("sceneName", scene_name_))
100  {
101  scene_name_ = "";
102  }
103  ROS_INFO_STREAM("sceneName: " << scene_name_);
104  }
105 
106 
108  {
109  ISM::RecordedPatternPtr recorded_pattern = table_helper_->getRecordedPattern(scene_name_);
110  if (recorded_pattern == nullptr)
111  {
112  ISM::printRed("No recorded pattern in the database, with the name: " + scene_name_ + "\n");
113  exit(0);
114  }
115 
116  std::vector<ISM::ObjectSetPtr> recorded_object_sets = recorded_pattern->objectSets;
117  if (recorded_object_sets.empty())
118  {
119  ISM::printRed("No tracks in the database for the pattern with the name: " + scene_name_ +"\n");
120  exit(0);
121  }
122 
123  tracks_ = ISM::TracksPtr(new ISM::Tracks(recorded_object_sets));
124  }
125 
126 
127  void initVotes()
128  {
129  std::vector<std::string> pattern_names = table_helper_->getModelPatternNames();
130  ISM::PatternToObjectToVoteMap pattern_to_object_to_votes;
131  bool pattern_exist = false;
132 
133  // retrieve all votes for the pattern and sub-patterns
134  for (std::string pattern_name : pattern_names)
135  {
136  if(pattern_name.find(scene_name_) == 0)
137  {
138  std::set<std::pair<std::string, std::string> > objects;
139  objects = table_helper_->getObjectTypesAndIdsBelongingToPattern(pattern_name);
140  pattern_to_object_to_votes[pattern_name] = table_helper_->getVoteSpecifiersForPatternAndObjects(pattern_name, objects);
141 
142  pattern_exist = pattern_exist || !pattern_to_object_to_votes[pattern_name].empty();
143  }
144  }
145  if (!pattern_exist)
146  {
147  ISM::printRed("No pattern in the database, with the name: " + scene_name_ + "\n");
148  exit(0);
149  }
150 
151  /* arrange votes by pattern and trackIndex */
152  for (std::pair<const std::string, ISM::ObjectToVoteMap>& object_to_votes : pattern_to_object_to_votes)
153  {
154  for (std::pair<const std::string, std::map<std::string, std::vector<ISM::VoteSpecifierPtr>>>& obj_id_to_votes : object_to_votes.second)
155  {
156  for (std::pair<const std::string, std::vector<ISM::VoteSpecifierPtr>>& votes : obj_id_to_votes.second)
157  {
158  for(ISM::VoteSpecifierPtr vote : votes.second)
159  {
160  pattern_to_track_index_to_votes_[object_to_votes.first][vote->trackIndex].push_back(vote);
161  }
162  }
163  }
164  }
165  }
166 
167 
169  {
170  ISM::TracksPtr overall_tracks = ISM::TracksPtr(new ISM::Tracks(tracks_->tracks));
171  ism_object_tracks_ = ISM::TracksPtr(new ISM::Tracks(std::vector<ISM::TrackPtr>()));
172 
173  // generate for each (sub-)pattern the corresponding reference track
174  for (std::pair<const std::string, std::map<int, std::vector<ISM::VoteSpecifierPtr>>>& track_index_to_votes : pattern_to_track_index_to_votes_)
175  {
176  ISM::TrackPtr reference_track = ISM::TrackPtr(new ISM::Track(track_index_to_votes.first));
177 
178  // find reference object for each timestep of a track
179  for (size_t i = 0; i < track_index_to_votes.second.size(); i++)
180  {
181  // fill timesteps until now which we hadn't found a reference for, with (empty)object without type or id
182  while(reference_track->objects.size() < i)
183  {
184  reference_track->objects.push_back(ISM::ObjectPtr());
185  }
186 
187  /* find reference object of the pattern and generate ism object which represents this pattern */
188  for (ISM::VoteSpecifierPtr vote : track_index_to_votes.second[i])
189  {
190  if (ISM::GeometryHelper::isSelfVote(vote))
191  {
192  ISM::ObjectPtr ism_object;
193  ISM::TrackPtr track = overall_tracks->getTrackByTypeAndId(vote->objectType, vote->observedId);
194  if (track != nullptr)
195  {
196  ism_object = ISM::ObjectPtr(new ISM::Object(*(track->objects[i])));
197 
198  if (ism_object != nullptr)
199  {
200  ism_object->type = track_index_to_votes.first;
201  ism_object->observedId = "";
202  }
203  }
204 
205  reference_track->objects.push_back(ism_object);
206  break;
207  }
208  }
209  }
210 
211  // fill timesteps after the last we found a reference for, with (empty)object without type or id
212  while(reference_track->objects.size() < overall_tracks->tracks[0]->objects.size())
213  {
214  reference_track->objects.push_back(ISM::ObjectPtr());
215  }
216 
217  ism_object_tracks_->tracks.push_back(reference_track);
218  overall_tracks->tracks.push_back(reference_track);
219  }
220  }
221 
222 
224  {
225  unsigned int previous_number = number_of_subscriber_;
227  if(number_of_subscriber_ > 0 && (number_of_subscriber_ > previous_number))
228  {
229  ISM::printGreen("Publish model marker!\n");
230  model_visualizer_->publishCollectedMarkers();
231  std::cout<<"\n";
232  }
233  }
234 
235 
236 
239  ISM::TableHelperPtr table_helper_;
240 
241  std::string scene_name_;
242  std::map<std::string, std::map<int, std::vector<ISM::VoteSpecifierPtr>>> pattern_to_track_index_to_votes_;
243  ISM::TracksPtr tracks_;
244  ISM::TracksPtr ism_object_tracks_;
245 
246  //Visualization stuff
248  VIZ::ModelVisualizerRVIZ* model_visualizer_;
249  unsigned int number_of_subscriber_;
250 };
251 
252 int main (int argc, char **argv)
253 {
254  //Usual ros node stuff
255  ros::init(argc, argv, "modelViewer");
256  ModelViewer* model_viewer = new ModelViewer();
257 
258  ros::spin();
259 
260  delete model_viewer;
261  return 0;
262 }
void getNodeParameters(std::string &db_filename, std::string &base_frame, std::string &visualization_topic)
Definition: modelViewer.cpp:79
void refreshVisualization(const ros::TimerEvent &e)
ros::Timer timer_
VIZ::ModelVisualizerRVIZ * model_visualizer_
void initObjectTracks()
void initISMObjectTrack()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL void spin(Spinner &spinner)
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
unsigned int number_of_subscriber_
ros::Publisher visualization_publisher_
ISM::TracksPtr tracks_
ISM::TracksPtr ism_object_tracks_
uint32_t getNumSubscribers() const
#define ROS_INFO_STREAM(args)
void initVotes()
bool getParam(const std::string &key, std::string &s) const
std::string scene_name_
std::map< std::string, std::map< int, std::vector< ISM::VoteSpecifierPtr > > > pattern_to_track_index_to_votes_
ros::NodeHandle nh_
ISM::TableHelperPtr table_helper_
int main(int argc, char **argv)


asr_ism
Author(s): Borella Jocelyn, Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Thu Jan 9 2020 07:20:58