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         * @return
086         */
087        @Override
088        public Point3d[] getVertices() {
089                double x = 0.5 * width;
090                double y = 0.5 * height;
091                double z = 0.5 * length;
092                Point3d[] vertices = new Point3d[8];
093                vertices[0] = new Point3d(-x, -y,  z);
094                vertices[1] = new Point3d(-x,  y,  z);
095                vertices[2] = new Point3d( x,  y,  z);
096                vertices[3] = new Point3d( x, -y,  z);
097                vertices[4] = new Point3d(-x, -y, -z);
098                vertices[5] = new Point3d(-x,  y, -z);
099                vertices[6] = new Point3d( x,  y, -z);
100                vertices[7] = new Point3d( x, -y, -z);
101
102                return vertices;
103        };
104
105        @Override
106        public List<int[]> getLineLoops() {
107                return Arrays.asList(lineLoop1, lineLoop2, lineLoop3, lineLoop4);
108        }
109
110        @Override
111        public int getViewCount() {
112                return viewNames.length;
113        }
114
115        @Override
116        public String getViewName(int index) {
117                return viewNames[index];
118        }
119
120        @Override
121        public Matrix3d getViewMatrix(int index) {
122                Matrix3d m = new Matrix3d();
123                switch (index) {
124                case 0:  m.setIdentity(); // front
125                break;
126                case 1:  m.rotY(Math.PI/2); // left
127                break;
128                case 2:  m.rotY(Math.PI); // back
129                break;
130                case 3:  m.rotY(-Math.PI/2); // right
131                break;
132                case 4:  m.rotX(Math.PI/2); // top
133                break;
134                case 5:  m.rotX(-Math.PI/2); // bottom
135                break;
136                default: throw new IllegalArgumentException("getViewMatrix: index out of range:" + index);
137                }
138                return m;
139        }
140}