FreeWRL/FreeX3D  3.0.0
VSFVec3f.java
1 // copyright (c) 1997,1998 stephen f. white
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2, or (at your option)
6 // any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; see the file COPYING. If not, write to
15 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 package sai.eai;
17 import java.io.*;
18 
19 public class VSFVec3f extends VField
20 {
21  private float[] values = new float[3];
22 
23  public VSFVec3f(float x, float y, float z)
24  {
25  values[0] = x;
26  values[1] = y;
27  values[2] = z;
28  }
29 
30  public VSFVec3f(float[] values)
31  {
32  if (values.length != 3) {
33  this.values[0] = values[0];
34  this.values[1] = values[1];
35  this.values[2] = values[2];
36  } else {
37  this.values = values;
38  }
39  }
40 
41  public VSFVec3f(DataInputStream in) throws IOException
42  {
43  values[0] = in.readFloat();
44  values[1] = in.readFloat();
45  values[2] = in.readFloat();
46  }
47 
48  public void write(DataOutputStream out) throws IOException
49  {
50  out.writeFloat(values[0]);
51  out.writeFloat(values[1]);
52  out.writeFloat(values[2]);
53  }
54 
55  public String toString()
56  {
57  return "(" + values[0] + ", " + values[1] + ", " + values[2] + ")";
58  }
59 
60  public byte getType() { return SFVEC3F; }
61 
62  public float[] getValue() { return values; }
63 
64  public VSFVec3f plus(VSFVec3f v) {
65  return new VSFVec3f(values[0] + v.values[0],
66  values[1] + v.values[1],
67  values[2] + v.values[2]);
68  }
69 
70  public VSFVec3f minus(VSFVec3f v) {
71  return new VSFVec3f(values[0] - v.values[0],
72  values[1] - v.values[1],
73  values[2] - v.values[2]);
74  }
75 
76  public VSFVec3f times(float s) {
77  return new VSFVec3f(values[0] * s,
78  values[1] * s,
79  values[2] * s);
80  }
81 
82  /* Isabelle April 8 1999 for proximity calculation */
83 
84  public double getDistance(VSFVec3f v) {
85 
86  double x;
87  double y;
88  double z;
89  double distance;
90 
91  x = (double)(values[0] - v.values[0]);
92  y = (double)(values[1] - v.values[1]);
93  z = (double)(values[2] - v.values[2]);
94 
95  distance = Math.sqrt((x*x) + (y*y) + (z*z));
96  return distance;
97  }
98 
99  public double getAngle(VSFVec3f v) {
100 
101  /* working on the x-z plan for now. I might add the y axis later on */
102  double delta_x;
103  double delta_z;
104  double angle; /* in radians */
105 
106  delta_x = (double) (values[0] - v.values[0]);
107  /* axis shifted 180 degrees with standard. Therefore need to invert the parameters */
108  delta_z = (double) (v.values[2] - values[2]);
109  angle = Math.atan2(delta_z, delta_x); /* returns the angle whose tangent is z/x */
110  return(angle);
111 
112  }
113 
114 
115 }