refactor: clamp

This commit is contained in:
ikpil 2023-09-23 07:38:46 +09:00
parent a46aa0eaee
commit 707d91afea
16 changed files with 69 additions and 77 deletions

View File

@ -30,17 +30,5 @@ namespace DotRecast.Core
{
return f * f;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Clamp(float v, float min, float max)
{
return Math.Max(Math.Min(v, max), min);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Clamp(int v, int min, int max)
{
return Math.Max(Math.Min(v, max), min);
}
}
}

View File

@ -1351,7 +1351,7 @@ namespace DotRecast.Detour.Crowd
private float Tween(float t, float t0, float t1)
{
return RcMath.Clamp((t - t0) / (t1 - t0), 0.0f, 1.0f);
return Math.Clamp((t - t0) / (t1 - t0), 0.0f, 1.0f);
}
}
}

View File

@ -243,7 +243,7 @@ namespace DotRecast.Detour.Crowd
vab = vab.Subtract(cir.vel);
// Side
side += RcMath.Clamp(Math.Min(cir.dp.Dot2D(vab) * 0.5f + 0.5f, cir.np.Dot2D(vab) * 2), 0.0f, 1.0f);
side += Math.Clamp(Math.Min(cir.dp.Dot2D(vab) * 0.5f + 0.5f, cir.np.Dot2D(vab) * 2), 0.0f, 1.0f);
nside++;
if (!SweepCircleCircle(pos, rad, vab, cir.p, cir.rad, out var htmin, out var htmax))
@ -410,8 +410,8 @@ namespace DotRecast.Detour.Crowd
int nrings = m_params.adaptiveRings;
int depth = m_params.adaptiveDepth;
int nd = RcMath.Clamp(ndivs, 1, DT_MAX_PATTERN_DIVS);
int nr = RcMath.Clamp(nrings, 1, DT_MAX_PATTERN_RINGS);
int nd = Math.Clamp(ndivs, 1, DT_MAX_PATTERN_DIVS);
int nr = Math.Clamp(nrings, 1, DT_MAX_PATTERN_RINGS);
float da = (1.0f / nd) * DT_PI * 2;
float ca = (float)Math.Cos(da);
float sa = (float)Math.Sin(da);

View File

@ -68,7 +68,7 @@ namespace DotRecast.Detour.Crowd.Tracking
float penRange = maxPen - minPen;
float s = penRange > 0.001f ? (1.0f / penRange) : 1;
for (int i = 0; i < n; ++i)
arr[i] = RcMath.Clamp((arr[i] - minPen) * s, 0.0f, 1.0f);
arr[i] = Math.Clamp((arr[i] - minPen) * s, 0.0f, 1.0f);
}
public void NormalizeSamples()

View File

@ -17,6 +17,7 @@ freely, subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution.
*/
using System;
using DotRecast.Core;
@ -50,12 +51,12 @@ namespace DotRecast.Detour.Extras
bmax.Max(data.verts, data.polys[i].verts[j] * 3);
}
it.bmin[0] = RcMath.Clamp((int)((bmin.x - data.header.bmin.x) * quantFactor), 0, 0x7fffffff);
it.bmin[1] = RcMath.Clamp((int)((bmin.y - data.header.bmin.y) * quantFactor), 0, 0x7fffffff);
it.bmin[2] = RcMath.Clamp((int)((bmin.z - data.header.bmin.z) * quantFactor), 0, 0x7fffffff);
it.bmax[0] = RcMath.Clamp((int)((bmax.x - data.header.bmin.x) * quantFactor), 0, 0x7fffffff);
it.bmax[1] = RcMath.Clamp((int)((bmax.y - data.header.bmin.y) * quantFactor), 0, 0x7fffffff);
it.bmax[2] = RcMath.Clamp((int)((bmax.z - data.header.bmin.z) * quantFactor), 0, 0x7fffffff);
it.bmin[0] = Math.Clamp((int)((bmin.x - data.header.bmin.x) * quantFactor), 0, 0x7fffffff);
it.bmin[1] = Math.Clamp((int)((bmin.y - data.header.bmin.y) * quantFactor), 0, 0x7fffffff);
it.bmin[2] = Math.Clamp((int)((bmin.z - data.header.bmin.z) * quantFactor), 0, 0x7fffffff);
it.bmax[0] = Math.Clamp((int)((bmax.x - data.header.bmin.x) * quantFactor), 0, 0x7fffffff);
it.bmax[1] = Math.Clamp((int)((bmax.y - data.header.bmin.y) * quantFactor), 0, 0x7fffffff);
it.bmax[2] = Math.Clamp((int)((bmax.z - data.header.bmin.z) * quantFactor), 0, 0x7fffffff);
}
return DtNavMeshBuilder.Subdivide(items, data.header.polyCount, 0, data.header.polyCount, 0, nodes);

View File

@ -346,12 +346,12 @@ namespace DotRecast.Detour
int[] bmin = new int[3];
int[] bmax = new int[3];
// dtClamp query box to world box.
float minx = RcMath.Clamp(qmin.x, tbmin.x, tbmax.x) - tbmin.x;
float miny = RcMath.Clamp(qmin.y, tbmin.y, tbmax.y) - tbmin.y;
float minz = RcMath.Clamp(qmin.z, tbmin.z, tbmax.z) - tbmin.z;
float maxx = RcMath.Clamp(qmax.x, tbmin.x, tbmax.x) - tbmin.x;
float maxy = RcMath.Clamp(qmax.y, tbmin.y, tbmax.y) - tbmin.y;
float maxz = RcMath.Clamp(qmax.z, tbmin.z, tbmax.z) - tbmin.z;
float minx = Math.Clamp(qmin.x, tbmin.x, tbmax.x) - tbmin.x;
float miny = Math.Clamp(qmin.y, tbmin.y, tbmax.y) - tbmin.y;
float minz = Math.Clamp(qmin.z, tbmin.z, tbmax.z) - tbmin.z;
float maxx = Math.Clamp(qmax.x, tbmin.x, tbmax.x) - tbmin.x;
float maxy = Math.Clamp(qmax.y, tbmin.y, tbmax.y) - tbmin.y;
float maxz = Math.Clamp(qmax.z, tbmin.z, tbmax.z) - tbmin.z;
// Quantize
bmin[0] = (int)(qfac * minx) & 0x7ffffffe;
bmin[1] = (int)(qfac * miny) & 0x7ffffffe;
@ -781,8 +781,8 @@ namespace DotRecast.Detour
tmax = temp;
}
link.bmin = (int)Math.Round(RcMath.Clamp(tmin, 0.0f, 1.0f) * 255.0f);
link.bmax = (int)Math.Round(RcMath.Clamp(tmax, 0.0f, 1.0f) * 255.0f);
link.bmin = (int)Math.Round(Math.Clamp(tmin, 0.0f, 1.0f) * 255.0f);
link.bmax = (int)Math.Round(Math.Clamp(tmax, 0.0f, 1.0f) * 255.0f);
}
else if (dir == 2 || dir == 6)
{
@ -797,8 +797,8 @@ namespace DotRecast.Detour
tmax = temp;
}
link.bmin = (int)Math.Round(RcMath.Clamp(tmin, 0.0f, 1.0f) * 255.0f);
link.bmax = (int)Math.Round(RcMath.Clamp(tmax, 0.0f, 1.0f) * 255.0f);
link.bmin = (int)Math.Round(Math.Clamp(tmin, 0.0f, 1.0f) * 255.0f);
link.bmax = (int)Math.Round(Math.Clamp(tmax, 0.0f, 1.0f) * 255.0f);
}
}
}

View File

@ -171,13 +171,13 @@ namespace DotRecast.Detour
}
// BV-tree uses cs for all dimensions
it.bmin[0] = RcMath.Clamp((int)((bmin.x - option.bmin.x) * quantFactor), 0, int.MaxValue);
it.bmin[1] = RcMath.Clamp((int)((bmin.y - option.bmin.y) * quantFactor), 0, int.MaxValue);
it.bmin[2] = RcMath.Clamp((int)((bmin.z - option.bmin.z) * quantFactor), 0, int.MaxValue);
it.bmin[0] = Math.Clamp((int)((bmin.x - option.bmin.x) * quantFactor), 0, int.MaxValue);
it.bmin[1] = Math.Clamp((int)((bmin.y - option.bmin.y) * quantFactor), 0, int.MaxValue);
it.bmin[2] = Math.Clamp((int)((bmin.z - option.bmin.z) * quantFactor), 0, int.MaxValue);
it.bmax[0] = RcMath.Clamp((int)((bmax.x - option.bmin.x) * quantFactor), 0, int.MaxValue);
it.bmax[1] = RcMath.Clamp((int)((bmax.y - option.bmin.y) * quantFactor), 0, int.MaxValue);
it.bmax[2] = RcMath.Clamp((int)((bmax.z - option.bmin.z) * quantFactor), 0, int.MaxValue);
it.bmax[0] = Math.Clamp((int)((bmax.x - option.bmin.x) * quantFactor), 0, int.MaxValue);
it.bmax[1] = Math.Clamp((int)((bmax.y - option.bmin.y) * quantFactor), 0, int.MaxValue);
it.bmax[2] = Math.Clamp((int)((bmax.z - option.bmin.z) * quantFactor), 0, int.MaxValue);
}
else
{

View File

@ -603,12 +603,12 @@ namespace DotRecast.Detour
int[] bmin = new int[3];
int[] bmax = new int[3];
// dtClamp query box to world box.
float minx = RcMath.Clamp(qmin.x, tbmin.x, tbmax.x) - tbmin.x;
float miny = RcMath.Clamp(qmin.y, tbmin.y, tbmax.y) - tbmin.y;
float minz = RcMath.Clamp(qmin.z, tbmin.z, tbmax.z) - tbmin.z;
float maxx = RcMath.Clamp(qmax.x, tbmin.x, tbmax.x) - tbmin.x;
float maxy = RcMath.Clamp(qmax.y, tbmin.y, tbmax.y) - tbmin.y;
float maxz = RcMath.Clamp(qmax.z, tbmin.z, tbmax.z) - tbmin.z;
float minx = Math.Clamp(qmin.x, tbmin.x, tbmax.x) - tbmin.x;
float miny = Math.Clamp(qmin.y, tbmin.y, tbmax.y) - tbmin.y;
float minz = Math.Clamp(qmin.z, tbmin.z, tbmax.z) - tbmin.z;
float maxx = Math.Clamp(qmax.x, tbmin.x, tbmax.x) - tbmin.x;
float maxy = Math.Clamp(qmax.y, tbmin.y, tbmax.y) - tbmin.y;
float maxz = Math.Clamp(qmax.z, tbmin.z, tbmax.z) - tbmin.z;
// Quantize
bmin[0] = (int)(qfac * minx) & 0x7ffffffe;
bmin[1] = (int)(qfac * miny) & 0x7ffffffe;
@ -2125,7 +2125,7 @@ namespace DotRecast.Detour
float t = 0.5f;
if (DtUtils.IntersectSegSeg2D(fromPos, toPos, left, right, out var _, out var t2))
{
t = RcMath.Clamp(t2, 0.1f, 0.9f);
t = Math.Clamp(t2, 0.1f, 0.9f);
}
pt = RcVec3f.Lerp(left, right, t);

View File

@ -433,13 +433,13 @@ public class RecastDemo : IRecastDemoChannel
_modState |= 0 < tempMoveAccel ? (int)KeyModState.Shift : (int)KeyModState.None;
//Logger.Information($"{_modState}");
_moveFront = RcMath.Clamp(_moveFront + tempMoveFront * dt * 4.0f, 0, 2.0f);
_moveLeft = RcMath.Clamp(_moveLeft + tempMoveLeft * dt * 4.0f, 0, 2.0f);
_moveBack = RcMath.Clamp(_moveBack + tempMoveBack * dt * 4.0f, 0, 2.0f);
_moveRight = RcMath.Clamp(_moveRight + tempMoveRight * dt * 4.0f, 0, 2.0f);
_moveUp = RcMath.Clamp(_moveUp + tempMoveUp * dt * 4.0f, 0, 2.0f);
_moveDown = RcMath.Clamp(_moveDown + tempMoveDown * dt * 4.0f, 0, 2.0f);
_moveAccel = RcMath.Clamp(_moveAccel + tempMoveAccel * dt * 4.0f, 0, 2.0f);
_moveFront = Math.Clamp(_moveFront + tempMoveFront * dt * 4.0f, 0, 2.0f);
_moveLeft = Math.Clamp(_moveLeft + tempMoveLeft * dt * 4.0f, 0, 2.0f);
_moveBack = Math.Clamp(_moveBack + tempMoveBack * dt * 4.0f, 0, 2.0f);
_moveRight = Math.Clamp(_moveRight + tempMoveRight * dt * 4.0f, 0, 2.0f);
_moveUp = Math.Clamp(_moveUp + tempMoveUp * dt * 4.0f, 0, 2.0f);
_moveDown = Math.Clamp(_moveDown + tempMoveDown * dt * 4.0f, 0, 2.0f);
_moveAccel = Math.Clamp(_moveAccel + tempMoveAccel * dt * 4.0f, 0, 2.0f);
}
}
@ -490,7 +490,7 @@ public class RecastDemo : IRecastDemoChannel
// Update sample simulation.
float SIM_RATE = 20;
float DELTA_TIME = 1.0f / SIM_RATE;
timeAcc = RcMath.Clamp((float)(timeAcc + dt), -1.0f, 1.0f);
timeAcc = Math.Clamp((float)(timeAcc + dt), -1.0f, 1.0f);
int simIter = 0;
while (timeAcc > DELTA_TIME)
{

View File

@ -1,4 +1,5 @@
using DotRecast.Core;
using System;
using DotRecast.Core;
using DotRecast.Recast.Demo.Draw;
using DotRecast.Recast.Toolset.Gizmos;
@ -50,7 +51,7 @@ public static class GizmoRenderer
normal.y = e0.z * e1.x - e0.x * e1.z;
normal.z = e0.x * e1.y - e0.y * e1.x;
RcVec3f.Normalize(ref normal);
float c = RcMath.Clamp(0.57735026f * (normal.x + normal.y + normal.z), -1, 1);
float c = Math.Clamp(0.57735026f * (normal.x + normal.y + normal.z), -1, 1);
int col = DebugDraw.DuLerpCol(
DebugDraw.DuRGBA(32, 32, 0, 160),
DebugDraw.DuRGBA(220, 220, 0, 160),
@ -139,7 +140,7 @@ public static class GizmoRenderer
for (int j = 0; j < 3; j++)
{
int v = sphere.triangles[i + j] * 3;
float c = RcMath.Clamp(0.57735026f * (sphere.vertices[v] + sphere.vertices[v + 1] + sphere.vertices[v + 2]), -1, 1);
float c = Math.Clamp(0.57735026f * (sphere.vertices[v] + sphere.vertices[v + 1] + sphere.vertices[v + 2]), -1, 1);
int col = DebugDraw.DuLerpCol(DebugDraw.DuRGBA(32, 32, 0, 160), DebugDraw.DuRGBA(220, 220, 0, 160), (int)(127 * (1 + c)));
debugDraw.Vertex(

View File

@ -1,3 +1,4 @@
using System;
using DotRecast.Core;
using static DotRecast.Recast.Toolset.Gizmos.RcGizmoHelper;
@ -48,7 +49,7 @@ namespace DotRecast.Recast.Toolset.Gizmos
v.y = vertices[i + 1] - center[1];
v.z = vertices[i + 2] - center[2];
RcVec3f.Normalize(ref v);
gradient[i / 3] = RcMath.Clamp(0.57735026f * (v.x + v.y + v.z), -1, 1);
gradient[i / 3] = Math.Clamp(0.57735026f * (v.x + v.y + v.z), -1, 1);
}
}

View File

@ -1,3 +1,4 @@
using System;
using DotRecast.Core;
using static DotRecast.Recast.Toolset.Gizmos.RcGizmoHelper;
@ -53,7 +54,7 @@ namespace DotRecast.Recast.Toolset.Gizmos
v.y = vertices[i + 1] - center.y;
v.z = vertices[i + 2] - center.z;
RcVec3f.Normalize(ref v);
gradient[i / 3] = RcMath.Clamp(0.57735026f * (v.x + v.y + v.z), -1, 1);
gradient[i / 3] = Math.Clamp(0.57735026f * (v.x + v.y + v.z), -1, 1);
}
}
}

View File

@ -93,8 +93,8 @@ namespace DotRecast.Recast
{
int bot = s.smax;
int top = s.next != null ? (int)s.next.smin : MAX_HEIGHT;
chf.spans[idx].y = RcMath.Clamp(bot, 0, MAX_HEIGHT);
chf.spans[idx].h = RcMath.Clamp(top - bot, 0, MAX_HEIGHT);
chf.spans[idx].y = Math.Clamp(bot, 0, MAX_HEIGHT);
chf.spans[idx].h = Math.Clamp(top - bot, 0, MAX_HEIGHT);
chf.areas[idx] = s.area;
idx++;
tmpCount++;

View File

@ -222,8 +222,8 @@ namespace DotRecast.Recast
int smax = (int)Math.Ceiling((h[1] - hf.bmin.y) * ich);
if (smin != smax)
{
int ismin = RcMath.Clamp(smin, 0, SPAN_MAX_HEIGHT);
int ismax = RcMath.Clamp(smax, ismin + 1, SPAN_MAX_HEIGHT);
int ismin = Math.Clamp(smin, 0, SPAN_MAX_HEIGHT);
int ismax = Math.Clamp(smax, ismin + 1, SPAN_MAX_HEIGHT);
RcRasterizations.AddSpan(hf, x, z, ismin, ismax, area, flagMergeThr);
}
}
@ -282,12 +282,12 @@ namespace DotRecast.Recast
{
float[] s = MergeIntersections(
RayCylinderIntersection(RcVec3f.Of(
RcMath.Clamp(start.x, rectangle[0], rectangle[2]), rectangle[4],
RcMath.Clamp(start.z, rectangle[1], rectangle[3])
Math.Clamp(start.x, rectangle[0], rectangle[2]), rectangle[4],
Math.Clamp(start.z, rectangle[1], rectangle[3])
), start, axis, radiusSqr),
RayCylinderIntersection(RcVec3f.Of(
RcMath.Clamp(end.x, rectangle[0], rectangle[2]), rectangle[4],
RcMath.Clamp(end.z, rectangle[1], rectangle[3])
Math.Clamp(end.x, rectangle[0], rectangle[2]), rectangle[4],
Math.Clamp(end.z, rectangle[1], rectangle[3])
), start, axis, radiusSqr));
float axisLen2dSqr = axis.x * axis.x + axis.z * axis.z;
if (axisLen2dSqr > EPSILON)
@ -398,7 +398,7 @@ namespace DotRecast.Recast
{
// 2d intersection of plane and segment
float t = (x - start.x) / direction.x;
float z = RcMath.Clamp(start.z + t * direction.z, rectangle[1], rectangle[3]);
float z = Math.Clamp(start.z + t * direction.z, rectangle[1], rectangle[3]);
return RcVec3f.Of(x, rectangle[4], z);
}
@ -411,7 +411,7 @@ namespace DotRecast.Recast
{
// 2d intersection of plane and segment
float t = (z - start.z) / direction.z;
float x = RcMath.Clamp(start.x + t * direction.x, rectangle[0], rectangle[2]);
float x = Math.Clamp(start.x + t * direction.x, rectangle[0], rectangle[2]);
return RcVec3f.Of(x, rectangle[4], z);
}

View File

@ -352,8 +352,8 @@ namespace DotRecast.Recast
{
int ix = (int)Math.Floor(fx * ics + 0.01f);
int iz = (int)Math.Floor(fz * ics + 0.01f);
ix = RcMath.Clamp(ix - hp.xmin, 0, hp.width - 1);
iz = RcMath.Clamp(iz - hp.ymin, 0, hp.height - 1);
ix = Math.Clamp(ix - hp.xmin, 0, hp.width - 1);
iz = Math.Clamp(iz - hp.ymin, 0, hp.height - 1);
int h = hp.data[ix + iz * hp.width];
if (h == RC_UNSET_HEIGHT)
{

View File

@ -258,8 +258,8 @@ namespace DotRecast.Recast
int w = heightfield.width;
int h = heightfield.height;
// use -1 rather than 0 to cut the polygon properly at the start of the tile
z0 = RcMath.Clamp(z0, -1, h - 1);
z1 = RcMath.Clamp(z1, 0, h - 1);
z0 = Math.Clamp(z0, -1, h - 1);
z1 = Math.Clamp(z1, 0, h - 1);
// Clip the triangle into all grid cells it touches.
float[] buf = new float[7 * 3 * 4];
@ -304,8 +304,8 @@ namespace DotRecast.Recast
continue;
}
x0 = RcMath.Clamp(x0, -1, w - 1);
x1 = RcMath.Clamp(x1, 0, w - 1);
x0 = Math.Clamp(x0, -1, w - 1);
x1 = Math.Clamp(x1, 0, w - 1);
int nv, nv2 = nvRow;
for (int x = x0; x <= x1; ++x)
@ -346,8 +346,8 @@ namespace DotRecast.Recast
spanMax = by;
// Snap the span to the heightfield height grid.
int spanMinCellIndex = RcMath.Clamp((int)Math.Floor(spanMin * inverseCellHeight), 0, SPAN_MAX_HEIGHT);
int spanMaxCellIndex = RcMath.Clamp((int)Math.Ceiling(spanMax * inverseCellHeight), spanMinCellIndex + 1, SPAN_MAX_HEIGHT);
int spanMinCellIndex = Math.Clamp((int)Math.Floor(spanMin * inverseCellHeight), 0, SPAN_MAX_HEIGHT);
int spanMaxCellIndex = Math.Clamp((int)Math.Ceiling(spanMax * inverseCellHeight), spanMinCellIndex + 1, SPAN_MAX_HEIGHT);
AddSpan(heightfield, x, z, spanMinCellIndex, spanMaxCellIndex, area, flagMergeThreshold);
}