Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/minivector/vec2d.h

raw symbols SCIP
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 A simple class for vector algebra on 2D vectors
32 (summation, normalization, dot product, etc.).
33 */
34
35 #ifndef _MINIVEC2D_H_
36 #define _MINIVEC2D_H_
37
38 #include <math.h>
39 #include <ostream>
40
41 class Vec2d {
42 public:
43
44 inline Vec2d() {}
45 inline Vec2d(double x, double y) { elt[0]=x; elt[1]=y; }
46 inline Vec2d(const double v[2]) { elt[0]=v[0]; elt[1]=v[1]; }
47 inline Vec2d(double entry); // // create a vector with all entries "entry" (can create zero vector for entry=0.0)
48
49 inline Vec2d & operator=(const Vec2d & source);
50 inline bool operator==(const Vec2d & vec2);
51
52 inline Vec2d operator+ (const Vec2d & vec2);
53 inline Vec2d & operator+= (const Vec2d & vec2);
54
55 inline Vec2d operator- (const Vec2d & vec2);
56 inline Vec2d & operator-= (const Vec2d & vec2);
57
58 inline Vec2d operator* (double scalar) const;
59 inline Vec2d & operator*= (double scalar);
60
61 inline Vec2d operator/ (double scalar) const;
62 inline Vec2d & operator/= (double scalar);
63
64 friend inline Vec2d operator* (double scalar, const Vec2d & vec2);
65 friend inline Vec2d operator/ (double scalar, const Vec2d & vec2);
66
67 friend inline double dot(const Vec2d & vec1, const Vec2d & vec2); // dot product
68
69 friend inline Vec2d norm(const Vec2d & vec1); // returns normalized vector (unit length)
70 friend inline std::ostream &operator << (std::ostream &s, const Vec2d &v);
71
72 friend class Mat3d;
73
74 inline double & operator[] (int index); // v[i] returns i-th vector component
75 inline const double & operator[] (int index) const;
76
77 protected:
78 double elt[2];
79 };
80
81 // === below is the implementation ===
82
83 inline Vec2d::Vec2d(double entry)
84 {
85 elt[0] = entry;
86 elt[1] = entry;
87 }
88
89 inline Vec2d & Vec2d::operator=(const Vec2d & source)
90 {
91 elt[0] = source.elt[0];
92 elt[1] = source.elt[1];
93
94 return *this;
95 }
96
97 inline bool Vec2d::operator==(const Vec2d & vec2)
98 {
99 return ((elt[0] == vec2[0]) &&
100 (elt[1] == vec2[1]));
101 }
102
103 inline Vec2d operator* (double scalar, const Vec2d & vec2)
104 {
105 Vec2d result = vec2;
106 result.elt[0] *= scalar;
107 result.elt[1] *= scalar;
108
109 return result;
110 }
111
112 inline Vec2d operator/ (double scalar, const Vec2d & vec2)
113 {
114 Vec2d result = vec2;
115 result.elt[0] /= scalar;
116 result.elt[1] /= scalar;
117
118 return result;
119 }
120
121 inline Vec2d Vec2d::operator+ (const Vec2d & vec2)
122 {
123 Vec2d sum = *this;
124 sum.elt[0] += vec2.elt[0];
125 sum.elt[1] += vec2.elt[1];
126
127 return sum;
128 }
129
130 inline Vec2d & Vec2d::operator+= (const Vec2d & vec2)
131 {
132 elt[0] += vec2.elt[0];
133 elt[1] += vec2.elt[1];
134
135 return *this;
136 }
137
138 inline Vec2d Vec2d::operator- (const Vec2d & vec2)
139 {
140 Vec2d sum = *this;
141 sum.elt[0] -= vec2.elt[0];
142 sum.elt[1] -= vec2.elt[1];
143
144 return sum;
145 }
146
147 inline Vec2d & Vec2d::operator-= (const Vec2d & vec2)
148 {
149 elt[0] -= vec2.elt[0];
150 elt[1] -= vec2.elt[1];
151
152 return *this;
153 }
154
155 inline double & Vec2d::operator[] (int index)
156 {
157 return elt[index];
158 }
159
160 inline const double & Vec2d::operator[] (int index) const
161 {
162 return elt[index];
163 }
164
165 inline double dot(const Vec2d & vec1, const Vec2d & vec2)
166 {
167 return (vec1.elt[0] * vec2.elt[0] + vec1.elt[1] * vec2.elt[1]);
168 }
169
170 inline Vec2d norm(const Vec2d & vec1)
171 {
172 double norm2 = dot(vec1,vec1);
173 Vec2d result = vec1;
174 result *= 1.0 / sqrt(norm2);
175
176 return result;
177 }
178
179 inline Vec2d & Vec2d::operator*= (double scalar)
180 {
181 elt[0] *= scalar;
182 elt[1] *= scalar;
183 return *this;
184 }
185
186 inline Vec2d Vec2d::operator* (double scalar) const
187 {
188 return (Vec2d(elt[0]*scalar,elt[1]*scalar));
189 }
190
191 inline Vec2d Vec2d::operator/ (double scalar) const
192 {
193 return (Vec2d(elt[0]/scalar,elt[1]/scalar));
194 }
195
196 inline Vec2d & Vec2d::operator/= (double scalar)
197 {
198 elt[0] /= scalar;
199 elt[1] /= scalar;
200 return *this;
201 }
202
203 inline double len(const Vec2d & vec1)
204 {
205 return(sqrt(dot(vec1,vec1)));
206 }
207
208 inline std::ostream &operator << (std::ostream &s, const Vec2d &v)
209 {
210 double a = v[0];
211 double b = v[1];
212
213 return(s << '[' << a << ' ' << b << ']');
214 }
215
216 #endif
217