Changeset - a601f040413e
[Not reviewed]
default
0 2 0
Laman - 6 years ago 2018-12-16 09:33:17

iterable Corners
2 files changed with 32 insertions and 22 deletions:
0 comments (0 inline, 0 general)
src/analyzer/corners.py
Show inline comments
 
from .epoint import EPoint
 
import logging
 

	
 
from .epoint import EPoint
 

	
 
log=logging.getLogger(__name__)
 

	
 

	
 
class Corners:
 
	def __init__(self):
 
		self.corners=[]
 
		self._corners=[]
 

	
 
	## Adds a new corner if there are less than four, replaces the closest otherwise.
 
	def add(self,x,y):
 
@@ -13,18 +17,18 @@ class Corners:
 
				# self.corners[i]=a
 
				# return
 

	
 
		if len(self.corners)<4: # add a new corner
 
			self.corners.append(a)
 
		if len(self._corners)<4: # add a new corner
 
			self._corners.append(a)
 

	
 
		if len(self.corners)<4:
 
		if len(self._corners)<4:
 
			return
 

	
 
		index,minDist=0,float('inf') # replace the corner closest to the clicked point
 
		for i,c in enumerate(self.corners):
 
		for i,c in enumerate(self._corners):
 
			if a.dist(c)<minDist:
 
				index,minDist=i,a.dist(c)
 

	
 
		self.corners[index]=a
 
		self._corners[index]=a
 

	
 
	## Order the corners (0,1,2,3) so they make a quadrangle with vertices KLMN in counter-clockwise order, K being in the upper left.
 
	#
 
@@ -48,9 +52,9 @@ class Corners:
 
	#
 
	#  @return True for a convex quadrangle, False for concave and degenerate cases.
 
	def canonizeOrder(self):
 
		if len(self.corners)!=4: return False # erroneus call
 
		if len(self._corners)!=4: return False # erroneus call
 

	
 
		a,b,c,d=self.corners
 
		a,b,c,d=self._corners
 
		abc=doubleTriangleArea(a,b,c)
 
		abd=doubleTriangleArea(a,b,d)
 
		acd=doubleTriangleArea(a,c,d)
 
@@ -63,24 +67,31 @@ class Corners:
 
		if index%3!=0: return False # concave degenerate
 
		swap=swaps[index//3]
 

	
 
		self.corners[swap[0]], self.corners[swap[1]] = self.corners[swap[1]], self.corners[swap[0]] # counter-clockwise order
 
		self._corners[swap[0]], self._corners[swap[1]] = self._corners[swap[1]], self._corners[swap[0]] # counter-clockwise order
 

	
 
		kIndex=None
 
		lowestSlope=float("inf")
 

	
 
		for i,corner in enumerate(self.corners): # find the NK edge: going right-left with the lowest slope, secondarily the one going down
 
		for i,corner in enumerate(self._corners): # find the NK edge: going right-left with the lowest slope, secondarily the one going down
 
			ii=(i+1)%4
 
			slope=abs(getSlope(corner,self.corners[ii]))
 
			if corner.x>self.corners[ii].x and (slope<lowestSlope or (slope==lowestSlope and corner.y<self.corners[ii].y)):
 
			slope=abs(getSlope(corner, self._corners[ii]))
 
			if corner.x>self._corners[ii].x and (slope < lowestSlope or (slope == lowestSlope and corner.y < self._corners[ii].y)):
 
				kIndex=ii
 
				lowestSlope=slope
 

	
 
		self.corners=self.corners[kIndex:]+self.corners[:kIndex] # rotate the upper left corner to the first place
 
		self._corners= self._corners[kIndex:] + self._corners[:kIndex] # rotate the upper left corner to the first place
 

	
 
		log.debug(self._corners)
 
		return True # success
 

	
 
	def scale(self,scale):
 
		self.corners=[c*scale for c in self.corners]
 
		self._corners=[c * scale for c in self._corners]
 

	
 
	def __iter__(self):
 
		return iter(self._corners)
 

	
 
	def __len__(self):
 
		return len(self._corners)
 

	
 

	
 
## Computes twice the area of the triangle formed by points a,b,c.
src/gui/imgview.py
Show inline comments
 
@@ -37,8 +37,8 @@ class ImgView(ResizableCanvas):
 
			self._tkImg=ImageTk.PhotoImage(img) # just to save the image from garbage collector
 
			self.create_image(self._width//2, self._height//2, anchor="center", image=self._tkImg)
 

	
 
		for corner in self._corners.corners:
 
			self.markPoint(corner.x,corner.y)
 
		for c in self._corners:
 
			self.markPoint(c.x,c.y)
 

	
 
		if self._boardGrid!=None and config.gui.showGrid:
 
			for r in range(19):
 
@@ -65,9 +65,8 @@ class ImgView(ResizableCanvas):
 
		log.debug("click on %d,%d",x,y)
 
		if self._corners.canonizeOrder():
 
			# transform corners from show coordinates to real coordinates
 
			log.debug(self._corners.corners)
 
			self._boardGrid=Grid(self._corners.corners)
 
			corners=[self._transformPoint(c) for c in self._corners.corners]
 
			self._boardGrid=Grid(self._corners)
 
			corners=[self._transformPoint(c) for c in self._corners]
 
			self._gui.detector.setCorners(corners)
 
			self._gui.preview()
 

	
 
@@ -91,8 +90,8 @@ class ImgView(ResizableCanvas):
 
		w=self._width
 
		super()._onResize(event)
 
		self._corners.scale(self._width/w)
 
		if len(self._corners.corners)==4:
 
			self._boardGrid=Grid(self._corners.corners)
 
		if len(self._corners)==4:
 
			self._boardGrid=Grid(self._corners)
 
		self.redraw()
 

	
 
	def _transformPoint(self,point):
0 comments (0 inline, 0 general)