Arc.ApplyTransform: rotation moves the center correctly but rotates the angles backwards

Open
#1,250 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
backend

Research direction

Start by reading src/ACadSharp/Entities/Arc.cs and tracing ApplyTransform and its angle-vector handling; compare that with the transform application paths described in the issue. Check the related Ellipse rotation handling and existing transform tests, then add regression coverage for rotations beyond 0° and 180°. Done means the arc angles and ellipse rotation follow the applied rotation while the center remains correct.

Written by the indexing model from the issue text.

Description

Arc.ApplyTransform: rotation moves the center correctly but rotates the angles backwards

Version: ACadSharp 3.7.1 (NuGet) — verified identical to master (src/ACadSharp/Entities/Arc.cs is byte-identical between tag v3.7.1 and master)
Runtime: .NET 10, Windows 11

Summary

Rotating an Arc with ApplyTransform places the arc's center correctly, but its
StartAngle / EndAngle are rotated by −φ instead of +φ. The arc therefore ends up
mirrored relative to its (correct) center.

Ellipse is affected as well, in a different way: its Rotation is not updated at all.

Line, Circle and LwPolyline rotate correctly. Translation, scaling and mirroring
(negative scale) are correct for all types.

Minimal reproduction

using ACadSharp.Entities;
using CSMath;

var arc = new Arc
{
    Center     = new XYZ(200, 0, 0),
    Radius     = 50,
    StartAngle = 0,                 // quarter circle, first quadrant
    EndAngle   = Math.PI / 2
};

arc.ApplyTransform(Transform.CreateRotation(XYZ.AxisZ, Math.PI / 2));   // +90°

Console.WriteLine($"Center: {arc.Center}");                              // (0, 200, 0)   correct
Console.WriteLine($"Angles: {arc.StartAngle * 180 / Math.PI} .. "
                + $"{arc.EndAngle * 180 / Math.PI}");                    // -90 .. 0
// expected:                                                                90 .. 180

The endpoints of the resulting arc are off by 70.7 mm for a radius of 50.

Which rotation angles are affected

Same arc, measuring the largest distance between the expected and the actual arc endpoint:

rotation 0° 15° 30° 45° 60° 90° 120° 135° 180° 225° 270° 315° 360°
error [mm] 0 25.9 50.0 70.7 86.6 70.7 86.6 70.7 0 70.7 70.7 70.7 0
✅ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ✅ ❌ ❌ ❌ ✅

Only 0° and 180° (and multiples) happen to be correct, because there −φ and +φ
describe the same arc. Note that 90° and 270° are not among the working cases — a test
suite that only checks 180° rotations will report success.

Root cause

Arc.ApplyTransform itself looks correct — it converts the angles to vectors, pushes them
through the matrix and converts back:

base.ApplyTransform(transform);                            // center: path A
Matrix3 trans = this.getWorldMatrix(transform, ...);       // == new Matrix3(transform.Matrix)
XY start = XY.Rotate(new XY(this.Radius, 0.0), this.StartAngle);
XYZ vStart = trans * new XYZ(start.X, start.Y, 0.0);       // angles: path B
this.StartAngle = startPoint.GetAngle();

The inconsistency is one layer below, in CSMath. The same matrix produces opposite
results depending on how it is applied:

var t = Transform.CreateRotation(XYZ.AxisZ, Math.PI / 2);
var p = new XYZ(100, 0, 0);

t.ApplyTransform(p, false);            // (0,  100, 0)  =  +90°   correct
new Matrix3(t.Matrix) * p;             // (0, -100, 0)  =  -90°   backwards

The stored matrix is [0 1; -1 0], i.e. the transpose of a +90° rotation.
Transform.ApplyTransform applies it using a row-vector convention (giving the correct
result), while operator *(Matrix3, XYZ) uses a column-vector convention.

Arc.ApplyTransform mixes the two: the center goes through path A (base.ApplyTransform),
the angles through path B (Matrix3 * XYZ). Hence center correct, angles mirrored.

This may be related to #1230, which mentions transforms being "transposed between the two"
in a different place.

Affected types (measured)

entity rotation note
Line ✅ correct
Circle ✅ correct no angles involved
LwPolyline ✅ correct vertices rotated, bulge unchanged (bulge is rotation-invariant)
Arc ❌ angles rotated by −φ center is correct
Ellipse ❌ Rotation not updated at all center is correct

Translation, scaling and mirroring (negative scale) are correct for all of the above.
Mirroring correctly flips Normal to (0,0,-1) and swaps start/end — that part works.

Workarounds (verified)

Until this is fixed, callers can compensate after a pure rotation by φ:

// Arc: shift both angles by +2φ
arc.ApplyTransform(t);
arc.StartAngle += 2 * phi;
arc.EndAngle   += 2 * phi;

// Ellipse: rotate the major axis by +φ (it is not rotated at all)
// e.g. by rotating MajorAxisEndPoint manually

Verified over 15 rotation angles: residual error 0.0000 mm, and the arc's opening angle
stays unchanged (no distortion). Also correct when combined with a translation.

Caveats: the caller must know φ explicitly (it is not readable from a composed matrix);
the correction must not be applied when the transform contains a mirror, since mirroring
is already correct; and the correction differs per type (+2φ for Arc, +φ for Ellipse).

Suggested fix

Make the two paths agree. Either have operator *(Matrix3, XYZ) use the same vector
convention as Transform.ApplyTransform, or have Arc/Ellipse route their angle vectors
through transform.ApplyRotation(...) instead of multiplying by Matrix3 directly.

Given #1230, #1227, #1111 and #1007, a single consistent convention across CSMath would
likely fix several of these at once.

Happy to provide the full test program if useful.

Dominant language
C#
Stars
873
Forks
223
Avg merge
1d 15h
Merged PRs (30d)
28

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DomCR/ACadSharp

All issues in DomCR/ACadSharp

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.