FOX/ObjCryst++  1.10.X (development)
mpVector.cpp
1 /* ObjCryst++ Object-Oriented Crystallographic Library
2  (c) 2000-2002 Vincent Favre-Nicolin vincefn@users.sourceforge.net
3  2000-2001 University of Geneva (Switzerland)
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
20 //
21 // FileName: mpVector.cpp
22 // Author : Michael Y. Polyakov
23 // email : myp@andrew.cmu.edu or mikepolyakov@hotmail.com
24 // Website : www.angelfire.com/linux/myp
25 // Date : 7/16/2002
26 //
27 // Provides basic vector handling.
29 
30 #include "mpVector.h"
31 
32 mpVector::mpVector(float xx, float yy, float zz) :
33  x(xx), y(yy), z(zz)
34  { }
35 
36 mpVector::mpVector() : x(0), y(0), z(0)
37  { }
38 mpVector::mpVector(const mpVector& other) : x(other.x), y(other.y), z(other.z)
39  { }
40 
41 mpVector& mpVector::Normalize()
42 {
43  float length = sqrt(x*x + y*y + z*z);
44  if(!length) return *this;
45  x /= length;
46  y /= length;
47  z /= length;
48  return *this;
49 }
50 
51 float mpVector::Magnitude()
52 {
53  return sqrt(x*x + y*y + z*z);
54 }
55 
56 mpVector mpVector::Cross(const mpVector& other)
57 {
58  return mpVector(y*other.z-z*other.y, z*other.x-x*other.z, x*other.y-y*other.x);
59 }
60 
61 mpVector mpVector::operator - (mpVector v)
62 {
63  return mpVector(x - v.x, y - v.y, z - v.z);
64 }
65 
66 mpVector mpVector::operator + (mpVector v)
67 {
68  return mpVector(x + v.x, y + v.y, z + v.z);
69 }
70 
71 float mpVector ::operator * (mpVector v)
72 {
73  return x*v.x + y*v.y + z*v.z;
74 }
75 mpVector mpVector::operator - (float c)
76 {
77  return mpVector(x-c, y-c, z-c);
78 }
79 
80 mpVector mpVector::operator + (float c)
81 {
82  return mpVector(x+c, y+c, z+c);
83 }
84 
85 mpVector mpVector::operator / (float c)
86 {
87  return mpVector(x/c, y/c, z/c);
88 }
89 
90 mpVector mpVector::operator * (float c)
91 {
92  return mpVector(x*c, y*c, z*c);
93 }
94 
95 void mpVector::operator = (const mpVector& other)
96 {
97  x = other.x;
98  y = other.y;
99  z = other.z;
100 }
101 
102 mpVector::operator mp4Vector() const
103 {
104  return mp4Vector(*this);
105 }
106 
107 mpVector::operator char*() const
108 {
109  return (char*)wxString::Format(_T("(%f %f %f)"), x, y, z).char_str();
110 }
111 
112 
113 
114 
115 
116 mp4Vector::mp4Vector() : x(0), y(0), z(0), val(0)
117 { }
118 
119 mp4Vector::mp4Vector(float aa, float bb, float cc, float dd) :
120  x(aa), y(bb), z(cc), val(dd)
121 { }
122 
123 mp4Vector::mp4Vector(const mp4Vector& other) :
124  x(other.x), y(other.y), z(other.z), val(other.val)
125 { }
126 
127 mp4Vector::mp4Vector(const mpVector& v, const float value) :
128  x(v.x), y(v.x), z(v.z), val(value)
129 { }
130 
131 void mp4Vector::operator = (const mp4Vector& v)
132 {
133  x = v.x; y = v.y; z = v.z; val = v.val;
134 }
135 
136 void mp4Vector::operator = (const mpVector& v)
137 {
138  x = v.x; y = v.y; z = v.z;
139 }
140 
141 mp4Vector::operator mpVector() const
142 {
143  return mpVector(x, y, z);
144 }
145 
146 bool mp4Vector::operator == (const mp4Vector& v) const
147 {
148  return x == v.x && y == v.y && z == v.z && val == v.val;
149 }