1 | using System.Collections.Generic;
|
---|
2 |
|
---|
3 | namespace Netron.Diagramming.Core.Layout.Force {
|
---|
4 | /// <summary>
|
---|
5 | /// Represents a spring in a force simulation.
|
---|
6 | /// </summary>
|
---|
7 | public class Spring {
|
---|
8 | #region Fields
|
---|
9 | private static SpringFactory mFactory = new SpringFactory();
|
---|
10 | private ForceItem item1;
|
---|
11 | private ForceItem item2;
|
---|
12 | private float length;
|
---|
13 | private float coeff;
|
---|
14 |
|
---|
15 | #endregion
|
---|
16 |
|
---|
17 | #region Properties
|
---|
18 | /// <summary>
|
---|
19 | /// Retrieve the SpringFactory instance, which serves as an object pool
|
---|
20 | /// for Spring instances.
|
---|
21 | /// </summary>
|
---|
22 | /// <value>The spring factory.</value>
|
---|
23 | public static SpringFactory Factory {
|
---|
24 | get {
|
---|
25 | return mFactory;
|
---|
26 | }
|
---|
27 | }
|
---|
28 | /// <summary>
|
---|
29 | /// The first ForceItem endpoint
|
---|
30 | /// </summary>
|
---|
31 |
|
---|
32 | public ForceItem Item1 {
|
---|
33 | get { return item1; }
|
---|
34 | set { item1 = value; }
|
---|
35 | }
|
---|
36 | /// <summary>
|
---|
37 | /// The second ForceItem endpoint
|
---|
38 | /// </summary>
|
---|
39 | public ForceItem Item2 {
|
---|
40 | get { return item2; }
|
---|
41 | set { item2 = value; }
|
---|
42 | }
|
---|
43 | /// <summary>
|
---|
44 | /// The spring's resting length
|
---|
45 | /// </summary>
|
---|
46 | public float Length {
|
---|
47 | get { return length; }
|
---|
48 | set { length = value; }
|
---|
49 | }
|
---|
50 | /// <summary>
|
---|
51 | /// The spring tension co-efficient
|
---|
52 | /// </summary>
|
---|
53 | public float Coeff {
|
---|
54 | get { return coeff; }
|
---|
55 | set { coeff = value; }
|
---|
56 | }
|
---|
57 | #endregion
|
---|
58 |
|
---|
59 | #region Constructor
|
---|
60 | /// <summary>
|
---|
61 | /// Create a new Spring instance
|
---|
62 | ///<param name="fi1"> the first ForceItem endpoint</param>
|
---|
63 | /// <param name="fi2"> the second ForceItem endpoint </param>
|
---|
64 | /// <param name="k">the spring tension co-efficient </param>
|
---|
65 | /// <param name="len">the spring's resting length </param>
|
---|
66 | /// </summary>
|
---|
67 | public Spring(ForceItem fi1, ForceItem fi2, float k, float len) {
|
---|
68 | item1 = fi1;
|
---|
69 | item2 = fi2;
|
---|
70 | coeff = k;
|
---|
71 | length = len;
|
---|
72 | }
|
---|
73 |
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | #region Classes
|
---|
77 | /// <summary>
|
---|
78 | /// The SpringFactory is responsible for generating Spring instances
|
---|
79 | /// and maintaining an object pool of Springs to reduce garbage collection
|
---|
80 | /// overheads while force simulations are running.
|
---|
81 | /// </summary>
|
---|
82 | public sealed class SpringFactory {
|
---|
83 | public const int Capacity = 5000;
|
---|
84 | private List<Spring> springs = new List<Spring>(Capacity);
|
---|
85 |
|
---|
86 | /// <summary>
|
---|
87 | /// Get a Spring instance and set it to the given parameters.
|
---|
88 | /// </summary>
|
---|
89 | public Spring getSpring(ForceItem f1, ForceItem f2, float k, float length) {
|
---|
90 | if (springs.Count > 0) {
|
---|
91 | Spring s = springs[springs.Count - 1];
|
---|
92 | springs.Remove(s);
|
---|
93 | s.Item1 = f1;
|
---|
94 | s.Item2 = f2;
|
---|
95 | s.Coeff = k;
|
---|
96 | s.Length = length;
|
---|
97 | return s;
|
---|
98 | } else {
|
---|
99 | return new Spring(f1, f2, k, length);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | /// <summary>
|
---|
103 | /// Reclaim a Spring into the object pool.
|
---|
104 | /// </summary>
|
---|
105 | public void reclaim(Spring s) {
|
---|
106 | s.Item1 = null;
|
---|
107 | s.Item2 = null;
|
---|
108 | if (springs.Count < Capacity)
|
---|
109 | springs.Add(s);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | #endregion
|
---|
113 | }
|
---|
114 | }
|
---|