Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JCircle2D.hh
Go to the documentation of this file.
1 #ifndef __JCIRCLE2D__
2 #define __JCIRCLE2D__
3 
4 #include <istream>
5 #include <ostream>
6 #include <iterator>
7 #include <cmath>
8 #include <limits>
9 
10 #include "JIO/JSerialisable.hh"
12 
13 
14 /**
15  * \author mdejong
16  */
17 
18 namespace JGEOMETRY2D {}
19 namespace JPP { using namespace JGEOMETRY2D; }
20 
21 namespace JGEOMETRY2D {
22 
23  using JIO::JReader;
24  using JIO::JWriter;
25 
26 
27  /**
28  * Data structure for circle in two dimensions.
29  */
30  class JCircle2D :
31  public JPosition2D
32  {
33  public:
34  /**
35  * Default constructor.
36  */
38  JPosition2D(),
39  __r(0.0)
40  {}
41 
42 
43  /**
44  * Constructor.
45  *
46  * \param point point
47  * \param r radius
48  */
49  JCircle2D(const JVector2D& point,
50  const double r) :
51  JPosition2D(point),
52  __r(r)
53  {}
54 
55 
56  /**
57  * Constructor.
58  * Determines circle through two points.
59  *
60  * \param p0 first point
61  * \param p1 second point
62  */
63  JCircle2D(const JVector2D& p0,
64  const JVector2D& p1) :
65  JPosition2D(),
66  __r(0.0)
67  {
68  set(p0, p1);
69  }
70 
71 
72  /**
73  * Constructor.
74  * Determines circle through three points.
75  *
76  * \param p0 first point
77  * \param p1 second point
78  * \param p2 third point
79  * \param precision precision
80  */
81  JCircle2D(const JVector2D& p0,
82  const JVector2D& p1,
83  const JVector2D& p2,
84  const double precision = std::numeric_limits<double>::epsilon()) :
85  JPosition2D(),
86  __r(0.0)
87  {
88  set(p0, p1, p2, precision);
89  }
90 
91 
92  /**
93  * Constructor.
94  * Determines smallest enclosing circle around set of points.\n
95  * The type of data should have the following member methods:
96  * <pre>
97  * double getX(); // x coordinate
98  * double getY(); // y coordinate
99  * </pre>
100  *
101  * Reference:\n
102  * Computational Geometry
103  * Algorithms and Applications\n
104  * Authors: de Berg, M., Cheong, O., van Kreveld, M., Overmars, M.
105  *
106  * \param __begin begin of data
107  * \param __end end of data
108  * \param precision precision
109  */
110  template<class T>
111  JCircle2D(T __begin,
112  T __end,
113  const double precision = std::numeric_limits<double>::epsilon()) :
114  JPosition2D(),
115  __r(0.0)
116  {
117  set(__begin, __end, precision);
118  }
119 
120 
121  /**
122  * Get radius.
123  *
124  * \return radius
125  */
126  double getRadius() const
127  {
128  return __r;
129  }
130 
131 
132  /**
133  * Set circle.
134  * Determines circle through two points.
135  *
136  * \param p0 first point
137  * \param p1 second point
138  */
139  void set(const JVector2D& p0,
140  const JVector2D& p1)
141  {
142  __x = 0.5 * (p0.getX() + p1.getX());
143  __y = 0.5 * (p0.getY() + p1.getY());
144  __r = this->getDistance(p0);
145  }
146 
147 
148  /**
149  * Set circle.
150  * Determines circle through three points.
151  *
152  * \param p0 first point
153  * \param p1 second point
154  * \param p2 third point
155  * \param precision precision
156  */
157  void set(const JVector2D& p0,
158  const JVector2D& p1,
159  const JVector2D& p2,
160  const double precision = std::numeric_limits<double>::epsilon())
161  {
162  const double x0 = p2.getX() - p1.getX();
163  const double x1 = p0.getX() - p2.getX();
164  const double x2 = p1.getX() - p0.getX();
165 
166  const double y0 = p1.getY() - p2.getY();
167  const double y1 = p2.getY() - p0.getY();
168  const double y2 = p0.getY() - p1.getY();
169 
170  const double D = 2.0 * (p0.getX()*y0 + p1.getX()*y1 + p2.getX()*y2);
171 
172  if (fabs(D) > precision) {
173 
174  const double a = p0.getLengthSquared();
175  const double b = p1.getLengthSquared();
176  const double c = p2.getLengthSquared();
177 
178  __x = (a*y0 + b*y1 + c*y2) / D;
179  __y = (a*x0 + b*x1 + c*x2) / D;
180  __r = this->getDistance(p0);
181 
182  } else {
183 
184  set(p0, p1);
185 
186  const JCircle2D c1(p1, p2);
187  const JCircle2D c2(p0, p2);
188 
189  if (c1.getRadius() > this->getRadius()) { *this = c1; }
190  if (c2.getRadius() > this->getRadius()) { *this = c2; }
191  }
192  }
193 
194 
195  /**
196  * Set circle.
197  * Determines smallest enclosing circle around set of points.
198  * <pre>
199  * double getX(); // x coordinate
200  * double getY(); // y coordinate
201  * </pre>
202  *
203  * \param __begin begin of data
204  * \param __end end of data
205  * \param precision precision
206  */
207  template<class T>
208  void set(T __begin,
209  T __end,
210  const double precision = std::numeric_limits<double>::epsilon())
211  {
212  if (__begin != __end) {
213 
214  __x = __begin->getX();
215  __y = __begin->getY();
216  __r = 0.0;
217 
218  T i = __begin;
219 
220  const JVector2D p0(i->getX(), i->getY());
221 
222  while (++i != __end && p0.getDistance(JVector2D(i->getX(), i->getY())) <= precision) {}
223 
224  if (i != __end) {
225 
226  const JVector2D p1(i->getX(), i->getY());
227 
228  set(p0, p1);
229 
230  while (++i != __end) {
231 
232  const JVector2D p2(i->getX(), i->getY());
233 
234  if (this->getDistance(p2) > this->getRadius() + precision &&
235  p0.getDistance(p2) > precision &&
236  p1.getDistance(p2) > precision) {
237  configure(__begin, i, p2, precision);
238  }
239  }
240  }
241  }
242  }
243 
244 
245  /**
246  * Check whether given point is inside circle.
247  *
248  * \param pos position
249  * \param precision precision
250  * \return true if inside; else false
251  */
252  inline bool is_inside(const JVector2D& pos,
253  const double precision = std::numeric_limits<double>::min()) const
254  {
255  return (this->getDistance(pos) <= this->getRadius() + precision);
256  }
257 
258 
259  /**
260  * Read circle from input stream.
261  *
262  * \param in input stream
263  * \param circle circle
264  * \return input stream
265  */
266  friend inline std::istream& operator>>(std::istream& in, JCircle2D& circle)
267  {
268  in >> static_cast<JPosition2D&>(circle);
269  in >> circle.__r;
270 
271  return in;
272  }
273 
274 
275  /**
276  * Write circle to output stream.
277  *
278  * \param out output stream
279  * \param circle circle
280  * \return output stream
281  */
282  friend inline std::ostream& operator<<(std::ostream& out, const JCircle2D& circle)
283  {
284  out << static_cast<const JPosition2D&>(circle);
285  out << ' ';
286  out << circle.__r;
287 
288  return out;
289  }
290 
291 
292  /**
293  * Read circle from input.
294  *
295  * \param in reader
296  * \param circle circle
297  * \return reader
298  */
299  friend inline JReader& operator>>(JReader& in, JCircle2D& circle)
300  {
301  in >> static_cast<JPosition2D&>(circle);
302  in >> circle.__r;
303 
304  return in;
305  }
306 
307 
308  /**
309  * Write circle to output.
310  *
311  * \param out writer
312  * \param circle circle
313  * \return writer
314  */
315  friend inline JWriter& operator<<(JWriter& out, const JCircle2D& circle)
316  {
317  out << static_cast<const JPosition2D&>(circle);
318  out << circle.__r;
319 
320  return out;
321  }
322 
323 
324  protected:
325  double __r;
326 
327 
328  private:
329  /**
330  * Determine smallest enclosing circle.
331  *
332  * \param __begin begin of data
333  * \param __end end of data
334  * \param p0 point on circle
335  * \param precision precision
336  */
337  template<class T>
338  void configure(T __begin,
339  T __end,
340  const JVector2D& p0,
341  const double precision)
342  {
343  this->set(JVector2D(__begin->getX(), __begin->getY()), p0);
344 
345  for (T i = __begin; ++i != __end; ) {
346 
347  const JVector2D p1(i->getX(), i->getY());
348 
349  if (this->getDistance(p1) > this->getRadius() + precision &&
350  p0.getDistanceSquared(p1) > precision) {
351  configure(__begin, i, p0, p1, precision);
352  }
353  }
354  }
355 
356 
357  /**
358  * Determine smallest enclosing circle.
359  *
360  * \param __begin begin of data
361  * \param __end end of data
362  * \param p0 point on circle
363  * \param p1 point on circle
364  * \param precision precision
365  */
366  template<class T>
367  void configure(T __begin,
368  T __end,
369  const JVector2D& p0,
370  const JVector2D& p1,
371  const double precision)
372  {
373  this->set(p0, p1);
374 
375  for (T i = __begin; i != __end; ++i) {
376 
377  const JVector2D p2(i->getX(), i->getY());
378 
379  if (this->getDistance(p2) > this->getRadius() + precision &&
380  p0.getDistanceSquared(p2) > precision &&
381  p1.getDistanceSquared(p2) > precision) {
382  this->set(p0, p1, p2, precision);
383  }
384  }
385  }
386  };
387 }
388 
389 #endif
Data structure for vector in two dimensions.
Definition: JVector2D.hh:32
JCircle2D()
Default constructor.
Definition: JCircle2D.hh:37
double getLengthSquared() const
Get length squared.
Definition: JVector2D.hh:188
Interface for binary output.
do echo Generating $dir eval D
Definition: JDrawLED.sh:50
double getRadius() const
Get radius.
Definition: JCircle2D.hh:126
TPaveText * p1
JVector2D()
Default constructor.
Definition: JVector2D.hh:39
double getDistanceSquared(const JVector2D &point) const
Get squared of distance to point.
Definition: JVector2D.hh:211
void configure(T __begin, T __end, const JVector2D &p0, const JVector2D &p1, const double precision)
Determine smallest enclosing circle.
Definition: JCircle2D.hh:367
Data structure for circle in two dimensions.
Definition: JCircle2D.hh:30
double getDistance(const JVector2D &point) const
Get distance to point.
Definition: JVector2D.hh:223
friend JWriter & operator<<(JWriter &out, const JCircle2D &circle)
Write circle to output.
Definition: JCircle2D.hh:315
data_type r[M+1]
Definition: JPolint.hh:742
void set(T __begin, T __end, const double precision=std::numeric_limits< double >::epsilon())
Set circle.
Definition: JCircle2D.hh:208
double getY() const
Get y position.
Definition: JVector2D.hh:74
JCircle2D(const JVector2D &p0, const JVector2D &p1)
Constructor.
Definition: JCircle2D.hh:63
friend std::ostream & operator<<(std::ostream &out, const JCircle2D &circle)
Write circle to output stream.
Definition: JCircle2D.hh:282
bool is_inside(const JVector2D &pos, const double precision=std::numeric_limits< double >::min()) const
Check whether given point is inside circle.
Definition: JCircle2D.hh:252
double getX() const
Get x position.
Definition: JVector2D.hh:63
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Interface for binary input.
void configure(T __begin, T __end, const JVector2D &p0, const double precision)
Determine smallest enclosing circle.
Definition: JCircle2D.hh:338
void set(const JVector2D &p0, const JVector2D &p1, const JVector2D &p2, const double precision=std::numeric_limits< double >::epsilon())
Set circle.
Definition: JCircle2D.hh:157
TCanvas * c1
Global variables to handle mouse events.
p2
Definition: module-Z:fit.sh:48
Data structure for position in two dimensions.
Definition: JPosition2D.hh:30
then $JPP_DIR software JCalibrate JCalibrateToT a
Definition: JTuneHV.sh:108
JCircle2D(const JVector2D &point, const double r)
Constructor.
Definition: JCircle2D.hh:49
friend JReader & operator>>(JReader &in, JCircle2D &circle)
Read circle from input.
Definition: JCircle2D.hh:299
JCircle2D(T __begin, T __end, const double precision=std::numeric_limits< double >::epsilon())
Constructor.
Definition: JCircle2D.hh:111
void set(const JVector2D &p0, const JVector2D &p1)
Set circle.
Definition: JCircle2D.hh:139
friend std::istream & operator>>(std::istream &in, JCircle2D &circle)
Read circle from input stream.
Definition: JCircle2D.hh:266
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
JCircle2D(const JVector2D &p0, const JVector2D &p1, const JVector2D &p2, const double precision=std::numeric_limits< double >::epsilon())
Constructor.
Definition: JCircle2D.hh:81