Skip to content

Commit

Permalink
Merge pull request #1177 from mcneel/1.24
Browse files Browse the repository at this point in the history
1.24
  • Loading branch information
kike-garbo authored Sep 4, 2024
2 parents 7a807fb + 76e70af commit f96158a
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/RhinoInside.Revit.External/DB/Extensions/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static bool IsEquivalent(this Element self, Element other)
if (ReferenceEquals(self, other))
return true;

if (!self.IsValidObject || !other.IsValidObject)
if (!(self?.IsValidObject is true) || !(other?.IsValidObject is true))
return false;

if (self?.Id != other?.Id)
Expand Down
98 changes: 83 additions & 15 deletions src/RhinoInside.Revit.External/Numerical.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,37 @@ internal static double Magnitude(double value)
{
return Math.Abs(value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double MinMagnitude(double x, double y)
{
#if NET5_0_OR_GREATER
return Math.MinMagnitude(x, y);
#else
double ax = Math.Abs(x);
double ay = Math.Abs(y);

return ax < ay ? x :
ax == ay ? (IsNegativeZero(x) ? -0.0 : y) :
double.IsNaN(ax) ? x : y;

#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double MaxMagnitude(double x, double y)
{
#if NET5_0_OR_GREATER
return Math.MaxMagnitude(x, y);
#else
double ax = Math.Abs(x);
double ay = Math.Abs(y);

return ax > ay ? x :
ax == ay ? (IsPositiveZero(x) ? +0.0 : y) :
double.IsNaN(ax) ? x : y;
#endif
}
#endregion

#region Interval
Expand All @@ -131,12 +162,10 @@ internal static double Magnitude(double value)
/// <param name="y">The value to compare with <paramref name="x"/>.</param>
/// <returns><paramref name="x"/> if is less than <paramref name="y"/>; otherwise <paramref name="y"/></returns>
/// <remarks>
/// This requires <see cref="double.NaN"/> inputs to not be propagated back to the caller and for -0.0 to be treated as less than +0.0.
/// This requires <see cref="double.NaN"/> inputs to be propagated back to the caller and for -0.0 to be treated as less than +0.0.
/// </remarks>
public static double Min(double x, double y) =>
x < y ? x :
x == y ? (IsNegativeZero(x) ? -0.0 : y) :
double.IsNaN(y) ? x : y ;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Min(double x, double y) => Math.Min(x, y);

/// <summary>
/// Computes the max point of the interval [<paramref name="x"/>, <paramref name="y"/>].
Expand All @@ -145,12 +174,10 @@ public static double Min(double x, double y) =>
/// <param name="y">The value to compare with <paramref name="x"/>.</param>
/// <returns><paramref name="x"/> if is grater than <paramref name="y"/>; otherwise <paramref name="y"/></returns>
/// <remarks>
/// This requires <see cref="double.NaN"/> inputs to not be propagated back to the caller and for -0.0 to be treated as less than +0.0.
/// This requires <see cref="double.NaN"/> inputs to be propagated back to the caller and for -0.0 to be treated as less than +0.0.
/// </remarks>
public static double Max(double x, double y) =>
x > y ? x :
x == y ? (IsPositiveZero(x) ? +0.0 : y) :
double.IsNaN(y) ? x : y ;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Max(double x, double y) => Math.Max(x, y);

/// <summary>
/// Computes the mean point of the interval [<paramref name="x"/>, <paramref name="y"/>].
Expand All @@ -165,17 +192,17 @@ public static double Mean(double x, double y)
{
if (x == y) return x;

var X = Math.Abs(x);
var Y = Math.Abs(y);
var ax = Math.Abs(x);
var ay = Math.Abs(y);

//// Avoids oveflow
//if (X <= double.MaxValue / 2 && Y <= double.MaxValue / 2)
//if (ax <= double.MaxValue * 0.5 && ay <= double.MaxValue * 0.5)
// return (x + y) * 0.5;

if (X < Constant.Upsilon)
if (ax < Constant.Upsilon)
return x + (y * 0.5);

if (Y < Constant.Upsilon)
if (ay < Constant.Upsilon)
return (x * 0.5) + y;

return (x * 0.5) + (y * 0.5);
Expand Down Expand Up @@ -227,11 +254,52 @@ public static double Mix(double x, double y, double t)
/// <param name="min">The lower bound of the result.</param>
/// <param name="max">The upper bound of the result.</param>
/// <returns>Clamped <paramref name="value"/> or <see cref="double.NaN"/> if <paramref name="value"/> equals <see cref="double.NaN"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Clamp(double value, double min, double max)
{
return value < min ? min : max < value ? max : value;
}
#endregion

#region Number
/// <summary>
/// Computes the min point of the interval [<paramref name="x"/>, <paramref name="y"/>].
/// </summary>
/// <param name="x">The value to compare with <paramref name="y"/>.</param>
/// <param name="y">The value to compare with <paramref name="x"/>.</param>
/// <returns><paramref name="x"/> if is less than <paramref name="y"/>; otherwise <paramref name="y"/></returns>
/// <remarks>
/// This requires <see cref="double.NaN"/> inputs to not be propagated back to the caller and for -0.0 to be treated as less than +0.0.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double MinNumber(double x, double y) =>
#if NET5_0_OR_GREATER
double.MinNumber(x, y);
#else
x < y ? x :
x == y ? (IsNegativeZero(x) ? -0.0 : y) :
double.IsNaN(y) ? x : y ;
#endif

/// <summary>
/// Computes the max point of the interval [<paramref name="x"/>, <paramref name="y"/>].
/// </summary>
/// <param name="x">The value to compare with <paramref name="y"/>.</param>
/// <param name="y">The value to compare with <paramref name="x"/>.</param>
/// <returns><paramref name="x"/> if is grater than <paramref name="y"/>; otherwise <paramref name="y"/></returns>
/// <remarks>
/// This requires <see cref="double.NaN"/> inputs to not be propagated back to the caller and for -0.0 to be treated as less than +0.0.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double MaxNumber(double x, double y) =>
#if NET5_0_OR_GREATER
double.MaxNumber(x, y);
#else
x > y ? x :
x == y ? (IsPositiveZero(x) ? +0.0 : y) :
double.IsNaN(y) ? x : y ;
#endif
#endregion
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/RhinoInside.Revit.GH/Components/Views/Extents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,17 @@ static void GetViewRangeOffsets(ARDB.View view, out double backOffset, out doubl
{
case ARDB.View3D view3D:
if (view3D.IsPerspective)
frontOffset = Arithmetic.Min(frontOffset, view3D.CropBox.Max.Z);
frontOffset = Arithmetic.MinNumber(frontOffset, view3D.CropBox.Max.Z);
break;

case ARDB.ViewPlan viewPlan:
var interval = viewPlan.GetViewRangeInterval();
backOffset = Arithmetic.Max(backOffset, interval.Left.Bound - view.Origin.Z);
frontOffset = Arithmetic.Min(frontOffset, interval.Right.Bound - view.Origin.Z);
backOffset = Arithmetic.MaxNumber(backOffset, interval.Left.Bound - view.Origin.Z);
frontOffset = Arithmetic.MinNumber(frontOffset, interval.Right.Bound - view.Origin.Z);
break;

case ARDB.ViewSection viewSection:
frontOffset = Arithmetic.Min(frontOffset, viewSection.CropBox.Max.Z);
frontOffset = Arithmetic.MinNumber(frontOffset, viewSection.CropBox.Max.Z);
break;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/RhinoInside.Revit.GH/Types/Element.Activator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public static Element FromReference(ARDB.Document doc, ARDB.Reference reference)
{ typeof(ARDB.CombinableElement), (element)=> new CombinableElement (element as ARDB.CombinableElement) },
{ typeof(ARDB.GeomCombination), (element)=> new GeomCombination (element as ARDB.GeomCombination) },
{ typeof(ARDB.GenericForm), (element)=> new GenericForm (element as ARDB.GenericForm) },
{ typeof(ARDB.ModelText), (element)=> new ModelText (element as ARDB.ModelText) },

{ typeof(ARDB.DatumPlane), (element)=> new DatumPlane (element as ARDB.DatumPlane) },
{ typeof(ARDB.Level), (element)=> new Level (element as ARDB.Level) },
Expand Down
File renamed without changes.
File renamed without changes.
62 changes: 62 additions & 0 deletions src/RhinoInside.Revit.GH/Types/Model/ModelText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using Rhino.Geometry;
using ARDB = Autodesk.Revit.DB;

namespace RhinoInside.Revit.GH.Types
{
using Convert.Geometry;
using External.DB.Extensions;

[Kernel.Attributes.Name("Model Text")]
public class ModelText : GeometricElement
{
protected override Type ValueType => typeof(ARDB.ModelText);
public new ARDB.ModelText Value => base.Value as ARDB.ModelText;

public ModelText() : base() { }
public ModelText(ARDB.ModelText modelText) : base(modelText) { }

#region Location
public override Plane Location
{
get
{
if (Value is ARDB.ModelText modelText)
{
using (var options = new ARDB.Options { DetailLevel = ARDB.ViewDetailLevel.Undefined })
{
var geometry = modelText.get_Geometry(options);
if (geometry.TryGetLocation(out var gO, out var gX, out var gY))
{
var xform = ARDB.Transform.Identity;
xform.SetAlignCoordSystem
(
ARDB.XYZ.Zero, External.DB.UnitXYZ.BasisX, External.DB.UnitXYZ.BasisY, External.DB.UnitXYZ.BasisZ,
gO, gX, gY, gX.CrossProduct(gY).ToUnitXYZ()
);

var box = geometry.GetBoundingBox(xform);

var origin = (box.GetCenter() - box.Transform.BasisZ * 0.5 * (box.Max.Z - box.Min.Z));
switch (modelText.HorizontalAlignment)
{
case ARDB.HorizontalAlign.Left: origin = box.GetCorners()[3]; break;
case ARDB.HorizontalAlign.Right: origin = box.GetCorners()[2]; break;
case ARDB.HorizontalAlign.Center: origin = (box.GetCorners()[3] + box.GetCorners()[2]) * 0.5; break;
}

return new Plane(origin.ToPoint3d(), box.Transform.BasisX.ToVector3d(), box.Transform.BasisY.ToVector3d());
}
}

var bbox = BoundingBox;
if (bbox.IsValid)
return new Plane(BoundingBox.Center, Vector3d.XAxis, Vector3d.YAxis);
}

return NaN.Plane;
}
}
#endregion
}
}
12 changes: 6 additions & 6 deletions src/RhinoInside.Revit.GH/Types/Views/ViewFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,16 @@ public override bool CastTo<Q>(ref Q target)

public Point3d Min => new Point3d
(
Arithmetic.Min( Value.FrustumLeft, Bound[AxisX].T0),
Arithmetic.Min( Value.FrustumBottom, Bound[AxisY].T0),
Arithmetic.Min(-Value.FrustumFar, Bound[AxisZ].T0)
Arithmetic.MinNumber( Value.FrustumLeft, Bound[AxisX].T0),
Arithmetic.MinNumber( Value.FrustumBottom, Bound[AxisY].T0),
Arithmetic.MinNumber(-Value.FrustumFar, Bound[AxisZ].T0)
);

public Point3d Max => new Point3d
(
Arithmetic.Max( Value.FrustumRight, Bound[AxisX].T1),
Arithmetic.Max( Value.FrustumTop, Bound[AxisY].T1),
Arithmetic.Max(-Value.FrustumNear, Bound[AxisZ].T1)
Arithmetic.MaxNumber( Value.FrustumRight, Bound[AxisX].T1),
Arithmetic.MaxNumber( Value.FrustumTop, Bound[AxisY].T1),
Arithmetic.MaxNumber(-Value.FrustumNear, Bound[AxisZ].T1)
);

internal ARDB.BoundingBoxXYZ ToBoundingBoxXYZ(bool ensurePositiveY = false)
Expand Down
8 changes: 4 additions & 4 deletions src/RhinoInside.Revit/Convert/Units/UnitScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace RhinoInside.Revit.Convert.Units
{
using Numerical;

[DebuggerDisplay("{Antecedent} ∶ {Consequent} ({Quotient})")]
readonly struct Ratio : IEquatable<Ratio>
{
Expand Down Expand Up @@ -391,10 +393,7 @@ public static double Convert(double value, UnitScale from, UnitScale to)
var den = f * T;

// Multiply value by resulting ratio considering magnitude.
if (Math.Abs(num) < Math.Abs(value))
return num * (value / den);
else
return value * (num / den);
return Arithmetic.MinMagnitude(num, value) * (Arithmetic.MaxMagnitude(num, value) / den);
}

public override string ToString() => Name ?? ((UnitSystem) this).ToString();
Expand Down Expand Up @@ -458,6 +457,7 @@ internal static void SetUnitScale(RhinoDoc doc, ActiveSpace space, UnitScale val
}

doc.ModelSpaceHatchScale *= scaleFactor;
doc.Linetypes.LinetypeScale *= scaleFactor;

foreach (var style in doc.DimStyles)
{
Expand Down

0 comments on commit f96158a

Please sign in to comment.