summaryrefslogtreecommitdiff
path: root/hw1
diff options
context:
space:
mode:
Diffstat (limited to 'hw1')
-rw-r--r--hw1/.gitignore3
-rw-r--r--hw1/CMakeLists.txt71
-rw-r--r--hw1/bin/HW1.cpp38
-rw-r--r--hw1/bin/helloworld.cpp7
-rw-r--r--hw1/include/vec3d.h120
-rw-r--r--hw1/src/vec3d.cpp329
6 files changed, 568 insertions, 0 deletions
diff --git a/hw1/.gitignore b/hw1/.gitignore
new file mode 100644
index 0000000..c0077e2
--- /dev/null
+++ b/hw1/.gitignore
@@ -0,0 +1,3 @@
+Debug
+Release
+
diff --git a/hw1/CMakeLists.txt b/hw1/CMakeLists.txt
new file mode 100644
index 0000000..984cdf2
--- /dev/null
+++ b/hw1/CMakeLists.txt
@@ -0,0 +1,71 @@
+##################
+# Initialization #
+##################
+# Project name is not mandatory, but you should use it
+project(CSCI427)
+
+# States that CMake required version must be >= 2.6
+cmake_minimum_required(VERSION 2.6)
+
+#####################
+# Setup Environment #
+#####################
+# set to include custom modules
+set(CMAKE_MODULE_PATH $ENV{CMAKE_MODULE_PATH} ${CSCI427_SOURCE_DIR}/cmake)
+
+# set build type if specified by environment
+if((NOT CMAKE_BUILD_TYPE) AND (NOT $ENV{CMAKE_BUILD_TYPE} STREQUAL ""))
+ set(CMAKE_BUILD_TYPE $ENV{CMAKE_BUILD_TYPE})
+endif()
+
+# Set include directories
+include_directories(${CSCI427_SOURCE_DIR}/include)
+
+# Get CPP files
+file(GLOB SRC ${CSCI427_SOURCE_DIR}/src/*cpp)
+
+# Get executable files
+file(GLOB EXECLIST ${CSCI427_SOURCE_DIR}/bin/*cpp)
+
+
+###############
+# C++ Options #
+###############
+# Enable C++11
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
+
+# determine build type based on directory name. Do not allow for in source building
+#
+if(${CSCI427_SOURCE_DIR} STREQUAL ${CSCI427_BINARY_DIR})
+ message(FATAL_ERROR " *** In-source building not allowed. Please create a subdir 'Release' or 'Debug', and run cmake from within this directory 'cmake ..' ***")
+else()
+ get_filename_component(TYPE ${CSCI427_BINARY_DIR} NAME)
+ string(TOUPPER "${TYPE}" TYPE)
+ if(${TYPE} STREQUAL "RELEASE")
+ set(CMAKE_BUILD_TYPE Release)
+ else()
+ set(CMAKE_BUILD_TYPE Debug)
+ endif()
+ message("-- Build type set to: ${TYPE}")
+endif()
+
+#######################
+# Set Compile Targets #
+#######################
+# src libraries
+if(NOT SRC STREQUAL "")
+ set(LIBNAME "427_core")
+ add_library(${LIBNAME} ${SRC})
+endif()
+
+# executables
+foreach(EXEC ${EXECLIST})
+ get_filename_component(EXECNAME ${EXEC} NAME_WE)
+ add_executable(${EXECNAME} ${EXEC})
+
+ if(NOT SRC STREQUAL "")
+ target_link_libraries(${EXECNAME} LINK_PUBLIC ${LIBNAME})
+ endif()
+
+ message("-- Adding executable: ${EXECNAME}")
+endforeach(EXEC)
diff --git a/hw1/bin/HW1.cpp b/hw1/bin/HW1.cpp
new file mode 100644
index 0000000..8e66297
--- /dev/null
+++ b/hw1/bin/HW1.cpp
@@ -0,0 +1,38 @@
+#include <iostream>
+#include "vec3d.h"
+
+int main(int argc, char** argv)
+{
+ std::cout << "CSCI-427/527 Homework 1: Vectors" << std::endl;
+
+ vec3d a(1.0f, 0.0f, -1.0f);
+ vec3d b(-1.0f, 1.0f, 1.0f);
+
+ std::cout << "a = " << a << std::endl;
+ std::cout << "b = " << b << std::endl;
+
+ std::cout << "a+b = " << a+b << std::endl;
+ std::cout << "a-b = " << a-b << std::endl;
+ std::cout << "a*b = " << a*b << std::endl;
+ std::cout << "a/b = " << a/b << std::endl;
+ std::cout << "a*2 = " << a*2.0f << std::endl;
+ std::cout << "b/2 = " << b/2.0f << std::endl;
+
+ std::cout << "a.dot(b) = " << a.dot(b) << std::endl;
+ std::cout << "b.dot(a) = " << b.dot(a) << std::endl;
+ std::cout << "a.squared_length() = " << a.squared_length() << std::endl;
+ std::cout << "b.length() = " << b.length() << std::endl;
+ std::cout << "a.distance(b) = " << a.distance(b) << std::endl;
+ std::cout << "a.cross(b) = " << a.cross(b) << std::endl;
+ std::cout << "b.cross(a) = " << b.cross(a) << std::endl;
+ std::cout << "normalize(a) = " << normalize(a) << std::endl;
+ std::cout << "abs(a) = " << abs(a) << std::endl;
+ std::cout << "b.normalize() = " << b.normalize() << std::endl;
+ std::cout << "b.abs() = " << b.abs() << std::endl;
+
+ std::cout << "a = " << a << std::endl;
+ std::cout << "b = " << b << std::endl;
+
+ // Done.
+ return 0;
+}
diff --git a/hw1/bin/helloworld.cpp b/hw1/bin/helloworld.cpp
new file mode 100644
index 0000000..4a1b8e4
--- /dev/null
+++ b/hw1/bin/helloworld.cpp
@@ -0,0 +1,7 @@
+#include <iostream>
+
+int main(int argc, char** argv)
+{
+ std::cout << "Hello World." << std::endl;
+ return(0);
+}
diff --git a/hw1/include/vec3d.h b/hw1/include/vec3d.h
new file mode 100644
index 0000000..18ba208
--- /dev/null
+++ b/hw1/include/vec3d.h
@@ -0,0 +1,120 @@
+/******************************************************************/
+/* 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. */
+/******************************************************************/
+#ifndef _VEC3D_H_
+#define _VEC3D_H_
+
+#include <ostream>
+#include <cmath>
+
+class vec3d {
+ public:
+ /////////////
+ // Typedef //
+ /////////////
+ typedef float value_type;
+ typedef value_type& reference;
+ typedef const value_type& const_reference;
+ typedef value_type* iterator;
+ typedef const value_type* const_iterator;
+
+ //////////////////
+ // Data Members //
+ //////////////////
+ union {
+ struct { value_type x, y, z; };
+ value_type data[3];
+ };
+
+ public:
+ /////////////////
+ // Constructor //
+ /////////////////
+ explicit vec3d(const_reference value=0.0f);
+ vec3d(const_reference x, const_reference y, const_reference z);
+ vec3d(const vec3d& v);
+
+ ////////////////
+ // Inspectors //
+ ////////////////
+ size_t size(void) const { return 3; }
+
+ iterator begin(void);
+ const_iterator begin(void) const;
+ iterator end(void);
+ const_iterator end(void) const;
+
+ const_reference operator[](size_t index) const;
+ reference operator[](size_t index);
+
+ ///////////////
+ // Operators //
+ ///////////////
+ vec3d& operator=(const vec3d& v);
+
+ bool operator==(const vec3d& v) const;
+ bool operator!=(const vec3d& v) const;
+
+ vec3d operator-(void) const;
+
+ vec3d operator+(const vec3d& v) const;
+ vec3d operator-(const vec3d& v) const;
+ vec3d operator*(const vec3d& v) const;
+ vec3d operator*(const_reference scale) const;
+ vec3d operator/(const vec3d& v) const;
+ vec3d operator/(const_reference scale) const;
+
+ vec3d& operator+=(const vec3d& v);
+ vec3d& operator-=(const vec3d& v);
+ vec3d& operator*=(const vec3d& v);
+ vec3d& operator*=(const_reference scale);
+ vec3d& operator/=(const vec3d& v);
+ vec3d& operator/=(const_reference scale);
+
+ /////////////
+ // Methods //
+ /////////////
+ value_type dot(const vec3d& v) const;
+ value_type squared_length(void) const;
+ value_type length(void) const;
+ value_type squared_distance(const vec3d& v) const;
+ value_type distance(const vec3d& v) const;
+
+ vec3d cross(const vec3d& v) const;
+
+ ///////////////
+ // Modifiers //
+ ///////////////
+ vec3d& abs(void);
+ vec3d& clamp(value_type lower=0.0f, value_type upper=1.0f);
+ vec3d& normalize(void);
+
+ /////////////
+ // Friends //
+ /////////////
+ friend void swap(vec3d& a, vec3d& b) { return a._swap(b); }
+ friend vec3d normalize(const vec3d& v) { return vec3d(v).normalize(); }
+ friend vec3d abs(const vec3d& v) { return vec3d(v).abs(); }
+ friend vec3d clamp(const vec3d& v, value_type lower=0.0f, value_type upper=1.0f) { return vec3d(v).clamp(lower, upper); }
+ friend vec3d operator*(const_reference scale, const vec3d& v) { return (v*scale); }
+
+ friend std::ostream& operator<<(std::ostream& s, const vec3d& v)
+ {
+ s << "[" << v.x << "," << v.y << "," << v.z << "]";
+ return s;
+ }
+
+ private:
+ /////////////////////
+ // Private Methods //
+ /////////////////////
+ void _assign(const vec3d& v);
+ void _swap(vec3d& v);
+};
+
+#endif /* _VEC3D_H_ */
diff --git a/hw1/src/vec3d.cpp b/hw1/src/vec3d.cpp
new file mode 100644
index 0000000..6f55ef6
--- /dev/null
+++ b/hw1/src/vec3d.cpp
@@ -0,0 +1,329 @@
+/******************************************************************/
+/* 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);
+}