Major cleanup of Utils class.
[trakem2.git] / ini / trakem2 / utils / Vector3.java
bloba56de8401aab71844bd54cb1bb1a022b822785ad
1 /**
2 * FROM : package org.freehep.graphics3d;
4 * Keeps a 3 dimensional vector.
5 * <p>
6 * Original ArcBall C code from Ken Shoemake, Graphics Gems IV, 1993.
8 * @author Mark Donszelmann
9 * @version $Id: Vector3.java,v 1.2 2002/06/12 23:15:15 duns Exp $
14 package ini.trakem2.utils;
16 public class Vector3 {
18 public double x, y, z;
20 /**
21 * creates vector from x,y,z
23 public Vector3(double x, double y, double z) {
24 this.x = x;
25 this.y = y;
26 this.z = z;
29 /**
30 * creates null vector
32 public Vector3() {
33 this(0,0,0);
36 /**
37 * creates vector from v
39 public Vector3(Vector3 v) {
40 this(v.x, v.y, v.z);
43 /**
44 * @return string representation of vector
47 /*public String toString() {
48 return "["+x+", "+y+", "+z+"]";
49 }*/
51 /**
52 * @return length
54 public double length() {
55 return Math.sqrt(x*x + y*y + z*z);
58 /**
59 * @return normal vector , or null if length is 0
63 public Vector3 normalize(Vector3 r) {
64 if (r == null) r = new Vector3();
65 double vlen = length();
66 if (vlen != 0.0) {
67 return r.set(x/vlen, y/vlen, z/vlen);
69 return null;
73 /**
74 * @return vector scaled by s
76 public Vector3 scale(double s, Vector3 r) {
77 if (r == null) r = new Vector3();
78 return r.set(s*x, s*y, s*z);
81 /**
82 * @return difference between vector and s
84 /*public Vector3 sub(Vector3 s, Vector3 r) {
85 if (r == null) r = new Vector3();
86 return r.set(x - s.x, y - s.y, z - s.z);
87 }*/
89 /**
90 * @return sum of vector and v
92 public Vector3 add(Vector3 v, Vector3 r) {
93 if (r == null) r = new Vector3();
94 return r.set(x + v.x, y + v.y, z + v.z);
97 /**
98 * @return the negation of vector
100 /*public Vector3 negate(Vector3 r) {
101 if (r == null) r = new Vector3();
102 return r.set(-x, -y, -z);
106 * @return dot product of vector and v
108 /*public double dot(Vector3 v) {
109 return x*v.x + y*v.y + z*v.z;
114 * @return cross produc of vector x v
116 /*public Vector3 cross(Vector3 v, Vector3 r) {
117 if (r == null) r = new Vector3();
118 return r.set(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
122 * @return half arc between vector and v
124 /*public Vector3 bisect(Vector3 v, Vector3 r) {
125 if (r == null) r = new Vector3();
126 add(v, r);
127 double length = r.length();
128 return (length < 1.0e-7) ? r.set(0, 0, 1) : r.scale(1/length, r);
132 * @return the vector set to x,y,z
134 public Vector3 set(double x, double y, double z) {
135 this.x = x;
136 this.y = y;
137 this.z = z;
138 return this;
141 } // End of class Vector3