render_points_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
31 
32 #include <QMouseEvent>
33 #include <QWheelEvent>
34 #include <QApplication>
35 #include <QVBoxLayout>
36 
37 #include <OgreCamera.h>
38 #include <OgreSceneNode.h>
39 
40 using namespace rviz;
41 
42 MyFrame::MyFrame(QWidget* parent)
43  : QWidget(parent)
44  , left_mouse_down_(false)
45  , middle_mouse_down_(false)
46  , right_mouse_down_(false)
47  , mouse_x_(0)
48  , mouse_y_(0)
49 {
50  render_system_ = RenderSystem::get();
52 
53  try
54  {
55 #if (OGRE_VERSION < OGRE_VERSION_CHECK(13, 0, 0))
56  scene_manager_ = root_->createSceneManager(Ogre::ST_GENERIC, "TestSceneManager");
57 #else
58  scene_manager_ = root_->createSceneManager(Ogre::DefaultSceneManagerFactory::FACTORY_TYPE_NAME,
59  "TestSceneManager");
60 #endif
61 
63  render_panel_->resize(this->size());
65 
66  QVBoxLayout* layout = new QVBoxLayout;
67  layout->setContentsMargins(0, 0, 0, 0);
68  layout->addWidget(render_panel_);
69  setLayout(layout);
70 
72  camera_->setPosition(0, 0, 15);
73  camera_->getOgreCamera()->setNearClipDistance(0.1);
74 
76  render_panel_->setBackgroundColor(Ogre::ColourValue(0.8, 0.8, 1));
77 
78  Ogre::Light* directional_light = scene_manager_->createLight("MainDirectional");
79  directional_light->setType(Ogre::Light::LT_DIRECTIONAL);
80  directional_light->setDirection(Ogre::Vector3(0, -1, 1));
81  directional_light->setDiffuseColour(Ogre::ColourValue(1.0f, 1.0f, 1.0f));
82 
83 #if 0
84  Grid* grid = new Grid( scene_manager_, NULL, Grid::Lines, 10, 1.0f, 0.02, Ogre::ColourValue(1.0f, 1.0f, 1.0f, 0.5f));
85  grid->setHeight(4);
86 
87 
89  line->setMaxPointsPerLine(105);
90  for ( int i = -50; i < 50; ++i )
91  {
92  line->addPoint( Ogre::Vector3( i*2, 0.0f, -1.0f ) );
93  }
94 
95  for ( int i = 0; i < 5; ++i )
96  {
97  line->addPoint( Ogre::Vector3( 4.0f, 0.0f, i ) );
98  }
99 
100  line->setLineWidth( 0.05 );
101  line->setColor( 0.0f, 1.0f, 0.0f, 0.5f );
102 
103  Shape* sphere = new Shape(Shape::Sphere, scene_manager_);
104  sphere->setPosition(Ogre::Vector3(0.0f, 0.0f, 2.0f));
105  sphere->setColor(0.0f, 1.0f, 2.0f, 1.0f);
106  Shape* cube = new Shape(Shape::Cube, scene_manager_);
107  cube->setPosition(Ogre::Vector3(0.0f, 1.0f, 2.0f));
108  cube->setColor(1.0f, 0.0f, 0.0f, 1.0f);
109  Shape* cylinder = new Shape(Shape::Cylinder, scene_manager_);
110  cylinder->setPosition(Ogre::Vector3(0.0f, 2.0f, 2.0f));
111  cylinder->setColor(1.0f, 1.0f, 0.0f, 1.0f);
112  Shape* cone = new Shape(Shape::Cone, scene_manager_);
113  cone->setPosition(Ogre::Vector3(0.0f, 3.0f, 2.0f));
114  cone->setColor(0.0f, 0.0f, 1.0f, 1.0f);
115 
116  Axes* axes = new Axes( scene_manager_ );
117  //axes->setScale( Ogre::Vector3( 2.0f, 2.0f, 2.0f ) );
118 
119  /*Cone* cone = new Cone( scene_manager_, NULL );
120  cone->setScale( Ogre::Vector3( 0.3f, 2.0f, 0.3f ) );*/
121 
122  Arrow* arrow = new Arrow( scene_manager_ );
123  arrow->setHeadColor( 1.0f, 0.0f, 0.0f );
124  arrow->setShaftColor( 0.0f, 0.0f, 1.0f );
125  arrow->setOrientation( Ogre::Quaternion::IDENTITY );
126  //arrow->setOrientation( Ogre::Quaternion( Ogre::Degree( 45 ), Ogre::Vector3::UNIT_X ) );
127  //arrow->setScale( Ogre::Vector3( 1.0f, 1.0f, 3.0f ) );
128 #endif
129 
130 #if 01
131  Ogre::SceneNode* scene_node = scene_manager_->getRootSceneNode()->createChildSceneNode();
132  PointCloud* pointCloud = new PointCloud();
133  pointCloud->setDimensions(0.05f, 0.05f, 0.05f);
134  // pointCloud->setColorByIndex(true);
135  pointCloud->setRenderMode(PointCloud::RM_SQUARES);
136  pointCloud->setCommonDirection(Ogre::Vector3(0.0, 1.0, 0.0));
137  pointCloud->setCommonUpVector(Ogre::Vector3(0.0, 0.0, -1.0));
138  pointCloud->setAlpha(1.0);
139  std::vector<PointCloud::Point> points;
140  int32_t xcount = 200;
141  int32_t ycount = 100;
142  int32_t zcount = 100;
143  // points.resize(xcount * ycount * zcount);
144  float factor = 0.1f;
145  for (int32_t x = 0; x < xcount; ++x)
146  {
147  for (int32_t y = 0; y < ycount; ++y)
148  {
149  for (int32_t z = 0; z < zcount; ++z)
150  {
151  // int32_t index = (ycount*zcount*x) + zcount*y + z;
152  PointCloud::Point point; // = points[index];
153  point.position.x = x * factor;
154  point.position.y = y * factor;
155  point.position.z = z * factor;
156 
157  point.setColor(x * 0.1, y * 0.1, z * 0.1);
158  points.push_back(point);
159  }
160  }
161  }
162 
163  printf("size: %d\n", (int)points.size());
164  pointCloud->addPoints(&points.front(), points.size());
165  scene_node->attachObject(pointCloud);
166 #endif
167  }
168  catch (Ogre::Exception& e)
169  {
170  printf("Fatal error: %s\n", e.what());
171  exit(1);
172  }
173 
174  connect(&render_timer_, SIGNAL(timeout()), this, SLOT(doRender()));
175  render_timer_.start(33);
176 }
177 
179 {
180 }
181 
183 {
185  root_->renderOneFrame();
187  printf("Render took [%f] msec\n", (end - start).toSec() * 1000.0f);
188 }
189 
190 void MyFrame::mousePressEvent(QMouseEvent* event)
191 {
192  left_mouse_down_ = false;
193  middle_mouse_down_ = false;
194  right_mouse_down_ = false;
195 
196  switch (event->button())
197  {
198  case Qt::LeftButton:
199  left_mouse_down_ = true;
200  break;
201  case Qt::MiddleButton:
202  middle_mouse_down_ = true;
203  break;
204  case Qt::RightButton:
205  right_mouse_down_ = true;
206  break;
207  default:
208  break;
209  }
210 }
211 
212 void MyFrame::mouseReleaseEvent(QMouseEvent* event)
213 {
214  switch (event->button())
215  {
216  case Qt::LeftButton:
217  left_mouse_down_ = false;
218  break;
219  case Qt::MiddleButton:
220  middle_mouse_down_ = false;
221  break;
222  case Qt::RightButton:
223  right_mouse_down_ = false;
224  break;
225  default:
226  break;
227  }
228 }
229 
230 void MyFrame::mouseMoveEvent(QMouseEvent* event)
231 {
232  int32_t diff_x = event->x() - mouse_x_;
233  int32_t diff_y = event->y() - mouse_y_;
234 
235  mouse_x_ = event->x();
236  mouse_y_ = event->y();
237 
238  bool cmd = event->modifiers() & Qt::ControlModifier;
239  bool shift = event->modifiers() & Qt::ShiftModifier;
240  bool alt = event->modifiers() & Qt::AltModifier;
241 
242  if (left_mouse_down_)
243  {
244  camera_->mouseLeftDrag(diff_x, diff_y, cmd, alt, shift);
245  }
246  else if (middle_mouse_down_)
247  {
248  camera_->mouseMiddleDrag(diff_x, diff_y, cmd, alt, shift);
249  }
250  else if (right_mouse_down_)
251  {
252  camera_->mouseRightDrag(diff_x, diff_y, cmd, alt, shift);
253  }
254 }
255 
256 void MyFrame::wheelEvent(QWheelEvent* event)
257 {
258  if (event->angleDelta().y() != 0)
259  {
260  bool cmd = event->modifiers() & Qt::ControlModifier;
261  bool shift = event->modifiers() & Qt::ShiftModifier;
262  bool alt = event->modifiers() & Qt::AltModifier;
263 
264  camera_->scrollWheel(event->angleDelta().y(), cmd, alt, shift);
265  }
266 }
267 
268 int main(int argc, char** argv)
269 {
270  QApplication app(argc, argv);
271 
272  MyFrame frame;
273  frame.resize(800, 600);
274  frame.setWindowTitle("I hope this is not all black.");
275  frame.show();
276 
277  return app.exec();
278 }
rviz::PointCloud
A visual representation of a set of points.
Definition: point_cloud.h:108
rviz::OrbitCamera
An orbital camera, controlled by yaw, pitch, distance, and focal point.
Definition: orbit_camera.h:61
rviz::BillboardLine
An object that displays a multi-segment line strip rendered as billboards.
Definition: billboard_line.h:58
MyFrame::doRender
void doRender()
Definition: render_points_test.cpp:182
rviz::CameraBase::getOgreCamera
Ogre::Camera * getOgreCamera()
Get the Ogre camera associated with this camera object.
Definition: camera_base.h:67
rviz::BillboardLine::setLineWidth
void setLineWidth(float width)
Definition: billboard_line.cpp:237
rviz::CameraBase::setPosition
virtual void setPosition(const Ogre::Vector3 &position)
Set the position of the camera.
Definition: camera_base.cpp:66
main
int main(int argc, char **argv)
Definition: render_points_test.cpp:268
render_points_test.h
NULL
#define NULL
Definition: global.h:37
rviz::PointCloud::setAlpha
void setAlpha(float alpha, bool per_point_alpha=false)
Definition: point_cloud.cpp:368
rviz::Shape::setColor
void setColor(float r, float g, float b, float a) override
Set the color of the object. Values are in the range [0, 1].
Definition: shape.cpp:136
rviz::Arrow
An arrow consisting of a cylinder and a cone.
Definition: arrow.h:57
MyFrame::render_timer_
QTimer render_timer_
Definition: render_points_test.h:90
rviz::PointCloud::addPoints
void addPoints(Point *points, uint32_t num_points)
Add points to this point cloud.
Definition: point_cloud.cpp:400
rviz::Arrow::setOrientation
void setOrientation(const Ogre::Quaternion &orientation) override
Set the orientation.
Definition: arrow.cpp:119
MyFrame::render_system_
RenderSystem * render_system_
Definition: render_points_test.h:75
MyFrame::render_panel_
QtOgreRenderWindow * render_panel_
Definition: render_points_test.h:78
MyFrame::root_
Ogre::Root * root_
Definition: render_points_test.h:74
MyFrame::camera_
CameraBase * camera_
Definition: render_points_test.h:81
rviz::CameraBase::mouseLeftDrag
virtual void mouseLeftDrag(int diff_x, int diff_y, bool ctrl, bool alt, bool shift)=0
Handle a left mouse button drag.
rviz::QtOgreRenderWindow
Definition: qt_ogre_render_window.h:54
MyFrame::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *event) override
Definition: render_points_test.cpp:212
MyFrame::mouse_y_
int mouse_y_
Definition: render_points_test.h:88
rviz::CameraBase::mouseRightDrag
virtual void mouseRightDrag(int diff_x, int diff_y, bool ctrl, bool alt, bool shift)=0
Handle a right mouse button drag.
rviz::BillboardLine::setMaxPointsPerLine
void setMaxPointsPerLine(uint32_t max)
Definition: billboard_line.cpp:169
rviz::CameraBase::mouseMiddleDrag
virtual void mouseMiddleDrag(int diff_x, int diff_y, bool ctrl, bool alt, bool shift)=0
Handle a middle mouse button drag.
f
f
rviz::PointCloud::Point
Representation of a point, with x/y/z position and r/g/b color.
Definition: point_cloud.h:133
rviz::Arrow::setHeadColor
void setHeadColor(float r, float g, float b, float a=1.0f)
Set the color of the arrow's head. Values are in the range [0, 1].
Definition: arrow.cpp:109
MyFrame::MyFrame
MyFrame(QWidget *parent=nullptr)
Definition: render_points_test.cpp:42
rviz::Grid::setHeight
void setHeight(uint32_t count)
Definition: grid.cpp:140
MyFrame::left_mouse_down_
bool left_mouse_down_
Definition: render_points_test.h:84
rviz
Definition: add_display_dialog.cpp:54
ros::WallTime::now
static WallTime now()
MyFrame::middle_mouse_down_
bool middle_mouse_down_
Definition: render_points_test.h:85
MyFrame::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *event) override
Definition: render_points_test.cpp:230
MyFrame::mousePressEvent
void mousePressEvent(QMouseEvent *event) override
Definition: render_points_test.cpp:190
rviz::PointCloud::Point::setColor
void setColor(float r, float g, float b, float a=1.0)
Definition: point_cloud.h:135
rviz::QtOgreRenderWindow::setBackgroundColor
void setBackgroundColor(Ogre::ColourValue color)
Definition: qt_ogre_render_window.cpp:251
ros::WallTime
app
app
rviz::PointCloud::setDimensions
void setDimensions(float width, float height, float depth)
Set the dimensions of the billboards used to render each point.
Definition: point_cloud.cpp:300
rviz::Axes
An object that displays a set of X/Y/Z axes, with X=Red, Y=Green, Z=Blue.
Definition: axes.h:57
rviz::Grid
Displays a grid of cells, drawn with lines.
Definition: grid.h:62
MyFrame::right_mouse_down_
bool right_mouse_down_
Definition: render_points_test.h:86
rviz::PointCloud::setRenderMode
void setRenderMode(RenderMode mode)
Set what type of rendering primitives should be used, currently points, billboards and boxes are supp...
Definition: point_cloud.cpp:218
rviz::RenderSystem::root
Ogre::Root * root()
Definition: render_system.h:61
start
ROSCPP_DECL void start()
rviz::PointCloud::Point::position
Ogre::Vector3 position
Definition: point_cloud.h:140
rviz::Arrow::setShaftColor
void setShaftColor(float r, float g, float b, float a=1.0f)
Set the color of the arrow's shaft. Values are in the range [0, 1].
Definition: arrow.cpp:104
rviz::Shape
Definition: shape.h:51
MyFrame
Definition: render_points_test.h:57
rviz::QtOgreRenderWindow::setCamera
void setCamera(Ogre::Camera *camera)
Definition: qt_ogre_render_window.cpp:219
MyFrame::wheelEvent
void wheelEvent(QWheelEvent *event) override
Definition: render_points_test.cpp:256
MyFrame::scene_manager_
Ogre::SceneManager * scene_manager_
Definition: render_points_test.h:76
rviz::QtOgreRenderWindow::setAutoRender
void setAutoRender(bool auto_render)
Definition: qt_ogre_render_window.h:117
MyFrame::mouse_x_
int mouse_x_
Definition: render_points_test.h:87
rviz::CameraBase::scrollWheel
virtual void scrollWheel(int diff, bool ctrl, bool alt, bool shift)=0
Handle a scrollwheel change.
cmd
string cmd
rviz::Shape::setPosition
void setPosition(const Ogre::Vector3 &position) override
Set the position of this object.
Definition: shape.cpp:146
rviz::PointCloud::setCommonDirection
void setCommonDirection(const Ogre::Vector3 &vec)
See Ogre::BillboardSet::setCommonDirection.
Definition: point_cloud.cpp:326
rviz::BillboardLine::setColor
void setColor(float r, float g, float b, float a) override
Set the color of the object. Values are in the range [0, 1].
Definition: billboard_line.cpp:271
MyFrame::~MyFrame
~MyFrame() override
Definition: render_points_test.cpp:178
rviz::PointCloud::setCommonUpVector
void setCommonUpVector(const Ogre::Vector3 &vec)
See Ogre::BillboardSet::setCommonUpVector.
Definition: point_cloud.cpp:338
rviz::BillboardLine::addPoint
void addPoint(const Ogre::Vector3 &point)
Definition: billboard_line.cpp:208


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Dec 13 2024 03:31:02