GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/vector.h Lines: 2 23 8.7 %
Date: 2017-11-29 Branches: 0 0 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2007-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#ifndef VECTOR_H
24
#define VECTOR_H
25
26
#ifndef USE_SDL2
27
#include <cmath>
28
#endif  // USE_SDL2
29
30
#include <iostream>
31
32
#include "localconsts.h"
33
34
/**
35
 * Vector class. Represents either a 3D point in space, a velocity or a force.
36
 * Provides several convenient operator overloads.
37
 */
38
class Vector final
39
{
40
    public:
41
        /**
42
         * Constructor.
43
         */
44
408
        Vector() :
45
            x(0.0F),
46
            y(0.0F),
47
408
            z(0.0F)
48
        {}
49
50
        /**
51
         * Constructor.
52
         */
53
        Vector(const float x0,
54
               const float y0,
55
               const float z0 = 0.0F) :
56
            x(x0),
57
            y(y0),
58
            z(z0)
59
        {}
60
61
        /**
62
         * Copy constructor.
63
         */
64
        Vector(const Vector &v) :
65
            x(v.x),
66
            y(v.y),
67
            z(v.z)
68
        {}
69
70
        A_DEFAULT_COPY(Vector)
71
72
        /**
73
         * Returns true if all coordinates are set to 0, otherwise returns
74
         * false.
75
         */
76
        bool isNull() const noexcept2 A_WARN_UNUSED
77
        {
78
            return x == 0.0F && y == 0.0F && z == 0.0F;
79
        }
80
81
        Vector &operator=(const Vector &v) noexcept2
82
        {
83
            x = v.x;
84
            y = v.y;
85
            z = v.z;
86
            return *this;
87
        }
88
89
        /**
90
         * Scale vector operator.
91
         */
92
        Vector operator*(const float c) const noexcept2 A_WARN_UNUSED
93
        {
94
            return Vector(x * c,
95
                          y * c,
96
                          z * c);
97
        }
98
99
        /**
100
         * In-place scale vector operator.
101
         */
102
        Vector &operator*=(const float c) noexcept2
103
        {
104
            x *= c;
105
            y *= c;
106
            z *= c;
107
            return *this;
108
        }
109
110
        /**
111
         * Scale vector operator.
112
         */
113
        Vector operator/(const float c) const noexcept2 A_WARN_UNUSED
114
        {
115
            return Vector(x / c,
116
                          y / c,
117
                          z / c);
118
        }
119
120
        /**
121
         * In-place scale vector operator.
122
         */
123
        Vector &operator/=(const float c) noexcept2 A_WARN_UNUSED
124
        {
125
            x /= c;
126
            y /= c;
127
            z /= c;
128
            return *this;
129
        }
130
131
        /**
132
         * Add vector operator.
133
         */
134
        Vector operator+(const Vector &v) const noexcept2 A_WARN_UNUSED
135
        {
136
            return Vector(x + v.x,
137
                          y + v.y,
138
                          z + v.z);
139
        }
140
141
        /**
142
         * In-place add vector operator.
143
         */
144
        Vector &operator+=(const Vector &v) noexcept2
145
        {
146
            x += v.x;
147
            y += v.y;
148
            z += v.z;
149
            return *this;
150
        }
151
152
        /**
153
         * Subtract vector operator.
154
         */
155
        Vector operator-(const Vector &v) const noexcept2 A_WARN_UNUSED
156
        {
157
            return Vector(x - v.x,
158
                          y - v.y,
159
                          z - v.z);
160
        }
161
162
        /**
163
         * In-place subtract vector operator.
164
         */
165
        Vector &operator-=(const Vector &v) noexcept2
166
        {
167
            x -= v.x;
168
            y -= v.y;
169
            z -= v.z;
170
            return *this;
171
        }
172
173
        /**
174
         * Returns the length of this vector. This method does a relatively
175
         * slow square root.
176
         */
177
        float length() const A_WARN_UNUSED
178
        {
179
            return sqrtf(x * x + y * y + z * z);
180
        }
181
182
        /**
183
         * Returns the squared length of this vector. Avoids the square root.
184
         */
185
        float squaredLength() const noexcept2 A_WARN_UNUSED
186
        {
187
            return x * x + y * y + z * z;
188
        }
189
190
        /**
191
         * Returns the manhattan length of this vector.
192
         */
193
        float manhattanLength() const A_WARN_UNUSED
194
        {
195
            return fabsf(x) + fabsf(y) + fabsf(z);
196
        }
197
198
        /**
199
         * Returns a normalized version of this vector. This is a unit vector
200
         * running parallel to it.
201
         */
202
        Vector normalized() const A_WARN_UNUSED
203
        {
204
            const float len = length();
205
            return Vector(x / len, y / len, z / len);
206
        }
207
208
        float x, y, z;
209
};
210
211
/**
212
 * Appends a string representation of a vector to the output stream.
213
 */
214
std::ostream& operator <<(std::ostream &os, const Vector &v);
215
216
#endif  // VECTOR_H