| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2011 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "BoundingBox.h"
- #include "Plane.h"
- #include "Ray.h"
- #include "Sphere.h"
- Vector3 Ray::Project(const Vector3& point) const
- {
- Vector3 offset = point - origin_;
- return origin_ + offset.DotProduct(direction_) * direction_;
- }
- float Ray::HitDistance(const Plane& plane) const
- {
- float d = plane.normal_.DotProduct(direction_);
- if (fabsf(d) >= M_EPSILON)
- return (-plane.normal_.DotProduct(origin_) + plane.intercept_) / d;
- else
- return M_INFINITY;
- }
- float Ray::HitDistance(const Sphere& sphere) const
- {
- Vector3 centeredOrigin = origin_ - sphere.center_;
- float squaredRadius = sphere.radius_ * sphere.radius_;
-
- // Check if ray originates inside the sphere
- if (centeredOrigin.LengthSquared() <= squaredRadius)
- return 0.0f;
-
- // Calculate intersection by quadratic equation
- float a = direction_.DotProduct(direction_);
- float b = 2.0f * centeredOrigin.DotProduct(direction_);
- float c = centeredOrigin.DotProduct(centeredOrigin) - squaredRadius;
- float d = b * b - 4.0f * a * c;
-
- // No solution
- if (d < 0.0f)
- return M_INFINITY;
-
- // Get the nearer solution
- float dSqrt = sqrtf(d);
- float dist = (-b - dSqrt) / (2.0f * a);
- if (dist >= 0.0f)
- return dist;
- else
- return (-b + dSqrt) / (2.0f * a);
- }
- float Ray::HitDistance(const BoundingBox& box) const
- {
- // If undefined, no hit (infinite distance)
- if (!box.defined_)
- return M_INFINITY;
-
- // Check for ray origin being inside the box
- if (box.IsInside(origin_))
- return 0.0f;
-
- float dist = M_INFINITY;
-
- // Check for intersecting in the X-direction
- if (origin_.x_ < box.min_.x_ && direction_.x_ > 0.0f)
- {
- float x = (box.min_.x_ - origin_.x_) / direction_.x_;
- if (x < dist)
- {
- Vector3 point = origin_ + x * direction_;
- if (point.y_ >= box.min_.y_ && point.y_ <= box.max_.y_ && point.z_ >= box.min_.z_ && point.z_ <= box.max_.z_)
- dist = x;
- }
- }
- if (origin_.x_ > box.max_.x_ && direction_.x_ < 0.0f)
- {
- float x = (box.max_.x_ - origin_.x_) / direction_.x_;
- if (x < dist)
- {
- Vector3 point = origin_ + x * direction_;
- if (point.y_ >= box.min_.y_ && point.y_ <= box.max_.y_ && point.z_ >= box.min_.z_ && point.z_ <= box.max_.z_)
- dist = x;
- }
- }
- // Check for intersecting in the Y-direction
- if (origin_.y_ < box.min_.y_ && direction_.y_ > 0.0f)
- {
- float x = (box.min_.y_ - origin_.y_) / direction_.y_;
- if (x < dist)
- {
- Vector3 point = origin_ + x * direction_;
- if (point.x_ >= box.min_.x_ && point.x_ <= box.max_.x_ && point.z_ >= box.min_.z_ && point.z_ <= box.max_.z_)
- dist = x;
- }
- }
- if (origin_.y_ > box.max_.y_ && direction_.y_ < 0.0f)
- {
- float x = (box.max_.y_ - origin_.y_) / direction_.y_;
- if (x < dist)
- {
- Vector3 point = origin_ + x * direction_;
- if (point.x_ >= box.min_.x_ && point.x_ <= box.max_.x_ && point.z_ >= box.min_.z_ && point.z_ <= box.max_.z_)
- dist = x;
- }
- }
- // Check for intersecting in the Z-direction
- if (origin_.z_ < box.min_.z_ && direction_.z_ > 0.0f)
- {
- float x = (box.min_.z_ - origin_.z_) / direction_.z_;
- if (x < dist)
- {
- Vector3 point = origin_ + x * direction_;
- if (point.x_ >= box.min_.x_ && point.x_ <= box.max_.x_ && point.y_ >= box.min_.y_ && point.y_ <= box.max_.y_)
- dist = x;
- }
- }
- if (origin_.z_ > box.max_.z_ && direction_.z_ < 0.0f)
- {
- float x = (box.max_.z_ - origin_.z_) / direction_.z_;
- if (x < dist)
- {
- Vector3 point = origin_ + x * direction_;
- if (point.x_ >= box.min_.x_ && point.x_ <= box.max_.x_ && point.y_ >= box.min_.y_ && point.y_ <= box.max_.y_)
- dist = x;
- }
- }
-
- return dist;
- }
- float Ray::HitDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2) const
- {
- // Based on Fast, Minimum Storage Ray/Triangle Intersection by Möller & Trumbore
- // http://www.graphics.cornell.edu/pubs/1997/MT97.pdf
- // Calculate edge vectors
- Vector3 edge1 = v1 - v0;
- Vector3 edge2 = v2 - v0;
-
- // Calculate determinant & check backfacing
- Vector3 p = direction_.CrossProduct(edge2);
- float det = edge1.DotProduct(p);
- if (det >= M_EPSILON)
- {
- // Calculate u & v parameters and test
- Vector3 t = origin_ - v0;
- float u = t.DotProduct(p);
- if (u >= 0.0f && u <= det)
- {
- Vector3 q = t.CrossProduct(edge1);
- float v = direction_.DotProduct(q);
- if (v >= 0.0f && u + v <= det)
- {
- // There is an intersection, so calculate distance
- return edge2.DotProduct(q) / det;
- }
- }
- }
-
- return M_INFINITY;
- }
- float Ray::HitDistance(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount) const
- {
- float nearest = M_INFINITY;
- const unsigned char* vertices = (const unsigned char*)vertexData;
-
- // 16-bit indices
- if (indexSize == sizeof(unsigned short))
- {
- const unsigned short* indices = (const unsigned short*)indexData;
-
- for (unsigned i = indexStart; i < indexStart + indexCount; i += 3)
- {
- const Vector3& v0 = *((const Vector3*)(&vertices[indices[i] * vertexSize]));
- const Vector3& v1 = *((const Vector3*)(&vertices[indices[i + 1] * vertexSize]));
- const Vector3& v2 = *((const Vector3*)(&vertices[indices[i + 2] * vertexSize]));
- nearest = Min(nearest, HitDistance(v0, v1, v2));
- }
- }
- // 32-bit indices
- else
- {
- const unsigned* indices = (const unsigned*)indexData;
-
- for (unsigned i = indexStart; i < indexStart + indexCount; i += 3)
- {
- const Vector3& v0 = *((const Vector3*)(&vertices[indices[i] * vertexSize]));
- const Vector3& v1 = *((const Vector3*)(&vertices[indices[i + 1] * vertexSize]));
- const Vector3& v2 = *((const Vector3*)(&vertices[indices[i + 2] * vertexSize]));
- nearest = Min(nearest, HitDistance(v0, v1, v2));
- }
- }
-
- return nearest;
- }
|