File: Vector.h

package info (click to toggle)
psurface 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: cpp: 12,339; makefile: 111; awk: 38
file content (143 lines) | stat: -rw-r--r-- 3,180 bytes parent folder | download | duplicates (3)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef VECTOR_H
#define VECTOR_H

#include "StaticVector.h"
#include <vector>
#include <cmath>
#include <assert.h>

namespace psurface {
  template <typename T>
  class Vector
    : public std::vector<StaticVector<T, 2> >
  {
  public:
    typedef StaticVector<T, 2> VType;

    /** \brief Default constructor.  Leaves the vector uninitialized. */
    Vector(const int& n)
      : std::vector<VType>(n)
    {}

    /** \brief Construction from a single scalar */
    explicit Vector(const int& n, const VType& s)
      : std::vector<VType>(n)
    {
      this->assign(n, s);
    }

    /** \brief Copy constructor */
    Vector(const Vector<T>& other)
      : std::vector<VType>(other.size())
    {
      for (int i=0; i<this->size(); i++)
        (*this)[i] = other[i];
    }

    /** \brief Addition */
    Vector<T>& operator+=(const Vector<T>& other) {
      assert(other.size() == this->size());

      for (size_t i=0; i<this->size(); i++)
        (*this)[i] += other[i];

      return *this;
    }

    /** \brief Division */
    Vector<T>& operator/=(const T& divisor) {
      for (size_t i=0; i<this->size(); i++)
        (*this)[i] /= divisor;

      return *this;
    }

    /** \brief Unary minus */
    friend Vector<T> operator-(const Vector<T>& a) {
      Vector<T> result(a.size());

      for (size_t i=0; i<a.size(); i++)
        result[i] = -a[i];

      return result;
    }

    /** \brief Addition */
    friend Vector<T> operator+(const Vector<T>& a, const Vector<T>& b) {
      assert(a.size() == b.size());

      Vector<T> result(a.size());

      for (size_t i=0; i<a.size(); i++)
        result[i] = a[i] + b[i];

      return result;
    }

    /** \brief Subtraction */
    friend Vector<T> operator-(const Vector<T>& a, const Vector<T>& b) {
      assert(a.size() == b.size());

      Vector<T> result(a.size());

      for (size_t i=0; i<a.size(); i++)
        result[i] = a[i] - b[i];

      return result;
    }

    /** \brief Vector Product */
    T operator*(const Vector<T>& other) const {
      assert(this->size() == other.size());

      T result = 0;

      for (size_t i=0; i<this->size(); i++)
        result += (*this)[i].dot(other[i]);

      return result;
    }

    /** \brief Scalar multiplication from the left */
    friend Vector<T> operator*(const T& s, const Vector<T>& a) {
      Vector<T> result(a.size());

      for (size_t i=0; i<a.size(); i++)
        result[i] = s * a[i];

      return result;
    }

    /** \brief Scalar multiplication from the right */
    friend Vector<T> operator*(const Vector<T>& a, const T& s) {
      return s * a;
    }

    /** \brief Scalar division */
    friend Vector<T> operator/(const Vector<T>& a, const T& s) {
      Vector<T> result(a.size());

      for (size_t i=0; i<a.size(); i++)
        result[i] = a[i] / s;

      return result;
    }

    /** \brief Vector product */
    T dot(const Vector<T>& a) const {
      return (*this) * a;
    }

    /** \brief Euclidean length */
    T length() const {
      return sqrt(dot(*this));
    }

    /** \brief Euclidean length squared */
    T length2() const {
      return dot(*this);
    }
  };
} // namespace psurface

#endif