Support mathutils.Vector.rotate for 2D vectors #73301

Closed
opened 2020-01-21 20:34:36 +01:00 by lob nico · 10 comments

System Information
Operating system: Windows-10-10.0.18362 64 Bits
Graphics card: GeForce GTX 970/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 441.08

Blender Version
Broken: version: 2.81 (sub 16), branch: master, commit date: 2019-12-04 11:32, hash: blender/blender@f1aa4d18d4
Worked: (optional)

Short description of error
Yet it is possible to rotate a 2D vector by a Matrix.Rotation
it cannot be done through mathutils.Vector.rotate

Exact steps for others to reproduce the error
// this works
v = Vector((1.0, 0.5))
m = Matrix.Rotation(radians(90), 2, 'X')
result = v @ m

// this will raise a ValueError: matrix must have minimum 3x3 dimensions
v = Vector((1.0, 0.5))
result = v.rotate(Matrix.Rotation(radians(90), 2, 'X'))

**System Information** Operating system: Windows-10-10.0.18362 64 Bits Graphics card: GeForce GTX 970/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 441.08 **Blender Version** Broken: version: 2.81 (sub 16), branch: master, commit date: 2019-12-04 11:32, hash: `blender/blender@f1aa4d18d4` Worked: (optional) **Short description of error** Yet it is possible to rotate a 2D vector by a Matrix.Rotation it cannot be done through mathutils.Vector.rotate **Exact steps for others to reproduce the error** // this works v = Vector((1.0, 0.5)) m = Matrix.Rotation(radians(90), 2, 'X') result = v @ m // this will raise a ValueError: matrix must have minimum 3x3 dimensions v = Vector((1.0, 0.5)) result = v.rotate(Matrix.Rotation(radians(90), 2, 'X'))
Author

Added subscriber: @lobnico

Added subscriber: @lobnico
lob nico changed title from mathutils.Vector.rotate callback TypeCheck fails on 2D vectors to python mathutils.Vector.rotate callback TypeCheck fails on 2D vectors 2020-01-21 21:34:21 +01:00
Campbell Barton changed title from python mathutils.Vector.rotate callback TypeCheck fails on 2D vectors to Support mathutils.Vector.rotate for 2D vectors 2020-01-22 08:17:42 +01:00

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

Added subscriber: @ideasman42

Added subscriber: @ideasman42

Marked this as a TODO for the Python module.

Marked this as a TODO for the Python module.

Added subscriber: @laurelkeys

Added subscriber: @laurelkeys

Hey @ideasman42 ,

I've just compiled Blender yesterday and started looking into this issue since it seemed like a good first contribution.

I noticed that on Vector_rotate (at source\blender\python\mathutils\mathutils_Vector.c:1283) there's a check for only allowing vectors with size 3 or 4.
However, the error that's raised comes from an earlier call to mathutils_any_to_rotmat, done right before the vector size check.

I added a "special case" check for when the vector has size = 2 and the matrix is 2x2 right before it.

I thought this would be better than changing mathutils_any_to_rotmat, as it'd need to resize the matrix to be 3x3 (or we'd have to create a new function to return 2x2 matrices).
Also, we'd also have to deal with Quaternions and Eulers, and imo using them to rotate 2D vectors should result in an error, since it's not an operation as "well defined" as with a 2x2 matrix.

If you think there's a better approach I'm happy to make the changes. Otherwise, can I create a Diff?

Cheers,
Tiago.

p.s.:
I noticed that v.rotate(m) is equivalent to m @ v, so when testing @lobnico 's example v.rotate(Matrix.Rotation(radians(90), 2, 'X')) will be different from v @ m.
Btw, this would be equivalent to a rotation of v along the Z axis, if we had made it a 3D vector by adding a Z component equal to 0.

fix-#73301.diff

Hey @ideasman42 , I've just compiled Blender yesterday and started looking into this issue since it seemed like a good first contribution. I noticed that on `Vector_rotate` (at `source\blender\python\mathutils\mathutils_Vector.c:1283`) there's a check for only allowing vectors with size 3 or 4. However, the error that's raised comes from an earlier call to `mathutils_any_to_rotmat`, done right before the vector size check. I added a "special case" check for when the vector has size = 2 and the matrix is 2x2 right before it. I thought this would be better than changing `mathutils_any_to_rotmat`, as it'd need to resize the matrix to be 3x3 (or we'd have to create a new function to return 2x2 matrices). Also, we'd also have to deal with Quaternions and Eulers, and imo using them to rotate 2D vectors should result in an error, since it's not an operation as "well defined" as with a 2x2 matrix. If you think there's a better approach I'm happy to make the changes. Otherwise, can I create a Diff? Cheers, Tiago. p.s.: I noticed that `v.rotate(m)` is equivalent to `m @ v`, so when testing @lobnico 's example `v.rotate(Matrix.Rotation(radians(90), 2, 'X'))` will be different from `v @ m`. Btw, this would be equivalent to a rotation of v along the Z axis, if we had made it a 3D vector by adding a Z component equal to 0. [fix-#73301.diff](https://archive.blender.org/developer/F8340541/fix-#73301.diff)

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Campbell Barton self-assigned this 2020-02-20 04:07:07 +01:00
Committed blender/blender@bc86eb1780

Added subscriber: @JacobMerrill-1

Added subscriber: @JacobMerrill-1
Sign in to join this conversation.
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-addons#73301
No description provided.