# Boost :

From: Patrick Kowalzick (Patrick.Kowalzick_at_[hidden])
Date: 2003-05-22 08:11:46

Hi Justinas,

do you mean something like this? Please regard code below.

Using the second parameter of the template arguments to define the size of a
vector, so that you can call specialized functions for the right dimension?

Greets,
Patrick

// *******************************************
template <typename T,std::size_t s>
class vector {
public:
T& operator[](const size_t& pos) { return v[pos]; }
const T& operator[](const size_t& pos) const { return v[pos]; }
private:
T v[s];
};

void cross(vector<double,3> &vr,
const vector<double,3> &v1,
const vector<double,3> &v2) {
vr[0]= v1[1]*v2[2]-v1[2]*v2[1];
vr[1]=-v1[0]*v2[2]+v1[2]*v2[0];
vr[2]= v1[0]*v2[1]-v1[1]*v2[0];
}

// Point
// no partial specialization due to MSVC 6.0
template <typename T,std::size_t s>
class point {};

template <>
class point<double,2> : public vector<double,3> {};
template <>
class point<double,3> : public vector<double,4> {};

// Line
template <typename T,std::size_t s>
class line {};

template <>
class line<double,2> : public vector<double,3> {};

template <>
class line<double,3> : public vector<double,6> {};

// Plane
template <typename T,std::size_t s>
class plane {};

template <>
class plane<double,3> : public vector<double,4> {};

// join-function
void join(line<double,2>& l,
const point<double,2> &p1,
const point<double,2> &p2)
{
cross(l,p1,p2);
}

void join(point<double,2>& l,
const line<double,2> &p1,
const line<double,2> &p2)
{
cross(l,p1,p2);
}

template <typename T,std::size_t s>
std::ostream& operator<< (std::ostream& ostr,vector<T,s> v) {
for (std::size_t i=0;i<s;i++) {
ostr << v[i];
if (i+1!=s) ostr << ",";
}
return ostr;
}

void main() {
point<double,2> p1,p2,p3,p4;
p1[0]=0;p1[1]=0;p1[2]=1;
p2[0]=2;p2[1]=2;p2[2]=1;
p3[0]=2;p3[1]=0;p3[2]=1;
p4[0]=0;p4[1]=2;p4[2]=1;
std::cout << p1 << std::endl;
std::cout << p2 << std::endl;

line<double,2> l1,l2;
join (l1,p1,p2);
join (l2,p3,p4);
std::cout << l1 << std::endl;
std::cout << l2 << std::endl;

point<double,2> ps;
join (ps,l1,l2);
std::cout << ps << std::endl;
}
// *******************************************

"jvd" <vygis_d_at_[hidden]> wrote in message
news:BAY8-DAV10GUjGnAKYo0000061e_at_hotmail.com...
> hello boosters,
>
> subject tells itself about the thing i'm going to
> propose.
>
> AFAIK, there're no vector (i mean
> geometrical vectors) classes in boost
> (well, there's uBLAS version of vectors but that's rather a container
> than a geometrical primitive, correct me if i'm wrong).
>
> idea itself is borrowed from blitz++.
>
> template< typename T, std::size_t N >
> class tiny_vector { /**/ };
>
> vector operations are and should be implemented
> inlinely. there should be no inner loops in additions
> substractions, scalar multiplication/division, etc.
>
> i suppose it would be neat to have boostified vectors
> as we have boostified quaternions, octonions, etc.
>