Fawkes API  Fawkes Development Version
circle.cpp
1 
2 /***************************************************************************
3  * circle.cpp - Implementation of a circle shape finder
4  *
5  * Created: Thu May 16 00:00:00 2005
6  * Copyright 2005 Tim Niemueller [www.niemueller.de]
7  * Hu Yuxiao <Yuxiao.Hu@rwth-aachen.de>
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #include <cmath>
26 #include <fvmodels/shape/circle.h>
27 
28 using namespace std;
29 using namespace fawkes;
30 
31 namespace firevision {
32 #if 0 /* just to make Emacs auto-indent happy */
33 }
34 #endif
35 
36 /** @class Circle <fvmodels/shape/circle.h>
37  * Circle shape.
38  */
39 
40 /** Constructor. */
41 Circle::Circle()
42 {
43  center.x=center.y=0.0f;
44  radius = -1.0f;
45  count = 0;
46 }
47 
48 /** Constructor.
49  * @param c center
50  * @param r radius
51  * @param n number of pixels
52  */
53 Circle::Circle(const center_in_roi_t& c, float r, int n)
54 {
55  center = c;
56  radius = r;
57  count = n;
58 }
59 
60 /** Print info.
61  * @param stream stream to print to
62  */
63 void
64 Circle::printToStream(std::ostream &stream)
65 {
66  stream << "center=(" << center.x << "," << center.y << ")"
67  << " radius=" << radius << " count= " << count;
68 }
69 
70 /** Fit circle.
71  * Fit a circle through the given points.
72  * @param points points to fit circle through.
73  */
74 void
75 Circle::fitCircle (vector< upoint_t > &points)
76 {
77  // due to fixed width, do not use arrays to save addressing time...
78  double A00=0.0, A01=0.0, A02=0.0;
79  double A10=0.0, A11=0.0, A12=0.0;
80  double A20=0.0, A21=0.0, A22=0.0;
81  double b0 =0.0, b1 =0.0, b2 =0.0;
82 
83  // generating A'A and A'b
84  int count = points.size();
85  for (int i = 0; i < count; i++)
86  {
87  upoint_t &t = points[i];
88  double x0 = 2.0f * t.x;
89  double y0 = 2.0f * t.y;
90  double b = (double)(t.x * t.x + t.y * t.y);
91  A00 += x0 * x0;
92  A01 += x0 * y0;
93  A02 += x0;
94  A10 += y0 * x0;
95  A11 += y0 * y0;
96  A12 += y0;
97  A20 += x0;
98  A21 += y0;
99  A22 += 1.0;
100  b0 += x0 * b;
101  b1 += y0 * b;
102  b2 += b;
103  }
104 
105  // solve the resulting 3 by 3 equations
106  double delta = + A00 * A11 * A22 + A01 * A12 * A20 + A02 * A10 * A21
107  - A00 * A12 * A21 - A01 * A10 * A22 - A02 * A11 * A20;
108  center.x = (float)( ( + b0 * A11 * A22 + A01 * A12 * b2 + A02 * b1 * A21
109  - b0 * A12 * A21 - A01 * b1 * A22 - A02 * A11 * b2 ) / delta);
110  center.y = (float)( ( + A00 * b1 * A22 + b0 * A12 * A20 + A02 * A10 * b2
111  - A00 * A12 * b2 - b0 * A10 * A22 - A02 * b1 * A20 ) / delta);
112  radius = (float)sqrt( ( + A00 * A11 * b2 + A01 * b1 * A20 + b0 * A10 * A21
113  - A00 * b1 * A21 - A01 * A10 * b2 - b0 * A11 * A20 ) / delta
114  + center.x * center.x + center.y * center.y);
115  count = points.size();
116 }
117 
118 
119 void
120 Circle::setMargin( unsigned int margin )
121 {
122  this->margin = margin;
123 }
124 
125 
126 bool
127 Circle::isClose( unsigned int in_roi_x, unsigned int in_roi_y )
128 {
129  float dx = in_roi_x - center.x;
130  float dy = in_roi_y - center.y;
131 
132  float dist = sqrt( dx * dx + dy * dy );
133 
134  return ( (dist <= (radius + margin)) &&
135  (dist >= (radius - margin)) );
136 
137 }
138 
139 } // end namespace firevision
Fawkes library namespace.
unsigned int y
y coordinate
Definition: types.h:36
STL namespace.
unsigned int x
x coordinate
Definition: types.h:35
float x
x in pixels
Definition: types.h:40
Point with cartesian coordinates as unsigned integers.
Definition: types.h:34
Center in ROI.
Definition: types.h:39