102 lines
3.7 KiB
C#
102 lines
3.7 KiB
C#
/*
|
|
* Copyright (C) 2012-2020 CypherCore <http://github.com/CypherCore>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
using System;
|
|
using System.Numerics;
|
|
|
|
namespace Framework.GameMath
|
|
{
|
|
public class Box
|
|
{
|
|
public Vector3[] _edgeVector = new Vector3[3];
|
|
public Vector3 _center;
|
|
public float _area;
|
|
public float _volume;
|
|
|
|
public Box(Vector3 min, Vector3 max)
|
|
{
|
|
Vector3 bounds = new Vector3(max.X - min.X, max.Y - min.Y, max.Z - min.Z);
|
|
_edgeVector[0] = new Vector3(bounds.X, 0, 0);
|
|
_edgeVector[1] = new Vector3(0, bounds.Y, 0);
|
|
_edgeVector[2] = new Vector3(0, 0, bounds.Z);
|
|
bool finiteExtent = true;
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
{
|
|
if (!float.IsFinite(_edgeVector[i].Length()))
|
|
{
|
|
finiteExtent = false;
|
|
// If the extent is infinite along an axis, make the center zero to avoid NaNs
|
|
_center.SetAt(0.0f, i);
|
|
}
|
|
}
|
|
|
|
|
|
if (finiteExtent)
|
|
{
|
|
_volume = bounds.X * bounds.Y * bounds.Z;
|
|
}
|
|
else
|
|
{
|
|
_volume = float.PositiveInfinity;
|
|
}
|
|
|
|
_area = 2 *
|
|
(bounds.X * bounds.Y +
|
|
bounds.Y * bounds.Z +
|
|
bounds.Z * bounds.X);
|
|
}
|
|
|
|
public bool Contains(Vector3 point)
|
|
{
|
|
Vector3 u = _edgeVector[2];
|
|
Vector3 v = _edgeVector[1];
|
|
Vector3 w = _edgeVector[0];
|
|
|
|
Matrix4x4 M = new(u.X, v.X, w.X, 0.0f, u.Y, v.Y, w.Y, 0.0f, u.Z, v.Z, w.Z, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
// M^-1 * (point - _corner[0]) = point in unit cube's object space
|
|
// compute the inverse of M
|
|
Matrix4x4.Invert(M, out M);
|
|
Vector3 osPoint = Vector3.TransformNormal(point - Corner(0), M);
|
|
|
|
return (osPoint.X >= 0) && (osPoint.Y >= 0) && (osPoint.Z >= 0) &&
|
|
(osPoint.X <= 1) && (osPoint.Y <= 1) && (osPoint.Z <= 1);
|
|
}
|
|
|
|
Vector3 Corner(int i)
|
|
{
|
|
switch (i)
|
|
{
|
|
case 0: return _center + (0.5f * (-_edgeVector[0] - _edgeVector[1] - _edgeVector[2]));
|
|
case 1: return _center + (0.5f * (_edgeVector[0] - _edgeVector[1] - _edgeVector[2]));
|
|
case 2: return _center + (0.5f * (-_edgeVector[0] + _edgeVector[1] - _edgeVector[2]));
|
|
case 3: return _center + (0.5f * (_edgeVector[0] + _edgeVector[1] - _edgeVector[2]));
|
|
case 4: return _center + (0.5f * (-_edgeVector[0] - _edgeVector[1] + _edgeVector[2]));
|
|
case 5: return _center + (0.5f * (_edgeVector[0] - _edgeVector[1] + _edgeVector[2]));
|
|
case 6: return _center + (0.5f * (-_edgeVector[0] + _edgeVector[1] + _edgeVector[2]));
|
|
default: return _center + (0.5f * (_edgeVector[0] + _edgeVector[1] + _edgeVector[2]));//case 7
|
|
}
|
|
}
|
|
|
|
public bool isFinite()
|
|
{
|
|
return float.IsFinite(_volume);
|
|
}
|
|
}
|
|
}
|