What do most people do when creating functions such as vector or quaternion normalizers? I mean, do they usually return the new vector / quat. or automatically set the vector to it? Thanks for any opinions!
This is a discussion on Certain functions within the C++ Programming forums, part of the General Programming Boards category; What do most people do when creating functions such as vector or quaternion normalizers? I mean, do they usually return ...
What do most people do when creating functions such as vector or quaternion normalizers? I mean, do they usually return the new vector / quat. or automatically set the vector to it? Thanks for any opinions!
Do not make direct eye contact with me.
I would imagine passing the vector to the function as a reference/pointer would be faster than returning it, as you don't have to create a variable for returning. So if the code is in a hurry I would guess the pointer makes more sense.
SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
I say what I say, I mean what I mean.
IDE: emacs + make + gcc and proud of it.
i usually pass a pointer argument and use set it directly. this can have problems in dlls i think
Try to help all less knowledgeable than yourself, within
the limits provided by time, complexity and tolerance.
- Nor
I think in general it's best not to create so many copies and just use it when you have to. Here's an old piece of code that demonstrates a typical canonical form used often with C++ objects (though some of the maths are probably off). The underlying implementation is written much like normal C functions. This just makes it more flexible (though it would be even better to place the functions in an 'extern "C" {' block, and get rid of any other C++ artifacts, too, like by switching the references with pointers, etc).
Code:#ifndef VECTOR_C #define VECTOR_C namespace psi { /* 'vector' is a 3D point object */ struct vector { vector(double x0 = 0, double y0 = 0, double z0 = 0); inline vector & set(double x0, double y0, double z0); inline vector & operator () (double x0, double y0, double z0); inline vector & operator = (const vector & other); inline vector & add(const vector & other); inline vector & operator += (const vector & other); inline vector & subtract(const vector & other); inline vector & operator -= (const vector & other); inline vector & multiply(const vector & other); inline vector & operator *= (const vector & other); inline vector & divide(const vector & other); inline vector & operator /= (const vector & other); inline const vector plus(const vector & other) const; inline const vector operator + (const vector & other) const; inline const vector minus(const vector & other) const; inline const vector operator - (const vector & other) const; inline const vector times(const vector & other) const; inline const vector operator * (const vector & other) const; inline const vector quotient(const vector & other) const; inline const vector operator / (const vector & other) const; inline double product(const vector & other) const; inline double angle(const vector & other) const; inline double product() const; inline double length() const; inline vector & normalize(); friend ostream & operator << (ostream & stream, vector & value) { return stream << "x( " << value.x << " ) y( " << value.y << " ) z( " << value.z << " )"; } friend istream & operator >> (istream & stream, vector & value) { while(!(stream >> value.x)) { stream.clear(); stream.ignore(); } while(!(stream >> value.y)) { stream.clear(); stream.ignore(); } while(!(stream >> value.z)) { stream.clear(); stream.ignore(); } if(stream.peek() == '\n') stream.get(); return stream; } public: double x, y, z; }; /* C-style inline functions */ inline vector & add(vector & dest, const vector & a, const vector & b) { dest.x = a.x + b.x; dest.y = a.y + b.y; dest.z = a.z + b.z; return dest; } inline vector & add(vector & dest, const vector & src) { return add(dest, dest, src); } inline vector & subtract(vector & dest, const vector & a, const vector & b) { dest.x = a.x - b.x; dest.y = a.y - b.y; dest.z = a.z - b.z; return dest; } inline vector & subtract(vector & dest, const vector & src) { return subtract(dest, dest, src); } inline vector & multiply(vector & dest, const vector & a, const vector & b) { dest.x = a.x * b.x; dest.y = a.y * b.y; dest.z = a.z * b.z; return dest; } inline vector & multiply(vector & dest, const vector & src) { return multiply(dest, dest, src); } inline vector & divide(vector & dest, const vector & a, const vector & b) { dest.x = b.x == 0 ? 0 : a.x / b.x; dest.y = b.y == 0 ? 0 : a.y / b.y; dest.z = b.z == 0 ? 0 : a.z / b.z; return dest; } inline vector & divide(vector & dest, const vector & src) { return divide(dest, dest, src); } inline const vector plus(const vector & a, const vector & b) { vector temp(a); return vector(temp.add(b)); } inline const vector minus(const vector & a, const vector & b) { vector temp(a); return vector(temp.subtract(b)); } inline const vector times(const vector & a, const vector & b) { vector temp(a); return vector(temp.multiply(b)); } inline const vector quotient(const vector & a, const vector & b) { vector temp(a); return vector(temp.divide(b)); } inline double product(const vector & a, const vector & b) { return (a.x * b.x) + (a.y * b.y) + (a.z * b.z); } inline double product(const vector & a) { return product(a, a); } inline double length(const vector & a) { return sqrt(product(a)); } inline vector & normalize(vector & a) { double length = a.length(); vector temp(length, length, length); return a.divide(temp); } /* ??Yarghh! */ inline double angle(const vector & a, const vector & b) { double al = a.length(), bl = b.length(); return al && bl ? acos(product(a, b)/(al * bl)) : 0; } /* The implementation uses the inline C-style functions */ vector:: vector(double x0, double y0, double z0) { set(x0, y0, z0); } vector & vector:: set(double x0, double y0, double z0) { x = x0, y = y0, z = z0; return *this; } vector & vector:: operator () (double x0, double y0, double z0) { return set(x0, y0, z0); } vector & vector:: operator = (const vector & other) { return set(other.x, other.y, other.z); } vector & vector:: add(const vector & other) { return psi::add(*this, other); } vector & vector:: operator += (const vector & other) { return psi::add(*this, other); } vector & vector:: subtract(const vector & other) { return psi::subtract(*this, other); } vector & vector:: operator -= (const vector & other) { return psi::subtract(*this, other); } vector & vector:: multiply(const vector & other) { return psi::multiply(*this, other); } vector & vector:: operator *= (const vector & other) { return psi::multiply(*this, other); } vector & vector:: divide(const vector & other) { return psi::divide(*this, other); } vector & vector:: operator /= (const vector & other) { return psi::divide(*this, other); } const vector vector:: plus(const vector & other) const { return psi::plus(*this, other); } const vector vector:: operator + (const vector & other) const { return psi::plus(*this, other); } const vector vector:: minus(const vector & other) const { return psi::minus(*this, other); } const vector vector:: operator - (const vector & other) const { return psi::minus(*this, other); } const vector vector:: times(const vector & other) const { return psi::times(*this, other); } const vector vector:: operator * (const vector & other) const { return psi::times(*this, other); } const vector vector:: quotient(const vector & other) const { return psi::quotient(*this, other); } const vector vector:: operator / (const vector & other) const { return psi::quotient(*this, other); } double vector:: product(const vector & other) const { return psi::product(*this, other); } double vector:: product() const { return psi::product(*this); } double vector:: length() const { return psi::length(*this); } vector & vector:: normalize() { return psi::normalize(*this); } double vector:: angle(const vector & other) const { return psi::angle(*this, other); } } // namespace #endif // VECTOR_C
Code:int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000 <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000 )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}