From c0d067d24b25e12ff565d8ecd3321fe7c4fdaad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Fri, 24 Dec 2021 00:11:12 +0100 Subject: [PATCH] Changed Math::Point into alias to glm::vec2, minor refactors --- src/graphics/core/vertex.h | 4 +- src/graphics/engine/camera.cpp | 6 +- src/graphics/engine/engine.cpp | 11 ++- src/graphics/engine/terrain.cpp | 8 +- src/math/geometry.h | 33 +++---- src/math/point.h | 151 +++---------------------------- src/object/motion/motionworm.cpp | 2 +- src/object/task/taskgoto.cpp | 16 ++-- src/object/task/taskgoto.h | 9 +- src/physics/physics.cpp | 2 +- src/ui/controls/map.cpp | 5 +- 11 files changed, 61 insertions(+), 186 deletions(-) diff --git a/src/graphics/core/vertex.h b/src/graphics/core/vertex.h index f53204ff..69d2ae2c 100644 --- a/src/graphics/core/vertex.h +++ b/src/graphics/core/vertex.h @@ -79,7 +79,7 @@ struct Vertex std::stringstream s; s.precision(3); s << "(c: " << coord.ToString() << ", n: " << normal.ToString() - << ", tc: " << texCoord.ToString() << ")"; + << ", tc: " << Math::ToString(texCoord) << ")"; return s.str(); } }; @@ -154,7 +154,7 @@ struct VertexTex2 std::stringstream s; s.precision(3); s << "(c: " << coord.ToString() << ", n: " << normal.ToString() - << ", tc: " << texCoord.ToString() << ", tc2: " << texCoord2.ToString() << ")"; + << ", tc: " << Math::ToString(texCoord) << ", tc2: " << Math::ToString(texCoord2) << ")"; return s.str(); } }; diff --git a/src/graphics/engine/camera.cpp b/src/graphics/engine/camera.cpp index 57e66dca..224eb55d 100644 --- a/src/graphics/engine/camera.cpp +++ b/src/graphics/engine/camera.cpp @@ -967,7 +967,8 @@ bool CCamera::EventProcess(const Event &event) m_engine->SetMouseType(ENG_MOUSE_MOVE); } - m_mouseDeltaEdge.LoadZero(); + m_mouseDeltaEdge = { 0, 0 }; + if (m_oldCameraScroll) { if (event.mousePos.x < MOUSE_EDGE_MARGIN) @@ -1457,7 +1458,8 @@ Math::Vector CCamera::CalculateCameraMovement(const Event &event, bool keysAllow delta.x += m_mouseDelta.x * 2*Math::PI; delta.y -= m_mouseDelta.y * Math::PI; - m_mouseDelta.LoadZero(); + + m_mouseDelta = { 0, 0 }; delta.z += m_mouseWheelDelta * 8.0f; m_mouseWheelDelta = 0.0f; diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index 6dc8eee3..421935d8 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -1291,8 +1291,11 @@ void CEngine::TrackTextureMapping(int objRank, const Material& mat, int state, } if (s == 3 && e == 3) { - pe = ps + Math::Point(vs[tBase + is[0]].position.x - vs[tBase + ie[0]].position.x, - vs[tBase + is[0]].position.y - vs[tBase + ie[0]].position.y).Length() / factor; // end position on the periphery + glm::vec2 endPosition{}; + endPosition.x = vs[tBase + is[0]].position.x - vs[tBase + ie[0]].position.x; + endPosition.y = vs[tBase + is[0]].position.y - vs[tBase + ie[0]].position.y; + + pe = ps + glm::length(endPosition) / factor; // end position on the periphery float pps = ps + pos; float ppe = pe + pos; @@ -4527,7 +4530,7 @@ void CEngine::UpdateGroundSpotTextures() if (dot == 0) intensity = 0.0f; else - intensity = Math::Point(ppx-cx, ppy-cy).Length()/dot; + intensity = glm::length(glm::vec2(ppx-cx, ppy-cy)) / dot; ppx -= min.x; // on the texture ppy -= min.y; @@ -4610,7 +4613,7 @@ void CEngine::UpdateGroundSpotTextures() ppx -= min.x; // on the texture ppy -= min.y; - float intensity = 1.0f - Math::Point(ix, iy).Length() / dot; + float intensity = 1.0f - glm::length(glm::vec2(ix, iy)) / dot; if (intensity <= 0.0f) continue; diff --git a/src/graphics/engine/terrain.cpp b/src/graphics/engine/terrain.cpp index 4b2d728c..5916b82d 100644 --- a/src/graphics/engine/terrain.cpp +++ b/src/graphics/engine/terrain.cpp @@ -605,7 +605,7 @@ bool CTerrain::CreateMosaic(int ox, int oy, int step, int objRank, float pixel = 1.0f/256.0f; // 1 pixel cover (*) float dp = 1.0f/512.0f; - Math::Point uv; + glm::vec2 uv; for (int my = 0; my < m_textureSubdivCount; my++) { @@ -763,7 +763,7 @@ void CTerrain::GetTexture(int x, int y, std::string& name, Math::Point &uv) if (tm == nullptr) { name = ""; - uv = Math::Point(0.0f, 0.0f); + uv = { 0.0f, 0.0f }; } else { @@ -1383,7 +1383,7 @@ float CTerrain::GetFineSlope(const Math::Vector &pos) { Math::Vector n; if (! GetNormal(n, pos)) return 0.0f; - return fabs(Math::RotateAngle(Math::Point(n.x, n.z).Length(), n.y) - Math::PI/2.0f); + return fabs(Math::RotateAngle(glm::length(glm::vec2(n.x, n.z)), n.y) - Math::PI/2.0f); } float CTerrain::GetCoarseSlope(const Math::Vector &pos) @@ -1785,7 +1785,7 @@ void CTerrain::ShowFlatGround(Math::Vector pos) p.z = (y-20)*radius; p.y = 0.0f; - if (Math::Point(p.x, p.y).Length() > 20.0f*radius) + if (glm::length(glm::vec2(p.x, p.y)) > 20.0f*radius) continue; float angle = GetFineSlope(pos+p); diff --git a/src/math/geometry.h b/src/math/geometry.h index 868efbe0..f2b90646 100644 --- a/src/math/geometry.h +++ b/src/math/geometry.h @@ -31,6 +31,7 @@ #include "math/point.h" #include "math/vector.h" +#include #include #include @@ -42,7 +43,7 @@ namespace Math //! Returns py up on the line \a a - \a b -inline float MidPoint(const Math::Point &a, const Math::Point &b, float px) +inline float MidPoint(const glm::vec2&a, const glm::vec2&b, float px) { if (IsEqual(a.x, b.x)) { @@ -55,7 +56,7 @@ inline float MidPoint(const Math::Point &a, const Math::Point &b, float px) } //! Tests whether the point \a p is inside the triangle (\a a,\a b,\a c) -inline bool IsInsideTriangle(Math::Point a, Math::Point b, Math::Point c, Math::Point p) +inline bool IsInsideTriangle(glm::vec2 a, glm::vec2 b, glm::vec2 c, glm::vec2 p) { float n, m; @@ -64,10 +65,10 @@ inline bool IsInsideTriangle(Math::Point a, Math::Point b, Math::Point c, Math:: if ( p.y < a.y && p.y < b.y && p.y < c.y ) return false; if ( p.y > a.y && p.y > b.y && p.y > c.y ) return false; - if ( a.x > b.x ) Swap(a,b); - if ( a.x > c.x ) Swap(a,c); - if ( c.x < a.x ) Swap(c,a); - if ( c.x < b.x ) Swap(c,b); + if ( a.x > b.x ) std::swap(a,b); + if ( a.x > c.x ) std::swap(a,c); + if ( c.x < a.x ) std::swap(c,a); + if ( c.x < b.x ) std::swap(c,b); n = MidPoint(a, b, p.x); m = MidPoint(a, c, p.x); @@ -86,13 +87,13 @@ inline bool IsInsideTriangle(Math::Point a, Math::Point b, Math::Point c, Math:: * \param angle angle [radians] (positive is CCW) * \param p the point to be rotated */ -inline Math::Point RotatePoint(const Math::Point ¢er, float angle, const Math::Point &p) +inline glm::vec2 RotatePoint(const glm::vec2& center, float angle, const glm::vec2& p) { - Math::Point a; + glm::vec2 a; a.x = p.x-center.x; a.y = p.y-center.y; - Math::Point b; + glm::vec2 b; b.x = a.x*cosf(angle) - a.y*sinf(angle); b.y = a.x*sinf(angle) + a.y*cosf(angle); @@ -107,12 +108,12 @@ inline Math::Point RotatePoint(const Math::Point ¢er, float angle, const Mat * \param angle angle [radians] (positive is CCW) * \param p the point to be rotated */ -inline Math::Point RotatePoint(float angle, const Math::Point &p) +inline glm::vec2 RotatePoint(float angle, const glm::vec2&p) { float x = p.x*cosf(angle) - p.y*sinf(angle); float y = p.x*sinf(angle) + p.y*cosf(angle); - return Math::Point(x, y); + return { x, y }; } //! Rotates a vector (dist, 0) @@ -120,12 +121,12 @@ inline Math::Point RotatePoint(float angle, const Math::Point &p) * \param angle angle [radians] (positive is CCW) * \param dist distance to origin */ -inline Math::Point RotatePoint(float angle, float dist) +inline glm::vec2 RotatePoint(float angle, float dist) { float x = dist*cosf(angle); float y = dist*sinf(angle); - return Math::Point(x, y); + return { x, y }; } //! Rotates a point around a center on 2D plane @@ -236,7 +237,7 @@ inline float RotateAngle(float x, float y) * \param p1,p2 the two points * \returns the angle [radians] (positive is CCW) */ -inline float RotateAngle(const Math::Point ¢er, const Math::Point &p1, const Math::Point &p2) +inline float RotateAngle(const glm::vec2¢er, const glm::vec2&p1, const glm::vec2&p2) { if (PointsEqual(p1, center)) return 0; @@ -244,8 +245,8 @@ inline float RotateAngle(const Math::Point ¢er, const Math::Point &p1, const if (PointsEqual(p2, center)) return 0; - float a1 = asinf((p1.y - center.y) / Distance(p1, center)); - float a2 = asinf((p2.y - center.y) / Distance(p2, center)); + float a1 = asinf((p1.y - center.y) / glm::distance(p1, center)); + float a2 = asinf((p2.y - center.y) / glm::distance(p2, center)); if (p1.x < center.x) a1 = PI - a1; if (p2.x < center.x) a2 = PI - a2; diff --git a/src/math/point.h b/src/math/point.h index 07339a79..4791ac3a 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -39,147 +39,18 @@ namespace Math { -/** - * \struct Point - * \brief 2D point - * - * Represents a 2D point (x, y). - * Contains the required methods for operating on points. - * - * All methods are made inline to maximize optimization. - */ -struct Point +// Temporary type alias +using Point = glm::vec2; + + +//! Returns a string "[x, y]" +inline std::string ToString(const Point& point) { - //! X coord - float x; - //! Y coord - float y; - - //! Constructs a zero point: (0,0) - inline Point() - : x(0.0f) - , y(0.0f) - {} - - //! Constructs a point from given coords: (x,y) - inline explicit Point(float _x, float _y) - : x(_x) - , y(_y) - {} - - inline Point(const glm::vec2& point) - : x(point.x) - , y(point.y) - {} - - //! Sets the zero point: (0,0) - inline void LoadZero() - { - x = y = 0.0f; - } - - //! Returns the struct cast to \c float* array; use with care! - inline float* Array() - { - return reinterpret_cast(this); - } - - //! Returns the struct cast to const float* array; use with care! - inline const float* Array() const - { - return reinterpret_cast(this); - } - - operator glm::vec2() const - { - return { x, y }; - } - - //! Returns the distance from (0,0) to the point (x,y) - inline float Length() - { - return sqrtf(x*x + y*y); - } - - //! Returns the inverted point - inline Point operator-() const - { - return Point(-x, -y); - } - - //! Adds the given point - inline const Point& operator+=(const Point &right) - { - x += right.x; - y += right.y; - return *this; - } - - //! Adds two points - inline friend const Point operator+(const Point &left, const Point &right) - { - return Point(left.x + right.x, left.y + right.y); - } - - //! Subtracts the given point - inline const Point& operator-=(const Point &right) - { - x -= right.x; - y -= right.y; - return *this; - } - - //! Subtracts two points - inline friend const Point operator-(const Point &left, const Point &right) - { - return Point(left.x - right.x, left.y - right.y); - } - - //! Multiplies by given scalar - inline const Point& operator*=(const float &right) - { - x *= right; - y *= right; - return *this; - } - - //! Multiplies point by scalar - inline friend const Point operator*(const float &left, const Point &right) - { - return Point(left * right.x, left * right.y); - } - - //! Multiplies point by scalar - inline friend const Point operator*(const Point &left, const float &right) - { - return Point(left.x * right, left.y * right); - } - - //! Divides by given scalar - inline const Point& operator/=(const float &right) - { - x /= right; - y /= right; - return *this; - } - - //! Divides point by scalar - inline friend const Point operator/(const Point &left, const float &right) - { - return Point(left.x / right, left.y / right); - } - - - //! Returns a string "[x, y]" - inline std::string ToString() const - { - std::stringstream s; - s.precision(3); - s << "[" << x << ", " << y << "]"; - return s.str(); - } -}; // struct Point - + std::stringstream s; + s.precision(3); + s << "[" << point.x << ", " << point.y << "]"; + return s.str(); +} //! Checks if two vectors are equal within given \a tolerance inline bool PointsEqual(const Point &a, const Point &b, float tolerance = TOLERANCE) diff --git a/src/object/motion/motionworm.cpp b/src/object/motion/motionworm.cpp index 757e65fa..a2bdb483 100644 --- a/src/object/motion/motionworm.cpp +++ b/src/object/motion/motionworm.cpp @@ -345,7 +345,7 @@ bool CMotionWorm::EventFrame(const Event &event) pos = m_object->GetPartPosition(i+2); pos -= m_object->GetPartPosition(i+1); - angle.z = -Math::RotateAngle(Math::Point(pos.x, pos.z).Length(), pos.y); + angle.z = -Math::RotateAngle(glm::length(glm::vec2(pos.x, pos.z)), pos.y); angle.y = Math::PI-Math::RotateAngle(pos.x, pos.z); angle.x = 0.0f; m_object->SetPartRotation(i+1, angle); diff --git a/src/object/task/taskgoto.cpp b/src/object/task/taskgoto.cpp index 5b55fac8..fd0a6238 100644 --- a/src/object/task/taskgoto.cpp +++ b/src/object/task/taskgoto.cpp @@ -77,7 +77,7 @@ CTaskGoto::~CTaskGoto() bool CTaskGoto::EventProcess(const Event &event) { Math::Vector pos, goal; - Math::Point rot, repulse; + glm::vec2 rot, repulse; float a, g, dist, linSpeed, cirSpeed, h, hh, factor, dir; Error ret; @@ -162,7 +162,7 @@ bool CTaskGoto::EventProcess(const Event &event) rot.x = m_leakPos.x-pos.x; rot.y = m_leakPos.z-pos.z; - dist = Math::Point(rot.x, rot.y).Length(); + dist = glm::length(glm::vec2(rot.x, rot.y)); if (dist != 0) { rot.x /= dist; @@ -300,7 +300,7 @@ bool CTaskGoto::EventProcess(const Event &event) rot.x = m_bmPoints[m_bmIndex].x-pos.x; rot.y = m_bmPoints[m_bmIndex].z-pos.z; - dist = Math::Point(rot.x, rot.y).Length(); + dist = glm::length(glm::vec2(rot.x, rot.y)); rot.x /= dist; rot.y /= dist; @@ -447,7 +447,7 @@ bool CTaskGoto::EventProcess(const Event &event) rot.x = m_goal.x-pos.x; rot.y = m_goal.z-pos.z; - dist = Math::Point(rot.x, rot.y).Length(); + dist = glm::length(glm::vec2(rot.x, rot.y)); rot.x /= dist; rot.y /= dist; @@ -1421,10 +1421,10 @@ bool CTaskGoto::LeakSearch(Math::Vector &pos, float &delay) // Calculates the force of repulsion due to obstacles. // The vector length rendered is between 0 and 1. -void CTaskGoto::ComputeRepulse(Math::Point &dir) +void CTaskGoto::ComputeRepulse(glm::vec2&dir) { ObjectType iType, oType; - Math::Point repulse; + glm::vec2 repulse; float gDist, add, addi, fac, dist; bool bAlien; @@ -2131,7 +2131,7 @@ void CTaskGoto::BitmapSetCircle(const Math::Vector &pos, float radius) { for ( ix=cx-static_cast(r) ; ix<=cx+static_cast(r) ; ix++ ) { - d = Math::Point(static_cast(ix-cx), static_cast(iy-cy)).Length(); + d = glm::length(glm::vec2(static_cast(ix-cx), static_cast(iy-cy))); if ( d > r ) continue; BitmapSetDot(0, ix, iy); } @@ -2153,7 +2153,7 @@ void CTaskGoto::BitmapClearCircle(const Math::Vector &pos, float radius) { for ( ix=cx-static_cast(r) ; ix<=cx+static_cast(r) ; ix++ ) { - d = Math::Point(static_cast(ix-cx), static_cast(iy-cy)).Length(); + d = glm::length(glm::vec2(static_cast(ix-cx), static_cast(iy-cy))); if ( d > r ) continue; BitmapClearDot(0, ix, iy); } diff --git a/src/object/task/taskgoto.h b/src/object/task/taskgoto.h index 93148d25..f2d81b42 100644 --- a/src/object/task/taskgoto.h +++ b/src/object/task/taskgoto.h @@ -23,12 +23,9 @@ #include "math/vector.h" -#include +#include -namespace Math -{ -struct Point; -} // namespace Math; +#include class CObject; @@ -96,7 +93,7 @@ protected: bool AdjustBuilding(Math::Vector &pos, float margin, float &distance); bool GetHotPoint(CObject *pObj, Math::Vector &pos, bool bTake, float distance, float &suppl); bool LeakSearch(Math::Vector &pos, float &delay); - void ComputeRepulse(Math::Point &dir); + void ComputeRepulse(glm::vec2& dir); void ComputeFlyingRepulse(float &dir); int BeamShortcut(); diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp index 0ec79189..56df3d29 100644 --- a/src/physics/physics.cpp +++ b/src/physics/physics.cpp @@ -2323,7 +2323,7 @@ void CPhysics::FloorAdapt(float aTime, float rTime, if ( !m_bLand ) // in flight? { m_terrain->GetNormal(norm, pos); - a1 = fabs(Math::RotateAngle(Math::Point(norm.x, norm.z).Length(), norm.y)); + a1 = fabs(Math::RotateAngle(glm::length(glm::vec2(norm.x, norm.z)), norm.y)); if ( a1 < (90.0f-55.0f)*Math::PI/180.0f ) // slope exceeds 55 degrees? { bSlopingTerrain = true; // very sloped ground diff --git a/src/ui/controls/map.cpp b/src/ui/controls/map.cpp index 33c770b2..ff11b58a 100644 --- a/src/ui/controls/map.cpp +++ b/src/ui/controls/map.cpp @@ -31,6 +31,7 @@ #include "level/robotmain.h" #include "math/geometry.h" +#include "math/point.h" #include "object/interface/controllable_object.h" #include "object/interface/transportable_object.h" @@ -298,7 +299,7 @@ CObject* CMap::DetectObject(Math::Point pos, bool &bInMap) if ( m_map[i].color == MAPCOLOR_ALIEN && !m_bRadar ) continue; - dist = Math::Point(m_map[i].pos.x - pos.x, m_map[i].pos.y - pos.y).Length(); + dist = glm::length(glm::vec2(m_map[i].pos.x - pos.x, m_map[i].pos.y - pos.y)); if ( dist > m_half / m_zoom * 8.0f / 100.0f ) continue; // too far? if ( dist < min ) @@ -1197,7 +1198,7 @@ void CMap::UpdateObject(CObject* pObj) if ( m_angle != 0.0f ) { - ppos = RotatePoint(m_angle, Math::Point(pos.x, pos.z)); + ppos = Math::RotatePoint(m_angle, glm::vec2(pos.x, pos.z)); pos.x = ppos.x; pos.z = ppos.y; dir += m_angle;