r97: Converted Scanner and Projections to C++
[ctsim.git] / include / kmath.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **    Name:          kmath.h
5 **    Purpose:       Header file containing definitions for numerical app
6 **    Programmer:    Kevin Rosenberg
7 **    Date Started:  Nov 84
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: kmath.h,v 1.14 2000/06/17 20:12:14 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #ifndef _H_kmath
29 #define _H_kmath
30
31 #include <stdio.h>
32 #include <math.h>
33
34 #define PI      3.14159265358979323846
35 #define HALFPI  1.57079632679489661923  /* PI divided by 2 */
36 #define QUARTPI 0.78539816339744830962  /* PI divided by 4 */
37 #define I_PI    0.31830988618379067154  /* Inverse of PI */
38 #define I_PID2  0.63661977236758134308  /* Inverse of PID2 */
39  
40 #define TWOPI   6.28318530717958647692
41 #define SQRT2   1.414213562373095049
42
43 #define F_EPSILON       1.0E-6
44 #define D_EPSILON       1.0E-10
45
46 #define ASSUMEDZERO  1E-10
47
48 typedef double GRFMTX_2D[3][3];
49 typedef double GRFMTX_3D[4][4];
50
51 inline double 
52 convertDegreesToRadians (double x)
53 { return (x * (PI/180.)); }
54
55 inline double
56 convertRadiansToDegrees (double x)
57 { return (x*(180./PI)); }
58
59 template<class T>
60 inline T nearest (double x)
61 { return (x > 0 ? static_cast<T>(x+0.5) : static_cast<T>(x-0.5)); }
62
63 template<class T>
64 inline T clamp (T value, T lowerBounds, T upperBounds)
65 { return (value >= upperBounds ? upperBounds : (value <= lowerBounds ? lowerBounds : value )); }
66
67 template<class T>
68 inline T lineLength (T x1, T y1, T x2, T y2)
69 { return static_cast<T>( sqrt ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) ); }
70
71 /* clip.cpp */
72 int clip_rect(double *x1, double *y1, double *x2, double *y2, const double rect[4]);
73 int clip_segment(double *x1, double *y1, double *x2, double *y2, const double u, const double v);
74 int clip_sector(double *x1, double *y1, double *x2, double *y2, const double u, const double v);
75 int clip_circle(double *x1, double *y1, double *x2, double *y2, const double cx, const double cy, const double radius, double t1, double t2);
76 int clip_triangle(double *x1, double *y1, double *x2, double *y2, const double u, const double v, const int clip_xaxis);
77
78 /* norm_ang.cpp */
79 double norm_ang(double theta);
80
81 /* xform.cpp */
82 void indent_mtx2(GRFMTX_2D m);
83 void xlat_mtx2(GRFMTX_2D m, const double x, const double y);
84 void scale_mtx2(GRFMTX_2D m, const double sx, const double sy);
85 void rot_mtx2(GRFMTX_2D m, const double theta);
86 void mult_mtx2(GRFMTX_2D m1, GRFMTX_2D m2, GRFMTX_2D result);
87 void xform_mtx2(GRFMTX_2D m, double *x, double *y);
88 void rotate2d(double x[], double y[], int pts, double angle);
89 void xlat2d(double x[], double y[], int pts, double xoffset, double yoffset);
90 void scale2d(double x[], double y[], int pts, double xfact, double yfact);
91
92 /* simpson.cpp */
93 double simpson(const double xmin, const double xmax, const double *y, const int np);
94
95 /* minmax.cpp */
96 void minmax_dvector(const double array[], const int pts, double *xmin, double *xmax);
97
98
99 #endif