Math-ThinPlateSpline

 view release on metacpan or  search on metacpan

src/linalg3d.cc  view on Meta::CPAN

/*
 *  One file long C++ library of linear algebra primitives for
 *  simple 3D programs
 *
 *  Copyright (C) 2001-2003 by Jarno Elonen
 *
 *  Permission to use, copy, modify, distribute and sell this software
 *  and its documentation for any purpose is hereby granted without fee,
 *  provided that the above copyright notice appear in all copies and
 *  that both that copyright notice and this permission notice appear
 *  in supporting documentation.  The authors make no representations
 *  about the suitability of this software for any purpose.
 *  It is provided "as is" without express or implied warranty.
 *
 *  Slight modifications:
 *    - introduce namespace
 *    - use M_PI instead of private constant
 *    - split in .cc and .h
 *    Copyright (C) 2010 by Steffen Mueller (smueller -at- cpan -dot- org)
 */

#include "linalg3d.h"
#include <cmath>
#include "TPSException.h"

#define Deg2Rad(Ang) ((float)( Ang * M_PI / 180.0 ))
#define Rad2Deg(Ang) ((float)( Ang * 180.0 / M_PI ))

using namespace TPS;

// Left hand float multplication
inline Vec TPS::operator* ( const float src, const Vec& v ) { Vec tmp(v); return (tmp *= src); }

// Dot product
inline float TPS::dot(const Vec& a, const Vec& b) { return a.x*b.x + a.y*b.y + a.z*b.z; }

// Cross product
inline Vec TPS::cross(const Vec &a, const Vec &b) {
  return Vec( a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x );
}

// streaming
std::ostream&
TPS::operator<<(std::ostream& stream, Vec const& obj)
{
  obj.WriteTo(stream);
  return stream;
}

std::istream&
TPS::operator>>(std::istream& stream, Vec& obj)
{
  if (!stream.good())
    throw EndOfFileException();
  stream >> obj.x;
  if (!stream.good())
    throw EndOfFileException();
  stream >> obj.y;
  if (!stream.good())
    throw EndOfFileException();
  stream >> obj.z;
  return stream;
}



// Creates an identity matrix
Mtx::Mtx()
{
  for ( int i = 0; i < 16; ++i )
    data[ i ] = 0;
  data[ 0 + 0 ] = data[ 4 + 1 ] = data[ 8 + 2 ] = data[ 12 + 3 ] = 1;
}

// Returns the transpose of this matrix
Mtx Mtx::transpose() const
{
  Mtx m;
  int idx = 0;
  for ( int row = 0; row < 4; ++row ) {
    for ( int col = 0; col < 4; ++col, ++idx )
      m.data[ idx ] = data[ row + col*4 ];
  }
  return m;
}

// Creates a scale matrix
Mtx TPS::scale(const Vec& scale)
{
  Mtx m;
  m.data[0+0] = scale.x;
  m.data[4+1] = scale.y;
  m.data[8+2] = scale.z;
  return m;
}

// Creates a translation matrix
Mtx TPS::translate(const Vec& moveAmt)
{
  Mtx m;
  m.data[0+3] = moveAmt.x;
  m.data[4+3] = moveAmt.y;



( run in 0.588 second using v1.01-cache-2.11-cpan-6aa56a78535 )