Skip to content

Commit

Permalink
Add properties for shape stats to Abstract Shape. Support adding mass…
Browse files Browse the repository at this point in the history
… for hollow volume
  • Loading branch information
NathanKell committed Sep 29, 2023
1 parent 12755a4 commit 527b277
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 10 deletions.
61 changes: 61 additions & 0 deletions Source/ProceduralAbstractShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,66 @@ protected set
}
private float _volume;

public float NominalVolume
{
get => _nominalVolume;
protected set
{
_nominalVolume = value;
}
}
private float _nominalVolume;

public float MaxDiameter
{
get => _maxDiameter;
protected set
{
_maxDiameter = value;
}
}
private float _maxDiameter;

public float MinDiameter
{
get => _minDiameter;
protected set
{
_minDiameter = value;
}
}
private float _minDiameter;

public float InnerMinDiameter
{
get => _innerMinDiameter;
protected set
{
_innerMinDiameter = value;
}
}
private float _innerMinDiameter;

public float InnerMaxDiameter
{
get => _innerMaxDiameter;
protected set
{
_innerMaxDiameter = value;
}
}
private float _innerMaxDiameter;

public float Length
{
get => _length;
protected set
{
_length = value;
}
}
private float _length;

#endregion

#region Events
Expand Down Expand Up @@ -279,6 +339,7 @@ public void ChangeVolume(string volName, double newVolume)
var data = new BaseEventDetails (BaseEventDetails.Sender.USER);
data.Set<string> ("volName", volName);
data.Set<double> ("newTotalVolume", newVolume);
data.Set<double> ("newNominalVolume", NominalVolume);
Debug.Log($"{ModTag} {part.name} OnPartVolumeChanged volName:{volName} vol:{newVolume:F4}");
part.SendEvent ("OnPartVolumeChanged", data, 0);
}
Expand Down
24 changes: 23 additions & 1 deletion Source/ProceduralPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace ProceduralParts
{
public class ProceduralPart : PartModule, IPartCostModifier
public class ProceduralPart : PartModule, IPartCostModifier, IPartMassModifier
{
public static readonly string ModTag = "[ProceduralParts]";
public const string PAWGroupName = "ProcParts";
Expand Down Expand Up @@ -594,6 +594,28 @@ public void UpdateProps()
prop.UpdateProp();
}

#region Mass override for hollow tanks

[KSPField]
public float densityForHollowVolume = 0f;

[KSPField(isPersistant = true)]
public float moduleMass = 0f;

public ModifierChangeWhen GetModuleMassChangeWhen() => ModifierChangeWhen.FIXED;

public float GetModuleMass(float defaultMass, ModifierStagingSituation sit)
{
if (shape == null || !HighLogic.LoadedSceneIsEditor)
return moduleMass;

moduleMass = shape.NominalVolume < shape.Volume ? 0f : densityForHollowVolume * (shape.NominalVolume - shape.Volume);

return moduleMass;
}

#endregion

#region TestFlight
public static Type tfInterface = null;
public static BindingFlags tfBindingFlags = BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static;
Expand Down
7 changes: 6 additions & 1 deletion Source/ProceduralShapeBezierCone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,12 @@ private void SetFieldVisibility()
internal override void UpdateShape(bool force = true)
{
Profiler.BeginSample("UpdateShape BCone");
Volume = CalculateVolume();
MaxDiameter = Mathf.Max(topDiameter, bottomDiameter);
MinDiameter = Mathf.Min(topDiameter, bottomDiameter);
InnerMaxDiameter = InnerMinDiameter = -1f;
Length = length;
NominalVolume = CalculateVolume();
Volume = NominalVolume;
SetControlPoints(length, topDiameter, bottomDiameter);
// Ensure correct control points if something called AdjustDimensionBounds or CalculateVolume when handling volume change event
WriteBezier();
Expand Down
9 changes: 7 additions & 2 deletions Source/ProceduralShapeCone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ internal override void UpdateShape(bool force = true)
{
Profiler.BeginSample("UpdateShape Cone");
part.CoMOffset = CoMOffset;
Volume = CalculateVolume();
MaxDiameter = Mathf.Max(topDiameter, bottomDiameter);
MinDiameter = Mathf.Min(topDiameter, bottomDiameter);
InnerMaxDiameter = InnerMinDiameter = -1f;
Length = length;
NominalVolume = CalculateVolume();
Volume = NominalVolume;
Vector2 norm = new Vector2(length, (bottomDiameter - topDiameter) / 2f);
norm.Normalize();

Expand Down Expand Up @@ -249,7 +254,7 @@ public override void AdjustDimensionBounds()
}

public override float CalculateVolume() => CalculateVolume(length, topDiameter, bottomDiameter);
public virtual float CalculateVolume(float length, float topDiameter, float bottomDiameter)
public static float CalculateVolume(float length, float topDiameter, float bottomDiameter)
{
return (Mathf.PI * length * (topDiameter * topDiameter + topDiameter * bottomDiameter + bottomDiameter * bottomDiameter)) / 12f;
}
Expand Down
8 changes: 6 additions & 2 deletions Source/ProceduralShapeCylinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ internal override void UpdateShape(bool force = true)
{
Profiler.BeginSample("UpdateShape Cyl");
part.CoMOffset = CoMOffset;
Volume = CalculateVolume();
MaxDiameter = MinDiameter = diameter;
InnerMaxDiameter = InnerMinDiameter = -1f;
Length = length;
NominalVolume = CalculateVolume();
Volume = NominalVolume;
Vector2 norm = new Vector2(1, 0);

WriteMeshes(
Expand Down Expand Up @@ -92,7 +96,7 @@ public override void AdjustDimensionBounds()
}

public override float CalculateVolume() => CalculateVolume(length, diameter);
public float CalculateVolume(float length, float diameter)
public static float CalculateVolume(float length, float diameter)
{
return diameter * diameter * 0.25f * Mathf.PI * length;
}
Expand Down
6 changes: 6 additions & 0 deletions Source/ProceduralShapeHollowCone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ internal override void UpdateShape(bool force = true)
{
Profiler.BeginSample("UpdateShape HCone");
part.CoMOffset = CoMOffset;
MaxDiameter = Mathf.Max(topOuterDiameter, bottomOuterDiameter);
MinDiameter = Mathf.Min(topOuterDiameter, bottomOuterDiameter);
InnerMaxDiameter = Mathf.Max(topInnerDiameter, bottomInnerDiameter);
InnerMinDiameter = Mathf.Min(topInnerDiameter, bottomInnerDiameter);
Length = length;
NominalVolume = ProceduralShapeCone.CalculateVolume(length, topOuterDiameter, bottomOuterDiameter);
Volume = CalculateVolume();
GenerateMeshes(bottomOuterDiameter / 2, bottomInnerDiameter / 2, topOuterDiameter / 2, topInnerDiameter / 2, length, numSides);

Expand Down
4 changes: 4 additions & 0 deletions Source/ProceduralShapeHollowCylinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ internal override void UpdateShape(bool force = true)
{
Profiler.BeginSample("UpdateShape HCyl");
part.CoMOffset = CoMOffset;
MinDiameter = MaxDiameter = outerDiameter;
InnerMinDiameter = InnerMaxDiameter = innerDiameter;
Length = length;
NominalVolume = ProceduralShapeCylinder.CalculateVolume(length, outerDiameter);
Volume = CalculateVolume();
GenerateMeshes(outerDiameter / 2, innerDiameter / 2, length, numSides);

Expand Down
6 changes: 6 additions & 0 deletions Source/ProceduralShapeHollowPill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ internal override void UpdateShape(bool force = true)
{
Profiler.BeginSample("UpdateShape HPill");
part.CoMOffset = CoMOffset;
MaxDiameter = outerDiameter;
MinDiameter = outerDiameter - fillet * 2f;
InnerMaxDiameter = innerDiameter;
InnerMinDiameter = innerDiameter - fillet * 2f;
Length = length;
NominalVolume = ProceduralShapePill.CalculateVolume(length, outerDiameter, fillet);
Volume = CalculateVolume();
GenerateMeshes(MajorRadius, MinorRadius, length, fillet / 2, (int)numSides);

Expand Down
6 changes: 6 additions & 0 deletions Source/ProceduralShapeHollowTruss.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ internal override void UpdateShape(bool force = true)
{
Profiler.BeginSample("UpdateShape Truss");
part.CoMOffset = CoMOffset;
MaxDiameter = Mathf.Max(bottomDiameter, topDiameter);
MinDiameter = Mathf.Min(bottomDiameter, topDiameter);
InnerMaxDiameter = MaxDiameter - rodDiameter * 2f;
InnerMinDiameter = MinDiameter - rodDiameter * 2f;
Length = length;
NominalVolume = ProceduralShapeCone.CalculateVolume(length, topDiameter, bottomDiameter);
Volume = CalculateVolume();
GenerateMeshes(bottomDiameter / 2, topDiameter / 2, length, rodDiameter / 2, (int)nbRods, tiltAngle * Mathf.Deg2Rad, offsetAngle * Mathf.Deg2Rad, symmetryRods);

Expand Down
8 changes: 6 additions & 2 deletions Source/ProceduralShapePill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ internal override void UpdateShape(bool forceUpdate=true)
{
Profiler.BeginSample("UpdateShape Pill");
part.CoMOffset = CoMOffset;
Volume = CalculateVolume();
MinDiameter = MaxDiameter = diameter;
InnerMinDiameter = InnerMaxDiameter = -1f;
Length = length;
NominalVolume = CalculateVolume();
Volume = NominalVolume;
LinkedList<ProfilePoint> points = new LinkedList<ProfilePoint>();

if (fillet == 0)
Expand Down Expand Up @@ -210,7 +214,7 @@ internal override void UpdateShape(bool forceUpdate=true)
}

public override float CalculateVolume() => CalculateVolume(length, diameter, fillet);
public virtual float CalculateVolume(float length, float diameter, float fillet)
public static float CalculateVolume(float length, float diameter, float fillet)
{
// To get formula for part volume: l = length, d = diameter, f = fillet
// body cylinder = pi * r^2 * h
Expand Down
6 changes: 5 additions & 1 deletion Source/ProceduralShapePolygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal SimPart() { }
// => (0.25 * innerDIameter * innerDiameter * normSideLength * cornerCount)
}

private float Length => length;
private new float Length => length;
private int CornerCount => (int)cornerCount;
private float CornerCenterCornerAngle => 2 * Mathf.PI / CornerCount;
private float EdgeToEdgeAngle => Mathf.PI - CornerCenterCornerAngle;
Expand Down Expand Up @@ -147,6 +147,10 @@ internal override void UpdateShape(bool force = true)
{
Profiler.BeginSample("UpdateShape Polygon");
part.CoMOffset = CoMOffset;
MinDiameter = MaxDiameter = diameter;
InnerMinDiameter = InnerMaxDiameter = -1f;
base.Length = length;
NominalVolume = ProceduralShapeCylinder.CalculateVolume(length, diameter);
Volume = CalculateVolume();
OuterDiameter = InnerDiameter / OuterToInnerFactor;
GenerateMeshes();
Expand Down
2 changes: 1 addition & 1 deletion Source/TankContentSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private double CalculateMaxResourceAmount(TankResource res)

#endregion

public ProceduralPart PPart => _pPart ?? (_pPart = GetComponent<ProceduralPart>());
public ProceduralPart PPart => _pPart != null ? _pPart : (_pPart = GetComponent<ProceduralPart>());
private ProceduralPart _pPart;
}
}

0 comments on commit 527b277

Please sign in to comment.