summaryrefslogblamecommitdiff
path: root/hw1/src/vec3d.cpp
blob: 6f55ef6a0192b424d015bd3c4afc3e6b4810acb4 (plain) (tree)








































































































































































































































































































































                                                                                          
/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527     */
/* without consent of either the College of William & Mary or     */
/* Pieter Peers.                                                  */
/******************************************************************/

#include <cassert>
#include <algorithm>
#include "vec3d.h"

//////////////////
// Constructors //
//////////////////
vec3d::vec3d(vec3d::const_reference value)
{
  x = y = z = value;
}


vec3d::vec3d(vec3d::const_reference x, vec3d::const_reference y, vec3d::const_reference z)
{
  this->x = x;
  this->y = y;
  this->z = z;
}


vec3d::vec3d(const vec3d& v)
{
  x = v.x;
  y = v.y;
  z = v.z;
}


////////////////
// Inspectors //
////////////////
vec3d::const_reference vec3d::operator[](size_t index) const
{
  assert(index < size());
  return data[index];
}


vec3d::reference vec3d::operator[](size_t index)
{
  assert(index < size());
  return data[index];
}


vec3d::iterator vec3d::begin(void)
{
  return data;
}


vec3d::const_iterator vec3d::begin(void) const
{
  return data;
}


vec3d::iterator vec3d::end(void)
{
  return begin() + size();
}


vec3d::const_iterator vec3d::end(void) const
{
  return begin() + size();
}


///////////////
// Operators //
///////////////
vec3d& vec3d::operator=(const vec3d& v)
{
  _assign(v);
  return *this;
}


bool vec3d::operator==(const vec3d& v) const
{
  return (x == v.x) && (y == v.y) && (z == v.z);
}


bool vec3d::operator!=(const vec3d& v) const
{
  return (x != v.x) || (y != v.y) || (z != v.z);
}


vec3d vec3d::operator-(void) const
{
  return vec3d(-x, -y, -z);
}


vec3d vec3d::operator+(const vec3d& v) const
{
  return vec3d(x + v.x, y + v.y, z + v.z);
}


vec3d vec3d::operator-(const vec3d& v) const
{
  // HW1: Implement this
  // Returns: difference of *this and v.
  // Modifies: nothing
  return vec3d(x - v.x, y - v.y, z - v.z);
}


vec3d vec3d::operator*(const vec3d& v) const
{
  // HW1: Implement this
  // Returns: product of *this and v.
  // Modifies: nothing
  return vec3d(x * v.x, y * v.y, z * v.z);
}


vec3d vec3d::operator*(vec3d::const_reference scale) const
{
  // HW1: Implement this
  // Returns: product of *this and scale.
  // Modifies: nothing
  return vec3d(x * scale, y * scale, z * scale);
}


vec3d vec3d::operator/(const vec3d& v) const
{
  // HW1: Implement this
  // Returns: division of *this and v.
  // Modifies: nothing
  return vec3d(x / v.x, y / v.y, z / v.z);
}


vec3d vec3d::operator/(vec3d::const_reference scale) const
{
  // HW1: Implement this
  // Returns: division of *this and scale.
  // Modifies: nothing
  return vec3d(x / scale, y / scale, z / scale);
}


vec3d& vec3d::operator+=(const vec3d& v)
{
  x += v.x;
  y += v.y;
  z += v.z;
  return *this;
}


vec3d& vec3d::operator-=(const vec3d& v)
{
  // HW1: Implement this
  // Returns: difference of *this and v.
  // Modifies: *this <= result
  x -= v.x;
  y -= v.y;
  z -= v.z;
  return *this;
}


vec3d& vec3d::operator*=(const vec3d& v)
{
  // HW1: Implement this
  // Returns: product of *this and v.
  // Modifies: *this <= result
  x *= v.x;
  y *= v.y;
  z *= v.z;
  return *this;
}


vec3d& vec3d::operator*=(vec3d::const_reference scale)
{
  // HW1: Implement this
  // Returns: product of *this and scale.
  // Modifies: *this <= result
  x *= scale;
  y *= scale;
  z *= scale;
  return *this;
}


vec3d& vec3d::operator/=(const vec3d& v)
{
  // HW1: Implement this
  // Returns: division of *this and v.
  // Modifies: *this <= result
  x /= v.x;
  y /= v.y;
  z /= v.z;
  return *this;
}


vec3d& vec3d::operator/=(vec3d::const_reference scale)
{
  // HW1: Implement this
  // Returns: division of *this and scale.
  // Modifies: *this <= result
  x /= scale;
  y /= scale;
  z /= scale;
  return *this;
}



///////////////
// Modifiers //
///////////////
vec3d::value_type vec3d::dot(const vec3d& v) const
{
  // HW1: Implement this
  // Returns: dot-product between *this and v (*this . v)
  // Modifies: nothing
  return x * v.x + y * v.y + z * v.z;
}


vec3d::value_type vec3d::squared_length(void) const
{
  return dot(*this);
}


vec3d::value_type vec3d::length(void) const
{
  return sqrt(squared_length());
}


vec3d::value_type vec3d::squared_distance(const vec3d& v) const
{
  return (*this - v).squared_length();
}


vec3d::value_type vec3d::distance(const vec3d& v) const
{
  return sqrt(squared_distance(v));
}


vec3d vec3d::cross(const vec3d& v) const
{
  // HW1: Implement this
  // Returns: cross-product of *this and v (*this x v).
  // Modifies: nothing
  return vec3d(y * v.z - z * v.y,
               z * v.x - x * v.z,
               x * v.y - y * v.x);
}


///////////////
// Modifiers //
///////////////
vec3d& vec3d::abs(void)
{
  std::for_each(begin(), end(), [](reference val)
  {
    if(val < 0) val = -val;
  });
  return *this;
}


vec3d& vec3d::clamp(vec3d::value_type lower, vec3d::value_type upper)
{
  std::for_each(begin(), end(), [&](reference val)
  {
    if(val < lower) val = lower;
    else if(val > upper) val = upper;
  });
  return *this;
}


vec3d& vec3d::normalize(void)
{
  // HW1: Implement this
  // Return: normalized vector of *this
  // Modifies: this <= result
  value_type vec_len = length();
  x /= vec_len;
  y /= vec_len;
  z /= vec_len;
  return *this;
}


/////////////////////
// Private Methods //
/////////////////////
void vec3d::_assign(const vec3d& v)
{
  x = v.x;
  y = v.y;
  z = v.z;
}


void vec3d::_swap(vec3d& v)
{
  std::swap(x, v.x);
  std::swap(y, v.y);
  std::swap(z, v.z);
}