convert string to a array of vector 3

Hi all,

how can i convert a string which looks like this

(41.8, -15.5, 110.7)(5.5, -15.5, 109.3)(-13.5, -15.5, 46.7)(-17.7, -15.5, 45.9)(30.4, -15.5, 68.2)(26.8, -15.9, 124.3)(54.8, -14.4, 133.4)(44.3, -14.4, 134.3)(27.3, -15.9, 117.3)(17.4, -15.9, 114.7)(47.2, -15.9, 116.4)(54.2, -15.9, 113.9)(52.8, -14.6, 112.7)(36.4, -15.9, 126.9)(31.0, -15.5, 115.0)(-8.9, -12.9, 125.5)

to a vector3 array.

There are some specific functions in .NET/Mono that read values from a string, like float.Parse(string), but they are usually too cranky: any non-numeric character appended to the input string will cause an exception, thus we must first split the string into numeric substrings.

An alternative is to write the whole thing from scratch: skip characters until a digit, decimal point or minus sign is found, then read the number until a non-numeric character is reached, and repeat this until all numbers have been read. Since the input data is supposed to contain Vector3 values, you can read 3 floats each time, assign them to a Vector3 variable and add the result to a list. When the whole string has been read, convert the list to a built-in array and return it.

The most usual way to read a float number is to start the result as zero, then for each digit read multiply the result by 10 and add the digit to it. When a decimal point is found, set a flag and start counting digits after the point, but keep reading the digits as before. At the end, you will get a whole number that simply ignores the decimal point: divide it by the appropriate power of 10 to fix the result. The negative sign is handled in a similar way: the number is read as a positive value, and its polarity is fixed at the end.

That’s what the script below does: the function GetVectors receives the input string and returns a Vector3 array with the values read. It just ignores non-numeric characters between the values, and can handle negative and decimal numbers. The numbers must not contain spaces or other non-numeric characters in between, or be in scientific notation.

#pragma strict

import System.Collections.Generic;

private var str: String;
private var numChars = "0123456789."; // numeric characters (in ascending order)
private var curPos: int;
private var size: int;

function GetVectors(originalString: String): Vector3[] {
	str = originalString;
	curPos = 0;
	size = str.length;
	var vecs = new List.<Vector3>(); // create a temporary list
	while (FindNumber()){ // if there's a number...
		vecs.Add(GetVector3()); // get a vector3
	}
	return vecs.ToArray(); // return a built-in array
}

function GetVector3(): Vector3 { // get a vector3 
	var v3: Vector3 = Vector3.zero;
	v3.x = ParseFloat(); // read the x coord...
	// if available, read y
	if (FindNumber()) v3.y = ParseFloat();
	// if available, read z
	if (FindNumber()) v3.z = ParseFloat();
	return v3;
}

// Read a float: ignore sign and decimal point when reading the
// number, then apply sign and decimal correction to the result
function ParseFloat(): float {
	var result: float = 0f;
	var decimals: int = 0;
	var hasDec: boolean = false;
	// has a negative sign?
	var negative: boolean = (str[curPos] == "-");
	if (negative) curPos++; // yes: skip it
	// read number until a non num char is found:
	while (curPos < size){
		var digit: int = numChars.IndexOf(str[curPos]);
		if (digit < 0){ // if it's an invalid char...
			break; // stop the loop
		}
		if (digit == 10){ // if it's a decimal point...
			hasDec = true; // flag that next digits are decimals
		} else { // if it's a regular digit...
			result = 10 * result + digit; // add it to the number
			if (hasDec) decimals++; // if it's a decimal, count it
		}
		curPos++; // pass to the next digit
	}
	if (hasDec){ // apply decimal correction, if needed
		result /= Mathf.Pow(10f, decimals);
	}
	if (negative){ // apply negative sign, if any
		result = -result;
	}
	return result;
}

function FindNumber(): boolean {
	// inc pointer until a digit, decimal point or negative signal is found:
	while (curPos < size && str[curPos] != "-" && numChars.IndexOf(str[curPos]) < 0){
		curPos ++;
	}
	// return true if a valid char was found, false otherwise
	return curPos < size;
}

// example on how to use GetVectors:

var s = "(41.8, -15.5, 110.7)(5.5, -15.5, 109.3)(-13.5, -15.5, 46.7)(-17.7, -15.5, 45.9)(30.4, -15.5, 68.2)(26.8, -15.9, 124.3)(54.8, -14.4, 133.4)(44.3, -14.4, 134.3)(27.3, -15.9, 117.3)(17.4, -15.9, 114.7)(47.2, -15.9, 116.4)(54.2, -15.9, 113.9)(52.8, -14.6, 112.7)(36.4, -15.9, 126.9)(31.0, -15.5, 115.0)(-8.9, -12.9, 125.5)";

function Start(){
	var v3s = GetVectors(s);
	print("Q Vectors=" + v3s.length);
	for (var v: Vector3 in v3s){
		print(v.ToString());
	}
}

If you can guarantee that the input is always in that format, then you can use String.Split and parseFloat:

function ParseVector3String (input : String) : Vector3[] {
	var stringArray = input.Substring (1, input.Length-2).Split ([")("], System.StringSplitOptions.RemoveEmptyEntries);
	var v3Array = new Vector3[stringArray.Length];
	for (var i = 0; i < stringArray.Length; i++) {
		var numbers = stringArray*.Split(","[0]);*

_ v3Array = Vector3(parseFloat(numbers[0]), parseFloat(numbers[1]), parseFloat(numbers[2]));_
* }*
* return v3Array;*
}

I think using regex is the most maintainable solution.

var numbers = new Regex(@"[\-]?\d*\.\d*")
	.Matches(numbersString).Cast<Match>()
	.Select( n => float.Parse(n.Value) ).ToArray();
var vector3s = new Vector3[numbers.Length / 3];
for (int vectorIndex = 0; vectorIndex < vector3s.Length; ++vectorIndex)
	for (int componentIndex = 0; componentIndex < 3; ++componentIndex)
		vector3s[vectorIndex][componentIndex] = 
			numbers[vectorIndex * 3 + componentIndex];