@@ -315,10 +315,11 @@ contains
315315 !> This procedure creates a transformation matrix.
316316 !! @param p Parameters for the transformation.
317317 !! @return Transformation matrix.
318- function f_create_transform_matrix(p) result(out_matrix)
318+ function f_create_transform_matrix(p, center ) result(out_matrix)
319319
320320 type(ic_model_parameters), intent(in) :: p
321- t_mat4x4 :: sc, rz, rx, ry, tr, out_matrix
321+ t_vec3, optional, intent(in) :: center
322+ t_mat4x4 :: sc, rz, rx, ry, tr, t_back, t_to_origin, out_matrix
322323
323324 sc = transpose(reshape([ &
324325 p%scale(1), 0._wp, 0._wp, 0._wp, &
@@ -350,7 +351,25 @@ contains
350351 0._wp, 0._wp, 1._wp, p%translate(3), &
351352 0._wp, 0._wp, 0._wp, 1._wp], shape(tr)))
352353
353- out_matrix = matmul(tr, matmul(ry, matmul(rx, matmul(rz, sc))))
354+ if (present(center)) then
355+ ! Translation matrix to move center to the origin
356+ t_to_origin = transpose(reshape([ &
357+ 1._wp, 0._wp, 0._wp, -center(1), &
358+ 0._wp, 1._wp, 0._wp, -center(2), &
359+ 0._wp, 0._wp, 1._wp, -center(3), &
360+ 0._wp, 0._wp, 0._wp, 1._wp], shape(tr)))
361+
362+ ! Translation matrix to move center back to original position
363+ t_back = transpose(reshape([ &
364+ 1._wp, 0._wp, 0._wp, center(1), &
365+ 0._wp, 1._wp, 0._wp, center(2), &
366+ 0._wp, 0._wp, 1._wp, center(3), &
367+ 0._wp, 0._wp, 0._wp, 1._wp], shape(tr)))
368+
369+ out_matrix = matmul(tr, matmul(t_back, matmul(ry, matmul(rx, matmul(rz, matmul(sc, t_to_origin))))))
370+ else
371+ out_matrix = matmul(ry, matmul(rx, rz))
372+ end if
354373
355374 end function f_create_transform_matrix
356375
@@ -372,10 +391,10 @@ contains
372391 !> This procedure transforms a triangle by a matrix, one vertex at a time.
373392 !! @param triangle Triangle to transform.
374393 !! @param matrix Transformation matrix.
375- subroutine s_transform_triangle(triangle, matrix)
394+ subroutine s_transform_triangle(triangle, matrix, matrix_n )
376395
377396 type(t_triangle), intent(inout) :: triangle
378- t_mat4x4, intent(in) :: matrix
397+ t_mat4x4, intent(in) :: matrix, matrix_n
379398
380399 integer :: i
381400
@@ -385,20 +404,22 @@ contains
385404 call s_transform_vec(triangle%v(i, :), matrix)
386405 end do
387406
407+ call s_transform_vec(triangle%n(1:3), matrix_n)
408+
388409 end subroutine s_transform_triangle
389410
390411 !> This procedure transforms a model by a matrix, one triangle at a time.
391412 !! @param model Model to transform.
392413 !! @param matrix Transformation matrix.
393- subroutine s_transform_model(model, matrix)
414+ subroutine s_transform_model(model, matrix, matrix_n )
394415
395416 type(t_model), intent(inout) :: model
396- t_mat4x4, intent(in) :: matrix
417+ t_mat4x4, intent(in) :: matrix, matrix_n
397418
398419 integer :: i
399420
400421 do i = 1, size(model%trs)
401- call s_transform_triangle(model%trs(i), matrix)
422+ call s_transform_triangle(model%trs(i), matrix, matrix_n )
402423 end do
403424
404425 end subroutine s_transform_model
0 commit comments