File: coordinates.l

package info (click to toggle)
euslisp 9.31%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,448 kB
  • sloc: ansic: 41,610; lisp: 3,339; makefile: 286; sh: 238; asm: 138; python: 53
file content (610 lines) | stat: -rw-r--r-- 22,577 bytes parent folder | download | duplicates (2)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
;;;;
;;;; coordinates.l
;;;; classes for representing 3-dimensional coordinate system
;;;; and their hierarchical cascading.
;;;;	Copyright 1987, Toshihiro MATSUI, Electrotechnical Laboratory
;;;;
;;;; timing measurements:
;;;;			sun3/60		sun4/260	Ustation/E20
;;;;  v+		 0.3ms		0.10ms
;;;;  m*		 0.7ms		0.23ms		1.4 ms 
;;;;  transform		 0.45ms		0.15ms
;;;;  rotate-matrix	 0.54ms		0.18ms
;;;;  rotation-matrix	 0.36ms

(eval-when(load eval)
   (unless (find-package "GEOMETRY")
	(make-package "GEOMETRY" :nicknames '("GEO"))
	(in-package "GEOMETRY")
	(use-package "LISP")))

(in-package "GEOMETRY")

(export '(
	#| coordinates cascaded-coords |#	;already exported by geoclasses
	coords cascoords make-cascoords make-coords transform-coords*
	transform-coords 
	coordinates-p))
(export '(*world-coords* *world-coords2*
	 coordinates-distance almost-same-coordinates-p))
(export '(coordinates-pos coordinates-rot))

;; export slot variable names
(export '(rot pos))
(export '(parent descendants worldcoords manager changed))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; class COORDINATES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defclass coordinates :super propertied-object
  :slots
  ((rot :type array)		;current rotation matrix
   (pos	:type float-vector))	;current position float-vector #v(x y z)
  )

(defun coordinates-p (obj)
  (derivedp obj coordinates))
(defmethod coordinates
  (:dimension () (length pos))
  (:rot () rot)	;inquire rotation matrix
  (:pos () pos)	;inquire positon vector
  (:x-axis () (matrix-row rot 0))
  (:y-axis () (matrix-row rot 1))
  (:z-axis () (matrix-row rot 2))
  (:name (&optional nm)
    (if nm (setf (get self :name) nm))
    (get self :name))
;update of coords is always done through :newcoords
  (:newcoords (c &optional p)
     (if p
	 (setq rot c pos p)
	 (setq rot (coordinates-rot c)   pos (coordinates-pos c)))
      self)
  (:replace-rot (r) (replace-matrix rot r))
  (:replace-pos (p) (replace pos p))
  (:replace-coords (c &optional p)
     (when (coordinates-p c)
	(setq p (coordinates-pos c) c (coordinates-rot c)))
     (replace-matrix rot c)
     (replace pos p)
     self)
  (:copy-rot () (copy-matrix rot))
  (:copy-pos () (copy-seq pos))
  (:copy-coords (&optional (dest (instance coordinates :create (length pos))))
     (replace (coordinates-pos dest) pos)
     (replace-matrix (coordinates-rot dest) rot)
     dest)
  (:coords (&optional (dest (instance coordinates :create (length pos))))
     (replace (coordinates-pos dest) pos)
     (replace-matrix (coordinates-rot dest) rot)
     dest)
  (:worldrot () rot)
  (:worldpos () pos)
  (:worldcoords () self)
  (:copy-worldcoords () (send self :coords))
  (:parentcoords () *world-coords*)
  (:changed () nil)
  (:reset-coords   ()
   (let ((dim (array-dimension rot 0)))
     (setq rot (unit-matrix dim)
	   pos (instantiate float-vector dim))
     (send self :changed)
     self))
  (:move-to (c &optional (wrt :local) &aux cc)
     (unless (coordinates-p c) (error "coordinates expected for :move-to"))
     (cond ((or (memq wrt '(:local local)) (eq wrt self))
	    (setq cc (transform-coords self c))
	    (send self :newcoords cc))
	   ((or (memq wrt '(:parent parent :world world))
		(equal wrt *world-coords*))
	    (send self :newcoords c))
	   ((coordinates-p wrt)
	    (setq cc (transform-coords (send wrt :worldcoords) c))
	    (transform-coords (send (send self :parentcoords)
				    :inverse-transformation) cc cc)
	    (send self :newcoords cc))))
  (:rotate-vector (v) (transform rot v))
  (:transform-vector  (v)
   ;vector v given in the local coords is converted to world representation
   (v+ (transform rot v) pos))
  (:inverse-transform-vector (vec)	;vec in world coordinates->local
    (let ((inv-rot (transpose rot)))
	(v- (transform inv-rot vec) (transform inv-rot pos))))
  (:inverse-transformation
   (&optional (dest (instance coordinates :create (length pos))))
   ;make new coordinates inverse to self
     (transpose rot (coordinates-rot dest))
     (transform (coordinates-rot dest) pos (coordinates-pos dest))
     (scale -1.0 (coordinates-pos dest) (coordinates-pos dest))
     dest)
  (:transformation   (c2 &optional (wrt :local))
   (setq c2 (send c2 :worldcoords)) 
   (let* ((c1 (send self :worldcoords)) 
	  (inv (send c1 :inverse-transformation))
	  xw)
      (cond
         ((or (memq wrt '(:local local)) (eq wrt self))
	   (transform-coords inv c2 inv))
         ((or (memq wrt '(:parent parent :world world))
	      (eq wrt *world-coords*))
	   (transform-coords c2 inv inv))
         ((coordinates-p wrt)
	   (setq xw (send wrt :worldcoords))
	   (transform-coords c2 inv inv)
	   (transform-coords (send xw :inverse-transformation) inv inv)
	   (transform-coords inv xw inv))
         (t (send self :error ":transform wrt?" wrt)))
      inv))
  (:transform  (c &optional (wrt :local))
   (cond
      ((or (memq wrt '(local :local)) (eq wrt self))	;multiply c from the right
	(transform-coords self c self))
      ((or (memq wrt '(parent :parent world :world))
	   (eq wrt *world-coords*))
	(transform-coords c self self))	;;multiply c from the left
      ((coordinates-p wrt)
	(transform-coords (send wrt :inverse-transformation) self self)
	(transform-coords c self self)
	(transform-coords (send wrt :worldcoords) self self))
      (t (send self :error ":transform wrt?" wrt)))
   (send self :newcoords rot pos))
  (:rotate-with-matrix (mat wrt)	;only for internal use
    (cond ((or (memq wrt '(local :local)) (eq wrt self))
	   (m* rot mat rot))
	  ((or (memq wrt '(:parent parent :world world nil))
		(eq wrt *world-coords*))
	   (m* mat rot rot))
	  ((coordinates-p wrt)
	   (let* ((r2 (send wrt :worldrot))
		  (r2t (transpose r2))
		  p2)
	      (m* mat r2t r2t)
	      (m* r2 r2t r2t) 
	      (m* r2t rot rot)))
	  (t (send self :error ":rotate wrt?"))))
  (:rotate (theta &optional axis (wrt :local))
    (cond ((= (length pos) 2)	; 2D
	   (cond ((numberp theta)
	          (rotate-matrix rot theta nil nil rot))
		 ((matrixp theta)
		  (m* theta rot rot))
		 (t (error "illegal rotation"))))
	  ((float-vector-p axis)
	   (send self :rotate-with-matrix (rotation-matrix theta axis) wrt))
	  ((null axis)
	   (send self :rotate-with-matrix theta wrt))
	  (t (cond
		((or (memq wrt '(:local local)) (eq wrt self))
		 (rotate-matrix rot theta axis nil rot))
		((or (memq wrt '(:parent parent :world world))
		     (eq wrt *world-coords*))
		 (rotate-matrix rot theta axis t rot))
		((coordinates-p wrt)	;C1'=C2*R*C2(-1)*C1
		 (send self :rotate-with-matrix
			    (rotation-matrix theta axis) wrt))
		(t  (send self :error ":rotate wrt?" wrt)))))
    (send self :newcoords rot pos) )
  (:orient-with-matrix (mat wrt)	;only for internal use
    (cond ((or (memq wrt '(local :local)) (eq wrt self))
	   (m* rot mat rot))
	  ((or  (memq wrt '(:parent parent world :world))
		(eq wrt *world-coords*))
	   (setf rot mat))
	  ((coordinates-p wrt)
	   (let* ((r2 (send wrt :worldrot))		  )
	      (m* r2 mat rot)))
	  (t (send self :error ":orient wrt?"))))
  (:orient (theta axis &optional (wrt :local))
    (cond ((float-vector-p axis)
	   (send self :orient-with-matrix (rotation-matrix theta axis) wrt))
	  ((matrixp theta)
	   (send self :orient-with-matrix theta wrt))
	  (t (cond
		((or (memq wrt '(local :local)) (eq wrt self))
		 (rotate-matrix rot theta axis nil rot))
		((or (memq wrt '(parent :parent world :world))
		 (eq wrt *world-coords*))
		 (setf rot (rotation-matrix theta axis)))
		((coordinates-p wrt)	;C1'=C2*R*C2(-1)*C1
		 (send self :orient-with-matrix
			    (rotation-matrix theta axis) wrt))
		(t  (send self :error ":orient wrt?" wrt)))))
    (send self :newcoords rot pos) )
  (:parent-vector (v wrt)
   (cond ((or (memq wrt '(:local local)) (eq wrt self))  (send self :transform-vector v))
	 ((or (memq wrt '(:world world :parent parent nil))
	      (eq wrt *world-coords*)) v)
	 ((coordinates-p wrt)
	  (send  wrt  :transform-vector v))
	 (t (send self :error ":parent-vector wrt?" wrt))))
  (:parent-orientation (v wrt)
   (cond ((or (memq wrt '(:local local)) (eq wrt self)) (transform rot v))
	 ((or (memq wrt '(:parent parent :world world nil))
	      (eq wrt *world-coords*)) v)
	 ((coordinates-p wrt)   (transform  (send wrt :worldrot) v))
	 (t (send self :error ":parent-orientation wrt? wrt must be :world, :local, :parent or coordinates instance" wrt))))
  (:translate (vec &optional (wrt :local))
     (send self :newcoords
	   rot (v+ (send self :parent-orientation vec wrt) pos pos)))
  (:locate (vec &optional (wrt :local))
     (send self :newcoords
		rot (replace pos (send self :parent-vector vec wrt))))
  (:scale (&optional s)
     (if s
	 (progn (scale-matrix s rot rot) (send self :newcoords self))
	 (norm (matrix-row rot 0))))
  (:euler (azimuth elevation rotation)	;be carefull with the angle order
	(send self :newcoords
		   (euler-matrix azimuth elevation rotation) pos))
  (:euler-angle () (euler-angle rot))
  (:rpy (r p y)
	(send self :newcoords
		   (rpy-matrix r p y) pos))
  (:rpy-angle () (rpy-angle rot))
  (:rotation-angle () (rotation-angle rot))
  (:4X4 (&optional mat44)
    (cond (mat44	;convert-from-4x4
	     (dotimes (i 3)
		(dotimes (j 3)
		   (setf (aref rot i j) (aref mat44 i j)))
	        (setf (aref pos i) (aref mat44 i 3)))
	     self)
	  (t	;makes 4x4 matrix representation
	    (setq mat44 (make-matrix 4 4))	;all zero elements
	    (setf (aref mat44 3 3) 1.0)
	    (dotimes (i 3)
		(dotimes (j 3)
		   (setf (aref mat44 i j) (aref rot i j)))
		(setf (aref mat44 i 3) (aref pos i)))
	    mat44)) )
 (:prin1 (&optional (strm t) &rest more-args
		&aux (rpy (send self :worldrot))
		     (p (send self :worldpos)))
     (cond ((> (length p) 2)
	    (setq rpy (car (rpy-angle rpy)))
	    (send-super :prin1 strm 
	     (format nil "~A ~1,3G ~1,3g ~1,3g / ~1,3g ~1,3g ~1,3g"
		   (if more-args (car more-args) "")
		   (aref p 0) (aref p 1) (aref p 2)
		   (first rpy) (second rpy) (third rpy))) )
	   ((= (length p) 2)
	    (send-super :prin1 strm 
	     (format nil "~A ~1,3g ~1,3g / ~1,3g"
		   (if more-args (car more-args) "")
		   (aref p 0) (aref p 1)   (rotation-angle rpy))))
	   (t (send-super :prin1 strm more-args))))
  (:create (&optional (dimension 3))
     (setq rot (unit-matrix dimension)
	   pos (instantiate float-vector dimension))
     self)
  (:init  (&key  (dimension 3)
		 ((:pos p) (instantiate float-vector dimension))
		 ((:rot r) (unit-matrix dimension))
		 (euler nil)
		 (rpy   nil)
		 (axis  nil)
		 (angle nil)
		 (at nil)
		 (coords at)
		 (4X4 nil)
		 (wrt :local)
		 ((:name nm))
		 (properties nil)
		 &allow-other-keys)
        (setq rot r)
	(setq pos 
	      (if (coordinates-p wrt)
		  (send wrt :transform-vector p)
		  p))
	(cond   (euler (send self :euler (elt euler 0) (elt euler 1) (elt euler 2)))
		(rpy (send self :rpy (elt rpy 0) (elt rpy 1) (elt rpy 2)))
		(coords  (send self :replace-coords coords))
		((consp angle)
		 (dolist (a angle)  (send self :rotate a (pop axis) wrt)))
		((numberp angle)
		 (send self :rotate angle axis wrt))
		(4X4 (send self :4X4 4X4)))
	(if nm (send self :name nm))
	(dolist (p properties)  (setf (get self (car p)) (cadr p)))
      self))	;coordinates

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; class CASCADED-COORDS
; Child coords inherits parent coords, so child coords is
; determined with respect to the parent.
; Only one parent is allowed for a child.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defclass cascaded-coords 
	  :super coordinates
	  :slots  (parent descendants worldcoords manager changed))


(defmethod cascaded-coords 
 (:parent () parent)
 (:descendants () descendants)
 (:inheritance ()
    (mapcar
	#'(lambda (child) (cons child (send child :inheritance)))
	descendants))
 (:leaves ()
    (if descendants 
	(flatten (send-all descendants :leaves))
	self))
 (:assoc (child &optional c)
    (unless (memq child descendants) 	;already assoc'ed?
	(unless c 
		(setq c  (send (send self :worldcoords) :transformation
			       (send child :worldcoords))))
	(send child :obey self)
	(send child :newcoords
		    (coordinates-rot c) (coordinates-pos c))
	(push child descendants)
	child))
 (:dissoc (child)
    (when (memq child descendants)
	(let ((c (send (send child :worldcoords) :copy-coords)))
	   (setq descendants (delete child descendants))
	   (send child :disobey self)
	   (send child :newcoords (coordinates-rot c) (coordinates-pos c))
	   child)))
 (:clear-assoc ()	;remove all the association
    (dolist (d descendants) (send self :dissoc d))
    nil)
 (:obey (mother)	;only for internal use
    (if parent (send parent :dissoc self))
    (setf parent mother))
 (:disobey (mother)	;only for internal use
    (if (eq mother parent) (setq parent nil))
    parent)
 (:newcoords  (c &optional p)
    (send-super :newcoords c p)
    (send self :changed)
    self)
 (:changed ()
    (unless changed		;Is this the first change?
	(setf changed t)	;Then, propagate changed signal.
	(send-all descendants :changed)))
 (:worldrot () (coordinates-rot (send self :worldcoords)))
 (:worldpos () (coordinates-pos (send self :worldcoords)))
 (:worldcoords  ()	;calculate rot and pos in the world
   (when changed
      (if parent
	  (transform-coords (send parent :worldcoords) self worldcoords)
	  (send worldcoords :replace-coords self))
      (send self :update)
      (setf changed nil))
   worldcoords)
 (:copy-worldcoords (&optional
			(dest (instance coordinates :create (length pos))))
     (send self :worldcoords)
     (replace (coordinates-pos dest) (coordinates-pos worldcoords))
     (replace-matrix (coordinates-rot dest) (coordinates-rot worldcoords))
     dest)
 (:update () )
 (:parentcoords ()
    (if parent
        (send parent :worldcoords)
	(case (send self :dimension)
	  (3 *world-coords*)
	  (2 *world-coords2*)
	  (t (error "dimension?"))) ))
 (:transform-vector (v)
   (send (send self :worldcoords) :transform-vector v))
 (:rotate-vector (v) 
   (send (send self :worldcoords) :rotate-vector v))
 (:inverse-transform-vector  (v)
  (send (send self :worldcoords) :inverse-transform-vector v))
 (:inverse-transformation  (&optional (dest (instance coordinates :create))) 
  (send (send self :worldcoords) :inverse-transformation dest))
 (:transformation (c2 &optional (wrt :local))
   (let* ((w2 (send c2 :worldcoords))
	  (w1 (send self :worldcoords))
	  (w1inv (send w1 :inverse-transformation))
	  (c1inv) (xw))
      (cond
        ((or (memq wrt '(:local local)) (eq wrt self))
	 (transform-coords w1inv w2 w1inv))
	((or (memq wrt '(:parent parent)) (eq wrt parent))	;nil?
	 (setq c1inv (send-super :inverse-transformation))
	 (transform-coords w2 c1inv c1inv)
	 (transform-coords w1inv c1inv c1inv)
	 (transform-coords self c1inv c1inv))
	((or (memq wrt '(world :world))  (equal wrt *world-coords*))
	 (transform-coords w2 w1inv w1inv))
        ((coordinates-p wrt)
	 (setq xw (send wrt :worldcoords))
	 (transform-coords w1inv xw w1inv)
	 (transform-coords w2 w1inv w1inv)
	 (transform-coords (send xw :inverse-transformation) w1inv w1inv))
      (t (send self :error ":transform wrt?" wrt)))))
 (:transform  (c &optional (wrt :local))
   (cond
    ((or (memq wrt '(:local local)) (eq wrt self))	;multiply c from the right
	(transform-coords self c self))
    ((or (memq wrt '(:parent parent)) (eq wrt parent))	;nil?
	(transform-coords c self self))	;;multiply c from the left
    ((or (memq wrt '(world :world))  (equal wrt *world-coords*))
	(let ((pc (send self :parentcoords)))
	   (transform-coords pc self self)
	   (transform-coords c self self)
	   (transform-coords (send pc :inverse-transformation) self self)))
    ((coordinates-p wrt)
	(let ((pc (send self :parentcoords)))
	   (transform-coords pc self self)
	   (transform-coords (send wrt :inverse-transformation) self self)
	   (transform-coords c self self)
	   (transform-coords (send wrt :worldcoords) self self)
	   (transform-coords (send pc :inverse-transformation) self self)))
    (t (send self :error ":transform wrt?" wrt)))
   (send self :newcoords rot pos))
 (:move-to (c &optional (wrt :local) &aux cc)
     (unless (coordinates-p c) (error "coordinates expected for :move-to"))
     (cond ((or (memq wrt '(:local local)) (eq wrt self))
	    (setq cc (transform-coords self c))
	    (send self :newcoords cc))
	   ((or (memq wrt '(:parent parent)) (eq wrt parent))
	    (send self :newcoords c))
	   ((or (memq wrt '(:world world)) (equal wrt *world-coords*))
	    (setq cc (transform-coords
			(send (send self :parentcoords)
				:inverse-transformation) c))
	    (send self :newcoords cc))
	   ((coordinates-p wrt)
	    (setq cc (transform-coords (send wrt :worldcoords) c))
	    (transform-coords (send (send self :parentcoords)
				    :inverse-transformation) cc cc)
	    (send self :newcoords cc))))
 (:rotate-with-matrix (mat wrt)   ;only for internal use
    (cond ((or (memq wrt '(:local local)) (eq wrt self))
	   (m* rot mat rot)
	   (send self :newcoords rot pos))
	  ((or (memq wrt '(:parent parent)) (eq wrt parent))
	   (m* mat rot rot)
	   (send self :newcoords rot pos))
	  (t
	      (let* ((wrtrot)
		     (pc (send self :parentcoords))
		     (pr (coordinates-rot pc)))
		(when (coordinates-p wrt)
		    (setq wrtrot (send wrt :worldrot))
		    (setq mat (m* wrtrot mat))
		    (m* mat (transpose wrtrot) mat))
	        (m* mat pr mat)
	        (m* (transpose pr) mat mat) 
	        (m* mat rot rot)
		(send self :newcoords rot pos)))))
  (:rotate (theta axis &optional (wrt :local))
    (cond ((= (length pos) 2)
	   (send-super :rotate theta axis))
    	  ((float-vector-p axis)
	   (send self :rotate-with-matrix (rotation-matrix theta axis) wrt))
	  ((matrixp theta)
	   (send self :rotate-with-matrix theta wrt))
	  (t (cond	;axis is given as an axis-symbol like :x, :y and :z
		((or (memq wrt '(local :local)) (eq wrt self))
		 (rotate-matrix rot theta axis nil rot)
		 (send self :newcoords rot pos))
		((or (memq wrt '(parent :parent)) (eq wrt parent))
		 (rotate-matrix rot theta axis t rot)
		 (send self :newcoords rot pos))
		(t
		 (send self :rotate-with-matrix
			    (rotation-matrix theta axis) wrt))))))
 (:orient-with-matrix (mat wrt)   ;only for internal use
    (cond ((or (memq wrt '(:local local)) (eq wrt self))
	   (m* rot mat rot)
	   (send self :newcoords rot pos))
	  ((or (memq wrt '(parent :parent)) (eq wrt parent))
	   (setf rot mat)
	   (send self :newcoords rot pos))
	  (t
	      (let* ((wrtrot)
		     (pc (send self :parentcoords))
		     (pr (coordinates-rot pc)))
		(when (coordinates-p wrt)
		      (setf wrtrot  (send wrt :worldrot))
		      (setf mat (m* wrtrot mat)))
	        (m* (transpose pr) mat rot) 
		(send self :newcoords rot pos)))))
  (:orient (theta axis &optional (wrt :local))
    (cond ((float-vector-p axis)
	   (send self :orient-with-matrix (rotation-matrix theta axis) wrt))
	  ((matrixp theta)
	   (send self :orient-with-matrix theta wrt))
	  (t (cond	;axis is given as an axis-symbol like :x, :y and :z
		((or (memq wrt '(:local local)) (eq wrt self))
		 (rotate-matrix rot theta axis nil rot)
		 (send self :newcoords rot pos))
		((or (memq wrt '(parent :parent)) (eq wrt parent))
		 (setf rot (rotation-matrix theta axis))
		 (send self :newcoords rot pos))
		(t
		 (send self :orient-with-matrix
			    (rotation-matrix theta axis) wrt))))))
 (:parent-vector (v wrt)
   (cond ((or (memq wrt '(:local local)) (eq wrt self)) (send-super :transform-vector v))
	 ((or (memq wrt '(:parent parent)) (eq wrt parent)) v)
	 (t
	  (if (coordinates-p wrt)  (setq v (send wrt :transform-vector v)))
	  (if parent
	      (send parent :inverse-transform-vector v)
	      v))))
 (:parent-orientation (v wrt)
   (cond ((or (memq wrt '(:local local)) (eq wrt self)) (transform rot v))
	 ((or (memq wrt '(:parent parent)) (eq wrt parent)) v)
	 (t
	  (if (coordinates-p wrt)
	      (setq v (transform (send wrt :worldrot) v)))
	  (if parent
	      (transform (transpose (send parent :worldrot)) v)
	      v))))
 (:manager (&optional m)
    (if m (setq manager  m))
    manager)
 (:init (&rest initargs &key ((:parent par)) at &allow-other-keys)
    (send-super* :init initargs)
    (setf manager self
	  changed t	;safer
	  worldcoords	;prepare a world-coordinates holder
          ;; avoid to use copy-object, see https://sourceforge.net/p/jskeus/tickets/28/
	  (instance coordinates :init :rot (copy-matrix rot) ; (copy-object rot)
				      :pos (copy-seq pos)    ; (copy-object pos)
                                      ))
    (if par (send par :assoc self at)) 
    self)
)
;;; Cancatenation of coordinates
;;; C2 is transformed by c1 from the left and the result is stored
;;; in the optional third argument.
;;; Efficiency effective.

(defun transform-coords (c1 c2 &optional
			       (c3 (let ((dim (send c1 :dimension)))
				      (instance coordinates 
					   :newcoords (unit-matrix dim)
						(instantiate float-vector dim)))))
   (if (eq c1 c3)
       (v+ (coordinates-pos c1)
           (transform (coordinates-rot c1) (coordinates-pos c2))
           (coordinates-pos c3))
       (v+ (coordinates-pos c1)
           (transform (coordinates-rot c1) (coordinates-pos c2) (coordinates-pos c3))
           (coordinates-pos c3)))
   (m* (coordinates-rot c1) (coordinates-rot c2) (coordinates-rot c3))
   c3)

(defun transform-coords* (c1 c2 &rest clist &aux cresult)
   (setq cresult (transform-coords c1 c2))
   (dolist (c clist)
        (transform-coords cresult c cresult))
   cresult)

(defun transform-vector (trans vec)
   (declare (coordinates trans))
   (v+ (transform (coordinates-rot trans) vec) (coordinates-pos trans)))

(defun make-coords (&rest initargs)
    (send* (instantiate coordinates) :init initargs))
(defun make-cascoords (&rest initargs)
    (send* (instantiate cascaded-coords) :init initargs))
(defun coords (&rest initargs)
    (send* (instantiate coordinates) :init initargs))
;(defun trans (&rest initargs)
;    (send* (instantiate coordinates) :init initargs))
(defun cascoords (&rest initargs)
    (send* (instantiate cascaded-coords) :init initargs))
(defun wrt (coords vec)
    (send coords :transform-vector vec))
(eval-when (load eval)
  (defconstant *world-coords*
	 (instance coordinates :init :name '*world-coords*))
  (defconstant *world-coords2*
	 (instance coordinates :init :dimension 2 :name '*world-coords2*))
)
(defun coordinates-distance (c1 c2 &aux (c (send c1 :transformation c2)))
     (list (norm (send c :worldpos))
	   (car (rotation-angle (send c :worldrot)))))

(provide :coordinates 
	"@(#)$Id$")