001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 *  image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause 
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 
006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 
007 * Visit http://imagingbook.com for additional details.
008 *******************************************************************************/
009package imagingbook.lib.math;
010
011import org.apache.commons.math3.linear.ArrayRealVector;
012import org.apache.commons.math3.linear.RealVector;
013
014/**
015 * This class represents a homogeneous coordinate vector.
016 * @author W. Burger
017 *
018 */
019public class HomogeneousVector extends ArrayRealVector {
020
021        private static final long serialVersionUID = 1;
022        
023        public static HomogeneousVector Homogen(RealVector c) {
024                return new HomogeneousVector(c);
025        }
026        
027        public static RealVector Cartesian(HomogeneousVector h) {
028                return h.toCartesian();
029        }
030        
031        /**
032         * Creates a new homogeneous vector from Cartesian
033         * coordinates.
034         * @param c Cartesian coordinates.
035         */
036        public HomogeneousVector(double[] c) {
037                super(c, new double[] {1});
038        }
039        
040        /**
041         * Creates a new homogeneous vector from Cartesian
042         * coordinates.
043         * @param c Cartesian coordinates.
044         */
045        public HomogeneousVector(RealVector c) {
046                this(c.toArray());
047        }
048        
049        /**
050         * Converts this homogeneous vector back to
051         * Cartesian coordinates.
052         * 
053         * @return the Cartesian vector
054         */
055        public RealVector toCartesian() {
056                final int n = getDimension();
057                RealVector cv = getSubVector(0, n - 1);
058                cv.mapDivideToSelf(this.getEntry(n - 1));
059                return cv;
060        }
061
062//      public static void main(String[] args) {
063//              HomogeneousVector hv = new HomogeneousVector(new double[] {1,2,3});
064//              System.out.println("hv = " + hv.toString());
065//              
066//              hv.mapMultiplyToSelf(10);
067//              System.out.println("hv * 10 = " + hv.toString());
068//              
069//              RealVector cv = hv.toCartesian();
070//              System.out.println("cv = " + cv.toString());
071//      }
072        
073        
074}