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  scene_manager_ = root_->createSceneManager( Ogre::ST_GENERIC, "TestSceneManager" );
56 
58  render_panel_->resize( this->size() );
60 
61  QVBoxLayout* layout = new QVBoxLayout;
62  layout->setContentsMargins( 0, 0, 0, 0 );
63  layout->addWidget( render_panel_ );
64  setLayout( layout );
65 
67  camera_->setPosition( 0, 0, 15 );
68  camera_->getOgreCamera()->setNearClipDistance( 0.1 );
69 
71  render_panel_->setBackgroundColor( Ogre::ColourValue( 0.8,0.8,1 ) );
72 
73  Ogre::Light* directional_light = scene_manager_->createLight( "MainDirectional" );
74  directional_light->setType( Ogre::Light::LT_DIRECTIONAL );
75  directional_light->setDirection( Ogre::Vector3( 0, -1, 1 ) );
76  directional_light->setDiffuseColour( Ogre::ColourValue( 1.0f, 1.0f, 1.0f ) );
77 
78 #if 0
79  Grid* grid = new Grid( scene_manager_, NULL, Grid::Lines, 10, 1.0f, 0.02, Ogre::ColourValue(1.0f, 1.0f, 1.0f, 0.5f));
80  grid->setHeight(4);
81 
82 
84  line->setMaxPointsPerLine(105);
85  for ( int i = -50; i < 50; ++i )
86  {
87  line->addPoint( Ogre::Vector3( i*2, 0.0f, -1.0f ) );
88  }
89 
90  for ( int i = 0; i < 5; ++i )
91  {
92  line->addPoint( Ogre::Vector3( 4.0f, 0.0f, i ) );
93  }
94 
95  line->setLineWidth( 0.05 );
96  line->setColor( 0.0f, 1.0f, 0.0f, 0.5f );
97 
98  Shape* sphere = new Shape(Shape::Sphere, scene_manager_);
99  sphere->setPosition(Ogre::Vector3(0.0f, 0.0f, 2.0f));
100  sphere->setColor(0.0f, 1.0f, 2.0f, 1.0f);
101  Shape* cube = new Shape(Shape::Cube, scene_manager_);
102  cube->setPosition(Ogre::Vector3(0.0f, 1.0f, 2.0f));
103  cube->setColor(1.0f, 0.0f, 0.0f, 1.0f);
104  Shape* cylinder = new Shape(Shape::Cylinder, scene_manager_);
105  cylinder->setPosition(Ogre::Vector3(0.0f, 2.0f, 2.0f));
106  cylinder->setColor(1.0f, 1.0f, 0.0f, 1.0f);
107  Shape* cone = new Shape(Shape::Cone, scene_manager_);
108  cone->setPosition(Ogre::Vector3(0.0f, 3.0f, 2.0f));
109  cone->setColor(0.0f, 0.0f, 1.0f, 1.0f);
110 
111  Axes* axes = new Axes( scene_manager_ );
112  //axes->setScale( Ogre::Vector3( 2.0f, 2.0f, 2.0f ) );
113 
114  /*Cone* cone = new Cone( scene_manager_, NULL );
115  cone->setScale( Ogre::Vector3( 0.3f, 2.0f, 0.3f ) );*/
116 
117  Arrow* arrow = new Arrow( scene_manager_ );
118  arrow->setHeadColor( 1.0f, 0.0f, 0.0f );
119  arrow->setShaftColor( 0.0f, 0.0f, 1.0f );
120  arrow->setOrientation( Ogre::Quaternion::IDENTITY );
121  //arrow->setOrientation( Ogre::Quaternion( Ogre::Degree( 45 ), Ogre::Vector3::UNIT_X ) );
122  //arrow->setScale( Ogre::Vector3( 1.0f, 1.0f, 3.0f ) );
123 #endif
124 
125 #if 01
126  Ogre::SceneNode* scene_node = scene_manager_->getRootSceneNode()->createChildSceneNode();
127  PointCloud* pointCloud = new PointCloud();
128  pointCloud->setDimensions(0.05f, 0.05f, 0.05f);
129  //pointCloud->setColorByIndex(true);
130  pointCloud->setRenderMode(PointCloud::RM_SQUARES);
131  pointCloud->setCommonDirection(Ogre::Vector3(0.0, 1.0, 0.0));
132  pointCloud->setCommonUpVector(Ogre::Vector3(0.0, 0.0, -1.0));
133  pointCloud->setAlpha(1.0);
134  std::vector<PointCloud::Point> points;
135  int32_t xcount = 200;
136  int32_t ycount = 100;
137  int32_t zcount = 100;
138  // points.resize(xcount * ycount * zcount);
139  float factor = 0.1f;
140  for (int32_t x = 0; x < xcount; ++x)
141  {
142  for (int32_t y = 0; y < ycount; ++y)
143  {
144  for (int32_t z = 0; z < zcount; ++z)
145  {
146  // int32_t index = (ycount*zcount*x) + zcount*y + z;
147  PointCloud::Point point;// = points[index];
148  point.position.x = x * factor;
149  point.position.y = y * factor;
150  point.position.z = z * factor;
151 
152  point.setColor(x * 0.1, y * 0.1, z * 0.1);
153  points.push_back(point);
154  }
155  }
156  }
157 
158  printf("size: %d\n", (int)points.size());
159  pointCloud->addPoints( &points.front(), points.size() );
160  scene_node->attachObject(pointCloud);
161 #endif
162  }
163  catch ( Ogre::Exception& e )
164  {
165  printf( "Fatal error: %s\n", e.what() );
166  exit(1);
167  }
168 
169  connect( &render_timer_, SIGNAL( timeout() ), this, SLOT( doRender() ));
170  render_timer_.start(33);
171 }
172 
174 {
175 }
176 
178 {
180  root_->renderOneFrame();
182  printf("Render took [%f] msec\n", (end - start).toSec() * 1000.0f);
183 }
184 
185 void MyFrame::mousePressEvent( QMouseEvent* event )
186 {
187  left_mouse_down_ = false;
188  middle_mouse_down_ = false;
189  right_mouse_down_ = false;
190 
191  switch( event->button() )
192  {
193  case Qt::LeftButton:
194  left_mouse_down_ = true;
195  break;
196  case Qt::MidButton:
197  middle_mouse_down_ = true;
198  break;
199  case Qt::RightButton:
200  right_mouse_down_ = true;
201  break;
202  default:
203  break;
204  }
205 }
206 
207 void MyFrame::mouseReleaseEvent( QMouseEvent* event )
208 {
209  switch( event->button() )
210  {
211  case Qt::LeftButton:
212  left_mouse_down_ = false;
213  break;
214  case Qt::MidButton:
215  middle_mouse_down_ = false;
216  break;
217  case Qt::RightButton:
218  right_mouse_down_ = false;
219  break;
220  default:
221  break;
222  }
223 }
224 
225 void MyFrame::mouseMoveEvent( QMouseEvent* event )
226 {
227  int32_t diff_x = event->x() - mouse_x_;
228  int32_t diff_y = event->y() - mouse_y_;
229 
230  mouse_x_ = event->x();
231  mouse_y_ = event->y();
232 
233  bool cmd = event->modifiers() & Qt::ControlModifier;
234  bool shift = event->modifiers() & Qt::ShiftModifier;
235  bool alt = event->modifiers() & Qt::AltModifier;
236 
237  if ( left_mouse_down_ )
238  {
239  camera_->mouseLeftDrag( diff_x, diff_y, cmd, alt, shift );
240  }
241  else if ( middle_mouse_down_ )
242  {
243  camera_->mouseMiddleDrag( diff_x, diff_y, cmd, alt, shift );
244  }
245  else if ( right_mouse_down_ )
246  {
247  camera_->mouseRightDrag( diff_x, diff_y, cmd, alt, shift );
248  }
249 }
250 
251 void MyFrame::wheelEvent( QWheelEvent* event )
252 {
253  if( event->delta() != 0 )
254  {
255  bool cmd = event->modifiers() & Qt::ControlModifier;
256  bool shift = event->modifiers() & Qt::ShiftModifier;
257  bool alt = event->modifiers() & Qt::AltModifier;
258 
259  camera_->scrollWheel( event->delta(), cmd, alt, shift );
260  }
261 }
262 
263 int main( int argc, char** argv )
264 {
265  QApplication app( argc, argv );
266 
267  MyFrame frame;
268  frame.resize( 800, 600 );
269  frame.setWindowTitle( "I hope this is not all black." );
270  frame.show();
271 
272  return app.exec();
273 }
app
virtual void mouseLeftDrag(int diff_x, int diff_y, bool ctrl, bool alt, bool shift)=0
Handle a left mouse button drag.
void addPoints(Point *points, uint32_t num_points)
Add points to this point cloud.
#define NULL
Definition: global.h:37
QTimer render_timer_
CameraBase * camera_
Ogre::SceneManager * scene_manager_
void addPoint(const Ogre::Vector3 &point)
void setRenderMode(RenderMode mode)
Set what type of rendering primitives should be used, currently points, billboards and boxes are supp...
f
string cmd
An object that displays a multi-segment line strip rendered as billboards.
virtual void mouseMoveEvent(QMouseEvent *event)
Ogre::Camera * getOgreCamera()
Get the Ogre camera associated with this camera object.
Definition: camera_base.h:67
virtual void wheelEvent(QWheelEvent *event)
void setAutoRender(bool auto_render)
virtual void mousePressEvent(QMouseEvent *event)
RenderSystem * render_system_
Ogre::Root * root_
void setHeight(uint32_t count)
Definition: grid.cpp:135
TFSIMD_FORCE_INLINE const tfScalar & y() const
Ogre::Root * root()
Definition: render_system.h:62
virtual void setColor(float r, float g, float b, float a)
Set the color of the object. Values are in the range [0, 1].
Definition: shape.cpp:142
virtual void setColor(float r, float g, float b, float a)
Set the color of the object. Values are in the range [0, 1].
void setCommonUpVector(const Ogre::Vector3 &vec)
See Ogre::BillboardSet::setCommonUpVector.
int main(int argc, char **argv)
QtOgreRenderWindow * render_panel_
bool middle_mouse_down_
virtual void mouseMiddleDrag(int diff_x, int diff_y, bool ctrl, bool alt, bool shift)=0
Handle a middle mouse button drag.
virtual ~MyFrame()
virtual void mouseRightDrag(int diff_x, int diff_y, bool ctrl, bool alt, bool shift)=0
Handle a right mouse button drag.
virtual void setPosition(const Ogre::Vector3 &position)
Set the position of this object.
Definition: shape.cpp:152
Representation of a point, with x/y/z position and r/g/b color.
Definition: point_cloud.h:123
void setDimensions(float width, float height, float depth)
Set the dimensions of the billboards used to render each point.
Ogre::Vector3 position
Definition: point_cloud.h:130
virtual void setOrientation(const Ogre::Quaternion &orientation)
Set the orientation.
Definition: arrow.cpp:116
TFSIMD_FORCE_INLINE const tfScalar & x() const
void setAlpha(float alpha, bool per_point_alpha=false)
void setBackgroundColor(Ogre::ColourValue color)
void setShaftColor(float r, float g, float b, float a=1.0f)
Set the color of the arrow&#39;s shaft. Values are in the range [0, 1].
Definition: arrow.cpp:101
An object that displays a set of X/Y/Z axes, with X=Red, Y=Green, Z=Blue.
Definition: axes.h:58
virtual void scrollWheel(int diff, bool ctrl, bool alt, bool shift)=0
Handle a scrollwheel change.
void setMaxPointsPerLine(uint32_t max)
Displays a grid of cells, drawn with lines.
Definition: grid.h:62
MyFrame(QWidget *parent=0)
TFSIMD_FORCE_INLINE const tfScalar & z() const
bool left_mouse_down_
virtual void mouseReleaseEvent(QMouseEvent *event)
static WallTime now()
A visual representation of a set of points.
Definition: point_cloud.h:98
An arrow consisting of a cylinder and a cone.
Definition: arrow.h:59
void setCommonDirection(const Ogre::Vector3 &vec)
See Ogre::BillboardSet::setCommonDirection.
An orbital camera, controlled by yaw, pitch, distance, and focal point.
Definition: orbit_camera.h:62
virtual void setPosition(const Ogre::Vector3 &position)
Set the position of the camera.
Definition: camera_base.cpp:68
void setCamera(Ogre::Camera *camera)
void setHeadColor(float r, float g, float b, float a=1.0f)
Set the color of the arrow&#39;s head. Values are in the range [0, 1].
Definition: arrow.cpp:106
bool right_mouse_down_
void setColor(float r, float g, float b, float a=1.0)
Definition: point_cloud.h:125
void setLineWidth(float width)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51