Mercurial > Math
changeset 4:f4e384b966b9
About half done with Vectors
author | Eris Caffee <discordia@eldalin.com> |
---|---|
date | Tue, 30 Aug 2011 11:08:12 -0500 |
parents | acb4b5af3dfa |
children | f6e6f3c8f7eb |
files | CMakeLists.txt include/Math.h notes notes~ src/Math.cpp src/main.cpp |
diffstat | 6 files changed, 576 insertions(+), 323 deletions(-) [+] |
line diff
1.1 --- a/CMakeLists.txt Tue Mar 29 04:36:57 2011 -0500 1.2 +++ b/CMakeLists.txt Tue Aug 30 11:08:12 2011 -0500 1.3 @@ -66,7 +66,7 @@ 1.4 #set (CMAKE_VERBOSE_MAKEFILE ON) 1.5 1.6 # Name your program! 1.7 -set (App_Name "") 1.8 +set (App_Name "Math") 1.9 if (App_Name STREQUAL "") 1.10 message (FATAL_ERROR "You must set the App_Name variable!") 1.11 endif () 1.12 @@ -133,336 +133,60 @@ 1.13 ################################################################################ 1.14 # The core project files 1.15 1.16 -file (GLOB SRCS src/*.c src/*.cpp) 1.17 -file (GLOB HDRS include/*.h include/*.hpp) 1.18 + 1.19 +# Not GLOBing here so that I can explicitly list main.cpp later as part of the 1.20 +# test driver. Also, it just seems good practice to explicitly list things since 1.21 +# it avoids accidental inclusion of things that ought not to be included. 1.22 +set (SRCS_${App_Name} 1.23 + src/Math.cpp 1.24 + include/Math.h 1.25 +) 1.26 1.27 -# The directories that contain the libraries we will be linking against. 1.28 -# This must come before the ADD_EXECUTABLE directive. 1.29 +include_directories ( 1.30 + ${PROJECT_SOURCE_DIR}/include 1.31 +) 1.32 + 1.33 +# Build both static and shared libraries 1.34 +# The target properties are needed because, by default, the output name 1.35 +# will be the name in the add_library command, and we need to have different 1.36 +# names in the two commands for the shared and static versions, even though 1.37 +# we want the final files to have the same names with different extensions. 1.38 +# 1.39 +# The prefix is needed mostly in case we build on windows, which has no prefix 1.40 +# by default. 1.41 +# 1.42 +# The clean_direct_output option makes sure that the two lib builds don't 1.43 +# clobber each others temp files since they are being built from the same 1.44 +# sources. 1.45 + 1.46 +add_library (${App_Name} SHARED 1.47 + ${SRCS_${App_Name}} 1.48 +) 1.49 +set_target_properties (${App_Name} PROPERTIES OUTPUT_NAME ${App_Name}) 1.50 +set_target_properties (${App_Name} PROPERTIES PREFIX "lib") 1.51 +set_target_properties (${App_Name} PROPERTIES CLEAN_DIRECT_OUTPUT 1) 1.52 + 1.53 +add_library (${App_Name}-static STATIC 1.54 + ${SRCS_${App_Name}} 1.55 +) 1.56 +set_target_properties (${App_Name}-static PROPERTIES OUTPUT_NAME ${App_Name}) 1.57 +set_target_properties (${App_Name}-static PROPERTIES PREFIX "lib") 1.58 +set_target_properties (${App_Name}-static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 1.59 + 1.60 +# Build a test application. 1.61 + 1.62 link_directories ( 1.63 ) 1.64 1.65 -# The directories that contain the include files our programs use. 1.66 -# This must come before the ADD_EXECUTABLE directive. 1.67 include_directories ( 1.68 ${CMAKE_SOURCE_DIR}/include 1.69 ) 1.70 1.71 -# Define the executable program file we are creating. We must list all of 1.72 -# the source files. 1.73 -if (WIN32) 1.74 - add_executable (${App_Name} WIN32 1.75 - ${SRCS} 1.76 - ${HDRS} 1.77 - ) 1.78 -else () 1.79 - add_executable (${App_Name} 1.80 - ${SRCS} 1.81 - ${HDRS} 1.82 - ) 1.83 -endif () 1.84 - 1.85 -# Although we listed the library directories above, we also need to list the 1.86 -# individual libraries we will be linking against. 1.87 -target_link_libraries (${App_Name} 1.88 +add_executable (${App_Name}-test 1.89 + src/main.cpp 1.90 ) 1.91 1.92 +target_link_libraries (${App_Name}-test 1.93 + ${App_Name} 1.94 +) 1.95 1.96 -# # An example for a unix library named utils with a test driver program. 1.97 - 1.98 -# set (SRCS_utils 1.99 -# src/utils.cpp 1.100 -# include/utils.h 1.101 -# ) 1.102 - 1.103 -# include_directories ( 1.104 -# ${PROJECT_SOURCE_DIR}/include 1.105 -# ) 1.106 - 1.107 -# # Build both static and shared libraries 1.108 -# # The target properties are needed because, by default, the output name 1.109 -# # will be the name in the add_library command, and we need to have different 1.110 -# # names in the two commands for the shared and static versions, even though 1.111 -# # we want the final files to have the same names with different extensions. 1.112 -# # 1.113 -# # The prefix is needed mostly in case we build on windows, which has no prefix 1.114 -# # by default. 1.115 -# # 1.116 -# # The clean_direct_output option makes sure that the two lib builds don't 1.117 -# # clobber each others temp files since they are being built from the same 1.118 -# # sources. 1.119 - 1.120 -# add_library (utils SHARED 1.121 -# ${SRCS_utils} 1.122 -# ) 1.123 -# set_target_properties (utils PROPERTIES OUTPUT_NAME "utils") 1.124 -# set_target_properties (utils PROPERTIES PREFIX "lib") 1.125 -# set_target_properties (utils PROPERTIES CLEAN_DIRECT_OUTPUT 1) 1.126 - 1.127 -# add_library (utils-static STATIC 1.128 -# ${SRCS_utils} 1.129 -# ) 1.130 -# set_target_properties (utils-static PROPERTIES OUTPUT_NAME "utils") 1.131 -# set_target_properties (utils-static PROPERTIES PREFIX "lib") 1.132 -# set_target_properties (utils-static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 1.133 - 1.134 -# # Build a test application. 1.135 - 1.136 -# add_executable (test 1.137 -# src/main.cpp 1.138 -# ) 1.139 - 1.140 -# target_link_libraries (test 1.141 -# utils 1.142 -# ) 1.143 - 1.144 - 1.145 -################################################################################ 1.146 -# X11 1.147 -# 1.148 -# Note that the FindX11.cmake package does _not_ include most X extensions 1.149 -# in the X11_LIBRARIES variable, although it does pick up their header files 1.150 -# in the X11_INCLUDE_DIR variable. 1.151 -# 1.152 -# To link a program with extensions, such as Xrandr, or Xv, you must manually 1.153 -# update the target_link_libraries to include the appropriate library variable. 1.154 -# ${X11_Xrandr_LIB} is an example. See the FindX11.cmake file for a complete 1.155 -# list of availble variables. 1.156 -# 1.157 - 1.158 -option(Option_X11_Dev "Build an X11 Application." OFF) 1.159 - 1.160 -if (Option_X11_Dev) 1.161 - 1.162 - option(Option_Xrandr "Use Xrandr" OFF) 1.163 - option(Option_Xinerama "Use Xinerama" OFF) 1.164 - 1.165 - 1.166 - ######################################## 1.167 - find_package(X11) 1.168 - if (NOT X11_FOUND) 1.169 - message (FATAL_ERROR "X11 not found!") 1.170 - endif () 1.171 - 1.172 - include_directories( 1.173 - ${X11_INCLUDE_DIR} 1.174 - ${INCLUDE_DIRECTORIES} 1.175 - ) 1.176 - 1.177 - target_link_libraries(${App_Name} 1.178 - ${X11_LIBRARIES} 1.179 - ${TARGET_LINK_LIBRARIES} 1.180 - ) 1.181 - 1.182 - ######################################## 1.183 - if (Option_Xrandr) 1.184 - if (NOT X11_Xrandr_FOUND) 1.185 - message (FATAL_ERRO "Xrandr not found!") 1.186 - endif () 1.187 - target_link_libraries(${App_Name} 1.188 - ${X11_Xrandr_LIB} 1.189 - ${TARGET_LINK_LIBRARIES} 1.190 - ) 1.191 - endif () 1.192 - 1.193 - ######################################## 1.194 - if (Option_Xinerama) 1.195 - if (NOT X11_Xinerama_FOUND) 1.196 - message (FATAL_ERRO "Xinerama not found!") 1.197 - endif () 1.198 - target_link_libraries(${App_Name} 1.199 - ${X11_Xinerama_LIB} 1.200 - ${TARGET_LINK_LIBRARIES} 1.201 - ) 1.202 - endif () 1.203 - 1.204 -endif () 1.205 - 1.206 -################################################################################ 1.207 -# SDL 1.208 -# 1.209 -# Enabling SDL support enables several suboptions to make some common SDL 1.210 -# extension libraries available as well. 1.211 -# 1.212 -# If any of the SDL libraries are not found automatically, you will need 1.213 -# to set the appropriate environment variables and rerun cmake. You will 1.214 -# need to remove the CMake cache before doing so. 1.215 - 1.216 -# If you don't want to set actual environment variables before running 1.217 -# CMake, then uncomment the if block below and put in the actual 1.218 -# locations of your SDL installation. 1.219 - 1.220 -option(Option_SDL_Dev "Build an SDL application." OFF) 1.221 - 1.222 -if (Option_SDL_Dev) 1.223 - 1.224 - # # Force SDL 1.3 only 1.225 - # if (WIN32) 1.226 - # set (ENV{SDLDIR} "c:/gamedev/deps/sdl/SDL-build") 1.227 - # else () 1.228 - # set (ENV{SDLDIR} "/home/eris/gamedev/deps/sdl/SDL-build") 1.229 - # endif () 1.230 - # add_definitions(-DSDL_NO_COMPAT) 1.231 - 1.232 - # SDL base package 1.233 - # To use a version of SDL other than your systems default, set the SDLDIR 1.234 - # environment variable to the installation location of your preferred version. 1.235 - find_package (SDL) 1.236 - if (NOT SDL_FOUND) 1.237 - message (FATAL_ERROR "SDL not found!") 1.238 - endif (NOT SDL_FOUND) 1.239 - include_directories( 1.240 - ${SDL_INCLUDE_DIR} 1.241 - ${INCLUDE_DIRECTORIES} 1.242 - ) 1.243 - target_link_libraries(${App_Name} 1.244 - ${SDL_LIBRARY} 1.245 - ${TARGET_LINK_LIBRARIES} 1.246 - ) 1.247 - 1.248 - # SDL_ttf 1.249 - # Environment variables SDLTTFDIR and SDLDIR will be checked in that order 1.250 - # and if set cmake will try to find SDL_ttf in the specified directory. 1.251 - option(Option_SDL_Dev_SDL_ttf "Use SDL_ttf." OFF) 1.252 - if (Option_SDL_Dev_SDL_ttf) 1.253 - find_package (SDL_ttf) 1.254 - if (NOT SDLTTF_FOUND) 1.255 - message (FATAL_ERROR "SDL_ttf not found!") 1.256 - endif (NOT SDLTTF_FOUND) 1.257 - include_directories( 1.258 - ${SDLTTF_INCLUDE_DIR} 1.259 - ${INCLUDE_DIRECTORIES} 1.260 - ) 1.261 - target_link_libraries(${App_Name} 1.262 - ${SDLTTF_LIBRARY} 1.263 - ${TARGET_LINK_LIBRARIES} 1.264 - ) 1.265 - endif () 1.266 - 1.267 - # SDL_image 1.268 - # Environment variables SDLIMAGEDIR and SDLDIR will be checked in that order 1.269 - # and if set cmake will try to find SDL_image in the specified directory. 1.270 - option(Option_SDL_Dev_SDL_image "Use SDL_image." OFF) 1.271 - if (Option_SDL_Dev_SDL_image) 1.272 - find_package (SDL_image) 1.273 - if (NOT SDLIMAGE_FOUND) 1.274 - message (FATAL_ERROR "SDL_image not found!") 1.275 - endif (NOT SDLIMAGE_FOUND) 1.276 - include_directories( 1.277 - ${SDLIMAGE_INCLUDE_DIR} 1.278 - ${INCLUDE_DIRECTORIES} 1.279 - ) 1.280 - target_link_libraries(${App_Name} 1.281 - ${SDLIMAGE_LIBRARY} 1.282 - ${TARGET_LINK_LIBRARIES} 1.283 - ) 1.284 - endif () 1.285 - 1.286 - # SDL_mixer 1.287 - # Environment variables SDLMIXERDIR and SDLDIR will be checked in that order 1.288 - # and if set cmake will try to find SDL_mixer in the specified directory. 1.289 - option(Option_SDL_Dev_SDL_mixer "Use SDL_mixer." OFF) 1.290 - if (Option_SDL_Dev_SDL_mixer) 1.291 - find_package (SDL_mixer) 1.292 - if (NOT SDLMIXER_FOUND) 1.293 - message (FATAL_ERROR "SDL_mixer not found!") 1.294 - endif (NOT SDLMIXER_FOUND) 1.295 - include_directories( 1.296 - ${SDLMIXER_INCLUDE_DIR} 1.297 - ${INCLUDE_DIRECTORIES} 1.298 - ) 1.299 - target_link_libraries(${App_Name} 1.300 - ${SDLMIXER_LIBRARY} 1.301 - ${TARGET_LINK_LIBRARIES} 1.302 - ) 1.303 - endif () 1.304 - 1.305 - # SDL_net 1.306 - # Environment variables SDLNETDIR and SDLDIR will be checked in that order 1.307 - # and if set cmake will try to find SDL_net in the specified directory. 1.308 - option(Option_SDL_Dev_SDL_net "Use SDL_net." OFF) 1.309 - if (Option_SDL_Dev_SDL_net) 1.310 - find_package (SDL_net) 1.311 - if (NOT SDLNET_FOUND) 1.312 - message (FATAL_ERROR "SDL_net not found!") 1.313 - endif (NOT SDLNET_FOUND) 1.314 - include_directories( 1.315 - ${SDLNET_INCLUDE_DIR} 1.316 - ${INCLUDE_DIRECTORIES} 1.317 - ) 1.318 - target_link_libraries(${App_Name} 1.319 - ${SDLNET_LIBRARY} 1.320 - ${TARGET_LINK_LIBRARIES} 1.321 - ) 1.322 - endif () 1.323 - 1.324 -endif (Option_SDL_Dev) 1.325 - 1.326 - 1.327 -################################################################################ 1.328 - 1.329 -option(Option_OpenGL_Dev "Build an OpenGL Application." OFF) 1.330 - 1.331 -if (Option_OpenGL_Dev) 1.332 - 1.333 - find_package(OpenGL) 1.334 - if (NOT OPENGL_FOUND) 1.335 - message (FATAL_ERROR "OpenGL not found!") 1.336 - endif (NOT OPENGL_FOUND) 1.337 - 1.338 - include_directories( 1.339 - ${OPENGL_INCLUDE_DIR} 1.340 - ${INCLUDE_DIRECTORIES} 1.341 - ) 1.342 - 1.343 - target_link_libraries(${App_Name} 1.344 - ${OPENGL_LIBRARIES} 1.345 - ${TARGET_LINK_LIBRARIES} 1.346 - ) 1.347 - 1.348 - option(Option_GLUT_Dev "Build a GLUT Application." OFF) 1.349 - 1.350 - if (Option_GLUT_Dev) 1.351 - 1.352 - find_package(GLUT) 1.353 - if (NOT GLUT_FOUND) 1.354 - message (FATAL_ERROR "GLUT not found!") 1.355 - endif() 1.356 - 1.357 - include_directories( 1.358 - ${GLUT_INCLUDE_DIR} 1.359 - ${INCLUDE_DIRECTORIES} 1.360 - ) 1.361 - 1.362 - target_link_libraries(${App_Name} 1.363 - ${GLUT_LIBRARIES} 1.364 - ${TARGET_LINK_LIBRARIES} 1.365 - ) 1.366 - 1.367 - endif() 1.368 - 1.369 -endif () 1.370 - 1.371 -################################################################################ 1.372 - 1.373 -option(Option_OpenAL_Dev "Build an OpenAL Application." OFF) 1.374 - 1.375 -if (Option_OpenAL_Dev) 1.376 - 1.377 - find_package(OpenAL) 1.378 - if (NOT OPENAL_FOUND) 1.379 - message (FATAL_ERROR "OpenAL not found!") 1.380 - endif (NOT OPENAL_FOUND) 1.381 - 1.382 - include_directories( 1.383 - ${OPENAL_INCLUDE_DIR} 1.384 - ${INCLUDE_DIRECTORIES} 1.385 - ) 1.386 - 1.387 - target_link_libraries(${App_Name} 1.388 - ${OPENAL_LIBRARY} 1.389 - ${TARGET_LINK_LIBRARIES} 1.390 - ) 1.391 - 1.392 -endif () 1.393 - 1.394 -################################################################################
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/include/Math.h Tue Aug 30 11:08:12 2011 -0500 2.3 @@ -0,0 +1,231 @@ 2.4 +#ifndef MATH_H_ 2.5 +#define MATH_H_ 2.6 + 2.7 +#include <cmath> 2.8 +#include <string> 2.9 + 2.10 +namespace arda 2.11 + { 2.12 + 2.13 + //////////////////////////////////////////////////////////////////////////////// 2.14 + // General notes: 2.15 + // 2.16 + // As much as possible is defined inline for performance. 2.17 + // Using typedef'ed arrays for vectors and matrices necessitates a lot of 2.18 + // redundant function names: scale2, scale3, scale4, for example. 2.19 + // This is, however, perfectly in line with the implementation of OpenGL and 2.20 + // allows the library to maintain optimal memory usage and performance. (I hope!) 2.21 + // In any event, I think I could do far worse than to emulate the OpenGL 2.22 + // working group - a group of people who have studied this for years and are 2.23 + // experts in the field. 2.24 + 2.25 + //////////////////////////////////////////////////////////////////////////////// 2.26 + // Vectors 2.27 + 2.28 + namespace Vector 2.29 + { 2.30 + 2.31 + typedef int Vector2i[2]; 2.32 + typedef float Vector2f[2]; 2.33 + typedef double Vector2d[2]; 2.34 + 2.35 + typedef int Vector3i[3]; 2.36 + typedef float Vector3f[3]; 2.37 + typedef double Vector3d[3]; 2.38 + 2.39 + typedef int Vector4i[4]; 2.40 + typedef float Vector4f[4]; 2.41 + typedef double Vector4d[4]; 2.42 + 2.43 + ////////////////////////////////////////////////////////////////////////// 2.44 + // vectostr*(Vector v) 2.45 + // Returns a C string representation of the vector. 2.46 + // This is one of the few non-inline Vector functions. 2.47 + 2.48 + std::string & vectostr2(Vector2i v, std::string & s); 2.49 + std::string & vectostr2(Vector2f v, std::string & s); 2.50 + std::string & vectostr2(Vector2d v, std::string & s); 2.51 + std::string & vectostr3(Vector3i v, std::string & s); 2.52 + std::string & vectostr3(Vector3f v, std::string & s); 2.53 + std::string & vectostr3(Vector3d v, std::string & s); 2.54 + std::string & vectostr4(Vector4i v, std::string & s); 2.55 + std::string & vectostr4(Vector4f v, std::string & s); 2.56 + std::string & vectostr4(Vector4d v, std::string & s); 2.57 + 2.58 + ////////////////////////////////////////////////////////////////////////// 2.59 + // scale*(Vector v, scalar s) 2.60 + // Multiply a vector by a scalar. 2.61 + // Returns v so that the result may be passed to other functions. 2.62 + 2.63 + inline int * scale2(Vector2i v, const int s) 2.64 + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } 2.65 + inline int * scale2(Vector2i v, const float s) 2.66 + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } 2.67 + inline int * scale2(Vector2i v, const double s) 2.68 + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } 2.69 + 2.70 + inline float * scale2(Vector2f v, const int s) 2.71 + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } 2.72 + inline float * scale2(Vector2f v, const float s) 2.73 + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } 2.74 + inline float * scale2(Vector2f v, const double s) 2.75 + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } 2.76 + 2.77 + inline double * scale2(Vector2d v, const int s) 2.78 + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } 2.79 + inline double * scale2(Vector2d v, const float s) 2.80 + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } 2.81 + inline double * scale2(Vector2d v, const double s) 2.82 + { v[0] = v[0] * s ; v[1] = v[1] * s; return v; } 2.83 + 2.84 + 2.85 + inline int * scale3(Vector3i v, const int s) 2.86 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } 2.87 + inline int * scale3(Vector3i v, const float s) 2.88 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } 2.89 + inline int * scale3(Vector3i v, const double s) 2.90 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } 2.91 + 2.92 + inline float * scale3(Vector3f v, const int s) 2.93 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } 2.94 + inline float * scale3(Vector3f v, const float s) 2.95 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } 2.96 + inline float * scale3(Vector3f v, const double s) 2.97 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } 2.98 + 2.99 + inline double * scale3(Vector3d v, const int s) 2.100 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } 2.101 + inline double * scale3(Vector3d v, const float s) 2.102 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } 2.103 + inline double * scale3(Vector3d v, const double s) 2.104 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; return v; } 2.105 + 2.106 + 2.107 + inline int * scale4(Vector4i v, const int s) 2.108 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } 2.109 + inline int * scale4(Vector4i v, const float s) 2.110 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } 2.111 + inline int * scale4(Vector4i v, const double s) 2.112 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } 2.113 + 2.114 + inline float * scale4(Vector4f v, const int s) 2.115 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } 2.116 + inline float * scale4(Vector4f v, const float s) 2.117 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } 2.118 + inline float * scale4(Vector4f v, const double s) 2.119 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } 2.120 + 2.121 + inline double * scale4(Vector4d v, const int s) 2.122 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } 2.123 + inline double * scale4(Vector4d v, const float s) 2.124 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } 2.125 + inline double * scale4(Vector4d v, const double s) 2.126 + { v[0] = v[0] * s ; v[1] = v[1] * s; v[2] = v[2] * s; v[3] = v[3] * s; return v; } 2.127 + 2.128 + 2.129 + ////////////////////////////////////////////////////////////////////////// 2.130 + // dot*(Vector v1, Vector v2) 2.131 + // The dot product of two vectors, which must be the same type 2.132 + // Returns a long for int vectors, a double for floating point vectors. 2.133 + 2.134 + inline long dot2(Vector2i v1, Vector2i v2) 2.135 + { return (long) v1[0]*v2[0] + (long) v1[1]*v2[1]; } 2.136 + inline double dot2(Vector2f v1, Vector2f v2) 2.137 + { return (double) v1[0]*v2[0] + (double) v1[1]*v2[1]; } 2.138 + inline double dot2(Vector2d v1, Vector2d v2) 2.139 + { return v1[0]*v2[0] + v1[1]*v2[1]; } 2.140 + 2.141 + inline long dot3(Vector3i v1, Vector3i v2) 2.142 + { return (long) v1[0]*v2[0] + (long) v1[1]*v2[1] + (long) v1[2]*v2[2]; } 2.143 + inline double dot3(Vector3f v1, Vector3f v2) 2.144 + { return (double) v1[0]*v2[0] + (double) v1[1]*v2[1] + (double) v1[2]*v2[2]; } 2.145 + inline double dot3(Vector3d v1, Vector3d v2) 2.146 + { return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]; } 2.147 + 2.148 + inline long dot4(Vector4i v1, Vector4i v2) 2.149 + { return (long) v1[0]*v2[0] + (long) v1[1]*v2[1] + (long) v1[2]*v2[2] + (long) v1[3]*v2[3]; } 2.150 + inline double dot4(Vector4f v1, Vector4f v2) 2.151 + { return (double) v1[0]*v2[0] + (double) v1[1]*v2[1] + (double) v1[2]*v2[2] + (double) v1[3]*v2[3]; } 2.152 + inline double dot4(Vector4d v1, Vector4d v2) 2.153 + { return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2] + v1[3]*v2[3]; } 2.154 + 2.155 + 2.156 + ////////////////////////////////////////////////////////////////////////// 2.157 + // length*(Vector v) 2.158 + // Get the length of a vector. 2.159 + // Returns a float for float for float vectors, and a double for 2.160 + // int and double vectors. 2.161 + 2.162 + inline double length2(Vector2i v) 2.163 + { return sqrt((dot2(v, v))); } 2.164 + inline float length2(Vector2f v) 2.165 + { return sqrtf((dot2(v, v))); } 2.166 + inline double length2(Vector2d v) 2.167 + { return sqrt((dot2(v, v))); } 2.168 + 2.169 + inline double length3(Vector3i v) 2.170 + { return sqrt((dot3(v, v))); } 2.171 + inline float length3(Vector3f v) 2.172 + { return sqrtf((dot3(v, v))); } 2.173 + inline double length3(Vector3d v) 2.174 + { return sqrt((dot3(v, v))); } 2.175 + 2.176 + inline double length4(Vector4i v) 2.177 + { return sqrt((dot4(v, v))); } 2.178 + inline float length4(Vector4f v) 2.179 + { return sqrtf((dot4(v, v))); } 2.180 + inline double length4(Vector4d v) 2.181 + { return sqrt((dot4(v, v))); } 2.182 + 2.183 + 2.184 + ////////////////////////////////////////////////////////////////////////// 2.185 + // add*(Vector v1, Vector v2, Vector vres) 2.186 + // Add two vectors together. 2.187 + // Returns vres so that the result may be used in further 2.188 + // calculations. 2.189 + 2.190 + inline int * add2(Vector2i v1, Vector2i v2, Vector2i vres) 2.191 + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; return vres; } 2.192 + inline float * add2(Vector2f v1, Vector2f v2, Vector2f vres) 2.193 + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; return vres; } 2.194 + inline double * add2(Vector2d v1, Vector2d v2, Vector2d vres) 2.195 + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; return vres; } 2.196 + 2.197 + inline int * add3(Vector3i v1, Vector3i v2, Vector3i vres) 2.198 + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; return vres; } 2.199 + inline float * add3(Vector3f v1, Vector3f v2, Vector3f vres) 2.200 + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; return vres; } 2.201 + inline double * add3(Vector3d v1, Vector3d v2, Vector3d vres) 2.202 + { vres[0] = v1[0] + v2[0]; vres[1] = v1[1] + v2[1]; vres[2] = v1[2] + v2[2]; return vres; } 2.203 + 2.204 + inline int * add4(Vector4i v1, Vector4i v2, Vector4i vres) 2.205 + { 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;} 2.206 + inline float * add4(Vector4f v1, Vector4f v2, Vector4f vres) 2.207 + { 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; } 2.208 + inline double * add4(Vector4d v1, Vector4d v2, Vector4d vres) 2.209 + { 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; } 2.210 + 2.211 + ////////////////////////////////////////////////////////////////////////// 2.212 + // normalize*(Vector v) 2.213 + // Normalizes the vecor. Only defined for floating point vectors. 2.214 + // Returns the vector. 2.215 + 2.216 + inline float * normalize2(Vector2f v) 2.217 + { float l = length2(v); v[0] /= l; v[1] /= l; return v; } 2.218 + inline double * normalize2(Vector2d v) 2.219 + { double l = length2(v); v[0] /= l; v[1] /= l; return v; } 2.220 + 2.221 + inline float * normalize3(Vector3f v) 2.222 + { float l = length3(v); v[0] /= l; v[1] /= l; v[2] /= l; return v; } 2.223 + inline double * normalize3(Vector3d v) 2.224 + { double l = length3(v); v[0] /= l; v[1] /= l; v[2] /= l; return v; } 2.225 + 2.226 + inline float * normalize4(Vector4f v) 2.227 + { float l = length4(v); v[0] /= l; v[1] /= l; v[2] /= l; v[3] /= l; return v; } 2.228 + inline double * normalize4(Vector4d v) 2.229 + { double l = length4(v); v[0] /= l; v[1] /= l; v[2] /= l; v[3] /= l; return v; } 2.230 + } 2.231 + 2.232 +} // namespace arda 2.233 + 2.234 +#endif
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/notes Tue Aug 30 11:08:12 2011 -0500 3.3 @@ -0,0 +1,31 @@ 3.4 +-------------------------------------------------------------------------------- 3.5 +Vectors 3.6 + 3.7 +Operations to support 3.8 + 3.9 +* dot 3.10 +* length (norm) 3.11 +* vector addition 3.12 +* scalar multiplication 3.13 +* normalize 3.14 +cross 3.15 +triple product i.e. dot(u, cross(v, w)) Is this really that common? 3.16 +angle between two vectors 3.17 +projection of one vector onto another. 3.18 +* vector to string 3.19 + 3.20 +Class with operator overloading? 3.21 + 3.22 +-------------------------------------------------------------------------------- 3.23 +Matrices 3.24 + 3.25 +det 3.26 +transpose 3.27 +inverse 3.28 +add 3.29 +matrix multiplication 3.30 +vector multiplication 3.31 +scalar multiplication 3.32 + 3.33 +-------------------------------------------------------------------------------- 3.34 +Quaternions
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/notes~ Tue Aug 30 11:08:12 2011 -0500 4.3 @@ -0,0 +1,28 @@ 4.4 +-------------------------------------------------------------------------------- 4.5 +Vectors 4.6 + 4.7 +Operations to support 4.8 + 4.9 +norm 4.10 +dot 4.11 +cross 4.12 +normalize 4.13 +vector addition 4.14 +scalar multiplication 4.15 +triple product i.e. dot(u, cross(v, w)) Is this really that common? 4.16 + 4.17 +Class with operator overloading? 4.18 + 4.19 +-------------------------------------------------------------------------------- 4.20 +Matrices 4.21 + 4.22 +det 4.23 +transpose 4.24 +inverse 4.25 +add 4.26 +matrix multiplication 4.27 +vector multiplication 4.28 +scalar multiplication 4.29 + 4.30 +-------------------------------------------------------------------------------- 4.31 +Quaternions
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/Math.cpp Tue Aug 30 11:08:12 2011 -0500 5.3 @@ -0,0 +1,95 @@ 5.4 +#include "Math.h" 5.5 + 5.6 +#include <iostream> 5.7 +#include <string> 5.8 +#include <sstream> 5.9 + 5.10 +//////////////////////////////////////////////////////////////////////////////// 5.11 +std::string & arda::Vector::vectostr2(Vector2i v, std::string & s) 5.12 + { 5.13 + s.clear(); 5.14 + std::stringstream ss; 5.15 + ss << "{ " << v[0] << ", " << v[1] << " }"; 5.16 + s = ss.str(); 5.17 + return s; 5.18 + } 5.19 + 5.20 +//////////////////////////////////////////////////////////////////////////////// 5.21 +std::string & arda::Vector::vectostr2(Vector2f v, std::string & s) 5.22 + { 5.23 + s.clear(); 5.24 + std::stringstream ss; 5.25 + ss << "{ " << v[0] << ", " << v[1] << " }"; 5.26 + s = ss.str(); 5.27 + return s; 5.28 + } 5.29 + 5.30 +//////////////////////////////////////////////////////////////////////////////// 5.31 +std::string & arda::Vector::vectostr2(Vector2d v, std::string & s) 5.32 + { 5.33 + s.clear(); 5.34 + std::stringstream ss; 5.35 + ss << "{ " << v[0] << ", " << v[1] << " }"; 5.36 + s = ss.str(); 5.37 + return s; 5.38 + } 5.39 + 5.40 +//////////////////////////////////////////////////////////////////////////////// 5.41 +std::string & arda::Vector::vectostr3(Vector3i v, std::string & s) 5.42 + { 5.43 + s.clear(); 5.44 + std::stringstream ss; 5.45 + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; 5.46 + s = ss.str(); 5.47 + return s; 5.48 + } 5.49 + 5.50 +//////////////////////////////////////////////////////////////////////////////// 5.51 +std::string & arda::Vector::vectostr3(Vector3f v, std::string & s) 5.52 + { 5.53 + s.clear(); 5.54 + std::stringstream ss; 5.55 + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; 5.56 + s = ss.str(); 5.57 + return s; 5.58 + } 5.59 + 5.60 +//////////////////////////////////////////////////////////////////////////////// 5.61 +std::string & arda::Vector::vectostr3(Vector3d v, std::string & s) 5.62 + { 5.63 + s.clear(); 5.64 + std::stringstream ss; 5.65 + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; 5.66 + s = ss.str(); 5.67 + return s; 5.68 + } 5.69 + 5.70 +//////////////////////////////////////////////////////////////////////////////// 5.71 +std::string & arda::Vector::vectostr4(Vector4i v, std::string & s) 5.72 + { 5.73 + s.clear(); 5.74 + std::stringstream ss; 5.75 + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << " }"; 5.76 + s = ss.str(); 5.77 + return s; 5.78 + } 5.79 + 5.80 +//////////////////////////////////////////////////////////////////////////////// 5.81 +std::string & arda::Vector::vectostr4(Vector4f v, std::string & s) 5.82 + { 5.83 + s.clear(); 5.84 + std::stringstream ss; 5.85 + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << " }"; 5.86 + s = ss.str(); 5.87 + return s; 5.88 + } 5.89 + 5.90 +//////////////////////////////////////////////////////////////////////////////// 5.91 +std::string & arda::Vector::vectostr4(Vector4d v, std::string & s) 5.92 + { 5.93 + s.clear(); 5.94 + std::stringstream ss; 5.95 + ss << "{ " << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << " }"; 5.96 + s = ss.str(); 5.97 + return s; 5.98 + }
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/main.cpp Tue Aug 30 11:08:12 2011 -0500 6.3 @@ -0,0 +1,144 @@ 6.4 +#include <iostream> 6.5 +#include <iomanip> 6.6 +#include <string> 6.7 +using namespace std; 6.8 + 6.9 +#include "Math.h" 6.10 +using namespace arda::Vector; 6.11 + 6.12 +int main (int argc, char * argv[]) 6.13 + { 6.14 + string s1, s2; 6.15 + 6.16 + // Vector::scale*() 6.17 + { 6.18 + cout << "===============================================================" << endl << 6.19 + "Testing Vector::scale*()" << endl; 6.20 + 6.21 + Vector2i v2i = { 1, 2 }; 6.22 + cout << setw(40) << "v2i: " << vectostr2(v2i, s1) << endl; 6.23 + scale2(v2i, 2); 6.24 + cout << setw(40) << "scale2(v2i, 2): " << vectostr2(v2i, s1) << endl; 6.25 + scale2(v2i, 2.4); 6.26 + cout << setw(40) << "scale2(vi, 2.4): " << vectostr2(v2i, s1) << endl; 6.27 + scale2(scale2(v2i, 2), 3); 6.28 + cout << setw(40) << "scale2(scale2(v2i, 2), 3): " << vectostr2(v2i, s1) << endl; 6.29 + 6.30 + 6.31 + Vector3f v3f = { 3.0, 4.1, 5.2 }; 6.32 + cout << setw(40) << "v3f: " << vectostr3(v3f, s1) << endl; 6.33 + scale3(v3f, 2.2f); 6.34 + cout << setw(40) << "scale3(v3f, 2.2f): " << vectostr3(v3f, s1) << endl; 6.35 + scale3(v3f, 2); 6.36 + cout << setw(40) << "scale3(v2f, 2): " << vectostr3(v3f, s1) << endl; 6.37 + 6.38 + Vector4d v4d = { 6.0L, 7.1L, 8.2L, 9.3L }; 6.39 + cout << setw(40) << "vd4: " << vectostr4(v4d, s1) << endl; 6.40 + scale4(v4d, 2.0); 6.41 + cout << setw(40) << "scale4(v4d, 2.0): " << vectostr4(v4d, s1) << endl; 6.42 + scale4(v4d, 2); 6.43 + cout << setw(40) << "scale4(v4d, 2): " << vectostr4(v4d, s1) << endl; 6.44 + } 6.45 + 6.46 + 6.47 + // Vector::dot*() 6.48 + { 6.49 + cout << "===============================================================" << endl << 6.50 + "Testing Vector::dot*()" << endl; 6.51 + 6.52 + Vector2i v2i[2] = { { 1, 2 }, { 3, 4 } }; 6.53 + cout << setw(40) << "v2i[0]: " << vectostr2(v2i[0], s1) << endl; 6.54 + cout << setw(40) << "v2i[1]: " << vectostr2(v2i[1], s1) << endl; 6.55 + cout << setw(40) << "dot2(v2i[0], v2i[1]): " << dot2(v2i[0], v2i[1]) << endl; 6.56 + 6.57 + Vector3f v3f[3] = { { 1.1f, 2.2f, 3.3f }, { 4.4f, 5.5f, 6.6f } }; 6.58 + cout << setw(40) << "v3f[0]: " << vectostr3(v3f[0], s1) << endl; 6.59 + cout << setw(40) << "v3f[1]: " << vectostr3(v3f[1], s1) << endl; 6.60 + cout << setw(40) << "dot3(v3f[0], v3f[1]): " << dot3(v3f[0], v3f[1]) << endl; 6.61 + 6.62 + Vector4d v4d[3] = { { 1.1, 2.2, 3.3, 4.4 }, { 5.5, 6.6, 7.7, 8.8 } }; 6.63 + cout << setw(40) << "v4d[0]: " << vectostr4(v4d[0], s1) << endl; 6.64 + cout << setw(40) << "v4d[1]: " << vectostr4(v4d[1], s1) << endl; 6.65 + cout << setw(40) << "dot4(v4d[0], v4d[1]): " << dot4(v4d[0], v4d[1]) << endl; 6.66 + 6.67 + } 6.68 + 6.69 + // Vector::length*() 6.70 + { 6.71 + cout << "===============================================================" << endl << 6.72 + "Testing Vector::length*()" << endl; 6.73 + 6.74 + Vector2i v2i = { 1, 2 }; 6.75 + cout << setw(40) << "v2i: " << vectostr2(v2i, s1) << endl; 6.76 + cout << setw(40) << "length2(v2i): " << length2(v2i) << endl; 6.77 + 6.78 + Vector3f v3f = { 1.1f, 2.2f, 3.3f }; 6.79 + cout << setw(40) << "v3f: " << vectostr3(v3f, s1) << endl; 6.80 + cout << setw(40) << "length3(v3f): " << length3(v3f) << endl; 6.81 + 6.82 + Vector4d v4d = { 1.1, 2.2, 3.3, 4.4 }; 6.83 + cout << setw(40) << "v4d: " << vectostr4(v4d, s1) << endl; 6.84 + cout << setw(40) << "length4(v4d): " << length4(v4d) << endl; 6.85 + 6.86 + } 6.87 + 6.88 + // Vector::add*() 6.89 + { 6.90 + cout << "===============================================================" << endl << 6.91 + "Testing Vector::add*()" << endl; 6.92 + 6.93 + Vector2i v2i[3] = { { 1, 2 }, { 3, 4 } }; 6.94 + Vector2i v2ires; 6.95 + cout << setw(40) << "v2i[0]: " << vectostr2(v2i[0], s1) << endl; 6.96 + cout << setw(40) << "v2i[1]: " << vectostr2(v2i[1], s1) << endl; 6.97 + add2(v2i[0], v2i[1], v2ires); 6.98 + cout << setw(40) << "add2(v2i[0], v2i[1], v2ires): " << vectostr2(v2ires, s1) << endl; 6.99 + cout << setw(40) << "length2(add2(v2i[0], v2i[1], v2ires)): " << length2(add2(v2i[0], v2i[1], v2ires)) << endl; 6.100 + 6.101 + Vector3f v3f[3] = { { 1.1f, 2.2f, 3.3f }, { 4.4f, 5.5f, 6.6f } }; 6.102 + Vector3f v3fres; 6.103 + cout << setw(40) << "v3f[0]: " << vectostr3(v3f[0], s1) << endl; 6.104 + cout << setw(40) << "v3f[1]: " << vectostr3(v3f[1], s1) << endl; 6.105 + add3(v3f[0], v3f[1], v3fres); 6.106 + cout << setw(40) << "add3(v3f[0], v3f[1], v3fres): " << vectostr3(v3fres, s1) << endl; 6.107 + cout << setw(40) << "length3(add3(v3f[0], v3f[1], v3fres)): " << length3(add3(v3f[0], v3f[1], v3fres)) << endl; 6.108 + 6.109 + Vector4d v4d[3] = { { 1.1, 2.2, 3.3, 4.4 }, { 5.5, 6.6, 7.7, 8.8 } }; 6.110 + Vector4d v4dres; 6.111 + cout << setw(40) << "v4d[0]: " << vectostr4(v4d[0], s1) << endl; 6.112 + cout << setw(40) << "v4d[1]: " << vectostr4(v4d[1], s1) << endl; 6.113 + add4(v4d[0], v4d[1], v4dres); 6.114 + cout << setw(40) << "add4(v4d[0], v4d[1], v4dres): " << vectostr4(v4dres, s1) << endl; 6.115 + cout << setw(40) << "length4(add4(v4d[0], v4d[1], v4dres)): " << length4(add4(v4d[0], v4d[1], v4dres)) << endl; 6.116 + 6.117 + } 6.118 + 6.119 + // Vector::add*() 6.120 + { 6.121 + cout << "===============================================================" << endl << 6.122 + "Testing Vector::normalize*()" << endl; 6.123 + 6.124 + Vector2f v2f = { 1.1, 2.2 }; 6.125 + cout << setw(40) << "v2f: " << vectostr2(v2f, s1) << endl; 6.126 + normalize2(v2f); 6.127 + cout << setw(40) << "normalize2(v2f): " << vectostr2(v2f, s1) << endl; 6.128 + scale2(normalize2(v2f), 2.0); 6.129 + cout << setw(40) << "scale(normalize2(v2f), 2.0): " << vectostr2(v2f, s1) << endl; 6.130 + 6.131 + Vector3f v3f = { 1.1f, 2.2f, 3.3f }; 6.132 + cout << setw(40) << "v3f: " << vectostr3(v3f, s1) << endl; 6.133 + normalize3(v3f); 6.134 + cout << setw(40) << "normalize3(v3f): " << vectostr3(v3f, s1) << endl; 6.135 + scale3(normalize3(v3f), 2.0); 6.136 + cout << setw(40) << "scale(normalize3(v3f), 2.0): " << vectostr3(v3f, s1) << endl; 6.137 + 6.138 + Vector4d v4d = { 1.1, 2.2, 3.3, 4.4 }; 6.139 + cout << setw(40) << "v4d: " << vectostr4(v4d, s1) << endl; 6.140 + normalize4(v4d); 6.141 + cout << setw(40) << "normalize4(v4d): " << vectostr4(v4d, s1) << endl; 6.142 + scale4(normalize4(v4d), 2.0); 6.143 + cout << setw(40) << "scale4(normalize4(v4d), 2.0): " << vectostr4(v4d, s1) << endl; 6.144 + 6.145 + } 6.146 + 6.147 + }