Browse Source

switched vector3d to template class

develop
Robin Thoni 7 years ago
parent
commit
f466a63e0c

+ 13
- 3
TheGame/openglrenderdevice.cpp View File

@@ -43,13 +43,21 @@ void OpenGLRenderDevice::initialize(int fov, int width, int height)
43 43
     _fov = fov;
44 44
     _width = width;
45 45
     _height = height;
46
+
46 47
     glClearColor(_clearColor.redF(), _clearColor.greenF(),
47 48
                  _clearColor.blueF(), _clearColor.alphaF());
48 49
 
50
+
51
+//    GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};  /* Red diffuse light. */
52
+//    GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */
53
+//    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
54
+//    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
55
+//    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, light_diffuse);
56
+//    glEnable(GL_LIGHT0);
57
+//    glEnable(GL_LIGHTING);
58
+    glDisable(GL_LIGHTING);
59
+
49 60
     glEnable(GL_DEPTH_TEST);
50
-    glEnable(GL_CULL_FACE);
51
-    glShadeModel(GL_SMOOTH);
52
-    glEnable(GL_MULTISAMPLE);
53 61
 
54 62
     glMatrixMode(GL_PROJECTION);
55 63
     gluPerspective(_fov, _width / _height, 0.1, 100.0);
@@ -81,6 +89,8 @@ void OpenGLRenderDevice::postDraw()
81 89
 
82 90
 void OpenGLRenderDevice::drawVertex(const ColorVector3D &point)
83 91
 {
92
+//    GLfloat d[] = { point.getColor().redF(), point.getColor().greenF(), point.getColor().blueF(), point.getColor().alphaF() };
93
+//    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, d);
84 94
     glColor4f(point.getColor().redF(), point.getColor().greenF(),
85 95
                   point.getColor().blue(), point.getColor().alpha());
86 96
     glVertex3d(point.getX(), point.getY(), point.getZ());

+ 4
- 3
TheGame/renderwidget.cpp View File

@@ -1,5 +1,4 @@
1 1
 #include "renderwidget.h"
2
-#include <GL/glu.h>
3 2
 #include <QTimer>
4 3
 #include <math.h>
5 4
 #include <QDebug>
@@ -17,17 +16,19 @@ RenderWidget::RenderWidget(QWidget *parent) :
17 16
     _device = new OpenGLRenderDevice(this);
18 17
     _engine = new UGameEngine(_device);
19 18
     UGEEntityCube* cube = new UGEEntityCube(_engine);
20
-    cube->move(Vector3D(0, 1, 0));
19
+//    cube->move(Vector3D(0, 1, 0));
20
+    cube->hide();
21 21
     _engine->addEntity(cube);
22 22
     _engine->addEntity(new UGEEntityAxes(_engine));
23 23
     setMouseTracking(true);
24 24
     setFocusPolicy(Qt::StrongFocus);
25 25
 
26 26
     WaveFrontObj* wavefrontObj = new WaveFrontObj(this);
27
-    qDebug() << wavefrontObj->openFile("/home/robin/Downloads/enterprise/obj/USSEnterprise.obj");
27
+    wavefrontObj->openFile("/home/robin/Downloads/enterprise/obj/USSEnterprise.obj");
28 28
 
29 29
     UGEEntityWaveFrontObj* obj = new UGEEntityWaveFrontObj(wavefrontObj, this);
30 30
     _engine->addEntity(obj);
31
+//    obj->hide();
31 32
 
32 33
 }
33 34
 

+ 6
- 2
UGameEngine/UGameEngine.pro View File

@@ -19,7 +19,8 @@ SOURCES += engine/ugameengine.cpp \
19 19
     entities/ugeentitycube.cpp \
20 20
     entities/ugeentityaxes.cpp \
21 21
     utils/wavefrontobj.cpp \
22
-    entities/ugeentitywavefrontobj.cpp
22
+    entities/ugeentitywavefrontobj.cpp \
23
+    utils/texturevector3d.cpp
23 24
 
24 25
 HEADERS += engine/ugameengine.h\
25 26
     engine/abstractrenderdevice.h \
@@ -29,7 +30,10 @@ HEADERS += engine/ugameengine.h\
29 30
     entities/ugeentitycube.h \
30 31
     entities/ugeentityaxes.h \
31 32
     utils/wavefrontobj.h \
32
-    entities/ugeentitywavefrontobj.h
33
+    entities/ugeentitywavefrontobj.h \
34
+    utils/texturevector3d.h \
35
+    utils/vectorxd.h \
36
+    utils/vectorxd.hxx
33 37
 
34 38
 
35 39
 # FLEX && BISON

+ 9
- 0
UGameEngine/engine/abstractrenderdevice.cpp View File

@@ -1,4 +1,5 @@
1 1
 #include "abstractrenderdevice.h"
2
+#include <QImage>
2 3
 
3 4
 AbstractRenderDevice::AbstractRenderDevice(QObject *parent) :
4 5
     QObject(parent)
@@ -42,6 +43,14 @@ void AbstractRenderDevice::lookAt(const Vector3D &eye, const Vector3D &center, c
42 43
     _lookUp = up;
43 44
 }
44 45
 
46
+//void AbstractRenderDevice::loadTextureFromFile(const QVariant &id, const QString &filename)
47
+//{
48
+//    QImage img;
49
+//    if (img.load(filename)) {
50
+//        loadTexture(id, img);
51
+//    }
52
+//}
53
+
45 54
 QColor AbstractRenderDevice::getClearColor() const
46 55
 {
47 56
     return _clearColor;

+ 6
- 0
UGameEngine/engine/abstractrenderdevice.h View File

@@ -34,6 +34,10 @@ public slots:
34 34
 
35 35
     virtual void lookAt(const Vector3D& eye, const Vector3D& center, const Vector3D& up = Vector3D(0.0, 1.0, 0.0));
36 36
 
37
+//    void loadTextureFromFile(const QVariant& id, const QString& filename);
38
+
39
+//    virtual void loadTexture(const QVariant& id, const QImage& texture) = 0;
40
+
37 41
     virtual void initialize(int fov, int width, int height) = 0;
38 42
 
39 43
     virtual void resize(int width, int height) = 0;
@@ -48,6 +52,8 @@ public slots:
48 52
 
49 53
     virtual void drawPolygon(const QList<ColorVector3D>& points) = 0;
50 54
 
55
+//    virtual void drawPolygonTexture(const QList<ColorVector3D>& points) = 0;
56
+
51 57
 protected:
52 58
     QColor _clearColor;
53 59
 

+ 1
- 1
UGameEngine/entities/ugeentitycube.cpp View File

@@ -13,7 +13,7 @@ float UGEEntityCube::getSize() const
13 13
 
14 14
 void UGEEntityCube::draw(AbstractRenderDevice *device)
15 15
 {
16
-    QColor color;
16
+    QColor color = Qt::red;
17 17
     float r = _size / 2;
18 18
     QList<ColorVector3D> points;
19 19
     points << ColorVector3D(color, -r, -r, r) << ColorVector3D(color, -r, r, r) << ColorVector3D(color, r, r, r) << ColorVector3D(color, r, -r, r);

+ 1
- 1
UGameEngine/entities/ugeentitywavefrontobj.cpp View File

@@ -14,7 +14,7 @@ void UGEEntityWaveFrontObj::draw(AbstractRenderDevice *device)
14 14
         QList<int> face = faces[i];
15 15
         QList<ColorVector3D> poly;
16 16
         for (int j = 0; j < face.size(); ++j) {
17
-            poly.append(ColorVector3D(Qt::gray, vertexes[face[j] - 1]));
17
+            poly.append(ColorVector3D(Qt::black, vertexes[face[j] - 1]));
18 18
         }
19 19
         drawPolygon(device, poly);
20 20
     }

+ 5
- 0
UGameEngine/utils/texturevector3d.cpp View File

@@ -0,0 +1,5 @@
1
+#include "texturevector3d.h"
2
+
3
+TextureVector3D::~TextureVector3D()
4
+{
5
+}

+ 15
- 0
UGameEngine/utils/texturevector3d.h View File

@@ -0,0 +1,15 @@
1
+#ifndef TEXTUREVECTOR3D_H
2
+#define TEXTUREVECTOR3D_H
3
+
4
+#include "colorvector3d.h"
5
+
6
+class TextureVector3D : public ColorVector3D
7
+{
8
+public:
9
+    TextureVector3D(QColor color = Qt::white, double x = 0.0, double y = 0.0, double z = 0.0);
10
+    TextureVector3D(QColor color = Qt::white, const Vector3D& other = Vector3D());
11
+    TextureVector3D(const ColorVector3D& other);
12
+    virtual ~TextureVector3D();
13
+};
14
+
15
+#endif // TEXTUREVECTOR3D_H

+ 22
- 181
UGameEngine/utils/vector3d.cpp View File

@@ -2,16 +2,20 @@
2 2
 #include <math.h>
3 3
 
4 4
 Vector3D::Vector3D(double x, double y, double z)
5
-    : _x(x)
6
-    , _y(y)
7
-    , _z(z)
5
+    : VectorXD<3>()
8 6
 {
7
+    _scalars[0] = x;
8
+    _scalars[1] = y;
9
+    _scalars[2] = z;
9 10
 }
10 11
 
11 12
 Vector3D::Vector3D(const Vector3D &other)
12
-    : _x(other._x)
13
-    , _y(other._y)
14
-    , _z(other._z)
13
+    : VectorXD<3>(other)
14
+{
15
+}
16
+
17
+Vector3D::Vector3D(const VectorXD<3> &other)
18
+    : VectorXD<3>(other)
15 19
 {
16 20
 }
17 21
 
@@ -21,94 +25,48 @@ Vector3D::~Vector3D()
21 25
 
22 26
 double Vector3D::getX() const
23 27
 {
24
-    return _x;
28
+    return _scalars[0];
25 29
 }
26 30
 
27 31
 Vector3D& Vector3D::setX(double x)
28 32
 {
29
-    _x = x;
33
+    _scalars[0] = x;
30 34
     return *this;
31 35
 }
32 36
 double Vector3D::getY() const
33 37
 {
34
-    return _y;
38
+    return _scalars[1];
35 39
 }
36 40
 
37 41
 Vector3D& Vector3D::setY(double y)
38 42
 {
39
-    _y = y;
43
+    _scalars[1] = y;
40 44
     return *this;
41 45
 }
42 46
 double Vector3D::getZ() const
43 47
 {
44
-    return _z;
48
+    return _scalars[2];
45 49
 }
46 50
 
47 51
 Vector3D& Vector3D::setZ(double z)
48 52
 {
49
-    _z = z;
53
+    _scalars[2] = z;
50 54
     return *this;
51 55
 }
52 56
 
53
-bool Vector3D::isNull() const
54
-{
55
-    return _x == 0 && _y == 0 && _z == 0;
56
-}
57
-
58
-bool Vector3D::equal(const Vector3D &other) const
59
-{
60
-    return _x == other._x && _y == other._y && _z == other._z;
61
-}
62
-
63
-Vector3D &Vector3D::add(double k)
64
-{
65
-    return add(Vector3D(k, k, k));
66
-}
67
-
68 57
 Vector3D &Vector3D::add(double x, double y, double z)
69 58
 {
70
-    return add(Vector3D(x, y, z));
71
-}
72
-
73
-Vector3D& Vector3D::add(const Vector3D &other)
74
-{
75
-    _x += other._x;
76
-    _y += other._y;
77
-    _z += other._z;
59
+    _scalars[0] += x;
60
+    _scalars[1] += y;
61
+    _scalars[2] += z;
78 62
     return *this;
79 63
 }
80 64
 
81
-Vector3D &Vector3D::sub(double k)
82
-{
83
-    return sub(Vector3D(k, k, k));
84
-}
85
-
86 65
 Vector3D &Vector3D::sub(double x, double y, double z)
87 66
 {
88
-    return sub(Vector3D(x, y, z));
89
-}
90
-
91
-Vector3D& Vector3D::sub(const Vector3D &other)
92
-{
93
-    _x -= other._x;
94
-    _y -= other._y;
95
-    _z -= other._z;
96
-    return *this;
97
-}
98
-
99
-Vector3D &Vector3D::mult(double k)
100
-{
101
-    _x *= k;
102
-    _y *= k;
103
-    _z *= k;
104
-    return *this;
105
-}
106
-
107
-Vector3D &Vector3D::div(double k)
108
-{
109
-    _x /= k;
110
-    _y /= k;
111
-    _z /= k;
67
+    _scalars[0] -= x;
68
+    _scalars[1] -= y;
69
+    _scalars[2] -= z;
112 70
     return *this;
113 71
 }
114 72
 
@@ -116,120 +74,3 @@ double Vector3D::dotProduct(double x, double y, double z) const
116 74
 {
117 75
     return dotProduct(Vector3D(x, y, z));
118 76
 }
119
-
120
-double Vector3D::dotProduct(const Vector3D &other) const
121
-{
122
-    return (_x * other._x) + (_y * other._y) + (_z * other._z);
123
-}
124
-
125
-double Vector3D::norm() const
126
-{
127
-    return sqrt((_x * _x) + (_y * _y) + (_z * _z));
128
-}
129
-
130
-Vector3D Vector3D::operator+() const
131
-{
132
-    return *this;
133
-}
134
-
135
-Vector3D Vector3D::operator+(const double &k) const
136
-{
137
-    return Vector3D(*this).add(k);
138
-}
139
-
140
-Vector3D &Vector3D::operator+=(const double &k)
141
-{
142
-    return add(k);
143
-}
144
-
145
-Vector3D Vector3D::operator+(const Vector3D &other) const
146
-{
147
-    return Vector3D(*this).add(other);
148
-}
149
-
150
-Vector3D &Vector3D::operator+=(const Vector3D &other)
151
-{
152
-    return add(other);
153
-}
154
-
155
-Vector3D Vector3D::operator-() const
156
-{
157
-    return Vector3D(-_x, -_y, -_z);
158
-}
159
-
160
-Vector3D Vector3D::operator-(const double &k) const
161
-{
162
-    return Vector3D(*this).sub(k);
163
-}
164
-
165
-Vector3D &Vector3D::operator-=(const double &k)
166
-{
167
-    return sub(k);
168
-}
169
-
170
-Vector3D Vector3D::operator-(const Vector3D &other) const
171
-{
172
-    return Vector3D(*this).sub(other);
173
-}
174
-
175
-Vector3D &Vector3D::operator-=(const Vector3D &other)
176
-{
177
-    return sub(other);
178
-}
179
-
180
-Vector3D Vector3D::operator*(const double &k) const
181
-{
182
-    return Vector3D(*this).mult(k);
183
-}
184
-
185
-Vector3D &Vector3D::operator*=(const double &k)
186
-{
187
-    return mult(k);
188
-}
189
-
190
-double Vector3D::operator*(const Vector3D &other) const
191
-{
192
-    return Vector3D(*this).dotProduct(other);
193
-}
194
-
195
-Vector3D &Vector3D::operator*=(const Vector3D &other)
196
-{
197
-    dotProduct(other);
198
-    return *this;
199
-}
200
-
201
-Vector3D Vector3D::operator/(const double &k) const
202
-{
203
-    return Vector3D(*this).div(k);
204
-}
205
-
206
-Vector3D &Vector3D::operator/=(const double &k)
207
-{
208
-    return div(k);
209
-}
210
-
211
-bool Vector3D::operator==(const Vector3D &other) const
212
-{
213
-    return equal(other);
214
-}
215
-
216
-bool Vector3D::operator!=(const Vector3D &other) const
217
-{
218
-    return !equal(other);
219
-}
220
-
221
-bool Vector3D::operator!() const
222
-{
223
-    return isNull();
224
-}
225
-
226
-Vector3D::operator bool() const
227
-{
228
-    return !isNull();
229
-}
230
-
231
-
232
-QDebug operator<<(QDebug dbg, const Vector3D &v)
233
-{
234
-    return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
235
-}

+ 6
- 50
UGameEngine/utils/vector3d.h View File

@@ -1,13 +1,14 @@
1 1
 #ifndef VECTOR3D_H
2 2
 #define VECTOR3D_H
3 3
 
4
-#include <QDebug>
4
+#include "vectorxd.h"
5 5
 
6
-class Vector3D
6
+class Vector3D: public VectorXD<3>
7 7
 {
8 8
 public:
9 9
     explicit Vector3D(double x = 0.0, double y = 0.0, double z = 0.0);
10 10
     Vector3D(const Vector3D& other);
11
+    Vector3D(const VectorXD<3>& other);
11 12
     virtual ~Vector3D();
12 13
 
13 14
     double getX() const;
@@ -19,59 +20,14 @@ public:
19 20
     double getZ() const;
20 21
     Vector3D& setZ(double z);
21 22
 
22
-    bool isNull() const;
23
-
24
-    bool equal(const Vector3D& other) const;
25
-
26
-    Vector3D& add(double k);
23
+    using VectorXD<3>::add;
27 24
     Vector3D& add(double x, double y, double z);
28
-    Vector3D& add(const Vector3D& other);
29 25
 
30
-    Vector3D& sub(double k);
26
+    using VectorXD<3>::sub;
31 27
     Vector3D& sub(double x, double y, double z);
32
-    Vector3D& sub(const Vector3D& other);
33
-
34
-    Vector3D& mult(double k);
35
-
36
-    Vector3D& div(double k);
37 28
 
29
+    using VectorXD<3>::dotProduct;
38 30
     double dotProduct(double x, double y, double z) const;
39
-    double dotProduct(const Vector3D& other) const;
40
-
41
-    double norm() const;
42
-
43
-    Vector3D operator+() const;
44
-    Vector3D operator+(const double& k) const;
45
-    Vector3D& operator+=(const double& k);
46
-    Vector3D operator+(const Vector3D& other) const;
47
-    Vector3D& operator+=(const Vector3D& other);
48
-
49
-    Vector3D operator-() const;
50
-    Vector3D operator-(const double& k) const;
51
-    Vector3D& operator-=(const double& k);
52
-    Vector3D operator-(const Vector3D& other) const;
53
-    Vector3D& operator-=(const Vector3D& other);
54
-
55
-    Vector3D operator*(const double& k) const;
56
-    Vector3D& operator*=(const double& k);
57
-    double operator*(const Vector3D& other) const;
58
-    Vector3D& operator*=(const Vector3D& other);
59
-
60
-    Vector3D operator/(const double& k) const;
61
-    Vector3D& operator/=(const double& k);
62
-
63
-    bool operator==(const Vector3D& other) const;
64
-    bool operator!=(const Vector3D& other) const;
65
-
66
-    bool operator!() const;
67
-    operator bool() const;
68
-
69
-private:
70
-    double _x;
71
-    double _y;
72
-    double _z;
73 31
 };
74 32
 
75
-QDebug operator<<(QDebug dbg, const Vector3D& v);
76
-
77 33
 #endif // VECTOR3D_H

+ 72
- 0
UGameEngine/utils/vectorxd.h View File

@@ -0,0 +1,72 @@
1
+#ifndef VECTORXD_H
2
+#define VECTORXD_H
3
+
4
+#include <QDebug>
5
+
6
+#define tmpl template<unsigned X>
7
+
8
+tmpl class VectorXD
9
+{
10
+public:
11
+    VectorXD();
12
+    VectorXD(const double scalars[X]);
13
+    VectorXD(const VectorXD<X>& other);
14
+
15
+    VectorXD<X>& setScalar(unsigned i, double value);
16
+    double getScalar(unsigned i);
17
+
18
+    bool isNull() const;
19
+
20
+    bool equal(const VectorXD<X>& other) const;
21
+
22
+    VectorXD<X>& add(double k);
23
+    VectorXD<X>& add(double scalars[X]);
24
+    VectorXD<X>& add(const VectorXD<X>& other);
25
+
26
+    VectorXD<X>& sub(double k);
27
+    VectorXD<X>& sub(double scalars[X]);
28
+    VectorXD<X>& sub(const VectorXD<X>& other);
29
+
30
+    VectorXD<X>& mult(double k);
31
+
32
+    VectorXD<X>& div(double k);
33
+
34
+    double dotProduct(const VectorXD<X>& other) const;
35
+
36
+    double norm() const;
37
+
38
+    VectorXD<X> operator+() const;
39
+    VectorXD<X> operator+(const double& k) const;
40
+    VectorXD<X>& operator+=(const double& k);
41
+    VectorXD<X> operator+(const VectorXD<X>& other) const;
42
+    VectorXD<X>& operator+=(const VectorXD<X>& other);
43
+
44
+    VectorXD<X> operator-() const;
45
+    VectorXD<X> operator-(const double& k) const;
46
+    VectorXD<X>& operator-=(const double& k);
47
+    VectorXD<X> operator-(const VectorXD<X>& other) const;
48
+    VectorXD<X>& operator-=(const VectorXD<X>& other);
49
+
50
+    VectorXD<X> operator*(const double& k) const;
51
+    VectorXD<X>& operator*=(const double& k);
52
+    double operator*(const VectorXD<X>& other) const;
53
+    VectorXD<X>& operator*=(const VectorXD<X>& other);
54
+
55
+    VectorXD<X> operator/(const double& k) const;
56
+    VectorXD<X>& operator/=(const double& k);
57
+
58
+    bool operator==(const VectorXD<X>& other) const;
59
+    bool operator!=(const VectorXD<X>& other) const;
60
+
61
+    bool operator!() const;
62
+    operator bool() const;
63
+
64
+protected:
65
+    double _scalars[X];
66
+};
67
+
68
+tmpl QDebug operator<<(QDebug dbg, const VectorXD<X>& v);
69
+
70
+#include "vectorxd.hxx"
71
+
72
+#endif // VECTORXD_H

+ 238
- 0
UGameEngine/utils/vectorxd.hxx View File

@@ -0,0 +1,238 @@
1
+#include <math.h>
2
+
3
+#define tmpl template<unsigned X>
4
+
5
+tmpl VectorXD<X>::VectorXD()
6
+{
7
+    for (unsigned i = 0; i < X; ++i) {
8
+        _scalars[i] = 0;
9
+    }
10
+}
11
+
12
+tmpl VectorXD<X>::VectorXD(const double scalars[X])
13
+{
14
+    for (unsigned i = 0; i < X; ++i) {
15
+        _scalars[i] = scalars[i];
16
+    }
17
+}
18
+
19
+tmpl VectorXD<X>::VectorXD(const VectorXD<X>& other)
20
+{
21
+    for (unsigned i = 0; i < X; ++i) {
22
+        _scalars[i] = other._scalars[i];
23
+    }
24
+}
25
+
26
+tmpl VectorXD<X>& VectorXD<X>::setScalar(unsigned i, double value)
27
+{
28
+    _scalars[i] = value;
29
+    return *this;
30
+}
31
+
32
+tmpl double VectorXD<X>::getScalar(unsigned i)
33
+{
34
+    return _scalars[i];
35
+}
36
+
37
+tmpl bool VectorXD<X>::isNull() const
38
+{
39
+    return equal(VectorXD<X>());
40
+}
41
+
42
+tmpl bool VectorXD<X>::equal(const VectorXD<X> &other) const
43
+{
44
+    for (unsigned i = 0; i < X; ++i) {
45
+        if (_scalars[i] != other._scalars[i]) {
46
+            return false;
47
+        }
48
+    }
49
+    return true;
50
+}
51
+
52
+tmpl VectorXD<X> &VectorXD<X>::add(double k)
53
+{
54
+    double scalars[X];
55
+    for (unsigned i = 0; i < X; ++i) {
56
+        scalars[i] = k;
57
+    }
58
+    return add(VectorXD<X>(scalars));
59
+}
60
+
61
+tmpl VectorXD<X> &VectorXD<X>::add(double scalars[X])
62
+{
63
+    return add(VectorXD<X>(scalars));
64
+}
65
+
66
+tmpl VectorXD<X>& VectorXD<X>::add(const  VectorXD<X> &other)
67
+{
68
+    for (unsigned i = 0; i < X; ++i) {
69
+        _scalars[i] += other._scalars[i];
70
+    }
71
+    return *this;
72
+}
73
+
74
+tmpl VectorXD<X> &VectorXD<X>::sub(double k)
75
+{
76
+    double scalars[X];
77
+    for (unsigned i = 0; i < X; ++i) {
78
+        scalars[i] = k;
79
+    }
80
+    return sub(VectorXD<X>(scalars));
81
+}
82
+
83
+tmpl VectorXD<X> &VectorXD<X>::sub(double scalars[X])
84
+{
85
+    return sub(VectorXD<X>(scalars));
86
+}
87
+
88
+tmpl VectorXD<X>& VectorXD<X>::sub(const VectorXD<X> &other)
89
+{
90
+    for (unsigned i = 0; i < X; ++i) {
91
+        _scalars[i] -= other._scalars[i];
92
+    }
93
+    return *this;
94
+}
95
+
96
+tmpl VectorXD<X> &VectorXD<X>::mult(double k)
97
+{
98
+    for (unsigned i = 0; i < X; ++i) {
99
+        _scalars[i] *= k;
100
+    }
101
+    return *this;
102
+}
103
+
104
+tmpl VectorXD<X> &VectorXD<X>::div(double k)
105
+{
106
+    for (unsigned i = 0; i < X; ++i) {
107
+        _scalars[i] /= k;
108
+    }
109
+    return *this;
110
+}
111
+
112
+tmpl double VectorXD<X>::dotProduct(const VectorXD<X> &other) const
113
+{
114
+    double total = 0;
115
+    for (unsigned i = 0; i < X; ++i) {
116
+        total += _scalars[i] * other._scalars[i];
117
+    }
118
+    return total;
119
+}
120
+
121
+tmpl double VectorXD<X>::norm() const
122
+{
123
+    double total = 0;
124
+    for (unsigned i = 0; i < X; ++i) {
125
+        total += _scalars[i] * _scalars[i];
126
+    }
127
+    return sqrt(total);
128
+}
129
+
130
+tmpl VectorXD<X> VectorXD<X>::operator+() const
131
+{
132
+    return *this;
133
+}
134
+
135
+tmpl VectorXD<X> VectorXD<X>::operator+(const double &k) const
136
+{
137
+    return VectorXD(*this).add(k);
138
+}
139
+
140
+tmpl VectorXD<X> &VectorXD<X>::operator+=(const double &k)
141
+{
142
+    return add(k);
143
+}
144
+
145
+tmpl VectorXD<X> VectorXD<X>::operator+(const VectorXD<X> &other) const
146
+{
147
+    return VectorXD<X>(*this).add(other);
148
+}
149
+
150
+tmpl VectorXD<X> &VectorXD<X>::operator+=(const VectorXD<X> &other)
151
+{
152
+    return add(other);
153
+}
154
+
155
+tmpl VectorXD<X> VectorXD<X>::operator-() const
156
+{
157
+    double scalars[X];
158
+    for (unsigned i = 0; i < X; ++i) {
159
+        scalars[i] = -_scalars[i];
160
+    }
161
+    return VectorXD<X>(scalars);
162
+}
163
+
164
+tmpl VectorXD<X> VectorXD<X>::operator-(const double &k) const
165
+{
166
+    return VectorXD(*this).sub(k);
167
+}
168
+
169
+tmpl VectorXD<X> &VectorXD<X>::operator-=(const double &k)
170
+{
171
+    return sub(k);
172
+}
173
+
174
+tmpl VectorXD<X> VectorXD<X>::operator-(const VectorXD<X> &other) const
175
+{
176
+    return VectorXD(*this).sub(other);
177
+}
178
+
179
+tmpl VectorXD<X> &VectorXD<X>::operator-=(const VectorXD<X> &other)
180
+{
181
+    return sub(other);
182
+}
183
+
184
+tmpl VectorXD<X> VectorXD<X>::operator*(const double &k) const
185
+{
186
+    return VectorXD<X>(*this).mult(k);
187
+}
188
+
189
+tmpl VectorXD<X> &VectorXD<X>::operator*=(const double &k)
190
+{
191
+    return mult(k);
192
+}
193
+
194
+tmpl double VectorXD<X>::operator*(const VectorXD<X> &other) const
195
+{
196
+    return VectorXD<X>(*this).dotProduct(other);
197
+}
198
+
199
+tmpl VectorXD<X> &VectorXD<X>::operator*=(const VectorXD<X> &other)
200
+{
201
+    dotProduct(other);
202
+    return *this;
203
+}
204
+
205
+tmpl VectorXD<X> VectorXD<X>::operator/(const double &k) const
206
+{
207
+    return VectorXD<X>(*this).div(k);
208
+}
209
+
210
+tmpl VectorXD<X> &VectorXD<X>::operator/=(const double &k)
211
+{
212
+    return div(k);
213
+}
214
+
215
+tmpl bool VectorXD<X>::operator==(const VectorXD<X> &other) const
216
+{
217
+    return equal(other);
218
+}
219
+
220
+tmpl bool VectorXD<X>::operator!=(const VectorXD<X> &other) const
221
+{
222
+    return !equal(other);
223
+}
224
+
225
+tmpl bool VectorXD<X>::operator!() const
226
+{
227
+    return isNull();
228
+}
229
+
230
+tmpl VectorXD<X>::operator bool() const
231
+{
232
+    return !isNull();
233
+}
234
+
235
+tmpl QDebug operator<<(QDebug dbg, const VectorXD<X> &v)
236
+{
237
+    return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
238
+}

Loading…
Cancel
Save