-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.lisp
255 lines (191 loc) · 7.22 KB
/
geometry.lisp
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
;;;; -*- Mode: Lisp -*-
;;;;
;;;; Copyright (c) 2008-2009 Arturs Grebstelis
;;;;
(in-package #:abop)
(cffi:define-foreign-library machine
(:windows "./machine.dll")
(:unix "./machine.so"))
(cffi:use-foreign-library machine)
(cffi:defcfun ("cseed" cseed) :void
(seed :unsigned-int))
(cffi:defcfun ("crandom" crandom) :int
(seed :unsigned-int))
;; --- point ------------------------------------------------------------------
(defconstant +x+ 0)
(defconstant +y+ 1)
(defconstant +z+ 2)
(defconstant +dimensions+ 3)
(def-exported-struct (point (:type vector))
(x 0.0s0 :type single-float)
(y 0.0s0 :type single-float)
(z 0.0s0 :type single-float)
(size -1.0s0 :type single-float))
(export 'create-point)
(defun create-point (x y z)
(make-point :x (sfloat x) :y (sfloat y) :z (sfloat z)))
(defun point-index (point i)
(svref point i))
(declaim (inline setf-point-index))
(defun setf-point-index (point i element)
(setf (svref point i) element))
(defsetf point-index setf-point-index)
; --- point data abstraction ---
(defun point-addf (a b)
(incf (point-x a) (point-x b))
(incf (point-y a) (point-y b))
(incf (point-z a) (point-z b))
(setf (point-size a) -1.0s0)
a)
(defun point-eq (a b)
(and (float-equal (point-x a) (point-x b))
(float-equal (point-y a) (point-y b))
(float-equal (point-z a) (point-z b))))
(export 'point-add)
(defun point-add (a b)
(point-addf (copy-point a) b))
(export 'point-scale)
(defun point-scale (p q)
(create-point (* q (point-x p)) (* q (point-y p)) (* q (point-z p))))
(defun point-sum (point)
(+ (point-x point) (point-y point) (point-z point)))
(defun calculate-point-length (p)
(sqrt (+ (expt (point-x p) 2) (expt (point-y p) 2) (expt (point-z p) 2))))
(defun point-length (p)
(if (minusp (point-size p))
(setf (point-size p) (calculate-point-length p))
(point-size p)))
(export 'point-invert)
(defun point-invert (p)
(create-point (- (point-x p)) (- (point-y p)) (- (point-z p))))
(defun point-sub (a b)
(create-point
(- (point-x a) (point-x b))
(- (point-y a) (point-y b))
(- (point-z a) (point-z b))))
(defun point-normalize (p &optional (size 1.0))
(point-scale p (/ size (point-length p))))
(export 'point-write)
(defun point-write (p &optional nz?)
(format nil "~6F ~6F ~:[~6F~;~*~]" (point-x p) (point-y p) nz? (point-z p)))
(export 'point-inv-x-y)
(defun point-inv-x-y (p)
(create-point (point-y p) (point-x p) (point-z p)))
(defun point-read (stream &optional ignore-z?)
(create-point (read stream) (read stream) (if ignore-z? 0.0 (read stream))))
(defun point-distance (a b)
(point-length (point-sub a b)))
(defun null-point ()
(create-point 0.0 0.0 0.0))
(defparameter +null-point+ (null-point))
(cffi:defcfun ("rotate_vector" rotate_vector) :void
(vector :pointer)
(axis :pointer)
(angle :float))
(defun point-import (array point)
(dotimes (i +dimensions+)
(setf (point-index point i) (cffi:mem-aref array :float i)))
point)
(defun point-export (point array)
(dotimes (i +dimensions+)
(setf (cffi:mem-aref array :float i) (point-index point i))))
(defmacro with-foreign-point ((ptr var) &rest body)
`(cffi:with-foreign-object (,ptr :float +dimensions+)
(point-export ,var ,ptr)
,@body))
(defun rotate-vector (vector axis angle)
(with-foreign-point (foreign-vector vector)
(with-foreign-point (foreign-axis axis)
(rotate_vector foreign-vector foreign-axis (sfloat angle))
(point-import foreign-vector vector))))
(defun cross-product (a b)
(create-point
(- (* (point-y a) (point-z b)) (* (point-z a) (point-y b)))
(- (* (point-z a) (point-x b)) (* (point-x a) (point-z b)))
(- (* (point-x a) (point-y b)) (* (point-y a) (point-x b)))))
(defun dot-product (a b)
(+ (* (point-x a) (point-x b))
(* (point-y a) (point-y b))
(* (point-z a) (point-z b))))
(defun get-angle-cos (a b)
(/ (dot-product a b) (* (point-length a) (point-length b))))
(defun safe-acos (val)
(cond ((= +1.0 val) 0.0)
((= -1.0 val) pi)
(t (acos val))))
(defun get-angle (a b)
(let ((result (safe-acos (get-angle-cos a b))))
(if (complexp result)
(realpart result)
result)))
;; --- space ------------------------------------------------------------------
(defconstant +space-elements+ (expt +dimensions+ 2))
(defstruct (vector-space (:type vector)) x y z)
(defun space-element (space index)
(multiple-value-bind (axis-index dimension-index) (floor index +dimensions+)
(point-index (elt space axis-index) dimension-index)))
(defparameter +x-axis+ (create-point 1.0 0.0 0.0))
(defparameter +y-axis+ (create-point 0.0 1.0 0.0))
(defparameter +z-axis+ (create-point 0.0 0.0 1.0))
(defun unit-space ()
(make-vector-space
:x (copy-point +x-axis+)
:y (copy-point +y-axis+)
:z (copy-point +z-axis+)))
(defun turn-space (space torque angle)
(flet ((rotate-axis (axis) (rotate-vector (copy-point axis) torque angle)))
(map 'vector #'rotate-axis space)))
(cffi:defcfun ("rotate_space" rotate_space) :void
(vector :pointer)
(space :pointer))
(defun export-space (space array)
(dotimes (i +space-elements+)
(setf (cffi:mem-aref array :float i) (space-element space i))))
(export 'rotate-space)
(defun rotate-space (vector space)
(with-foreign-point (foreign-vector vector)
(cffi:with-foreign-object (foreign-space :float +space-elements+)
(export-space space foreign-space)
(rotate_space foreign-vector foreign-space)
(point-import foreign-vector (null-point)))))
(defun normal-cross-product (vector1 vector2)
(point-normalize (cross-product vector1 vector2)))
(defun align-x-axis-with-horizon (space)
(let ((space-y (vector-space-y space)))
(when (< single-float-epsilon (point-distance +y-axis+ space-y))
(setf (vector-space-x space)
(normal-cross-product +y-axis+ space-y))
(setf (vector-space-z space)
(normal-cross-product (vector-space-x space) space-y)))))
;; --- plane ------------------------------------------------------------------
; plane equation A * x + B * y + C * z + D = 0
; (A, B, C) = normal, D = distance
(defstruct plane normal distance)
(defun create-plane (point normal)
(make-plane :normal normal :distance (- (dot-product point normal))))
(defun distance-factor (p v plane)
"distance from point 'p' to plane mesured in 'v' vectors"
(/ (- (+ (dot-product (plane-normal plane) p) (plane-distance plane)))
(dot-product (plane-normal plane) v)))
(defun plane-intersection (p v plane)
"intersection of plane and line formed by point 'p' and vector 'v'"
(point-add p (point-scale v (distance-factor p v plane))))
;; --- color ------------------------------------------------------------------
(declaim (inline create-color))
(def-exported-struct color
(r 0.0s0 :type single-float)
(g 0.0s0 :type single-float)
(b 0.0s0 :type single-float)
(a 0.0s0 :type single-float))
(defun create-color (r g b &optional (a 1.0))
(make-color :r (sfloat r) :g (sfloat g) :b (sfloat b) :a (sfloat a)))
(export 'color-scale)
(defun color-scale (color q)
(create-color
(* q (color-r color))
(* q (color-g color))
(* q (color-b color))
(color-a color)))
(defparameter +white+ (create-color 1.0 1.0 1.0))
(defparameter +black+ (create-color 0.0 0.0 0.0))
(defparameter +red+ (create-color 1.0 0.0 0.0))