// Filename: lvecBase3.I // Created by: drose (08Mar00) // //////////////////////////////////////////////////////////////////// #include "nearly_zero.h" #include #include #include template TypeHandle LVecBase3::_type_handle; //////////////////////////////////////////////////////////////////// // Function: LVecBase3::Default Constructor // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3:: LVecBase3() { } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::Copy Constructor // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3:: LVecBase3(const LVecBase3 ©) { (*this) = copy; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::Copy Assignment Operator // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 &LVecBase3:: operator = (const LVecBase3 ©) { set(copy[0], copy[1], copy[2]); return *this; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::Fill Assignment Operator // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 &LVecBase3:: operator = (NumType fill_value) { fill(fill_value); return *this; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::Constructor // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3:: LVecBase3(NumType fill_value) { fill(fill_value); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::Constructor // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3:: LVecBase3(NumType x, NumType y, NumType z) { set(x, y, z); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::zero Named Constructor // Access: Public // Description: Returns a zero-length vector. //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 LVecBase3:: zero() { return LVecBase3(0.0, 0.0, 0.0); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::unit_x Named Constructor // Access: Public // Description: Returns a unit X vector. //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 LVecBase3:: unit_x() { return LVecBase3(1.0, 0.0, 0.0); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::unit_y Named Constructor // Access: Public // Description: Returns a unit Y vector. //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 LVecBase3:: unit_y() { return LVecBase3(0.0, 1.0, 0.0); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::unit_z Named Constructor // Access: Public // Description: Returns a unit Z vector. //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 LVecBase3:: unit_z() { return LVecBase3(0.0, 0.0, 1.0); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::Destructor // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3:: ~LVecBase3() { } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::Indexing Operator // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE NumType LVecBase3:: operator [](int i) const { nassertr(i >= 0 && i < 3, 0); return _data[i]; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::Indexing Operator // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE NumType &LVecBase3:: operator [](int i) { nassertr(i >= 0 && i < 3, _data[0]); return _data[i]; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::is_nan // Access: Public // Description: Returns true if any component of the vector is // not-a-number, false otherwise. //////////////////////////////////////////////////////////////////// template INLINE bool LVecBase3:: is_nan() const { return cnan(_data[0]) || cnan(_data[1]) || cnan(_data[2]); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::get_cell // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE NumType LVecBase3:: get_cell(int i) const { nassertr(i >= 0 && i < 3, 0); return _data[i]; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::get_x // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE NumType LVecBase3:: get_x() const { return _data[0]; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::get_y // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE NumType LVecBase3:: get_y() const { return _data[1]; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::get_z // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE NumType LVecBase3:: get_z() const { return _data[2]; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::set_cell // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: set_cell(int i, NumType value) { nassertv(i >= 0 && i < 3); _data[i] = value; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::set_x // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: set_x(NumType value) { _data[0] = value; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::set_y // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: set_y(NumType value) { _data[1] = value; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::set_z // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: set_z(NumType value) { _data[2] = value; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::get_data // Access: Public // Description: Returns the address of the first of the three data // elements in the vector. The remaining elements // occupy the next positions consecutively in memory. //////////////////////////////////////////////////////////////////// template INLINE const NumType *LVecBase3:: get_data() const { return _data; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::get_num_components // Access: Public // Description: Returns the number of elements in the vector, three. //////////////////////////////////////////////////////////////////// template INLINE int LVecBase3:: get_num_components() const { return 3; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::begin // Access: Public // Description: Returns an iterator that may be used to traverse the // elements of the matrix, STL-style. //////////////////////////////////////////////////////////////////// template INLINE LVecBase3::iterator LVecBase3:: begin() { return _data; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::end // Access: Public // Description: Returns an iterator that may be used to traverse the // elements of the matrix, STL-style. //////////////////////////////////////////////////////////////////// template INLINE LVecBase3::iterator LVecBase3:: end() { return begin() + get_num_components(); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::begin // Access: Public // Description: Returns an iterator that may be used to traverse the // elements of the matrix, STL-style. //////////////////////////////////////////////////////////////////// template INLINE LVecBase3::const_iterator LVecBase3:: begin() const { return _data; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::end // Access: Public // Description: Returns an iterator that may be used to traverse the // elements of the matrix, STL-style. //////////////////////////////////////////////////////////////////// template INLINE LVecBase3::const_iterator LVecBase3:: end() const { return begin() + get_num_components(); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::fill // Access: Public // Description: Sets each element of the vector to the indicated // fill_value. This is particularly useful for // initializing to zero. //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: fill(NumType fill_value) { _data[0] = fill_value; _data[1] = fill_value; _data[2] = fill_value; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::set // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: set(NumType x, NumType y, NumType z) { _data[0] = x; _data[1] = y; _data[2] = z; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::dot // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE NumType LVecBase3:: dot(const LVecBase3 &other) const { return _data[0] * other[0] + _data[1] * other[1] + _data[2] * other[2]; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::cross // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 LVecBase3:: cross(const LVecBase3 &other) const { return LVecBase3(_data[1] * other[2] - other[1] * _data[2], other[0] * _data[2] - _data[0] * other[2], _data[0] * other[1] - other[0] * _data[1]); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::operator < // Access: Public // Description: This performs a lexicographical comparison. It's of // questionable mathematical meaning, but sometimes has // a practical purpose for sorting unique vectors, // especially in an STL container. Also see // compare_to(). //////////////////////////////////////////////////////////////////// template INLINE bool LVecBase3:: operator < (const LVecBase3 &other) const { return (compare_to(other) < 0); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::operator == // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE bool LVecBase3:: operator == (const LVecBase3 &other) const { return (_data[0] == other[0] && _data[1] == other[1] && _data[2] == other[2]); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::operator != // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE bool LVecBase3:: operator != (const LVecBase3 &other) const { return !operator == (other); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::compare_to // Access: Public // Description: This flavor of compare_to uses a default threshold // value based on the numeric type. //////////////////////////////////////////////////////////////////// template INLINE int LVecBase3:: compare_to(const LVecBase3 &other) const { return compare_to(other, NEARLY_ZERO(NumType)); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::compare_to // Access: Public // Description: Sorts vectors lexicographically, componentwise. // Returns a number less than 0 if this vector sorts // before the other one, greater than zero if it sorts // after, 0 if they are equivalent (within the indicated // tolerance). //////////////////////////////////////////////////////////////////// template INLINE int LVecBase3:: compare_to(const LVecBase3 &other, NumType threshold) const { if (!IS_THRESHOLD_EQUAL(_data[0], other[0], threshold)) { return (_data[0] < other[0]) ? -1 : 1; } if (!IS_THRESHOLD_EQUAL(_data[1], other[1], threshold)) { return (_data[1] < other[1]) ? -1 : 1; } if (!IS_THRESHOLD_EQUAL(_data[2], other[2], threshold)) { return (_data[2] < other[2]) ? -1 : 1; } return 0; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::unary - // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 LVecBase3:: operator - () const { return LVecBase3(-_data[0], -_data[1], -_data[2]); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::vector + vector // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 LVecBase3:: operator + (const LVecBase3 &other) const { return LVecBase3(_data[0] + other[0], _data[1] + other[1], _data[2] + other[2]); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::vector - vector // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 LVecBase3:: operator - (const LVecBase3 &other) const { return LVecBase3(_data[0] - other[0], _data[1] - other[1], _data[2] - other[2]); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::vector * scalar // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 LVecBase3:: operator * (NumType scalar) const { return LVecBase3(_data[0] * scalar, _data[1] * scalar, _data[2] * scalar); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::vector / scalar // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 LVecBase3:: operator / (NumType scalar) const { return LVecBase3(_data[0] / scalar, _data[1] / scalar, _data[2] / scalar); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::operator += // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: operator += (const LVecBase3 &other) { _data[0] += other[0]; _data[1] += other[1]; _data[2] += other[2]; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::operator -= // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: operator -= (const LVecBase3 &other) { _data[0] -= other[0]; _data[1] -= other[1]; _data[2] -= other[2]; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::operator *= // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: operator *= (NumType scalar) { _data[0] *= scalar; _data[1] *= scalar; _data[2] *= scalar; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::operator /= // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: operator /= (NumType scalar) { _data[0] /= scalar; _data[1] /= scalar; _data[2] /= scalar; } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::cross product (with assigment) // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: cross_into(const LVecBase3 &other) { (*this) = cross(other); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::almost_equal // Access: Public // Description: Returns true if two vectors are memberwise equal // within a specified tolerance. //////////////////////////////////////////////////////////////////// template INLINE bool LVecBase3:: almost_equal(const LVecBase3 &other, NumType threshold) const { return (IS_THRESHOLD_EQUAL(_data[0], other[0], threshold) && IS_THRESHOLD_EQUAL(_data[1], other[1], threshold) && IS_THRESHOLD_EQUAL(_data[2], other[2], threshold)); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::almost_equal // Access: Public // Description: Returns true if two vectors are memberwise equal // within a default tolerance based on the numeric type. //////////////////////////////////////////////////////////////////// template INLINE bool LVecBase3:: almost_equal(const LVecBase3 &other) const { return almost_equal(other, NEARLY_ZERO(NumType)); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::output // Access: Public // Description: //////////////////////////////////////////////////////////////////// template INLINE void LVecBase3:: output(ostream &out) const { out << MAYBE_ZERO(_data[0]) << " " << MAYBE_ZERO(_data[1]) << " " << MAYBE_ZERO(_data[2]); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::init_type // Access: Public, Static // Description: //////////////////////////////////////////////////////////////////// template void LVecBase3:: init_type() { if (_type_handle == TypeHandle::none()) { // Format a string to describe the type. do_init_type(NumType); string name = "LVecBase3<" + get_type_handle(NumType).get_name() + ">"; register_type(_type_handle, name); } } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::write_datagram // Access: Public // Description: Function to write itself into a datagram //////////////////////////////////////////////////////////////////// template void LVecBase3:: write_datagram(Datagram &destination) const { destination.add_float64(_data[0]); destination.add_float64(_data[1]); destination.add_float64(_data[2]); } //////////////////////////////////////////////////////////////////// // Function: LVecBase3::read_datagram // Access: Public // Description: Function to read itself from a datagramIterator //////////////////////////////////////////////////////////////////// template void LVecBase3:: read_datagram(DatagramIterator &source) { _data[0] = source.get_float64(); _data[1] = source.get_float64(); _data[2] = source.get_float64(); } //////////////////////////////////////////////////////////////////// // Function: lcast_to // Description: Converts a vector from one numeric representation to // another one. This is usually invoked using the macro // LCAST. //////////////////////////////////////////////////////////////////// template INLINE LVecBase3 lcast_to(NumType2 *, const LVecBase3 &source) { return LVecBase3(source[0], source[1], source[2]); }