OpenMM
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Vec3.h
1 #ifndef OPENMM_VEC3_H_
2 #define OPENMM_VEC3_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * OpenMM *
6  * -------------------------------------------------------------------------- *
7  * This is part of the OpenMM molecular simulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org. *
11  * *
12  * Portions copyright (c) 2008-2013 Stanford University and the Authors. *
13  * Authors: Peter Eastman *
14  * Contributors: *
15  * *
16  * Permission is hereby granted, free of charge, to any person obtaining a *
17  * copy of this software and associated documentation files (the "Software"), *
18  * to deal in the Software without restriction, including without limitation *
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
20  * and/or sell copies of the Software, and to permit persons to whom the *
21  * Software is furnished to do so, subject to the following conditions: *
22  * *
23  * The above copyright notice and this permission notice shall be included in *
24  * all copies or substantial portions of the Software. *
25  * *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
29  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
30  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
31  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
32  * USE OR OTHER DEALINGS IN THE SOFTWARE. *
33  * -------------------------------------------------------------------------- */
34 
35 #include <cassert>
36 #include <iosfwd>
37 
38 namespace OpenMM {
39 
45 class Vec3 {
46 public:
50  Vec3() {
51  data[0] = data[1] = data[2] = 0.0;
52  }
56  Vec3(double x, double y, double z) {
57  data[0] = x;
58  data[1] = y;
59  data[2] = z;
60  }
61  double operator[](int index) const {
62  assert(index >= 0 && index < 3);
63  return data[index];
64  }
65  double& operator[](int index) {
66  assert(index >= 0 && index < 3);
67  return data[index];
68  }
69 
70  bool operator==(const Vec3& rhs) const {
71  return (data[0] == rhs[0] && data[1] == rhs[1] && data[2] == rhs[2]);
72  }
73 
74  bool operator!=(const Vec3& rhs) const {
75  return (data[0] != rhs[0] || data[1] != rhs[1] || data[2] != rhs[2]);
76  }
77 
78  // Arithmetic operators
79 
80  // unary plus
81  Vec3 operator+() const {
82  return Vec3(*this);
83  }
84 
85  // plus
86  Vec3 operator+(const Vec3& rhs) const {
87  const Vec3& lhs = *this;
88  return Vec3(lhs[0] + rhs[0], lhs[1] + rhs[1], lhs[2] + rhs[2]);
89  }
90 
91  Vec3& operator+=(const Vec3& rhs) {
92  data[0] += rhs[0];
93  data[1] += rhs[1];
94  data[2] += rhs[2];
95  return *this;
96  }
97 
98  // unary minus
99  Vec3 operator-() const {
100  const Vec3& lhs = *this;
101  return Vec3(-lhs[0], -lhs[1], -lhs[2]);
102  }
103 
104  // minus
105  Vec3 operator-(const Vec3& rhs) const {
106  const Vec3& lhs = *this;
107  return Vec3(lhs[0] - rhs[0], lhs[1] - rhs[1], lhs[2] - rhs[2]);
108  }
109 
110  Vec3& operator-=(const Vec3& rhs) {
111  data[0] -= rhs[0];
112  data[1] -= rhs[1];
113  data[2] -= rhs[2];
114  return *this;
115  }
116 
117  // scalar product
118  Vec3 operator*(double rhs) const {
119  const Vec3& lhs = *this;
120  return Vec3(lhs[0]*rhs, lhs[1]*rhs, lhs[2]*rhs);
121  }
122 
123  Vec3& operator*=(double rhs) {
124  data[0] *= rhs;
125  data[1] *= rhs;
126  data[2] *= rhs;
127  return *this;
128  }
129 
130  // scalar division
131  Vec3 operator/(double rhs) const {
132  const Vec3& lhs = *this;
133  double scale = 1.0/rhs;
134  return Vec3(lhs[0]*scale, lhs[1]*scale, lhs[2]*scale);
135  }
136 
137  Vec3& operator/=(double rhs) {
138  double scale = 1.0/rhs;
139  data[0] *= scale;
140  data[1] *= scale;
141  data[2] *= scale;
142  return *this;
143  }
144 
145  // dot product
146  double dot(const Vec3& rhs) const {
147  const Vec3& lhs = *this;
148  return lhs[0]*rhs[0] + lhs[1]*rhs[1] + lhs[2]*rhs[2];
149  }
150 
151  // cross product
152  Vec3 cross(const Vec3& rhs) const {
153  return Vec3(data[1]*rhs[2]-data[2]*rhs[1], data[2]*rhs[0]-data[0]*rhs[2], data[0]*rhs[1]-data[1]*rhs[0]);
154  }
155 
156 private:
157  double data[3];
158 };
159 
160 template <class CHAR, class TRAITS>
161 std::basic_ostream<CHAR,TRAITS>& operator<<(std::basic_ostream<CHAR,TRAITS>& o, const Vec3& v) {
162  o<<'['<<v[0]<<", "<<v[1]<<", "<<v[2]<<']';
163  return o;
164 }
165 
166 } // namespace OpenMM
167 
168 #endif /*OPENMM_VEC3_H_*/
Vec3 operator+(const Vec3 &rhs) const
Definition: Vec3.h:86
double operator[](int index) const
Definition: Vec3.h:61
Vec3()
Create a Vec3 whose elements are all 0.
Definition: Vec3.h:50
Vec3 operator*(double rhs) const
Definition: Vec3.h:118
Vec3 operator+() const
Definition: Vec3.h:81
Vec3(double x, double y, double z)
Create a Vec3 with specified x, y, and z components.
Definition: Vec3.h:56
Vec3 operator-(const Vec3 &rhs) const
Definition: Vec3.h:105
Vec3 & operator+=(const Vec3 &rhs)
Definition: Vec3.h:91
Vec3 & operator*=(double rhs)
Definition: Vec3.h:123
Vec3 & operator/=(double rhs)
Definition: Vec3.h:137
bool operator==(const Vec3 &rhs) const
Definition: Vec3.h:70
Vec3 operator-() const
Definition: Vec3.h:99
This class represents a three component vector.
Definition: Vec3.h:45
Vec3 cross(const Vec3 &rhs) const
Definition: Vec3.h:152
double dot(const Vec3 &rhs) const
Definition: Vec3.h:146
Vec3 & operator-=(const Vec3 &rhs)
Definition: Vec3.h:110
Vec3 operator/(double rhs) const
Definition: Vec3.h:131
bool operator!=(const Vec3 &rhs) const
Definition: Vec3.h:74
Definition: AndersenThermostat.h:40
double & operator[](int index)
Definition: Vec3.h:65