iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-graphics.C
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #include <nrt/Core/Model/Manager.H>
36 #include <nrt/Graphics/Shapes.H>
37 #include <nrt/Graphics/Camera.H>
41 
42 //! Simple test that shows a bunch of graphics shapes animated in a 3D display
43 int main(int argc, char const * argv[])
44 {
45  nrt::Manager mgr(argc, argv);
46 
47  try
48  {
49  auto renderer = mgr.addComponent<nrt::graphics::ShapeRendererBasic>("Renderer");
50 
51  // get started:
52  mgr.launch();
53 
54  // Create a drawing context:
55  renderer->createContext();
56 
57  // Set our camera:
58  renderer->lookAt(Eigen::Vector3d(-20, 0, 20), Eigen::Vector3d(0, 0, 0), Eigen::Vector3d::UnitZ());
59 
60  // Create some (static) shapes:
61  nrt::graphics::Grid grid(10, 10, Eigen::Affine3f(Eigen::UniformScaling<float>(10.0F)));
62 
63  nrt::graphics::Axes axes(Eigen::Affine3f(Eigen::UniformScaling<float>(1.0F)));
64 
65  nrt::graphics::Sphere sphere(Eigen::Affine3f(Eigen::UniformScaling<float>(0.2F)),
66  nrt::forev(), nrt::PixRGB<nrt::byte>(255, 0, 0),
67  nrt::graphics::BasicMaterial::Gold);
68 
69  Eigen::Affine3f ctrans;
70  ctrans.fromPositionOrientationScale(Eigen::Vector3f(1.0, -1.0, 0.1),
71  Eigen::AngleAxisf(1, Eigen::Vector3f::UnitZ()),
72  Eigen::Vector3f(.3, .1, .2));
73  nrt::graphics::Cylinder cyl(nrt::graphics::Capped::Yes, ctrans,
74  nrt::forev(), nrt::PixRGB<nrt::byte>(255, 0, 0),
75  nrt::graphics::BasicMaterial::Pearl);
76 
77  std::default_random_engine g((unsigned int)time(0));
78  std::uniform_real_distribution<float> loc(-1, 1);
79  std::uniform_int_distribution<nrt::byte> col(0, 255);
80  nrt::PointCloud2 rgbCloud = nrt::PointCloud2::create<nrt::PixRGB<nrt::byte>>(2000);
81  for(auto p : rgbCloud.range<nrt::PixRGB<nrt::byte>>())
82  {
83  p.geometry() = {loc(g), loc(g), loc(g)+5};
84  p.get<nrt::PixRGB<nrt::byte>>() = nrt::PixRGB<nrt::byte>(col(g), col(g), col(g));
85  }
86  nrt::graphics::PointCloud rgbCloudGraphics(rgbCloud, 0.01);
88 
89 
90 
91  // Build a texture:
93  nrt::drawLine(tex, nrt::Line<nrt::int32>(0, 0, 127, 127), nrt::PixRGB<nrt::byte>(255, 0, 0), 5);
94  nrt::drawLine(tex, nrt::Line<nrt::int32>(0, 127, 127, 0), nrt::PixRGB<nrt::byte>(255, 0, 0), 5);
95  nrt::drawCircle(tex, nrt::Circle<nrt::int32>(63, 63, 30), nrt::PixRGB<nrt::byte>(0, 255, 0), 5);
96 
97  // Load the texture into our renderer: To achieve that, we build a Textures shape:
98  std::vector<nrt::Image<nrt::PixRGB<nrt::byte> > > texvec;
99  texvec.push_back(tex);
100  nrt::graphics::Textures texshape("mytexture", texvec);
101 
102  // note: we need an active OpenGL context:
103  renderer->activateContext();
104  //texshape.render(*renderer); // this is a one-time deal, do not put in main loop
105 
106  // Create a box with a brush that uses our texture:
107  ctrans.translate(Eigen::Vector3f(-3.0F, 7.0F, 0.0F));
108  nrt::graphics::Box box(ctrans, nrt::forev(), nrt::PixRGB<nrt::byte>(255, 0, 0), nrt::graphics::Brush("mytexture"));
109 
110  // Let's do this again but for a box with 6 textures (one per face):
111  std::vector<nrt::Image<nrt::PixRGB<nrt::byte> > > texvec2;
112  nrt::drawText(tex, nrt::Point2D<nrt::int32>(10, 10), "[1]"); texvec2.push_back(tex);
113  nrt::drawText(tex, nrt::Point2D<nrt::int32>(10, 10), "[2]"); texvec2.push_back(tex);
114  nrt::drawText(tex, nrt::Point2D<nrt::int32>(10, 10), "[3]"); texvec2.push_back(tex);
115  nrt::drawText(tex, nrt::Point2D<nrt::int32>(10, 10), "[4]"); texvec2.push_back(tex);
116  nrt::drawText(tex, nrt::Point2D<nrt::int32>(10, 10), "[5]"); texvec2.push_back(tex);
117  nrt::drawText(tex, nrt::Point2D<nrt::int32>(10, 10), "[6]"); texvec2.push_back(tex);
118  nrt::graphics::Textures texshape2("mytexture2", texvec2);
119  //texshape2.render(*renderer);
120  ctrans.translate(Eigen::Vector3f(5.0F, 7.0F, 0.0F));
121  nrt::graphics::Box box2(ctrans, nrt::forev(), nrt::PixRGB<nrt::byte>(255,0,0), nrt::graphics::BasicMaterial::Pearl);//, nrt::graphics::Brush("mytexture2"));
122 
123  nrt::graphics::Text3D text3D("Hello, World",
124  Eigen::Affine3f(Eigen::AngleAxisf(M_PI/2, Eigen::Vector3f::UnitX()) * Eigen::UniformScaling<float>(0.01) * Eigen::Translation3f(0,0,1)),
125  nrt::forev(), nrt::PixRGB<nrt::byte>(255, 0, 0));
126 
127  nrt::graphics::Text2D text2D("Goodbye, cruel world", nrt::Point2D<nrt::int32>(10, 10));
128 
129  // Finally, a plain box:
130  ctrans.translate(Eigen::Vector3f(-3.0F, -9.0F, 0.0F));
131  nrt::graphics::Box box3(ctrans, nrt::forev(), nrt::PixRGB<nrt::byte>(255,0,0), nrt::graphics::BasicMaterial::Ruby);
132 
133  // Create some more (dynamic) shapes, and display all shapes:
134  double angle = 0.0; nrt::Timer timer;
135  while (true)
136  {
137  // *** Init our display frame; note: this loads the camera:
138  renderer->initFrame();
139 
140  // *** Render all our static objects:
141  grid.render(*renderer);
142  axes.render(*renderer);
143  sphere.render(*renderer);
144  cyl.render(*renderer);
145  //box.render(*renderer);
146  box2.render(*renderer);
147  box3.render(*renderer);
148  text3D.render(*renderer);
149  text2D.render(*renderer);
150  rgbCloudGraphics.render(*renderer);
151 
152  // *** Create a spinning line:
153 
154  // Start with an identity transform:
155  Eigen::Affine3f trans;
156  trans.fromPositionOrientationScale(Eigen::Vector3f(5.0, 0, 0),
157  Eigen::AngleAxisf(angle, Eigen::Vector3f::UnitZ()),
158  Eigen::Vector3f(1.5, 1.5, 1.5));
159  nrt::graphics::Line line(trans, std::chrono::hours(1), nrt::graphics::Pen(nrt::PixRGBA<nrt::byte>(255,0,0,160)));
160 
161  // Render the line:
162  line.render(*renderer);
163 
164  // *** Render the scene into the window:
165  renderer->renderFrame();
166 
167  // *** Update our dynamic objects' angle for the next iteration:
168  angle += 0.01;
169  ///NRT_INFO("FPS = " << 1.0 / timer.getreset().count());
170 
171  std::this_thread::sleep_for(std::chrono::milliseconds(10));
172  }
173 
174  // done
175  renderer->destroyContext();
176  mgr.stop();
177  }
178  catch (...) { nrt::warnAndRethrowException(); return 1; }
179 
180  return 0;
181 }
182