viewer.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * esmini - Environment Simulator Minimalistic
  3. * https://github.com/esmini/esmini
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  8. *
  9. * Copyright (c) partners of Simulation Scenarios
  10. * https://sites.google.com/view/simulationscenarios
  11. */
  12. #ifndef VIEWER_HPP_
  13. #define VIEWER_HPP_
  14. #include <osg/PositionAttitudeTransform>
  15. #include <osgViewer/Viewer>
  16. #include <osgGA/NodeTrackerManipulator>
  17. #include <osg/MatrixTransform>
  18. #include <osg/Material>
  19. #include <osgText/Text>
  20. #include <osgAnimation/EaseMotion>
  21. #include <osg/BlendColor>
  22. #include <string>
  23. #include "RubberbandManipulator.hpp"
  24. #include "IdealSensor.hpp"
  25. #include "RoadManager.hpp"
  26. #include "CommonMini.hpp"
  27. #include "roadgeom.hpp"
  28. #define TRAIL_DOT_FADE_DURATION 3.0 // seconds
  29. #define TRAIL_DOTS_DT 0.5
  30. #define TRAIL_MAX_DOTS 50
  31. #define TRAIL_DOT_LIFE_SPAN (0.5 * TRAIL_MAX_DOTS * TRAIL_DOTS_DT) // Start fade when half of the dots have been launched (seconds)
  32. extern double color_green[3];
  33. extern double color_gray[3];
  34. extern double color_dark_gray[3];
  35. extern double color_red[3];
  36. extern double color_blue[3];
  37. extern double color_yellow[3];
  38. extern double color_white[3];
  39. using namespace scenarioengine;
  40. namespace viewer
  41. {
  42. typedef enum
  43. {
  44. NODE_MASK_NONE = (0),
  45. NODE_MASK_OBJECT_SENSORS = (1 << 0),
  46. NODE_MASK_TRAILS = (1 << 1),
  47. NODE_MASK_ODR_FEATURES = (1 << 2),
  48. NODE_MASK_OSI_POINTS = (1 << 3),
  49. NODE_MASK_OSI_LINES = (1 << 4),
  50. NODE_MASK_ENV_MODEL = (1 << 5),
  51. NODE_MASK_ENTITY_MODEL = (1 << 6),
  52. NODE_MASK_ENTITY_BB = (1 << 7),
  53. NODE_MASK_INFO = (1 << 8),
  54. NODE_MASK_ROAD_SENSORS = (1 << 9),
  55. } NodeMask;
  56. class Line
  57. {
  58. public:
  59. osg::ref_ptr<osg::Geometry> line_;
  60. osg::ref_ptr<osg::Vec3Array> line_vertex_data_;
  61. Line(double x0, double y0, double z0, double x1, double y1, double z1, double r, double g, double b);
  62. void SetPoints(double x0, double y0, double z0, double x1, double y1, double z1);
  63. };
  64. class SensorViewFrustum
  65. {
  66. public:
  67. osg::ref_ptr<osg::PositionAttitudeTransform> txNode_;
  68. osg::ref_ptr<osg::Group> line_group_;
  69. std::vector<Line*> lines_;
  70. ObjectSensor *sensor_;
  71. SensorViewFrustum(ObjectSensor *sensor, osg::Group *parent);
  72. ~SensorViewFrustum();
  73. void Update();
  74. };
  75. class AlphaFadingCallback : public osg::StateAttributeCallback
  76. {
  77. public:
  78. AlphaFadingCallback(osgViewer::Viewer *viewer, osg::Vec4 color)
  79. {
  80. _motion = new osgAnimation::InCubicMotion(0.0f, TRAIL_DOT_FADE_DURATION);
  81. color_ = color;
  82. viewer_ = viewer;
  83. Reset();
  84. }
  85. virtual void operator()(osg::StateAttribute*, osg::NodeVisitor*);
  86. void Reset()
  87. {
  88. born_time_stamp_ = viewer_->elapsedTime();
  89. time_stamp_ = born_time_stamp_;
  90. _motion->reset();
  91. }
  92. protected:
  93. osg::ref_ptr<osgAnimation::InCubicMotion> _motion;
  94. private:
  95. osg::Vec4 color_;
  96. double time_stamp_;
  97. double born_time_stamp_;
  98. osgViewer::Viewer *viewer_;
  99. };
  100. class TrailDot
  101. {
  102. public:
  103. osg::ref_ptr<osg::PositionAttitudeTransform> dot_;
  104. osg::ref_ptr<osg::Material> material_;
  105. TrailDot(double x, double y, double z, double heading,
  106. osgViewer::Viewer *viewer, osg::Group *parent, osg::ref_ptr<osg::Node> dot_node, osg::Vec4 trail_color);
  107. void Reset(double x, double y, double z, double heading);
  108. private:
  109. osg::ref_ptr<AlphaFadingCallback> fade_callback_;
  110. };
  111. class Trail
  112. {
  113. public:
  114. TrailDot* dot_[TRAIL_MAX_DOTS];
  115. int n_dots_;
  116. int current_;
  117. osg::Group *parent_;
  118. osg::Node *dot_node_;
  119. void AddDot(double x, double y, double z, double heading);
  120. Trail(osg::Group *parent, osgViewer::Viewer *viewer, osg::ref_ptr<osg::Node> dot_node, osg::Vec3 color) :
  121. parent_(parent),
  122. viewer_(viewer),
  123. n_dots_(0),
  124. current_(0),
  125. dot_node_(dot_node)
  126. {
  127. color_[0] = color[0];
  128. color_[1] = color[1];
  129. color_[2] = color[2];
  130. }
  131. ~Trail();
  132. private:
  133. osg::Vec4 color_;
  134. osgViewer::Viewer *viewer_;
  135. };
  136. class PointSensor
  137. {
  138. public:
  139. osg::ref_ptr<osg::Group> group_;
  140. osg::ref_ptr<osg::PositionAttitudeTransform> ball_;
  141. double ball_radius_;
  142. osg::ref_ptr<osg::Geometry> line_;
  143. osg::ref_ptr<osg::Vec3Array> line_vertex_data_;
  144. osg::Vec3 pivot_pos;
  145. osg::Vec3 target_pos;
  146. PointSensor() : line_(0), line_vertex_data_(0), ball_(0) {};
  147. void Show() { group_->setNodeMask(NODE_MASK_ROAD_SENSORS); }
  148. void Hide() { group_->setNodeMask(0x0); };
  149. };
  150. class EntityModel
  151. {
  152. public:
  153. typedef enum {
  154. ENTITY_TYPE_VEHICLE,
  155. ENTITY_TYPE_OTHER
  156. } EntityType;
  157. osg::ref_ptr<osg::Group> group_;
  158. osg::ref_ptr<osg::LOD> lod_;
  159. osg::ref_ptr<osg::PositionAttitudeTransform> txNode_;
  160. osg::ref_ptr<osg::Group> bb_;
  161. osg::Quat quat_;
  162. double size_x;
  163. double size_y;
  164. double center_x;
  165. double center_y;
  166. static const int entity_type_ = ENTITY_TYPE_OTHER;
  167. virtual int GetType() { return entity_type_; }
  168. std::string name_;
  169. std::string filename_;
  170. osg::ref_ptr<osg::BlendColor> blend_color_;
  171. osg::ref_ptr<osg::StateSet> state_set_;
  172. EntityModel(osgViewer::Viewer* viewer, osg::ref_ptr<osg::Group> group, osg::ref_ptr<osg::Group> parent, osg::ref_ptr<osg::Group>
  173. trail_parent, osg::ref_ptr<osg::Node> dot_node, osg::Vec3 trail_color, std::string name);
  174. void SetPosition(double x, double y, double z);
  175. void SetRotation(double h, double p, double r);
  176. void SetTransparency(double factor);
  177. Trail* trail_;
  178. osgViewer::Viewer* viewer_;
  179. };
  180. class CarModel : public EntityModel
  181. {
  182. public:
  183. std::vector<osg::ref_ptr<osg::PositionAttitudeTransform>> wheel_;
  184. double wheel_angle_;
  185. double wheel_rot_;
  186. PointSensor* road_sensor_;
  187. PointSensor* lane_sensor_;
  188. PointSensor* trail_sensor_;
  189. PointSensor* steering_sensor_;
  190. static const int entity_type_ = ENTITY_TYPE_VEHICLE;
  191. virtual int GetType() { return entity_type_; }
  192. CarModel(osgViewer::Viewer* viewer, osg::ref_ptr<osg::Group> group, osg::ref_ptr<osg::Group> parent, osg::ref_ptr<osg::Group>
  193. trail_parent, osg::ref_ptr<osg::Node> dot_node, osg::Vec3 trail_color, std::string name);
  194. ~CarModel();
  195. osg::ref_ptr<osg::PositionAttitudeTransform> AddWheel(osg::ref_ptr<osg::Node> carNode, const char* wheelName);
  196. void UpdateWheels(double wheel_angle, double wheel_rotation);
  197. void UpdateWheelsDelta(double wheel_angle, double wheel_rotation_delta);
  198. };
  199. class VisibilityCallback : public osg::NodeCallback
  200. {
  201. public:
  202. VisibilityCallback(osg::Node* node, scenarioengine::Object* object, EntityModel* entity)
  203. {
  204. node_ = (osg::LOD*)node;
  205. object_ = object;
  206. entity_ = entity;
  207. }
  208. virtual void operator()(osg::Node*, osg::NodeVisitor*);
  209. protected:
  210. osg::ref_ptr<osgAnimation::InCubicMotion> _motion;
  211. private:
  212. scenarioengine::Object* object_;
  213. EntityModel* entity_;
  214. osg::LOD* node_;
  215. };
  216. typedef struct
  217. {
  218. int key_;
  219. int modKeyMask_;
  220. bool down_;
  221. } KeyEvent;
  222. typedef enum
  223. {
  224. ENTITY_HIDE,
  225. ENTITY_3D_MODEL,
  226. ENTITY_BOUNDINGBOX,
  227. ENTITY_BOTH
  228. } ENTITY_SHOW_MODE;
  229. typedef void (*KeyEventCallbackFunc)(KeyEvent*, void*);
  230. typedef struct
  231. {
  232. KeyEventCallbackFunc func;
  233. void* data;
  234. } KeyEventCallback;
  235. class Viewer
  236. {
  237. public:
  238. int currentCarInFocus_;
  239. int camMode_;
  240. osg::ref_ptr<osg::Group> line_node_;
  241. // Vehicle position debug visualization
  242. osg::ref_ptr<osg::Node> shadow_node_;
  243. // Trail dot model
  244. osg::ref_ptr<osg::Node> dot_node_;
  245. // Road debug visualization
  246. osg::ref_ptr<osg::Group> odrLines_;
  247. osg::ref_ptr<osg::Group> osiLines_;
  248. osg::ref_ptr<osg::Group> osiPoints_;
  249. osg::ref_ptr<osg::PositionAttitudeTransform> envTx_;
  250. osg::ref_ptr<osg::Node> environment_;
  251. osg::ref_ptr<osgGA::RubberbandManipulator> rubberbandManipulator_;
  252. osg::ref_ptr<osgGA::NodeTrackerManipulator> nodeTrackerManipulator_;
  253. std::vector<EntityModel*> entities_;
  254. float lodScale_;
  255. osgViewer::Viewer *osgViewer_;
  256. osg::MatrixTransform* rootnode_;
  257. osg::ref_ptr<osg::Group> roadSensors_;
  258. osg::ref_ptr<osg::Group> trails_;
  259. roadmanager::OpenDrive *odrManager_;
  260. bool showInfoText;
  261. RoadGeom* roadGeom;
  262. std::string exe_path_;
  263. std::vector<KeyEventCallback> callback_;
  264. osg::ref_ptr<osg::Camera> infoTextCamera;
  265. osg::ref_ptr<osgText::Text> infoText;
  266. Viewer(roadmanager::OpenDrive *odrManager, const char* modelFilename, const char* scenarioFilename, const char* exe_path, osg::ArgumentParser arguments, SE_Options* opt = 0);
  267. ~Viewer();
  268. void SetCameraMode(int mode);
  269. void SetVehicleInFocus(int idx);
  270. int GetEntityInFocus() { return currentCarInFocus_; }
  271. EntityModel* AddEntityModel(std::string modelFilepath, osg::Vec3 trail_color, EntityModel::EntityType type,
  272. bool road_sensor, std::string name, OSCBoundingBox *boundingBox);
  273. EntityModel* AddRadarModel(osg::Vec3 trail_color,std::string name);
  274. void RemoveCar(std::string name);
  275. int LoadShadowfile(std::string vehicleModelFilename);
  276. int AddEnvironment(const char* filename);
  277. osg::ref_ptr<osg::Group> LoadEntityModel(const char *filename);
  278. void UpdateSensor(PointSensor *sensor);
  279. void SensorSetPivotPos(PointSensor *sensor, double x, double y, double z);
  280. void SensorSetTargetPos(PointSensor *sensor, double x, double y, double z);
  281. void UpdateRoadSensors(PointSensor *road_sensor, PointSensor *lane_sensor, roadmanager::Position *pos);
  282. void setKeyUp(bool pressed) { keyUp_ = pressed; }
  283. void setKeyDown(bool pressed) { keyDown_ = pressed; }
  284. void setKeyLeft(bool pressed) { keyLeft_ = pressed; }
  285. void setKeyRight(bool pressed) { keyRight_ = pressed; }
  286. bool getKeyUp() { return keyUp_; }
  287. bool getKeyDown() { return keyDown_; }
  288. bool getKeyLeft() { return keyLeft_; }
  289. bool getKeyRight() { return keyRight_; }
  290. void SetQuitRequest(bool value) { quit_request_ = value; }
  291. bool GetQuitRequest() { return quit_request_; }
  292. void SetInfoTextProjection(int width, int height);
  293. void SetInfoText(const char* text);
  294. void ShowInfoText(bool show);
  295. void SetNodeMaskBits(int bits);
  296. void SetNodeMaskBits(int mask, int bits);
  297. void ClearNodeMaskBits(int bits);
  298. void ToggleNodeMaskBits(int bits);
  299. int GetNodeMaskBit(int mask);
  300. PointSensor* CreateSensor(double color[], bool create_ball, bool create_line, double ball_radius, double line_width);
  301. bool CreateRoadSensors(CarModel *vehicle_model);
  302. void SetWindowTitle(std::string title);
  303. void SetWindowTitleFromArgs(std::vector<std::string> &arg);
  304. void SetWindowTitleFromArgs(int argc, char* argv[]);
  305. void RegisterKeyEventCallback(KeyEventCallbackFunc func, void* data);
  306. private:
  307. bool CreateRoadLines(roadmanager::OpenDrive* od);
  308. bool CreateRoadMarkLines(roadmanager::OpenDrive* od);
  309. int CreateOutlineObject(roadmanager::Outline* outline);
  310. int LoadRoadFeature(roadmanager::Road* road, std::string filename, double s, double t,
  311. double z_offset, double scale_x, double scale_y, double scale_z, double heading_offset);
  312. int CreateRoadSignsAndObjects(roadmanager::OpenDrive* od);
  313. bool keyUp_;
  314. bool keyDown_;
  315. bool keyLeft_;
  316. bool keyRight_;
  317. bool quit_request_;
  318. };
  319. class ViewerEventHandler : public osgGA::GUIEventHandler
  320. {
  321. public:
  322. ViewerEventHandler(Viewer *viewer) : viewer_(viewer) {}
  323. bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&);
  324. private:
  325. Viewer* viewer_;
  326. };
  327. }
  328. #endif // VIEWER_HPP_