Archiv der Kategorie: 3D

Creating Stereo Images in SketchUp

I do use the free Version of Trimble SketchUp (formerly known as Google SketchUp) quite a lot. The free version is missing a simple way to create stereoscopic images and I didn’t find a (free) Ruby script to do the job.

Time to revive some Ruby and computer graphic lectures.

SketchUp lets you manipulate the Camera object via its API.
The camera is defined by its eye, target and up-vector. The goal to achieve a stereoscopic image is to create a new camera, which is „parallel“ to the existing one.
This „parallel“ is a little bit more complicated in 3D space.

Figure 1 shows a generic camera which is not aligned within the global 3d space (colored axes).

Figure 2 shows the coordinate system of that camera itself. The stereoscopic cameras eye must be aligned on the red x axis.

Figure 3 shows how the second camera corresponds to the first, existing one.

Therefor the transformation which has to be applied is a translation in an angle of 90 degree to the up vector. SketchUps Points provide an offset method to do exactly that.

# get a reference to the active camera
view = Sketchup.active_model.active_view
cam = view.camera

# get copies of the cameras up vector and eye point
up = cam.up.clone
eye = cam.eye.clone

# create transformation for rotating the up vector by 90 degree an apply that
rotVec = Geom::Vector3d.new 0,1,0
rotAngle = -1 * Math::PI/2
transformation = Geom::Transformation.rotation eye, rotVec, rotAngle
vec = up.transform transformation

# calculate second cameras points with the calculated vector and the offset method
newEye = cam.eye.offset vec, distance
newTarget = cam.target.offset vec, distance
# up vector remains unchanged
newUp = cam.up

# apply new eye, target and up vector to the camera
cam.set newEye, newTarget, newUp

SketchUp also provides an API call to export images and so the complete script does that as well.

# define a value for eye distance in the document active unit
# 65 mm is a good average
# see http://en.wikipedia.org/wiki/Pupillary_distance
$eyeDistance = 65

# define the size of the 3D image
$imgWidth = 1024
$imgHeight = 768

# offset for stereo view (0 or 1)
$winOffset = 0

def moveCam(distance)
  view = Sketchup.active_model.active_view
  cam = view.camera

  up = cam.up.clone
  eye = cam.eye.clone
  rotVec = Geom::Vector3d.new 0,1,0
  rotAngle = -1 * Math::PI/2
  transformation = Geom::Transformation.rotation eye, rotVec, rotAngle
  vec = up.transform transformation
  newEye = cam.eye.offset vec, distance
  newTarget = cam.target.offset vec, distance
  newUp = cam.up
  newCam = cam.set newEye, newTarget, newUp
end

def exportPng(path, width, height)
  keys = {
    :filename => path,
    :width => width,
    :height => height,
    :antialias => true,
    :transparent => false
  }
  view = Sketchup.active_model.active_view
  view.write_image keys
end

def showInterlacedView(w, h, leftImg, rightImg)
  template = '<!DOCTYPE html><html><head><meta charset="utf-8"><title>SU-Stereo</title><meta name="copyright" content="Stefan Motz, 2013"><style type="text/css" media="screen">body {padding:0;margin:0} #display{position:relative;width:%spx;height:%spx;overflow:hidden;}#display .leftImg {position:absolute;-webkit-mask:url(\'http://stefan-motz.de/su-stereo/assets/mask-horizontal-bw.png\');top:0px;left:0px;height:100%%;}#display .rightImg {position : absolute;top:0px;left:0px;height:100%%;}</style></head><body><div id="display"><img class="rightImg" src="file://%s"><img class="leftImg" src="file://%s"></div></body></html>'
  html = sprintf(template, w, h, leftImg, rightImg)

  dlg = UI::WebDialog.new('SU-Stereo', false,'SUStereoView', w, h, 150, 150 + $winOffset, true)
  dlg.navigation_buttons_enabled = false
  dlg.set_html(html)
  dlg.show
end

### main program

view = Sketchup.active_model.active_view

# ask user for filename and setup incl. pathes
saveName = UI.savepanel 'Save 3D files', '', "Stereo"
if (saveName == nil)
  exit(0)
end
fileNames = {
  :left => saveName + "_L.png",
  :right => saveName + "_R.png"
}

# move camera to left eye position
moveCam($eyeDistance/2 * -1)
# save left picture
exportPng(fileNames[:left], $imgWidth, $imgHeight)

# move camera to right eye position
moveCam($eyeDistance)
# save left picture
exportPng(fileNames[:right], $imgWidth, $imgHeight)

# reset view
moveCam($eyeDistance/2 * -1)

showInterlacedView($imgWidth, $imgHeight, fileNames[:left], fileNames[:right])

And here is what it looks like:

As a little extra the script displays the stereo image thru a webview in an horizontally interlaced way. This probably works on Mac only and is only useful, if you have a passive 3D monitor connected.

The exported stereo images can be post-processed by various tools, on request I might write a follow up on how to integrate a workflow with ImageMagick.

During development the SketchUp Ruby Code Editor by Alex Schreyer was quite helpful – thanks for that!

3D camera 2.0

Now it’s been 3 years since I built the first version of my 3D camera. Meanwhile we took plenty of pictures in 3D.

It always bothered me, that the previous model had no synced release. And while the landscape mode of the cameras is good for anaglyph images or 3D TVs, the portrait mode is more suitable for side-by-side images as used by paralax viewers.

By placing the cameras side-by-side in portrait mode, the whole gadget becomes a little more handy.

Key to build a new frame was the laser cutting service of Formulor. They offer laser cutting for different materials like acryl, wood, paper, etc. All one has to do is send in a vector file and you get precisely cutted profiles within a few days.

So I do hope to present some new pictures soon.

3D camera with cameras attached
3D camera acryl frame

Magic Earth: Unsere Welt in 3D

Seit ein paar Tagen ist das Buch „Magic Earth: Unsere Welt in 3D“ im Handel erhältlich, in dem auch eine meiner Peru-3D-Aufnahmen abgedruckt ist. Mein Beleg-Exemplar ist auch bei mir angekommen.

Ein wenig schade ist, dass die Bilder über die Doppelseite und damit über den Falz gehen.

Dafür gibt es schöne Fotos und Relief-Karten in 3D rund um den Globus.

3D pictures from Peru

On our vacation to Peru I took several 3D pictures with my self made 3D camera. You will need red/cyan anaglyph glasses to see the 3D effect.


Adobe pyramid in Lima


Plants and animals of Peru


Ica, Nazcar and Maria-Reiche-Museum


Arequipa, Peru / Convent de Santa Catalina / Misti


Cañon de Colca


Tiwanacu, Bolivia


Chacaltaya mountain, Bolivia


Moon Valley, La Paz, Bolivia


Lake Titicaca, Copacabana, Uro Islands


Inca ruins and stones, Cusco, Peru


Wiñya Wayna and Inka trail


Machu Picchu in 3D

Building my own 3D camera

UPDATE: 3D camera 2.0

1999 i built my first 3D camera by glueing two on way paper cameras together. As 3D seems to be the buzz word of 2009 and more and more display technologies get available, I decided to build a new 3D camera with digital components. On incentive was to get it ready for our this years vacation in Peru.

Thru some internet research I found a Kodak Easyshare M1093 IS to be the perfect camera to buy. It has 10MP, 3x optical Zoom, image stabilization, a big bright display and – best of all – it is able to record video in 720p. Furthermore I made a very good deal at our local store of Conrad electronics. I got two cameras for as little as 220,- euro.

Next step was to construct a frame to hold the two cameras. First of all I took a caliper and made a drew a raw model of the camera using the free construction tool SketchUp from Google.  I focused on the outer body and the position of the lens/blind and the position of the tripod winding relative to the body. You may downlaod the SketchUp file here.

camera-sketchup

This very simple model allowed me to arrange the cameras and to draw a frame around them. I chose an eye distance of 68mm. The next picture show a very early stage of the final SketchUp. Beforehand I bought a square-cut aluminium bar, 10×10 mm, hollow, 8×8 mm inside (cost about 2 euro per meter). So I did know what to draw, roughly.

early-frame-modelThe final sketch was more detailed and layed out with all figures to start the construction. I will make it available here as well.

After a lot of cutting, rasping, drilling, finishing etc. I ended up with this construction:

3d-photo-kamera-eigenbau-diy

(The picture above is actually a photoshop mockup, as I had to use one camera to take the picture)

The reason why one camera is upside down is, that otherwise the eye distance would not be preserved. This makes it very difficult to construct a mechanic release. For me it works fine to press both release buttons simultaneously.

After all I got a cheap 3D camera. I spent 230,- euro (and up to 8 hours of work :-) ).

Of course there are some lessons learned:
Tripod threads/screws are measured in inches, no chance to get a screw at the next hardware store (when you are in europe, of course)
It is quite difficult to cut the aluminium bar angled. It’s a must to use a bench vise.
It is possible to buy such frames – as I figured out later ;-)

If you made it down to here, I’m sure you are eager to know, what the results are: take a look.