| 1 | /* |
| 2 | * Copyright (c) 2008, Carnegie Mellon University |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Code author: Jernej Barbic |
| 6 | * Research: Jernej Barbic, Doug L. James |
| 7 | * Funding: NSF, Link Foundation |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions are met: |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * * Neither the name of Carnegie Mellon University, nor the |
| 17 | * names of its contributors may be used to endorse or promote products |
| 18 | * derived from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND ANY |
| 21 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | * DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY |
| 24 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include "vec3d.h" |
| 33 | |
| 34 | /* |
| 35 | Given an input vector v, find a unit vector that is orthogonal to it |
| 36 | */ |
| 37 | Vec3d Vec3d::findOrthonormalVector() const |
| 38 | { |
| 39 | // find smallest abs component of v |
| 40 | int smallestIndex = 0; |
| 41 | for(int dim=1; dim<3; dim++) |
| 42 | if (fabs(elt[dim]) < fabs(elt[smallestIndex])) |
| 43 | smallestIndex = dim; |
| 44 | |
| 45 | Vec3d axis(0.0, 0.0, 0.0); |
| 46 | axis[smallestIndex] = 1.0; |
| 47 | |
| 48 | // this cross-product will be non-zero (as long as v is not zero) |
| 49 | Vec3d result = norm(cross(elt, axis)); |
| 50 | return result; |
| 51 | } |
| 52 |