Blame view

rts/point.h 2.08 KB
f1402849   dmayerich   renewed commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  #ifndef RTS_POINT_H

  #define RTS_POINT_H

  

  //#include "rts/vector.h"
  #include <string.h>

  

  namespace rts

  {

  

  template <class T, int N>

  struct point

  {

  	T p[N];

  

  	CUDA_CALLABLE point()

  	{
  

  	}

  

  	//efficiency constructor, makes construction easier for 1D-4D vectors

  	CUDA_CALLABLE point(T x, T y = (T)0.0, T z = (T)0.0, T w = (T)0.0)

  	{

  		if(N >= 1)

  			p[0] = x;

  		if(N >= 2)

  			p[1] = y;

  		if(N >= 3)

  			p[2] = z;

  		if(N >= 4)

  			p[3] = w;

  	}
  
  	//arithmetic operators
  	CUDA_CALLABLE rts::point<T, N> operator+(rts::vector<T, N> v)
  	{
          rts::point<T, N> r;
  
          //calculate the position of the resulting point
          for(int i=0; i<N; i++)
              r.p[i] = p[i] + v.v[i];
  
          return r;
  	}
  	CUDA_CALLABLE rts::point<T, N> operator-(rts::vector<T, N> v)
  	{
          rts::point<T, N> r;
  
          //calculate the position of the resulting point
          for(int i=0; i<N; i++)
              r.p[i] = p[i] - v.v[i];
  
          return r;
  	}
  	CUDA_CALLABLE rts::vector<T, N> operator-(rts::point<T, N> rhs)
  	{
          rts::vector<T, N> r;
  
          //calculate the position of the resulting point
          for(int i=0; i<N; i++)
              r.v[i] = p[i] - rhs.p[i];
  
          return r;
  	}
  	CUDA_CALLABLE rts::point<T, N> operator*(T rhs)
  	{
          rts::point<T, N> r;
  
          //calculate the position of the resulting point
          for(int i=0; i<N; i++)
              r.p[i] = p[i] * rhs;
  
          return r;
  	}

  

  	CUDA_CALLABLE point(const T(&data)[N])

  	{

  		memcpy(p, data, sizeof(T) * N);

  	}

  

  	std::string toStr()

  	{

  		std::stringstream ss;

  

  		ss<<"(";

  		for(int i=0; i<N; i++)

  		{

  			ss<<p[i];

  			if(i != N-1)

  				ss<<", ";

  		}

  		ss<<")";

  

  		return ss.str();

  	}
  
  	//bracket operator
  	CUDA_CALLABLE T& operator[](int i)
  	{

          return p[i];
      }

  

  };
  
  }	//end namespace rts
  
  template <typename T, int N>
  std::ostream& operator<<(std::ostream& os, rts::point<T, N> p)
  {
      os<<p.toStr();
      return os;
  }
  
  //arithmetic
  template <typename T, int N>
  CUDA_CALLABLE rts::point<T, N> operator*(T lhs, rts::point<T, N> rhs)
  {
      rts::point<T, N> r;
  
      return rhs * lhs;
  }
  

  

  

  #endif