I'll assume this is not considered off-topic since simd is in the swift repo and about the only mention I could find of simd_orient
, except its very sparse documentation, was @scanon introducing it at WWDC 2016.
If there are more thorough documentation of simd_orient
(and other barely documented simd functions), I'd appreciate pointers to such.
For example, I assume that the orientation()
function below is making proper use of it, but I'd like to know if using it for calculating the area and testing if the triangle contains a point is recommended or not:
struct Triangle2D {
let a, b, c: float2
init(_ a: float2, _ b: float2, _ c: float2) {
(self.a, self.b, self.c) = (a, b, c)
}
/// Returns a positive value if the triangle is positively oriented, a
/// negative value if it is negatively oriented and zero if it is
/// degenerate (the three points are colinear).
func orientation() -> Float {
return simd_orient(a, b, c)
}
/// Returns the area of the triangle. The area is positive if the triangle
/// is positively oriented, negative if it's negatively oriented and zero
/// if it's degenerate.
func area() -> Float {
return orientation() / 2
}
/// Returns true if the point is within or on the edge of the triangle.
/// The triangle has to be positively oriented.
func contains(point p: float2) -> Bool {
if simd_orient(a, b, p) < 0 { return false }
if simd_orient(b, c, p) < 0 { return false }
if simd_orient(c, a, p) < 0 { return false }
return true
}
/* ... */
}
Also, I'm not entirely sure how the version of simd_orient
that takes two rather than three float2s works. For example, is it always exactly like the three-argument one with the last argument being float2(0, 0):
$ swift
Welcome to Apple Swift version 4.2 (swiftlang-1000.0.36 clang-1000.0.4). Type :help for assistance.
1> import simd
2> simd_orient(float2(0.1, 2.3), float2(4.5, 6.7))
$R0: Float = -9.67999935
3> simd_orient(float2(0, 0), float2(0.1, 2.3), float2(4.5, 6.7))
$R1: Float = -9.6800003
4> simd_orient(float2(0.1, 2.3), float2(4.5, 6.7), float2(0, 0))
$R2: Float = -9.67999935
?
Is there any where I can read up on these things?