27 #include <tulip/Array.h>
28 #include <tulip/tuliphash.h>
32 #define VECTOR Vector<TYPE,SIZE,OTYPE>
33 #define TEMPLATEVECTOR template <typename TYPE, unsigned int SIZE, typename OTYPE>
37 template<
typename TYPE,
typename OTYPE>
38 inline OTYPE tlpsqr(
const TYPE a) {
39 return static_cast<OTYPE
>(a) * static_cast<OTYPE>(a);
42 template<
typename TYPE,
typename OTYPE>
43 inline TYPE tlpsqrt(
const OTYPE a) {
44 return static_cast<TYPE
>(sqrt(a));
48 inline double tlpsqrt<double, long double>(
long double a) {
49 return static_cast<double>(sqrtl(a));
66 template <
typename TYPE,
unsigned int SIZE,
typename OTYPE =
double>
67 class Vector:
public Array<TYPE,SIZE> {
70 memset( &((*
this)[0]), 0, SIZE *
sizeof(TYPE) );
72 inline VECTOR(
const Vector<TYPE, SIZE, OTYPE> &v) {
76 inline VECTOR(
const Vector<TYPE, SIZE + 1, OTYPE> &v) {
80 explicit inline VECTOR(
const TYPE x) {
89 explicit inline VECTOR(
const TYPE x,
const TYPE y) {
90 if (
int(SIZE) - 2 > 0)
91 memset( &((*
this)[2]), 0, (SIZE - 2) *
sizeof(TYPE) );
96 explicit inline VECTOR(
const TYPE x,
const TYPE y,
const TYPE z) {
97 if (
int(SIZE) - 3 > 0)
98 memset( &((*
this)[3]), 0, (SIZE - 3) *
sizeof(TYPE) );
102 explicit inline VECTOR(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z) {
105 explicit inline VECTOR(
const TYPE x,
const TYPE y,
const TYPE z,
const TYPE w) {
108 explicit inline VECTOR(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z,
const TYPE w) {
111 explicit inline VECTOR(
const Vector<TYPE, 3, OTYPE> &v,
const TYPE w) {
115 inline void set(
const TYPE x) {
118 inline void set(
const TYPE x,
const TYPE y) {
123 inline void set(
const TYPE x,
const TYPE y,
const TYPE z) {
129 inline void set(
const TYPE x,
const TYPE y,
const TYPE z,
const TYPE w) {
136 inline void set(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z) {
138 memcpy( &((*
this)[0]), (
void*)&(v.array[0]), 2 *
sizeof(TYPE) );
141 inline void set(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z,
const TYPE w) {
143 memcpy( &((*
this)[0]), (
void*)&(v.array[0]), 2 *
sizeof(TYPE) );
147 inline void set(
const Vector<TYPE, 3, OTYPE> &v,
const TYPE w) {
149 memcpy( &((*
this)[0]), (
void*)&(v.array[0]), 3 *
sizeof(TYPE) );
152 inline void set(
const Vector<TYPE, SIZE, OTYPE> &v) {
153 memcpy(&((*
this)[0]), (
void*)&(v.array[0]), SIZE *
sizeof(TYPE) );
155 inline void set(
const Vector<TYPE, SIZE + 1, OTYPE> &v) {
156 memcpy(&((*
this)[0]), &(v.array[0]), SIZE *
sizeof(TYPE) );
158 inline void get(TYPE &x)
const {
161 inline void get(TYPE &x,TYPE &y)
const {
166 inline void get(TYPE &x,TYPE &y,TYPE &z)
const {
172 inline void get(TYPE &x,TYPE &y,TYPE &z,TYPE &w)
const {
181 inline TYPE x()
const {
184 inline TYPE y()
const {
188 inline TYPE z()
const {
192 inline TYPE w()
const {
213 inline TYPE width()
const {
216 inline TYPE height()
const {
219 inline TYPE depth()
const {
223 inline TYPE& width() {
226 inline TYPE& height() {
229 inline TYPE& depth() {
233 inline TYPE r()
const {
236 inline TYPE g()
const {
239 inline TYPE b()
const {
242 inline TYPE a()
const {
259 inline TYPE s()
const {
262 inline TYPE t()
const {
265 inline TYPE p()
const {
268 inline TYPE q()
const {
285 inline void setX(TYPE xx) {
288 inline void setY(TYPE yy) {
291 inline void setZ(TYPE zz) {
295 inline TYPE getX()
const {
298 inline TYPE getY()
const {
301 inline TYPE getZ()
const {
305 inline void setW(
const TYPE width) {
309 inline void setH(
const TYPE height) {
313 inline void setD(
const TYPE depth) {
317 inline TYPE getW()
const {
320 inline TYPE getH()
const {
323 inline TYPE getD()
const {
330 inline VECTOR & operator*=(
const TYPE );
331 inline VECTOR & operator*=(
const VECTOR &);
333 inline VECTOR & operator/=(
const TYPE );
334 inline VECTOR & operator/=(
const VECTOR &);
336 inline VECTOR & operator+=(
const TYPE );
337 inline VECTOR & operator+=(
const VECTOR &);
339 inline VECTOR & operator-=(
const TYPE );
340 inline VECTOR & operator-=(
const VECTOR &);
341 inline VECTOR & operator^=(
const VECTOR &);
343 inline bool operator>(
const VECTOR &)
const;
344 inline bool operator<(
const VECTOR &)
const;
345 inline bool operator!=(
const VECTOR &)
const;
346 inline bool operator==(
const VECTOR &)
const;
347 inline VECTOR & fill(
const TYPE obj);
348 inline TYPE norm ()
const;
349 inline TYPE length ()
const {
352 inline VECTOR & normalize () {
355 for (
unsigned int i=0; i<SIZE; ++i)
356 tmp += tlpsqr<TYPE, OTYPE>((*
this)[i]);
358 if (tmp < sqrt(std::numeric_limits<TYPE>::epsilon())) {
362 for (
unsigned int i=0; i<SIZE; ++i) {
364 (*this)[i] = -tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>((*this)[i]) / tmp);
366 (*
this)[i] = tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>((*this)[i]) / tmp);
371 inline TYPE dist (
const VECTOR &)
const;
372 inline TYPE dotProduct(
const VECTOR &)
const;
376 inline TYPE dotProduct(
const VECTOR &a,
const VECTOR &b) {
377 return a.dotProduct(b);
381 inline TYPE dist(
const VECTOR &a,
const VECTOR &b) {
392 inline VECTOR minVector(
const VECTOR &u,
const VECTOR &v) {
395 for(
unsigned int i = 0; i<SIZE; ++i)
396 tmp[i] = std::min(u[i], v[i]);
406 inline VECTOR maxVector(
const VECTOR &u,
const VECTOR &v) {
409 for(
unsigned int i = 0; i<SIZE; ++i)
410 tmp[i] = std::max(u[i], v[i]);
416 inline VECTOR operator*(
const VECTOR &,
const VECTOR &);
418 inline VECTOR operator*(
const TYPE ,
const VECTOR &);
420 inline VECTOR operator*(
const VECTOR &,
const TYPE );
428 inline VECTOR operator+(
const VECTOR &,
const VECTOR &);
430 inline VECTOR operator+(
const VECTOR &,
const TYPE );
435 inline VECTOR operator-(
const VECTOR &,
const VECTOR &);
437 inline VECTOR operator-(
const VECTOR &,
const TYPE );
442 inline VECTOR operator/(
const VECTOR &,
const VECTOR &);
444 inline VECTOR operator/(
const VECTOR &,
const TYPE );
449 inline VECTOR operator^(
const VECTOR &,
const VECTOR &);
451 inline VECTOR operator-(
const VECTOR&);
455 typedef Vector<unsigned int, 2> Vec2ui;
459 typedef Vector<unsigned int, 3> Vec3ui;
463 typedef Vector<unsigned int, 4> Vec4ui;
467 typedef Vector<int, 2> Vec2i;
471 typedef Vector<int, 3> Vec3i;
475 typedef Vector<int, 4> Vec4i;
479 typedef Vector<double, 2, long double> Vec2d;
483 typedef Vector<double, 3, long double> Vec3d;
487 typedef Vector<double, 4, long double> Vec4d;
491 typedef Vector<float, 2, double> Vec2f;
495 typedef Vector<float, 3, double> Vec3f;
499 typedef Vector<float, 4, double> Vec4f;
504 static double sqrt(tlp::Vector<float, 5>& v) {
505 return sqrt((
double)v[0]);
509 TLP_BEGIN_HASH_NAMESPACE {
511 size_t hash_vector(
const tlp::VECTOR &v) {
514 for (
unsigned int i = 0 ; i < SIZE ; ++i) {
515 hash_combine(seed, v[i]);
522 struct hash<tlp::Vec2ui> {
523 inline std::size_t operator()(
const tlp::Vec2ui &v)
const {
524 return hash_vector(v);
529 struct hash<tlp::Vec3ui> {
530 inline std::size_t operator()(
const tlp::Vec3ui &v)
const {
531 return hash_vector(v);
536 struct hash<tlp::Vec4ui> {
537 inline std::size_t operator()(
const tlp::Vec4ui &v)
const {
538 return hash_vector(v);
543 struct hash<tlp::Vec2i> {
544 inline std::size_t operator()(
const tlp::Vec2i &v)
const {
545 return hash_vector(v);
550 struct hash<tlp::Vec3i> {
551 inline std::size_t operator()(
const tlp::Vec3i &v)
const {
552 return hash_vector(v);
557 struct hash<tlp::Vec4i> {
558 inline std::size_t operator()(
const tlp::Vec4i &v)
const {
559 return hash_vector(v);
564 struct hash<tlp::Vec2d> {
565 inline std::size_t operator()(
const tlp::Vec2d &v)
const {
566 return hash_vector(v);
571 struct hash<tlp::Vec3d> {
572 inline std::size_t operator()(
const tlp::Vec3f &v)
const {
573 return hash_vector(v);
578 struct hash<tlp::Vec4d> {
579 inline std::size_t operator()(
const tlp::Vec4d &v)
const {
580 return hash_vector(v);
585 struct hash<tlp::Vec2f> {
586 inline std::size_t operator()(
const tlp::Vec2f &v)
const {
587 return hash_vector(v);
592 struct hash<tlp::Vec3f> {
593 inline std::size_t operator()(
const tlp::Vec3f &v)
const {
594 return hash_vector(v);
599 struct hash<tlp::Vec4f> {
600 inline std::size_t operator()(
const tlp::Vec4f &v)
const {
601 return hash_vector(v);
605 } TLP_END_HASH_NAMESPACE
607 #include "cxx/Vector.cxx"