1 31 package org.objectweb.proactive.examples.c3d.geom; 32 33 36 public class Vec implements java.io.Serializable { 37 38 41 public double x; 42 45 public double y; 46 49 public double z; 50 51 52 58 public Vec(double a, double b, double c) { 59 x = a; 60 y = b; 61 z = c; 62 } 63 64 65 68 public Vec(Vec a) { 69 x = a.x; 70 y = a.y; 71 z = a.z; 72 } 73 74 75 78 public Vec() { 79 x = 0.0; 80 y = 0.0; 81 z = 0.0; 82 } 83 84 85 89 public final void add(Vec a) { 90 x += a.x; 91 y += a.y; 92 z += a.z; 93 } 94 95 96 100 public static Vec adds(double s, Vec a, Vec b) { 101 return new Vec(s * a.x + b.x, s * a.y + b.y, s * a.z + b.z); 102 } 103 104 105 111 public final void adds(double s, Vec b) { 112 x += s * b.x; 113 y += s * b.y; 114 z += s * b.z; 115 } 116 117 118 121 public static Vec sub(Vec a, Vec b) { 122 return new Vec(a.x - b.x, a.y - b.y, a.z - b.z); 123 } 124 125 126 132 public final void sub2(Vec a, Vec b) { 133 this.x = a.x - b.x; 134 this.y = a.y - b.y; 135 this.z = a.z - b.z; 136 } 137 138 139 public static Vec mult(Vec a, Vec b) { 140 return new Vec(a.x * b.x, a.y * b.y, a.z * b.z); 141 } 142 143 144 public static Vec cross(Vec a, Vec b) { 145 return new Vec(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); 146 } 147 148 149 public static double dot(Vec a, Vec b) { 150 return a.x * b.x + a.y * b.y + a.z * b.z; 151 } 152 153 154 public static Vec comb(double a, Vec A, double b, Vec B) { 155 return new Vec(a * A.x + b * B.x, a * A.y + b * B.y, a * A.z + b * B.z); 156 } 157 158 159 public final void comb2(double a, Vec A, double b, Vec B) { 160 x = a * A.x + b * B.x; 161 y = a * A.y + b * B.y; 162 z = a * A.z + b * B.z; 163 } 164 165 166 public final void scale(double t) { 167 x *= t; 168 y *= t; 169 z *= t; 170 } 171 172 173 public final void negate() { 174 x = -x; 175 y = -y; 176 z = -z; 177 } 178 179 180 public final double normalize() { 181 double len; 182 len = Math.sqrt(x * x + y * y + z * z); 183 if (len > 0.0) { 184 x /= len; 185 y /= len; 186 z /= len; 187 } 188 return len; 189 } 190 191 192 public final String toString() { 193 return "<" + x + "," + y + "," + z + ">"; 194 } 195 } 196 | Popular Tags |