MinusculeRender/src/triangle.cc

41 lines
1.4 KiB
C++

// Copyright 2024 Bytedance Inc. All Rights Reserved.
// Author: tianlei.richard@qq.com (tianlei.richard)
#include "triangle.h"
#include <algorithm>
Triangle::Triangle(const PointType &a, const PointType &b, const PointType &c)
: points_({HomoPointType{a[0], a[1], a[2], 1},
HomoPointType{b[0], b[1], b[2], 1},
HomoPointType{c[0], c[1], c[2], 1}}),
aabb_(calculate_aabb(points_[0], points_[1], points_[2])) {}
Triangle::Triangle(const HomoPointType &a, const HomoPointType &b,
const HomoPointType &c)
: points_({a, b, c}),
aabb_(calculate_aabb(points_[0], points_[1], points_[2])) {}
Triangle::BBoxType Triangle::calculate_aabb(const HomoPointType &a,
const HomoPointType &b,
const HomoPointType &c) {
const int x_min = std::min({a.x(), b.x(), c.x()});
const int y_min = std::min({a.y(), b.y(), c.y()});
const int x_max = std::max({a.x(), b.x(), c.x()});
const int y_max = std::max({a.y(), b.y(), c.y()});
return BBoxType{x_min, y_min, (x_max - x_min), (y_max - y_min)};
}
Triangle::BBoxType Triangle::axis_align_bbox() const { return aabb_; }
void Triangle::affine_transform(const AffineTransformType &m) {
for (auto &p : points_) {
p = m * p;
}
}
void Triangle::projective_transform(const ProjectiveTransformType &m) {
for (auto &p : points_) {
p = m * p;
}
}