NEURON
mymath.h
Go to the documentation of this file.
1 #ifndef mymath_h
2 #define mymath_h
3 
4 #include <OS/math.h>
5 #include <InterViews/geometry.h>
6 
7 extern "C" int nrn_isdouble(double*, double, double);
8 
9 class Extension;
10 
11 class MyMath {
12  public:
13  // increase all around in screen coords
14  static void extend(Extension&, Coord);
15  static void extend(Extension&, const Extension&);
16 
17  static void minmax(Coord& min, Coord& max);
18  static bool inside(Coord x, Coord min, Coord max);
19 
20  static float min(int count, const float*);
21  static float max(int count, const float*);
22 
23  static bool inside(Coord x, Coord y, Coord left, Coord bottom, Coord right, Coord top);
24 
25  // within epsilon distance from the infinite line
26  static bool near_line(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2, float epsilon);
27 
28  // within epsilon distance from the line segment
29  static bool
30  near_line_segment(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2, float epsilon);
31 
32  // returns distance between point and line segment
33  static float distance_to_line_segment(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2);
34 
35  // returns distance between point and line
36  static float distance_to_line(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2);
37 
38 
39  // returns square norm
40  static float norm2(Coord x, Coord y) {
41  return x * x + y * y;
42  }
43 
44  // unit length vector perpindicular to vector (x, y)
45  static bool unit_normal(Coord x, Coord y, Coord* perp);
46 
47  // returns range extended to nearest 1.5 digit accuracy.
48  // ie. digit is 1, 2, or 5
49  static void round_range(Coord x1, Coord x2, double& y1, double& y2, int& ntic);
50  static void round_range_down(Coord x1, Coord x2, double& y1, double& y2, int& ntic);
51 
52  enum { Expand, Contract, Higher, Lower };
53  static double round(float& x1, float& x2, int direction, int digits);
54 
55  static void box(Requisition&, Coord& x1, Coord& y1, Coord& x2, Coord& y2);
56 
57  static double anint(double); /* round toward nearest integer */
58  static double resolution(double); // 100, 10, .1,... least significant digit
59 
60  static bool lt(double x, double y, double e) {
61  return x < (y - e);
62  }
63  static bool le(double x, double y, double e) {
64  return x <= (y + e);
65  }
66  static bool eq(double x, double y, double e) {
67  return Math::equal(x, y, e);
68  }
69  static bool eq2(double x, double y, double e) {
70  return x - y <= e && y - x <= e;
71  }
72 };
73 
74 inline void MyMath::extend(Extension& e, const Extension& x) {
75  e.set_xy(NULL,
76  e.left() + x.left(),
77  e.bottom() + x.bottom(),
78  e.right() + x.right(),
79  e.top() + x.top());
80 }
81 
82 inline void MyMath::extend(Extension& e, Coord x) {
83  e.set_xy(NULL, e.left() - x, e.bottom() - x, e.right() + x, e.top() + x);
84 }
85 
86 inline void MyMath::minmax(Coord& x, Coord& y) {
87  if (y < x) {
88  Coord z = x;
89  x = y;
90  y = z;
91  }
92 }
93 
94 inline bool MyMath::inside(Coord x, Coord x1, Coord x2) {
95  return (x >= x1 && x <= x2);
96 }
97 
98 inline bool MyMath::inside(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2) {
99  return inside(x, x1, x2) && inside(y, y1, y2);
100 }
101 
102 #endif
#define Coord
Definition: _defines.h:19
Coord left() const
Definition: geometry.h:297
Coord top() const
Definition: geometry.h:300
Coord bottom() const
Definition: geometry.h:298
Coord right() const
Definition: geometry.h:299
static bool equal(float x, float y, float e)
Definition: math.h:108
Definition: mymath.h:11
static bool near_line(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2, float epsilon)
Definition: mymath.cpp:116
static void round_range_down(Coord x1, Coord x2, double &y1, double &y2, int &ntic)
Definition: mymath.cpp:301
@ Expand
Definition: mymath.h:52
@ Lower
Definition: mymath.h:52
@ Contract
Definition: mymath.h:52
@ Higher
Definition: mymath.h:52
static bool near_line_segment(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2, float epsilon)
Definition: mymath.cpp:194
static float distance_to_line_segment(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2)
Definition: mymath.cpp:165
static bool le(double x, double y, double e)
Definition: mymath.h:63
static double anint(double)
Definition: mymath.cpp:87
static void minmax(Coord &min, Coord &max)
Definition: mymath.h:86
static bool eq2(double x, double y, double e)
Definition: mymath.h:69
static bool unit_normal(Coord x, Coord y, Coord *perp)
Definition: mymath.cpp:368
static bool lt(double x, double y, double e)
Definition: mymath.h:60
static void round_range(Coord x1, Coord x2, double &y1, double &y2, int &ntic)
Definition: mymath.cpp:276
static double resolution(double)
static float max(int count, const float *)
Definition: mymath.cpp:104
static void extend(Extension &, Coord)
Definition: mymath.h:82
static bool inside(Coord x, Coord min, Coord max)
Definition: mymath.h:94
static void box(Requisition &, Coord &x1, Coord &y1, Coord &x2, Coord &y2)
Definition: mymath.cpp:359
static float min(int count, const float *)
Definition: mymath.cpp:94
static double round(float &x1, float &x2, int direction, int digits)
Definition: mymath.cpp:330
static float norm2(Coord x, Coord y)
Definition: mymath.h:40
static bool eq(double x, double y, double e)
Definition: mymath.h:66
static float distance_to_line(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2)
Definition: mymath.cpp:143
int nrn_isdouble(double *, double, double)
Definition: isoc99.cpp:12
#define e
Definition: passive0.cpp:22
#define left
Definition: rbtqueue.cpp:45
#define right
Definition: rbtqueue.cpp:46
#define NULL
Definition: sptree.h:16