2014-05-24 13:42:06 +03:00
|
|
|
/*
|
|
|
|
* float3.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
2014-05-24 13:42:06 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-08-30 19:45:11 +03:00
|
|
|
// FIXME: Class doesn't contain three float values. Update name and description.
|
2014-05-24 13:42:06 +03:00
|
|
|
/// Class which consists of three float values. Represents position virtual RMG (0;1) area.
|
|
|
|
class float3
|
|
|
|
{
|
|
|
|
public:
|
2024-01-10 00:38:54 +02:00
|
|
|
float x;
|
|
|
|
float y;
|
2014-05-24 13:42:06 +03:00
|
|
|
si32 z;
|
2014-08-30 19:45:11 +03:00
|
|
|
|
|
|
|
float3() : x(0), y(0), z(0) {}
|
|
|
|
float3(const float X, const float Y, const si32 Z): x(X), y(Y), z(Z) {}
|
|
|
|
float3(const float3 & copy) : x(copy.x), y(copy.y), z(copy.z) {}
|
|
|
|
float3 & operator=(const float3 & copy) { x = copy.x; y = copy.y; z = copy.z; return *this; }
|
|
|
|
|
|
|
|
// returns float3 with coordinates increased by corresponding coordinate of given float3
|
|
|
|
float3 operator+(const float3 & i) const { return float3(x + i.x, y + i.y, z + i.z); }
|
2024-06-24 03:23:26 +02:00
|
|
|
// returns float3 with coordinates increased by given number
|
2014-08-30 19:45:11 +03:00
|
|
|
float3 operator+(const float i) const { return float3(x + i, y + i, z + (si32)i); }
|
|
|
|
// returns float3 with coordinates decreased by corresponding coordinate of given float3
|
|
|
|
float3 operator-(const float3 & i) const { return float3(x - i.x, y - i.y, z - i.z); }
|
2024-06-24 03:23:26 +02:00
|
|
|
// returns float3 with coordinates decreased by given number
|
2014-08-30 19:45:11 +03:00
|
|
|
float3 operator-(const float i) const { return float3(x - i, y - i, z - (si32)i); }
|
2017-08-11 19:03:05 +02:00
|
|
|
|
2024-06-24 03:23:26 +02:00
|
|
|
// returns float3 with plane coordinates decreased by given number
|
2014-08-30 19:45:11 +03:00
|
|
|
float3 operator*(const float i) const {return float3(x * i, y * i, z);}
|
2024-06-24 03:23:26 +02:00
|
|
|
// returns float3 with plane coordinates decreased by given number
|
2014-08-30 19:45:11 +03:00
|
|
|
float3 operator/(const float i) const {return float3(x / i, y / i, z);}
|
2017-08-11 19:03:05 +02:00
|
|
|
|
2014-08-30 19:45:11 +03:00
|
|
|
// returns opposite position
|
|
|
|
float3 operator-() const { return float3(-x, -y, -z); }
|
2017-08-11 19:03:05 +02:00
|
|
|
|
2014-08-30 19:45:11 +03:00
|
|
|
// returns squared distance on Oxy plane (z coord is not used)
|
|
|
|
double dist2dSQ(const float3 & o) const
|
|
|
|
{
|
|
|
|
const double dx = (x - o.x);
|
|
|
|
const double dy = (y - o.y);
|
|
|
|
return dx*dx + dy*dy;
|
|
|
|
}
|
2014-10-31 13:58:55 +02:00
|
|
|
double mag() const
|
|
|
|
{
|
|
|
|
return sqrt(x*x + y*y);
|
|
|
|
}
|
|
|
|
float3 unitVector() const
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
return float3(x, y, z) / (float)mag();
|
2014-10-31 13:58:55 +02:00
|
|
|
}
|
2014-08-30 19:45:11 +03:00
|
|
|
// returns distance on Oxy plane (z coord is not used)
|
|
|
|
double dist2d(const float3 &other) const { return std::sqrt(dist2dSQ(other)); }
|
|
|
|
|
|
|
|
bool areNeighbours(const float3 &other) const { return (dist2dSQ(other) < 4.0) && z == other.z; }
|
2017-08-11 19:03:05 +02:00
|
|
|
|
2014-08-30 19:45:11 +03:00
|
|
|
float3& operator+=(const float3 & i)
|
2014-05-24 13:42:06 +03:00
|
|
|
{
|
2014-08-30 19:45:11 +03:00
|
|
|
x += i.x;
|
|
|
|
y += i.y;
|
|
|
|
z += i.z;
|
|
|
|
|
|
|
|
return *this;
|
2014-05-24 13:42:06 +03:00
|
|
|
}
|
2014-08-30 19:45:11 +03:00
|
|
|
float3& operator+=(const float & i)
|
2014-05-24 13:42:06 +03:00
|
|
|
{
|
2014-08-30 19:45:11 +03:00
|
|
|
x += i;
|
|
|
|
y += i;
|
|
|
|
z += (si32)i;
|
|
|
|
|
|
|
|
return *this;
|
2014-05-24 13:42:06 +03:00
|
|
|
}
|
2017-08-11 19:03:05 +02:00
|
|
|
|
2014-08-30 19:45:11 +03:00
|
|
|
float3& operator-=(const float3 & i)
|
2014-05-24 13:42:06 +03:00
|
|
|
{
|
2014-08-30 19:45:11 +03:00
|
|
|
x -= i.x;
|
|
|
|
y -= i.y;
|
|
|
|
z -= i.z;
|
|
|
|
|
|
|
|
return *this;
|
2014-05-24 13:42:06 +03:00
|
|
|
}
|
2014-08-30 19:45:11 +03:00
|
|
|
float3& operator-=(const float & i)
|
2014-05-24 13:42:06 +03:00
|
|
|
{
|
2014-08-30 19:45:11 +03:00
|
|
|
x += i;
|
|
|
|
y += i;
|
|
|
|
z += (si32)i;
|
|
|
|
|
|
|
|
return *this;
|
2014-05-24 13:42:06 +03:00
|
|
|
}
|
2014-08-30 19:45:11 +03:00
|
|
|
|
|
|
|
// scale on plane
|
|
|
|
float3& operator*=(const float & i)
|
2014-05-24 13:42:06 +03:00
|
|
|
{
|
2014-08-30 19:45:11 +03:00
|
|
|
x *= i;
|
|
|
|
y *= i;
|
|
|
|
|
|
|
|
return *this;
|
2014-05-24 13:42:06 +03:00
|
|
|
}
|
2014-08-30 19:45:11 +03:00
|
|
|
// scale on plane
|
|
|
|
float3& operator/=(const float & i)
|
2014-05-24 13:42:06 +03:00
|
|
|
{
|
2014-08-30 19:45:11 +03:00
|
|
|
x /= i;
|
|
|
|
y /= i;
|
|
|
|
|
|
|
|
return *this;
|
2014-05-24 13:42:06 +03:00
|
|
|
}
|
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
std::string toString() const
|
2014-05-24 13:42:06 +03:00
|
|
|
{
|
2023-03-09 15:36:46 +02:00
|
|
|
return "(" + std::to_string(x) +
|
|
|
|
" " + std::to_string(y) +
|
|
|
|
" " + std::to_string(z) + ")";
|
2014-05-24 13:42:06 +03:00
|
|
|
}
|
2014-08-30 19:45:11 +03:00
|
|
|
|
|
|
|
bool valid() const
|
2014-05-24 13:42:06 +03:00
|
|
|
{
|
|
|
|
return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map
|
|
|
|
}
|
2014-08-30 19:45:11 +03:00
|
|
|
|
2014-05-24 13:42:06 +03:00
|
|
|
template <typename Handler> void serialize(Handler &h, const float version)
|
|
|
|
{
|
2017-07-31 15:35:42 +02:00
|
|
|
h & x;
|
|
|
|
h & y;
|
|
|
|
h & z;
|
2014-05-24 13:42:06 +03:00
|
|
|
}
|
|
|
|
};
|
2014-08-30 19:45:11 +03:00
|
|
|
|
2014-05-24 13:42:06 +03:00
|
|
|
struct Shashfloat3
|
|
|
|
{
|
|
|
|
size_t operator()(float3 const& pos) const
|
|
|
|
{
|
|
|
|
size_t ret = std::hash<float>()(pos.x);
|
|
|
|
vstd::hash_combine(ret, pos.y);
|
|
|
|
vstd::hash_combine(ret, pos.z);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|