2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 19.01.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Plane.h"
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
Plane::Plane(const Triangle& tri) : _normal(tri.norm()), _point(tri[0]) {
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
Plane::Plane(const Vec3D &N, const Vec3D &P) : _normal(N.normalized()), _point(P) {
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
double Plane::distance(const Vec3D &point) const {
|
2021-10-28 16:58:02 +03:00
|
|
|
return point.dot(_normal) - _point.dot(_normal);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
std::pair<Vec3D, double> Plane::intersection(const Vec3D &start, const Vec3D &end) const {
|
2021-10-28 16:58:02 +03:00
|
|
|
double s_dot_n = start.dot(_normal);
|
|
|
|
double k = (s_dot_n - _point.dot(_normal)) / (s_dot_n - end.dot(_normal));
|
2021-10-12 17:12:47 +03:00
|
|
|
Vec3D res = start + (end - start)*k;
|
2021-09-13 15:53:43 +03:00
|
|
|
return std::make_pair(res, k);
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
std::vector<Triangle> Plane::clip(const Triangle &tri) const {
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
std::vector<Triangle> result;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
std::vector<Vec3D> insidePoints;
|
|
|
|
std::vector<Vec3D> outsidePoints;
|
|
|
|
|
|
|
|
double distances[3] = {distance(Vec3D(tri[0])),
|
|
|
|
distance(Vec3D(tri[1])),
|
|
|
|
distance(Vec3D(tri[2]))};
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
for(int i = 0; i < 3; i++) {
|
|
|
|
if (distances[i] >= 0) {
|
2021-10-12 17:12:47 +03:00
|
|
|
insidePoints.emplace_back(tri[i]);
|
2021-09-13 15:53:43 +03:00
|
|
|
} else {
|
2021-10-12 17:12:47 +03:00
|
|
|
outsidePoints.emplace_back(tri[i]);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
if(insidePoints.size() == 1) {
|
|
|
|
std::pair<Vec3D, double> intersect1 = intersection(insidePoints[0], outsidePoints[0]);
|
|
|
|
std::pair<Vec3D, double> intersect2 = intersection(insidePoints[0], outsidePoints[1]);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
result.emplace_back(insidePoints[0].makePoint4D(),
|
|
|
|
intersect1.first.makePoint4D(),
|
|
|
|
intersect2.first.makePoint4D(),
|
|
|
|
tri.color());
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
if(insidePoints.size() == 2) {
|
|
|
|
std::pair<Vec3D, double> intersect1 = intersection(insidePoints[0], outsidePoints[0]);
|
|
|
|
std::pair<Vec3D, double> intersect2 = intersection(insidePoints[1], outsidePoints[0]);
|
|
|
|
|
|
|
|
result.emplace_back(insidePoints[0].makePoint4D(),
|
|
|
|
intersect1.first.makePoint4D(),
|
|
|
|
insidePoints[1].makePoint4D(),
|
|
|
|
tri.color());
|
|
|
|
result.emplace_back(intersect1.first.makePoint4D(),
|
|
|
|
intersect2.first.makePoint4D(),
|
|
|
|
insidePoints[1].makePoint4D(),
|
|
|
|
tri.color());
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
if(insidePoints.size() == 3) {
|
|
|
|
result.emplace_back(tri);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
return result;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|