Skip to content
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
60 changes: 60 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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

Binary file removed Example.unity
Binary file not shown.
Binary file modified Materials/VertexColor.mat
Binary file not shown.
146 changes: 100 additions & 46 deletions Scripts/PointCloudManager.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)){
Expand All @@ -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");

Expand All @@ -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);

Expand All @@ -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
Expand All @@ -147,7 +203,6 @@ void InstantiateMesh(int meshInd, int nPoints){
pointGroup.GetComponent<MeshFilter> ().mesh = CreateMesh (meshInd, nPoints, limitPoints);
pointGroup.transform.parent = pointCloud.transform;


// Store Mesh
UnityEditor.AssetDatabase.CreateAsset(pointGroup.GetComponent<MeshFilter> ().mesh, "Assets/Resources/PointCloudMeshes/" + filename + @"/" + filename + meshInd + ".asset");
UnityEditor.AssetDatabase.SaveAssets ();
Expand Down Expand Up @@ -200,7 +255,6 @@ void createFolders(){
UnityEditor.AssetDatabase.CreateFolder ("Assets/Resources", "PointCloudMeshes");
}


void OnGUI(){


Expand Down