File: vtkOpenGLPropItem.cxx

package info (click to toggle)
paraview 5.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 497,236 kB
  • sloc: cpp: 3,171,290; ansic: 1,315,072; python: 134,290; xml: 103,324; sql: 65,887; sh: 5,286; javascript: 4,901; yacc: 4,383; java: 3,977; perl: 2,363; lex: 1,909; f90: 1,255; objc: 143; makefile: 119; tcl: 59; pascal: 50; fortran: 29
file content (167 lines) | stat: -rw-r--r-- 5,652 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkOpenGLPropItem.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtkOpenGLPropItem.h"

#include "vtkCamera.h"
#include "vtkContext2D.h"
#include "vtkContextScene.h"
#include "vtkMatrix4x4.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLContextDevice2D.h"
#include "vtkProp3D.h"
#include "vtkRenderer.h"
#include "vtkTransform.h"

VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkOpenGLPropItem);

void vtkOpenGLPropItem::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

vtkOpenGLPropItem::vtkOpenGLPropItem() = default;

vtkOpenGLPropItem::~vtkOpenGLPropItem() = default;

void vtkOpenGLPropItem::UpdateTransforms()
{
  vtkContextDevice2D* dev = this->Painter->GetDevice();
  vtkOpenGLContextDevice2D* glDev = vtkOpenGLContextDevice2D::SafeDownCast(dev);
  if (!glDev)
  {
    vtkErrorMacro(<< "Context device is not vtkOpenGLContextDevice2D.");
    return;
  }

  // Get the active camera:
  vtkRenderer* ren = this->Scene->GetRenderer();
  vtkCamera* activeCamera = ren->GetActiveCamera();

  // Cache the current state:
  this->CameraCache->DeepCopy(activeCamera);

  // Reset the info that computes the view:
  vtkNew<vtkTransform> identity;
  identity->Identity();
  activeCamera->SetUserViewTransform(identity);
  activeCamera->SetFocalPoint(0.0, 0.0, 0.0);
  activeCamera->SetPosition(0.0, 0.0, 1.0);
  activeCamera->SetViewUp(0.0, 1.0, 0.0);

  // Update the camera model matrix with the current context2D modelview matrix:
  double mv[16];
  double* glMv = glDev->GetModelMatrix()->Element[0];
  std::copy(glMv, glMv + 16, mv);
  activeCamera->SetModelTransformMatrix(mv);

  /* The perspective updates aren't nearly as straight-forward, and take a bit
   * of code-spelunking and algebra. By inspecting the following methods, we
   * see how the perspective matrix gets built at render-time:
   *
   * 1) vtkOpenGLCamera::Render() calls
   *    vtkCamera::GetProjectionTransformMatrix() with zRange = [-1, 1] and
   *    aspect = aspectModification * usize / vsize (see below).
   * 2) vtkCamera::GetProjectionTransformMatrix() calls
   *    vtkCamera::ComputeProjectionTransform with the same arguments.
   * 3) vtkCamera::ComputeProjectionTransform calls
   *    vtkPerspectiveTransform::Ortho with:
   *    xminGL = (vtkCamera::WindowCenter[0] - 1) * vtkCamera::ParallelScale * aspect
   *    xmaxGL = (vtkCamera::WindowCenter[0] + 1) * vtkCamera::ParallelScale * aspect
   *    yminGL = (vtkCamera::WindowCenter[1] - 1) * vtkCamera::ParallelScale
   *    ymaxGL = (vtkCamera::WindowCenter[1] + 1) * vtkCamera::ParallelScale
   *    zminGL = vtkCamera::ClippingRange[0]
   *    zmaxGL = vtkCamera::ClippingRange[1]
   *
   * In vtkOpenGLContext2D::Begin, glOrtho is called with:
   *    xminCTX = 0.5
   *    xmaxCTX = glViewport[0] - 0.5
   *    yminCTX = 0.5
   *    ymaxCTX = glViewport[1] - 0.5
   *    zminCTX = -2000
   *    zmaxCTX = 2000
   *
   * To set the camera parameters to reproduce the Context2D projective matrix,
   * the following set of equations can be built:
   *
   * Using:
   *   Cx = vtkCamera::WindowCenter[0] (unknown)
   *   Cy = vtkCamera::WindowCenter[1] (unknown)
   *   P = vtkCamera::ParallelScale (unknown)
   *   a = aspect (known)
   *
   * The equations are:
   *   xminCTX = (Cx - 1)aP
   *   xmaxCTX = (Cx + 1)aP
   *   yminCTX = (Cy - 1)P
   *   ymaxCTX = (Cy + 1)P
   *
   * Solving simultaneously for the unknowns Cx, Cy, and P, we get:
   *   Cx = (xminCTX * a) / (xmaxCTX - xminCTX) + 1
   *   Cy = a * (yminCTX + ymaxCTX) / (xmaxCTX - xminCTX)
   *   P = (xmaxCTX - xminCTX) / (2 * a)
   */

  // Collect the parameters to compute the projection matrix:
  // (see vtkOpenGLCamera::Render)
  int lowerLeft[2];
  int usize, vsize;
  double aspect1[2];
  double aspect2[2];
  vtkRecti vp = glDev->GetViewportRect();
  ren->GetTiledSizeAndOrigin(&usize, &vsize, lowerLeft, lowerLeft + 1);
  ren->ComputeAspect();
  ren->GetAspect(aspect1);
  ren->vtkViewport::ComputeAspect();
  ren->vtkViewport::GetAspect(aspect2);
  double aspectModification = (aspect1[0] * aspect2[1]) / (aspect1[1] * aspect2[0]);

  // Set the variables for the equations:
  double a = aspectModification * usize / vsize;
  double xminCTX = 0.5;
  double xmaxCTX = vp[2] - 0.5;
  double yminCTX = 0.5;
  double ymaxCTX = vp[3] - 0.5;
  double zminCTX = -2000;
  double zmaxCTX = 2000;

  double Cx = (xminCTX * a) / (xmaxCTX - xminCTX) + 1.;
  double Cy = a * (yminCTX + ymaxCTX) / (xmaxCTX - xminCTX);
  double P = (xmaxCTX - xminCTX) / (2 * a);

  // Set the camera state
  activeCamera->SetParallelProjection(1);
  activeCamera->SetParallelScale(P);
  activeCamera->SetWindowCenter(Cx, Cy);
  activeCamera->SetClippingRange(zminCTX, zmaxCTX);
}

void vtkOpenGLPropItem::ResetTransforms()
{
  // Reset the active camera:
  vtkCamera* activeCamera = this->Scene->GetRenderer()->GetActiveCamera();
  activeCamera->DeepCopy(this->CameraCache);
}

bool vtkOpenGLPropItem::Paint(vtkContext2D* painter)
{
  this->Painter = painter;
  bool result = this->Superclass::Paint(painter);
  this->Painter = nullptr;

  return result;
}
VTK_ABI_NAMESPACE_END