forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkCamera.cxx
1854 lines (1581 loc) · 54.5 KB
/
vtkCamera.cxx
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCamera.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 "vtkCamera.h"
#include "vtkMath.h"
#include "vtkTimeStamp.h"
#include "vtkObjectFactory.h"
#include "vtkPerspectiveTransform.h"
#include "vtkTransform.h"
#include "vtkCallbackCommand.h"
#include "vtkRenderer.h"
#include <cmath>
//----------------------------------------------------------------------------
// Needed when we don't use the vtkStandardNewMacro.
vtkAbstractObjectFactoryNewMacro(vtkCamera)
vtkCxxSetObjectMacro(vtkCamera, EyeTransformMatrix, vtkMatrix4x4);
vtkCxxSetObjectMacro(vtkCamera, ModelTransformMatrix, vtkMatrix4x4);
//-----------------------------------------------------------------------------
class vtkCameraCallbackCommand : public vtkCommand
{
public:
static vtkCameraCallbackCommand *New()
{ return new vtkCameraCallbackCommand; }
vtkCamera *Self;
void Execute(vtkObject *, unsigned long, void *) VTK_OVERRIDE
{
if (this->Self)
{
this->Self->Modified();
this->Self->ComputeViewTransform();
this->Self->ComputeDistance();
this->Self->ComputeCameraLightTransform();
}
}
protected:
vtkCameraCallbackCommand() { this->Self = NULL; }
~vtkCameraCallbackCommand() VTK_OVERRIDE {}
};
//----------------------------------------------------------------------------
// Construct camera instance with its focal point at the origin,
// and position=(0,0,1). The view up is along the y-axis,
// view angle is 30 degrees, and the clipping range is (.1,1000).
vtkCamera::vtkCamera()
{
this->FocalPoint[0] = 0.0;
this->FocalPoint[1] = 0.0;
this->FocalPoint[2] = 0.0;
this->Position[0] = 0.0;
this->Position[1] = 0.0;
this->Position[2] = 1.0;
this->ViewUp[0] = 0.0;
this->ViewUp[1] = 1.0;
this->ViewUp[2] = 0.0;
this->DirectionOfProjection[0] = 0.0;
this->DirectionOfProjection[1] = 0.0;
this->DirectionOfProjection[2] = 0.0;
this->ViewAngle = 30.0;
this->UseHorizontalViewAngle = 0;
this->UseOffAxisProjection = 0;
this->ScreenBottomLeft[0] = -0.5;
this->ScreenBottomLeft[1] = -0.5;
this->ScreenBottomLeft[2] = -0.5;
this->ScreenBottomRight[0] = 0.5;
this->ScreenBottomRight[1] = -0.5;
this->ScreenBottomRight[2] = -0.5;
this->ScreenTopRight[0] = 0.5;
this->ScreenTopRight[1] = 0.5;
this->ScreenTopRight[2] = -0.5;
this->EyeSeparation = 0.06;
this->WorldToScreenMatrix = vtkMatrix4x4::New();
this->WorldToScreenMatrix->Identity();
this->EyeTransformMatrix = vtkMatrix4x4::New();
this->EyeTransformMatrix->Identity();
this->ModelTransformMatrix = vtkMatrix4x4::New();
this->ModelTransformMatrix->Identity();
this->ClippingRange[0] = 0.01;
this->ClippingRange[1] = 1000.01;
this->Thickness = 1000.0;
this->ParallelProjection = 0;
this->ParallelScale = 1.0;
this->EyeAngle = 2.0;
this->Stereo = 0;
this->LeftEye = 1;
this->WindowCenter[0] = 0.0;
this->WindowCenter[1] = 0.0;
this->ViewShear[0] = 0.0;
this->ViewShear[1] = 0.0;
this->ViewShear[2] = 1.0;
this->FocalDisk = 1.0;
this->Transform = vtkPerspectiveTransform::New();
this->ViewTransform = vtkTransform::New();
this->ProjectionTransform = vtkPerspectiveTransform::New();
this->CameraLightTransform = vtkTransform::New();
this->ModelViewTransform = vtkTransform::New();
this->UserTransform = NULL;
this->UserViewTransform = NULL;
this->UserViewTransformCallbackCommand = NULL;
// initialize the ViewTransform
this->ComputeViewTransform();
this->ComputeDistance();
this->ComputeCameraLightTransform();
this->FreezeFocalPoint = false;
this->UseScissor = false;
}
//----------------------------------------------------------------------------
vtkCamera::~vtkCamera()
{
this->WorldToScreenMatrix->Delete();
this->WorldToScreenMatrix = NULL;
this->EyeTransformMatrix->Delete();
this->EyeTransformMatrix = NULL;
this->ModelTransformMatrix->Delete();
this->ModelTransformMatrix = NULL;
this->Transform->Delete();
this->ViewTransform->Delete();
this->ProjectionTransform->Delete();
this->CameraLightTransform->Delete();
this->ModelViewTransform->Delete();
if (this->UserTransform)
{
this->UserTransform->UnRegister(this);
this->UserTransform = NULL;
}
if (this->UserViewTransform)
{
this->UserViewTransform->RemoveObserver(
this->UserViewTransformCallbackCommand);
this->UserViewTransform->UnRegister(this);
this->UserViewTransform = NULL;
}
if (this->UserViewTransformCallbackCommand)
{
this->UserViewTransformCallbackCommand->Delete();
}
}
//----------------------------------------------------------------------------
void vtkCamera::SetScissorRect(vtkRecti scissorRect)
{
this->ScissorRect = scissorRect;
}
//----------------------------------------------------------------------------
void vtkCamera::GetScissorRect(vtkRecti& scissorRect)
{
scissorRect = this->ScissorRect;
}
//----------------------------------------------------------------------------
// The first set of methods deal exclusively with the ViewTransform, which
// is the only transform which is set up entirely in the camera. The
// perspective transform must be set up by the Renderer because the
// Camera doesn't know the Renderer's aspect ratio.
//----------------------------------------------------------------------------
void vtkCamera::SetPosition(double x, double y, double z)
{
if (x == this->Position[0] &&
y == this->Position[1] &&
z == this->Position[2])
{
return;
}
this->Position[0] = x;
this->Position[1] = y;
this->Position[2] = z;
vtkDebugMacro(<< " Position set to ( " << this->Position[0] << ", "
<< this->Position[1] << ", " << this->Position[2] << ")");
this->ComputeViewTransform();
// recompute the focal distance
this->ComputeDistance();
this->ComputeCameraLightTransform();
this->Modified();
}
//----------------------------------------------------------------------------
void vtkCamera::SetUserTransform(vtkHomogeneousTransform *transform)
{
if (transform == this->UserTransform)
{
return;
}
if (this->UserTransform)
{
this->UserTransform->Delete();
this->UserTransform = NULL;
}
if (transform)
{
this->UserTransform = transform;
this->UserTransform->Register(this);
}
this->Modified();
}
//----------------------------------------------------------------------------
void vtkCamera::SetUserViewTransform(vtkHomogeneousTransform *transform)
{
if (transform == this->UserViewTransform)
{
return;
}
if (this->UserViewTransform)
{
this->UserViewTransform->RemoveObserver(
this->UserViewTransformCallbackCommand);
this->UserViewTransform->Delete();
this->UserViewTransform = NULL;
}
if (transform)
{
this->UserViewTransform = transform;
this->UserViewTransform->Register(this);
if (!this->UserViewTransformCallbackCommand)
{
this->UserViewTransformCallbackCommand = vtkCameraCallbackCommand::New();
this->UserViewTransformCallbackCommand->Self = this;
}
this->UserViewTransform->AddObserver(
vtkCommand::ModifiedEvent,
this->UserViewTransformCallbackCommand);
}
this->Modified();
this->ComputeViewTransform();
this->ComputeDistance();
this->ComputeCameraLightTransform();
}
//----------------------------------------------------------------------------
void vtkCamera::SetFocalPoint(double x, double y, double z)
{
if (x == this->FocalPoint[0] &&
y == this->FocalPoint[1] &&
z == this->FocalPoint[2])
{
return;
}
this->FocalPoint[0] = x;
this->FocalPoint[1] = y;
this->FocalPoint[2] = z;
vtkDebugMacro(<< " FocalPoint set to ( " << this->FocalPoint[0] << ", " << this->FocalPoint[1] << ", " << this->FocalPoint[2] << ")");
this->ComputeViewTransform();
// recompute the focal distance
this->ComputeDistance();
this->ComputeCameraLightTransform();
this->Modified();
}
//----------------------------------------------------------------------------
void vtkCamera::SetViewUp(double x, double y, double z)
{
// normalize ViewUp, but do _not_ orthogonalize it by default
double norm = sqrt(x*x + y*y + z*z);
if(norm != 0)
{
x /= norm;
y /= norm;
z /= norm;
}
else
{
x = 0;
y = 1;
z = 0;
}
if (x == this->ViewUp[0] &&
y == this->ViewUp[1] &&
z == this->ViewUp[2])
{
return;
}
this->ViewUp[0] = x;
this->ViewUp[1] = y;
this->ViewUp[2] = z;
vtkDebugMacro(<< " ViewUp set to ( " << this->ViewUp[0] << ", "
<< this->ViewUp[1] << ", " << this->ViewUp[2] << ")");
this->ComputeViewTransform();
this->ComputeCameraLightTransform();
this->Modified();
}
//----------------------------------------------------------------------------
// The ViewTransform depends on only three ivars: the Position, the
// FocalPoint, and the ViewUp vector. All the other methods are there
// simply for the sake of the users' convenience.
void vtkCamera::ComputeViewTransform()
{
// main view through the camera
this->Transform->Identity();
if (this->UserViewTransform)
{
this->Transform->Concatenate(this->UserViewTransform);
}
this->Transform->SetupCamera(this->Position, this->FocalPoint, this->ViewUp);
this->ViewTransform->SetMatrix(this->Transform->GetMatrix());
}
//----------------------------------------------------------------------------
void vtkCamera::ComputeCameraLightTransform()
{
vtkTransform *t;
double d;
// assumes a valid view transform and valid camera distance
t = this->CameraLightTransform;
t->Identity();
t->SetMatrix(this->ViewTransform->GetMatrix());
t->Inverse();
d = this->Distance;
t->Scale(d, d, d);
t->Translate(0.0, 0.0, -1.0);
}
//----------------------------------------------------------------------------
void vtkCamera::ComputeWorldToScreenMatrix()
{
// Avoid recalculating screen orientation if we don't need to.
if(this->WorldToScreenMatrixMTime.GetMTime() < this->GetMTime())
{
double xAxis[3];
double yAxis[3];
double zAxis[3];
for(int i = 0; i < 3; ++i)
{
xAxis[i] = this->ScreenBottomRight[i] - this->ScreenBottomLeft[i];
yAxis[i] = this->ScreenTopRight[i] - this->ScreenBottomRight[i];
}
vtkMath::Normalize(xAxis);
vtkMath::Normalize(yAxis);
vtkMath::Cross(xAxis, yAxis, zAxis);
vtkMath::Normalize(zAxis);
// Setting individual elements of the matrix.
// Make it column major and then invert it to make sure the translation is correct
// This is using column major (vectors are copied into the column)
// \Note: while the initial element assignments are made in column-major
// ordering, the matrix will be inverted, resulting in a row-major
// matrix that provides the transformation from World to Screen space.
this->WorldToScreenMatrix->SetElement(0, 0, xAxis[0]);
this->WorldToScreenMatrix->SetElement(1, 0, xAxis[1]);
this->WorldToScreenMatrix->SetElement(2, 0, xAxis[2]);
this->WorldToScreenMatrix->SetElement(0, 1, yAxis[0]);
this->WorldToScreenMatrix->SetElement(1, 1, yAxis[1]);
this->WorldToScreenMatrix->SetElement(2, 1, yAxis[2]);
this->WorldToScreenMatrix->SetElement(0, 2, zAxis[0]);
this->WorldToScreenMatrix->SetElement(1, 2, zAxis[1]);
this->WorldToScreenMatrix->SetElement(2, 2, zAxis[2]);
this->WorldToScreenMatrix->SetElement(0, 3, this->ScreenBottomLeft[0]);
this->WorldToScreenMatrix->SetElement(1, 3, this->ScreenBottomLeft[1]);
this->WorldToScreenMatrix->SetElement(2, 3, this->ScreenBottomLeft[2]);
this->WorldToScreenMatrix->SetElement(3, 3, 1.0);
// The reason for doing this as an Invert as the goal here is to put
// the translation through the rotation that we've just assigned ie.
// the translation has to be put into screen space too.
this->WorldToScreenMatrix->Invert();
this->WorldToScreenMatrixMTime.Modified();
}
}
//----------------------------------------------------------------------------
void vtkCamera::ComputeOffAxisProjectionFrustum()
{
this->ComputeWorldToScreenMatrix();
// \NOTE: Varibles names reflect naming convention used in
// "High Resolution Virtual Reality", in Proc.
// SIGGRAPH '92, Computer Graphics, pages 195-202, 1992.
// OffAxis calculations.
// vtkMatrix::MultiplyPoint expect homogeneous coordinate.
double E[4] = {0.0, 0.0, 0.0, 1.0};
double L[4] = {this->ScreenBottomLeft[0], this->ScreenBottomLeft[1], this->ScreenBottomLeft[2], 1.0};
double H[4] = {this->ScreenTopRight[0], this->ScreenTopRight[1], this->ScreenTopRight[2], 1.0};
double eyeSeparationCorrectionFactor = 10.0;
double shiftDistance = this->EyeSeparation / (2.0 * eyeSeparationCorrectionFactor);
if(this->Distance < 1.0)
{
shiftDistance *= this->Distance;
}
if(this->LeftEye)
{
E[0] -= shiftDistance;
}
else
{
E[0] += shiftDistance;
}
// First transform the eye to new position.
this->EyeTransformMatrix->MultiplyPoint(E, E);
// Now transform the eye and screen corner points into the screen
// coordinate system.
this->WorldToScreenMatrix->MultiplyPoint(E, E);
this->WorldToScreenMatrix->MultiplyPoint(H, H);
this->WorldToScreenMatrix->MultiplyPoint(L, L);
double matrix[4][4];
double width = H[0] - L[0];
double height = H[1] - L[1];
// Back and front are not traditional near and far.
// Front (aka near)
double F = E[2] - (this->Distance + this->Thickness);//E[2] - 10000.0;//this->ClippingRange[1];
// Back (aka far)
double nearDistanceCorrectionFactor = 1000.0;
double B = E[2] - (this->Distance / nearDistanceCorrectionFactor);//E[2] - .1;//this->ClippingRange[0];
double depth = B - F;
matrix[0][0] = ( 2*E[2] ) / width;
matrix[1][0] = 0;
matrix[2][0] = 0;
matrix[3][0] = 0;
matrix[0][1] = 0;
matrix[1][1] = ( 2*E[2] )/ height;
matrix[2][1] = 0;
matrix[3][1] = 0;
matrix[0][2] = ( H[0]+L[0] - 2*E[0] )/width;
matrix[1][2] = ( H[1]+L[1] - 2*E[1] )/height;
matrix[2][2] = ( B+F-2*E[2] )/depth;
matrix[3][2] = -1;
matrix[0][3] = ( -E[2]*( H[0]+L[0] ) )/width;
matrix[1][3] = ( -E[2]*( H[1]+L[1] ) )/height;
matrix[2][3] = B-E[2]- ( B *( B+F - 2*E[2] )/depth );
matrix[3][3] = E[2];
for ( int i=0; i<4; i++ )
{
for ( int j=0; j<4; j++ )
{
this->ProjectionTransform->GetMatrix()->SetElement( i,j, matrix[i][j] ) ;
}
}
// Now move the world into display space.
vtkMatrix4x4::Multiply4x4(this->ProjectionTransform->GetMatrix(), this->WorldToScreenMatrix,
this->ProjectionTransform->GetMatrix());
}
//----------------------------------------------------------------------------
void vtkCamera::ComputeModelViewMatrix()
{
if(this->ModelViewTransform->GetMTime() < this->ModelTransformMatrix->GetMTime() ||
this->ModelViewTransform->GetMTime() < this->ViewTransform->GetMTime())
{
vtkMatrix4x4::Multiply4x4(this->ViewTransform->GetMatrix(), this->ModelTransformMatrix,
this->ModelViewTransform->GetMatrix());
}
}
//----------------------------------------------------------------------------
void vtkCamera::OrthogonalizeViewUp()
{
// the orthogonalized ViewUp is just the second row of the view matrix
vtkMatrix4x4 *matrix = this->ViewTransform->GetMatrix();
this->ViewUp[0] = matrix->GetElement(1,0);
this->ViewUp[1] = matrix->GetElement(1,1);
this->ViewUp[2] = matrix->GetElement(1,2);
this->Modified();
}
//----------------------------------------------------------------------------
// Set the distance of the focal point from the camera. The focal point is
// modified accordingly. This should be positive.
void vtkCamera::SetDistance(double d)
{
if (this->Distance == d)
{
return;
}
this->Distance = d;
// Distance should be greater than .0002
if (this->Distance < 0.0002)
{
this->Distance = 0.0002;
vtkDebugMacro(<< " Distance is set to minimum.");
}
// we want to keep the camera pointing in the same direction
double *vec = this->DirectionOfProjection;
// recalculate FocalPoint
this->FocalPoint[0] = this->Position[0] + vec[0]*this->Distance;
this->FocalPoint[1] = this->Position[1] + vec[1]*this->Distance;
this->FocalPoint[2] = this->Position[2] + vec[2]*this->Distance;
vtkDebugMacro(<< " Distance set to ( " << this->Distance << ")");
this->ComputeViewTransform();
this->ComputeCameraLightTransform();
this->Modified();
}
//----------------------------------------------------------------------------
// This method must be called when the focal point or camera position changes
void vtkCamera::ComputeDistance()
{
double dx = this->FocalPoint[0] - this->Position[0];
double dy = this->FocalPoint[1] - this->Position[1];
double dz = this->FocalPoint[2] - this->Position[2];
this->Distance = sqrt(dx*dx + dy*dy + dz*dz);
if (this->Distance < 1e-20)
{
this->Distance = 1e-20;
vtkDebugMacro(<< " Distance is set to minimum.");
double *vec = this->DirectionOfProjection;
// recalculate FocalPoint
this->FocalPoint[0] = this->Position[0] + vec[0]*this->Distance;
this->FocalPoint[1] = this->Position[1] + vec[1]*this->Distance;
this->FocalPoint[2] = this->Position[2] + vec[2]*this->Distance;
}
this->DirectionOfProjection[0] = dx/this->Distance;
this->DirectionOfProjection[1] = dy/this->Distance;
this->DirectionOfProjection[2] = dz/this->Distance;
this->ComputeViewPlaneNormal();
}
//----------------------------------------------------------------------------
// Move the position of the camera along the view plane normal. Moving
// towards the focal point (e.g., > 1) is a dolly-in, moving away
// from the focal point (e.g., < 1) is a dolly-out.
void vtkCamera::Dolly(double amount)
{
if (amount <= 0.0)
{
return;
}
// dolly moves the camera towards the focus
double d = this->Distance/amount;
this->SetPosition(this->FocalPoint[0] - d*this->DirectionOfProjection[0],
this->FocalPoint[1] - d*this->DirectionOfProjection[1],
this->FocalPoint[2] - d*this->DirectionOfProjection[2]);
}
//----------------------------------------------------------------------------
// Set the roll angle of the camera about the direction of projection
void vtkCamera::SetRoll(double roll)
{
// roll is a rotation of camera view up about the direction of projection
vtkDebugMacro(<< " Setting Roll to " << roll << "");
// subtract the current roll
roll -= this->GetRoll();
if (fabs(roll) < 0.00001)
{
return;
}
this->Roll(roll);
}
//----------------------------------------------------------------------------
// Returns the roll of the camera.
double vtkCamera::GetRoll()
{
double orientation[3];
this->ViewTransform->GetOrientation(orientation);
return orientation[2];
}
//----------------------------------------------------------------------------
// Rotate the camera around the view plane normal.
void vtkCamera::Roll(double angle)
{
double newViewUp[3];
this->Transform->Identity();
// rotate ViewUp about the Direction of Projection
this->Transform->RotateWXYZ(angle,this->DirectionOfProjection);
// okay, okay, TransformPoint shouldn't be used on vectors -- but
// the transform is rotation with no translation so this works fine.
this->Transform->TransformPoint(this->ViewUp,newViewUp);
this->SetViewUp(newViewUp);
}
//----------------------------------------------------------------------------
// Rotate the focal point about the view up vector centered at the camera's
// position.
void vtkCamera::Yaw(double angle)
{
double newFocalPoint[3];
double *pos = this->Position;
this->Transform->Identity();
// translate the camera to the origin,
// rotate about axis,
// translate back again
this->Transform->Translate(+pos[0],+pos[1],+pos[2]);
this->Transform->RotateWXYZ(angle,this->ViewUp);
this->Transform->Translate(-pos[0],-pos[1],-pos[2]);
// now transform focal point
this->Transform->TransformPoint(this->FocalPoint,newFocalPoint);
this->SetFocalPoint(newFocalPoint);
}
//----------------------------------------------------------------------------
// Rotate the focal point about the cross product of the view up vector
// and the negative of the , centered at the camera's position.
void vtkCamera::Pitch(double angle)
{
double axis[3], newFocalPoint[3];
double *pos = this->Position;
this->Transform->Identity();
// the axis is the first row of the view transform matrix
axis[0] = this->ViewTransform->GetMatrix()->GetElement(0,0);
axis[1] = this->ViewTransform->GetMatrix()->GetElement(0,1);
axis[2] = this->ViewTransform->GetMatrix()->GetElement(0,2);
// translate the camera to the origin,
// rotate about axis,
// translate back again
this->Transform->Translate(+pos[0],+pos[1],+pos[2]);
this->Transform->RotateWXYZ(angle,axis);
this->Transform->Translate(-pos[0],-pos[1],-pos[2]);
// now transform focal point
this->Transform->TransformPoint(this->FocalPoint,newFocalPoint);
this->SetFocalPoint(newFocalPoint);
}
//----------------------------------------------------------------------------
// Rotate the camera about the view up vector centered at the focal point.
void vtkCamera::Azimuth(double angle)
{
double newPosition[3];
double *fp = this->FocalPoint;
this->Transform->Identity();
// translate the focal point to the origin,
// rotate about view up,
// translate back again
this->Transform->Translate(+fp[0],+fp[1],+fp[2]);
this->Transform->RotateWXYZ(angle,this->ViewUp);
this->Transform->Translate(-fp[0],-fp[1],-fp[2]);
// apply the transform to the position
this->Transform->TransformPoint(this->Position,newPosition);
this->SetPosition(newPosition);
}
//----------------------------------------------------------------------------
// Rotate the camera about the cross product of the negative of the
// direction of projection and the view up vector centered on the focal point.
void vtkCamera::Elevation(double angle)
{
double axis[3], newPosition[3];
double *fp = this->FocalPoint;
this->Transform->Identity();
// snatch the axis from the view transform matrix
axis[0] = -this->ViewTransform->GetMatrix()->GetElement(0,0);
axis[1] = -this->ViewTransform->GetMatrix()->GetElement(0,1);
axis[2] = -this->ViewTransform->GetMatrix()->GetElement(0,2);
// translate the focal point to the origin,
// rotate about axis,
// translate back again
this->Transform->Translate(+fp[0],+fp[1],+fp[2]);
this->Transform->RotateWXYZ(angle,axis);
this->Transform->Translate(-fp[0],-fp[1],-fp[2]);
// now transform position
this->Transform->TransformPoint(this->Position,newPosition);
this->SetPosition(newPosition);
}
//----------------------------------------------------------------------------
// Apply Transform to camera
void vtkCamera::ApplyTransform(vtkTransform *t)
{
double posOld[4], posNew[4], fpOld[4], fpNew[4], vuOld[4], vuNew[4];
this->GetPosition(posOld);
this->GetFocalPoint(fpOld);
this->GetViewUp(vuOld);
posOld[3] = 1.0;
fpOld[3] = 1.0;
vuOld[3] = 1.0;
vuOld[0] += posOld[0];
vuOld[1] += posOld[1];
vuOld[2] += posOld[2];
t->MultiplyPoint(posOld, posNew);
t->MultiplyPoint(fpOld, fpNew);
t->MultiplyPoint(vuOld, vuNew);
vuNew[0] -= posNew[0];
vuNew[1] -= posNew[1];
vuNew[2] -= posNew[2];
this->SetPosition(posNew);
this->SetFocalPoint(fpNew);
this->SetViewUp(vuNew);
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// The following methods set up the information that the Renderer needs
// to set up the perspective transform. The transformation matrix is
// created using the GetPerspectiveTransformMatrix method.
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void vtkCamera::SetParallelProjection(int flag)
{
if ( this->ParallelProjection != flag )
{
this->ParallelProjection = flag;
this->Modified();
this->ViewingRaysModified();
}
}
//----------------------------------------------------------------------------
void vtkCamera::SetViewAngle(double angle)
{
double min = 0.00000001;
double max = 179.0;
if ( this->ViewAngle != angle )
{
this->ViewAngle = (angle<min?min:(angle>max?max:angle));
this->Modified();
this->ViewingRaysModified();
}
}
//----------------------------------------------------------------------------
void vtkCamera::SetUseHorizontalViewAngle(int flag)
{
if (flag == this->UseHorizontalViewAngle)
{
return;
}
this->UseHorizontalViewAngle = flag;
this->Modified();
this->ViewingRaysModified();
}
//----------------------------------------------------------------------------
void vtkCamera::SetParallelScale(double scale)
{
if ( this->ParallelScale != scale )
{
this->ParallelScale = scale;
this->Modified();
this->ViewingRaysModified();
}
}
//----------------------------------------------------------------------------
// Change the ViewAngle (for perspective) or the ParallelScale (for parallel)
// so that more or less of a scene occupies the viewport. A value > 1 is a
// zoom-in. A value < 1 is a zoom-out.
void vtkCamera::Zoom(double amount)
{
if (amount <= 0.0)
{
return;
}
if (this->ParallelProjection)
{
this->SetParallelScale(this->ParallelScale/amount);
}
else
{
this->SetViewAngle(this->ViewAngle/amount);
}
}
//----------------------------------------------------------------------------
void vtkCamera::SetClippingRange(double nearz, double farz)
{
double thickness;
// check the order
if ( nearz > farz )
{
vtkDebugMacro(<< " Front and back clipping range reversed");
double temp = nearz;
nearz = farz;
farz = temp;
}
thickness = farz - nearz;
// thickness should be greater than 1e-20
if (thickness < 1e-20)
{
thickness = 1e-20;
vtkDebugMacro(<< " ClippingRange thickness is set to minimum.");
// set back plane
farz = nearz + thickness;
}
if (nearz == this->ClippingRange[0] &&
farz == this->ClippingRange[1] &&
this->Thickness == thickness)
{
return;
}
this->ClippingRange[0] = nearz;
this->ClippingRange[1] = farz;
this->Thickness = thickness;
vtkDebugMacro(<< " ClippingRange set to ( " << this->ClippingRange[0]
<< ", " << this->ClippingRange[1] << ")");
this->Modified();
}
//----------------------------------------------------------------------------
// Set the distance between clipping planes.
// This method adjusts the back clipping plane to the specified thickness
// behind the front clipping plane
void vtkCamera::SetThickness(double s)
{
if (this->Thickness == s)
{
return;
}
this->Thickness = s;
// thickness should be greater than 1e-20
if (this->Thickness < 1e-20)
{
this->Thickness = 1e-20;
vtkDebugMacro(<< " ClippingRange thickness is set to minimum.");
}
// set back plane
this->ClippingRange[1] = this->ClippingRange[0] + this->Thickness;
vtkDebugMacro(<< " ClippingRange set to ( " << this->ClippingRange[0]
<< ", " << this->ClippingRange[1] << ")");
this->Modified();
}
//----------------------------------------------------------------------------
void vtkCamera::SetWindowCenter(double x, double y)
{
if (this->WindowCenter[0] != x || this->WindowCenter[1] != y)
{
this->Modified();
this->ViewingRaysModified();
this->WindowCenter[0] = x;
this->WindowCenter[1] = y;
}
}
//----------------------------------------------------------------------------
void vtkCamera::SetObliqueAngles(double alpha, double beta)
{
alpha = vtkMath::RadiansFromDegrees( alpha );
beta = vtkMath::RadiansFromDegrees( beta );
double cotbeta = cos( beta ) / sin( beta );
double dxdz = cos( alpha ) * cotbeta;
double dydz = sin( alpha ) * cotbeta;
this->SetViewShear( dxdz, dydz, 1.0 );
}
//----------------------------------------------------------------------------
// Set the shear transform of the viewing frustum. Parameters are
// dx/dz, dy/dz, and center. center is a factor that describes where
// to shear around. The distance dshear from the camera where
// no shear occurs is given by (dshear = center * FocalDistance).
//
void vtkCamera::SetViewShear(double dxdz, double dydz, double center)
{
if(dxdz != this->ViewShear[0] ||
dydz != this->ViewShear[1] ||
center != this->ViewShear[2])
{
this->Modified();
this->ViewingRaysModified();
this->ViewShear[0] = dxdz;
this->ViewShear[1] = dydz;
this->ViewShear[2] = center;
this->ComputeViewPlaneNormal();
}
}
//----------------------------------------------------------------------------
void vtkCamera::SetViewShear(double d[3])
{
this->SetViewShear(d[0], d[1], d[2]);
}
//----------------------------------------------------------------------------
// Compute the projection transform matrix. This is used in converting
// between view and world coordinates.
void vtkCamera::ComputeProjectionTransform(double aspect,
double nearz, double farz)
{
this->ProjectionTransform->Identity();
// apply user defined transform last if there is one
if ( this->UserTransform )
{
this->ProjectionTransform->Concatenate( this->UserTransform->GetMatrix() );
}