vtkLineWidget.cxx 20.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkLineWidget.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 1993-2002 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 "vtkLineWidget.h"
19
20
21

#include "vtkAssemblyNode.h"
#include "vtkCallbackCommand.h"
22
#include "vtkCamera.h"
23
24
25
#include "vtkCellPicker.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
26
#include "vtkMath.h"
27
28
#include "vtkObjectFactory.h"
#include "vtkPlanes.h"
29
30
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
31
#include "vtkProperty.h"
32
#include "vtkRenderWindowInteractor.h"
33
#include "vtkSphereSource.h"
34
#include "vtkRenderWindow.h"
35

Will Schroeder's avatar
Will Schroeder committed
36
vtkCxxRevisionMacro(vtkLineWidget, "1.23");
37
38
39
40
vtkStandardNewMacro(vtkLineWidget);

vtkLineWidget::vtkLineWidget()
{
Will Schroeder's avatar
Will Schroeder committed
41
42
  this->State = vtkLineWidget::Start;
  this->EventCallbackCommand->SetCallback(vtkLineWidget::ProcessEvents);
43
  
44
  this->Align = vtkLineWidget::XAxis;
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
  //Build the representation of the widget
  int i;
  // Represent the line
  this->LineSource = vtkLineSource::New();
  this->LineSource->SetResolution(5);
  this->LineMapper = vtkPolyDataMapper::New();
  this->LineMapper->SetInput(this->LineSource->GetOutput());
  this->LineActor = vtkActor::New();
  this->LineActor->SetMapper(this->LineMapper);

  // Create the handles
  this->Handle = new vtkActor* [2];
  this->HandleMapper = new vtkPolyDataMapper* [2];
  this->HandleGeometry = new vtkSphereSource* [2];
  for (i=0; i<2; i++)
    {
    this->HandleGeometry[i] = vtkSphereSource::New();
    this->HandleGeometry[i]->SetThetaResolution(16);
    this->HandleGeometry[i]->SetPhiResolution(8);
    this->HandleMapper[i] = vtkPolyDataMapper::New();
    this->HandleMapper[i]->SetInput(this->HandleGeometry[i]->GetOutput());
    this->Handle[i] = vtkActor::New();
    this->Handle[i]->SetMapper(this->HandleMapper[i]);
    }
  
  // Define the point coordinates
  float bounds[6];
  bounds[0] = -0.5;
  bounds[1] = 0.5;
  bounds[2] = -0.5;
  bounds[3] = 0.5;
  bounds[4] = -0.5;
  bounds[5] = 0.5;

  // Initial creation of the widget, serves to initialize it
  this->PlaceWidget(bounds);

  //Manage the picking stuff
  this->HandlePicker = vtkCellPicker::New();
  this->HandlePicker->SetTolerance(0.001);
  for (i=0; i<2; i++)
    {
    this->HandlePicker->AddPickList(this->Handle[i]);
    }
  this->HandlePicker->PickFromListOn();

  this->LinePicker = vtkCellPicker::New();
  this->LinePicker->SetTolerance(0.005); //need some fluff
  this->LinePicker->AddPickList(this->LineActor);
  this->LinePicker->PickFromListOn();
  
  this->CurrentHandle = NULL;

  // Set up the initial properties
  this->HandleProperty = NULL;
  this->SelectedHandleProperty = NULL;
  this->LineProperty = NULL;
  this->SelectedLineProperty = NULL;
  this->CreateDefaultProperties();
}

vtkLineWidget::~vtkLineWidget()
{
  this->LineActor->Delete();
  this->LineMapper->Delete();
  this->LineSource->Delete();

  for (int i=0; i<2; i++)
    {
    this->HandleGeometry[i]->Delete();
    this->HandleMapper[i]->Delete();
    this->Handle[i]->Delete();
    }
  delete [] this->Handle;
  delete [] this->HandleMapper;
  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();
    }
}

Will Schroeder's avatar
Will Schroeder committed
144
void vtkLineWidget::SetEnabled(int enabling)
145
{
Will Schroeder's avatar
Will Schroeder committed
146
147
148
149
150
  if ( ! this->Interactor )
    {
    vtkErrorMacro(<<"The interactor must be set prior to enabling/disabling widget");
    return;
    }
151

Will Schroeder's avatar
Will Schroeder committed
152
  if ( enabling ) //------------------------------------------------------------
153
    {
Will Schroeder's avatar
Will Schroeder committed
154
155
156
157
158
159
160
    vtkDebugMacro(<<"Enabling line widget");

    if ( this->Enabled ) //already enabled, just return
      {
      return;
      }
    
Sebastien Barre's avatar
Sebastien Barre committed
161
    this->CurrentRenderer = this->Interactor->FindPokedRenderer(this->Interactor->GetLastEventPosition()[0],this->Interactor->GetLastEventPosition()[1]);
162
163
164
165
166
    if (this->CurrentRenderer == NULL)
      {
      return;
      }

Will Schroeder's avatar
Will Schroeder committed
167
168
169
170
    this->Enabled = 1;

    // listen for the following events
    vtkRenderWindowInteractor *i = this->Interactor;
Will Schroeder's avatar
Will Schroeder committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    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);
Will Schroeder's avatar
Will Schroeder committed
185

186
187
188
    // Add the line
    this->CurrentRenderer->AddActor(this->LineActor);
    this->LineActor->SetProperty(this->LineProperty);
Will Schroeder's avatar
Will Schroeder committed
189

190
    // turn on the handles
Will Schroeder's avatar
Will Schroeder committed
191
    for (int j=0; j<2; j++)
192
      {
Will Schroeder's avatar
Will Schroeder committed
193
194
      this->CurrentRenderer->AddActor(this->Handle[j]);
      this->Handle[j]->SetProperty(this->HandleProperty);
195
196
      }

Will Schroeder's avatar
Will Schroeder committed
197
    this->InvokeEvent(vtkCommand::EnableEvent,NULL);
198
    }
Will Schroeder's avatar
Will Schroeder committed
199
200
201
202
  
  else //disabling----------------------------------------------------------
    {
    vtkDebugMacro(<<"Disabling line widget");
203

Will Schroeder's avatar
Will Schroeder committed
204
205
206
207
208
209
    if ( ! this->Enabled ) //already disabled, just return
      {
      return;
      }
    
    this->Enabled = 0;
210

Will Schroeder's avatar
Will Schroeder committed
211
212
    // don't listen for events any more
    this->Interactor->RemoveObserver(this->EventCallbackCommand);
213

Will Schroeder's avatar
Will Schroeder committed
214
215
    // turn off the line
    this->CurrentRenderer->RemoveActor(this->LineActor);
216

Will Schroeder's avatar
Will Schroeder committed
217
218
219
220
221
    // turn off the handles
    for (int i=0; i<2; i++)
      {
      this->CurrentRenderer->RemoveActor(this->Handle[i]);
      }
222

Will Schroeder's avatar
Will Schroeder committed
223
224
    this->CurrentHandle = NULL;
    this->InvokeEvent(vtkCommand::DisableEvent,NULL);
225
226
    }

Will Schroeder's avatar
Will Schroeder committed
227
  this->Interactor->Render();
228
229
}

Sebastien Barre's avatar
Sebastien Barre committed
230
231
232
233
void vtkLineWidget::ProcessEvents(vtkObject* vtkNotUsed(object), 
                                  unsigned long event,
                                  void* clientdata, 
                                  void* vtkNotUsed(calldata))
234
235
236
237
238
239
240
{
  vtkLineWidget* self = reinterpret_cast<vtkLineWidget *>( clientdata );

  //okay, let's do the right thing
  switch(event)
    {
    case vtkCommand::LeftButtonPressEvent:
241
      self->OnLeftButtonDown();
242
243
      break;
    case vtkCommand::LeftButtonReleaseEvent:
244
      self->OnLeftButtonUp();
245
      break;
246
    case vtkCommand::MiddleButtonPressEvent:
247
      self->OnMiddleButtonDown();
248
249
      break;
    case vtkCommand::MiddleButtonReleaseEvent:
250
      self->OnMiddleButtonUp();
251
      break;
252
    case vtkCommand::RightButtonPressEvent:
253
      self->OnRightButtonDown();
254
255
      break;
    case vtkCommand::RightButtonReleaseEvent:
256
      self->OnRightButtonUp();
257
      break;
Will Schroeder's avatar
Will Schroeder committed
258
    case vtkCommand::MouseMoveEvent:
259
      self->OnMouseMove();
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
      break;
    }
}

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

  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
    {
Will Schroeder's avatar
Will Schroeder committed
283
    os << indent << "Selected Handle Property: (none)\n";
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
    }

  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";
    }

304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
  os << indent << "Align with: ";
  switch ( this->Align ) 
    {
    case XAxis:
      os << "X Axis";
      break;
    case YAxis:
      os << "Y Axis";
      break;
    case ZAxis:
      os << "Z Axis";
      break;
    default:
      os << "None";
    }
319
  int res = this->LineSource->GetResolution();
320
321
  float *pt1 = this->LineSource->GetPoint1();
  float *pt2 = this->LineSource->GetPoint2();
322
323
324
325
326
327
328
329
330
331

  os << indent << "Resolution: " << res << "\n";
  os << indent << "Point 1: (" << pt1[0] << ", "
                               << pt1[1] << ", "
                               << pt1[2] << ")\n";
  os << indent << "Point 2: (" << pt2[0] << ", "
                               << pt2[1] << ", "
                               << pt2[2] << ")\n";
}

Will Schroeder's avatar
Will Schroeder committed
332
333
void vtkLineWidget::PositionHandles()
{
334
  //int res = this->LineSource->GetResolution();
335
336
  float *pt1 = this->LineSource->GetPoint1();
  float *pt2 = this->LineSource->GetPoint2();
Will Schroeder's avatar
Will Schroeder committed
337
338
339
340
341

  this->HandleGeometry[0]->SetCenter(pt1);
  this->HandleGeometry[1]->SetCenter(pt2);
}

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
int vtkLineWidget::HighlightHandle(vtkProp *prop)
{
  // first unhighlight anything picked
  if ( this->CurrentHandle )
    {
    this->CurrentHandle->SetProperty(this->HandleProperty);
    }

  this->CurrentHandle = (vtkActor *)prop;

  if ( this->CurrentHandle )
    {
    this->CurrentHandle->SetProperty(this->SelectedHandleProperty);
    for (int i=0; i<2; i++) //find handle
      {
      if ( this->CurrentHandle == this->Handle[i] )
        {
        return i;
        }
      }
    }
  
  return -1;
}

void vtkLineWidget::HighlightLine(int highlight)
{
  if ( highlight )
    {
    this->LineActor->SetProperty(this->SelectedLineProperty);
    }
  else
    {
    this->LineActor->SetProperty(this->LineProperty);
    }
}

379
void vtkLineWidget::OnLeftButtonDown()
380
{
Will Schroeder's avatar
Will Schroeder committed
381
382
  // We're only here is we are enabled
  this->State = vtkLineWidget::Moving;
383

384
385
386
  int X = this->Interactor->GetEventPosition()[0];
  int Y = this->Interactor->GetEventPosition()[1];

387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  // Okay, we can process this. Try to pick handles first;
  // if no handles picked, then try to pick the line.
  vtkAssemblyPath *path;
  this->Interactor->FindPokedRenderer(X,Y);
  this->HandlePicker->Pick(X,Y,0.0,this->CurrentRenderer);
  path = this->HandlePicker->GetPath();
  if ( path != NULL )
    {
    this->HighlightHandle(path->GetFirstNode()->GetProp());
    }
  else
    {
    this->LinePicker->Pick(X,Y,0.0,this->CurrentRenderer);
    path = this->LinePicker->GetPath();
    if ( path != NULL )
      {
      this->HighlightLine(1);
      }
    else
      {
      this->HighlightHandle(NULL);
Will Schroeder's avatar
Will Schroeder committed
408
      this->State = vtkLineWidget::Outside;
409
410
411
412
      return;
      }
    }
  
Will Schroeder's avatar
Will Schroeder committed
413
  this->EventCallbackCommand->SetAbortFlag(1);
414
  this->StartInteraction();
415
416
417
418
  this->InvokeEvent(vtkCommand::StartInteractionEvent,NULL);
  this->Interactor->Render();
}

419
void vtkLineWidget::OnMouseMove()
420
421
{
  // See whether we're active
422
423
  if ( this->State == vtkLineWidget::Outside || 
       this->State == vtkLineWidget::Start )
424
425
426
427
    {
    return;
    }
  
428
429
430
  int X = this->Interactor->GetEventPosition()[0];
  int Y = this->Interactor->GetEventPosition()[1];

431
432
433
434
435
  // Do different things depending on state
  // Calculations everybody does
  double focalPoint[4], pickPoint[4], prevPickPoint[4];
  double z;

436
437
438
  vtkRenderer *renderer = this->Interactor->FindPokedRenderer(X,Y);
  vtkCamera *camera = renderer->GetActiveCamera();
  if ( !camera )
439
440
441
442
443
    {
    return;
    }

  // Compute the two points defining the motion vector
444
  camera->GetFocalPoint(focalPoint);
445
446
447
  this->ComputeWorldToDisplay(focalPoint[0], focalPoint[1],
                              focalPoint[2], focalPoint);
  z = focalPoint[2];
Sebastien Barre's avatar
Sebastien Barre committed
448
  this->ComputeDisplayToWorld(double(this->Interactor->GetLastEventPosition()[0]),double(this->Interactor->GetLastEventPosition()[1]),
449
450
451
452
                              z, prevPickPoint);
  this->ComputeDisplayToWorld(double(X), double(Y), z, pickPoint);

  // Process the motion
Will Schroeder's avatar
Will Schroeder committed
453
  if ( this->State == vtkLineWidget::Moving )
454
455
    {
    // Okay to process
456
    if ( this->CurrentHandle )
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
      {
      if ( this->CurrentHandle == this->Handle[0] )
        {
        this->MovePoint1(prevPickPoint, pickPoint);
        }
      else if ( this->CurrentHandle == this->Handle[1] )
        {
        this->MovePoint2(prevPickPoint, pickPoint);
        }
      }
    else //must be moving the line
      {
      this->Translate(prevPickPoint, pickPoint);
      }
    }
Will Schroeder's avatar
Will Schroeder committed
472
  else if ( this->State == vtkLineWidget::Scaling )
473
474
475
476
477
    {
    this->Scale(prevPickPoint, pickPoint, X, Y);
    }

  // Interact, if desired
Will Schroeder's avatar
Will Schroeder committed
478
  this->EventCallbackCommand->SetAbortFlag(1);
479
480
481
482
  this->InvokeEvent(vtkCommand::InteractionEvent,NULL);
  this->Interactor->Render();
}

483
void vtkLineWidget::OnLeftButtonUp()
484
{
Will Schroeder's avatar
Will Schroeder committed
485
  if ( this->State == vtkLineWidget::Outside )
486
487
488
489
    {
    return;
    }

Will Schroeder's avatar
Will Schroeder committed
490
  this->State = vtkLineWidget::Start;
491
492
493
  this->HighlightHandle(NULL);
  this->HighlightLine(0);

Will Schroeder's avatar
Will Schroeder committed
494
  this->EventCallbackCommand->SetAbortFlag(1);
495
  this->EndInteraction();
496
  this->InvokeEvent(vtkCommand::EndInteractionEvent,NULL);
497

498
499
500
  this->Interactor->Render();
}

501
void vtkLineWidget::OnMiddleButtonDown()
502
503
504
{
  this->State = vtkLineWidget::Moving;

505
506
507
  int X = this->Interactor->GetEventPosition()[0];
  int Y = this->Interactor->GetEventPosition()[1];

508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  // Okay, we can process this. Try to pick handles first;
  // if no handles picked, then pick the bounding box.
  vtkAssemblyPath *path;
  this->Interactor->FindPokedRenderer(X,Y);
  this->HandlePicker->Pick(X,Y,0.0,this->CurrentRenderer);
  path = this->HandlePicker->GetPath();
  if ( path == NULL )
    {
    this->LinePicker->Pick(X,Y,0.0,this->CurrentRenderer);
    path = this->LinePicker->GetPath();
    if ( path == NULL )
      {
      this->State = vtkLineWidget::Outside;
      this->HighlightLine(0);
      return;
      }
    else
      {
      this->HighlightLine(1);
      }
    }
  
  this->EventCallbackCommand->SetAbortFlag(1);
531
  this->StartInteraction();
532
533
534
535
  this->InvokeEvent(vtkCommand::StartInteractionEvent,NULL);
  this->Interactor->Render();
}

536
void vtkLineWidget::OnMiddleButtonUp()
537
538
539
540
541
542
543
544
545
546
{
  if ( this->State == vtkLineWidget::Outside )
    {
    return;
    }

  this->State = vtkLineWidget::Start;
  this->HighlightLine(0);
  
  this->EventCallbackCommand->SetAbortFlag(1);
547
  this->EndInteraction();
548
549
550
551
  this->InvokeEvent(vtkCommand::EndInteractionEvent,NULL);
  this->Interactor->Render();
}

552
void vtkLineWidget::OnRightButtonDown()
553
{
Will Schroeder's avatar
Will Schroeder committed
554
  this->State = vtkLineWidget::Scaling;
555

556
557
558
  int X = this->Interactor->GetEventPosition()[0];
  int Y = this->Interactor->GetEventPosition()[1];

559
560
561
562
563
564
565
566
567
568
569
570
  // Okay, we can process this. Try to pick handles first;
  // if no handles picked, then pick the bounding box.
  vtkAssemblyPath *path;
  this->Interactor->FindPokedRenderer(X,Y);
  this->HandlePicker->Pick(X,Y,0.0,this->CurrentRenderer);
  path = this->HandlePicker->GetPath();
  if ( path == NULL )
    {
    this->LinePicker->Pick(X,Y,0.0,this->CurrentRenderer);
    path = this->LinePicker->GetPath();
    if ( path == NULL )
      {
Will Schroeder's avatar
Will Schroeder committed
571
      this->State = vtkLineWidget::Outside;
572
573
574
575
576
577
578
579
580
      this->HighlightLine(0);
      return;
      }
    else
      {
      this->HighlightLine(1);
      }
    }
  
Will Schroeder's avatar
Will Schroeder committed
581
  this->EventCallbackCommand->SetAbortFlag(1);
582
  this->StartInteraction();
583
584
585
586
  this->InvokeEvent(vtkCommand::StartInteractionEvent,NULL);
  this->Interactor->Render();
}

587
void vtkLineWidget::OnRightButtonUp()
588
{
Will Schroeder's avatar
Will Schroeder committed
589
  if ( this->State == vtkLineWidget::Outside )
590
591
592
593
    {
    return;
    }

Will Schroeder's avatar
Will Schroeder committed
594
  this->State = vtkLineWidget::Start;
595
596
  this->HighlightLine(0);
  
Will Schroeder's avatar
Will Schroeder committed
597
  this->EventCallbackCommand->SetAbortFlag(1);
598
  this->EndInteraction();
599
600
601
602
603
604
605
606
607
608
609
610
  this->InvokeEvent(vtkCommand::EndInteractionEvent,NULL);
  this->Interactor->Render();
}

void vtkLineWidget::MovePoint1(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];
  
611
  //int res = this->LineSource->GetResolution();
612
  float *pt1 = this->LineSource->GetPoint1();
613
614

  float point1[3];
615
616
617
  point1[0] = pt1[0] + v[0];
  point1[1] = pt1[1] + v[1];
  point1[2] = pt1[2] + v[2];
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
  
  this->LineSource->SetPoint1(point1);
  this->LineSource->Update();

  this->PositionHandles();
}

void vtkLineWidget::MovePoint2(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];
  
633
  //int res = this->LineSource->GetResolution();
634
  float *pt2 = this->LineSource->GetPoint2();
635
636

  float point2[3];
637
638
639
  point2[0] = pt2[0] + v[0];
  point2[1] = pt2[1] + v[1];
  point2[2] = pt2[2] + v[2];
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
  
  this->LineSource->SetPoint2(point2);
  this->LineSource->Update();

  this->PositionHandles();
}

// Loop through all points and translate them
void vtkLineWidget::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];
  
656
  //int res = this->LineSource->GetResolution();
657
658
  float *pt1 = this->LineSource->GetPoint1();
  float *pt2 = this->LineSource->GetPoint2();
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673

  float point1[3], point2[3];
  for (int i=0; i<3; i++)
    {
    point1[i] = pt1[i] + v[i];
    point2[i] = pt2[i] + v[i];
    }
  
  this->LineSource->SetPoint1(point1);
  this->LineSource->SetPoint2(point2);
  this->LineSource->Update();

  this->PositionHandles();
}

674
void vtkLineWidget::Scale(double *p1, double *p2, int vtkNotUsed(X), int Y)
675
676
677
678
679
680
681
{
  //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];

682
  //int res = this->LineSource->GetResolution();
683
684
685
  float *pt1 = this->LineSource->GetPoint1();
  float *pt2 = this->LineSource->GetPoint2();

686
687
688
689
690
691
692
  float center[3];
  center[0] = (pt1[0]+pt2[0]) / 2.0;
  center[1] = (pt1[1]+pt2[1]) / 2.0;
  center[2] = (pt1[2]+pt2[2]) / 2.0;

  // Compute the scale factor
  float sf = vtkMath::Norm(v) / sqrt(vtkMath::Distance2BetweenPoints(pt1,pt2));
Sebastien Barre's avatar
Sebastien Barre committed
693
  if ( Y > this->Interactor->GetLastEventPosition()[1] )
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
    {
    sf = 1.0 + sf;
    }
  else
    {
    sf = 1.0 - sf;
    }
  
  // Move the end points
  float point1[3], point2[3];
  for (int i=0; i<3; i++)
    {
    point1[i] = sf * (pt1[i] - center[i]) + center[i];
    point2[i] = sf * (pt2[i] - center[i]) + center[i];
    }

  this->LineSource->SetPoint1(point1);
  this->LineSource->SetPoint2(point2);
  this->LineSource->Update();

  this->PositionHandles();
}

void vtkLineWidget::CreateDefaultProperties()
{
  if ( ! this->HandleProperty )
    {
    this->HandleProperty = vtkProperty::New();
    this->HandleProperty->SetColor(1,1,1);
    }
  if ( ! this->SelectedHandleProperty )
    {
    this->SelectedHandleProperty = vtkProperty::New();
    this->SelectedHandleProperty->SetColor(1,0,0);
    }
  
  if ( ! this->LineProperty )
    {
    this->LineProperty = vtkProperty::New();
    this->LineProperty->SetRepresentationToWireframe();
    this->LineProperty->SetAmbient(1.0);
    this->LineProperty->SetAmbientColor(1.0,1.0,1.0);
    this->LineProperty->SetLineWidth(2.0);
    }
  if ( ! this->SelectedLineProperty )
    {
    this->SelectedLineProperty = vtkProperty::New();
    this->SelectedLineProperty->SetRepresentationToWireframe();
    this->SelectedLineProperty->SetAmbient(1.0);
    this->SelectedLineProperty->SetAmbientColor(0.0,1.0,0.0);
    this->SelectedLineProperty->SetLineWidth(2.0);
    }
}

Will Schroeder's avatar
Will Schroeder committed
748
void vtkLineWidget::PlaceWidget(float bds[6])
749
750
{
  int i;
Will Schroeder's avatar
Will Schroeder committed
751
  float bounds[6], center[3];
752

Will Schroeder's avatar
Will Schroeder committed
753
  this->AdjustBounds(bds, bounds, center);
754
  
755
  if ( this->Align == vtkLineWidget::YAxis )
756
757
758
759
    {
    this->LineSource->SetPoint1(center[0],bounds[2],center[2]);
    this->LineSource->SetPoint2(center[0],bounds[3],center[2]);
    }
760
  else if ( this->Align == vtkLineWidget::ZAxis )
761
762
763
764
    {
    this->LineSource->SetPoint1(center[0],center[1],bounds[4]);
    this->LineSource->SetPoint2(center[0],center[1],bounds[5]);
    }
765
  else if ( this->Align == vtkLineWidget::XAxis )//default or x-aligned
766
767
768
769
    {
    this->LineSource->SetPoint1(bounds[0],center[1],center[2]);
    this->LineSource->SetPoint2(bounds[1],center[1],center[2]);
    }
770
771
772
773
774
775
776
777
778
779
780
781
782
783
  this->LineSource->Update();

  // Position the handles at the end of the lines
  this->PositionHandles();

  for (i=0; i<6; i++)
    {
    this->InitialBounds[i] = bounds[i];
    }
  this->InitialLength = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
                             (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
                             (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
  for(i=0; i<2; i++)
    {
Will Schroeder's avatar
Will Schroeder committed
784
    this->HandleGeometry[i]->SetRadius(0.025*this->InitialLength);
785
786
787
    }
}