2017-11-17 02:09:00 +00:00
|
|
|
// file: core/math/aabb.h
|
2017-11-05 10:37:59 +00:00
|
|
|
// commit: 7ad14e7a3e6f87ddc450f7e34621eb5200808451
|
2017-11-17 02:09:00 +00:00
|
|
|
// file: core/math/aabb.cpp
|
2017-11-05 10:37:59 +00:00
|
|
|
// commit: bd282ff43f23fe845f29a3e25c8efc01bd65ffb0
|
|
|
|
// file: core/variant_call.cpp
|
|
|
|
// commit: 5ad9be4c24e9d7dc5672fdc42cea896622fe5685
|
2018-04-08 03:28:24 +00:00
|
|
|
using System;
|
2018-03-01 07:51:35 +00:00
|
|
|
#if REAL_T_IS_DOUBLE
|
|
|
|
using real_t = System.Double;
|
|
|
|
#else
|
|
|
|
using real_t = System.Single;
|
|
|
|
#endif
|
|
|
|
|
2017-11-05 10:37:59 +00:00
|
|
|
namespace Godot
|
|
|
|
{
|
2017-11-17 02:09:00 +00:00
|
|
|
public struct AABB : IEquatable<AABB>
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
private Vector3 _position;
|
|
|
|
private Vector3 _size;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
|
|
|
public Vector3 Position
|
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
get { return _position; }
|
|
|
|
set { _position = value; }
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector3 Size
|
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
get { return _size; }
|
|
|
|
set { _size = value; }
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector3 End
|
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
get { return _position + _size; }
|
|
|
|
set { _size = value - _position; }
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public bool Encloses(AABB with)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
Vector3 src_min = _position;
|
|
|
|
Vector3 src_max = _position + _size;
|
|
|
|
Vector3 dst_min = with._position;
|
|
|
|
Vector3 dst_max = with._position + with._size;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-04-08 03:39:35 +00:00
|
|
|
return src_min.x <= dst_min.x &&
|
|
|
|
src_max.x > dst_max.x &&
|
|
|
|
src_min.y <= dst_min.y &&
|
|
|
|
src_max.y > dst_max.y &&
|
|
|
|
src_min.z <= dst_min.z &&
|
|
|
|
src_max.z > dst_max.z;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 19:22:04 +00:00
|
|
|
public AABB Expand(Vector3 point)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
Vector3 begin = _position;
|
|
|
|
Vector3 end = _position + _size;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-09-10 19:22:04 +00:00
|
|
|
if (point.x < begin.x)
|
|
|
|
begin.x = point.x;
|
|
|
|
if (point.y < begin.y)
|
|
|
|
begin.y = point.y;
|
|
|
|
if (point.z < begin.z)
|
|
|
|
begin.z = point.z;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-09-10 19:22:04 +00:00
|
|
|
if (point.x > end.x)
|
|
|
|
end.x = point.x;
|
|
|
|
if (point.y > end.y)
|
|
|
|
end.y = point.y;
|
|
|
|
if (point.z > end.z)
|
|
|
|
end.z = point.z;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
return new AABB(begin, end - begin);
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public real_t GetArea()
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
return _size.x * _size.y * _size.z;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Vector3 GetEndpoint(int idx)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
switch (idx)
|
|
|
|
{
|
|
|
|
case 0:
|
2018-08-23 15:45:18 +00:00
|
|
|
return new Vector3(_position.x, _position.y, _position.z);
|
2017-11-05 10:37:59 +00:00
|
|
|
case 1:
|
2018-08-23 15:45:18 +00:00
|
|
|
return new Vector3(_position.x, _position.y, _position.z + _size.z);
|
2017-11-05 10:37:59 +00:00
|
|
|
case 2:
|
2018-08-23 15:45:18 +00:00
|
|
|
return new Vector3(_position.x, _position.y + _size.y, _position.z);
|
2017-11-05 10:37:59 +00:00
|
|
|
case 3:
|
2018-08-23 15:45:18 +00:00
|
|
|
return new Vector3(_position.x, _position.y + _size.y, _position.z + _size.z);
|
2017-11-05 10:37:59 +00:00
|
|
|
case 4:
|
2018-08-23 15:45:18 +00:00
|
|
|
return new Vector3(_position.x + _size.x, _position.y, _position.z);
|
2017-11-05 10:37:59 +00:00
|
|
|
case 5:
|
2018-08-23 15:45:18 +00:00
|
|
|
return new Vector3(_position.x + _size.x, _position.y, _position.z + _size.z);
|
2017-11-05 10:37:59 +00:00
|
|
|
case 6:
|
2018-08-23 15:45:18 +00:00
|
|
|
return new Vector3(_position.x + _size.x, _position.y + _size.y, _position.z);
|
2017-11-05 10:37:59 +00:00
|
|
|
case 7:
|
2018-08-23 15:45:18 +00:00
|
|
|
return new Vector3(_position.x + _size.x, _position.y + _size.y, _position.z + _size.z);
|
2017-11-05 10:37:59 +00:00
|
|
|
default:
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(idx), String.Format("Index is {0}, but a value from 0 to 7 is expected.", idx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Vector3 GetLongestAxis()
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var axis = new Vector3(1f, 0f, 0f);
|
2018-08-23 15:45:18 +00:00
|
|
|
real_t max_size = _size.x;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.y > max_size)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
axis = new Vector3(0f, 1f, 0f);
|
2018-08-23 15:45:18 +00:00
|
|
|
max_size = _size.y;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.z > max_size)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
axis = new Vector3(0f, 0f, 1f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return axis;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Vector3.Axis GetLongestAxisIndex()
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var axis = Vector3.Axis.X;
|
2018-08-23 15:45:18 +00:00
|
|
|
real_t max_size = _size.x;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.y > max_size)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
axis = Vector3.Axis.Y;
|
2018-08-23 15:45:18 +00:00
|
|
|
max_size = _size.y;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.z > max_size)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
axis = Vector3.Axis.Z;
|
|
|
|
}
|
|
|
|
|
|
|
|
return axis;
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public real_t GetLongestAxisSize()
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
real_t max_size = _size.x;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.y > max_size)
|
|
|
|
max_size = _size.y;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.z > max_size)
|
|
|
|
max_size = _size.z;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
|
|
|
return max_size;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Vector3 GetShortestAxis()
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var axis = new Vector3(1f, 0f, 0f);
|
2018-08-23 15:45:18 +00:00
|
|
|
real_t max_size = _size.x;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.y < max_size)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
axis = new Vector3(0f, 1f, 0f);
|
2018-08-23 15:45:18 +00:00
|
|
|
max_size = _size.y;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.z < max_size)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
axis = new Vector3(0f, 0f, 1f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return axis;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Vector3.Axis GetShortestAxisIndex()
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var axis = Vector3.Axis.X;
|
2018-08-23 15:45:18 +00:00
|
|
|
real_t max_size = _size.x;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.y < max_size)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
axis = Vector3.Axis.Y;
|
2018-08-23 15:45:18 +00:00
|
|
|
max_size = _size.y;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.z < max_size)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
axis = Vector3.Axis.Z;
|
|
|
|
}
|
|
|
|
|
|
|
|
return axis;
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public real_t GetShortestAxisSize()
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
real_t max_size = _size.x;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.y < max_size)
|
|
|
|
max_size = _size.y;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_size.z < max_size)
|
|
|
|
max_size = _size.z;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
|
|
|
return max_size;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Vector3 GetSupport(Vector3 dir)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
Vector3 half_extents = _size * 0.5f;
|
|
|
|
Vector3 ofs = _position + half_extents;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
|
|
|
return ofs + new Vector3(
|
2018-04-08 03:39:35 +00:00
|
|
|
dir.x > 0f ? -half_extents.x : half_extents.x,
|
|
|
|
dir.y > 0f ? -half_extents.y : half_extents.y,
|
|
|
|
dir.z > 0f ? -half_extents.z : half_extents.z);
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public AABB Grow(real_t by)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var res = this;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-08-23 15:45:18 +00:00
|
|
|
res._position.x -= by;
|
|
|
|
res._position.y -= by;
|
|
|
|
res._position.z -= by;
|
|
|
|
res._size.x += 2.0f * by;
|
|
|
|
res._size.y += 2.0f * by;
|
|
|
|
res._size.z += 2.0f * by;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public bool HasNoArea()
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
return _size.x <= 0f || _size.y <= 0f || _size.z <= 0f;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public bool HasNoSurface()
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
return _size.x <= 0f && _size.y <= 0f && _size.z <= 0f;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public bool HasPoint(Vector3 point)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
if (point.x < _position.x)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
2018-08-23 15:45:18 +00:00
|
|
|
if (point.y < _position.y)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
2018-08-23 15:45:18 +00:00
|
|
|
if (point.z < _position.z)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
2018-08-23 15:45:18 +00:00
|
|
|
if (point.x > _position.x + _size.x)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
2018-08-23 15:45:18 +00:00
|
|
|
if (point.y > _position.y + _size.y)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
2018-08-23 15:45:18 +00:00
|
|
|
if (point.z > _position.z + _size.z)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public AABB Intersection(AABB with)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
Vector3 src_min = _position;
|
|
|
|
Vector3 src_max = _position + _size;
|
|
|
|
Vector3 dst_min = with._position;
|
|
|
|
Vector3 dst_max = with._position + with._size;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
|
|
|
Vector3 min, max;
|
|
|
|
|
|
|
|
if (src_min.x > dst_max.x || src_max.x < dst_min.x)
|
|
|
|
{
|
2017-11-17 02:09:00 +00:00
|
|
|
return new AABB();
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
2018-04-08 03:28:24 +00:00
|
|
|
|
2018-04-08 03:39:35 +00:00
|
|
|
min.x = src_min.x > dst_min.x ? src_min.x : dst_min.x;
|
|
|
|
max.x = src_max.x < dst_max.x ? src_max.x : dst_max.x;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
|
|
|
if (src_min.y > dst_max.y || src_max.y < dst_min.y)
|
|
|
|
{
|
2017-11-17 02:09:00 +00:00
|
|
|
return new AABB();
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
2018-04-08 03:28:24 +00:00
|
|
|
|
2018-04-08 03:39:35 +00:00
|
|
|
min.y = src_min.y > dst_min.y ? src_min.y : dst_min.y;
|
|
|
|
max.y = src_max.y < dst_max.y ? src_max.y : dst_max.y;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
|
|
|
if (src_min.z > dst_max.z || src_max.z < dst_min.z)
|
|
|
|
{
|
2017-11-17 02:09:00 +00:00
|
|
|
return new AABB();
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
2018-04-08 03:28:24 +00:00
|
|
|
|
2018-04-08 03:39:35 +00:00
|
|
|
min.z = src_min.z > dst_min.z ? src_min.z : dst_min.z;
|
|
|
|
max.z = src_max.z < dst_max.z ? src_max.z : dst_max.z;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
return new AABB(min, max - min);
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public bool Intersects(AABB with)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_position.x >= with._position.x + with._size.x)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_position.x + _size.x <= with._position.x)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_position.y >= with._position.y + with._size.y)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_position.y + _size.y <= with._position.y)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_position.z >= with._position.z + with._size.z)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
2018-08-23 15:45:18 +00:00
|
|
|
if (_position.z + _size.z <= with._position.z)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public bool IntersectsPlane(Plane plane)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
Vector3[] points =
|
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
new Vector3(_position.x, _position.y, _position.z),
|
|
|
|
new Vector3(_position.x, _position.y, _position.z + _size.z),
|
|
|
|
new Vector3(_position.x, _position.y + _size.y, _position.z),
|
|
|
|
new Vector3(_position.x, _position.y + _size.y, _position.z + _size.z),
|
|
|
|
new Vector3(_position.x + _size.x, _position.y, _position.z),
|
|
|
|
new Vector3(_position.x + _size.x, _position.y, _position.z + _size.z),
|
|
|
|
new Vector3(_position.x + _size.x, _position.y + _size.y, _position.z),
|
|
|
|
new Vector3(_position.x + _size.x, _position.y + _size.y, _position.z + _size.z)
|
2017-11-05 10:37:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool over = false;
|
|
|
|
bool under = false;
|
|
|
|
|
2018-04-16 22:33:42 +00:00
|
|
|
for (int i = 0; i < 8; i++)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2017-11-21 22:32:19 +00:00
|
|
|
if (plane.DistanceTo(points[i]) > 0)
|
2017-11-05 10:37:59 +00:00
|
|
|
over = true;
|
|
|
|
else
|
|
|
|
under = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return under && over;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public bool IntersectsSegment(Vector3 from, Vector3 to)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-03-01 07:51:35 +00:00
|
|
|
real_t min = 0f;
|
|
|
|
real_t max = 1f;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-04-16 22:33:42 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-09-10 19:22:04 +00:00
|
|
|
real_t segFrom = from[i];
|
|
|
|
real_t segTo = to[i];
|
|
|
|
real_t boxBegin = _position[i];
|
|
|
|
real_t boxEnd = boxBegin + _size[i];
|
2018-03-01 07:51:35 +00:00
|
|
|
real_t cmin, cmax;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-09-10 19:22:04 +00:00
|
|
|
if (segFrom < segTo)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-09-10 19:22:04 +00:00
|
|
|
if (segFrom > boxEnd || segTo < boxBegin)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
|
|
|
|
2018-09-10 19:22:04 +00:00
|
|
|
real_t length = segTo - segFrom;
|
|
|
|
cmin = segFrom < boxBegin ? (boxBegin - segFrom) / length : 0f;
|
|
|
|
cmax = segTo > boxEnd ? (boxEnd - segFrom) / length : 1f;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-10 19:22:04 +00:00
|
|
|
if (segTo > boxEnd || segFrom < boxBegin)
|
2017-11-05 10:37:59 +00:00
|
|
|
return false;
|
|
|
|
|
2018-09-10 19:22:04 +00:00
|
|
|
real_t length = segTo - segFrom;
|
|
|
|
cmin = segFrom > boxEnd ? (boxEnd - segFrom) / length : 0f;
|
|
|
|
cmax = segTo < boxBegin ? (boxBegin - segFrom) / length : 1f;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cmin > min)
|
|
|
|
{
|
|
|
|
min = cmin;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmax < max)
|
|
|
|
max = cmax;
|
|
|
|
if (max < min)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public AABB Merge(AABB with)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-09-10 19:22:04 +00:00
|
|
|
Vector3 beg1 = _position;
|
|
|
|
Vector3 beg2 = with._position;
|
|
|
|
var end1 = new Vector3(_size.x, _size.y, _size.z) + beg1;
|
|
|
|
var end2 = new Vector3(with._size.x, with._size.y, with._size.z) + beg2;
|
2017-11-05 10:37:59 +00:00
|
|
|
|
2018-04-08 03:30:43 +00:00
|
|
|
var min = new Vector3(
|
2018-09-10 19:22:04 +00:00
|
|
|
beg1.x < beg2.x ? beg1.x : beg2.x,
|
|
|
|
beg1.y < beg2.y ? beg1.y : beg2.y,
|
|
|
|
beg1.z < beg2.z ? beg1.z : beg2.z
|
2017-11-05 10:37:59 +00:00
|
|
|
);
|
|
|
|
|
2018-04-08 03:30:43 +00:00
|
|
|
var max = new Vector3(
|
2018-09-10 19:22:04 +00:00
|
|
|
end1.x > end2.x ? end1.x : end2.x,
|
|
|
|
end1.y > end2.y ? end1.y : end2.y,
|
|
|
|
end1.z > end2.z ? end1.z : end2.z
|
2017-11-05 10:37:59 +00:00
|
|
|
);
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
return new AABB(min, max - min);
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
2018-03-01 07:51:35 +00:00
|
|
|
|
|
|
|
// Constructors
|
2017-11-17 02:09:00 +00:00
|
|
|
public AABB(Vector3 position, Vector3 size)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
_position = position;
|
|
|
|
_size = size;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
public static bool operator ==(AABB left, AABB right)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
return left.Equals(right);
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
public static bool operator !=(AABB left, AABB right)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
|
|
|
return !left.Equals(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
2017-11-17 02:09:00 +00:00
|
|
|
if (obj is AABB)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2017-11-17 02:09:00 +00:00
|
|
|
return Equals((AABB)obj);
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
public bool Equals(AABB other)
|
2017-11-05 10:37:59 +00:00
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
return _position == other._position && _size == other._size;
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
return _position.GetHashCode() ^ _size.GetHashCode();
|
2017-11-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return String.Format("{0} - {1}", new object[]
|
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
_position.ToString(),
|
|
|
|
_size.ToString()
|
2017-11-05 10:37:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ToString(string format)
|
|
|
|
{
|
|
|
|
return String.Format("{0} - {1}", new object[]
|
|
|
|
{
|
2018-08-23 15:45:18 +00:00
|
|
|
_position.ToString(format),
|
|
|
|
_size.ToString(format)
|
2017-11-05 10:37:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|