# HG changeset patch # User Eris Caffee # Date 1314720492 18000 # Node ID f4e384b966b9d8f4375eb768c3fc86c4d34b19e4 # Parent acb4b5af3dfa2e34cfb2f9cc8f475b082f8d63e4 About half done with Vectors diff -r acb4b5af3dfa -r f4e384b966b9 CMakeLists.txt --- a/CMakeLists.txt Tue Mar 29 04:36:57 2011 -0500 +++ b/CMakeLists.txt Tue Aug 30 11:08:12 2011 -0500 @@ -66,7 +66,7 @@ #set (CMAKE_VERBOSE_MAKEFILE ON) # Name your program! -set (App_Name "") +set (App_Name "Math") if (App_Name STREQUAL "") message (FATAL_ERROR "You must set the App_Name variable!") endif () @@ -133,336 +133,60 @@ ################################################################################ # The core project files -file (GLOB SRCS src/*.c src/*.cpp) -file (GLOB HDRS include/*.h include/*.hpp) + +# Not GLOBing here so that I can explicitly list main.cpp later as part of the +# test driver. Also, it just seems good practice to explicitly list things since +# it avoids accidental inclusion of things that ought not to be included. +set (SRCS_${App_Name} + src/Math.cpp + include/Math.h +) -# The directories that contain the libraries we will be linking against. -# This must come before the ADD_EXECUTABLE directive. +include_directories ( + ${PROJECT_SOURCE_DIR}/include +) + +# Build both static and shared libraries +# The target properties are needed because, by default, the output name +# will be the name in the add_library command, and we need to have different +# names in the two commands for the shared and static versions, even though +# we want the final files to have the same names with different extensions. +# +# The prefix is needed mostly in case we build on windows, which has no prefix +# by default. +# +# The clean_direct_output option makes sure that the two lib builds don't +# clobber each others temp files since they are being built from the same +# sources. + +add_library (${App_Name} SHARED + ${SRCS_${App_Name}} +) +set_target_properties (${App_Name} PROPERTIES OUTPUT_NAME ${App_Name}) +set_target_properties (${App_Name} PROPERTIES PREFIX "lib") +set_target_properties (${App_Name} PROPERTIES CLEAN_DIRECT_OUTPUT 1) + +add_library (${App_Name}-static STATIC + ${SRCS_${App_Name}} +) +set_target_properties (${App_Name}-static PROPERTIES OUTPUT_NAME ${App_Name}) +set_target_properties (${App_Name}-static PROPERTIES PREFIX "lib") +set_target_properties (${App_Name}-static PROPERTIES CLEAN_DIRECT_OUTPUT 1) + +# Build a test application. + link_directories ( ) -# The directories that contain the include files our programs use. -# This must come before the ADD_EXECUTABLE directive. include_directories ( ${CMAKE_SOURCE_DIR}/include ) -# Define the executable program file we are creating. We must list all of -# the source files. -if (WIN32) - add_executable (${App_Name} WIN32 - ${SRCS} - ${HDRS} - ) -else () - add_executable (${App_Name} - ${SRCS} - ${HDRS} - ) -endif () - -# Although we listed the library directories above, we also need to list the -# individual libraries we will be linking against. -target_link_libraries (${App_Name} +add_executable (${App_Name}-test + src/main.cpp ) +target_link_libraries (${App_Name}-test + ${App_Name} +) -# # An example for a unix library named utils with a test driver program. - -# set (SRCS_utils -# src/utils.cpp -# include/utils.h -# ) - -# include_directories ( -# ${PROJECT_SOURCE_DIR}/include -# ) - -# # Build both static and shared libraries -# # The target properties are needed because, by default, the output name -# # will be the name in the add_library command, and we need to have different -# # names in the two commands for the shared and static versions, even though -# # we want the final files to have the same names with different extensions. -# # -# # The prefix is needed mostly in case we build on windows, which has no prefix -# # by default. -# # -# # The clean_direct_output option makes sure that the two lib builds don't -# # clobber each others temp files since they are being built from the same -# # sources. - -# add_library (utils SHARED -# ${SRCS_utils} -# ) -# set_target_properties (utils PROPERTIES OUTPUT_NAME "utils") -# set_target_properties (utils PROPERTIES PREFIX "lib") -# set_target_properties (utils PROPERTIES CLEAN_DIRECT_OUTPUT 1) - -# add_library (utils-static STATIC -# ${SRCS_utils} -# ) -# set_target_properties (utils-static PROPERTIES OUTPUT_NAME "utils") -# set_target_properties (utils-static PROPERTIES PREFIX "lib") -# set_target_properties (utils-static PROPERTIES CLEAN_DIRECT_OUTPUT 1) - -# # Build a test application. - -# add_executable (test -# src/main.cpp -# ) - -# target_link_libraries (test -# utils -# ) - - -################################################################################ -# X11 -# -# Note that the FindX11.cmake package does _not_ include most X extensions -# in the X11_LIBRARIES variable, although it does pick up their header files -# in the X11_INCLUDE_DIR variable. -# -# To link a program with extensions, such as Xrandr, or Xv, you must manually -# update the target_link_libraries to include the appropriate library variable. -# ${X11_Xrandr_LIB} is an example. See the FindX11.cmake file for a complete -# list of availble variables. -# - -option(Option_X11_Dev "Build an X11 Application." OFF) - -if (Option_X11_Dev) - - option(Option_Xrandr "Use Xrandr" OFF) - option(Option_Xinerama "Use Xinerama" OFF) - - - ######################################## - find_package(X11) - if (NOT X11_FOUND) - message (FATAL_ERROR "X11 not found!") - endif () - - include_directories( - ${X11_INCLUDE_DIR} - ${INCLUDE_DIRECTORIES} - ) - - target_link_libraries(${App_Name} - ${X11_LIBRARIES} - ${TARGET_LINK_LIBRARIES} - ) - - ######################################## - if (Option_Xrandr) - if (NOT X11_Xrandr_FOUND) - message (FATAL_ERRO "Xrandr not found!") - endif () - target_link_libraries(${App_Name} - ${X11_Xrandr_LIB} - ${TARGET_LINK_LIBRARIES} - ) - endif () - - ######################################## - if (Option_Xinerama) - if (NOT X11_Xinerama_FOUND) - message (FATAL_ERRO "Xinerama not found!") - endif () - target_link_libraries(${App_Name} - ${X11_Xinerama_LIB} - ${TARGET_LINK_LIBRARIES} - ) - endif () - -endif () - -################################################################################ -# SDL -# -# Enabling SDL support enables several suboptions to make some common SDL -# extension libraries available as well. -# -# If any of the SDL libraries are not found automatically, you will need -# to set the appropriate environment variables and rerun cmake. You will -# need to remove the CMake cache before doing so. - -# If you don't want to set actual environment variables before running -# CMake, then uncomment the if block below and put in the actual -# locations of your SDL installation. - -option(Option_SDL_Dev "Build an SDL application." OFF) - -if (Option_SDL_Dev) - - # # Force SDL 1.3 only - # if (WIN32) - # set (ENV{SDLDIR} "c:/gamedev/deps/sdl/SDL-build") - # else () - # set (ENV{SDLDIR} "/home/eris/gamedev/deps/sdl/SDL-build") - # endif () - # add_definitions(-DSDL_NO_COMPAT) - - # SDL base package - # To use a version of SDL other than your systems default, set the SDLDIR - # environment variable to the installation location of your preferred version. - find_package (SDL) - if (NOT SDL_FOUND) - message (FATAL_ERROR "SDL not found!") - endif (NOT SDL_FOUND) - include_directories( - ${SDL_INCLUDE_DIR} - ${INCLUDE_DIRECTORIES} - ) - target_link_libraries(${App_Name} - ${SDL_LIBRARY} - ${TARGET_LINK_LIBRARIES} - ) - - # SDL_ttf - # Environment variables SDLTTFDIR and SDLDIR will be checked in that order - # and if set cmake will try to find SDL_ttf in the specified directory. - option(Option_SDL_Dev_SDL_ttf "Use SDL_ttf." OFF) - if (Option_SDL_Dev_SDL_ttf) - find_package (SDL_ttf) - if (NOT SDLTTF_FOUND) - message (FATAL_ERROR "SDL_ttf not found!") - endif (NOT SDLTTF_FOUND) - include_directories( - ${SDLTTF_INCLUDE_DIR} - ${INCLUDE_DIRECTORIES} - ) - target_link_libraries(${App_Name} - ${SDLTTF_LIBRARY} - ${TARGET_LINK_LIBRARIES} - ) - endif () - - # SDL_image - # Environment variables SDLIMAGEDIR and SDLDIR will be checked in that order - # and if set cmake will try to find SDL_image in the specified directory. - option(Option_SDL_Dev_SDL_image "Use SDL_image." OFF) - if (Option_SDL_Dev_SDL_image) - find_package (SDL_image) - if (NOT SDLIMAGE_FOUND) - message (FATAL_ERROR "SDL_image not found!") - endif (NOT SDLIMAGE_FOUND) - include_directories( - ${SDLIMAGE_INCLUDE_DIR} - ${INCLUDE_DIRECTORIES} - ) - target_link_libraries(${App_Name} - ${SDLIMAGE_LIBRARY} - ${TARGET_LINK_LIBRARIES} - ) - endif () - - # SDL_mixer - # Environment variables SDLMIXERDIR and SDLDIR will be checked in that order - # and if set cmake will try to find SDL_mixer in the specified directory. - option(Option_SDL_Dev_SDL_mixer "Use SDL_mixer." OFF) - if (Option_SDL_Dev_SDL_mixer) - find_package (SDL_mixer) - if (NOT SDLMIXER_FOUND) - message (FATAL_ERROR "SDL_mixer not found!") - endif (NOT SDLMIXER_FOUND) - include_directories( - ${SDLMIXER_INCLUDE_DIR} - ${INCLUDE_DIRECTORIES} - ) - target_link_libraries(${App_Name} - ${SDLMIXER_LIBRARY} - ${TARGET_LINK_LIBRARIES} - ) - endif () - - # SDL_net - # Environment variables SDLNETDIR and SDLDIR will be checked in that order - # and if set cmake will try to find SDL_net in the specified directory. - option(Option_SDL_Dev_SDL_net "Use SDL_net." OFF) - if (Option_SDL_Dev_SDL_net) - find_package (SDL_net) - if (NOT SDLNET_FOUND) - message (FATAL_ERROR "SDL_net not found!") - endif (NOT SDLNET_FOUND) - include_directories( - ${SDLNET_INCLUDE_DIR} - ${INCLUDE_DIRECTORIES} - ) - target_link_libraries(${App_Name} - ${SDLNET_LIBRARY} - ${TARGET_LINK_LIBRARIES} - ) - endif () - -endif (Option_SDL_Dev) - - -################################################################################ - -option(Option_OpenGL_Dev "Build an OpenGL Application." OFF) - -if (Option_OpenGL_Dev) - - find_package(OpenGL) - if (NOT OPENGL_FOUND) - message (FATAL_ERROR "OpenGL not found!") - endif (NOT OPENGL_FOUND) - - include_directories( - ${OPENGL_INCLUDE_DIR} - ${INCLUDE_DIRECTORIES} - ) - - target_link_libraries(${App_Name} - ${OPENGL_LIBRARIES} - ${TARGET_LINK_LIBRARIES} - ) - - option(Option_GLUT_Dev "Build a GLUT Application." OFF) - - if (Option_GLUT_Dev) - - find_package(GLUT) - if (NOT GLUT_FOUND) - message (FATAL_ERROR "GLUT not found!") - endif() - - include_directories( - ${GLUT_INCLUDE_DIR} - ${INCLUDE_DIRECTORIES} - ) - - target_link_libraries(${App_Name} - ${GLUT_LIBRARIES} - ${TARGET_LINK_LIBRARIES} - ) - - endif() - -endif () - -################################################################################ - -option(Option_OpenAL_Dev "Build an OpenAL Application." OFF) - -if (Option_OpenAL_Dev) - - find_package(OpenAL) - if (NOT OPENAL_FOUND) - message (FATAL_ERROR "OpenAL not found!") - endif (NOT OPENAL_FOUND) - - include_directories( - ${OPENAL_INCLUDE_DIR} - ${INCLUDE_DIRECTORIES} - ) - - target_link_libraries(${App_Name} - ${OPENAL_LIBRARY} - ${TARGET_LINK_LIBRARIES} - ) - -endif () - -################################################################################ diff -r acb4b5af3dfa -r f4e384b966b9 include/Math.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/Math.h Tue Aug 30 11:08:12 2011 -0500 @@ -0,0 +1,231 @@ +#ifndef MATH_H_ +#define MATH_H_ + +#include +#include + +namespace arda + { + + //////////////////////////////////////////////////////////////////////////////// + // General notes: + // + // As much as possible is defined inline for performance. + // Using typedef'ed arrays for vectors and matrices necessitates a lot of + // redundant function names: scale2, scale3, scale4, for example. + // This is, however, perfectly in line with the implementation of OpenGL and + // allows the library to maintain optimal memory usage and performance. (I hope!) + // In any event, I think I could do far worse than to emulate the OpenGL + // working group - a group of people who have studied this for years and are + // experts in the field. + + //////////////////////////////////////////////////////////////////////////////// + // Vectors + + namespace Vector + { + + typedef int Vector2i[2]; + typedef float Vector2f[2]; + typedef double Vector2d[2]; + + typedef int Vector3i[3]; + typedef float Vector3f[3]; + typedef double Vector3d[3]; + + typedef int Vector4i[4]; + typedef float Vector4f[4]; + typedef double Vector4d[4]; + + ////////////////////////////////////////////////////////////////////////// + // vectostr*(Vector v) + // Returns a C string representation of the vector. + // This is one of the few non-inline Vector functions. + + std::string & vectostr2(Vector2i v, std::string & s); + std::string & vectostr2(Vector2f v, std::string & s); + std::string & vectostr2(Vector2d v, std::string & s); + std::string & vectostr3(Vector3i v, std::string & s); + std::string & vectostr3(Vector3f v, std::string & s); + std::string & vectostr3(Vector3d v, std::string & s); + std::string & vectostr4(Vector4i v, std::string & s); + std::string & vectostr4(Vector4f v, std::string & s); + std::string & vectostr4(Vector4d v, std::string & s); + + ////////////////////////////////////////////////////////////////////////// + // scale*(Vector v, scalar s) + // Multiply a vector by a scalar. + // Returns v so that the result may be passed to other functions. + + inline int * scale2(Vector2i v, const int s) + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } + inline int * scale2(Vector2i v, const float s) + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } + inline int * scale2(Vector2i v, const double s) + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } + + inline float * scale2(Vector2f v, const int s) + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } + inline float * scale2(Vector2f v, const float s) + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } + inline float * scale2(Vector2f v, const double s) + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } + + inline double * scale2(Vector2d v, const int s) + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } + inline double * scale2(Vector2d v, const float s) + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } + inline double * scale2(Vector2d v, const double s) + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } + + + inline int * scale3(Vector3i v, const int s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } + inline int * scale3(Vector3i v, const float s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } + inline int * scale3(Vector3i v, const double s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } + + inline float * scale3(Vector3f v, const int s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } + inline float * scale3(Vector3f v, const float s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } + inline float * scale3(Vector3f v, const double s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } + + inline double * scale3(Vector3d v, const int s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } + inline double * scale3(Vector3d v, const float s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } + inline double * scale3(Vector3d v, const double s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } + + + inline int * scale4(Vector4i v, const int s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } + inline int * scale4(Vector4i v, const float s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } + inline int * scale4(Vector4i v, const double s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } + + inline float * scale4(Vector4f v, const int s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } + inline float * scale4(Vector4f v, const float s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } + inline float * scale4(Vector4f v, const double s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } + + inline double * scale4(Vector4d v, const int s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } + inline double * scale4(Vector4d v, const float s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } + inline double * scale4(Vector4d v, const double s) + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } + + + ////////////////////////////////////////////////////////////////////////// + // dot*(Vector v1, Vector v2) + // The dot product of two vectors, which must be the same type + // Returns a long for int vectors, a double for floating point vectors. + + inline long dot2(Vector2i v1, Vector2i v2) + { return (long) v1[0]*v2[0] + (long) v1[1]*v2[1]; } + inline double dot2(Vector2f v1, Vector2f v2) + { return (double) v1[0]*v2[0] + (double) v1[1]*v2[1]; } + inline double dot2(Vector2d v1, Vector2d v2) + { return v1[0]*v2[0] + v1[1]*v2[1]; } + + inline long dot3(Vector3i v1, Vector3i v2) + { return (long) v1[0]*v2[0] + (long) v1[1]*v2[1] + (long) v1[2]*v2[2]; } + inline double dot3(Vector3f v1, Vector3f v2) + { return (double) v1[0]*v2[0] + (double) v1[1]*v2[1] + (double) v1[2]*v2[2]; } + inline double dot3(Vector3d v1, Vector3d v2) + { return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]; } + + inline long dot4(Vector4i v1, Vector4i v2) + { return (long) v1[0]*v2[0] + (long) v1[1]*v2[1] + (long) v1[2]*v2[2] + (long) v1[3]*v2[3]; } + inline double dot4(Vector4f v1, Vector4f v2) + { return (double) v1[0]*v2[0] + (double) v1[1]*v2[1] + (double) v1[2]*v2[2] + (double) v1[3]*v2[3]; } + inline double dot4(Vector4d v1, Vector4d v2) + { return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2] + v1[3]*v2[3]; } + + + ////////////////////////////////////////////////////////////////////////// + // length*(Vector v) + // Get the length of a vector. + // Returns a float for float for float vectors, and a double for + // int and double vectors. + + inline double length2(Vector2i v) + { return sqrt((dot2(v, v))); } + inline float length2(Vector2f v) + { return sqrtf((dot2(v, v))); } + inline double length2(Vector2d v) + { return sqrt((dot2(v, v))); } + + inline double length3(Vector3i v) + { return sqrt((dot3(v, v))); } + inline float length3(Vector3f v) + { return sqrtf((dot3(v, v))); } + inline double length3(Vector3d v) + { return sqrt((dot3(v, v))); } + + inline double length4(Vector4i v) + { return sqrt((dot4(v, v))); } + inline float length4(Vector4f v) + { return sqrtf((dot4(v, v))); } + inline double length4(Vector4d v) + { return sqrt((dot4(v, v))); } + + + ////////////////////////////////////////////////////////////////////////// + // add*(Vector v1, Vector v2, Vector vres) + // Add two vectors together. + // Returns vres so that the result may be used in further + // calculations. + + inline int * add2(Vector2i v1, Vector2i v2, Vector2i vres) + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; return vres; } + inline float * add2(Vector2f v1, Vector2f v2, Vector2f vres) + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; return vres; } + inline double * add2(Vector2d v1, Vector2d v2, Vector2d vres) + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; return vres; } + + inline int * add3(Vector3i v1, Vector3i v2, Vector3i vres) + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; return vres; } + inline float * add3(Vector3f v1, Vector3f v2, Vector3f vres) + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; return vres; } + inline double * add3(Vector3d v1, Vector3d v2, Vector3d vres) + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; return vres; } + + inline int * add4(Vector4i v1, Vector4i v2, Vector4i vres) + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; vres[3] = v1[3] + v2[3]; return vres;} + inline float * add4(Vector4f v1, Vector4f v2, Vector4f vres) + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; vres[3] = v1[3] + v2[3]; return vres; } + inline double * add4(Vector4d v1, Vector4d v2, Vector4d vres) + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; vres[3] = v1[3] + v2[3]; return vres; } + + ////////////////////////////////////////////////////////////////////////// + // normalize*(Vector v) + // Normalizes the vecor. Only defined for floating point vectors. + // Returns the vector. + + inline float * normalize2(Vector2f v) + { float l = length2(v); v[0] /= l; v[1] /= l; return v; } + inline double * normalize2(Vector2d v) + { double l = length2(v); v[0] /= l; v[1] /= l; return v; } + + inline float * normalize3(Vector3f v) + { float l = length3(v); v[0] /= l; v[1] /= l; v[2] /= l; return v; } + inline double * normalize3(Vector3d v) + { double l = length3(v); v[0] /= l; v[1] /= l; v[2] /= l; return v; } + + inline float * normalize4(Vector4f v) + { float l = length4(v); v[0] /= l; v[1] /= l; v[2] /= l; v[3] /= l; return v; } + inline double * normalize4(Vector4d v) + { double l = length4(v); v[0] /= l; v[1] /= l; v[2] /= l; v[3] /= l; return v; } + } + +} // namespace arda + +#endif diff -r acb4b5af3dfa -r f4e384b966b9 notes --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/notes Tue Aug 30 11:08:12 2011 -0500 @@ -0,0 +1,31 @@ +-------------------------------------------------------------------------------- +Vectors + +Operations to support + +* dot +* length (norm) +* vector addition +* scalar multiplication +* normalize +cross +triple product i.e. dot(u, cross(v, w)) Is this really that common? +angle between two vectors +projection of one vector onto another. +* vector to string + +Class with operator overloading? + +-------------------------------------------------------------------------------- +Matrices + +det +transpose +inverse +add +matrix multiplication +vector multiplication +scalar multiplication + +-------------------------------------------------------------------------------- +Quaternions diff -r acb4b5af3dfa -r f4e384b966b9 notes~ --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/notes~ Tue Aug 30 11:08:12 2011 -0500 @@ -0,0 +1,28 @@ +-------------------------------------------------------------------------------- +Vectors + +Operations to support + +norm +dot +cross +normalize +vector addition +scalar multiplication +triple product i.e. dot(u, cross(v, w)) Is this really that common? + +Class with operator overloading? + +-------------------------------------------------------------------------------- +Matrices + +det +transpose +inverse +add +matrix multiplication +vector multiplication +scalar multiplication + +-------------------------------------------------------------------------------- +Quaternions diff -r acb4b5af3dfa -r f4e384b966b9 src/Math.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Math.cpp Tue Aug 30 11:08:12 2011 -0500 @@ -0,0 +1,95 @@ +#include "Math.h" + +#include +#include +#include + +//////////////////////////////////////////////////////////////////////////////// +std::string & arda::Vector::vectostr2(Vector2i v, std::string & s) + { + s.clear(); + std::stringstream ss; + ss << "{ " << v[0] << ", " << v[1] << " }"; + s = ss.str(); + return s; + } + +//////////////////////////////////////////////////////////////////////////////// +std::string & arda::Vector::vectostr2(Vector2f v, std::string & s) + { + s.clear(); + std::stringstream ss; + ss << "{ " << v[0] << ", " << v[1] << " }"; + s = ss.str(); + return s; + } + +//////////////////////////////////////////////////////////////////////////////// +std::string & arda::Vector::vectostr2(Vector2d v, std::string & s) + { + s.clear(); + std::stringstream ss; + ss << "{ " << v[0] << ", " << v[1] << " }"; + s = ss.str(); + return s; + } + +//////////////////////////////////////////////////////////////////////////////// +std::string & arda::Vector::vectostr3(Vector3i v, std::string & s) + { + s.clear(); + std::stringstream ss; + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; + s = ss.str(); + return s; + } + +//////////////////////////////////////////////////////////////////////////////// +std::string & arda::Vector::vectostr3(Vector3f v, std::string & s) + { + s.clear(); + std::stringstream ss; + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; + s = ss.str(); + return s; + } + +//////////////////////////////////////////////////////////////////////////////// +std::string & arda::Vector::vectostr3(Vector3d v, std::string & s) + { + s.clear(); + std::stringstream ss; + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; + s = ss.str(); + return s; + } + +//////////////////////////////////////////////////////////////////////////////// +std::string & arda::Vector::vectostr4(Vector4i v, std::string & s) + { + s.clear(); + std::stringstream ss; + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << " }"; + s = ss.str(); + return s; + } + +//////////////////////////////////////////////////////////////////////////////// +std::string & arda::Vector::vectostr4(Vector4f v, std::string & s) + { + s.clear(); + std::stringstream ss; + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << " }"; + s = ss.str(); + return s; + } + +//////////////////////////////////////////////////////////////////////////////// +std::string & arda::Vector::vectostr4(Vector4d v, std::string & s) + { + s.clear(); + std::stringstream ss; + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << " }"; + s = ss.str(); + return s; + } diff -r acb4b5af3dfa -r f4e384b966b9 src/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main.cpp Tue Aug 30 11:08:12 2011 -0500 @@ -0,0 +1,144 @@ +#include +#include +#include +using namespace std; + +#include "Math.h" +using namespace arda::Vector; + +int main (int argc, char * argv[]) + { + string s1, s2; + + // Vector::scale*() + { + cout << "===============================================================" << endl << + "Testing Vector::scale*()" << endl; + + Vector2i v2i = { 1, 2 }; + cout << setw(40) << "v2i: " << vectostr2(v2i, s1) << endl; + scale2(v2i, 2); + cout << setw(40) << "scale2(v2i, 2): " << vectostr2(v2i, s1) << endl; + scale2(v2i, 2.4); + cout << setw(40) << "scale2(vi, 2.4): " << vectostr2(v2i, s1) << endl; + scale2(scale2(v2i, 2), 3); + cout << setw(40) << "scale2(scale2(v2i, 2), 3): " << vectostr2(v2i, s1) << endl; + + + Vector3f v3f = { 3.0, 4.1, 5.2 }; + cout << setw(40) << "v3f: " << vectostr3(v3f, s1) << endl; + scale3(v3f, 2.2f); + cout << setw(40) << "scale3(v3f, 2.2f): " << vectostr3(v3f, s1) << endl; + scale3(v3f, 2); + cout << setw(40) << "scale3(v2f, 2): " << vectostr3(v3f, s1) << endl; + + Vector4d v4d = { 6.0L, 7.1L, 8.2L, 9.3L }; + cout << setw(40) << "vd4: " << vectostr4(v4d, s1) << endl; + scale4(v4d, 2.0); + cout << setw(40) << "scale4(v4d, 2.0): " << vectostr4(v4d, s1) << endl; + scale4(v4d, 2); + cout << setw(40) << "scale4(v4d, 2): " << vectostr4(v4d, s1) << endl; + } + + + // Vector::dot*() + { + cout << "===============================================================" << endl << + "Testing Vector::dot*()" << endl; + + Vector2i v2i[2] = { { 1, 2 }, { 3, 4 } }; + cout << setw(40) << "v2i[0]: " << vectostr2(v2i[0], s1) << endl; + cout << setw(40) << "v2i[1]: " << vectostr2(v2i[1], s1) << endl; + cout << setw(40) << "dot2(v2i[0], v2i[1]): " << dot2(v2i[0], v2i[1]) << endl; + + Vector3f v3f[3] = { { 1.1f, 2.2f, 3.3f }, { 4.4f, 5.5f, 6.6f } }; + cout << setw(40) << "v3f[0]: " << vectostr3(v3f[0], s1) << endl; + cout << setw(40) << "v3f[1]: " << vectostr3(v3f[1], s1) << endl; + cout << setw(40) << "dot3(v3f[0], v3f[1]): " << dot3(v3f[0], v3f[1]) << endl; + + Vector4d v4d[3] = { { 1.1, 2.2, 3.3, 4.4 }, { 5.5, 6.6, 7.7, 8.8 } }; + cout << setw(40) << "v4d[0]: " << vectostr4(v4d[0], s1) << endl; + cout << setw(40) << "v4d[1]: " << vectostr4(v4d[1], s1) << endl; + cout << setw(40) << "dot4(v4d[0], v4d[1]): " << dot4(v4d[0], v4d[1]) << endl; + + } + + // Vector::length*() + { + cout << "===============================================================" << endl << + "Testing Vector::length*()" << endl; + + Vector2i v2i = { 1, 2 }; + cout << setw(40) << "v2i: " << vectostr2(v2i, s1) << endl; + cout << setw(40) << "length2(v2i): " << length2(v2i) << endl; + + Vector3f v3f = { 1.1f, 2.2f, 3.3f }; + cout << setw(40) << "v3f: " << vectostr3(v3f, s1) << endl; + cout << setw(40) << "length3(v3f): " << length3(v3f) << endl; + + Vector4d v4d = { 1.1, 2.2, 3.3, 4.4 }; + cout << setw(40) << "v4d: " << vectostr4(v4d, s1) << endl; + cout << setw(40) << "length4(v4d): " << length4(v4d) << endl; + + } + + // Vector::add*() + { + cout << "===============================================================" << endl << + "Testing Vector::add*()" << endl; + + Vector2i v2i[3] = { { 1, 2 }, { 3, 4 } }; + Vector2i v2ires; + cout << setw(40) << "v2i[0]: " << vectostr2(v2i[0], s1) << endl; + cout << setw(40) << "v2i[1]: " << vectostr2(v2i[1], s1) << endl; + add2(v2i[0], v2i[1], v2ires); + cout << setw(40) << "add2(v2i[0], v2i[1], v2ires): " << vectostr2(v2ires, s1) << endl; + cout << setw(40) << "length2(add2(v2i[0], v2i[1], v2ires)): " << length2(add2(v2i[0], v2i[1], v2ires)) << endl; + + Vector3f v3f[3] = { { 1.1f, 2.2f, 3.3f }, { 4.4f, 5.5f, 6.6f } }; + Vector3f v3fres; + cout << setw(40) << "v3f[0]: " << vectostr3(v3f[0], s1) << endl; + cout << setw(40) << "v3f[1]: " << vectostr3(v3f[1], s1) << endl; + add3(v3f[0], v3f[1], v3fres); + cout << setw(40) << "add3(v3f[0], v3f[1], v3fres): " << vectostr3(v3fres, s1) << endl; + cout << setw(40) << "length3(add3(v3f[0], v3f[1], v3fres)): " << length3(add3(v3f[0], v3f[1], v3fres)) << endl; + + Vector4d v4d[3] = { { 1.1, 2.2, 3.3, 4.4 }, { 5.5, 6.6, 7.7, 8.8 } }; + Vector4d v4dres; + cout << setw(40) << "v4d[0]: " << vectostr4(v4d[0], s1) << endl; + cout << setw(40) << "v4d[1]: " << vectostr4(v4d[1], s1) << endl; + add4(v4d[0], v4d[1], v4dres); + cout << setw(40) << "add4(v4d[0], v4d[1], v4dres): " << vectostr4(v4dres, s1) << endl; + cout << setw(40) << "length4(add4(v4d[0], v4d[1], v4dres)): " << length4(add4(v4d[0], v4d[1], v4dres)) << endl; + + } + + // Vector::add*() + { + cout << "===============================================================" << endl << + "Testing Vector::normalize*()" << endl; + + Vector2f v2f = { 1.1, 2.2 }; + cout << setw(40) << "v2f: " << vectostr2(v2f, s1) << endl; + normalize2(v2f); + cout << setw(40) << "normalize2(v2f): " << vectostr2(v2f, s1) << endl; + scale2(normalize2(v2f), 2.0); + cout << setw(40) << "scale(normalize2(v2f), 2.0): " << vectostr2(v2f, s1) << endl; + + Vector3f v3f = { 1.1f, 2.2f, 3.3f }; + cout << setw(40) << "v3f: " << vectostr3(v3f, s1) << endl; + normalize3(v3f); + cout << setw(40) << "normalize3(v3f): " << vectostr3(v3f, s1) << endl; + scale3(normalize3(v3f), 2.0); + cout << setw(40) << "scale(normalize3(v3f), 2.0): " << vectostr3(v3f, s1) << endl; + + Vector4d v4d = { 1.1, 2.2, 3.3, 4.4 }; + cout << setw(40) << "v4d: " << vectostr4(v4d, s1) << endl; + normalize4(v4d); + cout << setw(40) << "normalize4(v4d): " << vectostr4(v4d, s1) << endl; + scale4(normalize4(v4d), 2.0); + cout << setw(40) << "scale4(normalize4(v4d), 2.0): " << vectostr4(v4d, s1) << endl; + + } + + }