Skip to content

Commit 77cb8c9

Browse files
authored
Merge pull request #457 from DomCR/Geometric-transform
Geometric transform
2 parents 7d5174d + 4924e81 commit 77cb8c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2369
-1174
lines changed

src/ACadSharp.Tests/Entities/ArcTests.cs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace ACadSharp.Tests.Entities
88
{
9-
public class ArcTests
9+
public class ArcTests : CommonEntityTests<Arc>
1010
{
1111
[Fact]
1212
public void CreateFromBulgeTest()
@@ -99,5 +99,70 @@ public void GetEndVerticesTest()
9999
AssertUtils.AreEqual<XY>(start, s1, "start point");
100100
AssertUtils.AreEqual<XY>(end, e2, "end point");
101101
}
102+
103+
[Fact]
104+
public void TranslationTest()
105+
{
106+
double radius = 5;
107+
XYZ center = new XYZ(1, 1, 0);
108+
Arc arc = new Arc
109+
{
110+
Radius = radius,
111+
Center = center,
112+
};
113+
114+
XYZ move = new XYZ(5, 5, 0);
115+
Transform transform = Transform.CreateTranslation(move);
116+
arc.ApplyTransform(transform);
117+
118+
AssertUtils.AreEqual(XYZ.AxisZ, arc.Normal);
119+
AssertUtils.AreEqual(center.Add(move), arc.Center);
120+
Assert.Equal(radius, arc.Radius);
121+
Assert.Equal(0, arc.StartAngle);
122+
Assert.Equal(Math.PI, arc.EndAngle);
123+
}
124+
125+
[Fact]
126+
public void RotationTest()
127+
{
128+
double radius = 5;
129+
XYZ center = new XYZ(1, 1, 0);
130+
Arc arc = new Arc
131+
{
132+
Radius = radius,
133+
Center = center
134+
};
135+
136+
Transform transform = Transform.CreateRotation(XYZ.AxisX, MathHelper.DegToRad(90));
137+
arc.ApplyTransform(transform);
138+
139+
AssertUtils.AreEqual(new XYZ(1, 0, 1), arc.Center);
140+
Assert.Equal(radius, arc.Radius);
141+
Assert.Equal(Math.PI, arc.StartAngle);
142+
Assert.Equal(0, arc.EndAngle);
143+
AssertUtils.AreEqual(XYZ.AxisY, arc.Normal);
144+
}
145+
146+
[Fact]
147+
public void ScalingTest()
148+
{
149+
double radius = 5;
150+
XYZ center = new XYZ(1, 1, 0);
151+
Arc arc = new Arc
152+
{
153+
Radius = radius,
154+
Center = center
155+
};
156+
157+
XYZ scale = new XYZ(2, 2, 1);
158+
Transform transform = Transform.CreateScaling(scale, center);
159+
arc.ApplyTransform(transform);
160+
161+
AssertUtils.AreEqual(XYZ.AxisZ, arc.Normal);
162+
AssertUtils.AreEqual(center, arc.Center);
163+
Assert.Equal(10, arc.Radius);
164+
Assert.Equal(0, arc.StartAngle);
165+
Assert.Equal(Math.PI, arc.EndAngle);
166+
}
102167
}
103168
}

src/ACadSharp.Tests/Entities/CircleTests.cs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,73 @@
11
using ACadSharp.Entities;
2+
using ACadSharp.Tests.Common;
23
using CSMath;
34
using Xunit;
45

56
namespace ACadSharp.Tests.Entities
67
{
7-
public class CircleTests
8+
public class CircleTests : CommonEntityTests<Circle>
89
{
10+
private CSMathRandom _random = new CSMathRandom();
11+
12+
[Fact]
13+
public void TranslationTest()
14+
{
15+
double radius = 5;
16+
XYZ center = new XYZ(1, 1, 0);
17+
Circle circle = new Circle
18+
{
19+
Radius = radius,
20+
Center = center
21+
};
22+
23+
XYZ move = new XYZ(5, 5, 0);
24+
Transform transform = Transform.CreateTranslation(move);
25+
circle.ApplyTransform(transform);
26+
27+
AssertUtils.AreEqual(XYZ.AxisZ, circle.Normal);
28+
AssertUtils.AreEqual(center.Add(move), circle.Center);
29+
Assert.Equal(radius, circle.Radius);
30+
}
31+
32+
[Fact]
33+
public void RotationTest()
34+
{
35+
double radius = 5;
36+
XYZ center = new XYZ(1, 1, 0);
37+
Circle circle = new Circle
38+
{
39+
Radius = radius,
40+
Center = center
41+
};
42+
43+
Transform transform = Transform.CreateRotation(XYZ.AxisX, MathHelper.DegToRad(90));
44+
circle.ApplyTransform(transform);
45+
46+
AssertUtils.AreEqual(new XYZ(1, 0, 1), circle.Center);
47+
Assert.Equal(radius, circle.Radius);
48+
AssertUtils.AreEqual(XYZ.AxisY, circle.Normal);
49+
}
50+
51+
[Fact]
52+
public void ScalingTest()
53+
{
54+
double radius = 5;
55+
XYZ center = new XYZ(1, 1, 0);
56+
Circle circle = new Circle
57+
{
58+
Radius = radius,
59+
Center = center
60+
};
61+
62+
XYZ scale = new XYZ(2, 2, 1);
63+
Transform transform = Transform.CreateScaling(scale, center);
64+
circle.ApplyTransform(transform);
65+
66+
AssertUtils.AreEqual(XYZ.AxisZ, circle.Normal);
67+
AssertUtils.AreEqual(center, circle.Center);
68+
Assert.Equal(10, circle.Radius);
69+
}
70+
971
[Fact]
1072
public void GetBoundingBoxTest()
1173
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using ACadSharp.Entities;
2+
using Xunit;
3+
4+
namespace ACadSharp.Tests.Entities
5+
{
6+
public abstract class CommonEntityTests<T>
7+
where T : Entity, new()
8+
{
9+
[Fact]
10+
public void DefaultConstructor()
11+
{
12+
T entity = new T();
13+
14+
Assert.NotNull(entity);
15+
Assert.True(0 == entity.Handle);
16+
17+
Assert.NotEqual(ObjectType.UNDEFINED, entity.ObjectType);
18+
19+
Assert.False(string.IsNullOrEmpty(entity.ObjectName));
20+
Assert.False(string.IsNullOrEmpty(entity.SubclassMarker));
21+
22+
Assert.Null(entity.XDictionary);
23+
}
24+
}
25+
}

src/ACadSharp.Tests/Entities/LineTests.cs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,93 @@
11
using ACadSharp.Entities;
2+
using ACadSharp.Tests.Common;
23
using CSMath;
34
using Xunit;
45

56
namespace ACadSharp.Tests.Entities
67
{
7-
public class LineTests
8+
public class LineTests : CommonEntityTests<Line>
89
{
10+
private CSMathRandom _random = new CSMathRandom();
11+
12+
[Fact]
13+
public void TranslationTest()
14+
{
15+
var start = XYZ.Zero;
16+
var end = new XYZ(1, 1, 0);
17+
Line line = new Line
18+
{
19+
StartPoint = start,
20+
EndPoint = end,
21+
};
22+
23+
XYZ move = new XYZ(5, 5, 0);
24+
Transform transform = Transform.CreateTranslation(move);
25+
line.ApplyTransform(transform);
26+
27+
AssertUtils.AreEqual(start.Add(move), line.StartPoint);
28+
AssertUtils.AreEqual(end.Add(move), line.EndPoint);
29+
AssertUtils.AreEqual(XYZ.AxisZ, line.Normal);
30+
}
31+
32+
[Fact]
33+
public void RotationTest()
34+
{
35+
var start = XYZ.Zero;
36+
var end = new XYZ(1, 1, 0);
37+
Line line = new Line
38+
{
39+
StartPoint = start,
40+
EndPoint = end,
41+
};
42+
43+
Transform translation = Transform.CreateRotation(XYZ.AxisX, MathHelper.DegToRad(90));
44+
line.ApplyTransform(translation);
45+
46+
AssertUtils.AreEqual(start, line.StartPoint);
47+
AssertUtils.AreEqual(new XYZ(1, 0, 1), line.EndPoint);
48+
AssertUtils.AreEqual(XYZ.AxisY, line.Normal);
49+
}
50+
51+
[Fact]
52+
public void ScalingTest()
53+
{
54+
var start = new XYZ(-1, -1, 0);
55+
var end = new XYZ(1, 1, 0);
56+
Line line = new Line
57+
{
58+
StartPoint = start,
59+
EndPoint = end,
60+
};
61+
62+
XYZ scale = new XYZ(2, 2, 1);
63+
Transform transform = Transform.CreateScaling(scale);
64+
line.ApplyTransform(transform);
65+
66+
AssertUtils.AreEqual(start.Multiply(scale), line.StartPoint);
67+
AssertUtils.AreEqual(end.Multiply(scale), line.EndPoint);
68+
AssertUtils.AreEqual(XYZ.AxisZ, line.Normal);
69+
}
70+
71+
[Fact]
72+
public void RandomTranslationTest()
73+
{
74+
XYZ start = this._random.Next<XYZ>();
75+
XYZ end = this._random.Next<XYZ>();
76+
Line line = new Line
77+
{
78+
StartPoint = start,
79+
EndPoint = end,
80+
};
81+
82+
XYZ move = this._random.Next<XYZ>();
83+
Transform translation = Transform.CreateTranslation(move);
84+
line.ApplyTransform(translation);
85+
86+
AssertUtils.AreEqual(start.Add(move), line.StartPoint);
87+
AssertUtils.AreEqual(end.Add(move), line.EndPoint);
88+
}
89+
90+
991
[Fact]
1092
public void GetBoundingBoxTest()
1193
{
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using ACadSharp.Entities;
2+
using ACadSharp.Tests.Common;
3+
using CSMath;
4+
using Xunit;
5+
6+
namespace ACadSharp.Tests.Entities
7+
{
8+
public class PointTests : CommonEntityTests<Point>
9+
{
10+
private CSMathRandom _random = new CSMathRandom();
11+
12+
[Fact]
13+
public void TranslateTest()
14+
{
15+
XYZ init = _random.Next<XYZ>();
16+
XYZ transform = _random.Next<XYZ>();
17+
XYZ result = init + transform;
18+
19+
Point point = new Point(init);
20+
21+
point.ApplyTranslation(transform);
22+
23+
AssertUtils.AreEqual(result, point.Location, "Point Location");
24+
AssertUtils.AreEqual(XYZ.AxisZ, point.Normal);
25+
}
26+
27+
28+
[Fact]
29+
public void RotationTest()
30+
{
31+
XYZ init = new(5, 5, 0);
32+
33+
Point point = new Point(init);
34+
35+
Transform translation = Transform.CreateRotation(new XYZ(1, 0, 0), MathHelper.DegToRad(90));
36+
point.ApplyTransform(translation);
37+
38+
//Rotation around origin
39+
AssertUtils.AreEqual(new XYZ(5, 0, 5), point.Location, "Point Location");
40+
AssertUtils.AreEqual(XYZ.AxisY, point.Normal);
41+
}
42+
}
43+
}

src/ACadSharp/ACadSharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<PropertyGroup>
1818
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1919
<PackageReadmeFile>README.md</PackageReadmeFile>
20-
<Version>1.1.5</Version>
20+
<Version>1.1.6</Version>
2121
<PackageOutputPath>../nupkg</PackageOutputPath>
2222
</PropertyGroup>
2323

src/ACadSharp/Blocks/Block.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public Block(BlockRecord record) : base()
7676

7777
/// <inheritdoc/>
7878
/// <remarks>
79-
/// Cloning a block will also unatach it from the record
79+
/// Cloning a block will also unattached it from the record.
8080
/// </remarks>
8181
public override CadObject Clone()
8282
{
@@ -87,6 +87,14 @@ public override CadObject Clone()
8787
return clone;
8888
}
8989

90+
/// <inheritdoc/>
91+
/// <remarks>
92+
/// Block entities don't have any geometric properties.
93+
/// </remarks>
94+
public override void ApplyTransform(Transform transform)
95+
{
96+
}
97+
9098
/// <inheritdoc/>
9199
public override BoundingBox GetBoundingBox()
92100
{

src/ACadSharp/Blocks/BlockEnd.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,10 @@ public override BoundingBox GetBoundingBox()
4343
{
4444
return BoundingBox.Null;
4545
}
46+
47+
/// <inheritdoc/>
48+
public override void ApplyTransform(Transform transform)
49+
{
50+
}
4651
}
4752
}

0 commit comments

Comments
 (0)