forked from alliepiper/XtalComp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcvector.h
133 lines (105 loc) · 3.38 KB
/
xcvector.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
129
130
131
132
133
/**********************************************************************
XcVector - vector of three doubles, in-line class
Copyright (C) 2011 by David C. Lonie
This source code is released under the New BSD License, (the "License").
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***********************************************************************/
#ifndef XCVECTOR_H
#define XCVECTOR_H
#include <math.h>
class XcVector
{
public:
XcVector() {};
XcVector(const double s) {arr[0]=s; arr[1]=s; arr[2]=s;}
XcVector(const double x, const double y, const double z)
{
arr[0] = x; arr[1] = y; arr[2] = z;
}
XcVector(const XcVector &other)
{
this->arr[0] = other.arr[0];
this->arr[1] = other.arr[1];
this->arr[2] = other.arr[2];
}
XcVector & fill(const double s) {arr[0]=s; arr[1]=s; arr[2]=s; return *this;}
XcVector & set(const double x, const double y, const double z)
{
arr[0] = x; arr[1]=y; arr[2]=z; return *this;
}
double & x() {return arr[0];}
double & y() {return arr[1];}
double & z() {return arr[2];}
const double & x() const {return arr[0];}
const double & y() const {return arr[1];}
const double & z() const {return arr[2];}
double & operator()(const unsigned short i)
{return arr[i];}
const double & operator()(const unsigned short i) const
{return arr[i];}
double & operator[](const unsigned short i)
{return arr[i];}
const double & operator[](const unsigned short i) const
{return arr[i];}
XcVector & operator*=(const double scalar)
{
this->arr[0] *= scalar;
this->arr[1] *= scalar;
this->arr[2] *= scalar;
return *this;
}
XcVector & operator/=(const double scalar) {return ((*this) *= (1.0 / scalar));}
XcVector & operator+=(const XcVector &other)
{
this->arr[0] += other.arr[0];
this->arr[1] += other.arr[1];
this->arr[2] += other.arr[2];
return *this;
}
XcVector & operator-=(const XcVector &other)
{
this->arr[0] -= other.arr[0];
this->arr[1] -= other.arr[1];
this->arr[2] -= other.arr[2];
return *this;
}
XcVector operator-() const
{
return XcVector(-this->arr[0], -this->arr[1], -this->arr[2]);
}
XcVector operator*(const double scalar) const
{
return XcVector(this->arr[0] * scalar,
this->arr[1] * scalar,
this->arr[2] * scalar);
}
XcVector operator/(const double scalar) const {return ((*this) * (1.0 / scalar));}
XcVector operator+(const XcVector &other) const
{
return XcVector(this->arr[0] + other.arr[0],
this->arr[1] + other.arr[1],
this->arr[2] + other.arr[2]);
}
XcVector operator-(const XcVector &other) const
{
return XcVector(this->arr[0] - other.arr[0],
this->arr[1] - other.arr[1],
this->arr[2] - other.arr[2]);
}
double squaredNorm() const {return this->dot(*this);}
double norm() const {return sqrt(this->squaredNorm());}
double dot(const XcVector &other) const
{
return
this->arr[0]*other.arr[0] +
this->arr[1]*other.arr[1] +
this->arr[2]*other.arr[2];
}
private:
double arr[3];
};
#endif