001/* ===========================================================
002 * Orson Charts : a 3D chart library for the Java(tm) platform
003 * ===========================================================
004 * 
005 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
006 * 
007 * http://www.object-refinery.com/orsoncharts/index.html
008 * 
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published by
011 * the Free Software Foundation, either version 3 of the License, or
012 * (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
021 * 
022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
023 * Other names may be trademarks of their respective owners.]
024 * 
025 * If you do not wish to be bound by the terms of the GPL, an alternative
026 * commercial license can be purchased.  For details, please see visit the
027 * Orson Charts home page:
028 * 
029 * http://www.object-refinery.com/orsoncharts/index.html
030 * 
031 */
032
033package com.orsoncharts.graphics3d;
034
035/**
036 * A collection of utility methods for 3D geometry.
037 */
038public class Utils3D {
039    
040    private Utils3D() {
041        // no need to instantiate this class
042    }
043    
044    /**
045     * Returns the length of the vector v.
046     * 
047     * @param v  the vector ({@code null} not permitted).
048     * 
049     * @return The length.
050     */
051    public static double length(Point3D v) {
052        return Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
053    }
054    
055    /**
056     * Returns a new vector that is the normalised form of the specified 
057     * vector.
058     * 
059     * @param v  the vector ({@code null} not permitted).
060     * 
061     * @return The normalised form of the specified vector.
062     * 
063     * @since 1.2
064     */
065    public static Point3D normalise(Point3D v) {
066        double length = length(v);
067        return new Point3D(v.x / length, v.y / length, v.z / length);
068    }
069    
070    /**
071     * Returns the scalar product of two vectors.
072     * 
073     * @param a  vector A ({@code null} not permitted).
074     * @param b  vector B ({@code null} not permitted).
075     * 
076     * @return The scalar product. 
077     */
078    public static double scalarprod(Point3D a, Point3D b) {
079        return a.x * b.x + a.y * b.y + a.z * b.z;    
080    }
081    
082    /**
083     * Returns the normal vector for the plane defined by three points.
084     * 
085     * @param a  point A ({@code null} not permitted).
086     * @param b  point B ({@code null} not permitted).
087     * @param c  point C ({@code null} not permitted).
088     * 
089     * @return The normal vector. 
090     */
091    public static Point3D normal(Point3D a, Point3D b, Point3D c) {
092        double ax = a.x - c.x;
093        double ay = a.y - c.y;
094        double az = a.z - c.z;
095        double bx = b.x - c.x;
096        double by = b.y - c.y;
097        double bz = b.z - c.z;
098        return new Point3D(ay * bz - az * by, az * bx - ax * bz, 
099                ax * by - ay * bx);
100    }
101
102    /**
103     * Returns the angle between the two vectors.
104     * 
105     * @param a  vector A ({@code null} not permitted).
106     * @param b  vector B ({@code null} not permitted).
107     * 
108     * @return The (positive) angle in radians. 
109     */
110    public static double angle(Point3D a, Point3D b) {
111        double dp = a.x * b.x + a.y * b.y + a.z * b.z;
112        double alen = length(a);
113        double blen = length(b);
114        double c = dp / (alen * blen);
115        // rounding can cause abs(c) to be greater than one, let's sweep that 
116        // under the carpet...
117        c = Math.max(-1.0, Math.min(1.0, c));
118        return Math.acos(c);    
119    }
120
121}