Changeset - f125391c937d
[Not reviewed]
default
0 4 0
Laman - 6 years ago 2018-12-30 18:47:24

corners: order canonization moved to the add method
4 files changed with 13 insertions and 7 deletions:
0 comments (0 inline, 0 general)
exp/color_sampler.py
Show inline comments
 
@@ -124,7 +124,7 @@ class Sampler:
 
		for c in self.corners:
 
			(x,y)=(c.x,c.y)
 
			self.canvas.create_oval(x-2,y-2,x+2,y+2,fill="#00ff00",tags="mark")
 
		if self.corners.canonizeOrder():
 
		if self.corners.is_canon():
 
			(a,b,c,d)=self.corners
 
			self.canvas.create_line(a.x,a.y,b.x,b.y,fill="#00ff00",tags="mark")
 
			self.canvas.create_line(b.x,b.y,c.x,c.y,fill="#00ff00",tags="mark")
src/analyzer/corners.py
Show inline comments
 
@@ -8,6 +8,8 @@ log=logging.getLogger(__name__)
 
class Corners:
 
	def __init__(self,cornerList=[]):
 
		self._corners=cornerList[:]
 
		self._is_canon=False
 
		self._canonizeOrder()
 

	
 
	## Adds a new corner if there are less than four, replaces the closest otherwise.
 
	def add(self,x,y):
 
@@ -29,6 +31,7 @@ class Corners:
 
				index,minDist=i,a.dist(c)
 

	
 
		self._corners[index]=a
 
		self._canonizeOrder()
 

	
 
	## 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.
 
	#
 
@@ -51,8 +54,8 @@ class Corners:
 
	#  From these pick the one with the lowest slope (dy/dx) and declare its ending point the upper left corner. For the same slope pick the one further left.
 
	#
 
	#  @return True for a convex quadrangle, False for concave and degenerate cases.
 
	def canonizeOrder(self):
 
		if len(self._corners)!=4: return False # erroneus call
 
	def _canonizeOrder(self):
 
		if len(self._corners)!=4: self._is_canon=False
 

	
 
		a,b,c,d=self._corners
 
		abc=doubleTriangleArea(a,b,c)
 
@@ -60,7 +63,7 @@ class Corners:
 
		acd=doubleTriangleArea(a,c,d)
 
		bcd=doubleTriangleArea(b,c,d)
 

	
 
		if any(x==0 for x in (abc,abd,acd,bcd)): return False # collinear degenerate
 
		if any(x==0 for x in (abc,abd,acd,bcd)): self._is_canon=False # collinear degenerate
 

	
 
		swaps=[(1,3),(0,1),(1,2),(0,3),(2,3),(0,0)]
 
		index=(8 if abc>0 else 0)|(4 if abd>0 else 0)|(2 if acd>0 else 0)|(1 if bcd>0 else 0)
 
@@ -82,7 +85,7 @@ class Corners:
 
		self._corners= self._corners[kIndex:] + self._corners[:kIndex] # rotate the upper left corner to the first place
 

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

	
 
	def scale(self,scale):
 
		self._corners=[c * scale for c in self._corners]
 
@@ -93,6 +96,9 @@ class Corners:
 
	def __len__(self):
 
		return len(self._corners)
 

	
 
	def is_canon(self):
 
		return self._is_canon
 

	
 

	
 
## Computes twice the area of the triangle formed by points a,b,c.
 
#
src/analyzer/grid.py
Show inline comments
 
@@ -21,7 +21,7 @@ class Grid:
 
	#
 
	#  The result is stored in grid.intersections, a boardSize*boardSize list with [row][column] coordinates.
 
	#
 
	#  @param corners list of EPoints in ABCD order per corners.Corners.canonizeOrder().
 
	#  @param corners iterable of 4 EPoints in ABCD order per corners.Corners._canonizeOrder().
 
	# !! Needs a check for proper initialization.
 
	def __init__(self,corners):
 
		# ad
src/gui/imgview.py
Show inline comments
 
@@ -63,7 +63,7 @@ class ImgView(ResizableCanvas):
 
	def addCorner(self,x,y):
 
		self._corners.add(x,y)
 
		log.debug("click on %d,%d",x,y)
 
		if self._corners.canonizeOrder():
 
		if self._corners.is_canon():
 
			# transform corners from show coordinates to real coordinates
 
			self._boardGrid=Grid(self._corners)
 
			corners=[self._transformPoint(c) for c in self._corners]
0 comments (0 inline, 0 general)