-
Notifications
You must be signed in to change notification settings - Fork 9
/
LinearScale.cs
348 lines (289 loc) · 12 KB
/
LinearScale.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#region Copyright
/////////////////////////////////////////////////////////////////////////////
// Altaxo: a data processing and data plotting program
// Copyright (C) 2002-2011 Dr. Dirk Lellinger
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
/////////////////////////////////////////////////////////////////////////////
#endregion Copyright
#nullable enable
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Altaxo.Graph.Scales.Boundaries;
using Altaxo.Graph.Scales.Rescaling;
namespace Altaxo.Graph.Scales
{
/// <summary>
/// A linear axis, i.e a axis where physical values v can be translated to logical values l by v=a+b*l.
/// </summary>
[Serializable]
[DisplayName("${res:ClassNames.Altaxo.Graph.Scales.LinearScale}")]
public class LinearScale : NumericalScale
{
/// <summary>Holds the <see cref="NumericalBoundaries"/> for that axis.</summary>
protected NumericalBoundaries _dataBounds;
protected NumericScaleRescaleConditions _rescaling;
protected Ticks.TickSpacing _tickSpacing;
// cached values
/// <summary>Current axis origin (cached value).</summary>
protected double _cachedAxisOrg = 0;
/// <summary>Current axis end (cached value).</summary>
protected double _cachedAxisEnd = 1;
/// <summary>Current axis span (i.e. end-org) (cached value).</summary>
protected double _cachedAxisSpan = 1;
/// <summary>Current inverse of axis span (cached value).</summary>
protected double _cachedOneByAxisSpan = 1;
#region Serialization
[Altaxo.Serialization.Xml.XmlSerializationSurrogateFor("AltaxoBase", "Altaxo.Graph.Scales.LinearScale", 3)]
private class XmlSerializationSurrogate3 : Altaxo.Serialization.Xml.IXmlSerializationSurrogate
{
public void Serialize(object obj, Altaxo.Serialization.Xml.IXmlSerializationInfo info)
{
throw new InvalidOperationException("Serialization of old version");
/*
LinearScale s = (LinearScale)obj;
info.AddValue("Org", s._cachedAxisOrg);
info.AddValue("End", s._cachedAxisEnd);
info.AddValue("Rescaling", s._rescaling);
info.AddValue("Bounds", s._dataBounds);
*/
}
public object Deserialize(object? o, Altaxo.Serialization.Xml.IXmlDeserializationInfo info, object? parent)
{
var s = (LinearScale?)o ?? new LinearScale(info);
s._cachedAxisOrg = info.GetDouble("Org");
s._cachedAxisEnd = info.GetDouble("End");
s._cachedAxisSpan = s._cachedAxisEnd - s._cachedAxisOrg;
s._cachedOneByAxisSpan = 1 / s._cachedAxisSpan;
s.ChildSetMember(ref s._rescaling, (NumericScaleRescaleConditions)info.GetValue("Rescaling", s));
s.ChildSetMember(ref s._dataBounds, (FiniteNumericalBoundaries)info.GetValue("Bounds", s));
s.ChildSetMember(ref s._tickSpacing, new Ticks.LinearTickSpacing());
s.EhChildChanged(s._dataBounds, EventArgs.Empty); // for this old version, rescaling is not fully serialized, thus we have to simulate a DataBoundChanged event to get _rescaling updated, and finally _tickSpacing updated
return s;
}
}
/// <summary>
/// 2015-02-11 Add Tickspacing
/// </summary>
[Altaxo.Serialization.Xml.XmlSerializationSurrogateFor(typeof(LinearScale), 4)]
private class XmlSerializationSurrogate4 : Altaxo.Serialization.Xml.IXmlSerializationSurrogate
{
public void Serialize(object obj, Altaxo.Serialization.Xml.IXmlSerializationInfo info)
{
var s = (LinearScale)obj;
info.AddValue("Org", s._cachedAxisOrg);
info.AddValue("End", s._cachedAxisEnd);
info.AddValue("Bounds", s._dataBounds);
info.AddValue("Rescaling", s._rescaling);
info.AddValue("TickSpacing", s._tickSpacing);
}
public object Deserialize(object? o, Altaxo.Serialization.Xml.IXmlDeserializationInfo info, object? parent)
{
var s = (LinearScale?)o ?? new LinearScale(info);
s._cachedAxisOrg = info.GetDouble("Org");
s._cachedAxisEnd = info.GetDouble("End");
s._cachedAxisSpan = s._cachedAxisEnd - s._cachedAxisOrg;
s._cachedOneByAxisSpan = 1 / s._cachedAxisSpan;
s.ChildSetMember(ref s._dataBounds, (FiniteNumericalBoundaries)info.GetValue("Bounds", s));
s.ChildSetMember(ref s._rescaling, (NumericScaleRescaleConditions)info.GetValue("Rescaling", s));
s.ChildSetMember(ref s._tickSpacing, (Ticks.TickSpacing)info.GetValue("TickSpacing", s));
s.UpdateTicksAndOrgEndUsingRescalingObject();
return s;
}
}
#endregion Serialization
/// <summary>
/// Constructor for deserialization only.
/// </summary>
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
protected LinearScale(Altaxo.Serialization.Xml.IXmlDeserializationInfo info)
{
}
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
/// <summary>
/// Creates a default linear axis with org=0 and end=1.
/// </summary>
public LinearScale()
{
_dataBounds = new FiniteNumericalBoundaries() { ParentObject = this };
_rescaling = new LinearScaleRescaleConditions() { ParentObject = this };
_tickSpacing = new Ticks.LinearTickSpacing() { ParentObject = this };
UpdateTicksAndOrgEndUsingRescalingObject();
}
/// <summary>
/// Copy constructor.
/// </summary>
/// <param name="from">A other linear axis from which to copy from.</param>
public LinearScale(LinearScale from)
{
CopyFrom(from);
}
[MemberNotNull(nameof(_dataBounds), nameof(_rescaling), nameof(_tickSpacing))]
protected void CopyFrom(LinearScale from)
{
using (var suspendToken = SuspendGetToken())
{
_cachedAxisEnd = from._cachedAxisEnd;
_cachedAxisOrg = from._cachedAxisOrg;
_cachedAxisSpan = from._cachedAxisSpan;
_cachedOneByAxisSpan = from._cachedOneByAxisSpan;
ChildCopyToMemberOrCreateNew(ref _dataBounds, from._dataBounds, () => new FiniteNumericalBoundaries());
ChildCopyToMemberOrCreateNew(ref _rescaling, from._rescaling, () => new LinearScaleRescaleConditions());
ChildCopyToMemberOrCreateNew(ref _tickSpacing, from._tickSpacing, () => new Ticks.LinearTickSpacing());
EhSelfChanged(EventArgs.Empty);
suspendToken.Resume();
}
}
public override bool CopyFrom(object obj)
{
if (ReferenceEquals(this, obj))
return true;
if (obj is LinearScale from)
{
CopyFrom(from);
return true;
}
return false;
}
protected override System.Collections.Generic.IEnumerable<Main.DocumentNodeAndName> GetDocumentNodeChildrenWithName()
{
if (_dataBounds is not null)
yield return new Main.DocumentNodeAndName(_dataBounds, () => _dataBounds = null!, "DataBounds");
if (_rescaling is not null)
yield return new Main.DocumentNodeAndName(_rescaling, () => _rescaling = null!, "Rescaling");
if (_tickSpacing is not null)
yield return new Main.DocumentNodeAndName(_tickSpacing, () => _tickSpacing = null!, "TickSpacing");
}
public override object Clone()
{
return new LinearScale(this);
}
/// <summary>
/// Get/sets the axis origin (physical units).
/// </summary>
public override double Org
{
get { return _cachedAxisOrg; }
}
/// <summary>
/// Get/sets the axis end (physical units).
/// </summary>
public override double End
{
get { return _cachedAxisEnd; }
}
/// <summary>
/// Returns the rescaling conditions for this axis
/// </summary>
public override NumericScaleRescaleConditions Rescaling
{
get
{
return _rescaling;
}
}
/// <summary>
/// Get the internal DataBound object (mostly for merging).
/// </summary>
public override NumericalBoundaries DataBounds
{
get { return _dataBounds; }
}
public override Ticks.TickSpacing TickSpacing
{
get
{
return _tickSpacing;
}
set
{
if (value is null)
throw new ArgumentNullException();
if (ChildSetMember(ref _tickSpacing, value))
EhChildChanged(Rescaling, EventArgs.Empty);
}
}
/// <summary>
/// Converts a value in axis (physical) units to a "normalized" value, which is 0 for the axis org and 1 for the axis end.
/// </summary>
/// <param name="x">Value to convert (physical units).</param>
/// <returns>Normalized value.</returns>
public override double PhysicalToNormal(double x)
{
return (x - _cachedAxisOrg) * _cachedOneByAxisSpan;
}
public override double NormalToPhysical(double x)
{
return _cachedAxisOrg + x * _cachedAxisSpan;
}
/// <summary>
/// PhysicalVariantToNormal translates physical values into a normal value linear along the axis
/// a physical value of the axis origin must return a value of zero
/// a physical value of the axis end must return a value of one
/// the function physicalToNormal must be provided by any derived class
/// </summary>
/// <param name="x">the physical value</param>
/// <returns>
/// the normalized value linear along the axis,
/// 0 for axis origin, 1 for axis end</returns>
public override double PhysicalVariantToNormal(Altaxo.Data.AltaxoVariant x)
{
return PhysicalToNormal(x.ToDoubleOrNaN());
}
/// <summary>
/// NormalToPhysicalVariant is the inverse function to PhysicalToNormal
/// It translates a normalized value (0 for the axis origin, 1 for the axis end)
/// into the physical value
/// </summary>
/// <param name="x">the normal value (0 for axis origin, 1 for axis end</param>
/// <returns>the corresponding physical value</returns>
public override Altaxo.Data.AltaxoVariant NormalToPhysicalVariant(double x)
{
return new Altaxo.Data.AltaxoVariant(NormalToPhysical(x));
}
/// <summary>
/// Returns the default axis boundaries when no valid data are entered into.
/// </summary>
/// <param name="org"></param>
/// <param name="end"></param>
public void GetDefaultAxisBoundaries(out double org, out double end)
{
org = 0;
end = 1;
}
protected override string? SetScaleOrgEnd(Altaxo.Data.AltaxoVariant org, Altaxo.Data.AltaxoVariant end)
{
double o = org.ToDouble();
double e = end.ToDouble();
if (!(o < e))
return "org is not less than end";
InternalSetOrgEnd(o, e, false, false);
return null;
}
private void InternalSetOrgEnd(double org, double end, bool isOrgExtendable, bool isEndExtendable)
{
bool changed = _cachedAxisOrg != org ||
_cachedAxisEnd != end;
_cachedAxisOrg = org;
_cachedAxisEnd = end;
_cachedAxisSpan = end - org;
_cachedOneByAxisSpan = 1 / _cachedAxisSpan;
if (changed)
EhSelfChanged(EventArgs.Empty);
}
} // end of class LinearAxis
}