Skip to content

Commit c65abd7

Browse files
committed
Merge pull request #268 from YoheiKakiuchi/fix_camramodel
Fix camera model on irtsensor
2 parents c8e608b + c6c5941 commit c65abd7

File tree

3 files changed

+87
-12
lines changed

3 files changed

+87
-12
lines changed

irteus/demo/sample-camera-model.l

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
;; create camera and camera viewer
2+
(setq *camera-model*
3+
(make-camera-from-param :pwidth 640 :pheight 360
4+
:fx 400 :fy 400
5+
:cx 319.5 :cy 179.5 :name "camtest"
6+
:create-viewer t))
7+
;; move camera
8+
(send *camera-model* :translate #f(0 100 0) :world)
9+
(send *camera-model* :rotate 0.25 :x :world)
10+
11+
;; make objects
12+
(setq *obj1* (make-cube 100 100 100))
13+
(send *obj1* :translate #f(-50 0 235))
14+
(send *obj1* :set-color #f(0 0 1))
15+
(setq *obj2* (make-cube 100 100 100))
16+
(send *obj2* :translate #f(50 0 265))
17+
(send *obj2* :set-color #f(1 0 0))
18+
(setq *obj3* (make-cube 100 100 100))
19+
(send *obj3* :translate #f(0 100 250))
20+
(send *obj3* :set-color #f(0 1 0))
21+
(objects (list *obj1* *obj2* *obj3* *camera-model*))
22+
23+
24+
;; draw objects on camera viewer
25+
(send *camera-model* :draw-objects (list *obj1* *obj2* *obj3*))
26+
27+
;; get image and point cloud
28+
(let ((ret (send *camera-model* :get-image :with-points t :with-colors t)))
29+
(setq *image* (car ret))
30+
(setq *points* (cdr ret))
31+
)
32+
33+
;; transform origin of point cloud
34+
(send *points* :transform (send *camera-model* :worldcoords))
35+
36+
(objects (list *points* *camera-model*))

irteus/irtgl.l

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
"Get current view to a image object. It returns color-image24 object."
184184
(let ()
185185
(send self :makecurrent)
186-
(glReadBuffer GL_BACK)
186+
(glReadBuffer GL_FRONT)
187187
(glPixelStorei GL_PACK_ALIGNMENT 1)
188188
(glReadPixels x y width height GL_RGB GL_UNSIGNED_BYTE imgbuf)
189189
#-:x86_64

irteus/irtsensor.l

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
(:projection :newprojection
7979
:view :viewpoint :view-direction :viewdistance
8080
:yon :hither))
81-
pwidth pheight))
81+
img-viewer pwidth pheight))
8282
(defmethod camera-model
8383
(:init
8484
(b &rest args
@@ -109,6 +109,7 @@
109109
(:width () "Returns width of the camera in pixel." pwidth)
110110
(:height () "Returns height of the camera in pixel." pheight)
111111
(:viewing (&rest args) (forward-message-to vwing args))
112+
(:image-viewer (&rest args) (forward-message-to img-viewer args))
112113
(:fovy () "Returns field of view in degree"
113114
(let ((proj (send vwing :projection)))
114115
(* 2 (atan2 (/ pwidth 2.0) (aref proj 0 0)))))
@@ -190,7 +191,8 @@
190191
(send vwer :viewsurface :color pcolor)
191192
(gl::draw-glbody vwer self)
192193
(if flush (send vwer :viewsurface :flush))))
193-
(:draw-objects
194+
(:draw-objects (objs) (send self :draw-objects-raw img-viewer objs))
195+
(:draw-objects-raw
194196
(vwr objs)
195197
(let* (pcurrent pcolor (draw-things (x::draw-things objs))
196198
viewpoint viewtarget
@@ -199,6 +201,9 @@
199201
(f (aref proj 0 0)))
200202
(send vwr :viewsurface :makecurrent)
201203
;;(resetperspective (send vwr :viewing) (send vwr :viewsurface))
204+
(if (> pwidth pheight)
205+
(gl::glviewport 0 (- (/ (- pwidth pheight) 2)) pwidth pwidth)
206+
(gl::glviewport (- (/ (- pheight pwidth) 2)) 0 pheight pheight))
202207
(gl::glMatrixMode gl::GL_PROJECTION)
203208
(gl::glLoadIdentity)
204209
;; the following should get aspect ration from viewport
@@ -209,12 +214,13 @@
209214
(setq viewpoint
210215
(v+ (send self :worldpos) ;; for right camera
211216
(send self :viewing :rotate-vector
212-
(scale 1000.0 (float-vector (/ (- (- (/ pwidth 2.0) 1) cx) f)
213-
(/ (- (- (/ pheight 2.0) 1) cy) f)
217+
(scale 1000.0 (float-vector (/ (- (/ (1- pwidth) 2.0) cx) f)
218+
(/ (- (/ (1- pheight) 2.0) cy) f)
214219
0)))))
215220
;; glview define view-directoin to oppsite direction
216221
(setq viewtarget
217222
(v- viewpoint (send self :viewing :view-direction) ))
223+
(pprint (list viewpoint viewtarget (v- (send self :viewing :view-up))))
218224
(gl::gluLookAtfv (concatenate vector viewpoint viewtarget
219225
(v- (send self :viewing :view-up))))
220226
(gl::glMatrixMode gl::GL_MODELVIEW)
@@ -236,7 +242,27 @@
236242
)
237243
(send vwr :viewsurface :flush)
238244
))
239-
(:get-image (vwr &key (points) (colors))
245+
(:get-image
246+
(&key (with-points) (with-colors))
247+
(let (points colors img pc)
248+
(if with-points
249+
(setq points (make-matrix (* pwidth pheight) 3)))
250+
(if with-colors
251+
(setq colors (make-matrix (* pwidth pheight) 3)))
252+
(setq img (send self :get-image-raw img-viewer :points points :colors colors))
253+
(cond
254+
((and with-points with-colors)
255+
(setq pc (instance pointcloud :init
256+
:height pheight :width pwidth
257+
:points points :colors colors)))
258+
(with-points
259+
(setq pc (instance pointcloud :init
260+
:height pheight :width pwidth
261+
:points points))))
262+
(if with-points
263+
(cons img pc)
264+
img)))
265+
(:get-image-raw (vwr &key (points) (colors))
240266
(let* ((sf (send vwr :viewsurface))
241267
(width (send sf :width))
242268
(height (send sf :height))
@@ -256,7 +282,7 @@
256282
(when points
257283
(unless (and (= width pwidth) (= height pheight))
258284
(warn ";; width: %d /= %d or height: %d /= %d~%" width pwidth height pheight)
259-
(return-from :get-image))
285+
(return-from :get-image-raw))
260286
(setq fv (make-array num :element-type :float))
261287
(setq mat-ent (array-entity points))
262288
(fill mat-ent 0.0)
@@ -271,9 +297,10 @@
271297
(dotimes (x width)
272298
(if (< (elt fv vptr) 1.0)
273299
(let ((zpos (/ (* fp np) (- (* (elt fv vptr) (- fp np)) fp))))
300+
(setq mptr (* 3 (+ (* (- height y 1) width) x)))
274301
(setf (elt pos 0) (* (- cx x) (/ zpos focus)))
275-
(setf (elt pos 1) (* (- y cy) (/ zpos focus)))
276-
(setf (elt pos 2) (- zpos))
302+
(setf (elt pos 1) (* (- cy (- height y 1)) (/ zpos focus)))
303+
(setf (elt pos 2) (- zpos)) ;; ok
277304
(sys::vector-replace mat-ent pos mptr)
278305
(when colors
279306
(setf (elt col 0)
@@ -283,15 +310,15 @@
283310
(setf (elt col 2)
284311
(/ (sys::peek img-ent (+ mptr 2) :byte) 255.0))
285312
(sys::vector-replace col-ent col mptr))))
286-
(incf mptr 3)
287313
(incf vptr 1)
288314
)))
289315
img
290316
))
291317
)
292318

293319
;; utility functions
294-
(defun make-camera-from-param (&key pwidth pheight fx fy cx cy (tx 0) (ty 0) parent-coords name)
320+
(defun make-camera-from-param (&key pwidth pheight fx fy cx cy (tx 0) (ty 0) parent-coords name
321+
create-viewer)
295322
"Create camera object from given parameters."
296323
(let* ((b (body+ (make-cube 40 30 30)
297324
(send (make-cylinder 2 30) :rotate pi/2 :x)
@@ -310,9 +337,21 @@
310337
(send c :translate (float-vector (- tx) (- ty) 0))
311338
(send (c . vwing) :translate (float-vector tx ty 0))
312339
(if parent-coords (send parent-coords :assoc c))
340+
(when create-viewer
341+
(unless (boundp '*irtviewer*) (make-irtviewer))
342+
(let ((cv
343+
(view
344+
:x pwidth :y pheight
345+
:viewing (send c :viewing)
346+
:viewsurface
347+
(instance gl::glviewsurface :create
348+
:glcon ((send *irtviewer* :viewer :viewsurface) . gl::glcon)
349+
:title (format nil "~A_view" name) :width pwidth :height pheight)
350+
:title (format nil "~A_view" name))))
351+
(setq (c . img-viewer) cv)
352+
))
313353
c))
314354

315-
316355
(in-package "GEOMETRY")
317356

318357
(provide :irtsensor "$Id: $")

0 commit comments

Comments
 (0)