iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-Geometry.C
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies
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 
36 #include <nrt/Core/Model/Manager.H>
38 #include <nrt/Core/Geometry/Line.H>
46 
47 #include <chrono>
48 
49 using namespace nrt;
50 
51 int main(int argc, const char** argv)
52 {
53  Image<PixRGB<byte>> tmpimg(10,10);
54 
55  Manager mgr(argc, argv);
56 
57  auto mySink = mgr.addComponent<ImageSink>("MySink");
58 
59  mgr.launch();
60 
61  // create a bunch of random points:
62  srand(time(NULL));
63  std::vector<Point2D<double> > randompoints;
64  for (int i = 0; i < 10; ++i)
65  randompoints.push_back(Point2D<double>((rand()/float(RAND_MAX)-0.5)*200.0+250,
66  (rand()/float(RAND_MAX)-0.5)*200.0+250));
67 
68  std::vector<Point2D<int> > randompoints2;
69  for (int i = 0; i < 200; ++i)
70  randompoints2.push_back(Point2D<int>(int((rand()/float(RAND_MAX)-0.5)*100.0+100),
71  int((rand()/float(RAND_MAX)-0.5)*100.0+100)));
72 
73  NRT_INFO("try: test-Geometry --out=display to see a visual output.");
74 
75  while(1)
76  {
77  auto start = std::chrono::steady_clock::now();
78  for (int i = 0; i < 360; ++i)
79  {
80  Image<PixRGB<byte> > img(Dims<int32>(500, 500), ImageInitPolicy::Zeros);
81 
82  // Rotate our points:
83  std::vector<Point2D<double> > rotatedPoints(randompoints.size());
84  std::transform(randompoints.begin(), randompoints.end(), rotatedPoints.begin(),
85  [i](Point2D<double> const & p)
86  { return nrt::rotateAbout(p, Point2D<double>(250,250), i*M_PI/180.0); });
87 
88  // Draw each of the rotated points, a semi-transparent small disk for each point:
89  for (Point2D<double> const & p : rotatedPoints)
90  drawDisk(img, nrt::Circle<int32>(Point2D<int>(p), 4), PixRGBA<byte>(0,255,255,128));
91 
92  // Find the convex hull around our rotated points and draw it:
93  Polygon<double> hull = Polygon<double>::convexHull(rotatedPoints);
94  drawPolygon(img, Polygon<int>(hull), PixRGBA<byte>(255,0,0,128), 6, PixRGBA<byte>(0,0,255,64));
95 
96  // Find the smallest rectangle that contains our rotated convex hull:
98 
99  // Get the vertices of our rotated rectangle, to draw them as a polygon:
100  Polygon<double> rectPoly(rotatedrect.vertices());
101  drawPolygon(img, Polygon<int>(rectPoly), PixRGB<byte>(0,255,0));
102 
103  // Draw a line of the same angle as our rotated rectangle:
104  Line<double> rectAngleLine(rectPoly.centroid<void>(),
105  rectPoly.centroid<void>() + Point2D<double>(cos(rotatedrect.angle())*20,
106  sin(rotatedrect.angle())*20));
107  drawLine(img, Line<int>(rectAngleLine), PixRGBA<byte>(255,255,255,128), 3);
108  drawDisk(img, nrt::Circle<int>(Point2D<int>(rectPoly.centroid<void>()), 5), PixRGBA<byte>(255,255,0,128));
109 
110  // Draw some text:
111  drawText(img, Point2D<int32>(10,10), "This is a test", PixRGB<byte>(0,255,0), PixRGBA<byte>(255,255,255, 128));
112 
113  // Draw a Rectangle with semi-transparent outline and fill:
114  Rectangle<int32> rect(Point2D<int32>(250,250), Dims<int32>(50,50));
115  rect = nrt::scale(rect, i/360.0);
116  drawRectangle(img, rect, PixRGBA<byte>(255,0,0, 150), 4, PixRGBA<byte>(0,255,0,150));
117 
118  // Rotate our second set of points: we are now operating with int coordinates all the way
119  std::vector<Point2D<int> > rotatedPoints2(randompoints2.size());
120  std::transform(randompoints2.begin(), randompoints2.end(), rotatedPoints2.begin(),
121  [i](Point2D<int> const & p)
122  { return nrt::rotateAbout(p, Point2D<int>(100,100), -i*M_PI/180.0) +
123  Point2D<int>(std::abs(i - 180)*2, std::abs(i - 180)*2); });
124 
125  for (Point2D<int> const & p : rotatedPoints2)
126  drawDisk(img, nrt::Circle<int>(Point2D<int>(p), 2), PixRGBA<byte>(255,0,255,220));
127  Polygon<int> hull2 = Polygon<int>::approximateConvexHull(rotatedPoints2);
128 
129  hull2 = nrt::scale(hull2, 0.5+i/360.0);
130 
131  drawPolygonAA(img, hull2, PixRGBA<byte>(0,255,0,220));
132 
133 
134  // a little circle to amuse us:
135  nrt::Circle<int> cir(100, 400, 30);
136  cir = nrt::scale(nrt::rotateAbout(cir, Point2D<int>(120,370), i*M_PI/180.0), 0.5+i/360.0)-Point2D<int>(i/40,i/60);
137  drawCircle(img, cir, PixGray<byte>(192), 2);
138  drawCross(img, cir.center(), PixGray<byte>(150), cir.radius(), 1, i*M_PI/180.0);
139 
140  // a grid!
141  drawGrid(img, 10, 10, 1, PixRGBA<float>(128.0F, 1280.0F, 23.0F, 100.0F));
142 
143 
144  // Flip the image...
145  img = nrt::flipVertical(img);
146  mySink->out(nrt::GenericImage(img), "Look at it go!");
147 
148  std::this_thread::sleep_for(std::chrono::milliseconds(10));
149  }
150 
151  std::chrono::duration<float> duration = std::chrono::steady_clock::now() - start;
152  NRT_INFO(1.0/(duration.count())*360.0 << " fps");
154  }
155  return 0;
156 }