# Path: geosolver/matfunc.py
# class Vec(Table):
#     def dot( self, otherVec ):  return reduce(operator.add, map(operator.mul, self, otherVec), 0.0)
#     def norm( self ):  return math.sqrt(abs( self.dot(self.conjugate()) ))
#     def normSquared( self ):  return abs( self.dot(self.conjugate()) )
#     def normalize( self ):  return self / self.norm()
#     def outer( self, otherVec ):  return Mat([otherVec*x for x in self])
#     def cross( self, otherVec ):
#         'Compute a Vector or Cross Product with another vector'
#         assert len(self) == len(otherVec) == 3, 'Cross product only defined for 3-D vectors'
#         u, v = self, otherVec
#         return Vec([ u[1]*v[2]-u[2]*v[1], u[2]*v[0]-u[0]*v[2], u[0]*v[1]-u[1]*v[0] ])
#     def house( self, index ):
#         'Compute a Householder vector which zeroes all but the index element after a reflection'
#         v = Vec( Table([0]*index).concat(self[index:]) ).normalize()
#         t = v[index]
#         sigma = 1.0 - t**2
#         if sigma != 0.0:
#             t = v[index] = t<=0 and t-1.0 or -sigma / (t + 1.0)
#             v /= t
#         return v, 2.0 * t**2 / (sigma + t**2)
#     def polyval( self, x ):
#         'Vec([6,3,4]).polyval(5) evaluates to 6*x**2 + 3*x + 4 at x=5'
#         return reduce( lambda cum,c: cum*x+c, self, 0.0 )
#     def ratval( self, x ):
#         'Vec([10,20,30,40,50]).ratfit(5) evaluates to (10*x**2 + 20*x + 30) / (40*x**2 + 50*x + 1) at x=5.'
#         degree = len(self) / 2
#         num, den = self[:degree+1], self[degree+1:] + [1]
#         return num.polyval(x) / den.polyval(x)


from includes import *
from camera import *
from geosolver.matfunc import Vec
		rotAxis = vTo.cross(vFrom)
		d = Vec(vFrom - vTo)
		t = d.norm()/(2.0*1.0)
		phi = 2.0*math.asin(t)
		rotQuat = Quaternion()
		rotQuat.fromAxisAngle(rotAxis, phi)
		rotQuat.normalize()
		return rotQuat

	def mouseWheelEvent(self, event, camera):
		pass

	def mouseReleaseEvent(self, event, camera):
		pass

	def handleFit(self, camera):
		center = camera.getSceneCenter()
		radius = camera.getSceneRadius()
		distance = 0.0
		cameraType = camera.getCameraType()
		if cameraType == CameraType.PERSPECTIVE:
			yView = radius / math.sin(camera.getFieldOfView()/2.0)
			xView = radius / math.sin(camera.getHorizontalFov()/2.0)
			if xView >= yView:
				distance = xView
			else:
				distance = yView
		elif cameraType == CameraType.ORTHOGRAPHIC:
			distance = ((center - camera.getRevolveAroundPoint()) * camera.getViewDirection()) + (radius/camera.orthoCoef)
			

# Path: geosolver/matfunc.py
# class Vec(Table):
#     def dot( self, otherVec ):  return reduce(operator.add, map(operator.mul, self, otherVec), 0.0)
#     def norm( self ):  return math.sqrt(abs( self.dot(self.conjugate()) ))
#     def normSquared( self ):  return abs( self.dot(self.conjugate()) )
#     def normalize( self ):  return self / self.norm()
#     def outer( self, otherVec ):  return Mat([otherVec*x for x in self])
#     def cross( self, otherVec ):
#         'Compute a Vector or Cross Product with another vector'
#         assert len(self) == len(otherVec) == 3, 'Cross product only defined for 3-D vectors'
#         u, v = self, otherVec
#         return Vec([ u[1]*v[2]-u[2]*v[1], u[2]*v[0]-u[0]*v[2], u[0]*v[1]-u[1]*v[0] ])
#     def house( self, index ):
#         'Compute a Householder vector which zeroes all but the index element after a reflection'
#         v = Vec( Table([0]*index).concat(self[index:]) ).normalize()
#         t = v[index]
#         sigma = 1.0 - t**2
#         if sigma != 0.0:
#             t = v[index] = t<=0 and t-1.0 or -sigma / (t + 1.0)
#             v /= t
#         return v, 2.0 * t**2 / (sigma + t**2)
#     def polyval( self, x ):
#         'Vec([6,3,4]).polyval(5) evaluates to 6*x**2 + 3*x + 4 at x=5'
#         return reduce( lambda cum,c: cum*x+c, self, 0.0 )
#     def ratval( self, x ):
#         'Vec([10,20,30,40,50]).ratfit(5) evaluates to (10*x**2 + 20*x + 30) / (40*x**2 + 50*x + 1) at x=5.'
#         degree = len(self) / 2
#         num, den = self[:degree+1], self[degree+1:] + [1]
#         return num.polyval(x) / den.polyval(x)


# Path: workbench/cameraHandler.py
from includes import *
from camera import *
from geosolver.matfunc import Vec

		rotAxis = vTo.cross(vFrom)
		d = Vec(vFrom - vTo)
		t = d.norm()/(2.0*1.0)
		phi = 2.0*math.asin(t)
		rotQuat = Quaternion()
		rotQuat.fromAxisAngle(rotAxis, phi)
		rotQuat.normalize()
		return rotQuat

	def mouseWheelEvent(self, event, camera):
		pass

	def mouseReleaseEvent(self, event, camera):
		pass

	def handleFit(self, camera):
		center = camera.getSceneCenter()
		radius = camera.getSceneRadius()
		distance = 0.0
		cameraType = camera.getCameraType()
		if cameraType == CameraType.PERSPECTIVE:
			yView = radius / math.sin(camera.getFieldOfView()/2.0)
			xView = radius / math.sin(camera.getHorizontalFov()/2.0)
			if xView >= yView:
				distance = xView
			else:
				distance = yView
		elif cameraType == CameraType.ORTHOGRAPHIC:
			distance = ((center - camera.getRevolveAroundPoint()) * camera.getViewDirection()) + (radius/camera.orthoCoef)
			

		position = Vec(center-distance * camera.getViewDirection())