NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxFiberBundleRep.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 
34 #include "cxFiberBundleRep.h"
35 
36 #include "boost/bind.hpp"
37 #include <vtkActor.h>
38 #include <vtkProperty.h>
39 #include <vtkPolyDataMapper.h>
40 #include <vtkPolyDataNormals.h>
41 #include <vtkMatrix4x4.h>
42 #include <vtkRenderer.h>
43 #include <vtkRibbonFilter.h>
44 
45 #include "cxGraphicalPrimitives.h"
46 #include "cxView.h"
47 #include "cxTypeConversions.h"
48 #include "cxMesh.h"
49 
50 namespace cx
51 {
54  : RepImpl(), mFiberWidth(.5)
55 {
56  mPolyDataMapper = vtkPolyDataMapperPtr::New();
57  mProperty = vtkPropertyPtr::New();
58  // mProperty->SetInterpolationToFlat();
59  // mProperty->SetInterpolationToGouraud();
60 
61  mActor = vtkActorPtr::New();
62 
63  mActor->SetMapper(mPolyDataMapper);
64  mActor->SetProperty(mProperty);
65 }
66 
71 {
72  return wrap_new(new FiberBundleRep(), uid);
73 }
74 
76 QString FiberBundleRep::getType() const
77 {
78  return "FiberBundleRep";
79 }
80 
85 {
86  // Already has this bundle assigned
87  if (hasBundle(bundle)) return;
88 
89  std::cout << "New fiber bundle assigned" << std::endl;
90 
91  if (mBundle)
92  {
93  disconnect(mBundle.get(), SIGNAL(transformChanged()), this, SLOT(bundleTransformChanged()));
94  disconnect(mBundle.get(), SIGNAL(meshChanged()), this, SLOT(bundleChanged()));
95  }
96 
97  mBundle = bundle;
98 
99  if (mBundle)
100  {
101  connect(mBundle.get(), SIGNAL(transformChanged()), this, SLOT(bundleTransformChanged()));
102  connect(mBundle.get(), SIGNAL(meshChanged()), this, SLOT(bundleChanged()));
103  }
104 
105  bundleChanged();
106  this->bundleTransformChanged();
107 }
108 
113 {
114  view->getRenderer()->AddActor(mActor);
115 
116  if (mViewportListener)
117  mViewportListener->startListen(view->getRenderer());
118 }
119 
124 {
125  view->getRenderer()->RemoveActor(mActor);
126 
127  if (mViewportListener)
128  mViewportListener->stopListen();
129 }
130 
132 void FiberBundleRep::bundleChanged()
133 {
134  vtkPolyDataPtr model = mBundle->getVtkPolyData();
135 
136  if (model)
137  {
138  QColor color = mBundle->getColor();
139  mActor->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
140  mActor->GetProperty()->SetOpacity(color.alphaF());
141 
142  if (!mBundle->getIsWireframe())
143  {
147  vtkSmartPointer<vtkRibbonFilter> ribbonFilter = vtkSmartPointer<vtkRibbonFilter>::New();
148  ribbonFilter->SetInputData(model);
149  ribbonFilter->SetWidth(mFiberWidth);
150  ribbonFilter->SetWidthFactor(3);
151 
152  mPolyDataMapper->SetInputConnection(ribbonFilter->GetOutputPort());
153  }
154  else
155  {
156  {
157  vtkPolyDataNormalsPtr normals = vtkPolyDataNormalsPtr::New();
158  normals->SetInputData(model);
159  normals->Update();
160  model = normals->GetOutput();
161  }
162  mPolyDataMapper->SetInputData(model);
163  mActor->SetMapper(mPolyDataMapper);
164  }
165 
166  mPolyDataMapper->ScalarVisibilityOn();
167  mPolyDataMapper->SetScalarModeToUsePointFieldData();
168  }
169 
170 }
171 
174 void FiberBundleRep::bundleTransformChanged()
175 {
176  if (!mBundle) return;
177 
178  mActor->SetUserMatrix(mBundle->get_rMd().getVtkMatrix());
179 }
180 
181 } // end namespace
boost::shared_ptr< class FiberBundleRep > FiberBundleRepPtr
virtual void removeRepActorsFromViewRenderer(ViewPtr view)
virtual void setBundle(MeshPtr bundle)
static boost::shared_ptr< REP > wrap_new(REP *object, QString uid)
Definition: cxRepImpl.h:83
static FiberBundleRepPtr New(const QString &uid="")
boost::shared_ptr< class View > ViewPtr
virtual bool hasBundle(MeshPtr bundle) const
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
virtual QString getType() const
vtkSmartPointer< class vtkPolyDataNormals > vtkPolyDataNormalsPtr
virtual void addRepActorsToViewRenderer(ViewPtr view)
Default implementation of Rep.
Definition: cxRepImpl.h:63
boost::shared_ptr< class Mesh > MeshPtr