TypeConversion of a string to bytearray - c#

I have a string value:
Example:
string msg = "array('B', [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0,
1, 0, 1, 0, 0, 255, 219, 0, 67, 0, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2,
2, 2, 2, 4, 3, 2, 2, 2, 2, 5, 4, 4, 3, 4, 6, 5, 6, 6, 6, 5, 6, 6, 6,
7, 9, 8, 6, 7, 9, 7, 6, 6, 8, 11, 8, 9, 10, 10, 10, 10, 10, 6, 8, 11,
12, 11, 10, 12, 9, 10, 10, 10, 255, 219, 0, 67, 1, 2, 2, 2, 2, 2, 2,
5, 3, 3, 5, 10, 7, 6, 7, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10])"
I want to convert this string value to type byte[].
private byte[] data;
//after type conversion from string to byte array
Debug.Log("data in byte array is: " + data);
Output should be:
data in byte array is: [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0,
1, 0, 1, 0, 0, 255, 219, 0, 67, 0, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2,
2, 2, 2, 4, 3, 2, 2, 2, 2, 5, 4, 4, 3, 4, 6, 5, 6, 6, 6, 5, 6, 6, 6,
7, 9, 8, 6, 7, 9, 7, 6, 6, 8, 11, 8, 9, 10, 10, 10, 10, 10, 6, 8, 11,
12, 11, 10, 12, 9, 10, 10, 10, 255, 219, 0, 67, 1, 2, 2, 2, 2, 2, 2,
5, 3, 3, 5, 10, 7, 6, 7, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
Since the actual string msg is a very large value, I want to do this with minimum execution time possible.

Looks like a fairly simple parsing problem. We could run up a full parser for the format, but that's probably well beyond your needs.
Let's assume instead that your msg is always going to be in the format shown, starting with "array('B', [", ending with "])" and having 0 or more comma-separated decimal values. Since any option you choose is going to scan the string at least once, we can design code around doing exactly that.
The actual content contains only 4 types of characters:
digit (0-9)
comma (,)
space ( )
end-array (])
We can iterate through the characters starting at the first character after the start-array ([ at offset 11) and do something with each of those character types: ignore it, update the current value, yield the current value and/or exit.
If all of the assumptions are correct then this is a simple process. One scan through the characters, no allocations in the parser itself. Here's a sample:
IEnumerable<byte> ParseArrayMsg(string msg)
{
if (!msg.StartsWith("array('B', [") || !msg.EndsWith("])"))
yield break;
int value = 0;
for (int i = msg.IndexOf('[') + 1; i < msg.Length; i++)
{
var c = msg[i];
if (c == ',' || c == ']')
{
yield return (byte)value;
value = 0;
if (c == ']')
break;
}
else if (char.IsDigit(c))
value = value * 10 + (int)(c - '0');
else if (c != ' ')
throw new Exception($"Invalid character '{c}' at index '{i}'.");
}
}
The returned enumerable can then be processed however you like.
Your code won't actually produce the output you want, but this will:
var data = ParseArrayMsg(msg).ToArray();
Console.WriteLine($"Data in byte array is: [{string.Join(", ", data.Select(b => b.ToString()))}]");
Data in byte array is: [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0,
0, 1, 0, 1, 0, 0, 255, 219, 0, 67, 0, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 2,
2, 4, 3, 2, 2, 2, 2, 5, 4, 4, 3, 4, 6, 5, 6, 6, 6, 5, 6, 6, 6, 7, 9, 8, 6, 7,
9, 7, 6, 6, 8, 11, 8, 9, 10, 10, 10, 10, 10, 6, 8, 11, 12, 11, 10, 12, 9, 10,
10, 10, 255, 219, 0, 67, 1, 2, 2, 2, 2, 2, 2, 5, 3, 3, 5, 10, 7, 6, 7, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10]

You can try like the following:
string yourString= "Your String";
// Convert a C# string to a byte array
byte[] bytes = Encoding.ASCII.GetBytes(yourString);

Related

Why is my marching cubes algorythm leaving holes behind?

I just made a marching cubes algorythm and after 2 days i saw some holes in the mesh.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MarchinCubes : MonoBehaviour
{
public int size = 1;
public float surfaceValue = .5f;
public int xOffset = 0;
public int yOffset = 0;
public int zOffset = 0;
public float zoom = 1;
public bool interpolation;
public bool shading;
public bool autoUpdate;
public bool sphereValues;
void Start()
{
xOffset = (int)transform.position.x;
yOffset = (int)transform.position.y;
zOffset = (int)transform.position.z;
Starter();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.U) || autoUpdate)
{
xOffset = (int)transform.position.x;
yOffset = (int)transform.position.y;
zOffset = (int)transform.position.z;
Starter();
Vector3 position = transform.position;
Vector3 newPosition = new Vector3(Mathf.Floor(position.x), Mathf.Floor(position.y), Mathf.Floor(position.z));
transform.position = newPosition;
}
}
void Starter()
{
int pointSize = size + 1;
float[][][] values = sphereValues ? sphere(pointSize) : Perlin(pointSize);
Mesh mesh = CreateMesh(values, pointSize - 1);
mesh.RecalculateNormals();
GetComponent<MeshFilter>().mesh = mesh;
}
float[][][] sphere(int size)
{
float[][][] values = initializeFloatArray(size);
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
for (int z = 0; z < size; z++)
{
values[x][y][z] = 10 - distance(x, y, z, size / 2, size / 2, size / 2);
}
}
}
return values;
}
float distance(float x, float y, float z, float xCenter, float yCenter, float zCenter)
{
return Mathf.Sqrt(Mathf.Pow(x - xCenter, 2) + Mathf.Pow(y - yCenter, 2) + Mathf.Pow(z - zCenter, 2));
}
float[][][] Perlin(int size)
{
float[][][] values = initializeFloatArray(size);
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
for (int z = 0; z < size; z++)
{
if (x == 0 || x == size - 1 || y == 0 || y == size - 1 ||z == 0 || z == size - 1)
{
values[x][y][z] = 0;
}
else
{
values[x][y][z] = (GetPerlin(x, y, z, 40f) + GetPerlin(x, y, z, 20f)) / 2;
}
}
}
}
return values;
}
float GetPerlin(int xInt, int yInt, int zInt, float divider)
{
float z = (zInt + zOffset) / divider;
float y = (yInt + yOffset) / divider;
float x = (xInt + xOffset) / divider;
float zy = Mathf.PerlinNoise(x, y);
float zx = Mathf.PerlinNoise(x, z);
float yx = Mathf.PerlinNoise(y, z);
float yz = Mathf.PerlinNoise(y, x);
float xz = Mathf.PerlinNoise(z, x);
float xy = Mathf.PerlinNoise(z, y);
return (xy + xz + yz + yx + zx + zy) / 6;
}
float[][][] initializeFloatArray(int size)
{
float[][][] values = new float[size][][];
for (int x = 0; x < size; x++)
{
values[x] = new float[size][];
for (int i = 0; i < size; i++)
{
values[x][i] = new float[size];
}
}
return values;
}
Mesh CreateMesh(float[][][] values, int size)
{
Vector3[] vertices = initializeVector3Array(size * 2 + 1, size * 2 + 1, size * 2 + 1);
int[] triangles = new int[size * size * size * 3 * 2];
Mesh mesh = new Mesh();
int triangleCount = 0;
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
for (int z = 0; z < size; z++)
{
int[] triangleTable = table[getTriangleIndex(
new[] {
values[x][y][z],
values[x + 1][y][z],
values[x + 1][y][z + 1],
values[x][y][z + 1],
values[x][y + 1][z],
values[x + 1][y + 1][z],
values[x + 1][y + 1][z + 1],
values[x][y + 1][z + 1]
},
surfaceValue)];
int[] trianglesToAdd = new int[triangleTable.Length];
for (int i = 0; i < triangleTable.Length; i += 3)
{
trianglesToAdd[i] = getVertexIndex(x, y, z, triangleTable[i], size * 2 + 2);
trianglesToAdd[i + 1] = getVertexIndex(x, y, z, triangleTable[i + 1], size * 2 + 2);
trianglesToAdd[i + 2] = getVertexIndex(x, y, z, triangleTable[i + 2], size * 2 + 2);
}
for (int i = 0; i < trianglesToAdd.Length; i++)
{
triangles[triangleCount] = trianglesToAdd[i];
triangleCount++;
}
}
}
}
if (interpolation)
{
vertices = addIndterpolation(vertices, size * 2 + 2, values, surfaceValue);
}
vertices = ScaleVertices(vertices);
if (shading == false)
{
RemakeMeshToDiscrete(vertices, triangles, out vertices, out triangles);
}
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
mesh.vertices = vertices;
mesh.triangles = triangles;
return mesh;
}
Vector3[] ScaleVertices(Vector3[] vertices)
{
for (int i = 0; i < vertices.Length; i++)
{
vertices[i].x = vertices[i].x / transform.localScale.x;
vertices[i].y = vertices[i].y / transform.localScale.y;
vertices[i].z = vertices[i].z / transform.localScale.z;
}
return vertices;
}
void RemakeMeshToDiscrete(Vector3[] vert, int[] trig, out Vector3[] outVertices, out int[] outTriangles)
{
Vector3[] vertDiscrete = new Vector3[trig.Length];
int[] trigDiscrete = new int[trig.Length];
for (int i = 0; i < trig.Length; i++)
{
vertDiscrete[i] = vert[trig[i]];
trigDiscrete[i] = i;
}
outVertices = vertDiscrete;
outTriangles = trigDiscrete;
}
Vector3[] addIndterpolation(Vector3[] array, int sizeVertices, float[][][] values, float surfaceValue)
{
int sizeValues = values.Length;
for (int x = 0; x < sizeValues - 1; x++)
{
for (int y = 0; y < sizeValues - 1; y++)
{
for (int z = 0; z < sizeValues - 1; z++)
{
int arrayCords = z * 2 + y * sizeVertices * 2 + x * sizeVertices * sizeVertices * 2;
float value = values[x][y][z];
float valueZ = values[x + 1][y][z];
float valueY = values[x][y + 1][z];
float valueX = values[x][y][z + 1];
float interpolationValueX = Mathf.Abs(surfaceValue - value) / (Mathf.Abs(surfaceValue - value) + Mathf.Abs(surfaceValue - valueX));
float interpolationValueY = Mathf.Abs(surfaceValue - value) / (Mathf.Abs(surfaceValue - value) + Mathf.Abs(surfaceValue - valueY));
float interpolationValueZ = Mathf.Abs(surfaceValue - value) / (Mathf.Abs(surfaceValue - value) + Mathf.Abs(surfaceValue - valueZ));
array[arrayCords + 1] += new Vector3(0, 0, interpolationValueX - .5f);
array[arrayCords + sizeVertices] += new Vector3(0, interpolationValueY - .5f, 0);
array[arrayCords + sizeVertices * sizeVertices] += new Vector3(interpolationValueZ - .5f, 0, 0);
}
}
}
return array;
}
int getVertexIndex(int z, int y, int x, int cubeValue, int size)
{
int[][] vertecieValues = new[] {
new []{1, 0, 0 },
new []{2, 0, 1 },
new []{1, 0, 2 },
new []{0, 0, 1 },
new []{1, 2, 0 },
new []{2, 2, 1 },
new []{1, 2, 2 },
new []{0, 2, 1 },
new []{0, 1, 0 },
new []{2, 1, 0 },
new []{2, 1, 2 },
new []{0, 1, 2 }
};
int xReturn = z * 2 * size * size;
int yReturn = y * 2 * size;
int zReturn = x * 2;
return xReturn + vertecieValues[cubeValue][0] * size * size + yReturn + vertecieValues[cubeValue][1] * size + zReturn + vertecieValues[cubeValue][2];
}
int getTriangleIndex(float[] values, float surfaceValue)
{
int index = 0;
for (int i = 0; i < 8; i++)
{
if (values[i] > surfaceValue)
{
index |= 1 << i;
}
}
return index;
}
Vector3[] initializeVector3Array(int Xsize, int Ysize, int Zsize)
{
Vector3[] array = new Vector3[(Xsize + 1) * (Ysize + 1) * (Zsize + 1)];
int count = 0;
for (int x = 0; x < Xsize + 1; x++)
{
for (int y = 0; y < Ysize + 1; y++)
{
for (int z = 0; z < Zsize + 1; z++)
{
array[count] = new Vector3(x / 2f, y / 2f, z / 2f);
count++;
}
}
}
return array;
}
int[][] table = new int[][] {
new int[] { },
new int[] { 8, 3, 0},
new int[] { 9, 0, 1},
new int[] { 8, 3, 1, 8, 1, 9},
new int[] {10, 1, 2},
new int[] { 8, 3, 0, 1, 2,10},
new int[] { 9, 0, 2, 9, 2,10},
new int[] { 3, 2, 8, 2,10, 8, 8,10, 9},
new int[] {11, 2, 3},
new int[] {11, 2, 0,11, 0, 8},
new int[] {11, 2, 3, 0, 1, 9},
new int[] { 2, 1,11, 1, 9,11,11, 9, 8},
new int[] {10, 1, 3,10, 3,11},
new int[] { 1, 0,10, 0, 8,10,10, 8,11},
new int[] { 0, 3, 9, 3,11, 9, 9,11,10},
new int[] { 8,10, 9, 8,11,10},
new int[] { 8, 4, 7},
new int[] { 3, 0, 4, 3, 4, 7},
new int[] { 1, 9, 0, 8, 4, 7},
new int[] { 9, 4, 1, 4, 7, 1, 1, 7, 3},
new int[] {10, 1, 2, 8, 4, 7},
new int[] { 2,10, 1, 0, 4, 7, 0, 7, 3},
new int[] { 4, 7, 8, 0, 2,10, 0,10, 9},
new int[] { 2, 7, 3, 2, 9, 7, 7, 9, 4, 2,10, 9},
new int[] { 2, 3,11, 7, 8, 4},
new int[] { 7,11, 4,11, 2, 4, 4, 2, 0},
new int[] { 3,11, 2, 4, 7, 8, 9, 0, 1},
new int[] { 2, 7,11, 2, 1, 7, 1, 4, 7, 1, 9, 4},
new int[] { 8, 4, 7,11,10, 1,11, 1, 3},
new int[] {11, 4, 7, 1, 4,11, 1,11,10, 1, 0, 4},
new int[] { 3, 8, 0, 7,11, 4,11, 9, 4,11,10, 9},
new int[] { 7,11, 4, 4,11, 9,11,10, 9},
new int[] { 9, 5, 4},
new int[] { 3, 0, 8, 4, 9, 5},
new int[] { 5, 4, 0, 5, 0, 1},
new int[] { 4, 8, 5, 8, 3, 5, 5, 3, 1},
new int[] { 2,10, 1, 9, 5, 4},
new int[] { 0, 8, 3, 5, 4, 9,10, 1, 2},
new int[] {10, 5, 2, 5, 4, 2, 2, 4, 0},
new int[] { 3, 4, 8, 3, 2, 4, 2, 5, 4, 2,10, 5},
new int[] {11, 2, 3, 9, 5, 4},
new int[] { 9, 5, 4, 8,11, 2, 8, 2, 0},
new int[] { 3,11, 2, 1, 5, 4, 1, 4, 0},
new int[] { 8, 5, 4, 2, 5, 8, 2, 8,11, 2, 1, 5},
new int[] { 5, 4, 9, 1, 3,11, 1,11,10},
new int[] { 0, 9, 1, 4, 8, 5, 8,10, 5, 8,11,10},
new int[] { 3, 4, 0, 3,10, 4, 4,10, 5, 3,11,10},
new int[] { 4, 8, 5, 5, 8,10, 8,11,10},
new int[] { 9, 5, 7, 9, 7, 8},
new int[] { 0, 9, 3, 9, 5, 3, 3, 5, 7},
new int[] { 8, 0, 7, 0, 1, 7, 7, 1, 5},
new int[] { 1, 7, 3, 1, 5, 7},
new int[] { 1, 2,10, 5, 7, 8, 5, 8, 9},
new int[] { 9, 1, 0,10, 5, 2, 5, 3, 2, 5, 7, 3},
new int[] { 5, 2,10, 8, 2, 5, 8, 5, 7, 8, 0, 2},
new int[] {10, 5, 2, 2, 5, 3, 5, 7, 3},
new int[] {11, 2, 3, 8, 9, 5, 8, 5, 7},
new int[] { 9, 2, 0, 9, 7, 2, 2, 7,11, 9, 5, 7},
new int[] { 0, 3, 8, 2, 1,11, 1, 7,11, 1, 5, 7},
new int[] { 2, 1,11,11, 1, 7, 1, 5, 7},
new int[] { 3, 9, 1, 3, 8, 9, 7,11,10, 7,10, 5},
new int[] { 9, 1, 0,10, 7,11,10, 5, 7},
new int[] { 3, 8, 0, 7,10, 5, 7,11,10},
new int[] {11, 5, 7,11,10, 5},
new int[] {10, 6, 5},
new int[] { 8, 3, 0,10, 6, 5},
new int[] { 0, 1, 9, 5,10, 6},
new int[] {10, 6, 5, 9, 8, 3, 9, 3, 1},
new int[] { 1, 2, 6, 1, 6, 5},
new int[] { 0, 8, 3, 2, 6, 5, 2, 5, 1},
new int[] { 5, 9, 6, 9, 0, 6, 6, 0, 2},
new int[] { 9, 6, 5, 3, 6, 9, 3, 9, 8, 3, 2, 6},
new int[] { 3,11, 2,10, 6, 5},
new int[] { 6, 5,10, 2, 0, 8, 2, 8,11},
new int[] { 1, 9, 0, 6, 5,10,11, 2, 3},
new int[] { 1,10, 2, 5, 9, 6, 9,11, 6, 9, 8,11},
new int[] {11, 6, 3, 6, 5, 3, 3, 5, 1},
new int[] { 0, 5, 1, 0,11, 5, 5,11, 6, 0, 8,11},
new int[] { 0, 5, 9, 0, 3, 5, 3, 6, 5, 3,11, 6},
new int[] { 5, 9, 6, 6, 9,11, 9, 8,11},
new int[] {10, 6, 5, 4, 7, 8},
new int[] { 5,10, 6, 7, 3, 0, 7, 0, 4},
new int[] { 5,10, 6, 0, 1, 9, 8, 4, 7},
new int[] { 4, 5, 9, 6, 7,10, 7, 1,10, 7, 3, 1},
new int[] { 7, 8, 4, 5, 1, 2, 5, 2, 6},
new int[] { 4, 1, 0, 4, 5, 1, 6, 7, 3, 6, 3, 2},
new int[] { 9, 4, 5, 8, 0, 7, 0, 6, 7, 0, 2, 6},
new int[] { 4, 5, 9, 6, 3, 2, 6, 7, 3},
new int[] { 7, 8, 4, 2, 3,11,10, 6, 5},
new int[] {11, 6, 7,10, 2, 5, 2, 4, 5, 2, 0, 4},
new int[] {11, 6, 7, 8, 0, 3, 1,10, 2, 9, 4, 5},
new int[] { 6, 7,11, 1,10, 2, 9, 4, 5},
new int[] { 6, 7,11, 4, 5, 8, 5, 3, 8, 5, 1, 3},
new int[] { 6, 7,11, 4, 1, 0, 4, 5, 1},
new int[] { 4, 5, 9, 3, 8, 0,11, 6, 7},
new int[] { 9, 4, 5, 7,11, 6},
new int[] {10, 6, 4,10, 4, 9},
new int[] { 8, 3, 0, 9,10, 6, 9, 6, 4},
new int[] { 1,10, 0,10, 6, 0, 0, 6, 4},
new int[] { 8, 6, 4, 8, 1, 6, 6, 1,10, 8, 3, 1},
new int[] { 9, 1, 4, 1, 2, 4, 4, 2, 6},
new int[] { 1, 0, 9, 3, 2, 8, 2, 4, 8, 2, 6, 4},
new int[] { 2, 4, 0, 2, 6, 4},
new int[] { 3, 2, 8, 8, 2, 4, 2, 6, 4},
new int[] { 2, 3,11, 6, 4, 9, 6, 9,10},
new int[] { 0,10, 2, 0, 9,10, 4, 8,11, 4,11, 6},
new int[] {10, 2, 1,11, 6, 3, 6, 0, 3, 6, 4, 0},
new int[] {10, 2, 1,11, 4, 8,11, 6, 4},
new int[] { 1, 4, 9,11, 4, 1,11, 1, 3,11, 6, 4},
new int[] { 0, 9, 1, 4,11, 6, 4, 8,11},
new int[] {11, 6, 3, 3, 6, 0, 6, 4, 0},
new int[] { 8, 6, 4, 8,11, 6},
new int[] { 6, 7,10, 7, 8,10,10, 8, 9},
new int[] { 9, 3, 0, 6, 3, 9, 6, 9,10, 6, 7, 3},
new int[] { 6, 1,10, 6, 7, 1, 7, 0, 1, 7, 8, 0},
new int[] { 6, 7,10,10, 7, 1, 7, 3, 1},
new int[] { 7, 2, 6, 7, 9, 2, 2, 9, 1, 7, 8, 9},
new int[] { 1, 0, 9, 3, 6, 7, 3, 2, 6},
new int[] { 8, 0, 7, 7, 0, 6, 0, 2, 6},
new int[] { 2, 7, 3, 2, 6, 7},
new int[] { 7,11, 6, 3, 8, 2, 8,10, 2, 8, 9,10},
new int[] {11, 6, 7,10, 0, 9,10, 2, 0},
new int[] { 2, 1,10, 7,11, 6, 8, 0, 3},
new int[] { 1,10, 2, 6, 7,11},
new int[] { 7,11, 6, 3, 9, 1, 3, 8, 9},
new int[] { 9, 1, 0,11, 6, 7},
new int[] { 0, 3, 8,11, 6, 7},
new int[] {11, 6, 7},
new int[] {11, 7, 6},
new int[] { 0, 8, 3,11, 7, 6},
new int[] { 9, 0, 1,11, 7, 6},
new int[] { 7, 6,11, 3, 1, 9, 3, 9, 8},
new int[] { 1, 2,10, 6,11, 7},
new int[] { 2,10, 1, 7, 6,11, 8, 3, 0},
new int[] {11, 7, 6,10, 9, 0,10, 0, 2},
new int[] { 7, 6,11, 3, 2, 8, 8, 2,10, 8,10, 9},
new int[] { 2, 3, 7, 2, 7, 6},
new int[] { 8, 7, 0, 7, 6, 0, 0, 6, 2},
new int[] { 1, 9, 0, 3, 7, 6, 3, 6, 2},
new int[] { 7, 6, 2, 7, 2, 9, 2, 1, 9, 7, 9, 8},
new int[] { 6,10, 7,10, 1, 7, 7, 1, 3},
new int[] { 6,10, 1, 6, 1, 7, 7, 1, 0, 7, 0, 8},
new int[] { 9, 0, 3, 6, 9, 3, 6,10, 9, 6, 3, 7},
new int[] { 6,10, 7, 7,10, 8,10, 9, 8},
new int[] { 8, 4, 6, 8, 6,11},
new int[] {11, 3, 6, 3, 0, 6, 6, 0, 4},
new int[] { 0, 1, 9, 4, 6,11, 4,11, 8},
new int[] { 1, 9, 4,11, 1, 4,11, 3, 1,11, 4, 6},
new int[] {10, 1, 2,11, 8, 4,11, 4, 6},
new int[] {10, 1, 2,11, 3, 6, 6, 3, 0, 6, 0, 4},
new int[] { 0, 2,10, 0,10, 9, 4,11, 8, 4, 6,11},
new int[] { 2,11, 3, 6, 9, 4, 6,10, 9},
new int[] { 3, 8, 2, 8, 4, 2, 2, 4, 6},
new int[] { 2, 0, 4, 2, 4, 6},
new int[] { 1, 9, 0, 3, 8, 2, 2, 8, 4, 2, 4, 6},
new int[] { 9, 4, 1, 1, 4, 2, 4, 6, 2},
new int[] { 8, 4, 6, 8, 6, 1, 6,10, 1, 8, 1, 3},
new int[] { 1, 0,10,10, 0, 6, 0, 4, 6},
new int[] { 8, 0, 3, 9, 6,10, 9, 4, 6},
new int[] {10, 4, 6,10, 9, 4},
new int[] { 9, 5, 4, 7, 6,11},
new int[] { 4, 9, 5, 3, 0, 8,11, 7, 6},
new int[] { 6,11, 7, 4, 0, 1, 4, 1, 5},
new int[] { 6,11, 7, 4, 8, 5, 5, 8, 3, 5, 3, 1},
new int[] { 6,11, 7, 1, 2,10, 9, 5, 4},
new int[] {11, 7, 6, 8, 3, 0, 1, 2,10, 9, 5, 4},
new int[] {11, 7, 6,10, 5, 2, 2, 5, 4, 2, 4, 0},
new int[] { 7, 4, 8, 2,11, 3,10, 5, 6},
new int[] { 4, 9, 5, 6, 2, 3, 6, 3, 7},
new int[] { 9, 5, 4, 8, 7, 0, 0, 7, 6, 0, 6, 2},
new int[] { 4, 0, 1, 4, 1, 5, 6, 3, 7, 6, 2, 3},
new int[] { 7, 4, 8, 5, 2, 1, 5, 6, 2},
new int[] { 4, 9, 5, 6,10, 7, 7,10, 1, 7, 1, 3},
new int[] { 5, 6,10, 0, 9, 1, 8, 7, 4},
new int[] { 5, 6,10, 7, 0, 3, 7, 4, 0},
new int[] {10, 5, 6, 4, 8, 7},
new int[] { 5, 6, 9, 6,11, 9, 9,11, 8},
new int[] { 0, 9, 5, 0, 5, 3, 3, 5, 6, 3, 6,11},
new int[] { 0, 1, 5, 0, 5,11, 5, 6,11, 0,11, 8},
new int[] {11, 3, 6, 6, 3, 5, 3, 1, 5},
new int[] { 1, 2,10, 5, 6, 9, 9, 6,11, 9,11, 8},
new int[] { 1, 0, 9, 6,10, 5,11, 3, 2},
new int[] { 6,10, 5, 2, 8, 0, 2,11, 8},
new int[] { 3, 2,11,10, 5, 6},
new int[] { 9, 5, 6, 3, 9, 6, 3, 8, 9, 3, 6, 2},
new int[] { 5, 6, 9, 9, 6, 0, 6, 2, 0},
new int[] { 0, 3, 8, 2, 5, 6, 2, 1, 5},
new int[] { 1, 6, 2, 1, 5, 6},
new int[] {10, 5, 6, 9, 3, 8, 9, 1, 3},
new int[] { 0, 9, 1, 5, 6,10},
new int[] { 8, 0, 3,10, 5, 6},
new int[] {10, 5, 6},
new int[] {11, 7, 5,11, 5,10},
new int[] { 3, 0, 8, 7, 5,10, 7,10,11},
new int[] { 9, 0, 1,10,11, 7,10, 7, 5},
new int[] { 3, 1, 9, 3, 9, 8, 7,10,11, 7, 5,10},
new int[] { 2,11, 1,11, 7, 1, 1, 7, 5},
new int[] { 0, 8, 3, 2,11, 1, 1,11, 7, 1, 7, 5},
new int[] { 9, 0, 2, 9, 2, 7, 2,11, 7, 9, 7, 5},
new int[] {11, 3, 2, 8, 5, 9, 8, 7, 5},
new int[] {10, 2, 5, 2, 3, 5, 5, 3, 7},
new int[] { 5,10, 2, 8, 5, 2, 8, 7, 5, 8, 2, 0},
new int[] { 9, 0, 1,10, 2, 5, 5, 2, 3, 5, 3, 7},
new int[] { 1,10, 2, 5, 8, 7, 5, 9, 8},
new int[] { 1, 3, 7, 1, 7, 5},
new int[] { 8, 7, 0, 0, 7, 1, 7, 5, 1},
new int[] { 0, 3, 9, 9, 3, 5, 3, 7, 5},
new int[] { 9, 7, 5, 9, 8, 7},
new int[] { 4, 5, 8, 5,10, 8, 8,10,11},
new int[] { 3, 0, 4, 3, 4,10, 4, 5,10, 3,10,11},
new int[] { 0, 1, 9, 4, 5, 8, 8, 5,10, 8,10,11},
new int[] { 5, 9, 4, 1,11, 3, 1,10,11},
new int[] { 8, 4, 5, 2, 8, 5, 2,11, 8, 2, 5, 1},
new int[] { 3, 2,11, 1, 4, 5, 1, 0, 4},
new int[] { 9, 4, 5, 8, 2,11, 8, 0, 2},
new int[] {11, 3, 2, 9, 4, 5},
new int[] { 3, 8, 4, 3, 4, 2, 2, 4, 5, 2, 5,10},
new int[] {10, 2, 5, 5, 2, 4, 2, 0, 4},
new int[] { 0, 3, 8, 5, 9, 4,10, 2, 1},
new int[] { 2, 1,10, 9, 4, 5},
new int[] { 4, 5, 8, 8, 5, 3, 5, 1, 3},
new int[] { 5, 0, 4, 5, 1, 0},
new int[] { 3, 8, 0, 4, 5, 9},
new int[] { 9, 4, 5},
new int[] { 7, 4,11, 4, 9,11,11, 9,10},
new int[] { 3, 0, 8, 7, 4,11,11, 4, 9,11, 9,10},
new int[] {11, 7, 4, 1,11, 4, 1,10,11, 1, 4, 0},
new int[] { 8, 7, 4,11, 1,10,11, 3, 1},
new int[] { 2,11, 7, 2, 7, 1, 1, 7, 4, 1, 4, 9},
new int[] { 3, 2,11, 4, 8, 7, 9, 1, 0},
new int[] { 7, 4,11,11, 4, 2, 4, 0, 2},
new int[] { 2,11, 3, 7, 4, 8},
new int[] { 2, 3, 7, 2, 7, 9, 7, 4, 9, 2, 9,10},
new int[] { 4, 8, 7, 0,10, 2, 0, 9,10},
new int[] { 2, 1,10, 0, 7, 4, 0, 3, 7},
new int[] {10, 2, 1, 8, 7, 4},
new int[] { 9, 1, 4, 4, 1, 7, 1, 3, 7},
new int[] { 1, 0, 9, 8, 7, 4},
new int[] { 3, 4, 0, 3, 7, 4},
new int[] { 8, 7, 4},
new int[] { 8, 9,10, 8,10,11},
new int[] { 0, 9, 3, 3, 9,11, 9,10,11},
new int[] { 1,10, 0, 0,10, 8,10,11, 8},
new int[] {10, 3, 1,10,11, 3},
new int[] { 2,11, 1, 1,11, 9,11, 8, 9},
new int[] {11, 3, 2, 0, 9, 1},
new int[] {11, 0, 2,11, 8, 0},
new int[] {11, 3, 2},
new int[] { 3, 8, 2, 2, 8,10, 8, 9,10},
new int[] { 9, 2, 0, 9,10, 2},
new int[] { 8, 0, 3, 1,10, 2},
new int[] {10, 2, 1},
new int[] { 8, 1, 3, 8, 9, 1},
new int[] { 9, 1, 0},
new int[] { 8, 0, 3},
new int[] {}
};
}
I know the code is really messy but the problem probably is in the CreateMesh method or in the triangulation table.
Does someone know why the hole is appearing there?
I have the triangulation table from the internet.
Thanks.
I got it fixed with a new triangulation table. Thanks for your help.

Get array items by index

I have two arrays, one with values an one with indices
int[] items = { 1, 2, 3, 7, 8, 9, 13, 16, 19, 23, 25, 26, 29, 31, 35, 36, 39, 45 };
int[] indices = { 1, 3, 5, 6, 7, 9 };
now I want a result array from the items selected by the indices of indices array
// 2, 7, 9, 13, 19
int[] result = new []{ items[1], items[3], items[5], items[6], items[7], items[9] };
Question: Is there a more generic approach for this?
var results = Array.ConvertAll(indices, i => items[i]);
Try using Linq:
int[] items = { 1, 2, 3, 7, 8, 9, 13, 16, 19, 23, 25, 26, 29, 31, 35, 36, 39, 45 };
int[] indices = { 1, 3, 5, 6, 7, 9 };
int[] result = indices
.Select(index => items[index])
.ToArray();
A good old for loop should be able to do this job as well:
int[] items = { 1, 2, 3, 7, 8, 9, 13, 16, 19, 23, 25, 26, 29, 31, 35, 36, 39, 45 };
int[] indices = { 1, 3, 5, 6, 7, 9 };
List<int> resultList = new List<int>();
for (int i = 0; i < indices.Length; i++)
{
resultList .Add(items[indices[i]]);
}
Explanation:
when using the [ ] operator to access a specific index in indices it will return the number. This can again be used to index/access a specific location in items. So you have a double indexing.
EDIT:
If you need the result as an array you can use the ToArray method to convert it:
int [] result = resultList.ToArray();
For the sake of alternative:
int[] result = items.Select((value, index) => new { Index = index, Value = value }) //Add indexes
.Where(w => indices.Contains(w.Index)) //Filter by indexes
.Select(s => s.Value).ToArray(); //Extract values to result array

How to get the bit size of an int

Of course I know a method to do this:
/// <summary>Gets the number of bits needed to represent the number.</summary>
public static int Size(int bits)
{
var size = 0;
while(bits != 0)
{
bits >>= 1;
size++;
}
return size;
}
So the Size(15) returns 4, and Size(16) returns 5.
But I guess (hope) there is a quicker way. But I couldn't think of (or Google) a nice and smooth (and fast) algorithm.
Not the fastest, but, probably the shortest one:
public static int Size(int bits) {
return (int) (Math.Log(bits, 2)) + 1;
}
Your code can be shortened by converting while into for:
public static int Size(int bits) {
int size = 0;
for (; bits != 0; bits >>= 1)
size++;
return size;
}
So first of all, I really doubt this is a bottleneck you're looking at. (Premature optimization is the root of all evil)
That being said, there are some interesting methods in this standford bit twiddeling article: Bit Twiddling Hacks
Including the naive approach you took (unrolling would indeed help):
unsigned int v; // 32-bit word to find the log base 2 of
unsigned int r = 0; // r will be lg(v)
while (v >>= 1) // unroll for more speed...
{
r++;
}
Or this one in O(log n):
uint32_t v; // find the log base 2 of 32-bit v
int r; // result goes here
static const int MultiplyDeBruijnBitPosition[32] =
{
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
};
v |= v >> 1; // first round down to one less than a power of 2
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
r = MultiplyDeBruijnBitPosition[(uint32_t)(v * 0x07C4ACDDU) >> 27];
And some more as well.
However note that what's faster in C/C++, might turn out to be slower in C#. Your code purely makes use of some local variables which is actually not bad, if you want to make sure another method is better, benchmark it first)
This isn't short, but it's quick (averaged over the full range of ints)
internal static readonly byte[] msbPos256 = new byte[] {
255, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
public static int SignificantBits(this int value) {
return HighBitPosition((uint)value) + 1;
}
public static int HighBitPosition(this ushort value) {
byte hiByte = (byte)(value >> 8);
if (hiByte != 0) return 8 + msbPos256[hiByte];
return (sbyte)msbPos256[(byte)value];
}
public static int HighBitPosition(this uint value) {
byte hiByte = (byte)(value >> 24);
if (hiByte != 0) return 24 + msbPos256[hiByte];
hiByte = (byte)(value >> 16);
return (hiByte != 0) ? 16 + msbPos256[hiByte] : HighBitPosition((ushort)value);
}
Call the SignificantBits method. Something to note is that 99.6% of all possible int values will have the most significant bit within the top 8 most significant bits (out of the 32). This requires just a right shift operation, two add operations (one of which likely get optimized by the compiler to an increment), a !=0 test, and an array reference. So, for most of the possible values, it's very quick. To cover the second 8 most significant bits takes an additional right shift, and !=0 test, and that covers 99.998% of possible int values. The casts don't cost much.
You could shave off the +1 operation by incrementing the msbPos256 values by one. I was more interested in the HighBitPosition function than the SignficantBits function when I wrote it, which is why I did it the way I did (I added SignificantBits as an afterthought).
IIRC, when I was testing the various tricks, this was faster than the DeBruijn technique that I had originally used for this.
If you are looking for the the highest bit set, see Find first set. One possible implementation (does not handle zero input) is:
table[0..31] = {0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31}
function lg_debruijn (x)
for each y in {1, 2, 4, 8, 16}: x ← x | (x >> y)
return table[(x * 0x07C4ACDD) >> 27]
If you want to calculate number of 1-bits (sometimes called population count), look at Hamming weight. One possibility solution would be:
int popcount_4(uint64_t x) {
int count;
for (count=0; x; count++)
x &= x-1;
return count;
}
Sometimes this functionality has even language support:
Some C compilers provide intrinsics that provide bit counting facilities. For example, GCC (since version 3.4 in April 2004) includes a builtin function __builtin_popcount that will use a processor instruction if available or an efficient library implementation otherwise. LLVM-GCC has included this function since version 1.5 in June, 2005.
I'm just adding this as an alternative to the other answers here, although they seem to address your question already.
This method counts the number of leading zeros using bit shifting, and then subtracts that from 32 to find out how much space was left.
static int NecessaryBits(int num)
{
const int mask = Int32.MinValue;
int leadingZeros = 0;
for(; leadingZeros < 32; leadingZeros++)
{
if( (num & mask) != 0)
break;
num <<= 1;
}
return 32 - leadingZeros;
}

How can I use Rx to observe a falling edge?

I have an input sequence like 8, 7, 6, 5, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 0, 4. What the result should show is 0, 0.
Yeah this would be easy. But I don't want the result being 0, 0, when the input is only 0, 0.
The thing here is that it should only publish the 0 when the previous value was something greater than zero.
IObservable<int> source = new[] { 8, 7, 6, 5, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 0, 4 }.ToObservable();
IObservable<int> edges = source.Zip(source.Skip(1), (f, s) => Tuple.Create(f, s))
.Where(t => t.Item1 > 0 && t.Item2 == 0)
.Select(t => t.Item2);

error Method name expected

private void button1_Click(object sender, EventArgs e)
{
int[] ml = new int[10] ( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
MessageBox.Show(Convert.ToString(ml.Length), "Length");
}
I get this error can someone tell me what I am doing wrong
Error 1 Method name expected C:\Users\Admin\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 21 24 WindowsFormsApplication1
int[] ml = new int[10] ( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
Should be
int[] ml = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
In C# array values are surrounded by { and } and not parens. Switch to using them in the array declaration and the error will go away.
int[] ml = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Categories

Resources