~registry/texmacs/trunk

« back to all changes in this revision

Viewing changes to src/src/Graphics/Mathematics/ball.hpp

  • Committer: mgubi
  • Date: 2009-06-04 15:13:41 UTC
  • Revision ID: svn-v4:64cb5145-927a-446d-8aed-2fb7b4773692:trunk:2717
Support for X11 TeXmacs.app on Mac

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/******************************************************************************
3
 
* MODULE     : ball.hpp
4
 
* DESCRIPTION: balls with center in C and radius in R (= norm_type (C))
5
 
* COPYRIGHT  : (C) 2006  Joris van der Hoeven
6
 
*******************************************************************************
7
 
* This software falls under the GNU general public license version 3 or later.
8
 
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9
 
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10
 
******************************************************************************/
11
 
 
12
 
#ifndef BALL_H
13
 
#define BALL_H
14
 
#include "properties.hpp"
15
 
#include "operators.hpp"
16
 
#define TMPL template<typename C>
17
 
#define BINARY_TMPL template<typename C1,typename C2>
18
 
#define R typename properties<C>::norm_type
19
 
#define M typename binary_properties<C1,C2>::product_type
20
 
 
21
 
/******************************************************************************
22
 
* The ball class
23
 
******************************************************************************/
24
 
 
25
 
TMPL
26
 
class ball {
27
 
public:
28
 
  C cen;
29
 
  R rad;
30
 
public:
31
 
  inline ball (C c=0, R r=0): cen (c), rad (r) {}
32
 
};
33
 
 
34
 
TMPL inline C center (const ball<C>& b) { return b.cen; }
35
 
TMPL inline R radius (const ball<C>& b) { return b.rad; }
36
 
TMPL inline R upper (const ball<C>& b) { return norm (b.cen) + b.rad; }
37
 
TMPL inline R lower (const ball<C>& b) { return norm (b.cen) - b.rad; }
38
 
 
39
 
TMPL inline tree as_tree (const ball<C>& b) {
40
 
  return compound ("ball", as_tree (center (b)), as_tree (radius (b))); }
41
 
TMPL inline ostream& operator << (ostream& out, const ball<C>& b) {
42
 
  return out << as_math_string (as_tree (b)); }
43
 
 
44
 
TMPL
45
 
class properties<ball<C> > {
46
 
public:
47
 
  typedef ball<typename properties<C>::scalar_type> scalar_type;
48
 
  typedef typename properties<C>::norm_type norm_type;
49
 
  typedef typename properties<C>::index_type index_type;
50
 
  static inline tree index_name (index_type i) {
51
 
    return properties<C>::index_name (i); }
52
 
  static inline scalar_type access (ball<C> b, index_type var) {
53
 
    return scalar_type (properties<C>::access (center (b), var), radius (b)); }
54
 
};
55
 
 
56
 
BINARY_TMPL
57
 
class binary_properties<ball<C1>,ball<C2> > {
58
 
public:
59
 
  typedef ball<M > product_type;
60
 
};
61
 
 
62
 
/******************************************************************************
63
 
* Basic ball arithmetic
64
 
******************************************************************************/
65
 
 
66
 
TMPL ball<C>
67
 
operator - (const ball<C>& b) {
68
 
  return ball<C> (-center (b), radius (b));
69
 
}
70
 
 
71
 
TMPL ball<C>
72
 
operator + (const ball<C>& b1, const ball<C>& b2) {
73
 
  return ball<C> (center (b1) + center (b2), radius (b1) + radius (b2));
74
 
}
75
 
 
76
 
TMPL ball<C>
77
 
operator - (const ball<C>& b1, const ball<C>& b2) {
78
 
  return ball<C> (center (b1) - center (b2), radius (b1) + radius (b2));
79
 
}
80
 
 
81
 
BINARY_TMPL ball<M >
82
 
operator * (const ball<C1>& b1, const ball<C2>& b2) {
83
 
  return ball<M > (center (b1) * center (b2),
84
 
                   norm (center (b1)) * radius (b2) +
85
 
                   radius (b1) * norm (center (b2)) +
86
 
                   radius (b1) * radius (b2));
87
 
}
88
 
 
89
 
TMPL ball<C>
90
 
invert (const ball<C>& b) {
91
 
  return ball<C> (invert (center (b)), radius (b) / square (lower (b)));
92
 
}
93
 
 
94
 
BINARY_TMPL ball<M >
95
 
operator / (const ball<C1>& b1, const ball<C2>& b2) {
96
 
  return b1 * invert (b2);
97
 
}
98
 
 
99
 
/******************************************************************************
100
 
* Special functions
101
 
******************************************************************************/
102
 
 
103
 
TMPL ball<C>
104
 
sqrt (const ball<C>& b) {
105
 
  return ball<C> (sqrt (center (b)), radius (b) / (2 * square (loer (b))));
106
 
}
107
 
 
108
 
TMPL ball<C>
109
 
exp (const ball<C>& b) {
110
 
  return ball<C> (exp (center (b)), exp (upper (b)) * radius (b));
111
 
}
112
 
 
113
 
TMPL ball<C>
114
 
log (const ball<C>& b) {
115
 
  return ball<C> (log (center (b)), radius (b) / lower (b));
116
 
}
117
 
 
118
 
TMPL inline ball<C>
119
 
pow (const ball<C>& b1, const ball<C>& b2) {
120
 
  return exp (b2 * log (b1));
121
 
}
122
 
 
123
 
TMPL ball<C>
124
 
cos (const ball<C>& z) {
125
 
  if (radius (z) >= R (3.14159))
126
 
    return ball<C> (C(0), R(1));
127
 
  else {
128
 
    R u1= sin (lower (z));
129
 
    R u2= sin (upper (z));
130
 
    if (u1 * u2 <= 0)
131
 
      return ball<C> (cos (center (z)), radius (z));
132
 
    return ball<C> (cos (center (z)),
133
 
                    max (norm (u1), norm (u2)) * radius (z));
134
 
  }
135
 
}
136
 
 
137
 
TMPL ball<C>
138
 
sin (const ball<C>& z) {
139
 
  if (radius (z) >= R (3.14159))
140
 
    return ball<C> (sin (center (z)), radius (z));
141
 
  else {
142
 
    R u1= cos (lower (z));
143
 
    R u2= cos (upper (z));
144
 
    if (u1 * u2 <= 0)
145
 
      return ball<C> (sin (center (z)), radius (z));
146
 
    return ball<C> (sin (center (z)),
147
 
                    max (norm (u1), norm (u2)) * radius (z));
148
 
  }
149
 
}
150
 
 
151
 
TMPL inline ball<C>
152
 
tan (const ball<C>& b) {
153
 
  return sin (b) / cos (b);
154
 
}
155
 
 
156
 
/******************************************************************************
157
 
* Other routines
158
 
******************************************************************************/
159
 
 
160
 
TMPL ball<R>
161
 
norm (const ball<C>& b) {
162
 
  return ball<R> (norm (center (b)), radius (b));
163
 
}
164
 
 
165
 
#undef TMPL
166
 
#undef BINARY_TMPL
167
 
#undef R
168
 
#undef M
169
 
#endif // defined BALL_H