Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by crazy_boy_02 · Mar 29, 2013 at 11:23 AM · vector3arraysconvert

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · Mar 29, 2013 at 03:29 PM

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());
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image crazy_boy_02 · Mar 29, 2013 at 03:43 PM 0
Share

@aldonaletto : thanks a lot for you effort and help, but i figured out some other way of solving my problem.

anyways some good info for future...

avatar image
3

Answer by Eric5h5 · Mar 29, 2013 at 03:55 PM

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[i].Split(","[0]);
         v3Array[i] = Vector3(parseFloat(numbers[0]), parseFloat(numbers[1]), parseFloat(numbers[2]));
     }
     return v3Array;
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scribe · Mar 29, 2013 at 05:49 PM 0
Share

where do you find references for things like

someString.Split([")("], System.StringSplitOptions.RemoveEmptyEntries);
I can't find anything similar in the unity references. Do I need to delve into unitys coding to find this?

Thanks, Scribe

avatar image crazy_boy_02 · Mar 29, 2013 at 05:52 PM 1
Share

you will not get all the help in unity references for all the other help refer to

http://msdn.microsoft.com/en-us/library

for string.split http://msdn.microsoft.com/en-us/library/b873y76a.aspx

avatar image Eric5h5 · Mar 29, 2013 at 05:55 PM 2
Share

Unity doesn't document the $$anonymous$$ono/.NET stuff because it would be redundant; those docs already exist as mentioned by crazy_boy_02.

avatar image Scribe · Mar 29, 2013 at 05:55 PM 0
Share

Great, thanks for the links

avatar image
2

Answer by Jessy · Mar 29, 2013 at 09:04 PM

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];
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

SendMessage - how to send 2 details? - 2 Vector3's actually 1 Answer

Collecting MASSIVE Array of Vector3 Points 1 Answer

How do I convert angle to vector3? 1 Answer

Using a variable as a components for Vector3 1 Answer

C# Convert Vector3[] to Vector2[] 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges