Add initial support for python bindings
[alexxy/gromacs.git] / src / pygromacs / include / vec.h
1 #ifndef VEC_H
2 #define VEC_H
3
4 #include <cstdlib>
5 #include <stdexcept>
6 #include <gromacs/math/vectypes.h>
7
8 template<typename T> class RealVector {
9 public:
10     T x, y, z;
11     RealVector(T v[3]) : x(v[0]), y(v[1]), z(v[2]) {};
12     T operator[] (size_t i) {
13         switch (i) {
14         case 0:
15             return x;
16         case 1:
17             return y;
18         case 2:
19             return z;
20         default:
21             throw std::out_of_range("Inexistent component requested");
22         };
23     };
24 };
25
26 template<typename T, typename Adaptor, typename Inner> class Array {
27 private:
28     Inner *data;
29     size_t length;
30 public:
31     Array(Inner *data, size_t length) : data(data), length(length) {};
32     Adaptor operator[] (size_t i) {
33         if (i >= length)
34             throw std::out_of_range("Array index out of range");
35
36         return Adaptor(data[i]);
37     };
38     size_t len() { return length; };
39 };
40
41 class Matrix {
42 private:
43     real data[3][3];
44 public:
45     Matrix(real data[3][3]) {
46         std::copy((real*) data, ((real*) data) + 9, (real*) this->data);
47     };
48     real get(size_t i, size_t j) {
49         if (i > 2 || j > 2)
50             throw std::out_of_range("Matrix index out of range");
51         return data[i][j];
52     }
53     RealVector<real> get(size_t i) {
54         if (i > 2)
55             throw std::out_of_range("Matrix index out of range");
56         return RealVector<real>(data[i]);
57     }
58 };
59
60 typedef RealVector<real> py_rvec;
61 typedef Array<real, py_rvec, rvec> rvecs;
62 #endif