OpenMM
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends
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 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  // dot product
131  double dot(const Vec3& rhs) const {
132  const Vec3& lhs = *this;
133  return lhs[0]*rhs[0] + lhs[1]*rhs[1] + lhs[2]*rhs[2];
134  }
135 
136  // cross product
137  Vec3 cross(const Vec3& rhs) const {
138  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]);
139  }
140 
141 private:
142  double data[3];
143 };
144 
145 template <class CHAR, class TRAITS>
146 std::basic_ostream<CHAR,TRAITS>& operator<<(std::basic_ostream<CHAR,TRAITS>& o, const Vec3& v) {
147  o<<'['<<v[0]<<", "<<v[1]<<", "<<v[2]<<']';
148  return o;
149 }
150 
151 } // namespace OpenMM
152 
153 #endif /*OPENMM_VEC3_H_*/