From 72c06a258940696093f255fb1061beb58e1cdd0b Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sat, 29 May 2010 07:25:38 +0000 Subject: copied refactor to trunk --- src/base/tl/algorithm.h | 135 +++++++++++++++++ src/base/tl/algorithm.hpp | 135 ----------------- src/base/tl/allocator.h | 15 ++ src/base/tl/allocator.hpp | 15 -- src/base/tl/array.h | 341 +++++++++++++++++++++++++++++++++++++++++++ src/base/tl/array.hpp | 341 ------------------------------------------- src/base/tl/base.h | 19 +++ src/base/tl/base.hpp | 19 --- src/base/tl/math.hpp | 45 ------ src/base/tl/matrix.hpp | 163 --------------------- src/base/tl/quat.hpp | 211 -------------------------- src/base/tl/range.h | 232 +++++++++++++++++++++++++++++ src/base/tl/range.hpp | 232 ----------------------------- src/base/tl/sorted_array.h | 31 ++++ src/base/tl/sorted_array.hpp | 31 ---- src/base/tl/stream.hpp | 65 --------- src/base/tl/string.h | 68 +++++++++ src/base/tl/string.hpp | 68 --------- src/base/tl/vector.hpp | 198 ------------------------- 19 files changed, 841 insertions(+), 1523 deletions(-) create mode 100644 src/base/tl/algorithm.h delete mode 100644 src/base/tl/algorithm.hpp create mode 100644 src/base/tl/allocator.h delete mode 100644 src/base/tl/allocator.hpp create mode 100644 src/base/tl/array.h delete mode 100644 src/base/tl/array.hpp create mode 100644 src/base/tl/base.h delete mode 100644 src/base/tl/base.hpp delete mode 100644 src/base/tl/math.hpp delete mode 100644 src/base/tl/matrix.hpp delete mode 100644 src/base/tl/quat.hpp create mode 100644 src/base/tl/range.h delete mode 100644 src/base/tl/range.hpp create mode 100644 src/base/tl/sorted_array.h delete mode 100644 src/base/tl/sorted_array.hpp delete mode 100644 src/base/tl/stream.hpp create mode 100644 src/base/tl/string.h delete mode 100644 src/base/tl/string.hpp delete mode 100644 src/base/tl/vector.hpp (limited to 'src/base/tl') diff --git a/src/base/tl/algorithm.h b/src/base/tl/algorithm.h new file mode 100644 index 00000000..32c2da73 --- /dev/null +++ b/src/base/tl/algorithm.h @@ -0,0 +1,135 @@ +#ifndef TL_FILE_ALGORITHM_HPP +#define TL_FILE_ALGORITHM_HPP + +#include "range.h" + + +/* + insert 4 + v + 1 2 3 4 5 6 + +*/ + + +template +R partition_linear(R range, T value) +{ + concept_empty::check(range); + concept_forwarditeration::check(range); + concept_sorted::check(range); + + for(; !range.empty(); range.pop_front()) + { + if(!(range.front() < value)) + return range; + } + return range; +} + + +template +R partition_binary(R range, T value) +{ + concept_empty::check(range); + concept_index::check(range); + concept_size::check(range); + concept_slice::check(range); + concept_sorted::check(range); + + if(range.empty()) + return range; + if(range.back() < value) + return R(); + + while(range.size() > 1) + { + unsigned pivot = (range.size()-1)/2; + if(range.index(pivot) < value) + range = range.slice(pivot+1, range.size()-1); + else + range = range.slice(0, pivot+1); + } + return range; +} + +template +R find_linear(R range, T value) +{ + concept_empty::check(range); + concept_forwarditeration::check(range); + for(; !range.empty(); range.pop_front()) + if(value == range.front()) + break; + return range; +} + +template +R find_binary(R range, T value) +{ + range = partition_linear(range, value); + if(range.empty()) return range; + if(range.front() == value) return range; + return R(); +} + + +template +void sort_bubble(R range) +{ + concept_empty::check(range); + concept_forwarditeration::check(range); + concept_backwarditeration::check(range); + + // slow bubblesort :/ + for(; !range.empty(); range.pop_back()) + { + R section = range; + typename R::type *prev = §ion.front(); + section.pop_front(); + for(; !section.empty(); section.pop_front()) + { + typename R::type *cur = §ion.front(); + if(*cur < *prev) + swap(*cur, *prev); + prev = cur; + } + } +} + +/* +template +void sort_quick(R range) +{ + concept_index::check(range); +}*/ + + +template +void sort(R range) +{ + sort_bubble(range); +} + + +template +bool sort_verify(R range) +{ + concept_empty::check(range); + concept_forwarditeration::check(range); + + typename R::type *prev = &range.front(); + range.pop_front(); + for(; !range.empty(); range.pop_front()) + { + typename R::type *cur = &range.front(); + + if(*cur < *prev) + return false; + prev = cur; + } + + return true; +} + +#endif // TL_FILE_ALGORITHMS_HPP diff --git a/src/base/tl/algorithm.hpp b/src/base/tl/algorithm.hpp deleted file mode 100644 index 9d78810b..00000000 --- a/src/base/tl/algorithm.hpp +++ /dev/null @@ -1,135 +0,0 @@ -#ifndef TL_FILE_ALGORITHM_HPP -#define TL_FILE_ALGORITHM_HPP - -#include "range.hpp" - - -/* - insert 4 - v - 1 2 3 4 5 6 - -*/ - - -template -R partition_linear(R range, T value) -{ - concept_empty::check(range); - concept_forwarditeration::check(range); - concept_sorted::check(range); - - for(; !range.empty(); range.pop_front()) - { - if(!(range.front() < value)) - return range; - } - return range; -} - - -template -R partition_binary(R range, T value) -{ - concept_empty::check(range); - concept_index::check(range); - concept_size::check(range); - concept_slice::check(range); - concept_sorted::check(range); - - if(range.empty()) - return range; - if(range.back() < value) - return R(); - - while(range.size() > 1) - { - unsigned pivot = (range.size()-1)/2; - if(range.index(pivot) < value) - range = range.slice(pivot+1, range.size()-1); - else - range = range.slice(0, pivot+1); - } - return range; -} - -template -R find_linear(R range, T value) -{ - concept_empty::check(range); - concept_forwarditeration::check(range); - for(; !range.empty(); range.pop_front()) - if(value < range.front()) - break; - return range; -} - -template -R find_binary(R range, T value) -{ - range = partition_linear(range, value); - if(range.empty()) return range; - if(range.front() == value) return range; - return R(); -} - - -template -void sort_bubble(R range) -{ - concept_empty::check(range); - concept_forwarditeration::check(range); - concept_backwarditeration::check(range); - - // slow bubblesort :/ - for(; !range.empty(); range.pop_back()) - { - R section = range; - typename R::type *prev = §ion.front(); - section.pop_front(); - for(; !section.empty(); section.pop_front()) - { - typename R::type *cur = §ion.front(); - if(*cur < *prev) - swap(*cur, *prev); - prev = cur; - } - } -} - -/* -template -void sort_quick(R range) -{ - concept_index::check(range); -}*/ - - -template -void sort(R range) -{ - sort_bubble(range); -} - - -template -bool sort_verify(R range) -{ - concept_empty::check(range); - concept_forwarditeration::check(range); - - typename R::type *prev = &range.front(); - range.pop_front(); - for(; !range.empty(); range.pop_front()) - { - typename R::type *cur = &range.front(); - - if(*cur < *prev) - return false; - prev = cur; - } - - return true; -} - -#endif // TL_FILE_ALGORITHMS_HPP diff --git a/src/base/tl/allocator.h b/src/base/tl/allocator.h new file mode 100644 index 00000000..3baa1c19 --- /dev/null +++ b/src/base/tl/allocator.h @@ -0,0 +1,15 @@ +#ifndef TL_FILE_ALLOCATOR_HPP +#define TL_FILE_ALLOCATOR_HPP + +template +class allocator_default +{ +public: + static T *alloc() { return new T; } + static void free(T *p) { delete p; } + + static T *alloc_array(int size) { return new T [size]; } + static void free_array(T *p) { delete [] p; } +}; + +#endif // TL_FILE_ALLOCATOR_HPP diff --git a/src/base/tl/allocator.hpp b/src/base/tl/allocator.hpp deleted file mode 100644 index 3baa1c19..00000000 --- a/src/base/tl/allocator.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef TL_FILE_ALLOCATOR_HPP -#define TL_FILE_ALLOCATOR_HPP - -template -class allocator_default -{ -public: - static T *alloc() { return new T; } - static void free(T *p) { delete p; } - - static T *alloc_array(int size) { return new T [size]; } - static void free_array(T *p) { delete [] p; } -}; - -#endif // TL_FILE_ALLOCATOR_HPP diff --git a/src/base/tl/array.h b/src/base/tl/array.h new file mode 100644 index 00000000..580f4682 --- /dev/null +++ b/src/base/tl/array.h @@ -0,0 +1,341 @@ +#ifndef TL_FILE_ARRAY_HPP +#define TL_FILE_ARRAY_HPP + +#include "range.h" +#include "allocator.h" + + +/* + Class: array + Normal dynamic array class + + Remarks: + - Grows 50% each time it needs to fit new items + - Use set_size() if you know how many elements + - Use optimize() to reduce the needed space. +*/ +template > +class array : private ALLOCATOR +{ + void init() + { + list = 0x0; + clear(); + } + +public: + typedef plain_range range; + + /* + Function: array constructor + */ + array() + { + init(); + } + + /* + Function: array copy constructor + */ + array(const array &other) + { + init(); + set_size(other.size()); + for(int i = 0; i < size(); i++) + (*this)[i] = other[i]; + } + + + /* + Function: array destructor + */ + ~array() + { + ALLOCATOR::free_array(list); + list = 0x0; + } + + + /* + Function: delete_all + + Remarks: + - Invalidates ranges + */ + void delete_all() + { + for(int i = 0; i < size(); i++) + delete list[i]; + clear(); + } + + + /* + Function: clear + + Remarks: + - Invalidates ranges + */ + void clear() + { + ALLOCATOR::free_array(list); + list_size = 1; + list = ALLOCATOR::alloc_array(list_size); + num_elements = 0; + } + + /* + Function: size + */ + int size() const + { + return num_elements; + } + + /* + Function: remove_index_fast + + Remarks: + - Invalidates ranges + */ + void remove_index_fast(int index) + { + list[index] = list[num_elements-1]; + set_size(size()-1); + } + + /* + Function: remove_fast + + Remarks: + - Invalidates ranges + */ + void remove_fast(const T& item) + { + for(int i = 0; i < size(); i++) + if(list[i] == item) + { + remove_index_fast(i); + return; + } + } + + /* + Function: remove_index + + Remarks: + - Invalidates ranges + */ + void remove_index(int index) + { + for(int i = index+1; i < num_elements; i++) + list[i-1] = list[i]; + + set_size(size()-1); + } + + /* + Function: remove + + Remarks: + - Invalidates ranges + */ + bool remove(const T& item) + { + for(int i = 0; i < size(); i++) + if(list[i] == item) + { + remove_index(i); + return true; + } + return false; + } + + /* + Function: add + Adds an item to the array. + + Arguments: + item - Item to add. + + Remarks: + - Invalidates ranges + - See remarks about how the array grows. + */ + int add(const T& item) + { + incsize(); + set_size(size()+1); + list[num_elements-1] = item; + return num_elements-1; + } + + /* + Function: insert + Inserts an item into the array at a specified location. + + Arguments: + item - Item to insert. + r - Range where to insert the item + + Remarks: + - Invalidates ranges + - See remarks about how the array grows. + */ + int insert(const T& item, range r) + { + if(r.empty()) + return add(item); + + int index = (int)(&r.front()-list); + incsize(); + set_size(size()+1); + + for(int i = num_elements-1; i > index; i--) + list[i] = list[i-1]; + + list[index] = item; + + return num_elements-1; + } + + /* + Function: operator[] + */ + T& operator[] (int index) + { + return list[index]; + } + + /* + Function: const operator[] + */ + const T& operator[] (int index) const + { + return list[index]; + } + + /* + Function: base_ptr + */ + T *base_ptr() + { + return list; + } + + /* + Function: base_ptr + */ + const T *base_ptr() const + { + return list; + } + + /* + Function: set_size + Resizes the array to the specified size. + + Arguments: + new_size - The new size for the array. + */ + void set_size(int new_size) + { + alloc(new_size); + num_elements = new_size; + } + + /* + Function: hint_size + Allocates the number of elements wanted but + does not increase the list size. + + Arguments: + hint - Size to allocate. + + Remarks: + - If the hint is smaller then the number of elements, nothing will be done. + - Invalidates ranges + */ + void hint_size(int hint) + { + if(num_elements < hint) + alloc(hint); + } + + + /* + Function: optimize + Removes unnessasary data, returns how many bytes was earned. + + Remarks: + - Invalidates ranges + */ + int optimize() + { + int before = memusage(); + alloc(num_elements); + return before - memusage(); + } + + /* + Function: memusage + Returns how much memory this dynamic array is using + */ + int memusage() + { + return sizeof(array) + sizeof(T)*size; + } + + /* + Function: operator=(array) + + Remarks: + - Invalidates ranges + */ + array &operator = (const array &other) + { + set_size(other.size()); + for(int i = 0; i < size(); i++) + (*this)[i] = other[i]; + return *this; + } + + /* + Function: all + Returns a range that contains the whole array. + */ + range all() { return range(list, list+num_elements); } +protected: + + void incsize() + { + if(num_elements == list_size) + { + if(list_size < 2) + alloc(list_size+1); + else + alloc(list_size+list_size/2); + } + } + + void alloc(int new_len) + { + list_size = new_len; + T *new_list = ALLOCATOR::alloc_array(list_size); + + int end = num_elements < list_size ? num_elements : list_size; + for(int i = 0; i < end; i++) + new_list[i] = list[i]; + + ALLOCATOR::free_array(list); + + num_elements = num_elements < list_size ? num_elements : list_size; + list = new_list; + } + + T *list; + int list_size; + int num_elements; +}; + +#endif // TL_FILE_ARRAY_HPP diff --git a/src/base/tl/array.hpp b/src/base/tl/array.hpp deleted file mode 100644 index 7b8698d4..00000000 --- a/src/base/tl/array.hpp +++ /dev/null @@ -1,341 +0,0 @@ -#ifndef TL_FILE_ARRAY_HPP -#define TL_FILE_ARRAY_HPP - -#include "range.hpp" -#include "allocator.hpp" - - -/* - Class: array - Normal dynamic array class - - Remarks: - - Grows 50% each time it needs to fit new items - - Use set_size() if you know how many elements - - Use optimize() to reduce the needed space. -*/ -template > -class array : private ALLOCATOR -{ - void init() - { - list = 0x0; - clear(); - } - -public: - typedef plain_range range; - - /* - Function: array constructor - */ - array() - { - init(); - } - - /* - Function: array copy constructor - */ - array(const array &other) - { - init(); - set_size(other.size()); - for(int i = 0; i < size(); i++) - (*this)[i] = other[i]; - } - - - /* - Function: array destructor - */ - ~array() - { - ALLOCATOR::free_array(list); - list = 0x0; - } - - - /* - Function: delete_all - - Remarks: - - Invalidates ranges - */ - void delete_all() - { - for(int i = 0; i < size(); i++) - delete list[i]; - clear(); - } - - - /* - Function: clear - - Remarks: - - Invalidates ranges - */ - void clear() - { - ALLOCATOR::free_array(list); - list_size = 1; - list = ALLOCATOR::alloc_array(list_size); - num_elements = 0; - } - - /* - Function: size - */ - int size() const - { - return num_elements; - } - - /* - Function: remove_index_fast - - Remarks: - - Invalidates ranges - */ - void remove_index_fast(int index) - { - list[index] = list[num_elements-1]; - set_size(size()-1); - } - - /* - Function: remove_fast - - Remarks: - - Invalidates ranges - */ - void remove_fast(const T& item) - { - for(int i = 0; i < size(); i++) - if(list[i] == item) - { - remove_index_fast(i); - return; - } - } - - /* - Function: remove_index - - Remarks: - - Invalidates ranges - */ - void remove_index(int index) - { - for(int i = index+1; i < num_elements; i++) - list[i-1] = list[i]; - - set_size(size()-1); - } - - /* - Function: remove - - Remarks: - - Invalidates ranges - */ - bool remove(const T& item) - { - for(int i = 0; i < size(); i++) - if(list[i] == item) - { - remove_index(i); - return true; - } - return false; - } - - /* - Function: add - Adds an item to the array. - - Arguments: - item - Item to add. - - Remarks: - - Invalidates ranges - - See remarks about how the array grows. - */ - int add(const T& item) - { - incsize(); - num_elements = size()+1; - list[num_elements-1] = item; - return num_elements-1; - } - - /* - Function: insert - Inserts an item into the array at a specified location. - - Arguments: - item - Item to insert. - r - Range where to insert the item - - Remarks: - - Invalidates ranges - - See remarks about how the array grows. - */ - int insert(const T& item, range r) - { - if(r.empty()) - return add(item); - - int index = (int)(&r.front()-list); - incsize(); - num_elements = size()+1; - - for(int i = num_elements-1; i > index; i--) - list[i] = list[i-1]; - - list[index] = item; - - return num_elements-1; - } - - /* - Function: operator[] - */ - T& operator[] (int index) - { - return list[index]; - } - - /* - Function: const operator[] - */ - const T& operator[] (int index) const - { - return list[index]; - } - - /* - Function: base_ptr - */ - T *base_ptr() - { - return list; - } - - /* - Function: base_ptr - */ - const T *base_ptr() const - { - return list; - } - - /* - Function: set_size - Resizes the array to the specified size. - - Arguments: - new_size - The new size for the array. - */ - void set_size(int new_size) - { - alloc(new_size); - num_elements = new_size; - } - - /* - Function: hint_size - Allocates the number of elements wanted but - does not increase the list size. - - Arguments: - hint - Size to allocate. - - Remarks: - - If the hint is smaller then the number of elements, nothing will be done. - - Invalidates ranges - */ - void hint_size(int hint) - { - if(num_elements < hint) - alloc(hint); - } - - - /* - Function: optimize - Removes unnessasary data, returns how many bytes was earned. - - Remarks: - - Invalidates ranges - */ - int optimize() - { - int before = memusage(); - alloc(num_elements); - return before - memusage(); - } - - /* - Function: memusage - Returns how much memory this dynamic array is using - */ - int memusage() - { - return sizeof(*this) + sizeof(T)*list_size; - } - - /* - Function: operator=(array) - - Remarks: - - Invalidates ranges - */ - array &operator = (const array &other) - { - set_size(other.size()); - for(int i = 0; i < size(); i++) - (*this)[i] = other[i]; - return *this; - } - - /* - Function: all - Returns a range that contains the whole array. - */ - range all() { return range(list, list+num_elements); } -protected: - - void incsize() - { - if(num_elements == list_size) - { - if(list_size < 2) - alloc(list_size+1); - else - alloc(list_size+list_size/2); - } - } - - void alloc(int new_len) - { - list_size = new_len; - T *new_list = ALLOCATOR::alloc_array(list_size); - - int end = num_elements < list_size ? num_elements : list_size; - for(int i = 0; i < end; i++) - new_list[i] = list[i]; - - ALLOCATOR::free_array(list); - - num_elements = num_elements < list_size ? num_elements : list_size; - list = new_list; - } - - T *list; - int list_size; - int num_elements; -}; - -#endif // TL_FILE_ARRAY_HPP diff --git a/src/base/tl/base.h b/src/base/tl/base.h new file mode 100644 index 00000000..c202de79 --- /dev/null +++ b/src/base/tl/base.h @@ -0,0 +1,19 @@ +#ifndef TL_FILE_BASE_HPP +#define TL_FILE_BASE_HPP + +#include + +inline void assert(bool statement) +{ + dbg_assert(statement, "assert!"); +} + +template +inline void swap(T &a, T &b) +{ + T c = b; + b = a; + a = c; +} + +#endif diff --git a/src/base/tl/base.hpp b/src/base/tl/base.hpp deleted file mode 100644 index c202de79..00000000 --- a/src/base/tl/base.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef TL_FILE_BASE_HPP -#define TL_FILE_BASE_HPP - -#include - -inline void assert(bool statement) -{ - dbg_assert(statement, "assert!"); -} - -template -inline void swap(T &a, T &b) -{ - T c = b; - b = a; - a = c; -} - -#endif diff --git a/src/base/tl/math.hpp b/src/base/tl/math.hpp deleted file mode 100644 index b323dea8..00000000 --- a/src/base/tl/math.hpp +++ /dev/null @@ -1,45 +0,0 @@ -/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#ifndef BASE_MATH_H -#define BASE_MATH_H - -#include - -template -inline T clamp(T val, T min, T max) -{ - if(val < min) - return min; - if(val > max) - return max; - return val; -} - -inline float sign(float f) -{ - return f<0.0f?-1.0f:1.0f; -} - -inline int round(float f) -{ - if(f > 0) - return (int)(f+0.5f); - return (int)(f-0.5f); -} - -template -inline T mix(const T a, const T b, TB amount) -{ - return a + (b-a)*amount; -} - -inline float frandom() { return rand()/(float)(RAND_MAX); } - -const float pi = 3.1415926535897932384626433f; - -template inline T min(T a, T b) { return a inline T max(T a, T b) { return a>b?a:b; } - -template inline T min(T a, T b, T c) { return min(min(a,b),c); } -template inline T max(T a, T b, T c) { return max(max(a,b),c); } - -#endif // BASE_MATH_H diff --git a/src/base/tl/matrix.hpp b/src/base/tl/matrix.hpp deleted file mode 100644 index b723eaa7..00000000 --- a/src/base/tl/matrix.hpp +++ /dev/null @@ -1,163 +0,0 @@ -#ifndef TL_FILE_MATRIX_HPP -#define TL_FILE_MATRIX_HPP - -/* - - Looks like OpenGL - - Column Major - - / m[0][0] m[1][0] m[2][0] m[3][0] \ / v[0] \ - | | | | - | m[0][1] m[1][1] m[2][1] m[3][1] | | v[1] | - | | | | - M(v) = | m[0][2] m[1][2] m[2][2] m[3][2] | X | v[2] | - | | | | - \ m[0][3] m[1][3] m[2][3] m[3][3] / \ v[3] / - - v[0] = x - v[1] = y - v[2] = z - v[3] = w or 1 - - +y - | - | - |_____ +x - / - / - +z - - right = +x - up = +y - forward = -z - -*/ - -template -class matrix4_base -{ -public: - // [col][row] - T m[4][4]; - - // - inline matrix4_base() - {} - - inline vector3_base get_column3(const int i) const { return vector3_base(m[i][0], m[i][1], m[i][2]); } - inline vector3_base get_column4(const int i) const { return vector4_base(m[i][0], m[i][1], m[i][2], m[i][3]); } - inline vector3_base get_row3(const int i) const { return vector3_base(m[0][i], m[1][i], m[2][i]); } - inline vector4_base get_row4(const int i) const { return vector4_base(m[0][i], m[1][i], m[2][i], m[3][i]); } - - inline vector3_base get_right() const { return get_row3(0); } - inline vector3_base get_up() const { return get_row3(1); } - inline vector3_base get_forward() const { return -get_row3(2); } - inline vector3_base get_translation() const { return get_column3(3); } - - // - void unit() - { - m[0][1] = m[0][2] = m[0][3] = 0; - m[1][0] = m[1][2] = m[1][3] = 0; - m[2][0] = m[2][1] = m[2][3] = 0; - m[3][0] = m[3][1] = m[3][2] = 0; - - m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1; - } - - // - vector3_base operator *(const vector3_base &v) const - { - vector3_base r(0,0,0); - - r.x += v.x*m[0][0] + v.y*m[1][0] + v.z*m[2][0] + m[3][0]; - r.y += v.x*m[0][1] + v.y*m[1][1] + v.z*m[2][1] + m[3][1]; - r.z += v.x*m[0][2] + v.y*m[1][2] + v.z*m[2][2] + m[3][2]; - return r; - } - - // - vector4_base operator *(const vector4_base &v) const - { - vector4_base r(0,0,0,0); - - r.x += v.x*m[0][0] + v.y*m[1][0] + v.z*m[2][0] + v.w*m[3][0]; - r.y += v.x*m[0][1] + v.y*m[1][1] + v.z*m[2][1] + v.w*m[3][1]; - r.z += v.x*m[0][2] + v.y*m[1][2] + v.z*m[2][2] + v.w*m[3][2]; - r.w += v.x*m[0][3] + v.y*m[1][3] + v.z*m[2][3] + v.w*m[3][3]; - return r; - } - // - matrix4_base operator *(const matrix4_base &other) const - { - matrix4_base r; - - for(int i = 0; i < 4; i++) - for(int j = 0; j < 4; j++) - { - r.m[i][j] = 0; - for(int a = 0; a < 4; a++) - r.m[i][j] += m[a][j] * other.m[i][a]; - } - return r; - } - - - // - // THIS PART IS KINDA UGLY BECAUSE MAT4 IS NOT IMMUTABLE - // - - inline void set_row(const vector3_base& v, const int row) - { - m[0][row] = v.x; - m[1][row] = v.y; - m[2][row] = v.z; - } - - inline void set_column(const vector3_base& v, const int col) - { - m[col][0] = v.x; - m[col][1] = v.y; - m[col][2] = v.z; - } - - inline void set_translation(const vector3_base& v) { set_column(v,3); } - - // - void rot_x(T angle) - { - T sina = (T)sin(angle); - T cosa = (T)cos(angle); - - unit(); - m[1][1] = cosa; m[2][1] =-sina; - m[1][2] = sina; m[2][2] = cosa; - } - - // - void rot_y(T angle) - { - T sina = (T)sin(-angle); - T cosa = (T)cos(-angle); - - unit(); - m[0][0] = cosa; m[2][0] =-sina; - m[0][2] = sina; m[2][2] = cosa; - } - - // - void rot_z(T angle) - { - T sina = (T)sin(angle); - T cosa = (T)cos(angle); - - unit(); - m[0][0] = cosa; m[1][0] =-sina; - m[0][1] = sina; m[1][1] = cosa; - } -}; - -typedef matrix4_base mat4; - -#endif diff --git a/src/base/tl/quat.hpp b/src/base/tl/quat.hpp deleted file mode 100644 index 2f381e11..00000000 --- a/src/base/tl/quat.hpp +++ /dev/null @@ -1,211 +0,0 @@ - - -template -class quaternion_base -{ -public: - T k[4]; - - /*void Unit() - { - k[0] = 0; - k[1] = 0; - k[2] = 0; - k[3] = 1; - }*/ - - inline quaternion_base(){} - - inline quaternion_base(T x, T y, T z, T w) - { - k[0] = x; - k[1] = y; - k[2] = z; - k[3] = w; - } - - inline quaternion_base(vector3_base axis, T angle) - { - T sin_angle = sin(angle * (T)0.5); - T cos_angle = cos(angle * (T)0.5); - k[0] = axis.x * sin_angle; - k[1] = axis.y * sin_angle; - k[2] = axis.z * sin_angle; - k[3] = cos_angle; - } - - - T magnitude() - { - return sqrt(k[0] * k[0] + k[1] * k[1] + k[2] * k[2] + k[3] * k[3]); - } - - matrix4_base create_matrix() const - { - matrix4_base mat; - - T xx = k[0] * k[0]; - T xy = k[0] * k[1]; - T xz = k[0] * k[2]; - T xw = k[0] * k[3]; - T yy = k[1] * k[1]; - T yz = k[1] * k[2]; - T yw = k[1] * k[3]; - T zz = k[2] * k[2]; - T zw = k[2] * k[3]; - - mat.k[0][0] = 1 - 2 * (yy + zz); - mat.k[0][1] = 2 * (xy + zw); - mat.k[0][2] = 2 * (xz - yw); - mat.k[0][3] = 0; - - mat.k[1][0] = 2 * (xy - zw); - mat.k[1][1] = 1 - 2 * (xx + zz); - mat.k[1][2] = 2 * (yz + xw); - mat.k[1][3] = 0; - - mat.k[2][0] = 2 * (xz + yw); - mat.k[2][1] = 2 * (yz - xw); - mat.k[2][2] = 1 - 2 * (xx + yy); - mat.k[2][3] = 0; - - mat.k[3][0] = 0; - mat.k[3][1] = 0; - mat.k[3][2] = 0; - mat.k[3][3] = 1; - } - - /* - void CreateDOOM(T x, T y, T z) - { - k[0] = x; - k[1] = y; - k[2] = z; - T Term = 1 - (x * x) - (y * y) - (z * z); - if (Term < 0) - k[3] = 0; - else - k[3] = -sqrt(Term); - - Normalize(); - } - - T DotProd(const TQuaternion& Quat) const - { - return (k[0] * Quat.k[0] + k[1] * Quat.k[1] + k[2] * Quat.k[2] + k[3] * Quat.k[3]); - } - - void Interpolate(const TQuaternion& Quat, TQuaternion& Dest, T Scale) - { - T Separation = k[0] * Quat.k[0] + k[1] * Quat.k[1] + k[2] * Quat.k[2] + k[3] * Quat.k[3]; - T Factor1,Factor2; - - if (Separation > 1) - Separation = 1; - if (Separation < -1) - Separation = -1; - Separation = acos(Separation); - if (Separation == 0 || Separation == pi) - { - Factor1 = 1; - Factor2 = 0; - } - else - { - Factor1 = sin((1 - Scale)*Separation) / sin(Separation); - Factor2 = sin(Scale * Separation) / sin(Separation); - } - - Dest.k[0] = k[0] * Factor1 + Quat.k[0] * Factor2; - Dest.k[1] = k[1] * Factor1 + Quat.k[1] * Factor2; - Dest.k[2] = k[2] * Factor1 + Quat.k[2] * Factor2; - Dest.k[3] = k[3] * Factor1 + Quat.k[3] * Factor2; - } - - void Slerp(const TQuaternion& Quat, TQuaternion& Dest, T Scale) const - { - T Sq1,Sq2; - T Dot = DotProd(Quat); - - TQuaternion Temp; - - if (Dot < 0.0f) - { - Dot = -Dot; - Temp.k[0] = -Quat.k[0]; - Temp.k[1] = -Quat.k[1]; - Temp.k[2] = -Quat.k[2]; - Temp.k[3] = -Quat.k[3]; - } - else - { - Temp = Quat; - } - - if ((1.0 + Dot) > 0.00001) - { - if ((1.0 - Dot) > 0.00001) - { - T om = (T)acos(Dot); - T rsinom = (T)(1.0f / sin(om)); - Sq1 = (T)sin(((T)1.0 - Scale) * om) * rsinom; - Sq2 = (T)sin(Scale * om) * rsinom; - } - else - { - Sq1 = (T)(1.0 - Scale); - Sq2 = Scale; - } - Dest.k[0] = Sq1 * k[0] + Sq2 * Temp[0]; - Dest.k[1] = Sq1 * k[1] + Sq2 * Temp[1]; - Dest.k[2] = Sq1 * k[2] + Sq2 * Temp[2]; - Dest.k[3] = Sq1 * k[3] + Sq2 * Temp[3]; - } - else - { - Sq1 = (T)sin(((T)1.0 - Scale) * (T)0.5 * pi); - Sq2 = (T)sin(Scale * (T)0.5 * pi); - - Dest.k[0] = Sq1 * k[0] + Sq2 * Temp[1]; - Dest.k[1] = Sq1 * k[1] + Sq2 * Temp[0]; - Dest.k[2] = Sq1 * k[2] + Sq2 * Temp[3]; - Dest.k[3] = Sq1 * k[3] + Sq2 * Temp[2]; - } - }*/ - - // perators - T& operator [] (int i) { return k[i]; } - - // quaternion multiply - quaternion_base operator *(const quaternion_base& other) const - { - // (w1 dot w2 - v1 dot v2, w1 dot v2 + w2 dot v1 + v1 cross v2) - quaternion_base r; - r.k[0] = k[3] * other.k[0] + k[0] * other.k[3] + k[1] * other.k[2] - k[2] * other.k[1]; - r.k[1] = k[3] * other.k[1] + k[1] * other.k[3] + k[2] * other.k[0] - k[0] * other.k[2]; - r.k[2] = k[3] * other.k[2] + k[2] * other.k[3] + k[0] * other.k[1] - k[1] * other.k[0]; - r.k[3] = k[3] * other.k[3] - k[0] * other.k[0] - k[1] * other.k[1] - k[2] * other.k[2]; - - return normalize(r); - } - - /* - bool operator == (const quaternion_base& t) const { return ((k[0] == t.k[0]) && (k[1] == t.k[1]) && (k[2] == t.k[2]) && (k[3] == t.k[3])); } - bool operator != (const quaternion_base& t) const { return ((k[0] != t.k[0]) || (k[1] != t.k[1]) || (k[2] != t.k[2]) || (k[3] != t.k[3])); } - - void operator = (const quaternion_base& other) { k[0] = other.k[0]; k[1] = other.k[1]; k[2] = other.k[2]; k[3] = other.k[3]; } - */ - - /*void operator *= (const TQuaternion& t) { TQuaternion Temp = Multiply(t); *this = Temp; } - TQuaternion operator * (const TQuaternion& t) const { return Multiply(t); } - */ -}; - -template -inline quaternion_base normalize(const quaternion_base &v) -{ - T factor = 1.0f/v.magnitude(); - return quaternion_base(v.k[0]*factor, v.k[1]*factor,v. k[2]*factor,v. k[3]*factor); -} - - diff --git a/src/base/tl/range.h b/src/base/tl/range.h new file mode 100644 index 00000000..1c63e73c --- /dev/null +++ b/src/base/tl/range.h @@ -0,0 +1,232 @@ +#ifndef TL_FILE_RANGE_HPP +#define TL_FILE_RANGE_HPP + +#include "base.h" + +/* + Group: Range concepts +*/ + +/* + Concept: concept_empty + + template + struct range + { + bool empty() const; + }; +*/ +struct concept_empty +{ + template static void check(T &t) { if(0) t.empty(); }; +}; + +/* + Concept: concept_index + + template + struct range + { + T &index(size_t); + }; +*/ +struct concept_index +{ + template static void check(T &t) { if(0) t.index(0); }; +}; + +/* + Concept: concept_size + + template + struct range + { + size_t size(); + }; +*/ +struct concept_size +{ + template static void check(T &t) { if(0) t.size(); }; +}; + +/* + Concept: concept_slice + + template + struct range + { + range slice(size_t start, size_t count); + }; +*/ +struct concept_slice +{ + template static void check(T &t) { if(0) t.slice(0, 0); }; +}; + +/* + Concept: concept_sorted + + template + struct range + { + void sorted(); + }; +*/ +struct concept_sorted +{ + template static void check(T &t) { if(0) t.sorted(); }; +}; + +/* + Concept: concept_forwarditeration + Checks for the front and pop_front methods + + template + struct range + { + void pop_front(); + T &front() const; + }; +*/ +struct concept_forwarditeration +{ + template static void check(T &t) { if(0) { t.front(); t.pop_front(); } }; +}; + +/* + Concept: concept_backwarditeration + Checks for the back and pop_back methods + + template + struct range + { + void pop_back(); + T &back() const; + }; +*/ +struct concept_backwarditeration +{ + template static void check(T &t) { if(0) { t.back(); t.pop_back(); } }; +}; + + +/* + Group: Range classes +*/ + + +/* + Class: plain_range + + Concepts: + + + + + +*/ +template +class plain_range +{ +public: + typedef T type; + plain_range() + { + begin = 0x0; + end = 0x0; + } + + plain_range(const plain_range &r) + { + *this = r; + } + + plain_range(T *b, T *e) + { + begin = b; + end = e; + } + + bool empty() const { return begin >= end; } + void pop_front() { assert(!empty()); begin++; } + void pop_back() { assert(!empty()); end--; } + T& front() { assert(!empty()); return *begin; } + T& back() { assert(!empty()); return *(end-1); } + T& index(unsigned i) { assert(i >= 0 && i < (unsigned)(end-begin)); return begin[i]; } + unsigned size() const { return (unsigned)(end-begin); } + plain_range slice(unsigned startindex, unsigned endindex) + { + return plain_range(begin+startindex, begin+endindex); + } + +protected: + T *begin; + T *end; +}; + +/* + Class: plain_range_sorted + + Concepts: + Same as but with these additions: + +*/ +template +class plain_range_sorted : public plain_range +{ + typedef plain_range parent; +public: + /* sorted concept */ + void sorted() const { } + + plain_range_sorted() + {} + + plain_range_sorted(const plain_range_sorted &r) + { + *this = r; + } + + plain_range_sorted(T *b, T *e) + : parent(b, e) + {} + + plain_range_sorted slice(unsigned start, unsigned count) + { + return plain_range_sorted(parent::begin+start, parent::begin+start+count); + } +}; + +template +class reverse_range +{ +private: + reverse_range() {} +public: + typedef typename R::type type; + + reverse_range(R r) + { + range = r; + } + + reverse_range(const reverse_range &other) { range = other.range; } + + + bool empty() const { return range.empty(); } + void pop_front() { range.pop_back(); } + void pop_back() { range.pop_front(); } + type& front() { return range.back(); } + type& back() { return range.front(); } + + R range; +}; + +template reverse_range reverse(R range) { + return reverse_range(range); +} +template R reverse(reverse_range range) { + return range.range; +} + +#endif // TL_FILE_RANGE_HPP diff --git a/src/base/tl/range.hpp b/src/base/tl/range.hpp deleted file mode 100644 index 559daffa..00000000 --- a/src/base/tl/range.hpp +++ /dev/null @@ -1,232 +0,0 @@ -#ifndef TL_FILE_RANGE_HPP -#define TL_FILE_RANGE_HPP - -#include "base.hpp" - -/* - Group: Range concepts -*/ - -/* - Concept: concept_empty - - template - struct range - { - bool empty() const; - }; -*/ -struct concept_empty -{ - template static void check(T &t) { if(0) t.empty(); }; -}; - -/* - Concept: concept_index - - template - struct range - { - T &index(size_t); - }; -*/ -struct concept_index -{ - template static void check(T &t) { if(0) t.index(0); }; -}; - -/* - Concept: concept_size - - template - struct range - { - size_t size(); - }; -*/ -struct concept_size -{ - template static void check(T &t) { if(0) t.size(); }; -}; - -/* - Concept: concept_slice - - template - struct range - { - range slice(size_t start, size_t count); - }; -*/ -struct concept_slice -{ - template static void check(T &t) { if(0) t.slice(0, 0); }; -}; - -/* - Concept: concept_sorted - - template - struct range - { - void sorted(); - }; -*/ -struct concept_sorted -{ - template static void check(T &t) { if(0) t.sorted(); }; -}; - -/* - Concept: concept_forwarditeration - Checks for the front and pop_front methods - - template - struct range - { - void pop_front(); - T &front() const; - }; -*/ -struct concept_forwarditeration -{ - template static void check(T &t) { if(0) { t.front(); t.pop_front(); } }; -}; - -/* - Concept: concept_backwarditeration - Checks for the back and pop_back methods - - template - struct range - { - void pop_back(); - T &back() const; - }; -*/ -struct concept_backwarditeration -{ - template static void check(T &t) { if(0) { t.back(); t.pop_back(); } }; -}; - - -/* - Group: Range classes -*/ - - -/* - Class: plain_range - - Concepts: - - - - - -*/ -template -class plain_range -{ -public: - typedef T type; - plain_range() - { - begin = 0x0; - end = 0x0; - } - - plain_range(const plain_range &r) - { - *this = r; - } - - plain_range(T *b, T *e) - { - begin = b; - end = e; - } - - bool empty() const { return begin >= end; } - void pop_front() { assert(!empty()); begin++; } - void pop_back() { assert(!empty()); end--; } - T& front() { assert(!empty()); return *begin; } - T& back() { assert(!empty()); return *(end-1); } - T& index(unsigned i) { assert(i >= 0 && i < (unsigned)(end-begin)); return begin[i]; } - unsigned size() const { return (unsigned)(end-begin); } - plain_range slice(unsigned startindex, unsigned endindex) - { - return plain_range(begin+startindex, begin+endindex); - } - -protected: - T *begin; - T *end; -}; - -/* - Class: plain_range_sorted - - Concepts: - Same as but with these additions: - -*/ -template -class plain_range_sorted : public plain_range -{ - typedef plain_range parent; -public: - /* sorted concept */ - void sorted() const { } - - plain_range_sorted() - {} - - plain_range_sorted(const plain_range_sorted &r) - { - *this = r; - } - - plain_range_sorted(T *b, T *e) - : parent(b, e) - {} - - plain_range_sorted slice(unsigned start, unsigned count) - { - return plain_range_sorted(parent::begin+start, parent::begin+start+count); - } -}; - -template -class reverse_range -{ -private: - reverse_range() {} -public: - typedef typename R::type type; - - reverse_range(R r) - { - range = r; - } - - reverse_range(const reverse_range &other) { range = other.range; } - - - bool empty() const { return range.empty(); } - void pop_front() { range.pop_back(); } - void pop_back() { range.pop_front(); } - type& front() { return range.back(); } - type& back() { return range.front(); } - - R range; -}; - -template reverse_range reverse(R range) { - return reverse_range(range); -} -template R reverse(reverse_range range) { - return range.range; -} - -#endif // TL_FILE_RANGE_HPP diff --git a/src/base/tl/sorted_array.h b/src/base/tl/sorted_array.h new file mode 100644 index 00000000..95f06157 --- /dev/null +++ b/src/base/tl/sorted_array.h @@ -0,0 +1,31 @@ +#ifndef TL_FILE_SORTED_ARRAY_HPP +#define TL_FILE_SORTED_ARRAY_HPP + +#include "algorithm.h" +#include "array.h" + +template > +class sorted_array : public array +{ + typedef array parent; + + // insert and size is not allowed + int insert(const T& item, typename parent::range r) { dbg_break(); return 0; } + int set_size(int new_size) { dbg_break(); return 0; } + +public: + typedef plain_range_sorted range; + + int add(const T& item) + { + return parent::insert(item, partition_binary(all(), item)); + } + + /* + Function: all + Returns a sorted range that contains the whole array. + */ + range all() { return range(parent::list, parent::list+parent::num_elements); } +}; + +#endif // TL_FILE_SORTED_ARRAY_HPP diff --git a/src/base/tl/sorted_array.hpp b/src/base/tl/sorted_array.hpp deleted file mode 100644 index 30c1df24..00000000 --- a/src/base/tl/sorted_array.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef TL_FILE_SORTED_ARRAY_HPP -#define TL_FILE_SORTED_ARRAY_HPP - -#include "algorithm.hpp" -#include "array.hpp" - -template > -class sorted_array : public array -{ - typedef array parent; - - // insert and size is not allowed - int insert(const T& item, typename parent::range r) { dbg_break(); return 0; } - int set_size(int new_size) { dbg_break(); return 0; } - -public: - typedef plain_range_sorted range; - - int add(const T& item) - { - return parent::insert(item, partition_binary(all(), item)); - } - - /* - Function: all - Returns a sorted range that contains the whole array. - */ - range all() { return range(parent::list, parent::list+parent::num_elements); } -}; - -#endif // TL_FILE_SORTED_ARRAY_HPP diff --git a/src/base/tl/stream.hpp b/src/base/tl/stream.hpp deleted file mode 100644 index c307b968..00000000 --- a/src/base/tl/stream.hpp +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef TL_FILE_STREAM_HPP -#define TL_FILE_STREAM_HPP - -class input_stream -{ -public: - virtual ~input_stream() {} - virtual size_t read(void *data, size_t size) = 0; - virtual size_t size() = 0; -}; - -class output_stream -{ -public: - virtual ~output_stream() {} - virtual size_t write(const void *data, size_t size) = 0; -}; - - -// input wrapping -// RAII style -class file_backend -{ -private: - file_backend(const file_backend &other) { /* no copy allowed */ } -protected: - IOHANDLE file_handle; - - explicit file_backend(const char *filename, int flags) - { - file_handle = io_open(filename, flags); - } - - ~file_backend() - { - if(file_handle) - io_close(file_handle); - } -public: - bool is_open() const { return file_handle != 0; } -}; - -class file_reader : public input_stream, public file_backend -{ -public: - explicit file_reader(const char *filename) - : file_backend(filename, IOFLAG_READ) - {} - - virtual size_t read(void *data, size_t size) { return io_read(file_handle, data, size); } - virtual size_t size() { return io_length(file_handle); } -}; - - -class file_writer : public output_stream, public file_backend -{ -public: - explicit file_writer(const char *filename) - : file_backend(filename, IOFLAG_WRITE) - {} - - virtual size_t write(const void *data, size_t size) { return io_write(file_handle, data, size); } -}; - -#endif diff --git a/src/base/tl/string.h b/src/base/tl/string.h new file mode 100644 index 00000000..155ca2a4 --- /dev/null +++ b/src/base/tl/string.h @@ -0,0 +1,68 @@ +#ifndef TL_FILE_STRING_HPP +#define TL_FILE_STRING_HPP + +#include "base.h" +#include "allocator.h" + +template +class string_base : private ALLOCATOR +{ + char *str; + int length; + + void reset() + { + str = 0; length = 0; + } + + void free() + { + ALLOCATOR::free_array(str); + reset(); + } + + void copy(const char *other_str, int other_length) + { + length = other_length; + str = ALLOCATOR::alloc_array(length+1); + mem_copy(str, other_str, length+1); + } + + void copy(const string_base &other) + { + if(!other.str) + return; + copy(other.str, other.length); + } + +public: + string_base() { reset(); } + string_base(const char *other_str) { copy(other_str, str_length(other_str)); } + string_base(const string_base &other) { reset(); copy(other); } + ~string_base() { free(); } + + string_base &operator = (const char *other) + { + free(); + if(other) + copy(other, str_length(other)); + return *this; + } + + string_base &operator = (const string_base &other) + { + free(); + copy(other); + return *this; + } + + bool operator < (const char *other_str) const { return str_comp(str, other_str) < 0; } + operator const char *() const { return str; } + + const char *cstr() const { return str; } +}; + +/* normal allocated string */ +typedef string_base > string; + +#endif // TL_FILE_STRING_HPP diff --git a/src/base/tl/string.hpp b/src/base/tl/string.hpp deleted file mode 100644 index 2b164091..00000000 --- a/src/base/tl/string.hpp +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef TL_FILE_STRING_HPP -#define TL_FILE_STRING_HPP - -#include "base.hpp" -#include "allocator.hpp" - -template -class string_base : private ALLOCATOR -{ - char *str; - int length; - - void reset() - { - str = 0; length = 0; - } - - void free() - { - ALLOCATOR::free_array(str); - reset(); - } - - void copy(const char *other_str, int other_length) - { - length = other_length; - str = ALLOCATOR::alloc_array(length+1); - mem_copy(str, other_str, length+1); - } - - void copy(const string_base &other) - { - if(!other.str) - return; - copy(other.str, other.length); - } - -public: - string_base() { reset(); } - string_base(const char *other_str) { copy(other_str, str_length(other_str)); } - string_base(const string_base &other) { reset(); copy(other); } - ~string_base() { free(); } - - string_base &operator = (const char *other) - { - free(); - if(other) - copy(other, str_length(other)); - return *this; - } - - string_base &operator = (const string_base &other) - { - free(); - copy(other); - return *this; - } - - bool operator < (const char *other_str) const { return str_comp(str, other_str) < 0; } - operator const char *() const { return str; } - - const char *cstr() const { return str; } -}; - -/* normal allocated string */ -typedef string_base > string; - -#endif // TL_FILE_STRING_HPP diff --git a/src/base/tl/vector.hpp b/src/base/tl/vector.hpp deleted file mode 100644 index 1c1bb804..00000000 --- a/src/base/tl/vector.hpp +++ /dev/null @@ -1,198 +0,0 @@ -/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#ifndef TL_FILE_VECTOR_HPP -#define TL_FILE_VECTOR_HPP - -#include - -// ------------------------------------ - -template -class vector2_base -{ -public: - union { T x,u; }; - union { T y,v; }; - - vector2_base() {} - vector2_base(float nx, float ny) - { - x = nx; - y = ny; - } - - vector2_base operator -() const { return vector2_base(-x, -y); } - vector2_base operator -(const vector2_base &v) const { return vector2_base(x-v.x, y-v.y); } - vector2_base operator +(const vector2_base &v) const { return vector2_base(x+v.x, y+v.y); } - vector2_base operator *(const T v) const { return vector2_base(x*v, y*v); } - - const vector2_base &operator =(const vector2_base &v) { x = v.x; y = v.y; return *this; } - - const vector2_base &operator +=(const vector2_base &v) { x += v.x; y += v.y; return *this; } - const vector2_base &operator -=(const vector2_base &v) { x -= v.x; y -= v.y; return *this; } - const vector2_base &operator *=(const T v) { x *= v; y *= v; return *this; } - - bool operator ==(const vector2_base &v) const { return x == v.x && y == v.y; } //TODO: do this with an eps instead - - operator const T* () { return &x; } -}; - - -template -inline T length(const vector2_base &a) -{ - return sqrtf(a.x*a.x + a.y*a.y); -} - -template -inline T distance(const vector2_base a, const vector2_base &b) -{ - return length(a-b); -} - -template -inline T dot(const vector2_base a, const vector2_base &b) -{ - return a.x*b.x + a.y*b.y; -} - -template -inline vector2_base normalize(const vector2_base &v) -{ - T l = (T)(1.0f/sqrtf(v.x*v.x + v.y*v.y)); - return vector2_base(v.x*l, v.y*l); -} - -typedef vector2_base vec2; -typedef vector2_base bvec2; -typedef vector2_base ivec2; - -template -inline vector2_base closest_point_on_line(vector2_base line_point0, vector2_base line_point1, vector2_base target_point) -{ - vector2_base c = target_point - line_point0; - vector2_base v = (line_point1 - line_point0); - v = normalize(v); - T d = length(line_point0-line_point1); - T t = dot(v, c)/d; - return mix(line_point0, line_point1, clamp(t, (T)0, (T)1)); - /* - if (t < 0) t = 0; - if (t > 1.0f) return 1.0f; - return t;*/ -} - -// ------------------------------------ -template -class vector3_base -{ -public: - union { T x,r,h; }; - union { T y,g,s; }; - union { T z,b,v,l; }; - - vector3_base() {} - vector3_base(float nx, float ny, float nz) - { - x = nx; - y = ny; - z = nz; - } - - const vector3_base &operator =(const vector3_base &v) { x = v.x; y = v.y; z = v.z; return *this; } - - vector3_base operator -(const vector3_base &v) const { return vector3_base(x-v.x, y-v.y, z-v.z); } - vector3_base operator -() const { return vector3_base(-x, -y, -z); } - vector3_base operator +(const vector3_base &v) const { return vector3_base(x+v.x, y+v.y, z+v.z); } - vector3_base operator *(const T v) const { return vector3_base(x*v, y*v, z*v); } - vector3_base operator *(const vector3_base &v) const { return vector3_base(x*v.x, y*v.y, z*v.z); } - vector3_base operator /(const T v) const { return vector3_base(x/v, y/v, z/v); } - - const vector3_base &operator +=(const vector3_base &v) { x += v.x; y += v.y; z += v.z; return *this; } - const vector3_base &operator -=(const vector3_base &v) { x -= v.x; y -= v.y; z -= v.z; return *this; } - const vector3_base &operator *=(const T v) { x *= v; y *= v; z *= v; return *this; } - - bool operator ==(const vector3_base &v) const { return x == v.x && y == v.y && z == v.z; } //TODO: do this with an eps instead - - operator const T* () { return &x; } -}; - -template -inline T length(const vector3_base &a) -{ - return sqrtf(a.x*a.x + a.y*a.y + a.z*a.z); -} - -template -inline T distance(const vector3_base &a, const vector3_base &b) -{ - return length(a-b); -} - -template -inline T dot(const vector3_base &a, const vector3_base &b) -{ - return a.x*b.x + a.y*b.y + a.z*b.z; -} - -template -inline vector3_base normalize(const vector3_base &v) -{ - T l = (T)(1.0f/sqrtf(v.x*v.x + v.y*v.y + v.z*v.z)); - return vector3_base(v.x*l, v.y*l, v.z*l); -} - -template -inline vector3_base cross(const vector3_base &a, const vector3_base &b) -{ - return vector3_base( - a.y*b.z - a.z*b.y, - a.z*b.x - a.x*b.z, - a.x*b.y - a.y*b.x); -} - -typedef vector3_base vec3; -typedef vector3_base bvec3; -typedef vector3_base ivec3; - -// ------------------------------------ - -template -class vector4_base -{ -public: - union { T x,r; }; - union { T y,g; }; - union { T z,b; }; - union { T w,a; }; - - vector4_base() {} - vector4_base(float nx, float ny, float nz, float nw) - { - x = nx; - y = ny; - z = nz; - w = nw; - } - - vector4_base operator +(const vector4_base &v) const { return vector4_base(x+v.x, y+v.y, z+v.z, w+v.w); } - vector4_base operator -(const vector4_base &v) const { return vector4_base(x-v.x, y-v.y, z-v.z, w-v.w); } - vector4_base operator -() const { return vector4_base(-x, -y, -z, -w); } - vector4_base operator *(const vector4_base &v) const { return vector4_base(x*v.x, y*v.y, z*v.z, w*v.w); } - vector4_base operator *(const T v) const { return vector4_base(x*v, y*v, z*v, w*v); } - - const vector4_base &operator =(const vector4_base &v) { x = v.x; y = v.y; z = v.z; w = v.w; return *this; } - - const vector4_base &operator +=(const vector4_base &v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; } - const vector4_base &operator -=(const vector4_base &v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; } - const vector4_base &operator *=(const T v) { x *= v; y *= v; z *= v; w *= v; return *this; } - - bool operator ==(const vector4_base &v) const { return x == v.x && y == v.y && z == v.z && w == v.w; } //TODO: do this with an eps instead - - operator const T* () { return &x; } -}; - -typedef vector4_base vec4; -typedef vector4_base bvec4; -typedef vector4_base ivec4; - -#endif -- cgit 1.4.1