1 | using System;
|
---|
2 |
|
---|
3 | namespace Netron.Diagramming.Core.Layout.Force {
|
---|
4 | /// <summary>
|
---|
5 | ///Abstract base class for force functions in a force simulation. This
|
---|
6 | ///skeletal version provides support for storing and retrieving float-valued
|
---|
7 | ///parameters of the force function. Subclasses should use the protected
|
---|
8 | ///field <code>parms</code> to store parameter values.
|
---|
9 | /// </summary>
|
---|
10 | public abstract class AbstractForce : IForce {
|
---|
11 | #region Fields
|
---|
12 | /// <summary>
|
---|
13 | /// temporary paramters blobs
|
---|
14 | /// </summary>
|
---|
15 | protected float[] parms;
|
---|
16 | /// <summary>
|
---|
17 | /// min values
|
---|
18 | /// </summary>
|
---|
19 | protected float[] minValues;
|
---|
20 | /// <summary>
|
---|
21 | /// max values
|
---|
22 | /// </summary>
|
---|
23 | protected float[] maxValues;
|
---|
24 |
|
---|
25 | #endregion
|
---|
26 |
|
---|
27 | #region Properties
|
---|
28 | /// <summary>
|
---|
29 | ///Returns false.
|
---|
30 | /// </summary>
|
---|
31 | public virtual bool IsSpringForce {
|
---|
32 | get {
|
---|
33 | return false;
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | /// <summary>
|
---|
38 | /// Returns the number of parameters (e.g., gravitational constant or
|
---|
39 | /// spring force coefficient) affecting this force function.
|
---|
40 | /// </summary>
|
---|
41 | /// <value></value>
|
---|
42 | /// <returns>the number of parameters</returns>
|
---|
43 | public int ParameterCount {
|
---|
44 | get {
|
---|
45 | return (parms == null ? 0 : parms.Length);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | ///Gets whether this is a force.
|
---|
51 | /// </summary>
|
---|
52 | public virtual bool IsItemForce {
|
---|
53 | get {
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 | }
|
---|
57 | #endregion
|
---|
58 |
|
---|
59 | #region Methods
|
---|
60 | /// <summary>
|
---|
61 | /// Initialize this force function. This default implementation does nothing.
|
---|
62 | /// Subclasses should override this method with any needed initialization.
|
---|
63 | /// </summary>
|
---|
64 | /// <param name="fsim">the encompassing ForceSimulator</param>
|
---|
65 | public virtual void Init(ForceSimulator fsim) {
|
---|
66 | // do nothing.
|
---|
67 | }
|
---|
68 |
|
---|
69 | /// <summary>
|
---|
70 | /// Gets the parameter names.
|
---|
71 | /// </summary>
|
---|
72 | /// <returns></returns>
|
---|
73 | protected abstract String[] ParameterNames { get; }
|
---|
74 |
|
---|
75 |
|
---|
76 | /// <summary>
|
---|
77 | /// Returns the specified, numbered parameter.
|
---|
78 | /// </summary>
|
---|
79 | /// <param name="i">i the index of the parameter to return</param>
|
---|
80 | /// <returns>the parameter value</returns>
|
---|
81 | public float GetParameter(int i) {
|
---|
82 | if (i < 0 || parms == null || i >= parms.Length) {
|
---|
83 | throw new ArgumentOutOfRangeException();
|
---|
84 | } else {
|
---|
85 | return parms[i];
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /// <summary>
|
---|
90 | /// Gets the minimum value.
|
---|
91 | /// </summary>
|
---|
92 | /// <param name="i">The i.</param>
|
---|
93 | /// <returns></returns>
|
---|
94 | public float GetMinValue(int i) {
|
---|
95 | if (i < 0 || parms == null || i >= parms.Length) {
|
---|
96 | throw new ArgumentOutOfRangeException();
|
---|
97 | } else {
|
---|
98 | return minValues[i];
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /// <summary>
|
---|
103 | /// Gets the maximum value.
|
---|
104 | /// </summary>
|
---|
105 | /// <param name="i">The i.</param>
|
---|
106 | /// <returns></returns>
|
---|
107 | public float GetMaxValue(int i) {
|
---|
108 | if (i < 0 || parms == null || i >= parms.Length) {
|
---|
109 | throw new ArgumentOutOfRangeException();
|
---|
110 | } else {
|
---|
111 | return maxValues[i];
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | /// <summary>
|
---|
116 | /// Gets the text name of the requested parameter.
|
---|
117 | /// </summary>
|
---|
118 | /// <param name="i">i the index of the parameter</param>
|
---|
119 | /// <returns>
|
---|
120 | /// a String containing the name of this parameter
|
---|
121 | /// </returns>
|
---|
122 | public String GetParameterName(int i) {
|
---|
123 | String[] pnames = this.ParameterNames;
|
---|
124 | if (i < 0 || pnames == null || i >= pnames.Length) {
|
---|
125 | throw new ArgumentOutOfRangeException();
|
---|
126 | } else {
|
---|
127 | return pnames[i];
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | /// <summary>
|
---|
132 | /// Sets the specified parameter value.
|
---|
133 | /// </summary>
|
---|
134 | /// <param name="i">the index of the parameter</param>
|
---|
135 | /// <param name="val">the new value of the parameter</param>
|
---|
136 | public void SetParameter(int i, float val) {
|
---|
137 | if (i < 0 || parms == null || i >= parms.Length) {
|
---|
138 | throw new ArgumentOutOfRangeException();
|
---|
139 | } else {
|
---|
140 | parms[i] = val;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /// <summary>
|
---|
146 | /// Set the suggested minimum value for a parameter. This value is not
|
---|
147 | /// strictly enforced, but is used by interface components that allow force
|
---|
148 | /// parameters to be varied.
|
---|
149 | /// </summary>
|
---|
150 | /// <param name="i">the parameter index</param>
|
---|
151 | /// <param name="val">the suggested minimum value to use</param>
|
---|
152 | public void SetMinValue(int i, float val) {
|
---|
153 | if (i < 0 || parms == null || i >= parms.Length) {
|
---|
154 | throw new ArgumentOutOfRangeException();
|
---|
155 | } else {
|
---|
156 | minValues[i] = val;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /// <summary>
|
---|
161 | /// Set the suggested maximum value for a parameter. This value is not
|
---|
162 | /// strictly enforced, but is used by interface components that allow force
|
---|
163 | /// parameters to be varied.
|
---|
164 | /// </summary>
|
---|
165 | /// <param name="i">the parameter index</param>
|
---|
166 | /// <param name="val">the suggested maximum value to use</param>
|
---|
167 | public void SetMaxValue(int i, float val) {
|
---|
168 | if (i < 0 || parms == null || i >= parms.Length) {
|
---|
169 | throw new ArgumentOutOfRangeException();
|
---|
170 | } else {
|
---|
171 | maxValues[i] = val;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | /// <summary>
|
---|
176 | ///Returns the force acting on the given item.
|
---|
177 | /// </summary>
|
---|
178 | public virtual void GetForce(ForceItem item) {
|
---|
179 | throw new NotImplementedException("This class does not support this operation");
|
---|
180 | }
|
---|
181 |
|
---|
182 | /// <summary>
|
---|
183 | /// Updates the force calculation on the given Spring. The ForceItems
|
---|
184 | /// attached to Spring will have their force values updated appropriately.
|
---|
185 | /// </summary>
|
---|
186 | /// <param name="spring">spring the Spring on which to compute updated forces</param>
|
---|
187 | public virtual void GetForce(Spring spring) {
|
---|
188 | throw new NotImplementedException("This class does not support this operation");
|
---|
189 | }
|
---|
190 | #endregion
|
---|
191 |
|
---|
192 | }
|
---|
193 | }
|
---|