vector.inl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. // This code is in the public domain -- [email protected]
  2. #ifndef NV_MATH_VECTOR_INL
  3. #define NV_MATH_VECTOR_INL
  4. #include "vector.h"
  5. #include "nvcore/utils.h" // min, max
  6. #include "nvcore/hash.h" // hash
  7. namespace nv
  8. {
  9. // Helpers to convert vector types. Assume T has x,y members and 2 argument constructor.
  10. //template <typename T> T to(Vector2::Arg v) { return T(v.x, v.y); }
  11. // Helpers to convert vector types. Assume T has x,y,z members and 3 argument constructor.
  12. //template <typename T> T to(Vector3::Arg v) { return T(v.x, v.y, v.z); }
  13. // Helpers to convert vector types. Assume T has x,y,z members and 3 argument constructor.
  14. //template <typename T> T to(Vector4::Arg v) { return T(v.x, v.y, v.z, v.w); }
  15. // Vector2
  16. inline Vector2::Vector2() {}
  17. inline Vector2::Vector2(float f) : x(f), y(f) {}
  18. inline Vector2::Vector2(float x, float y) : x(x), y(y) {}
  19. inline Vector2::Vector2(Vector2::Arg v) : x(v.x), y(v.y) {}
  20. inline const Vector2 & Vector2::operator=(Vector2::Arg v)
  21. {
  22. x = v.x;
  23. y = v.y;
  24. return *this;
  25. }
  26. inline const float * Vector2::ptr() const
  27. {
  28. return &x;
  29. }
  30. inline void Vector2::set(float x, float y)
  31. {
  32. this->x = x;
  33. this->y = y;
  34. }
  35. inline Vector2 Vector2::operator-() const
  36. {
  37. return Vector2(-x, -y);
  38. }
  39. inline void Vector2::operator+=(Vector2::Arg v)
  40. {
  41. x += v.x;
  42. y += v.y;
  43. }
  44. inline void Vector2::operator-=(Vector2::Arg v)
  45. {
  46. x -= v.x;
  47. y -= v.y;
  48. }
  49. inline void Vector2::operator*=(float s)
  50. {
  51. x *= s;
  52. y *= s;
  53. }
  54. inline void Vector2::operator*=(Vector2::Arg v)
  55. {
  56. x *= v.x;
  57. y *= v.y;
  58. }
  59. inline bool operator==(Vector2::Arg a, Vector2::Arg b)
  60. {
  61. return a.x == b.x && a.y == b.y;
  62. }
  63. inline bool operator!=(Vector2::Arg a, Vector2::Arg b)
  64. {
  65. return a.x != b.x || a.y != b.y;
  66. }
  67. // Vector3
  68. inline Vector3::Vector3() {}
  69. inline Vector3::Vector3(float f) : x(f), y(f), z(f) {}
  70. inline Vector3::Vector3(float x, float y, float z) : x(x), y(y), z(z) {}
  71. inline Vector3::Vector3(Vector2::Arg v, float z) : x(v.x), y(v.y), z(z) {}
  72. inline Vector3::Vector3(Vector3::Arg v) : x(v.x), y(v.y), z(v.z) {}
  73. inline const Vector3 & Vector3::operator=(Vector3::Arg v)
  74. {
  75. x = v.x;
  76. y = v.y;
  77. z = v.z;
  78. return *this;
  79. }
  80. inline Vector2 Vector3::xy() const
  81. {
  82. return Vector2(x, y);
  83. }
  84. inline const float * Vector3::ptr() const
  85. {
  86. return &x;
  87. }
  88. inline void Vector3::set(float x, float y, float z)
  89. {
  90. this->x = x;
  91. this->y = y;
  92. this->z = z;
  93. }
  94. inline Vector3 Vector3::operator-() const
  95. {
  96. return Vector3(-x, -y, -z);
  97. }
  98. inline void Vector3::operator+=(Vector3::Arg v)
  99. {
  100. x += v.x;
  101. y += v.y;
  102. z += v.z;
  103. }
  104. inline void Vector3::operator-=(Vector3::Arg v)
  105. {
  106. x -= v.x;
  107. y -= v.y;
  108. z -= v.z;
  109. }
  110. inline void Vector3::operator*=(float s)
  111. {
  112. x *= s;
  113. y *= s;
  114. z *= s;
  115. }
  116. inline void Vector3::operator/=(float s)
  117. {
  118. float is = 1.0f / s;
  119. x *= is;
  120. y *= is;
  121. z *= is;
  122. }
  123. inline void Vector3::operator*=(Vector3::Arg v)
  124. {
  125. x *= v.x;
  126. y *= v.y;
  127. z *= v.z;
  128. }
  129. inline void Vector3::operator/=(Vector3::Arg v)
  130. {
  131. x /= v.x;
  132. y /= v.y;
  133. z /= v.z;
  134. }
  135. inline bool operator==(Vector3::Arg a, Vector3::Arg b)
  136. {
  137. return a.x == b.x && a.y == b.y && a.z == b.z;
  138. }
  139. inline bool operator!=(Vector3::Arg a, Vector3::Arg b)
  140. {
  141. return a.x != b.x || a.y != b.y || a.z != b.z;
  142. }
  143. // Vector4
  144. inline Vector4::Vector4() {}
  145. inline Vector4::Vector4(float f) : x(f), y(f), z(f), w(f) {}
  146. inline Vector4::Vector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
  147. inline Vector4::Vector4(Vector2::Arg v, float z, float w) : x(v.x), y(v.y), z(z), w(w) {}
  148. inline Vector4::Vector4(Vector2::Arg v, Vector2::Arg u) : x(v.x), y(v.y), z(u.x), w(u.y) {}
  149. inline Vector4::Vector4(Vector3::Arg v, float w) : x(v.x), y(v.y), z(v.z), w(w) {}
  150. inline Vector4::Vector4(Vector4::Arg v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
  151. inline const Vector4 & Vector4::operator=(const Vector4 & v)
  152. {
  153. x = v.x;
  154. y = v.y;
  155. z = v.z;
  156. w = v.w;
  157. return *this;
  158. }
  159. inline Vector2 Vector4::xy() const
  160. {
  161. return Vector2(x, y);
  162. }
  163. inline Vector2 Vector4::zw() const
  164. {
  165. return Vector2(z, w);
  166. }
  167. inline Vector3 Vector4::xyz() const
  168. {
  169. return Vector3(x, y, z);
  170. }
  171. inline const float * Vector4::ptr() const
  172. {
  173. return &x;
  174. }
  175. inline void Vector4::set(float x, float y, float z, float w)
  176. {
  177. this->x = x;
  178. this->y = y;
  179. this->z = z;
  180. this->w = w;
  181. }
  182. inline Vector4 Vector4::operator-() const
  183. {
  184. return Vector4(-x, -y, -z, -w);
  185. }
  186. inline void Vector4::operator+=(Vector4::Arg v)
  187. {
  188. x += v.x;
  189. y += v.y;
  190. z += v.z;
  191. w += v.w;
  192. }
  193. inline void Vector4::operator-=(Vector4::Arg v)
  194. {
  195. x -= v.x;
  196. y -= v.y;
  197. z -= v.z;
  198. w -= v.w;
  199. }
  200. inline void Vector4::operator*=(float s)
  201. {
  202. x *= s;
  203. y *= s;
  204. z *= s;
  205. w *= s;
  206. }
  207. inline void Vector4::operator/=(float s)
  208. {
  209. x /= s;
  210. y /= s;
  211. z /= s;
  212. w /= s;
  213. }
  214. inline void Vector4::operator*=(Vector4::Arg v)
  215. {
  216. x *= v.x;
  217. y *= v.y;
  218. z *= v.z;
  219. w *= v.w;
  220. }
  221. inline void Vector4::operator/=(Vector4::Arg v)
  222. {
  223. x /= v.x;
  224. y /= v.y;
  225. z /= v.z;
  226. w /= v.w;
  227. }
  228. inline bool operator==(Vector4::Arg a, Vector4::Arg b)
  229. {
  230. return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
  231. }
  232. inline bool operator!=(Vector4::Arg a, Vector4::Arg b)
  233. {
  234. return a.x != b.x || a.y != b.y || a.z != b.z || a.w != b.w;
  235. }
  236. // Functions
  237. // Vector2
  238. inline Vector2 add(Vector2::Arg a, Vector2::Arg b)
  239. {
  240. return Vector2(a.x + b.x, a.y + b.y);
  241. }
  242. inline Vector2 operator+(Vector2::Arg a, Vector2::Arg b)
  243. {
  244. return add(a, b);
  245. }
  246. inline Vector2 sub(Vector2::Arg a, Vector2::Arg b)
  247. {
  248. return Vector2(a.x - b.x, a.y - b.y);
  249. }
  250. inline Vector2 operator-(Vector2::Arg a, Vector2::Arg b)
  251. {
  252. return sub(a, b);
  253. }
  254. inline Vector2 scale(Vector2::Arg v, float s)
  255. {
  256. return Vector2(v.x * s, v.y * s);
  257. }
  258. inline Vector2 scale(Vector2::Arg v, Vector2::Arg s)
  259. {
  260. return Vector2(v.x * s.x, v.y * s.y);
  261. }
  262. inline Vector2 operator*(Vector2::Arg v, float s)
  263. {
  264. return scale(v, s);
  265. }
  266. inline Vector2 operator*(Vector2::Arg v1, Vector2::Arg v2)
  267. {
  268. return Vector2(v1.x*v2.x, v1.y*v2.y);
  269. }
  270. inline Vector2 operator*(float s, Vector2::Arg v)
  271. {
  272. return scale(v, s);
  273. }
  274. inline Vector2 operator/(Vector2::Arg v, float s)
  275. {
  276. return scale(v, 1.0f/s);
  277. }
  278. inline Vector2 lerp(Vector2::Arg v1, Vector2::Arg v2, float t)
  279. {
  280. const float s = 1.0f - t;
  281. return Vector2(v1.x * s + t * v2.x, v1.y * s + t * v2.y);
  282. }
  283. inline float dot(Vector2::Arg a, Vector2::Arg b)
  284. {
  285. return a.x * b.x + a.y * b.y;
  286. }
  287. inline float lengthSquared(Vector2::Arg v)
  288. {
  289. return v.x * v.x + v.y * v.y;
  290. }
  291. inline float length(Vector2::Arg v)
  292. {
  293. return sqrtf(lengthSquared(v));
  294. }
  295. inline float distance(Vector2::Arg a, Vector2::Arg b)
  296. {
  297. return length(a - b);
  298. }
  299. inline float inverseLength(Vector2::Arg v)
  300. {
  301. return 1.0f / sqrtf(lengthSquared(v));
  302. }
  303. inline bool isNormalized(Vector2::Arg v, float epsilon = NV_NORMAL_EPSILON)
  304. {
  305. return equal(length(v), 1, epsilon);
  306. }
  307. inline Vector2 normalize(Vector2::Arg v, float epsilon = NV_EPSILON)
  308. {
  309. float l = length(v);
  310. NV_UNUSED(epsilon);
  311. nvDebugCheck(!isZero(l, epsilon));
  312. Vector2 n = scale(v, 1.0f / l);
  313. nvDebugCheck(isNormalized(n));
  314. return n;
  315. }
  316. inline Vector2 normalizeSafe(Vector2::Arg v, Vector2::Arg fallback, float epsilon = NV_EPSILON)
  317. {
  318. float l = length(v);
  319. if (isZero(l, epsilon)) {
  320. return fallback;
  321. }
  322. return scale(v, 1.0f / l);
  323. }
  324. // Safe, branchless normalization from Andy Firth. All error checking ommitted.
  325. // http://altdevblogaday.com/2011/08/21/practical-flt-point-tricks/
  326. inline Vector2 normalizeFast(Vector2::Arg v)
  327. {
  328. const float very_small_float = 1.0e-037f;
  329. float l = very_small_float + length(v);
  330. return scale(v, 1.0f / l);
  331. }
  332. inline bool equal(Vector2::Arg v1, Vector2::Arg v2, float epsilon = NV_EPSILON)
  333. {
  334. return equal(v1.x, v2.x, epsilon) && equal(v1.y, v2.y, epsilon);
  335. }
  336. inline Vector2 min(Vector2::Arg a, Vector2::Arg b)
  337. {
  338. return Vector2(min(a.x, b.x), min(a.y, b.y));
  339. }
  340. inline Vector2 max(Vector2::Arg a, Vector2::Arg b)
  341. {
  342. return Vector2(max(a.x, b.x), max(a.y, b.y));
  343. }
  344. inline Vector2 clamp(Vector2::Arg v, float min, float max)
  345. {
  346. return Vector2(clamp(v.x, min, max), clamp(v.y, min, max));
  347. }
  348. inline Vector2 saturate(Vector2::Arg v)
  349. {
  350. return Vector2(saturate(v.x), saturate(v.y));
  351. }
  352. inline bool isFinite(Vector2::Arg v)
  353. {
  354. return isFinite(v.x) && isFinite(v.y);
  355. }
  356. inline Vector2 validate(Vector2::Arg v, Vector2::Arg fallback = Vector2(0.0f))
  357. {
  358. if (!isFinite(v)) return fallback;
  359. Vector2 vf = v;
  360. nv::floatCleanup(vf.component, 2);
  361. return vf;
  362. }
  363. // Note, this is the area scaled by 2!
  364. inline float triangleArea(Vector2::Arg v0, Vector2::Arg v1)
  365. {
  366. return (v0.x * v1.y - v0.y * v1.x); // * 0.5f;
  367. }
  368. inline float triangleArea(Vector2::Arg a, Vector2::Arg b, Vector2::Arg c)
  369. {
  370. // IC: While it may be appealing to use the following expression:
  371. //return (c.x * a.y + a.x * b.y + b.x * c.y - b.x * a.y - c.x * b.y - a.x * c.y); // * 0.5f;
  372. // That's actually a terrible idea. Small triangles far from the origin can end up producing fairly large floating point
  373. // numbers and the results becomes very unstable and dependent on the order of the factors.
  374. // Instead, it's preferable to substract the vertices first, and multiply the resulting small values together. The result
  375. // in this case is always much more accurate (as long as the triangle is small) and less dependent of the location of
  376. // the triangle.
  377. //return ((a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x)); // * 0.5f;
  378. return triangleArea(a-c, b-c);
  379. }
  380. template <>
  381. inline uint hash(const Vector2 & v, uint h)
  382. {
  383. return sdbmFloatHash(v.component, 2, h);
  384. }
  385. // Vector3
  386. inline Vector3 add(Vector3::Arg a, Vector3::Arg b)
  387. {
  388. return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
  389. }
  390. inline Vector3 add(Vector3::Arg a, float b)
  391. {
  392. return Vector3(a.x + b, a.y + b, a.z + b);
  393. }
  394. inline Vector3 operator+(Vector3::Arg a, Vector3::Arg b)
  395. {
  396. return add(a, b);
  397. }
  398. inline Vector3 operator+(Vector3::Arg a, float b)
  399. {
  400. return add(a, b);
  401. }
  402. inline Vector3 sub(Vector3::Arg a, Vector3::Arg b)
  403. {
  404. return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
  405. }
  406. inline Vector3 sub(Vector3::Arg a, float b)
  407. {
  408. return Vector3(a.x - b, a.y - b, a.z - b);
  409. }
  410. inline Vector3 operator-(Vector3::Arg a, Vector3::Arg b)
  411. {
  412. return sub(a, b);
  413. }
  414. inline Vector3 operator-(Vector3::Arg a, float b)
  415. {
  416. return sub(a, b);
  417. }
  418. inline Vector3 cross(Vector3::Arg a, Vector3::Arg b)
  419. {
  420. return Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
  421. }
  422. inline Vector3 scale(Vector3::Arg v, float s)
  423. {
  424. return Vector3(v.x * s, v.y * s, v.z * s);
  425. }
  426. inline Vector3 scale(Vector3::Arg v, Vector3::Arg s)
  427. {
  428. return Vector3(v.x * s.x, v.y * s.y, v.z * s.z);
  429. }
  430. inline Vector3 operator*(Vector3::Arg v, float s)
  431. {
  432. return scale(v, s);
  433. }
  434. inline Vector3 operator*(float s, Vector3::Arg v)
  435. {
  436. return scale(v, s);
  437. }
  438. inline Vector3 operator*(Vector3::Arg v, Vector3::Arg s)
  439. {
  440. return scale(v, s);
  441. }
  442. inline Vector3 operator/(Vector3::Arg v, float s)
  443. {
  444. return scale(v, 1.0f/s);
  445. }
  446. /*inline Vector3 add_scaled(Vector3::Arg a, Vector3::Arg b, float s)
  447. {
  448. return Vector3(a.x + b.x * s, a.y + b.y * s, a.z + b.z * s);
  449. }*/
  450. inline Vector3 lerp(Vector3::Arg v1, Vector3::Arg v2, float t)
  451. {
  452. const float s = 1.0f - t;
  453. return Vector3(v1.x * s + t * v2.x, v1.y * s + t * v2.y, v1.z * s + t * v2.z);
  454. }
  455. inline float dot(Vector3::Arg a, Vector3::Arg b)
  456. {
  457. return a.x * b.x + a.y * b.y + a.z * b.z;
  458. }
  459. inline float lengthSquared(Vector3::Arg v)
  460. {
  461. return v.x * v.x + v.y * v.y + v.z * v.z;
  462. }
  463. inline float length(Vector3::Arg v)
  464. {
  465. return sqrtf(lengthSquared(v));
  466. }
  467. inline float distance(Vector3::Arg a, Vector3::Arg b)
  468. {
  469. return length(a - b);
  470. }
  471. inline float distanceSquared(Vector3::Arg a, Vector3::Arg b)
  472. {
  473. return lengthSquared(a - b);
  474. }
  475. inline float inverseLength(Vector3::Arg v)
  476. {
  477. return 1.0f / sqrtf(lengthSquared(v));
  478. }
  479. inline bool isNormalized(Vector3::Arg v, float epsilon = NV_NORMAL_EPSILON)
  480. {
  481. return equal(length(v), 1, epsilon);
  482. }
  483. inline Vector3 normalize(Vector3::Arg v, float epsilon = NV_EPSILON)
  484. {
  485. float l = length(v);
  486. NV_UNUSED(epsilon);
  487. nvDebugCheck(!isZero(l, epsilon));
  488. Vector3 n = scale(v, 1.0f / l);
  489. nvDebugCheck(isNormalized(n));
  490. return n;
  491. }
  492. inline Vector3 normalizeSafe(Vector3::Arg v, Vector3::Arg fallback, float epsilon = NV_EPSILON)
  493. {
  494. float l = length(v);
  495. if (isZero(l, epsilon)) {
  496. return fallback;
  497. }
  498. return scale(v, 1.0f / l);
  499. }
  500. // Safe, branchless normalization from Andy Firth. All error checking ommitted.
  501. // http://altdevblogaday.com/2011/08/21/practical-flt-point-tricks/
  502. inline Vector3 normalizeFast(Vector3::Arg v)
  503. {
  504. const float very_small_float = 1.0e-037f;
  505. float l = very_small_float + length(v);
  506. return scale(v, 1.0f / l);
  507. }
  508. inline bool equal(Vector3::Arg v1, Vector3::Arg v2, float epsilon = NV_EPSILON)
  509. {
  510. return equal(v1.x, v2.x, epsilon) && equal(v1.y, v2.y, epsilon) && equal(v1.z, v2.z, epsilon);
  511. }
  512. inline Vector3 min(Vector3::Arg a, Vector3::Arg b)
  513. {
  514. return Vector3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
  515. }
  516. inline Vector3 max(Vector3::Arg a, Vector3::Arg b)
  517. {
  518. return Vector3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
  519. }
  520. inline Vector3 clamp(Vector3::Arg v, float min, float max)
  521. {
  522. return Vector3(clamp(v.x, min, max), clamp(v.y, min, max), clamp(v.z, min, max));
  523. }
  524. inline Vector3 saturate(Vector3::Arg v)
  525. {
  526. return Vector3(saturate(v.x), saturate(v.y), saturate(v.z));
  527. }
  528. inline Vector3 floor(Vector3::Arg v)
  529. {
  530. return Vector3(floorf(v.x), floorf(v.y), floorf(v.z));
  531. }
  532. inline Vector3 ceil(Vector3::Arg v)
  533. {
  534. return Vector3(ceilf(v.x), ceilf(v.y), ceilf(v.z));
  535. }
  536. inline bool isFinite(Vector3::Arg v)
  537. {
  538. return isFinite(v.x) && isFinite(v.y) && isFinite(v.z);
  539. }
  540. inline Vector3 validate(Vector3::Arg v, Vector3::Arg fallback = Vector3(0.0f))
  541. {
  542. if (!isFinite(v)) return fallback;
  543. Vector3 vf = v;
  544. nv::floatCleanup(vf.component, 3);
  545. return vf;
  546. }
  547. inline Vector3 reflect(Vector3::Arg v, Vector3::Arg n)
  548. {
  549. return v - (2 * dot(v, n)) * n;
  550. }
  551. template <>
  552. inline uint hash(const Vector3 & v, uint h)
  553. {
  554. return sdbmFloatHash(v.component, 3, h);
  555. }
  556. // Vector4
  557. inline Vector4 add(Vector4::Arg a, Vector4::Arg b)
  558. {
  559. return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
  560. }
  561. inline Vector4 operator+(Vector4::Arg a, Vector4::Arg b)
  562. {
  563. return add(a, b);
  564. }
  565. inline Vector4 sub(Vector4::Arg a, Vector4::Arg b)
  566. {
  567. return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
  568. }
  569. inline Vector4 operator-(Vector4::Arg a, Vector4::Arg b)
  570. {
  571. return sub(a, b);
  572. }
  573. inline Vector4 scale(Vector4::Arg v, float s)
  574. {
  575. return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
  576. }
  577. inline Vector4 scale(Vector4::Arg v, Vector4::Arg s)
  578. {
  579. return Vector4(v.x * s.x, v.y * s.y, v.z * s.z, v.w * s.w);
  580. }
  581. inline Vector4 operator*(Vector4::Arg v, float s)
  582. {
  583. return scale(v, s);
  584. }
  585. inline Vector4 operator*(float s, Vector4::Arg v)
  586. {
  587. return scale(v, s);
  588. }
  589. inline Vector4 operator*(Vector4::Arg v, Vector4::Arg s)
  590. {
  591. return scale(v, s);
  592. }
  593. inline Vector4 operator/(Vector4::Arg v, float s)
  594. {
  595. return scale(v, 1.0f/s);
  596. }
  597. /*inline Vector4 add_scaled(Vector4::Arg a, Vector4::Arg b, float s)
  598. {
  599. return Vector4(a.x + b.x * s, a.y + b.y * s, a.z + b.z * s, a.w + b.w * s);
  600. }*/
  601. inline Vector4 lerp(Vector4::Arg v1, Vector4::Arg v2, float t)
  602. {
  603. const float s = 1.0f - t;
  604. return Vector4(v1.x * s + t * v2.x, v1.y * s + t * v2.y, v1.z * s + t * v2.z, v1.w * s + t * v2.w);
  605. }
  606. inline float dot(Vector4::Arg a, Vector4::Arg b)
  607. {
  608. return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  609. }
  610. inline float lengthSquared(Vector4::Arg v)
  611. {
  612. return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
  613. }
  614. inline float length(Vector4::Arg v)
  615. {
  616. return sqrtf(lengthSquared(v));
  617. }
  618. inline float inverseLength(Vector4::Arg v)
  619. {
  620. return 1.0f / sqrtf(lengthSquared(v));
  621. }
  622. inline bool isNormalized(Vector4::Arg v, float epsilon = NV_NORMAL_EPSILON)
  623. {
  624. return equal(length(v), 1, epsilon);
  625. }
  626. inline Vector4 normalize(Vector4::Arg v, float epsilon = NV_EPSILON)
  627. {
  628. float l = length(v);
  629. NV_UNUSED(epsilon);
  630. nvDebugCheck(!isZero(l, epsilon));
  631. Vector4 n = scale(v, 1.0f / l);
  632. nvDebugCheck(isNormalized(n));
  633. return n;
  634. }
  635. inline Vector4 normalizeSafe(Vector4::Arg v, Vector4::Arg fallback, float epsilon = NV_EPSILON)
  636. {
  637. float l = length(v);
  638. if (isZero(l, epsilon)) {
  639. return fallback;
  640. }
  641. return scale(v, 1.0f / l);
  642. }
  643. // Safe, branchless normalization from Andy Firth. All error checking ommitted.
  644. // http://altdevblogaday.com/2011/08/21/practical-flt-point-tricks/
  645. inline Vector4 normalizeFast(Vector4::Arg v)
  646. {
  647. const float very_small_float = 1.0e-037f;
  648. float l = very_small_float + length(v);
  649. return scale(v, 1.0f / l);
  650. }
  651. inline bool equal(Vector4::Arg v1, Vector4::Arg v2, float epsilon = NV_EPSILON)
  652. {
  653. return equal(v1.x, v2.x, epsilon) && equal(v1.y, v2.y, epsilon) && equal(v1.z, v2.z, epsilon) && equal(v1.w, v2.w, epsilon);
  654. }
  655. inline Vector4 min(Vector4::Arg a, Vector4::Arg b)
  656. {
  657. return Vector4(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
  658. }
  659. inline Vector4 max(Vector4::Arg a, Vector4::Arg b)
  660. {
  661. return Vector4(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w));
  662. }
  663. inline Vector4 clamp(Vector4::Arg v, float min, float max)
  664. {
  665. return Vector4(clamp(v.x, min, max), clamp(v.y, min, max), clamp(v.z, min, max), clamp(v.w, min, max));
  666. }
  667. inline Vector4 saturate(Vector4::Arg v)
  668. {
  669. return Vector4(saturate(v.x), saturate(v.y), saturate(v.z), saturate(v.w));
  670. }
  671. inline bool isFinite(Vector4::Arg v)
  672. {
  673. return isFinite(v.x) && isFinite(v.y) && isFinite(v.z) && isFinite(v.w);
  674. }
  675. inline Vector4 validate(Vector4::Arg v, Vector4::Arg fallback = Vector4(0.0f))
  676. {
  677. if (!isFinite(v)) return fallback;
  678. Vector4 vf = v;
  679. nv::floatCleanup(vf.component, 4);
  680. return vf;
  681. }
  682. template <>
  683. inline uint hash(const Vector4 & v, uint h)
  684. {
  685. return sdbmFloatHash(v.component, 4, h);
  686. }
  687. #if NV_OS_IOS // LLVM is not happy with implicit conversion of immediate constants to float
  688. //int:
  689. inline Vector2 scale(Vector2::Arg v, int s)
  690. {
  691. return Vector2(v.x * s, v.y * s);
  692. }
  693. inline Vector2 operator*(Vector2::Arg v, int s)
  694. {
  695. return scale(v, s);
  696. }
  697. inline Vector2 operator*(int s, Vector2::Arg v)
  698. {
  699. return scale(v, s);
  700. }
  701. inline Vector2 operator/(Vector2::Arg v, int s)
  702. {
  703. return scale(v, 1.0f/s);
  704. }
  705. inline Vector3 scale(Vector3::Arg v, int s)
  706. {
  707. return Vector3(v.x * s, v.y * s, v.z * s);
  708. }
  709. inline Vector3 operator*(Vector3::Arg v, int s)
  710. {
  711. return scale(v, s);
  712. }
  713. inline Vector3 operator*(int s, Vector3::Arg v)
  714. {
  715. return scale(v, s);
  716. }
  717. inline Vector3 operator/(Vector3::Arg v, int s)
  718. {
  719. return scale(v, 1.0f/s);
  720. }
  721. inline Vector4 scale(Vector4::Arg v, int s)
  722. {
  723. return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
  724. }
  725. inline Vector4 operator*(Vector4::Arg v, int s)
  726. {
  727. return scale(v, s);
  728. }
  729. inline Vector4 operator*(int s, Vector4::Arg v)
  730. {
  731. return scale(v, s);
  732. }
  733. inline Vector4 operator/(Vector4::Arg v, int s)
  734. {
  735. return scale(v, 1.0f/s);
  736. }
  737. //double:
  738. inline Vector3 operator*(Vector3::Arg v, double s)
  739. {
  740. return scale(v, (float)s);
  741. }
  742. inline Vector3 operator*(double s, Vector3::Arg v)
  743. {
  744. return scale(v, (float)s);
  745. }
  746. inline Vector3 operator/(Vector3::Arg v, double s)
  747. {
  748. return scale(v, 1.f/((float)s));
  749. }
  750. #endif //NV_OS_IOS
  751. } // nv namespace
  752. #endif // NV_MATH_VECTOR_INL