#pragma once #include #include #include #include template 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 Vec3f; typedef Vec3 Vec3u; class Mesh { public: std::vector vertices; std::vector indices; }; class AABB { public: Vec3f min, max; /* AABB() :min(std::numeric_limits::max(), std::numeric_limits::max(),std::numeric_limits::max()), max(std::numeric_limits::min(), std::numeric_limits::min(),std::numeric_limits::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& faceCenters; // 替换 YourVectorType 为你的向量类型 int longAxis; FaceCenterComparator(const std::vector& 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]; } };