1
2
3
4
5
6 package cz.cuni.amis.pogamut.unreal.t3dgenerator.datatypes;
7
8 import cz.cuni.amis.pogamut.unreal.t3dgenerator.annotations.UnrealDataType;
9
10
11
12
13
14 @UnrealDataType
15 public class Vector3D {
16 float x;
17 float y;
18 float z;
19
20 public Vector3D(float x, float y, float z) {
21 this.x = x;
22 this.y = y;
23 this.z = z;
24 }
25
26
27
28 public float getX() {
29 return x;
30 }
31
32 public float getY() {
33 return y;
34 }
35
36 public float getZ() {
37 return z;
38 }
39
40 public Vector3D multiply(float a){
41 return new Vector3D(x * a, y * a, z * a);
42 }
43
44 public Vector3D divide(float a){
45 return new Vector3D(x / a, y / a, z / a);
46 }
47
48 public Vector3D add(Vector3D p){
49 return new Vector3D(x + p.getX(), y + p.getY(), z + p.getZ());
50 }
51
52 public Vector3D subtract(Vector3D p){
53 return new Vector3D(x - p.getX(), y - p.getY(), z - p.getZ());
54 }
55
56
57 public Vector3D negateX(){
58 return new Vector3D(-getX(), getY(),getZ());
59 }
60 public Vector3D negateY(){
61 return new Vector3D(getX(), -getY(),getZ());
62 }
63
64 public Vector3D negateXandY(){
65 return new Vector3D(-getX(), -getY(),getZ());
66 }
67
68 public Vector3D switchXandY(){
69 return new Vector3D(getY(), getX(), getZ());
70 }
71
72 public Vector3D negate(){
73 return new Vector3D(-x, -y, -z);
74 }
75
76 public Vector3D crossProduct(Vector3D p){
77 return new Vector3D(
78 y * p.z - z * p.y,
79 z * p.x - x * p.z,
80 x * p.y - y * p.x);
81 }
82
83 public float dotProduct(Vector3D v){
84 return x * v.x + y * v.y + z * v.z;
85 }
86
87 public double length(){
88 return Math.sqrt(x * x + y * y + z * z);
89 }
90
91 public Vector3D normalize(){
92 return divide((float)length());
93 }
94
95
96
97 public static final Vector3D ZERO = new Vector3D(0, 0, 0);
98 public static final Vector3D X_AXIS = new Vector3D(1, 0, 0);
99 public static final Vector3D Y_AXIS = new Vector3D(0, 1, 0);
100 public static final Vector3D Z_AXIS = new Vector3D(0, 0, 1);
101
102 public static Vector3D centroid(Vector3D ... points){
103 if(points.length <= 0){
104 throw new IllegalArgumentException("At least one point must be given");
105 }
106 Vector3D sum = points[0];
107 for(int i = 1; i < points.length ; i++){
108 sum = sum.add(points[i]);
109 }
110 return sum.divide(points.length);
111 }
112
113 @Override
114 public boolean equals(Object obj) {
115 if (obj == null) {
116 return false;
117 }
118 if (getClass() != obj.getClass()) {
119 return false;
120 }
121 final Vector3D other = (Vector3D) obj;
122 if (Float.floatToIntBits(this.x) != Float.floatToIntBits(other.x)) {
123 return false;
124 }
125 if (Float.floatToIntBits(this.y) != Float.floatToIntBits(other.y)) {
126 return false;
127 }
128 if (Float.floatToIntBits(this.z) != Float.floatToIntBits(other.z)) {
129 return false;
130 }
131 return true;
132 }
133
134 @Override
135 public int hashCode() {
136 int hash = 3;
137 hash = 31 * hash + Float.floatToIntBits(this.x);
138 hash = 31 * hash + Float.floatToIntBits(this.y);
139 hash = 31 * hash + Float.floatToIntBits(this.z);
140 return hash;
141 }
142
143 @Override
144 public String toString() {
145 return "Point3D{" + "x=" + x + "y=" + y + "z=" + z + '}';
146 }
147
148
149 }