Changeset 1052


Ignore:
Timestamp:
Nov 7, 2011, 1:36:25 AM (9 years ago)
Author:
sam
Message:

core: implement complex numbers and add unit tests for these.

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/matrix.cpp

    r1048 r1052  
    108108}
    109109
     110template<> void cmplx::printf() const
     111{
     112    Log::Debug("[ %6.6f %6.6f ]\n", x, y);
     113}
     114
    110115template<> void vec3::printf() const
    111116{
     
    153158}
    154159
     160template<> std::ostream &operator<<(std::ostream &stream, icmplx const &v)
     161{
     162    return stream << "(" << v.x << ", " << v.y << ")";
     163}
     164
    155165template<> std::ostream &operator<<(std::ostream &stream, ivec3 const &v)
    156166{
     
    171181
    172182template<> std::ostream &operator<<(std::ostream &stream, vec2 const &v)
     183{
     184    return stream << "(" << v.x << ", " << v.y << ")";
     185}
     186
     187template<> std::ostream &operator<<(std::ostream &stream, cmplx const &v)
    173188{
    174189    return stream << "(" << v.x << ", " << v.y << ")";
  • trunk/src/matrix.h

    r1048 r1052  
    3939
    4040VECTOR_TYPES(Vec2, vec2)
     41VECTOR_TYPES(Cmplx, cmplx)
    4142VECTOR_TYPES(Vec3, vec3)
    4243VECTOR_TYPES(Vec4, vec4)
     
    117118    \
    118119    void printf() const;
     120
     121#define COMPLEX_OPS() \
     122    inline type_t operator *(type_t const &val) const \
     123    { \
     124        return type_t(x * val.x - y * val.y, x * val.y + y * val.x); \
     125    } \
     126    \
     127    inline type_t operator *=(type_t const &val) \
     128    { \
     129        return *this = (*this) * val; \
     130    } \
     131    \
     132    inline type_t operator ~() const \
     133    { \
     134        return type_t(x, -y); \
     135    } \
     136    \
     137    inline T norm() const { return len(); }
    119138
    120139#define QUATERNION_OPS() \
     
    259278
    260279/*
     280 * 2-element complexes
     281 */
     282
     283template <typename T> struct Cmplx
     284{
     285    typedef Cmplx<T> type_t;
     286
     287    inline Cmplx() { }
     288    inline Cmplx(T val) : x(val), y(0) { }
     289    inline Cmplx(T _x, T _y) : x(_x), y(_y) { }
     290
     291    LINEAR_OPS()
     292    COMPLEX_OPS()
     293
     294#if !defined __ANDROID__
     295    template<typename U>
     296    friend std::ostream &operator<<(std::ostream &stream, Cmplx<U> const &v);
     297#endif
     298
     299    T x, y;
     300};
     301
     302template<typename T>
     303static inline Cmplx<T> re(Cmplx<T> const &val)
     304{
     305    return ~val / val.sqlen();
     306}
     307
     308template<typename T>
     309static inline Cmplx<T> operator /(T x, Cmplx<T> const &y)
     310{
     311    return x * re(y);
     312}
     313
     314template<typename T>
     315static inline Cmplx<T> operator /(Cmplx<T> x, Cmplx<T> const &y)
     316{
     317    return x * re(y);
     318}
     319
     320/*
    261321 * 3-element vectors
    262322 */
     
    400460
    401461GLOBALS(Vec2)
     462GLOBALS(Cmplx)
    402463GLOBALS(Vec3)
    403464GLOBALS(Vec4)
  • trunk/test/Makefile.am

    r1047 r1052  
    2525testsuite_SOURCES = testsuite.cpp \
    2626    unit/matrix.cpp unit/half.cpp unit/trig.cpp unit/build.cpp \
    27     unit/real.cpp unit/image.cpp unit/quat.cpp
     27    unit/real.cpp unit/image.cpp unit/quat.cpp unit/cmplx.cpp
    2828testsuite_CPPFLAGS = @LOL_CFLAGS@ @PIPI_CFLAGS@
    2929testsuite_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ @PIPI_LIBS@
Note: See TracChangeset for help on using the changeset viewer.