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