Skip to content

Commit

Permalink
#563 Minor code improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
koschke authored and falko17 committed Sep 18, 2023
1 parent da8a1e1 commit b45dc59
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 91 deletions.
39 changes: 27 additions & 12 deletions Assets/SEE/Game/City/IncrementalTreeMapSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class IncrementalTreeMapSetting : ConfigIO.PersistentConfigItem
[Range(0, 5)]
[Tooltip("The maximal depth for local moves algorithm. Increase for higher visual quality, " +
"decrease for higher stability and to save runtime")]
public int localMovesDepth = 3;
public int LocalMovesDepth = 3;

/// <summary>
/// The maximal branching factor of the local moves search.
Expand All @@ -29,7 +29,7 @@ public class IncrementalTreeMapSetting : ConfigIO.PersistentConfigItem
[Range(1, 10)]
[Tooltip("The maximal branching factor for local moves algorithm. Increase for higher visual quality, " +
"decrease for higher stability and to save runtime")]
public int localMovesBranchingLimit = 4;
public int LocalMovesBranchingLimit = 4;

/// <summary>
/// Defines the specific p norm used in the local moves algorithm. See here:
Expand Down Expand Up @@ -57,7 +57,7 @@ public class IncrementalTreeMapSetting : ConfigIO.PersistentConfigItem
[Range(0.1f, 100f)]
[LabelText("Padding (mm)")]
[Tooltip("The distance between two neighbour nodes in mm")]
public float paddingMm = 5f;
public float PaddingMm = 5f;

/// <summary>
/// The maximal error for the method
Expand All @@ -67,7 +67,7 @@ public class IncrementalTreeMapSetting : ConfigIO.PersistentConfigItem
[Range(-7, -2)]
[LabelText("Gradient Descent Precision (10^n)")]
[Tooltip("The maximal error for the gradient descent method as power of 10")]
public int gradientDescentPrecisionExponent = -4;
public int GradientDescentPrecisionExponent = -4;

/// <summary>
/// Maps <see cref="pNorm"/> to a double.
Expand All @@ -85,11 +85,11 @@ public class IncrementalTreeMapSetting : ConfigIO.PersistentConfigItem
public void Save(ConfigWriter writer, string label)
{
writer.BeginGroup(label);
writer.Save(localMovesDepth, LocalMovesDepthLabel);
writer.Save(localMovesBranchingLimit, LocalMovesBranchingLimitLabel);
writer.Save(LocalMovesDepth, LocalMovesDepthLabel);
writer.Save(LocalMovesBranchingLimit, LocalMovesBranchingLimitLabel);
writer.Save(pNorm.ToString(), PNormLabel);
writer.Save(gradientDescentPrecisionExponent, GradientDescentPrecisionLabel);
writer.Save(paddingMm, PaddingLabel);
writer.Save(GradientDescentPrecisionExponent, GradientDescentPrecisionLabel);
writer.Save(PaddingMm, PaddingLabel);
writer.EndGroup();
}

Expand All @@ -100,18 +100,33 @@ public bool Restore(Dictionary<string, object> attributes, string label)
return false;
}
Dictionary<string, object> values = dictionary as Dictionary<string, object>;
bool result = ConfigIO.Restore(values, LocalMovesDepthLabel, ref localMovesDepth);
result |= ConfigIO.Restore(values, LocalMovesBranchingLimitLabel, ref localMovesBranchingLimit);
bool result = ConfigIO.Restore(values, LocalMovesDepthLabel, ref LocalMovesDepth);
result |= ConfigIO.Restore(values, LocalMovesBranchingLimitLabel, ref LocalMovesBranchingLimit);
result |= ConfigIO.RestoreEnum(values, PNormLabel, ref pNorm);
result |= ConfigIO.Restore(values, GradientDescentPrecisionLabel, ref gradientDescentPrecisionExponent);
result |= ConfigIO.Restore(values, PaddingLabel, ref paddingMm);
result |= ConfigIO.Restore(values, GradientDescentPrecisionLabel, ref GradientDescentPrecisionExponent);
result |= ConfigIO.Restore(values, PaddingLabel, ref PaddingMm);
return result;
}

/// <summary>
/// Configuration label for <see cref="LocalMovesDepth"/>.
/// </summary>
private const string LocalMovesDepthLabel = "LocalMovesDepth";
/// <summary>
/// Configuration label for <see cref="LocalMovesBranchingLimit"/>.
/// </summary>
private const string LocalMovesBranchingLimitLabel = "LocalMovesBranchingLimit";
/// <summary>
/// Configuration label for <see cref="PNorm"/>.
/// </summary>
private const string PNormLabel = "PNorm";
/// <summary>
/// Configuration label for <see cref="GradientDescentPrecisionExponent"/>.
/// </summary>
private const string GradientDescentPrecisionLabel = "GradientDescentPrecision";
/// <summary>
/// Configuration label for <see cref="PaddingMm"/>.
/// </summary>
private const string PaddingLabel = "Padding";
}

Expand Down
22 changes: 15 additions & 7 deletions Assets/SEE/Game/City/NodeLayoutAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class NodeLayoutAttributes : LayoutSettings
/// <summary>
/// Settings for the <see cref="SEE.Layout.NodeLayouts.IncrementalTreeMapLayout"/>.
/// </summary>
public IncrementalTreeMapSetting incrementalTreeMapSetting = new();
public IncrementalTreeMapSetting IncrementalTreeMapSetting = new();

/// <summary>
/// The path for the layout file containing the node layout information.
Expand All @@ -29,16 +29,14 @@ public class NodeLayoutAttributes : LayoutSettings
/// data of a game object.
/// </summary>
[OdinSerialize]
public FilePath LayoutPath = new FilePath();

private const string LayoutPathLabel = "LayoutPath";

public FilePath LayoutPath = new();

public override void Save(ConfigWriter writer, string label)
{
writer.BeginGroup(label);
writer.Save(Kind.ToString(), NodeLayoutLabel);
LayoutPath.Save(writer, LayoutPathLabel);
incrementalTreeMapSetting.Save(writer,IncrementalTreeMapLabel);
IncrementalTreeMapSetting.Save(writer, IncrementalTreeMapLabel);
writer.EndGroup();
}

Expand All @@ -50,11 +48,21 @@ public override void Restore(Dictionary<string, object> attributes, string label

ConfigIO.RestoreEnum(values, NodeLayoutLabel, ref Kind);
LayoutPath.Restore(values, LayoutPathLabel);
incrementalTreeMapSetting.Restore(values, IncrementalTreeMapLabel);
IncrementalTreeMapSetting.Restore(values, IncrementalTreeMapLabel);
}
}

/// <summary>
/// Configuration label for <see cref="LayoutPath"/>.
/// </summary>
private const string LayoutPathLabel = "LayoutPath";
/// <summary>
/// Configuration label for <see cref="IncrementalTreeMapSetting"/>.
/// </summary>
private const string IncrementalTreeMapLabel = "IncrementalTreeMap";
/// <summary>
/// Configuration label for <see cref="Kind"/>.
/// </summary>
private const string NodeLayoutLabel = "NodeLayout";
}
}
2 changes: 1 addition & 1 deletion Assets/SEE/Game/Evolution/EvolutionRendererLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void CalculateLayout(Graph graph)
// Since incremental layouts must know the layout of the last revision
// but are also bound to the function calls of NodeLayout
// we must hand over this argument here separately
if(nodeLayout is IIncrementalNodeLayout iNodeLayout && oldLayout is IIncrementalNodeLayout iOldLayout)
if (nodeLayout is IIncrementalNodeLayout iNodeLayout && oldLayout is IIncrementalNodeLayout iOldLayout)
{
iNodeLayout.OldLayout = iOldLayout;
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/SEE/Game/GraphRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ public NodeLayout GetLayout(GameObject parent) =>
GroundLevel,
parent.transform.lossyScale.x,
parent.transform.lossyScale.z,
Settings.NodeLayoutSettings.incrementalTreeMapSetting),
Settings.NodeLayoutSettings.IncrementalTreeMapSetting),
NodeLayoutKind.Balloon => new BalloonNodeLayout(GroundLevel),
NodeLayoutKind.CirclePacking => new CirclePackingNodeLayout(GroundLevel),
NodeLayoutKind.CompoundSpringEmbedder => new CoseLayout(GroundLevel, Settings),
Expand Down
2 changes: 1 addition & 1 deletion Assets/SEE/Layout/NodeLayouts/Cose/CoseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static NodeLayout GetNodelayout(NodeLayoutKind nodeLayout, float groundLe
groundLevel,
1000.0f,
1000.0f,
settings.NodeLayoutSettings.incrementalTreeMapSetting);
settings.NodeLayoutSettings.IncrementalTreeMapSetting);
case NodeLayoutKind.Balloon:
return new BalloonNodeLayout(groundLevel);
case NodeLayoutKind.CirclePacking:
Expand Down
3 changes: 1 addition & 2 deletions Assets/SEE/Layout/NodeLayouts/IIncrementalNodeLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Defines the interface for incremental node layouts.
///
/// Incremental node layouts are designed for evolution setting
/// Incremental node layouts are designed for the animation of evolution
/// and each layout depends on the layout of the last revision.
///
/// This interface extends a layout by <see cref="OldLayout"/>
Expand All @@ -15,6 +15,5 @@ public interface IIncrementalNodeLayout
/// Setter for the layout of the last revision.
/// </summary>
IIncrementalNodeLayout OldLayout { set; }

}
}
35 changes: 18 additions & 17 deletions Assets/SEE/Layout/NodeLayouts/IncrementalTreeMap/CorrectArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static bool Correct(IList<Node> nodes, IncrementalTreeMapSetting settings

// adjust the position of slicingSegments
AdjustSliced(nodes, partition1, partition2, slicingSegment);
// recursively adjust the two sub layouts
// recursively adjust the two sublayouts
// since both sublayouts are temporally independent from each other
// the segment that separates these must be considered as a border (IsConst = true)
slicingSegment.IsConst = true;
Expand All @@ -49,7 +49,7 @@ public static bool Correct(IList<Node> nodes, IncrementalTreeMapSetting settings
Vector<double>.Build.DenseOfArray(nodes.Select(node => node.Rectangle.Area()).ToArray());
double error = (nodesSizesWanted - nodesSizesCurrent).Norm(p: 1);

return error <= Math.Pow(10, settings.gradientDescentPrecisionExponent) && CheckPositiveLength(nodes);
return error <= Math.Pow(10, settings.GradientDescentPrecisionExponent) && CheckPositiveLength(nodes);
}
else
{
Expand All @@ -58,11 +58,11 @@ public static bool Correct(IList<Node> nodes, IncrementalTreeMapSetting settings
}

/// <summary>
/// Checks if the layout of <paramref name="nodes"/> can be divided into two disjoint sub layouts.
/// Checks if the layout of <paramref name="nodes"/> can be divided into two disjoint sublayouts.
/// </summary>
/// <param name="nodes">nodes with layout</param>
/// <param name="slicingSegment">a segment that would separate the sub layouts</param>
/// <returns>true if nodes are sliceable else false</returns>
/// <param name="slicingSegment">a segment that would separate the sublayouts</param>
/// <returns>true if nodes are sliceable, else false</returns>
private static bool IsSliceAble(IList<Node> nodes, out Segment slicingSegment)
{
slicingSegment = null;
Expand Down Expand Up @@ -106,8 +106,8 @@ private static bool IsSliceAble(IList<Node> nodes, out Segment slicingSegment)
/// </summary>
/// <param name="nodes">nodes with layout</param>
/// <param name="slicingSegment"> the segment that divides both layouts</param>
/// <param name="partition1">the <see cref="Direction.Lower"/>/<see cref="Direction.Left"/> sub layout</param>
/// <param name="partition2">the <see cref="Direction.Upper"/>/<see cref="Direction.Right"/> sub layout</param>
/// <param name="partition1">the <see cref="Direction.Lower"/>/<see cref="Direction.Left"/> sublayout</param>
/// <param name="partition2">the <see cref="Direction.Upper"/>/<see cref="Direction.Right"/> sublayout</param>
private static void Split(IList<Node> nodes, Segment slicingSegment,
out IList<Node> partition1, out IList<Node> partition2)
{
Expand Down Expand Up @@ -146,15 +146,15 @@ private static void Split(IList<Node> nodes, Segment slicingSegment,
}

/// <summary>
/// This method recalibrates a layout that is sliced in 2 sublayouts,
/// This method recalibrates a layout that is sliced in two sublayouts,
/// so the sublayouts get the size they should have.
/// A sub layout can still have internal wrong node sizes.
/// A sublayout can still have internal wrong node sizes.
/// </summary>
/// <param name="nodes">nodes of a slice able layout </param>
/// <param name="nodes">nodes of a sliceable layout </param>
/// <param name="partition1">partition of <paramref name="nodes"/>,
/// the <see cref="Direction.Lower"/>/<see cref="Direction.Left"/> sub layout</param>
/// the <see cref="Direction.Lower"/>/<see cref="Direction.Left"/> sublayout</param>
/// <param name="partition2">partition of <paramref name="nodes"/>,
/// the <see cref="Direction.Upper"/>/<see cref="Direction.Right"/> sub layout</param>
/// the <see cref="Direction.Upper"/>/<see cref="Direction.Right"/> sublayout</param>
/// <param name="slicingSegment">the segment that slices the layout</param>
private static void AdjustSliced(
IList<Node> nodes,
Expand Down Expand Up @@ -222,8 +222,9 @@ Dictionary<Segment, int> mapSegmentIndex
= segments.ToDictionary(s => s, _ => i++);

double distance = 0;
double maximalError = Math.Pow(10, settings.gradientDescentPrecisionExponent);
for (int j = 0; j < 50; j++)
double maximalError = Math.Pow(10, settings.GradientDescentPrecisionExponent);
const int NumberOfIterations = 50;
for (int j = 0; j < NumberOfIterations; j++)
{
distance = CalculateOneStep(nodes, mapSegmentIndex);
if (distance <= maximalError)
Expand All @@ -232,11 +233,11 @@ Dictionary<Segment, int> mapSegmentIndex
}
}

return (CheckPositiveLength(nodes) && distance < maximalError);
return CheckPositiveLength(nodes) && distance < maximalError;
}

/// <summary>
/// Calculates the jacobian matrix, a derivative for the function that maps
/// Calculates the Jacobian matrix, a derivative for the function that maps
/// the position of the segments to the area of each node (its rectangle).
/// </summary>
/// <param name="nodes">the nodes of the layout</param>
Expand Down Expand Up @@ -344,7 +345,7 @@ private static void ApplyShift(
}

/// <summary>
/// Checks that the result has no rectangles with non-positive width or depth
/// Verifies that the result has no rectangles with non-positive width or depth.
/// </summary>
/// <param name="nodes">nodes of the layout</param>
/// <returns>true if all rectangles have positive lengths, else false</returns>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace SEE.Layout.NodeLayouts.IncrementalTreeMap
{
/// <summary>
/// Direction in in the plane containing the x-axis and z-axis
/// Direction in the plane containing the x-axis and z-axis
/// </summary>
internal enum Direction
{
Expand Down
6 changes: 3 additions & 3 deletions Assets/SEE/Layout/NodeLayouts/IncrementalTreeMap/Dissect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace SEE.Layout.NodeLayouts.IncrementalTreeMap
internal static class Dissect
{
/// <summary>
/// Calculates new Layout for <paramref name="nodes"/>.
/// Calculates new layout for <paramref name="nodes"/>.
/// Assigns rectangles and segments to each node in <paramref name="nodes"/>.
/// </summary>
/// <param name="nodes">nodes to be laid out</param>
/// <param name="rectangle">rectangle in that the nodes should be placed</param>
/// <param name="rectangle">rectangle in which the nodes should be placed</param>
public static void Apply(IEnumerable<Node> nodes, Rectangle rectangle)
{
Node[] nodesArray = nodes.ToArray();
Expand Down Expand Up @@ -115,7 +115,7 @@ private static void Apply(Rectangle rectangle,
/// Calculates the index that separates the <paramref name="nodes"/> array into two
/// partitions. The specific split should result in good visual quality.
/// </summary>
/// <param name="nodes"> sorted list</param>
/// <param name="nodes">sorted list</param>
/// <returns>index</returns>
private static int GetSplitIndex(Node[] nodes)
{
Expand Down
14 changes: 7 additions & 7 deletions Assets/SEE/Layout/NodeLayouts/IncrementalTreeMap/FlipMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ namespace SEE.Layout.NodeLayouts.IncrementalTreeMap
{
/// <summary>
/// A flip move is a kind of <see cref="LocalMove"/>.
/// It rotates two nodes 90 degrees.
/// It rotates two nodes by 90 degrees.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
internal class FlipMove : LocalMove
{
/// <summary>
/// if the rotation is clockwise
/// Whether the rotation is clockwise.
/// </summary>
private readonly bool clockwise;

/// <summary>
/// Constructor
/// Constructor.
/// </summary>
/// <param name="node1">one affected node</param>
/// <param name="node2">other affected node</param>
/// <param name="clockwise">if the rotation is clockwise </param>
/// <param name="clockwise">whether the rotation is clockwise</param>
public FlipMove(Node node1, Node node2, bool clockwise)
{
this.Node1 = node1;
Expand Down Expand Up @@ -58,7 +58,7 @@ override public void Apply()
}
else
{
throw new ArgumentException("Can't apply flip move");
throw new ArgumentException("Can't apply flip move.");
}
}

Expand All @@ -68,7 +68,7 @@ override public LocalMove Clone(IDictionary<string, Node> cloneMap)
}

/// <summary>
/// Applies the local move for the case that the nodes are separated vertically
/// Applies the local move for the case that the nodes are separated vertically.
/// </summary>
/// <param name="leftNode">the node on the <see cref="Direction.Left"/> side</param>
/// <param name="rightNode">the node on the <see cref="Direction.Right"/> side</param>
Expand Down Expand Up @@ -170,7 +170,7 @@ private void FlipOnHorizontalSegment(Node lowerNode, Node upperNode)
}

/// <summary>
/// Method for better overview in debugger
/// Method for better overview in debugger.
/// </summary>
private string DebuggerDisplay => "flip " + Node1.ID + " " + Node2.ID + " {clockwise}";
}
Expand Down
Loading

0 comments on commit b45dc59

Please sign in to comment.