GOAT (Geometrical optics application tool) 0.1
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1/*****************************************************************/
9
10#pragma once
11#include <ostream>
12#include <stdlib.h>
13#include <stdio.h>
14#include <complex>
15#include <fstream>
16#ifdef _WIN32
17#define _USE_MATH_DEFINES
18#endif
19#include<math.h>
20#include<iostream>
21#include "goodies.h"
22namespace GOAT
23{
24 namespace maths
25 {
26 double arctan(double y, double x);
27 std::complex<double> tan(std::complex<double> z);
28
29 inline std::complex<double> operator + (std::complex<double> x, int y) { return x + (double)y; }
30 inline std::complex<double> operator + (int y, std::complex<double> x) { return (double)y + x; }
31 inline std::complex<double> operator - (std::complex<double> x, int y) { return x - (double)y; }
32 inline std::complex<double> operator - (int y, std::complex<double> x) { return (double)y - x; }
33
34 inline std::complex<double> operator * (std::complex<double> x, int y) { return x * (double)y; }
35 inline std::complex<double> operator * (int y, std::complex<double> x) { return (double)y * x; }
36 inline std::complex<double> operator / (std::complex<double> x, int y) { return x / (double)y; }
37 inline std::complex<double> operator / (int y, std::complex<double> x) { return (double)y / x; }
38
39 inline std::complex<double> operator + (std::complex<double> x, long long int y) { return x + (double)y; }
40 inline std::complex<double> operator + (long long int y, std::complex<double> x) { return (double)y + x; }
41 inline std::complex<double> operator - (std::complex<double> x, long long int y) { return x - (double)y; }
42 inline std::complex<double> operator - (long long int y, std::complex<double> x) { return (double)y - x; }
43
44 inline std::complex<double> operator * (std::complex<double> x, long long int y) { return x * (double)y; }
45 inline std::complex<double> operator * (long long int y, std::complex<double> x) { return (double)y * x; }
46 inline std::complex<double> operator / (std::complex<double> x, long long int y) { return x / (double)y; }
47 inline std::complex<double> operator / (long long int y, std::complex<double> x) { return (double)y / x; }
48
49
55 template <class T>
56 class Vector
57 {
58 public:
59
60
61 void binWrite(std::ofstream& os)
62 {
63 for (int i = 0; i < 3; i++)
64 os.write((char*)&data[i], (char)sizeof(data[i]));
65 }
66
67
68 void binRead(std::ifstream& is)
69 {
70 for (int i = 0; i < 3; i++)
71 {
72 is.read((char*)&data[i], (char)sizeof(data[i]));
73 }
74 }
75
78 {
79 data[0] = 0;
80 data[1] = 0;
81 data[2] = 0;
82 }
83
91 Vector(T x, T y, T z)
92 {
93 data[0] = x;
94 data[1] = y;
95 data[2] = z;
96 }
97
99 inline Vector(const Vector& r)
100 {
101 for (int i = 0; i < 3; i++)
102 data[i] = r.data[i];
103 }
104
112 inline friend Vector operator + (const Vector& r1, const Vector& r2)
113 {
114 Vector<T> h;
115 h.data[0] = r1.data[0] + r2.data[0];
116 h.data[1] = r1.data[1] + r2.data[1];
117 h.data[2] = r1.data[2] + r2.data[2];
118 return h;
119 }
120
123 {
124 Vector Erg;
125 for (int i = 0; i < 3; i++)
126 Erg.data[i] = -data[i];
127 return Erg;
128 }
129
137 inline friend Vector operator - (const Vector& r1, const Vector& r2)
138 {
139 Vector<T> h;
140 h.data[0] = r1.data[0] - r2.data[0];
141 h.data[1] = r1.data[1] - r2.data[1];
142 h.data[2] = r1.data[2] - r2.data[2];
143 return h;
144 }
145
146
154 inline friend Vector operator % (const Vector& r1, const Vector& r2)
155 {
156 Vector<T> h;
157 h.data[0] = r1.data[1] * r2.data[2] - r1.data[2] * r2.data[1];
158 h.data[1] = r1.data[2] * r2.data[0] - r1.data[0] * r2.data[2];
159 h.data[2] = r1.data[0] * r2.data[1] - r1.data[1] * r2.data[0];
160 return h;
161 }
162
167 inline friend Vector sqrt(const Vector& r)
168 {
169 return Vector<T>(sqrt(r.data[0]), sqrt(r.data[1]), sqrt(r.data[2]));
170 }
171
175 inline friend Vector exp(const Vector& r)
176 {
177 return Vector<T>(exp(r.data[0]), exp(r.data[1]), exp(r.data[2]));
178 }
179
181 inline friend Vector emult(const Vector& r1, const Vector& r2)
182 {
183 Vector<T> Erg;
184 for (int i = 0; i < 3; i++)
185 Erg.data[i] = r1.data[i] * r2.data[i];
186 return Erg;
187 }
188
190 inline friend Vector ediv(const Vector& r1, const Vector& r2)
191 {
192 Vector<T> Erg;
193 for (int i = 0; i < 3; i++)
194 Erg.data[i] = r1.data[i] / r2.data[i];
195 return Erg;
196 }
197
205 inline friend Vector ediv(T s, const Vector& r)
206 {
207 Vector<T> Erg;
208 for (int i = 0; i < 3; i++)
209 Erg.data[i] = s / r.data[i];
210 return Erg;
211 }
212
213
216 {
217 // if (&r == NULL) return *this;
218 if (this == &r) return *this;
219 for (int i = 0; i < 3; i++)
220 data[i] = r.data[i];
221 return *this;
222 }
223
226 {
227 for (int i = 0; i < 3; i++)
228 data[i] = r[i];
229 return *this;
230 }
231
233 bool operator == (const Vector& r)
234 {
235 bool Erg;
236 Erg = ((data[0] == r.data[0]) && (data[1] == r.data[1]) && (data[2] == r.data[2]));
237 return Erg;
238 }
239
241 bool operator != (const Vector& r)
242 {
243 bool Erg;
244 Erg = ((data[0] != r.data[0]) || (data[1] != r.data[1]) || (data[2] != r.data[2]));
245 return Erg;
246 }
247
250 {
251 for (int i = 0; i < 3; i++)
252 data[i] = data[i] + r.data[i];
253 return *this;
254 }
255
258 {
259 for (int i = 0; i < 3; i++)
260 data[i] = data[i] - r.data[i];
261 return *this;
262 }
263
266 {
267 for (int i = 0; i < 3; i++)
268 data[i] = data[i] * r;
269 return *this;
270 }
271
274 {
275 for (int i = 0; i < 3; i++)
276 data[i] = data[i] / r;
277 return *this;
278 }
279
280
281 inline friend bool operator == (const Vector& A, const Vector& B)
282 {
283 return ((A[0] == B[0]) && (A[1] == B[1]) && (A[2] == B[2]));
284 }
285
286 inline friend bool operator != (const Vector& A, const Vector& B)
287 {
288 return !((A[0] == B[0]) && (A[1] == B[1]) && (A[2] == B[2]));
289 }
290
291
292
293 inline friend T operator * (const Vector& a, const Vector& b)
294 {
295 T h;
296 h = 0;
297 for (int i = 0; i < 3; i++)
298 h += a[i] * b[i];
299 return h;
300 }
301
309 friend Vector operator * (T x, const Vector& r)
310 {
311 Vector<T> h;
312 for (int i = 0; i < 3; i++)
313 h.data[i] = r.data[i] * x;
314 return h;
315 }
316
324 friend Vector operator * (const Vector& r, T x)
325 {
326 Vector<T> h;
327 for (int i = 0; i < 3; i++)
328 h.data[i] = r.data[i] * x;
329 return h;
330 }
331
332
340 friend Vector operator / (const Vector& r, T x)
341 {
342 Vector<T> Erg;
343 for (int i = 0; i < 3; i++)
344 Erg.data[i] = r.data[i] / x;
345 return Erg;
346 }
347
348
354 T& operator[] (int i) { return data[i]; };
355
361 const T& operator[ ] (int i) const { return data[i]; };
362 T data[3];
363 //int Dim;
364 };
365
366
373 template <class T> Vector<T> cart2sphere(const Vector<T>& x)
374 {
375 Vector<T> y;
376
377 x[0];
378 x[1];
379 x[2];
380
381 // calculate r
382 y[0] = abs(x);
383
384 // r==0 => phi=0, theta=0
385 if (y[0]==0)
386 {
387 y[1]=0;
388 y[2]=0;
389 return y;
390 }
391
392 // calculate phi=atan2 (y,x); x==0 => set phi to 0
393 if (x[0]==0) y[2]=0;
394 else
395 {
396 y[2]=atan2(x[1],x[0]);
397 if(y[2]<0) y[2]+=2.0 * M_PI;
398 }
399
400 // calculate theta=acos(z/r)
401 y[1]=acos (x[2]/y[0]);
402
403 return y;
404
405 /*if (a[2] == 0.0)
406 if (y[0] == 0.0)
407 y[1] = 0;
408 else
409 y[1] = M_PI / 2.0;
410 else
411 {
412 y[1] = atan(std::sqrt(a[0] * a[0] + a[1] * a[1]) / a[2]);
413 if (a[2] < 0.0)
414 y[1] = M_PI + y[1];
415 }
416 if (a[0] == 0.0)
417 if (a[1] == 0.0)
418 y[2] = 0.0;
419 else
420 if (a[1] > 0.0)
421 y[2] = M_PI / 2.0;
422 else
423 y[2] = 3.0 * M_PI / 2.0;
424 else
425 {
426 y[2] = atan(a[1] / a[0]);
427 if (a[0] < 0.0)
428 y[2] = M_PI + y[2];
429 }
430
431 return y;
432 */
433
434
435 }
436
437
438
445 template <class T> Vector<T> sphere2cart(const Vector<T>& P)
446 {
447
448
449 Vector<T> Erg;
450 Erg[0] = P[0] * sin(P[1]) * cos(P[2]);
451 Erg[1] = P[0] * sin(P[1]) * sin(P[2]);
452 Erg[2] = P[0] * cos(P[1]);
453 return Erg;
454 }
455
456
463 std::istream& operator >> (std::istream& is, Vector<std::complex<double> >& r);
464 std::istream& operator >> (std::istream& is, Vector<double>& r);
465 std::istream& operator >> (std::istream& is, Vector<int>& r);
466
467 std::ostream& operator << (std::ostream& is, const Vector<std::complex<double> >& r);
468 std::ostream& operator << (std::ostream& is, const Vector<double>& r);
469 std::ostream& operator << (std::ostream& is, const Vector<int>& r);
470
471 void errmsg(char* S);
472 double abs(const Vector<double>& r);
473 double abs(const Vector<std::complex<double> >& r);
474 double abs(const Vector<int>& r);
475 double abs(const Vector<long long int>& r);
476 double abs2(const Vector<double>& r);
477 double abs2(const Vector<std::complex<double> >& r);
478 double abs2(std::complex<double> x);
479 double abs2(double x);
480
491
492
493
494 Vector<std::complex<double> > operator - (const Vector<double>& r1, const Vector<std::complex<double> >& r2);
495 Vector<std::complex<double> > operator - (const Vector<std::complex<double> >& r1, const Vector<double>& r2);
496 Vector<std::complex<double> > operator - (const Vector<std::complex<double> >& r1, const Vector<int>& r2);
497 Vector<std::complex<double> > operator - (const Vector<int>& r1, const Vector<std::complex<double> >& r2);
498 Vector<std::complex<double> > operator - (const Vector<std::complex<double> >& r1, const Vector<long long int>& r2);
499 Vector<std::complex<double> > operator - (const Vector<long long int>& r1, const Vector<std::complex<double> >& r2);
500
501 Vector<std::complex<double> > operator + (const Vector<double>& r1, const Vector<std::complex<double> >& r2);
502 Vector<std::complex<double> > operator + (const Vector<std::complex<double> >& r1, const Vector<double>& r2);
505 Vector<std::complex<double> > operator + (const Vector<std::complex<double> >& r1, const Vector<int>& r2);
506 Vector<std::complex<double> > operator + (const Vector<int>& r1, const Vector<std::complex<double> >& r2);
507
510 Vector<std::complex<double> > operator + (const Vector<std::complex<double> >& r1, const Vector<long long int>& r2);
511 Vector<std::complex<double> > operator + (const Vector<long long int>& r1, const Vector<std::complex<double> >& r2);
512
517
518 Vector<double> operator * (long long int x, const Vector<double>& r);
519 Vector<double> operator * (const Vector<double>& r, long long int x);
522
523 Vector<std::complex<double> > operator * (std::complex<double> x, const Vector<double>& r);
524 Vector<std::complex<double> > operator * (const Vector<double>& r, std::complex<double> x);
525 Vector<std::complex<double> > operator * (std::complex<double> x, const Vector<int>& r);
526 Vector<std::complex<double> > operator * (const Vector<int>& r, std::complex<double> x);
529
530
531 Vector<std::complex<double> > operator / (const Vector<double>& r, std::complex<double> x);
533 Vector<double> operator / (const Vector<double>& r, long long int x);
534
535 Vector<std::complex<double> > operator / (const Vector<std::complex<double> >& r, double x);
536 Vector<std::complex<double> > operator / (const Vector<std::complex<double> >& r, int x);
537 Vector<std::complex<double> > operator / (const Vector<std::complex<double> >& r, long long int x);
540 Vector<std::complex<double> > operator / (const Vector<int>& r, std::complex<double> x);
542
543
544 std::complex<double> operator * (const Vector<double>& a, const Vector<std::complex<double> >& b);
545 std::complex<double> operator * (const Vector<std::complex<double> >& a, const Vector<double>& b);
546 double operator * (const Vector<double>& a, const Vector<int>& b);
547 double operator * (const Vector<int>& a, const Vector<double>& b);
548 std::complex<double> operator * (const Vector<std::complex<double> >& a, const Vector<int>& b);
549 std::complex<double> operator * (const Vector<int>& a, const Vector<std::complex<double> >& b);
550
551 Vector<std::complex<double> > operator % (const Vector<std::complex<double> >& a,
552 const Vector<double>& b);
554 const Vector<std::complex<double> >& b);
555 Vector<std::complex<double> > operator % (const Vector<std::complex<double> >& a, const Vector<int>& b);
556 Vector<std::complex<double> > operator % (const Vector<int>& a, const Vector<std::complex<double> >& b);
559
561
562
563 Vector<std::complex<double> > conj(const Vector<std::complex<double> >& r);
564
571 double* conv2double(int numV, Vector<std::complex<double> >* r);
572
585 inline Vector <double> ceil(const Vector<double>& r)
586 {
587 return Vector<double>((int)std::ceil(r[0]), (int)std::ceil(r[1]), (int)std::ceil(r[2]));
588 }
589
590 inline Vector <int> iceil(const Vector<double>& r)
591 {
592 return Vector<int>((int)std::ceil(r[0]), (int)std::ceil(r[1]), (int)std::ceil(r[2]));
593 }
594
595
596 inline double sign(double x)
597 {
598 double y = (x > 0) ? 1.0 : 0.0 + (x < 0) ? -1.0 : 0.0;
599 return y;
600 }
601
609 Vector<std::complex<double> > grad2d(double(*f(Vector<std::complex<double> >)), Vector<std::complex<double> > x, double dx);
611 Vector<double> real(Vector <std::complex<double> > r);
612 Vector<double> imag(Vector <std::complex<double> > r);
615 void rotate(Vector<double>& r, double phi);
616 Vector<double> rotate(const Vector<double>& r, double dtheta, double dphi);
618 Vector<std::complex<double> > makeReal(const Vector<std::complex<double> >& r);
625 Vector<double> cart2sphere(double x, double y, double z);
632 Vector<double> sphere2cart(double r, double theta, double phi);
633
642 void getKSystem(const Vector<double>& n, const Vector<double>& k,
644
645 Vector<double> arg(Vector<std::complex<double> >& r);
646 std::complex<double> asin(std::complex<double> z);
647 std::complex<double> tan(std::complex<double> z);
648 std::complex<double> ihoch(int l);
650 Vector <double> emax(Vector<double>& P1, Vector<double>& P2);
651
652#define ex Vector<double> (1.0,0.0,0.0)
653#define ey Vector<double> (0.0,1.0,0.0)
654#define ez Vector<double> (0.0,0.0,1.0)
655#define dzero Vector<double> (0.0,0.0,0.0)
656// #define one Vector<double> (1.0,1.0,1.0)
657 const Vector<double> one=Vector<double>(1.0, 1.0, 1.0);
658#define czero Vector<std::complex<double> > (0.0,0.0,0.0)
659#define cone Vector<std::complex<double> > (1.0,1.0,1.0)
660
661
662 Vector<double> er(double theta, double phi);
663 Vector<double> etheta(double theta, double phi);
664 Vector<double> ephi(double theta, double phi);
665
666 Vector<std::complex<double> > operator * (std::complex<double> x, const Vector<double>& r);
667 Vector<std::complex<double> > operator * (const Vector<double>& r, std::complex<double> x);
675 double* conv2double(int no, Vector<std::complex<double> >* r);
683 Vector<std::complex<double> >* conv2vector(int Anz, double* r);
690 Vector<std::complex<double> > grad2d(double(*f(Vector<std::complex<double> >)), Vector<std::complex<double> > r, double d);
691 Vector<std::complex<double> > convd2c(const Vector<std::complex<double> >& r);
692 void rotate(Vector<double>& r, double phi);
693 std::complex<double> asin(std::complex<double> z);
694 std::complex<double> tan(std::complex<double> z);
695 Vector<std::complex<double> > conj(const Vector<std::complex<double> >& r);
696 Vector<std::complex<double> > norm(const Vector<std::complex<double> >& r); // Normiert Vektor r
697 Vector<double> norm(const Vector<double>& r); // Normiert Vektor r (r=r/|r|)
703 char* toString(char* S, Vector<double> P);
709 char* toString(char* S, Vector<std::complex<double> > P);
710 Vector<double> nanV(const char* tagp);
711
712 bool isnan(Vector<std::complex<double> > v);
714 }
715}
716
717#ifndef complex_I
718#define complex_I
719 const std::complex<double> I = std::complex<double>(0.0, 1.0);
720#endif
Template class for threedimensional vectors.
Definition vector.h:57
Vector & operator+=(const Vector &r)
Addition operator.
Definition vector.h:249
bool operator==(const Vector &r)
Comparison operators: two vectors are equal if their components are (exactly) the same.
Definition vector.h:233
friend T operator*(const Vector &a, const Vector &b)
Definition vector.h:293
friend Vector emult(const Vector &r1, const Vector &r2)
Component-wise multiplication of two vectors.
Definition vector.h:181
Vector & operator=(const Vector &r)
Assignment operator.
Definition vector.h:215
Vector & operator*=(T r)
Multiplication with scalar.
Definition vector.h:265
friend Vector ediv(T s, const Vector &r)
Component-wise division, scalar (s) by vector (r)
Definition vector.h:205
Vector & operator-=(const Vector &r)
Subtraction operator.
Definition vector.h:257
friend Vector operator+(const Vector &r1, const Vector &r2)
Addition of two vectors.
Definition vector.h:112
friend Vector sqrt(const Vector &r)
calculates the square root of a vector: result: sqrt((x,y,z))
Definition vector.h:167
Vector(T x, T y, T z)
Constructor with the three components as parameters.
Definition vector.h:91
Vector(const Vector &r)
Definition vector.h:99
void binRead(std::ifstream &is)
Definition vector.h:68
T & operator[](int i)
Bracket operator, gives access to the i-th component of the vector (as for a usual array).
Definition vector.h:354
void binWrite(std::ofstream &os)
Definition vector.h:61
Vector()
Standard constructor (all components are initialized with 0)
Definition vector.h:77
Vector & operator/=(T r)
Division by scalar.
Definition vector.h:273
bool operator!=(const Vector &r)
Inequality operator: Two vectors are inequal if at least one component is not equal.
Definition vector.h:241
friend Vector operator%(const Vector &r1, const Vector &r2)
cross product between two vectors.
Definition vector.h:154
friend Vector ediv(const Vector &r1, const Vector &r2)
Component-wise division of two vectors.
Definition vector.h:190
Vector operator-()
Definition vector.h:122
friend Vector exp(const Vector &r)
exponential function of a vector (x,y,z), defined by exp((x,y,z))
Definition vector.h:175
friend Vector operator/(const Vector &r, T x)
Division of a vector by a scalar.
Definition vector.h:340
double abs(std::complex< double > x)
absolute value of the complex valued variable x
const Vector< double > one
double valued vector with all components set to 1
Definition vector.h:657
double sign(double x)
Definition vector.h:596
Vector< double > etheta(double theta, double phi)
returns a normalized vector in theta-direction on a unit sphere with spherical coordinates /p theta a...
Vector< int > ifloor(const Vector< double > &r)
rounding up, component-by-component rounding
Vector< double > emult(const Vector< double > &r1, const Vector< int > &r2)
Vector< std::complex< double > > operator%(const Vector< std::complex< double > > &a, const Vector< double > &b)
Vector< double > ceil(const Vector< double > &r)
Definition vector.h:585
void rotate(Vector< double > &r, double phi)
Vector< std::complex< double > > norm(const Vector< std::complex< double > > &r)
std::ostream & operator<<(std::ostream &is, const Vector< std::complex< double > > &r)
std::complex< double > ihoch(int l)
void sphereunitv(Vector< double > &P, Vector< double > &er, Vector< double > &etheta, Vector< double > &ephi)
std::complex< double > asin(std::complex< double > z)
Vector< int > iceil(const Vector< double > &r)
Definition vector.h:590
double abs2(const Vector< double > &r)
Calculates the squared absolute value of double vector.
Vector< std::complex< double > > grad2d(double(*f(Vector< std::complex< double > >)), Vector< std::complex< double > > x, double dx)
calculates the gradient of a given function at a certain position
Vector< double > er(double theta, double phi)
returns a normalized vector in r-direction on a unit sphere with spherical coordinates /p theta and /...
Vector< std::complex< double > > vdc(const Vector< double > &r)
Vector< double > imag(Vector< std::complex< double > > r)
returns a vector with the imaginary parts of the components of the complex vector r
void getKSystem(const Vector< double > &n, const Vector< double > &k, Vector< double > &e1, Vector< double > &e2, Vector< double > &e3)
Calculates the three direction vectors of a coordinate system formed by the surface normal n and the ...
Vector< std::complex< double > > convd2c(const Vector< double > &r)
Vector< double > emax(Vector< double > &P1, Vector< double > &P2)
Matrix< std::complex< double > > operator-(const Matrix< double > &, const Matrix< std::complex< double > > &)
std::istream & operator>>(std::istream &is, Vector< std::complex< double > > &r)
input operator, reads complex vector from file
double arctan(double y, double x)
Matrix< std::complex< double > > operator/(const Matrix< double > &, const std::complex< double > &)
Vector< double > floor(const Vector< double > &r)
rounding up, Component-by-component
Vector< double > real(Vector< std::complex< double > > r)
returns a vector with the real parts of the components of the complex vector r
Matrix< std::complex< double > > operator*(const Matrix< double > &, const Matrix< std::complex< double > > &)
Vector< std::complex< double > > * conv2vector(int num, double *r)
This function converts a list of doubles into a list of complex vectors.
void errmsg(char *S)
Matrix< std::complex< double > > operator+(const Matrix< double > &, const Matrix< std::complex< double > > &)
Vector< double > arg(Vector< std::complex< double > > &r)
Vector< std::complex< double > > conj(const Vector< std::complex< double > > &r)
Conjugate complex of the vector r.
Vector< std::complex< double > > makeReal(const Vector< std::complex< double > > &r)
double * conv2double(int numV, Vector< std::complex< double > > *r)
This function returns an array with all components of the Vector-array r.
double sViereck(Vector< double >, Vector< double >, Vector< double >, Vector< double >)
std::complex< double > tan(std::complex< double > z)
Vector< T > sphere2cart(const Vector< T > &P)
Transforms vector from spherical coordinates into cartesian coordinates.
Definition vector.h:445
Vector< double > ephi(double theta, double phi)
returns a normalized vector in phi-direction on a unit sphere with spherical coordinates /p theta and...
double sDreieck(Vector< double >, Vector< double >, Vector< double >)
bool isnan(Vector< std::complex< double > > v)
returns true if one of the components of v is NaN
Vector< T > cart2sphere(const Vector< T > &x)
Transformation from cartesian coordinates to spherical coordinates.
Definition vector.h:373
char * toString(char *S, Vector< double > P)
Converts a double vector into a string.
Vector< double > nanV(const char *tagp)
This class is used for the iray class. This class is intended for internal use only....
Definition fresnel.h:7
#define I
Definition fft.h:13