FaceGen 3 SDKs Reference
Loading...
Searching...
No Matches
vect2.hpp
1//
2// Copyright (c) Singular Inversions Inc. 1998.
3//
4// 2D vector class of templated type. All member functions except
5// rotate can be defined for 'int', 'float' or 'double'.
6//
7// Objects of this class were designed to act like built-ins with respect
8// to operators, with 2 differences: 1. Operators which modify the object
9// (+=,-=,*=,/=) do not return an instance as well, for efficiency. 2.
10// Operators which return an object (all others) return it const to avoid
11// silly things like (a + b) += c;
12//
13// Design Decisions: 'const' member functions are used whenever member
14// values remain unchanged. 'const' return values are used throughout,
15// as described in [Meyers 98] Item 22. Pass by reference is not used
16// for the arguments since 1. Most functions are inline and have no
17// passing, and 2. The data type is almost as small as a pointer anyway.
18
19#ifndef FR2_VECT2_HPP
20#define FR2_VECT2_HPP
21
22#include "FgSerial.hpp"
23
24namespace Fg {
25
26template<class T> class FR2Vect2TC
27{
28public:
29 T x1,x2;
30
31 // Constructors:
32 FR2Vect2TC() : x1(T(0)), x2(T(0)) {}; // default.
33 FR2Vect2TC(const FR2Vect2TC &v) = default;
34 FR2Vect2TC & operator=(FR2Vect2TC const &) = default;
35 explicit FR2Vect2TC(T a) : x1(a), x2(a) {}; // initialize to single val.
36 FR2Vect2TC(T y1,T y2) : x1(y1), x2(y2) {}; // initialize all vals.
37 template<class U>
38 explicit FR2Vect2TC(const FR2Vect2TC<U>& v) {futConvert(v.x1,x1); futConvert(v.x2,x2); }
39
40 T& operator[](unsigned int n)
41 {FGASSERT_FAST(n < 2);return (&x1)[n];}
42 T const& operator[](unsigned int n) const
43 {FGASSERT_FAST(n < 2);return (&x1)[n];}
44
45 void set(T a) {x1 = a;x2 = a;}
46 inline const FR2Vect2TC operator-() const;
47 inline void operator-=(FR2Vect2TC);
48 inline void operator+=(FR2Vect2TC);
49 inline const FR2Vect2TC operator+(const FR2Vect2TC&) const;
50 inline const FR2Vect2TC operator-(const FR2Vect2TC&) const;
51 inline const FR2Vect2TC operator*(T) const;
52 inline const FR2Vect2TC operator/(T) const;
53 inline void operator/=(T);
54 inline void operator*=(T);
55 inline bool operator==(const FR2Vect2TC &v) const
56 {return (x1==v.x1 && x2==v.x2);}
57 inline bool operator!=(const FR2Vect2TC &v) const
58 {return (x1!=v.x1 || x2!=v.x2);}
59 // Note that the MS-specific _hypot is faster for len() and
60 // could be used within an #ifdef WIN32 if speedup is required.
61 inline T const len() const {return((T)sqrt(x1*x1 + x2*x2));}
62 inline T const mag() const {return(x1*x1 + x2*x2);}
63 const FR2Vect2TC rotate(T) const; // This function is only actually
64 // defined for float and double,
65 // not int.
66 inline T const dot(FR2Vect2TC) const; // Dot product.
67 inline T const cross(FR2Vect2TC) const; // Returns Z component
68 // of cross product
69};
70
71// **********************************************************************
72// **********************************************************************
73
77typedef FR2Vect2TC<int> FutVect2IC; // convention.
79
80std::ostream& operator<<(std::ostream& s,FR2Vect2IC vec);
81std::ostream& operator<<(std::ostream& s,FR2Vect2FC vec);
82std::ostream& operator<<(std::ostream& s,FR2Vect2DC vec);
83
84template <class T>
86{
87 bool operator()(const FR2Vect2TC<T> &x, const FR2Vect2TC<T> &y) const
88 { return ((x.x2 < y.x2) ||
89 (x.x2 == y.x2 && x.x1 < y.x1)); }
90};
91
96
97template<class T>
98const inline FR2Vect2TC<T> FR2Vect2TC<T>::operator-() const
99{
100 return FR2Vect2TC<T>(-x1,-x2);
101}
102
103template<class T>
104const inline FR2Vect2TC<T> FR2Vect2TC<T>::operator+(
105 const FR2Vect2TC<T> &v1)
106 const
107{
108 return FR2Vect2TC<T>(x1 + v1.x1,
109 x2 + v1.x2);
110}
111
112template<class T>
113const inline FR2Vect2TC<T> FR2Vect2TC<T>::operator-(
114 const FR2Vect2TC<T> &v1)
115 const
116{
117 return FR2Vect2TC<T>(x1 - v1.x1,
118 x2 - v1.x2);
119}
120
121template<class T>
122inline void FR2Vect2TC<T>::operator-=(
123 FR2Vect2TC<T> v)
124{
125 x1 -= v.x1;
126 x2 -= v.x2;
127}
128
129template<class T>
130inline void FR2Vect2TC<T>::operator+=(
131 FR2Vect2TC<T> v)
132{
133 x1 += v.x1;
134 x2 += v.x2;
135}
136
137template<class T>
138const inline FR2Vect2TC<T> FR2Vect2TC<T>::operator*(
139 T val)
140 const
141{
142 return FR2Vect2TC<T>(x1*val, x2*val);
143}
144
145template<class T>
146const inline FR2Vect2TC<T> FR2Vect2TC<T>::operator/(
147 T val)
148 const
149{
150 FGASSERT_FAST(val != (T)0);
151
152 return FR2Vect2TC<T>(x1/val, x2/val);
153}
154
155template<class T>
156inline void FR2Vect2TC<T>::operator/=(
157 T val)
158{
159 FGASSERT_FAST(val != (T)0);
160 x1 /= val;
161 x2 /= val;
162}
163
164template<class T>
165inline void FR2Vect2TC<T>::operator*=(
166 T val)
167{
168 x1 *= val;
169 x2 *= val;
170}
171
172template<class T>
173inline T const FR2Vect2TC<T>::dot(
174 FR2Vect2TC<T> operand)
175 const
176{
177 return(x1*operand.x1 + x2*operand.x2);
178}
179
180template<class T>
181inline T const FR2Vect2TC<T>::cross(
182 FR2Vect2TC<T> v2)
183 const
184{
185 return (x1*v2.x2 - x2*v2.x1);
186}
187
188}
189
190#endif