Package lib :: Package geometry :: Module lines
[hide private]
[frames] | no frames]

Source Code for Module lib.geometry.lines

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2013 Edward d'Auvergne                                        # 
 4  #                                                                             # 
 5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
 6  #                                                                             # 
 7  # This program is free software: you can redistribute it and/or modify        # 
 8  # it under the terms of the GNU General Public License as published by        # 
 9  # the Free Software Foundation, either version 3 of the License, or           # 
10  # (at your option) any later version.                                         # 
11  #                                                                             # 
12  # This program is distributed in the hope that it will be useful,             # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
15  # GNU General Public License for more details.                                # 
16  #                                                                             # 
17  # You should have received a copy of the GNU General Public License           # 
18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
19  #                                                                             # 
20  ############################################################################### 
21   
22  # Module docstring. 
23  """Functions relating to line geometry.""" 
24   
25  # Python module imports. 
26  from numpy import dot 
27  from numpy.linalg import norm 
28   
29   
30 -def closest_point(line_pt1=None, line_pt2=None, point=None):
31 """Determine the closest position on the line to the given point. 32 33 This function defines the line using any two points on the line. 34 35 36 @keyword line_pt1: The first point defining the line. 37 @type line_pt1: numpy rank-1 array 38 @keyword line_pt2: The second point defining the line. 39 @type line_pt2: numpy rank-1 array 40 @keyword point: The point. 41 @type point: numpy rank-1 array 42 @return: The position on the line closest to the point. 43 @rtype: numpy rank-1 array 44 """ 45 46 # The vector along the line. 47 vect = line_pt2 - line_pt1 48 49 # Forward. 50 return closest_point_ax(line_pt=line_pt1, axis=vect, point=point)
51 52
53 -def closest_point_ax(line_pt=None, axis=None, point=None):
54 """Determine the closest position on the line to the given point. 55 56 This function defines the line using any point on the line and the axis. 57 58 59 @keyword line_pt1: The point defining the line. 60 @type line_pt1: numpy rank-1 array 61 @keyword axis: The axis defining the line. 62 @type axis: numpy rank-1 array 63 @keyword point: The point. 64 @type point: numpy rank-1 array 65 @return: The position on the line closest to the point. 66 @rtype: numpy rank-1 array 67 """ 68 69 # Check if the two points are the same, returning the point to avoid NaNs. 70 if norm(line_pt - point) < 1e-6: 71 return point 72 73 # The hypotenuse. 74 hypo = point - line_pt 75 hypo_len = norm(hypo) 76 unit_hypo = hypo / hypo_len 77 78 # Normalise the axis. 79 axis = axis / norm(axis) 80 81 # The distance from the point defining the line to the closest point. 82 d = hypo_len * dot(axis, unit_hypo) 83 84 # The closest point. 85 return line_pt + d*axis
86