Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////
- //+ VECTOR CLASS
- ///////////////////////////////
- class Vec3
- {
- float x; float y; float z;
- //Constructors
- Vec3()
- { x = 0; y = 0; z = 0; }
- Vec3(float afX, float afY, float afZ)
- { x = afX; y = afY; z=afZ; }
- Vec3(string &in asVecName)
- { load(asVecName); }
- Vec3(float[] values)
- { x = values[0]; y = values[1]; z = values[2]; }
- Vec3(Vec3 &in v1)
- { x = v1.x; y = v1.y; z = v1.z; }
- //Save/load vector from a variable
- void save(string &in asVecName)
- {
- SetLocalVarFloat(asVecName + "_VEC3_X",x);
- SetLocalVarFloat(asVecName + "_VEC3_Y",y);
- SetLocalVarFloat(asVecName + "_VEC3_Z",z);
- }
- void saveGlobal(string &in asVecName)
- {
- SetGlobalVarFloat(asVecName + "_VEC3_X",x);
- SetGlobalVarFloat(asVecName + "_VEC3_Y",y);
- SetGlobalVarFloat(asVecName + "_VEC3_Z",z);
- }
- void load(string &in asVecName)
- {
- x = GetLocalVarFloat(asVecName + "_VEC3_X");
- y = GetLocalVarFloat(asVecName + "_VEC3_Y");
- z = GetLocalVarFloat(asVecName + "_VEC3_Z");
- }
- void loadGlobal(string &in asVecName)
- {
- x = GetGlobalVarFloat(asVecName + "_VEC3_X");
- y = GetGlobalVarFloat(asVecName + "_VEC3_Y");
- z = GetGlobalVarFloat(asVecName + "_VEC3_Z");
- }
- //Indexing Overload
- float opIndex(uint idx)
- {
- switch(idx)
- {
- case 0:
- return x;
- case 1:
- return y;
- case 2:
- return z;
- }
- return 0;
- }
- void opIndex(uint idx, float value)
- {
- switch(idx)
- {
- case 0:
- x = value;
- case 1:
- y = value;
- case 2:
- z = value;
- }
- }
- // Equality operator
- bool opEquals(const Vec3 &in v1) const
- { return (x == v1.x) && (y == v1.y) && (z == v1.z); }
- // Compound assignment operators
- void opAddAssign(const Vec3 &in v1)
- { x = v1.x; y = v1.y; z=v1.z; }
- void opSubAssign(const Vec3 &in v1)
- { x = v1.x; y = v1.y; z=v1.z; }
- // Math operators
- Vec3 opAdd(const Vec3 &in v1) const
- { return Vec3(x+v1.x,y+v1.y,z+v1.z); }
- Vec3 opAdd(const float &in n) const
- { return Vec3(x+n,y+n,z+n); }
- Vec3 opAdd(const int &in n) const
- { return Vec3(x+n,y+n,z+n); }
- Vec3 opSub(const Vec3 &in v1) const
- { return Vec3(x-v1.x,y-v1.y,z-v1.z); }
- Vec3 opSub(const float &in n) const
- { return Vec3(x-n,y-n,z-n); }
- Vec3 opSub(const int &in n) const
- { return Vec3(x-n,y-n,z-n); }
- //Dot Product / Multiplication
- float opMul(const Vec3 &in v1) const
- { return (x*v1.x) + (y*v1.y) + (z*v1.z); }
- Vec3 opMul(const float &in n) const
- { return Vec3(x*n,y*n,z*n); }
- Vec3 opMul(const int &in n) const
- { return Vec3(x*n,y*n,z*n); }
- //Cross product (^)
- Vec3 opXor(const Vec3 &in v1) const
- { return Vec3( y*v1.z - v1.y*z,
- z*v1.x - v1.z*x,
- x*v1.y - v1.x*y); }
- Vec3 opDiv(const float &in n) const
- { return Vec3(x/n,y/n,z/n); }
- Vec3 opDiv(const int &in n) const
- { return Vec3(x/n,y/n,z/n); }
- //Length
- float lengthSQR()
- { return x*x + y*y + z*z; }
- float length()
- { return sqrt(x*x + y*y + z*z); }
- Vec3 normalised()
- { float n = length(); return Vec3(x/n,y/n,z/n); }
- //Named dot/cross product
- float dot(const Vec3 &in v1)
- { return opMul(v1); }
- Vec3 cross(const Vec3 &in v1)
- { return opXor(v1); }
- //Display
- string ToString()
- { return "(" + x + "," + y + "," + z + ")"; }
- //Parsing from a string representation. Returns true iff successful.
- bool FromString(string &in asString)
- {
- float[] vals = parseStringToFloatArray(asString);
- if(vals.length() < 3)
- { AddDebugMessage("Invalid dimensions in vector parsed from array: " + asString,false);
- return false; }
- x = vals[0]; y=vals[1]; z=vals[2];
- return true;
- }
- }
- ///////////////////////////////
- //- VECTOR CLASS
- ///////////////////////////////
- ///////////////////////////////
- //+ MATH - Remove this section if you are already using MATH Library
- ///////////////////////////////
- const uint32 _SQRTITERATIONS=16; //Maximum number of iterations for sqrt computation
- const float _SQRTDELTA=0.00001f; //Margin of error allowable if complete before iteration ceiling
- bool approx(float &in x, float &in y, float &in epsilon)
- {
- float delta = x-y;
- return ((delta>0?delta:-delta) <= (epsilon>0?epsilon:-epsilon));
- }
- float sqrt(float &in x)
- {
- if(x<=0) return 0; //Early out - not valid input.
- uint32 i = 0;
- float o = x * 0.5f;
- while( i<_SQRTITERATIONS && !approx(o*o,x,_SQRTDELTA) && o != 0)
- {
- o = 0.5f * (o + x/o);
- i++;
- }
- return o;
- }
- ///////////////////////////////
- //- MATH
- ///////////////////////////////
- ///////////////////////////////
- //+ String Parsing - Remove this section if you are already using PARSING Library
- ///////////////////////////////
- float[] parseStringToFloatArray(string &in asString)
- {
- float[] output = {};
- int integer = 0; //Integer part of the number
- float decimal = 0; //Decimal part of the number
- int exponent = 0; //Exponent of the number
- bool negativeInteger = false; //Is the integer part negative
- bool negativeExponent = false; //Is the exponent part negative
- double dMul = 1; //Multiplier for the decimal
- bool readNumber = false; //Read a number yet or not (stops +. etc returning 0's).
- //Parsing loop
- int state = 0;
- string temp = asString + "!"; //Add an invalid character to end of string
- for(uint i=0; i<temp.length(); i++)
- {
- //Determine properties about this character
- uint8 chr = temp[i];
- int digit = _parseDigit(chr);
- int sign = _parseSign(chr);
- bool isExponent = _parseHasExponent(chr);
- bool isDecimalP = _parseHasDecimalPoint(chr);
- bool isWhitesp = _parseHasWhitespace(chr);
- bool isDigit = digit != -1;
- switch(state)
- {
- case 0: //Initial state
- {
- if(sign != 0) { negativeInteger=(sign<0); integer =0; state=1; } //Read +/-XX
- else if(isDigit) { integer = digit; state = 1; readNumber=true; } //Read N
- else if(isDecimalP){ state = 2; } //Read .XX -> Assume 0.XX
- break;
- }
- case 1: //Read the number part of the float
- {
- if(isDigit) { integer = (integer * 10) + digit; readNumber=true; } //Read NNXX
- else if(isDecimalP) { state = 2; } //Read NN.XX
- else if(isExponent && readNumber) { state = 3; } //Read NNeXX -> assume NN.0eXX
- else if(readNumber) { state = -1; } //Finished reading the number.
- else { state = 0; }
- break;
- }
- case 2: //Read the decimal part of the float
- {
- if(isDigit) { decimal += float(digit) * dMul; dMul *= 0.1; readNumber=true; }
- else if(isExponent && readNumber) { state = 3; }
- else if(readNumber) { state = -1; }
- else { state = 0; }
- break;
- }
- case 3: //Ignore a single whitespace char after the exponent.
- {
- if(isDigit) { exponent = digit; state = 5; } //Read NNeXX
- else if(isWhitesp) { state = 4; } //Read NNe XX
- else if(sign != 0) { negativeExponent=(sign<0); state = 5; }
- else { state = -1; }
- break;
- }
- case 4: //Read a +/- Sign or a digit for the exponent
- {
- if(isDigit) { exponent = digit; state = 5; } //Read NNe NX
- else if(sign != 0) { negativeExponent=(sign<0); state = 5; } //Read NNe +/-XX
- else { state = -1; }
- break;
- }
- case 5: //Reading the exponent
- {
- if(isDigit) { exponent = (exponent * 10) + digit; } //Read NNe NX
- else { state = -1; }
- break;
- }
- }
- //At the end of a number?
- if(state < 0)
- {
- //Construct the number & add it to the output array
- int index = output.length(); output.resize(index+1);
- output[index] = _parseFloatHelper(integer,decimal,exponent,negativeInteger,negativeExponent);
- //Reset parsing state
- state = 0; integer=0; decimal=0; exponent=0; dMul =1;
- negativeInteger=false; negativeExponent=false; readNumber=false;
- //This terminating char was possibly the start of next number. Start from this character
- if(isDecimalP || sign!=0) i-=1;
- }
- }
- return output;
- }
- float _parseFloatHelper(int integer, float decimal, int exponent, bool negI, bool negE)
- {
- //Calculate the number with no exponent applied
- float x = float(integer) + decimal * 0.1f;
- //Apply 10^E
- float mul = negE?0.1f:10.0f;
- for(int i=0; i<exponent; i++)
- x = x*mul;
- //Apply sign of the number
- return negI?-x:x;
- }
- //+ Parsing Helper Functions
- int _parseDigit(uint8 digit) {
- int d = digit-48; //48 is ASCII code for 0
- return ((d >= 0)&&(d<=9)) ? d : -1;
- }
- int _parseSign(uint8 chr) {
- if (chr == 45) return -1;
- else if(chr == 43) return 1;
- else return 0;
- }
- bool _parseHasExponent(uint8 chr) { return (chr == 69) || (chr == 101); }
- bool _parseHasDecimalPoint(uint8 chr) { return (chr==46); }
- bool _parseHasWhitespace(uint8 chr) { return (chr == 43)||(chr == 32); }
- //-
- ///////////////////////////////
- //- String Parsing
- ///////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement