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