public ExpandableMatrix(IntVector3 size, int growExtraSpaces = 0) { this._growExtraSpaces = growExtraSpaces; size = new IntVector3( Mathf.Max(0, size.x), Mathf.Max(0, size.y), Mathf.Max(0, size.z) ); this._positives = new T[size.x, size.y, size.z]; this._negativesX = new T[0, 0, 0]; this._negativesY = new T[0, 0, 0]; this._negativesZ = new T[0, 0, 0]; this._negativesXY = new T[0, 0, 0]; this._negativesXZ = new T[0, 0, 0]; this._negativesYZ = new T[0, 0, 0]; this._negativesXYZ = new T[0, 0, 0]; }
public static T[, ,] Fill <T>(this T[, ,] mat, T item) { if (mat.Length == 0) { return(mat); } for (int i = 0; i < mat.GetLength(0); i++) { for (int j = 0; j < mat.GetLength(1); j++) { for (int k = 0; k < mat.GetLength(2); k++) { mat[i, j, k] = item; } } } return(mat); }
public IEnumerator Upload <T>(T[,,] param) { CheckWWWUsage(); // Get our data into a byte array using Memory Writer. using (ES2Writer writer = ES2Writer.Create(settings)) { writer.Write <T>(param, settings.filenameData.tag); writer.Save(); using (var webRequest = UnityWebRequest.Post(settings.filenameData.filePath, CreateUploadForm(writer.stream.ReadAllBytes()))) { yield return(SendWebRequest(webRequest)); getError(); } isDone = true; } }
public Cube(T[] faces) { if (faces.Length != 6) { throw new Exception("The array faces should have 6 elements"); } _cube = new T[3, 3, 6]; for (byte i = 0; i < 3; i++) { for (byte j = 0; j < 3; j++) { for (byte k = 0; k < 6; k++) { _cube[i, j, k] = faces[k]; } } } }
public static T[][][] ConvertDimensional <T>(T[,,] In) { int Length1 = In.GetLength(0); int Length2 = In.GetLength(1); int Length3 = In.GetLength(2); var Out = Create <T>(Length1, Length2, Length3); for (int a = 0; a < Length1; a++) { for (int b = 0; b < Length2; b++) { for (int c = 0; c < Length3; c++) { Out[a][b][c] = In[a, b, c]; } } } return(Out); }
private static T CreateElement <T>(FactoryLocalizable <T> factory, T[, ,] GridArray, int column, int row, T currentElement, int level) where T : IConnectedGridElement3D, ILocalizableGridElement3D { currentElement = factory(column, row, level); GridArray[column, row, level] = currentElement; if (column > 0) { currentElement.Connect(GridArray[column - 1, row, level], Direction.Left); } if (row > 0) { currentElement.Connect(GridArray[column, row - 1, level], Direction.Back); } if (level > 0) { currentElement.Connect(GridArray[column, row, level - 1], Direction.Down); } return(currentElement); }
/// <summary> /// 加载地图 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="func">更具输入数据转化为NodeType</param> /// <param name="arr">二维数组</param> /// <param name="walkSideways">是否可以斜着走</param> public void ReadMap <T>(Func <T, AStarNodeType> func, T[,,] arr, bool walkSideways = true) { this.walkSideways = walkSideways; map = new AStarNode3X[arr.GetLength(0), arr.GetLength(1), arr.GetLength(2)]; size = new Point3(map.GetLength(1), map.GetLength(0), map.GetLength(2)); for (int i = 0; i < size.x; i++) { for (int j = 0; j < size.y; j++) { for (int k = 0; k < size.z; k++) { map[i, j, k] = new AStarNode3X(new Point3(i, j, k), func(arr[i, j, k])); } } } }
protected override void Write(ContentWriter output, DirectionalLookupTableF <T> value) { dynamic internals = value.Internals; int width = internals.Width; T[,,] cubeMap = internals.CubeMap; output.Write(width); for (int face = 0; face < 6; face++) { for (int y = 0; y < width; y++) { for (int x = 0; x < width; x++) { output.WriteRawObject(cubeMap[face, y, x]); } } } }
/// <summary> /// 获取一个二维数组的某一部分并返回 /// </summary> /// <param name="array">目标数组</param> /// <param name="base_0">第一维的起始索引</param> /// <param name="base_1">第二维的起始索引</param> /// <param name="base_2">第三维的起始索引</param> /// <param name="length_0">第一维要获取的数据长度</param> /// <param name="length_1">第二维要获取的数据长度</param> /// <param name="length_2">第三维要获取的数据长度</param> /// <returns></returns> public static T[,,] GetPart <T>(this T[,,] array, int base_0, int base_1, int base_2, int length_0, int length_1, int length_2) { if (base_0 + length_0 > array.GetLength(0) || base_1 + length_1 > array.GetLength(1) || base_2 + length_2 > array.GetLength(2)) { throw new System.Exception("索引超出范围"); } T[,,] ret = new T[length_0, length_1, length_2]; for (int i = 0; i < length_0; i++) { for (int j = 0; j < length_1; j++) { for (int k = 0; k < length_2; k++) { ret[i, j, k] = array[i + base_0, j + base_1, k + base_2]; } } } return(ret); }
/// <summary> /// Initializes a new instance of the <see cref="Memory2D{T}"/> struct wrapping a layer in a 3D array. /// </summary> /// <param name="array">The given 3D array to wrap.</param> /// <param name="depth">The target layer to map within <paramref name="array"/>.</param> /// <param name="row">The target row to map within <paramref name="array"/>.</param> /// <param name="column">The target column to map within <paramref name="array"/>.</param> /// <param name="height">The height to map within <paramref name="array"/>.</param> /// <param name="width">The width to map within <paramref name="array"/>.</param> /// <exception cref="ArrayTypeMismatchException"> /// Thrown when <paramref name="array"/> doesn't match <typeparamref name="T"/>. /// </exception> /// <exception cref="ArgumentOutOfRangeException">Thrown when a parameter is invalid.</exception> public Memory2D(T[,,] array, int depth, int row, int column, int height, int width) { if (array.IsCovariant()) { ThrowHelper.ThrowArrayTypeMismatchException(); } if ((uint)depth >= (uint)array.GetLength(0)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForDepth(); } int rows = array.GetLength(1), columns = array.GetLength(2); if ((uint)row >= (uint)rows) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow(); } if ((uint)column >= (uint)columns) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForColumn(); } if ((uint)height > (uint)(rows - row)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForHeight(); } if ((uint)width > (uint)(columns - column)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForWidth(); } this.instance = array; this.offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, row, column)); this.height = height; this.width = width; this.pitch = columns - width; }
/// <summary> /// Finds the biggest value in a 3 dimensional array. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="matrix"></param> /// <param name="subTopLeftXY">Enclosing box top left corner {z, y, x}</param> /// <param name="subBotRightXY">Enclosing box bottom right corner {z, y , x} (botRight z = topLeft z)</param> /// <param name="subDepth">Enclosing box depth {z}</param> /// <returns>{z, y, x} index of the biggest value found in the matrix</returns> public static int[] Biggest <T>(this T[, ,] matrix, int[] subTopLeftXY = null, int[] subBotRightXY = null, int subDepth = 0) where T : IComparable { int maxZ, maxY, maxX; if (subTopLeftXY == null || subTopLeftXY[0] != subBotRightXY[0]) { maxX = matrix.GetLength(2); maxY = matrix.GetLength(1); maxZ = matrix.GetLength(0); subTopLeftXY = subTopLeftXY ?? new int[] { 0, 0, 0 }; subBotRightXY = subBotRightXY ?? new int[] { maxZ, maxY, maxX }; } else { maxX = subBotRightXY[2] - subTopLeftXY[2] + 1; maxY = subBotRightXY[1] - subTopLeftXY[1] + 1; maxZ = subDepth + subTopLeftXY[0] + 1; } T biggest = default(T); int[] biggestIndex = new int[3]; bool biggestIsDefault = true; for (int z = subTopLeftXY[0], y = subTopLeftXY[1], x = subTopLeftXY[2]; z < maxZ; z++, y = 0) { for (; y < maxY; y++, x = 0) { for (; x < maxX; x++) { if (matrix[z, y, x].CompareTo(biggest) > 0 || biggestIsDefault) { biggest = matrix[z, y, x]; biggestIndex[0] = z; biggestIndex[1] = y; biggestIndex[2] = x; biggestIsDefault = false; } } } } return(biggestIndex); }
/// <summary> /// Creates an <see cref="Enumerator"/> for the specified rank 3 array. /// </summary> public Enumerator(T[,,] array) { this.source = array; if (array == null || array.Length == 0) // <-- The Length check is important! (Because "... = new T[42,0,0];" is legal.) { this.lenx = 0; this.leny = 0; this.lenz = 0; } else { this.lenx = array.GetLength(0); this.leny = array.GetLength(1); this.lenz = array.GetLength(2); } this.x = 0; this.y = 0; this.z = 0; this.current = default(T); }
static public T[, ] GetPlane <T>(this T[, , ] array, Direction constIndex, int planeIndex) { switch (constIndex) { case Direction.x: return(array.GetXPlane(planeIndex)); case Direction.y: return(array.GetYPlane(planeIndex)); case Direction.z: return(array.GetZPlane(planeIndex)); case Direction.zero: throw new ArgumentException("Direction.zero is not a proper plane's const index."); default: throw new ArgumentException("Not known value."); } }
/// <summary> /// Crop volume to selected length. /// </summary> /// <typeparam name="T">Data type can be selected by user.</typeparam> /// <param name="volume">Volume to be cropped.</param> /// <param name="dims">Cropping dimensions. Format: x1, x2, y1, y2, z1, z2.</param> /// <returns>Cropped volume.</returns> public static T[,,] Crop3D <T>(T[,,] volume, int[] dims) { if (dims.Length != 6) { throw new Exception("Invalid number of dimensions on dims variable. Include 6 dimensions."); } T[,,] croppedVolume = new T[dims[1] - dims[0] + 1, dims[3] - dims[2] + 1, dims[5] - dims[4] + 1]; Parallel.For(dims[0], dims[1] + 1, x => { Parallel.For(dims[2], dims[3] + 1, y => { Parallel.For(dims[4], dims[5] + 1, z => { croppedVolume[x - dims[0], y - dims[2], z - dims[4]] = volume[x, y, z]; }); }); }); return(croppedVolume); }
public static T[] FlattenArray3D <T>(T[,,] data) { int rows0 = data.GetLength(0); int rows1 = data.GetLength(1); int rows2 = data.GetLength(2); T[] flattened = new T[rows0 * rows1 * rows2]; for (int i = 0; i < rows0; i++) { for (int j = 0; j < rows1; j++) { for (int k = 0; k < rows2; k++) { flattened[i + rows0 * (j + rows1 * k)] = data[i, j, k]; } } } return(flattened); }
public static T[] Flatten <T>(this T[,,] threeD) { var dimensions = new Vector3Int(threeD.GetLength(0), threeD.GetLength(1), threeD.GetLength(2)); var result = new T[threeD.Length]; int i = 0; for (int z = 0; z < dimensions.z; z++) { for (int y = 0; y < dimensions.y; y++) { for (int x = 0; x < dimensions.x; x++) { result[i] = threeD[x, y, z]; i++; } } } return(result); }
/// <summary> /// Up- or down-samples the given 3D array, /// assuming that the change in size isn't more than 2x along each axis. /// </summary> public static T[,,] Resample <T>(this T[,,] original, Func <T, T, float, T> lerp, int newSizeX, int newSizeY, int newSizeZ) { T[,,] newVals = new T[newSizeX, newSizeY, newSizeZ]; Vector3 scale = new Vector3(1.0f / newSizeX, 1.0f / newSizeY, 1.0f / newSizeZ); for (int z = 0; z < newSizeZ; ++z) { for (int y = 0; y < newSizeY; ++y) { for (int x = 0; x < newSizeX; ++x) { Vector3 uv = new Vector3(x * scale.x, y * scale.y, z * scale.z); newVals[x, y, z] = original.Sample(uv, lerp, false); } } } return(newVals); }
/// <summary> /// Displays 3D array. /// </summary> /// <param name="array">3D array</param> public static void Display3DArray <T>(T[,,] array) { Console.Write("{"); for (int i = 0; i < array.GetLength(0); i++) { Console.Write("{"); for (int j = 0; j < array.GetLength(1); j++) { Console.Write("{"); for (int k = 0; k < array.GetLength(2) - 1; k++) { Console.Write("{0} ", array[i, j, k]); } Console.Write("{0}", array[i, j, array.GetLength(2) - 1]); Console.Write("}"); } Console.Write("}"); } Console.WriteLine("}"); }
private void ResizeArray(ref T[, ,] array, int xLength, int yLength, int zLength) { var newArray = new T[xLength, yLength, zLength]; int minX = Math.Min(xLength, array.GetLength(0)); int minY = Math.Min(yLength, array.GetLength(1)); int minZ = Math.Min(zLength, array.GetLength(2)); for (int i = 0; i < minX; i++) { for (int j = 0; j < minY; j++) { for (int k = 0; k < minZ; k++) { newArray[i, j, k] = array[i, j, k]; } } } array = newArray; }
private void ResizeGrid(Int3 _newSize) { T[,,] newGrid = new T[_newSize.x, _newSize.y, _newSize.z]; // Copy over data from previous grid. for (int xi = 0; xi < grid.GetLength(0); xi++) { for (int yi = 0; yi < grid.GetLength(1); yi++) { for (int zi = 0; zi < grid.GetLength(2); zi++) { newGrid[xi, yi, zi] = grid[xi, yi, zi]; } } } grid = newGrid; return; }
public static T[,,] InsertInto <T>(T[,,] target, T[,,] source, int insertAt, int size) { int zActual = 0; int xActual = 0; for (int z = insertAt; z < insertAt + size; z++) { xActual = 0; for (int x = insertAt; x < insertAt + size; x++) { for (int c = 0; c < target.GetLength(2); c++) { target[z, x, c] = source[zActual, xActual, c]; } xActual++; } zActual++; } return(target); }
public static Int3?IndexOf <T>(this T[,,] array, T item) where T : IEquatable <T> { Int3 size = array.Size(); for (int x = 0; x < size.x; x++) { for (int y = 0; y < size.y; y++) { for (int z = 0; z < size.z; z++) { if (array[x, y, z].Equals(item)) { return(new Int3(x, y, z)); } } } } return(null); }
/// <summary> /// 加载地图 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="func">更具输入数据转化为NodeType</param> /// <param name="arr">二维数组</param> /// <param name="walkSideways">是否可以斜着走</param> public void ReadMap <T>(Func <T, AStarNodeType> func, T[,,] arr, bool walkSideways = true) { this.walkSideways = walkSideways; map = new AStarNode3X[arr.GetLength(0), arr.GetLength(1), arr.GetLength(2)]; this.wid = map.GetLength(1); this.len = map.GetLength(0); this.hei = map.GetLength(2); for (int i = 0; i < len; i++) { for (int j = 0; j < wid; j++) { for (int k = 0; k < hei; k++) { map[i, j, k] = new AStarNode3X(i, j, k, func(arr[i, j, k])); } } } }
/// <summary> /// Resizes an array. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="newWidth">The new width of the array.</param> /// <param name="newHeight">The new height of the array.</param> /// <param name="newDepth">The new depth of the array.</param> /// <param name="paddingElement">The padding element used to fill up out of bounds locations.</param> /// <returns></returns> public static T[,,] Resize <T>(this T[,,] source, int newWidth, int newHeight, int newDepth, T paddingElement = default(T)) { if (source == null) { throw new ArgumentNullException("source"); } if (newWidth <= 0) { throw new ArgumentOutOfRangeException("newWidth"); } if (newHeight <= 0) { throw new ArgumentOutOfRangeException("newHeight"); } if (newDepth <= 0) { throw new ArgumentOutOfRangeException("newDepth"); } int width = source.GetLength(0), height = source.GetLength(1), depth = source.GetLength(2); T[,,] result = new T[newWidth, newHeight, newDepth]; for (int x = 0; x < newWidth; x++) { for (int y = 0; y < newHeight; y++) { for (int z = 0; z < newDepth; z++) { result[x, y, z] = (x < width && y < height && z < depth) ? source[x, y, z] : paddingElement; } } } return(result); }
public static List <T> ToList <T>(this T[,,] mat) { if (mat == null || mat.Length == 0) { return(new List <T>()); } int xs = mat.GetLength(0), ys = mat.GetLength(1), zs = mat.GetLength(2); List <T> l = new List <T>(xs * ys * zs); for (int k = 0; k < zs; k++) { for (int j = 0; j < ys; j++) { for (int i = 0; i < xs; i++) { l.Add(mat[i, j, k]); } } } return(l); }
public void Resize(int newSize) { if (this.size < newSize) { var newArray = new T[newSize, newSize, newSize]; var xMin = Mathf.Min(newSize, data.GetLength(0)); var yMin = Mathf.Min(newSize, data.GetLength(1)); var zMin = Mathf.Min(newSize, data.GetLength(2)); for (var x = 0; x < xMin; x++) { for (var y = 0; y < yMin; y++) { for (var z = 0; z < zMin; z++) { newArray[x, y, z] = data[x, y, z]; } } } data = newArray; } }
/// <summary> /// Initializes a new instance of the <see cref="Span2D{T}"/> struct wrapping a layer in a 3D array. /// </summary> /// <param name="array">The given 3D array to wrap.</param> /// <param name="depth">The target layer to map within <paramref name="array"/>.</param> /// <exception cref="ArrayTypeMismatchException"> /// Thrown when <paramref name="array"/> doesn't match <typeparamref name="T"/>. /// </exception> /// <exception cref="ArgumentOutOfRangeException">Thrown when a parameter is invalid.</exception> public Span2D(T[,,] array, int depth) { if (array.IsCovariant()) { ThrowHelper.ThrowArrayTypeMismatchException(); } if ((uint)depth >= (uint)array.GetLength(0)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForDepth(); } #if SPAN_RUNTIME_SUPPORT this.span = MemoryMarshal.CreateSpan(ref array.DangerousGetReferenceAt(depth, 0, 0), array.GetLength(1)); #else this.Instance = array; this.Offset = array.DangerousGetObjectDataByteOffset(ref array.DangerousGetReferenceAt(depth, 0, 0)); this.height = array.GetLength(1); #endif this.width = this.Stride = array.GetLength(2); }
public static T[,,] Invert <T>(T[,,] array) { int zLength = array.GetLength(0); int xLength = array.GetLength(1); int channels = array.GetLength(2); T[,,] tempArray = new T[zLength, xLength, channels]; for (int z = 0; z < zLength; z++) { for (int x = 0; x < xLength; x++) { for (int c = 0; c < channels; c++) { tempArray[z, x, c] = array[z, zLength - x - 1, c]; } } } return(array = tempArray); }
static public int[] FindIndex <T>(this T[, , ] array, Func <T, int, int, int, bool> predicate) { bool breakFlag = false; int[] indexes = null; for (int i = 0; i < array.GetLength(0) && !breakFlag; i++) { for (int j = 0; j < array.GetLength(1) && !breakFlag; j++) { for (int k = 0; k < array.GetLength(2) && !breakFlag; k++) { if (predicate(array[i, j, k], i, j, k)) { indexes = new [] { i, j, k }; breakFlag = true; } } } } return(indexes); }
public static T[][][] Convert <T>(this T[,,] array) { var h = array.GetLength(0); var w = array.GetLength(1); var d = array.GetLength(2); T[][][] map = new T[h][][]; for (int y = 0; h > y; y++) { map[y] = new T[w][]; for (int x = 0; w > x; x++) { for (int z = 0; d > z; z++) { map[y][x][z] = array[y, x, z]; } } } return(map); }