Evita  0.16
evVector.h
Go to the documentation of this file.
1 /*
2  *
3  * EVITA: Efficient Visualization of Terascale Datasets
4  * Copyright (C) 2000-2016 Team Evita
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
19  * MA 02139, USA.
20  *
21  */
22 
23 
24 #ifndef __evVector_h
25 #define __evVector_h
26 
27 
28 #include "evAddress.h"
29 
30 #include <numeric>
31 #include <algorithm>
32 #include <vector>
33 #include <iostream>
34 
35 
36 class evVector : public evDataObject
37 {
38 public:
39  evVector(void);
40  evVector(const unsigned int num_dimensions);
41  evVector(const QccVector vector, const unsigned int num_dimensions);
42  evVector(const evVector &vector);
43  evVector(const evVector &vector, unsigned int num_dimensions);
44  evVector(const evAddress &address);
45 
46  virtual ~evVector(void);
47 
48  virtual const evString GetClassName(void) const
49  {
50  return(evString("evVector"));
51  }
52 
53  void SetDimension(const unsigned int dimension);
54 
55  unsigned int Dimension(void) const;
56  bool IsNull(void) const;
57 
58  double &operator [] (const unsigned int index);
59  const double &operator [] (const unsigned int index) const;
60 
61  evVector operator = (const evVector &vector);
62  evVector operator = (const double value);
63  evVector operator + (const evVector &vector) const;
64  evVector operator - (void) const;
65  evVector operator - (const evVector &vector) const;
66  evVector &operator += (const evVector &vector);
67  evVector &operator -= (const evVector &vector);
68  double operator * (const evVector &vector) const;
69  evVector &operator *= (const double scalar);
70  evVector &operator /= (const double scalar);
71 
72  bool operator == (const evVector &vector) const;
73  bool operator != (const evVector &vector) const;
74 
75  operator double() const;
76 
77  double Norm(void) const;
78  double Angle(void) const;
79  double Max(void) const;
80  void Max(const evVector &vector1, const evVector &vector2);
81  double Min(void) const;
82  void Min(const evVector &vector1, const evVector &vector2);
83 
84  virtual void PrintSelf(const int tabbing) const;
85 
86 protected:
87 
88 private:
89  std::vector<double> Vector;
90 };
91 
92 
93 inline evVector::evVector(void)
94 { }
95 
96 
97 inline evVector::evVector(const unsigned int num_dimensions) :
98  Vector(num_dimensions, 0.0)
99 { }
100 
101 
102 inline evVector::evVector(const QccVector vector,
103  const unsigned int num_dimensions)
104 {
105  unsigned int index;
106 
107  this->Vector = std::vector<double>();
108  for (index = 0; index < num_dimensions; index++)
109  this->Vector.push_back(vector[index]);
110 }
111 
112 
113 inline evVector::evVector(const evVector &vector) :
114  Vector(vector.Vector)
115 { }
116 
117 
118 inline evVector::evVector(const evVector &vector,
119  const unsigned int num_dimensions)
120 {
121  this->Vector = std::vector<double>();
122  for (unsigned int index = 0;
123  index < std::min(num_dimensions, (unsigned int)vector.Vector.size());
124  index++)
125  this->Vector.push_back(vector[index]);
126 }
127 
128 
129 inline evVector::evVector(const evAddress &address)
130 {
131  this->Vector.resize(address.GetNumDimensions());
132  for (unsigned int component = 0; component < this->Vector.size();
133  component++)
134  this->Vector[component] = (double)address.Get(component);
135 }
136 
137 
139 { }
140 
141 
142 inline void evVector::SetDimension(const unsigned int dimension)
143 {
144  double value = 0;
145  this->Vector.resize(dimension, value);
146 }
147 
148 
149 inline unsigned int evVector::Dimension(void) const
150 {
151  return(this->Vector.size());
152 }
153 
154 
155 inline bool evVector::IsNull(void) const
156 {
157  return(this->Vector.empty());
158 }
159 
160 
161 inline double &evVector::operator [] (const unsigned int index)
162 {
163  return(this->Vector[index]);
164 }
165 
166 
167 inline const double &evVector::operator [] (const unsigned int index) const
168 {
169  return(this->Vector[index]);
170 }
171 
172 
174 {
175  this->Vector = vector.Vector;
176 
177  return(*this);
178 }
179 
180 
181 inline evVector evVector::operator = (const double value)
182 {
183  std::fill(this->Vector.begin(),
184  this->Vector.end(),
185  value);
186  return(*this);
187 }
188 
189 
190 inline evVector evVector::operator + (const evVector &vector) const
191 {
192  unsigned int index;
193 
194  if (this->Vector.size() != vector.Vector.size())
195  {
196  evVector result;
197  return(result);
198  }
199 
200  evVector result = *this;
201 
202  for (index = 0;
203  index < result.Vector.size();
204  index++)
205  result.Vector[index] += vector.Vector[index];
206 
207  return(result);
208 }
209 
210 
211 inline evVector evVector::operator - (void) const
212 {
213  unsigned int index;
214 
215  evVector result = *this;
216 
217  for (index = 0;
218  index < result.Vector.size();
219  index++)
220  result.Vector[index] = - result.Vector[index];
221 
222  return(result);
223 }
224 
225 
226 inline evVector evVector::operator - (const evVector &vector) const
227 {
228  unsigned int index;
229 
230  if (this->Vector.size() != vector.Vector.size())
231  {
232  evVector result;
233  return(result);
234  }
235 
236  evVector result = *this;
237 
238  for (index = 0;
239  index < result.Vector.size();
240  index++)
241  result.Vector[index] -= vector.Vector[index];
242 
243  return(result);
244 }
245 
246 
248 {
249  unsigned int index;
250 
251  if (this->Vector.size() != vector.Vector.size())
252  this->Vector = std::vector<double>();
253 
254  else
255  for (index = 0;
256  index < this->Vector.size();
257  index++)
258  this->Vector[index] += vector.Vector[index];
259 
260  return(*this);
261 }
262 
263 
265 {
266  unsigned int index;
267 
268  if (this->Vector.size() != vector.Vector.size())
269  this->Vector = std::vector<double>();
270 
271  else
272  for (index = 0;
273  index < this->Vector.size();
274  index++)
275  this->Vector[index] -= vector.Vector[index];
276 
277  return(*this);
278 }
279 
280 
281 static inline double NaN(void)
282 {
283  union
284  {
285  double x1;
286  unsigned char x2[sizeof(double)];
287  } x;
288 
289  unsigned int i;
290  for (i = 0; i < sizeof(double); i++)
291  x.x2[i] = 0xff;
292 
293  return(x.x1);
294 }
295 
296 
297 inline double evVector::operator * (const evVector &vector) const
298 {
299  if (this->Vector.size() != vector.Vector.size())
300  return(NaN());
301 
302  return(std::inner_product(this->Vector.begin(),
303  this->Vector.end(),
304  vector.Vector.begin(),
305  (double)0));
306 }
307 
308 
309 inline evVector operator * (const double scalar, const evVector &vector)
310 {
311  unsigned int index;
312 
313  evVector result = vector;
314 
315  for (index = 0; index < vector.Dimension(); index++)
316  result[index] *= scalar;
317 
318  return(result);
319 }
320 
321 
322 inline evVector operator * (const evVector &vector, const double scalar)
323 {
324  unsigned int index;
325 
326  evVector result = vector;
327 
328  for (index = 0; index < vector.Dimension(); index++)
329  result[index] *= scalar;
330 
331  return(result);
332 }
333 
334 
335 inline evVector &evVector::operator *= (const double scalar)
336 {
337  unsigned int index;
338 
339  for (index = 0;
340  index < this->Vector.size();
341  index++)
342  this->Vector[index] *= scalar;
343 
344  return(*this);
345 }
346 
347 
348 inline evVector operator / (const evVector &vector, const double scalar)
349 {
350  unsigned int index;
351 
352  evVector result = vector;
353 
354  for (index = 0; index < vector.Dimension(); index++)
355  result[index] /= scalar;
356 
357  return(result);
358 }
359 
360 
361 inline evVector &evVector::operator /= (const double scalar)
362 {
363  unsigned int index;
364 
365  for (index = 0;
366  index < this->Vector.size();
367  index++)
368  this->Vector[index] /= scalar;
369 
370  return(*this);
371 }
372 
373 
374 inline bool evVector::operator == (const evVector &vector) const
375 {
376  return(this->Vector == vector.Vector);
377 }
378 
379 
380 inline bool evVector::operator != (const evVector &vector) const
381 {
382  return(this->Vector != vector.Vector);
383 }
384 
385 
386 inline evVector::operator double() const
387 {
388  if (this->Vector.size() == 1)
389  return(this->Vector[0]);
390  else
391  return(0);
392 }
393 
394 
395 inline double evVector::Norm(void) const
396 {
397  return(sqrt((*this) * (*this)));
398 }
399 
400 
401 inline double evVector::Angle(void) const
402 {
403  if (this->Dimension() != 2)
404  return(0);
405  else
406  return(atan2((*this)[1], (*this)[0]) + M_PI);
407 }
408 
409 
410 inline double evVector::Max(void) const
411 {
412  if (this->Vector.empty())
413  return(NaN());
414 
415  double max = *std::max_element(this->Vector.begin(),
416  this->Vector.end());
417 
418  return(max);
419 }
420 
421 
422 inline void evVector::Max(const evVector &vector1, const evVector &vector2)
423 {
424  if (vector1.Vector.size() != vector2.Vector.size())
425  return;
426 
427  this->Vector.resize(vector1.Vector.size());
428 
429  for (unsigned int component = 0;
430  component < this->Vector.size();
431  component++)
432  (*this)[component] = std::max(vector1[component],
433  vector2[component]);
434 }
435 
436 
437 inline double evVector::Min(void) const
438 {
439  if (this->Vector.empty())
440  return(NaN());
441 
442  double min = *std::min_element(this->Vector.begin(),
443  this->Vector.end());
444 
445  return(min);
446 }
447 
448 
449 inline void evVector::Min(const evVector &vector1, const evVector &vector2)
450 {
451  if (vector1.Vector.size() != vector2.Vector.size())
452  return;
453 
454  this->Vector.resize(vector1.Vector.size());
455 
456  for (unsigned int component = 0;
457  component < this->Vector.size();
458  component++)
459  (*this)[component] = std::min(vector1[component],
460  vector2[component]);
461 }
462 
463 
464 inline std::ostream &operator << (std::ostream &outfile,
465  const evVector &vector)
466 {
467  outfile << "< ";
468 
469  if (vector.IsNull())
470  outfile << "NULL ";
471  else
472  {
473  unsigned int index;
474  for (index = 0; index < vector.Dimension(); index++)
475  outfile << vector[index] << " ";
476  }
477 
478  outfile << ">";
479 
480  return(outfile);
481 }
482 
483 
484 #endif
evVector operator-(void) const
Definition: evVector.h:211
double operator*(const evVector &vector) const
Definition: evVector.h:297
unsigned int Dimension(void) const
Definition: evVector.h:149
static double NaN(void)
Definition: evVector.h:281
void SetDimension(const unsigned int dimension)
Definition: evVector.h:142
bool operator!=(const evVector &vector) const
Definition: evVector.h:380
virtual ~evVector(void)
Definition: evVector.h:138
double Min(void) const
Definition: evVector.h:437
virtual void PrintSelf(const int tabbing) const
Definition: evVector.h:36
double & operator[](const unsigned int index)
Definition: evVector.h:161
Definition: evAddress.h:37
virtual const evString GetClassName(void) const
Definition: evVector.h:48
Definition: evDataObject.h:31
evVector operator/(const evVector &vector, const double scalar)
Definition: evVector.h:348
int * Get(void)
Definition: evAddress.h:124
double Angle(void) const
Definition: evVector.h:401
std::ostream & operator<<(std::ostream &outfile, const evVector &vector)
Definition: evVector.h:464
bool IsNull(void) const
Definition: evVector.h:155
evVector & operator-=(const evVector &vector)
Definition: evVector.h:264
evVector operator=(const evVector &vector)
Definition: evVector.h:173
double Norm(void) const
Definition: evVector.h:395
unsigned int GetNumDimensions(void) const
Definition: evAddress.h:148
std::vector< double > Vector
Definition: evVector.h:89
evVector & operator/=(const double scalar)
Definition: evVector.h:361
double Max(void) const
Definition: evVector.h:410
bool operator==(const evVector &vector) const
Definition: evVector.h:374
evVector & operator+=(const evVector &vector)
Definition: evVector.h:247
Definition: evString.h:30
evVector & operator*=(const double scalar)
Definition: evVector.h:335
evVector operator+(const evVector &vector) const
Definition: evVector.h:190
evVector(void)
Definition: evVector.h:93