ViewDesignEngine  3.1.0
高性能 CAD 计算几何引擎
triangle.h
浏览该文件的文档.
1 #pragma once
2 #include "vde/core/point.h"
3 #include <array>
4 
5 namespace vde::core {
6 
17 template <typename T>
18 class Triangle {
19 public:
21  Triangle() = default;
22 
30  Triangle(const Point3D& v0, const Point3D& v1, const Point3D& v2)
31  : v_{v0, v1, v2} {}
32 
39  [[nodiscard]] const Point3D& v(int i) const { return v_[i]; }
40 
42  [[nodiscard]] const std::array<Point3D, 3>& vertices() const { return v_; }
43 
54  [[nodiscard]] Vector3D normal() const {
55  return (v_[1] - v_[0]).cross(v_[2] - v_[0]).normalized();
56  }
57 
62  [[nodiscard]] T area() const {
63  return (v_[1] - v_[0]).cross(v_[2] - v_[0]).norm() * 0.5;
64  }
65 
70  [[nodiscard]] Point3D centroid() const {
71  return (v_[0] + v_[1] + v_[2]) / 3.0;
72  }
73 
90  [[nodiscard]] bool contains(const Point3D& p) const {
91  Vector3D v0 = v_[2] - v_[0], v1 = v_[1] - v_[0], v2 = p - v_[0];
92  T d00 = v0.dot(v0), d01 = v0.dot(v1), d11 = v1.dot(v1);
93  T d20 = v2.dot(v0), d21 = v2.dot(v1);
94  T denom = d00 * d11 - d01 * d01;
95  T v = (d11 * d20 - d01 * d21) / denom;
96  T w = (d00 * d21 - d01 * d20) / denom;
97  return v >= 0 && w >= 0 && v + w <= 1;
98  }
99 
100 private:
101  std::array<Point3D, 3> v_{};
102 };
103 
108 
109 } // namespace vde::core
三维三角形
Definition: triangle.h:18
Vector3D normal() const
三角形单位法向量
Definition: triangle.h:54
Point3D centroid() const
三角形质心(几何中心)
Definition: triangle.h:70
T area() const
三角形面积
Definition: triangle.h:62
const Point3D & v(int i) const
获取第 i 个顶点
Definition: triangle.h:39
bool contains(const Point3D &p) const
重心坐标包含测试
Definition: triangle.h:90
Triangle(const Point3D &v0, const Point3D &v1, const Point3D &v2)
从三个顶点构造三角形
Definition: triangle.h:30
Triangle()=default
默认构造:三个顶点均在原点(退化三角形)
const std::array< Point3D, 3 > & vertices() const
获取所有顶点数组
Definition: triangle.h:42
Definition: aabb.h:6
foundation::Vector3D Vector3D
双精度三维向量(重新导出)
Definition: point.h:49
foundation::Point3D Point3D
双精度三维点(重新导出)
Definition: point.h:31