/** * operating with 3-vectors here because there are a lot of cross products */ double Triangle::intersection(const ray &viewRay)const { Vector4d edge1 = p2 - p1; Vector4d edge2 = p3 - p1; Vector4d h = cross(viewRay.dir, edge2); double a = edge1.dot(h); if (a > -EPSILON && a < EPSILON) return -1 * std::numeric_limits<double>::max(); double f = 1/a; Vector4d s = viewRay.orig - p1; double u = f * s.dot(h); if (u < 0.0 || u > 1.0) return -1 * std::numeric_limits<double>::max(); Vector4d q = cross(s, edge1); double v = f * q.dot(viewRay.dir); if (v < 0.0 || u + v > 1.0) return -1 * std::numeric_limits<double>::max(); double t = f * edge2.dot(q); return t; }
double geometric(Vector4d s1, Vector4d n1, Vector4d s2, Vector4d n2) { ASSERT(isUnitVector(n1), "assumes the normals are normalized"); ASSERT(isUnitVector(n2), "assumes the normals are normalized"); Vector4d segment = s1 - s2; Vector4d seg_dir = segment.normalized(); double cos1 = n1.dot(seg_dir); double cos2 = n2.dot(-1 * seg_dir); return std::abs(cos1 * cos2) / segment.squaredNorm(); }
inline bool cullSphere(const Vector4d& center, double radius) const { return (center.z() - radius > m_nearZ || center.z() + radius < m_farZ || center.dot(m_planeNormals[0]) < -radius || center.dot(m_planeNormals[1]) < -radius || center.dot(m_planeNormals[2]) < -radius || center.dot(m_planeNormals[3]) < -radius); }
Vector4d CProxyCamera::intersect(const Vector4d &coord, const Vector4d& abcd) const { Vector4d ray = m_center - coord; const double A = coord.dot(abcd); const double B = ray.dot(abcd); if (fabs(B)<1e-8) return Vector4d(0.0f, 0.0f, 0.0f, -1.0f); else return coord - A / B * ray; }
// Test a point to see if it lies within the frustum defined by // planes z=nearZ, z=farZ, and the four side planes with specified // normals. static inline bool frustumCull(const Vector4d& curvePoint, double curveBoundingRadius, double nearZ, double farZ, const Vector4d viewFrustumPlaneNormals[]) { return (curvePoint.z() - curveBoundingRadius > nearZ || curvePoint.z() + curveBoundingRadius < farZ || curvePoint.dot(viewFrustumPlaneNormals[0]) < -curveBoundingRadius || curvePoint.dot(viewFrustumPlaneNormals[1]) < -curveBoundingRadius || curvePoint.dot(viewFrustumPlaneNormals[2]) < -curveBoundingRadius || curvePoint.dot(viewFrustumPlaneNormals[3]) < -curveBoundingRadius); }
std::tuple<double, double> Triangle::uvCoords(const Vector4d &point) const { Vector4d edge1 = p2 - p1; Vector4d edge2 = p3 - p1; Vector4d h = cross(trueNormal, edge2); double a = edge1.dot(h); double f = 1/a; Vector4d s = point - p1; double u = f * s.dot(h); Vector4d q = cross(s, edge1); double v = f * q.dot(trueNormal); return std::make_tuple(u, v); }