Fraxinus  17.12-rc1
An IGT application
cxGeometricRep.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 "cxGeometricRep.h"
35 
36 #include <vtkPolyData.h>
37 #include <vtkPointData.h>
38 #include <vtkProperty.h>
39 #include <vtkActor.h>
40 #include <vtkRenderer.h>
41 #include <vtkMatrix4x4.h>
42 #include <vtkArrowSource.h>
43 #include <vtkPlane.h>
44 
45 #include "cxMesh.h"
46 #include "cxView.h"
47 
48 #include "cxTypeConversions.h"
49 #include "cxVtkHelperClasses.h"
50 
51 namespace cx
52 {
53 
55 {
56  m_rMrr = Transform3D::Identity();
59 }
61 {
62 }
63 
65 {
66  if (mesh == mMesh)
67  return;
68  if (mMesh)
69  {
70  disconnect(mMesh.get(), &Mesh::meshChanged, this, &GraphicalGeometric::meshChangedSlot);
71  disconnect(mMesh.get(), &Data::transformChanged, this, &GraphicalGeometric::transformChangedSlot);
72  disconnect(mMesh.get(), &Data::clipPlanesChanged, this, &GraphicalGeometric::clipPlanesChangedSlot);
73  }
74  mMesh = mesh;
75  if (mMesh)
76  {
77  connect(mMesh.get(), &Mesh::meshChanged, this, &GraphicalGeometric::meshChangedSlot);
78  connect(mMesh.get(), &Data::transformChanged, this, &GraphicalGeometric::transformChangedSlot);
79  connect(mMesh.get(), &Data::clipPlanesChanged, this, &GraphicalGeometric::clipPlanesChangedSlot);
80  this->meshChangedSlot();
81  this->transformChangedSlot();
82  this->clipPlanesChangedSlot();
83  }
84 }
85 
86 //Copied from ImageMapperMonitor (used by VolumetricRep)
87 void GraphicalGeometric::clipPlanesChangedSlot()
88 {
89  this->clearClipping();
90 
91  if (!mMesh)
92  return;
93 
94  std::vector<vtkPlanePtr> mPlanes;
95  mPlanes = mMesh->getAllClipPlanes();
96  for (unsigned i=0; i<mPlanes.size(); ++i)
97  {
98  mGraphicalPolyDataPtr->getMapper()->AddClippingPlane(mPlanes[i]);
99  }
100 }
101 
103 {
104  if (!mMesh)
105  return;
106 
107  mGraphicalPolyDataPtr->getMapper()->RemoveAllClippingPlanes();
108 }
109 
111 {
112  return mMesh;
113 }
114 
116 {
117  mGraphicalPolyDataPtr->setRenderer(renderer);
118  mGraphicalGlyph3DDataPtr->setRenderer(renderer);
119 }
120 
122 {
123  m_rMrr = rMrr;
124  this->transformChangedSlot();
125 }
126 
127 void GraphicalGeometric::meshChangedSlot()
128 {
129  mGraphicalGlyph3DDataPtr->setVisibility(mMesh->showGlyph());
130  if(mMesh->showGlyph())
131  {
132  mGraphicalGlyph3DDataPtr->setData(mMesh->getVtkPolyData());
133  mGraphicalGlyph3DDataPtr->setOrientationArray(mMesh->getOrientationArray());
134  mGraphicalGlyph3DDataPtr->setColorArray(mMesh->getColorArray());
135  mGraphicalGlyph3DDataPtr->setColor(mMesh->getColor().redF(), mMesh->getColor().greenF(), mMesh->getColor().blueF());
136  mGraphicalGlyph3DDataPtr->setLUT(mMesh->getGlyphLUT());
137  mGraphicalGlyph3DDataPtr->setScaleFactor(mMesh->getVisSize());
138  }
139 
140  mMesh->updateVtkPolyDataWithTexture();
141  mGraphicalPolyDataPtr->setData(mMesh->getVtkPolyData());
142  mGraphicalPolyDataPtr->setTexture(mMesh->getVtkTexture());
143 
144  mGraphicalPolyDataPtr->setOpacity(1.0);
145  mGraphicalPolyDataPtr->setScalarVisibility(mMesh->getUseColorFromPolydataScalars());
146  if(!mMesh->getUseColorFromPolydataScalars())
147  {
148  mGraphicalPolyDataPtr->setColor(mMesh->getColor().redF(), mMesh->getColor().greenF(), mMesh->getColor().blueF());
149  mGraphicalPolyDataPtr->setOpacity(mMesh->getColor().alphaF());
150  }
151 
152  //Set other properties
153  vtkPropertyPtr dest = mGraphicalPolyDataPtr->getProperty();
154  const MeshPropertyData& src = mMesh->getProperties();
155 
156  dest->SetPointSize(src.mVisSize->getValue());
157  dest->SetBackfaceCulling(src.mBackfaceCulling->getValue());
158  dest->SetFrontfaceCulling(src.mFrontfaceCulling->getValue());
159  dest->SetRepresentation(src.mRepresentation->getValue().toInt());
160  dest->SetEdgeVisibility(src.mEdgeVisibility->getValue());
161  dest->SetEdgeColor(cx::getColorAsVector3D(src.mEdgeColor->getValue()).begin());
162  dest->SetAmbient(src.mAmbient->getValue());
163  dest->SetDiffuse(src.mDiffuse->getValue());
164  dest->SetSpecular(src.mSpecular->getValue());
165  dest->SetSpecularPower(src.mSpecularPower->getValue());
166 }
167 
170 void GraphicalGeometric::transformChangedSlot()
171 {
172  if (!mMesh)
173  {
174  return;
175  }
176 
177  Transform3D rrMd = mMesh->get_rMd();
178  Transform3D rMd = m_rMrr * rrMd;
179 
180  mGraphicalPolyDataPtr->setUserMatrix(rMd.getVtkMatrix());
181  mGraphicalGlyph3DDataPtr->setUserMatrix(rMd.getVtkMatrix());
182 }
183 
184 
185 //---------------------------------------------------------
186 //---------------------------------------------------------
187 //---------------------------------------------------------
188 
189 
191  RepImpl()
192 {
193  mGraphics.reset(new GraphicalGeometric());
194 }
196 {
197 }
199 {
200  return wrap_new(new GeometricRep(), uid);
201 }
202 
204 {
205  mGraphics->setRenderer(view->getRenderer());
206 }
207 
209 {
210  mGraphics->setRenderer(NULL);
211 }
212 
214 {
215  mGraphics->setMesh(mesh);
216 }
217 
219 {
220  return mGraphics->getMesh();
221 }
223 {
224  return mGraphics->getMesh() == mesh;
225 }
226 
227 //---------------------------------------------------------
228 }
229 // namespace cx
230 //---------------------------------------------------------
Vector3D getColorAsVector3D(QColor color)
DoublePropertyPtr mVisSize
Scalar * begin()
Display one Mesh in 3D.
vtkSmartPointer< class vtkProperty > vtkPropertyPtr
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
BoolPropertyPtr mFrontfaceCulling
DoublePropertyPtr mAmbient
void transformChanged()
emitted when transform is changed
void setRenderer(vtkRendererPtr renderer)
DoublePropertyPtr mSpecularPower
void setMesh(MeshPtr mesh)
sets this reps mesh
static boost::shared_ptr< REP > wrap_new(REP *object, QString uid)
Definition: cxRepImpl.h:83
DoublePropertyPtr mDiffuse
boost::shared_ptr< class View > ViewPtr
void clipPlanesChanged()
bool hasMesh(MeshPtr mesh) const
checks if this rep has the give mesh
StringPropertyPtr mRepresentation
MeshPtr getMesh()
gives this reps mesh
vtkSmartPointer< class vtkRenderer > vtkRendererPtr
Helper for rendering a a polydata in 3D.
virtual void addRepActorsToViewRenderer(ViewPtr view)
virtual void removeRepActorsFromViewRenderer(ViewPtr view)
void meshChanged()
BoolPropertyPtr mBackfaceCulling
GraphicalPolyData3DPtr mGraphicalPolyDataPtr
static GeometricRepPtr New(const QString &uid="")
Default implementation of Rep.
Definition: cxRepImpl.h:63
void setTransformOffset(Transform3D rMrr)
ColorPropertyPtr mEdgeColor
GraphicalGlyph3DDataPtr mGraphicalGlyph3DDataPtr
BoolPropertyPtr mEdgeVisibility
Helper for rendering a a glyph in 3D.
boost::shared_ptr< class Mesh > MeshPtr
MeshPtr getMesh()
gives this reps mesh
void setMesh(MeshPtr mesh)
sets this reps mesh
DoublePropertyPtr mSpecular
boost::shared_ptr< class GeometricRep > GeometricRepPtr
Namespace for all CustusX production code.