lvecBase4_src.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Filename: lvecBase4_src.h
  2. // Created by: drose (08Mar00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. class FLOATNAME(LVecBase2);
  15. class FLOATNAME(LVecBase3);
  16. ////////////////////////////////////////////////////////////////////
  17. // Class : LVecBase4
  18. // Description : This is the base class for all three-component
  19. // vectors and points.
  20. ////////////////////////////////////////////////////////////////////
  21. class EXPCL_PANDA_LINMATH FLOATNAME(LVecBase4) {
  22. PUBLISHED:
  23. typedef const FLOATTYPE *iterator;
  24. typedef const FLOATTYPE *const_iterator;
  25. INLINE_LINMATH FLOATNAME(LVecBase4)();
  26. INLINE_LINMATH FLOATNAME(LVecBase4)(const FLOATNAME(LVecBase4) &copy);
  27. INLINE_LINMATH FLOATNAME(LVecBase4) &operator = (const FLOATNAME(LVecBase4) &copy);
  28. INLINE_LINMATH FLOATNAME(LVecBase4) &operator = (FLOATTYPE fill_value);
  29. INLINE_LINMATH FLOATNAME(LVecBase4)(FLOATTYPE fill_value);
  30. INLINE_LINMATH FLOATNAME(LVecBase4)(FLOATTYPE x, FLOATTYPE y, FLOATTYPE z, FLOATTYPE w);
  31. ALLOC_DELETED_CHAIN(FLOATNAME(LVecBase4));
  32. INLINE_LINMATH static const FLOATNAME(LVecBase4) &zero();
  33. INLINE_LINMATH static const FLOATNAME(LVecBase4) &unit_x();
  34. INLINE_LINMATH static const FLOATNAME(LVecBase4) &unit_y();
  35. INLINE_LINMATH static const FLOATNAME(LVecBase4) &unit_z();
  36. INLINE_LINMATH static const FLOATNAME(LVecBase4) &unit_w();
  37. INLINE_LINMATH ~FLOATNAME(LVecBase4)();
  38. #ifdef HAVE_PYTHON
  39. PyObject *__reduce__(PyObject *self) const;
  40. PyObject *__getattr__(const string &attr_name) const;
  41. int __setattr__(PyObject *self, const string &attr_name, FLOATTYPE val);
  42. int __setattr__(PyObject *self, const string &attr_name, FLOATNAME(LVecBase2) val);
  43. int __setattr__(PyObject *self, const string &attr_name, FLOATNAME(LVecBase3) val);
  44. int __setattr__(PyObject *self, const string &attr_name, FLOATNAME(LVecBase4) val);
  45. #endif
  46. INLINE_LINMATH FLOATTYPE operator [](int i) const;
  47. INLINE_LINMATH FLOATTYPE &operator [](int i);
  48. #ifdef HAVE_PYTHON
  49. INLINE_LINMATH void __setitem__(int i, FLOATTYPE v);
  50. #endif
  51. INLINE_LINMATH static int size();
  52. INLINE_LINMATH bool is_nan() const;
  53. INLINE_LINMATH FLOATTYPE get_cell(int i) const;
  54. INLINE_LINMATH FLOATTYPE get_x() const;
  55. INLINE_LINMATH FLOATTYPE get_y() const;
  56. INLINE_LINMATH FLOATTYPE get_z() const;
  57. INLINE_LINMATH FLOATTYPE get_w() const;
  58. INLINE_LINMATH void set_cell(int i, FLOATTYPE value);
  59. INLINE_LINMATH void set_x(FLOATTYPE value);
  60. INLINE_LINMATH void set_y(FLOATTYPE value);
  61. INLINE_LINMATH void set_z(FLOATTYPE value);
  62. INLINE_LINMATH void set_w(FLOATTYPE value);
  63. // These next functions add to an existing value.
  64. // i.e. foo.set_x(foo.get_x() + value)
  65. // These are useful to reduce overhead in scripting
  66. // languages:
  67. INLINE_LINMATH void add_to_cell(int i, FLOATTYPE value);
  68. INLINE_LINMATH void add_x(FLOATTYPE value);
  69. INLINE_LINMATH void add_y(FLOATTYPE value);
  70. INLINE_LINMATH void add_z(FLOATTYPE value);
  71. INLINE_LINMATH void add_w(FLOATTYPE value);
  72. INLINE_LINMATH const FLOATTYPE *get_data() const;
  73. INLINE_LINMATH int get_num_components() const;
  74. public:
  75. INLINE_LINMATH iterator begin();
  76. INLINE_LINMATH iterator end();
  77. INLINE_LINMATH const_iterator begin() const;
  78. INLINE_LINMATH const_iterator end() const;
  79. PUBLISHED:
  80. INLINE_LINMATH void fill(FLOATTYPE fill_value);
  81. INLINE_LINMATH void set(FLOATTYPE x, FLOATTYPE y, FLOATTYPE z, FLOATTYPE w);
  82. INLINE_LINMATH FLOATTYPE length() const;
  83. INLINE_LINMATH FLOATTYPE length_squared() const;
  84. INLINE_LINMATH bool normalize();
  85. INLINE_LINMATH FLOATTYPE dot(const FLOATNAME(LVecBase4) &other) const;
  86. INLINE_LINMATH FLOATNAME(LVecBase4) project(const FLOATNAME(LVecBase4) &onto) const;
  87. INLINE_LINMATH bool operator < (const FLOATNAME(LVecBase4) &other) const;
  88. INLINE_LINMATH bool operator == (const FLOATNAME(LVecBase4) &other) const;
  89. INLINE_LINMATH bool operator != (const FLOATNAME(LVecBase4) &other) const;
  90. INLINE_LINMATH int compare_to(const FLOATNAME(LVecBase4) &other) const;
  91. INLINE_LINMATH int compare_to(const FLOATNAME(LVecBase4) &other,
  92. FLOATTYPE threshold) const;
  93. INLINE_LINMATH size_t get_hash() const;
  94. INLINE_LINMATH size_t get_hash(FLOATTYPE threshold) const;
  95. INLINE_LINMATH size_t add_hash(size_t hash) const;
  96. INLINE_LINMATH size_t add_hash(size_t hash, FLOATTYPE threshold) const;
  97. INLINE_LINMATH FLOATNAME(LVecBase4) operator - () const;
  98. INLINE_LINMATH FLOATNAME(LVecBase4)
  99. operator + (const FLOATNAME(LVecBase4) &other) const;
  100. INLINE_LINMATH FLOATNAME(LVecBase4)
  101. operator - (const FLOATNAME(LVecBase4) &other) const;
  102. INLINE_LINMATH FLOATNAME(LVecBase4) operator * (FLOATTYPE scalar) const;
  103. INLINE_LINMATH FLOATNAME(LVecBase4) operator / (FLOATTYPE scalar) const;
  104. INLINE_LINMATH void operator += (const FLOATNAME(LVecBase4) &other);
  105. INLINE_LINMATH void operator -= (const FLOATNAME(LVecBase4) &other);
  106. INLINE_LINMATH void operator *= (FLOATTYPE scalar);
  107. INLINE_LINMATH void operator /= (FLOATTYPE scalar);
  108. INLINE_LINMATH FLOATNAME(LVecBase4) fmax(const FLOATNAME(LVecBase4) &other);
  109. INLINE_LINMATH FLOATNAME(LVecBase4) fmin(const FLOATNAME(LVecBase4) &other);
  110. INLINE_LINMATH bool almost_equal(const FLOATNAME(LVecBase4) &other,
  111. FLOATTYPE threshold) const;
  112. INLINE_LINMATH bool almost_equal(const FLOATNAME(LVecBase4) &other) const;
  113. INLINE_LINMATH void output(ostream &out) const;
  114. #ifdef HAVE_PYTHON
  115. INLINE_LINMATH void python_repr(ostream &out, const string &class_name) const;
  116. #endif
  117. public:
  118. INLINE_LINMATH void generate_hash(ChecksumHashGenerator &hashgen) const;
  119. INLINE_LINMATH void generate_hash(ChecksumHashGenerator &hashgen,
  120. FLOATTYPE threshold) const;
  121. public:
  122. union {
  123. FLOATTYPE data[4];
  124. struct {FLOATTYPE _0, _1, _2, _3;} v;
  125. } _v;
  126. private:
  127. static const FLOATNAME(LVecBase4) _zero;
  128. static const FLOATNAME(LVecBase4) _unit_x;
  129. static const FLOATNAME(LVecBase4) _unit_y;
  130. static const FLOATNAME(LVecBase4) _unit_z;
  131. static const FLOATNAME(LVecBase4) _unit_w;
  132. public:
  133. INLINE_LINMATH void write_datagram(Datagram &destination) const;
  134. INLINE_LINMATH void read_datagram(DatagramIterator &source);
  135. public:
  136. static TypeHandle get_class_type() {
  137. return _type_handle;
  138. }
  139. static void init_type();
  140. private:
  141. static TypeHandle _type_handle;
  142. };
  143. INLINE_LINMATH ostream &operator << (ostream &out, const FLOATNAME(LVecBase4) &vec) {
  144. vec.output(out);
  145. return out;
  146. }
  147. #include "lvecBase4_src.I"