SpatialOps
Numeric3Vec.h
1 /*
2  * \file Numeric3Vec.h
3  *
4  * \date Aug 16, 2013
5  * \author John Hutchins
6  *
7  * Copyright (c) 2014-2017 The University of Utah
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to
11  * deal in the Software without restriction, including without limitation the
12  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13  * sell copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25  * IN THE SOFTWARE.
26  */
27 
28 #ifndef Numeric3Vec_H_
29 #define Numeric3Vec_H_
30 
31 #include <ostream>
32 #include <vector>
33 #include <cassert>
34 #include <iosfwd>
35 
36 
37 namespace SpatialOps{
38 
44 template<typename T>
46 {
47  T ijk[3];
48 
49  public:
50 
51  Numeric3Vec(){ ijk[0]=0; ijk[1]=0; ijk[2]=0; }
52 
53  inline Numeric3Vec( const T i, const T j, const T k ){
54  ijk[0]=i; ijk[1]=j; ijk[2]=k;
55  }
56 
57  inline Numeric3Vec( const T vec[3] ){
58  ijk[0]=vec[0]; ijk[1]=vec[1]; ijk[2]=vec[2];
59  }
60 
61  inline Numeric3Vec( const std::vector<T>& vec ){
62  assert(vec.size() == 3);
63  ijk[0]=vec[0]; ijk[1]=vec[1]; ijk[2]=vec[2];
64  }
65 
66  inline Numeric3Vec( const Numeric3Vec& x ){
67  ijk[0]=x.ijk[0]; ijk[1]=x.ijk[1]; ijk[2]=x.ijk[2];
68  }
69 
70  template<typename T1>
71  inline Numeric3Vec(const Numeric3Vec<T1>& x){
72  ijk[0]=(T)(x[0]); ijk[1]=(T)(x[1]); ijk[2]=(T)(x[2]);
73  }
74 
75  inline T operator[](const size_t i) const{
76 # ifndef NDEBUG
77  assert(i<3);
78 # endif
79  return ijk[i];
80  }
81  inline T& operator[](const size_t i){
82 # ifndef NDEBUG
83  assert(i<3);
84 # endif
85  return ijk[i];
86  }
87 
88  template<typename T2>
89  Numeric3Vec& operator=(const Numeric3Vec<T2>& x){
90  ijk[0] = x[0];
91  ijk[1] = x[1];
92  ijk[2] = x[2];
93  return *this;
94  }
95 
96  inline bool operator==(const Numeric3Vec& v) const{
97  return (ijk[0]==v[0]) & (ijk[1]==v[1]) & (ijk[2]==v[2]);
98  }
99  inline bool operator!=(const Numeric3Vec& v) const{
100  return (ijk[0]!=v[0]) | (ijk[1]!=v[1]) | (ijk[2]!=v[2]);
101  }
102 
103 
104  inline bool operator<(const Numeric3Vec<T>& v) const;
105 
106 
107  inline bool operator<=(const Numeric3Vec<T>& v) const;
108 
109 
110  inline bool operator>(const Numeric3Vec<T>& v) const;
111 
112 
113  inline bool operator>=(const Numeric3Vec<T>& v) const;
114 
115  /*
116  * For use in STD::LESS sepecialized below, provides total ordering.
117  */
118  inline bool stl_less(const Numeric3Vec<T>& v) const
119  {
120  if(ijk[0]<v[0]) return true;
121  if(ijk[0]==v[0])
122  {
123  if(ijk[1]<v[1]) return true;
124  if(ijk[1]==v[1]){
125  if(ijk[2]<v[2]) return true;
126  }
127  }
128  return false;
129  }
130 
131 
132  template<typename T2>
133  inline Numeric3Vec operator+( const Numeric3Vec<T2>& v ) const{
134  return Numeric3Vec( ijk[0] + v[0],
135  ijk[1] + v[1],
136  ijk[2] + v[2] );
137  }
138  template<typename T2>
139  inline Numeric3Vec operator-( const Numeric3Vec<T2>& v ) const{
140  return Numeric3Vec( ijk[0] - v[0],
141  ijk[1] - v[1],
142  ijk[2] - v[2] );
143  }
144  template<typename T2>
145  inline Numeric3Vec operator*( const Numeric3Vec<T2>& v ) const{
146  return Numeric3Vec( ijk[0] * v[0],
147  ijk[1] * v[1],
148  ijk[2] * v[2] );
149  }
150  template<typename T2>
151  inline Numeric3Vec operator/( const Numeric3Vec<T2>& v ) const{
152  return Numeric3Vec( ijk[0] / v[0],
153  ijk[1] / v[1],
154  ijk[2] / v[2] );
155  }
156  inline Numeric3Vec operator-() const{
157  return Numeric3Vec( - ijk[0],
158  - ijk[1],
159  - ijk[2] );
160  }
161 
162  template<typename T1>
163  inline Numeric3Vec operator+( const T1 v ) const{
164  return Numeric3Vec( ijk[0] + v,
165  ijk[1] + v,
166  ijk[2] + v );
167  }
168  template<typename T1>
169  inline Numeric3Vec operator-( const T1 v ) const{
170  return Numeric3Vec( ijk[0] - v,
171  ijk[1] - v,
172  ijk[2] - v );
173  }
174  template<typename T1>
175  inline Numeric3Vec operator*( const T1 v ) const{
176  return Numeric3Vec( ijk[0] * v,
177  ijk[1] * v,
178  ijk[2] * v );
179  }
180  template<typename T1>
181  inline Numeric3Vec operator/( const T1 v) const{
182  return Numeric3Vec(ijk[0]/v, ijk[1]/v, ijk[2]/v);
183  }
184 
185  template<typename T2>
186  inline Numeric3Vec& operator+=( const Numeric3Vec<T2>& v ){
187  ijk[0] += v[0];
188  ijk[1] += v[1];
189  ijk[2] += v[2];
190  return *this;
191  }
192  template<typename T2>
193  inline Numeric3Vec& operator-=( const Numeric3Vec<T2>& v ){
194  ijk[0] -= v[0];
195  ijk[1] -= v[1];
196  ijk[2] -= v[2];
197  return *this;
198  }
199 
200  inline T sum() const{
201  return ijk[0]+ijk[1]+ijk[2];
202  }
203 
204  };
205 
206 
207 template<typename T>
208  inline std::ostream& operator<<( std::ostream& os, const Numeric3Vec<T>& v ){
209  os << "[ " << v[0] << "," << v[1] << "," << v[2] << " ]";
210  return os;
211  }
212 
213 } // namespace SpatialOps
214 
215 //Specialization of Less for Numeric3Vecs for use in Map, Set, and other STL objects that require total ordering.
216 namespace std{
217 template<typename T>
218 struct less<SpatialOps::Numeric3Vec<T> >
219 {
220  bool operator()(const SpatialOps::Numeric3Vec<T>& v1, const SpatialOps::Numeric3Vec<T>& v2) const
221  {
222  return v1.stl_less(v2);
223  }
224 };
225 }
226 
227 
228 #endif /* Numeric3Vec_H_ */
provides a template lightweight class for storing 3d vectors of numbers.
Definition: Numeric3Vec.h:45
STL namespace.