Use type_identity to simplify Clamp usage.

Differential Revision: https://code.wildfiregames.com/D3859
This was SVN commit r25266.
This commit is contained in:
wraitii 2021-04-15 13:01:24 +00:00
parent b74ab54cf7
commit 7ee94f23df
8 changed files with 63 additions and 54 deletions

View File

@ -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<float>((mapSize - 1) * TERRAIN_TILE_SIZE));
waterPoint.Z = Clamp(waterPoint.Z, 0.f, static_cast<float>((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<float>((mapSize - 1) * TERRAIN_TILE_SIZE));
waterPoint.Z = Clamp(waterPoint.Z, 0.f, static_cast<float>((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)

View File

@ -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<ssize_t>(0), m_Terrain->GetVerticesPerSide() - 1);
j0 = Clamp(j0, static_cast<ssize_t>(0), m_Terrain->GetVerticesPerSide() - 1);
i1 = Clamp(i1, static_cast<ssize_t>(0), m_Terrain->GetVerticesPerSide() - 1);
j1 = Clamp(j1, static_cast<ssize_t>(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()

View File

@ -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<int>(entryPatch.X / TERRAIN_TILE_SIZE), 0, static_cast<int>(PATCH_SIZE) - 1);
int j = Clamp(static_cast<int>(entryPatch.Z / TERRAIN_TILE_SIZE), 0, static_cast<int>(PATCH_SIZE) - 1);
int i = Clamp<int>(entryPatch.X / TERRAIN_TILE_SIZE, 0, PATCH_SIZE - 1);
int j = Clamp<int>(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);

View File

@ -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<double>(min), static_cast<double>(max));
m_Target = Clamp(m_Target, min, max);
}
float Update(float time);

View File

@ -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<ssize_t>(0), m_MapSize - 1);
ssize_t hj = Clamp(j, static_cast<ssize_t>(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<ssize_t>(0), m_MapSize - 1);
ssize_t hj = Clamp(j, static_cast<ssize_t>(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<ssize_t>(floor(x / TERRAIN_TILE_SIZE)), static_cast<ssize_t>(0), m_MapSize - 2);
const ssize_t zi = Clamp(static_cast<ssize_t>(floor(z / TERRAIN_TILE_SIZE)), static_cast<ssize_t>(0), m_MapSize - 2);
const ssize_t xi = Clamp<ssize_t>(floor(x / TERRAIN_TILE_SIZE), 0, m_MapSize - 2);
const ssize_t zi = Clamp<ssize_t>(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<ssize_t>(0), m_MapSize - 1);
j = Clamp(j, static_cast<ssize_t>(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<ssize_t>(0), m_MapSize - 1);
j = Clamp(j, static_cast<ssize_t>(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<ssize_t>(0), m_MapSize - 2);
j = Clamp(j, static_cast<ssize_t>(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<ssize_t>(0), m_MapSize - 2);
j = Clamp(j, static_cast<ssize_t>(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<ssize_t>(0), m_MapSizePatches-1);
ssize_t pi1 = Clamp((i1-1)/PATCH_SIZE, static_cast<ssize_t>(0), m_MapSizePatches-1);
ssize_t pj0 = Clamp( j0 /PATCH_SIZE, static_cast<ssize_t>(0), m_MapSizePatches-1);
ssize_t pj1 = Clamp((j1-1)/PATCH_SIZE, static_cast<ssize_t>(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<ssize_t>(0), m_MapSize - 1),
Clamp(j0, static_cast<ssize_t>(0), m_MapSize - 1),
Clamp(i1, static_cast<ssize_t>(1), m_MapSize),
Clamp(j1, static_cast<ssize_t>(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<ssize_t>(0), m_MapSize - 1);
j0 = Clamp(j0, static_cast<ssize_t>(0), m_MapSize - 1);
i1 = Clamp(i1, static_cast<ssize_t>(0), m_MapSize - 1);
j1 = Clamp(j1, static_cast<ssize_t>(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;

View File

@ -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<typename T>
struct type_identity
{
using type = T;
};
template<typename T>
using type_identity_t = typename type_identity<T>::type;
template <typename T>
inline T Clamp(T value, T min, T max)
inline T Clamp(T value, type_identity_t<T> min, type_identity_t<T> max)
{
if (value <= min)
return min;

View File

@ -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<u16>::min(), static_cast<u16>(g_MaxCoord - 1));
occluder.y0 = Clamp(y0, std::numeric_limits<u16>::min(), static_cast<u16>(g_MaxCoord - 1));
occluder.x1 = Clamp(x1, std::numeric_limits<u16>::min(), static_cast<u16>(g_MaxCoord - 1));
occluder.y1 = Clamp(y1, std::numeric_limits<u16>::min(), static_cast<u16>(g_MaxCoord - 1));
occluder.x0 = Clamp(x0, std::numeric_limits<u16>::min(), g_MaxCoord - 1);
occluder.y0 = Clamp(y0, std::numeric_limits<u16>::min(), g_MaxCoord - 1);
occluder.x1 = Clamp(x1, std::numeric_limits<u16>::min(), g_MaxCoord - 1);
occluder.y1 = Clamp(y1, std::numeric_limits<u16>::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<int>(g_HalfMaxCoord * svec.X / svec.W);
u16 y = g_HalfMaxCoord + static_cast<int>(g_HalfMaxCoord * svec.Y / svec.W);
caster.x = Clamp(x, std::numeric_limits<u16>::min(), static_cast<u16>(g_MaxCoord - 1));
caster.y = Clamp(y, std::numeric_limits<u16>::min(), static_cast<u16>(g_MaxCoord - 1));
caster.x = Clamp(x, std::numeric_limits<u16>::min(), g_MaxCoord - 1);
caster.y = Clamp(y, std::numeric_limits<u16>::min(), g_MaxCoord - 1);
caster.z = svec.Z / svec.W;
}

View File

@ -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<int>(m_MapSize)) * Pathfinding::NAVCELLS_PER_TILE;
iend = Clamp(itile1 + 1, 0, static_cast<int>(m_MapSize)) * Pathfinding::NAVCELLS_PER_TILE;
jstart = Clamp(jtile0 - 1, 0, static_cast<int>(m_MapSize)) * Pathfinding::NAVCELLS_PER_TILE;
jend = Clamp(jtile1 + 1, 0, static_cast<int>(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