Skip to content

Commit

Permalink
Removed NLog dependency. Throwing exceptions instead in case of errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcserep committed Oct 23, 2022
1 parent 46144ab commit b1b19d9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 63 deletions.
25 changes: 9 additions & 16 deletions Octree/BoundsOctree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// Copyright (c) 2017, Máté Cserép, http://codenet.hu
// All rights reserved.
// </copyright>

namespace Octree
{
using NLog;
using System;
using System.Collections.Generic;
using System.Numerics;

Expand Down Expand Up @@ -37,11 +38,6 @@ namespace Octree
/// <typeparam name="T">The content of the octree can be anything, since the bounds data is supplied separately.</typeparam>
public partial class BoundsOctree<T>
{
/// <summary>
/// The logger
/// </summary>
private static readonly Logger Logger = LogManager.GetLogger("octree");

/// <summary>
/// Root node of the octree
/// </summary>
Expand Down Expand Up @@ -97,15 +93,13 @@ public BoundingBox[] GetChildBounds()
/// <param name="initialWorldPos">Position of the center of the initial node.</param>
/// <param name="minNodeSize">Nodes will stop splitting if the new nodes would be smaller than this (metres).</param>
/// <param name="loosenessVal">Clamped between 1 and 2. Values > 1 let nodes overlap.</param>
/// <exception cref="ArgumentException">Minimum node size must be at least as big as the initial world size.</exception>
public BoundsOctree(float initialWorldSize, Vector3 initialWorldPos, float minNodeSize, float loosenessVal)
{
if (minNodeSize > initialWorldSize)
{
Logger.Warn(
"Minimum node size must be at least as big as the initial world size. Was: " + minNodeSize
+ " Adjusted to: " + initialWorldSize);
minNodeSize = initialWorldSize;
}
throw new ArgumentException("Minimum node size must be at least as big as the initial world size.",
nameof(minNodeSize));

Count = 0;
_initialSize = initialWorldSize;
_minSize = minNodeSize;
Expand All @@ -120,6 +114,7 @@ public BoundsOctree(float initialWorldSize, Vector3 initialWorldPos, float minNo
/// </summary>
/// <param name="obj">Object to add.</param>
/// <param name="objBounds">3D bounding box around the object.</param>
/// <exception cref="InvalidOperationException">Add operation required growing the octree too much.</exception>
public void Add(T obj, BoundingBox objBounds)
{
// Add object or expand the octree until it can be added
Expand All @@ -129,10 +124,8 @@ public void Add(T obj, BoundingBox objBounds)
Grow(objBounds.Center - _rootNode.Center);
if (++count > 20)
{
Logger.Error(
"Aborted Add operation as it seemed to be going on forever (" + (count - 1)
+ ") attempts at growing the octree.");
return;
throw new InvalidOperationException($"Aborted Add operation as it seemed to be going on forever " +
$"({count - 1} attempts at growing the octree).");
}
}
Count++;
Expand Down
19 changes: 5 additions & 14 deletions Octree/BoundsOctreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// Copyright (c) 2017, Máté Cserép, http://codenet.hu
// All rights reserved.
// </copyright>

namespace Octree
{
using NLog;
using System;
using System.Collections.Generic;
using System.Numerics;

Expand All @@ -18,11 +19,6 @@ public partial class BoundsOctree<T>
/// </summary>
private class Node
{
/// <summary>
/// The logger
/// </summary>
private static readonly Logger Logger = LogManager.GetLogger("octree");

/// <summary>
/// Centre of this node
/// </summary>
Expand Down Expand Up @@ -360,10 +356,8 @@ public void GetColliding(ref Ray checkRay, List<T> result, float maxDistance = f
public void SetChildren(Node[] childOctrees)
{
if (childOctrees.Length != 8)
{
Logger.Error("Child octree array must be length 8. Was length: " + childOctrees.Length);
return;
}
throw new ArgumentException("Child octree array must be length 8. Was length: " + childOctrees.Length,
nameof(childOctrees));

_children = childOctrees;
}
Expand Down Expand Up @@ -550,10 +544,7 @@ private void SubAdd(T obj, BoundingBox objBounds)
{
Split();
if (_children == null)
{
Logger.Error("Child creation failed for an unknown reason. Early exit.");
return;
}
throw new InvalidOperationException("Child creation failed for an unknown reason. Early exit.");

// Now that we have the new children, see if this node's existing objects would fit there
for (int i = _objects.Count - 1; i >= 0; i--)
Expand Down
6 changes: 1 addition & 5 deletions Octree/Octree.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageReadmeFile>NuGetReadme.md</PackageReadmeFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand All @@ -36,8 +36,4 @@
<Folder Include="Data\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NLog" Version="4.7.2" />
</ItemGroup>

</Project>
25 changes: 9 additions & 16 deletions Octree/PointOctree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// Copyright (c) 2017, Máté Cserép, http://codenet.hu
// All rights reserved.
// </copyright>

namespace Octree
{
using NLog;
using System;
using System.Collections.Generic;
using System.Numerics;

Expand All @@ -29,11 +30,6 @@ namespace Octree
/// <typeparam name="T">The content of the octree can be anything, since the bounds data is supplied separately.</typeparam>
public partial class PointOctree<T>
{
/// <summary>
/// The logger
/// </summary>
private static readonly Logger Logger = LogManager.GetLogger("octree");

/// <summary>
/// Root node of the octree
/// </summary>
Expand Down Expand Up @@ -80,15 +76,13 @@ public BoundingBox[] GetChildBounds()
/// <param name="initialWorldSize">Size of the sides of the initial node. The octree will never shrink smaller than this.</param>
/// <param name="initialWorldPos">Position of the centre of the initial node.</param>
/// <param name="minNodeSize">Nodes will stop splitting if the new nodes would be smaller than this.</param>
/// <exception cref="ArgumentException">Minimum node size must be at least as big as the initial world size.</exception>
public PointOctree(float initialWorldSize, Vector3 initialWorldPos, float minNodeSize)
{
if (minNodeSize > initialWorldSize)
{
Logger.Warn(
"Minimum node size must be at least as big as the initial world size. Was: " + minNodeSize
+ " Adjusted to: " + initialWorldSize);
minNodeSize = initialWorldSize;
}
throw new ArgumentException("Minimum node size must be at least as big as the initial world size.",
nameof(minNodeSize));

Count = 0;
_initialSize = initialWorldSize;
_minSize = minNodeSize;
Expand All @@ -102,6 +96,7 @@ public PointOctree(float initialWorldSize, Vector3 initialWorldPos, float minNod
/// </summary>
/// <param name="obj">Object to add.</param>
/// <param name="objPos">Position of the object.</param>
/// <exception cref="InvalidOperationException">Add operation required growing the octree too much.</exception>
public void Add(T obj, Vector3 objPos)
{
// Add object or expand the octree until it can be added
Expand All @@ -111,10 +106,8 @@ public void Add(T obj, Vector3 objPos)
Grow(objPos - _rootNode.Center);
if (++count > 20)
{
Logger.Error(
"Aborted Add operation as it seemed to be going on forever (" + (count - 1)
+ ") attempts at growing the octree.");
return;
throw new InvalidOperationException($"Aborted Add operation as it seemed to be going on forever " +
$"({count - 1} attempts at growing the octree).");
}
}
Count++;
Expand Down
17 changes: 5 additions & 12 deletions Octree/PointOctreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// Copyright (c) 2017, Máté Cserép, http://codenet.hu
// All rights reserved.
// </copyright>

namespace Octree
{
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
Expand All @@ -19,11 +20,6 @@ public partial class PointOctree<T>
/// </summary>
private class Node
{
/// <summary>
/// The logger
/// </summary>
private static readonly Logger Logger = LogManager.GetLogger("octree");

/// <summary>
/// Center of this node
/// </summary>
Expand Down Expand Up @@ -309,8 +305,8 @@ public void SetChildren(Node[] childOctrees)
{
if (childOctrees.Length != 8)
{
Logger.Error("Child octree array must be length 8. Was length: " + childOctrees.Length);
return;
throw new ArgumentException("Child octree array must be length 8. Was length: " + childOctrees.Length,
nameof(childOctrees));
}

_children = childOctrees;
Expand Down Expand Up @@ -489,10 +485,7 @@ private void SubAdd(T obj, Vector3 objPos)
{
Split();
if (_children == null)
{
Logger.Error("Child creation failed for an unknown reason. Early exit.");
return;
}
throw new InvalidOperationException("Child creation failed for an unknown reason. Early exit.");

// Now that we have the new children, move this node's existing objects into them
for (int i = _objects.Count - 1; i >= 0; i--)
Expand Down

0 comments on commit b1b19d9

Please sign in to comment.