Skip to content

Commit

Permalink
Task/increase test coverage v3 (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpne authored Jul 17, 2023
1 parent fecd455 commit 124c721
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 75 deletions.
Binary file added ExampleFiles/Components/Elem2dFromBrep.gh
Binary file not shown.
75 changes: 0 additions & 75 deletions GsaGH/Helpers/GH/RhinoConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -625,81 +625,6 @@ public static Mesh ConvertMeshToTriMeshSolid(Mesh mesh) {
return m;
}

/// <summary>
/// Method to convert a NURBS Brep into a planar trimmed surface with PolyCurve
/// internal and external edges of lines and arcs
/// BRep conversion to planar routine first converts the external edge to a PolyCurve
/// of lines and arcs and uses these controlpoints to fit a plane through points.
/// Will output a Tuple containing:
/// - PolyCurve
/// - TopologyList of control points
/// - TopoTypeList (" " or "a") corrosponding to control points
/// - List of PolyCurves for internal (void) curves
/// - Corrosponding list of topology points
/// - Corrosponding list of topologytypes
/// </summary>
/// <param name="brep"></param>
/// <param name="tolerance"></param>
/// <returns></returns>
public static
Tuple<PolyCurve, List<Point3d>, List<string>, List<PolyCurve>, List<List<Point3d>>,
List<List<string>>> ConvertPolyBrep(Brep brep, double tolerance = -1) {
var voidCrvs = new List<PolyCurve>();
var voidTopo = new List<List<Point3d>>();
var voidTopoType = new List<List<string>>();

Curve outer = null;
var inner = new List<Curve>();
foreach (BrepLoop brepLoop in brep.Loops) {
if (brepLoop.LoopType == BrepLoopType.Outer) {
outer = brepLoop.To3dCurve();
} else {
inner.Add(brepLoop.To3dCurve());
}
}

var edges = new List<Curve> {
outer,
};
edges.AddRange(inner);

for (int i = 0; i < edges.Count; i++) {
if (edges[i].IsPlanar()) {
continue;
}

List<Point3d> ctrlPts;
if (edges[0].TryGetPolyline(out Polyline polyline)) {
ctrlPts = polyline.ToList();
} else {
Tuple<PolyCurve, List<Point3d>, List<string>> convertBadSrf
= ConvertMem2dCrv(edges[0], tolerance);
ctrlPts = convertBadSrf.Item2;
}

Plane.FitPlaneToPoints(ctrlPts, out Plane plane);
for (int j = 0; j < edges.Count; j++) {
edges[j] = Curve.ProjectToPlane(edges[j], plane);
}
}

Tuple<PolyCurve, List<Point3d>, List<string>> convert = ConvertMem2dCrv(edges[0], tolerance);
PolyCurve edgeCrv = convert.Item1;
List<Point3d> point3ds = convert.Item2;
List<string> topoType = convert.Item3;

for (int i = 1; i < edges.Count; i++) {
convert = ConvertMem2dCrv(edges[i], tolerance);
voidCrvs.Add(convert.Item1);
voidTopo.Add(convert.Item2);
voidTopoType.Add(convert.Item3);
}

return new
Tuple<PolyCurve, List<Point3d>, List<string>, List<PolyCurve>, List<List<Point3d>>,
List<List<string>>>(edgeCrv, point3ds, topoType, voidCrvs, voidTopo, voidTopoType);
}

/// <summary>
/// Method to convert a NURBS Brep into a planar trimmed surface with PolyCurve
/// internal and external edges of lines and arcs.
Expand Down
61 changes: 61 additions & 0 deletions GsaGHTests/Helpers/GH/RhinoConversionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using GsaGH.Components;
using GsaGH.Helpers.GH;
using GsaGH.Helpers.Import;
using GsaGHTests.Helpers;
using OasysGH.Components;
using Rhino.Geometry;
using Xunit;

namespace GsaGHTests.Helpers.Export.GH {
[Collection("GrasshopperFixture collection")]
public class RhinoConversionsTests {
[Fact]
public void BuildArcLineCurveFromPtsAndTopoTypeTestArc1d() {
var topolist = new List<Point3d>() {
new Point3d(0, 0, 0),
new Point3d(2, 2, 0),
new Point3d(4, 0, 0)
};
var topotype = new List<string>() {
string.Empty,
"A",
string.Empty,
};

PolyCurve crv = RhinoConversions.BuildArcLineCurveFromPtsAndTopoType(topolist, topotype);
Assert.True(crv != null);
Assert.True(crv.IsArc());
}

[Fact]
public void ConvertMem1dCrvTest() {
var topolist = new List<Point3d>() {
new Point3d(0, 0, 0),
new Point3d(2, 2, 0),
new Point3d(4, 0, 0)
};
var crvs = new PolyCurve();
crvs.Append(new Arc(topolist[0], topolist[1], topolist[2]));

Tuple<PolyCurve, List<Point3d>, List<string>> mem1d = RhinoConversions.ConvertMem1dCrv(
crvs);

Assert.True(mem1d.Item1.IsArc());
Assert.Equal(3, mem1d.Item2.Count);
TestPoint(topolist[0], mem1d.Item2[0]);
TestPoint(topolist[1], mem1d.Item2[1]);
TestPoint(topolist[2], mem1d.Item2[2]);
Assert.Equal(string.Empty, mem1d.Item3[0]);
Assert.Equal("A", mem1d.Item3[1]);
Assert.Equal(string.Empty, mem1d.Item3[2]);
}

private void TestPoint(Point3d expected, Point3d actual) {
Assert.Equal(expected.X, actual.X, 10);
Assert.Equal(expected.Y, actual.Y, 10);
Assert.Equal(expected.Z, actual.Z, 10);
}
}
}
32 changes: 32 additions & 0 deletions GsaGHTests/Helpers/GsaAPI/ValueHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using GsaGH.Helpers.GsaApi;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace GsaGHTests.Helpers.GsaApi {
public class ValueHelpersTests {
[Theory]
[InlineData(1.23456789, 4, "1.235")]
[InlineData(123456, 4, "123500")]
[InlineData(0.000123456, 4, "0.0001235")]
[InlineData(-1.23456789, 4, "-1.235")]
[InlineData(-123456, 4, "-123500")]
[InlineData(-0.000123456, 4, "-0.0001235")]
public void RoundToSignificantDigitsTest(double value, int digits, string expected) {
double d = ResultHelper.RoundToSignificantDigits(value, digits);
Assert.Equal(expected, d.ToString());
}

[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(74, 26, 75, 25)]
[InlineData(7499, 26, 8000, 0)]
[InlineData(24, 15, 25, 15)]
[InlineData(0, -24, 0, -25)]
public void SmartRounderTests(double max, double min, double expectedMax, double expectedMin) {
List<double> vals = ResultHelper.SmartRounder(max, min);
Assert.Equal(expectedMax, vals[0]);
Assert.Equal(expectedMin, vals[1]);
}
}
}
62 changes: 62 additions & 0 deletions IntegrationTests/3_Components/Elem2dFromBrepTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.IO;
using System.Reflection;
using Grasshopper.Kernel;
using Xunit;

namespace IntegrationTests.Components {
[Collection("GrasshopperFixture collection")]
public class Elem2dFromBrepTests {
public static GH_Document Document {
get {
if (document == null) {
document = OpenDocument();
}

return document;
}
}
private static GH_Document document = null;

[Fact]
public void NoRuntimeErrorTest() {
Helper.TestNoRuntimeMessagesInDocument(Document, GH_RuntimeMessageLevel.Error);
Helper.TestNoRuntimeMessagesInDocument(Document, GH_RuntimeMessageLevel.Warning);
}

[Theory]
[InlineData("Types", new string[] {
"QUAD8",
"TRI6"
})]
[InlineData("TriQuadCounts", new int[] {
0,
100,
540,
})]
[InlineData("Inclusions", new int[] {
1,
14,
})]
public void Test(string groupIdentifier, object expected) {
IGH_Param param = Helper.FindParameter(Document, groupIdentifier);
Helper.TestGhPrimitives(param, expected);
}

private static GH_Document OpenDocument() {
Type thisClass = MethodBase.GetCurrentMethod().DeclaringType;
string fileName = thisClass.Name + ".gh";
fileName = fileName.Replace(thisClass.Namespace, string.Empty).Replace("Tests", string.Empty);

string solutiondir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent
.Parent.FullName;
string path = Path.Combine(new string[] {
solutiondir,
"ExampleFiles",
"Components",
});

return Helper.CreateDocument(Path.Combine(path, fileName));
}
}
}

0 comments on commit 124c721

Please sign in to comment.