File: linraphs.l

package info (click to toggle)
euslisp 9.32%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,268 kB
  • sloc: ansic: 41,693; lisp: 3,339; makefile: 286; sh: 238; asm: 138; python: 53
file content (427 lines) | stat: -rw-r--r-- 9,798 bytes parent folder | download | duplicates (3)
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
;;;; linraphs.l
;;;  fitting process with LINearized newton-RAPHSon method.
;;;  H1.1.25  *MeR*
;;;
(eval-when (load)
	   (format t "Loading linraphs.o~%"))
(eval-when (eval)
	   (format t "Loading linraphs.l~%"))
(eval-when
 (compile)
 (defclass point2point-calib-sample
   :super camera-calib-sample
   :slots nil)
 (defclass line2line-calib-sample
   :super camera-calib-sample
   :slots (dudv dxdydz))
 (defclass segment2segment-calib-sample 
   :super camera-calib-sample
   :slots (otheruv otherxyz))
 )

(defclass camera-calib-coord-raphson
  :super camera-calib-coord
  :slots (many-samples
	  th-ps
	  3th-ps3
	  invertpos
	  previous-err
	  previous-th-wo
	  now-err
	  last-orient
	  ))

(defmethod point2point-calib-sample
  (:ccam-lineareq-vector-element-list
   (mat-i-wo uv0 rk)
   (let* ((iro
	   (v. (homo xyz) (matrix-row mat-i-wo 0)))
	  (niho
	   (v. (homo xyz) (matrix-row mat-i-wo 1)))
	  (toti
	   (v. (homo xyz) (matrix-row mat-i-wo 2)))
	  )
     (v+ (scale iro
		(v- uv0 uv))
	 (float-vector
	  (* (vector-x rk) niho)
	  (* (vector-y rk) toti)))
     ))
  (:ccam-lineareq-matrix-element-list
   (mat-i-wo uv0 rk)
   (let* ((u (vector-x uv))
	  (v (vector-y uv))
	  (u0 (vector-x uv0))
	  (v0 (vector-y uv0))
	  (k1 (vector-x rk))
	  (k2 (vector-y rk))
	  (vec-iroha
	   (float-vector 
	    (aref mat-i-wo 0 0)
	    (aref mat-i-wo 0 1)
	    (aref mat-i-wo 0 2)))
	  (vec-nihohe
	   (float-vector
	    (aref mat-i-wo 1 0)
	    (aref mat-i-wo 1 1)
	    (aref mat-i-wo 1 2)))
	  (vec-totiri
	   (float-vector
	    (aref mat-i-wo 2 0)
	    (aref mat-i-wo 2 1)
	    (aref mat-i-wo 2 2)))
	  )
     (list
      (v-
       (scale
	(- u u0)
	(concatenate
	 float-vector
	 (v* vec-iroha xyz)
	 (float-vector 1 0 0)
	 ))
       (scale
	k1
	(concatenate
	 float-vector
	 (v* vec-nihohe xyz)
	 (float-vector 0 1 0)
	 )))
      (v-
       (scale
	(- v v0)
	(concatenate
	 float-vector
	 (v* vec-iroha xyz)
	 (float-vector 1 0 0)
	 ))
       (scale
	k2
	(concatenate
	 float-vector
	 (v* vec-totiri xyz)
	 (float-vector 0 0 1)
	 )))      
      )))
  )
(defmethod segment2segment-calib-sample
  (:ccam-lineareq-vector-element-list
   (mat-i-wo uv0 rk)
   (let* ((dudv (v- otheruv uv))
	  (u (vector-x uv))
	  (v (vector-y uv))
	  (du (vector-x dudv))
	  (dv (vector-y dudv))
	  (k1 (vector-x rk))
	  (k2 (vector-y rk))
	  (duvdvu
	   (- (* du (- v (vector-y uv0)))
	      (* dv (- u (vector-x uv0)))))
	  (homo1 (homo xyz))
	  (homo2 (homo otherxyz))
	  (i-to1 (transform mat-i-wo homo1))
	  (i-to2 (transform mat-i-wo homo2))	  
	  (iro1
	   (vector-x i-to1))
	  (niho1
	   (vector-y i-to1))
	  (toti1
	   (vector-z i-to1))
	  (iro2
	   (vector-x i-to2))
	  (niho2
	   (vector-y i-to2))
	  (toti2
	   (vector-z i-to2))	  
	  (vv1
	   (+ (* dv k1 niho1)
	      (- (* du k2 toti1))
	      (* duvdvu iro1)))
	  (vv2
	   (+ (* dv k1 niho2)
	      (- (* du k2 toti2))
	      (* duvdvu iro2))))     
     (float-vector (- vv1)  (- vv2)) ))
  (:ccam-lineareq-matrix-element-list
   (mat-i-wo uv0 rk)
   (let* ((k1 (vector-x rk))
	  (k2 (vector-y rk))
	  (u (vector-x uv))
	  (v (vector-y uv))
	  (dudv (v- otheruv uv))
	  (du (vector-x dudv))
	  (dv (vector-y dudv))
	  (duvdvu
	   (- (* du (- v (vector-y uv0)))
	      (* dv (- u (vector-x uv0)))))	  
	  (vec-iroha
	   (float-vector 
	    (aref mat-i-wo 0 0)
	    (aref mat-i-wo 0 1)
	    (aref mat-i-wo 0 2)))
	  (vec-nihohe
	   (float-vector
	    (aref mat-i-wo 1 0)
	    (aref mat-i-wo 1 1)
	    (aref mat-i-wo 1 2)))
	  (vec-totiri
	   (float-vector
	    (aref mat-i-wo 2 0)
	    (aref mat-i-wo 2 1)
	    (aref mat-i-wo 2 2)))	  
	  (vectorcel1
	   (v* vec-iroha xyz))
	  (vectorcel2
	   (v* vec-nihohe xyz))
	  (vectorcel3
	   (v* vec-totiri xyz))
	  (vec100 (float-vector 1 0 0))
	  (vec010 (float-vector 0 1 0))
	  (vec001 (float-vector 0 0 1))
	  (vec-a1
	   (concatenate float-vector 
			vectorcel1 vec100))
	  (vec-b1
	   (concatenate float-vector
			vectorcel2 vec010))
	  (vec-c1
	   (concatenate float-vector
			vectorcel3 vec001))
	  vec-a2 vec-b2 vec-c2)
     (v* vec-iroha otherxyz vectorcel1)
     (v* vec-nihohe otherxyz vectorcel2)     
     (v* vec-totiri otherxyz vectorcel3)
     (setq vec-a2
	   (concatenate float-vector
			vectorcel1 vec100))
     (setq vec-b2
	   (concatenate float-vector
			vectorcel2 vec010))
     (setq vec-c2
	   (concatenate float-vector
			vectorcel3 vec001))
     (list
      (v+ 
       (v-
	(scale (* dv k1) vec-b1 vec-b1)
	(scale (* du k2) vec-c1 vec-c1)
	vec-b1
	)
       (scale duvdvu vec-a1 vec-a1)
       vec-a1
       )
      (v+ 
       (v-
	(scale (* dv k1) vec-b2 vec-b2)
	(scale (* du k2) vec-c2 vec-c2)
	vec-b2
	)
       (scale duvdvu vec-a2 vec-a2)
       vec-a2
       ))))
  )

(defmethod camera-calib-coord-raphson
  (:init (file) ;identical to "raphson.l"
	 (send-super :init :file file)
	 (setq many-samples samplist)
	 ;; (setq uv0 (float-vector 100 100))
	 ;; (setq rk (float-vector 100 -100))
	 ;; (send self :set-th-wo (float-vector 0 0 0  100 0 0))
	 self)
  (:set-th-wo  ;identical to "raphson.l"
   (f)
   (setq th-ps (float-vector (elt f 0)
			     (elt f 1)
			     (elt f 2)))
   (setq invertpos (float-vector (elt f 3)
				(elt f 4)
				(elt f 5)))
   (let* ((th (elt th-ps 0))
	  (fi (elt th-ps 1))
	  (ps (elt th-ps 2))
	  (sth (sin th))
	  (cth (cos th))
	  (sfi (sin fi))
	  (cfi (cos fi))
	  (sps (sin ps))
	  (cps (cos ps)))
     (setq 3th-ps3
	   (float-vector sth cth sfi cfi sps cps))
     )
   (list th-ps invertpos))  
  (:cal-camera-param  ;identical to "raphson.l"
   () (send-super :cal-camera-param)
   (send self :th-wo-frp))  
  (:th-wo-frp ;identical to "raphson.l"
   ()
   (let* ((he (aref rot 2 1))
	  (ni (aref rot 0 1))
	  (ho (aref rot 1 1))
	  (ha (aref rot 2 0))
	  (ri (aref rot 2 2))
	  (fi (asin-mer he))
	  (cfi (cos fi))
	  (sth (- (/ ni cfi)))
	  (cth (/ ho cfi))
	  (th
	   (cond ((< sth 0.0)
		  (- (acos-mer cth)))
		 (t
		  (acos-mer cth))))
	  (sps (- (/ ha cfi)))
	  (cps (/ ri cfi))
	  (ps
	   (cond ((< sps 0.0)
		  (- (acos-mer cps)))
		 (t
		  (acos-mer cps))))
	  (nu-wo
	   (scale -1.0
		  (transform
		   (transpose rot)
		   pos))))
     (send self :set-th-wo
	   (concatenate float-vector (float-vector th fi ps) nu-wo))
     ))  
  (:setup-by-other-camera  ; identical to "raphson.l"
   (cam)
   (send-super :setup-by-other-camera cam)
   (send self :th-wo-frp)
   )  
  (:make-mat-i-wo
   ()
   (make-matrix-from-cvector
    (matrix-row rot 0)
    (matrix-row rot 1)    
    (matrix-row rot 2)
    invertpos))
  (:raphson-primitive
   ()
   (let* ((mat-i-wo (send self :make-mat-i-wo))
   	  (matrix-work (send self :ccam-lineareq-matrix mat-i-wo))
	  (vector-work (send self :ccam-lineareq-vector mat-i-wo))
	  (as (m* matrix-work (transpose matrix-work)))
	  (bs (transform matrix-work vector-work))
	  (x (simultaneous-equation as bs))
	  (o1 (aref x 0))
	  (o2 (aref x 1))
	  (o3 (aref x 2))
	  (dnu (aref x 3))
	  (dru (elt x 4))
	  (dwo (aref x 5))
	  dr)
     (v+ invertpos (float-vector dnu dru dwo) invertpos)
     (setq dr
	   (make-matrix-from-rvector 
	    (float-vector 1 (- o3) o2)
	    (float-vector o3 1 (- o1))
	    (float-vector (- o2) o1 1)))
     (format t "linear dx~s ~%" x)
     (authorized-normalize (m* dr rot rot))
     ))
  (:raphson
   ()
   (send self :raphson-primitive)
   (scale -1.0 (transform rot invertpos pos) pos)
   (send self :th-wo-frp)
   
   (format t "linear ..> sqrt err ~s~%" 
	   (send self :now-err-frp)))
  (:ccam-lineareq-matrix
   (mat-i-wo)
   (apply #'make-matrix-from-cvector 
	  (apply #'append
		 (mapcar
		  #'(lambda (s)
		      (send s :ccam-lineareq-matrix-element-list
			    mat-i-wo
			    uv0 rk))
		  many-samples))))
  (:ccam-lineareq-vector
   (mat-i-wo)
   (apply #'concatenate
	  float-vector
	  (mapcar
	   #'(lambda (s)
	       (send s :ccam-lineareq-vector-element-list
		     mat-i-wo
		     uv0 rk)) many-samples))
   )
  (:now-err-frp
   ()
   (let* ((sq-err 0.0))
     (dolist (x many-samples)
	     (setq sq-err (+ sq-err (send x :ccam-err-primitive
					 (send self :make-mat-i-wo)
					 uv0 rk)))
	     )
     (setq now-err sq-err)))
  )

(defun authorized-normalize (r)
  (let* ((r1 (matrix-row r 0))
	 (r2 (matrix-row r 1))
	 (r3 (normalize-vector (v* r1 r2))))
    (v* r3 (normalize-vector r1) r2)
    (v* r2 r3 r1)
    (replace-matrix r (make-matrix-from-rvector r1 r2 r3))))
;;(defun authorized-normalize (r) r)

(eval-when (load eval)
	   (format t "linraphs.l read completed. ~%")
	   )

;;;
;;;  methods appended by miya
;;;

(defmethod point2point-calib-sample
  (:err-allowance
   ()
   4.0)
  )

(defmethod segment2segment-calib-sample
  (:err-allowance
   ()
   8.0)
  )

(defmethod camera-calib-coord-raphson
  (:converge
   ()
   (let ((error-allowance 0.0)
	 (backup-rot (copy-object rot))
	 (backup-invertpos (copy-object invertpos)))
     (dolist (sample many-samples)
	     (inc error-allowance (send sample :err-allowance)))
     (setq now-err (send self :now-err-frp))
     (loop
      (replace-matrix backup-rot rot)
      (replace backup-invertpos invertpos)
      (setq previous-err now-err)
      (send self :raphson-primitive)
      (setq now-err (send self :now-err-frp))
      (format t "error is ~s~%" now-err)
      (when (>= now-err previous-err) (return)))
     (replace-matrix rot backup-rot)
     (replace invertpos backup-invertpos)
     (setq now-err previous-err)
     (scale -1.0 (transform rot invertpos pos) pos)
     (send self :th-wo-frp)
     (send self :reconstruct-transform34)
     (if (< now-err error-allowance)
	 (progn
	   (format t "Converged. err/allowance: ~s/~s ~%"
		   now-err error-allowance)
	   t)
       (progn
	 (format t "Could't converge well. Sorry. err/allowance: ~s/~s ~%"
		 now-err error-allowance)
	 nil))))
  )

(eval-when (load eval)
	   (format t "...Done~%"))