StarSim

StarSim.git
git clone git://git.lenczewski.org/StarSim.git
Log | Files | Refs | README | LICENSE

commit bbd69cc229eba112300e33359d91f9cd01b3e3b6
parent 1d301abba7a3a0d2cdd6db39b5c90ad8cc11236b
Author: Mikolaj Lenczewski <33129490+EnderRifter@users.noreply.github.com>
Date:   Fri,  5 Jul 2019 10:57:22 +0100

Fixed octant tree generation bug

Diffstat:
MStarSim/StarSimLib/Constants.cs | 2+-
MStarSim/StarSimLib/Data Structures/Octant.cs | 31+++++++++++++++++++++++++++++++
MStarSim/StarSimLib/Data Structures/OctantTree.cs | 106+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
MStarSim/StarSimLib/StarSimLib.xml | 45+++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 143 insertions(+), 41 deletions(-)

diff --git a/StarSim/StarSimLib/Constants.cs b/StarSim/StarSimLib/Constants.cs @@ -10,7 +10,7 @@ namespace StarSimLib /// <summary> /// The amount of bodies that are rendered by default. /// </summary> - public const int BodyCount = 1_000; + public const int BodyCount = 2; /// <summary> /// The mass of the central body, if it is included. diff --git a/StarSim/StarSimLib/Data Structures/Octant.cs b/StarSim/StarSimLib/Data Structures/Octant.cs @@ -102,6 +102,14 @@ namespace StarSimLib.Data_Structures } /// <summary> + /// The central point for this instance. + /// </summary> + public Vector4 Midpoint + { + get { return midpoint; } + } + + /// <summary> /// Indexes this instance, shorthand for <see cref="SubOctant(PositionSpecifier)"/>. /// </summary> /// <param name="specifier">Which child octant instance to return.</param> @@ -112,6 +120,16 @@ namespace StarSimLib.Data_Structures } /// <summary> + /// Indexes this instance, shorthand for <see cref="SubOctant(PositionSpecifier)"/>. + /// </summary> + /// <param name="specifier">Which child octant instance to return.</param> + /// <returns>The specified child octant instance.</returns> + public Octant this[int specifier] + { + get { return SubOctant(specifier); } + } + + /// <summary> /// Returns the specified child octant instance, or instantiates a new octant if the specified child instance /// is <c>null</c>. The newly constructed instance will then be returned. /// </summary> @@ -151,6 +169,19 @@ namespace StarSimLib.Data_Structures /// </summary> /// <param name="specifier">Which child octant instance to return.</param> /// <returns>The specified child octant instance.</returns> + /// <exception cref="ArgumentOutOfRangeException"> + /// Thrown when the given specifier does not equate to one of the values in the <see cref="PositionSpecifier"/> enum. + /// </exception> + public Octant SubOctant(int specifier) => SubOctant((PositionSpecifier)specifier); + + /// <summary> + /// Returns the specified child octant instance. + /// </summary> + /// <param name="specifier">Which child octant instance to return.</param> + /// <returns>The specified child octant instance.</returns> + /// <exception cref="ArgumentOutOfRangeException"> + /// Thrown when the given specifier does not equate to one of the values in the <see cref="PositionSpecifier"/> enum. + /// </exception> public Octant SubOctant(PositionSpecifier specifier) { switch (specifier) diff --git a/StarSim/StarSimLib/Data Structures/OctantTree.cs b/StarSim/StarSimLib/Data Structures/OctantTree.cs @@ -45,6 +45,16 @@ namespace StarSimLib.Data_Structures } /// <summary> + /// Indexes this instance, shorthand for <see cref="SubTree(PositionSpecifier)"/>. + /// </summary> + /// <param name="specifier">Which child octant tree instance to return.</param> + /// <returns>The specified child octant tree instance.</returns> + public OctantTree this[int specifier] + { + get { return SubTree(specifier); } + } + + /// <summary> /// Returns the specified child octant tree instance, or instantiates a new octant tree if the specified child /// instance is <c>null</c>. The newly constructed instance will then be returned. /// </summary> @@ -72,46 +82,47 @@ namespace StarSimLib.Data_Structures /// <param name="newBody">The <see cref="Body"/> instance to add.</param> public void AddBody(Body newBody) { - while (true) + if (body == null) { - if (body == null) - { - // this is an empty instance that has not yet had any bodies added to it. - body = newBody; - } - else if (IsExternal()) + // this is an empty instance that has not yet had any bodies added to it. + body = newBody; + } + else if (IsExternal()) + { + // this instance is 'external' and contains another body. figure out where the new body should go and + // create a new octant tree instance to hold the new body + for (int i = 0; i < childTrees.Length; i++) { - // this instance is 'external' and contains another body. figure out where the new body should go and - // create a new octant tree instance to hold the new body - foreach (OctantTree tree in childTrees) + OctantTree tree = SubTree(i); + + if (body.IsInOctant(tree.octant)) { - if (body.IsInOctant(tree.octant)) - { - tree.AddBody(body); - } + tree.AddBody(body); + break; } - - continue; } - else if (!IsExternal()) - { - // this instance already has a body to represent it, and it is not an 'external' tree instance, that is - // it has child trees of its own. figure out in which child tree the new body should be stored and update - // any further child nodes - // make the held body an aggregate body - body.Collide(newBody); + AddBody(newBody); + } + else if (!IsExternal()) + { + // this instance already has a body to represent it, and it is not an 'external' tree instance, that is + // it has child trees of its own. figure out in which child tree the new body should be stored and update + // any further child nodes + + // make the held body an aggregate body + body.Collide(newBody); - foreach (OctantTree tree in childTrees) + for (int i = 0; i < childTrees.Length; i++) + { + OctantTree tree = SubTree(i); + + if (newBody.IsInOctant(tree.octant)) { - if (newBody.IsInOctant(tree.octant)) - { - tree.AddBody(newBody); - } + tree.AddBody(newBody); + break; } } - - break; } } @@ -126,33 +137,46 @@ namespace StarSimLib.Data_Structures /// </summary> /// <param name="specifier">Which child octant tree instance to return.</param> /// <returns>The specified child octant tree instance.</returns> + /// <exception cref="ArgumentOutOfRangeException"> + /// Thrown when the given specifier does not equate to one of the values in the <see cref="PositionSpecifier"/> enum. + /// </exception> + public OctantTree SubTree(int specifier) => SubTree((PositionSpecifier)specifier); + + /// <summary> + /// Returns the specified child octant tree instance. + /// </summary> + /// <param name="specifier">Which child octant tree instance to return.</param> + /// <returns>The specified child octant tree instance.</returns> + /// <exception cref="ArgumentOutOfRangeException"> + /// Thrown when the given specifier does not equate to one of the values in the <see cref="PositionSpecifier"/> enum. + /// </exception> public OctantTree SubTree(PositionSpecifier specifier) { switch (specifier) { case PositionSpecifier.TopNorthWest: - return GetOrSetSubTree(ref childTrees, 0, octant?[specifier]); + return GetOrSetSubTree(ref childTrees, 0, octant?[0]); case PositionSpecifier.TopNorthEast: - return GetOrSetSubTree(ref childTrees, 1, octant?[specifier]); + return GetOrSetSubTree(ref childTrees, 1, octant?[1]); case PositionSpecifier.TopSouthEast: - return GetOrSetSubTree(ref childTrees, 2, octant?[specifier]); + return GetOrSetSubTree(ref childTrees, 2, octant?[2]); case PositionSpecifier.TopSouthWest: - return GetOrSetSubTree(ref childTrees, 3, octant?[specifier]); + return GetOrSetSubTree(ref childTrees, 3, octant?[3]); case PositionSpecifier.BottomNorthWest: - return GetOrSetSubTree(ref childTrees, 4, octant?[specifier]); + return GetOrSetSubTree(ref childTrees, 4, octant?[4]); case PositionSpecifier.BottomNorthEast: - return GetOrSetSubTree(ref childTrees, 5, octant?[specifier]); + return GetOrSetSubTree(ref childTrees, 5, octant?[5]); case PositionSpecifier.BottomSouthEast: - return GetOrSetSubTree(ref childTrees, 6, octant?[specifier]); + return GetOrSetSubTree(ref childTrees, 6, octant?[6]); case PositionSpecifier.BottomSouthWest: - return GetOrSetSubTree(ref childTrees, 7, octant?[specifier]); + return GetOrSetSubTree(ref childTrees, 7, octant?[7]); default: throw new ArgumentOutOfRangeException(nameof(specifier), specifier, @@ -183,9 +207,11 @@ namespace StarSimLib.Data_Structures } else { - foreach (OctantTree tree in childTrees) + for (int i = 0; i < childTrees.Length; i++) { - tree?.UpdateForces(referenceBody); + OctantTree tree = SubTree(i); + + tree.UpdateForces(referenceBody); } } } diff --git a/StarSim/StarSimLib/StarSimLib.xml b/StarSim/StarSimLib/StarSimLib.xml @@ -471,6 +471,11 @@ The length of 1 complete side of this instance. </summary> </member> + <member name="P:StarSimLib.Data_Structures.Octant.Midpoint"> + <summary> + The central point for this instance. + </summary> + </member> <member name="P:StarSimLib.Data_Structures.Octant.Item(StarSimLib.Data_Structures.PositionSpecifier)"> <summary> Indexes this instance, shorthand for <see cref="M:StarSimLib.Data_Structures.Octant.SubOctant(StarSimLib.Data_Structures.PositionSpecifier)"/>. @@ -478,6 +483,13 @@ <param name="specifier">Which child octant instance to return.</param> <returns>The specified child octant instance.</returns> </member> + <member name="P:StarSimLib.Data_Structures.Octant.Item(System.Int32)"> + <summary> + Indexes this instance, shorthand for <see cref="M:StarSimLib.Data_Structures.Octant.SubOctant(StarSimLib.Data_Structures.PositionSpecifier)"/>. + </summary> + <param name="specifier">Which child octant instance to return.</param> + <returns>The specified child octant instance.</returns> + </member> <member name="M:StarSimLib.Data_Structures.Octant.GetOrSetChildOctant(StarSimLib.Data_Structures.Octant[]@,System.Int32,StarSimLib.Data_Structures.Vector4,System.Double)"> <summary> Returns the specified child octant instance, or instantiates a new octant if the specified child instance @@ -500,12 +512,25 @@ </param> <returns>Whether the given point is within the bounds of this instance.</returns> </member> + <member name="M:StarSimLib.Data_Structures.Octant.SubOctant(System.Int32)"> + <summary> + Returns the specified child octant instance. + </summary> + <param name="specifier">Which child octant instance to return.</param> + <returns>The specified child octant instance.</returns> + <exception cref="T:System.ArgumentOutOfRangeException"> + Thrown when the given specifier does not equate to one of the values in the <see cref="T:StarSimLib.Data_Structures.PositionSpecifier"/> enum. + </exception> + </member> <member name="M:StarSimLib.Data_Structures.Octant.SubOctant(StarSimLib.Data_Structures.PositionSpecifier)"> <summary> Returns the specified child octant instance. </summary> <param name="specifier">Which child octant instance to return.</param> <returns>The specified child octant instance.</returns> + <exception cref="T:System.ArgumentOutOfRangeException"> + Thrown when the given specifier does not equate to one of the values in the <see cref="T:StarSimLib.Data_Structures.PositionSpecifier"/> enum. + </exception> </member> <member name="T:StarSimLib.Data_Structures.OctantTree"> <summary> @@ -540,6 +565,13 @@ <param name="specifier">Which child octant tree instance to return.</param> <returns>The specified child octant tree instance.</returns> </member> + <member name="P:StarSimLib.Data_Structures.OctantTree.Item(System.Int32)"> + <summary> + Indexes this instance, shorthand for <see cref="M:StarSimLib.Data_Structures.OctantTree.SubTree(StarSimLib.Data_Structures.PositionSpecifier)"/>. + </summary> + <param name="specifier">Which child octant tree instance to return.</param> + <returns>The specified child octant tree instance.</returns> + </member> <member name="M:StarSimLib.Data_Structures.OctantTree.GetOrSetSubTree(StarSimLib.Data_Structures.OctantTree[]@,System.Int32,StarSimLib.Data_Structures.Octant)"> <summary> Returns the specified child octant tree instance, or instantiates a new octant tree if the specified child @@ -566,12 +598,25 @@ </summary> <returns>Whether this instance has no non-null child instance.</returns> </member> + <member name="M:StarSimLib.Data_Structures.OctantTree.SubTree(System.Int32)"> + <summary> + Returns the specified child octant tree instance. + </summary> + <param name="specifier">Which child octant tree instance to return.</param> + <returns>The specified child octant tree instance.</returns> + <exception cref="T:System.ArgumentOutOfRangeException"> + Thrown when the given specifier does not equate to one of the values in the <see cref="T:StarSimLib.Data_Structures.PositionSpecifier"/> enum. + </exception> + </member> <member name="M:StarSimLib.Data_Structures.OctantTree.SubTree(StarSimLib.Data_Structures.PositionSpecifier)"> <summary> Returns the specified child octant tree instance. </summary> <param name="specifier">Which child octant tree instance to return.</param> <returns>The specified child octant tree instance.</returns> + <exception cref="T:System.ArgumentOutOfRangeException"> + Thrown when the given specifier does not equate to one of the values in the <see cref="T:StarSimLib.Data_Structures.PositionSpecifier"/> enum. + </exception> </member> <member name="M:StarSimLib.Data_Structures.OctantTree.UpdateForces(StarSimLib.Data_Structures.Body)"> <summary>