This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
coord3d.h
129 lines (93 loc) · 2.4 KB
/
coord3d.h
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
#ifndef COORD3D_H
#define COORD3D_H
#include <math.h>
#include <string>
#include <vector>
#include "basetypes.h"
namespace PTools{
struct Coord3D
{
dbl x,y,z;
Coord3D() {x=0.0; y=0.0;z=0.0;};
Coord3D(dbl nx, dbl ny, dbl nz){x=nx;y=ny;z=nz;};
Coord3D(const Coord3D& old) {x=old.x; y=old.y; z=old.z;};
inline Coord3D & operator= (const Coord3D &);
inline bool operator==(const Coord3D& b) {return (b.x==x && b.y==y && b.z==z); };
Coord3D& Normalize(); ///< Normalize vector to unity (in place)
std::string toString(bool newline=true);
};
typedef std::vector<Coord3D> VCoord3D;
/// Define = operator : Coord3D = Coord3D
inline Coord3D & Coord3D::operator= (const Coord3D & C)
{
x = C.x;
y = C.y;
z = C.z;
return *this;
}
/// Define + operator : Coord3D + Coord3D
inline Coord3D operator+ (const Coord3D& A,const Coord3D& B)
{
Coord3D P(A);
P.x += B.x ;
P.y += B.y ;
P.z += B.z ;
return P;
}
/// define - operator : Coord3D - Coord3D
inline Coord3D operator- (const Coord3D& A,const Coord3D& B)
{
Coord3D P(A);
P.x -= B.x ;
P.y -= B.y ;
P.z -= B.z ;
return P;
}
inline Coord3D & operator+=(Coord3D & a, const Coord3D & x ){a = a + x ; return a; } //operator +=
inline Coord3D & operator-=(Coord3D & a, const Coord3D & x ){a = a - x ; return a; } //operator -=
/// Vector Norm
inline dbl Norm(const Coord3D & A)
{
return sqrt (A.x*A.x + A.y*A.y + A.z*A.z) ;
}
/// Vector norm * norm
inline dbl Norm2(const Coord3D & A)
{
return (A.x*A.x + A.y*A.y + A.z*A.z);
}
/// define * operator : Coord3D x dbl
inline Coord3D operator* (const Coord3D& A, dbl scal)
{
Coord3D P(A);
P.x *= scal ;
P.y *= scal ;
P.z *= scal ;
return P;
}
/// define * operator : dbl * Coord3D
inline Coord3D operator* (dbl scal, const Coord3D& A) {
return A * scal ;
}
/// define / operator : Coord3D / dbl
inline Coord3D operator/ (const Coord3D& A, dbl d) {
return (1/d) * A ;
}
/// print coordinates in string
inline std::string PrintCoord(const Coord3D& A) {
int size=100;
char *info = new char [size];
snprintf(info, size, "%8.3f %8.3f %8.3f", real(A.x), real(A.y), real(A.z));
std::string result(info);
delete[] info;
return result;
}
inline Coord3D minus(const Coord3D& orig)
{
Coord3D min;
min.x = -orig.x;
min.y = -orig.y;
min.z = -orig.z;
return min;
}
}
#endif //CORRD3D_H