vhacdVector.h
Go to the documentation of this file.
1 /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
2  All rights reserved.
3 
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 
9  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 
11  3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  */
15 #pragma once
16 #ifndef VHACD_VECTOR_H
17 #define VHACD_VECTOR_H
18 #include <iostream>
19 #include <math.h>
20 
21 namespace VHACD {
23 template <typename T>
24 class Vec3 {
25 public:
26  T& operator[](size_t i) { return m_data[i]; }
27  const T& operator[](size_t i) const { return m_data[i]; }
28  T& X();
29  T& Y();
30  T& Z();
31  const T& X() const;
32  const T& Y() const;
33  const T& Z() const;
34  void Normalize();
35  T GetNorm() const;
36  void operator=(const Vec3& rhs);
37  void operator+=(const Vec3& rhs);
38  void operator-=(const Vec3& rhs);
39  void operator-=(T a);
40  void operator+=(T a);
41  void operator/=(T a);
42  void operator*=(T a);
43  Vec3 operator^(const Vec3& rhs) const;
44  T operator*(const Vec3& rhs) const;
45  Vec3 operator+(const Vec3& rhs) const;
46  Vec3 operator-(const Vec3& rhs) const;
47  Vec3 operator-() const;
48  Vec3 operator*(T rhs) const;
49  Vec3 operator/(T rhs) const;
50  bool operator<(const Vec3& rhs) const;
51  bool operator>(const Vec3& rhs) const;
52  Vec3();
53  Vec3(T a);
54  Vec3(T x, T y, T z);
55  Vec3(const Vec3& rhs);
56  /*virtual*/ ~Vec3(void);
57 
58  // Compute the center of this bounding box and return the diagonal length
59  T GetCenter(const Vec3 &bmin, const Vec3 &bmax)
60  {
61  X() = (bmin.X() + bmax.X())*0.5;
62  Y() = (bmin.Y() + bmax.Y())*0.5;
63  Z() = (bmin.Z() + bmax.Z())*0.5;
64  T dx = bmax.X() - bmin.X();
65  T dy = bmax.Y() - bmin.Y();
66  T dz = bmax.Z() - bmin.Z();
67  T diagonal = T(sqrt(dx*dx + dy*dy + dz*dz));
68  return diagonal;
69  }
70 
71  // Update the min/max values relative to this point
72  void UpdateMinMax(Vec3 &bmin,Vec3 &bmax) const
73  {
74  if (X() < bmin.X())
75  {
76  bmin.X() = X();
77  }
78  if (Y() < bmin.Y())
79  {
80  bmin.Y() = Y();
81  }
82  if (Z() < bmin.Z())
83  {
84  bmin.Z() = Z();
85  }
86  if (X() > bmax.X())
87  {
88  bmax.X() = X();
89  }
90  if (X() > bmax.X())
91  {
92  bmax.X() = X();
93  }
94  if (Y() > bmax.Y())
95  {
96  bmax.Y() = Y();
97  }
98  if (Z() > bmax.Z())
99  {
100  bmax.Z() = Z();
101  }
102  }
103 
104  // Returns the squared distance between these two points
105  T GetDistanceSquared(const Vec3 &p) const
106  {
107  T dx = X() - p.X();
108  T dy = Y() - p.Y();
109  T dz = Z() - p.Z();
110  return dx*dx + dy*dy + dz*dz;
111  }
112 
113  T GetDistance(const Vec3 &p) const
114  {
115  return sqrt(GetDistanceSquared(p));
116  }
117 
118  // Returns the raw vector data as a pointer
119  T* GetData(void)
120  {
121  return m_data;
122  }
123 private:
124  T m_data[3];
125 };
127 template <typename T>
128 class Vec2 {
129 public:
130  T& operator[](size_t i) { return m_data[i]; }
131  const T& operator[](size_t i) const { return m_data[i]; }
132  T& X();
133  T& Y();
134  const T& X() const;
135  const T& Y() const;
136  void Normalize();
137  T GetNorm() const;
138  void operator=(const Vec2& rhs);
139  void operator+=(const Vec2& rhs);
140  void operator-=(const Vec2& rhs);
141  void operator-=(T a);
142  void operator+=(T a);
143  void operator/=(T a);
144  void operator*=(T a);
145  T operator^(const Vec2& rhs) const;
146  T operator*(const Vec2& rhs) const;
147  Vec2 operator+(const Vec2& rhs) const;
148  Vec2 operator-(const Vec2& rhs) const;
149  Vec2 operator-() const;
150  Vec2 operator*(T rhs) const;
151  Vec2 operator/(T rhs) const;
152  Vec2();
153  Vec2(T a);
154  Vec2(T x, T y);
155  Vec2(const Vec2& rhs);
156  /*virtual*/ ~Vec2(void);
157 
158 private:
159  T m_data[2];
160 };
161 
162 template <typename T>
163 const bool Colinear(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c);
164 template <typename T>
165 const T ComputeVolume4(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c, const Vec3<T>& d);
166 }
167 #include "vhacdVector.inl" // template implementation
168 #endif
void operator+=(const Vec3 &rhs)
Vec3 operator-() const
Vector dim 2.
Definition: vhacdVector.h:128
T & operator[](size_t i)
Definition: vhacdVector.h:26
SIMD_FORCE_INLINE const btScalar & x() const
Return the x value.
Definition: btVector3.h:275
const bool Colinear(const Vec3< T > &a, const Vec3< T > &b, const Vec3< T > &c)
void operator=(const Vec3 &rhs)
T operator*(const Vec3 &rhs) const
T GetDistance(const Vec3 &p) const
Definition: vhacdVector.h:113
Vec3 operator^(const Vec3 &rhs) const
T & operator[](size_t i)
Definition: vhacdVector.h:130
Vec3 operator+(const Vec3 &rhs) const
Vec3 operator/(T rhs) const
void operator*=(T a)
T * GetData(void)
Definition: vhacdVector.h:119
const T & operator[](size_t i) const
Definition: vhacdVector.h:27
SIMD_FORCE_INLINE const btScalar & y() const
Return the y value.
Definition: btVector3.h:277
void Normalize()
void UpdateMinMax(Vec3 &bmin, Vec3 &bmax) const
Definition: vhacdVector.h:72
bool operator>(const Vec3 &rhs) const
Definition: vhacdCircularList.h:19
bool operator<(const Vec3 &rhs) const
T GetDistanceSquared(const Vec3 &p) const
Definition: vhacdVector.h:105
const T & operator[](size_t i) const
Definition: vhacdVector.h:131
void operator-=(const Vec3 &rhs)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition: btVector3.h:279
T GetNorm() const
void operator/=(T a)
Vector dim 3.
Definition: vhacdVector.h:24
T GetCenter(const Vec3 &bmin, const Vec3 &bmax)
Definition: vhacdVector.h:59
const T ComputeVolume4(const Vec3< T > &a, const Vec3< T > &b, const Vec3< T > &c, const Vec3< T > &d)