Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / src / synfig / vector.h
index a279db8..46baff8 100644 (file)
@@ -1,37 +1,39 @@
-/* === S I N F G =========================================================== */
+/* === S Y N F I G ========================================================= */
 /*!    \file vector.h
 **     \brief Various discreet type definitions
 **
-**     $Id: vector.h,v 1.2 2005/01/23 04:03:21 darco Exp $
+**     $Id$
 **
 **     \legal
-**     Copyright (c) 2002 Robert B. Quattlebaum Jr.
+**     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2007 Chris Moore
 **
-**     This software and associated documentation
-**     are CONFIDENTIAL and PROPRIETARY property of
-**     the above-mentioned copyright holder.
+**     This package is free software; you can redistribute it and/or
+**     modify it under the terms of the GNU General Public License as
+**     published by the Free Software Foundation; either version 2 of
+**     the License, or (at your option) any later version.
 **
-**     You may not copy, print, publish, or in any
-**     other way distribute this software without
-**     a prior written agreement with
-**     the copyright holder.
+**     This package is distributed in the hope that it will be useful,
+**     but WITHOUT ANY WARRANTY; without even the implied warranty of
+**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+**     General Public License for more details.
 **     \endlegal
 */
 /* ========================================================================= */
 
 /* === S T A R T =========================================================== */
 
-#ifndef __SINFG_VECTOR_H
-#define __SINFG_VECTOR_H
+#ifndef __SYNFIG_VECTOR_H
+#define __SYNFIG_VECTOR_H
 
 /* === H E A D E R S ======================================================= */
 
+#include "angle.h"
 #include "real.h"
-#include <cmath>
+#include <math.h>
 
 /* === M A C R O S ========================================================= */
 
-#ifndef isnan
 
 #ifdef WIN32
 #include <float.h>
@@ -41,17 +43,23 @@ extern "C" { int _isnan(double x); }
 #endif
 #endif
 
-#ifdef __APPLE__
-#define isnan __isnanf
+// For some reason isnan() isn't working on macosx any more.
+// This is a quick fix.
+#if defined(__APPLE__) && !defined(SYNFIG_ISNAN_FIX)
+#ifdef isnan
+#undef isnan
 #endif
-
+inline bool isnan(double x) { return x != x; }
+inline bool isnan(float x) { return x != x; }
+#define SYNFIG_ISNAN_FIX 1
 #endif
 
+
 /* === T Y P E D E F S ===================================================== */
 
 /* === C L A S S E S & S T R U C T S ======================================= */
 
-namespace sinfg {
+namespace synfig {
 
 /*!    \class Vector
 **     \todo writeme
@@ -65,19 +73,19 @@ private:
        value_type _x, _y;
 
 public:
-       Vector() { };
+       Vector(): _x(0.0), _y(0.0) { };
        Vector(const value_type &x, const value_type &y):_x(x),_y(y) { };
 
        bool is_valid()const { return !(isnan(_x) || isnan(_y)); }
-       
+
        value_type &
        operator[](const int& i)
-       { return (&_x)[i] ; }
+       { return i?_y:_x; }
 
        const value_type &
        operator[](const int& i) const
-       { return (&_x)[i] ; }
-       
+       { return i?_y:_x; }
+
        const Vector &
        operator+=(const Vector &rhs)
        {
@@ -85,7 +93,7 @@ public:
                _y+=rhs._y;
                return *this;
        }
-       
+
        const Vector &
        operator-=(const Vector &rhs)
        {
@@ -93,7 +101,7 @@ public:
                _y-=rhs._y;
                return *this;
        }
-       
+
        const Vector &
        operator*=(const value_type &rhs)
        {
@@ -101,7 +109,7 @@ public:
                _y*=rhs;
                return *this;
        }
-       
+
        const Vector &
        operator/=(const value_type &rhs)
        {
@@ -110,7 +118,7 @@ public:
                _y*=tmp;
                return *this;
        }
-               
+
        Vector
        operator+(const Vector &rhs)const
                { return Vector(*this)+=rhs; }
@@ -138,15 +146,15 @@ public:
        bool
        operator==(const Vector &rhs)const
                { return _x==rhs._x && _y==rhs._y; }
-       
+
        bool
        operator!=(const Vector &rhs)const
                { return _y!=rhs._y || _x!=rhs._x; }
-       
+
        //! Returns the squared magnitude of the vector
        value_type mag_squared()const
                { return _x*_x+_y*_y; }
-       
+
        //! Returns the magnitude of the vector
        value_type mag()const
                { return sqrt(mag_squared()); }
@@ -162,14 +170,17 @@ public:
        //! Returns a perpendicular version of the vector
        Vector perp()const
                { return Vector(_y,-_x); }
-               
+
+       Angle angle()const
+               { return Angle::rad(atan2(_y, _x)); }
+
        bool is_equal_to(const Vector& rhs)const
        {
                static const value_type epsilon(0.0000000000001);
 //             return (_x>rhs._x)?_x-rhs._x<=epsilon:rhs._x-_x<=epsilon && (_y>rhs._y)?_y-rhs._y<=epsilon:rhs._y-_y<=epsilon;
                return (*this-rhs).mag_squared()<=epsilon;
        }
-       
+
        static const Vector zero() { return Vector(0,0); }
 };
 
@@ -180,12 +191,12 @@ typedef Vector Point;
 
 
 
-}; // END of namespace sinfg
+}; // END of namespace synfig
 
 namespace std {
 
-inline sinfg::Vector::value_type
-abs(const sinfg::Vector &rhs)
+inline synfig::Vector::value_type
+abs(const synfig::Vector &rhs)
        { return rhs.mag(); }
 
 }; // END of namespace std
@@ -195,19 +206,19 @@ abs(const sinfg::Vector &rhs)
 _ETL_BEGIN_NAMESPACE
 
 template <>
-class bezier_base<sinfg::Vector,float> : public std::unary_function<float,sinfg::Vector>
+class bezier_base<synfig::Vector,float> : public std::unary_function<float,synfig::Vector>
 {
 public:
-       typedef sinfg::Vector value_type;
+       typedef synfig::Vector value_type;
        typedef float time_type;
 private:
 
-       bezier_base<sinfg::Vector::value_type,time_type> bezier_x,bezier_y;
+       bezier_base<synfig::Vector::value_type,time_type> bezier_x,bezier_y;
 
        value_type a,b,c,d;
 
 protected:
-       affine_combo<value_type,time_type> affine_func; 
+       affine_combo<value_type,time_type> affine_func;
 
 public:
        bezier_base() { }
@@ -215,7 +226,7 @@ public:
                const value_type &a, const value_type &b, const value_type &c, const value_type &d,
                const time_type &r=0.0, const time_type &s=1.0):
                a(a),b(b),c(c),d(d) { set_rs(r,s); sync(); }
-               
+
        void sync()
        {
                bezier_x[0]=a[0],bezier_y[0]=a[1];
@@ -229,13 +240,13 @@ public:
        value_type
        operator()(time_type t)const
        {
-               return sinfg::Vector(bezier_x(t),bezier_y(t));
+               return synfig::Vector(bezier_x(t),bezier_y(t));
        }
-       
+
        void evaluate(time_type t, value_type &f, value_type &df) const
        {
                t=(t-get_r())/get_dt();
-               
+
                const value_type p1 = affine_func(
                                                        affine_func(a,b,t),
                                                        affine_func(b,c,t)
@@ -244,7 +255,7 @@ public:
                                                        affine_func(b,c,t),
                                                        affine_func(c,d,t)
                                                ,t);
-               
+
                f = affine_func(p1,p2,t);
                df = (p2-p1)*3;
        }
@@ -265,7 +276,7 @@ public:
        { return (&a)[i]; }
 
        //! Bezier curve intersection function
-       time_type intersect(const bezier_base<value_type,time_type> &x, time_type near=0.0)const
+       time_type intersect(const bezier_base<value_type,time_type> &/*x*/, time_type /*near*/=0.0)const
        {
                return 0;
        }