rotation.scm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;;; Utilities for dealing with rotations
  21. ;;; Rotation matrices
  22. (define (rotate-x-matrix-2 cos-angle sin-angle)
  23. (matrix-by-rows
  24. (list 1 0 0)
  25. (list 0 cos-angle (- sin-angle))
  26. (list 0 sin-angle cos-angle)))
  27. (define (rotate-x-matrix angle)
  28. (rotate-x-matrix-2 (cos angle) (sin angle)))
  29. (define Rx-matrix rotate-x-matrix)
  30. (define (rotate-y-matrix-2 cos-angle sin-angle)
  31. (matrix-by-rows
  32. (list cos-angle 0 sin-angle)
  33. (list 0 1 0)
  34. (list (- sin-angle) 0 cos-angle)))
  35. (define (rotate-y-matrix angle)
  36. (rotate-y-matrix-2 (cos angle) (sin angle)))
  37. (define Ry-matrix rotate-y-matrix)
  38. (define (rotate-z-matrix-2 cos-angle sin-angle)
  39. (matrix-by-rows
  40. (list cos-angle (- sin-angle) 0)
  41. (list sin-angle cos-angle 0)
  42. (list 0 0 1)))
  43. (define (rotate-z-matrix angle)
  44. (rotate-z-matrix-2 (cos angle) (sin angle)))
  45. (define Rz-matrix rotate-z-matrix)
  46. (define (angle&axis->rotation-matrix theta n)
  47. ;; (assert (v:unit? n))
  48. (let ((x (ref n 0)) (y (ref n 1)) (z (ref n 2)))
  49. (let ((colatitude (acos z))
  50. (longitude (atan y x)))
  51. (* (rotate-z-matrix longitude)
  52. (rotate-y-matrix colatitude)
  53. (rotate-z-matrix theta)
  54. (m:transpose (rotate-y-matrix colatitude))
  55. (m:transpose (rotate-z-matrix longitude))))))
  56. ;;; Rotation tuples
  57. (define (rotate-x-tuple-2 c s)
  58. (m->s (down 'ignore 'ignore 'ignore)
  59. (rotate-x-matrix-2 c s)
  60. (up 'ignore 'ignore 'ignore)))
  61. (define (rotate-x-tuple angle)
  62. (rotate-x-tuple-2 (cos angle) (sin angle)))
  63. (define (rotate-y-tuple-2 c s)
  64. (m->s (down 'ignore 'ignore 'ignore)
  65. (rotate-y-matrix-2 c s)
  66. (up 'ignore 'ignore 'ignore)))
  67. (define (rotate-y-tuple angle)
  68. (rotate-y-tuple-2 (cos angle) (sin angle)))
  69. (define (rotate-z-tuple-2 c s)
  70. (m->s (down 'ignore 'ignore 'ignore)
  71. (rotate-z-matrix-2 c s)
  72. (up 'ignore 'ignore 'ignore)))
  73. (define (rotate-z-tuple angle)
  74. (rotate-z-tuple-2 (cos angle) (sin angle)))
  75. ;;; Rotation procedures
  76. (define (rotate-x-2 c s)
  77. (let ((tuple (rotate-x-tuple-2 c s)))
  78. (lambda (v) (* tuple v))))
  79. (define (rotate-x angle)
  80. (rotate-x-2 (cos angle) (sin angle)))
  81. (define (rotate-y-2 c s)
  82. (let ((tuple (rotate-y-tuple-2 c s)))
  83. (lambda (v) (* tuple v))))
  84. (define (rotate-y angle)
  85. (rotate-y-2 (cos angle) (sin angle)))
  86. (define (rotate-z-2 c s)
  87. (let ((tuple (rotate-z-tuple-2 c s)))
  88. (lambda (v) (* tuple v))))
  89. (define (rotate-z angle)
  90. (rotate-z-2 (cos angle) (sin angle)))
  91. (define (wcross->w A)
  92. (up (ref A 1 2)
  93. (ref A 2 0)
  94. (ref A 0 1)))