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 144 145 146 147 148 149 150 151 152
|
/* Point.h
Copyright (c) 2014 by Michael Zahniser
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifdef __SSE3__
#include <pmmintrin.h>
#elif defined(__SSE2__)
#include <xmmintrin.h>
#endif
// Class representing a 2D point with functions for a variety of vector operations.
// A Point can represent either a location or a vector (e.g. a velocity, or a
// distance between two points, or a unit vector representing a direction). All
// basic mathematical operations that make sense for vectors are supported.
// Internally the coordinates are stored in a SSE vector and the processor's vector
// extensions are used to optimize all operations.
class Point {
public:
Point() noexcept;
Point(double x, double y) noexcept;
// Check if the point is anything but (0, 0).
explicit operator bool() const noexcept;
bool operator!() const noexcept;
bool operator==(const Point &other) const noexcept;
bool operator!=(const Point &other) const noexcept;
Point operator+(const Point &point) const;
Point &operator+=(const Point &point);
Point operator-(const Point &point) const;
Point &operator-=(const Point &point);
Point operator-() const;
Point operator*(double scalar) const;
friend Point operator*(double scalar, const Point &point);
Point &operator*=(double scalar);
Point operator/(double scalar) const;
Point &operator/=(double scalar);
// Multiply the respective components of each Point.
Point operator*(const Point &other) const;
Point &operator*=(const Point &other);
double &X();
const double &X() const noexcept;
double &Y();
const double &Y() const noexcept;
void Set(double x, double y);
// Operations that treat this point as a vector from (0, 0):
double Dot(const Point &point) const;
double Cross(const Point &point) const;
double Length() const;
double LengthSquared() const;
Point Unit() const;
double Distance(const Point &point) const;
double DistanceSquared(const Point &point) const;
Point Lerp(const Point &to, const double c) const;
// Take the absolute value of both coordinates.
friend Point abs(const Point &p);
// Use the min of each x and each y coordinates.
friend Point min(const Point &p, const Point &q);
// Use the max of each x and each y coordinates.
friend Point max(const Point &p, const Point &q);
private:
#ifdef __SSE2__
// Private constructor, using a vector.
explicit Point(const __m128d &v);
private:
struct PointInternal {
double x;
double y;
};
union {
__m128d v;
PointInternal val;
};
#else
double x;
double y;
#endif
};
// Inline accessor functions, for speed:
inline double &Point::X()
{
#ifdef __SSE2__
return val.x;
#else
return x;
#endif
}
inline const double &Point::X() const noexcept
{
#ifdef __SSE2__
return val.x;
#else
return x;
#endif
}
inline double &Point::Y()
{
#ifdef __SSE2__
return val.y;
#else
return y;
#endif
}
inline const double &Point::Y() const noexcept
{
#ifdef __SSE2__
return val.y;
#else
return y;
#endif
}
|