Skip to content

Commit 861a3e0

Browse files
committed
feat: add volume transformation to meta data
1 parent 7c00225 commit 861a3e0

File tree

4 files changed

+355
-5
lines changed

4 files changed

+355
-5
lines changed

src/PiWeb.Volume.Convert/Gom.cs

+25-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Zeiss.PiWeb.Volume.Convert;
55
using System;
66
using System.Globalization;
77
using System.IO;
8+
using System.Linq;
89
using System.Xml;
910

1011
#endregion
@@ -41,7 +42,8 @@ public static VolumeMetadata ParseMetadata( Stream stream, out double minValue,
4142

4243
rawFileName = ReadValue( document, "//volume/raw_data/raw_data_block" );
4344

44-
var result = new VolumeMetadata( sx, sy, sz, rx, ry, rz );
45+
var coordinateSystem = ReadCoordinateSystem( document );
46+
var result = new VolumeMetadata( sx, sy, sz, rx, ry, rz, coordinateSystem: coordinateSystem );
4547

4648
result.Properties.Add( Property.Create( MaximumValue, minValue ) );
4749
result.Properties.Add( Property.Create( MinimumValue, maxValue ) );
@@ -50,6 +52,28 @@ public static VolumeMetadata ParseMetadata( Stream stream, out double minValue,
5052
return result;
5153
}
5254

55+
private static CoordinateSystem? ReadCoordinateSystem( XmlDocument document )
56+
{
57+
var transformationMatrixParts = ReadValue( document, "//volume/volume_transformation" ).Split( ',' );
58+
if( transformationMatrixParts.Length != 16 )
59+
return default;
60+
61+
var values = new double[ 16 ];
62+
for( var i = 0; i < 16; i++ )
63+
{
64+
if( !double.TryParse( transformationMatrixParts[ i ], NumberStyles.Float, CultureInfo.InvariantCulture, out values[ i ] ) )
65+
return default;
66+
}
67+
68+
return new CoordinateSystem
69+
{
70+
Axis1 = new Vector( values[ 0 ], values[ 4 ], values[ 8 ] ),
71+
Axis2 = new Vector( values[ 1 ], values[ 5 ], values[ 9 ] ),
72+
Axis3 = new Vector( values[ 2 ], values[ 6 ], values[ 10 ] ),
73+
Origin = new Vector( values[ 3 ], values[ 7 ], values[ 11 ] )
74+
};
75+
}
76+
5377
private static DataType ReadDataType( XmlDocument document, string path )
5478
{
5579
var node = ReadValue( document, path );

src/PiWeb.Volume/CoordinateSystem.cs

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
using System.Xml.Serialization;
19+
20+
#endregion
21+
22+
/// <summary>
23+
/// Describes a coordinate system, composed of a position vector and 3 direction vectors.
24+
/// </summary>
25+
public sealed class CoordinateSystem
26+
{
27+
#region constructors
28+
29+
/// <summary>Constructor.</summary>
30+
public CoordinateSystem()
31+
{
32+
Origin = new Vector( 0, 0, 0 );
33+
Axis1 = new Vector( 1, 0, 0 );
34+
Axis2 = new Vector( 0, 1, 0 );
35+
Axis3 = new Vector( 0, 0, 1 );
36+
}
37+
38+
#endregion
39+
40+
#region properties
41+
42+
/// <summary>
43+
/// Gets or sets the position vector.
44+
/// </summary>
45+
public Vector Origin { get; set; }
46+
47+
/// <summary>
48+
/// Gets or sets the first direction vector.
49+
/// </summary>
50+
public Vector Axis1 { get; set; }
51+
52+
/// <summary>
53+
/// Gets or sets the second direction vector.
54+
/// </summary>
55+
public Vector Axis2 { get; set; }
56+
57+
/// <summary>
58+
/// Gets or sets the third direction vector.
59+
/// </summary>
60+
public Vector Axis3 { get; set; }
61+
62+
#endregion
63+
64+
#region methods
65+
66+
/// <inheritdoc cref="IXmlSerializable.WriteXml" />
67+
internal void Serialize( XmlWriter writer )
68+
{
69+
if( writer == null )
70+
{
71+
throw new ArgumentNullException( nameof( writer ) );
72+
}
73+
74+
writer.WriteStartElement( "Origin" );
75+
Origin.Serialize( writer );
76+
writer.WriteEndElement();
77+
78+
writer.WriteStartElement( "Axis1" );
79+
Axis1.Serialize( writer );
80+
writer.WriteEndElement();
81+
82+
writer.WriteStartElement( "Axis2" );
83+
Axis2.Serialize( writer );
84+
writer.WriteEndElement();
85+
86+
writer.WriteStartElement( "Axis3" );
87+
Axis3.Serialize( writer );
88+
writer.WriteEndElement();
89+
}
90+
91+
/// <inheritdoc cref="IXmlSerializable.ReadXml" />
92+
public static CoordinateSystem Deserialize( XmlReader reader )
93+
{
94+
if( reader == null )
95+
{
96+
throw new ArgumentNullException( nameof( reader ) );
97+
}
98+
99+
var result = new CoordinateSystem();
100+
101+
while( reader.Read() && reader.NodeType != XmlNodeType.EndElement )
102+
{
103+
switch( reader.Name )
104+
{
105+
case "Origin":
106+
result.Origin = Vector.Deserialize( reader );
107+
break;
108+
case "Axis1":
109+
result.Axis1 = Vector.Deserialize( reader );
110+
break;
111+
case "Axis2":
112+
result.Axis2 = Vector.Deserialize( reader );
113+
break;
114+
case "Axis3":
115+
result.Axis3 = Vector.Deserialize( reader );
116+
break;
117+
}
118+
}
119+
120+
return result;
121+
}
122+
123+
/// <inheritdoc />
124+
public override string ToString()
125+
{
126+
return string.Format(
127+
CultureInfo.InvariantCulture,
128+
"Origin={{{0}}}; Axis1={{{1}}}; Axis2={{{2}}}; Axis3={{{3}}}",
129+
Origin, Axis1, Axis2, Axis3 );
130+
}
131+
132+
#endregion
133+
}
134+
}

src/PiWeb.Volume/Vector.cs

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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

Comments
 (0)