+++ /dev/null
-#include "stdafx.h"\r
-/*\r
-* Copyright (c) 2007-2010 SlimDX Group\r
-* \r
-* Permission is hereby granted, free of charge, to any person obtaining a copy\r
-* of this software and associated documentation files (the "Software"), to deal\r
-* in the Software without restriction, including without limitation the rights\r
-* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
-* copies of the Software, and to permit persons to whom the Software is\r
-* furnished to do so, subject to the following conditions:\r
-* \r
-* The above copyright notice and this permission notice shall be included in\r
-* all copies or substantial portions of the Software.\r
-* \r
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
-* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
-* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
-* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
-* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
-* THE SOFTWARE.\r
-*/\r
-\r
-#include <d3dx9.h>\r
-\r
-#include "../SlimDXException.h"\r
-\r
-#include "BoundingSphere.h"\r
-#include "BoundingBox.h"\r
-#include "Ray.h"\r
-#include "Plane.h"\r
-\r
-using namespace System;\r
-using namespace System::Globalization;\r
-\r
-namespace SlimDX\r
-{\r
- BoundingSphere::BoundingSphere( Vector3 center, float radius )\r
- {\r
- Center = center;\r
- Radius = radius;\r
- }\r
-\r
- ContainmentType BoundingSphere::Contains( BoundingSphere sphere, BoundingBox box )\r
- {\r
- Vector3 vector;\r
-\r
- if( !BoundingBox::Intersects( box, sphere ) )\r
- return ContainmentType::Disjoint;\r
-\r
- float radius = sphere.Radius * sphere.Radius;\r
- vector.X = sphere.Center.X - box.Minimum.X;\r
- vector.Y = sphere.Center.Y - box.Maximum.Y;\r
- vector.Z = sphere.Center.Z - box.Maximum.Z;\r
-\r
- if( vector.LengthSquared() > radius )\r
- return ContainmentType::Intersects;\r
-\r
- vector.X = sphere.Center.X - box.Maximum.X;\r
- vector.Y = sphere.Center.Y - box.Maximum.Y;\r
- vector.Z = sphere.Center.Z - box.Maximum.Z;\r
-\r
- if( vector.LengthSquared() > radius )\r
- return ContainmentType::Intersects;\r
-\r
- vector.X = sphere.Center.X - box.Maximum.X;\r
- vector.Y = sphere.Center.Y - box.Minimum.Y;\r
- vector.Z = sphere.Center.Z - box.Maximum.Z;\r
-\r
- if( vector.LengthSquared() > radius )\r
- return ContainmentType::Intersects;\r
-\r
- vector.X = sphere.Center.X - box.Minimum.X;\r
- vector.Y = sphere.Center.Y - box.Minimum.Y;\r
- vector.Z = sphere.Center.Z - box.Maximum.Z;\r
-\r
- if( vector.LengthSquared() > radius )\r
- return ContainmentType::Intersects;\r
-\r
- vector.X = sphere.Center.X - box.Minimum.X;\r
- vector.Y = sphere.Center.Y - box.Maximum.Y;\r
- vector.Z = sphere.Center.Z - box.Minimum.Z;\r
-\r
- if( vector.LengthSquared() > radius )\r
- return ContainmentType::Intersects;\r
-\r
- vector.X = sphere.Center.X - box.Maximum.X;\r
- vector.Y = sphere.Center.Y - box.Maximum.Y;\r
- vector.Z = sphere.Center.Z - box.Minimum.Z;\r
-\r
- if( vector.LengthSquared() > radius )\r
- return ContainmentType::Intersects;\r
-\r
- vector.X = sphere.Center.X - box.Maximum.X;\r
- vector.Y = sphere.Center.Y - box.Minimum.Y;\r
- vector.Z = sphere.Center.Z - box.Minimum.Z;\r
-\r
- if( vector.LengthSquared() > radius )\r
- return ContainmentType::Intersects;\r
-\r
- vector.X = sphere.Center.X - box.Minimum.X;\r
- vector.Y = sphere.Center.Y - box.Minimum.Y;\r
- vector.Z = sphere.Center.Z - box.Minimum.Z;\r
-\r
- if( vector.LengthSquared() > radius )\r
- return ContainmentType::Intersects;\r
-\r
- return ContainmentType::Contains;\r
- }\r
-\r
- ContainmentType BoundingSphere::Contains( BoundingSphere sphere1, BoundingSphere sphere2 )\r
- {\r
- float distance;\r
- float x = sphere1.Center.X - sphere2.Center.X;\r
- float y = sphere1.Center.Y - sphere2.Center.Y;\r
- float z = sphere1.Center.Z - sphere2.Center.Z;\r
-\r
- distance = static_cast<float>( Math::Sqrt( (x * x) + (y * y) + (z * z) ) );\r
- float radius = sphere1.Radius;\r
- float radius2 = sphere2.Radius;\r
-\r
- if( radius + radius2 < distance )\r
- return ContainmentType::Disjoint;\r
-\r
- if( radius - radius2 < distance )\r
- return ContainmentType::Intersects;\r
-\r
- return ContainmentType::Contains;\r
- }\r
-\r
- ContainmentType BoundingSphere::Contains( BoundingSphere sphere, Vector3 vector )\r
- {\r
- float x = vector.X - sphere.Center.X;\r
- float y = vector.Y - sphere.Center.Y;\r
- float z = vector.Z - sphere.Center.Z;\r
-\r
- float distance = (x * x) + (y * y) + (z * z);\r
-\r
- if( distance >= (sphere.Radius * sphere.Radius) )\r
- return ContainmentType::Disjoint;\r
-\r
- return ContainmentType::Contains;\r
- }\r
-\r
- BoundingSphere BoundingSphere::FromBox( BoundingBox box )\r
- {\r
- BoundingSphere sphere;\r
- Vector3::Lerp( box.Minimum, box.Maximum, 0.5f, sphere.Center );\r
-\r
- float x = box.Minimum.X - box.Maximum.X;\r
- float y = box.Minimum.Y - box.Maximum.Y;\r
- float z = box.Minimum.Z - box.Maximum.Z;\r
-\r
- float distance = static_cast<float>( Math::Sqrt( (x * x) + (y * y) + (z * z) ) );\r
-\r
- sphere.Radius = distance * 0.5f;\r
-\r
- return sphere;\r
- }\r
-\r
- BoundingSphere BoundingSphere::FromPoints( array<Vector3>^ points )\r
- {\r
- D3DXVECTOR3 center;\r
- FLOAT radius;\r
- pin_ptr<Vector3> pinnedPoints = &points[0];\r
-\r
- HRESULT hr = D3DXComputeBoundingSphere( reinterpret_cast<const D3DXVECTOR3*>( pinnedPoints ), points->Length, sizeof(float) * 3, ¢er, &radius );\r
- if( RECORD_SDX( hr ).IsFailure )\r
- return BoundingSphere();\r
-\r
- BoundingSphere sphere;\r
- sphere.Center = Vector3( center.x, center.y, center.z );\r
- sphere.Radius = radius;\r
-\r
- return sphere;\r
- }\r
-\r
- BoundingSphere BoundingSphere::Merge( BoundingSphere sphere1, BoundingSphere sphere2 )\r
- {\r
- BoundingSphere sphere;\r
- Vector3 difference = sphere2.Center - sphere1.Center;\r
-\r
- float length = difference.Length();\r
- float radius = sphere1.Radius;\r
- float radius2 = sphere2.Radius;\r
-\r
- if( radius + radius2 >= length)\r
- {\r
- if( radius - radius2 >= length )\r
- return sphere1;\r
-\r
- if( radius2 - radius >= length )\r
- return sphere2;\r
- }\r
-\r
- Vector3 vector = difference * ( 1.0f / length );\r
- float min = Math::Min( -radius, length - radius2 );\r
- float max = ( Math::Max( radius, length + radius2 ) - min ) * 0.5f;\r
-\r
- sphere.Center = sphere1.Center + vector * ( max + min );\r
- sphere.Radius = max;\r
-\r
- return sphere;\r
- }\r
-\r
- bool BoundingSphere::Intersects( BoundingSphere sphere, BoundingBox box )\r
- {\r
- return BoundingBox::Intersects( box, sphere );\r
- }\r
-\r
- bool BoundingSphere::Intersects( BoundingSphere sphere1, BoundingSphere sphere2 )\r
- {\r
- float distance;\r
- distance = Vector3::DistanceSquared( sphere1.Center, sphere2.Center );\r
- float radius = sphere1.Radius;\r
- float radius2 = sphere2.Radius;\r
-\r
- if( (radius * radius) + (2.0f * radius * radius2) + (radius2 * radius2) <= distance )\r
- return false;\r
-\r
- return true;\r
- }\r
-\r
- bool BoundingSphere::Intersects( BoundingSphere sphere, Ray ray, [Out] float% distance )\r
- {\r
- return Ray::Intersects( ray, sphere, distance );\r
- }\r
-\r
- PlaneIntersectionType BoundingSphere::Intersects( BoundingSphere sphere, Plane plane )\r
- {\r
- return Plane::Intersects( plane, sphere );\r
- }\r
-\r
- bool BoundingSphere::operator == ( BoundingSphere left, BoundingSphere right )\r
- {\r
- return BoundingSphere::Equals( left, right );\r
- }\r
-\r
- bool BoundingSphere::operator != ( BoundingSphere left, BoundingSphere right )\r
- {\r
- return !BoundingSphere::Equals( left, right );\r
- }\r
-\r
- String^ BoundingSphere::ToString()\r
- {\r
- return String::Format( CultureInfo::CurrentCulture, "Center:{0} Radius:{1}", Center.ToString(), Radius.ToString(CultureInfo::CurrentCulture) );\r
- }\r
-\r
- int BoundingSphere::GetHashCode()\r
- {\r
- return Center.GetHashCode() + Radius.GetHashCode();\r
- }\r
-\r
- bool BoundingSphere::Equals( Object^ value )\r
- {\r
- if( value == nullptr )\r
- return false;\r
-\r
- if( value->GetType() != GetType() )\r
- return false;\r
-\r
- return Equals( safe_cast<BoundingSphere>( value ) );\r
- }\r
-\r
- bool BoundingSphere::Equals( BoundingSphere value )\r
- {\r
- return ( Center == value.Center && Radius == value.Radius );\r
- }\r
-\r
- bool BoundingSphere::Equals( BoundingSphere% value1, BoundingSphere% value2 )\r
- {\r
- return ( value1.Center == value2.Center && value1.Radius == value2.Radius );\r
- }\r
-}\r