001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.structure.symmetry.geometry;
022
023import javax.vecmath.Matrix3d;
024import javax.vecmath.Point3d;
025import java.util.Arrays;
026import java.util.List;
027
028
029public class RectangularPrism implements Polyhedron {
030        private static int[] lineLoop1 = {0,1,2,3,0,4,5,6,7,4};
031        private static int[] lineLoop2 = {1,5};
032        private static int[] lineLoop3 = {2,6};
033        private static int[] lineLoop4 = {3,7};
034        private double length = 1.0;
035        private double width = 1.0;
036        private double height = 1.0;
037        private static String[] viewNames = {"Front", "Left", "Back", "Right", "Top", "Bottom"};
038
039        public RectangularPrism(double length, double width, double height) {
040                this.length = length;
041                this.width = width;
042                this.height = height;
043        }
044
045        /**
046         * Returns the radius of a circumscribed sphere, that goes
047         * through all vertices
048         * @return the cirumscribedRadius
049         */
050        public double getLength() {
051                return length;
052        }
053
054        /**
055         * Returns the radius of an inscribed sphere, that is tangent to each
056         * of the octahedron's faces
057         * @return the inscribedRadius
058         */
059        public double getWidth() {
060                return width;
061        }
062
063        /**
064         * Returns the radius of a sphere, that is tangent to each
065         * of the octahedron's edges
066         *
067         * @return the midRadius
068         */
069        public double getHeight() {
070                return height;
071        }
072
073        /**
074         * Returns the radius of a circumscribed sphere (length of diagonal of
075         * rectangular prism/2, that goes through at least four vertices
076         * @return the cirumscribedRadius
077         */
078        @Override
079        public double getCirumscribedRadius() {
080                return 0.5* Math.sqrt(width*width + height*height + length*length);
081        }
082
083        /**
084         * Returns the vertices of an n-fold polygon of given radius and center
085         * @param n
086         * @param radius
087         * @param center
088         * @return
089         */
090        @Override
091        public Point3d[] getVertices() {
092                double x = 0.5 * width;
093                double y = 0.5 * height;
094                double z = 0.5 * length;
095                Point3d[] vertices = new Point3d[8];
096                vertices[0] = new Point3d(-x, -y,  z);
097                vertices[1] = new Point3d(-x,  y,  z);
098                vertices[2] = new Point3d( x,  y,  z);
099                vertices[3] = new Point3d( x, -y,  z);
100                vertices[4] = new Point3d(-x, -y, -z);
101                vertices[5] = new Point3d(-x,  y, -z);
102                vertices[6] = new Point3d( x,  y, -z);
103                vertices[7] = new Point3d( x, -y, -z);
104
105                return vertices;
106        };
107
108        @Override
109        public List<int[]> getLineLoops() {
110                return Arrays.asList(lineLoop1, lineLoop2, lineLoop3, lineLoop4);
111        }
112
113        @Override
114        public int getViewCount() {
115                return viewNames.length;
116        }
117
118        @Override
119        public String getViewName(int index) {
120                return viewNames[index];
121        }
122
123        @Override
124        public Matrix3d getViewMatrix(int index) {
125                Matrix3d m = new Matrix3d();
126                switch (index) {
127                case 0:  m.setIdentity(); // front
128                break;
129                case 1:  m.rotY(Math.PI/2); // left
130                break;
131                case 2:  m.rotY(Math.PI); // back
132                break;
133                case 3:  m.rotY(-Math.PI/2); // right
134                break;
135                case 4:  m.rotX(Math.PI/2); // top
136                break;
137                case 5:  m.rotX(-Math.PI/2); // bottom
138                break;
139                default: throw new IllegalArgumentException("getViewMatrix: index out of range:" + index);
140                }
141                return m;
142        }
143}