You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

181 lines
3.8 KiB

#pragma once
#include <numeric>
#include <limits>
#include <cmath>
#include <vector>
template <typename T>
class Vec3
{
public:
T a, b, c;
Vec3(T x = T(), T y = T(), T z = T()) : a(x), b(y), c(z) {} // Ð޸ĺó´úÂë
T operator[](int i) const
{
if (i == 0)
{
return a;
}
else if (i == 1)
{
return b;
}
else if (i == 2)
{
return c;
}
return a;
}
T &operator[](int i)
{
if (i == 0)
{
return a;
}
else if (i == 1)
{
return b;
}
else if (i == 2)
{
return c;
}
return a;
}
// Vec3 operator+(const Vec3 &other) const {
// return {a + other.a, b + other.b, c + other.c};
// }
Vec3 operator+(const Vec3 &other) const
{
return Vec3(a + other.a, b + other.b, c + other.c);
} // VS2008
// Vec3 operator/(const float w) const {
// return {a / w, b / w, c / w};
// }
Vec3 operator/(const float w) const
{
return Vec3(a / w, b / w, c / w);
} // VS2008
// Vec3 operator-(const Vec3 &other) const {
// return {a - other.a, b - other.b, c - other.c};
// }
Vec3 operator-(const Vec3 &other) const
{
return Vec3(a - other.a, b - other.b, c - other.c);
} // VS2008
// Vec3 operator*(const float &w) const {
// return {a * w, b * w, c * w};
// }
Vec3 operator*(const float &w) const
{
return Vec3(a * w, b * w, c * w);
} // VS2008
float length() const
{
return sqrt(a * a + b * b + c * c);
}
Vec3 norm() const
{
return *this / length();
}
// Vec3 cross(const Vec3 &other) const {
// return {b * other.c - c * other.b, c * other.a - a * other.c, a * other.b - b * other.a};
// }
Vec3 cross(const Vec3 &other) const
{
return Vec3(b * other.c - c * other.b, c * other.a - a * other.c, a * other.b - b * other.a);
}
float dot(const Vec3 &other) const
{
return a * other.a + b * other.b + c * other.c;
}
};
typedef Vec3<float> Vec3f;
typedef Vec3<unsigned int> Vec3u;
class Mesh
{
public:
std::vector<Vec3f> vertices;
std::vector<Vec3u> indices;
};
class AABB
{
public:
Vec3f min, max;
/*
AABB()
:min(std::numeric_limits<float>::max(), std::numeric_limits<float>::max(),std::numeric_limits<float>::max()),
max(std::numeric_limits<float>::min(), std::numeric_limits<float>::min(),std::numeric_limits<float>::min())
{}
*/
AABB()
{
min = Vec3f(1e5, 1e5, 1e5);
max = Vec3f(0, 0, 0);
}
};
class LineSegment
{
public:
Vec3f start;
Vec3f end;
// LineSegment(const Vec3f &s, const Vec3f &e) : start(s), end(e) {
// length = (end - start).length();
// dir = (end - start).norm();
// }
LineSegment(const Vec3f &s, const Vec3f &e) : start(s), end(e)
{
calculate();
}
float getLength() const
{
return length;
}
Vec3f getDir() const
{
return dir;
}
// private:
float length;
Vec3f dir;
void calculate()
{ // VS2008
Vec3f diff = end - start; // VS2008
length = diff.length(); // VS2008
dir = diff.norm(); // VS2008
} // VS2008
};
struct FaceCenterComparator
{
const std::vector<Vec3f> &faceCenters; // Ìæ»» YourVectorType ΪÄãµÄÏòÁ¿ÀàÐÍ
int longAxis;
FaceCenterComparator(const std::vector<Vec3f> &centers, int axis)
: faceCenters(centers), longAxis(axis) {}
bool operator()(const size_t &a, const size_t &b) const
{
return faceCenters[a][longAxis] < faceCenters[b][longAxis];
}
};