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 <fvmodels/shape/circle.h>
26 
27 #include <cmath>
28 
29 using namespace std;
30 using namespace fawkes;
31 
32 namespace firevision {
33 
34 /** @class Circle <fvmodels/shape/circle.h>
35  * Circle shape.
36  */
37 
38 /** Constructor. */
39 Circle::Circle()
40 {
41  center.x = center.y = 0.0f;
42  radius = -1.0f;
43  count = 0;
44 }
45 
46 /** Constructor.
47  * @param c center
48  * @param r radius
49  * @param n number of pixels
50  */
51 Circle::Circle(const center_in_roi_t &c, float r, int n)
52 {
53  center = c;
54  radius = r;
55  count = n;
56 }
57 
58 /** Print info.
59  * @param stream stream to print to
60  */
61 void
62 Circle::printToStream(std::ostream &stream)
63 {
64  stream << "center=(" << center.x << "," << center.y << ")"
65  << " radius=" << radius << " count= " << count;
66 }
67 
68 /** Fit circle.
69  * Fit a circle through the given points.
70  * @param points points to fit circle through.
71  */
72 void
73 Circle::fitCircle(vector<upoint_t> &points)
74 {
75  // due to fixed width, do not use arrays to save addressing time...
76  double A00 = 0.0, A01 = 0.0, A02 = 0.0;
77  double A10 = 0.0, A11 = 0.0, A12 = 0.0;
78  double A20 = 0.0, A21 = 0.0, A22 = 0.0;
79  double b0 = 0.0, b1 = 0.0, b2 = 0.0;
80 
81  // generating A'A and A'b
82  int count = points.size();
83  for (int i = 0; i < count; i++) {
84  upoint_t &t = points[i];
85  double x0 = 2.0f * t.x;
86  double y0 = 2.0f * t.y;
87  double b = (double)(t.x * t.x + t.y * t.y);
88  A00 += x0 * x0;
89  A01 += x0 * y0;
90  A02 += x0;
91  A10 += y0 * x0;
92  A11 += y0 * y0;
93  A12 += y0;
94  A20 += x0;
95  A21 += y0;
96  A22 += 1.0;
97  b0 += x0 * b;
98  b1 += y0 * b;
99  b2 += b;
100  }
101 
102  // solve the resulting 3 by 3 equations
103  double delta = +A00 * A11 * A22 + A01 * A12 * A20 + A02 * A10 * A21 - A00 * A12 * A21
104  - A01 * A10 * A22 - A02 * A11 * A20;
105  center.x = (float)((+b0 * A11 * A22 + A01 * A12 * b2 + A02 * b1 * A21 - b0 * A12 * A21
106  - A01 * b1 * A22 - A02 * A11 * b2)
107  / delta);
108  center.y = (float)((+A00 * b1 * A22 + b0 * A12 * A20 + A02 * A10 * b2 - A00 * A12 * b2
109  - b0 * A10 * A22 - A02 * b1 * A20)
110  / delta);
111  radius = (float)sqrtf((+A00 * A11 * b2 + A01 * b1 * A20 + b0 * A10 * A21 - A00 * b1 * A21
112  - A01 * A10 * b2 - b0 * A11 * A20)
113  / delta
114  + (double)center.x * center.x + (double)center.y * center.y);
115  count = points.size();
116 }
117 
118 void
119 Circle::setMargin(unsigned int margin)
120 {
121  this->margin = margin;
122 }
123 
124 bool
125 Circle::isClose(unsigned int in_roi_x, unsigned int in_roi_y)
126 {
127  float dx = in_roi_x - center.x;
128  float dy = in_roi_y - center.y;
129 
130  float dist = sqrt(dx * dx + dy * dy);
131 
132  return ((dist <= (radius + margin)) && (dist >= (radius - margin)));
133 }
134 
135 } // end namespace firevision
fawkes::upoint_t
Point with cartesian coordinates as unsigned integers.
Definition: types.h:35
fawkes
Fawkes library namespace.
fawkes::upoint_t::y
unsigned int y
y coordinate
Definition: types.h:37
firevision::center_in_roi_t::x
float x
x in pixels
Definition: types.h:39
firevision::center_in_roi_t
Center in ROI.
Definition: types.h:38
fawkes::upoint_t::x
unsigned int x
x coordinate
Definition: types.h:36