Commit 09c49f5d authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Fixing Windows CL18 build errors about inline methods. dos2unix also.

parent 8d1a4775
/*
* File: vector3d.cc
* Author: Jordan P. Lefebvre, lefebvre.jordan@gmail.com
*
* Created on August 9, 2012, 6:06 PM
*/
#include <algorithm>
#include <cmath> // for sqrt
#include <iomanip>
#include <sstream>
#include "radixbug/bug.hh"
#include "radixmath/vector3d.hh"
#include "radixmath/normal.hh"
#include "radixmath/point3d.hh"
#include "radixmath/matrix.hh"
namespace radix
{
/*
* Default constructor
*/
Vector3D::Vector3D(void)
: x(0.0), y(0.0), z(0.0) {
}
/**
* Constructor
*/
Vector3D::Vector3D(Real a)
: x(a), y(a), z(a) {
}
/**
* Constructor
*/
Vector3D::Vector3D(Real _x, Real _y, Real _z)
: x(_x), y(_y), z(_z) {
}
/**
* Copy constructor
*/
Vector3D::Vector3D(const Vector3D& vector)
: x(vector.x), y(vector.y), z(vector.z) {
}
/**
* Constructor
* @brief constructs a vector from a normal
*/
Vector3D::Vector3D(const Normal& n)
: x(n.x), y(n.y), z(n.z) {
}
/**
* Constructor
* @brief constructs a vector from a point
* this is used in the ConcaveHemisphere hit functions
*/
Vector3D::Vector3D(const Point3D& p)
: x(p.x), y(p.y), z(p.z) {
}
/**
* Destructor
*/
Vector3D::~Vector3D(void) {
}
/**
* Assignment operator
*/
Vector3D& Vector3D::operator=(const Vector3D& rhs) {
if (this == &rhs)
return (*this);
x = rhs.x;
y = rhs.y;
z = rhs.z;
return (*this);
}
bool Vector3D::operator==(const Vector3D& rhs)const{
if( x == rhs.x && y == rhs.y && z == rhs.z ) return true;
return false;
}
bool Vector3D::operator!=(const Vector3D& rhs)const{
if( x == rhs.x && y == rhs.y && z == rhs.z ) return false;
return false;
}
/**
* Assignment operator
* @brief assign a Normal to a vector
*/
Vector3D& Vector3D::operator=(const Normal& rhs) {
x = rhs.x;
y = rhs.y;
z = rhs.z;
return (*this);
}
/**
* Assignment operator
* @brief assign a point to a vector
*/
Vector3D& Vector3D::operator=(const Point3D& rhs) {
x = rhs.x;
y = rhs.y;
z = rhs.z;
return (*this);
}
std::string Vector3D::toString()const{
std::stringstream stream;
stream<<std::setprecision(13)<<"["<<x<<", "<<y<<", "<<z<<"] length="<<length();
return stream.str();
}
/**
* Length
* @brief Length of the vector
*/
Real Vector3D::length(void)const {
return (std::sqrt(x * x + y * y + z * z));
}
/**
* Normalize
* @brief converts the vector to a unit vector
*/
void Vector3D::normalize(void) {
Real length = std::sqrt(x * x + y * y + z * z);
radix_check(length > 0);
x /= length;
y /= length;
z /= length;
}
/**
* Normalized unit vector
* @brief converts the vector to a unit vector and returns the vector
*/
Vector3D& Vector3D::normalized_vector(void) {
Real length = std::sqrt(x * x + y * y + z * z);
x /= length;
y /= length;
z /= length;
return (*this);
}
Vector3D Vector3D::orthogonal(void)const{
Real orthogonal[3];
static const int UNSET = -1;
static const int UNCHANGED_INDEX=0;
static const int NEGATED_INDEX=1;
static const int ZEROED_INDEX=2;
int indices[]={UNSET,UNSET,UNSET}; // indices of values to swap, initialized to as UNSET
// index[0] : unchanged value
// index[1] : negated value
// index[2] : zeroed value
radix_insist( !( x==y && y==z && z==0 ), "Cannot obtain an orthogonal vector from a zero vector!" );
orthogonal[0] = x;
orthogonal[1] = y;
orthogonal[2] = z;
// loop over vector variables and determine which to zero, which to negate, and which to leave unchanged
for( int i = 0; i < 3; i++ ){
// if the index is zero and we have not already chosen an index to zero
// choose this index as the index to be zeroed
if( orthogonal[i] == 0.0 && indices[ZEROED_INDEX] == UNSET){
indices[ZEROED_INDEX] = i;
}else if( orthogonal[i] != 0.0 && indices[NEGATED_INDEX] == UNSET){
indices[NEGATED_INDEX] = i;
}else if( indices[ZEROED_INDEX] == UNSET){
indices[ZEROED_INDEX] = i;
}else{
radix_check( indices[UNCHANGED_INDEX] == UNSET );
indices[UNCHANGED_INDEX] = i;
}
}
radix_check( indices[UNCHANGED_INDEX] != UNSET );
radix_check( indices[NEGATED_INDEX] != UNSET );
radix_check( indices[ZEROED_INDEX] != UNSET );
// zero the index to be zeroed
orthogonal[indices[ZEROED_INDEX]] = 0.0;
// negate the index to be negated
orthogonal[indices[NEGATED_INDEX]] = -orthogonal[indices[NEGATED_INDEX]];
// the algorithm swaps the unchanged value with the negated value
std::swap(orthogonal[indices[NEGATED_INDEX]], orthogonal[indices[UNCHANGED_INDEX]]);
// return a vector that is orthogonal
return Vector3D(orthogonal[0],orthogonal[1],orthogonal[2]);
}
/**
* @funcion operator*
* @brief multiplication by a matrix on the left
*/
Vector3D operator*(const Matrix& mat, const Vector3D& v) {
return (Point3D(mat.matrix[0][0] * v.x + mat.matrix[0][1] * v.y + mat.matrix[0][2] * v.z,
mat.matrix[1][0] * v.x + mat.matrix[1][1] * v.y + mat.matrix[1][2] * v.z,
mat.matrix[2][0] * v.x + mat.matrix[2][1] * v.y + mat.matrix[2][2] * v.z));
}
std::ostream& operator<<(std::ostream& os, const Vector3D& v){
os<<v.toString();
return os;
}
} // namespace radix
/*
* File: vector3d.cc
* Author: Jordan P. Lefebvre, lefebvre.jordan@gmail.com
*
* Created on August 9, 2012, 6:06 PM
*/
#include <algorithm>
#include <cmath> // for sqrt
#include <iomanip>
#include <sstream>
#include "radixbug/bug.hh"
#include "radixmath/vector3d.hh"
#include "radixmath/normal.hh"
#include "radixmath/point3d.hh"
#include "radixmath/matrix.hh"
namespace radix
{
/*
* Default constructor
*/
Vector3D::Vector3D(void)
: x(0.0), y(0.0), z(0.0) {
}
/**
* Constructor
*/
Vector3D::Vector3D(Real a)
: x(a), y(a), z(a) {
}
/**
* Constructor
*/
Vector3D::Vector3D(Real _x, Real _y, Real _z)
: x(_x), y(_y), z(_z) {
}
/**
* Copy constructor
*/
Vector3D::Vector3D(const Vector3D& vector)
: x(vector.x), y(vector.y), z(vector.z) {
}
/**
* Constructor
* @brief constructs a vector from a normal
*/
Vector3D::Vector3D(const Normal& n)
: x(n.x), y(n.y), z(n.z) {
}
/**
* Constructor
* @brief constructs a vector from a point
* this is used in the ConcaveHemisphere hit functions
*/
Vector3D::Vector3D(const Point3D& p)
: x(p.x), y(p.y), z(p.z) {
}
/**
* Destructor
*/
Vector3D::~Vector3D(void) {
}
Vector3D Vector3D::spherical(void) const
{
// assume normalized
return Vector3D(1, std::acos(z),std::atan2(this->y,this->x));
}
Vector3D Vector3D::cartesian() const
{
const Real & sinY = std::sin(y);
return Vector3D(x*sinY*std::cos(z)
,x*sinY*std::sin(z)
,x*std::cos(y));
}
Vector3D Vector3D::operator-(void) const {
return (Vector3D(-x, -y, -z));
}
Real Vector3D::len_squared(void) const {
return (x * x + y * y + z * z);
}
Vector3D Vector3D::operator*(const Real a) const {
return (Vector3D(x * a, y * a, z * a));
}
Vector3D Vector3D::operator/(const Real a) const {
return (Vector3D(x / a, y / a, z / a));
}
Vector3D Vector3D::operator+(const Vector3D& v) const {
return (Vector3D(x + v.x, y + v.y, z + v.z));
}
Vector3D Vector3D::operator-(const Vector3D& v) const {
return (Vector3D(x - v.x, y - v.y, z - v.z));
}
Real Vector3D::operator*(const Vector3D& v) const {
return (x * v.x + y * v.y + z * v.z);
}
Vector3D Vector3D::operator^ (const Vector3D& v) const {
return (Vector3D(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x));
}
Vector3D& Vector3D::operator+=(const Vector3D& v) {
x += v.x;
y += v.y;
z += v.z;
return (*this);
}
Vector3D operator*(const Real a, const Vector3D& v) {
return (Vector3D(a * v.x, a * v.y, a * v.z));
}
/**
* Assignment operator
*/
Vector3D& Vector3D::operator=(const Vector3D& rhs) {
if (this == &rhs)
return (*this);
x = rhs.x;
y = rhs.y;
z = rhs.z;
return (*this);
}
bool Vector3D::operator==(const Vector3D& rhs)const{
if( x == rhs.x && y == rhs.y && z == rhs.z ) return true;
return false;
}
bool Vector3D::operator!=(const Vector3D& rhs)const{
if( x == rhs.x && y == rhs.y && z == rhs.z ) return false;
return false;
}
/**
* Assignment operator
* @brief assign a Normal to a vector
*/
Vector3D& Vector3D::operator=(const Normal& rhs) {
x = rhs.x;
y = rhs.y;
z = rhs.z;
return (*this);
}
/**
* Assignment operator
* @brief assign a point to a vector
*/
Vector3D& Vector3D::operator=(const Point3D& rhs) {
x = rhs.x;
y = rhs.y;
z = rhs.z;
return (*this);
}
std::string Vector3D::toString()const{
std::stringstream stream;
stream<<std::setprecision(13)<<"["<<x<<", "<<y<<", "<<z<<"] length="<<length();
return stream.str();
}
/**
* Length
* @brief Length of the vector
*/
Real Vector3D::length(void)const {
return (std::sqrt(x * x + y * y + z * z));
}
/**
* Normalize
* @brief converts the vector to a unit vector
*/
void Vector3D::normalize(void) {
Real length = std::sqrt(x * x + y * y + z * z);
radix_check(length > 0);
x /= length;
y /= length;
z /= length;
}
/**
* Normalized unit vector
* @brief converts the vector to a unit vector and returns the vector
*/
Vector3D& Vector3D::normalized_vector(void) {
Real length = std::sqrt(x * x + y * y + z * z);
x /= length;
y /= length;
z /= length;
return (*this);
}
Vector3D Vector3D::orthogonal(void)const{
Real orthogonal[3];
static const int UNSET = -1;
static const int UNCHANGED_INDEX=0;
static const int NEGATED_INDEX=1;
static const int ZEROED_INDEX=2;
int indices[]={UNSET,UNSET,UNSET}; // indices of values to swap, initialized to as UNSET
// index[0] : unchanged value
// index[1] : negated value
// index[2] : zeroed value
radix_insist( !( x==y && y==z && z==0 ), "Cannot obtain an orthogonal vector from a zero vector!" );
orthogonal[0] = x;
orthogonal[1] = y;
orthogonal[2] = z;
// loop over vector variables and determine which to zero, which to negate, and which to leave unchanged
for( int i = 0; i < 3; i++ ){
// if the index is zero and we have not already chosen an index to zero
// choose this index as the index to be zeroed
if( orthogonal[i] == 0.0 && indices[ZEROED_INDEX] == UNSET){
indices[ZEROED_INDEX] = i;
}else if( orthogonal[i] != 0.0 && indices[NEGATED_INDEX] == UNSET){
indices[NEGATED_INDEX] = i;
}else if( indices[ZEROED_INDEX] == UNSET){
indices[ZEROED_INDEX] = i;
}else{
radix_check( indices[UNCHANGED_INDEX] == UNSET );
indices[UNCHANGED_INDEX] = i;
}
}
radix_check( indices[UNCHANGED_INDEX] != UNSET );
radix_check( indices[NEGATED_INDEX] != UNSET );
radix_check( indices[ZEROED_INDEX] != UNSET );
// zero the index to be zeroed
orthogonal[indices[ZEROED_INDEX]] = 0.0;
// negate the index to be negated
orthogonal[indices[NEGATED_INDEX]] = -orthogonal[indices[NEGATED_INDEX]];
// the algorithm swaps the unchanged value with the negated value
std::swap(orthogonal[indices[NEGATED_INDEX]], orthogonal[indices[UNCHANGED_INDEX]]);
// return a vector that is orthogonal
return Vector3D(orthogonal[0],orthogonal[1],orthogonal[2]);
}
/**
* @funcion operator*
* @brief multiplication by a matrix on the left
*/
Vector3D operator*(const Matrix& mat, const Vector3D& v) {
return (Point3D(mat.matrix[0][0] * v.x + mat.matrix[0][1] * v.y + mat.matrix[0][2] * v.z,
mat.matrix[1][0] * v.x + mat.matrix[1][1] * v.y + mat.matrix[1][2] * v.z,
mat.matrix[2][0] * v.x + mat.matrix[2][1] * v.y + mat.matrix[2][2] * v.z));
}
std::ostream& operator<<(std::ostream& os, const Vector3D& v){
os<<v.toString();
return os;
}
} // namespace radix
/*
* File: vector3d.hh
* Author: Jordan
*
* Created on August 9, 2012, 6:06 PM
*/
#ifndef RADIX_RADIXMATH_VECTOR3D_H
#define RADIX_RADIXMATH_VECTOR3D_H
#include <string>
#include <stdexcept>
#include "radixmath/constants.hh"
namespace radix
{
class Normal;
class Point3D;
class Matrix;
/**
* @class Vector3D
*/
class Vector3D {
public:
Real x, y, z;
public:
/**
* Constructor
*/
Vector3D(void);
/**
* Constructor
* @param a
*/
Vector3D(Real a);
/**
* Constructor
* @param _x
* @param _y
* @param _z
*/
Vector3D(Real _x, Real _y, Real _z);
/**
* Copy constructor
* @param v
*/
Vector3D(const Vector3D& v);
/**
* Copy Normal Constructor
* @param n
*/
Vector3D(const Normal& n);
/**
* Copy Point3D constructor
* @param p
*/
Vector3D(const Point3D& p);
/**
* Destructor
*/
~Vector3D(void);
/**
*
* @param rhs
* @return
*/
Vector3D& operator=(const Vector3D& rhs);
bool operator==(const Vector3D& rhs)const;
bool operator!=(const Vector3D& rhs)const;
/**
* Assign a {@code Normal} to a vector
* @param rhs
* @return
*/
Vector3D& operator=(const Normal& rhs);
/**
* Assign a {@code Point3D to a vector
* @param rhs
* @return
*/
Vector3D& operator=(const Point3D& rhs);
/**
* Unary minus
* @return
*/
Vector3D operator-(void) const;
Real & operator[](size_t i){
switch(i)
{
case 0:
return x;
case 1:
return y;
case 2:
return z;
}
throw std::runtime_error("Unknown Vector3D member!");
}
std::string toString()const;
/**
* Length
* @return
*/
Real length(void)const;
/**
* Square of the length
* @return
*/
Real len_squared(void)const;
/**
* Multiplication by a Real on the right
* @param a
* @return
*/
Vector3D operator*(const Real a) const;
/**
* Division by a Real
* @param a
* @return
*/
Vector3D operator/(const Real a) const;
/**
* Addition
* @param v
* @return
*/
Vector3D operator+(const Vector3D& v) const;
/**
* Compound addition
* @param v