From 913545aa41b74a07559e1322287e2f4c028bd328 Mon Sep 17 00:00:00 2001 From: wraitii Date: Wed, 11 Nov 2015 20:50:02 +0000 Subject: [PATCH] Mark several CFixedVector2D as const and passed by reference in Geometry and a few other places. Mark some functions (that probably already were) inline. Also make sure we don't include Geometry.h where it's not necessary. This was SVN commit r17238. --- source/maths/FixedVector2D.h | 4 +-- .../components/CCmpObstructionManager.cpp | 4 +-- .../components/CCmpPathfinder_Common.h | 1 - .../components/CCmpPathfinder_Vertex.cpp | 12 ++++----- .../components/CCmpTerritoryManager.cpp | 1 - source/simulation2/helpers/Geometry.cpp | 18 ++++++------- source/simulation2/helpers/Geometry.h | 25 +++++++++---------- source/simulation2/helpers/LongPathfinder.cpp | 2 ++ source/simulation2/helpers/PathGoal.cpp | 2 ++ source/simulation2/helpers/Pathfinding.h | 2 +- 10 files changed, 36 insertions(+), 35 deletions(-) diff --git a/source/maths/FixedVector2D.h b/source/maths/FixedVector2D.h index 9e6e34b71b..d2728f412a 100644 --- a/source/maths/FixedVector2D.h +++ b/source/maths/FixedVector2D.h @@ -198,7 +198,7 @@ public: /** * Compute the dot product of this vector with another. */ - fixed Dot(const CFixedVector2D& v) + fixed Dot(const CFixedVector2D& v) const { i64 x = FIXED_MUL_I64_I32_I32(X.GetInternalValue(), v.X.GetInternalValue()); i64 y = FIXED_MUL_I64_I32_I32(Y.GetInternalValue(), v.Y.GetInternalValue()); @@ -212,7 +212,7 @@ public: return ret; } - CFixedVector2D Perpendicular() + CFixedVector2D Perpendicular() const { return CFixedVector2D(Y, -X); } diff --git a/source/simulation2/components/CCmpObstructionManager.cpp b/source/simulation2/components/CCmpObstructionManager.cpp index e292a1873f..cc8a3a673e 100644 --- a/source/simulation2/components/CCmpObstructionManager.cpp +++ b/source/simulation2/components/CCmpObstructionManager.cpp @@ -640,7 +640,7 @@ private: /** * Return whether the given point is within the world bounds by at least r */ - bool IsInWorld(entity_pos_t x, entity_pos_t z, entity_pos_t r) + inline bool IsInWorld(entity_pos_t x, entity_pos_t z, entity_pos_t r) { return (m_WorldX0+r <= x && x <= m_WorldX1-r && m_WorldZ0+r <= z && z <= m_WorldZ1-r); } @@ -648,7 +648,7 @@ private: /** * Return whether the given point is within the world bounds */ - bool IsInWorld(CFixedVector2D p) + inline bool IsInWorld(const CFixedVector2D& p) { return (m_WorldX0 <= p.X && p.X <= m_WorldX1 && m_WorldZ0 <= p.Y && p.Y <= m_WorldZ1); } diff --git a/source/simulation2/components/CCmpPathfinder_Common.h b/source/simulation2/components/CCmpPathfinder_Common.h index a0b5158108..48cf851ef8 100644 --- a/source/simulation2/components/CCmpPathfinder_Common.h +++ b/source/simulation2/components/CCmpPathfinder_Common.h @@ -36,7 +36,6 @@ #include "maths/MathUtil.h" #include "ps/CLogger.h" #include "simulation2/components/ICmpObstructionManager.h" -#include "simulation2/helpers/Geometry.h" #include "simulation2/helpers/LongPathfinder.h" class SceneCollector; diff --git a/source/simulation2/components/CCmpPathfinder_Vertex.cpp b/source/simulation2/components/CCmpPathfinder_Vertex.cpp index bf5f62d7fa..1af85730aa 100644 --- a/source/simulation2/components/CCmpPathfinder_Vertex.cpp +++ b/source/simulation2/components/CCmpPathfinder_Vertex.cpp @@ -133,7 +133,7 @@ static const entity_pos_t EDGE_EXPAND_DELTA = entity_pos_t::FromInt(1)/16; * Check whether a ray from 'a' to 'b' crosses any of the edges. * (Edges are one-sided so it's only considered a cross if going from front to back.) */ -inline static bool CheckVisibility(CFixedVector2D a, CFixedVector2D b, const std::vector& edges) +inline static bool CheckVisibility(const CFixedVector2D& a, const CFixedVector2D& b, const std::vector& edges) { CFixedVector2D abn = (b - a).Perpendicular(); @@ -177,7 +177,7 @@ inline static bool CheckVisibility(CFixedVector2D a, CFixedVector2D b, const std // They assume the caller has already excluded edges for which 'a' is // on the wrong side.) -inline static bool CheckVisibilityLeft(CFixedVector2D a, CFixedVector2D b, const std::vector& edges) +inline static bool CheckVisibilityLeft(const CFixedVector2D& a, const CFixedVector2D& b, const std::vector& edges) { if (a.X >= b.X) return true; @@ -205,7 +205,7 @@ inline static bool CheckVisibilityLeft(CFixedVector2D a, CFixedVector2D b, const return true; } -inline static bool CheckVisibilityRight(CFixedVector2D a, CFixedVector2D b, const std::vector& edges) +inline static bool CheckVisibilityRight(const CFixedVector2D& a, const CFixedVector2D& b, const std::vector& edges) { if (a.X <= b.X) return true; @@ -233,7 +233,7 @@ inline static bool CheckVisibilityRight(CFixedVector2D a, CFixedVector2D b, cons return true; } -inline static bool CheckVisibilityBottom(CFixedVector2D a, CFixedVector2D b, const std::vector& edges) +inline static bool CheckVisibilityBottom(const CFixedVector2D& a, const CFixedVector2D& b, const std::vector& edges) { if (a.Y >= b.Y) return true; @@ -261,7 +261,7 @@ inline static bool CheckVisibilityBottom(CFixedVector2D a, CFixedVector2D b, con return true; } -inline static bool CheckVisibilityTop(CFixedVector2D a, CFixedVector2D b, const std::vector& edges) +inline static bool CheckVisibilityTop(const CFixedVector2D& a, const CFixedVector2D& b, const std::vector& edges) { if (a.Y <= b.Y) return true; @@ -480,7 +480,7 @@ static void AddTerrainEdges(std::vector& edges, std::vector& verte } } -static void SplitAAEdges(CFixedVector2D a, +static void SplitAAEdges(const CFixedVector2D& a, const std::vector& edges, const std::vector& squares, std::vector& edgesUnaligned, diff --git a/source/simulation2/components/CCmpTerritoryManager.cpp b/source/simulation2/components/CCmpTerritoryManager.cpp index e43650358a..3d963c50ec 100644 --- a/source/simulation2/components/CCmpTerritoryManager.cpp +++ b/source/simulation2/components/CCmpTerritoryManager.cpp @@ -36,7 +36,6 @@ #include "simulation2/components/ICmpPlayerManager.h" #include "simulation2/components/ICmpPosition.h" #include "simulation2/components/ICmpTerritoryInfluence.h" -#include "simulation2/helpers/Geometry.h" #include "simulation2/helpers/Grid.h" #include "simulation2/helpers/Render.h" diff --git a/source/simulation2/helpers/Geometry.cpp b/source/simulation2/helpers/Geometry.cpp index ba0e8059ca..8ece08679b 100644 --- a/source/simulation2/helpers/Geometry.cpp +++ b/source/simulation2/helpers/Geometry.cpp @@ -23,7 +23,7 @@ using namespace Geometry; // TODO: all of these things could be optimised quite easily -CFixedVector2D Geometry::GetHalfBoundingBox(CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize) +CFixedVector2D Geometry::GetHalfBoundingBox(const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize) { return CFixedVector2D( u.X.Multiply(halfSize.X).Absolute() + v.X.Multiply(halfSize.Y).Absolute(), @@ -36,7 +36,7 @@ float Geometry::ChordToCentralAngle(const float chordLength, const float radius) return acosf(1.f - SQR(chordLength)/(2.f*SQR(radius))); // cfr. law of cosines } -fixed Geometry::DistanceToSquare(CFixedVector2D point, CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize, bool countInsideAsZero) +fixed Geometry::DistanceToSquare(const CFixedVector2D& point, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize, bool countInsideAsZero) { /* * Relative to its own coordinate system, we have a square like: @@ -102,7 +102,7 @@ fixed Geometry::DistanceToSquare(CFixedVector2D point, CFixedVector2D u, CFixedV // Same as above except it does not use Length. // For explanations refer to DistanceToSquare -fixed Geometry::DistanceToSquareSquared(CFixedVector2D point, CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize, bool countInsideAsZero) +fixed Geometry::DistanceToSquareSquared(const CFixedVector2D& point, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize, bool countInsideAsZero) { fixed du = point.Dot(u); fixed dv = point.Dot(v); @@ -140,7 +140,7 @@ fixed Geometry::DistanceToSquareSquared(CFixedVector2D point, CFixedVector2D u, } } -CFixedVector2D Geometry::NearestPointOnSquare(CFixedVector2D point, CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize) +CFixedVector2D Geometry::NearestPointOnSquare(const CFixedVector2D& point, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize) { /* * Relative to its own coordinate system, we have a square like: @@ -214,7 +214,7 @@ CFixedVector2D Geometry::NearestPointOnSquare(CFixedVector2D point, CFixedVector } } -bool Geometry::TestRaySquare(CFixedVector2D a, CFixedVector2D b, CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize) +bool Geometry::TestRaySquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize) { /* * We only consider collisions to be when the ray goes from outside to inside the shape (and possibly out again). @@ -262,7 +262,7 @@ bool Geometry::TestRaySquare(CFixedVector2D a, CFixedVector2D b, CFixedVector2D return false; } -bool Geometry::TestRayAASquare(CFixedVector2D a, CFixedVector2D b, CFixedVector2D halfSize) +bool Geometry::TestRayAASquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& halfSize) { // Exactly like TestRaySquare with u=(1,0), v=(0,1) @@ -308,7 +308,7 @@ bool Geometry::TestRayAASquare(CFixedVector2D a, CFixedVector2D b, CFixedVector2 * Separating axis test; returns true if the square defined by u/v/halfSize at the origin * is not entirely on the clockwise side of a line in direction 'axis' passing through 'a' */ -static bool SquareSAT(CFixedVector2D a, CFixedVector2D axis, CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize) +static bool SquareSAT(const CFixedVector2D& a, const CFixedVector2D& axis, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize) { fixed hw = halfSize.X; fixed hh = halfSize.Y; @@ -327,8 +327,8 @@ static bool SquareSAT(CFixedVector2D a, CFixedVector2D axis, CFixedVector2D u, C } bool Geometry::TestSquareSquare( - CFixedVector2D c0, CFixedVector2D u0, CFixedVector2D v0, CFixedVector2D halfSize0, - CFixedVector2D c1, CFixedVector2D u1, CFixedVector2D v1, CFixedVector2D halfSize1) + const CFixedVector2D& c0, const CFixedVector2D& u0, const CFixedVector2D& v0, const CFixedVector2D& halfSize0, + const CFixedVector2D& c1, const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1) { // TODO: need to test this carefully diff --git a/source/simulation2/helpers/Geometry.h b/source/simulation2/helpers/Geometry.h index 887ea7036e..6256e4f86a 100644 --- a/source/simulation2/helpers/Geometry.h +++ b/source/simulation2/helpers/Geometry.h @@ -27,7 +27,6 @@ #include "maths/FixedVector2D.h" #include "maths/MathUtil.h" - namespace Geometry { @@ -40,7 +39,7 @@ namespace Geometry * * The @p u and @p v vectors must be perpendicular. */ -inline bool PointIsInSquare(CFixedVector2D point, CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize) +inline bool PointIsInSquare(const CFixedVector2D& point, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize) { return point.Dot(u).Absolute() <= halfSize.X && point.Dot(v).Absolute() <= halfSize.Y; } @@ -53,7 +52,7 @@ inline bool PointIsInSquare(CFixedVector2D point, CFixedVector2D u, CFixedVector * The rectangle is defined by the four vertexes * (+/-u*halfSize.X +/-v*halfSize.Y). */ -CFixedVector2D GetHalfBoundingBox(CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize); +CFixedVector2D GetHalfBoundingBox(const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize); /** * Returns the minimum Euclidean distance from the given point to @@ -69,15 +68,15 @@ CFixedVector2D GetHalfBoundingBox(CFixedVector2D u, CFixedVector2D v, CFixedVect * * The @p u and @p v vectors must be perpendicular and unit length. */ -fixed DistanceToSquare(CFixedVector2D point, - CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize, +fixed DistanceToSquare(const CFixedVector2D& point, + const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize, bool countInsideAsZero = false); /** * Similar to above but never uses sqrt, so it returns the squared distance. */ -fixed DistanceToSquareSquared(CFixedVector2D point, - CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize, +fixed DistanceToSquareSquared(const CFixedVector2D& point, + const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize, bool countInsideAsZero = false); /** * Returns a point on the boundary of the given rotated rectangle @@ -89,8 +88,8 @@ fixed DistanceToSquareSquared(CFixedVector2D point, * * The @p u and @p v vectors must be perpendicular and unit length. */ -CFixedVector2D NearestPointOnSquare(CFixedVector2D point, - CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize); +CFixedVector2D NearestPointOnSquare(const CFixedVector2D& point, + const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize); /** * Given a circle of radius @p radius, and a chord of length @p chordLength @@ -101,13 +100,13 @@ CFixedVector2D NearestPointOnSquare(CFixedVector2D point, */ float ChordToCentralAngle(const float chordLength, const float radius); -bool TestRaySquare(CFixedVector2D a, CFixedVector2D b, CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize); +bool TestRaySquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize); -bool TestRayAASquare(CFixedVector2D a, CFixedVector2D b, CFixedVector2D halfSize); +bool TestRayAASquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& halfSize); bool TestSquareSquare( - CFixedVector2D c0, CFixedVector2D u0, CFixedVector2D v0, CFixedVector2D halfSize0, - CFixedVector2D c1, CFixedVector2D u1, CFixedVector2D v1, CFixedVector2D halfSize1); + const CFixedVector2D& c0, const CFixedVector2D& u0, const CFixedVector2D& v0, const CFixedVector2D& halfSize0, + const CFixedVector2D& c1, const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1); } // namespace diff --git a/source/simulation2/helpers/LongPathfinder.cpp b/source/simulation2/helpers/LongPathfinder.cpp index ce2f810ff4..6cb666b2d9 100644 --- a/source/simulation2/helpers/LongPathfinder.cpp +++ b/source/simulation2/helpers/LongPathfinder.cpp @@ -22,6 +22,8 @@ #include "lib/bits.h" #include "ps/Profile.h" +#include "Geometry.h" + /** * Jump point cache. * diff --git a/source/simulation2/helpers/PathGoal.cpp b/source/simulation2/helpers/PathGoal.cpp index 20568ebe60..462a630c38 100644 --- a/source/simulation2/helpers/PathGoal.cpp +++ b/source/simulation2/helpers/PathGoal.cpp @@ -22,6 +22,8 @@ #include "graphics/Terrain.h" #include "Pathfinding.h" +#include "Geometry.h" + static bool NavcellContainsCircle(int i, int j, fixed x, fixed z, fixed r, bool inside) { // Accept any navcell (i,j) that contains a point which is inside[/outside] diff --git a/source/simulation2/helpers/Pathfinding.h b/source/simulation2/helpers/Pathfinding.h index 4c13e292de..56280431c3 100644 --- a/source/simulation2/helpers/Pathfinding.h +++ b/source/simulation2/helpers/Pathfinding.h @@ -18,11 +18,11 @@ #ifndef INCLUDED_PATHFINDING #define INCLUDED_PATHFINDING +#include "maths/MathUtil.h" #include "ps/CLogger.h" #include "simulation2/system/ParamNode.h" #include "graphics/Terrain.h" -#include "Geometry.h" #include "Grid.h" #include "PathGoal.h"