SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MiscMath.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 
31 #ifndef CORE_MATH_MISCMATH_H
32 #define CORE_MATH_MISCMATH_H 1
33 
34 #include <cmath>
35 #include <limits>
37 #include <boost/math/special_functions/fpclassify.hpp>
38 #include <Core/Math/share.h>
39 
40 #include <boost/test/floating_point_comparison.hpp>
41 namespace btt=boost::test_tools;
42 
43 #ifndef M_PI
44 #define M_PI 3.14159265358979323846
45 #endif
46 
47 // Define missing Windows function
48 #ifdef _WIN32
49 inline double acosh(double x)
50 {
51  return (x<1) ? log(-1.0) : log(x+sqrt(x*x-1));
52 }
53 #endif
54 
55 namespace SCIRun {
56 
57 template<typename T>
58 inline bool nonzero(T d)
59 {
60  btt::close_at_tolerance<T> comp(btt::percent_tolerance(std::numeric_limits<T>::epsilon()));
61  return(! comp(d, 0));
62 }
63 
64 /// Faster versions of several math functions
65 
66 //------------------------------------------
67 // Min/Max functions
68 //
69 // 2 unsigned long long values
70 inline unsigned long long Min(unsigned long long d1, unsigned long long d2)
71 {
72  return d1<d2?d1:d2;
73 }
74 
75 inline unsigned long long Max(unsigned long long d1, unsigned long long d2)
76 {
77  return d1>d2?d1:d2;
78 }
79 
80 // 2 Integers
81 inline int Min(int d1, int d2)
82 {
83  return d1<d2?d1:d2;
84 }
85 
86 inline int Max(int d1, int d2)
87 {
88  return d1>d2?d1:d2;
89 }
90 
91 // 2 Unsigned Integers
92 inline unsigned int Min(unsigned int d1, unsigned int d2)
93 {
94  return d1<d2?d1:d2;
95 }
96 
97 inline unsigned int Max(unsigned int d1, unsigned int d2)
98 {
99  return d1>d2?d1:d2;
100 }
101 
102 // 2 Long Integers
103 inline long Min(long d1, long d2)
104 {
105  return d1<d2?d1:d2;
106 }
107 
108 inline long Max(long d1, long d2)
109 {
110  return d1>d2?d1:d2;
111 }
112 
113 
114 // 2 Long Long Integers
115 inline long long Min(long long d1, long long d2)
116 {
117  return d1<d2?d1:d2;
118 }
119 
120 inline long long Max(long long d1, long long d2)
121 {
122  return d1>d2?d1:d2;
123 }
124 
125 // 2 floats
126 inline float Max(float d1, float d2)
127 {
128  return d1>d2?d1:d2;
129 }
130 
131 inline float Min(float d1, float d2)
132 {
133  return d1<d2?d1:d2;
134 }
135 
136 // 2 doubles
137 inline double Max(double d1, double d2)
138 {
139  return d1>d2?d1:d2;
140 }
141 
142 inline double Min(double d1, double d2)
143 {
144  return d1<d2?d1:d2;
145 }
146 
147 // 3 unsigned long long values
148 inline unsigned long long Min(unsigned long long d1, unsigned long long d2, unsigned long long d3)
149 {
150  unsigned long long m=d1<d2?d1:d2;
151  m=m<d3?m:d3;
152  return m;
153 }
154 
155 inline unsigned long long Mid(unsigned long long a, unsigned long long b, unsigned long long c)
156 {
157  return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) : \
158  ((b < c) ? b : ((a > c) ? a : c)));
159 }
160 
161 inline unsigned long long Max(unsigned long long d1, unsigned long long d2, unsigned long long d3)
162 {
163  unsigned long long m=d1>d2?d1:d2;
164  m=m>d3?m:d3;
165  return m;
166 }
167 
168 // 3 doubles
169 inline double Min(double d1, double d2, double d3)
170 {
171  double m=d1<d2?d1:d2;
172  m=m<d3?m:d3;
173  return m;
174 }
175 
176 inline double Mid(double a, double b, double c)
177 {
178  return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) :
179  ((b < c) ? b : ((a > c) ? a : c)));
180 }
181 
182 inline double Max(double d1, double d2, double d3)
183 {
184  double m=d1>d2?d1:d2;
185  m=m>d3?m:d3;
186  return m;
187 }
188 
189 // 3 integers
190 inline int Min(int d1, int d2, int d3)
191 {
192  int m=d1<d2?d1:d2;
193  m=m<d3?m:d3;
194  return m;
195 }
196 
197 inline int Mid(int a, int b, int c)
198 {
199  return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) :
200  ((b < c) ? b : ((a > c) ? a : c)));
201 }
202 
203 inline int Max(int d1, int d2, int d3)
204 {
205  int m=d1>d2?d1:d2;
206  m=m>d3?m:d3;
207  return m;
208 }
209 
210 // 3 unsigned integers
211 inline unsigned int Min(unsigned int d1,
212  unsigned int d2,
213  unsigned int d3)
214 {
215  unsigned int m=d1<d2?d1:d2;
216  m=m<d3?m:d3;
217  return m;
218 }
219 
220 inline unsigned int Mid(unsigned int a,
221  unsigned int b,
222  unsigned int c)
223 {
224  return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) :
225  ((b < c) ? b : ((a > c) ? a : c)));
226 }
227 
228 inline unsigned int Max(unsigned int d1,
229  unsigned int d2,
230  unsigned int d3)
231 {
232  unsigned int m=d1>d2?d1:d2;
233  m=m>d3?m:d3;
234  return m;
235 }
236 
237 // 3 Long Integers
238 inline long Min(long d1, long d2, long d3)
239 {
240  long m=d1<d2?d1:d2;
241  m=m<d3?m:d3;
242  return m;
243 }
244 
245 inline long Mid(long a, long b, long c)
246 {
247  return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) :
248  ((b < c) ? b : ((a > c) ? a : c)));
249 }
250 
251 
252 inline long Max(long d1, long d2, long d3)
253 {
254  long m=d1>d2?d1:d2;
255  m=m>d3?m:d3;
256  return m;
257 }
258 
259 // 3 Long Integers
260 inline long long Min(long long d1, long long d2, long long d3)
261 {
262  long long m=d1<d2?d1:d2;
263  m=m<d3?m:d3;
264  return m;
265 }
266 
267 inline long long Mid(long long a, long long b, long long c)
268 {
269  return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) :
270  ((b < c) ? b : ((a > c) ? a : c)));
271 }
272 
273 
274 inline long long Max(long long d1, long long d2, long long d3)
275 {
276  long long m=d1>d2?d1:d2;
277  m=m>d3?m:d3;
278  return m;
279 }
280 
281 //------------------------------------------
282 // Power function
283 
284 inline double Pow(double d, double p)
285 {
286  return pow(d,p);
287 }
288 
289 inline double Pow(double x, unsigned int p)
290 {
291  double result=1;
292  while(p)
293  {
294  if(p&1) result*=x;
295  x*=x;
296  p>>=1;
297  }
298  return result;
299 }
300 
301 inline double Pow(double x, int p)
302 {
303  if(p < 0)
304  {
305  p=-p;
306  double result=1;
307  while(p)
308  {
309  if(p&1)
310  result*=x;
311  x*=x;
312  p>>=1;
313  }
314  return 1./result;
315  }
316  else
317  {
318  double result=1;
319  while(p){
320  if(p&1)
321  result*=x;
322  x*=x;
323  p>>=1;
324  }
325  return result;
326  }
327 }
328 
329 //------------------------------------------
330 // Sqrt and Cbrt function
331 
332 inline int Sqrt(int i)
333 {
334  return static_cast<int>(sqrt(static_cast<double>(i)));
335 }
336 
337 inline double Sqrt(double d)
338 {
339  return sqrt(d);
340 }
341 
342 inline float Sqrt(float d)
343 {
344  return sqrtf(d);
345 }
346 
347 inline double Cbrt(double d)
348 {
349  return pow(d, 1.0/3.0);
350 }
351 
352 inline float Cbrt(float d)
353 {
354  return static_cast<float>(pow(static_cast<double>(d), 1.0/3.0));
355 }
356 
357 inline double Sqr(double x)
358 {
359  return x*x;
360 }
361 
362 inline float Sqr(float x)
363 {
364  return x*x;
365 }
366 
367 //------------------------------------------
368 // Absolute function
369 inline double Abs(double d)
370 {
371  return d<0?-d:d;
372 }
373 
374 inline float Abs(float f)
375 {
376  return f<0?-f:f;
377 }
378 
379 inline int Abs(int i)
380 {
381  return i<0?-i:i;
382 }
383 
384 //-----------------------------------------
385 // Sign function
386 inline int Sign(double d)
387 {
388  return d<0.0?-1:1;
389 }
390 
391 inline int Sign(float f)
392 {
393  return f<0.0?-1:1;
394 }
395 
396 inline int Sign(int i)
397 {
398  return i<0?-1:1;
399 }
400 
401 
402 //-----------------------------------------
403 // Clamp function
404 inline double Clamp(double d, double min, double max)
405 {
406  return d<=min?min:d>=max?max:d;
407 }
408 
409 inline float Clamp(float f, float min, float max)
410 {
411  return f<=min?min:f>=max?max:f;
412 }
413 
414 inline int Clamp(int i, int min, int max)
415 {
416  return i<min?min:i>max?max:i;
417 }
418 
419 //-----------------------------------------
420 // Interpolation function
421 template <class T>
422 inline T Interpolate(T d1, T d2, double weight)
423 {
424  return T(d2*weight+d1*(1.0-weight));
425 }
426 
427 template <class T>
428 inline T Interpolate(T d1, T d2, float weight)
429 {
430  return T(d2*weight+d1*(1-weight));
431 }
432 
433 inline int Round(double d)
434 {
435  return static_cast<int>(d+0.5);
436 }
437 
438 inline int Round(float f)
439 {
440  return static_cast<int>(f+0.5);
441 }
442 
443 inline int Floor(double d)
444 {
445  if(d<0)
446  {
447  int i=-static_cast<int>(-d);
448  if(static_cast<double>(i) == d)
449  return i;
450  else
451  return i-1;
452  }
453  else
454  {
455  return (static_cast<int>(d));
456  }
457 }
458 
459 inline int Floor(float f)
460 {
461  if(f<0)
462  {
463  int i=-static_cast<int>(-f);
464  if(static_cast<float>(i) == f)
465  return (i);
466  else
467  return (i-1);
468  }
469  else
470  {
471  return (static_cast<int>(f));
472  }
473 }
474 
475 // this version is twice as fast for negative numbers
476 // (on an available Pentium 4 machine, only about 25%
477 // faster on Powerbook G4)
478 // than the more robust version above, but will only
479 //work for fabs(d) <= offset, use with caution
480 inline int Floor(double d, int offset)
481 {
482  return (static_cast<int>((d + offset) - offset));
483 }
484 
485 inline int Floor(float f, int offset)
486 {
487  return (static_cast<int>((f + offset) - offset));
488 }
489 
490 inline int Ceil(double d)
491 {
492  if(d<0)
493  {
494  int i=-static_cast<int>(-d);
495  return (i);
496  }
497  else
498  {
499  int i=static_cast<int>(d);
500  if(static_cast<double>(i) == d)
501  return (i);
502  else
503  return (i+1);
504  }
505 }
506 
507 inline int Ceil(float f)
508 {
509  if(f<0)
510  {
511  int i=-static_cast<int>(-f);
512  return (i);
513  }
514  else
515  {
516  int i=static_cast<int>(f);
517  if(static_cast<float>(i) == f)
518  return (i);
519  else
520  return (i+1);
521  }
522 }
523 
524 // using the same trick as above, this
525 // version of Ceil is a bit faster for
526 // the architectures it has been tested
527 // on (Pentium4, Powerbook G4)
528 inline int Ceil(double d, int offset)
529 {
530  return (static_cast<int>((d - offset) + offset));
531 }
532 
533 inline int Ceil(float f, int offset)
534 {
535  return (static_cast<int>((f - offset) + offset));
536 }
537 
538 inline bool IsNan(double val)
539 {
540  return boost::math::isnan(val);
541 }
542 
543 inline bool IsFinite(double val)
544 {
545  return boost::math::isfinite(val);
546 }
547 
548 inline bool IsInfinite(double val)
549 {
550  return boost::math::isinf(val);
551 }
552 
553 // Fast way to check for power of two
554 inline bool IsPowerOf2(unsigned int n)
555 {
556  return ((n & (n-1)) == 0);
557 }
558 
559 inline bool is_integral_value(double x)
560 {
561  return std::numeric_limits<SCIRun::size_type>::min() <= x &&
562  x <= std::numeric_limits<SCIRun::size_type>::max() &&
563  x == static_cast<SCIRun::size_type>(x);
564 }
565 
566 // Returns a number Greater Than or Equal to dim
567 // that is an exact power of 2
568 // Used for determining what size of texture to
569 // allocate to store an image
570 inline unsigned int
571 Pow2(const unsigned int dim)
572 {
573  if (IsPowerOf2(dim)) return dim;
574  unsigned int val = 4;
575  while (val < dim) val = val << 1;;
576  return val;
577 }
578 
579 // Returns a number Less Than or Equal to dim
580 // that is an exact power of 2
581 inline unsigned int
582 LargestPowerOf2(const unsigned int dim)
583 {
584  if (IsPowerOf2(dim)) return dim;
585  return Pow2(dim) >> 1;
586 }
587 
588 // Returns the power of 2 of the next higher number that is a power of 2
589 // Log2 of Pow2 function above
590 inline unsigned int
591 Log2(const unsigned int dim)
592 {
593  unsigned int log = 0;
594  unsigned int val = 1;
595  while (val < dim) { val = val << 1; log++; };
596  return log;
597 }
598 
599 // Takes the square root of "value" and tries to find two factors that
600 // are closest to that square root.
601 void findFactorsNearRoot(const int value, int& factor1, int& factor2);
602 
603 inline double Cot(double d)
604 {
605  return 1./tan(d);
606 }
607 
608 inline double DtoR(double d)
609 {
610  return d*(M_PI/180.);
611 }
612 
613 inline double RtoD(double r)
614 {
615  return r*(180./M_PI);
616 }
617 
618 inline float DtoR(float d)
619 {
620  return d*static_cast<float>(M_PI/180.0);
621 }
622 
623 inline float RtoD(float r)
624 {
625  return r*static_cast<float>(180.0/M_PI);
626 }
627 
628 } // End namespace SCIRun
629 
630 
631 #endif
int Sign(double d)
Definition: MiscMath.h:386
int Ceil(double d)
Definition: MiscMath.h:490
bool IsNan(double val)
Definition: MiscMath.h:538
double Abs(double d)
Definition: MiscMath.h:369
bool IsPowerOf2(unsigned int n)
Definition: MiscMath.h:554
unsigned int Log2(const unsigned int dim)
Definition: MiscMath.h:591
double RtoD(double r)
Definition: MiscMath.h:613
double Clamp(double d, double min, double max)
Definition: MiscMath.h:404
double Sqr(double x)
Definition: MiscMath.h:357
double Cot(double d)
Definition: MiscMath.h:603
bool IsInfinite(double val)
Definition: MiscMath.h:548
double Pow(double d, double p)
Definition: MiscMath.h:284
bool nonzero(T d)
Definition: MiscMath.h:58
int Round(double d)
Definition: MiscMath.h:433
Definition: ParallelLinearAlgebraTests.cc:358
int Sqrt(int i)
Definition: MiscMath.h:332
void findFactorsNearRoot(const int value, int &factor1, int &factor2)
Definition: MiscMath.cc:51
unsigned long long Mid(unsigned long long a, unsigned long long b, unsigned long long c)
Definition: MiscMath.h:155
double Cbrt(double d)
Definition: MiscMath.h:347
double DtoR(double d)
Definition: MiscMath.h:608
#define M_PI
Definition: MiscMath.h:44
unsigned int Pow2(const unsigned int dim)
Definition: MiscMath.h:571
bool IsFinite(double val)
Definition: MiscMath.h:543
unsigned long long Max(unsigned long long d1, unsigned long long d2)
Definition: MiscMath.h:75
T Interpolate(T d1, T d2, double weight)
Definition: MiscMath.h:422
unsigned long long Min(unsigned long long d1, unsigned long long d2)
Faster versions of several math functions.
Definition: MiscMath.h:70
int n
Definition: eab.py:9
unsigned int LargestPowerOf2(const unsigned int dim)
Definition: MiscMath.h:582
int Floor(double d)
Definition: MiscMath.h:443
bool is_integral_value(double x)
Definition: MiscMath.h:559