Ran update-copyright.py
[hooke.git] / hooke / ui / gui / dialog / points.py
1 # Copyright (C) 2010-2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
17
18 from numpy import arange
19
20 import wx
21
22
23 class ClickedPoint(object):
24     """Defines a clicked point from a curve plot.
25     """
26     def __init__(self):
27
28         self.is_marker=None #boolean ; decides if it is a marker
29         self.is_line_edge=None #boolean ; decides if it is the edge of a line (unused)
30         self.absolute_coords=(None,None) #(float,float) ; the absolute coordinates of the clicked point on the graph
31         self.graph_coords=(None,None) #(float,float) ; the coordinates of the plot that are nearest in X to the clicked point
32         self.index=None #integer ; the index of the clicked point with respect to the vector selected
33         self.dest=None #0 or 1 ; 0=top plot 1=bottom plot
34
35     def find_graph_coords(self,xvector,yvector):
36         """Find the point in the dataset that is closest to `self`.
37
38         Given a clicked point on the plot, finds the nearest point in
39         the dataset (in X) that corresponds to the clicked point.
40         """
41         dists=[]
42         for index in arange(1,len(xvector),1):
43             dists.append(((self.absolute_coords[0]-xvector[index])**2)+((self.absolute_coords[1]-yvector[index])**2))
44
45         self.index=dists.index(min(dists))
46         self.graph_coords=(xvector[self.index],yvector[self.index])
47
48
49 def measure_N_points(hooke_frame, N, message='', block=0):
50     '''
51     General helper function for N-points measurements
52     By default, measurements are done on the retraction
53     '''
54     if message:
55         dialog = wx.MessageDialog(None, message, 'Info', wx.OK)
56         dialog.ShowModal()
57
58     figure = self.GetActiveFigure()
59
60     xvector = self.displayed_plot.curves[block].x
61     yvector = self.displayed_plot.curves[block].y
62
63     clicked_points = figure.ginput(N, timeout=-1, show_clicks=True)
64
65     points = []
66     for clicked_point in clicked_points:
67         point = lib.clickedpoint.ClickedPoint()
68         point.absolute_coords = clicked_point[0], clicked_point[1]
69         point.dest = 0
70         #TODO: make this optional?
71         #so far, the clicked point is taken, not the corresponding data point
72         point.find_graph_coords(xvector, yvector)
73         point.is_line_edge = True
74         point.is_marker = True
75         points.append(point)
76     return points