From 7ee94f23df560f92341127130f628742cfccd776 Mon Sep 17 00:00:00 2001 From: wraitii Date: Thu, 15 Apr 2021 13:01:24 +0000 Subject: [PATCH] Use type_identity to simplify Clamp usage. Differential Revision: https://code.wildfiregames.com/D3859 This was SVN commit r25266. --- source/graphics/Camera.cpp | 8 +-- source/graphics/Decal.cpp | 10 ++-- source/graphics/HFTracer.cpp | 6 +-- source/graphics/SmoothedValue.h | 4 +- source/graphics/Terrain.cpp | 54 +++++++++---------- source/maths/MathUtil.h | 13 ++++- source/renderer/SilhouetteRenderer.cpp | 14 ++--- .../simulation2/components/CCmpPathfinder.cpp | 8 +-- 8 files changed, 63 insertions(+), 54 deletions(-) diff --git a/source/graphics/Camera.cpp b/source/graphics/Camera.cpp index 00ba650ce3..163ea20176 100644 --- a/source/graphics/Camera.cpp +++ b/source/graphics/Camera.cpp @@ -262,8 +262,8 @@ CVector3D CCamera::GetWorldCoordinates(int px, int py, bool aboveWater) const ssize_t mapSize = g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide(); if (gotWater) { - waterPoint.X = Clamp(waterPoint.X, 0.f, static_cast((mapSize - 1) * TERRAIN_TILE_SIZE)); - waterPoint.Z = Clamp(waterPoint.Z, 0.f, static_cast((mapSize - 1) * TERRAIN_TILE_SIZE)); + waterPoint.X = Clamp(waterPoint.X, 0.f, (mapSize - 1) * TERRAIN_TILE_SIZE); + waterPoint.Z = Clamp(waterPoint.Z, 0.f, (mapSize - 1) * TERRAIN_TILE_SIZE); } if (gotTerrain) @@ -340,8 +340,8 @@ CVector3D CCamera::GetFocus() const ssize_t mapSize = g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide(); if (gotWater) { - waterPoint.X = Clamp(waterPoint.X, 0.f, static_cast((mapSize - 1) * TERRAIN_TILE_SIZE)); - waterPoint.Z = Clamp(waterPoint.Z, 0.f, static_cast((mapSize - 1) * TERRAIN_TILE_SIZE)); + waterPoint.X = Clamp(waterPoint.X, 0.f, (mapSize - 1) * TERRAIN_TILE_SIZE); + waterPoint.Z = Clamp(waterPoint.Z, 0.f, (mapSize - 1) * TERRAIN_TILE_SIZE); } if (gotTerrain) diff --git a/source/graphics/Decal.cpp b/source/graphics/Decal.cpp index f8b513da98..0c5019aea0 100644 --- a/source/graphics/Decal.cpp +++ b/source/graphics/Decal.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -45,10 +45,10 @@ void CModelDecal::CalcVertexExtents(ssize_t& i0, ssize_t& j0, ssize_t& i1, ssize i1 = ceil(std::max(std::max(corner0.X, corner1.X), std::max(corner2.X, corner3.X)) / TERRAIN_TILE_SIZE); j1 = ceil(std::max(std::max(corner0.Z, corner1.Z), std::max(corner2.Z, corner3.Z)) / TERRAIN_TILE_SIZE); - i0 = Clamp(i0, static_cast(0), m_Terrain->GetVerticesPerSide() - 1); - j0 = Clamp(j0, static_cast(0), m_Terrain->GetVerticesPerSide() - 1); - i1 = Clamp(i1, static_cast(0), m_Terrain->GetVerticesPerSide() - 1); - j1 = Clamp(j1, static_cast(0), m_Terrain->GetVerticesPerSide() - 1); + i0 = Clamp(i0, 0, m_Terrain->GetVerticesPerSide() - 1); + j0 = Clamp(j0, 0, m_Terrain->GetVerticesPerSide() - 1); + i1 = Clamp(i1, 0, m_Terrain->GetVerticesPerSide() - 1); + j1 = Clamp(j1, 0, m_Terrain->GetVerticesPerSide() - 1); } void CModelDecal::CalcBounds() diff --git a/source/graphics/HFTracer.cpp b/source/graphics/HFTracer.cpp index c0c7418ae1..5217c0cd50 100644 --- a/source/graphics/HFTracer.cpp +++ b/source/graphics/HFTracer.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -323,8 +323,8 @@ bool CHFTracer::PatchRayIntersect(CPatch* patch, const CVector3D& origin, const // should be extremely rare, and it's safe and simple).) // Work out which tile we're starting in - int i = Clamp(static_cast(entryPatch.X / TERRAIN_TILE_SIZE), 0, static_cast(PATCH_SIZE) - 1); - int j = Clamp(static_cast(entryPatch.Z / TERRAIN_TILE_SIZE), 0, static_cast(PATCH_SIZE) - 1); + int i = Clamp(entryPatch.X / TERRAIN_TILE_SIZE, 0, PATCH_SIZE - 1); + int j = Clamp(entryPatch.Z / TERRAIN_TILE_SIZE, 0, PATCH_SIZE - 1); // Work out which direction the ray is going in int di = (dir.X >= 0 ? 1 : 0); diff --git a/source/graphics/SmoothedValue.h b/source/graphics/SmoothedValue.h index 1b78039cfa..461b015fda 100644 --- a/source/graphics/SmoothedValue.h +++ b/source/graphics/SmoothedValue.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -72,7 +72,7 @@ public: void ClampSmoothly(float min, float max) { - m_Target = Clamp(m_Target, static_cast(min), static_cast(max)); + m_Target = Clamp(m_Target, min, max); } float Update(float time); diff --git a/source/graphics/Terrain.cpp b/source/graphics/Terrain.cpp index 67b620870a..0d4ed7677a 100644 --- a/source/graphics/Terrain.cpp +++ b/source/graphics/Terrain.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -118,8 +118,8 @@ CStr8 CTerrain::GetMovementClass(ssize_t i, ssize_t j) const // outwards to infinity void CTerrain::CalcPosition(ssize_t i, ssize_t j, CVector3D& pos) const { - ssize_t hi = Clamp(i, static_cast(0), m_MapSize - 1); - ssize_t hj = Clamp(j, static_cast(0), m_MapSize - 1); + ssize_t hi = Clamp(i, 0, m_MapSize - 1); + ssize_t hj = Clamp(j, 0, m_MapSize - 1); u16 height = m_Heightmap[hj*m_MapSize + hi]; pos.X = float(i*TERRAIN_TILE_SIZE); pos.Y = float(height*HEIGHT_SCALE); @@ -130,8 +130,8 @@ void CTerrain::CalcPosition(ssize_t i, ssize_t j, CVector3D& pos) const // CalcPositionFixed: calculate the world space position of the vertex at (i,j) void CTerrain::CalcPositionFixed(ssize_t i, ssize_t j, CFixedVector3D& pos) const { - ssize_t hi = Clamp(i, static_cast(0), m_MapSize - 1); - ssize_t hj = Clamp(j, static_cast(0), m_MapSize - 1); + ssize_t hi = Clamp(i, 0, m_MapSize - 1); + ssize_t hj = Clamp(j, 0, m_MapSize - 1); u16 height = m_Heightmap[hj*m_MapSize + hi]; pos.X = fixed::FromInt(i) * (int)TERRAIN_TILE_SIZE; // fixed max value is 32767, but height is a u16, so divide by two to avoid overflow @@ -236,8 +236,8 @@ void CTerrain::CalcNormalFixed(ssize_t i, ssize_t j, CFixedVector3D& normal) con CVector3D CTerrain::CalcExactNormal(float x, float z) const { // Clamp to size-2 so we can use the tiles (xi,zi)-(xi+1,zi+1) - const ssize_t xi = Clamp(static_cast(floor(x / TERRAIN_TILE_SIZE)), static_cast(0), m_MapSize - 2); - const ssize_t zi = Clamp(static_cast(floor(z / TERRAIN_TILE_SIZE)), static_cast(0), m_MapSize - 2); + const ssize_t xi = Clamp(floor(x / TERRAIN_TILE_SIZE), 0, m_MapSize - 2); + const ssize_t zi = Clamp(floor(z / TERRAIN_TILE_SIZE), 0, m_MapSize - 2); const float xf = Clamp(x / TERRAIN_TILE_SIZE-xi, 0.0f, 1.0f); const float zf = Clamp(z / TERRAIN_TILE_SIZE-zi, 0.0f, 1.0f); @@ -308,15 +308,15 @@ CMiniPatch* CTerrain::GetTile(ssize_t i, ssize_t j) const float CTerrain::GetVertexGroundLevel(ssize_t i, ssize_t j) const { - i = Clamp(i, static_cast(0), m_MapSize - 1); - j = Clamp(j, static_cast(0), m_MapSize - 1); + i = Clamp(i, 0, m_MapSize - 1); + j = Clamp(j, 0, m_MapSize - 1); return HEIGHT_SCALE * m_Heightmap[j*m_MapSize + i]; } fixed CTerrain::GetVertexGroundLevelFixed(ssize_t i, ssize_t j) const { - i = Clamp(i, static_cast(0), m_MapSize - 1); - j = Clamp(j, static_cast(0), m_MapSize - 1); + i = Clamp(i, 0, m_MapSize - 1); + j = Clamp(j, 0, m_MapSize - 1); // Convert to fixed metres (being careful to avoid intermediate overflows) return fixed::FromInt(m_Heightmap[j*m_MapSize + i] / 2) / (int)(HEIGHT_UNITS_PER_METRE / 2); } @@ -324,8 +324,8 @@ fixed CTerrain::GetVertexGroundLevelFixed(ssize_t i, ssize_t j) const fixed CTerrain::GetSlopeFixed(ssize_t i, ssize_t j) const { // Clamp to size-2 so we can use the tiles (i,j)-(i+1,j+1) - i = Clamp(i, static_cast(0), m_MapSize - 2); - j = Clamp(j, static_cast(0), m_MapSize - 2); + i = Clamp(i, 0, m_MapSize - 2); + j = Clamp(j, 0, m_MapSize - 2); u16 h00 = m_Heightmap[j*m_MapSize + i]; u16 h01 = m_Heightmap[(j+1)*m_MapSize + i]; @@ -480,8 +480,8 @@ fixed CTerrain::GetExactGroundLevelFixed(fixed x, fixed z) const bool CTerrain::GetTriangulationDir(ssize_t i, ssize_t j) const { // Clamp to size-2 so we can use the tiles (i,j)-(i+1,j+1) - i = Clamp(i, static_cast(0), m_MapSize - 2); - j = Clamp(j, static_cast(0), m_MapSize - 2); + i = Clamp(i, 0, m_MapSize - 2); + j = Clamp(j, 0, m_MapSize - 2); int h00 = m_Heightmap[j*m_MapSize + i]; int h01 = m_Heightmap[(j+1)*m_MapSize + i]; @@ -774,10 +774,10 @@ void CTerrain::SetHeightMap(u16* heightmap) void CTerrain::MakeDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1, int dirtyFlags) { // Finds the inclusive limits of the patches that include the specified range of tiles - ssize_t pi0 = Clamp( i0 /PATCH_SIZE, static_cast(0), m_MapSizePatches-1); - ssize_t pi1 = Clamp((i1-1)/PATCH_SIZE, static_cast(0), m_MapSizePatches-1); - ssize_t pj0 = Clamp( j0 /PATCH_SIZE, static_cast(0), m_MapSizePatches-1); - ssize_t pj1 = Clamp((j1-1)/PATCH_SIZE, static_cast(0), m_MapSizePatches-1); + ssize_t pi0 = Clamp( i0 /PATCH_SIZE, 0, m_MapSizePatches-1); + ssize_t pi1 = Clamp((i1-1)/PATCH_SIZE, 0, m_MapSizePatches-1); + ssize_t pj0 = Clamp( j0 /PATCH_SIZE, 0, m_MapSizePatches-1); + ssize_t pj1 = Clamp((j1-1)/PATCH_SIZE, 0, m_MapSizePatches-1); for (ssize_t j = pj0; j <= pj1; j++) { @@ -793,10 +793,10 @@ void CTerrain::MakeDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1, int dir if (m_Heightmap) { m_HeightMipmap.Update(m_Heightmap, - Clamp(i0, static_cast(0), m_MapSize - 1), - Clamp(j0, static_cast(0), m_MapSize - 1), - Clamp(i1, static_cast(1), m_MapSize), - Clamp(j1, static_cast(1), m_MapSize) + Clamp(i0, 0, m_MapSize - 1), + Clamp(j0, 0, m_MapSize - 1), + Clamp(i1, 1, m_MapSize), + Clamp(j1, 1, m_MapSize) ); } } @@ -820,10 +820,10 @@ void CTerrain::MakeDirty(int dirtyFlags) CBoundingBoxAligned CTerrain::GetVertexesBound(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1) { - i0 = Clamp(i0, static_cast(0), m_MapSize - 1); - j0 = Clamp(j0, static_cast(0), m_MapSize - 1); - i1 = Clamp(i1, static_cast(0), m_MapSize - 1); - j1 = Clamp(j1, static_cast(0), m_MapSize - 1); + i0 = Clamp(i0, 0, m_MapSize - 1); + j0 = Clamp(j0, 0, m_MapSize - 1); + i1 = Clamp(i1, 0, m_MapSize - 1); + j1 = Clamp(j1, 0, m_MapSize - 1); u16 minH = 65535; u16 maxH = 0; diff --git a/source/maths/MathUtil.h b/source/maths/MathUtil.h index 6f2cd6de0e..b0021f6e9e 100644 --- a/source/maths/MathUtil.h +++ b/source/maths/MathUtil.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -28,8 +28,17 @@ inline T Interpolate(const T& a, const T& b, float t) return a + (b - a) * t; } +// TODO C++20: use the proper one. +template +struct type_identity +{ + using type = T; +}; +template +using type_identity_t = typename type_identity::type; + template -inline T Clamp(T value, T min, T max) +inline T Clamp(T value, type_identity_t min, type_identity_t max) { if (value <= min) return min; diff --git a/source/renderer/SilhouetteRenderer.cpp b/source/renderer/SilhouetteRenderer.cpp index 4793af25ab..548bc40b08 100644 --- a/source/renderer/SilhouetteRenderer.cpp +++ b/source/renderer/SilhouetteRenderer.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2018 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -189,10 +189,10 @@ static void ComputeScreenBounds(Occluder& occluder, const CBoundingBoxAligned& b // TODO: there must be a quicker way to do this than to test every vertex, // given the symmetry of the bounding box - occluder.x0 = Clamp(x0, std::numeric_limits::min(), static_cast(g_MaxCoord - 1)); - occluder.y0 = Clamp(y0, std::numeric_limits::min(), static_cast(g_MaxCoord - 1)); - occluder.x1 = Clamp(x1, std::numeric_limits::min(), static_cast(g_MaxCoord - 1)); - occluder.y1 = Clamp(y1, std::numeric_limits::min(), static_cast(g_MaxCoord - 1)); + occluder.x0 = Clamp(x0, std::numeric_limits::min(), g_MaxCoord - 1); + occluder.y0 = Clamp(y0, std::numeric_limits::min(), g_MaxCoord - 1); + occluder.x1 = Clamp(x1, std::numeric_limits::min(), g_MaxCoord - 1); + occluder.y1 = Clamp(y1, std::numeric_limits::min(), g_MaxCoord - 1); occluder.z = z0; } @@ -201,8 +201,8 @@ static void ComputeScreenPos(Caster& caster, const CVector3D& pos, CMatrix3D& pr CVector4D svec = proj.Transform(CVector4D(pos.X, pos.Y, pos.Z, 1.0f)); u16 x = g_HalfMaxCoord + static_cast(g_HalfMaxCoord * svec.X / svec.W); u16 y = g_HalfMaxCoord + static_cast(g_HalfMaxCoord * svec.Y / svec.W); - caster.x = Clamp(x, std::numeric_limits::min(), static_cast(g_MaxCoord - 1)); - caster.y = Clamp(y, std::numeric_limits::min(), static_cast(g_MaxCoord - 1)); + caster.x = Clamp(x, std::numeric_limits::min(), g_MaxCoord - 1); + caster.y = Clamp(y, std::numeric_limits::min(), g_MaxCoord - 1); caster.z = svec.Z / svec.W; } diff --git a/source/simulation2/components/CCmpPathfinder.cpp b/source/simulation2/components/CCmpPathfinder.cpp index db9c728fcb..8e99a27e03 100644 --- a/source/simulation2/components/CCmpPathfinder.cpp +++ b/source/simulation2/components/CCmpPathfinder.cpp @@ -613,10 +613,10 @@ void CCmpPathfinder::TerrainUpdateHelper(bool expandPassability, int itile0, int // We need to extend the boundaries by 1 tile, because slope and ground // level are calculated by multiple neighboring tiles. // TODO: add CTerrain constant instead of 1. - istart = Clamp(itile0 - 1, 0, static_cast(m_MapSize)) * Pathfinding::NAVCELLS_PER_TILE; - iend = Clamp(itile1 + 1, 0, static_cast(m_MapSize)) * Pathfinding::NAVCELLS_PER_TILE; - jstart = Clamp(jtile0 - 1, 0, static_cast(m_MapSize)) * Pathfinding::NAVCELLS_PER_TILE; - jend = Clamp(jtile1 + 1, 0, static_cast(m_MapSize)) * Pathfinding::NAVCELLS_PER_TILE; + istart = Clamp(itile0 - 1, 0, m_MapSize) * Pathfinding::NAVCELLS_PER_TILE; + iend = Clamp(itile1 + 1, 0, m_MapSize) * Pathfinding::NAVCELLS_PER_TILE; + jstart = Clamp(jtile0 - 1, 0, m_MapSize) * Pathfinding::NAVCELLS_PER_TILE; + jend = Clamp(jtile1 + 1, 0, m_MapSize) * Pathfinding::NAVCELLS_PER_TILE; } // Compute initial terrain-dependent passability