| [0b990d] | 1 | // | 
|---|
|  | 2 | // vector3.cc | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // Copyright (C) 1996 Limit Point Systems, Inc. | 
|---|
|  | 5 | // | 
|---|
|  | 6 | // Author: Curtis Janssen <cljanss@limitpt.com> | 
|---|
|  | 7 | // Maintainer: LPS | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // This file is part of the SC Toolkit. | 
|---|
|  | 10 | // | 
|---|
|  | 11 | // The SC Toolkit is free software; you can redistribute it and/or modify | 
|---|
|  | 12 | // it under the terms of the GNU Library General Public License as published by | 
|---|
|  | 13 | // the Free Software Foundation; either version 2, or (at your option) | 
|---|
|  | 14 | // any later version. | 
|---|
|  | 15 | // | 
|---|
|  | 16 | // The SC Toolkit is distributed in the hope that it will be useful, | 
|---|
|  | 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 19 | // GNU Library General Public License for more details. | 
|---|
|  | 20 | // | 
|---|
|  | 21 | // You should have received a copy of the GNU Library General Public License | 
|---|
|  | 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to | 
|---|
|  | 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
|  | 24 | // | 
|---|
|  | 25 | // The U.S. Government is granted a limited license as per AL 91-7. | 
|---|
|  | 26 | // | 
|---|
|  | 27 |  | 
|---|
|  | 28 | #ifdef __GNUC__ | 
|---|
|  | 29 | #pragma implementation | 
|---|
|  | 30 | #endif | 
|---|
|  | 31 |  | 
|---|
|  | 32 | #include <iostream> | 
|---|
|  | 33 | #include <iomanip> | 
|---|
|  | 34 |  | 
|---|
|  | 35 | #include <math/scmat/matrix.h> | 
|---|
|  | 36 | #include <math/scmat/vector3.h> | 
|---|
|  | 37 | #include <math.h> | 
|---|
|  | 38 |  | 
|---|
|  | 39 | #include <util/misc/formio.h> | 
|---|
|  | 40 | #include <util/keyval/keyval.h> | 
|---|
|  | 41 |  | 
|---|
|  | 42 | using namespace std; | 
|---|
|  | 43 | using namespace sc; | 
|---|
|  | 44 |  | 
|---|
|  | 45 | namespace sc { | 
|---|
|  | 46 |  | 
|---|
|  | 47 | //////////////////////////////////////////////////////////////////////// | 
|---|
|  | 48 | // DVector3 | 
|---|
|  | 49 |  | 
|---|
|  | 50 | SCVector3::SCVector3(const Ref<KeyVal>&keyval) | 
|---|
|  | 51 | { | 
|---|
|  | 52 | _v[0] = keyval->doublevalue(0); | 
|---|
|  | 53 | _v[1] = keyval->doublevalue(1); | 
|---|
|  | 54 | _v[2] = keyval->doublevalue(2); | 
|---|
|  | 55 | } | 
|---|
|  | 56 |  | 
|---|
|  | 57 | SCVector3::SCVector3(const RefSCVector&x) | 
|---|
|  | 58 | { | 
|---|
|  | 59 | if (x.dim().n() != 3) { | 
|---|
|  | 60 | ExEnv::errn() << indent << "SCVector3::SCVector3(RefSCVEctor&): bad length\n"; | 
|---|
|  | 61 | abort(); | 
|---|
|  | 62 | } | 
|---|
|  | 63 | _v[0] = x.get_element(0); | 
|---|
|  | 64 | _v[1] = x.get_element(1); | 
|---|
|  | 65 | _v[2] = x.get_element(2); | 
|---|
|  | 66 | }; | 
|---|
|  | 67 |  | 
|---|
|  | 68 | SCVector3 | 
|---|
|  | 69 | operator*(double d,const SCVector3& v) | 
|---|
|  | 70 | { | 
|---|
|  | 71 | SCVector3 result; | 
|---|
|  | 72 | for (int i=0; i<3; i++) result[i] = d * v[i]; | 
|---|
|  | 73 | return result; | 
|---|
|  | 74 | } | 
|---|
|  | 75 |  | 
|---|
|  | 76 | SCVector3 SCVector3::operator*(double d) const | 
|---|
|  | 77 | { | 
|---|
|  | 78 | return d*(*this); | 
|---|
|  | 79 | } | 
|---|
|  | 80 |  | 
|---|
|  | 81 | SCVector3 SCVector3::cross(const SCVector3&v) const | 
|---|
|  | 82 | { | 
|---|
|  | 83 | SCVector3 result(_v[1]*v._v[2]-_v[2]*v._v[1], | 
|---|
|  | 84 | _v[2]*v._v[0]-_v[0]*v._v[2], | 
|---|
|  | 85 | _v[0]*v._v[1]-_v[1]*v._v[0]); | 
|---|
|  | 86 | return result; | 
|---|
|  | 87 | } | 
|---|
|  | 88 |  | 
|---|
|  | 89 | SCVector3 SCVector3::perp_unit(const SCVector3&v) const | 
|---|
|  | 90 | { | 
|---|
|  | 91 | // try the cross product | 
|---|
|  | 92 | SCVector3 result(_v[1]*v._v[2]-_v[2]*v._v[1], | 
|---|
|  | 93 | _v[2]*v._v[0]-_v[0]*v._v[2], | 
|---|
|  | 94 | _v[0]*v._v[1]-_v[1]*v._v[0]); | 
|---|
|  | 95 | double resultdotresult = result.dot(result); | 
|---|
|  | 96 | if (resultdotresult < 1.e-16) { | 
|---|
|  | 97 | // the cross product is too small to normalize | 
|---|
|  | 98 |  | 
|---|
|  | 99 | // find the largest of this and v | 
|---|
|  | 100 | double dotprodt = this->dot(*this); | 
|---|
|  | 101 | double dotprodv = v.dot(v); | 
|---|
|  | 102 | const SCVector3 *d; | 
|---|
|  | 103 | double dotprodd; | 
|---|
|  | 104 | if (dotprodt < dotprodv) { | 
|---|
|  | 105 | d = &v; | 
|---|
|  | 106 | dotprodd = dotprodv; | 
|---|
|  | 107 | } | 
|---|
|  | 108 | else { | 
|---|
|  | 109 | d = this; | 
|---|
|  | 110 | dotprodd = dotprodt; | 
|---|
|  | 111 | } | 
|---|
|  | 112 | // see if d is big enough | 
|---|
|  | 113 | if (dotprodd < 1.e-16) { | 
|---|
|  | 114 | // choose an arbitrary vector, since the biggest vector is small | 
|---|
|  | 115 | result[0] = 1.0; | 
|---|
|  | 116 | result[1] = 0.0; | 
|---|
|  | 117 | result[2] = 0.0; | 
|---|
|  | 118 | return result; | 
|---|
|  | 119 | } | 
|---|
|  | 120 | else { | 
|---|
|  | 121 | // choose a vector perpendicular to d | 
|---|
|  | 122 | // choose it in one of the planes xy, xz, yz | 
|---|
|  | 123 | // choose the plane to be that which contains the two largest | 
|---|
|  | 124 | // components of d | 
|---|
|  | 125 | double absd[3]; | 
|---|
|  | 126 | absd[0] = fabs(d->_v[0]); | 
|---|
|  | 127 | absd[1] = fabs(d->_v[1]); | 
|---|
|  | 128 | absd[2] = fabs(d->_v[2]); | 
|---|
|  | 129 | int axis0, axis1; | 
|---|
|  | 130 | if (absd[0] < absd[1]) { | 
|---|
|  | 131 | axis0 = 1; | 
|---|
|  | 132 | if (absd[0] < absd[2]) { | 
|---|
|  | 133 | axis1 = 2; | 
|---|
|  | 134 | } | 
|---|
|  | 135 | else { | 
|---|
|  | 136 | axis1 = 0; | 
|---|
|  | 137 | } | 
|---|
|  | 138 | } | 
|---|
|  | 139 | else { | 
|---|
|  | 140 | axis0 = 0; | 
|---|
|  | 141 | if (absd[1] < absd[2]) { | 
|---|
|  | 142 | axis1 = 2; | 
|---|
|  | 143 | } | 
|---|
|  | 144 | else { | 
|---|
|  | 145 | axis1 = 1; | 
|---|
|  | 146 | } | 
|---|
|  | 147 | } | 
|---|
|  | 148 | result[0] = 0.0; | 
|---|
|  | 149 | result[1] = 0.0; | 
|---|
|  | 150 | result[2] = 0.0; | 
|---|
|  | 151 | // do the pi/2 rotation in the plane | 
|---|
|  | 152 | result[axis0] = d->_v[axis1]; | 
|---|
|  | 153 | result[axis1] = -d->_v[axis0]; | 
|---|
|  | 154 | } | 
|---|
|  | 155 | result.normalize(); | 
|---|
|  | 156 | return result; | 
|---|
|  | 157 | } | 
|---|
|  | 158 | else { | 
|---|
|  | 159 | // normalize the cross product and return the result | 
|---|
|  | 160 | result *= 1.0/sqrt(resultdotresult); | 
|---|
|  | 161 | return result; | 
|---|
|  | 162 | } | 
|---|
|  | 163 | } | 
|---|
|  | 164 |  | 
|---|
|  | 165 | void SCVector3::rotate(double theta,SCVector3&axis) | 
|---|
|  | 166 | { | 
|---|
|  | 167 | SCVector3 result; | 
|---|
|  | 168 | SCVector3 unitaxis = axis; | 
|---|
|  | 169 | unitaxis.normalize(); | 
|---|
|  | 170 |  | 
|---|
|  | 171 | // split this into parallel and perpendicular components along axis | 
|---|
|  | 172 | SCVector3 parallel = axis * (this->dot(axis) / axis.dot(axis)); | 
|---|
|  | 173 | SCVector3 perpendicular = (*this) - parallel; | 
|---|
|  | 174 |  | 
|---|
|  | 175 | // form a unit vector perpendicular to parallel and perpendicular | 
|---|
|  | 176 | SCVector3 third_axis = axis.perp_unit(perpendicular); | 
|---|
|  | 177 | third_axis = third_axis * perpendicular.norm(); | 
|---|
|  | 178 |  | 
|---|
|  | 179 | result = parallel + cos(theta) * perpendicular + sin(theta) * third_axis; | 
|---|
|  | 180 | (*this) = result; | 
|---|
|  | 181 | } | 
|---|
|  | 182 |  | 
|---|
|  | 183 | void SCVector3::normalize() | 
|---|
|  | 184 | { | 
|---|
|  | 185 | double tmp=0.0; | 
|---|
|  | 186 | int i; | 
|---|
|  | 187 | for (i=0; i<3; i++) tmp += _v[i]*_v[i]; | 
|---|
|  | 188 | tmp = 1.0/sqrt(tmp); | 
|---|
|  | 189 | for (i=0; i<3; i++) _v[i] *= tmp; | 
|---|
|  | 190 | } | 
|---|
|  | 191 |  | 
|---|
|  | 192 | double | 
|---|
|  | 193 | SCVector3::maxabs() const | 
|---|
|  | 194 | { | 
|---|
|  | 195 | double result = fabs(_v[0]); | 
|---|
|  | 196 | double tmp; | 
|---|
|  | 197 | if ((tmp = fabs(_v[1])) > result) result = tmp; | 
|---|
|  | 198 | if ((tmp = fabs(_v[2])) > result) result = tmp; | 
|---|
|  | 199 | return result; | 
|---|
|  | 200 | } | 
|---|
|  | 201 |  | 
|---|
|  | 202 | void | 
|---|
|  | 203 | SCVector3::spherical_to_cartesian(SCVector3&cart) const | 
|---|
|  | 204 | { | 
|---|
|  | 205 | cart.spherical_coord(theta(), phi(), r()); | 
|---|
|  | 206 | } | 
|---|
|  | 207 |  | 
|---|
|  | 208 | void SCVector3::print(ostream& os) const | 
|---|
|  | 209 | { | 
|---|
|  | 210 | os << indent << "{" | 
|---|
|  | 211 | << setw(8) << setprecision(5) << x() << " " | 
|---|
|  | 212 | << setw(8) << setprecision(5) << y() << " " | 
|---|
|  | 213 | << setw(8) << setprecision(5) << z() << "}" | 
|---|
|  | 214 | << endl; | 
|---|
|  | 215 | } | 
|---|
|  | 216 |  | 
|---|
|  | 217 | ostream & | 
|---|
|  | 218 | operator<<(ostream&o, const SCVector3 &v) | 
|---|
|  | 219 | { | 
|---|
|  | 220 | o << scprintf("{% 8.5f % 8.5f % 8.5f}", v.x(), v.y(), v.z()); | 
|---|
|  | 221 | return o; | 
|---|
|  | 222 | } | 
|---|
|  | 223 |  | 
|---|
|  | 224 | } | 
|---|
|  | 225 |  | 
|---|
|  | 226 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 227 |  | 
|---|
|  | 228 | // Local Variables: | 
|---|
|  | 229 | // mode: c++ | 
|---|
|  | 230 | // c-file-style: "CLJ" | 
|---|
|  | 231 | // End: | 
|---|