#region License
/*
MIT License
Copyright(c) 2017 Mattias Edlund
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.
*/
#endregion
using System;
using System.Runtime.CompilerServices;
namespace UnityMeshSimplifier
{
///
/// Math helpers.
///
public static class MathHelper
{
#region Consts
///
/// The Pi constant.
///
public const float PI = 3.14159274f;
///
/// The Pi constant.
///
public const double PId = 3.1415926535897932384626433832795;
///
/// Degrees to radian constant.
///
public const float Deg2Rad = PI / 180f;
///
/// Degrees to radian constant.
///
public const double Deg2Radd = PId / 180.0;
///
/// Radians to degrees constant.
///
public const float Rad2Deg = 180f / PI;
///
/// Radians to degrees constant.
///
public const double Rad2Degd = 180.0 / PId;
#endregion
#region Min
///
/// Returns the minimum of three values.
///
/// The first value.
/// The second value.
/// The third value.
/// The minimum value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Min(double val1, double val2, double val3)
{
return (val1 < val2 ? (val1 < val3 ? val1 : val3) : (val2 < val3 ? val2 : val3));
}
#endregion
#region Clamping
///
/// Clamps a value between a minimum and a maximum value.
///
/// The value to clamp.
/// The minimum value.
/// The maximum value.
/// The clamped value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Clamp(double value, double min, double max)
{
return (value >= min ? (value <= max ? value : max) : min);
}
#endregion
#region Triangle Area
///
/// Calculates the area of a triangle.
///
/// The first point.
/// The second point.
/// The third point.
/// The triangle area.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double TriangleArea(ref Vector3d p0, ref Vector3d p1, ref Vector3d p2)
{
var dx = p1 - p0;
var dy = p2 - p0;
return dx.Magnitude * (Math.Sin(Vector3d.Angle(ref dx, ref dy) * Deg2Radd) * dy.Magnitude) * 0.5f;
}
#endregion
}
}