Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geometric transform #457

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion src/ACadSharp.Tests/Entities/ArcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace ACadSharp.Tests.Entities
{
public class ArcTests
public class ArcTests : CommonEntityTests<Arc>
{
[Fact]
public void CreateFromBulgeTest()
Expand Down Expand Up @@ -93,5 +93,70 @@ public void GetEndVerticesTest()
AssertUtils.AreEqual<XY>(start, s1, "start point");
AssertUtils.AreEqual<XY>(end, e2, "end point");
}

[Fact]
public void TranslationTest()
{
double radius = 5;
XYZ center = new XYZ(1, 1, 0);
Arc arc = new Arc
{
Radius = radius,
Center = center,
};

XYZ move = new XYZ(5, 5, 0);
Transform transform = Transform.CreateTranslation(move);
arc.ApplyTransform(transform);

AssertUtils.AreEqual(XYZ.AxisZ, arc.Normal);
AssertUtils.AreEqual(center.Add(move), arc.Center);
Assert.Equal(radius, arc.Radius);
Assert.Equal(0, arc.StartAngle);
Assert.Equal(Math.PI, arc.EndAngle);
}

[Fact]
public void RotationTest()
{
double radius = 5;
XYZ center = new XYZ(1, 1, 0);
Arc arc = new Arc
{
Radius = radius,
Center = center
};

Transform transform = Transform.CreateRotation(XYZ.AxisX, MathHelper.DegToRad(90));
arc.ApplyTransform(transform);

AssertUtils.AreEqual(new XYZ(1, 0, 1), arc.Center);
Assert.Equal(radius, arc.Radius);
Assert.Equal(Math.PI, arc.StartAngle);
Assert.Equal(0, arc.EndAngle);
AssertUtils.AreEqual(XYZ.AxisY, arc.Normal);
}

[Fact]
public void ScalingTest()
{
double radius = 5;
XYZ center = new XYZ(1, 1, 0);
Arc arc = new Arc
{
Radius = radius,
Center = center
};

XYZ scale = new XYZ(2, 2, 1);
Transform transform = Transform.CreateScaling(scale, center);
arc.ApplyTransform(transform);

AssertUtils.AreEqual(XYZ.AxisZ, arc.Normal);
AssertUtils.AreEqual(center, arc.Center);
Assert.Equal(10, arc.Radius);
Assert.Equal(0, arc.StartAngle);
Assert.Equal(Math.PI, arc.EndAngle);
}
}
}
64 changes: 63 additions & 1 deletion src/ACadSharp.Tests/Entities/CircleTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,73 @@
using ACadSharp.Entities;
using ACadSharp.Tests.Common;
using CSMath;
using Xunit;

namespace ACadSharp.Tests.Entities
{
public class CircleTests
public class CircleTests : CommonEntityTests<Circle>
{
private CSMathRandom _random = new CSMathRandom();

[Fact]
public void TranslationTest()
{
double radius = 5;
XYZ center = new XYZ(1, 1, 0);
Circle circle = new Circle
{
Radius = radius,
Center = center
};

XYZ move = new XYZ(5, 5, 0);
Transform transform = Transform.CreateTranslation(move);
circle.ApplyTransform(transform);

AssertUtils.AreEqual(XYZ.AxisZ, circle.Normal);
AssertUtils.AreEqual(center.Add(move), circle.Center);
Assert.Equal(radius, circle.Radius);
}

[Fact]
public void RotationTest()
{
double radius = 5;
XYZ center = new XYZ(1, 1, 0);
Circle circle = new Circle
{
Radius = radius,
Center = center
};

Transform transform = Transform.CreateRotation(XYZ.AxisX, MathHelper.DegToRad(90));
circle.ApplyTransform(transform);

AssertUtils.AreEqual(new XYZ(1, 0, 1), circle.Center);
Assert.Equal(radius, circle.Radius);
AssertUtils.AreEqual(XYZ.AxisY, circle.Normal);
}

[Fact]
public void ScalingTest()
{
double radius = 5;
XYZ center = new XYZ(1, 1, 0);
Circle circle = new Circle
{
Radius = radius,
Center = center
};

XYZ scale = new XYZ(2, 2, 1);
Transform transform = Transform.CreateScaling(scale, center);
circle.ApplyTransform(transform);

AssertUtils.AreEqual(XYZ.AxisZ, circle.Normal);
AssertUtils.AreEqual(center, circle.Center);
Assert.Equal(10, circle.Radius);
}

[Fact]
public void GetBoundingBoxTest()
{
Expand Down
25 changes: 25 additions & 0 deletions src/ACadSharp.Tests/Entities/CommonEntityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using ACadSharp.Entities;
using Xunit;

namespace ACadSharp.Tests.Entities
{
public abstract class CommonEntityTests<T>
where T : Entity, new()
{
[Fact]
public void DefaultConstructor()
{
T entity = new T();

Assert.NotNull(entity);
Assert.True(0 == entity.Handle);

Assert.NotEqual(ObjectType.UNDEFINED, entity.ObjectType);

Assert.False(string.IsNullOrEmpty(entity.ObjectName));
Assert.False(string.IsNullOrEmpty(entity.SubclassMarker));

Assert.Null(entity.XDictionary);
}
}
}
84 changes: 83 additions & 1 deletion src/ACadSharp.Tests/Entities/LineTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,93 @@
using ACadSharp.Entities;
using ACadSharp.Tests.Common;
using CSMath;
using Xunit;

namespace ACadSharp.Tests.Entities
{
public class LineTests
public class LineTests : CommonEntityTests<Line>
{
private CSMathRandom _random = new CSMathRandom();

[Fact]
public void TranslationTest()
{
var start = XYZ.Zero;
var end = new XYZ(1, 1, 0);
Line line = new Line
{
StartPoint = start,
EndPoint = end,
};

XYZ move = new XYZ(5, 5, 0);
Transform transform = Transform.CreateTranslation(move);
line.ApplyTransform(transform);

AssertUtils.AreEqual(start.Add(move), line.StartPoint);
AssertUtils.AreEqual(end.Add(move), line.EndPoint);
AssertUtils.AreEqual(XYZ.AxisZ, line.Normal);
}

[Fact]
public void RotationTest()
{
var start = XYZ.Zero;
var end = new XYZ(1, 1, 0);
Line line = new Line
{
StartPoint = start,
EndPoint = end,
};

Transform translation = Transform.CreateRotation(XYZ.AxisX, MathHelper.DegToRad(90));
line.ApplyTransform(translation);

AssertUtils.AreEqual(start, line.StartPoint);
AssertUtils.AreEqual(new XYZ(1, 0, 1), line.EndPoint);
AssertUtils.AreEqual(XYZ.AxisY, line.Normal);
}

[Fact]
public void ScalingTest()
{
var start = new XYZ(-1, -1, 0);
var end = new XYZ(1, 1, 0);
Line line = new Line
{
StartPoint = start,
EndPoint = end,
};

XYZ scale = new XYZ(2, 2, 1);
Transform transform = Transform.CreateScaling(scale);
line.ApplyTransform(transform);

AssertUtils.AreEqual(start.Multiply(scale), line.StartPoint);
AssertUtils.AreEqual(end.Multiply(scale), line.EndPoint);
AssertUtils.AreEqual(XYZ.AxisZ, line.Normal);
}

[Fact]
public void RandomTranslationTest()
{
XYZ start = this._random.Next<XYZ>();
XYZ end = this._random.Next<XYZ>();
Line line = new Line
{
StartPoint = start,
EndPoint = end,
};

XYZ move = this._random.Next<XYZ>();
Transform translation = Transform.CreateTranslation(move);
line.ApplyTransform(translation);

AssertUtils.AreEqual(start.Add(move), line.StartPoint);
AssertUtils.AreEqual(end.Add(move), line.EndPoint);
}


[Fact]
public void GetBoundingBoxTest()
{
Expand Down
43 changes: 43 additions & 0 deletions src/ACadSharp.Tests/Entities/PointTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using ACadSharp.Entities;
using ACadSharp.Tests.Common;
using CSMath;
using Xunit;

namespace ACadSharp.Tests.Entities
{
public class PointTests : CommonEntityTests<Point>
{
private CSMathRandom _random = new CSMathRandom();

[Fact]
public void TranslateTest()
{
XYZ init = _random.Next<XYZ>();
XYZ transform = _random.Next<XYZ>();
XYZ result = init + transform;

Point point = new Point(init);

point.ApplyTranslation(transform);

AssertUtils.AreEqual(result, point.Location, "Point Location");
AssertUtils.AreEqual(XYZ.AxisZ, point.Normal);
}


[Fact]
public void RotationTest()
{
XYZ init = new(5, 5, 0);

Point point = new Point(init);

Transform translation = Transform.CreateRotation(new XYZ(1, 0, 0), MathHelper.DegToRad(90));
point.ApplyTransform(translation);

//Rotation around origin
AssertUtils.AreEqual(new XYZ(5, 0, 5), point.Location, "Point Location");
AssertUtils.AreEqual(XYZ.AxisY, point.Normal);
}
}
}
8 changes: 7 additions & 1 deletion src/ACadSharp/Blocks/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Block(BlockRecord record) : base()

/// <inheritdoc/>
/// <remarks>
/// Cloning a block will also unatach it from the record
/// Cloning a block will also unattached it from the record
/// </remarks>
public override CadObject Clone()
{
Expand All @@ -86,6 +86,12 @@ public override CadObject Clone()
return clone;
}

public override void ApplyTransform(Transform transform)
{
throw new System.NotImplementedException();
}

/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
BoundingBox box = BoundingBox.Null;
Expand Down
5 changes: 5 additions & 0 deletions src/ACadSharp/Blocks/BlockEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@ public override BoundingBox GetBoundingBox()
{
return BoundingBox.Null;
}

/// <inheritdoc/>
public override void ApplyTransform(Transform transform)
{
}
}
}
Loading
Loading