/* * $Id: Vector3D.java,v 1.74 2007/02/09 20:08:54 rl Exp $ ********************************************************** * kaleido * * Kaleidoscopic construction of uniform polyhedra * Copyright © 1991-2007 Dr. Zvi Har’El * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * “This product includes software developed by * Dr. Zvi Har’El (http://www.math.technion.ac.il/~rl/).” * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * This software is provided ‘as-is’, without any express or implied * warranty. In no event will the author be held liable for any * damages arising from the use of this software. * * Author: * Dr. Zvi Har’El, * Deptartment of Mathematics, * Technion, Israel Institue of Technology, * Haifa 32000, Israel. * E-Mail: rl@math.technion.ac.il ********************************************************** */ package kaleido; import java.io.PrintStream; /** * Class Vector3D * @author Zvi Har’El * @version $Id: Vector3D.java,v 1.74 2007/02/09 20:08:54 rl Exp $ * @see Class source code */ public class Vector3D { double x, y, z; Vector3D(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } Vector3D add(Vector3D a) { return new Vector3D(x + a.x, y + a.y, z + a.z); } Vector3D sub(Vector3D a) { return new Vector3D(x - a.x, y - a.y, z - a.z); } Vector3D scale(double k) { return new Vector3D(k * x, k * y, k * z); } double dot(Vector3D a) { return x * a.x + y * a.y + z * a.z; } Vector3D cross(Vector3D a) { return new Vector3D(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x); } Vector3D rotate(Vector3D axis, double angle) { Vector3D p = axis.scale(dot(axis)); return p.add(sub(p).scale(Math.cos(angle))). sub(cross(axis).scale(Math.sin(angle))); } private final static Vector3D[] xyz = {new Vector3D(1,0,0), new Vector3D(0,1,0), new Vector3D(0,0,1)}; /** * rotates an array of vectors. */ static void rotArray(Vector3D[] v, Vector3D[] u, double azimuth, double elevation, double angle) { Vector3D axis = xyz[0].rotate(xyz[1], elevation).rotate(xyz[2], azimuth); Vector3D[] XYZ = new Vector3D[3]; for (int i = 0; i < 3; i++) XYZ[i] = xyz[i].rotate(axis, angle); for (int i = 0; i < v.length; i++) { u[i] = XYZ[0].scale(v[i].x). add(XYZ[1].scale(v[i].y)). add(XYZ[2].scale(v[i].z)); } } boolean same(Vector3D a, double epsilon) { return Math.abs(x - a.x) < epsilon && Math.abs(y - a.y) < epsilon && Math.abs (z - a.z) < epsilon; } /** * Compute the polar reciprocal of the plane containing a, b and c. * * If this plane does not contain the origin, return p such that * dot(p,a) = dot(p,b) = dot(p,b) = r. * * Otherwise, return p such that * dot(p,a) = dot(p,b) = dot(p,c) = 0 * and * dot(p,p) = 1. */ static Vector3D pole(double r, Vector3D a, Vector3D b, Vector3D c) { Vector3D p = b.sub(a).cross(c.sub(a)); double k = p.dot(a); if (Math.abs(k) < 1e-6) return p.scale(1 / Math.sqrt(p.dot(p))); else return p.scale(r / k); } }