PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/tests/v3math_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 585 lines | 507 code | 51 blank | 27 comment | 175 complexity | 2b1aba8cbeb3e3aa6deffe88139a33cc MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file v3math_test.cpp
  3. * @author Adroit
  4. * @date 2007-02
  5. * @brief v3math test cases.
  6. *
  7. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #include "linden_common.h"
  29. #include "../test/lltut.h"
  30. #include "llsd.h"
  31. #include "../v3dmath.h"
  32. #include "../m3math.h"
  33. #include "../v4math.h"
  34. #include "../v3math.h"
  35. #include "../llquaternion.h"
  36. #include "../llquantize.h"
  37. namespace tut
  38. {
  39. struct v3math_data
  40. {
  41. };
  42. typedef test_group<v3math_data> v3math_test;
  43. typedef v3math_test::object v3math_object;
  44. tut::v3math_test v3math_testcase("v3math_h");
  45. template<> template<>
  46. void v3math_object::test<1>()
  47. {
  48. LLVector3 vec3;
  49. ensure("1:LLVector3:Fail to initialize ", ((0.f == vec3.mV[VX]) && (0.f == vec3.mV[VY]) && (0.f == vec3.mV[VZ])));
  50. F32 x = 2.32f, y = 1.212f, z = -.12f;
  51. LLVector3 vec3a(x,y,z);
  52. ensure("2:LLVector3:Fail to initialize ", ((2.32f == vec3a.mV[VX]) && (1.212f == vec3a.mV[VY]) && (-.12f == vec3a.mV[VZ])));
  53. const F32 vec[3] = {1.2f ,3.2f, -4.2f};
  54. LLVector3 vec3b(vec);
  55. ensure("3:LLVector3:Fail to initialize ", ((1.2f == vec3b.mV[VX]) && (3.2f == vec3b.mV[VY]) && (-4.2f == vec3b.mV[VZ])));
  56. }
  57. template<> template<>
  58. void v3math_object::test<2>()
  59. {
  60. F32 x = 2.32f, y = 1.212f, z = -.12f;
  61. LLVector3 vec3(x,y,z);
  62. LLVector3d vector3d(vec3);
  63. LLVector3 vec3a(vector3d);
  64. ensure("1:LLVector3:Fail to initialize ", vec3 == vec3a);
  65. LLVector4 vector4(vec3);
  66. LLVector3 vec3b(vector4);
  67. ensure("2:LLVector3:Fail to initialize ", vec3 == vec3b);
  68. }
  69. template<> template<>
  70. void v3math_object::test<3>()
  71. {
  72. S32 a = 231;
  73. LLSD llsd(a);
  74. LLVector3 vec3(llsd);
  75. LLSD sd = vec3.getValue();
  76. LLVector3 vec3a(sd);
  77. ensure("1:LLVector3:Fail to initialize ", (vec3 == vec3a));
  78. }
  79. template<> template<>
  80. void v3math_object::test<4>()
  81. {
  82. S32 a = 231;
  83. LLSD llsd(a);
  84. LLVector3 vec3(llsd),vec3a;
  85. vec3a = vec3;
  86. ensure("1:Operator= Fail to initialize " ,(vec3 == vec3a));
  87. }
  88. template<> template<>
  89. void v3math_object::test<5>()
  90. {
  91. F32 x = 2.32f, y = 1.212f, z = -.12f;
  92. LLVector3 vec3(x,y,z);
  93. ensure("1:isFinite= Fail to initialize ", (TRUE == vec3.isFinite()));//need more test cases:
  94. vec3.clearVec();
  95. ensure("2:clearVec:Fail to set values ", ((0.f == vec3.mV[VX]) && (0.f == vec3.mV[VY]) && (0.f == vec3.mV[VZ])));
  96. vec3.setVec(x,y,z);
  97. ensure("3:setVec:Fail to set values ", ((2.32f == vec3.mV[VX]) && (1.212f == vec3.mV[VY]) && (-.12f == vec3.mV[VZ])));
  98. vec3.zeroVec();
  99. ensure("4:zeroVec:Fail to set values ", ((0.f == vec3.mV[VX]) && (0.f == vec3.mV[VY]) && (0.f == vec3.mV[VZ])));
  100. }
  101. template<> template<>
  102. void v3math_object::test<6>()
  103. {
  104. F32 x = 2.32f, y = 1.212f, z = -.12f;
  105. LLVector3 vec3(x,y,z),vec3a;
  106. vec3.abs();
  107. ensure("1:abs:Fail ", ((x == vec3.mV[VX]) && (y == vec3.mV[VY]) && (-z == vec3.mV[VZ])));
  108. vec3a.setVec(vec3);
  109. ensure("2:setVec:Fail to initialize ", (vec3a == vec3));
  110. const F32 vec[3] = {1.2f ,3.2f, -4.2f};
  111. vec3.clearVec();
  112. vec3.setVec(vec);
  113. ensure("3:setVec:Fail to initialize ", ((1.2f == vec3.mV[VX]) && (3.2f == vec3.mV[VY]) && (-4.2f == vec3.mV[VZ])));
  114. vec3a.clearVec();
  115. LLVector3d vector3d(vec3);
  116. vec3a.setVec(vector3d);
  117. ensure("4:setVec:Fail to initialize ", (vec3 == vec3a));
  118. LLVector4 vector4(vec3);
  119. vec3a.clearVec();
  120. vec3a.setVec(vector4);
  121. ensure("5:setVec:Fail to initialize ", (vec3 == vec3a));
  122. }
  123. template<> template<>
  124. void v3math_object::test<7>()
  125. {
  126. F32 x = 2.32f, y = 3.212f, z = -.12f;
  127. F32 min = 0.0001f, max = 3.0f;
  128. LLVector3 vec3(x,y,z);
  129. ensure("1:clamp:Fail ", TRUE == vec3.clamp(min, max) && x == vec3.mV[VX] && max == vec3.mV[VY] && min == vec3.mV[VZ]);
  130. x = 1.f, y = 2.2f, z = 2.8f;
  131. vec3.setVec(x,y,z);
  132. ensure("2:clamp:Fail ", FALSE == vec3.clamp(min, max));
  133. }
  134. template<> template<>
  135. void v3math_object::test<8>()
  136. {
  137. F32 x = 2.32f, y = 1.212f, z = -.12f;
  138. LLVector3 vec3(x,y,z);
  139. ensure("1:magVecSquared:Fail ", is_approx_equal(vec3.magVecSquared(), (x*x + y*y + z*z)));
  140. ensure("2:magVec:Fail ", is_approx_equal(vec3.magVec(), (F32) sqrt(x*x + y*y + z*z)));
  141. }
  142. template<> template<>
  143. void v3math_object::test<9>()
  144. {
  145. F32 x =-2.0f, y = -3.0f, z = 1.23f ;
  146. LLVector3 vec3(x,y,z);
  147. ensure("1:abs():Fail ", (TRUE == vec3.abs()));
  148. ensure("2:isNull():Fail", (FALSE == vec3.isNull())); //Returns TRUE if vector has a _very_small_ length
  149. x =.00000001f, y = .000001001f, z = .000001001f;
  150. vec3.setVec(x,y,z);
  151. ensure("3:isNull(): Fail ", (TRUE == vec3.isNull()));
  152. }
  153. template<> template<>
  154. void v3math_object::test<10>()
  155. {
  156. F32 x =-2.0f, y = -3.0f, z = 1.f ;
  157. LLVector3 vec3(x,y,z),vec3a;
  158. ensure("1:isExactlyZero():Fail ", (TRUE == vec3a.isExactlyZero()));
  159. vec3a = vec3a.scaleVec(vec3);
  160. ensure("2:scaleVec: Fail ", vec3a.mV[VX] == 0.f && vec3a.mV[VY] == 0.f && vec3a.mV[VZ] == 0.f);
  161. vec3a.setVec(x,y,z);
  162. vec3a = vec3a.scaleVec(vec3);
  163. ensure("3:scaleVec: Fail ", ((4 == vec3a.mV[VX]) && (9 == vec3a.mV[VY]) &&(1 == vec3a.mV[VZ])));
  164. ensure("4:isExactlyZero():Fail ", (FALSE == vec3.isExactlyZero()));
  165. }
  166. template<> template<>
  167. void v3math_object::test<11>()
  168. {
  169. F32 x =20.0f, y = 30.0f, z = 15.f ;
  170. F32 angle = 100.f;
  171. LLVector3 vec3(x,y,z),vec3a(1.f,2.f,3.f);
  172. vec3a = vec3a.rotVec(angle, vec3);
  173. LLVector3 vec3b(1.f,2.f,3.f);
  174. vec3b = vec3b.rotVec(angle, vec3);
  175. ensure_equals("rotVec():Fail" ,vec3b,vec3a);
  176. }
  177. template<> template<>
  178. void v3math_object::test<12>()
  179. {
  180. F32 x =-2.0f, y = -3.0f, z = 1.f ;
  181. LLVector3 vec3(x,y,z);
  182. ensure("1:operator [] failed",( x == vec3[0]));
  183. ensure("2:operator [] failed",( y == vec3[1]));
  184. ensure("3:operator [] failed",( z == vec3[2]));
  185. vec3.clearVec();
  186. x = 23.f, y = -.2361f, z = 3.25;
  187. vec3.setVec(x,y,z);
  188. F32 &ref1 = vec3[0];
  189. ensure("4:operator [] failed",( ref1 == vec3[0]));
  190. F32 &ref2 = vec3[1];
  191. ensure("5:operator [] failed",( ref2 == vec3[1]));
  192. F32 &ref3 = vec3[2];
  193. ensure("6:operator [] failed",( ref3 == vec3[2]));
  194. }
  195. template<> template<>
  196. void v3math_object::test<13>()
  197. {
  198. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f, x2 = -2.3f, y2 = 1.11f, z2 = 1234.234f;
  199. F32 val1, val2, val3;
  200. LLVector3 vec3(x1,y1,z1), vec3a(x2,y2,z2), vec3b;
  201. vec3b = vec3 + vec3a ;
  202. val1 = x1+x2;
  203. val2 = y1+y2;
  204. val3 = z1+z2;
  205. ensure("1:operator+ failed",(val1 == vec3b.mV[VX]) && (val2 == vec3b.mV[VY]) && (val3 == vec3b.mV[VZ]));
  206. vec3.clearVec();
  207. vec3a.clearVec();
  208. vec3b.clearVec();
  209. x1 = -.235f, y1 = -24.32f,z1 = 2.13f, x2 = -2.3f, y2 = 1.f, z2 = 34.21f;
  210. vec3.setVec(x1,y1,z1);
  211. vec3a.setVec(x2,y2,z2);
  212. vec3b = vec3 + vec3a;
  213. val1 = x1+x2;
  214. val2 = y1+y2;
  215. val3 = z1+z2;
  216. ensure("2:operator+ failed",(val1 == vec3b.mV[VX]) && (val2 == vec3b.mV[VY]) && (val3 == vec3b.mV[VZ]));
  217. }
  218. template<> template<>
  219. void v3math_object::test<14>()
  220. {
  221. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f, x2 = -2.3f, y2 = 1.11f, z2 = 1234.234f;
  222. F32 val1, val2, val3;
  223. LLVector3 vec3(x1,y1,z1), vec3a(x2,y2,z2), vec3b;
  224. vec3b = vec3 - vec3a ;
  225. val1 = x1-x2;
  226. val2 = y1-y2;
  227. val3 = z1-z2;
  228. ensure("1:operator- failed",(val1 == vec3b.mV[VX]) && (val2 == vec3b.mV[VY]) && (val3 == vec3b.mV[VZ]));
  229. vec3.clearVec();
  230. vec3a.clearVec();
  231. vec3b.clearVec();
  232. x1 = -.235f, y1 = -24.32f,z1 = 2.13f, x2 = -2.3f, y2 = 1.f, z2 = 34.21f;
  233. vec3.setVec(x1,y1,z1);
  234. vec3a.setVec(x2,y2,z2);
  235. vec3b = vec3 - vec3a;
  236. val1 = x1-x2;
  237. val2 = y1-y2;
  238. val3 = z1-z2;
  239. ensure("2:operator- failed",(val1 == vec3b.mV[VX]) && (val2 == vec3b.mV[VY]) && (val3 == vec3b.mV[VZ]));
  240. }
  241. template<> template<>
  242. void v3math_object::test<15>()
  243. {
  244. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f, x2 = -2.3f, y2 = 1.11f, z2 = 1234.234f;
  245. F32 val1, val2, val3;
  246. LLVector3 vec3(x1,y1,z1), vec3a(x2,y2,z2);
  247. val1 = vec3 * vec3a;
  248. val2 = x1*x2 + y1*y2 + z1*z2;
  249. ensure_equals("1:operator* failed",val1,val2);
  250. vec3a.clearVec();
  251. F32 mulVal = 4.332f;
  252. vec3a = vec3 * mulVal;
  253. val1 = x1*mulVal;
  254. val2 = y1*mulVal;
  255. val3 = z1*mulVal;
  256. ensure("2:operator* failed",(val1 == vec3a.mV[VX]) && (val2 == vec3a.mV[VY])&& (val3 == vec3a.mV[VZ]));
  257. vec3a.clearVec();
  258. vec3a = mulVal * vec3;
  259. ensure("3:operator* failed ", (val1 == vec3a.mV[VX]) && (val2 == vec3a.mV[VY])&& (val3 == vec3a.mV[VZ]));
  260. }
  261. template<> template<>
  262. void v3math_object::test<16>()
  263. {
  264. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f, x2 = -2.3f, y2 = 1.11f, z2 = 1234.234f;
  265. F32 val1, val2, val3;
  266. LLVector3 vec3(x1,y1,z1), vec3a(x2,y2,z2), vec3b;
  267. vec3b = vec3 % vec3a ;
  268. val1 = y1*z2 - y2*z1;
  269. val2 = z1*x2 -z2*x1;
  270. val3 = x1*y2-x2*y1;
  271. ensure("1:operator% failed",(val1 == vec3b.mV[VX]) && (val2 == vec3b.mV[VY]) && (val3 == vec3b.mV[VZ]));
  272. vec3.clearVec();
  273. vec3a.clearVec();
  274. vec3b.clearVec();
  275. x1 =112.f, y1 = 22.3f,z1 = 1.2f, x2 = -2.3f, y2 = 341.11f, z2 = 1234.234f;
  276. vec3.setVec(x1,y1,z1);
  277. vec3a.setVec(x2,y2,z2);
  278. vec3b = vec3 % vec3a ;
  279. val1 = y1*z2 - y2*z1;
  280. val2 = z1*x2 -z2*x1;
  281. val3 = x1*y2-x2*y1;
  282. ensure("2:operator% failed ", (val1 == vec3b.mV[VX]) && (val2 == vec3b.mV[VY]) && (val3 == vec3b.mV[VZ]));
  283. }
  284. template<> template<>
  285. void v3math_object::test<17>()
  286. {
  287. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f, div = 3.2f;
  288. F32 t = 1.f / div, val1, val2, val3;
  289. LLVector3 vec3(x1,y1,z1), vec3a;
  290. vec3a = vec3 / div;
  291. val1 = x1 * t;
  292. val2 = y1 * t;
  293. val3 = z1 *t;
  294. ensure("1:operator/ failed",(val1 == vec3a.mV[VX]) && (val2 == vec3a.mV[VY]) && (val3 == vec3a.mV[VZ]));
  295. vec3a.clearVec();
  296. x1 = -.235f, y1 = -24.32f, z1 = .342f, div = -2.2f;
  297. t = 1.f / div;
  298. vec3.setVec(x1,y1,z1);
  299. vec3a = vec3 / div;
  300. val1 = x1 * t;
  301. val2 = y1 * t;
  302. val3 = z1 *t;
  303. ensure("2:operator/ failed",(val1 == vec3a.mV[VX]) && (val2 == vec3a.mV[VY]) && (val3 == vec3a.mV[VZ]));
  304. }
  305. template<> template<>
  306. void v3math_object::test<18>()
  307. {
  308. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f;
  309. LLVector3 vec3(x1,y1,z1), vec3a(x1,y1,z1);
  310. ensure("1:operator== failed",(vec3 == vec3a));
  311. vec3a.clearVec();
  312. x1 = -.235f, y1 = -24.32f, z1 = .342f;
  313. vec3.clearVec();
  314. vec3a.clearVec();
  315. vec3.setVec(x1,y1,z1);
  316. vec3a.setVec(x1,y1,z1);
  317. ensure("2:operator== failed ", (vec3 == vec3a));
  318. }
  319. template<> template<>
  320. void v3math_object::test<19>()
  321. {
  322. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f, x2 =112.f, y2 = 2.234f,z2 = 11.2f;;
  323. LLVector3 vec3(x1,y1,z1), vec3a(x2,y2,z2);
  324. ensure("1:operator!= failed",(vec3a != vec3));
  325. vec3.clearVec();
  326. vec3.clearVec();
  327. vec3a.setVec(vec3);
  328. ensure("2:operator!= failed", ( FALSE == (vec3a != vec3)));
  329. }
  330. template<> template<>
  331. void v3math_object::test<20>()
  332. {
  333. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f, x2 =112.f, y2 = 2.2f,z2 = 11.2f;;
  334. LLVector3 vec3(x1,y1,z1), vec3a(x2,y2,z2);
  335. vec3a += vec3;
  336. F32 val1, val2, val3;
  337. val1 = x1+x2;
  338. val2 = y1+y2;
  339. val3 = z1+z2;
  340. ensure("1:operator+= failed",(val1 == vec3a.mV[VX]) && (val2 == vec3a.mV[VY])&& (val3 == vec3a.mV[VZ]));
  341. }
  342. template<> template<>
  343. void v3math_object::test<21>()
  344. {
  345. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f, x2 =112.f, y2 = 2.2f,z2 = 11.2f;;
  346. LLVector3 vec3(x1,y1,z1), vec3a(x2,y2,z2);
  347. vec3a -= vec3;
  348. F32 val1, val2, val3;
  349. val1 = x2-x1;
  350. val2 = y2-y1;
  351. val3 = z2-z1;
  352. ensure("1:operator-= failed",(val1 == vec3a.mV[VX]) && (val2 == vec3a.mV[VY])&& (val3 == vec3a.mV[VZ]));
  353. }
  354. template<> template<>
  355. void v3math_object::test<22>()
  356. {
  357. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f, x2 = -2.3f, y2 = 1.11f, z2 = 1234.234f;
  358. F32 val1,val2,val3;
  359. LLVector3 vec3(x1,y1,z1), vec3a(x2,y2,z2);
  360. vec3a *= vec3;
  361. val1 = x1*x2;
  362. val2 = y1*y2;
  363. val3 = z1*z2;
  364. ensure("1:operator*= failed",(val1 == vec3a.mV[VX]) && (val2 == vec3a.mV[VY])&& (val3 == vec3a.mV[VZ]));
  365. F32 mulVal = 4.332f;
  366. vec3 *=mulVal;
  367. val1 = x1*mulVal;
  368. val2 = y1*mulVal;
  369. val3 = z1*mulVal;
  370. ensure("2:operator*= failed ", is_approx_equal(val1, vec3.mV[VX]) && is_approx_equal(val2, vec3.mV[VY]) && is_approx_equal(val3, vec3.mV[VZ]));
  371. }
  372. template<> template<>
  373. void v3math_object::test<23>()
  374. {
  375. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f, x2 = -2.3f, y2 = 1.11f, z2 = 1234.234f;
  376. LLVector3 vec3(x1,y1,z1), vec3a(x2,y2,z2),vec3b;
  377. vec3b = vec3a % vec3;
  378. vec3a %= vec3;
  379. ensure_equals("1:operator%= failed",vec3a,vec3b);
  380. }
  381. template<> template<>
  382. void v3math_object::test<24>()
  383. {
  384. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f, div = 3.2f;
  385. F32 t = 1.f / div, val1, val2, val3;
  386. LLVector3 vec3a(x1,y1,z1);
  387. vec3a /= div;
  388. val1 = x1 * t;
  389. val2 = y1 * t;
  390. val3 = z1 *t;
  391. ensure("1:operator/= failed",(val1 == vec3a.mV[VX]) && (val2 == vec3a.mV[VY]) && (val3 == vec3a.mV[VZ]));
  392. }
  393. template<> template<>
  394. void v3math_object::test<25>()
  395. {
  396. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f;
  397. LLVector3 vec3(x1,y1,z1), vec3a;
  398. vec3a = -vec3;
  399. ensure("1:operator- failed",(-vec3a == vec3));
  400. }
  401. template<> template<>
  402. void v3math_object::test<26>()
  403. {
  404. F32 x1 =1.f, y1 = 2.f,z1 = 1.2f;
  405. std::ostringstream stream1, stream2;
  406. LLVector3 vec3(x1,y1,z1), vec3a;
  407. stream1 << vec3;
  408. vec3a.setVec(x1,y1,z1);
  409. stream2 << vec3a;
  410. ensure("1:operator << failed",(stream1.str() == stream2.str()));
  411. }
  412. template<> template<>
  413. void v3math_object::test<27>()
  414. {
  415. F32 x1 =-2.3f, y1 = 2.f,z1 = 1.2f, x2 = 1.3f, y2 = 1.11f, z2 = 1234.234f;
  416. LLVector3 vec3(x1,y1,z1), vec3a(x2,y2,z2);
  417. ensure("1:operator< failed", (TRUE == (vec3 < vec3a)));
  418. x1 =-2.3f, y1 = 2.f,z1 = 1.2f, x2 = 1.3f, y2 = 2.f, z2 = 1234.234f;
  419. vec3.setVec(x1,y1,z1);
  420. vec3a.setVec(x2,y2,z2);
  421. ensure("2:operator< failed ", (TRUE == (vec3 < vec3a)));
  422. x1 =2.3f, y1 = 2.f,z1 = 1.2f, x2 = 1.3f,
  423. vec3.setVec(x1,y1,z1);
  424. vec3a.setVec(x2,y2,z2);
  425. ensure("3:operator< failed ", (FALSE == (vec3 < vec3a)));
  426. }
  427. template<> template<>
  428. void v3math_object::test<28>()
  429. {
  430. F32 x1 =1.23f, y1 = 2.f,z1 = 4.f;
  431. std::string buf("1.23 2. 4");
  432. LLVector3 vec3, vec3a(x1,y1,z1);
  433. LLVector3::parseVector3(buf, &vec3);
  434. ensure_equals("1:parseVector3 failed", vec3, vec3a);
  435. }
  436. template<> template<>
  437. void v3math_object::test<29>()
  438. {
  439. F32 x1 =1.f, y1 = 2.f,z1 = 4.f;
  440. LLVector3 vec3(x1,y1,z1),vec3a,vec3b;
  441. vec3a.setVec(1,1,1);
  442. vec3a.scaleVec(vec3);
  443. ensure_equals("1:scaleVec failed", vec3, vec3a);
  444. vec3a.clearVec();
  445. vec3a.setVec(x1,y1,z1);
  446. vec3a.scaleVec(vec3);
  447. ensure("2:scaleVec failed", ((1.f ==vec3a.mV[VX])&& (4.f ==vec3a.mV[VY]) && (16.f ==vec3a.mV[VZ])));
  448. }
  449. template<> template<>
  450. void v3math_object::test<30>()
  451. {
  452. F32 x1 =-2.3f, y1 = 2.f,z1 = 1.2f, x2 = 1.3f, y2 = 1.11f, z2 = 1234.234f;
  453. F32 val = 2.3f,val1,val2,val3;
  454. val1 = x1 + (x2 - x1)* val;
  455. val2 = y1 + (y2 - y1)* val;
  456. val3 = z1 + (z2 - z1)* val;
  457. LLVector3 vec3(x1,y1,z1),vec3a(x2,y2,z2);
  458. LLVector3 vec3b = lerp(vec3,vec3a,val);
  459. ensure("1:lerp failed", ((val1 ==vec3b.mV[VX])&& (val2 ==vec3b.mV[VY]) && (val3 ==vec3b.mV[VZ])));
  460. }
  461. template<> template<>
  462. void v3math_object::test<31>()
  463. {
  464. F32 x1 =-2.3f, y1 = 2.f,z1 = 1.2f, x2 = 1.3f, y2 = 1.f, z2 = 1.f;
  465. F32 val1,val2;
  466. LLVector3 vec3(x1,y1,z1),vec3a(x2,y2,z2);
  467. val1 = dist_vec(vec3,vec3a);
  468. val2 = (F32) sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2) + (z1 - z2)* (z1 -z2));
  469. ensure_equals("1:dist_vec: Fail ",val2, val1);
  470. val1 = dist_vec_squared(vec3,vec3a);
  471. val2 =((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2) + (z1 - z2)* (z1 -z2));
  472. ensure_equals("2:dist_vec_squared: Fail ",val2, val1);
  473. val1 = dist_vec_squared2D(vec3, vec3a);
  474. val2 =(x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2);
  475. ensure_equals("3:dist_vec_squared2D: Fail ",val2, val1);
  476. }
  477. template<> template<>
  478. void v3math_object::test<32>()
  479. {
  480. F32 x =12.3524f, y = -342.f,z = 4.126341f;
  481. LLVector3 vec3(x,y,z);
  482. F32 mag = vec3.normVec();
  483. mag = 1.f/ mag;
  484. F32 val1 = x* mag, val2 = y* mag, val3 = z* mag;
  485. ensure("1:normVec: Fail ", is_approx_equal(val1, vec3.mV[VX]) && is_approx_equal(val2, vec3.mV[VY]) && is_approx_equal(val3, vec3.mV[VZ]));
  486. x = 0.000000001f, y = 0.f, z = 0.f;
  487. vec3.clearVec();
  488. vec3.setVec(x,y,z);
  489. mag = vec3.normVec();
  490. val1 = x* mag, val2 = y* mag, val3 = z* mag;
  491. ensure("2:normVec: Fail ", (mag == 0.) && (0. == vec3.mV[VX]) && (0. == vec3.mV[VY])&& (0. == vec3.mV[VZ]));
  492. }
  493. template<> template<>
  494. void v3math_object::test<33>()
  495. {
  496. F32 x = -202.23412f, y = 123.2312f, z = -89.f;
  497. LLVector3 vec(x,y,z);
  498. vec.snap(2);
  499. ensure("1:snap: Fail ", is_approx_equal(-202.23f, vec.mV[VX]) && is_approx_equal(123.23f, vec.mV[VY]) && is_approx_equal(-89.f, vec.mV[VZ]));
  500. }
  501. template<> template<>
  502. void v3math_object::test<34>()
  503. {
  504. F32 x = 10.f, y = 20.f, z = -15.f;
  505. F32 x1, y1, z1;
  506. F32 lowerxy = 0.f, upperxy = 1.0f, lowerz = -1.0f, upperz = 1.f;
  507. LLVector3 vec3(x,y,z);
  508. vec3.quantize16(lowerxy,upperxy,lowerz,upperz);
  509. x1 = U16_to_F32(F32_to_U16(x, lowerxy, upperxy), lowerxy, upperxy);
  510. y1 = U16_to_F32(F32_to_U16(y, lowerxy, upperxy), lowerxy, upperxy);
  511. z1 = U16_to_F32(F32_to_U16(z, lowerz, upperz), lowerz, upperz);
  512. ensure("1:quantize16: Fail ", is_approx_equal(x1, vec3.mV[VX]) && is_approx_equal(y1, vec3.mV[VY]) && is_approx_equal(z1, vec3.mV[VZ]));
  513. LLVector3 vec3a(x,y,z);
  514. vec3a.quantize8(lowerxy,upperxy,lowerz,upperz);
  515. x1 = U8_to_F32(F32_to_U8(x, lowerxy, upperxy), lowerxy, upperxy);
  516. y1 = U8_to_F32(F32_to_U8(y, lowerxy, upperxy), lowerxy, upperxy);
  517. z1 = U8_to_F32(F32_to_U8(z, lowerz, upperz), lowerz, upperz);
  518. ensure("2:quantize8: Fail ", is_approx_equal(x1, vec3a.mV[VX]) && is_approx_equal(y1, vec3a.mV[VY]) && is_approx_equal(z1, vec3a.mV[VZ]));
  519. }
  520. template<> template<>
  521. void v3math_object::test<35>()
  522. {
  523. LLSD sd = LLSD::emptyArray();
  524. sd[0] = 1.f;
  525. LLVector3 parsed_1(sd);
  526. ensure("1:LLSD parse: Fail ", is_approx_equal(parsed_1.mV[VX], 1.f) && is_approx_equal(parsed_1.mV[VY], 0.f) && is_approx_equal(parsed_1.mV[VZ], 0.f));
  527. sd[1] = 2.f;
  528. LLVector3 parsed_2(sd);
  529. ensure("2:LLSD parse: Fail ", is_approx_equal(parsed_2.mV[VX], 1.f) && is_approx_equal(parsed_2.mV[VY], 2.f) && is_approx_equal(parsed_2.mV[VZ], 0.f));
  530. sd[2] = 3.f;
  531. LLVector3 parsed_3(sd);
  532. ensure("3:LLSD parse: Fail ", is_approx_equal(parsed_3.mV[VX], 1.f) && is_approx_equal(parsed_3.mV[VY], 2.f) && is_approx_equal(parsed_3.mV[VZ], 3.f));
  533. }
  534. }