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 by the GROMACS development team.
7 * Copyright (c) 2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
38 #ifndef GMX_MATH_VECTYPES_H
39 #define GMX_MATH_VECTYPES_H
44 #include <type_traits>
46 #include "gromacs/utility/gmxassert.h"
47 #include "gromacs/utility/real.h"
49 #define XX 0 /* Defines for indexing in */
50 #define YY 1 /* vectors */
52 #define DIM 3 /* Dimension of vectors */
54 typedef real rvec[DIM];
56 typedef double dvec[DIM];
58 typedef real matrix[DIM][DIM];
60 typedef real tensor[DIM][DIM];
62 typedef int ivec[DIM];
68 * C++ class for 3D vectors.
70 * \tparam ValueType Type
72 * This class provides a C++ version of rvec/dvec/ivec that can be put into STL
73 * containers etc. It is more or less a drop-in replacement for `rvec` and
74 * friends: it can be used in most contexts that accept the equivalent C type.
75 * However, there is one case where explicit conversion is necessary:
76 * - An array of these objects needs to be converted with as_vec_array() (or
77 * convenience methods like as_rvec_array()).
79 * For the array conversion to work, the compiler should not add any extra
80 * alignment/padding in the layout of this class; that this actually works as
81 * intended is tested in the unit tests.
85 template<typename ValueType>
89 //! Underlying raw C array type (rvec/dvec/ivec).
90 using RawArray = ValueType[DIM];
92 // The code here assumes ValueType has been deduced as a data type like int
93 // and not a pointer like int*. If there is a use case for a 3-element array
94 // of pointers, the implementation will be different enough that the whole
95 // template class should have a separate partial specialization. We try to avoid
96 // accidental matching to pointers, but this assertion is a no-cost extra check.
97 static_assert(!std::is_pointer_v<std::remove_cv_t<ValueType>>,
98 "BasicVector value type must not be a pointer.");
100 //! Constructs default (uninitialized) vector.
102 //! Constructs a vector from given values.
103 BasicVector(ValueType x, ValueType y, ValueType z) : x_{ x, y, z } {}
105 * Constructs a vector from given values.
107 * This constructor is not explicit to support implicit conversions
108 * that allow, e.g., calling `std::vector<RVec>:``:push_back()` directly
109 * with an `rvec` parameter.
111 BasicVector(const RawArray x) : x_{ x[XX], x[YY], x[ZZ] } {}
112 //! Default copy constructor.
113 BasicVector(const BasicVector& src) = default;
114 //! Default copy assignment operator.
115 BasicVector& operator=(const BasicVector& v) = default;
116 //! Default move constructor.
117 BasicVector(BasicVector&& src) noexcept = default;
118 //! Default move assignment operator.
119 BasicVector& operator=(BasicVector&& v) noexcept = default;
120 //! Indexing operator to make the class work as the raw array.
121 ValueType& operator[](int i) { return x_[i]; }
122 //! Indexing operator to make the class work as the raw array.
123 ValueType operator[](int i) const { return x_[i]; }
124 //! Allow inplace addition for BasicVector
125 BasicVector<ValueType>& operator+=(const BasicVector<ValueType>& right)
127 return *this = *this + right;
129 //! Allow inplace subtraction for BasicVector
130 BasicVector<ValueType>& operator-=(const BasicVector<ValueType>& right)
132 return *this = *this - right;
134 //! Allow vector addition
135 BasicVector<ValueType> operator+(const BasicVector<ValueType>& right) const
137 return { x_[0] + right[0], x_[1] + right[1], x_[2] + right[2] };
139 //! Allow vector subtraction
140 BasicVector<ValueType> operator-(const BasicVector<ValueType>& right) const
142 return { x_[0] - right[0], x_[1] - right[1], x_[2] - right[2] };
144 //! Allow vector scalar division
145 BasicVector<ValueType> operator/(const ValueType& right) const
147 GMX_ASSERT(right != 0, "Cannot divide by zero");
149 return *this * (1 / right);
151 //! Scale vector by a scalar
152 BasicVector<ValueType>& operator*=(const ValueType& right)
160 //! Divide vector by a scalar
161 BasicVector<ValueType>& operator/=(const ValueType& right)
163 GMX_ASSERT(right != 0, "Cannot divide by zero");
165 return *this *= 1 / right;
167 //! Return dot product
168 ValueType dot(const BasicVector<ValueType>& right) const
170 return x_[0] * right[0] + x_[1] * right[1] + x_[2] * right[2];
173 //! Allow vector vector multiplication (cross product)
174 BasicVector<ValueType> cross(const BasicVector<ValueType>& right) const
176 return { x_[YY] * right.x_[ZZ] - x_[ZZ] * right.x_[YY],
177 x_[ZZ] * right.x_[XX] - x_[XX] * right.x_[ZZ],
178 x_[XX] * right.x_[YY] - x_[YY] * right.x_[XX] };
181 //! Return normalized to unit vector
182 BasicVector<ValueType> unitVector() const
184 const ValueType vectorNorm = norm();
185 GMX_ASSERT(vectorNorm != 0, "unitVector() should not be called with a zero vector");
187 return *this / vectorNorm;
190 //! Length^2 of vector
191 ValueType norm2() const { return dot(*this); }
193 //! Norm or length of vector
194 ValueType norm() const { return std::sqrt(norm2()); }
197 BasicVector<real> toRVec() const { return { real(x_[0]), real(x_[1]), real(x_[2]) }; }
200 BasicVector<int> toIVec() const
202 return { static_cast<int>(x_[0]), static_cast<int>(x_[1]), static_cast<int>(x_[2]) };
206 BasicVector<double> toDVec() const { return { double(x_[0]), double(x_[1]), double(x_[2]) }; }
208 //! Converts to a raw C array where implicit conversion does not work.
209 RawArray& as_vec() { return x_; }
210 //! Converts to a raw C array where implicit conversion does not work.
211 const RawArray& as_vec() const { return x_; }
212 //! Makes BasicVector usable in contexts where a raw C array is expected.
213 operator RawArray&() { return x_; }
214 //! Makes BasicVector usable in contexts where a raw C array is expected.
215 operator const RawArray&() const { return x_; }
221 //! Allow vector scalar multiplication
222 template<typename ValueType>
223 BasicVector<ValueType> operator*(const BasicVector<ValueType>& basicVector, const ValueType& scalar)
225 return { basicVector[0] * scalar, basicVector[1] * scalar, basicVector[2] * scalar };
228 //! Allow scalar vector multiplication
229 template<typename ValueType>
230 BasicVector<ValueType> operator*(const ValueType& scalar, const BasicVector<ValueType>& basicVector)
232 return { scalar * basicVector[0], scalar * basicVector[1], scalar * basicVector[2] };
236 * unitv for gmx::BasicVector
238 template<typename VectorType>
239 static inline VectorType unitVector(const VectorType& v)
241 return v.unitVector();
245 * norm for gmx::BasicVector
247 template<typename ValueType>
248 static inline ValueType norm(BasicVector<ValueType> v)
254 * Square of the vector norm for gmx::BasicVector
256 template<typename ValueType>
257 static inline ValueType norm2(BasicVector<ValueType> v)
263 * cross product for gmx::BasicVector
265 template<typename VectorType>
266 static inline VectorType cross(const VectorType& a, const VectorType& b)
272 * dot product for gmx::BasicVector
274 template<typename ValueType>
275 static inline ValueType dot(BasicVector<ValueType> a, BasicVector<ValueType> b)
281 * Multiply two vectors element by element and return the result.
283 template<typename VectorType>
284 static inline VectorType scaleByVector(const VectorType& a, const VectorType& b)
286 return { a[0] * b[0], a[1] * b[1], a[2] * b[2] };
290 * Return the element-wise minimum of two vectors.
292 template<typename VectorType>
293 static inline VectorType elementWiseMin(const VectorType& a, const VectorType& b)
295 return { std::min(a[0], b[0]), std::min(a[1], b[1]), std::min(a[2], b[2]) };
299 * Return the element-wise maximum of two vectors.
301 template<typename VectorType>
302 static inline VectorType elementWiseMax(const VectorType& a, const VectorType& b)
304 return { std::max(a[0], b[0]), std::max(a[1], b[1]), std::max(a[2], b[2]) };
308 * Casts a gmx::BasicVector array into an equivalent raw C array.
310 template<typename ValueType>
311 static inline typename BasicVector<ValueType>::RawArray* as_vec_array(BasicVector<ValueType>* x)
313 return reinterpret_cast<typename BasicVector<ValueType>::RawArray*>(x);
317 * Casts a gmx::BasicVector array into an equivalent raw C array.
319 template<typename ValueType>
320 static inline const typename BasicVector<ValueType>::RawArray* as_vec_array(const BasicVector<ValueType>* x)
322 return reinterpret_cast<const typename BasicVector<ValueType>::RawArray*>(x);
325 //! Shorthand for C++ `rvec`-equivalent type.
326 typedef BasicVector<real> RVec;
327 //! Shorthand for C++ `dvec`-equivalent type.
328 typedef BasicVector<double> DVec;
329 //! Shorthand for C++ `ivec`-equivalent type.
330 typedef BasicVector<int> IVec;
331 //! Casts a gmx::RVec array into an `rvec` array.
332 static inline rvec* as_rvec_array(RVec* x)
334 return as_vec_array(x);
336 //! Casts a gmx::RVec array into an `rvec` array.
337 static inline const rvec* as_rvec_array(const RVec* x)
339 return as_vec_array(x);
341 //! Casts a gmx::DVec array into an `Dvec` array.
342 static inline dvec* as_dvec_array(DVec* x)
344 return as_vec_array(x);
346 //! Casts a gmx::IVec array into an `ivec` array.
347 static inline ivec* as_ivec_array(IVec* x)
349 return as_vec_array(x);
353 //! Casts a gmx::DVec array into an `dvec` array.
354 static inline const dvec* as_dvec_array(const DVec* x)
356 return as_vec_array(x);
358 //! Casts a gmx::IVec array into an `ivec` array.
359 static inline const ivec* as_ivec_array(const IVec* x)
361 return as_vec_array(x);
364 //! Shorthand for C++ `ivec`-equivalent type.
365 typedef BasicVector<int> IVec;
369 #endif // include guard