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