Titan  v1.0
A high-performance CUDA-based physics simulation sandbox for robotics, physics, and reinforcement learning.
vec.h
Go to the documentation of this file.
1 //
2 // vec.hpp
3 // CUDA Physics
4 //
5 // Created by Jacob Austin on 5/13/18.
6 // Copyright © 2018 Jacob Austin. All rights reserved.
7 //
8 
9 #ifndef VEC_H
10 #define VEC_H
11 
12 #ifdef __CUDACC__
13 #define CUDA_CALLABLE_MEMBER __host__ __device__
14 #else
15 #define CUDA_CALLABLE_MEMBER
16 #endif
17 
18 #ifdef __CUDACC__
19 #define CUDA_DEVICE __device__
20 #else
21 #define CUDA_DEVICE
22 #endif
23 
24 #include <iostream>
25 #include <cuda_runtime.h>
26 #include <cuda.h>
27 #include <iostream>
28 #include <cmath>
29 #include <vector>
30 
31 namespace titan {
32 
33 class Vec {
34 public:
36  data[0] = 0;
37  data[1] = 0;
38  data[2] = 0;
39  } // default
40 
42  data[0] = v.data[0];
43  data[1] = v.data[1];
44  data[2] = v.data[2];
45  } // copy constructor
46 
47  CUDA_CALLABLE_MEMBER Vec(double x, double y, double z) {
48  data[0] = x;
49  data[1] = y;
50  data[2] = z;
51  } // initialization from x, y, and z values
52 
53  CUDA_CALLABLE_MEMBER Vec(const std::vector<double> & v) {
54  data[0] = v[0];
55  data[1] = v[1];
56  data[2] = v[2];
57  }
58 
60  if (this == &v) {
61  return *this;
62  }
63 
64  data[0] = v.data[0];
65  data[1] = v.data[1];
66  data[2] = v.data[2];
67 
68  return *this;
69  }
70 
72  data[0] += v.data[0];
73  data[1] += v.data[1];
74  data[2] += v.data[2];
75  return *this;
76  }
77 
79  data[0] -= v.data[0];
80  data[1] -= v.data[1];
81  data[2] -= v.data[2];
82  return *this;
83  }
84 
85  CUDA_DEVICE void atomicVecAdd(const Vec & v);
86 
88  return Vec(-data[0], -data[1], -data[2]);
89  }
90 
92  if (n < 0 || n >= 3) {
93  printf("%s\n", "Out of bounds!");
94  return data[0];
95  } else {
96  return data[n];
97  }
98  }
99 
100  CUDA_CALLABLE_MEMBER const double & operator [] (int n) const {
101  if (n < 0 || n >= 3) {
102  printf("%s\n", "Out of bounds!");
103  return data[0];
104  } else {
105  return data[n];
106  }
107  }
108 
109  CUDA_CALLABLE_MEMBER friend Vec operator+(const Vec & v1, const Vec & v2) {
110  return Vec(v1.data[0] + v2.data[0], v1.data[1] + v2.data[1], v1.data[2] + v2.data[2]);
111  }
112 
113  CUDA_CALLABLE_MEMBER friend Vec operator-(const Vec & v1, const Vec & v2) {
114  return Vec(v1.data[0] - v2.data[0], v1.data[1] - v2.data[1], v1.data[2] - v2.data[2]);
115  }
116 
117  CUDA_CALLABLE_MEMBER friend Vec operator*(const double x, const Vec & v) {
118  return Vec(v.data[0] * x, v.data[1] * x, v.data[2] * x);
119  }
120 
121  CUDA_CALLABLE_MEMBER friend Vec operator*(const Vec & v, const double x) {
122  return x * v;
123  } // double times Vec
124 
125  CUDA_CALLABLE_MEMBER friend bool operator==(const Vec & v1, const Vec & v2) {
126  return (v1[0] == v2[0] && v1[1] == v2[1] && v1[2] == v2[2]);
127  }
128 
129  CUDA_CALLABLE_MEMBER friend Vec operator*(const Vec & v1, const Vec & v2) {
130  return Vec(v1.data[0] * v2.data[0], v1.data[1] * v2.data[1], v1.data[2] * v2.data[2]);
131  } // Multiplies two Vecs (elementwise)
132 
133  CUDA_CALLABLE_MEMBER friend Vec operator/(const Vec & v, const double x) {
134  return Vec(v.data[0] / x, v.data[1] / x, v.data[2] / x);
135  } // vector over double
136 
137  CUDA_CALLABLE_MEMBER friend Vec operator/(const Vec & v1, const Vec & v2) {
138  return Vec(v1.data[0] / v2.data[0], v1.data[1] / v2.data[1], v1.data[2] / v2.data[2]);
139  } // divides two Vecs (elementwise)
140 
141  friend std::ostream & operator << (std::ostream & strm, const Vec & v) {
142  return strm << "(" << v[0] << ", " << v[1] << ", " << v[2] << ")";
143  } // print
144 
146  printf("(%3f, %3f, %3f)\n", data[0], data[1], data[2]);
147  }
148 
149  CUDA_CALLABLE_MEMBER double norm() const {
150  return sqrt(pow(data[0], 2) + pow(data[1], 2) + pow(data[2], 2));
151  } // gives vector norm
152 
153  CUDA_CALLABLE_MEMBER double sum() const {
154  return data[0] + data[1] + data[2];
155  } // sums all components of the vector
156 
158  return *this / norm();
159  }
160 
161 private:
162  double data[3] = { 0 }; // initialize data to 0
163 };
164 
165 CUDA_CALLABLE_MEMBER double dot(const Vec & a, const Vec & b);
166 CUDA_CALLABLE_MEMBER Vec cross(const Vec &v1, const Vec &v2);
167 
168 } // namespace titan
169 
170 #endif
titan
Definition: mass.h:11
titan::Vec::operator=
CUDA_CALLABLE_MEMBER Vec & operator=(const Vec &v)
Definition: vec.h:59
titan::Vec::norm
CUDA_CALLABLE_MEMBER double norm() const
Definition: vec.h:149
titan::dot
CUDA_CALLABLE_MEMBER double dot(const Vec &a, const Vec &b)
titan::Vec::print
CUDA_CALLABLE_MEMBER void print()
Definition: vec.h:145
titan::Vec::operator-
CUDA_CALLABLE_MEMBER Vec operator-() const
Definition: vec.h:87
titan::Vec::Vec
CUDA_CALLABLE_MEMBER Vec(const Vec &v)
Definition: vec.h:41
titan::Vec::operator<<
friend std::ostream & operator<<(std::ostream &strm, const Vec &v)
Definition: vec.h:141
titan::Vec::operator[]
CUDA_CALLABLE_MEMBER double & operator[](int n)
Definition: vec.h:91
titan::Vec::Vec
CUDA_CALLABLE_MEMBER Vec()
Definition: vec.h:35
CUDA_DEVICE
#define CUDA_DEVICE
Definition: vec.h:21
titan::Vec
Definition: vec.h:33
titan::Vec::atomicVecAdd
CUDA_DEVICE void atomicVecAdd(const Vec &v)
titan::Vec::operator*
CUDA_CALLABLE_MEMBER friend Vec operator*(const double x, const Vec &v)
Definition: vec.h:117
titan::Vec::sum
CUDA_CALLABLE_MEMBER double sum() const
Definition: vec.h:153
titan::Vec::operator+
CUDA_CALLABLE_MEMBER friend Vec operator+(const Vec &v1, const Vec &v2)
Definition: vec.h:109
titan::Vec::operator+=
CUDA_CALLABLE_MEMBER Vec & operator+=(const Vec &v)
Definition: vec.h:71
titan::Vec::operator-
CUDA_CALLABLE_MEMBER friend Vec operator-(const Vec &v1, const Vec &v2)
Definition: vec.h:113
titan::Vec::operator*
CUDA_CALLABLE_MEMBER friend Vec operator*(const Vec &v, const double x)
Definition: vec.h:121
CUDA_CALLABLE_MEMBER
#define CUDA_CALLABLE_MEMBER
Definition: vec.h:15
titan::Vec::normalize
CUDA_CALLABLE_MEMBER Vec normalize() const
Definition: vec.h:157
titan::Vec::operator/
CUDA_CALLABLE_MEMBER friend Vec operator/(const Vec &v, const double x)
Definition: vec.h:133
titan::Vec::operator==
CUDA_CALLABLE_MEMBER friend bool operator==(const Vec &v1, const Vec &v2)
Definition: vec.h:125
titan::Vec::operator-=
CUDA_CALLABLE_MEMBER Vec & operator-=(const Vec &v)
Definition: vec.h:78
titan::Vec::operator*
CUDA_CALLABLE_MEMBER friend Vec operator*(const Vec &v1, const Vec &v2)
Definition: vec.h:129
titan::Vec::Vec
CUDA_CALLABLE_MEMBER Vec(double x, double y, double z)
Definition: vec.h:47
titan::Vec::Vec
CUDA_CALLABLE_MEMBER Vec(const std::vector< double > &v)
Definition: vec.h:53
titan::Vec::operator/
CUDA_CALLABLE_MEMBER friend Vec operator/(const Vec &v1, const Vec &v2)
Definition: vec.h:137
titan::cross
CUDA_CALLABLE_MEMBER Vec cross(const Vec &v1, const Vec &v2)