6#include <Common/assertion.h>
35 attributeA, barycentricCoords.
x(),
36 attributeB, barycentricCoords.
y(),
37 attributeC, barycentricCoords.
z());
43 const std::array<
TVector2<T>, 3>& parameterizations,
50 const auto deltaUv01 = parameterizations[1] - parameterizations[0];
51 const auto deltaUv02 = parameterizations[2] - parameterizations[0];
57 const auto deltaAttr01 = attributes[1] - attributes[0];
58 const auto deltaAttr02 = attributes[2] - attributes[0];
60 const std::array<std::array<T, 2>, 3> bs = {
61 deltaAttr01.x(), deltaAttr02.x(),
62 deltaAttr01.y(), deltaAttr02.y(),
63 deltaAttr01.z(), deltaAttr02.z()};
65 std::array<std::array<T, 2>, 3> xs;
68 out_dXdU->
x() = xs[0][0]; out_dXdV->
x() = xs[0][1];
69 out_dXdU->
y() = xs[1][0]; out_dXdV->
y() = xs[1][1];
70 out_dXdU->
z() = xs[2][0]; out_dXdV->
z() = xs[2][1];
94 std::move(vertices[0]),
95 std::move(vertices[1]),
96 std::move(vertices[2]))
102 const auto [eAB, eAC] = getEdgeVectors();
104 return eAB.cross(eAC).length() * T(0.5);
110 const auto [eAB, eAC] = getEdgeVectors();
112 return eAB.cross(eAC).normalizeLocal();
118 const auto [eAB, eAC] = getEdgeVectors();
120 return !isDegenerate() ? eAB.cross(eAC).safeNormalize(failSafe) : failSafe;
126 if constexpr(std::is_floating_point_v<T>)
128 constexpr auto oneThird =
static_cast<T
>(1) /
static_cast<T
>(3);
129 return (m_vA + m_vB + m_vC) * oneThird;
133 return (m_vA + m_vB + m_vC) /
static_cast<T
>(3);
140 T minX = m_vA.x(), maxX = m_vA.x(),
141 minY = m_vA.y(), maxY = m_vA.y(),
142 minZ = m_vA.z(), maxZ = m_vA.z();
144 if (m_vB.x() > maxX) maxX = m_vB.x();
145 else if(m_vB.x() < minX) minX = m_vB.x();
146 if (m_vB.y() > maxY) maxY = m_vB.y();
147 else if(m_vB.y() < minY) minY = m_vB.y();
148 if (m_vB.z() > maxZ) maxZ = m_vB.z();
149 else if(m_vB.z() < minZ) minZ = m_vB.z();
151 if (m_vC.x() > maxX) maxX = m_vC.x();
152 else if(m_vC.x() < minX) minX = m_vC.x();
153 if (m_vC.y() > maxY) maxY = m_vC.y();
154 else if(m_vC.y() < minY) minY = m_vC.y();
155 if (m_vC.z() > maxZ) maxZ = m_vC.z();
156 else if(m_vC.z() < minZ) minZ = m_vC.z();
158 constexpr auto TRIANGLE_EPSILON = T(0.0001);
161 math::Vector3R(minX - TRIANGLE_EPSILON, minY - TRIANGLE_EPSILON, minZ - TRIANGLE_EPSILON),
162 math::Vector3R(maxX + TRIANGLE_EPSILON, maxY + TRIANGLE_EPSILON, maxZ + TRIANGLE_EPSILON));
168 const auto ab = (m_vB - m_vA).
length();
169 const auto ac = (m_vC - m_vA).
length();
170 const auto bc = (m_vC - m_vB).
length();
172 const auto termA = ab + ac - bc;
173 const auto termB = ac + bc - ab;
174 const auto termC = ab + bc - ac;
175 if(termA <=
static_cast<T
>(0) || termB <= static_cast<T>(0) || termC <= static_cast<T>(0) ||
178 return std::numeric_limits<T>::infinity();
181 return (ab * ac * bc) / (termA * termB * termC);
200 const auto eAP = position.
sub(m_vA);
201 const auto [eAB, eAC] = getEdgeVectors();
203 const T d00 = eAB.dot(eAB);
204 const T d01 = eAB.dot(eAC);
205 const T d11 = eAC.dot(eAC);
206 const T d20 = eAP.dot(eAB);
207 const T d21 = eAP.dot(eAC);
211 const T denominator = d00 * d11 - d01 * d01;
212 if(denominator ==
static_cast<T
>(0))
217 const T rcpDenominator =
static_cast<T
>(1) / denominator;
219 const T b = (d11 * d20 - d01 * d21) * rcpDenominator;
220 const T c = (d00 * d21 - d01 * d20) * rcpDenominator;
221 const T a =
static_cast<T
>(1) - b - c;
230 m_vA, barycentricCoords.
x(),
231 m_vB, barycentricCoords.
y(),
232 m_vC, barycentricCoords.
z());
238 PH_ASSERT_IN_RANGE_INCLUSIVE(sample[0], T(0), T(1));
239 PH_ASSERT_IN_RANGE_INCLUSIVE(sample[1], T(0), T(1));
241 const T
A = std::sqrt(sample[0]);
242 const T
B = sample[1];
256 *out_pdfA = uniformSurfaceSamplePdfA();
258 return sampleToBarycentricOsada(sample);
264 return static_cast<T
>(1) / getArea();
270 const auto [eAB, eAC] = getEdgeVectors();
271 const T crossFactor = eAB.cross(eAC).lengthSquared();
274 return !(crossFactor > T(0)) || std::isinf(crossFactor);
A 3-D Axis-Aligned Bounding Box (AABB).
Definition TAABB3D.h:32
Basic 3-D triangle functionalities.
Definition TBasicTriangle.h:21
TVector3< T > getCentroid() const
Definition TBasicTriangle.ipp:124
T getAspectRatio() const
Calculate aspect ratio of this triangle. For a triangle, its aspect ratio is defined to be the ratio ...
Definition TBasicTriangle.ipp:166
T uniformSurfaceSamplePdfA() const
Definition TBasicTriangle.ipp:262
TVector3< T > getFaceNormal() const
Definition TBasicTriangle.ipp:108
static TVector3< T > interpolate(const std::array< TVector3< T >, 3 > &attributes, const TVector3< T > &barycentricCoords)
Definition TBasicTriangle.ipp:16
static bool calcSurfaceParamDerivatives(const std::array< TVector3< T >, 3 > &attributes, const std::array< TVector2< T >, 3 > ¶meterizations, TVector3< T > *out_dXdU, TVector3< T > *out_dXdV)
Definition TBasicTriangle.ipp:41
TVector3< T > getVb() const
Definition TBasicTriangle.ipp:284
bool isDegenerate() const
Definition TBasicTriangle.ipp:268
TVector3< T > sampleToBarycentricOsada(const std::array< T, 2 > &sample) const
Map the 2D sample to a position on the surface of the triangle. A common mapping on triangles which i...
Definition TBasicTriangle.ipp:236
TVector3< T > surfaceToBarycentric(const TVector3< T > &position) const
Definition TBasicTriangle.ipp:194
TVector3< T > safeGetFaceNormal(const TVector3< T > &failSafe={0, 1, 0}) const
Calculate face normal with a fail-safe value. The vertices may form a degenerate triangle (zero cross...
Definition TBasicTriangle.ipp:116
TVector3< T > getVa() const
Definition TBasicTriangle.ipp:278
TVector3< T > getVc() const
Definition TBasicTriangle.ipp:290
TAABB3D< T > getAABB() const
Definition TBasicTriangle.ipp:138
T getArea() const
Definition TBasicTriangle.ipp:100
TVector3< T > barycentricToSurface(const TVector3< T > &barycentricCoords) const
Definition TBasicTriangle.ipp:227
std::pair< TVector3< T >, TVector3< T > > getEdgeVectors() const
Definition TBasicTriangle.ipp:185
TBasicTriangle(TVector3< T > vA, TVector3< T > vB, TVector3< T > vC)
Definition TBasicTriangle.ipp:81
Represents a 2x2 matrix.
Definition TMatrix2.h:16
Represents a 2-D vector.
Definition TVector2.h:19
Represents a 3-D vector.
Definition TVector3.h:17
T & y()
Definition TVector3.ipp:189
T & z()
Definition TVector3.ipp:195
T & x()
Definition TVector3.ipp:183
static TVector3 weightedSum(const TVector3 &vA, T wA, const TVector3 &vB, T wB, const TVector3 &vC, T wC)
Definition TVector3.ipp:19
Derived sub(const Derived &rhs) const
Definition TArithmeticArrayBase.ipp:62
Math functions and utilities.
Definition TransformInfo.h:10
T length(const std::array< T, N > &vec)
Treating input values as a vector and calculates its length.
Definition math.ipp:50