25 #include <tulip/Array.h> 26 #include <tulip/tulipconf.h> 27 #include <tulip/tuliphash.h> 32 #define VECTOR Vector<TYPE, SIZE, OTYPE, DTYPE> 33 #define TEMPLATEVECTOR template <typename TYPE, size_t SIZE, typename OTYPE, typename DTYPE> 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 double(sqrtl(a));
65 template <
typename TYPE,
size_t SIZE,
typename OTYPE =
double,
typename DTYPE = TYPE>
66 class Vector :
public Array<TYPE, SIZE> {
69 memset(this->data(), 0, SIZE *
sizeof(TYPE));
71 inline VECTOR(
const Vector<TYPE, SIZE, OTYPE> &v) {
74 inline VECTOR(
const Vector<TYPE, SIZE + 1, OTYPE> &v) {
77 inline VECTOR(
const TYPE x) {
80 inline VECTOR(
const TYPE x,
const TYPE y) {
81 if (
int(SIZE) - 2 > 0)
82 memset(this->data() + 2, 0, (SIZE - 2) *
sizeof(TYPE));
85 inline VECTOR(
const TYPE x,
const TYPE y,
const TYPE z) {
86 if (
int(SIZE) - 3 > 0)
87 memset(this->data() + 3, 0, (SIZE - 3) *
sizeof(TYPE));
90 inline VECTOR(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z) {
93 inline VECTOR(
const TYPE x,
const TYPE y,
const TYPE z,
const TYPE w) {
96 inline VECTOR(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z,
const TYPE w) {
99 inline VECTOR(
const Vector<TYPE, 3, OTYPE> &v,
const TYPE w) {
103 inline void set(
const TYPE x) {
106 inline void set(
const TYPE x,
const TYPE y) {
111 inline void set(
const TYPE x,
const TYPE y,
const TYPE z) {
117 inline void set(
const TYPE x,
const TYPE y,
const TYPE z,
const TYPE w) {
124 inline void set(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z) {
126 memcpy(this->data(), v.data(), 2 *
sizeof(TYPE));
129 inline void set(
const Vector<TYPE, 2, OTYPE> &v,
const TYPE z,
const TYPE w) {
131 memcpy(this->data(), v.data(), 2 *
sizeof(TYPE));
135 inline void set(
const Vector<TYPE, 3, OTYPE> &v,
const TYPE w) {
137 memcpy(this->data(), v.data(), 3 *
sizeof(TYPE));
140 inline void set(
const Vector<TYPE, SIZE, OTYPE> &v) {
141 memcpy(this->data(), v.data(), SIZE *
sizeof(TYPE));
143 inline void set(
const Vector<TYPE, SIZE + 1, OTYPE> &v) {
144 memcpy(this->data(), v.data(), SIZE *
sizeof(TYPE));
146 inline void get(TYPE &x)
const {
149 inline void get(TYPE &x, TYPE &y)
const {
154 inline void get(TYPE &x, TYPE &y, TYPE &z)
const {
160 inline void get(TYPE &x, TYPE &y, TYPE &z, TYPE &w)
const {
169 inline TYPE x()
const {
172 inline TYPE y()
const {
176 inline TYPE z()
const {
180 inline TYPE w()
const {
201 inline TYPE width()
const {
204 inline TYPE height()
const {
207 inline TYPE depth()
const {
211 inline TYPE &width() {
214 inline TYPE &height() {
217 inline TYPE &depth() {
221 inline TYPE r()
const {
224 inline TYPE g()
const {
227 inline TYPE b()
const {
230 inline TYPE a()
const {
247 inline TYPE s()
const {
250 inline TYPE t()
const {
253 inline TYPE p()
const {
256 inline TYPE q()
const {
273 inline void setX(TYPE xx) {
276 inline void setY(TYPE yy) {
279 inline void setZ(TYPE zz) {
283 inline TYPE getX()
const {
286 inline TYPE getY()
const {
289 inline TYPE getZ()
const {
293 inline void setW(
const TYPE width) {
297 inline void setH(
const TYPE height) {
301 inline void setD(
const TYPE depth) {
305 inline TYPE getW()
const {
308 inline TYPE getH()
const {
311 inline TYPE getD()
const {
315 inline VECTOR &operator*=(
const TYPE);
316 inline VECTOR &operator*=(
const VECTOR &);
317 inline VECTOR &operator/=(
const TYPE);
318 inline VECTOR &operator/=(
const VECTOR &);
319 inline VECTOR &operator+=(
const TYPE);
320 inline VECTOR &operator+=(
const VECTOR &);
321 inline VECTOR &operator-=(
const TYPE);
322 inline VECTOR &operator-=(
const VECTOR &);
323 inline VECTOR &operator^=(
const VECTOR &);
325 inline bool operator>(
const VECTOR &)
const;
326 inline bool operator<(
const VECTOR &)
const;
327 inline bool operator!=(
const VECTOR &)
const;
328 inline bool operator==(
const VECTOR &)
const;
329 inline VECTOR &fill(
const TYPE obj);
330 inline TYPE norm()
const;
331 inline TYPE length()
const {
334 inline VECTOR &normalize() {
337 for (
size_t i = 0; i < SIZE; ++i)
338 tmp += tlpsqr<TYPE, OTYPE>((*
this)[i]);
340 if (tmp < sqrt(std::numeric_limits<TYPE>::epsilon())) {
344 for (
size_t i = 0; i < SIZE; ++i) {
346 (*this)[i] = -tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>((*this)[i]) / tmp);
348 (*
this)[i] = tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>((*this)[i]) / tmp);
353 inline DTYPE dist(
const VECTOR &)
const;
354 inline TYPE dotProduct(
const VECTOR &)
const;
358 inline TYPE dotProduct(
const VECTOR &a,
const VECTOR &b) {
359 return a.dotProduct(b);
363 inline TYPE dist(
const VECTOR &a,
const VECTOR &b) {
373 inline VECTOR minVector(
const VECTOR &u,
const VECTOR &v) {
376 for (
size_t i = 0; i < SIZE; ++i)
377 tmp[i] = std::min(u[i], v[i]);
387 inline VECTOR maxVector(
const VECTOR &u,
const VECTOR &v) {
390 for (
size_t i = 0; i < SIZE; ++i)
391 tmp[i] = std::max(u[i], v[i]);
400 inline void minMaxVectors(
const VECTOR &u,
const VECTOR &v, VECTOR &min, VECTOR &max) {
401 for (
size_t i = 0; i < SIZE; ++i) {
413 inline VECTOR operator*(
const VECTOR &,
const VECTOR &);
415 inline VECTOR operator*(
const TYPE,
const VECTOR &);
417 inline VECTOR operator*(
const VECTOR &,
const TYPE);
420 inline VECTOR operator+(
const VECTOR &,
const VECTOR &);
422 inline VECTOR operator+(
const VECTOR &,
const TYPE);
425 inline VECTOR operator-(
const VECTOR &,
const VECTOR &);
427 inline VECTOR operator-(
const VECTOR &,
const TYPE);
430 inline VECTOR operator/(
const VECTOR &,
const VECTOR &);
432 inline VECTOR operator/(
const VECTOR &,
const TYPE);
435 inline VECTOR operator^(
const VECTOR &,
const VECTOR &);
437 inline VECTOR operator-(
const VECTOR &);
441 typedef Vector<unsigned int, 2> Vec2ui;
445 typedef Vector<unsigned int, 3> Vec3ui;
449 typedef Vector<unsigned int, 4> Vec4ui;
453 typedef Vector<int, 2> Vec2i;
457 typedef Vector<int, 3> Vec3i;
461 typedef Vector<int, 4> Vec4i;
465 typedef Vector<double, 2, long double> Vec2d;
469 typedef Vector<double, 3, long double> Vec3d;
473 typedef Vector<double, 4, long double> Vec4d;
477 typedef Vector<float, 2, double> Vec2f;
481 typedef Vector<float, 3, double> Vec3f;
485 typedef Vector<float, 4, double> Vec4f;
489 static double sqrt(tlp::Vector<float, 5> &v) {
490 return sqrt((
double)v[0]);
493 template class TLP_SCOPE tlp::Vector<unsigned char, 4>;
497 TLP_BEGIN_HASH_NAMESPACE {
499 size_t hash_vector(
const tlp::VECTOR &v) {
502 for (
size_t i = 0; i < SIZE; ++i) {
503 tlp_hash_combine(seed, v[i]);
510 struct hash<tlp::Vec2ui> {
511 inline std::size_t operator()(
const tlp::Vec2ui &v)
const {
512 return hash_vector(v);
517 struct hash<tlp::Vec3ui> {
518 inline std::size_t operator()(
const tlp::Vec3ui &v)
const {
519 return hash_vector(v);
524 struct hash<tlp::Vec4ui> {
525 inline std::size_t operator()(
const tlp::Vec4ui &v)
const {
526 return hash_vector(v);
531 struct hash<tlp::Vec2i> {
532 inline std::size_t operator()(
const tlp::Vec2i &v)
const {
533 return hash_vector(v);
538 struct hash<tlp::Vec3i> {
539 inline std::size_t operator()(
const tlp::Vec3i &v)
const {
540 return hash_vector(v);
545 struct hash<tlp::Vec4i> {
546 inline std::size_t operator()(
const tlp::Vec4i &v)
const {
547 return hash_vector(v);
552 struct hash<tlp::Vec2d> {
553 inline std::size_t operator()(
const tlp::Vec2d &v)
const {
554 return hash_vector(v);
559 struct hash<tlp::Vec3d> {
560 inline std::size_t operator()(
const tlp::Vec3f &v)
const {
561 return hash_vector(v);
566 struct hash<tlp::Vec4d> {
567 inline std::size_t operator()(
const tlp::Vec4d &v)
const {
568 return hash_vector(v);
573 struct hash<tlp::Vec2f> {
574 inline std::size_t operator()(
const tlp::Vec2f &v)
const {
575 return hash_vector(v);
580 struct hash<tlp::Vec3f> {
581 inline std::size_t operator()(
const tlp::Vec3f &v)
const {
582 return hash_vector(v);
587 struct hash<tlp::Vec4f> {
588 inline std::size_t operator()(
const tlp::Vec4f &v)
const {
589 return hash_vector(v);
593 TLP_END_HASH_NAMESPACE
595 #include "cxx/Vector.cxx" 599 #undef TEMPLATEVECTOR 601 #endif // TLP_VECTOR_H