Changes between Initial Version and Version 1 of 3DDistancecalc


Ignore:
Timestamp:
Mar 30, 2011, 11:40:22 AM (13 years ago)
Author:
nicklas
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • 3DDistancecalc

    v1 v1  
     1== About the new 3D distance functions in PostGIS 2.0.0 ==
     2
     3This is a presentation of the thoughts and logic behind the new 3D distance functions in PostGIS. [[BR]]
     4Please read it and don't hesitate to make some noise on the e-mail lists if something looks strange.
     5
     6'''First some notes about the difference between 2D and 3D distance calculations''':
     7
     8After all it, all boils down to using Pythagoras theorem on two points to get the distance.[[BR]]
     9 Pythagoras theorem works on three dimensions too. The problem is to find the two points...
     10
     11In 2D the two closest points that we are seeking is always:
     12
     13One vertexpoint on geometry 1 and one geometry on geometry 2[[BR]]
     14Example:
     15{{{
     16LINESTRING(1 1, 1 10) - LINESTRING(1 12, 1 14)
     17}}}
     18
     19'''or'''
     20
     21One vertex point on geometry 1 and a point somewhere along an edge on geometry 2
     22(or of course vice verse according to geometry 1 and 2)[[BR]]
     23Example:
     24{{{
     25LINESTRING(2 5, 10 5) - LINESTRING(1 1, 1 10)
     26}}}
     27
     28What we also need to check is intersection edge to edge
     29
     30In 3D there is a third possibility:[[BR]]
     31Both the points we are looking for (the two closest points on the geometries) might be on the edges.[[BR]]
     32Example:
     33{{{
     34LINESTRING(1 1 5, 10 1 5) LINESTRING(5 2 1, 5 2 10)
     35}}}
     36
     37
     38'''So, what we need to check for the different geometry types is:'''
     39
     40
     41'''Point – Point'''
     42
     43Just Pythagoras theorem on the two points.[[BR]]
     44Example:
     45{{{
     46point(1 1 1) - point(2 2 2)
     47}}}
     48
     49
     50'''Point-Line'''
     51
     52''point-point'' calculation between the point and every vertex in the linestring[[BR]]
     53Example: see ''point-point'' above
     54
     55''point-edge'' calculation between the point and every edge in the linestring[[BR]]
     56Example:
     57{{{
     58point(5 5 5) - line(1 1 0, 1 10 0)
     59}}}
     60
     61
     62'''Point-Polygon'''
     63
     64''point-point'' calculation between the point and every vertex in the polygon[[BR]]
     65Example: see ''point-point'' above
     66
     67''point-edge'' calculation between the point and every edge in the polygon[[BR]]
     68Example: see ''point-edge'' above
     69
     70''point-surface'' calculation between the point and the surface of the polygon[[BR]]
     71Example:
     72{{{
     73point(5 5 10) polygon((1 1 1, 1 10 1, 10 10 1, 10 1 1, 1 1 1))
     74}}}
     75
     76
     77'''Line-Line'''
     78
     79''point-point'' calculation between every vertex in line 1 to every vertex in line 2[[BR]]
     80Example: see ''point-point'' above
     81
     82''point-edge'' calculation between every vertex point to every edge in the other geoemtry.[[BR]]
     83Example: see ''point-line'' above
     84
     85''edge-edge'' calculation between every edge in line 1 to every edge in line 2[[BR]]
     86Example:
     87{{{
     88line(1 1 5, 10 1 5) line(5 2 1, 5 2 10)
     89}}}
     90
     91'''Line-Polygon'''
     92
     93''point-point'' calculation between every vertex in the line to every vertex in the polygon[[BR]]
     94Example: see ''point-point'' above
     95
     96''point-edge'' calculation between every vertex in the line to every edge in the boundary of the polygon and vice verse.[[BR]]
     97Example: see ''point-line'' above
     98
     99''edge-edge'' calculation between every edge in the line to every edge in the boundary of the polygon[[BR]]
     100Example: see ''line-line'' above
     101
     102''point-surface'' calculations between every vertex in the linestring to the surface of the polygon[[BR]]
     103Example: see ''point-polygon'' above
     104
     105
     106'''Polygon-Polygon'''
     107
     108Actually polygon-polygon is the same calculations as line-polygon. [[BR]]
     109The two surfaces can not be closer to each other than any of the other options. [[BR]]
     110This is the equivalence in 3d to the not needed edge-edge calculation in 2d.
     111
     112
     113 This is not the whole truth since one of the combinations here also needs a specific test to see if there is an intersection.[[BR]]
     114That is the case of edge-surface intersection.[[BR]]
     115Example:
     116{{{
     117line(5 5 0, 5 5 10) polygon((1 1 1, 1 10 1, 10 10 1, 10 1 1, 1 1 1))
     118}}}
     119
     120In the other cases we can find the intersections by checking if the distance is 0
     121
     122'''Defining the plane for surface calculations'''
     123
     124
     125One of the key parts of the 3D distance calculations is in calculations involving surfaces.[[BR]]
     126To do those calculations we need all points in a surface to be coplanar and we need to define this plane.[[BR]]
     127
     128A plane can be represented in many ways.
     129 
     130 * 3 points on the plane gives a definition of the plane
     131 * 2 vectors can define a plane.
     132 * 1 point on the plane and 1 vector perpendicular to the plane
     133
     134There we use the third option
     135
     136If our surface is a triangle like
     137{{{
     138POLYGON((1 1 1, 1 5 1, 9 3 2, 1 1 1))
     139}}}
     140then there is no problem.[[BR]]
     141The three unique points defines the plane and can not be anything else than coplanar.
     142
     143But if there is more than 3 vertexes we can not be sure that they are coplanar.[[BR]]
     144Or more right, in most cases we can be sure that they are not coplanar because of precision problems.
     145
     146So, how to handle this?
     147
     148Here I will describe how this is done now in the distance calculations.[[BR]]
     149This is probably the part to most critical about.
     150
     151There is no attempt to find if the points is not coplanar, just an attempt to get an approximation between the points if they are not perfectly coplanar.
     152
     153Judging them as “not coplanar enough” should be a job for ST_IsValid instead since it will take to much power to do that on every distance calculation.
     154
     155'''Ok, here is how the plane definition is done now:'''
     156
     157First we find the average point of the surface by just adding all x-values, dividing with the number of vertexpoints and do the same for y and z.
     158
     159This average-point should be on the plane and is the “point on the plane” in our plane definition.
     160
     161Now we just need a perpendicular vector.[[BR]]
     162 That we can get from any two points in the boundary of the surface together with our newly defined “point on the plane”.
     163
     164If we take two points on the boundary of the surface and define lines between our point on plane and each of those, then we seek the line that is perpendicular to both this lines.
     165
     166The most robust calculation we get if the line from our “point on plane” to the first point in the boundary is perpendicular to the second point on the boundary.[[BR]]
     167Since we don't know how the points are distributed along the boundary we have to do some assumptions.[[BR]]
     168We will also do the calculation about 4 times (3-5) and use the average answer.
     169
     170Lets say that we are trying to define the plane of a surface with 22 vertex points.
     171We divide the surface in slices with ¼ of the vertex points in each slice.
     172So in our example each slice contains 5 vertex-points.
     173
     174Then we use the first vertex point together with the 6:th and the “point on plane” to define a perpendicular line (vector actually) to the plane.
     175
     176Then we use the 6:th vertex point and the 11 and do the same.
     177
     178Then the 11 and the 16 and last 16 and the 21.
     179
     180Depending on if the total number of vertex points is dividable by 4 the outcome will vary.[[BR]]
     181The idea is to walk the way around in about 4 steps and use the average result.
     182
     183The code doing this can be found in [http://trac.osgeo.org/postgis/browser/trunk/liblwgeom/measures3d.c]
     184
     185/Nicklas