point.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /G/wwlib/Point.h $*
  25. * *
  26. * $Author:: Eric_c $*
  27. * *
  28. * $Modtime:: 4/07/99 5:24p $*
  29. * *
  30. * $Revision:: 5 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if _MSC_VER >= 1000
  36. #pragma once
  37. #endif // _MSC_VER >= 1000
  38. #ifndef POINT_H
  39. #define POINT_H
  40. template<class T> class TRect;
  41. /*
  42. ** This class describes a point in 2 dimensional space using arbitrary
  43. ** components. The interpretation of which is outside the scope
  44. ** of this class. This class is the successor to the old style COORDINATE
  45. ** and CELL types but also serves anywhere an X and Y value are treated
  46. ** as a logical object (e.g., pixel location).
  47. */
  48. template<class T>
  49. class TPoint2D {
  50. public:
  51. TPoint2D(void) {} // Default constructor does nothing by design.
  52. TPoint2D(T x, T y) : X(x), Y(y) {}
  53. // Equality comparison operators.
  54. bool operator == (TPoint2D<T> const & rvalue) const {return(X==rvalue.X && Y==rvalue.Y);}
  55. bool operator != (TPoint2D<T> const & rvalue) const {return(X!=rvalue.X || Y!=rvalue.Y);}
  56. // Addition and subtraction operators.
  57. TPoint2D<T> const & operator += (TPoint2D<T> const & rvalue) {X += rvalue.X;Y += rvalue.Y;return(*this);}
  58. TPoint2D<T> const & operator -= (TPoint2D<T> const & rvalue) {X -= rvalue.X;Y -= rvalue.Y;return(*this);}
  59. TPoint2D<T> const operator - (TPoint2D<T> const & rvalue) const {return(TPoint2D<T>(X - rvalue.X, Y - rvalue.Y));}
  60. TPoint2D<T> const operator + (TPoint2D<T> const & rvalue) const {return(TPoint2D<T>(X + rvalue.X, Y + rvalue.Y));}
  61. // Scalar multiplication and division.
  62. TPoint2D<T> const operator * (T rvalue) const {return(TPoint2D<T>(X * rvalue, Y * rvalue));}
  63. TPoint2D<T> const & operator *= (T rvalue) {X *= rvalue; Y *= rvalue;return(*this);}
  64. TPoint2D<T> const operator / (T rvalue) const {if (rvalue == T(0)) return(TPoint2D<T>(0,0));return(TPoint2D<T>(X / rvalue, Y / rvalue));}
  65. TPoint2D<T> const & operator /= (T rvalue) {if (rvalue != T(0)) {X /= rvalue;Y /= rvalue;}return(*this);}
  66. // Dot and cross product.
  67. TPoint2D<T> const operator * (TPoint2D<T> const & rvalue) const {return(TPoint2D<T>(X * rvalue.X, Y * rvalue.Y));}
  68. TPoint2D<T> const Dot_Product(TPoint2D<T> const & rvalue) const {return(TPoint2D<T>(X * rvalue.X, Y * rvalue.Y));}
  69. TPoint2D<T> const Cross_Product(TPoint2D<T> const & rvalue) const {return(TPoint2D<T>(Y - rvalue.Y, rvalue.X - X));}
  70. // Negation operator -- simple and effective
  71. TPoint2D<T> const operator - (void) const {return(TPoint2D<T>(-X, -Y));}
  72. // Vector support functions.
  73. T Length(void) const {return(T(sqrt(X*X + Y*Y)));}
  74. TPoint2D<T> const Normalize(void) const {
  75. double len = sqrt(X*X + Y*Y);
  76. if (len != 0.0) {
  77. return(TPoint2D<T>((T)((double)X / len), (T)((double)Y / len)));
  78. } else {
  79. return(*this);
  80. }
  81. }
  82. // Bias a point into a clipping rectangle.
  83. TPoint2D<T> const Bias_To(TRect<T> const & rect) const;
  84. // Find distance between points.
  85. T Distance_To(TPoint2D<T> const & point) const {return((*this - point).Length());}
  86. public:
  87. T X;
  88. T Y;
  89. };
  90. template<class T>
  91. TPoint2D<T> const operator * (T lvalue, TPoint2D<T> const & rvalue)
  92. {
  93. return(rvalue * lvalue);
  94. }
  95. /*
  96. ** This typedef provides an uncluttered type name for use by simple integer points.
  97. */
  98. typedef TPoint2D<int> Point2D;
  99. /*
  100. ** This describes a point in 3 dimensional space using arbitrary
  101. ** components. This is the successor to the COORDINATE type for those
  102. ** times when height needs to be tracked.
  103. **
  104. ** Notice that it is not implemented as a virtually derived class. This
  105. ** is for efficiency reasons. This class chooses to be smaller and faster at the
  106. ** expense of polymorphism. However, since it is publicly derived, inheritance is
  107. ** the next best thing.
  108. */
  109. template<class T>
  110. class TPoint3D : public TPoint2D<T> {
  111. typedef TPoint2D<T> BASECLASS;
  112. public:
  113. TPoint3D(void) {} // Default constructor does nothing by design.
  114. TPoint3D(T x, T y, T z) : TPoint2D<T>(x, y), Z(z) {}
  115. TPoint3D(BASECLASS const & rvalue) : TPoint2D<T>(rvalue), Z(0) {}
  116. // Equality comparison operators.
  117. bool operator == (TPoint3D<T> const & rvalue) const {return(X==rvalue.X && Y==rvalue.Y && Z==rvalue.Z);}
  118. bool operator != (TPoint3D<T> const & rvalue) const {return(X!=rvalue.X || Y!=rvalue.Y || Z!=rvalue.Z);}
  119. // Addition and subtraction operators.
  120. TPoint3D<T> const & operator += (TPoint3D<T> const & rvalue) {X += rvalue.X;Y += rvalue.Y;Z += rvalue.Z;return(*this);}
  121. TPoint2D<T> const & operator += (TPoint2D<T> const & rvalue) {BASECLASS::operator += (rvalue);return(*this);}
  122. TPoint3D<T> const & operator -= (TPoint3D<T> const & rvalue) {X -= rvalue.X;Y -= rvalue.Y;Z -= rvalue.Z;return(*this);}
  123. TPoint2D<T> const & operator -= (TPoint2D<T> const & rvalue) {BASECLASS::operator -= (rvalue);return(*this);}
  124. TPoint3D<T> const operator - (TPoint3D<T> const & rvalue) const {return(TPoint3D<T>(X - rvalue.X, Y - rvalue.Y, Z - rvalue.Z));}
  125. TPoint3D<T> const operator - (TPoint2D<T> const & rvalue) const {return(TPoint3D<T>(X - rvalue.X, Y - rvalue.Y, Z));}
  126. TPoint3D<T> const operator + (TPoint3D<T> const & rvalue) const {return(TPoint3D<T>(X + rvalue.X, Y + rvalue.Y, Z + rvalue.Z));}
  127. TPoint3D<T> const operator + (TPoint2D<T> const & rvalue) const {return(TPoint3D<T>(X + rvalue.X, Y + rvalue.Y, Z));}
  128. // Scalar multiplication and division.
  129. TPoint3D<T> const operator * (T rvalue) const {return(TPoint3D<T>(X * rvalue, Y * rvalue, Z * rvalue));}
  130. TPoint3D<T> const & operator *= (T rvalue) {X *= rvalue;Y *= rvalue;Z *= rvalue;return(*this);}
  131. TPoint3D<T> const operator / (T rvalue) const {if (rvalue == T(0)) return(TPoint3D<T>(0,0,0));return(TPoint3D<T>(X / rvalue, Y / rvalue, Z / rvalue));}
  132. TPoint3D<T> const & operator /= (T rvalue) {if (rvalue != T(0)) {X /= rvalue;Y /= rvalue;Z /= rvalue;}return(*this);}
  133. // Dot and cross product.
  134. TPoint3D<T> const operator * (TPoint3D<T> const & rvalue) const {return(TPoint3D<T>(X * rvalue.X, Y * rvalue.Y, Z * rvalue.Z));}
  135. TPoint3D<T> const Dot_Product(TPoint3D<T> const & rvalue) const {return(TPoint3D<T>(X * rvalue.X, Y * rvalue.Y, Z * rvalue.Z));}
  136. TPoint3D<T> const Cross_Product(TPoint3D<T> const & rvalue) const {return TPoint3D<T>(Y * rvalue.Z - Z * rvalue.Y, Z * rvalue.X - X * rvalue.Z, X * rvalue.Y - Y * rvalue.X);}
  137. // Negation operator -- simple and effective
  138. TPoint3D<T> const operator - (void) const {return(TPoint3D<T>(-X, -Y, -Z));}
  139. // Vector support functions.
  140. T Length(void) const {return(T(sqrt(X*X + Y*Y + Z*Z)));}
  141. TPoint3D<T> const Normalize(void) const {
  142. double len = sqrt(X*X + Y*Y + Z*Z);
  143. if (len != 0.0) {
  144. return(TPoint3D<T>(X / len, Y / len, Z / len));
  145. } else {
  146. return(*this);
  147. }
  148. }
  149. // Find distance between points.
  150. T Distance_To(TPoint3D<T> const & point) const {return((*this - point).Length());}
  151. T Distance_To(TPoint2D<T> const & point) const {return(BASECLASS::Distance_To(point));}
  152. public:
  153. /*
  154. ** The Z component of this point.
  155. */
  156. T Z;
  157. };
  158. template<class T>
  159. TPoint3D<T> const operator * (T lvalue, TPoint3D<T> const & rvalue)
  160. {
  161. return(rvalue * lvalue);
  162. }
  163. /*
  164. ** This typedef provides a simple uncluttered type name for use by
  165. ** integer 3D points.
  166. */
  167. typedef TPoint3D<int> Point3D;
  168. #endif