bool PtInTriangle(VECTOR4D &V0, VECTOR4D &V1, VECTOR4D &V2, VECTOR4D &P) { float alpha, beta, gamma; VECTOR4D I = V0 - P, J = V1 - P, K = V2 - P; float mI = Magnity(I), mJ = Magnity(J), mK = Magnity(K); alpha = acosf(Dot(I, J) / (mI * mJ)); beta = acosf(Dot(J, K) / (mJ * mK)); gamma = acosf(Dot(K, I) / (mK * mI)); return fabsf((2 * PI - (alpha + beta + gamma))) < EPSILON; }
bool CMesh::RayCast(VECTOR4D & RayOrigin, VECTOR4D & RayDir, multimap<float, unsigned long>& Vertices, float radius) { for (size_t i = 0; i < m_Vertices.size(); i++) { if (RaySphereIntersect(RayOrigin, RayDir, m_Vertices[i].Position, radius)) { float dist = Magnity(m_Vertices[i].Position - RayOrigin); Vertices.insert(make_pair(dist, (unsigned long)i)); } } return Vertices.size() > 0; }
bool CMesh::RayCast(VECTOR4D &RayOrigin, VECTOR4D &RayDir, multimap<float, unsigned long> &Vertices, float radius) { // Ecuacion de la esfera con centro en el origen // x^2 + y^2 + z^2 = r^2 for (unsigned long index = 0; index < m_Vertexes.size(); ++index) { if (RaySphereIntersect(RayOrigin, RayDir, m_Vertexes[index].Position, radius)) { Vertices.insert(make_pair(Magnity(m_Vertexes[index].Position - RayOrigin), index)); } } return Vertices.size() != 0; }
bool CMesh::RayCast(VECTOR4D &RayOrigin, VECTOR4D &RayDir, multimap<float, INTERSECTIONINFO>& Faces) { unsigned long nFaces = (unsigned long)m_Indices.size() / 3; unsigned long nBaseIndex = 0; unsigned long nIntersectedFaces = 0; for (unsigned long iFace = 0; iFace < nFaces; iFace++) { VECTOR4D &V0 = m_Vertices[m_Indices[nBaseIndex + 0]].Position; VECTOR4D &V1 = m_Vertices[m_Indices[nBaseIndex + 1]].Position; VECTOR4D &V2 = m_Vertices[m_Indices[nBaseIndex + 2]].Position; VECTOR4D Intersection; if (RayCastOnTriangle(V0, V1, V2, RayOrigin, RayDir, Intersection)) { float dist = Magnity(Intersection - RayOrigin); INTERSECTIONINFO II; II.Face = iFace; II.LocalPosition = Intersection; Faces.insert(make_pair(dist, II)); nIntersectedFaces++; } nBaseIndex += 3; } return nIntersectedFaces > 0; }
bool CMesh::RayCast(VECTOR4D &RayOrigin, VECTOR4D &RayDir, multimap<float, INTERSECTIONINFO> &Faces) { unsigned long nFaces = m_Indexes.size() / 3; unsigned long nBaseIndex = 0; unsigned long nIntersectedFaces = 0; for (unsigned long iFace = 0; iFace < nFaces; ++iFace, nBaseIndex+=3) { VECTOR4D &V0 = m_Vertexes[m_Indexes[nBaseIndex]].Position; VECTOR4D &V1 = m_Vertexes[m_Indexes[nBaseIndex + 1]].Position; VECTOR4D &V2 = m_Vertexes[m_Indexes[nBaseIndex + 2]].Position; VECTOR4D Intersection; if (RayCastOnTriangle(V0, V1, V2, RayOrigin, RayDir, Intersection)) { // Distancia entre el origen y esa interseccion float dist = Magnity(Intersection - RayOrigin); INTERSECTIONINFO II; II.Face = iFace; II.LocalPosition = Intersection; Faces.insert(make_pair(dist, II)); ++nIntersectedFaces; } } return nIntersectedFaces != 0; }
VECTOR4D Normalize(VECTOR4D &A) { float inv = 1.0f / Magnity(A); VECTOR4D R = { A.x * inv, A.y * inv, A.z * inv, A.w * inv }; return R; }