Skip to content

Conversation

@mohamadalbaaj
Copy link
Contributor

Description
This pull request addresses a dimension mismatch issue in matrix multiplication. The original code attempts to multiply a 4x4 transformation matrix (mat) by a 3-element vertex array (verts), resulting in an invalid operation. The proposed fix involves ensuring that the vertex array has a fourth element representing the homogeneous coordinate before performing the matrix multiplication.

Changes Made
The original code: np.array([mat @ np.array([verts[i], verts[i + 1], verts[i + 2]]) for i in range(0, len(verts), 3)])

the updated code: np.delete((mat @ np.hstack((verts, np.ones((len(verts), 1)))).T).T, -1, axis=1)

Explanation
To resolve the dimension mismatch, a modification has been made to ensure that each vertex array has a fourth element (homogeneous coordinate) before the matrix multiplication. This is achieved by appending a column of ones to the verts array.

  1. np.ones((len(verts), 1)): This creates a column vector of ones with a length equal to the number of rows in the verts array. This is done to represent the homogeneous coordinates

  2. np.hstack((verts, np.ones((len(verts), 1)))): This horizontally stacks the verts array with the column vector of ones. This is essentially adding an extra column to verts to represent homogeneous coordinates.

  3. (mat @ np.hstack((verts, np.ones((len(verts), 1)))).T: The .T transposes the vertecies to match the dimension of the transformation matrix and perform a valid multiplication

  4. mat @ np.hstack((verts, np.ones((len(verts), 1)))): This performs matrix multiplication between the matrix mat and the stacked array

  5. (mat @ np.hstack((verts, np.ones((len(verts), 1)))).T).T: The second .T transposes the matrix back to its original form.

  6. np.delete(..., -1, axis=1): This deletes the last column from the result of the matrix multiplication.

Example

mat [
[ 0.0, -1.0, 0.0, 0.0],
[ 1.0, 0.0, 0.0, -0.175],
[ 0.0, 0.0, 1.0, 0.0],
[ 0.0, 0.0, 0.0, 1.0]
]

verts [ [ 8.35, 0.175, 0. ],
[ 8.35, 0.175, 5.638],
[ 0. , 0.175, 5.638],
[ 0. , 0.175, 0. ],
[ 0. , -0.175, 5.638],
[ 0. , -0.175, 0. ],
[ 8.35, -0.175, 5.638],
[ 8.35, -0.175, 0. ]
]

result [ [-0.175, 8.175, 0. ],
[-0.175, 8.175, 5.638],
[-0.175, -0.175, 5.638],
[-0.175, -0.175, 0. ],
[ 0.175, -0.175, 5.638],
[ 0.175, -0.175, 0. ],
[ 0.175, 8.175, 5.638],
[ 0.175, 8.175, 0. ]
]

Testing
The fix has been tested with various vertex arrays and transformation matrices to ensure correct behavior.

Code compiles successfully

Tests pass

Documentation updating, not necessary

Other ways to solve the problem:
(mat @ np.hstack((verts, np.ones((len(verts), 1)))).T)[:3, :].T
(mat @ np.hstack((verts, np.ones((len(verts), 1)))).T).T[:, :-1]
(mat @ np.column_stack((verts, np.ones(verts.shape[0]))).T).T[:, :-1]

@Moult Moult merged commit 83d3645 into IfcOpenShell:v0.7.0 Jan 14, 2024
@Moult
Copy link
Contributor

Moult commented Jan 14, 2024

Thanks so much! What an oversight!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants