forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkChartXYZ.cxx
1661 lines (1477 loc) · 48.1 KB
/
vtkChartXYZ.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: vtkChartXYZ.h
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 "vtkChartXYZ.h"
#include "vtkAnnotationLink.h"
#include "vtkAxis.h"
#include "vtkCommand.h"
#include "vtkContext2D.h"
#include "vtkContext3D.h"
#include "vtkContextKeyEvent.h"
#include "vtkContextMouseEvent.h"
#include "vtkContextScene.h"
#include "vtkLookupTable.h"
#include "vtkMath.h"
#include "vtkPen.h"
#include "vtkPlane.h"
#include "vtkPlaneCollection.h"
#include "vtkPlot3D.h"
#include "vtkTable.h"
#include "vtkTextProperty.h"
#include "vtkTransform.h"
#include "vtkVector.h"
#include "vtkVectorOperators.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkIdTypeArray.h"
#include "vtkObjectFactory.h"
#include <sstream>
vtkStandardNewMacro(vtkChartXYZ)
//-----------------------------------------------------------------------------
vtkChartXYZ::vtkChartXYZ() : Geometry(0, 0, 10, 10), IsX(false), Angle(0)
{
this->Pen->SetWidth(5);
this->Pen->SetColor(0, 0, 0, 255);
this->AxisPen->SetWidth(1);
this->AxisPen->SetColor(0, 0, 0, 255);
this->Rotation->Identity();
this->Rotation->PostMultiply();
this->Translation->Identity();
this->Translation->PostMultiply();
this->Scale->Identity();
this->Scale->PostMultiply();
this->Interactive = true;
this->SceneWidth = 0;
this->SceneHeight = 0;
this->InitializeAxesBoundaryPoints();
this->AutoRotate = false;
this->DrawAxesDecoration = true;
this->FitToScene = true;
this->Axes.resize(3);
for(unsigned int i = 0; i < 3; ++i)
{
vtkNew<vtkAxis> axis;
this->Axes[i] = axis;
}
}
//-----------------------------------------------------------------------------
vtkChartXYZ::~vtkChartXYZ() = default;
//-----------------------------------------------------------------------------
void vtkChartXYZ::SetAngle(double angle)
{
this->Angle = angle;
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::SetAroundX(bool IsX_)
{
this->IsX = IsX_;
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::SetAutoRotate(bool b)
{
this->AutoRotate = b;
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::SetDecorateAxes(bool b)
{
this->DrawAxesDecoration = b;
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::SetAnnotationLink(vtkAnnotationLink *link)
{
if (this->Link != link)
{
this->Link = link;
this->Modified();
}
}
//-----------------------------------------------------------------------------
vtkAxis * vtkChartXYZ::GetAxis(int axis)
{
assert(axis >= 0 && axis < 3);
return this->Axes[axis];
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::SetAxis(int axisIndex, vtkAxis* axis)
{
assert(axisIndex >= 0 && axisIndex < 3);
this->Axes[axisIndex] = axis;
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::SetAxisColor(const vtkColor4ub& color)
{
this->AxisPen->SetColor(color);
this->Modified();
}
//-----------------------------------------------------------------------------
vtkColor4ub vtkChartXYZ::GetAxisColor()
{
return this->AxisPen->GetColorObject();
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::SetGeometry(const vtkRectf &bounds)
{
this->Geometry = bounds;
this->Axes[0]->SetPoint1(vtkVector2f(this->Geometry.GetX(),
this->Geometry.GetY()));
this->Axes[0]->SetPoint2(vtkVector2f(this->Geometry.GetX() + this->Geometry.GetWidth(),
this->Geometry.GetY()));
this->Axes[1]->SetPoint1(vtkVector2f(this->Geometry.GetX(),
this->Geometry.GetY()));
this->Axes[1]->SetPoint2(vtkVector2f(this->Geometry.GetX(),
this->Geometry.GetY() + this->Geometry.GetHeight()));
// Z is faked, largely to get valid ranges and rounded numbers...
this->Axes[2]->SetPoint1(vtkVector2f(this->Geometry.GetX(),
0));
if (this->IsX)
{
this->Axes[2]->SetPoint2(vtkVector2f(this->Geometry.GetX(),
this->Geometry.GetHeight()));
}
else
{
this->Axes[2]->SetPoint2(vtkVector2f(this->Geometry.GetX(),
this->Geometry.GetWidth()));
}
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::RecalculateBounds()
{
if (this->Plots.empty())
{
return;
}
double bounds[] = { VTK_DOUBLE_MAX, VTK_DOUBLE_MIN,
VTK_DOUBLE_MAX, VTK_DOUBLE_MIN,
VTK_DOUBLE_MAX, VTK_DOUBLE_MIN};
// Need to calculate the bounds in three dimensions and set up the axes.
for (unsigned int i = 0; i < this->Plots.size(); ++i)
{
std::vector<vtkVector3f> const& points = this->Plots[i]->GetPoints();
for (unsigned int j = 0; j < points.size(); ++j)
{
const vtkVector3f &v = points[j];
for (int k = 0; k < 3; ++k)
{
if (v[k] < bounds[2 * k])
{
bounds[2 * k] = v[k];
}
if (v[k] > bounds[2 * k + 1])
{
bounds[2 * k + 1] = v[k];
}
}
}
}
for (int i = 0; i < 3; ++i)
{
this->Axes[i]->SetUnscaledRange(&bounds[2*i]);
}
// Recalculate transform since axes' ranges were modified
this->RecalculateTransform();
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::PrintSelf(ostream &os, vtkIndent indent)
{
Superclass::PrintSelf(os, indent);
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::Update()
{
if (this->Link)
{
vtkSelection *selection =
vtkSelection::SafeDownCast(this->Link->GetOutputDataObject(2));
if (selection->GetNumberOfNodes())
{
vtkSelectionNode *node = selection->GetNode(0);
vtkIdTypeArray *idArray =
vtkArrayDownCast<vtkIdTypeArray>(node->GetSelectionList());
for (size_t i = 0; i < this->Plots.size(); ++i)
{
this->Plots[i]->SetSelection(idArray);
}
}
}
}
//-----------------------------------------------------------------------------
bool vtkChartXYZ::Paint(vtkContext2D *painter)
{
if (!this->Visible)
return false;
this->Update();
// Get the 3D context.
vtkContext3D *context = painter->GetContext3D();
if (!context)
return false;
this->Update();
// Check if the scene changed size
bool resizeHappened = false;
if (this->FitToScene)
{
resizeHappened = this->CheckForSceneResize();
}
// Calculate the transforms required for the current rotation.
this->CalculateTransforms();
// Set up clipping planes
for (int i = 0; i < 6; i++)
{
double planeEquation[4];
this->GetClippingPlaneEquation(i, planeEquation);
context->EnableClippingPlane(i, planeEquation);
}
// Draw plots
context->PushMatrix();
context->AppendTransform(this->ContextTransform);
this->PaintChildren(painter);
// Remove clipping planes
for (int i = 0; i < 6; i++)
{
context->DisableClippingPlane(i);
}
// Calculate the bounds of the data within the axes
this->ComputeDataBounds();
// Pop the ContextTransform now that we're done drawing data within the axes
context->PopMatrix();
// Draw the axes, tick marks, and labels
this->DrawAxes(context);
if(this->DrawAxesDecoration)
{
this->DetermineWhichAxesToLabel();
this->DrawTickMarks(painter);
this->DrawAxesLabels(painter);
}
// If necessary, rescale the axes so they fits our scene nicely
if (resizeHappened)
{
this->RescaleAxes();
}
return true;
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::DrawAxes(vtkContext3D *context)
{
context->PushMatrix();
context->AppendTransform(this->Box);
context->ApplyPen(this->AxisPen);
vtkVector3f box[4];
box[0] = vtkVector3f(0, 0, 0);
box[1] = vtkVector3f(0, 1, 0);
box[2] = vtkVector3f(1, 1, 0);
box[3] = vtkVector3f(1, 0, 0);
context->DrawLine(box[0], box[1]);
context->DrawLine(box[1], box[2]);
context->DrawLine(box[2], box[3]);
context->DrawLine(box[3], box[0]);
for (int i = 0; i < 4; ++i)
{
box[i].SetZ(1);
}
context->DrawLine(box[0], box[1]);
context->DrawLine(box[1], box[2]);
context->DrawLine(box[2], box[3]);
context->DrawLine(box[3], box[0]);
context->DrawLine(vtkVector3f(0, 0, 0), vtkVector3f(0, 0, 1));
context->DrawLine(vtkVector3f(1, 0, 0), vtkVector3f(1, 0, 1));
context->DrawLine(vtkVector3f(0, 1, 0), vtkVector3f(0, 1, 1));
context->DrawLine(vtkVector3f(1, 1, 0), vtkVector3f(1, 1, 1));
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::ComputeDataBounds()
{
double xMin = VTK_DOUBLE_MAX;
double xMax = VTK_DOUBLE_MIN;
double yMin = VTK_DOUBLE_MAX;
double yMax = VTK_DOUBLE_MIN;
float transformedPoint[3];
for (unsigned int i = 0; i < this->Plots.size(); ++i)
{
vtkPlot3D *plot = this->Plots[i];
// examine the eight corners of this plot's bounding cube
for (unsigned int j = 0; j < 8; ++j)
{
this->ContextTransform->TransformPoint(
plot->GetDataBounds()[j].GetData(), transformedPoint);
if (transformedPoint[0] < xMin)
{
xMin = transformedPoint[0];
}
if (transformedPoint[0] > xMax)
{
xMax = transformedPoint[0];
}
if (transformedPoint[1] < yMin)
{
yMin = transformedPoint[1];
}
if (transformedPoint[1] > yMax)
{
yMax = transformedPoint[1];
}
}
}
this->DataBounds[0] = xMin;
this->DataBounds[1] = yMin;
this->DataBounds[2] = xMax;
this->DataBounds[3] = yMax;
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::DrawAxesLabels(vtkContext2D *painter)
{
vtkContext3D *context = painter->GetContext3D();
// set up text property
vtkNew<vtkTextProperty> textProperties;
textProperties->SetJustificationToCentered();
textProperties->SetVerticalJustificationToCentered();
textProperties->SetColor(0.0, 0.0, 0.0);
textProperties->SetFontFamilyToArial();
textProperties->SetFontSize(14);
painter->ApplyTextProp(textProperties);
// if we're looking directly down any dimension, we shouldn't draw the
// corresponding label
bool shouldDrawAxis[3];
for (int axis = 0; axis < 3; ++axis)
{
shouldDrawAxis[axis] = true;
float start[3] = { 0, 0, 0 };
float end[3] = { 0, 0, 0 };
end[axis] = 1;
this->Box->TransformPoint(start, start);
this->Box->TransformPoint(end, end);
float axisLength = sqrt(
(end[0] - start[0]) * (end[0] - start[0]) +
(end[1] - start[1]) * (end[1] - start[1]));
if (axisLength == 0)
{
shouldDrawAxis[axis] = false;
}
}
float bounds[4];
float xLabelPos[3];
float yLabelPos[3];
float zLabelPos[3];
float offset[2] = {0, 0};
// calculate the pixel coordinates of the lines we wish to label
if (shouldDrawAxis[0])
{
xLabelPos[0] = 0.5;
xLabelPos[1] = this->XAxisToLabel[0];
xLabelPos[2] = this->XAxisToLabel[1];
this->Box->TransformPoint(xLabelPos, xLabelPos);
}
if (shouldDrawAxis[1])
{
yLabelPos[0] = this->YAxisToLabel[0];
yLabelPos[1] = 0.5;
yLabelPos[2] = this->YAxisToLabel[1];
this->Box->TransformPoint(yLabelPos, yLabelPos);
}
if (shouldDrawAxis[2])
{
zLabelPos[0] = this->ZAxisToLabel[0];
zLabelPos[1] = this->ZAxisToLabel[1];
zLabelPos[2] = 0.5;
this->Box->TransformPoint(zLabelPos, zLabelPos);
}
context->PopMatrix();
if (shouldDrawAxis[0])
{
painter->ComputeStringBounds(this->XAxisLabel, bounds);
this->GetOffsetForAxisLabel(0, bounds, offset);
xLabelPos[0] += (offset[0] + this->TickLabelOffset[0][0]);
xLabelPos[1] += (offset[1] + this->TickLabelOffset[0][1]);
painter->DrawString(xLabelPos[0], xLabelPos[1], this->XAxisLabel);
}
if (shouldDrawAxis[1])
{
painter->ComputeStringBounds(this->YAxisLabel, bounds);
offset[0] = 0;
offset[1] = 0;
this->GetOffsetForAxisLabel(1, bounds, offset);
yLabelPos[0] += (offset[0] + this->TickLabelOffset[1][0]);
yLabelPos[1] += (offset[1] + this->TickLabelOffset[1][1]);
painter->DrawString(yLabelPos[0], yLabelPos[1], this->YAxisLabel);
}
if (shouldDrawAxis[2])
{
painter->ComputeStringBounds(this->ZAxisLabel, bounds);
offset[0] = 0;
offset[1] = 0;
this->GetOffsetForAxisLabel(2, bounds, offset);
zLabelPos[0] += (offset[0] + this->TickLabelOffset[2][0]);
zLabelPos[1] += (offset[1] + this->TickLabelOffset[2][1]);
painter->DrawString(zLabelPos[0], zLabelPos[1], this->ZAxisLabel);
}
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::GetOffsetForAxisLabel(int axis, float *bounds,
float *offset)
{
offset[0] = 0;
offset[1] = 0;
switch (this->DirectionToData[axis])
{
// data is to the north
// offset is -y
case 0:
offset[1] = -bounds[3];
break;
// data is northeast
// offset is -x, -y
case 1:
offset[0] = -bounds[2];
offset[1] = -bounds[3];
break;
// data is east
// offset is -x
case 2:
offset[0] = -bounds[2];
break;
// data is southeast
// offset is -x, +y
case 3:
offset[0] = -bounds[2];
offset[1] = bounds[3];
break;
// data is south
// offset is +y
case 4:
offset[1] = bounds[3];
break;
// data is southwest
// offset is +x, +y
case 5:
offset[0] = bounds[2];
offset[1] = bounds[3];
break;
// data is west
// offset is +y
case 6:
offset[0] = bounds[2];
break;
// data is northwest
// offset is +x, -y
case 7:
default:
offset[0] = bounds[2];
offset[1] = -bounds[3];
break;
}
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::DrawTickMarks(vtkContext2D *painter)
{
vtkContext3D *context = painter->GetContext3D();
float bounds[4];
// draw points instead of lines
context->ApplyPen(this->Pen);
// treat each axis separately
for (int axis = 0; axis < 3; ++axis)
{
// pop matrix since we'll be drawing text in 2D before we draw the
// actual tick marks
context->PopMatrix();
float labelOffset[2] = { 0, 0 };
// initialize start and end of the axis to label in box coordinates
double startBox[3] = { 0, 0, 0 };
double endBox[3] = { 0, 0, 0 };
switch (axis)
{
case 0:
startBox[0] = 0;
endBox[0] = 1;
startBox[1] = endBox[1] = this->XAxisToLabel[0];
startBox[2] = endBox[2] = this->XAxisToLabel[1];
break;
case 1:
startBox[0] = this->YAxisToLabel[0];
startBox[1] = 0;
endBox[1] = 1;
startBox[2] = endBox[2] = this->YAxisToLabel[1];
break;
case 2:
default:
startBox[0] = endBox[0] = this->ZAxisToLabel[0];
startBox[1] = endBox[1] = this->ZAxisToLabel[1];
startBox[2] = 0;
endBox[2] = 1;
break;
}
// convert these values to pixel coordinates
double start[3];
double end[3];
this->Box->TransformPoint(startBox, start);
this->Box->TransformPoint(endBox, end);
// ...and then into data coordinates
this->ContextTransform->GetInverse()->TransformPoint(start, start);
this->ContextTransform->GetInverse()->TransformPoint(end, end);
// get "nice" values for min, max, and spacing (again, in data coordinates)
double tickSpacing =
this->CalculateNiceMinMax(start[axis], end[axis], axis);
if (tickSpacing == -1)
{
continue;
}
std::vector < vtkVector3f > tickPoints;
int currentTick = 0;
float tickPositionAlongAxis = start[axis];
while (tickPositionAlongAxis < end[axis])
{
vtkVector3f tick;
// convert tick position back into box coordinates
// during this process, we save the tick position in pixels for labeling
float tickPosition[3];
tickPosition[0] = start[0];
tickPosition[1] = start[1];
tickPosition[2] = start[2];
tickPosition[axis] = tickPositionAlongAxis;
float tickPositionInPixels[3];
this->ContextTransform->TransformPoint(tickPosition,
tickPositionInPixels);
this->Box->GetInverse()->TransformPoint(tickPositionInPixels,
tickPosition);
// determine the location of this tick mark and push it onto the vector
// if it falls within the bounds of the axis
tick[0] = startBox[0];
tick[1] = startBox[1];
tick[2] = startBox[2];
tick[axis] = tickPosition[axis];
if (tick[axis] >= startBox[axis] && tick[axis] <= endBox[axis])
{
tickPoints.push_back(tick);
// get the tick mark label
std::stringstream sstream;
sstream << std::fixed << setprecision(1) << tickPositionAlongAxis;
std::string tickLabel = sstream.str();
// offset the label from the axis
float offset[2] = {0, 0};
painter->ComputeStringBounds(tickLabel, bounds);
this->GetOffsetForAxisLabel(axis, bounds, offset);
tickPositionInPixels[0] += offset[0];
tickPositionInPixels[1] += offset[1];
// we store this offset so we know where to draw the axis label later
if (fabs(offset[0]) > fabs(labelOffset[0]))
{
labelOffset[0] = offset[0];
}
if (fabs(offset[1]) > fabs(labelOffset[1]))
{
labelOffset[1] = offset[1];
}
// draw the label for this tick mark
painter->DrawString(tickPositionInPixels[0], tickPositionInPixels[1],
tickLabel);
}
++currentTick;
tickPositionAlongAxis = start[axis] + (tickSpacing * currentTick);
}
// re-apply the Box matrix and draw the tick marks as points
if (!tickPoints.empty())
{
context->PushMatrix();
context->AppendTransform(this->Box);
context->DrawPoints(tickPoints[0].GetData(),
static_cast<int>(tickPoints.size()));
this->TickLabelOffset[axis][0] = labelOffset[0];
this->TickLabelOffset[axis][1] = labelOffset[1];
}
}
//revert from drawing points.
context->ApplyPen(this->AxisPen);
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::DetermineWhichAxesToLabel()
{
// for each dimension (XYZ)
for (int axis = 0; axis < 3; ++axis)
{
double maxDistance = -1;
// for each of the four "axis" lines corresponding to this dimension
for (float i = 0; i < 2; ++i)
{
for (float j = 0; j < 2; ++j)
{
for (float k = 0; k < 2; ++k)
{
// convert this line's midpoint to screen (pixel) coordinates
float midpoint[3] = { i, j, k };
midpoint[axis] = 0.5;
this->Box->TransformPoint(midpoint, midpoint);
// ignore any lines whose midpoint falls within the data range.
// we increment the iterators so we don't evaluate the same line
// twice.
if (midpoint[0] > this->DataBounds[0] &&
midpoint[1] > this->DataBounds[1] &&
midpoint[0] < this->DataBounds[2] &&
midpoint[1] < this->DataBounds[3])
{
switch (axis)
{
case 0:
++i;
break;
case 1:
++j;
break;
case 2:
++k;
break;
default:
break;
}
continue;
}
// calculate the distance from this line's midpoint to the data range
double d = 0;
int directionToData = 0;
// case 1: midpoint falls within x range (but not y)
if (midpoint[0] > this->DataBounds[0] &&
midpoint[0] < this->DataBounds[2])
{
double d1 = fabs(midpoint[1] - this->DataBounds[1]);
double d2 = fabs(midpoint[1] - this->DataBounds[3]);
if (d1 < d2)
{
directionToData = 0; // data is "up" from the axis
d = d1;
}
else
{
directionToData = 4; // data is "down" from the axis
d = d2;
}
}
// case 2: midpoint falls within y range (but not x)
else if (midpoint[1] > this->DataBounds[1] &&
midpoint[1] < this->DataBounds[3])
{
double d1 = fabs(midpoint[0] - this->DataBounds[0]);
double d2 = fabs(midpoint[0] - this->DataBounds[2]);
if (d1 < d2)
{
directionToData = 2; // data is "right" from the axis
d = d1;
}
else
{
directionToData = 6; // data is "left" from the axis
d = d2;
}
}
// case 3: compute distance to nearest corner
else
{
//x min, y min
d = sqrt( (this->DataBounds[0] - midpoint[0]) *
(this->DataBounds[0] - midpoint[0]) +
(this->DataBounds[1] - midpoint[1]) *
(this->DataBounds[1] - midpoint[1]) );
directionToData = 1; // data is to the northeast
//x min, y max
double d0 =
sqrt( (this->DataBounds[0] - midpoint[0]) *
(this->DataBounds[0] - midpoint[0]) +
(this->DataBounds[3] - midpoint[1]) *
(this->DataBounds[3] - midpoint[1]) );
if (d0 < d)
{
d = d0;
directionToData = 3; // data is to the southeast
}
//x max, y min
d0 = sqrt( (this->DataBounds[2] - midpoint[0]) *
(this->DataBounds[2] - midpoint[0]) +
(this->DataBounds[1] - midpoint[1]) *
(this->DataBounds[1] - midpoint[1]) );
if (d0 < d)
{
d = d0;
directionToData = 7; // data is to the northwest
}
//x max, y max
d0 = sqrt( (this->DataBounds[2] - midpoint[0]) *
(this->DataBounds[2] - midpoint[0]) +
(this->DataBounds[3] - midpoint[1]) *
(this->DataBounds[3] - midpoint[1]) );
if (d0 < d)
{
d = d0;
directionToData = 5; // data is to the southwest
}
// Test if the data falls within the bounds of our axis line,
// despite the fact that it is diagonal from the line's midpoint.
// This is performed to determine how the label should be offset
// from the line. To do this, we transform the line's start and
// end point to pixel coordinates.
float start[3] = { i, j, k };
start[axis] = 0;
this->Box->TransformPoint(start, start);
float end[3] = { i, j, k };
end[axis] = 1;
this->Box->TransformPoint(end, end);
if (start[0] < this->DataBounds[0] && end[0] > this->DataBounds[2])
{
// data falls within horizontal range of this axis line
// set directionToData as purely up or purely down
if (directionToData == 1 || directionToData == 7)
{
directionToData = 0;
}
else
{
directionToData = 4;
}
}
else if (start[1] < this->DataBounds[1] &&
end[1] > this->DataBounds[3])
{
// data falls within vertical range of this axis line
// set directionToData as purely left or purely right
if (directionToData == 1 || directionToData == 3)
{
directionToData = 2;
}
else
{
directionToData = 6;
}
}
}
// record this axis line if it has the greatest distance to the data
if (d > maxDistance)
{
this->DirectionToData[axis] = directionToData;
maxDistance = d;
switch (axis)
{
case 0:
this->XAxisToLabel[0] = static_cast<int>(j);
this->XAxisToLabel[1] = static_cast<int>(k);
break;
case 1:
this->YAxisToLabel[0] = static_cast<int>(i);
this->YAxisToLabel[1] = static_cast<int>(k);
break;
case 2:
default:
this->ZAxisToLabel[0] = static_cast<int>(i);
this->ZAxisToLabel[1] = static_cast<int>(j);
break;
}
}
// these three cases keep us from evaluating the same line twice.
if (axis == 2)
{
++k;
}
}
if (axis == 1)
{
++j;
}
}
if (axis == 0)
{
++i;
}
}
}
}
//-----------------------------------------------------------------------------
bool vtkChartXYZ::Hit(const vtkContextMouseEvent& vtkNotUsed(mouse))
{
if (!this->Interactive || !this->Visible || this->AutoRotate)
{
return false;
}
return true;
}
//-----------------------------------------------------------------------------
bool vtkChartXYZ::MouseButtonPressEvent(const vtkContextMouseEvent
&mouse)
{
if (mouse.GetButton() == vtkContextMouseEvent::LEFT_BUTTON)
{
return true;
}
return false;
}
//-----------------------------------------------------------------------------
bool vtkChartXYZ::MouseMoveEvent(const vtkContextMouseEvent &mouse)
{
if (mouse.GetButton() == vtkContextMouseEvent::LEFT_BUTTON)
{
if (mouse.GetModifiers() == vtkContextMouseEvent::SHIFT_MODIFIER)
{
return this->Spin(mouse);
}
else
{
return this->Rotate(mouse);
}
}
if (mouse.GetButton() == vtkContextMouseEvent::RIGHT_BUTTON)
{
if (mouse.GetModifiers() == vtkContextMouseEvent::SHIFT_MODIFIER)
{
return this->Pan(mouse);
}
else
{
return this->Zoom(mouse);
}
}
return false;
}
//-----------------------------------------------------------------------------
bool vtkChartXYZ::MouseWheelEvent(const vtkContextMouseEvent&,
int delta)
{
// Ten "wheels" to double/halve zoom level
float scaling = pow(2.0f, delta/10.0f);
this->Scale->Scale(scaling, scaling, scaling);
// Mark the scene as dirty
this->Scene->SetDirty(true);
this->InvokeEvent(vtkCommand::InteractionEvent);
return true;
}
//-----------------------------------------------------------------------------
void vtkChartXYZ::ZoomAxes(int delta)
{
float scaling = pow(2.0f, delta/10.0f);
this->BoxScale->Scale(scaling, scaling, scaling);
// Mark the scene as dirty
this->Scene->SetDirty(true);
}
//-----------------------------------------------------------------------------
bool vtkChartXYZ::Rotate(const vtkContextMouseEvent &mouse)
{
// avoid NaNs in our transformation matrix if the scene has not yet been
// rendered.
if (this->Scene->GetSceneHeight() == 0 || this->Scene->GetSceneWidth() == 0)
{
return false;
}
// Figure out how much the mouse has moved in plot coordinates
vtkVector2d screenPos(mouse.GetScreenPos().Cast<double>().GetData());
vtkVector2d lastScreenPos(mouse.GetLastScreenPos().Cast<double>().GetData());
double dx = screenPos[0] - lastScreenPos[0];
double dy = screenPos[1] - lastScreenPos[1];
double delta_elevation = -20.0 / this->Scene->GetSceneHeight();
double delta_azimuth = -20.0 / this->Scene->GetSceneWidth();
double rxf = -dx * delta_azimuth * 10.0;
double ryf = -dy * delta_elevation * 10.0;
this->Rotation->RotateY(rxf);
this->Rotation->RotateX(-ryf);
// Mark the scene as dirty
this->Scene->SetDirty(true);
this->InvokeEvent(vtkCommand::InteractionEvent);
return true;
}
//-----------------------------------------------------------------------------
bool vtkChartXYZ::Pan(const vtkContextMouseEvent &mouse)
{
// Figure out how much the mouse has moved in plot coordinates
vtkVector2d screenPos(mouse.GetScreenPos().Cast<double>().GetData());
vtkVector2d lastScreenPos(mouse.GetLastScreenPos().Cast<double>().GetData());