001/*
002 * Copyright 2006 - 2013
003 *     Stefan Balev     <stefan.balev@graphstream-project.org>
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné      <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.ui.geom;
033
034/**
035 * A three component vector made of doubles.
036 * 
037 * @author Antoine Dutot
038 * @since 20000613
039 * @version 0.1
040 */
041public class Vector3 extends Vector2 {
042        // Attributes:
043
044        private static final long serialVersionUID = 8839258036865851454L;
045
046        // Constructors
047
048        /**
049         * New zero vector.
050         */
051        public Vector3() {
052                data = new double[3];
053                data[0] = 0;
054                data[1] = 0;
055                data[2] = 0;
056        }
057
058        /**
059         * New (<code>x</code>,<code>y</code>,<code>z</code>) vector.
060         */
061        public Vector3(double x, double y, double z) {
062                data = new double[3];
063                data[0] = x;
064                data[1] = y;
065                data[2] = z;
066        }
067
068        /**
069         * New vector copy of <code>other</code>.
070         */
071        public Vector3(Vector3 other) {
072                data = new double[3];
073                copy(other);
074        }
075
076        /**
077         * New vector copy of <code>point</code>.
078         */
079        public Vector3(Point3 point) {
080                data = new double[3];
081                copy(point);
082        }
083
084        // Predicates
085
086        /**
087         * Are all components to zero?.
088         */
089        @Override
090        public boolean isZero() {
091                return (data[0] == 0 && data[1] == 0 && data[2] == 0);
092        }
093
094        /**
095         * Is this equal to other ?
096         */
097        @Override
098        public boolean equals(Object other) {
099                Vector3 v;
100
101                if (!(other instanceof Vector3)) {
102                        return false;
103                }
104
105                v = (Vector3) other;
106
107                return (data[0] == v.data[0] && data[1] == v.data[1] && data[2] == v.data[2]);
108        }
109
110        /**
111         * Is i the index of a component ?
112         * 
113         * In other words, is i &gt;= 0 &amp;&amp; &lt; than #count() ?
114         */
115        @Override
116        public boolean validComponent(int i) {
117                return (i >= 0 && i < 3);
118        }
119
120        // Access
121
122        @Override
123        public Object clone() {
124                return new Vector3(this);
125        }
126
127        // Access
128
129        public double dotProduct(double ox, double oy, double oz) {
130                return ((data[0] * ox) + (data[1] * oy) + (data[2] * oz));
131        }
132
133        /**
134         * Dot product of this and other.
135         */
136        public double dotProduct(Vector3 other) {
137                return ((data[0] * other.data[0]) + (data[1] * other.data[1]) + (data[2] * other.data[2]));
138        }
139
140        /**
141         * Cartesian length.
142         */
143        @Override
144        public double length() {
145                return Math.sqrt((data[0] * data[0]) + (data[1] * data[1])
146                                + (data[2] * data[2]));
147        }
148        
149        public double z() {
150                return data[2];
151        }
152
153        // Commands
154
155        /**
156         * Assign value to all elements.
157         */
158        @Override
159        public void fill(double value) {
160                data[0] = data[1] = data[2] = value;
161        }
162
163        /**
164         * Explicitly set the i-th component to value.
165         */
166        @Override
167        public void set(int i, double value) {
168                data[i] = value;
169        }
170
171        /**
172         * Explicitly set the three components.
173         */
174        public void set(double x, double y, double z) {
175                data[0] = x;
176                data[1] = y;
177                data[2] = z;
178        }
179
180        /**
181         * Add each element of other to the corresponding element of this.
182         */
183        public void add(Vector3 other) {
184                data[0] += other.data[0];
185                data[1] += other.data[1];
186                data[2] += other.data[2];
187        }
188
189        /**
190         * Substract each element of other to the corresponding element of this.
191         */
192        public void sub(Vector3 other) {
193                data[0] -= other.data[0];
194                data[1] -= other.data[1];
195                data[2] -= other.data[2];
196        }
197
198        /**
199         * Multiply each element of this by the corresponding element of other.
200         */
201        public void mult(Vector3 other) {
202                data[0] *= other.data[0];
203                data[1] *= other.data[1];
204                data[2] *= other.data[2];
205        }
206
207        /**
208         * Add value to each element.
209         */
210        @Override
211        public void scalarAdd(double value) {
212                data[0] += value;
213                data[1] += value;
214                data[2] += value;
215        }
216
217        /**
218         * Substract value to each element.
219         */
220        @Override
221        public void scalarSub(double value) {
222                data[0] -= value;
223                data[1] -= value;
224                data[2] -= value;
225        }
226
227        /**
228         * Multiply each element by value.
229         */
230        @Override
231        public void scalarMult(double value) {
232                data[0] *= value;
233                data[1] *= value;
234                data[2] *= value;
235        }
236
237        /**
238         * Divide each element by value.
239         */
240        @Override
241        public void scalarDiv(double value) {
242                data[0] /= value;
243                data[1] /= value;
244                data[2] /= value;
245        }
246
247        /**
248         * Set this to the cross product of this and other.
249         */
250        public void crossProduct(Vector3 other) {
251                double x;
252                double y;
253
254                x = (data[1] * other.data[2]) - (data[2] * other.data[1]);
255                y = (data[2] * other.data[0]) - (data[0] * other.data[2]);
256                data[2] = (data[0] * other.data[1]) - (data[1] * other.data[0]);
257                data[0] = x;
258                data[1] = y;
259        }
260
261        /**
262         * Set this to the cross product of A and B.
263         */
264        public void crossProduct(Vector3 A, Vector3 B) {
265                data[0] = (A.data[1] * B.data[2]) - (A.data[2] * B.data[1]);
266                data[1] = (A.data[2] * B.data[0]) - (A.data[0] * B.data[2]);
267                data[2] = (A.data[0] * B.data[1]) - (A.data[1] * B.data[0]);
268        }
269
270        /**
271         * Transform this into an unit vector.
272         * 
273         * @return the vector length.
274         */
275        @Override
276        public double normalize() {
277                double len = length();
278
279                if (len != 0) {
280                        data[0] /= len;
281                        data[1] /= len;
282                        data[2] /= len;
283                }
284
285                return len;
286        }
287
288        // Utility
289
290        /**
291         * Make this a copy of other.
292         */
293        public void copy(Vector3 other) {
294                data[0] = other.data[0];
295                data[1] = other.data[1];
296                data[2] = other.data[2];
297        }
298
299        /**
300         * Make this a copy of <code>point</code>.
301         */
302        public void copy(Point3 point) {
303                data[0] = point.x;
304                data[1] = point.y;
305                data[2] = point.z;
306        }
307
308        // Misc.
309
310        @Override
311        public String toString() {
312                StringBuffer sb = new StringBuffer("[");
313
314                sb.append(data[0]);
315                sb.append('|');
316                sb.append(data[1]);
317                sb.append('|');
318                sb.append(data[2]);
319                sb.append(']');
320
321                return sb.toString();
322        }
323}