1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2004-2012 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #include <cassert> |
---|
9 | |
---|
10 | #include <vtkDataSet.h> |
---|
11 | #include <vtkPolyData.h> |
---|
12 | #include <vtkPolyDataMapper.h> |
---|
13 | #include <vtkActor.h> |
---|
14 | #include <vtkProperty.h> |
---|
15 | #include <vtkTransform.h> |
---|
16 | #include <vtkTransformPolyDataFilter.h> |
---|
17 | #include <vtkCubeSource.h> |
---|
18 | #include <vtkReverseSense.h> |
---|
19 | #include <vtkVector.h> |
---|
20 | |
---|
21 | #include "Parallelepiped.h" |
---|
22 | #include "Trace.h" |
---|
23 | |
---|
24 | using namespace VtkVis; |
---|
25 | |
---|
26 | Parallelepiped::Parallelepiped() : |
---|
27 | Shape() |
---|
28 | { |
---|
29 | } |
---|
30 | |
---|
31 | Parallelepiped::~Parallelepiped() |
---|
32 | { |
---|
33 | TRACE("Deleting Parallelepiped"); |
---|
34 | } |
---|
35 | |
---|
36 | void Parallelepiped::update() |
---|
37 | { |
---|
38 | if (_box == NULL) { |
---|
39 | _box = vtkSmartPointer<vtkCubeSource>::New(); |
---|
40 | _box->SetCenter(0.5, 0.5, 0.5); |
---|
41 | _box->SetXLength(1); |
---|
42 | _box->SetYLength(1); |
---|
43 | _box->SetZLength(1); |
---|
44 | } |
---|
45 | |
---|
46 | if (_pdMapper == NULL) { |
---|
47 | _pdMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); |
---|
48 | _pdMapper->SetResolveCoincidentTopologyToPolygonOffset(); |
---|
49 | _pdMapper->ScalarVisibilityOff(); |
---|
50 | } |
---|
51 | |
---|
52 | if (_transform == NULL) { |
---|
53 | _transform = vtkSmartPointer<vtkTransform>::New(); |
---|
54 | } |
---|
55 | |
---|
56 | vtkSmartPointer<vtkTransformPolyDataFilter> filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New(); |
---|
57 | filter->SetInputConnection(_box->GetOutputPort()); |
---|
58 | filter->SetTransform(_transform); |
---|
59 | _pdMapper->SetInputConnection(filter->GetOutputPort()); |
---|
60 | |
---|
61 | initProp(); |
---|
62 | |
---|
63 | getActor()->SetMapper(_pdMapper); |
---|
64 | _pdMapper->Update(); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * \brief Create parallelepiped from 3 basis vectors |
---|
69 | * |
---|
70 | * This method expects the order of vectors to define a right-handed coordinate |
---|
71 | * system, so ((vec1 cross vec2) dot vec3) should be positive. |
---|
72 | */ |
---|
73 | void Parallelepiped::setVectors(double vec1[3], double vec2[3], double vec3[3]) |
---|
74 | { |
---|
75 | vtkVector3d v1(vec1); |
---|
76 | vtkVector3d v2(vec2); |
---|
77 | vtkVector3d v3(vec3); |
---|
78 | vtkVector3d tmp = v1.Cross(v2); |
---|
79 | if (tmp.Dot(v3) < 0) { |
---|
80 | // Got left-handed system, convert to right-handed |
---|
81 | v2.Set(vec1[0], vec1[1], vec1[2]); |
---|
82 | v1.Set(vec2[0], vec2[1], vec2[2]); |
---|
83 | } |
---|
84 | TRACE("v1: %g %g %g", v1[0], v1[1], v1[2]); |
---|
85 | TRACE("v2: %g %g %g", v2[0], v2[1], v2[2]); |
---|
86 | TRACE("v3: %g %g %g", v3[0], v3[1], v3[2]); |
---|
87 | double matrix[16]; |
---|
88 | memset(matrix, 0, sizeof(double)*16); |
---|
89 | matrix[0] = v1[0]; |
---|
90 | matrix[4] = v1[1]; |
---|
91 | matrix[8] = v1[2]; |
---|
92 | matrix[1] = v2[0]; |
---|
93 | matrix[5] = v2[1]; |
---|
94 | matrix[9] = v2[2]; |
---|
95 | matrix[2] = v3[0]; |
---|
96 | matrix[6] = v3[1]; |
---|
97 | matrix[10] = v3[2]; |
---|
98 | matrix[15] = 1; |
---|
99 | _transform->SetMatrix(matrix); |
---|
100 | } |
---|
101 | |
---|
102 | void Parallelepiped::flipNormals(bool state) |
---|
103 | { |
---|
104 | if (_box == NULL || _pdMapper == NULL) |
---|
105 | return; |
---|
106 | |
---|
107 | if (state) { |
---|
108 | vtkSmartPointer<vtkReverseSense> filter = vtkSmartPointer<vtkReverseSense>::New(); |
---|
109 | filter->ReverseCellsOn(); |
---|
110 | filter->ReverseNormalsOn(); |
---|
111 | filter->SetInputConnection(_box->GetOutputPort()); |
---|
112 | |
---|
113 | _pdMapper->SetInputConnection(filter->GetOutputPort()); |
---|
114 | } else { |
---|
115 | _pdMapper->SetInputConnection(_box->GetOutputPort()); |
---|
116 | } |
---|
117 | } |
---|