27 #include <tulip/Array.h>
28 #include <tulip/tuliphash.h>
33 #define VECTOR Vector<TYPE,SIZE,OTYPE,DTYPE>
34 #define TEMPLATEVECTOR template <typename TYPE, unsigned int SIZE, typename OTYPE, typename DTYPE>
38 template<
typename TYPE,
typename OTYPE>
39 inline OTYPE tlpsqr(
const TYPE a) {
40 return static_cast<OTYPE
>(a) * static_cast<OTYPE>(a);
43 template<
typename TYPE,
typename OTYPE>
44 inline TYPE tlpsqrt(
const OTYPE a) {
45 return static_cast<TYPE
>(sqrt(a));
49 inline double tlpsqrt<double, long double>(
long double a) {
50 return static_cast<double>(sqrtl(a));
67 template <
typename TYPE,
unsigned int SIZE,
typename OTYPE =
double,
typename DTYPE = TYPE>
68 class Vector:
public Array<TYPE,SIZE> {
71 memset( &((*
this)[0]), 0, SIZE *
sizeof(TYPE) );
73 inline VECTOR(
const Vector<TYPE, SIZE, OTYPE> &v) {
77 inline VECTOR(
const Vector<TYPE, SIZE + 1, OTYPE> &v) {
81 explicit inline VECTOR(
const TYPE x) {
90 explicit inline VECTOR(
const TYPE x,
const TYPE y) {
91 if (
int(SIZE) - 2 > 0)
92 memset( &((*
this)[2]), 0, (SIZE - 2) *
sizeof(TYPE) );
97 explicit inline VECTOR(
const TYPE x,
const TYPE y,
const TYPE z) {
98 if (
int(SIZE) - 3 > 0)
99 memset( &((*
this)[3]), 0, (SIZE - 3) *
sizeof(TYPE) );
103 explicit inline VECTOR(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z) {
106 explicit inline VECTOR(
const TYPE x,
const TYPE y,
const TYPE z,
const TYPE w) {
109 explicit inline VECTOR(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z,
const TYPE w) {
112 explicit inline VECTOR(
const Vector<TYPE, 3, OTYPE> &v,
const TYPE w) {
116 inline void set(
const TYPE x) {
119 inline void set(
const TYPE x,
const TYPE y) {
124 inline void set(
const TYPE x,
const TYPE y,
const TYPE z) {
130 inline void set(
const TYPE x,
const TYPE y,
const TYPE z,
const TYPE w) {
137 inline void set(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z) {
139 memcpy( &((*
this)[0]), (
void*)&(v.array[0]), 2 *
sizeof(TYPE) );
142 inline void set(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z,
const TYPE w) {
144 memcpy( &((*
this)[0]), (
void*)&(v.array[0]), 2 *
sizeof(TYPE) );
148 inline void set(
const Vector<TYPE, 3, OTYPE> &v,
const TYPE w) {
150 memcpy( &((*
this)[0]), (
void*)&(v.array[0]), 3 *
sizeof(TYPE) );
153 inline void set(
const Vector<TYPE, SIZE, OTYPE> &v) {
154 memcpy(&((*
this)[0]), (
void*)&(v.array[0]), SIZE *
sizeof(TYPE) );
156 inline void set(
const Vector<TYPE, SIZE + 1, OTYPE> &v) {
157 memcpy(&((*
this)[0]), &(v.array[0]), SIZE *
sizeof(TYPE) );
159 inline void get(TYPE &x)
const {
162 inline void get(TYPE &x,TYPE &y)
const {
167 inline void get(TYPE &x,TYPE &y,TYPE &z)
const {
173 inline void get(TYPE &x,TYPE &y,TYPE &z,TYPE &w)
const {
182 inline TYPE x()
const {
185 inline TYPE y()
const {
189 inline TYPE z()
const {
193 inline TYPE w()
const {
214 inline TYPE width()
const {
217 inline TYPE height()
const {
220 inline TYPE depth()
const {
224 inline TYPE& width() {
227 inline TYPE& height() {
230 inline TYPE& depth() {
234 inline TYPE r()
const {
237 inline TYPE g()
const {
240 inline TYPE b()
const {
243 inline TYPE a()
const {
260 inline TYPE s()
const {
263 inline TYPE t()
const {
266 inline TYPE p()
const {
269 inline TYPE q()
const {
286 inline void setX(TYPE xx) {
289 inline void setY(TYPE yy) {
292 inline void setZ(TYPE zz) {
296 inline TYPE getX()
const {
299 inline TYPE getY()
const {
302 inline TYPE getZ()
const {
306 inline void setW(
const TYPE width) {
310 inline void setH(
const TYPE height) {
314 inline void setD(
const TYPE depth) {
318 inline TYPE getW()
const {
321 inline TYPE getH()
const {
324 inline TYPE getD()
const {
331 inline VECTOR & operator*=(
const TYPE );
332 inline VECTOR & operator*=(
const VECTOR &);
334 inline VECTOR & operator/=(
const TYPE );
335 inline VECTOR & operator/=(
const VECTOR &);
337 inline VECTOR & operator+=(
const TYPE );
338 inline VECTOR & operator+=(
const VECTOR &);
340 inline VECTOR & operator-=(
const TYPE );
341 inline VECTOR & operator-=(
const VECTOR &);
342 inline VECTOR & operator^=(
const VECTOR &);
344 inline bool operator>(
const VECTOR &)
const;
345 inline bool operator<(
const VECTOR &)
const;
346 inline bool operator!=(
const VECTOR &)
const;
347 inline bool operator==(
const VECTOR &)
const;
348 inline VECTOR & fill(
const TYPE obj);
349 inline TYPE norm ()
const;
350 inline TYPE length ()
const {
353 inline VECTOR & normalize () {
356 for (
unsigned int i=0; i<SIZE; ++i)
357 tmp += tlpsqr<TYPE, OTYPE>((*
this)[i]);
359 if (tmp < sqrt(std::numeric_limits<TYPE>::epsilon())) {
363 for (
unsigned int i=0; i<SIZE; ++i) {
365 (*this)[i] = -tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>((*this)[i]) / tmp);
367 (*
this)[i] = tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>((*this)[i]) / tmp);
372 inline DTYPE dist (
const VECTOR &)
const;
373 inline TYPE dotProduct(
const VECTOR &)
const;
377 inline TYPE dotProduct(
const VECTOR &a,
const VECTOR &b) {
378 return a.dotProduct(b);
382 inline TYPE dist(
const VECTOR &a,
const VECTOR &b) {
393 inline VECTOR minVector(
const VECTOR &u,
const VECTOR &v) {
396 for(
unsigned int i = 0; i<SIZE; ++i)
397 tmp[i] = std::min(u[i], v[i]);
407 inline VECTOR maxVector(
const VECTOR &u,
const VECTOR &v) {
410 for(
unsigned int i = 0; i<SIZE; ++i)
411 tmp[i] = std::max(u[i], v[i]);
417 inline VECTOR operator*(
const VECTOR &,
const VECTOR &);
419 inline VECTOR operator*(
const TYPE ,
const VECTOR &);
421 inline VECTOR operator*(
const VECTOR &,
const TYPE );
429 inline VECTOR operator+(
const VECTOR &,
const VECTOR &);
431 inline VECTOR operator+(
const VECTOR &,
const TYPE );
436 inline VECTOR operator-(
const VECTOR &,
const VECTOR &);
438 inline VECTOR operator-(
const VECTOR &,
const TYPE );
443 inline VECTOR operator/(
const VECTOR &,
const VECTOR &);
445 inline VECTOR operator/(
const VECTOR &,
const TYPE );
450 inline VECTOR operator^(
const VECTOR &,
const VECTOR &);
452 inline VECTOR operator-(
const VECTOR&);
456 typedef Vector<unsigned int, 2> Vec2ui;
460 typedef Vector<unsigned int, 3> Vec3ui;
464 typedef Vector<unsigned int, 4> Vec4ui;
468 typedef Vector<int, 2> Vec2i;
472 typedef Vector<int, 3> Vec3i;
476 typedef Vector<int, 4> Vec4i;
480 typedef Vector<double, 2, long double> Vec2d;
484 typedef Vector<double, 3, long double> Vec3d;
488 typedef Vector<double, 4, long double> Vec4d;
492 typedef Vector<float, 2, double> Vec2f;
496 typedef Vector<float, 3, double> Vec3f;
500 typedef Vector<float, 4, double> Vec4f;
505 static double sqrt(tlp::Vector<float, 5>& v) {
506 return sqrt((
double)v[0]);
509 template class TLP_SCOPE tlp::Vector<unsigned char, 4>;
513 TLP_BEGIN_HASH_NAMESPACE {
515 size_t hash_vector(
const tlp::VECTOR &v) {
518 for (
unsigned int i = 0 ; i < SIZE ; ++i) {
519 hash_combine(seed, v[i]);
526 struct hash<tlp::Vec2ui> {
527 inline std::size_t operator()(
const tlp::Vec2ui &v)
const {
528 return hash_vector(v);
533 struct hash<tlp::Vec3ui> {
534 inline std::size_t operator()(
const tlp::Vec3ui &v)
const {
535 return hash_vector(v);
540 struct hash<tlp::Vec4ui> {
541 inline std::size_t operator()(
const tlp::Vec4ui &v)
const {
542 return hash_vector(v);
547 struct hash<tlp::Vec2i> {
548 inline std::size_t operator()(
const tlp::Vec2i &v)
const {
549 return hash_vector(v);
554 struct hash<tlp::Vec3i> {
555 inline std::size_t operator()(
const tlp::Vec3i &v)
const {
556 return hash_vector(v);
561 struct hash<tlp::Vec4i> {
562 inline std::size_t operator()(
const tlp::Vec4i &v)
const {
563 return hash_vector(v);
568 struct hash<tlp::Vec2d> {
569 inline std::size_t operator()(
const tlp::Vec2d &v)
const {
570 return hash_vector(v);
575 struct hash<tlp::Vec3d> {
576 inline std::size_t operator()(
const tlp::Vec3f &v)
const {
577 return hash_vector(v);
582 struct hash<tlp::Vec4d> {
583 inline std::size_t operator()(
const tlp::Vec4d &v)
const {
584 return hash_vector(v);
589 struct hash<tlp::Vec2f> {
590 inline std::size_t operator()(
const tlp::Vec2f &v)
const {
591 return hash_vector(v);
596 struct hash<tlp::Vec3f> {
597 inline std::size_t operator()(
const tlp::Vec3f &v)
const {
598 return hash_vector(v);
603 struct hash<tlp::Vec4f> {
604 inline std::size_t operator()(
const tlp::Vec4f &v)
const {
605 return hash_vector(v);
610 } TLP_END_HASH_NAMESPACE
612 #include "cxx/Vector.cxx"
616 #undef TEMPLATEVECTOR