1

let me begin by saying that I've read through Calculate point A from given point E and angle and afterwards calculate Point A from E and angle , Calculate point, given x, y, angle, and distance , a problem involving trigonometry functions, and especially How to get coordinates of a point in a coordinate system based on angle and distance and none of them managed to lift the veil of confusion, surrounding me.

What I am doing: I want to create a sort of Instantaneous Field of View (FOV) for a bunch of sequences of points; the FOVs would represent what is visible from each point, depending of course, on the direction at which we are looking at ( 0 - North; 90 - East; 180 - South; 270 - West; 360 - North). The FOV is essentially a triangle, where the central (C) vertex is the point itself, vertex A and vertex B, whose coordinates I am looking for, being the ones connected to the base of the triangle.

The code snippet: I am essentially approaching this via leveraging two right triangles, that together constitute the FOV, like so:

enter image description here

--------- A VERTEX -------------

for (p in 1:nrow(pnp.90.deg@data)){   #pnp is the spatial points dataframe, containing attribute information such as lon/lat(coordinates) and ca(camera angle - showing the direction of sight/movement in degrees)
   a_alfa1 <- pnp.90.deg@data$ca - (pnp.90.deg@data$ca - 60)
   a_alfa1rad <- a_alfa1 * (pi/180)
   a_x1 <- pnp.90.deg@data$lon + 0.00035 * cos(a_alfa1rad)
   a_y1 <- pnp.90.deg@data$lat + 0.00035 * sin(a_alfa1rad) 
   avert1 <- cbind(a_x1, a_y1)
   colnames(avert1) <- c("lon", "lat") 
   avert.90<-SpatialPoints(avert1, proj4string=CRS("+proj=longlat +datum=WGS84      +no_defs +ellps=WGS84 +towgs84=0,0,0"), bbox=NULL) 
 }

--------- B VERTEX -------------

for (p in 1:nrow(pnp.90.deg@data)){
  b_alfa1 <- pnp.90.deg@data$ca - (pnp.90.deg@data$ca + 60)
  b_alfa1rad <- b_alfa1 * (pi/180)
  b_x1 <- pnp.90.deg@data$lon + 0.00035 * cos(b_alfa1rad)
  b_y1 <- pnp.90.deg@data$lat + 0.00035 * sin(b_alfa1rad)
  bvert1 <- cbind(b_x1, b_y1)
  colnames(bvert1) <- c("lon", "lat") 
  bvert.90<-SpatialPoints(bvert1, proj4string=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"), bbox=NULL) 
}

The result: the code produces the triangle as one would expect, yet it does so only when the angle (ca) is between 0-90 degrees:

enter image description here

The Problem

This formula doesn't seem to work for other camera angles. To my mind (and according to the provided links to topics) the formula should be universally applicable for any angle measure. Can someone provide some input on whether I am a) using the right formula and b) using it in the correct way.

UPDATE: link to the spatial points data frame in shapefile format: https://drive.google.com/file/d/1ax5OG8c8Cl-Hz3N16ye9OoG4z7l8HSAQ/view?usp=sharing

UPDATE 2: The process of getting from the pnpover (the shared spdf) to pnp.90.deg is just a spatial subset:

pnp.90.deg <- subset(pnpover, pnpover@data$ca <= 90)

I decided to bucket looking angles in ranges of 0-90; 91-180; 181-270; 271-360 in order to test what was going wrong.

Thank you kindly!

Momchill
  • 417
  • 6
  • 15
  • Posting problem code without data is a cause for question closure. – IRTFM Jul 05 '18 at 21:13
  • Apologies for that, I updated the post to include the spdf I am using. – Momchill Jul 05 '18 at 21:22
  • You need to show the R code that takes you from 0_DemoGrassland_MetaData to pnp.90.deg@data – IRTFM Jul 05 '18 at 22:32
  • @42- the operations are a few and consist of harvesting said points (that actually represent images) and performing some buffer and intersect to only keep points(images) within a certain area. The next step is the FOV generation. and pnp.90.deg is just a spatial subset of the file I shared that holds only the 0-90 degree images. – Momchill Jul 06 '18 at 07:56
  • What exactly does *"doesn't seem to work"* mean? As far as I see, `alfa1` is always +- 60 and does not depend on `pnp.90.deg@data$ca`. Is this correct? – Nico Schertler Jul 06 '18 at 12:42
  • @NicoSchertler yes, indeed I would like all the FOVs to be the same shape and size, which requires them to have the same angles. By "doesn't work" I mean something like this happening: https://imgur.com/a/NCSeqfm . For this result I ran the same code but on a sequence of points (images) that have a looking angle (ca) between 270-360. – Momchill Jul 06 '18 at 13:11
  • Unless I've grossly misunderstood R syntax, `pnp.90.deg@data$ca - (pnp.90.deg@data$ca + 60)` should not depend on the angle `ca` at all... – meowgoesthedog Jul 06 '18 at 14:31
  • @meowgoesthedog , yes the angle would always be the same, that is quite central to my idea - have the same triangle build from each point. – Momchill Jul 06 '18 at 14:40
  • But your first problem statement is that the code does not work for any values of `ca` outside 0-90 degrees. – meowgoesthedog Jul 06 '18 at 14:41
  • Correct, might you elaborate on where you are going with this? – Momchill Jul 06 '18 at 14:45
  • So these two statements are contradictory...? Can you clarify what the actual problem is? Does it occur when the "60" is changed to > 90? In such a case, the triangle will become inverted, so what you need is a circular sector, or a "fan". – meowgoesthedog Jul 06 '18 at 14:50
  • What is wrong with the picture you posted? What @meowgoesthedog and I mean is that you should replace your alfa calculation with `pnp.90.deg@data$ca + 60` and `pnp.90.deg@data$ca - 60`. Otherwise, all triangles will look in the same direction. – Nico Schertler Jul 06 '18 at 15:02
  • Why? I am grounding the angle to a local angle within the respective triangle, while still wanting to keep the overall direction the base (long side) would be facing. Maybe a bit of a prehistory - initially i tried with `a_alfa1 <- pnp@data$ca +/- 60` and that produced equally unsatisfactory results when ran on larger angles. That is why I decided to ground it in the local one. The problem is that, to my mind using the formula for arriving at rectangular coordinates (x,y) using the polar coordinates (r, angle) should work universally. Just to clarify "increased angle" relates to ca – Momchill Jul 06 '18 at 15:02
  • As I pointed out, a triangle approach is not **supposed** to work with reflex angles, as it will invert. This doesn't mean `pnp@data$ca +/- 60` is wrong in itself - you applied the wrong fix because you didn't know what the source of the problem was. – meowgoesthedog Jul 06 '18 at 15:13
  • Ok, that was informative. I was myself puzzled as to how to get this triangle stuff to work when angles are so big that the total sum of all three becomes larger than 180. Hence my (not too intelligent) solution. I wonder then, would you have a suggestion? As of now I am bucketing angles in the groups of the four sectors and working a custom angle-radius solution, but this is not too efficient... – Momchill Jul 06 '18 at 15:17
  • The angle for sin/cos usage is the angle of the tangent at central vertex (lon,lat) **added** with +-60/2, being '60' the inner angle to the FOV. – Ripi2 Jul 06 '18 at 15:18
  • @Ripi2 , I though that is what I am doing: - cos/sin(camera angle -/+ 60) * r(distance) + x/y, no? – Momchill Jul 06 '18 at 15:33
  • What is `camera angle`? If it's the direction on view in general axis, that's right. If it's an inner angle, that's wrong. – Ripi2 Jul 06 '18 at 15:36
  • @Ripi2 , camera angle is the direction, in which the point (image) is looking at: 0 - North; 90 - East; 180 - South; 270 - West; 360 - North . So a point with a `ca` of 45 is looking north-east, etc. – Momchill Jul 06 '18 at 15:43

1 Answers1

0

Look at this picture:

enter image description here

with:

  • ca is the direction where the camera points to. It is angle that starts at North and goes clockwise, so 0=North, 90=East, etc.

  • fa is the inner angle for the field of view (FOV).

  • C is the camera position.

  • A and B are the coordinates to calculate. Their distance to C is d.

You may already know the conversion from polar to Cartesian coordinates:
x = cx + d * cos(alpha) and y = cy + d * sin(alpha)

But we have said that ca goes clockwise. It means that we must change sin/cos usage.

The needed formulas are:

x = cx + d * sin(alpha)
y = cy + d * cos(alpha)

So, for A, B coordinates:

ax = cx + d * sin(ca - fa/2)
ay = cy + d * cos(ca - fa/2)
bx = cx + d * sin(ca + fa/2)
by = cy + d * cos(ca + fa/2)

These are valid formulas for the whole of {0, 360} range. Or {0, 2PI} as you prefer.
If the origin was different from North (say 0=East) then we would need some sign corrections.

Likely the compiler is able to use any angle, even out of {0, 2PI} range. If not, you must first clamp the angle into the range. I don't speak r, so I write it in general form:

float parts = angle / 360
float rest = parts - int(parts)
float cAngle = rest * 360
better cAngleRadians = rest * 2 *pi
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • Thank you for the effort! I really appreciated it! It doesn't seem to work for me though: On the picture: I have this point (C), the camera angle (ca) is 85 and after executing your suggestion I get the following result: https://imgur.com/a/ixhUMKy , whereas the triangle should point its base at the direction of the arrow. Let me ask you this: is `fa` supposed to be a fixed value, vis-a-vis, if I want the measure of that angle to be 60, then it's sixty for every point. OR, `fa = ca -/+ 60` (for example) ? Again - thank you very much for your time! – Momchill Jul 06 '18 at 18:42
  • @Momchill In your image it seems you have a 90-degree error for camera angle `ca`. Are you sure your `ca` follows the meanings of my picture? – Ripi2 Jul 06 '18 at 18:50
  • @Momchill I'm using my own picture meanings. So `fa` is always the same, unless you want a different field of view amplitude for each center `C`. – Ripi2 Jul 06 '18 at 18:51
  • I am quite sure. And, yes, I also want `ca` to be constant. So, your suggestion works fine for your images, all of which have different `ca` and you manage to produce triangular FOVs with the up-stated formula? Can you share this point shapefile so that I may try it myself? – Momchill Jul 06 '18 at 19:00
  • One more thing - why do you do `fa / 2` ? – Momchill Jul 06 '18 at 19:08
  • @Momchill Sorry. I don't use any *shapefile*. And I don't know how to open the files you linked to in your post. – Ripi2 Jul 06 '18 at 19:08
  • @Momchill `fa` is the angle A-C-B. `fa/2` is the angle A-C-direction or B-C-direction. I suppose the FOV is centered in this 'direction' definde by angle `ca` – Ripi2 Jul 06 '18 at 19:10
  • I stand corrected, sir, your solution produces the desired results, yet I was foolish in not afore-hand converting my coordinate pairs from WGS84 to the relevant UTM zone, or whatever other projected CRS. This was causing the triangles to be skewed in whichever direction. I thank you! – Momchill Jul 10 '18 at 10:12