Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.rosjava_geometry;
00018
00019 import com.google.common.collect.Lists;
00020
00021 import java.util.List;
00022
00029 public class Vector3 {
00030
00031 private static final Vector3 ZERO = new Vector3(0, 0, 0);
00032 private static final Vector3 X_AXIS = new Vector3(1, 0, 0);
00033 private static final Vector3 Y_AXIS = new Vector3(0, 1, 0);
00034 private static final Vector3 Z_AXIS = new Vector3(0, 0, 1);
00035
00036 private final double x;
00037 private final double y;
00038 private final double z;
00039
00040 public static Vector3 fromVector3Message(geometry_msgs.Vector3 message) {
00041 return new Vector3(message.getX(), message.getY(), message.getZ());
00042 }
00043
00044 public static Vector3 fromPointMessage(geometry_msgs.Point message) {
00045 return new Vector3(message.getX(), message.getY(), message.getZ());
00046 }
00047
00048 public static Vector3 zero() {
00049 return ZERO;
00050 }
00051
00052 public static Vector3 xAxis() {
00053 return X_AXIS;
00054 }
00055
00056 public static Vector3 yAxis() {
00057 return Y_AXIS;
00058 }
00059
00060 public static Vector3 zAxis() {
00061 return Z_AXIS;
00062 }
00063
00064 public Vector3(double x, double y, double z) {
00065 this.x = x;
00066 this.y = y;
00067 this.z = z;
00068 }
00069
00070 public Vector3 add(Vector3 other) {
00071 return new Vector3(x + other.x, y + other.y, z + other.z);
00072 }
00073
00074 public Vector3 subtract(Vector3 other) {
00075 return new Vector3(x - other.x, y - other.y, z - other.z);
00076 }
00077
00078 public Vector3 invert() {
00079 return new Vector3(-x, -y, -z);
00080 }
00081
00082 public double dotProduct(Vector3 other) {
00083 return x * other.x + y * other.y + z * other.z;
00084 }
00085
00086 public Vector3 normalize() {
00087 return new Vector3(x / getMagnitude(), y / getMagnitude(), z / getMagnitude());
00088 }
00089
00090 public Vector3 scale(double factor) {
00091 return new Vector3(x * factor, y * factor, z * factor);
00092 }
00093
00094 public double getX() {
00095 return x;
00096 }
00097
00098 public double getY() {
00099 return y;
00100 }
00101
00102 public double getZ() {
00103 return z;
00104 }
00105
00106 public double getMagnitudeSquared() {
00107 return x * x + y * y + z * z;
00108 }
00109
00110 public double getMagnitude() {
00111 return Math.sqrt(getMagnitudeSquared());
00112 }
00113
00114 public geometry_msgs.Vector3 toVector3Message(geometry_msgs.Vector3 result) {
00115 result.setX(x);
00116 result.setY(y);
00117 result.setZ(z);
00118 return result;
00119 }
00120
00121 public geometry_msgs.Point toPointMessage(geometry_msgs.Point result) {
00122 result.setX(x);
00123 result.setY(y);
00124 result.setZ(z);
00125 return result;
00126 }
00127
00128 public boolean almostEquals(Vector3 other, double epsilon) {
00129 List<Double> epsilons = Lists.newArrayList();
00130 epsilons.add(x - other.x);
00131 epsilons.add(y - other.y);
00132 epsilons.add(z - other.z);
00133 for (double e : epsilons) {
00134 if (Math.abs(e) > epsilon) {
00135 return false;
00136 }
00137 }
00138 return true;
00139 }
00140
00141 @Override
00142 public String toString() {
00143 return String.format("Vector3<x: %.4f, y: %.4f, z: %.4f>", x, y, z);
00144 }
00145
00146 @Override
00147 public int hashCode() {
00148
00149 double x = this.x == 0 ? 0 : this.x;
00150 double y = this.y == 0 ? 0 : this.y;
00151 double z = this.z == 0 ? 0 : this.z;
00152 final int prime = 31;
00153 int result = 1;
00154 long temp;
00155 temp = Double.doubleToLongBits(x);
00156 result = prime * result + (int) (temp ^ (temp >>> 32));
00157 temp = Double.doubleToLongBits(y);
00158 result = prime * result + (int) (temp ^ (temp >>> 32));
00159 temp = Double.doubleToLongBits(z);
00160 result = prime * result + (int) (temp ^ (temp >>> 32));
00161 return result;
00162 }
00163
00164 @Override
00165 public boolean equals(Object obj) {
00166 if (this == obj)
00167 return true;
00168 if (obj == null)
00169 return false;
00170 if (getClass() != obj.getClass())
00171 return false;
00172 Vector3 other = (Vector3) obj;
00173
00174 double x = this.x == 0 ? 0 : this.x;
00175 double y = this.y == 0 ? 0 : this.y;
00176 double z = this.z == 0 ? 0 : this.z;
00177 double otherX = other.x == 0 ? 0 : other.x;
00178 double otherY = other.y == 0 ? 0 : other.y;
00179 double otherZ = other.z == 0 ? 0 : other.z;
00180 if (Double.doubleToLongBits(x) != Double.doubleToLongBits(otherX))
00181 return false;
00182 if (Double.doubleToLongBits(y) != Double.doubleToLongBits(otherY))
00183 return false;
00184 if (Double.doubleToLongBits(z) != Double.doubleToLongBits(otherZ))
00185 return false;
00186 return true;
00187 }
00188 }