1 // -*- C++ -*-
2 // IVec.h: Integer version of the 3-vector class in Vec.h
3 //
4 // Copyright (C) 2008-2011 Jakob Schiotz and Center for Individual
5 // Nanoparticle Functionality, Department of Physics, Technical
6 // University of Denmark.  Email: schiotz@fysik.dtu.dk
7 //
8 // This file is part of Asap version 3.
9 // Asap is released under the GNU Lesser Public License (LGPL) version 3.
10 // However, the parts of Asap distributed within the OpenKIM project
11 // (including this file) are also released under the Common Development
12 // and Distribution License (CDDL) version 1.0.
13 //
14 // This program is free software: you can redistribute it and/or
15 // modify it under the terms of the GNU Lesser General Public License
16 // version 3 as published by the Free Software Foundation.  Permission
17 // to use other versions of the GNU Lesser General Public License may
18 // granted by Jakob Schiotz or the head of department of the
19 // Department of Physics, Technical University of Denmark, as
20 // described in section 14 of the GNU General Public License.
21 //
22 // This program is distributed in the hope that it will be useful,
23 // but WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 // GNU General Public License for more details.
26 //
27 // You should have received a copy of the GNU General Public License
28 // and the GNU Lesser Public License along with this program.  If not,
29 // see <http://www.gnu.org/licenses/>.
30 
31 
32 #ifndef __IVEC_H__
33 #define __IVEC_H__
34 
35 #include <iostream>
36 using std::istream;
37 using std::ostream;
38 
39 namespace ASAPSPACE {
40 
41 /// An integer 3-vector.
42 
43 /// The only data is the three positions (and there are no virtual
44 /// functions), so the memory layout of an array of IVecs will be x0,
45 /// y0, z0, x1, y1, z1, x2, ...
46 ///
47 /// Almost all operations are inline for speed.
48 
49 class IVec
50 {
51 public:
52   /// Dummy constructor needed by STL containers.
IVec()53   IVec() {};
54   /// Construct a 3-vector from three ints.
55   IVec(int x0, int x1, int x2);
56   /// Dot product
57   int operator*(const IVec& v) const;
58   /// Multiplication with scalar
59   IVec operator*(const int& s) const;
60   /// Add two IVecs
61   IVec operator+(const IVec& v) const;
62   /// Subtract two IVecs
63   IVec operator-(const IVec& v) const;
64   /// Unary minus
65   IVec operator-() const;
66   /// Add a IVec to this one.
67   IVec& operator+=(const IVec& v);
68   /// Subtract a vec from this one.
69   IVec& operator-=(const IVec& v);
70   /// Multiply this vec with a scalar
71   IVec& operator*=(int s);
72   /// Divide this IVec with a scalar.
73   IVec& operator/=(int s);
74   /// IVec equality
75   bool operator==(const IVec &v) const;
76   /// const indexing
77   int operator[](int n) const;
78   /// Non-const indexing
79   int& operator[](int n);
80   /// Cross product of two IVecs.
81   friend IVec Cross(const IVec& v1, const IVec& v2);
82   /// The length of a IVec
83   friend int Length2(const IVec& v);
84   /// Increment y with a times x.
85   friend void Vaxpy(int a, const IVec& x, IVec& y);
86   /// Print a Vec
87   friend ostream& operator<<(ostream& out, const Vec& v);
88 public:
89   int x, y, z;
90 };
91 
IVec(int x0,int x1,int x2)92 inline IVec::IVec(int x0, int x1, int x2)
93 {
94   x = x0;
95   y = x1;
96   z = x2;
97 }
98 
99 inline int IVec::operator*(const IVec& v) const
100 {
101   return x * v.x + y * v.y + z * v.z;
102 }
103 
104 inline IVec IVec::operator*(const int& s) const
105 {
106   return IVec(s * x, s * y, s * z);
107 }
108 
109 inline IVec IVec::operator+(const IVec& v) const
110 {
111   return IVec(x + v.x, y + v.y, z + v.z);
112 }
113 
114 inline IVec IVec::operator-(const IVec& v) const
115 {
116   return IVec(x - v.x, y - v.y, z - v.z);
117 }
118 
119 inline IVec IVec::operator-() const
120 {
121   return IVec(-x, -y, -z);
122 }
123 
124 inline IVec& IVec::operator+=(const IVec& v)
125 {
126   x += v.x; y += v.y; z += v.z;
127   return *this;
128 }
129 
130 inline IVec& IVec::operator-=(const IVec& v)
131 {
132   x -= v.x; y -= v.y; z -= v.z;
133   return *this;
134 }
135 
136 inline IVec& IVec::operator*=(int s)
137 {
138   x *= s; y *= s; z *= s;
139   return *this;
140 }
141 
142 inline IVec& IVec::operator/=(int s)
143 {
144   x /= s; y /= s; z /= s;
145   return *this;
146 }
147 
148 inline bool IVec::operator==(const IVec &v) const
149 {
150   return (x == v.x) && (y == v.y) && (z == v.z);
151 }
152 
153 inline int IVec::operator[](int n) const
154 {
155   return (&x)[n];
156 }
157 
158 inline int& IVec::operator[](int n)
159 {
160   return (&x)[n];
161 }
162 
Cross(const IVec & v1,const IVec & v2)163 inline IVec Cross(const IVec& v1, const IVec& v2)
164 {
165   return IVec(v1.y * v2.z - v1.z * v2.y,
166              v1.z * v2.x - v1.x * v2.z,
167              v1.x * v2.y - v1.y * v2.x);
168 }
169 
Vaxpy(int a,const IVec & x,IVec & y)170 inline void Vaxpy(int a, const IVec& x, IVec& y)
171 {
172   y.x += a * x.x;
173   y.y += a * x.y;
174   y.z += a * x.z;
175 }
176 
Length2(const IVec & v)177 inline int Length2(const IVec& v)
178 {
179   return v.x * v.x + v.y * v.y + v.z * v.z;
180 }
181 
182 inline ostream& operator<<(ostream& out, const IVec& v)
183 {
184   out << "(" << v.x << ", " << v.y << ", " << v.z << ")";
185   return out;
186 }
187 
188 } // end namespace
189 
190 #endif // __IVEC_H__
191 
192