1
+ #region copyright
2
+
3
+ /* * * * * * * * * * * * * * * * * * * * * * * * * */
4
+ /* Carl Zeiss Industrielle Messtechnik GmbH */
5
+ /* Softwaresystem PiWeb */
6
+ /* (c) Carl Zeiss 2013 */
7
+ /* * * * * * * * * * * * * * * * * * * * * * * * * */
8
+
9
+ #endregion
10
+
11
+ namespace Zeiss . PiWeb . Volume
12
+ {
13
+ #region usings
14
+
15
+ using System ;
16
+ using System . Globalization ;
17
+ using System . Xml ;
18
+
19
+ #endregion
20
+
21
+ /// <summary>
22
+ /// Representation of a three-dimensional vector.
23
+ /// </summary>
24
+ public readonly struct Vector : IEquatable < Vector >
25
+ {
26
+ #region constructors
27
+
28
+ /// <summary>Constructor.</summary>
29
+ /// <param name="x">The x.</param>
30
+ /// <param name="y">The y.</param>
31
+ /// <param name="z">The z.</param>
32
+ public Vector ( double x = 0.0 , double y = 0.0 , double z = 0.0 )
33
+ {
34
+ X = x ;
35
+ Y = y ;
36
+ Z = z ;
37
+ }
38
+
39
+ #endregion
40
+
41
+ #region properties
42
+
43
+ /// <summary>
44
+ /// X coordinate.
45
+ /// </summary>
46
+ public double X { get ; }
47
+
48
+ /// <summary>
49
+ /// Y coordinate.
50
+ /// </summary>
51
+ public double Y { get ; }
52
+
53
+ /// <summary>
54
+ /// Z coordinate.
55
+ /// </summary>
56
+ public double Z { get ; }
57
+
58
+ #endregion
59
+
60
+ #region methods
61
+
62
+ /// <summary>
63
+ /// Serializes this instances information with the specified writer.
64
+ /// </summary>
65
+ /// <param name="writer">The writer.</param>
66
+ /// <exception cref="System.ArgumentNullException"></exception>
67
+ internal void Serialize ( XmlWriter writer )
68
+ {
69
+ if ( writer == null )
70
+ throw new ArgumentNullException ( nameof ( writer ) ) ;
71
+
72
+ writer . WriteAttributeString ( "X" , XmlConvert . ToString ( X ) ) ;
73
+ writer . WriteAttributeString ( "Y" , XmlConvert . ToString ( Y ) ) ;
74
+ writer . WriteAttributeString ( "Z" , XmlConvert . ToString ( Z ) ) ;
75
+ }
76
+
77
+ /// <summary>
78
+ /// Deserializes information from the specified reader.
79
+ /// </summary>
80
+ /// <param name="reader">The reader.</param>
81
+ /// <returns></returns>
82
+ /// <exception cref="System.ArgumentNullException">reader</exception>
83
+ internal static Vector Deserialize ( XmlReader reader )
84
+ {
85
+ if ( reader == null )
86
+ throw new ArgumentNullException ( nameof ( reader ) ) ;
87
+
88
+ var value = reader . GetAttribute ( "X" ) ;
89
+ var x = string . IsNullOrWhiteSpace ( value ) ? default : XmlConvert . ToDouble ( value ) ;
90
+ value = reader . GetAttribute ( "Y" ) ;
91
+ var y = string . IsNullOrWhiteSpace ( value ) ? default : XmlConvert . ToDouble ( value ) ;
92
+ value = reader . GetAttribute ( "Z" ) ;
93
+ var z = string . IsNullOrWhiteSpace ( value ) ? default : XmlConvert . ToDouble ( value ) ;
94
+
95
+ return new Vector ( x , y , z ) ;
96
+ }
97
+
98
+ /// <summary>
99
+ /// Adds the given vectors.
100
+ /// </summary>
101
+ public static Vector operator + ( Vector a , Vector b )
102
+ {
103
+ return new Vector ( a . X + b . X , a . Y + b . Y , a . Z + b . Z ) ;
104
+ }
105
+
106
+ /// <summary>
107
+ /// Subtracts the given vectors.
108
+ /// </summary>
109
+ public static Vector operator - ( Vector a , Vector b )
110
+ {
111
+ return new Vector ( a . X - b . X , a . Y - b . Y , a . Z - b . Z ) ;
112
+ }
113
+
114
+ /// <summary>
115
+ /// Binary equality.
116
+ /// </summary>
117
+ public static bool operator == ( Vector a , Vector b )
118
+ {
119
+ return Equals ( a , b ) ;
120
+ }
121
+
122
+ /// <summary>
123
+ /// Binary inequality.
124
+ /// </summary>
125
+ public static bool operator != ( Vector a , Vector b )
126
+ {
127
+ return ! Equals ( a , b ) ;
128
+ }
129
+
130
+ /// <summary>
131
+ /// Determines whether the two specified instances are equal.
132
+ /// </summary>
133
+ private static bool Equals ( Vector a , Vector b )
134
+ {
135
+ return Math . Abs ( a . X - b . X ) < double . Epsilon &&
136
+ Math . Abs ( a . Y - b . Y ) < double . Epsilon &&
137
+ Math . Abs ( a . Z - b . Z ) < double . Epsilon ;
138
+ }
139
+
140
+ /// <inheritdoc />
141
+ public override bool Equals ( object ? obj )
142
+ {
143
+ return obj is Vector vector && Equals ( this , vector ) ;
144
+ }
145
+
146
+ /// <inheritdoc />
147
+ public override int GetHashCode ( )
148
+ {
149
+ return X . GetHashCode ( ) ^ Y . GetHashCode ( ) ^ Z . GetHashCode ( ) ;
150
+ }
151
+
152
+ /// <inheritdoc />
153
+ public override string ToString ( )
154
+ {
155
+ return string . Format ( CultureInfo . InvariantCulture , "{0}, {1}, {2}" , X , Y , Z ) ;
156
+ }
157
+
158
+ #endregion
159
+
160
+ #region interface IEquatable<Vector>
161
+
162
+ /// <summary>
163
+ /// Indicates whether the current object is equal to another object of the same type.
164
+ /// </summary>
165
+ /// <param name="other">An object to compare with this object.</param>
166
+ /// <returns>
167
+ /// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
168
+ /// </returns>
169
+ public bool Equals ( Vector other )
170
+ {
171
+ return Equals ( this , other ) ;
172
+ }
173
+
174
+ #endregion
175
+ }
176
+ }
0 commit comments