2
votes

Given a flat (equarectangilar panoramic) image of for instance 6000px x 3000px (spreading 360 degrees wide and 180 degree high). How would I translate for instance x = -10, y=-10 to spherical coordinates (pan/tilt or vertical/horizontal offset) where the center of the image would mean a horizontal/vertical offset of 0?

Is it possible to calculate this, or do you need other variables like the radius, distance or z coordinate?

Edit: What I have so far:

def self.translate_xy_to_spherical(x, y)
  h = (x / (6000 / 360)) - 180
  v = ((y / (3000 / 180)) - 90) / - 1
  [h, v]
end

def self.translate_spherical_to_xy(h, v)
  x = ((h + 180) * (6000  / 360))
  y = ((v * -1) + 90) * (3000/ 180)

  [x, y]
end

If I put in 0,0 in the first method, I get -180,90 which is correct. But if I set 3000,0 I'd expect 0,90 but I get 7,90. Same goes with the other formula (xy to spherical). When I input 0,0 I'd expect 3000,1500 but I get 2880x1440px. There is a small offset, probally because I calculate in a straight line.

Update: The Answer

I've updated the answer from below to take in account that degrees could be bigger than 360 degrees. I use the modulo to fix this:

IMAGE_WIDTH = 6000
IMAGE_HEIGHT = 3000
def self.translate_xy_to_spherical(x, y)
  h = (x / (IMAGE_WIDTH / 360.0)) - 180
  v = ((y / (IMAGE_HEIGHT / 180.0)) - 90) / -1
  [h, v]
end

def self.translate_spherical_to_xy(h, v)
  x = (((h % 360) + 180) * (IMAGE_WIDTH  / 360.0))
  y = (((v % 180) * -1) + 90) * (IMAGE_HEIGHT/ 180.0)

  [x, y]
end
1
Do you want to map a flat image to sphere ?Kamen Stoykov
Yes, an equarectangilar flat image. en.wikipedia.org/wiki/Equirectangular_projectionJareish
Why can't you use the formulas from wikipedia ?Kamen Stoykov
I've edited my question with what I have so far. I've seen dozens of formulas on wikipedia, but I'm not quite sure what I'm looking for. Spherical, cartessian, polar, cylindrical. Also most talk about distances and a z coordinate. For instance: geom.uiuc.edu/docs/reference/CRC-formulas/node42.html I can't see how to apply thisJareish
Do you know what you want to translate your x/y coordinates to/from? If you want spherical, that's a radius and two angles. If you want latitude/longitude, that's two angular values that don't have a radial dimension. I don't think you want cylindrical (just a hunch). What are you wanting to use this for?lurker

1 Answers

4
votes

Your equations are mathematically correct, but when dividing integers in Ruby (and in most languages), you will lose the remainder. For example 6000/360 = 16.666... in real life, but in Ruby you'll get 16. All these rounding errors will lead to errors in your final result. A trick to avoid this avoid this is to make some of the numbers in your arithmetic are Floats instead of just Fixnums. Try:

def self.translate_xy_to_spherical(x, y)
  h = (x / (6000 / 360.0)) - 180
  v = ((y / (3000 / 180.0)) - 90) / - 1
  [h, v]
end

def self.translate_spherical_to_xy(h, v)
  x = ((h + 180) * (6000  / 360.0))
  y = ((v * -1) + 90) * (3000/ 180.0)

  [x, y]
end