1 | #ifndef MXVECTOR_INCLUDED // -*- C++ -*-
|
---|
2 | #define MXVECTOR_INCLUDED
|
---|
3 | #if !defined(__GNUC__)
|
---|
4 | # pragma once
|
---|
5 | #endif
|
---|
6 |
|
---|
7 | /************************************************************************
|
---|
8 |
|
---|
9 | Generic n-dimensional vector class
|
---|
10 |
|
---|
11 | Copyright (C) 1998 Michael Garland. See "COPYING.txt" for details.
|
---|
12 |
|
---|
13 | $Id: MxVector.h,v 1.1 2002/09/24 16:53:54 wimmer Exp $
|
---|
14 |
|
---|
15 | ************************************************************************/
|
---|
16 |
|
---|
17 | #include "MxMath.h"
|
---|
18 |
|
---|
19 | #ifdef __T
|
---|
20 | #undef __T
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | #define __T float
|
---|
24 | #include "mixvops.h"
|
---|
25 |
|
---|
26 | #define __T double
|
---|
27 | #include "mixvops.h"
|
---|
28 |
|
---|
29 | ////////////////////////////////////////////////////////////////////////
|
---|
30 | //
|
---|
31 | // MxVBlock -- fixed-size vector class template
|
---|
32 | //
|
---|
33 | // The idea here is that we want a small collection of vector classes,
|
---|
34 | // {2,3,4}D say, and we want *zero* space overhead. No length fields,
|
---|
35 | // no vtables, just N numerical values.
|
---|
36 | //
|
---|
37 | template<class T, unsigned int N>
|
---|
38 | class MxVBlock
|
---|
39 | {
|
---|
40 | private:
|
---|
41 | T elt[N];
|
---|
42 |
|
---|
43 | protected:
|
---|
44 | inline void copy(const MxVBlock<T,N>& v)
|
---|
45 | { for(uint i=0; i<N; i++) elt[i]=v.elt[i]; }
|
---|
46 |
|
---|
47 | public:
|
---|
48 | MxVBlock() { }
|
---|
49 | MxVBlock(const MxVBlock<T,N>& v) { mxv_set(elt, v, N); }
|
---|
50 | MxVBlock(const T *v) { mxv_set(elt, v, N); }
|
---|
51 |
|
---|
52 | inline unsigned int dim() const { return N; }
|
---|
53 |
|
---|
54 | T& operator()(uint i) { AssertBound(i<N); return elt[i]; }
|
---|
55 | T operator()(uint i) const { AssertBound(i<N); return elt[i]; }
|
---|
56 |
|
---|
57 | operator T*() { return elt; }
|
---|
58 | operator const T*() const { return elt; }
|
---|
59 |
|
---|
60 |
|
---|
61 | // In-place arithmetic methods
|
---|
62 | //
|
---|
63 | inline MxVBlock<T,N>& operator=(const MxVBlock<T,N>& v)
|
---|
64 | { mxv_set(elt, v, N); return *this; }
|
---|
65 | inline MxVBlock<T,N>& operator+=(const MxVBlock<T,N>& v)
|
---|
66 | { mxv_addinto(elt, v, N); return *this; }
|
---|
67 | inline MxVBlock<T,N>& operator-=(const MxVBlock<T,N>& v)
|
---|
68 | { mxv_subfrom(elt, v, N); return *this; }
|
---|
69 | inline MxVBlock<T,N>& operator*=(T s) { mxv_scale(elt,s,N); return *this; }
|
---|
70 | inline MxVBlock<T,N>& operator/=(T s) {mxv_invscale(elt,s,N);return *this;}
|
---|
71 | inline MxVBlock<T,N>& negate() { mxv_neg(elt, N); return *this; }
|
---|
72 |
|
---|
73 | // Binary arithmetic methods
|
---|
74 | //
|
---|
75 | inline MxVBlock<T,N> operator+(const MxVBlock<T,N>& v) const
|
---|
76 | { MxVBlock<T,N> r; mxv_add(r, elt, v, N); return r; }
|
---|
77 | inline MxVBlock<T,N> operator-(const MxVBlock<T,N>& v) const
|
---|
78 | { MxVBlock<T,N> r; mxv_sub(r, elt, v, N); return r; }
|
---|
79 | inline MxVBlock<T,N> operator*(T s) const
|
---|
80 | { MxVBlock<T,N> r; mxv_scale(r, elt, s, N); return r; }
|
---|
81 | inline MxVBlock<T,N> operator/(T s) const
|
---|
82 | { MxVBlock<T,N> r; mxv_invscale(r, elt, s, N); return r; }
|
---|
83 | inline MxVBlock<T,N> operator-() const
|
---|
84 | { MxVBlock<T,N> r; mxv_neg(r, elt, N); return r; }
|
---|
85 |
|
---|
86 | inline T operator*(const MxVBlock<T,N>& v) const {return mxv_dot(elt,v,N);}
|
---|
87 |
|
---|
88 | // Comparison operators
|
---|
89 | inline bool operator==(const MxVBlock<T,N>& v) const
|
---|
90 | { return mxv_equal(elt, v, N); }
|
---|
91 | inline bool operator!=(const MxVBlock<T,N>& v) const
|
---|
92 | { return !mxv_equal(elt, v, N); }
|
---|
93 | };
|
---|
94 |
|
---|
95 |
|
---|
96 | ////////////////////////////////////////////////////////////////////////
|
---|
97 | //
|
---|
98 | // MxVector -- arbitrarily sized vectors
|
---|
99 | //
|
---|
100 | // With MxVector vectors, we're willing to pay a little per-vector space
|
---|
101 | // overhead to achieve greater flexibility without having to instantiate
|
---|
102 | // a template class for every single dimension we care about.
|
---|
103 | //
|
---|
104 |
|
---|
105 | #include "MxBlock.h"
|
---|
106 |
|
---|
107 | class MxVector : public MxBlock<double>
|
---|
108 | {
|
---|
109 | public:
|
---|
110 | MxVector(unsigned int n) : MxBlock<double>(n) {mxv_set(*this,0.0,dim());}
|
---|
111 | MxVector(const MxVector& v) : MxBlock<double>(v.length()) { copy(v); }
|
---|
112 |
|
---|
113 | MxVector& operator=(const MxVector& v) { copy(v); return *this; }
|
---|
114 | MxVector& operator=(double d) { mxv_set(*this, d, dim()); return *this; }
|
---|
115 |
|
---|
116 | unsigned int dim() const { return length(); }
|
---|
117 |
|
---|
118 | MxVector& operator+=(const MxVector& v)
|
---|
119 | { mxv_addinto(*this, v, dim()); return *this; }
|
---|
120 | MxVector& operator-=(const MxVector& v)
|
---|
121 | { mxv_subfrom(*this, v, dim()); return *this; }
|
---|
122 | MxVector& operator*=(double d)
|
---|
123 | { mxv_scale(*this, d, dim()); return *this; }
|
---|
124 | MxVector& operator/=(double d)
|
---|
125 | { mxv_invscale(*this, d, dim()); return *this; }
|
---|
126 |
|
---|
127 | double operator*(const MxVector& v) const {return mxv_dot(*this,v,dim());}
|
---|
128 | };
|
---|
129 |
|
---|
130 |
|
---|
131 | // Convenient wrappers for mixvops functionality
|
---|
132 | //
|
---|
133 | inline double norm(const MxVector& v) { return mxv_norm(v, v.dim()); }
|
---|
134 | inline double norm2(const MxVector& v) { return mxv_dot(v, v, v.dim()); }
|
---|
135 | inline double unitize(MxVector& v) { return mxv_unitize(v, v.dim()); }
|
---|
136 |
|
---|
137 | inline ostream&
|
---|
138 | operator<<(ostream& out,const MxVector& v) { return mxv_write(out,v,v.dim()); }
|
---|
139 | inline istream&
|
---|
140 | operator>>(istream& in, MxVector& v) { return mxv_read(in, v, v.dim()); }
|
---|
141 |
|
---|
142 | // MXVECTOR_INCLUDED
|
---|
143 | #endif
|
---|