Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt new version of livescan3d to mesh the point cloud #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions HololensReceiver/Assets/ElemRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UnityEngine;
using System.Collections;
using System.Linq;

public class ElemRenderer : MonoBehaviour
{
Expand All @@ -19,34 +20,35 @@ void Update()
{
}

public void UpdateMesh(float[] arrVertices, byte[] arrColors, int nPointsToRender, int nPointsRendered)
public void UpdateMesh(float[] arrVertices, byte[] arrColors, int[] arrTriangles, int VerticesStart, int nVerticesToRender, int TrianglesStart, int nTrianglesToRender)
{
int nPoints;

if (arrVertices == null || arrColors == null)
nPoints = 0;
else
nPoints = System.Math.Min(nPointsToRender, (arrVertices.Length / 3) - nPointsRendered);
nPoints = System.Math.Min(nPoints, 65535);
int nPoints = nVerticesToRender;
int nTriangles = nTrianglesToRender * 3;

Vector3[] points = new Vector3[nPoints];
int[] indices = new int[nPoints];
Color[] colors = new Color[nPoints];
int[] triangles = new int[nTriangles];
for (int i = 0; i < nPoints; i++)
{
int ptIdx = 3 * (nPointsRendered + i);

int ptIdx = 3 * (VerticesStart + i);
points[i] = new Vector3(arrVertices[ptIdx + 0], arrVertices[ptIdx + 1], -arrVertices[ptIdx + 2]);
indices[i] = i;
colors[i] = new Color((float)arrColors[ptIdx + 0] / 256.0f, (float)arrColors[ptIdx + 1] / 256.0f, (float)arrColors[ptIdx + 2] / 256.0f, 1.0f);
colors[i] = new Color((float)arrColors[ptIdx + 0] / 256.0f, (float)arrColors[ptIdx + 1] / 256.0f, (float)arrColors[ptIdx + 2] / 256.0f, 1.0f);
}

for (int i = 0; i < nTriangles; i++) {
triangles[i] = arrTriangles[TrianglesStart + i];
}

if (mesh != null)
Destroy(mesh);
mesh = new Mesh();
mesh.vertices = points;
mesh.colors = colors;
mesh.SetIndices(indices, MeshTopology.Points, 0);
mesh.triangles = triangles;
GetComponent<MeshFilter>().mesh = mesh;

}
}
Binary file modified HololensReceiver/Assets/Plugins/x86/NetworkCommunication.dll
Binary file not shown.
42 changes: 33 additions & 9 deletions HololensReceiver/Assets/Plugins/x86/NetworkCommunication.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified HololensReceiver/Assets/Plugins/x86/NetworkCommunication.exp
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified HololensReceiver/Assets/Plugins/x86/NetworkCommunication.iobj
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified HololensReceiver/Assets/Plugins/x86/NetworkCommunication.ipdb
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified HololensReceiver/Assets/Plugins/x86/NetworkCommunication.lib
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified HololensReceiver/Assets/Plugins/x86/NetworkCommunication.pdb
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified HololensReceiver/Assets/Plugins/x86/NetworkCommunication.winmd
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified HololensReceiver/Assets/PointCloudMaterial.mat
Binary file not shown.
68 changes: 54 additions & 14 deletions HololensReceiver/Assets/PointCloudReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using UnityEngine.UI;
using System;
using System.Collections;
using System.Linq;

#if WINDOWS_UWP
using NetworkCommunication;
Expand Down Expand Up @@ -36,7 +37,9 @@ void Update()

float[] vertices;
byte[] colors;

int[] triangles;
int[] chunksVertices;
int[] chunksTriangles;
if (bReadyForNextFrame)
{
Debug.Log("Requesting frame");
Expand All @@ -51,13 +54,14 @@ void Update()
}

#if WINDOWS_UWP
if (socket.GetFrame(out vertices, out colors))
#else
if (ReceiveFrame(out vertices, out colors))
if (socket.GetFrame(out vertices, out colors, out triangles, out chunksVertices, out chunksTriangles))
#else
if (ReceiveFrame(out vertices, out colors,out triangles, out chunksVertices, out chunksTriangles))
#endif
{
Debug.Log("Frame received");
pointCloudRenderer.Render(vertices, colors);
Debug.Log("max and min of index: " + triangles.Max() + " " + triangles.Min() + " " + triangles.Length);
pointCloudRenderer.Render(vertices, colors, triangles, chunksVertices,chunksTriangles);
bReadyForNextFrame = true;
}
}
Expand Down Expand Up @@ -94,27 +98,51 @@ int ReadInt()
return BitConverter.ToInt32(buffer, 0);
}

bool ReceiveFrame(out float[] lVertices, out byte[] lColors)
bool ReceiveFrame(out float[] lVertices, out byte[] lColors, out int[] lTriangles, out int[] chunksVertices, out int[] chunksTriangles)
{
int nPointsToRead = ReadInt();

lVertices = new float[3 * nPointsToRead];
short[] lShortVertices = new short[3 * nPointsToRead];
lColors = new byte[3 * nPointsToRead];
int nTrianglesToRead = ReadInt();
int nChunks = ReadInt();


int nBytesToRead = sizeof(short) * 3 * nPointsToRead;
chunksVertices = new int[nChunks];
int nBytesToRead = sizeof(int) * nChunks;
int nBytesRead = 0;
byte[] buffer = new byte[nBytesToRead];

while (nBytesRead < nBytesToRead)
nBytesRead += socket.GetStream().Read(buffer, nBytesRead, Math.Min(nBytesToRead - nBytesRead, 64000));

System.Buffer.BlockCopy(buffer, 0, lShortVertices, 0, nBytesToRead);
System.Buffer.BlockCopy(buffer, 0, chunksVertices, 0, nBytesToRead);



chunksTriangles = new int[nChunks];
nBytesToRead = sizeof(int) * nChunks;
nBytesRead = 0;
buffer = new byte[nBytesToRead];

while (nBytesRead < nBytesToRead)
nBytesRead += socket.GetStream().Read(buffer, nBytesRead, Math.Min(nBytesToRead - nBytesRead, 64000));

System.Buffer.BlockCopy(buffer, 0, chunksTriangles, 0, nBytesToRead);




lVertices = new float[3 * nPointsToRead];
nBytesToRead = sizeof(float) * 3 * nPointsToRead;
nBytesRead = 0;
buffer = new byte[nBytesToRead];

while (nBytesRead < nBytesToRead)
nBytesRead += socket.GetStream().Read(buffer, nBytesRead, Math.Min(nBytesToRead - nBytesRead, 64000));

System.Buffer.BlockCopy(buffer, 0, lVertices, 0, nBytesToRead);

for (int i = 0; i < lShortVertices.Length; i++)
lVertices[i] = lShortVertices[i] / 1000.0f;


lColors = new byte[3 * nPointsToRead];
nBytesToRead = sizeof(byte) * 3 * nPointsToRead;
nBytesRead = 0;
buffer = new byte[nBytesToRead];
Expand All @@ -124,6 +152,18 @@ bool ReceiveFrame(out float[] lVertices, out byte[] lColors)

System.Buffer.BlockCopy(buffer, 0, lColors, 0, nBytesToRead);


lTriangles = new int[3 * nTrianglesToRead];
nBytesToRead = sizeof(int) * 3 * nTrianglesToRead;
nBytesRead = 0;
buffer = new byte[nBytesToRead];
//Debug.Log(nBytesToRead);
while (nBytesRead < nBytesToRead)
nBytesRead += socket.GetStream().Read(buffer, nBytesRead, Math.Min(nBytesToRead - nBytesRead, 64000));

System.Buffer.BlockCopy(buffer, 0, lTriangles, 0, nBytesToRead);


return true;
}
#endif
Expand Down
28 changes: 10 additions & 18 deletions HololensReceiver/Assets/PointCloudRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,26 @@ void UpdatePointSize()
pointCloudMaterial.SetFloat("_PointSize", pointSize * transform.localScale.x);
}

public void Render(float[] arrVertices, byte[] arrColors)
public void Render(float[] arrVertices, byte[] arrColors, int[] arrTriangles, int[] chunksVertices, int[] chunksTriangles)
{
int nPoints, nChunks;
if (arrVertices == null || arrColors == null)
{
nPoints = 0;
nChunks = 0;
}
else
{
nPoints = arrVertices.Length / 3;
nChunks = 1 + nPoints / maxChunkSize;
}


int nChunks = chunksVertices.length;

int VerticesRead = 0;
int TrianglesRead = 0;

if (elems.Count < nChunks)
AddElems(nChunks - elems.Count);
if (elems.Count > nChunks)
RemoveElems(elems.Count - nChunks);

int offset = 0;
for (int i = 0; i < nChunks; i++)
{
int nPointsToRender = System.Math.Min(maxChunkSize, nPoints - offset);

ElemRenderer renderer = elems[i].GetComponent<ElemRenderer>();
renderer.UpdateMesh(arrVertices, arrColors, nPointsToRender, offset);

offset += nPointsToRender;
renderer.UpdateMesh(arrVertices, arrColors, arrTriangles, VerticesRead, chunksVertices[i], TrianglesRead*3, chunksTriangles[i]);
VerticesRead += chunksVertices[i];
TrianglesRead += chunksTriangles[i];
}
}

Expand Down
Loading