1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/*****
* triple.h
* John Bowman
*
* Stores a three-dimensional point.
*
*****/
#ifndef TRIPLE_H
#define TRIPLE_H
#include <cassert>
#include <iostream>
#include <cmath>
#include "common.h"
#include "angle.h"
namespace camp {
typedef double Triple[3];
class triple : virtual public gc {
double x;
double y;
double z;
public:
triple() : x(0.0), y(0.0), z(0.0) {}
triple(double x, double y=0.0, double z=0.0) : x(x), y(y), z(z) {}
triple(const Triple& v) : x(v[0]), y(v[1]), z(v[2]) {}
virtual ~triple() {}
double getx() const { return x; }
double gety() const { return y; }
double getz() const { return z; }
friend triple operator+ (const triple& z, const triple& w)
{
return triple(z.x + w.x, z.y + w.y, z.z + w.z);
}
friend triple operator- (const triple& z, const triple& w)
{
return triple(z.x - w.x, z.y - w.y, z.z - w.z);
}
friend triple operator- (const triple& z)
{
return triple(-z.x, -z.y, -z.z);
}
friend triple operator* (double s, const triple& z)
{
return triple(s*z.x, s*z.y, s*z.z);
}
friend triple operator* (const triple& z, double s)
{
return triple(z.x*s, z.y*s, z.z*s);
}
friend triple operator/ (const triple& z, double s)
{
if (s == 0.0)
reportError("division by 0");
s=1.0/s;
return triple(z.x*s, z.y*s, z.z*s);
}
const triple& operator+= (const triple& w)
{
x += w.x;
y += w.y;
z += w.z;
return *this;
}
const triple& operator-= (const triple& w)
{
x -= w.x;
y -= w.y;
z -= w.z;
return *this;
}
friend bool operator== (const triple& z, const triple& w)
{
return z.x == w.x && z.y == w.y && z.z == w.z;
}
friend bool operator!= (const triple& z, const triple& w)
{
return z.x != w.x || z.y != w.y || z.z != w.z;
}
double abs2() const
{
return x*x+y*y+z*z;
}
double length() const /* r */
{
return sqrt(abs2());
}
friend double length(const triple& v)
{
return v.length();
}
double polar() const /* theta */
{
double r=length();
if (r == 0.0)
reportError("taking polar angle of (0,0,0)");
return acos(z/r);
}
double azimuth() const /* phi */
{
return angle(x,y);
}
friend triple unit(const triple& v)
{
double scale=v.length();
if(scale != 0.0) scale=1.0/scale;
return triple(v.x*scale,v.y*scale,v.z*scale);
}
friend double dot(const triple& u, const triple& v)
{
return u.x*v.x+u.y*v.y+u.z*v.z;
}
friend triple cross(const triple& u, const triple& v)
{
return triple(u.y*v.z-u.z*v.y,
u.z*v.x-u.x*v.z,
u.x*v.y-v.x*u.y);
}
// Returns a unit triple in the direction (theta,phi), in radians.
friend triple expi(double theta, double phi)
{
double sintheta=sin(theta);
return triple(sintheta*cos(phi),sintheta*sin(phi),cos(theta));
}
friend istream& operator >> (istream& s, triple& z)
{
char c;
s >> std::ws;
bool paren=s.peek() == '('; // parenthesis are optional
if(paren) s >> c;
s >> z.x >> std::ws;
if(s.peek() == ',') s >> c >> z.y;
else z.y=0.0;
if(s.peek() == ',') s >> c >> z.z;
else z.z=0.0;
if(paren) {
s >> std::ws;
if(s.peek() == ')') s >> c;
}
return s;
}
friend ostream& operator << (ostream& out, const triple& z)
{
out << "(" << z.x << "," << z.y << "," << z.z << ")";
return out;
}
};
triple expi(double theta, double phi);
// Return the component of vector v perpendicular to a unit vector u.
inline triple perp(triple v, triple u)
{
return v-dot(v,u)*u;
}
} //namespace camp
GC_DECLARE_PTRFREE(camp::triple);
#endif
|