summaryrefslogtreecommitdiff
path: root/chickadee/math/vector.scm
blob: b34d5a2263d2ca08f0757a7a43bb89b7daca7e24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
;;; Chickadee Game Toolkit
;;; Copyright © 2016 David Thompson <davet@gnu.org>
;;;
;;; Chickadee is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published
;;; by the Free Software Foundation, either version 3 of the License,
;;; or (at your option) any later version.
;;;
;;; Chickadee is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (chickadee math vector)
  #:use-module (rnrs bytevectors)
  #:use-module (srfi srfi-9)
  #:use-module (system foreign)
  #:use-module (chickadee math)
  #:export (vec2
            vec2/polar
            vec2?
            vec2->pointer
            copy-vec2
            copy-vec2!
            vec2-x
            vec2-y
            vec2-magnitude
            vec2-dot-product
            vec2-normalize
            set-vec2-x!
            set-vec2-y!
            vec2-normalize!
            vec2-mult!
            vec2-add!
            vec2-sub!))

(define-record-type <vec2>
  (wrap-vec2 bv pointer)
  vec2?
  (bv unwrap-vec2)
  (pointer vec2-pointer set-vec2-pointer!))

(define (vec2->pointer v)
  "Return a foreign pointer to V."
  ;; Create foreign pointer lazily.
  (or (vec2-pointer v)
      (let ((pointer (bytevector->pointer (unwrap-vec2 v))))
        (set-vec2-pointer! v pointer)
        pointer)))

(define (make-null-vec2)
  (wrap-vec2 (make-f32vector 2) #f))

(define-syntax-rule (with-new-vec2 name body ...)
  (let ((name (make-null-vec2))) body ... name))

(define-inlinable (vec2-ref v i)
  (f32vector-ref (unwrap-vec2 v) i))

(define-inlinable (vec2-set! v i x)
  (f32vector-set! (unwrap-vec2 v) i x))

(define-inlinable (vec2 x y)
  "Return a new vec2 with coordinates (X, Y)."
  (with-new-vec2 v
    (vec2-set! v 0 x)
    (vec2-set! v 1 y)))

(define-inlinable (vec2/polar r theta)
  "Return a new vec2 containing the Cartesian representation of the
polar coordinate (R, THETA)."
  (vec2 (* r (cos theta)) (* r (sin theta))))

(define (vec2-copy! source-vec2 target-vec2)
  "Copy TARGET-VEC2 to SOURCE-VEC2."
  (bytevector-copy! (unwrap-vec2 source-vec2)
                    0
                    (unwrap-vec2 target-vec2)
                    0
                    16))

(define (vec2-copy vec2)
  "Return a new vec2 that is a copy of VEC2."
  (with-new-vec2 new
    (vec2-copy! vec2 new)))

(define-inlinable (vec2-x v)
  "Return the x coordinate of the vec2 V."
  (vec2-ref v 0))

(define-inlinable (vec2-y v)
  "Return the y coordinate of the vec2 V."
  (vec2-ref v 1))

(define-inlinable (vec2-magnitude v)
  "Return the magnitude of the vec2 V."
  (sqrt (+ (square (vec2-x v)) (square (vec2-y v)))))

(define-inlinable (vec2-dot-product v1 v2)
  "Return the dot product of the vec2s V1 and V2."
  (+ (* (vec2-x v1) (vec2-x v2))
     (* (vec2-y v1) (vec2-y v2))))

(define (vec2-normalize v)
  "Return the normalized form of the vec2 V."
  (with-new-vec2 new
    (vec2-copy! v new)
    (vec2-normalize! new)))

(define-inlinable (set-vec2-x! v x)
  "Set the x coordinate of the vec2 V to X."
  (vec2-set! v 0 x))

(define-inlinable (set-vec2-y! v y)
  "Set the y coordinate of the vec2 V to Y."
  (vec2-set! v 1 y))

(define-inlinable (vec2-normalize! v)
  "Normalize the vec2 V in-place."
  (unless (and (zero? (vec2-x v)) (zero? (vec2-y v)))
    (let ((m (vec2-magnitude v)))
      (set-vec2-x! v (/ (vec2-x v) m))
      (set-vec2-y! v (/ (vec2-y v) m)))))

(define-inlinable (vec2-mult! v x)
  "Multiply the vec2 V by X, a real number or vec2."
  (if (real? x)
      (begin
        (set-vec2-x! v (* (vec2-x v) x))
        (set-vec2-y! v (* (vec2-y v) x)))
      (begin
        (set-vec2-x! v (* (vec2-x v) (vec2-x x)))
        (set-vec2-y! v (* (vec2-y v) (vec2-y x))))))

(define-inlinable (vec2-add! v x)
  "Add X, a real number or vec2, to the vec2 V."
  (if (real? x)
      (begin
        (set-vec2-x! v (+ (vec2-x v) x))
        (set-vec2-y! v (+ (vec2-y v) x)))
      (begin
        (set-vec2-x! v (+ (vec2-x v) (vec2-x x)))
        (set-vec2-y! v (+ (vec2-y v) (vec2-y x))))))

(define-inlinable (vec2-sub! v x)
  "Subtract X, a real number or vec2, from the vec2 V."
  (if (real? x)
      (begin
        (set-vec2-x! v (- (vec2-x v) x))
        (set-vec2-y! v (- (vec2-y v) x)))
      (begin
        (set-vec2-x! v (- (vec2-x v) (vec2-x x)))
        (set-vec2-y! v (- (vec2-y v) (vec2-y x))))))