Changeset - 3bc6cb7cfd8b
[Not reviewed]
default
3 1 3
Laman - 10 years ago 2015-08-19 22:18:35

random commit
getting hang of projective geometry and numpy
4 files changed with 88 insertions and 7 deletions:
0 comments (0 inline, 0 general)
.hgignore
Show inline comments
 
^images/
 
\ No newline at end of file
 
^images/
 
^__pycache__/
 
\ No newline at end of file
src/grid.py
Show inline comments
 
file renamed from grid.py to src/grid.py
 
class Grid:
 
  pass
 
\ No newline at end of file
 
import numpy
 
 
## Multiplicates the vector as to set the first nonzero coordinate to 1.
 
def canonize(v):
 
  if v.item(0)!=0: factor=v.item(0)
 
  elif v.item(1)!=0: factor=v.item(1)
 
  elif v.item(2)!=0: factor=v.item(2)
 
  else: factor=1
 
  return v/factor
 
  return [x/factor for x in v]
 
 
def transformPoint(point,A):
 
  # print('#68',numpy.asarray(([1]+point)*A))
 
  x=canonize((A*numpy.matrix([1]+point).transpose()).getA1())
 
  return x[1:]
 
 
class Grid:
 
  
 
  def __init__(self,corners):
 
    # ab
 
    # cd
 
    a,b,c,d=corners
 
    
 
    p1=numpy.cross(a,b)
 
    p2=numpy.cross(c,d)
 
    vanish1=numpy.cross(p1,p2)
 
    
 
    print('#16',p1,p2,vanish1)
 
    
 
    p3=numpy.cross(a,c)
 
    p4=numpy.cross(b,d)
 
    vanish2=numpy.cross(p3,p4)
 
    
 
    print('#32',p3,p4,vanish2)
 
    
 
    horizon=canonize(numpy.cross(vanish1,vanish2))
 
    
 
    # horizon.x+=10
 
    # horizon[1]+=10
 
    
 
    print('#48',horizon)
 
    
 
    rectiMatrix=numpy.matrix([horizon,[0,1,0],[0,0,1]])
 
    rectiMatrixInv=numpy.linalg.inv(rectiMatrix)
 
    
 
    print('#64',rectiMatrixInv)
 
    print('#72',transformPoint([0,0],rectiMatrixInv))
 
    
 
    self.intersections=[[[c,r] for c in range(3)] for r in range(3)]
 
    self.intersections=[[transformPoint(point,rectiMatrixInv) for point in line] for line in self.intersections]
 
    
 
    
 
    # b1=canonize(numpy.cross(horizon,p1))
 
    # b2=canonize(numpy.cross(horizon,p2))
 
    # b3=canonize(numpy.cross(horizon,p3))
 
    # b4=canonize(numpy.cross(horizon,p4))
 
    
 
    # print('#64',b1,b2,b3,b4)
 
    
 
    # self.intersections=[]
 
    # boardSize=4
 
    # for r in range(boardSize):
 
      # self.intersections.append([None]*boardSize)
 
      # rowLine=numpy.cross(((b1*r+b2*(boardSize-1-r)) / (boardSize-1)), vanish1)
 
      # print('#80',rowLine)
 
      # for c in range(boardSize):
 
        # colLine=numpy.cross(((b3*c+b4*(boardSize-1-c)) / (boardSize-1)), vanish2)
 
        # print('#88',colLine)
 
        # self.intersections[r][c]=numpy.cross(rowLine,colLine)
 
        
 
# x=Grid([Vector3(1,0,10),Vector3(1,10,10),Vector3(1,0,0),Vector3(1,10,0)])
 
x=Grid([[1,0,10],[1,7,7],[1,0,0],[1,10,0]])
 
for line in x.intersections:
 
  print('#96',line)
src/gui.py
Show inline comments
 
file renamed from gui.py to src/gui.py
 
@@ -18,25 +18,25 @@ class Application(tk.Frame):
 
    tk.Frame.__init__(self, master)
 
    self.pack()
 
    self.createWidgets()
 
 
  def createWidgets(self):
 
    self.hi_there = tk.Button(self)
 
    self.hi_there["text"] = "Hello World\n(click me)"
 
    self.hi_there["command"] = self.say_hi
 
    self.hi_there.pack(side="top")
 
    
 
    self.canvas=tk.Canvas(self)
 
    self.canvas.configure(width=480,height=360,background="#ff4444")
 
    imgOrig=PIL.Image.open("images/1.jpg")
 
    imgOrig=PIL.Image.open("../images/1.jpg")
 
    self.img=ImageTk.PhotoImage(imgOrig.resize((int(self.canvas['width']),int(self.canvas['height'])),resample=PIL.Image.BILINEAR))
 
    
 
    self.canvas.bind('<1>',lambda e: self.addCorner(e.x,e.y))
 
    self.canvas.create_image(2,2,anchor="nw",image=self.img)
 
    self.canvas.create_line(30,30,40,40,fill="#00ff00")
 
    self.canvas.create_line(30,40,40,30,fill="#00ff00")
 
    
 
    self.canvas.pack()
 
 
    self.QUIT = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
 
    self.QUIT.pack(side="bottom")
 
 
@@ -51,19 +51,20 @@ class Application(tk.Frame):
 
        return
 
    
 
    if len(self.corners)<4: # add a new corner
 
      self.corners.append(a)
 
      return
 
    
 
    index,minDist=0,float('inf') # replace the corner closest to the clicked point
 
    for i,c in enumerate(self.corners):
 
      if a.dist(c)<minDist:
 
        index,minDist=i,a.dist(c)
 
    
 
    self.corners[index]=a
 
    print(self.corners)
 
    
 
  def redrawGrid(self):
 
    pass
 
 
root = tk.Tk()
 
app = Application(master=root)
 
app.mainloop()
src/vector3.py
Show inline comments
 
file renamed from vector3.py to src/vector3.py
 
@@ -58,32 +58,39 @@ class Vector3:
 
    return Vector3(self.x//a, self.y//a, self.z//a)
 
    
 
  def __ifloordiv__(self,a):
 
    self.x//=a
 
    self.y//=a
 
    self.z//=a
 
    return self
 
    
 
  def __xor__(self,v): # vector cross product
 
    return Vector3(self.y*v.z-self.z*v.y, self.z*v.x-self.x*v.z, self.x*v.y-self.y*v.x)
 
    
 
  def __ixor__(self,v):
 
    self.x=self.y*v.z-self.z*v.y
 
    self.y=self.z*v.x-self.x*v.z
 
    self.z=self.x*v.y-self.y*v.x
 
    (self.x, self.y, self.z)=(self.y*v.z-self.z*v.y, self.z*v.x-self.x*v.z, self.x*v.y-self.y*v.x)
 
    return self
 
    
 
  def __abs__(self):
 
    return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z)
 
    
 
  ## Multiplicates the vector as to set the first nonzero coordinate to 1.
 
  def canonize(self):
 
    if self.x!=0: factor=self.x
 
    elif self.y!=0: factor=self.y
 
    elif self.z!=0: factor=self.z
 
    else: factor=1
 
    self/=factor
 
    return self
 
    
 
  def __str__(self):
 
    return str((self.x,self.y,self.z))
 
    
 
  def __repr__(self):
 
    return 'Vector3'+self.__str__()
 
    
 
    
 
class BadOperandError(ArithmeticError):
 
  def __init__(self,u,v,message):
 
    self.u=u
 
    self.v=v
 
    self.message=message
0 comments (0 inline, 0 general)