Commit a30db6845eba378787909e24a75edd7ed10b7c04

Authored by Thiago Franco de Moraes
1 parent 994b42f7

Drawing inside LinearMeasure and AngularMeasure

invesalius/data/measures.py
@@ -5,6 +5,8 @@ import math @@ -5,6 +5,8 @@ import math
5 import random 5 import random
6 6
7 from wx.lib.pubsub import pub as Publisher 7 from wx.lib.pubsub import pub as Publisher
  8 +
  9 +import numpy as np
8 import vtk 10 import vtk
9 11
10 import constants as const 12 import constants as const
@@ -559,6 +561,37 @@ class LinearMeasure(object): @@ -559,6 +561,37 @@ class LinearMeasure(object):
559 a.GetProperty().SetOpacity(0.75) 561 a.GetProperty().SetOpacity(0.75)
560 self.text_actor = a 562 self.text_actor = a
561 563
  564 + def draw_to_canvas(self, gc, viewer):
  565 + """
  566 + Draws to an wx.GraphicsContext.
  567 +
  568 + Parameters:
  569 + gc: is a wx.GraphicsContext
  570 + viewer: the viewer where the canvas is in.
  571 + """
  572 + coord = vtk.vtkCoordinate()
  573 + lines = []
  574 + for p in self.points:
  575 + coord.SetValue(p)
  576 + cx, cy = coord.GetComputedDisplayValue(viewer.slice_data.renderer)
  577 + cy = -cy
  578 + lines.append((cx, cy))
  579 + path = gc.CreatePath()
  580 + path.AddCircle(cx, cy, 2.5)
  581 + gc.StrokePath(path)
  582 + gc.FillPath(path)
  583 +
  584 + if len(lines) > 1:
  585 + path = gc.CreatePath()
  586 +
  587 + path.MoveToPoint(*lines[0])
  588 + for p in lines[1::]:
  589 + path.AddLineToPoint(*p)
  590 + gc.StrokePath(path)
  591 +
  592 + txt = u"%.3f mm" % self.GetValue()
  593 + gc.DrawText(txt, *lines[0])
  594 +
562 def GetNumberOfPoints(self): 595 def GetNumberOfPoints(self):
563 return len(self.points) 596 return len(self.points)
564 597
@@ -631,7 +664,7 @@ class LinearMeasure(object): @@ -631,7 +664,7 @@ class LinearMeasure(object):
631 class AngularMeasure(object): 664 class AngularMeasure(object):
632 def __init__(self, colour=(1, 0, 0), representation=None): 665 def __init__(self, colour=(1, 0, 0), representation=None):
633 self.colour = colour 666 self.colour = colour
634 - self.points = [0, 0, 0] 667 + self.points = []
635 self.number_of_points = 0 668 self.number_of_points = 0
636 self.point_actor1 = None 669 self.point_actor1 = None
637 self.point_actor2 = None 670 self.point_actor2 = None
@@ -659,7 +692,7 @@ class AngularMeasure(object): @@ -659,7 +692,7 @@ class AngularMeasure(object):
659 692
660 def SetPoint1(self, x, y, z): 693 def SetPoint1(self, x, y, z):
661 if self.number_of_points == 0: 694 if self.number_of_points == 0:
662 - self.points[0] = (x, y, z) 695 + self.points.append((x, y, z))
663 self.number_of_points = 1 696 self.number_of_points = 1
664 self.point_actor1 = self.representation.GetRepresentation(x, y, z) 697 self.point_actor1 = self.representation.GetRepresentation(x, y, z)
665 else: 698 else:
@@ -678,7 +711,7 @@ class AngularMeasure(object): @@ -678,7 +711,7 @@ class AngularMeasure(object):
678 def SetPoint2(self, x, y, z): 711 def SetPoint2(self, x, y, z):
679 if self.number_of_points == 1: 712 if self.number_of_points == 1:
680 self.number_of_points = 2 713 self.number_of_points = 2
681 - self.points[1] = (x, y, z) 714 + self.points.append((x, y, z))
682 self.point_actor2 = self.representation.GetRepresentation(x, y, z) 715 self.point_actor2 = self.representation.GetRepresentation(x, y, z)
683 else: 716 else:
684 self.points[1] = (x, y, z) 717 self.points[1] = (x, y, z)
@@ -696,7 +729,7 @@ class AngularMeasure(object): @@ -696,7 +729,7 @@ class AngularMeasure(object):
696 def SetPoint3(self, x, y, z): 729 def SetPoint3(self, x, y, z):
697 if self.number_of_points == 2: 730 if self.number_of_points == 2:
698 self.number_of_points = 3 731 self.number_of_points = 3
699 - self.points[2] = (x, y, z) 732 + self.points.append((x, y, z))
700 self.point_actor3 = self.representation.GetRepresentation(x, y, z) 733 self.point_actor3 = self.representation.GetRepresentation(x, y, z)
701 self.CreateMeasure() 734 self.CreateMeasure()
702 else: 735 else:
@@ -794,6 +827,63 @@ class AngularMeasure(object): @@ -794,6 +827,63 @@ class AngularMeasure(object):
794 a.GetPositionCoordinate().SetValue(x,y,z) 827 a.GetPositionCoordinate().SetValue(x,y,z)
795 self.text_actor = a 828 self.text_actor = a
796 829
  830 + def draw_to_canvas(self, gc, viewer):
  831 + """
  832 + Draws to an wx.GraphicsContext.
  833 +
  834 + Parameters:
  835 + gc: is a wx.GraphicsContext
  836 + viewer: the viewer where the canvas is in.
  837 + """
  838 + coord = vtk.vtkCoordinate()
  839 + lines = []
  840 + for p in self.points:
  841 + print p
  842 + coord.SetValue(p)
  843 + cx, cy = coord.GetComputedDisplayValue(viewer.slice_data.renderer)
  844 + cy = -cy
  845 + lines.append((cx, cy))
  846 + path = gc.CreatePath()
  847 + path.AddCircle(cx, cy, 2.5)
  848 + gc.StrokePath(path)
  849 + gc.FillPath(path)
  850 +
  851 + if len(lines) > 1:
  852 + path = gc.CreatePath()
  853 +
  854 + path.MoveToPoint(*lines[0])
  855 + for p in lines[1::]:
  856 + path.AddLineToPoint(*p)
  857 + gc.StrokePath(path)
  858 +
  859 + if len(lines) == 3:
  860 + txt = u"%.3f° / %.3f°" % (self.GetValue(), 360.0 - self.GetValue())
  861 + gc.DrawText(txt, *lines[0])
  862 +
  863 + path = gc.CreatePath()
  864 +
  865 + c = np.array(lines[1])
  866 + v0 = np.array(lines[0]) - c
  867 + v1 = np.array(lines[2]) - c
  868 +
  869 + s0 = np.linalg.norm(v0)
  870 + s1 = np.linalg.norm(v1)
  871 +
  872 + a0 = np.arctan2(v0[1] , v0[0])
  873 + a1 = np.arctan2(v1[1] , v1[0])
  874 +
  875 + if (a1 - a0) % (np.pi*2) < (a0 - a1) % (np.pi*2):
  876 + sa = a0
  877 + ea = a1
  878 + else:
  879 + sa = a1
  880 + ea = a0
  881 +
  882 + path.AddArc((c[0], c[1]), min(s0, s1), sa, ea)
  883 + gc.StrokePath(path)
  884 +
  885 + gc.DrawText(txt, *lines[0])
  886 +
797 def GetNumberOfPoints(self): 887 def GetNumberOfPoints(self):
798 return self.number_of_points 888 return self.number_of_points
799 889
invesalius/data/viewer_slice.py
@@ -217,55 +217,9 @@ class CanvasRendererCTX: @@ -217,55 +217,9 @@ class CanvasRendererCTX:
217 gc.Scale(1, -1) 217 gc.Scale(1, -1)
218 218
219 for (m, mr) in self.viewer.measures.get(self.viewer.orientation, self.viewer.slice_data.number): 219 for (m, mr) in self.viewer.measures.get(self.viewer.orientation, self.viewer.slice_data.number):
220 - lines = []  
221 if not m.visible: 220 if not m.visible:
222 continue 221 continue
223 - for p in m.points:  
224 - coord.SetValue(p)  
225 - cx, cy = coord.GetComputedDisplayValue(self.viewer.slice_data.renderer)  
226 - print (p, cx, cy)  
227 - cy = -cy  
228 - lines.append((cx, cy))  
229 - path = gc.CreatePath()  
230 - path.AddCircle(cx, cy, 2.5)  
231 - gc.StrokePath(path)  
232 - gc.FillPath(path)  
233 -  
234 - if len(lines) > 1:  
235 - path = gc.CreatePath()  
236 -  
237 - path.MoveToPoint(*lines[0])  
238 - for p in lines[1::]:  
239 - path.AddLineToPoint(*p)  
240 - gc.StrokePath(path)  
241 - if m.type == const.ANGULAR:  
242 - txt = u"%.3f° / %.3f" % (m.value, 360.0 - m.value)  
243 -  
244 - if len(lines) == 3:  
245 - path = gc.CreatePath()  
246 -  
247 - c = np.array(lines[1])  
248 - v0 = np.array(lines[0]) - c  
249 - v1 = np.array(lines[2]) - c  
250 -  
251 - s0 = np.linalg.norm(v0)  
252 - s1 = np.linalg.norm(v1)  
253 -  
254 - a0 = np.arctan2(v0[1] , v0[0])  
255 - a1 = np.arctan2(v1[1] , v1[0])  
256 -  
257 - if (a1 - a0) % (np.pi*2) < (a0 - a1) % (np.pi*2):  
258 - sa = a0  
259 - ea = a1  
260 - else:  
261 - sa = a1  
262 - ea = a0  
263 -  
264 - # path.AddArc((c[0], c[1]), min(s0, s1), sa, ea)  
265 - # gc.StrokePath(path)  
266 - else:  
267 - txt = u"%.3f mm" % m.value  
268 - gc.DrawText(txt, *lines[0]) 222 + mr.draw_to_canvas(gc, self.viewer)
269 223
270 gc.Destroy() 224 gc.Destroy()
271 225
@@ -274,10 +228,6 @@ class CanvasRendererCTX: @@ -274,10 +228,6 @@ class CanvasRendererCTX:
274 228
275 self._cv_image.Modified() 229 self._cv_image.Modified()
276 230
277 - # self.canvas_renderer.Render()  
278 -  
279 - # img = self.bitmap.ConvertToImage()  
280 - # img.SaveFile('/tmp/manolo.png', wx.BITMAP_TYPE_PNG)  
281 231
282 class Viewer(wx.Panel): 232 class Viewer(wx.Panel):
283 233