# Enable parallel projection without resetting the camera

**URL:** https://discourse.vtk.org/t/enable-parallel-projection-without-resetting-the-camera/5289
**Category:** Development
**Created:** [March 1, 2021, 8:54pm UTC](https://discourse.vtk.org/t/enable-parallel-projection-without-resetting-the-camera/5289 "2021-03-01T20:54:04Z")
**Posts on this page:** 1
**Showing post:** 8

<div class="post-metadata">

### Author: ![rodrigomologni](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@rodrigomologni](https://discourse.vtk.org/u/rodrigomologni)
#### Post date: [March 6, 2021, 12:29pm UTC](https://discourse.vtk.org/t/enable-parallel-projection-without-resetting-the-camera/5289/8 "2021-03-06T12:29:38Z")

</div>

### To: @dgobbi

Dear Gobbi,

> [@dgobbi](#):
>
> ```auto
> ParallelScale = 2*(Distance*tan(0.5*ViewAngle))
> 
> ```

The equation you provided is producing different results than VTK. I found another equation in the VTK code that solved the problem:

```auto
ParallelScale = Distance * sin(0.5*ViewAngle)

```

Thank you very much for your help!

### To: @lassoan

Dear Lasso,

> [@lassoan](#):
>
> These two projections are completely different, so you cannot keep camera parameters when switching.

I solved the problem by setting the position and the parallel projection scale of the active camera.

> [@lassoan](#):
>
> What I meant is that parallel and perspective projections are fundamentally different, so it is not possible to compute parameters that results in generally equivalent views.

@dgobbi is right! There is a geometric relationship between parallel and perspective projections. I used this relationship to enable/disable the parallel projection of a same camera without producing the “reset camera” effect.

> [@lassoan](#):
>
> Probably it is not a bug.

You’re right! It’s not a bug. However, I see this problem as an unwanted effect that could be solved within the VTK.

Thank you for your help!

### Solution

> **parallel\_projection\_bug\_solved.py**
>
> ```python
> import vtk
> import math
> 
> def keyboard_callback(caller, event):
> if caller.GetKeyCode() == 'p':
> camera = renderer.GetActiveCamera()
> angle = camera.GetViewAngle()
> angle = math.radians(angle)
> if camera.GetParallelProjection():
> point = camera.GetFocalPoint()
> direction = camera.GetDirectionOfProjection()
> scale = camera.GetParallelScale()
> distance = scale / math.sin(.5 * angle)
> x = point[0] - distance * direction[0]
> y = point[1] - distance * direction[1]
> z = point[2] - distance * direction[2]
> camera.SetPosition(x, y, z)
> camera.ParallelProjectionOff()
> renderer.ResetCameraClippingRange()
> else:
> distance = camera.GetDistance()
> scale = distance * math.sin(.5 * angle)
> camera.SetParallelScale(scale)
> camera.ParallelProjectionOn()
> window.Render()
> 
> source = vtk.vtkSphereSource()
> 
> mapper = vtk.vtkPolyDataMapper()
> mapper.SetInputConnection(source.GetOutputPort())
> 
> actor = vtk.vtkActor()
> actor.SetMapper(mapper)
> actor.GetProperty().EdgeVisibilityOn()
> 
> renderer = vtk.vtkRenderer()
> renderer.AddActor(actor)
> renderer.SetBackground(.3, .3, .3)
> renderer.GetActiveCamera().ParallelProjectionOn()
> renderer.ResetCamera()
> 
> window = vtk.vtkRenderWindow()
> window.AddRenderer(renderer)
> window.SetSize(800, 600)
> 
> interactor = vtk.vtkRenderWindowInteractor()
> interactor.AddObserver('KeyPressEvent', keyboard_callback)
> interactor.SetRenderWindow(window)
> interactor.SetInteractorStyle(vtk.vtkInteractorStyleRubberBandPick())
> interactor.Start()
> 
> ```

![Untitled](https://discourse.vtk.org/uploads/default/original/2X/7/71ee8575be4dc2b10ed03b5c28ba4eb620603056.gif)

![Untitled](https://discourse.vtk.org/uploads/default/original/2X/3/3d5a1cbbcbddcee3e72038cf3b777c06d4a05133.gif)

---

_[View the full topic](https://discourse.vtk.org/t/enable-parallel-projection-without-resetting-the-camera/5289)._
