/geogebra/geogebra/kernel/Matrix/Coords3D.java

https://github.com/ancsing/ggw · Java · 412 lines · 99 code · 34 blank · 279 comment · 23 complexity · ae34ba8b7cccd5e63263a05620e029fb MD5 · raw file

  1. package geogebra.kernel.Matrix;
  2. public class Coords3D{
  3. double[] val = new double[4];
  4. private double norm, sqNorm;
  5. private boolean calcNorm = true;
  6. private boolean calcSqNorm = true;
  7. public Coords3D(double x, double y, double z, double w) {
  8. val[0]=x; val[1]=y; val[2]=z; val[3]=w;
  9. }
  10. public Coords3D(int i) {
  11. val[0]=val[1]=val[2]=val[3]=0;
  12. }
  13. ///////////////////////////////////////////////////:
  14. //setters and getters
  15. /** sets v(i) to val0
  16. * @param i number of the row
  17. * @param val0 value
  18. */
  19. public void set(int i, double val0){
  20. if(i==1) val[0]=val0;
  21. if(i==2) val[1]=val0;
  22. if(i==3) val[2]=val0;
  23. if(i==4) val[3]=val0;
  24. calcNorm=calcSqNorm=true;
  25. }
  26. /** sets v to vals0
  27. * @param vals0 values {x1, x2, ...}
  28. */
  29. public void set(double[] vals0){
  30. val[0]=vals0[0];
  31. val[1]=vals0[1];
  32. val[2]=vals0[2];
  33. val[3]=vals0[3];
  34. calcNorm=calcSqNorm=true;
  35. }
  36. /** returns v(i)
  37. * @param i number of the row
  38. * @return value*/
  39. public double get(int i){
  40. if(i==0) return val[0];
  41. if(i==1) return val[1];
  42. if(i==2) return val[2];
  43. else return val[3];
  44. }
  45. /** returns v "val[0]-coord"
  46. * @return val[0]-coord*/
  47. public double getX(){ return val[0]; }
  48. /** returns v "val[1]-coord"
  49. * @return val[1]-coord*/
  50. public double getY(){ return val[1]; }
  51. /** returns v "val[2]-coord"
  52. * @return val[2]-coord*/
  53. public double getZ(){ return val[2]; }
  54. /** returns v "val[3]-coord"
  55. * @return val[3]-coord*/
  56. public double getW(){ return val[3]; }
  57. /** sets the "val[0]-coord"
  58. * @param val
  59. */
  60. public void setX(double v){ val[0]=v; calcNorm=calcSqNorm=true; }
  61. /** sets the "val[1]-coord"
  62. * @param val
  63. */
  64. public void setY(double v){ val[1]=v; calcNorm=calcSqNorm=true; }
  65. /** sets the "val[2]-coord"
  66. * @param val
  67. */
  68. public void setZ(double v){ val[2]=v; calcNorm=calcSqNorm=true; }
  69. /** sets the "val[3]-coord"
  70. * @param val
  71. */
  72. public void setW(double v){ val[3]=v; calcNorm=calcSqNorm=true; }
  73. ///////////////////////////////////////////////////:
  74. //basic operations
  75. /** returns dot product this * v.
  76. * <p>
  77. * If this={x1,x2,...} and v={val[0]'1,val[0]'2,...}, the dot product is x1*val[0]'1+x2*val[0]'2+...
  78. * @param v vector multiplied with
  79. * @return value of the dot product*/
  80. public double dotproduct(Coords3D a){
  81. return val[0]*a.val[0]+val[1]*a.val[1]+val[2]*a.val[2];
  82. }
  83. /** returns cross product this * v.
  84. * Attempt that the two vectors are of dimension 3.
  85. * <p>
  86. * If this={val[0],val[1],val[2]} and v={val[0]',val[1]',val[2]'}, then cross product={yz'-val[1]'val[2],zx'-val[2]'val[0],xy'-yx'}
  87. * @param v vector multiplied with
  88. * @return vector resulting of the cross product
  89. */
  90. public Coords3D crossProduct(Coords3D a){
  91. return new Coords3D(val[1]*a.val[2]-val[2]*a.val[1],val[2]*a.val[0]-val[0]*a.val[2],
  92. val[0]*a.val[1]-val[1]*a.val[0],0);
  93. }
  94. /** returns the scalar norm.
  95. * <p>
  96. * If this={x1,x2,...}, then norm=sqrt(x1*x1+x2*x2+...).
  97. * Same result as Math.sqrt(this.dotproduct(this))
  98. * @return the scalar norm*/
  99. public double norm(){
  100. if(calcNorm){
  101. norm=Math.sqrt(val[0]*val[0]+val[1]*val[1]+val[2]*val[2]);
  102. calcNorm=false;
  103. }
  104. return norm;
  105. }
  106. /** returns the square of the scalar norm.
  107. * <p>
  108. * If this={x1,x2,...}, then norm=x1*x1+x2*x2+...
  109. * Same result as this.dotproduct(this)
  110. * @return the scalar norm*/
  111. public double squareNorm(){
  112. if(calcSqNorm){
  113. sqNorm=val[0]*val[0]+val[1]*val[1]+val[2]*val[2];
  114. calcSqNorm=false;
  115. }
  116. return sqNorm;
  117. }
  118. /** returns this normalized
  119. * @return this/this.norm()
  120. */
  121. public Coords3D normalized(){
  122. double inv;
  123. if(calcNorm)
  124. inv=1/Math.sqrt(val[0]*val[0]+val[1]*val[1]+val[2]*val[2]);
  125. else
  126. inv=1/norm;
  127. return new Coords3D(val[0]*inv, val[1]*inv, val[2]*inv, val[3]*inv);
  128. }
  129. /** normalize this */
  130. public Coords3D normalize(){
  131. double inv;
  132. if(calcNorm)
  133. inv=1/Math.sqrt(val[0]*val[0]+val[1]*val[1]+val[2]*val[2]);
  134. else
  135. inv=1/norm;
  136. val[0]*=inv;
  137. val[1]*=inv;
  138. val[2]*=inv;
  139. norm=sqNorm=1.0;
  140. return this;
  141. }
  142. /** returns this-v
  143. * @param v vector subtracted
  144. * @return this-v
  145. */
  146. public Coords3D sub(Coords3D v){
  147. return new Coords3D(val[0]-v.val[0],val[1]-v.val[1],val[2]-v.val[2],0);
  148. }
  149. /** returns this-v
  150. * @param v vector subtracted
  151. * @return this-v
  152. */
  153. public Coords3D add(Coords3D v){
  154. return new Coords3D(val[0]+v.val[0],val[1]+v.val[1],val[2]+v.val[2],0);
  155. }
  156. /**
  157. * @return
  158. */
  159. public boolean isDefined() {
  160. return !(val[0]!=val[0] || val[1]!=val[1] || val[2]!=val[2]);
  161. }
  162. /** returns a copy of the vector
  163. * @return a copy of the vector
  164. */
  165. public Coords3D copyVector(){
  166. return new Coords3D(val[0],val[1],val[2],val[3]);
  167. }
  168. public boolean isFinite() {
  169. return !((val[0] == Double.POSITIVE_INFINITY) || (val[0] == Double.NEGATIVE_INFINITY) ||
  170. (val[1] == Double.POSITIVE_INFINITY) || (val[1] == Double.NEGATIVE_INFINITY) ||
  171. (val[2] == Double.POSITIVE_INFINITY) || (val[2] == Double.NEGATIVE_INFINITY));
  172. }
  173. }
  174. //package geogebra.Matrix;
  175. //
  176. //import geogebra.kernel.Kernel;
  177. //
  178. //public class GgbVector3D extends GgbVector{
  179. // private double norm, sqNorm;
  180. // private boolean calcNorm = true;
  181. // private boolean calcSqNorm = true;
  182. //
  183. // public GgbVector3D(double x, double y, double z, double w) {
  184. // super(x,y,z,w);
  185. // val[0]=x; val[1]=y; val[2]=z; val[3]=w;
  186. // }
  187. //
  188. // public GgbVector3D(int i) {
  189. // super(i);
  190. // }
  191. //
  192. // ///////////////////////////////////////////////////:
  193. // //setters and getters
  194. // /** sets v(i) to val0
  195. // * @param i number of the row
  196. // * @param val0 value
  197. // */
  198. // public void set(int i, double val0){
  199. // if(i==0) val[0]=val0;
  200. // if(i==1) val[1]=val0;
  201. // if(i==2) val[2]=val0;
  202. // calcNorm=calcSqNorm=true;
  203. // }
  204. //
  205. // /** sets v to vals0
  206. // * @param vals0 values {x1, x2, ...}
  207. // */
  208. // public void set(double[] vals0){
  209. // val[0]=vals0[0];
  210. // val[1]=vals0[1];
  211. // val[2]=vals0[2];
  212. // val[3]=vals0[3];
  213. // calcNorm=calcSqNorm=true;
  214. // }
  215. //
  216. // /** returns v(i)
  217. // * @param i number of the row
  218. // * @return value*/
  219. // public double get(int i){
  220. // if(i==0) return val[0];
  221. // if(i==1) return val[1];
  222. // if(i==2) return val[2];
  223. // else return val[3];
  224. // }
  225. //
  226. // /** returns v "val[0]-coord"
  227. // * @return val[0]-coord*/
  228. // public double getX(){ return val[0]; }
  229. //
  230. // /** returns v "val[1]-coord"
  231. // * @return val[1]-coord*/
  232. // public double getY(){ return val[1]; }
  233. //
  234. // /** returns v "val[2]-coord"
  235. // * @return val[2]-coord*/
  236. // public double getZ(){ return val[2]; }
  237. //
  238. // /** returns v "val[3]-coord"
  239. // * @return val[3]-coord*/
  240. // public double getW(){ return val[3]; }
  241. //
  242. // /** sets the "val[0]-coord"
  243. // * @param val
  244. // */
  245. // public void setX(double v){ val[0]=v; calcNorm=calcSqNorm=true; }
  246. //
  247. // /** sets the "val[1]-coord"
  248. // * @param val
  249. // */
  250. // public void setY(double v){ val[1]=v; calcNorm=calcSqNorm=true; }
  251. //
  252. // /** sets the "val[2]-coord"
  253. // * @param val
  254. // */
  255. // public void setZ(double v){ val[2]=v; calcNorm=calcSqNorm=true; }
  256. //
  257. // /** sets the "val[3]-coord"
  258. // * @param val
  259. // */
  260. // public void setW(double v){ val[3]=v; calcNorm=calcSqNorm=true; }
  261. //
  262. // ///////////////////////////////////////////////////:
  263. // //basic operations
  264. //
  265. // /** returns dot product this * v.
  266. // * <p>
  267. // * If this={x1,x2,...} and v={val[0]'1,val[0]'2,...}, the dot product is x1*val[0]'1+x2*val[0]'2+...
  268. // * @param v vector multiplied with
  269. // * @return value of the dot product*/
  270. // public double dotproduct(GgbVector3D a){
  271. // return val[0]*a.val[0]+val[1]*a.val[1]+val[2]*a.val[2];
  272. // }
  273. //
  274. // /** returns cross product this * v.
  275. // * Attempt that the two vectors are of dimension 3.
  276. // * <p>
  277. // * If this={val[0],val[1],val[2]} and v={val[0]',val[1]',val[2]'}, then cross product={yz'-val[1]'val[2],zx'-val[2]'val[0],xy'-yx'}
  278. // * @param v vector multiplied with
  279. // * @return vector resulting of the cross product
  280. // */
  281. // public GgbVector3D crossProduct(GgbVector3D a){
  282. // return new GgbVector3D(val[1]*a.val[2]-val[2]*a.val[1],val[2]*a.val[0]-val[0]*a.val[2],
  283. // val[0]*a.val[1]-val[1]*a.val[0],0);
  284. // }
  285. //
  286. //
  287. //
  288. // /** returns the scalar norm.
  289. // * <p>
  290. // * If this={x1,x2,...}, then norm=sqrt(x1*x1+x2*x2+...).
  291. // * Same result as Math.sqrt(this.dotproduct(this))
  292. // * @return the scalar norm*/
  293. // public double norm(){
  294. // if(calcNorm){
  295. // norm=Math.sqrt(val[0]*val[0]+val[1]*val[1]+val[2]*val[2]);
  296. // calcNorm=false;
  297. // }
  298. // return norm;
  299. // }
  300. //
  301. // /** returns the square of the scalar norm.
  302. // * <p>
  303. // * If this={x1,x2,...}, then norm=x1*x1+x2*x2+...
  304. // * Same result as this.dotproduct(this)
  305. // * @return the scalar norm*/
  306. // public double squareNorm(){
  307. // if(calcSqNorm){
  308. // sqNorm=val[0]*val[0]+val[1]*val[1]+val[2]*val[2];
  309. // calcSqNorm=false;
  310. // }
  311. // return sqNorm;
  312. // }
  313. //
  314. // /** returns this normalized
  315. // * @return this/this.norm()
  316. // */
  317. // public GgbVector3D normalized(){
  318. //
  319. // double inv;
  320. // if(calcNorm)
  321. // inv=1/Math.sqrt(val[0]*val[0]+val[1]*val[1]+val[2]*val[2]);
  322. // else
  323. // inv=1/norm;
  324. // return new GgbVector3D(val[0]*inv, val[1]*inv, val[2]*inv, val[3]*inv);
  325. // }
  326. //
  327. //
  328. // /** normalize this */
  329. // public GgbVector3D normalize(){
  330. // double inv;
  331. // if(calcNorm)
  332. // inv=1/Math.sqrt(val[0]*val[0]+val[1]*val[1]+val[2]*val[2]);
  333. // else
  334. // inv=1/norm;
  335. // val[0]*=inv;
  336. // val[1]*=inv;
  337. // val[2]*=inv;
  338. // norm=sqNorm=1.0;
  339. // return this;
  340. // }
  341. //
  342. // /** returns this-v
  343. // * @param v vector subtracted
  344. // * @return this-v
  345. // */
  346. // public GgbVector3D sub(GgbVector3D v){
  347. // return new GgbVector3D(val[0]-v.val[0],val[1]-v.val[1],val[2]-v.val[2],0);
  348. // }
  349. //
  350. // /** returns this-v
  351. // * @param v vector subtracted
  352. // * @return this-v
  353. // */
  354. // public GgbVector3D add(GgbVector3D v){
  355. // return new GgbVector3D(val[0]+v.val[0],val[1]+v.val[1],val[2]+v.val[2],0);
  356. // }
  357. //
  358. // /**
  359. // * @return
  360. // */
  361. // public boolean isDefined() {
  362. // return !(val[0]!=val[0] || val[1]!=val[1] || val[2]!=val[2]);
  363. // }
  364. //
  365. // /** returns a copy of the vector
  366. // * @return a copy of the vector
  367. // */
  368. // public GgbVector3D copyVector(){
  369. // return new GgbVector3D(val[0],val[1],val[2],val[3]);
  370. //
  371. // }
  372. //
  373. // public boolean isFinite() {
  374. // return !((val[0] == Double.POSITIVE_INFINITY) || (val[0] == Double.NEGATIVE_INFINITY) ||
  375. // (val[1] == Double.POSITIVE_INFINITY) || (val[1] == Double.NEGATIVE_INFINITY) ||
  376. // (val[2] == Double.POSITIVE_INFINITY) || (val[2] == Double.NEGATIVE_INFINITY));
  377. // }
  378. //}