public void SerializationXml2() { Matrix33D m1 = new Matrix33D(12, 23, 45, 67, 89, 90, 43, 65, 87.3); Matrix33D m2; string fileName = "SerializationMatrix33D_DataContractSerializer.xml"; if (File.Exists(fileName)) { File.Delete(fileName); } var serializer = new DataContractSerializer(typeof(Matrix33D)); using (var stream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write)) using (var writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8)) serializer.WriteObject(writer, m1); using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) using (var reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas())) m2 = (Matrix33D)serializer.ReadObject(reader); Assert.AreEqual(m1, m2); }
public void SerializationXml() { Matrix33D m1 = new Matrix33D(12, 23, 45, 67, 89, 90, 43, 65, 87.3); Matrix33D m2; string fileName = "SerializationMatrix33D.xml"; if (File.Exists(fileName)) { File.Delete(fileName); } XmlSerializer serializer = new XmlSerializer(typeof(Matrix33D)); StreamWriter writer = new StreamWriter(fileName); serializer.Serialize(writer, m1); writer.Close(); serializer = new XmlSerializer(typeof(Matrix33D)); FileStream fileStream = new FileStream(fileName, FileMode.Open); m2 = (Matrix33D)serializer.Deserialize(fileStream); Assert.AreEqual(m1, m2); }
public void Properties() { Matrix33D m = new Matrix33D(rowMajor, MatrixOrder.RowMajor); Assert.AreEqual(1.0, m.M00); Assert.AreEqual(2.0, m.M01); Assert.AreEqual(3.0, m.M02); Assert.AreEqual(4.0, m.M10); Assert.AreEqual(5.0, m.M11); Assert.AreEqual(6.0, m.M12); Assert.AreEqual(7.0, m.M20); Assert.AreEqual(8.0, m.M21); Assert.AreEqual(9.0, m.M22); m = Matrix33D.Zero; m.M00 = 1.0; m.M01 = 2.0; m.M02 = 3.0; m.M10 = 4.0; m.M11 = 5.0; m.M12 = 6.0; m.M20 = 7.0; m.M21 = 8.0; m.M22 = 9.0; Assert.AreEqual(new Matrix33D(rowMajor, MatrixOrder.RowMajor), m); }
public void MultiplyTransposed() { var m = RandomHelper.Random.NextMatrix33D(1, 10); var v = RandomHelper.Random.NextVector3D(1, 10); Assert.AreEqual(m.Transposed * v, Matrix33D.MultiplyTransposed(m, v)); }
public void SerializationBinary() { Matrix33D m1 = new Matrix33D(12, 23, 45, 56, 67, 89, 90, 12, 43.3); Matrix33D m2; string fileName = "SerializationMatrix33D.bin"; if (File.Exists(fileName)) File.Delete(fileName); FileStream fs = new FileStream(fileName, FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, m1); fs.Close(); fs = new FileStream(fileName, FileMode.Open); formatter = new BinaryFormatter(); m2 = (Matrix33D)formatter.Deserialize(fs); fs.Close(); Assert.AreEqual(m1, m2); }
public void AbsoluteStatic() { Matrix33D m = new Matrix33D(-1, -2, -3, -4, -5, -6, -7, -8, -9); Matrix33D absoluteM = Matrix33D.Absolute(m); Assert.AreEqual(1, absoluteM.M00); Assert.AreEqual(2, absoluteM.M01); Assert.AreEqual(3, absoluteM.M02); Assert.AreEqual(4, absoluteM.M10); Assert.AreEqual(5, absoluteM.M11); Assert.AreEqual(6, absoluteM.M12); Assert.AreEqual(7, absoluteM.M20); Assert.AreEqual(8, absoluteM.M21); Assert.AreEqual(9, absoluteM.M22); m = new Matrix33D(1, 2, 3, 4, 5, 6, 7, 8, 9); absoluteM = Matrix33D.Absolute(m); Assert.AreEqual(1, absoluteM.M00); Assert.AreEqual(2, absoluteM.M01); Assert.AreEqual(3, absoluteM.M02); Assert.AreEqual(4, absoluteM.M10); Assert.AreEqual(5, absoluteM.M11); Assert.AreEqual(6, absoluteM.M12); Assert.AreEqual(7, absoluteM.M20); Assert.AreEqual(8, absoluteM.M21); Assert.AreEqual(9, absoluteM.M22); }
public void IsRotation() { Assert.IsTrue(!Matrix33D.Zero.IsRotation); Assert.IsTrue(Matrix33D.Identity.IsRotation); Assert.IsTrue(Matrix33D.CreateRotation(new Vector3D(1, 2, 3).Normalized, 0.5).IsRotation); Assert.IsTrue(!new Matrix33D(1, 0, 0, 0, 1, 0, 0, 0, -1).IsRotation); }
public void Transposed() { Matrix33D m = new Matrix33D(rowMajor, MatrixOrder.RowMajor); Matrix33D mt = new Matrix33D(rowMajor, MatrixOrder.ColumnMajor); Assert.AreEqual(mt, m.Transposed); Assert.AreEqual(Matrix33D.Identity, Matrix33D.Identity.Transposed); }
public void Trace() { Matrix33D m = new Matrix33D(new double[3, 3] { { 1, 2, 3 }, { 2, 4, 5 }, { 3, 5, 7 } }); Assert.AreEqual(12, m.Trace); }
public void GetColumn() { Matrix33D m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); Assert.AreEqual(new Vector3D(1.0, 4.0, 7.0), m.GetColumn(0)); Assert.AreEqual(new Vector3D(2.0, 5.0, 8.0), m.GetColumn(1)); Assert.AreEqual(new Vector3D(3.0, 6.0, 9.0), m.GetColumn(2)); }
public void GetRow() { Matrix33D m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); Assert.AreEqual(new Vector3D(1.0, 2.0, 3.0), m.GetRow(0)); Assert.AreEqual(new Vector3D(4.0, 5.0, 6.0), m.GetRow(1)); Assert.AreEqual(new Vector3D(7.0, 8.0, 9.0), m.GetRow(2)); }
protected override PoseD Read(ContentReader input, PoseD existingInstance) { Vector3D position = input.ReadRawObject <Vector3D>(); Matrix33D orientation = input.ReadRawObject <Matrix33D>(); return(new PoseD(position, orientation)); }
public void MultiplyMatrix() { Matrix33D m = new Matrix33D(12, 23, 45, 67, 89, 90, 43, 65, 87); Assert.AreEqual(Matrix33D.Zero, Matrix33D.Multiply(m, Matrix33D.Zero)); Assert.AreEqual(Matrix33D.Zero, Matrix33D.Multiply(Matrix33D.Zero, m)); Assert.AreEqual(m, Matrix33D.Multiply(m, Matrix33D.Identity)); Assert.AreEqual(m, Matrix33D.Multiply(Matrix33D.Identity, m)); Assert.IsTrue(Matrix33D.AreNumericallyEqual(Matrix33D.Identity, Matrix33D.Multiply(m, m.Inverse))); Assert.IsTrue(Matrix33D.AreNumericallyEqual(Matrix33D.Identity, Matrix33D.Multiply(m.Inverse, m))); Matrix33D m1 = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); Matrix33D m2 = new Matrix33D(12, 23, 45, 67, 89, 90, 43, 65, 87); Matrix33D result = Matrix33D.Multiply(m1, m2); for (int column = 0; column < 3; column++) { for (int row = 0; row < 3; row++) { Assert.AreEqual(Vector3D.Dot(m1.GetRow(row), m2.GetColumn(column)), result[row, column]); } } }
public void Constructors() { Matrix33D m = new Matrix33D(1.0, 2.0, 3.0f, 4.0, 5.0, 6.0f, 7.0, 8.0, 9.0); for (int i = 0; i < 9; i++) { Assert.AreEqual(rowMajor[i], m[i]); } m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); for (int i = 0; i < 9; i++) { Assert.AreEqual(rowMajor[i], m[i]); } m = new Matrix33D(rowMajor, MatrixOrder.RowMajor); for (int i = 0; i < 9; i++) { Assert.AreEqual(rowMajor[i], m[i]); } m = new Matrix33D(new List <double>(columnMajor), MatrixOrder.ColumnMajor); for (int i = 0; i < 9; i++) { Assert.AreEqual(rowMajor[i], m[i]); } m = new Matrix33D(new List <double>(rowMajor), MatrixOrder.RowMajor); for (int i = 0; i < 9; i++) { Assert.AreEqual(rowMajor[i], m[i]); } m = new Matrix33D(new double[3, 3] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }); for (int i = 0; i < 9; i++) { Assert.AreEqual(rowMajor[i], m[i]); } m = new Matrix33D(new double[3][] { new double[3] { 1, 2, 3 }, new double[3] { 4, 5, 6 }, new double[3] { 7, 8, 9 } }); for (int i = 0; i < 9; i++) { Assert.AreEqual(rowMajor[i], m[i]); } }
public void CrossProductMatrix() { Vector3D v = new Vector3D(-1.0, -2.0, -3.0); Vector3D w = new Vector3D(4.0, 5.0, 6.0); Matrix33D m = v.ToCrossProductMatrix(); Assert.AreEqual(Vector3D.Cross(v, w), v.ToCrossProductMatrix() * w); }
public void Subtraction() { Matrix33D m1 = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); Matrix33D m2 = Matrix33D.One; Matrix33D result = Matrix33D.Subtract(m1, m2); for (int i = 0; i < 9; i++) Assert.AreEqual(rowMajor[i] - 1.0, result[i]); }
public void Determinant() { Matrix33D m = new Matrix33D(rowMajor, MatrixOrder.RowMajor); Assert.AreEqual(0.0, m.Determinant); Assert.AreEqual(0.0, Matrix33D.Zero.Determinant); Assert.AreEqual(0.0, Matrix33D.One.Determinant); Assert.AreEqual(1.0, Matrix33D.Identity.Determinant); }
public void SubtractionOperator() { Matrix33D m1 = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); Matrix33D m2 = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor) * 3; Matrix33D result = m1 - m2; for (int i = 0; i < 9; i++) Assert.AreEqual(-rowMajor[i] * 2, result[i]); }
public void Multiplication() { double s = 0.1234; Matrix33D m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); m = Matrix33D.Multiply(s, m); for (int i = 0; i < 9; i++) Assert.AreEqual(rowMajor[i] * s, m[i]); }
public void Division() { double s = 0.1234; Matrix33D m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); m = Matrix33D.Divide(m, s); for (int i = 0; i < 9; i++) Assert.IsTrue(Numeric.AreEqual(rowMajor[i] / s, m[i])); }
public void Negation() { Matrix33D m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); for (int i = 0; i < 9; i++) { Assert.AreEqual(-rowMajor[i], Matrix33D.Negate(m)[i]); } }
public void ToArray1D() { Matrix33D m = new Matrix33D(1, 2, 3, 4, 5, 6, 7, 8, 9); double[] array = m.ToArray1D(MatrixOrder.RowMajor); for (int i = 0; i < 9; i++) Assert.AreEqual(rowMajor[i], array[i]); array = m.ToArray1D(MatrixOrder.ColumnMajor); for (int i = 0; i < 9; i++) Assert.AreEqual(columnMajor[i], array[i]); }
public void FromMatrixWithZeroTrace() { QuaternionD q; Matrix33D m = new Matrix33D(0, 1, 0, 0, 0, 1, 1, 0, 0); q = QuaternionD.CreateRotation(m); Assert.IsTrue(QuaternionD.AreNumericallyEqual(new QuaternionD(-0.5, 0.5, 0.5, 0.5), q)); }
public void Constants() { Matrix33D zero = Matrix33D.Zero; for (int i = 0; i < 9; i++) Assert.AreEqual(0.0, zero[i]); Matrix33D one = Matrix33D.One; for (int i = 0; i < 9; i++) Assert.AreEqual(1.0, one[i]); }
public void Transpose() { Matrix33D m = new Matrix33D(rowMajor, MatrixOrder.RowMajor); m.Transpose(); Matrix33D mt = new Matrix33D(rowMajor, MatrixOrder.ColumnMajor); Assert.AreEqual(mt, m); Matrix33D i = Matrix33D.Identity; i.Transpose(); Assert.AreEqual(Matrix33D.Identity, i); }
public void InverseWithNearSingularMatrix() { Matrix33D m = new Matrix33D(0.0001, 0, 0, 0, 0.0001, 0, 0, 0, 0.0001); Vector3D v = Vector3D.One; Vector3D w = m * v; Assert.IsTrue(Vector3D.AreNumericallyEqual(v, m.Inverse * w)); Assert.IsTrue(Matrix33D.AreNumericallyEqual(Matrix33D.Identity, m * m.Inverse)); }
public void ToList() { Matrix33D m = new Matrix33D(1, 2, 3, 4, 5, 6, 7, 8, 9); IList<double> list = m.ToList(MatrixOrder.RowMajor); for (int i = 0; i < 9; i++) Assert.AreEqual(rowMajor[i], list[i]); list = m.ToList(MatrixOrder.ColumnMajor); for (int i = 0; i < 9; i++) Assert.AreEqual(columnMajor[i], list[i]); }
/// <summary> /// Computes the covariance matrix for a list of 3-dimensional points (double-precision). /// </summary> /// <param name="points">The points.</param> /// <returns>The covariance matrix.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="points"/> is <see langword="null"/>. /// </exception> public static Matrix33D ComputeCovarianceMatrix(IList <Vector3D> points) { // Notes: See "Real-Time Collision Detection" p. 93 if (points == null) { throw new ArgumentNullException("points"); } int numberOfPoints = points.Count; float oneOverNumberOfPoints = 1f / numberOfPoints; // Compute the center of mass. Vector3D centerOfMass = Vector3D.Zero; for (int i = 0; i < numberOfPoints; i++) { centerOfMass += points[i]; } centerOfMass *= oneOverNumberOfPoints; // Compute covariance matrix. double c00 = 0; double c11 = 0; double c22 = 0; double c01 = 0; double c02 = 0; double c12 = 0; for (int i = 0; i < numberOfPoints; i++) { // Translate points so that center of mass is at origin. Vector3D p = points[i] - centerOfMass; // Compute covariance of translated point. c00 += p.X * p.X; c11 += p.Y * p.Y; c22 += p.Z * p.Z; c01 += p.X * p.Y; c02 += p.X * p.Z; c12 += p.Y * p.Z; } c00 *= oneOverNumberOfPoints; c11 *= oneOverNumberOfPoints; c22 *= oneOverNumberOfPoints; c01 *= oneOverNumberOfPoints; c02 *= oneOverNumberOfPoints; c12 *= oneOverNumberOfPoints; Matrix33D covarianceMatrix = new Matrix33D(c00, c01, c02, c01, c11, c12, c02, c12, c22); return(covarianceMatrix); }
public void Indexer1d() { Matrix33D m = new Matrix33D(rowMajor, MatrixOrder.RowMajor); for (int i = 0; i < 9; i++) Assert.AreEqual(rowMajor[i], m[i]); m = Matrix33D.Zero; for (int i = 0; i < 9; i++) m[i] = rowMajor[i]; Assert.AreEqual(new Matrix33D(rowMajor, MatrixOrder.RowMajor), m); }
public void CreateRotationZ() { double angle = (double)MathHelper.ToRadians(30); Matrix33D m = Matrix33D.CreateRotationZ(angle); Assert.IsTrue(Vector3D.AreNumericallyEqual(new Vector3D((double)Math.Cos(angle), (double)Math.Sin(angle), 0), m * Vector3D.UnitX)); QuaternionD q = QuaternionD.CreateRotation(Vector3D.UnitZ, angle); Assert.IsTrue(Vector3D.AreNumericallyEqual(q.Rotate(Vector3D.One), m * Vector3D.One)); Assert.IsTrue(Matrix33D.AreNumericallyEqual(Matrix33D.CreateRotation(Vector3D.UnitZ, angle), m)); }
public void Addition() { Matrix33D m1 = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); Matrix33D m2 = Matrix33D.One; Matrix33D result = Matrix33D.Add(m1, m2); for (int i = 0; i < 9; i++) Assert.AreEqual(rowMajor[i] + 1.0, result[i]); }
public void InvertException() { Matrix33D m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); m.Invert(); }
public void GetColumnException1() { Matrix33D m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); m.GetColumn(-1); }
public void SetMinor() { Matrix44D m = Matrix44D.Zero; Matrix33D minor = new Matrix33D(1.0, 2.0, 3.0f, 5.0, 6.0, 7.0f, 9.0, 10.0, 11.0); m.Minor = minor; Assert.AreEqual(minor, m.Minor); }
public void MultiplyVectorOperator() { Vector3D v = new Vector3D(2.34, 3.45, 4.56); Assert.AreEqual(v, Matrix33D.Identity * v); Assert.AreEqual(Vector3D.Zero, Matrix33D.Zero * v); Matrix33D m = new Matrix33D(12, 23, 45, 67, 89, 90, 43, 65, 87); Assert.IsTrue(Vector3D.AreNumericallyEqual(v, m * m.Inverse * v)); for (int i = 0; i < 3; i++) Assert.AreEqual(Vector3D.Dot(m.GetRow(i), v), (m * v)[i]); }
public void IsSymmetric() { Matrix33D m = new Matrix33D(new double[3, 3] { { 1, 2, 3 }, { 2, 4, 5 }, { 3, 5, 7 } }); Assert.AreEqual(true, m.IsSymmetric); m = new Matrix33D(new double[3, 3] { { 1, 2, 3 }, { 4, 5, 2 }, { 7, 4, 1 } }); Assert.AreEqual(false, m.IsSymmetric); }
public void MultiplicationOperator() { double s = 0.1234; Matrix33D m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); m = s * m; for (int i = 0; i < 9; i++) Assert.AreEqual(rowMajor[i] * s, m[i]); m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); m = m * s; for (int i = 0; i < 9; i++) Assert.AreEqual(rowMajor[i] * s, m[i]); }
public void Indexer1dException4() { Matrix33D m = new Matrix33D(); double x = m[9]; }
public void IsNaN() { const int numberOfRows = 3; const int numberOfColumns = 3; Assert.IsFalse(new Matrix33D().IsNaN); for (int r = 0; r < numberOfRows; r++) { for (int c = 0; c < numberOfColumns; c++) { Matrix33D m = new Matrix33D(); m[r, c] = double.NaN; Assert.IsTrue(m.IsNaN); } } }
public void Indexer2d() { Matrix33D m = new Matrix33D(rowMajor, MatrixOrder.RowMajor); for (int column = 0; column < 3; column++) for (int row = 0; row < 3; row++) Assert.AreEqual(columnMajor[column * 3 + row], m[row, column]); m = Matrix33D.Zero; for (int column = 0; column < 3; column++) for (int row = 0; row < 3; row++) m[row, column] = (double)(row * 3 + column + 1); Assert.AreEqual(new Matrix33D(rowMajor, MatrixOrder.RowMajor), m); }
public void Invert() { Assert.AreEqual(Matrix33D.Identity, Matrix33D.Identity.Inverse); Matrix33D m = new Matrix33D(1, 2, 3, 2, 5, 8, 7, 6, -1); Vector3D v = Vector3D.One; Vector3D w = m * v; Matrix33D im = m; im.Invert(); Assert.IsTrue(Vector3D.AreNumericallyEqual(v, im * w)); Assert.IsTrue(Matrix33D.AreNumericallyEqual(Matrix33D.Identity, m * im)); }
public void GetRowException2() { Matrix33D m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); m.GetRow(3); }
public void QuaternionDFromMatrix33() { Vector3D v = Vector3D.One; Matrix33D m = Matrix33D.Identity; QuaternionD q = QuaternionD.CreateRotation(m); Assert.IsTrue(Vector3D.AreNumericallyEqual(m * v, q.Rotate(v))); m = Matrix33D.CreateRotation(Vector3D.UnitX, 0.3); q = QuaternionD.CreateRotation(m); Assert.IsTrue(Vector3D.AreNumericallyEqual(m * v, q.Rotate(v))); m = Matrix33D.CreateRotation(Vector3D.UnitY, 1.0); q = QuaternionD.CreateRotation(m); Assert.IsTrue(Vector3D.AreNumericallyEqual(m * v, q.Rotate(v))); m = Matrix33D.CreateRotation(Vector3D.UnitZ, 4.0); q = QuaternionD.CreateRotation(m); Assert.IsTrue(Vector3D.AreNumericallyEqual(m * v, q.Rotate(v))); m = Matrix33D.Identity; q = QuaternionD.CreateRotation(m); Assert.IsTrue(Vector3D.AreNumericallyEqual(m * v, q.Rotate(v))); m = Matrix33D.CreateRotation(-Vector3D.UnitX, 1.3); q = QuaternionD.CreateRotation(m); Assert.IsTrue(Vector3D.AreNumericallyEqual(m * v, q.Rotate(v))); m = Matrix33D.CreateRotation(-Vector3D.UnitY, -1.4); q = QuaternionD.CreateRotation(m); Assert.IsTrue(Vector3D.AreNumericallyEqual(m * v, q.Rotate(v))); m = Matrix33D.CreateRotation(-Vector3D.UnitZ, -0.1); q = QuaternionD.CreateRotation(m); Assert.IsTrue(Vector3D.AreNumericallyEqual(m * v, q.Rotate(v))); m = new Matrix33D(0, 0, 1, 0, -1, 0, 1, 0, 0); q = QuaternionD.CreateRotation(m); Assert.IsTrue(Vector3D.AreNumericallyEqual(m * v, q.Rotate(v))); m = new Matrix33D(-1, 0, 0, 0, 1, 0, 0, 0, -1); q = QuaternionD.CreateRotation(m); Assert.IsTrue(Vector3D.AreNumericallyEqual(m * v, q.Rotate(v))); }
public void Indexer1dException2() { Matrix33D m = new Matrix33D(); m[9] = 0.0; }
public void NegationOperator() { Matrix33D m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); for (int i = 0; i < 9; i++) Assert.AreEqual(-rowMajor[i], (-m)[i]); }
public void GetMinor() { Matrix44D m = new Matrix44D(rowMajor, MatrixOrder.RowMajor); Matrix33D minor = m.Minor; Matrix33D expected = new Matrix33D(1.0, 2.0, 3.0f, 5.0, 6.0, 7.0f, 9.0, 10.0, 11.0); Assert.AreEqual(expected, minor); }
public void HashCode() { Matrix33D m = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); Assert.AreNotEqual(Matrix33D.Identity.GetHashCode(), m.GetHashCode()); }
public void MultiplyMatrixOperator() { Matrix33D m = new Matrix33D(12, 23, 45, 67, 89, 90, 43, 65, 87); Assert.AreEqual(Matrix33D.Zero, m * Matrix33D.Zero); Assert.AreEqual(Matrix33D.Zero, Matrix33D.Zero * m); Assert.AreEqual(m, m * Matrix33D.Identity); Assert.AreEqual(m, Matrix33D.Identity * m); Assert.IsTrue(Matrix33D.AreNumericallyEqual(Matrix33D.Identity, m * m.Inverse)); Assert.IsTrue(Matrix33D.AreNumericallyEqual(Matrix33D.Identity, m.Inverse * m)); Matrix33D m1 = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); Matrix33D m2 = new Matrix33D(12, 23, 45, 67, 89, 90, 43, 65, 87); Matrix33D result = m1 * m2; for (int column = 0; column < 3; column++) for (int row = 0; row < 3; row++) Assert.AreEqual(Vector3D.Dot(m1.GetRow(row), m2.GetColumn(column)), result[row, column]); }
public void SerializationJson() { Matrix33D m1 = new Matrix33D(12, 23, 45, 67, 89, 90, 43, 65, 87.3); Matrix33D m2; string fileName = "SerializationMatrix33D.json"; if (File.Exists(fileName)) File.Delete(fileName); var serializer = new DataContractJsonSerializer(typeof(Matrix33D)); using (var stream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write)) serializer.WriteObject(stream, m1); using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) m2 = (Matrix33D)serializer.ReadObject(stream); Assert.AreEqual(m1, m2); }
//-------------------------------------------------------------- /// <summary> /// Computes the derived values, like sun/moon positions, transformation matrices and light /// intensities. This method must be called when the location or time has changed. /// </summary> /// <remarks> /// This method must be called when the input properties <see cref="Latitude"/>, /// <see cref="Longitude"/>, <see cref="Altitude"/>, or <see cref="Time"/>) have changed. /// </remarks> public void Update() { _epoch2000Centuries = ToEpoch2000Centuries(Time, true); _epoch1990Days = ToEpoch1990Days(Time, false); // To transform from ecliptic to equatorial, we rotate by the obliquity of the ecliptic. _e = 0.409093 - 0.000227 * _epoch2000Centuries; EclipticToEquatorial = Matrix33D.CreateRotationX(_e); // GMST = Greenwich mean sidereal time (mittlere Greenwich-Sternzeit) in radians. double gmst = 4.894961 + 230121.675315 * ToEpoch2000Centuries(Time, false); EquatorialToGeographic = Matrix33D.CreateRotationZ(-gmst); // The earth axis slowly changes over time (precession). The precession movement repeats // itself approx. all 26000 years. When we move from to horizontal or geographics, // we need to apply the precession. // In Game Engine Gems: //var Rx = Matrix33D.CreateRotationX(0.1118 * _epoch2000Centuries); //var Ry = Matrix33D.CreateRotationY(-0.00972 * _epoch2000Centuries); //var Rz = Matrix33D.CreateRotationZ(0.01118 * _epoch2000Centuries); //var precession = Rz * (Ry * Rx); // In original article: var Ry = Matrix33D.CreateRotationY(-0.00972 * _epoch2000Centuries); var Rz = Matrix33D.CreateRotationZ(0.01118 * _epoch2000Centuries); var precession = Rz * Ry * Rz; // In game engine gems precession is applied in EclipticToWorld and in // EquatorialToWorld. This makes no sense since precession cannot be valid for both // coordinate systems. --> We assume the precession is given in equatorial space. //EclipticToWorld = rLat * rLong * EclipticToEquatorial * precession; // Latitude rotation var rLat = Matrix33D.CreateRotationY(MathHelper.ToRadians(Latitude) - ConstantsD.PiOver2); // Longitude rotation // LMST = Local mean sidereal time (mittlere Ortssternzeit) in radians. double lmst = gmst + MathHelper.ToRadians(Longitude); var rLong = Matrix33D.CreateRotationZ(-lmst); // Earth radius at the equator. (We assume a perfect sphere. We do not support geodetic // systems with imperfect earth spheres.) const double earthRadius = 6378.137 * 1000; var equatorialToHorizontalTranslation = new Vector3D(0, -earthRadius - Altitude, 0); // Switching of the coordinate axes between Equatorial (z up) and Horizontal (y up). var axisSwitch = new Matrix33D(0, 1, 0, 0, 0, 1, 1, 0, 0); EquatorialToWorld = new Matrix44D(axisSwitch * rLat * rLong * precession, equatorialToHorizontalTranslation); _equatorialToWorldNoPrecession = new Matrix44D(axisSwitch * rLat * rLong, equatorialToHorizontalTranslation); //WorldToGeographic = EquatorialToGeographic * EquatorialToWorld.Minor.Transposed; ComputeSunPosition(); ComputeMoonPosition(); ComputeEarthPosition(); //for (int i = 0; i < NumberOfPlanets; i++) //{ // var planet = (VisiblePlanets)i; // if (planet != VisiblePlanets.Earth) // ComputePlanetData(planet); //} }
public void EqualityOperators() { Matrix33D m1 = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); Matrix33D m2 = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); Assert.IsTrue(m1 == m2); Assert.IsFalse(m1 != m2); for (int i = 0; i < 9; i++) { m2 = new Matrix33D(columnMajor, MatrixOrder.ColumnMajor); m2[i] += 0.1; Assert.IsFalse(m1 == m2); Assert.IsTrue(m1 != m2); } }