Skip to content

Commit 304eccf

Browse files
committed
make fittings using representation instances
1 parent 1db3e1f commit 304eccf

File tree

12 files changed

+181
-35
lines changed

12 files changed

+181
-35
lines changed

Elements.MEP/src/Fittings/Assembly.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,11 @@ public override Transform GetRotatedTransform()
331331
{
332332
throw new NotImplementedException();
333333
}
334+
335+
/// <inheritdoc/>
336+
public override string GetRepresentationHash()
337+
{
338+
return Id.ToString();
339+
}
334340
}
335341
}

Elements.MEP/src/Fittings/Coupler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,10 @@ public override Transform GetRotatedTransform()
9292
var t = new Transform(Vector3.Origin, End.Direction, zAxis);
9393
return t;
9494
}
95+
96+
public override string GetRepresentationHash()
97+
{
98+
throw new NotImplementedException();
99+
}
95100
}
96101
}

Elements.MEP/src/Fittings/Cross.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,10 @@ public override Transform GetRotatedTransform()
154154
var t = new Transform(Vector3.Origin, Trunk.Direction, zAxis);
155155
return t;
156156
}
157+
158+
public override string GetRepresentationHash()
159+
{
160+
throw new NotImplementedException();
161+
}
157162
}
158163
}

Elements.MEP/src/Fittings/Elbow.cs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public Elbow(Vector3 position, Vector3 startDirection, Vector3 endDirection, dou
2929

3030
public override void UpdateRepresentations()
3131
{
32-
var profile = new Circle(Vector3.Origin, this.Start.Diameter / 2).ToPolygon(FlowSystemConstants.CIRCLE_SEGMENTS);
32+
var profile = new Circle(Vector3.Origin, Start.Diameter / 2).ToPolygon(FlowSystemConstants.CIRCLE_SEGMENTS);
3333

3434
var oneSweep = new Sweep(profile,
3535
GetSweepLine(),
@@ -38,8 +38,19 @@ public override void UpdateRepresentations()
3838
0,
3939
false);
4040

41-
var arrows = this.Start.GetArrow(this.Transform.Origin).Concat(this.End.GetArrow(this.Transform.Origin));
42-
this.Representation = new Representation(new List<SolidOperation> { oneSweep }.Concat(arrows).Concat(GetExtensions()).ToList());
41+
var arrows = new List<SolidOperation>();
42+
arrows.AddRange(Start.GetArrow(Transform.Origin, fittingRotationTransform: GetRotatedTransform()));
43+
arrows.AddRange(End.GetArrow(Transform.Origin, fittingRotationTransform: GetRotatedTransform()));
44+
var solidOperations = new List<SolidOperation> { oneSweep }.Concat(arrows).Concat(GetExtensions()).ToList();
45+
46+
if (UseRepresentationInstances)
47+
{
48+
FittingRepresentationStorageClass.SetFittingRepresentation(this, () => solidOperations);
49+
}
50+
else
51+
{
52+
Representation = new Representation(solidOperations);
53+
}
4354
}
4455

4556
public override Port[] GetPorts()
@@ -64,23 +75,25 @@ private Vector3 BendRadiusOffset(double? bendRadius, Vector3 direction)
6475

6576
private Polyline GetSweepLine()
6677
{
67-
var sweepLine = new List<Vector3>();
68-
sweepLine.Add(this.Start.Position - this.Transform.Origin);
78+
var sweepLine = new List<Vector3>
79+
{
80+
Start.Position - Transform.Origin
81+
};
6982

70-
if (this.BendRadius != 0)
83+
if (BendRadius != 0)
7184
{
7285
var startDirection = Vector3.XAxis;
73-
var startPoint = startDirection * this.BendRadius;
86+
var startPoint = startDirection * BendRadius;
7487
var startNormal = startDirection.Cross(Vector3.ZAxis).Unitized();
7588

76-
var originalPlane = new Polygon(Vector3.Origin, (this.Start.Position - this.Transform.Origin).Unitized(), (this.End.Position - this.Transform.Origin).Unitized());
89+
var originalPlane = new Polygon(Vector3.Origin, (Start.Position - Transform.Origin).Unitized(), (End.Position - Transform.Origin).Unitized());
7790
var transform = originalPlane.ToTransform();
7891
var inverted = transform.Inverted();
7992
originalPlane.Transform(inverted);
8093

8194
var angleBetweenOriginalVectors = originalPlane.Vertices[1].PlaneAngleTo(originalPlane.Vertices[2]) * Math.PI / 180;
8295
var endDirection = new Vector3(Math.Cos(angleBetweenOriginalVectors), Math.Sin(angleBetweenOriginalVectors));
83-
var endPoint = endDirection * this.BendRadius;
96+
var endPoint = endDirection * BendRadius;
8497
var endNormal = endDirection.Cross(Vector3.ZAxis).Unitized();
8598

8699
new Ray(startPoint, startNormal).Intersects(new Ray(endPoint, endNormal), out var intersectionPoint, true);
@@ -103,9 +116,17 @@ private Polyline GetSweepLine()
103116
sweepLine.Add(Vector3.Origin);
104117
}
105118

106-
sweepLine.Add(this.End.Position - this.Transform.Origin);
119+
sweepLine.Add(End.Position - Transform.Origin);
107120

108-
return new Polyline(sweepLine);
121+
if (UseRepresentationInstances)
122+
{
123+
var t = GetRotatedTransform().Inverted();
124+
return new Polyline(sweepLine.Select(v => t.OfPoint(v)).ToList());
125+
}
126+
else
127+
{
128+
return new Polyline(sweepLine);
129+
}
109130
}
110131

111132
public override Transform GetRotatedTransform()
@@ -114,5 +135,11 @@ public override Transform GetRotatedTransform()
114135
var t = new Transform(Vector3.Origin, End.Direction, zAxis);
115136
return t;
116137
}
138+
139+
/// <inheritdoc/>
140+
public override string GetRepresentationHash()
141+
{
142+
return $"{this.GetType().Name}-{this.Diameter}-{this.BendRadius}-{this.Angle}";
143+
}
117144
}
118145
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
using System;
3+
using System.Collections.Generic;
4+
using Elements.Geometry;
5+
using Elements.Geometry.Solids;
6+
7+
namespace Elements.Fittings
8+
{
9+
static class FittingRepresentationStorageClass
10+
{
11+
private static readonly Dictionary<string, List<RepresentationInstance>> _fittings = new Dictionary<string, List<RepresentationInstance>>();
12+
public static Dictionary<string, List<RepresentationInstance>> Fittings => _fittings;
13+
14+
public static void SetFittingRepresentation(Fitting fitting, Func<IList<SolidOperation>> makeSolids)
15+
{
16+
var hash = fitting.GetRepresentationHash();
17+
if (!_fittings.ContainsKey(hash))
18+
{
19+
var solids = makeSolids();
20+
_fittings.Add(hash, new List<RepresentationInstance> { new RepresentationInstance(new SolidRepresentation(solids), fitting.Material) });
21+
}
22+
fitting.RepresentationInstances = _fittings[hash];
23+
24+
fitting.Transform = fitting.GetRotatedTransform().Concatenated(new Transform(fitting.Transform.Origin));
25+
}
26+
}
27+
}

Elements.MEP/src/Fittings/IComponent.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Elements.Fittings
1313

1414
public abstract partial class ComponentBase : IComponent
1515
{
16+
public static bool UseRepresentationInstances = false;
1617
/// <summary>
1718
/// The component that is towards the trunk of the tree.
1819
/// </summary>
@@ -43,6 +44,8 @@ public abstract partial class ComponentBase : IComponent
4344

4445
public abstract void ClearAdditionalTransform();
4546

47+
public abstract string GetRepresentationHash();
48+
4649
/// <summary>
4750
/// Checks if transformation should be applied and propagated to the next connections.
4851
/// It's intended to "pull" the supplied transform into the current component,
@@ -348,10 +351,10 @@ public static double GetLength(this ComponentBase component)
348351
return ps.Length();
349352
case Terminal t:
350353
var heightDelta = Math.Abs(t.Transform.Origin.Z - t.Port.Position.Z);
351-
354+
352355
var terminalTransformOrigin = t.Transform.Origin.Project(Plane.XY);
353356
var terminalPortPosition = t.Port.Position.Project(Plane.XY);
354-
357+
355358
return terminalTransformOrigin.IsAlmostEqualTo(terminalPortPosition)
356359
? heightDelta
357360
: heightDelta + terminalTransformOrigin.DistanceTo(terminalPortPosition);

Elements.MEP/src/Fittings/Manifold.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public override List<Port> BranchSidePorts()
3939

4040
public override Port[] GetPorts()
4141
{
42-
return new[] {Trunk}.Concat(Branches).ToArray();
42+
return new[] { Trunk }.Concat(Branches).ToArray();
4343
}
4444

4545
public override Port TrunkSidePort()
@@ -64,5 +64,10 @@ public override Transform GetRotatedTransform()
6464
{
6565
throw new NotImplementedException();
6666
}
67+
68+
public override string GetRepresentationHash()
69+
{
70+
throw new NotImplementedException();
71+
}
6772
}
6873
}

Elements.MEP/src/Fittings/Port.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ public bool IsComplimentaryConnector(Port other, double positionTolerance = Vect
5656
{
5757
return false;
5858
}
59-
6059
var angle = Direction.AngleTo(other.Direction);
6160

62-
return angle.ApproximatelyEquals(180, angleTolerance);
61+
return angle.ApproximatelyEquals(180, angleTolerance);
6362
}
6463

6564
public bool IsIdenticalConnector(Port other, double positionTolerance = Vector3.EPSILON, double angleTolerance = 0.5)
@@ -68,22 +67,23 @@ public bool IsIdenticalConnector(Port other, double positionTolerance = Vector3.
6867
{
6968
return false;
7069
}
71-
7270
var angle = Direction.AngleTo(other.Direction);
7371

7472
return angle.ApproximatelyEquals(0, angleTolerance);
7573
}
7674

77-
public Sweep[] GetArrow(Vector3 relativeTo, double arrowLineLength = 0.1)
75+
public Sweep[] GetArrow(Vector3 relativeTo, double arrowLineLength = 0.1, Transform fittingRotationTransform = null)
7876
{
77+
var fittingRotationTransformInverted = fittingRotationTransform == null || !ComponentBase.UseRepresentationInstances ? new Transform() : fittingRotationTransform.Inverted();
78+
7979
var arrayHeadLength = 0.01;
8080
if (ShowArrows)
8181
{
82-
var transformedOrigin = Position - relativeTo;
82+
var transformedPosition = fittingRotationTransformInverted.OfPoint(Position - relativeTo);
8383
var arrowProfile = new Circle(Vector3.Origin, 0.01).ToPolygon(FlowSystemConstants.CIRCLE_SEGMENTS);
84-
var arrowLine = new Line(transformedOrigin, transformedOrigin + Direction * arrowLineLength);
84+
var arrowLine = new Line(transformedPosition, transformedPosition + fittingRotationTransformInverted.OfPoint(Direction) * arrowLineLength);
8585
var headProfile = new Circle(Vector3.Origin, 0.02).ToPolygon(FlowSystemConstants.CIRCLE_SEGMENTS);
86-
var headLine = new Line(transformedOrigin + Direction * arrowLineLength, transformedOrigin + Direction * (arrowLineLength + arrayHeadLength));
86+
var headLine = new Line(transformedPosition + fittingRotationTransformInverted.OfPoint(Direction) * arrowLineLength, transformedPosition + fittingRotationTransformInverted.OfPoint(Direction) * (arrowLineLength + arrayHeadLength));
8787
var shaft = new Sweep(arrowProfile, arrowLine, 0, 0, 0, false);
8888
var head = new Sweep(headProfile, headLine, 0, 0, 0, false);
8989
return new Sweep[] { shaft, head };

Elements.MEP/src/Fittings/Reducer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void Move(Vector3 translation)
133133
}
134134

135135
/// <summary>
136-
/// Port with smaller diameter points to the +X axis.
136+
/// Port with smaller diameter points to the +X axis.
137137
/// If there is eccentric transform, the smaller part will be shifted to the -Z axis.
138138
/// We point smaller diameter in the +X direction so that there is one reducer defined in the standard orientation, to which this transformation is then applied.
139139
/// This let's us just have one size 110/90 that is rotated into a 90/110 orientation when needed.
@@ -163,5 +163,10 @@ public override Transform GetRotatedTransform()
163163
var t = new Transform(Vector3.Origin, xAxis, zAxis);
164164
return t;
165165
}
166+
167+
public override string GetRepresentationHash()
168+
{
169+
throw new NotImplementedException();
170+
}
166171
}
167172
}

Elements.MEP/src/Fittings/Terminal.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,10 @@ public override Transform GetRotatedTransform()
109109
var t = new Transform(Vector3.Origin, Port.Direction, zAxis);
110110
return t;
111111
}
112+
113+
public override string GetRepresentationHash()
114+
{
115+
throw new NotImplementedException();
116+
}
112117
}
113118
}

0 commit comments

Comments
 (0)