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); }
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> /// 设置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> /// 设置参数数据 /// </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> /// 将32位单精度浮点数写入当前位置 /// </summary> /// <param name="data">要写入的32位单精度浮点数</param> internal void Write(Single data) { Byte[] temp = C3DBitConverter.GetBytes(data); this.Write(temp); }