iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tests/test-Geometry.C
/*! @file
@author Randolph Voorhies
@copyright GNU Public License (GPL v3)
@section License
@verbatim
// ////////////////////////////////////////////////////////////////////////
// The iLab Neuromorphic Robotics Toolkit (NRT) //
// Copyright 2010-2012 by the University of Southern California (USC) //
// and the iLab at USC. //
// //
// iLab - University of Southern California //
// Hedco Neurociences Building, Room HNB-10 //
// Los Angeles, Ca 90089-2520 - USA //
// //
// See http://ilab.usc.edu for information about this project. //
// ////////////////////////////////////////////////////////////////////////
// This file is part of The iLab Neuromorphic Robotics Toolkit. //
// //
// The iLab Neuromorphic Robotics Toolkit is free software: you can //
// redistribute it and/or modify it under the terms of the GNU General //
// Public License as published by the Free Software Foundation, either //
// version 3 of the License, or (at your option) any later version. //
// //
// The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
// that it will be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
// PURPOSE. See the GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with The iLab Neuromorphic Robotics Toolkit. If not, see //
// <http://www.gnu.org/licenses/>. //
// ////////////////////////////////////////////////////////////////////////
@endverbatim */
#include <chrono>
using namespace nrt;
int main(int argc, const char** argv)
{
Image<PixRGB<byte>> tmpimg(10,10);
Manager mgr(argc, argv);
auto mySink = mgr.addComponent<ImageSink>("MySink");
mgr.launch();
// create a bunch of random points:
srand(time(NULL));
std::vector<Point2D<double> > randompoints;
for (int i = 0; i < 10; ++i)
randompoints.push_back(Point2D<double>((rand()/float(RAND_MAX)-0.5)*200.0+250,
(rand()/float(RAND_MAX)-0.5)*200.0+250));
std::vector<Point2D<int> > randompoints2;
for (int i = 0; i < 200; ++i)
randompoints2.push_back(Point2D<int>(int((rand()/float(RAND_MAX)-0.5)*100.0+100),
int((rand()/float(RAND_MAX)-0.5)*100.0+100)));
NRT_INFO("try: test-Geometry --out=display to see a visual output.");
while(1)
{
for (int i = 0; i < 360; ++i)
{
Image<PixRGB<byte> > img(Dims<int32>(500, 500), ImageInitPolicy::Zeros);
// Rotate our points:
std::vector<Point2D<double> > rotatedPoints(randompoints.size());
std::transform(randompoints.begin(), randompoints.end(), rotatedPoints.begin(),
[i](Point2D<double> const & p)
{ return nrt::rotateAbout(p, Point2D<double>(250,250), i*M_PI/180.0); });
// Draw each of the rotated points, a semi-transparent small disk for each point:
for (Point2D<double> const & p : rotatedPoints)
drawDisk(img, nrt::Circle<int32>(Point2D<int>(p), 4), PixRGBA<byte>(0,255,255,128));
// Find the convex hull around our rotated points and draw it:
drawPolygon(img, Polygon<int>(hull), PixRGBA<byte>(255,0,0,128), 6, PixRGBA<byte>(0,0,255,64));
// Find the smallest rectangle that contains our rotated convex hull:
// Get the vertices of our rotated rectangle, to draw them as a polygon:
Polygon<double> rectPoly(rotatedrect.vertices());
drawPolygon(img, Polygon<int>(rectPoly), PixRGB<byte>(0,255,0));
// Draw a line of the same angle as our rotated rectangle:
Line<double> rectAngleLine(rectPoly.centroid<void>(),
rectPoly.centroid<void>() + Point2D<double>(cos(rotatedrect.angle())*20,
sin(rotatedrect.angle())*20));
drawLine(img, Line<int>(rectAngleLine), PixRGBA<byte>(255,255,255,128), 3);
drawDisk(img, nrt::Circle<int>(Point2D<int>(rectPoly.centroid<void>()), 5), PixRGBA<byte>(255,255,0,128));
// Draw some text:
drawText(img, Point2D<int32>(10,10), "This is a test", PixRGB<byte>(0,255,0), PixRGBA<byte>(255,255,255, 128));
// Draw a Rectangle with semi-transparent outline and fill:
rect = nrt::scale(rect, i/360.0);
drawRectangle(img, rect, PixRGBA<byte>(255,0,0, 150), 4, PixRGBA<byte>(0,255,0,150));
// Rotate our second set of points: we are now operating with int coordinates all the way
std::vector<Point2D<int> > rotatedPoints2(randompoints2.size());
std::transform(randompoints2.begin(), randompoints2.end(), rotatedPoints2.begin(),
[i](Point2D<int> const & p)
{ return nrt::rotateAbout(p, Point2D<int>(100,100), -i*M_PI/180.0) +
Point2D<int>(std::abs(i - 180)*2, std::abs(i - 180)*2); });
for (Point2D<int> const & p : rotatedPoints2)
drawDisk(img, nrt::Circle<int>(Point2D<int>(p), 2), PixRGBA<byte>(255,0,255,220));
hull2 = nrt::scale(hull2, 0.5+i/360.0);
drawPolygonAA(img, hull2, PixRGBA<byte>(0,255,0,220));
// a little circle to amuse us:
nrt::Circle<int> cir(100, 400, 30);
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);
drawCircle(img, cir, PixGray<byte>(192), 2);
drawCross(img, cir.center(), PixGray<byte>(150), cir.radius(), 1, i*M_PI/180.0);
// a grid!
drawGrid(img, 10, 10, 1, PixRGBA<float>(128.0F, 1280.0F, 23.0F, 100.0F));
// Flip the image...
img = nrt::flipVertical(img);
mySink->out(nrt::GenericImage(img), "Look at it go!");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
std::chrono::duration<float> duration = std::chrono::steady_clock::now() - start;
NRT_INFO(1.0/(duration.count())*360.0 << " fps");
}
return 0;
}