home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch15 / NURBS / Point4.java < prev   
Encoding:
Java Source  |  1997-01-29  |  5.1 KB  |  203 lines

  1. /*
  2.  * Copyright (c) 1997 ORC Incorporated.  All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please contact <info@ocnus.com> for
  8.  * further information regarding copyright and licensing.
  9.  * This code is provided without warranty, either expressed or implied.
  10.  */
  11. /* 
  12.  * Portions of this code appear in the book
  13.  * "Late Night VRML 2.0 with Java", Ziff-Davis Press, 1997
  14.  *
  15.  * Point4.java
  16.  *
  17.  * author   Timothy F. Rohaly
  18.  * version  1.0, 01/15/97
  19.  */
  20.  
  21. package nurbs;
  22.  
  23.  
  24. /**
  25.  * Representation of a homogeneous point in 3-space, used
  26.  * as control points for a NURBS curve or surface.
  27.  * @author   Timothy F. Rohaly
  28.  * @version  1.0, 1/15/97
  29.  */
  30. public class Point4 {
  31.  
  32.     public static final float TOLERANCE = 1.0E-7f;
  33.  
  34.     /**
  35.      * The x coordinate.
  36.      */
  37.     public float x;
  38.  
  39.     /**
  40.      * The y coordinate.
  41.      */
  42.     public float y;
  43.  
  44.     /**
  45.      * The z coordinate.
  46.      */
  47.     public float z;
  48.  
  49.     /**
  50.      * The w coordinate.
  51.      */
  52.     public float w;
  53.  
  54.  
  55.     /**
  56.      * Constructor.  Initializes coordinates to 0.0f.
  57.      */
  58.     public Point4() {
  59.         this(0.0f, 0.0f, 0.0f, 0.0f);
  60.     }
  61.  
  62.  
  63.     /**
  64.      * Constructor.
  65.      * @param x the x coordinate.
  66.      * @param y the y coordinate.
  67.      * @param z the z coordinate.
  68.      * @param w the w coordinate.
  69.      */
  70.     public Point4(float x, float y, float z, float w) {
  71.         this.x = x;
  72.         this.y = y;
  73.         this.z = z;
  74.         this.w = w;
  75.     }
  76.  
  77.  
  78.     /**
  79.      * Constructor.
  80.      * @param point a Point4 object to copy.
  81.      */
  82.     public Point4(Point4 point) {
  83.         this.x = point.x;
  84.         this.y = point.y;
  85.         this.z = point.z;
  86.         this.w = point.w;
  87.     }
  88.  
  89.  
  90.     /**
  91.      * Set the values of this Point4.
  92.      * @param x the x coordinate.
  93.      * @param y the y coordinate.
  94.      * @param z the z coordinate.
  95.      * @param w the w coordinate.
  96.      */
  97.     public void setValue(float x, float y, float z, float w) {
  98.         this.x = x;
  99.         this.y = y;
  100.         this.z = z;
  101.         this.w = w;
  102.     }
  103.  
  104.  
  105.     /**
  106.      * Check to see if this Point4 approximately equals
  107.      * another.  "Approximately" means within one part
  108.      * in ten million.  This is useful when comparing
  109.      * to within roundoff error
  110.      * @param p1 the Point4 to compare
  111.      */
  112.     public boolean approxEquals(Point4 p1) {
  113.         if (Math.abs(this.x - p1.x) <= TOLERANCE &&
  114.             Math.abs(this.y - p1.y) <= TOLERANCE &&
  115.             Math.abs(this.z - p1.z) <= TOLERANCE &&
  116.             Math.abs(this.w - p1.w) <= TOLERANCE    )
  117.             return true;
  118.         return false;
  119.     }
  120.  
  121.  
  122.     /**
  123.      * Add another Point4 to this one.
  124.      * @param p1 the Point4 to add
  125.      */
  126.     public void add(Point4 p1) {
  127.         this.x += p1.x;
  128.         this.y += p1.y;
  129.         this.z += p1.z;
  130.         this.w += p1.w;
  131.     }
  132.  
  133.  
  134.     /**
  135.      * Translate this Point4.
  136.      * @param x the X translation component
  137.      * @param y the Y translation component
  138.      * @param z the Z translation component
  139.      */
  140.     public void translate(float x, float y, float z) {
  141.         this.x = this.x + this.w*x;
  142.         this.y = this.y + this.w*y;
  143.         this.z = this.z + this.w*z;
  144.     }
  145.  
  146.  
  147.     /**
  148.      * Scale this Point4 uniformly.
  149.      * @param scale the uniform scale factor
  150.      */
  151.     public void scale(float scale) {
  152.         this.scale(scale, scale, scale);
  153.     }
  154.  
  155.  
  156.     /**
  157.      * Scale this Point4 non-uniformly.
  158.      * @param xscale the x scale factor
  159.      * @param yscale the y scale factor
  160.      * @param zscale the z scale factor
  161.      */
  162.     public void scale(float xscale, float yscale, float zscale) {
  163.         this.x *= xscale;
  164.         this.y *= yscale;
  165.         this.z *= zscale;
  166.     }
  167.  
  168.  
  169.     /**
  170.      * Apply a rotation transformation to this Point4.
  171.      * @param xaxis the X component of the rotation axis
  172.      * @param yaxis the Y component of the rotation axis
  173.      * @param zaxis the Z component of the rotation axis
  174.      * @param angle the angle of the rotation in radians
  175.      */
  176.     public void rotate(float xaxis, float yaxis, float zaxis, float angle) {
  177.         float cos = (float) Math.cos(angle);
  178.         float sin = (float) Math.sin(angle);
  179.         float x = this.x * ( xaxis*xaxis  +  cos*(1f - xaxis*xaxis)) +
  180.                   this.y * ( zaxis*sin    +  xaxis*yaxis*(1f - cos)) +
  181.                   this.z * ( yaxis*sin    +  xaxis*zaxis*(1f - cos));
  182.         float y = this.x * ( zaxis*sin    +  xaxis*yaxis*(1f - cos)) +
  183.                   this.y * ( yaxis*yaxis  +  cos*(1f - yaxis*yaxis)) +
  184.                   this.z * (-xaxis*sin    +  yaxis*zaxis*(1f - cos));
  185.         float z = this.x * (-yaxis*sin    +  xaxis*zaxis*(1f - cos)) +
  186.                   this.y * ( xaxis*sin    +  yaxis*zaxis*(1f - cos)) +
  187.                   this.z * ( zaxis*zaxis  +  cos*(1f - zaxis*zaxis));
  188.         this.x = x;
  189.         this.y = y;
  190.         this.z = z;
  191.     }
  192.  
  193.  
  194.     /**
  195.      * Create a string representation of this point.
  196.      * @return the String
  197.      */
  198.     public String toString() {
  199.         return getClass().getName() + "[x=" + x + ", y=" + y +
  200.                                       ", z=" + z + ", w=" + w + "]";
  201.     }
  202. }
  203.