snow_display.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, David Lu!!
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 the copyright holder 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 HOLDER 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 
36 #include <OgreSceneNode.h>
37 
39 {
40 static unsigned int random_seed = std::time(NULL);
41 inline double randScale()
42 {
43  return static_cast<double>(rand_r(&random_seed)) / RAND_MAX;
44 }
45 
46 SnowDisplay::SnowDisplay() : point_cloud_(nullptr)
47 {
48  height_property_ = new rviz::FloatProperty("Height", 10.0, "Maximum Height", this, SLOT(updatePosition()));
50 
51  width_property_ = new rviz::FloatProperty("Width", 10.0, "Total XY Dimension", this, SLOT(updatePosition()));
52  width_property_->setMin(0.0);
53 
54  gravity_property_ = new rviz::FloatProperty("Gravity", 0.05, "Z motion per time step", this, SLOT(updatePosition()));
55  wind_property_ = new rviz::FloatProperty("Wind", 0.02, "X motion per time step", this, SLOT(updatePosition()));
56  jiggle_property_ = new rviz::FloatProperty("Jiggle", 0.03, "Magnitude of Jiggle", this, SLOT(updatePosition()));
57 
58  size_property_ = new rviz::IntProperty("Snowflakes", 1000, "Number of snowflakes", this, SLOT(updateSize()));
60 }
61 
63 {
64  Display::onInitialize();
65  if (!point_cloud_)
66  {
68  scene_node_->attachObject(point_cloud_);
69  point_cloud_->setAlpha(1.0);
70  }
71 
72  updateSize();
73 }
74 
75 void SnowDisplay::update(float wall_dt, float ros_dt)
76 {
78 }
79 
80 void SnowDisplay::initializeXY(geometry_msgs::Point& pt) const
81 {
82  pt.x = (randScale() - 0.5) * width_;
83  pt.y = (randScale() - 0.5) * width_;
84 }
85 
87 {
88  unsigned int size = static_cast<unsigned int>(size_property_->getInt());
91 
92  points_.resize(size);
93  flakes_.resize(size);
94 
95  for (geometry_msgs::Point& point : points_)
96  {
97  initializeXY(point);
98  point.z = randScale() * height_;
99  }
100  updatePosition();
101 }
102 
104 {
105  double gravity = gravity_property_->getFloat();
106  double wind = wind_property_->getFloat();
107  double jiggle = jiggle_property_->getFloat();
108 
109  for (geometry_msgs::Point& point : points_)
110  {
111  point.x += wind;
112 
113  point.x += (randScale() - 0.5) * jiggle;
114  point.y += (randScale() - 0.5) * jiggle;
115 
116  if (point.x >= width_ / 2)
117  {
118  point.x -= width_;
119  }
120  else if (point.x <= -width_ / 2)
121  {
122  point.x += width_;
123  }
124 
125  point.z -= gravity;
126  if (point.z <= 0.0)
127  {
128  initializeXY(point);
129  point.z = height_;
130  }
131  else if (point.z >= height_)
132  {
133  point.z = 0.0;
134  }
135  }
136  letItSnow();
137 }
138 
140 {
141  if (!point_cloud_)
142  {
143  return;
144  }
145 
146  point_cloud_->clear();
147  for (unsigned int i = 0; i < flakes_.size(); ++i)
148  {
149  flakes_[i].position.x = points_[i].x;
150  flakes_[i].position.y = points_[i].y;
151  flakes_[i].position.z = points_[i].z;
152  flakes_[i].setColor(1.0, 1.0, 1.0, 1.0);
153  }
154  point_cloud_->addPoints(&flakes_.front(), flakes_.size());
155 }
156 } // namespace snowbot_operating_system
157 
rviz::IntProperty::setMin
void setMin(int min)
snowbot_operating_system::SnowDisplay::height_
double height_
Definition: snow_display.h:79
rviz::PointCloud::setAlpha
void setAlpha(float alpha, bool per_point_alpha=false)
snowbot_operating_system::SnowDisplay::updateSize
void updateSize()
Definition: snow_display.cpp:86
snowbot_operating_system::SnowDisplay
Definition: snow_display.h:47
rviz::PointCloud::addPoints
void addPoints(Point *points, uint32_t num_points)
snowbot_operating_system::SnowDisplay::height_property_
rviz::FloatProperty * height_property_
Definition: snow_display.h:73
snowbot_operating_system::random_seed
static unsigned int random_seed
Definition: snow_display.cpp:40
snowbot_operating_system::SnowDisplay::gravity_property_
rviz::FloatProperty * gravity_property_
Definition: snow_display.h:74
rviz::FloatProperty::setMin
void setMin(float min)
rviz::Display
snowbot_operating_system::SnowDisplay::jiggle_property_
rviz::FloatProperty * jiggle_property_
Definition: snow_display.h:76
rviz::PointCloud
rviz::FloatProperty
snowbot_operating_system::SnowDisplay::points_
std::vector< geometry_msgs::Point > points_
Definition: snow_display.h:70
snowbot_operating_system::SnowDisplay::point_cloud_
rviz::PointCloud * point_cloud_
Definition: snow_display.h:67
snowbot_operating_system::SnowDisplay::flakes_
std::vector< rviz::PointCloud::Point > flakes_
Definition: snow_display.h:68
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
rviz::FloatProperty::getFloat
virtual float getFloat() const
rviz::Display::scene_node_
Ogre::SceneNode * scene_node_
snowbot_operating_system::SnowDisplay::size_property_
rviz::IntProperty * size_property_
Definition: snow_display.h:77
snowbot_operating_system::SnowDisplay::width_
double width_
Definition: snow_display.h:79
snowbot_operating_system::SnowDisplay::initializeXY
void initializeXY(geometry_msgs::Point &pt) const
Definition: snow_display.cpp:80
snowbot_operating_system::SnowDisplay::onInitialize
void onInitialize() override
Definition: snow_display.cpp:62
snowbot_operating_system::SnowDisplay::letItSnow
void letItSnow()
Definition: snow_display.cpp:139
snowbot_operating_system::SnowDisplay::wind_property_
rviz::FloatProperty * wind_property_
Definition: snow_display.h:75
snowbot_operating_system::randScale
double randScale()
Definition: snow_display.cpp:41
rviz::PointCloud::clear
void clear()
snowbot_operating_system
Definition: snow_display.h:45
class_list_macros.hpp
snowbot_operating_system::SnowDisplay::SnowDisplay
SnowDisplay()
Definition: snow_display.cpp:46
rviz::IntProperty::getInt
virtual int getInt() const
snow_display.h
snowbot_operating_system::SnowDisplay::update
void update(float wall_dt, float ros_dt) override
Definition: snow_display.cpp:75
snowbot_operating_system::SnowDisplay::updatePosition
void updatePosition()
Definition: snow_display.cpp:103
snowbot_operating_system::SnowDisplay::width_property_
rviz::FloatProperty * width_property_
Definition: snow_display.h:72
rviz::IntProperty


snowbot_operating_system
Author(s):
autogenerated on Wed Mar 2 2022 01:03:22