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