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