以前以为vt中如果使用vsl来进行数据类型间的转换会像c语言类似的那种,用前置括符加个数据类型强制进行转换…… 但是确不是的。后来没办法,搜吧,资料又少 @_@ 功夫不负有心人 ... 在Virtools Community Swap-Meet 上得到答案,当时看到就对应到使用了,后来又陆续用了几次,于是特挂贴于此,以后懒得去翻了,主要是theswapmeet 网站并入到 3dvia 里了,搞得我重新注册了一次 -_-!!!
原文地址如下:
http://www.3dvia.com/forums/topic/conversion-between-string-float-integer-vector-in-vsl#post-35384
VT里各数据类型间的转换:
// Convert Vector to float
Vector vec;
float x = vec.x,
y = vec.y,
z = vec.z;
// ==================
// Convert Float to Int
float fNum = 1.1f;
int nTrunc = floori( fNum ); // truncated
int nRound = floori( fNum + 0.5f ); // rounded
// ==================
// Convert int to float
float x = nRound; // cast not needed, no possible data loss
// ==================
// String to Int / Float (as mentioned)
String strNum = "1";
int nNum = strNum.ToInt();
strNum = "1.1";
float fNum = strNum.ToFloat();
// ==================
// Int/Float to String
int nInt = 1337;
float fFloat = 13.37;
String strNum = ""; // make sure string is empty if you want just the number
strNum += nInt; // to get int, appended to end of string
strNum = "";
strNum += fFloat // to get float. You must crop the extra zeros manually afterwards
// ==================
// String to vector
// This will depend on how you want to represent your vector
// I'll assume 3D Vector and String like this "[#,#,#]" where # are floats
// you could design the algorithm to be more flexible if you want.
String strVec = "[1.0,2.0,3.0]";
Vector vec;
String strTemp,// string for holding the number to be converted
strCopy = strVec; // copy the string for holding the parts not yet processed
int nComma; // position of a comma in the string
strCopy.Cut(0,1); // take off the first [
strCopy.Cut( strCopy.Length()-1, 1 ); // take off the last ]
strTemp = strCopy; // copy to our temp string to cut it down to just one number
nComma = strTemp.Find(',',0); // find the first comma
strTemp.Cut( nComma, strTemp.Length()-nComma); // remove the first comma and everything after it
vec.x = strTemp.ToFloat();
strCopy.Cut(0,nComma+1); // remove everything up to and including the first comma since we're done with that part
strTemp = strCopy; // copy what remains to our temp for the next number
nComma = strTemp.Find(',',0); // find the second comma (now the first comma)
strTemp.Cut( nComma, strTemp.Length()-nComma ); // remove the comma and everything after it
vec.y = strTemp.ToFloat();
strCopy.Cut(0,nComma+1); // remove everything up to and including the comma since we're done with that part
// now strCopy just contains the final number
vec.z = strCopy.ToFloat(); // And we're done!
// ==================
// To Convert a vector to a string, just append parts together
Vector vec(1.0f, 2.0f, 3.0f);
String strVec = "[";
strVec += vec.x; strVec += ",";
strVec += vec.y; strVec += ",";
strVec += vec.z; strVec += "]";
// Result: "[1.000000,2.000000,3.000000]" so you might want to put each
// number into a string and crop it first
vt的转换看来要对应规范下,不知道是不是考虑到数据处理的安全性问题,所以才没有弄成类似c的...
已经很少碰纯代码了…… 记错了 莫怪 6_6
CR 于 2009-11-3 0:09:21
没有评论:
发表评论