Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleDrawLines.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 /**
27  * @example ExampleDrawLines.cpp
28  @brief Simple example that draws
29  - a rotating line (like a clock hand)
30  - a moving line that moves sqrt(2) pixels during the whole iteration
31  (x and y are both increased by one)
32  - a static line with a very low slope
33  - a clipped line (with start and end points outside of ROI)
34  - another clipped line (with only the end point outside of ROI)
35  - one line crossing all others (using contrast color functionality)
36 
37  A set of images is produced (according to variable "steps") that you can view
38  in biasviewwx as an animation.
39  @ingroup g_examples
40  @author MIP
41 */
42 
43 #include <iostream>
44 #include <sstream>
45 
46 #include <Base/Image/Image.hh>
47 #include <Base/Image/ImageIO.hh>
48 #include <Base/ImageUtils/ImageDraw.hh>
49 
50 #include <Base/Geometry/HomgPoint2D.hh>
51 #include <Base/Geometry/HomgPoint3D.hh>
52 #include <Geometry/RMatrix.hh>
53 
54 using namespace BIAS;
55 using namespace std;
56 
57 
58 
59 int main(int argc, char** argv) {
60 
61  // leading zeroes in filenames will only be set correctly if steps is below 999
62  unsigned int steps = 90;
63  // unused variable: int antialiased = 1; // 0 = no AA, 1 = AA
64 
65  Image<unsigned char> image(400, 400, 3); // min reasonable size: 200x200
66 
67  unsigned char bgCol[] = {127, 0, 127};
68 
69  std::vector<unsigned char> red, green, blue, white, gray, none;
70  red.push_back(255);
71  red.push_back(0);
72  red.push_back(0);
73  green.push_back(0);
74  green.push_back(255);
75  green.push_back(0);
76  blue.push_back(0);
77  blue.push_back(0);
78  blue.push_back(255);
79  white.push_back(255);
80  white.push_back(255);
81  white.push_back(255);
82  gray.push_back(127);
83  gray.push_back(127);
84  gray.push_back(127);
85 
86  HomgPoint2D clockCenter(image.GetWidth() / 2, image.GetHeight() / 2);
87  HomgPoint2D clockOuter(image.GetWidth() / 2, image.GetHeight() / 4);
88  HomgPoint2D clockOuterRunner(clockOuter);
89  HomgPoint2D moveStart(10, image.GetHeight() / 4);
90  HomgPoint2D moveEnd(110, image.GetHeight() / 4);
91  HomgPoint2D moveStartRunner(moveStart);
92  HomgPoint2D moveEndRunner(moveEnd);
93  HomgPoint2D staticStart(10, image.GetHeight() / 8);
94  HomgPoint2D staticEnd(image.GetWidth() - 10, image.GetHeight() / 8 + 2);
95  HomgPoint2D clippedStart(image.GetWidth() / 2, image.GetHeight() + 20);
96  HomgPoint2D clippedEnd(image.GetWidth() + 20, image.GetHeight() / 2);
97  HomgPoint2D clippedStart2(image.GetWidth() / 2, image.GetHeight() * 2 / 3);
98  HomgPoint2D clippedEnd2(image.GetWidth() + 20, image.GetHeight() * 2 / 3);
99  HomgPoint2D crossedStart(1, 1);
100  HomgPoint2D crossedEnd(image.GetWidth(), image.GetHeight());
101 
102  float angle = 2.0f * float(M_PI) / float(steps); // angle per iteration
103 
104  ostringstream filename;
105 
106  for (unsigned int i = 0; i < steps; i++) {
107  cout << endl << endl << "step: " << (i + 1) << "/" << steps << endl;
108 
109  // prepare image and filename
110  image.GetROI()->UnsetROI();
111  image.FillImageWithConstValue(bgCol);
112  image.SetROICorners(0, 0, image.GetWidth() - 50, image.GetHeight() - 50);
113 
114  filename.str("");
115  filename << "clock_";
116  if (i < 100) {
117  filename << 0;
118  }
119  if (i < 10) {
120  filename << 0;
121  }
122  filename << i << ".mip";
123 
124  /////
125  // WHITE CIRCLE
126  /////
127  unsigned char col[] = {255, 255, 255};
129  image.GetWidth() / 2,
130  image.GetHeight() / 2,
131  100,
132  col);
133 
134  /////
135  // ROTATING LINE (clock)
136  /////
137 
138  // calc new postion of clock point:
139  // - reset
140  clockOuterRunner[0] = clockOuter[0];
141  clockOuterRunner[1] = clockOuter[1];
142 
143  // - translate clockOuterRunner so that clockCenter is in coord center
144  clockOuterRunner[0] -= clockCenter[0];
145  clockOuterRunner[1] -= clockCenter[1];
146 
147  // - rotation
148  RMatrix R;
149  Vector3<ROTATION_MATRIX_TYPE> p3D1(clockOuterRunner[0], clockOuterRunner[1], 0);
151  R.SetXYZ(0.0, 0.0, i * angle);
152  R.Mult(p3D1, p3D2);
153  clockOuterRunner[0] = p3D2[0];
154  clockOuterRunner[1] = p3D2[1];
155 
156  // - undo translation
157  clockOuterRunner[0] += clockCenter[0];
158  clockOuterRunner[1] += clockCenter[1];
159 
160  // draw line
161  // ImageDraw<unsigned char>::Line(image,
162 // clockCenter, clockOuterRunner,
163 // red,
164 // antialiased);
165 
166  /////
167  // MOVING LINE
168  /////
169 
170  moveStartRunner[0] = moveStart[0];
171  moveStartRunner[0] += (HOMGPOINT2D_TYPE)i / (HOMGPOINT2D_TYPE)steps;
172  moveStartRunner[1] = moveStart[1] + i / steps;
173  moveStartRunner[1] += (HOMGPOINT2D_TYPE)i / (HOMGPOINT2D_TYPE)steps;
174 
175  moveEndRunner[0] = moveEnd[0] + i / steps;
176  moveEndRunner[0] += (HOMGPOINT2D_TYPE)i / (HOMGPOINT2D_TYPE)steps;
177  moveEndRunner[1] = moveEnd[1] + i / steps;
178  moveEndRunner[1] += (HOMGPOINT2D_TYPE)i / (HOMGPOINT2D_TYPE)steps;
179 
180  // ImageDraw<unsigned char>::Line(image,
181 // moveStartRunner, moveEndRunner,
182 // blue,
183 // antialiased);
184 
185  /////
186  // STATIC LINE
187  /////
188 
189  // ImageDraw<unsigned char>::Line(image,
190 // staticStart, staticEnd,
191 // green,
192 // antialiased);
193 
194  /////
195  // CLIPPED LINE
196  /////
197 
198  // ImageDraw<unsigned char>::Line(image,
199 // clippedStart, clippedEnd,
200 // white,
201 // antialiased);
202 
203  /////
204  // CLIPPED LINE 2
205  /////
206 
207  // ImageDraw<unsigned char>::Line(image,
208 // clippedStart2, clippedEnd2,
209 // gray,
210 // antialiased);
211 
212  /////
213  // CROSSED LINES
214  /////
215 
216  // ImageDraw<unsigned char>::Line(image,
217 // crossedStart, crossedEnd,
218 // none,
219 // antialiased);
220 
221  // save image
222  ImageIO::Save(filename.str(), image);
223  }
224 
225  return 0;
226 }
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
void SetXYZ(ROTATION_MATRIX_TYPE PhiX, ROTATION_MATRIX_TYPE PhiY, ROTATION_MATRIX_TYPE PhiZ)
Set Euler angles (in rad) in order XYZ.
static int CircleCenterFilled(Image< StorageType > &im, unsigned int CenterX, unsigned int CenterY, unsigned int Radius, const StorageType Value[])
draws a filled circle using Value
Definition: ImageDraw.cpp:1023
3D rotation matrix
Definition: RMatrix.hh:49
void Mult(const Vector3< T > &argvec, Vector3< T > &destvec) const
matrix - vector multiplicate this matrix with Vector3, storing the result in destvec calculates: dest...
Definition: Matrix3x3.hh:302
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53