Fawkes API  Fawkes Development Version
fit_accum.cpp
1 /***************************************************************************
2  * fit_accum.cpp - Implementation of 'fitted circle' accumulator
3  * used by Fix-Point RCD Algorithm
4  *
5  * Generated: Sat Sep 10 2005 17:28:12
6  * Copyright 2005 Hu Yuxiao <Yuxiao.Hu@rwth-aachen.de>
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvmodels/shape/accumulators/fit_accum.h>
25 
26 #include <fvmodels/shape/circle.h>
27 #include <cmath>
28 
29 using namespace fawkes;
30 
31 namespace firevision {
32 #if 0 /* just to make Emacs auto-indent happy */
33 }
34 #endif
35 
36 const float FitAccum::TOO_SMALL_DELTA = 1.0e-3f;
37 
38 /** @class FitAccum <fvmodels/shape/accumulators/fit_accum.h>
39  * FIT Accumulator.
40  */
41 
42 /** Constructor. */
43 FitAccum::FitAccum(void)
44 {
45  reset();
46 }
47 
48 /** Destructor. */
49 FitAccum::~FitAccum(void)
50 {
51 }
52 
53 /** Reset. */
54 void
55 FitAccum::reset(void)
56 {
57  count = 0;
58  A00 = A01 = A02 = 0.0f;
59  A10 = A11 = A12 = 0.0f;
60  A20 = A21 = A22 = 0.0f;
61  b0 = b1 = b2 = 0.0f;
62 }
63 
64 /** Add point.
65  * @param pt point
66  */
67 void
68 FitAccum::addPoint(const upoint_t& pt)
69 {
70  ++count;
71 
72  A00 += 4 * pt.x * pt.x;
73  A01 += 4 * pt.x * pt.y;
74  A02 += 2 * pt.x;
75 
76  A10 += 4 * pt.y * pt.x;
77  A11 += 4 * pt.y * pt.y;
78  A12 += 2 * pt.y;
79 
80  A20 += 2 * pt.x;
81  A21 += 2 * pt.y;
82  A22 += 1;
83 
84  float r2 = pt.x * pt.x + pt.y * pt.y;
85  b0 += 2 * r2 * pt.x;
86  b1 += 2 * r2 * pt.y;
87  b2 += r2;
88 }
89 
90 
91 /** Remove point.
92  * @param pt point
93  */
94 void
95 FitAccum::removePoint(const upoint_t& pt)
96 {
97  --count;
98  A00 -= 4 * pt.x * pt.x;
99  A01 -= 4 * pt.x * pt.y;
100  A02 -= 2 * pt.x;
101 
102  A10 -= 4 * pt.y * pt.x;
103  A11 -= 4 * pt.y * pt.y;
104  A12 -= 2 * pt.y;
105 
106  A20 -= 2 * pt.x;
107  A21 -= 2 * pt.y;
108  A22 -= 1;
109 
110  float r2 = pt.x * pt.x + pt.y * pt.y;
111  b0 -= 2 * r2 * pt.x;
112  b1 -= 2 * r2 * pt.y;
113  b2 -= r2;
114 }
115 
116 /** Get count.
117  * @return count
118  */
119 int
120 FitAccum::getCount(void) const
121 {
122  return count;
123 }
124 
125 /** Get circle.
126  * @return circle
127  */
128 Circle*
129 FitAccum::getCircle(void) const
130 {
131  // solve the resulting 3 by 3 equations
132  static Circle c;
133 
134  float delta = + A00 * A11 * A22 + A01 * A12 * A20 + A02 * A10 * A21
135  - A00 * A12 * A21 - A01 * A10 * A22 - A02 * A11 * A20;
136 
137  if (delta > -TOO_SMALL_DELTA && delta < TOO_SMALL_DELTA)
138  {
139 // printf("A=\n");
140 // printf("\t%f\t%f\t%f\n", A00, A01, A02);
141 // printf("\t%f\t%f\t%f\n", A10, A11, A12);
142 // printf("\t%f\t%f\t%f\n", A20, A21, A22);
143 // printf("b=\n");
144 // printf("\t%f\t%f\t%f\n", b0, b1, b2);
145 // printf("Delta too small: %e\n", delta);
146  return NULL;
147  }
148  else
149  {
150  c.center.x = (float)( ( + b0 * A11 * A22 + A01 * A12 * b2 + A02 * b1 * A21
151  - b0 * A12 * A21 - A01 * b1 * A22 - A02 * A11 * b2 ) / delta);
152  c.center.y = (float)( ( + A00 * b1 * A22 + b0 * A12 * A20 + A02 * A10 * b2
153  - A00 * A12 * b2 - b0 * A10 * A22 - A02 * b1 * A20 ) / delta);
154  c.radius = (float)sqrt((+ A00 * A11 * b2 + A01 * b1 * A20 + b0 * A10 * A21
155  - A00 * b1 * A21 - A01 * A10 * b2 - b0 * A11 * A20 ) / delta
156  + c.center.x * c.center.x + c.center.y * c.center.y);
157  c.count = count;
158  return &c;
159  }
160 }
161 
162 } // end namespace firevision
Fawkes library namespace.
unsigned int y
y coordinate
Definition: types.h:36
unsigned int x
x coordinate
Definition: types.h:35
float x
x in pixels
Definition: types.h:40
Circle shape.
Definition: circle.h:45
Point with cartesian coordinates as unsigned integers.
Definition: types.h:34
int count
Number of pixels.
Definition: circle.h:64
center_in_roi_t center
Center of object in ROI.
Definition: circle.h:60
float y
y in pixels
Definition: types.h:41
float radius
Radius of object.
Definition: circle.h:62