diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b901558 --- /dev/null +++ b/.gitignore @@ -0,0 +1,60 @@ +# This .gitignore file should be placed at the root of your Unity project directory +# +# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore +# +/[Ll]ibrary/ +/[Tt]emp/ +/[Oo]bj/ +/[Bb]uild/ +/[Bb]uilds/ +/[Ll]ogs/ +/[Mm]emoryCaptures/ + +# Asset meta data should only be ignored when the corresponding asset is also ignored +# !/[Aa]ssets/**/*.meta + +# Uncomment this line if you wish to ignore the asset store tools plugin +# /[Aa]ssets/AssetStoreTools* + +# Autogenerated Jetbrains Rider plugin +[Aa]ssets/Plugins/Editor/JetBrains* + +# Visual Studio cache directory +.vs/ + +# Gradle cache directory +.gradle/ + +# Autogenerated VS/MD/Consulo solution and project files +ExportedObj/ +.consulo/ +*.csproj +*.unityproj +*.sln +*.suo +*.tmp +*.user +*.userprefs +*.pidb +*.booproj +*.svd +*.pdb +*.mdb +*.opendb +*.VC.db + +# Unity3D generated meta files +*.pidb.meta +*.pdb.meta +*.mdb.meta + +# Unity3D generated file on crash reports +sysinfo.txt + +# Builds +*.apk +*.unitypackage + +# Crashlytics generated file +crashlytics-build.properties + diff --git a/Example.unity b/Example.unity deleted file mode 100644 index 18c9911..0000000 Binary files a/Example.unity and /dev/null differ diff --git a/Materials/VertexColor.mat b/Materials/VertexColor.mat index 9808796..6baf1ea 100644 Binary files a/Materials/VertexColor.mat and b/Materials/VertexColor.mat differ diff --git a/Scripts/PointCloudManager.cs b/Scripts/PointCloudManager.cs index 6731650..d3e1b99 100644 --- a/Scripts/PointCloudManager.cs +++ b/Scripts/PointCloudManager.cs @@ -1,24 +1,34 @@ -using UnityEngine; +// Original project by Gerard Llorach (2014) +// Updated by Oliver Dawkins and Dominic Zisch (2017) to visualise points using height and intensity gradients + +using UnityEngine; using System.Collections; using System.IO; public class PointCloudManager : MonoBehaviour { - // File + // File location public string dataPath; private string filename; public Material matVertex; - // GUI - private float progress = 0; - private string guiText; + // Methods to colour points + public enum cpb {Default, RGB, Height, Intensity}; + public cpb colourPointsBy = cpb.RGB; + public Color defaultPointColour; + public Gradient colourGradient; + + // Processing GUI + private float progress = 0; + private new string guiText; private bool loaded = false; - // PointCloud + // Point cloud properties private GameObject pointCloud; public float scale = 1; - public bool invertYZ = false; + public bool relocateToOrigin = false; + public bool invertYZ = false; public bool forceReload = false; public int numPoints; @@ -29,19 +39,35 @@ public class PointCloudManager : MonoBehaviour { private Color[] colors; private Vector3 minValue; - - void Start () { - // Create Resources folder - createFolders (); + // Point height properties + public float minHeight; + public float maxHeight; + private float heightDiff; + private float localDiff; + + // Point intensity properties + public float minIntensity; + public float maxIntensity; + private float intensityDiff; + private float relativeDiff; + + void Start () { + + //Calculate height difference for the visualising height gradient + heightDiff = maxHeight - minHeight; + + //Calculate intensity difference for visualising intensity gradient + intensityDiff = maxIntensity - minIntensity; + + // Create Resources folder + createFolders(); // Get Filename filename = Path.GetFileName(dataPath); - loadScene (); + loadScene(); } - - void loadScene(){ // Check if the PointCloud was loaded previously if(!Directory.Exists (Application.dataPath + "/Resources/PointCloudMeshes/" + filename)){ @@ -55,14 +81,13 @@ void loadScene(){ } else // Load stored PointCloud loadStoredMeshes(); - } - - + } + void loadPointCloud(){ // Check what file exists - if (File.Exists (Application.dataPath + dataPath + ".off")) - // load off - StartCoroutine ("loadOFF", dataPath + ".off"); + if (File.Exists (Application.dataPath + dataPath + ".xyz")) + // Load XYZ + StartCoroutine ("loadXYZ", dataPath + ".xyz"); else Debug.Log ("File '" + dataPath + "' could not be found"); @@ -78,44 +103,76 @@ void loadStoredMeshes(){ loaded = true; } - // Start Coroutine of reading the points from the OFF file and creating the meshes - IEnumerator loadOFF(string dPath){ + // Start Coroutine of reading the points from the XYZ file and creating the meshes + IEnumerator loadXYZ(string dPath){ // Read file + numPoints = File.ReadAllLines (Application.dataPath + dPath).Length; + StreamReader sr = new StreamReader (Application.dataPath + dPath); - sr.ReadLine (); // OFF - string[] buffer = sr.ReadLine ().Split(); // nPoints, nFaces - - numPoints = int.Parse (buffer[0]); + points = new Vector3[numPoints]; colors = new Color[numPoints]; minValue = new Vector3(); for (int i = 0; i< numPoints; i++){ - buffer = sr.ReadLine ().Split (); + string[] buffer = sr.ReadLine ().Split(','); if (!invertYZ) - points[i] = new Vector3 (float.Parse (buffer[0])*scale, float.Parse (buffer[1])*scale,float.Parse (buffer[2])*scale) ; - else - points[i] = new Vector3 (float.Parse (buffer[0])*scale, float.Parse (buffer[2])*scale,float.Parse (buffer[1])*scale) ; - - if (buffer.Length >= 5) - colors[i] = new Color (int.Parse (buffer[3])/255.0f,int.Parse (buffer[4])/255.0f,int.Parse (buffer[5])/255.0f); + points[i] = new Vector3 (float.Parse (buffer[0])*scale, float.Parse (buffer[1])*scale,float.Parse (buffer[2])*scale); else - colors[i] = Color.cyan; - - // Relocate Points near the origin - //calculateMin(points[i]); - - // GUI - progress = i *1.0f/(numPoints-1)*1.0f; - if (i%Mathf.FloorToInt(numPoints/20) == 0){ + points[i] = new Vector3 (float.Parse (buffer[0])*scale, float.Parse (buffer[2])*scale,float.Parse (buffer[1])*scale); + + // Test enum for technique to colour points + // Apply default point colour + if (colourPointsBy == cpb.Default) + { + colors[i] = defaultPointColour; + } + + // Colour points by RGB values + if (colourPointsBy == cpb.RGB) + { + if (buffer.Length >= 5) + colors[i] = new Color(int.Parse(buffer[3]) / 255.0f, int.Parse(buffer[4]) / 255.0f, int.Parse(buffer[5]) / 255.0f); + else + colors[i] = defaultPointColour; + } + + // TO DO - Automate calculation of minHeight and maxHeight + // Colour points by Height + else if (colourPointsBy == cpb.Height) + { + if (!invertYZ) + localDiff = float.Parse(buffer[1]) - minHeight; + else + localDiff = float.Parse(buffer[2]) - minHeight; + colors[i] = colourGradient.Evaluate(localDiff / heightDiff); + } + + //TO DO - Automate calculation of minIntensity and maxIntensity + // Colour points by intensity + else if (colourPointsBy == cpb.Intensity) + { + relativeDiff = float.Parse(buffer[6]) - minIntensity; + colors[i] = colourGradient.Evaluate(relativeDiff / intensityDiff); + } + + // Relocate points near the origin + if (relocateToOrigin == true) + { + calculateMin(points[i]); + } + + // Processing GUI + progress = i *1.0f/(numPoints-1)*1.0f; + if (i%Mathf.FloorToInt(numPoints/20) == 0) + { guiText=i.ToString() + " out of " + numPoints.ToString() + " loaded"; yield return null; } } - // Instantiate Point Groups numPointGroups = Mathf.CeilToInt (numPoints*1.0f / limitPoints*1.0f); @@ -131,11 +188,10 @@ IEnumerator loadOFF(string dPath){ InstantiateMesh (numPointGroups-1, numPoints- (numPointGroups-1) * limitPoints); //Store PointCloud - UnityEditor.PrefabUtility.CreatePrefab ("Assets/Resources/PointCloudMeshes/" + filename + ".prefab", pointCloud); + UnityEditor.PrefabUtility.SaveAsPrefabAsset(pointCloud, "Assets/Resources/PointCloudMeshes/" + filename + ".prefab"); loaded = true; } - void InstantiateMesh(int meshInd, int nPoints){ // Create Mesh @@ -147,7 +203,6 @@ void InstantiateMesh(int meshInd, int nPoints){ pointGroup.GetComponent ().mesh = CreateMesh (meshInd, nPoints, limitPoints); pointGroup.transform.parent = pointCloud.transform; - // Store Mesh UnityEditor.AssetDatabase.CreateAsset(pointGroup.GetComponent ().mesh, "Assets/Resources/PointCloudMeshes/" + filename + @"/" + filename + meshInd + ".asset"); UnityEditor.AssetDatabase.SaveAssets (); @@ -200,7 +255,6 @@ void createFolders(){ UnityEditor.AssetDatabase.CreateFolder ("Assets/Resources", "PointCloudMeshes"); } - void OnGUI(){