-
Notifications
You must be signed in to change notification settings - Fork 976
Expand file tree
/
Copy pathImageAnnotation.cs
More file actions
320 lines (276 loc) · 10.6 KB
/
Copy pathImageAnnotation.cs
File metadata and controls
320 lines (276 loc) · 10.6 KB
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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ImageAnnotation.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Represents an annotation that shows an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
#nullable enable
namespace OxyPlot.Annotations
{
using System;
/// <summary>
/// Represents an annotation that shows an image.
/// </summary>
public class ImageAnnotation : TransposableAnnotation
{
/// <summary>
/// The actual bounds of the rendered image.
/// </summary>
private OxyRect actualBounds;
/// <summary>
/// Initializes a new instance of the <see cref="ImageAnnotation" /> class.
/// </summary>
public ImageAnnotation()
{
this.X = new PlotLength(0.5, PlotLengthUnit.RelativeToPlotArea);
this.Y = new PlotLength(0.5, PlotLengthUnit.RelativeToPlotArea);
this.OffsetX = new PlotLength(0, PlotLengthUnit.ScreenUnits);
this.OffsetY = new PlotLength(0, PlotLengthUnit.ScreenUnits);
this.Width = new PlotLength(double.NaN, PlotLengthUnit.ScreenUnits);
this.Height = new PlotLength(double.NaN, PlotLengthUnit.ScreenUnits);
this.Opacity = 1.0;
this.Interpolate = true;
this.HorizontalAlignment = HorizontalAlignment.Center;
this.VerticalAlignment = VerticalAlignment.Middle;
}
/// <summary>
/// Gets or sets the image source.
/// </summary>
/// <value>The image source.</value>
public OxyImage? ImageSource { get; set; }
/// <summary>
/// Gets or sets the horizontal alignment.
/// </summary>
/// <value>The horizontal alignment.</value>
public HorizontalAlignment HorizontalAlignment { get; set; }
/// <summary>
/// Gets or sets the X position of the image.
/// </summary>
/// <value>The X.</value>
public PlotLength X { get; set; }
/// <summary>
/// Gets or sets the Y position of the image.
/// </summary>
/// <value>The Y.</value>
public PlotLength Y { get; set; }
/// <summary>
/// Gets or sets the X offset.
/// </summary>
/// <value>The offset X.</value>
public PlotLength OffsetX { get; set; }
/// <summary>
/// Gets or sets the Y offset.
/// </summary>
/// <value>The offset Y.</value>
public PlotLength OffsetY { get; set; }
/// <summary>
/// Gets or sets the width.
/// </summary>
/// <value>The width.</value>
public PlotLength Width { get; set; }
/// <summary>
/// Gets or sets the height.
/// </summary>
/// <value>The height.</value>
public PlotLength Height { get; set; }
/// <summary>
/// Gets or sets the opacity (0-1).
/// </summary>
/// <value>The opacity value.</value>
public double Opacity { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to apply smooth interpolation to the image.
/// </summary>
/// <value><c>true</c> if the image should be interpolated (using a high-quality bi-cubic interpolation); <c>false</c> if the nearest neighbor should be used.</value>
public bool Interpolate { get; set; }
/// <summary>
/// Gets or sets the vertical alignment.
/// </summary>
/// <value>The vertical alignment.</value>
public VerticalAlignment VerticalAlignment { get; set; }
/// <inheritdoc/>
public override void Render(IRenderContext rc)
{
if (this.ImageSource == null)
{
throw new InvalidOperationException($"{nameof(this.ImageSource)} must be non-null before rendering.");
}
base.Render(rc);
var p = this.GetPoint(this.X, this.Y, this.PlotModel);
var o = this.GetVector(this.OffsetX, this.OffsetY, this.PlotModel);
var position = p + o;
var s = this.GetVector(this.Width, this.Height, this.PlotModel);
var width = s.X;
var height = s.Y;
if (double.IsNaN(width) && double.IsNaN(height))
{
width = this.ImageSource.Width;
height = this.ImageSource.Height;
}
if (double.IsNaN(width))
{
width = height / this.ImageSource.Height * this.ImageSource.Width;
}
if (double.IsNaN(height))
{
height = width / this.ImageSource.Width * this.ImageSource.Height;
}
width = Math.Abs(width);
height = Math.Abs(height);
var x = position.X;
var y = position.Y;
var ha = this.HorizontalAlignment;
var va = this.VerticalAlignment;
if (ha == HorizontalAlignment.Center)
{
x -= width * 0.5;
}
else if (ha == HorizontalAlignment.Right)
{
x -= width;
}
if (va == VerticalAlignment.Middle)
{
y -= height * 0.5;
}
else if (va == VerticalAlignment.Bottom)
{
y -= height;
}
this.actualBounds = new OxyRect(x, y, width, height);
rc.DrawImage(this.ImageSource, x, y, width, height, this.Opacity, this.Interpolate);
}
/// <summary>
/// When overridden in a derived class, tests if the plot element is hit by the specified point.
/// </summary>
/// <param name="args">The hit test arguments.</param>
/// <returns>
/// The result of the hit test.
/// </returns>
protected override HitTestResult? HitTestOverride(HitTestArguments args)
{
if (this.actualBounds.Contains(args.Point))
{
return new HitTestResult(this, args.Point);
}
return null;
}
/// <summary>
/// Gets the point.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <param name="model">The model.</param>
/// <returns>The point in screen coordinates.</returns>
protected ScreenPoint GetPoint(PlotLength x, PlotLength y, PlotModel model)
{
var xd = double.NaN;
var yd = double.NaN;
if (x.Unit == PlotLengthUnit.Data || y.Unit == PlotLengthUnit.Data)
{
var dataX = x.Unit == PlotLengthUnit.Data ? x.Value : double.NaN;
var dataY = y.Unit == PlotLengthUnit.Data ? y.Value : double.NaN;
var p = this.Transform(dataX, dataY);
xd = p.X;
yd = p.Y;
}
switch (x.Unit)
{
case PlotLengthUnit.Data:
break;
case PlotLengthUnit.ScreenUnits:
xd = x.Value;
break;
case PlotLengthUnit.RelativeToViewport:
xd = model.Width * x.Value;
break;
case PlotLengthUnit.RelativeToPlotArea:
xd = model.PlotArea.Left + (model.PlotArea.Width * x.Value);
break;
default:
throw new ArgumentOutOfRangeException();
}
switch (y.Unit)
{
case PlotLengthUnit.Data:
break;
case PlotLengthUnit.ScreenUnits:
yd = y.Value;
break;
case PlotLengthUnit.RelativeToViewport:
yd = model.Height * y.Value;
break;
case PlotLengthUnit.RelativeToPlotArea:
yd = model.PlotArea.Top + (model.PlotArea.Height * y.Value);
break;
default:
throw new ArgumentOutOfRangeException();
}
return new ScreenPoint(xd, yd);
}
/// <summary>
/// Gets the vector.
/// </summary>
/// <param name="x">The x component.</param>
/// <param name="y">The y component.</param>
/// <param name="model">The model.</param>
/// <returns>The vector in screen coordinates.</returns>
protected ScreenVector GetVector(PlotLength x, PlotLength y, PlotModel model)
{
var xd = double.NaN;
var yd = double.NaN;
if (x.Unit == PlotLengthUnit.Data || y.Unit == PlotLengthUnit.Data)
{
var dataX = x.Unit == PlotLengthUnit.Data ? x.Value : double.NaN;
var dataY = y.Unit == PlotLengthUnit.Data ? y.Value : double.NaN;
var v = this.Transform(dataX, dataY) - this.Transform(0, 0);
xd = v.X;
yd = v.Y;
}
switch (x.Unit)
{
case PlotLengthUnit.Data:
break;
case PlotLengthUnit.ScreenUnits:
xd = x.Value;
break;
case PlotLengthUnit.RelativeToViewport:
xd = model.Width * x.Value;
break;
case PlotLengthUnit.RelativeToPlotArea:
xd = model.PlotArea.Width * x.Value;
break;
default:
throw new ArgumentOutOfRangeException();
}
switch (y.Unit)
{
case PlotLengthUnit.Data:
break;
case PlotLengthUnit.ScreenUnits:
yd = y.Value;
break;
case PlotLengthUnit.RelativeToViewport:
yd = model.Height * y.Value;
break;
case PlotLengthUnit.RelativeToPlotArea:
yd = model.PlotArea.Height * y.Value;
break;
default:
throw new ArgumentOutOfRangeException();
}
return new ScreenVector(xd, yd);
}
/// <inheritdoc/>
public override OxyRect GetClippingRect()
{
if (this.X.Unit == PlotLengthUnit.Data || this.Y.Unit == PlotLengthUnit.Data)
{
return base.GetClippingRect();
}
return OxyRect.Everything;
}
}
}