forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkBrokenLineWidget.cxx
1375 lines (1190 loc) · 35.8 KB
/
vtkBrokenLineWidget.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: vtkBrokenLineWidget.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 "vtkBrokenLineWidget.h"
#include "vtkActor.h"
#include "vtkAssemblyNode.h"
#include "vtkAssemblyPath.h"
#include "vtkCallbackCommand.h"
#include "vtkCamera.h"
#include "vtkCellArray.h"
#include "vtkCellPicker.h"
#include "vtkLineSource.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPickingManager.h"
#include "vtkPlaneSource.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSphereSource.h"
#include "vtkTransform.h"
vtkStandardNewMacro(vtkBrokenLineWidget);
vtkCxxSetObjectMacro(vtkBrokenLineWidget, HandleProperty, vtkProperty);
vtkCxxSetObjectMacro(vtkBrokenLineWidget, SelectedHandleProperty, vtkProperty);
vtkCxxSetObjectMacro(vtkBrokenLineWidget, LineProperty, vtkProperty);
vtkCxxSetObjectMacro(vtkBrokenLineWidget, SelectedLineProperty, vtkProperty);
vtkBrokenLineWidget::vtkBrokenLineWidget()
{
this->State = vtkBrokenLineWidget::Start;
this->EventCallbackCommand->SetCallback( vtkBrokenLineWidget::ProcessEventsHandler );
this->ProjectToPlane = 0; //default off
this->ProjectionNormal = 0; //default YZ not used
this->ProjectionPosition = 0.;
this->PlaneSource = NULL;
// Does this widget respond to interaction?
this->ProcessEvents = 1;
// Default handle size factor
this->HandleSizeFactor = 1.;
// Build the representation of the widget
// Default bounds to get started
double bounds[6] = { -.5, .5, -.5, .5, -.5, .5 };
// Create the handles along a straight line within the bounds of a unit cube
this->NumberOfHandles = 5;
this->Handle = new vtkActor* [this->NumberOfHandles];
this->HandleGeometry = new vtkSphereSource* [this->NumberOfHandles];
double u;
double x0 = bounds[0];
double x1 = bounds[1];
double y0 = bounds[2];
double y1 = bounds[3];
double z0 = bounds[4];
double z1 = bounds[5];
double x;
double y;
double z;
vtkPoints* points = vtkPoints::New( VTK_DOUBLE );
points->SetNumberOfPoints( this->NumberOfHandles );
for ( int i = 0; i < this->NumberOfHandles; ++ i )
{
this->HandleGeometry[i] = vtkSphereSource::New();
this->HandleGeometry[i]->SetThetaResolution( 16 );
this->HandleGeometry[i]->SetPhiResolution( 8 );
vtkPolyDataMapper* handleMapper = vtkPolyDataMapper::New();
handleMapper->SetInputConnection(this->HandleGeometry[i]->GetOutputPort() );
this->Handle[i] = vtkActor::New();
this->Handle[i]->SetMapper( handleMapper );
handleMapper->Delete();
u = i / ( this->NumberOfHandles - 1. );
x = ( 1. - u ) * x0 + u * x1;
y = ( 1. - u ) * y0 + u * y1;
z = ( 1. - u ) * z0 + u * z1;
points->SetPoint( i, x, y, z );
this->HandleGeometry[i]->SetCenter( x,y,z );
}
// Create the broken line
this->LineSource = vtkLineSource::New();
this->LineSource->SetPoints( points );
points->Delete();
// Represent the broken line
this->LineMapper = vtkPolyDataMapper::New();
this->LineMapper->SetInputConnection( this->LineSource->GetOutputPort() );
this->LineMapper->ImmediateModeRenderingOn();
this->LineMapper->SetResolveCoincidentTopologyToPolygonOffset();
this->LineActor = vtkActor::New();
this->LineActor->SetMapper( this->LineMapper );
// Initial creation of the widget, serves to initialize it
this->PlaceFactor = 1.;
this->PlaceWidget( bounds );
// Manage the picking stuff
this->HandlePicker = vtkCellPicker::New();
this->HandlePicker->SetTolerance( .005 );
for ( int i = 0; i < this->NumberOfHandles; ++ i )
{
this->HandlePicker->AddPickList( this->Handle[i]);
}
this->HandlePicker->PickFromListOn();
this->LinePicker = vtkCellPicker::New();
this->LinePicker->SetTolerance( .01 );
this->LinePicker->AddPickList( this->LineActor );
this->LinePicker->PickFromListOn();
this->CurrentHandle = NULL;
this->CurrentHandleIndex = -1;
this->Transform = vtkTransform::New();
// Set up the initial properties
this->HandleProperty = NULL;
this->SelectedHandleProperty = NULL;
this->LineProperty = NULL;
this->SelectedLineProperty = NULL;
this->CreateDefaultProperties();
}
vtkBrokenLineWidget::~vtkBrokenLineWidget()
{
this->LineActor->Delete();
this->LineMapper->Delete();
this->LineSource->Delete();
for ( int i = 0; i < this->NumberOfHandles; ++ i )
{
this->HandleGeometry[i]->Delete();
this->Handle[i]->Delete();
}
delete [] this->Handle;
delete [] this->HandleGeometry;
this->HandlePicker->Delete();
this->LinePicker->Delete();
if ( this->HandleProperty )
{
this->HandleProperty->Delete();
}
if ( this->SelectedHandleProperty )
{
this->SelectedHandleProperty->Delete();
}
if ( this->LineProperty )
{
this->LineProperty->Delete();
}
if ( this->SelectedLineProperty )
{
this->SelectedLineProperty->Delete();
}
this->Transform->Delete();
}
void vtkBrokenLineWidget::SetHandlePosition( int handle, double x,
double y, double z )
{
if ( handle < 0 || handle >= this->NumberOfHandles )
{
vtkErrorMacro(<<"vtkBrokenLineWidget: handle index out of range.");
return;
}
this->HandleGeometry[handle]->SetCenter( x,y,z );
this->HandleGeometry[handle]->Update();
if ( this->ProjectToPlane )
{
this->ProjectPointsToPlane();
}
this->BuildRepresentation();
}
void vtkBrokenLineWidget::SetHandlePosition( int handle, double xyz[3])
{
this->SetHandlePosition( handle,xyz[0],xyz[1],xyz[2]);
}
void vtkBrokenLineWidget::GetHandlePosition( int handle, double xyz[3])
{
if ( handle < 0 || handle >= this->NumberOfHandles )
{
vtkErrorMacro(<<"vtkBrokenLineWidget: handle index out of range.");
return;
}
this->HandleGeometry[handle]->GetCenter( xyz );
}
double* vtkBrokenLineWidget::GetHandlePosition( int handle )
{
if ( handle < 0 || handle >= this->NumberOfHandles )
{
vtkErrorMacro(<<"vtkBrokenLineWidget: handle index out of range.");
return NULL;
}
return this->HandleGeometry[handle]->GetCenter();
}
void vtkBrokenLineWidget::SetEnabled( int enabling )
{
if ( ! this->Interactor )
{
vtkErrorMacro(<<"The interactor must be set prior to enabling/disabling widget");
return;
}
if ( enabling ) //------------------------------------------------------------
{
vtkDebugMacro(<<"Enabling broken line widget");
if ( this->Enabled ) //already enabled, just return
{
return;
}
if ( ! this->CurrentRenderer )
{
this->SetCurrentRenderer( this->Interactor->FindPokedRenderer(
this->Interactor->GetLastEventPosition()[0],
this->Interactor->GetLastEventPosition()[1]) );
if ( this->CurrentRenderer == NULL )
{
return;
}
}
this->Enabled = 1;
// Listen for the following events
vtkRenderWindowInteractor *i = this->Interactor;
i->AddObserver( vtkCommand::MouseMoveEvent, this->EventCallbackCommand,
this->Priority );
i->AddObserver( vtkCommand::LeftButtonPressEvent, this->EventCallbackCommand,
this->Priority );
i->AddObserver( vtkCommand::LeftButtonReleaseEvent, this->EventCallbackCommand,
this->Priority );
i->AddObserver( vtkCommand::MiddleButtonPressEvent, this->EventCallbackCommand,
this->Priority );
i->AddObserver( vtkCommand::MiddleButtonReleaseEvent, this->EventCallbackCommand,
this->Priority );
i->AddObserver( vtkCommand::RightButtonPressEvent, this->EventCallbackCommand,
this->Priority );
i->AddObserver( vtkCommand::RightButtonReleaseEvent, this->EventCallbackCommand,
this->Priority );
// Add the line
this->CurrentRenderer->AddActor( this->LineActor );
this->LineActor->SetProperty( this->LineProperty );
// Turn on the handles
for ( int j = 0; j < this->NumberOfHandles; ++j )
{
this->CurrentRenderer->AddActor( this->Handle[j]);
this->Handle[j]->SetProperty( this->HandleProperty );
}
this->BuildRepresentation();
this->SizeHandles();
this->InvokeEvent( vtkCommand::EnableEvent,NULL );
}
else //disabling----------------------------------------------------------
{
vtkDebugMacro(<<"Disabling broken line widget");
if ( ! this->Enabled ) //already disabled, just return
{
return;
}
this->Enabled = 0;
// Don't listen for events any more
this->Interactor->RemoveObserver( this->EventCallbackCommand );
// Turn off the line
this->CurrentRenderer->RemoveActor( this->LineActor );
// Turn off the handles
for ( int i = 0; i < this->NumberOfHandles; ++ i )
{
this->CurrentRenderer->RemoveActor( this->Handle[i]);
}
this->CurrentHandle = NULL;
this->InvokeEvent( vtkCommand::DisableEvent,NULL );
this->SetCurrentRenderer( NULL );
}
this->Interactor->Render();
}
void vtkBrokenLineWidget::ProcessEventsHandler( vtkObject* vtkNotUsed( object ),
unsigned long event,
void* clientdata,
void* vtkNotUsed( calldata ) )
{
vtkBrokenLineWidget* self = reinterpret_cast<vtkBrokenLineWidget *>( clientdata );
// if ProcessEvents is Off, we ignore all interaction events.
if (! self->GetProcessEvents() )
{
return;
}
// Okay, let's do the right thing
switch( event )
{
case vtkCommand::LeftButtonPressEvent:
self->OnLeftButtonDown();
break;
case vtkCommand::LeftButtonReleaseEvent:
self->OnLeftButtonUp();
break;
case vtkCommand::MiddleButtonPressEvent:
self->OnMiddleButtonDown();
break;
case vtkCommand::MiddleButtonReleaseEvent:
self->OnMiddleButtonUp();
break;
case vtkCommand::RightButtonPressEvent:
self->OnRightButtonDown();
break;
case vtkCommand::RightButtonReleaseEvent:
self->OnRightButtonUp();
break;
case vtkCommand::MouseMoveEvent:
self->OnMouseMove();
break;
}
}
void vtkBrokenLineWidget::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf( os,indent );
os << indent << "ProcessEvents: "
<< ( this->ProcessEvents? "On" : "Off") << "\n";
if ( this->HandleProperty )
{
os << indent << "Handle Property: " << this->HandleProperty << "\n";
}
else
{
os << indent << "Handle Property: ( none )\n";
}
if ( this->SelectedHandleProperty )
{
os << indent << "Selected Handle Property: "
<< this->SelectedHandleProperty << "\n";
}
else
{
os << indent << "Selected Handle Property: ( none )\n";
}
if ( this->LineProperty )
{
os << indent << "Line Property: " << this->LineProperty << "\n";
}
else
{
os << indent << "Line Property: ( none )\n";
}
if ( this->SelectedLineProperty )
{
os << indent << "Selected Line Property: "
<< this->SelectedLineProperty << "\n";
}
else
{
os << indent << "Selected Line Property: ( none )\n";
}
os << indent << "Project To Plane: "
<< ( this->ProjectToPlane ? "On" : "Off") << "\n";
os << indent << "Projection Normal: " << this->ProjectionNormal << "\n";
os << indent << "Projection Position: " << this->ProjectionPosition << "\n";
os << indent << "Number Of Handles: " << this->NumberOfHandles << "\n";
os << indent << "Handle Size Factor" << this->HandleSizeFactor << "\n";
}
void vtkBrokenLineWidget::ProjectPointsToPlane()
{
if ( this->ProjectionNormal == VTK_PROJECTION_OBLIQUE )
{
if ( this->PlaneSource != NULL )
{
this->ProjectPointsToObliquePlane();
}
else
{
vtkGenericWarningMacro(<<"Set the plane source for oblique projections...");
}
}
else
{
this->ProjectPointsToOrthoPlane();
}
}
void vtkBrokenLineWidget::ProjectPointsToObliquePlane()
{
double o[3];
double u[3];
double v[3];
this->PlaneSource->GetPoint1( u );
this->PlaneSource->GetPoint2( v );
this->PlaneSource->GetOrigin( o );
int i;
for ( i = 0; i < 3; ++ i )
{
u[i] = u[i] - o[i];
v[i] = v[i] - o[i];
}
vtkMath::Normalize( u );
vtkMath::Normalize( v );
double o_dot_u = vtkMath::Dot( o,u );
double o_dot_v = vtkMath::Dot( o,v );
double fac1;
double fac2;
double ctr[3];
for ( i = 0; i < this->NumberOfHandles; ++ i )
{
this->HandleGeometry[i]->GetCenter( ctr );
fac1 = vtkMath::Dot( ctr,u ) - o_dot_u;
fac2 = vtkMath::Dot( ctr,v ) - o_dot_v;
ctr[0] = o[0] + fac1*u[0] + fac2*v[0];
ctr[1] = o[1] + fac1*u[1] + fac2*v[1];
ctr[2] = o[2] + fac1*u[2] + fac2*v[2];
this->HandleGeometry[i]->SetCenter( ctr );
this->HandleGeometry[i]->Update();
}
}
void vtkBrokenLineWidget::ProjectPointsToOrthoPlane()
{
double ctr[3];
for ( int i = 0; i < this->NumberOfHandles; ++ i )
{
this->HandleGeometry[i]->GetCenter( ctr );
ctr[this->ProjectionNormal] = this->ProjectionPosition;
this->HandleGeometry[i]->SetCenter( ctr );
this->HandleGeometry[i]->Update();
}
}
//------------------------------------------------------------------------------
void vtkBrokenLineWidget::RegisterPickers()
{
this->Interactor->GetPickingManager()->AddPicker(this->HandlePicker, this);
this->Interactor->GetPickingManager()->AddPicker(this->LinePicker, this);
}
void vtkBrokenLineWidget::BuildRepresentation()
{
// Get points array from line source
vtkPoints* points = this->LineSource->GetPoints();
if ( points->GetNumberOfPoints() != this->NumberOfHandles )
{
points->SetNumberOfPoints( this->NumberOfHandles );
}
double pt[3];
int i;
for ( i = 0; i < this->NumberOfHandles; ++ i )
{
this->HandleGeometry[i]->GetCenter( pt );
points->SetPoint( i, pt );
}
this->LineSource->Modified();
}
int vtkBrokenLineWidget::HighlightHandle( vtkProp *prop )
{
// First unhighlight anything picked
if ( this->CurrentHandle )
{
this->CurrentHandle->SetProperty( this->HandleProperty );
}
this->CurrentHandle = static_cast<vtkActor *>( prop );
if ( this->CurrentHandle )
{
for ( int i = 0; i < this->NumberOfHandles; ++ i ) // find handle
{
if ( this->CurrentHandle == this->Handle[i] )
{
this->ValidPick = 1;
this->HandlePicker->GetPickPosition( this->LastPickPosition );
this->CurrentHandle->SetProperty( this->SelectedHandleProperty );
return i;
}
}
}
return -1;
}
void vtkBrokenLineWidget::HighlightLine( int highlight )
{
if ( highlight )
{
this->ValidPick = 1;
this->LinePicker->GetPickPosition( this->LastPickPosition );
this->LineActor->SetProperty( this->SelectedLineProperty );
}
else
{
this->LineActor->SetProperty( this->LineProperty );
}
}
void vtkBrokenLineWidget::OnLeftButtonDown()
{
int X = this->Interactor->GetEventPosition()[0];
int Y = this->Interactor->GetEventPosition()[1];
// Okay, make sure that the pick is in the current renderer
if ( ! this->CurrentRenderer || ! this->CurrentRenderer->IsInViewport( X, Y ) )
{
this->State = vtkBrokenLineWidget::Outside;
return;
}
this->State = vtkBrokenLineWidget::Moving;
// Okay, we can process this. Try to pick handles first;
// if no handles picked, then try to pick the line.
vtkAssemblyPath* path = this->GetAssemblyPath(X, Y, 0., this->HandlePicker);
if ( path != NULL )
{
this->CurrentHandleIndex = this->HighlightHandle( path->GetFirstNode()->GetViewProp() );
}
else
{
path = this->GetAssemblyPath(X, Y, 0., this->LinePicker);
if ( path != NULL )
{
this->HighlightLine( 1 );
}
else
{
this->CurrentHandleIndex = this->HighlightHandle( NULL );
this->State = vtkBrokenLineWidget::Outside;
return;
}
}
this->EventCallbackCommand->SetAbortFlag( 1 );
this->StartInteraction();
this->InvokeEvent( vtkCommand::StartInteractionEvent,NULL );
this->Interactor->Render();
}
void vtkBrokenLineWidget::OnLeftButtonUp()
{
if ( this->State == vtkBrokenLineWidget::Outside ||
this->State == vtkBrokenLineWidget::Start )
{
return;
}
this->State = vtkBrokenLineWidget::Start;
this->HighlightHandle( NULL );
this->HighlightLine( 0 );
this->SizeHandles();
this->EventCallbackCommand->SetAbortFlag( 1 );
this->EndInteraction();
this->InvokeEvent( vtkCommand::EndInteractionEvent,NULL );
this->Interactor->Render();
}
void vtkBrokenLineWidget::OnMiddleButtonDown()
{
int X = this->Interactor->GetEventPosition()[0];
int Y = this->Interactor->GetEventPosition()[1];
// Okay, make sure that the pick is in the current renderer
if ( ! this->CurrentRenderer || ! this->CurrentRenderer->IsInViewport( X, Y ) )
{
this->State = vtkBrokenLineWidget::Outside;
return;
}
if ( this->Interactor->GetControlKey() )
{
this->State = vtkBrokenLineWidget::Spinning;
this->CalculateCentroid();
}
else
{
this->State = vtkBrokenLineWidget::Moving;
}
// Okay, we can process this. Try to pick handles first;
// if no handles picked, then try to pick the line.
vtkAssemblyPath* path = this->GetAssemblyPath(X, Y, 0., this->HandlePicker);
if ( path == NULL )
{
path = this->GetAssemblyPath(X, Y, 0., this->LinePicker);
if ( path == NULL )
{
this->State = vtkBrokenLineWidget::Outside;
this->HighlightLine( 0 );
return;
}
else
{
this->HighlightLine( 1 );
}
}
else //we picked a handle but lets make it look like the line is picked
{
this->HighlightLine( 1 );
}
this->EventCallbackCommand->SetAbortFlag( 1 );
this->StartInteraction();
this->InvokeEvent( vtkCommand::StartInteractionEvent,NULL );
this->Interactor->Render();
}
void vtkBrokenLineWidget::OnMiddleButtonUp()
{
if ( this->State == vtkBrokenLineWidget::Outside ||
this->State == vtkBrokenLineWidget::Start )
{
return;
}
this->State = vtkBrokenLineWidget::Start;
this->HighlightLine( 0 );
this->SizeHandles();
this->EventCallbackCommand->SetAbortFlag( 1 );
this->EndInteraction();
this->InvokeEvent( vtkCommand::EndInteractionEvent,NULL );
this->Interactor->Render();
}
void vtkBrokenLineWidget::OnRightButtonDown()
{
int X = this->Interactor->GetEventPosition()[0];
int Y = this->Interactor->GetEventPosition()[1];
// Okay, make sure that the pick is in the current renderer
if ( ! this->CurrentRenderer || ! this->CurrentRenderer->IsInViewport( X, Y ) )
{
this->State = vtkBrokenLineWidget::Outside;
return;
}
if ( this->Interactor->GetShiftKey() )
{
this->State = vtkBrokenLineWidget::Inserting;
}
else if ( this->Interactor->GetControlKey() )
{
this->State = vtkBrokenLineWidget::Erasing;
}
else
{
this->State = vtkBrokenLineWidget::Scaling;
}
vtkAssemblyPath* path = this->GetAssemblyPath(X, Y, 0., this->HandlePicker);
if ( path != NULL )
{
switch ( this->State )
{
// deny insertion over existing handles
case vtkBrokenLineWidget::Inserting:
this->State = vtkBrokenLineWidget::Outside;
return;
case vtkBrokenLineWidget::Erasing:
this->CurrentHandleIndex = \
this->HighlightHandle( path->GetFirstNode()->GetViewProp() );
break;
case vtkBrokenLineWidget::Scaling:
this->HighlightLine( 1 );
break;
}
}
else
{
// trying to erase handle but nothing picked
if ( this->State == vtkBrokenLineWidget::Erasing )
{
this->State = vtkBrokenLineWidget::Outside;
return;
}
// try to insert or scale so pick the line
path = this->GetAssemblyPath(X, Y, 0., this->LinePicker);
if ( path != NULL )
{
this->HighlightLine( 1 );
}
else
{
this->State = vtkBrokenLineWidget::Outside;
return;
}
}
this->EventCallbackCommand->SetAbortFlag( 1 );
this->StartInteraction();
this->InvokeEvent( vtkCommand::StartInteractionEvent,NULL );
this->Interactor->Render();
}
void vtkBrokenLineWidget::OnRightButtonUp()
{
if ( this->State == vtkBrokenLineWidget::Outside ||
this->State == vtkBrokenLineWidget::Start )
{
return;
}
if ( this->State == vtkBrokenLineWidget::Inserting )
{
this->InsertHandleOnLine( this->LastPickPosition );
}
else if ( this->State == vtkBrokenLineWidget::Erasing )
{
int index = this->CurrentHandleIndex;
this->CurrentHandleIndex = this->HighlightHandle( NULL );
this->EraseHandle( index );
}
this->State = vtkBrokenLineWidget::Start;
this->HighlightLine( 0 );
this->SizeHandles();
this->EventCallbackCommand->SetAbortFlag( 1 );
this->EndInteraction();
this->InvokeEvent( vtkCommand::EndInteractionEvent,NULL );
this->Interactor->Render();
}
void vtkBrokenLineWidget::OnMouseMove()
{
// See whether we're active
if ( this->State == vtkBrokenLineWidget::Outside ||
this->State == vtkBrokenLineWidget::Start )
{
return;
}
int X = this->Interactor->GetEventPosition()[0];
int Y = this->Interactor->GetEventPosition()[1];
// Do different things depending on state
// Calculations everybody does
double focalPoint[4], pickPoint[4], prevPickPoint[4];
double z, vpn[3];
vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
if ( ! camera )
{
return;
}
// Compute the two points defining the motion vector
this->ComputeWorldToDisplay( this->LastPickPosition[0], this->LastPickPosition[1],
this->LastPickPosition[2], focalPoint );
z = focalPoint[2];
this->ComputeDisplayToWorld( double( this->Interactor->GetLastEventPosition()[0]),
double( this->Interactor->GetLastEventPosition()[1]),
z, prevPickPoint );
this->ComputeDisplayToWorld( double( X ), double( Y ), z, pickPoint );
// Process the motion
if ( this->State == vtkBrokenLineWidget::Moving )
{
// Okay to process
if ( this->CurrentHandle )
{
this->MovePoint( prevPickPoint, pickPoint );
}
else // Must be moving the spline
{
this->Translate( prevPickPoint, pickPoint );
}
}
else if ( this->State == vtkBrokenLineWidget::Scaling )
{
this->Scale( prevPickPoint, pickPoint, X, Y );
}
else if ( this->State == vtkBrokenLineWidget::Spinning )
{
camera->GetViewPlaneNormal( vpn );
this->Spin( prevPickPoint, pickPoint, vpn );
}
if ( this->ProjectToPlane )
{
this->ProjectPointsToPlane();
}
this->BuildRepresentation();
// Interact, if desired
this->EventCallbackCommand->SetAbortFlag( 1 );
this->InvokeEvent( vtkCommand::InteractionEvent,NULL );
this->Interactor->Render();
}
void vtkBrokenLineWidget::MovePoint( double *p1, double *p2 )
{
if ( this->CurrentHandleIndex < 0 || this->CurrentHandleIndex >= this->NumberOfHandles )
{
vtkGenericWarningMacro(<<"BrokenLine handle index out of range.");
return;
}
// Get the motion vector
double v[3];
v[0] = p2[0] - p1[0];
v[1] = p2[1] - p1[1];
v[2] = p2[2] - p1[2];
double *ctr = this->HandleGeometry[this->CurrentHandleIndex]->GetCenter();
double newCtr[3];
newCtr[0] = ctr[0] + v[0];
newCtr[1] = ctr[1] + v[1];
newCtr[2] = ctr[2] + v[2];
this->HandleGeometry[this->CurrentHandleIndex]->SetCenter( newCtr );
this->HandleGeometry[this->CurrentHandleIndex]->Update();
}
void vtkBrokenLineWidget::Translate( double *p1, double *p2 )
{
// Get the motion vector
double v[3];
v[0] = p2[0] - p1[0];
v[1] = p2[1] - p1[1];
v[2] = p2[2] - p1[2];
double newCtr[3];
for ( int i = 0; i < this->NumberOfHandles; ++ i )
{
double* ctr = this->HandleGeometry[i]->GetCenter();
for ( int j = 0; j < 3; ++j )
{
newCtr[j] = ctr[j] + v[j];
}
this->HandleGeometry[i]->SetCenter( newCtr );
this->HandleGeometry[i]->Update();
}
}
void vtkBrokenLineWidget::Scale( double *p1, double *p2, int vtkNotUsed( X ), int Y )
{
// Get the motion vector
double v[3];
v[0] = p2[0] - p1[0];
v[1] = p2[1] - p1[1];
v[2] = p2[2] - p1[2];
double center[3] = { 0., 0., 0. };
double avgdist = 0.;
double *prevctr = this->HandleGeometry[0]->GetCenter();
double *ctr;
center[0] += prevctr[0];
center[1] += prevctr[1];
center[2] += prevctr[2];
int i;
for ( i = 1; i < this->NumberOfHandles; ++ i )
{
ctr = this->HandleGeometry[i]->GetCenter();
center[0] += ctr[0];
center[1] += ctr[1];
center[2] += ctr[2];
avgdist += sqrt( vtkMath::Distance2BetweenPoints( ctr,prevctr ) );
prevctr = ctr;
}
avgdist /= this->NumberOfHandles;
center[0] /= this->NumberOfHandles;
center[1] /= this->NumberOfHandles;
center[2] /= this->NumberOfHandles;
// Compute the scale factor
double sf = vtkMath::Norm( v ) / avgdist;
if ( Y > this->Interactor->GetLastEventPosition()[1] )
{
sf = 1.0 + sf;
}
else
{
sf = 1.0 - sf;
}
// Move the handle points
double newCtr[3];
for ( i = 0; i < this->NumberOfHandles; ++ i )
{
ctr = this->HandleGeometry[i]->GetCenter();
for ( int j = 0; j < 3; ++j )
{
newCtr[j] = sf * ( ctr[j] - center[j]) + center[j];
}
this->HandleGeometry[i]->SetCenter( newCtr );
this->HandleGeometry[i]->Update();
}
}
void vtkBrokenLineWidget::Spin( double *p1, double *p2, double *vpn )
{
// Mouse motion vector in world space
double v[3];
v[0] = p2[0] - p1[0];
v[1] = p2[1] - p1[1];
v[2] = p2[2] - p1[2];
// Axis of rotation
double axis[3] = { 0., 0., 0. };
if ( this->ProjectToPlane )
{
if ( this->ProjectionNormal == VTK_PROJECTION_OBLIQUE)
{
if (this->PlaneSource != NULL )
{
double* normal = this->PlaneSource->GetNormal();
axis[0] = normal[0];
axis[1] = normal[1];
axis[2] = normal[2];
vtkMath::Normalize( axis );
}
else
{
axis[0] = 1.;
}
}
else
{
axis[ this->ProjectionNormal ] = 1.;
}
}
else
{
// Create axis of rotation and angle of rotation
vtkMath::Cross( vpn,v,axis );
if ( vtkMath::Normalize( axis ) == 0. )
{
return;
}
}
// Radius vector ( from mean center to cursor position )
double rv[3] = {p2[0] - this->Centroid[0],
p2[1] - this->Centroid[1],
p2[2] - this->Centroid[2]};