d3835d2049e1ab378a593a178aa9c02d4f988f04
[alexxy/gromacs.git] / src / gromacs / math / vectypes.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #ifndef GMX_MATH_VECTYPES_H
38 #define GMX_MATH_VECTYPES_H
39
40 #include <cmath>
41
42 #include <algorithm>
43 #include <type_traits>
44
45 #include "gromacs/utility/gmxassert.h"
46 #include "gromacs/utility/real.h"
47
48 #define XX 0 /* Defines for indexing in */
49 #define YY 1 /* vectors                 */
50 #define ZZ 2
51 #define DIM 3 /* Dimension of vectors    */
52
53 typedef real rvec[DIM];
54
55 typedef double dvec[DIM];
56
57 typedef real matrix[DIM][DIM];
58
59 typedef real tensor[DIM][DIM];
60
61 typedef int ivec[DIM];
62
63 namespace gmx
64 {
65
66 /*! \brief
67  * C++ class for 3D vectors.
68  *
69  * \tparam ValueType  Type
70  *
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()).
77  *
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.
81  *
82  * \inpublicapi
83  */
84 template<typename ValueType>
85 class BasicVector
86 {
87 public:
88     //! Underlying raw C array type (rvec/dvec/ivec).
89     using RawArray = ValueType[DIM];
90
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.");
98
99     //! Constructs default (uninitialized) vector.
100     BasicVector() {}
101     //! Constructs a vector from given values.
102     BasicVector(ValueType x, ValueType y, ValueType z) : x_{ x, y, z } {}
103     /*! \brief
104      * Constructs a vector from given values.
105      *
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.
109      */
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)
125     {
126         return *this = *this + right;
127     }
128     //! Allow inplace subtraction for BasicVector
129     BasicVector<ValueType>& operator-=(const BasicVector<ValueType>& right)
130     {
131         return *this = *this - right;
132     }
133     //! Allow vector addition
134     BasicVector<ValueType> operator+(const BasicVector<ValueType>& right) const
135     {
136         return { x_[0] + right[0], x_[1] + right[1], x_[2] + right[2] };
137     }
138     //! Allow vector subtraction
139     BasicVector<ValueType> operator-(const BasicVector<ValueType>& right) const
140     {
141         return { x_[0] - right[0], x_[1] - right[1], x_[2] - right[2] };
142     }
143     //! Allow vector scalar division
144     BasicVector<ValueType> operator/(const ValueType& right) const
145     {
146         GMX_ASSERT(right != 0, "Cannot divide by zero");
147
148         return *this * (1 / right);
149     }
150     //! Scale vector by a scalar
151     BasicVector<ValueType>& operator*=(const ValueType& right)
152     {
153         x_[0] *= right;
154         x_[1] *= right;
155         x_[2] *= right;
156
157         return *this;
158     }
159     //! Divide vector by a scalar
160     BasicVector<ValueType>& operator/=(const ValueType& right)
161     {
162         GMX_ASSERT(right != 0, "Cannot divide by zero");
163
164         return *this *= 1 / right;
165     }
166     //! Return dot product
167     ValueType dot(const BasicVector<ValueType>& right) const
168     {
169         return x_[0] * right[0] + x_[1] * right[1] + x_[2] * right[2];
170     }
171
172     //! Allow vector vector multiplication (cross product)
173     BasicVector<ValueType> cross(const BasicVector<ValueType>& right) const
174     {
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] };
178     }
179
180     //! Return normalized to unit vector
181     BasicVector<ValueType> unitVector() const
182     {
183         const ValueType vectorNorm = norm();
184         GMX_ASSERT(vectorNorm != 0, "unitVector() should not be called with a zero vector");
185
186         return *this / vectorNorm;
187     }
188
189     //! Length^2 of vector
190     ValueType norm2() const { return dot(*this); }
191
192     //! Norm or length of vector
193     ValueType norm() const { return std::sqrt(norm2()); }
194
195     //! cast to RVec
196     BasicVector<real> toRVec() const { return { real(x_[0]), real(x_[1]), real(x_[2]) }; }
197
198     //! cast to IVec
199     BasicVector<int> toIVec() const
200     {
201         return { static_cast<int>(x_[0]), static_cast<int>(x_[1]), static_cast<int>(x_[2]) };
202     }
203
204     //! cast to DVec
205     BasicVector<double> toDVec() const { return { double(x_[0]), double(x_[1]), double(x_[2]) }; }
206
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_; }
215
216 private:
217     RawArray x_;
218 };
219
220 //! Allow vector scalar multiplication
221 template<typename ValueType>
222 BasicVector<ValueType> operator*(const BasicVector<ValueType>& basicVector, const ValueType& scalar)
223 {
224     return { basicVector[0] * scalar, basicVector[1] * scalar, basicVector[2] * scalar };
225 }
226
227 //! Allow scalar vector multiplication
228 template<typename ValueType>
229 BasicVector<ValueType> operator*(const ValueType& scalar, const BasicVector<ValueType>& basicVector)
230 {
231     return { scalar * basicVector[0], scalar * basicVector[1], scalar * basicVector[2] };
232 }
233
234 /*! \brief
235  * unitv for gmx::BasicVector
236  */
237 template<typename VectorType>
238 static inline VectorType unitVector(const VectorType& v)
239 {
240     return v.unitVector();
241 }
242
243 /*! \brief
244  * norm for gmx::BasicVector
245  */
246 template<typename ValueType>
247 static inline ValueType norm(BasicVector<ValueType> v)
248 {
249     return v.norm();
250 }
251
252 /*! \brief
253  * Square of the vector norm for gmx::BasicVector
254  */
255 template<typename ValueType>
256 static inline ValueType norm2(BasicVector<ValueType> v)
257 {
258     return v.norm2();
259 }
260
261 /*! \brief
262  * cross product for gmx::BasicVector
263  */
264 template<typename VectorType>
265 static inline VectorType cross(const VectorType& a, const VectorType& b)
266 {
267     return a.cross(b);
268 }
269
270 /*! \brief
271  * dot product for gmx::BasicVector
272  */
273 template<typename ValueType>
274 static inline ValueType dot(BasicVector<ValueType> a, BasicVector<ValueType> b)
275 {
276     return a.dot(b);
277 }
278
279 /*! \brief
280  * Multiply two vectors element by element and return the result.
281  */
282 template<typename VectorType>
283 static inline VectorType scaleByVector(const VectorType& a, const VectorType& b)
284 {
285     return { a[0] * b[0], a[1] * b[1], a[2] * b[2] };
286 }
287
288 /*! \brief
289  * Return the element-wise minimum of two vectors.
290  */
291 template<typename VectorType>
292 static inline VectorType elementWiseMin(const VectorType& a, const VectorType& b)
293 {
294     return { std::min(a[0], b[0]), std::min(a[1], b[1]), std::min(a[2], b[2]) };
295 }
296
297 /*! \brief
298  * Return the element-wise maximum of two vectors.
299  */
300 template<typename VectorType>
301 static inline VectorType elementWiseMax(const VectorType& a, const VectorType& b)
302 {
303     return { std::max(a[0], b[0]), std::max(a[1], b[1]), std::max(a[2], b[2]) };
304 }
305
306 /*! \brief
307  * Casts a gmx::BasicVector array into an equivalent raw C array.
308  */
309 template<typename ValueType>
310 static inline typename BasicVector<ValueType>::RawArray* as_vec_array(BasicVector<ValueType>* x)
311 {
312     return reinterpret_cast<typename BasicVector<ValueType>::RawArray*>(x);
313 }
314
315 /*! \brief
316  * Casts a gmx::BasicVector array into an equivalent raw C array.
317  */
318 template<typename ValueType>
319 static inline const typename BasicVector<ValueType>::RawArray* as_vec_array(const BasicVector<ValueType>* x)
320 {
321     return reinterpret_cast<const typename BasicVector<ValueType>::RawArray*>(x);
322 }
323
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)
332 {
333     return as_vec_array(x);
334 }
335 //! Casts a gmx::RVec array into an `rvec` array.
336 static inline const rvec* as_rvec_array(const RVec* x)
337 {
338     return as_vec_array(x);
339 }
340 //! Casts a gmx::DVec array into an `Dvec` array.
341 static inline dvec* as_dvec_array(DVec* x)
342 {
343     return as_vec_array(x);
344 }
345 //! Casts a gmx::IVec array into an `ivec` array.
346 static inline ivec* as_ivec_array(IVec* x)
347 {
348     return as_vec_array(x);
349 }
350
351
352 //! Casts a gmx::DVec array into an `dvec` array.
353 static inline const dvec* as_dvec_array(const DVec* x)
354 {
355     return as_vec_array(x);
356 }
357 //! Casts a gmx::IVec array into an `ivec` array.
358 static inline const ivec* as_ivec_array(const IVec* x)
359 {
360     return as_vec_array(x);
361 }
362
363 //! Shorthand for C++ `ivec`-equivalent type.
364 typedef BasicVector<int> IVec;
365
366 } // namespace gmx
367
368 #endif // include guard