RenderingOption.cpp 7.03 KB
Newer Older
1
/*****************************************************************************
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2012 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/
25

26
#include "RenderingOption.h"
27
28
29
30


#include <MeshComponent.h>
#include <InteractiveViewer.h>
31
#include <complex>
32

33
using namespace camitk;
34

35
36
37
38
39
40
41
//-- normal generation
#include <vtkDataSetSurfaceFilter.h>
#include <vtkPolyDataNormals.h>
#include <vtkDataSetMapper.h>
#include <vtkGlyph3D.h>
#include <vtkArrowSource.h>

42
43

// --------------- constructor -------------------
44
45
46
RenderingOption::RenderingOption(ActionExtension* extension) : Action(extension) {
    setName("Rendering Option");
    setIcon(QPixmap(":/renderingOption"));
47
    setDescription("Change the rendering option (surface, wireframe, points, label...)");
48
49
50
51
52
53
54
55
56
57
    setComponent("MeshComponent");
    setFamily("Basic Mesh");
    addTag("RenderingMode");
    addTag("Label");

    setProperty("Surface", false);
    setProperty("Wireframe", false);
    setProperty("Points", false);
    setProperty("Label", false);
    setProperty("Glyph", false);
58
    setProperty("Normals", false);
59
60
}

61
RenderingOption::~RenderingOption() {
62
    // do not delete the widget has it might have been used in the ActionViewer (i.e. the ownership might have been taken by the stacked widget)
63
64
65
}

// --------------- getWidget -------------------
66
QWidget * RenderingOption::getWidget() {
67

68
    //-- init the property values using the selection state
69
    if (getTargets().size() == 1) {
70
        // only one selected, easy!
71
72
73
        setProperty("Surface", (bool) (getTargets().first()->getRenderingModes() & InterfaceGeometry::Surface));
        setProperty("Wireframe", (bool) (getTargets().first()->getRenderingModes() & InterfaceGeometry::Wireframe));
        setProperty("Points", (bool) (getTargets().first()->getRenderingModes() & InterfaceGeometry::Points));
74
75
76
    }
    else {
        // more than one: they should all have the same rendering mode to set a proper state
77
        InterfaceGeometry::RenderingModes m = getTargets().first()->getRenderingModes();
78
        int i = 1;
79

80
        while (i < getTargets().size() && (m == getTargets()[i]->getRenderingModes()))
81
82
            i++;

83
        if (i == getTargets().size()) {
84
85
86
87
88
89
            setProperty("Surface", (bool) (m & InterfaceGeometry::Surface));
            setProperty("Wireframe", (bool) (m & InterfaceGeometry::Wireframe));
            setProperty("Points", (bool) (m & InterfaceGeometry::Points) );
        }
    }

90
    // additional prop visibility
91
92
93
    if (getTargets().size()>0) {
        setProperty("Glyph", (bool) (getTargets().first()->getProp("glyph")->GetVisibility()));
        setProperty("Label", (bool) (getTargets().first()->getProp("label")->GetVisibility()));
94
95
        // normal is an extra prop build by this action if needed, check if it exists first)
        setProperty("Normals", getTargets().first()->getProp("normals")!=NULL && (bool) (getTargets().first()->getProp("normals")->GetVisibility()));
96
    }
97

98
    return Action::getWidget();
99
100
101
102
};


// --------------- apply -------------------
103
Action::ApplyStatus RenderingOption::apply() {
104
105
    // get the current rendering mode from the property values
    InterfaceGeometry::RenderingModes m = InterfaceGeometry::None;
106

107
108
    if (property("Surface").toBool())
        m |= InterfaceGeometry::Surface;
109

110
111
    if (property("Wireframe").toBool())
        m |= InterfaceGeometry::Wireframe;
112

113
114
115
116
    if (property("Points").toBool())
        m |= InterfaceGeometry::Points;

    // update the rendering mode of selected
117
    foreach(Component *comp, getTargets()) {
118
        comp->setRenderingModes(m);
119
120
121
        // set the additional prop visibility
        comp->getProp("glyph")->SetVisibility(property("Glyph").toBool());
        comp->getProp("label")->SetVisibility(property("Label").toBool());
122
123

        // normal is a specific prop, create it only if needed
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
        if (comp->getProp("normals") == NULL) {
            if (property("Normals").toBool()) {

                //-- get the surface (in order to have polygon only)
                vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter = vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
                surfaceFilter->SetInput(comp->getPointSet());
                surfaceFilter->Update();

                //-- generate the normals
                vtkSmartPointer<vtkPolyDataNormals> normalGenerator = vtkSmartPointer<vtkPolyDataNormals>::New();
                normalGenerator->SetInputConnection(surfaceFilter->GetOutputPort());
                normalGenerator->ComputePointNormalsOn();
                normalGenerator->ComputeCellNormalsOff();
                normalGenerator->SplittingOff();
                normalGenerator->Update();

                //-- create the arrow glyph for the surface normal
                vtkSmartPointer<vtkArrowSource> arrowSource = vtkSmartPointer<vtkArrowSource>::New();
                vtkSmartPointer<vtkGlyph3D> arrowGlyph = vtkSmartPointer<vtkGlyph3D>::New();
                arrowGlyph->ScalingOn();
                arrowGlyph->SetScaleFactor(comp->getBoundingRadius()/10.0);
                arrowGlyph->SetVectorModeToUseNormal();
                arrowGlyph->SetScaleModeToScaleByVector();
                arrowGlyph->OrientOn();
                arrowGlyph->SetSourceConnection(arrowSource->GetOutputPort());
                arrowGlyph->SetInputConnection(normalGenerator->GetOutputPort());

                vtkSmartPointer<vtkDataSetMapper> normalMapper = vtkSmartPointer<vtkDataSetMapper>::New();
                normalMapper->SetInputConnection(arrowGlyph->GetOutputPort());

                vtkSmartPointer<vtkActor> normalActor = vtkSmartPointer<vtkActor>::New();
                normalActor->SetMapper(normalMapper);

                //-- add the prop to the Component
                comp->addProp("normals",normalActor);
                comp->getProp("normals")->SetVisibility(true);
            }
            // else: nothing todo there is no normal to display anyway
        }
        else {
            // check weither to show the normals or no
            comp->getProp("normals")->SetVisibility(property("Normals").toBool());
166
167
        }

168
        
169
170
    }

171
    // refresh the viewers (here only the viewer of the last component, but should be ok)
172
    getTargets().last()->refresh();
173

174
    return SUCCESS;
175
176

}