private void Set3DArray <T>(T[, ,] array, C3DParameterType type) where T : IConvertible { Int32 size = this.GetParameterDataSize(type); this.SetDimension((Byte)array.GetLength(0), (Byte)array.GetLength(1), (Byte)array.GetLength(2)); this._parameterType = type; this._parameterData = new Byte[array.Length * size]; Int32 index = 0; for (Int32 x = 0; x < array.GetLength(0); x++) { for (Int32 y = 0; y < array.GetLength(1); y++) { for (Int32 z = 0; z < array.GetLength(2); z++) { if (size == 1) { this._parameterData[index++] = array[x, y, z].ToByte(null); } else { Array.Copy(C3DBitConverter.GetBytes <T>(array[x, y, z]), 0, this._parameterData, index++ *size, size); } } } } }
/// <summary> /// 初始化C3D 3D坐标点结构体 /// </summary> /// <param name="x">X轴坐标点</param> /// <param name="y">Y轴坐标点</param> /// <param name="z">Z轴坐标点</param> /// <param name="lastPart">剩余部分</param> /// <param name="scaleFactor">比例因子</param> public C3DPoint3DData(Int16 x, Int16 y, Int16 z, Int16 lastPart, Single scaleFactor) { Byte[] data = C3DBitConverter.GetBytes(lastPart); this._x = x * scaleFactor; this._y = y * scaleFactor; this._z = z * scaleFactor; this._residual = (lastPart > -1 ? Math.Abs((SByte)data[0] * scaleFactor) : -1.0F); this._cameraMask = (this._residual > -1.0F ? (Byte)(data[1] & 0x7F) : (Byte)0); }
/// <summary> /// 初始化C3D 3D坐标点结构体 /// </summary> /// <param name="x">X轴坐标点</param> /// <param name="y">Y轴坐标点</param> /// <param name="z">Z轴坐标点</param> /// <param name="lastPart">剩余部分</param> /// <param name="scaleFactor">比例因子</param> public C3DPoint3DData(Single x, Single y, Single z, Single lastPart, Single scaleFactor) { Byte[] data = C3DBitConverter.GetBytes((Int16)lastPart); this._x = x; this._y = y; this._z = z; this._residual = ((Int16)lastPart > -1 ? Math.Abs((SByte)data[0] * scaleFactor) : -1.0F); this._cameraMask = (this._residual > -1.0F ? (Byte)(data[1] & 0x7F) : (Byte)0); }
/// <summary> /// 获取整型剩余部分 /// </summary> /// <param name="scaleFactor">比例因子</param> /// <returns>剩余部分</returns> internal Int16 GetIntLastPart(Single scaleFactor) { if (this._residual == -1.0F) { return(-1);//0xFFFF } else { Byte[] data = new Byte[2]; data[0] = (Byte)Math.Round(Math.Abs(this._residual / scaleFactor), MidpointRounding.AwayFromZero); data[1] = (Byte)(this._cameraMask); return(C3DBitConverter.ToInt16(data)); } }
private void Set1DArray <T>(T[] array, C3DParameterType type) where T : IConvertible { Int32 size = this.GetParameterDataSize(type); this.SetDimension((Byte)array.Length); this._parameterType = type; this._parameterData = new Byte[array.Length * size]; for (Int32 i = 0; i < array.Length; i++) { if (size == 1) { this._parameterData[i] = array[i].ToByte(null); } else { Array.Copy(C3DBitConverter.GetBytes <T>(array[i]), 0, this._parameterData, i * size, size); } } }
/// <summary> /// 设置32位单精度浮点数记录 /// </summary> /// <param name="index">记录索引(从1开始计数,2字节单位)</param> /// <param name="value">记录内容</param> private void SetSingleRecord(Int16 index, Single value) { Array.Copy(C3DBitConverter.GetBytes(value), 0, this._data, (index - 1) * 2, sizeof(Single)); }
/// <summary> /// 获取32位单精度浮点数记录 /// </summary> /// <param name="index">记录索引(从1开始计数,2字节单位)</param> /// <returns>记录内容</returns> private Single GetSingleRecord(Int16 index) { return(C3DBitConverter.ToSingle(this._data, (index - 1) * 2)); }
/// <summary> /// 设置16位整数记录 /// </summary> /// <param name="index">记录索引(从1开始计数,2字节单位)</param> /// <param name="value">记录内容</param> private void SetInt16Record(Int16 index, Int16 value) { Array.Copy(C3DBitConverter.GetBytes(value), 0, this._data, (index - 1) * 2, sizeof(Int16)); }
/// <summary> /// 获取16位整数记录 /// </summary> /// <param name="index">记录索引(从1开始计数,2字节单位)</param> /// <returns>记录内容</returns> private Int16 GetInt16Record(Int16 index) { return(C3DBitConverter.ToInt16(this._data, (index - 1) * 2)); }
/// <summary> /// 从当前流中读取下一个32位单精度浮点数 /// </summary> /// <returns>下一个32位单精度浮点数</returns> internal Single ReadSingle() { Byte[] data = this.ReadBytes(4); return(C3DBitConverter.ToSingle(this._processorType, data)); }
/// <summary> /// 更新参数内数据 /// </summary> /// <param name="parameterType">参数类型</param> /// <param name="processorType">处理器类型</param> /// <param name="dimensionCount">维数</param> private void UpdateData(C3DParameterType parameterType, C3DProcessorType processorType, Int32 dimensionCount) { #region Basic Type if (dimensionCount == 0) { if (parameterType == C3DParameterType.Int16) { this.InternalSetData <Int16>(C3DBitConverter.ToInt16(processorType, this._parameterData)); } else if (parameterType == C3DParameterType.Single) { this.InternalSetData <Single>(C3DBitConverter.ToSingle(processorType, this._parameterData)); } return; } #endregion #region 1D-Array if (dimensionCount == 1) { if (parameterType == C3DParameterType.Int16) { Byte[][] datas = this.Get1DByteArray(sizeof(Int16)); Int16[] newdata = new Int16[datas.Length]; for (Int32 i = 0; i < datas.Length; i++) { newdata[i] = C3DBitConverter.ToInt16(processorType, datas[i]); } this.InternalSetData <Int16[]>(newdata); } else if (parameterType == C3DParameterType.Single) { Byte[][] datas = this.Get1DByteArray(sizeof(Single)); Single[] newdata = new Single[datas.Length]; for (Int32 i = 0; i < datas.Length; i++) { newdata[i] = C3DBitConverter.ToSingle(processorType, datas[i]); } this.InternalSetData <Single[]>(newdata); } return; } #endregion #region 2D-Array if (dimensionCount == 2) { if (parameterType == C3DParameterType.Int16) { Byte[, ][] datas = this.Get2DByteArray(sizeof(Int16)); Int16[,] newdata = new Int16[datas.GetLength(0), datas.GetLength(1)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { newdata[x, y] = C3DBitConverter.ToInt16(processorType, datas[x, y]); } } this.InternalSetData <Int16[, ]>(newdata); } else if (parameterType == C3DParameterType.Single) { Byte[, ][] datas = this.Get2DByteArray(sizeof(Single)); Single[,] newdata = new Single[datas.GetLength(0), datas.GetLength(1)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { newdata[x, y] = C3DBitConverter.ToSingle(processorType, datas[x, y]); } } this.InternalSetData <Single[, ]>(newdata); } return; } #endregion #region 3D-Array if (dimensionCount == 3) { if (parameterType == C3DParameterType.Int16) { Byte[, , ][] datas = this.Get3DByteArray(sizeof(Int16)); Int16[, ,] newdata = new Int16[datas.GetLength(0), datas.GetLength(1), datas.GetLength(2)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { for (Int32 z = 0; z < datas.GetLength(2); z++) { newdata[x, y, z] = C3DBitConverter.ToInt16(processorType, datas[x, y, z]); } } } this.InternalSetData <Int16[, , ]>(newdata); } else if (parameterType == C3DParameterType.Single) { Byte[, , ][] datas = this.Get3DByteArray(sizeof(Single)); Single[, ,] newdata = new Single[datas.GetLength(0), datas.GetLength(1), datas.GetLength(2)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { for (Int32 z = 0; z < datas.GetLength(2); z++) { newdata[x, y, z] = C3DBitConverter.ToSingle(processorType, datas[x, y, z]); } } } this.InternalSetData <Single[, , ]>(newdata); } return; } #endregion }
/// <summary> /// 设置参数数据 /// </summary> /// <typeparam name="T">参数类型</typeparam> /// <param name="data">参数数据</param> /// <exception cref="C3DException">未知数据类型</exception> internal void InternalSetData <T>(T data) { #region Basic Type if (typeof(T) == typeof(Char)) { this.SetDimension(); this._parameterType = C3DParameterType.Char; this._parameterData = new Byte[1] { Convert.ToByte(data) }; return; } if (typeof(T) == typeof(Byte)) { this.SetDimension(); this._parameterType = C3DParameterType.Byte; this._parameterData = new Byte[1] { Convert.ToByte(data) }; return; } if (typeof(T) == typeof(Int16)) { this.SetDimension(); this._parameterType = C3DParameterType.Int16; this._parameterData = C3DBitConverter.GetBytes((Int16)(Object)data); return; } if (typeof(T) == typeof(UInt16)) { this.SetDimension(); this._parameterType = C3DParameterType.Int16; this._parameterData = C3DBitConverter.GetBytes((Int16)(UInt16)(Object)data); return; } if (typeof(T) == typeof(Single)) { this.SetDimension(); this._parameterType = C3DParameterType.Single; this._parameterData = C3DBitConverter.GetBytes((Single)(Object)data); return; } if (typeof(T) == typeof(String)) { String s = data as String; this.SetDimension((Byte)s.Length); this._parameterType = C3DParameterType.Char; this._parameterData = this.WrtieStringToBytes(s); return; } #endregion #region 1D-Array if (typeof(T) == typeof(Char[])) { this.Set1DArray <Char>(data as Char[], C3DParameterType.Char); return; } if (typeof(T) == typeof(Byte[])) { this.Set1DArray <Byte>(data as Byte[], C3DParameterType.Byte); return; } if (typeof(T) == typeof(Int16[])) { this.Set1DArray <Int16>(data as Int16[], C3DParameterType.Int16); return; } if (typeof(T) == typeof(UInt16[])) { this.Set1DArray <UInt16>(data as UInt16[], C3DParameterType.Int16); return; } if (typeof(T) == typeof(Single[])) { this.Set1DArray <Single>(data as Single[], C3DParameterType.Single); return; } if (typeof(T) == typeof(String[])) { this.Set1DStringArray(data as String[]); return; } #endregion #region 2D-Array if (typeof(T) == typeof(Char[, ])) { this.Set2DArray <Char>(data as Char[, ], C3DParameterType.Char); return; } if (typeof(T) == typeof(Byte[, ])) { this.Set2DArray <Byte>(data as Byte[, ], C3DParameterType.Byte); return; } if (typeof(T) == typeof(Int16[, ])) { this.Set2DArray <Int16>(data as Int16[, ], C3DParameterType.Int16); return; } if (typeof(T) == typeof(UInt16[, ])) { this.Set2DArray <UInt16>(data as UInt16[, ], C3DParameterType.Int16); return; } if (typeof(T) == typeof(Single[, ])) { this.Set2DArray <Single>(data as Single[, ], C3DParameterType.Single); return; } #endregion #region 3D-Array if (typeof(T) == typeof(Char[, , ])) { this.Set3DArray <Char>(data as Char[, , ], C3DParameterType.Char); return; } if (typeof(T) == typeof(Byte[, , ])) { this.Set3DArray <Byte>(data as Byte[, , ], C3DParameterType.Byte); return; } if (typeof(T) == typeof(Int16[, , ])) { this.Set3DArray <Int16>(data as Int16[, , ], C3DParameterType.Int16); return; } if (typeof(T) == typeof(UInt16[, , ])) { this.Set3DArray <UInt16>(data as UInt16[, , ], C3DParameterType.Int16); return; } if (typeof(T) == typeof(Single[, , ])) { this.Set3DArray <Single>(data as Single[, , ], C3DParameterType.Single); return; } #endregion throw new C3DException("Parameter type is unknown."); }
/// <summary> /// 获取指定索引的参数数据 /// </summary> /// <typeparam name="T">参数类型</typeparam> /// <param name="index">指定索引(从0开始计数)</param> /// <exception cref="C3DException">数据类型错误</exception> /// <returns>参数数据</returns> public T GetData <T>(Int32 index) { #region Base Type if (typeof(T) == typeof(Char)) { return((T)(Object)(Char)this._parameterData[index]); } if (typeof(T) == typeof(Byte)) { return((T)(Object)this._parameterData[index]); } if (typeof(T) == typeof(Int16)) { return((T)(Object)C3DBitConverter.ToInt16(this._parameterData, index * sizeof(Int16))); } if (typeof(T) == typeof(UInt16)) { return((T)(Object)(UInt16)C3DBitConverter.ToInt16(this._parameterData, index * sizeof(UInt16))); } if (typeof(T) == typeof(Single)) { return((T)(Object)C3DBitConverter.ToSingle(this._parameterData, index * sizeof(Single))); } if (typeof(T) == typeof(String)) { if (this._dimensions == null || this._dimensions.Length != 1 || this._parameterType != C3DParameterType.Char) { throw new C3DException("Parameter \"" + this.Name + "\" is not string type."); } return((T)(Object)this.ReadStringFromBytes(this._parameterData, 0, this._dimensions[0])); } #endregion #region 1D-Array if (typeof(T) == typeof(Char[])) { return((T)(Object)this.Get1DArray <Char>()); } if (typeof(T) == typeof(Byte[])) { return((T)(Object)this.Get1DArray <Byte>()); } if (typeof(T) == typeof(Int16[])) { return((T)(Object)this.Get1DArray <Int16>()); } if (typeof(T) == typeof(UInt16[])) { return((T)(Object)this.Get1DArray <UInt16>()); } if (typeof(T) == typeof(Single[])) { return((T)(Object)this.Get1DArray <Single>()); } if (typeof(T) == typeof(String[])) { if (this._dimensions == null || this._dimensions.Length != 2 || this._parameterType != C3DParameterType.Char) { throw new C3DException("Parameter \"" + Name + "\" is not string array type."); } String[] ret = new String[this._dimensions[1]]; for (Int32 i = 0; i < this._dimensions[1]; i++) { ret[i] = this.ReadStringFromBytes(this._parameterData, i * this._dimensions[0], this._dimensions[0]); } return((T)(Object)ret); } #endregion #region 2D-Array if (typeof(T) == typeof(Char[, ])) { return((T)(Object)this.Get2DArray <Char>()); } if (typeof(T) == typeof(Byte[, ])) { return((T)(Object)this.Get2DArray <Byte>()); } if (typeof(T) == typeof(Int16[, ])) { return((T)(Object)this.Get2DArray <Int16>()); } if (typeof(T) == typeof(UInt16[, ])) { return((T)(Object)this.Get2DArray <UInt16>()); } if (typeof(T) == typeof(Single[, ])) { return((T)(Object)this.Get2DArray <Single>()); } #endregion #region 3D-Array if (typeof(T) == typeof(Char[, , ])) { return((T)(Object)this.Get3DArray <Char>()); } if (typeof(T) == typeof(Byte[, , ])) { return((T)(Object)this.Get3DArray <Byte>()); } if (typeof(T) == typeof(Int16[, , ])) { return((T)(Object)this.Get3DArray <Int16>()); } if (typeof(T) == typeof(UInt16[, , ])) { return((T)(Object)this.Get3DArray <UInt16>()); } if (typeof(T) == typeof(Single[, , ])) { return((T)(Object)this.Get3DArray <Single>()); } #endregion throw new C3DException("Parameter type is unknown."); }
/// <summary> /// 将32位单精度浮点数写入当前位置 /// </summary> /// <param name="data">要写入的32位单精度浮点数</param> internal void Write(Single data) { Byte[] temp = C3DBitConverter.GetBytes(data); this.Write(temp); }
/// <summary> /// 更新16位整数记录 /// </summary> /// <param name="oldType">原处理器类型</param> /// <param name="index">记录索引(从1开始计数,2字节单位)</param> private void UpdateInt16Record(C3DProcessorType oldType, Int16 index) { Int16 value = C3DBitConverter.ToInt16(oldType, this._data, (index - 1) * 2); this.SetInt16Record(index, value); }
/// <summary> /// 更新32位单精度浮点数记录 /// </summary> /// <param name="oldType">原处理器类型</param> /// <param name="index">记录索引(从1开始计数,2字节单位)</param> private void UpdateSingleRecord(C3DProcessorType oldType, Int16 index) { Single value = C3DBitConverter.ToSingle(oldType, this._data, (index - 1) * 2); this.SetSingleRecord(index, value); }
/// <summary> /// 从当前流中读取下一个16位有符号整数 /// </summary> /// <returns>下一个16位有符号整数</returns> internal Int16 ReadInt16() { Byte[] data = this.ReadBytes(2); return(C3DBitConverter.ToInt16(this._processorType, data, 0)); }