Thread: Certain functions

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    Certain functions

    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.

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    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.

  3. #3
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    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

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM