2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2016,2017,2018,2019, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 #ifndef GMX_MATH_VECTYPES_H
38 #define GMX_MATH_VECTYPES_H
43 #include <type_traits>
45 #include "gromacs/utility/gmxassert.h"
46 #include "gromacs/utility/real.h"
48 #define XX 0 /* Defines for indexing in */
49 #define YY 1 /* vectors */
51 #define DIM 3 /* Dimension of vectors */
53 typedef real rvec[DIM];
55 typedef double dvec[DIM];
57 typedef real matrix[DIM][DIM];
59 typedef real tensor[DIM][DIM];
61 typedef int ivec[DIM];
67 * C++ class for 3D vectors.
69 * \tparam ValueType Type
71 * This class provides a C++ version of rvec/dvec/ivec that can be put into STL
72 * containers etc. It is more or less a drop-in replacement for `rvec` and
73 * friends: it can be used in most contexts that accept the equivalent C type.
74 * However, there is one case where explicit conversion is necessary:
75 * - An array of these objects needs to be converted with as_vec_array() (or
76 * convenience methods like as_rvec_array()).
78 * For the array conversion to work, the compiler should not add any extra
79 * alignment/padding in the layout of this class; that this actually works as
80 * intended is tested in the unit tests.
84 template<typename ValueType>
88 //! Underlying raw C array type (rvec/dvec/ivec).
89 using RawArray = ValueType[DIM];
91 // The code here assumes ValueType has been deduced as a data type like int
92 // and not a pointer like int*. If there is a use case for a 3-element array
93 // of pointers, the implementation will be different enough that the whole
94 // template class should have a separate partial specialization. We try to avoid
95 // accidental matching to pointers, but this assertion is a no-cost extra check.
96 static_assert(!std::is_pointer<std::remove_cv_t<ValueType>>::value,
97 "BasicVector value type must not be a pointer.");
99 //! Constructs default (uninitialized) vector.
101 //! Constructs a vector from given values.
102 BasicVector(ValueType x, ValueType y, ValueType z) : x_{ x, y, z } {}
104 * Constructs a vector from given values.
106 * This constructor is not explicit to support implicit conversions
107 * that allow, e.g., calling `std::vector<RVec>:``:push_back()` directly
108 * with an `rvec` parameter.
110 BasicVector(const RawArray x) : x_{ x[XX], x[YY], x[ZZ] } {}
111 //! Default copy constructor.
112 BasicVector(const BasicVector& src) = default;
113 //! Default copy assignment operator.
114 BasicVector& operator=(const BasicVector& v) = default;
115 //! Default move constructor.
116 BasicVector(BasicVector&& src) noexcept = default;
117 //! Default move assignment operator.
118 BasicVector& operator=(BasicVector&& v) noexcept = default;
119 //! Indexing operator to make the class work as the raw array.
120 ValueType& operator[](int i) { return x_[i]; }
121 //! Indexing operator to make the class work as the raw array.
122 ValueType operator[](int i) const { return x_[i]; }
123 //! Allow inplace addition for BasicVector
124 BasicVector<ValueType>& operator+=(const BasicVector<ValueType>& right)
126 return *this = *this + right;
128 //! Allow inplace subtraction for BasicVector
129 BasicVector<ValueType>& operator-=(const BasicVector<ValueType>& right)
131 return *this = *this - right;
133 //! Allow vector addition
134 BasicVector<ValueType> operator+(const BasicVector<ValueType>& right) const
136 return { x_[0] + right[0], x_[1] + right[1], x_[2] + right[2] };
138 //! Allow vector subtraction
139 BasicVector<ValueType> operator-(const BasicVector<ValueType>& right) const
141 return { x_[0] - right[0], x_[1] - right[1], x_[2] - right[2] };
143 //! Allow vector scalar division
144 BasicVector<ValueType> operator/(const ValueType& right) const
146 GMX_ASSERT(right != 0, "Cannot divide by zero");
148 return *this * (1 / right);
150 //! Scale vector by a scalar
151 BasicVector<ValueType>& operator*=(const ValueType& right)
159 //! Divide vector by a scalar
160 BasicVector<ValueType>& operator/=(const ValueType& right)
162 GMX_ASSERT(right != 0, "Cannot divide by zero");
164 return *this *= 1 / right;
166 //! Return dot product
167 ValueType dot(const BasicVector<ValueType>& right) const
169 return x_[0] * right[0] + x_[1] * right[1] + x_[2] * right[2];
172 //! Allow vector vector multiplication (cross product)
173 BasicVector<ValueType> cross(const BasicVector<ValueType>& right) const
175 return { x_[YY] * right.x_[ZZ] - x_[ZZ] * right.x_[YY],
176 x_[ZZ] * right.x_[XX] - x_[XX] * right.x_[ZZ],
177 x_[XX] * right.x_[YY] - x_[YY] * right.x_[XX] };
180 //! Return normalized to unit vector
181 BasicVector<ValueType> unitVector() const
183 const ValueType vectorNorm = norm();
184 GMX_ASSERT(vectorNorm != 0, "unitVector() should not be called with a zero vector");
186 return *this / vectorNorm;
189 //! Length^2 of vector
190 ValueType norm2() const { return dot(*this); }
192 //! Norm or length of vector
193 ValueType norm() const { return std::sqrt(norm2()); }
196 BasicVector<real> toRVec() const { return { real(x_[0]), real(x_[1]), real(x_[2]) }; }
199 BasicVector<int> toIVec() const
201 return { static_cast<int>(x_[0]), static_cast<int>(x_[1]), static_cast<int>(x_[2]) };
205 BasicVector<double> toDVec() const { return { double(x_[0]), double(x_[1]), double(x_[2]) }; }
207 //! Converts to a raw C array where implicit conversion does not work.
208 RawArray& as_vec() { return x_; }
209 //! Converts to a raw C array where implicit conversion does not work.
210 const RawArray& as_vec() const { return x_; }
211 //! Makes BasicVector usable in contexts where a raw C array is expected.
212 operator RawArray&() { return x_; }
213 //! Makes BasicVector usable in contexts where a raw C array is expected.
214 operator const RawArray&() const { return x_; }
220 //! Allow vector scalar multiplication
221 template<typename ValueType>
222 BasicVector<ValueType> operator*(const BasicVector<ValueType>& basicVector, const ValueType& scalar)
224 return { basicVector[0] * scalar, basicVector[1] * scalar, basicVector[2] * scalar };
227 //! Allow scalar vector multiplication
228 template<typename ValueType>
229 BasicVector<ValueType> operator*(const ValueType& scalar, const BasicVector<ValueType>& basicVector)
231 return { scalar * basicVector[0], scalar * basicVector[1], scalar * basicVector[2] };
235 * unitv for gmx::BasicVector
237 template<typename VectorType>
238 static inline VectorType unitVector(const VectorType& v)
240 return v.unitVector();
244 * norm for gmx::BasicVector
246 template<typename ValueType>
247 static inline ValueType norm(BasicVector<ValueType> v)
253 * Square of the vector norm for gmx::BasicVector
255 template<typename ValueType>
256 static inline ValueType norm2(BasicVector<ValueType> v)
262 * cross product for gmx::BasicVector
264 template<typename VectorType>
265 static inline VectorType cross(const VectorType& a, const VectorType& b)
271 * dot product for gmx::BasicVector
273 template<typename ValueType>
274 static inline ValueType dot(BasicVector<ValueType> a, BasicVector<ValueType> b)
280 * Multiply two vectors element by element and return the result.
282 template<typename VectorType>
283 static inline VectorType scaleByVector(const VectorType& a, const VectorType& b)
285 return { a[0] * b[0], a[1] * b[1], a[2] * b[2] };
289 * Return the element-wise minimum of two vectors.
291 template<typename VectorType>
292 static inline VectorType elementWiseMin(const VectorType& a, const VectorType& b)
294 return { std::min(a[0], b[0]), std::min(a[1], b[1]), std::min(a[2], b[2]) };
298 * Return the element-wise maximum of two vectors.
300 template<typename VectorType>
301 static inline VectorType elementWiseMax(const VectorType& a, const VectorType& b)
303 return { std::max(a[0], b[0]), std::max(a[1], b[1]), std::max(a[2], b[2]) };
307 * Casts a gmx::BasicVector array into an equivalent raw C array.
309 template<typename ValueType>
310 static inline typename BasicVector<ValueType>::RawArray* as_vec_array(BasicVector<ValueType>* x)
312 return reinterpret_cast<typename BasicVector<ValueType>::RawArray*>(x);
316 * Casts a gmx::BasicVector array into an equivalent raw C array.
318 template<typename ValueType>
319 static inline const typename BasicVector<ValueType>::RawArray* as_vec_array(const BasicVector<ValueType>* x)
321 return reinterpret_cast<const typename BasicVector<ValueType>::RawArray*>(x);
324 //! Shorthand for C++ `rvec`-equivalent type.
325 typedef BasicVector<real> RVec;
326 //! Shorthand for C++ `dvec`-equivalent type.
327 typedef BasicVector<double> DVec;
328 //! Shorthand for C++ `ivec`-equivalent type.
329 typedef BasicVector<int> IVec;
330 //! Casts a gmx::RVec array into an `rvec` array.
331 static inline rvec* as_rvec_array(RVec* x)
333 return as_vec_array(x);
335 //! Casts a gmx::RVec array into an `rvec` array.
336 static inline const rvec* as_rvec_array(const RVec* x)
338 return as_vec_array(x);
340 //! Casts a gmx::DVec array into an `Dvec` array.
341 static inline dvec* as_dvec_array(DVec* x)
343 return as_vec_array(x);
345 //! Casts a gmx::IVec array into an `ivec` array.
346 static inline ivec* as_ivec_array(IVec* x)
348 return as_vec_array(x);
352 //! Casts a gmx::DVec array into an `dvec` array.
353 static inline const dvec* as_dvec_array(const DVec* x)
355 return as_vec_array(x);
357 //! Casts a gmx::IVec array into an `ivec` array.
358 static inline const ivec* as_ivec_array(const IVec* x)
360 return as_vec_array(x);
363 //! Shorthand for C++ `ivec`-equivalent type.
364 typedef BasicVector<int> IVec;
368 #endif // include guard