/// <summary> /// Serialize and Deserialize a Vector3 and a Quarternion for n-times by use /// of Buffer.BlockCopy. /// </summary> /// <param name="v">The vector to serialize</param> /// <param name="q">The quarternion to serialize</param> public static void useBufferBlockCopy(int n, Vector3 v3, Quarternion q4) { var FLOAT_SIZE = sizeof(float); var source = new[] { v3.x, v3.y, v3.z, q4.w, q4.x, q4.y, q4.z }; for (int i = 0; i < n; i++) { // create a byte array and copy the floats into it... var byteArray = new byte[source.Length * FLOAT_SIZE]; Buffer.BlockCopy(source, 0, byteArray, 0, byteArray.Length); // create a second float array and copy the bytes into it... var floatArray2 = new float[byteArray.Length / 4]; Buffer.BlockCopy(byteArray, 0, floatArray2, 0, byteArray.Length); Vector3 deserializedVector = new Vector3() { x = floatArray2[0], y = floatArray2[1], z = floatArray2[2], }; Quarternion deserializedQuaternion = new Quarternion { w = floatArray2[3], x = floatArray2[4], y = floatArray2[5], z = floatArray2[6] }; } }
public static Quarternion GetRandomQuaternion() { var q4 = new Quarternion { w = GetRandomFloat(), x = GetRandomFloat(), y = GetRandomFloat(), z = GetRandomFloat() }; return(q4); }
/// <summary> /// Serialize and Deserialize a Vector3 and a Quarternion for n-times by use /// of MemoryStream and BinaryFormatter. /// </summary> /// <param name="v">The vector to serialize</param> /// <param name="q">The quarternion to serialize</param> public static void useBinaryFormatterWithFloatArray(int n, Vector3 v3, Quarternion q4) { for (int i = 0; i < n; i++) { var source = new[] { v3.x, v3.y, v3.z, q4.w, q4.x, q4.y, q4.z }; // serialize ArrayList MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); try { formatter.Serialize(stream, source); } catch (SerializationException e) { Console.WriteLine("Serialization Failed : " + e.Message); } byte[] objectAsBytes = stream.ToArray(); stream.Close(); // deserialize ArrayList stream = new MemoryStream(); stream.Write(objectAsBytes, 0, objectAsBytes.Length); stream.Seek(0, SeekOrigin.Begin); formatter = new BinaryFormatter(); try { float[] objectThatWasDeserialized = (float[])formatter.Deserialize(stream); Vector3 deserializedVector = new Vector3() { x = objectThatWasDeserialized[0], y = objectThatWasDeserialized[1], z = objectThatWasDeserialized[2], }; Quarternion deserializedQuaternion = new Quarternion { w = objectThatWasDeserialized[3], x = objectThatWasDeserialized[4], y = objectThatWasDeserialized[5], z = objectThatWasDeserialized[6] }; } catch (SerializationException e) { Console.WriteLine("Deserialization Failed : " + e.Message); } stream.Close(); } }
/// <summary> /// Serialize and Deserialize a Vector3 and a Quarternion for n-times by use /// of MemoryStream and BinaryFormatter. /// </summary> /// <param name="v">The vector to serialize</param> /// <param name="q">The quarternion to serialize</param> public static void useBinaryFormatter(int n, Vector3 v3, Quarternion q4) { for (int i = 0; i < n; i++) { ArrayList list = new ArrayList() { v3, q4 }; // serialize ArrayList MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); try { formatter.Serialize(stream, list); } catch (SerializationException e) { Console.WriteLine("Serialization Failed : " + e.Message); } byte[] objectAsBytes = stream.ToArray(); stream.Close(); // deserialize ArrayList stream = new MemoryStream(); stream.Write(objectAsBytes, 0, objectAsBytes.Length); stream.Seek(0, SeekOrigin.Begin); formatter = new BinaryFormatter(); try { var objectThatWasDeserialized = formatter.Deserialize(stream); } catch (SerializationException e) { Console.WriteLine("Deserialization Failed : " + e.Message); } stream.Close(); } }