Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleDrawLines.cpp

Simple example that draws

A set of images is produced (according to variable "steps") that you can view in biasviewwx as an animation.

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* @example ExampleDrawLines.cpp
@brief Simple example that draws
- a rotating line (like a clock hand)
- a moving line that moves sqrt(2) pixels during the whole iteration
(x and y are both increased by one)
- a static line with a very low slope
- a clipped line (with start and end points outside of ROI)
- another clipped line (with only the end point outside of ROI)
- one line crossing all others (using contrast color functionality)
A set of images is produced (according to variable "steps") that you can view
in biasviewwx as an animation.
@ingroup g_examples
@author MIP
*/
#include <iostream>
#include <sstream>
#include <Base/Image/Image.hh>
#include <Base/Image/ImageIO.hh>
#include <Base/ImageUtils/ImageDraw.hh>
#include <Base/Geometry/HomgPoint2D.hh>
#include <Base/Geometry/HomgPoint3D.hh>
#include <Geometry/RMatrix.hh>
using namespace BIAS;
using namespace std;
int main(int argc, char** argv) {
// leading zeroes in filenames will only be set correctly if steps is below 999
unsigned int steps = 90;
// unused variable: int antialiased = 1; // 0 = no AA, 1 = AA
Image<unsigned char> image(400, 400, 3); // min reasonable size: 200x200
unsigned char bgCol[] = {127, 0, 127};
std::vector<unsigned char> red, green, blue, white, gray, none;
red.push_back(255);
red.push_back(0);
red.push_back(0);
green.push_back(0);
green.push_back(255);
green.push_back(0);
blue.push_back(0);
blue.push_back(0);
blue.push_back(255);
white.push_back(255);
white.push_back(255);
white.push_back(255);
gray.push_back(127);
gray.push_back(127);
gray.push_back(127);
HomgPoint2D clockCenter(image.GetWidth() / 2, image.GetHeight() / 2);
HomgPoint2D clockOuter(image.GetWidth() / 2, image.GetHeight() / 4);
HomgPoint2D clockOuterRunner(clockOuter);
HomgPoint2D moveStart(10, image.GetHeight() / 4);
HomgPoint2D moveEnd(110, image.GetHeight() / 4);
HomgPoint2D moveStartRunner(moveStart);
HomgPoint2D moveEndRunner(moveEnd);
HomgPoint2D staticStart(10, image.GetHeight() / 8);
HomgPoint2D staticEnd(image.GetWidth() - 10, image.GetHeight() / 8 + 2);
HomgPoint2D clippedStart(image.GetWidth() / 2, image.GetHeight() + 20);
HomgPoint2D clippedEnd(image.GetWidth() + 20, image.GetHeight() / 2);
HomgPoint2D clippedStart2(image.GetWidth() / 2, image.GetHeight() * 2 / 3);
HomgPoint2D clippedEnd2(image.GetWidth() + 20, image.GetHeight() * 2 / 3);
HomgPoint2D crossedStart(1, 1);
HomgPoint2D crossedEnd(image.GetWidth(), image.GetHeight());
float angle = 2.0f * float(M_PI) / float(steps); // angle per iteration
ostringstream filename;
for (unsigned int i = 0; i < steps; i++) {
cout << endl << endl << "step: " << (i + 1) << "/" << steps << endl;
// prepare image and filename
image.GetROI()->UnsetROI();
image.SetROICorners(0, 0, image.GetWidth() - 50, image.GetHeight() - 50);
filename.str("");
filename << "clock_";
if (i < 100) {
filename << 0;
}
if (i < 10) {
filename << 0;
}
filename << i << ".mip";
/////
// WHITE CIRCLE
/////
unsigned char col[] = {255, 255, 255};
image.GetWidth() / 2,
image.GetHeight() / 2,
100,
col);
/////
// ROTATING LINE (clock)
/////
// calc new postion of clock point:
// - reset
clockOuterRunner[0] = clockOuter[0];
clockOuterRunner[1] = clockOuter[1];
// - translate clockOuterRunner so that clockCenter is in coord center
clockOuterRunner[0] -= clockCenter[0];
clockOuterRunner[1] -= clockCenter[1];
// - rotation
Vector3<ROTATION_MATRIX_TYPE> p3D1(clockOuterRunner[0], clockOuterRunner[1], 0);
R.SetXYZ(0.0, 0.0, i * angle);
R.Mult(p3D1, p3D2);
clockOuterRunner[0] = p3D2[0];
clockOuterRunner[1] = p3D2[1];
// - undo translation
clockOuterRunner[0] += clockCenter[0];
clockOuterRunner[1] += clockCenter[1];
// draw line
// ImageDraw<unsigned char>::Line(image,
// clockCenter, clockOuterRunner,
// red,
// antialiased);
/////
// MOVING LINE
/////
moveStartRunner[0] = moveStart[0];
moveStartRunner[0] += (HOMGPOINT2D_TYPE)i / (HOMGPOINT2D_TYPE)steps;
moveStartRunner[1] = moveStart[1] + i / steps;
moveStartRunner[1] += (HOMGPOINT2D_TYPE)i / (HOMGPOINT2D_TYPE)steps;
moveEndRunner[0] = moveEnd[0] + i / steps;
moveEndRunner[0] += (HOMGPOINT2D_TYPE)i / (HOMGPOINT2D_TYPE)steps;
moveEndRunner[1] = moveEnd[1] + i / steps;
moveEndRunner[1] += (HOMGPOINT2D_TYPE)i / (HOMGPOINT2D_TYPE)steps;
// ImageDraw<unsigned char>::Line(image,
// moveStartRunner, moveEndRunner,
// blue,
// antialiased);
/////
// STATIC LINE
/////
// ImageDraw<unsigned char>::Line(image,
// staticStart, staticEnd,
// green,
// antialiased);
/////
// CLIPPED LINE
/////
// ImageDraw<unsigned char>::Line(image,
// clippedStart, clippedEnd,
// white,
// antialiased);
/////
// CLIPPED LINE 2
/////
// ImageDraw<unsigned char>::Line(image,
// clippedStart2, clippedEnd2,
// gray,
// antialiased);
/////
// CROSSED LINES
/////
// ImageDraw<unsigned char>::Line(image,
// crossedStart, crossedEnd,
// none,
// antialiased);
// save image
ImageIO::Save(filename.str(), image);
}
return 0;
}