Skip to content

Instantly share code, notes, and snippets.

@andybak
Created August 27, 2019 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybak/e4337d3399b3937aa809a0179293bd13 to your computer and use it in GitHub Desktop.
Save andybak/e4337d3399b3937aa809a0179293bd13 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class DataPlotter : MonoBehaviour {
// Name of the input file, no extension
public string inputfile;
// List for holding data from CSV reader
private List<Dictionary<string, object>> pointList;
// Indices for columns to be assigned
public int columnX = 0;
public int columnY = 1;
public int columnZ = 2;
public int columnWidth = 3;
public int columnHeight = 4;
public int columnDepth = 5;
public int columnRed = 0;
public int columnGreen = 1;
public int columnBlue = 2;
// Full column names
public string xName;
public string yName;
public string zName;
public string widthName;
public string heightName;
public string depthName;
public string redName;
public string greenName;
public string blueName;
public float plotScale = 10;
// The prefab for the data points that will be instantiated
public GameObject PointPrefab;
// Object which will contain instantiated prefabs in hiearchy
public GameObject PointHolder;
// Use this for initialization
void Start () {
// Set pointlist to results of function Reader with argument inputfile
pointList = CSVReader.Read(inputfile);
//Log to console
Debug.Log(pointList);
// Declare list of strings, fill with keys (column names)
List<string> columnList = new List<string>(pointList[1].Keys);
// Print number of keys (using .count)
Debug.Log("There are " + columnList.Count + " columns in the CSV");
foreach (string key in columnList)
Debug.Log("Column name is " + key);
// Assign column name from columnList to Name variables
xName = columnList[columnX];
yName = columnList[columnY];
zName = columnList[columnZ];
heightName = columnList[columnHeight];
widthName = columnList[columnWidth];
depthName = columnList[columnDepth];
redName = columnList[columnRed];
greenName = columnList[columnGreen];
blueName = columnList[columnBlue];
// Get maxes of each axis
float xMax = FindMaxValue(xName);
float yMax = FindMaxValue(yName);
float zMax = FindMaxValue(zName);
// Get minimums of each axis
float xMin = FindMinValue(xName);
float yMin = FindMinValue(yName);
float zMin = FindMinValue(zName);
//Loop through Pointlist
for (var i = 0; i < pointList.Count; i++)
{
// Get value in poinList at ith "row", in "column" Name, normalize
float x = (Convert.ToSingle(pointList[i][xName]) - xMin) / (xMax - xMin);
float y = (Convert.ToSingle(pointList[i][yName]) - yMin) / (yMax - yMin);
float z = (Convert.ToSingle(pointList[i][zName]) - zMin) / (zMax - zMin);
var color = new Color(
Convert.ToSingle(pointList[i][redName]),
Convert.ToSingle(pointList[i][greenName]),
Convert.ToSingle(pointList[i][blueName])
);
var scale = new Vector3(
Convert.ToSingle(pointList[i][widthName]),
Convert.ToSingle(pointList[i][heightName]),
Convert.ToSingle(pointList[i][depthName])
);
// Instantiate as gameobject variable so that it can be manipulated within loop
GameObject dataPoint = Instantiate(
PointPrefab,
new Vector3(x, y, z)* plotScale,
Quaternion.identity);
dataPoint.GetComponent<Transform>().localScale = scale;
// Make child of PointHolder object, to keep points within container in hiearchy
dataPoint.transform.parent = PointHolder.transform;
// Assigns original values to dataPointName
string dataPointName =
pointList[i][xName] + " "
+ pointList[i][yName] + " "
+ pointList[i][zName];
// Assigns name to the prefab
dataPoint.transform.name = dataPointName;
// Gets material color and sets it to a new RGB color we define
dataPoint.GetComponent<Renderer>().material.color = color;
//new Color(x,y,z, 1.0f);
}
}
private float FindMaxValue(string columnName)
{
//set initial value to first value
float maxValue = Convert.ToSingle(pointList[0][columnName]);
//Loop through Dictionary, overwrite existing maxValue if new value is larger
for (var i = 0; i < pointList.Count; i++)
{
if (maxValue < Convert.ToSingle(pointList[i][columnName]))
maxValue = Convert.ToSingle(pointList[i][columnName]);
}
//Spit out the max value
return maxValue;
}
private float FindMinValue(string columnName)
{
float minValue = Convert.ToSingle(pointList[0][columnName]);
//Loop through Dictionary, overwrite existing minValue if new value is smaller
for (var i = 0; i < pointList.Count; i++)
{
if (Convert.ToSingle(pointList[i][columnName]) < minValue)
minValue = Convert.ToSingle(pointList[i][columnName]);
}
return minValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment