001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030/**
031 *
032 */
033package org.openimaj.vis.general;
034
035
036
037/**
038 *      This provides an implementation of the camera position provider interface
039 *      that points a camera at a specific point and rotates the camera around the
040 *      X, Y and Z planes with specific speeds at a specific radius.
041 *
042 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
043 *  @created 5 Jul 2013
044 */
045public class RotatingCameraProvider implements CameraPositionProvider
046{
047        /** The lookAt x coordinate */
048        private final float x;
049
050        /** The lookAt y coordinate */
051        private final float y;
052
053        /** The lookAt z coordinate */
054        private final float z;
055
056        /** The speed of the x oscillation */
057        private final float xS;
058
059        /** The speed of the y oscillation */
060        private final float yS;
061
062        /** The speed of the z oscillation */
063        private final float zS;
064
065        /** The radius of the X oscillation */
066        private final float radiusX;
067
068        /** The radius of the Y oscillation */
069        private final float radiusY;
070
071        /** The radius of the Z oscillation */
072        private final float radiusZ;
073
074        /** The time the provider was initialised */
075        private long startTime = 0;
076
077        /** The position of the camera x coordinate */
078        private final float xPos;
079
080        /** The position of the camera y coordinate */
081        private final float yPos;
082
083        /** The position of the camera z coordinate */
084        private final float zPos;
085
086        /**
087         *      Rotating camera provider looking at x, y, z with the given radius.
088         *
089         *      @param xPos The initial position of the camera x coordinate
090         *      @param yPos The initial position of the camera y coordinate
091         *      @param zPos The initial position of the camera z coordinate
092         *      @param x The x The look-at point of the camera x coordinate
093         *      @param y The y The look-at point of the camera y coordinate
094         *      @param z The z The look-at point of the camera z coordinate
095         *      @param xS The x speed The x oscillation speed
096         *      @param yS The y speed The y oscillation speed
097         *      @param zS The z speed The z oscillation speed
098         *      @param radius The amplitude of all the osciallitions
099         */
100        public RotatingCameraProvider(
101                        final float xPos, final float yPos, final float zPos,
102                        final float x, final float y, final float z,
103                        final float xS, final float yS, final float zS,
104                        final float radius )
105        {
106                this.xPos = xPos;
107                this.yPos = yPos;
108                this.zPos = zPos;
109                this.x = x;
110                this.y = y;
111                this.z = z;
112                this.xS = yS;
113                this.yS = yS;
114                this.zS = zS;
115                this.radiusX = this.radiusY = this.radiusZ = radius;
116                this.startTime = System.currentTimeMillis();
117        }
118
119        /**
120         *      Rotating camera provider looking at x, y, z with the given radius.
121         *
122         *      @param xPos The initial position of the camera x coordinate
123         *      @param yPos The initial position of the camera y coordinate
124         *      @param zPos The initial position of the camera z coordinate
125         *      @param x The x The look-at point of the camera x coordinate
126         *      @param y The y The look-at point of the camera y coordinate
127         *      @param z The z The look-at point of the camera z coordinate
128         *      @param xS The x speed The x oscillation speed
129         *      @param yS The y speed The y oscillation speed
130         *      @param zS The z speed The z oscillation speed
131         *      @param radiusX The amplitude of the X osciallitions
132         *      @param radiusY The amplitude of the Y osciallitions
133         *      @param radiusZ The amplitude of the Z osciallitions
134         */
135        public RotatingCameraProvider(
136                        final float xPos, final float yPos, final float zPos,
137                        final float x, final float y, final float z,
138                        final float xS, final float yS, final float zS,
139                        final float radiusX, final float radiusY, final float radiusZ )
140        {
141                this.xPos = xPos;
142                this.yPos = yPos;
143                this.zPos = zPos;
144                this.x = x;
145                this.y = y;
146                this.z = z;
147                this.xS = yS;
148                this.yS = yS;
149                this.zS = zS;
150                this.radiusX = radiusX;
151                this.radiusY = radiusY;
152                this.radiusZ = radiusZ;
153                this.startTime = System.currentTimeMillis();
154        }
155
156        /**
157         *      {@inheritDoc}
158         *      @see org.openimaj.vis.general.CameraPositionProvider#getCameraPosition()
159         */
160        @Override
161        public float[] getCameraPosition()
162        {
163                final long currentTime = System.currentTimeMillis();
164                final long diffTime = currentTime - this.startTime;
165
166                final float xPos = (float)(this.xPos + this.radiusX * Math.sin( diffTime * this.xS ));
167                final float yPos = (float)(this.yPos + this.radiusY * Math.cos( diffTime * this.yS ));
168                final float zPos = (float)(this.zPos + this.radiusZ * Math.sin( diffTime * this.zS ));
169
170                final float[] f = new float[] {xPos, yPos, zPos, this.x, this.y, this.z, 0, 1, 0};
171                return f;
172        }
173}