bugfix: creating convex volume with two points(#34)

- Issue reporter @adambennett55
- https://github.com/ikpil/DotRecast/issues/34
This commit is contained in:
ikpil 2024-01-12 01:46:00 +09:00 committed by Ikpil
parent 9de4b1ee40
commit 4c8c596450
2 changed files with 44 additions and 17 deletions

View File

@ -160,15 +160,11 @@ public class ConvexVolumeSampleTool : ISampleTool
var geom = _sample.GetInputGeom(); var geom = _sample.GetInputGeom();
if (shift) if (shift)
{ {
_tool.RemoveByPos(geom, p); _tool.TryRemove(geom, p, out var volume);
} }
else else
{ {
if (_tool.PlottingShape(p, out var pts, out var hull)) _tool.TryAdd(geom, p, _areaType, _boxDescent, _boxHeight, _polyOffset, out var volume);
{
var vol = RcConvexVolumeTool.CreateConvexVolume(pts, hull, _areaType, _boxDescent, _boxHeight, _polyOffset);
_tool.Add(geom, vol);
}
} }
} }

View File

@ -74,7 +74,7 @@ namespace DotRecast.Recast.Toolset.Tools
} }
public RcConvexVolume RemoveByPos(IInputGeomProvider geom, RcVec3f pos) public bool TryRemove(IInputGeomProvider geom, RcVec3f pos, out RcConvexVolume volume)
{ {
// Delete // Delete
int nearestIndex = -1; int nearestIndex = -1;
@ -90,26 +90,57 @@ namespace DotRecast.Recast.Toolset.Tools
// If end point close enough, delete it. // If end point close enough, delete it.
if (nearestIndex == -1) if (nearestIndex == -1)
return null; {
volume = null;
return false;
}
var removal = geom.ConvexVolumes()[nearestIndex]; var removal = geom.ConvexVolumes()[nearestIndex];
geom.ConvexVolumes().RemoveAt(nearestIndex); geom.ConvexVolumes().RemoveAt(nearestIndex);
return removal; volume = removal;
return null != volume;
} }
public void Add(IInputGeomProvider geom, RcConvexVolume volume) public bool TryAdd(IInputGeomProvider geom, RcVec3f p, RcAreaModification areaType, float boxDescent, float boxHeight, float polyOffset, out RcConvexVolume volume)
{ {
geom.AddConvexVolume(volume); // Create
// If clicked on that last pt, create the shape.
if (_pts.Count > 0 && RcVec3f.DistanceSquared(p, _pts[^1]) < 0.2f * 0.2f)
{
//
if (_hull.Count > 2)
{
volume = CreateConvexVolume(_pts, _hull, areaType, boxDescent, boxHeight, polyOffset);
geom.AddConvexVolume(volume);
}
_pts.Clear();
_hull.Clear();
}
else
{
// Add new point
_pts.Add(p);
// Update hull.
if (_pts.Count > 1)
{
_hull.Clear();
_hull.AddRange(RcConvexUtils.Convexhull(_pts));
}
else
{
_hull.Clear();
}
}
volume = null;
return false;
} }
public static RcConvexVolume CreateConvexVolume(List<RcVec3f> pts, List<int> hull, RcAreaModification areaType, float boxDescent, float boxHeight, float polyOffset) public static RcConvexVolume CreateConvexVolume(List<RcVec3f> pts, List<int> hull, RcAreaModification areaType, float boxDescent, float boxHeight, float polyOffset)
{ {
//
if (hull.Count <= 2)
{
return null;
}
// Create shape. // Create shape.
float[] verts = new float[hull.Count * 3]; float[] verts = new float[hull.Count * 3];
for (int i = 0; i < hull.Count; ++i) for (int i = 0; i < hull.Count; ++i)