Skip to content

Commit

Permalink
point cloud streaming (beyond proof-of-concept)
Browse files Browse the repository at this point in the history
- third example with point cloud decoding
- use new NHVD interface
- updated readme

Related to:
#5
  • Loading branch information
bmegli committed Jan 19, 2020
1 parent c08809f commit 5664d67
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 174 deletions.
17 changes: 0 additions & 17 deletions Assets/LookAt.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/LookAt.cs.meta

This file was deleted.

10 changes: 3 additions & 7 deletions Assets/MouseRts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
using UnityEngine;
using UnityEngine.EventSystems;

namespace Ev3devMapping
{

public class MouseRts : MonoBehaviour
{
public int LevelArea = 100;
Expand All @@ -28,7 +25,10 @@ public class MouseRts : MonoBehaviour
public float TouchRotationMinMagSquared = 1f;
public float TouchRotationMinAngle = 0.1f;

#if UNITY_STANDALONE
#else
private bool touchRotating = false;
#endif
private Vector2 touchRotationStart = Vector2.zero;

public float UpDownScale = 0.03f;
Expand All @@ -38,8 +38,6 @@ public class MouseRts : MonoBehaviour
public float TouchUpDownScale = 0.15f;
public float TouchRotateScale = 0.015f;
public float TouchMoveScale = 1f;


private float yrotation;

public void Start()
Expand Down Expand Up @@ -189,5 +187,3 @@ private Vector3 CameraMovement()
return move;
}
}

} //namespace
61 changes: 54 additions & 7 deletions Assets/NHVD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@

public class NHVD
{
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct nhvd_net_config
{
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public string ip;
public ushort port;
public int timeout_ms;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct nhvd_hw_config
{
Expand All @@ -36,12 +45,13 @@ public struct nhvd_hw_config
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct nhvd_net_config
{
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public string ip;
public ushort port;
public int timeout_ms;
public struct nhvd_depth_config
{
public float ppx;
public float ppy;
public float fx;
public float fy;
public float depth_unit;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
Expand Down Expand Up @@ -71,12 +81,29 @@ public struct nhvd_point_cloud
/// Return Type: nhvd*
///net_config: nhvd_net_config*
///hw_config: nhvd_hw_config*
///depth_config: nhvd_depth_config*
#if (UNITY_IPHONE || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport ("__Internal")]
#else
[DllImport ("nhvd")]
#endif
public static extern System.IntPtr nhvd_init(ref nhvd_net_config net_config, ref nhvd_hw_config hw_config) ;
public static extern System.IntPtr nhvd_init(ref nhvd_net_config net_config, ref nhvd_hw_config hw_config, ref nhvd_depth_config depth_config) ;

///Return Type: nhvd*
///net_config: nhvd_net_config*
///hw_config: nhvd_hw_config*
///depth_config: nhvd_depth_config*
#if (UNITY_IPHONE || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport ("__Internal")]
#else
[DllImport ("nhvd")]
#endif
private static extern System.IntPtr nhvd_init(ref nhvd_net_config net_config, ref nhvd_hw_config hw_config, System. IntPtr depth_config) ;

public static System.IntPtr nhvd_init(ref nhvd_net_config net_config, ref nhvd_hw_config hw_config)
{
return nhvd_init(ref net_config, ref hw_config, System.IntPtr.Zero);
}

/// Return Type: void
///n: nhvd *
Expand All @@ -87,6 +114,26 @@ public struct nhvd_point_cloud
#endif
public static extern void nhvd_close(System.IntPtr n) ;

/// Return Type: int
///n: nhvd*
///frame: nhvd_frame*
///pc: nhvd_point_cloud*
#if (UNITY_IPHONE || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport ("__Internal")]
#else
[DllImport ("nhvd")]
#endif
public static extern int nhvd_get_begin(System.IntPtr n, ref nhvd_frame frame, ref nhvd_point_cloud pc) ;

/// Return Type: int
///n: nhvd *
#if (UNITY_IPHONE || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport ("__Internal")]
#else
[DllImport ("nhvd")]
#endif
public static extern int nhvd_get_end(System.IntPtr n) ;

/// Return Type: int
///n: void*
///frame: nhvd_frame*
Expand Down
60 changes: 37 additions & 23 deletions Assets/PointCloudRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,76 @@
using UnityEngine;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class PointCloudRenderer : MonoBehaviour
{
public string device = "/dev/dri/renderD128";
public string ip = "";
public ushort port = 9766;
public ushort port = 9768;

private IntPtr nhvd;
private NHVD.nhvd_frame frame = new NHVD.nhvd_frame{ data=new System.IntPtr[3], linesize=new int[3] };
private NHVD.nhvd_point_cloud point_cloud = new NHVD.nhvd_point_cloud {data = System.IntPtr.Zero, size=0, used=0};

const int MAX_VERTICES=848*480;
private Mesh mesh;

void Awake()
{
NHVD.nhvd_hw_config hw_config = new NHVD.nhvd_hw_config{hardware="vaapi", codec="hevc", device=this.device, pixel_format="p010le", width=848, height=480, profile=2};
NHVD.nhvd_net_config net_config = new NHVD.nhvd_net_config{ip=this.ip, port=this.port, timeout_ms=500 };
NHVD.nhvd_hw_config hw_config = new NHVD.nhvd_hw_config{hardware="vaapi", codec="hevc", device=this.device, pixel_format="p010le", width=848, height=480, profile=2};
NHVD.nhvd_depth_config depth_config = new NHVD.nhvd_depth_config{ppx = 421.353f, ppy=240.93f, fx=426.768f, fy=426.768f, depth_unit = 0.0001f };

nhvd=NHVD.nhvd_init (ref net_config, ref hw_config);
nhvd=NHVD.nhvd_init (ref net_config, ref hw_config, ref depth_config);

if (nhvd == IntPtr.Zero)
{
Debug.Log ("failed to initialize NHVD");
gameObject.SetActive (false);
}
}
}
void OnDestroy()
{
NHVD.nhvd_close (nhvd);
}

Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;

Vector3[] vertices = new Vector3[MAX_VERTICES];
private void PrepareMesh(int size)
{
if(mesh == null)
mesh = new Mesh();

for(int i=0;i<MAX_VERTICES;i++)
vertices[i] = new Vector3(i/1000f, i/1000f, i/1000f);
mesh.vertices = vertices;
if(mesh.vertexCount == size)
return;

mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
Vector3[] vertices = new Vector3[size];
for(int i=0;i<size;i++)
vertices[i] = new Vector3();
mesh.vertices = vertices;

int[] indices = new int[MAX_VERTICES];
for(int i=0;i<MAX_VERTICES;++i)
int[] indices = new int[size];
for(int i=0;i<size;++i)
indices[i] = i;
mesh.SetIndices(indices, MeshTopology.Points,0);

//we don't want to recalculate bounds for half million dynamic points so just set wide bounds
mesh.bounds = new Bounds(new Vector3(0, 0, 0), new Vector3(10, 10, 10));

GetComponent<MeshFilter>().mesh = mesh;
}
void OnDestroy()
{
NHVD.nhvd_close (nhvd);
}
NativeArray<Vector3> pc;

void LateUpdate ()
{
if (NHVD.nhvd_get_point_cloud_begin(nhvd, ref point_cloud) == 0)
{
PrepareMesh(point_cloud.size);

//possible optimization - only render non-zero points (point_cloud.used)
unsafe
{
pc = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<Vector3>(point_cloud.data.ToPointer(), point_cloud.size, Allocator.None);
NativeArray<Vector3> pc = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<Vector3>(point_cloud.data.ToPointer(), point_cloud.size, Allocator.None);
NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref pc, AtomicSafetyHandle.Create());
int points = Math.Min(MAX_VERTICES, point_cloud.size);

GetComponent<MeshFilter>().mesh.SetVertices(pc, 0, points);
mesh.SetVertices(pc, 0, point_cloud.size);
}
}

Expand Down
Loading

0 comments on commit 5664d67

Please sign in to comment.