Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
8.6 KiB

  1. #ifndef _Point_h
  2. #define _Point_h
  3. // File: Point.h
  4. //
  5. // Classes to support 2D and 3D affine space
  6. // D. P. Mitchell 95/06/02.
  7. //
  8. // History:
  9. // -@- 08/01/95 (mikemarr) - unified & cleaned up interface
  10. // - added Print and Parse
  11. // -@- 08/01/95 (mikemarr) - define all inlined functions with macros
  12. // -@- 06/21/96 (mikemarr) - added IsCCW
  13. // -@- 10/29/97 (mikemarr) - changed data to be floats, not vectors
  14. // - removed data accessors & made data public
  15. // - changed +=, -= operators to return reference
  16. // - removed I/O
  17. // - added operator const float *
  18. // - changed fuzzy equal to be IsEqual, operator == to be exact
  19. // - removed macro junk
  20. // -@- 11/04/97 (mikemarr) - added initialization with POINT
  21. // -@- 11/10/97 (mikemarr) - added operator *,/,*=,/=
  22. #ifndef _VecMath_h
  23. #include "VecMath.h"
  24. #endif
  25. // Class: Point2
  26. // Hungarian: pnt
  27. class Point2 {
  28. public:
  29. Point2() {}
  30. Point2(float fX, float fY) : x(fX), y(fY) {}
  31. Point2(const Vector2 &v) : x(v.x), y(v.y) {}
  32. Point2(const POINT &pt) : x(pt.x + 0.5f), y(pt.y + 0.5f) {}
  33. // ALGEBRAIC OPERATORS
  34. friend Point2 operator +(const Point2 &p, const Vector2 &v);
  35. friend Point2 operator +(const Vector2 &v, const Point2 &p);
  36. friend Point2 operator -(const Point2 &p, const Vector2 &v);
  37. friend Point2 operator -(const Vector2 &v, const Point2 &p);
  38. friend Point2 operator *(const Point2 &p, float a);
  39. friend Point2 operator *(float a, const Point2 &p);
  40. friend Point2 operator /(const Point2 &p, float a);
  41. friend Vector2 operator -(const Point2 &p, const Point2 &q);
  42. friend Point2 & operator +=(Point2 &p, const Vector2 &v);
  43. friend Point2 & operator -=(Point2 &p, const Vector2 &v);
  44. friend Point2 & operator *=(Point2 &p, float a);
  45. friend Point2 & operator /=(Point2 &p, float a);
  46. friend float operator *(const CoVector2 &cv, const Point2 &p);
  47. friend int operator ==(const Point2 &p, const Point2 &q);
  48. friend int operator !=(const Point2 &p, const Point2 &q);
  49. friend int IsEqual(const Point2 &p, const Point2 &q);
  50. friend Point2 Lerp(const Point2 &p, const Point2 &q, float t);
  51. friend bool IsCCW(const Point2 &p0, const Point2 &p, const Point2 &q);
  52. operator const float *() const { return &x; }
  53. float X() const { return x; }
  54. float Y() const { return y; }
  55. float & X() { return x; }
  56. float & Y() { return y; }
  57. public:
  58. float x, y;
  59. };
  60. // Class: Point3
  61. // Hungarian: pnt
  62. class Point3 {
  63. public:
  64. Point3() {}
  65. Point3(float fX, float fY, float fZ) : x(fX), y(fY), z(fZ) {}
  66. Point3(const Vector3 &v) : x(v.x), y(v.y), z(v.z) {}
  67. // ALGEBRAIC OPERATORS
  68. friend Point3 operator +(const Point3 &p, const Vector3 &v);
  69. friend Point3 operator +(const Vector3 &v, const Point3 &p);
  70. friend Point3 operator -(const Point3 &p, const Vector3 &v);
  71. friend Point3 operator -(const Vector3 &v, const Point3 &p);
  72. friend Vector3 operator -(const Point3 &p, const Point3 &q);
  73. friend Point3 operator *(const Point3 &p, float a);
  74. friend Point3 operator *(float a, const Point3 &p);
  75. friend Point3 operator /(const Point3 &p, float a);
  76. friend Point3 & operator +=(Point3 &p, const Vector3 &v);
  77. friend Point3 & operator -=(Point3 &p, const Vector3 &v);
  78. friend Point3 & operator *=(Point3 &p, float a);
  79. friend Point3 & operator /=(Point3 &p, float a);
  80. friend float operator *(const CoVector3 &cv, const Point3 &p);
  81. friend int operator ==(const Point3 &p, const Point3 &q);
  82. friend int operator !=(const Point3 &p, const Point3 &q);
  83. friend int IsEqual(const Point3 &p, const Point3 &q);
  84. friend Point3 Lerp(const Point3 &p, const Point3 &q, float t);
  85. Point2 Project(DWORD iAxis) const;
  86. operator const float *() const { return &x; }
  87. float X() const { return x; }
  88. float Y() const { return y; }
  89. float Z() const { return z; }
  90. float & X() { return x; }
  91. float & Y() { return y; }
  92. float & Z() { return z; }
  93. public:
  94. float x, y, z;
  95. };
  96. ///////////
  97. // Point2
  98. ///////////
  99. inline Point2
  100. operator +(const Point2 &p, const Vector2 &v)
  101. {
  102. return Point2(p.x + v.x, p.y + v.y);
  103. }
  104. inline Point2
  105. operator +(const Vector2 &v, const Point2 &p)
  106. {
  107. return Point2(p.x + v.x, p.y + v.y);
  108. }
  109. inline Point2
  110. operator -(const Point2 &p, const Vector2 &v)
  111. {
  112. return Point2(p.x - v.x, p.y - v.y);
  113. }
  114. inline Point2
  115. operator -(const Vector2 &v, const Point2 &p)
  116. {
  117. return Point2(p.x - v.x, p.y - v.y);
  118. }
  119. inline Vector2
  120. operator -(const Point2 &p, const Point2 &q)
  121. {
  122. return Vector2(p.x - q.x, p.y - q.y);
  123. }
  124. inline Point2
  125. operator *(float a, const Point2 &p)
  126. {
  127. return Point2(a*p.x, a*p.y);
  128. }
  129. inline Point2
  130. operator *(const Point2 &p, float a)
  131. {
  132. return Point2(a*p.x, a*p.y);
  133. }
  134. inline Point2
  135. operator /(const Point2 &p, float a)
  136. {
  137. MMASSERT(a != 0.f);
  138. float fTmp = 1.f/a;
  139. return Point2(p.x * fTmp, p.y * fTmp);
  140. }
  141. inline Point2 &
  142. operator +=(Point2 &p, const Vector2 &v)
  143. {
  144. p.x += v.x;
  145. p.y += v.y;
  146. return p;
  147. }
  148. inline Point2 &
  149. operator -=(Point2 &p, const Vector2 &v)
  150. {
  151. p.x -= v.x;
  152. p.y -= v.y;
  153. return p;
  154. }
  155. inline Point2 &
  156. operator *=(Point2 &p, float a)
  157. {
  158. p.x *= a;
  159. p.y *= a;
  160. return p;
  161. }
  162. inline Point2 &
  163. operator /=(Point2 &p, float a)
  164. {
  165. MMASSERT(a != 0.f);
  166. float fTmp = 1.f/a;
  167. p.x *= fTmp;
  168. p.y *= fTmp;
  169. return p;
  170. }
  171. inline int
  172. operator ==(const Point2 &p, const Point2 &q)
  173. {
  174. return ((p.x == q.x) && (p.y == q.y));
  175. }
  176. inline int
  177. operator !=(const Point2 &p, const Point2 &q)
  178. {
  179. return ((p.x != q.x) || (p.y != q.y));
  180. }
  181. inline int
  182. IsEqual(const Point2 &p, const Point2 &q)
  183. {
  184. return (FloatEquals(p.x, q.x) && FloatEquals(p.y, q.y));
  185. }
  186. inline Point2
  187. Lerp(const Point2 &p, const Point2 &q, float t)
  188. {
  189. return Point2(p.x + (q.x - p.x) * t, p.y + (q.y - p.y) * t);
  190. }
  191. inline bool
  192. IsCCW(const Point2 &p0, const Point2 &p1, const Point2 &p2)
  193. {
  194. #ifdef MIRRORY
  195. return ((p0.y - p1.y) * (p2.x - p1.x) <= (p0.x - p1.x) * (p2.y - p1.y));
  196. #else
  197. return ((p0.y - p1.y) * (p2.x - p1.x) >= (p0.x - p1.x) * (p2.y - p1.y));
  198. #endif
  199. }
  200. inline float
  201. operator *(const CoVector2 &cv, const Point2 &p)
  202. {
  203. return cv.x*p.x + cv.y*p.y;
  204. }
  205. ///////////
  206. // Point3
  207. ///////////
  208. inline Point3
  209. operator +(const Point3 &p, const Vector3 &v)
  210. {
  211. return Point3(p.x + v.x, p.y + v.y, p.z + v.z);
  212. }
  213. inline Point3
  214. operator +(const Vector3 &v, const Point3 &p)
  215. {
  216. return Point3(p.x + v.x, p.y + v.y, p.z + v.z);
  217. }
  218. inline Point3
  219. operator -(const Point3 &p, const Vector3 &v)
  220. {
  221. return Point3(p.x - v.x, p.y - v.y, p.z - v.z);
  222. }
  223. inline Point3
  224. operator -(const Vector3 &v, const Point3 &p)
  225. {
  226. return Point3(p.x - v.x, p.y - v.y, p.z - v.z);
  227. }
  228. inline Vector3
  229. operator -(const Point3 &p, const Point3 &q)
  230. {
  231. return Vector3(p.x - q.x, p.y - q.y, p.z - q.z);
  232. }
  233. inline Point3
  234. operator *(float a, const Point3 &p)
  235. {
  236. return Point3(a*p.x, a*p.y, a*p.z);
  237. }
  238. inline Point3
  239. operator *(const Point3 &p, float a)
  240. {
  241. return Point3(a*p.x, a*p.y, a*p.z);
  242. }
  243. inline Point3
  244. operator /(const Point3 &p, float a)
  245. {
  246. MMASSERT(a != 0.f);
  247. float fTmp = 1.f/a;
  248. return Point3(p.x * fTmp, p.y * fTmp, p.z * fTmp);
  249. }
  250. inline Point3 &
  251. operator +=(Point3 &p, const Vector3 &v)
  252. {
  253. p.x += v.x;
  254. p.y += v.y;
  255. p.z += v.z;
  256. return p;
  257. }
  258. inline Point3 &
  259. operator -=(Point3 &p, const Vector3 &v)
  260. {
  261. p.x -= v.x;
  262. p.y -= v.y;
  263. p.z -= v.z;
  264. return p;
  265. }
  266. inline Point3 &
  267. operator *=(Point3 &p, float a)
  268. {
  269. p.x *= a;
  270. p.y *= a;
  271. p.z *= a;
  272. return p;
  273. }
  274. inline Point3 &
  275. operator /=(Point3 &p, float a)
  276. {
  277. MMASSERT(a != 0.f);
  278. float fTmp = 1.f/a;
  279. p.x *= fTmp;
  280. p.y *= fTmp;
  281. p.z *= fTmp;
  282. return p;
  283. }
  284. inline int
  285. operator ==(const Point3 &p, const Point3 &q)
  286. {
  287. return ((p.x == q.x) && (p.y == q.y) && (p.z == q.z));
  288. }
  289. inline int
  290. operator !=(const Point3 &p, const Point3 &q)
  291. {
  292. return ((p.x != q.x) || (p.y != q.y) || (p.z != q.z));
  293. }
  294. inline int
  295. IsEqual(const Point3 &p, const Point3 &q)
  296. {
  297. return (FloatEquals(p.x, q.x) && FloatEquals(p.y, q.y) && FloatEquals(p.z, q.z));
  298. }
  299. inline Point3
  300. Lerp(const Point3 &p, const Point3 &q, float t)
  301. {
  302. return Point3(p.x + (q.x - p.x) * t, p.y + (q.y - p.y) * t, p.z + (q.z - p.z) * t);
  303. }
  304. inline Point2
  305. Point3::Project(DWORD iAxis) const
  306. {
  307. switch (iAxis) {
  308. case 0: return Point2(y, z);
  309. case 1: return Point2(x, z);
  310. case 2: return Point2(x, y);
  311. }
  312. return Point2(0.f, 0.f);
  313. }
  314. inline float
  315. operator *(const CoVector3 &cv, const Point3 &p)
  316. {
  317. return cv.x*p.x + cv.y*p.y + cv.z*p.z;
  318. }
  319. #endif