Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Rocon/include/NVec.h
Go to the documentation of this file.
1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4 #include <cstdarg>
5 #include <fstream>
6 #include <cmath>
7 
8 #ifndef _NVEC_H_
9 #define _NVEC_H_
10 
11 using namespace std;
12 
13 namespace nvc
14 {
15 
16  template <int DIM, typename T>
17  class NVec : public vector<T>
18  {
19  public:
20  NVec():vector<T>(DIM){};//this->resize(DIM);}
21 
22  NVec(T * v):vector<double>(DIM)
23  {
24  for(int i =0;i<DIM;i++)
25  this->push_back(v[i]);
26  }
27 
28  NVec(T t1,...): vector<T>(DIM)
29  {
30  va_list marker;
31  this->push_back(t1);
32  va_start( marker, t1);
33  for(int i =1;i<DIM;i++)
34  this->push_back((T) va_arg( marker, T));
35  va_end( marker );
36  }
37 
38  /*****************************************/
39  // Some more constructors
40  NVec(T t1, T t2, T t3, T t4):vector<T>(DIM)
41  {
42  (*this)[0] = t1;(*this)[1] = t2;
43  (*this)[2] = t3;(*this)[3] = t4;
44  }
45  NVec(T t1, T t2, T t3):vector<T>(DIM)
46  {
47  (*this)[0] = t1;(*this)[1] = t2;(*this)[2] = t3;
48  }
49  NVec(T t1, T t2):vector<T>(DIM)
50  {
51  (*this)[0] = t1;(*this)[1] = t2;
52  }
53  /******************************************/
54 
55  virtual ~NVec(){};
56 
58  {
59  va_list marker;
60  this->push_back(t1);
61  va_start( marker, t1);
62  for(int i =1;i<DIM;i++)
63  this->push_back((T) va_arg( marker, T));
64  va_end( marker );
65  }
66 
68  {
69  for(int i=0;i<DIM;i++)
70  (*this)[i]=s;
71  return *this;
72  }
73  };
74 
75  template<int DIM, typename T>
76  inline ostream &operator<<(ostream &out, const NVec<DIM,T>& v)
77  {
78  //out << "( ";
79  for(int i =0;i<DIM-1;i++) out << v[i] << " ";
80  out << v[DIM-1];
81  //out << " )";
82  return out;
83  }
84 
85  template<int DIM, typename T>
86  inline std::istream &operator>>(std::istream &in, NVec<DIM,T>& v)
87  {
88  for(int i =0;i<DIM;i++) in >> v[i];
89  return in;
90  }
91 
92  template<int DIM, typename T>
93  inline bool operator==(const NVec<DIM,T>& v1, const NVec<DIM,T>& v2)
94  {
95  for(int i =0;i<DIM;i++)
96  if (v1[i] != v2[i]) return false;
97  return true;
98  }
99 
100  /****************************************************************/
101  // Arithmetic operators
102 
103  template<int DIM, typename T>
104  inline NVec<DIM,T> operator+(const NVec<DIM,T> &u, const NVec<DIM,T>& v)
105  {
106  NVec<DIM,T> n;
107  for(int i=0;i<DIM;i++)
108  n[i]=u[i]+v[i];
109  return n;
110  }
111 
112  template<int DIM, typename T>
114  {
115  for(int i=0;i<DIM;i++)
116  u[i]=u[i]+v[i];
117  return u;
118  }
119 
120 
121  template<int DIM, typename T>
122  inline NVec<DIM,T> operator-(const NVec<DIM,T> &u, const NVec<DIM,T>& v)
123  {
124  NVec<DIM,T> n;
125  for(int i=0;i<DIM;i++)
126  n[i]=u[i]-v[i];
127  return n;
128  }
129 
130  template<int DIM, typename T> inline NVec<DIM,T> operator-(const NVec<DIM,T> &v)
131  {
132  NVec<DIM,T> n;
133  for(int i=0;i<DIM;i++)
134  n[i]=-v[i];
135  return n;
136  }
137 
138  template<int DIM, class T, class N>
139  inline NVec<DIM,T> operator*(N s, const NVec<DIM,T> &v)
140  {
141  NVec<DIM,T> n;
142  for(int i=0;i<DIM;i++)
143  n[i]=s*v[i];
144  return n;
145  }
146 
147  template<int DIM, class T, class N>
148  inline NVec<DIM,T> operator*(const NVec<DIM,T> &v, N s)
149  { return s*v; }
150 
151  template<int DIM, class T, class N>
152  inline NVec<DIM,T> operator/(const NVec<DIM,T> &v, N s)
153  {
154  NVec<DIM,T> n;
155  for(int i=0;i<DIM;i++)
156  n[i]=v[i]/(T)s;
157  return n;
158  }
159 
160  template<int DIM, class T, class N>
162  {
163  for(int i=0;i<DIM;i++)
164  v[i]=v[i]/(T)s;
165  return v;
166  }
167 
168  template<int DIM, typename T>
169  inline T operator*(const NVec<DIM,T> &u, const NVec<DIM,T>& v)
170  {
171  T n = 0;
172  for(int i=0;i<DIM;i++)
173  n += (u[i]*v[i]);
174  return n;
175  }
176 
177  // Implement n-dimensional "cross product"
178  // template<int DIM, typename T> inline NVec<DIM,T> cross(const NVec<DIM,T>& u, const NVec<DIM,T>& v)
179  // {
180  // return NVec<DIM,T>( u[1]*v[2] - v[1]*u[2],
181  // -u[0]*v[2] + v[0]*u[2],
182  // u[0]*v[1] - v[0]*u[1] );
183  // }
184 
185  template<typename T> inline NVec<3,T> cross(const NVec<3,T>& u, const NVec<3,T>& v)
186  {
187  return NVec<3,T>( u[1]*v[2] - v[1]*u[2],
188  -u[0]*v[2] + v[0]*u[2],
189  u[0]*v[1] - v[0]*u[1] );
190  }
191 
192 
193  template<int DIM, typename T> inline T norm2(const NVec<DIM,T>& v) { return v*v; }
194 
195  template<int DIM, typename T> inline T norm(const NVec<DIM,T>& v) { return sqrt(norm2(v)); }
196 
197  template<int DIM, typename T> inline void unitize(NVec<DIM,T>& v)
198  {
199  T l = norm2(v);
200  if( l!=1.0 && l!=0.0 ) v /= sqrt(l);
201  }
202 
203  template<int DIM, typename T> inline T euclid_distance(const NVec<DIM,T>& u, const NVec<DIM,T>& v)
204  { return norm(v-u); }
205 
206  template<int DIM, typename T>
207  inline T angle(const NVec<DIM,T> &u, const NVec<DIM,T>& v)
208  {
209  return acos((u*v)/(norm(u) * norm(v)));
210  }
211 
212  template<int DIM, typename T>
213  inline T angle_unitized(const NVec<DIM,T> &u, const NVec<DIM,T>& v)
214  {
215  return acos(u*v);
216  }
217 typedef unsigned int id_t;
228 
229 
230 };
231 
232 #endif
NVec< DIM, T > & operator()(T t1,...)
NVec< DIM, T > & operator=(T s)
T euclid_distance(const NVec< DIM, T > &u, const NVec< DIM, T > &v)
CGAL_END_NAMESPACE CGAL_BEGIN_NAMESPACE bool operator==(const Origin &o, const Point_2< R > &p)
Definition: Point_2.h:239
Vector_3< T > operator*(T t, const Vector_3< T > &v)
Definition: mapbasic.h:139
NVec< 3, T > cross(const NVec< 3, T > &u, const NVec< 3, T > &v)
NVec(T t1, T t2, T t3, T t4)
NVec< 2, unsigned int > IdxHEdge
double s
Definition: blastest.C:80
NVec< 2, float > TexCoord
NVec(T t1, T t2)
Iterator_from_circulator< C, Ref, Ptr > operator+(Dist n, const Iterator_from_circulator< C, Ref, Ptr > &circ)
Definition: circulator.h:689
virtual ~NVec()
double sqrt(double d)
Definition: double.h:73
NVec< 3, double > Vec3D
T norm(const NVec< DIM, T > &v)
NVec< 3, unsigned int > Tri
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed free of to any person **obtaining a copy of this software and associated documentation to deal with the Software without including without limitation the rights to and or **sell copies of the and to permit persons to whom the **Software is furnished to do subject to the following this list of conditions and the following disclaimers ****Redistributions in binary form must reproduce the above **copyright this list of conditions and the following **disclaimers in the documentation and or other materials **provided with the distribution ****Neither the names of the Center for Simulation of Advanced the University of nor the names of its **contributors may be used to endorse or promote products derived **from this Software without specific prior written permission ****THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY **EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES **OF FITNESS FOR A PARTICULAR PURPOSE AND **NONINFRINGEMENT IN NO EVENT SHALL THE CONTRIBUTORS OR **COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR **ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE **USE OR OTHER DEALINGS WITH THE SOFTWARE v
Definition: roccomf90.h:20
NVec< DIM, T > operator/=(NVec< DIM, T > &v, N s)
T norm2(const NVec< DIM, T > &v)
void unitize(NVec< DIM, T > &v)
NVec< 3, double > Vector3D
NVec(T t1, T t2, T t3)
NVec< 3, double > Vertex3D
blockLoc i
Definition: read.cpp:79
double angle(Vector_3< double > v1, Vector_3< double > v2)
Compute the angle between two vectors.
Definition: geometry.C:61
const NT & n
NVec< DIM, T > operator+=(NVec< DIM, T > &u, const NVec< DIM, T > &v)
NVec< 4, float > rgbColor
NVec< 3, double > Normal3D
CImg< _cimg_Tfloat > acos(const CImg< T > &instance)
Definition: CImg.h:6051
std::istream & operator>>(std::istream &is, CGAL::Aff_transformation_2< R > &t)
unsigned int id_t
Vector_n operator/(const Array_n_const &v1, Real a)
Definition: Vector_n.h:323
NVec(T t1,...)
Point_2< R > operator-(const Origin &o, const Vector_2< R > &v)
NVec< 3, unsigned int > Quad
T angle_unitized(const NVec< DIM, T > &u, const NVec< DIM, T > &v)
NVec< 3, double > Point3D