Skip to content

Commit

Permalink
Camera tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
sjackso committed Jul 30, 2019
1 parent 7cbc53e commit 1092c1a
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions src/athena/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ class CameraController:
def createFrom( cls, cc ):
'''Copy constructor'''
ret = cls( cc.window, cc.camera, cc.mesh, cc.split )
ret.camCenter = cc.camCenter
ret.camLoc = cc.camLoc
ret.upVector = cc.upVector
ret.rightVector = cc.rightVector
ret._apply()
if( cc.mesh ):
ret.camCenter = cc.camCenter
ret.camLoc = cc.camLoc
ret.upVector = cc.upVector
ret.rightVector = cc.rightVector
ret._apply()
ret._setProjection()
return ret

def __init__(self, window, camera, mesh, split):
Expand All @@ -38,6 +40,12 @@ def __init__(self, window, camera, mesh, split):
self.aabb = geom.AABB(self.mesh.geometry)
self._setupCamera()

def newMesh( self, mesh ):
self.mesh = mesh
self.aabb = geom.AABB( self.mesh.geometry )
self._setupCamera()
self.reset()

def _windowAspectRatio(self):
w = self.window.width()
h = self.window.height()
Expand Down Expand Up @@ -95,11 +103,11 @@ def pan(self, dx, dy):
delta_x = -dx * self.rightVector
delta_y = dy * self._currentUp()
delta = delta_x + delta_y
delta *= .01
delta *= self._panfactor()
self.camCenter += delta
self.camLoc += delta
self._apply()
self._setProjection()
self._apply()

def rotate( self, dx, dy ):
up = self.upVector
Expand All @@ -120,15 +128,20 @@ def rotate( self, dx, dy ):
self._apply()

def resize(self, ratio=None):
self._setProjection()
if( self.mesh ):
self._setProjection()

class OrthoCamController(CameraController):

def __init__(self, window, camera, geometry, split):
super().__init__(window, camera, geometry, split)
self.margin = 1.4
self.reset()
if( self.mesh ):
self._setupCamera()

def _panfactor(self):
f = self.bounding_radius * self.margin / self.window.width()
return f

def _setProjection(self):
r = self.bounding_radius
Expand All @@ -140,26 +153,35 @@ def zoom( self, dx, dy ):
delta = pow ( 1.1, -dy/100 )
self.margin *= delta
self._setProjection()
self._apply()

class PerspectiveCamController(CameraController):

def __init__(self, window, camera, geometry, split):
super().__init__(window, camera, geometry, split)
self.reset()
self.fov = 50
if( self.mesh ):
self._setupCamera()

def _panfactor(self):
f = self.bounding_radius * ( self.fov / 15 ) / self.window.width()
return f

def _setProjection(self):
frustum_min = self.bounding_radius
frustum_max = 3 * frustum_min
ratio = self._windowAspectRatio()
self.camera.lens().setPerspectiveProjection(50, ratio, frustum_min, frustum_max)
self.camera.lens().setPerspectiveProjection(self.fov, ratio, frustum_min, frustum_max)

def zoom( self, dx, dy ):
delta = dy / 25
fov = self.camera.fieldOfView()
fov = self.fov
def clamp(min_, max_, value):
return min( max( value, min_ ), max_ )
new_fov = clamp (5, 150, fov - delta)
self.camera.setFieldOfView( new_fov )
self.fov = new_fov
self._setProjection()
self._apply()


class OffscreenRenderTarget( Qt3DRender.QRenderTarget ):
Expand Down Expand Up @@ -503,7 +525,6 @@ def __init__(self):
self.renderSettings().setRenderPolicy(self.renderSettings().OnDemand)

self.rootEntity = Qt3DCore.QEntity()
self.camControl = CameraController(None, None, None, False)

self.initParameters() # defined in metaclass
self.faceEnableChanged.connect( self.handleFaceRenderChange )
Expand Down Expand Up @@ -563,6 +584,7 @@ def __init__(self, parent):
self.mouseTool = 'rotate'

self.setDpi( self.screen().physicalDotsPerInch() )
self.camControl = OrthoCamController(self,self.camera(),None,False)

#import IPython
#IPython.embed()
Expand Down Expand Up @@ -655,15 +677,11 @@ def reloadGeom(self, filepath):
self.clearAllGeometry()
self.meshEntity = plymesh.PlyMesh2(self.meshEntityParent, self.plydata)
mesh_3d = self.meshEntity.dimensions == 3
split = self.camControl.split
self.camControl.newMesh(self.meshEntity)
if( mesh_3d ):
self.meshEntity.addComponent(self.gooch_material)
self.camControl = OrthoCamController(self, self.camera(), self.meshEntity, split)
self.setProjOrthographic(1.0)
else:
self.meshEntity.addComponent(self.flat_material)
self.camControl = PerspectiveCamController(self, self.camera(), self.meshEntity, split)
self.setProjOrthographic(0.0)
self.camControl.reset()
return mesh_3d

Expand Down

0 comments on commit 1092c1a

Please sign in to comment.