CustusX  20.03-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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 
13 #include "cxGeometricRep.h"
14 
15 #include <vtkPolyData.h>
16 #include <vtkPointData.h>
17 #include <vtkProperty.h>
18 #include <vtkActor.h>
19 #include <vtkRenderer.h>
20 #include <vtkMatrix4x4.h>
21 #include <vtkArrowSource.h>
22 #include <vtkPlane.h>
23 
24 #include "cxMesh.h"
25 #include "cxView.h"
26 
27 #include "cxTypeConversions.h"
28 #include "cxVtkHelperClasses.h"
29 
30 namespace cx
31 {
32 
34 {
35  m_rMrr = Transform3D::Identity();
38 }
40 {
41 }
42 
44 {
45  if (mesh == mMesh)
46  return;
47  if (mMesh)
48  {
49  disconnect(mMesh.get(), &Mesh::meshChanged, this, &GraphicalGeometric::meshChangedSlot);
50  disconnect(mMesh.get(), &Data::transformChanged, this, &GraphicalGeometric::transformChangedSlot);
51  disconnect(mMesh.get(), &Data::clipPlanesChanged, this, &GraphicalGeometric::clipPlanesChangedSlot);
52  }
53  mMesh = mesh;
54  if (mMesh)
55  {
56  connect(mMesh.get(), &Mesh::meshChanged, this, &GraphicalGeometric::meshChangedSlot);
57  connect(mMesh.get(), &Data::transformChanged, this, &GraphicalGeometric::transformChangedSlot);
58  connect(mMesh.get(), &Data::clipPlanesChanged, this, &GraphicalGeometric::clipPlanesChangedSlot);
59  this->meshChangedSlot();
60  this->transformChangedSlot();
61  this->clipPlanesChangedSlot();
62  }
63 }
64 
65 //Copied from ImageMapperMonitor (used by VolumetricRep)
66 void GraphicalGeometric::clipPlanesChangedSlot()
67 {
68  this->clearClipping();
69 
70  if (!mMesh)
71  return;
72 
73  std::vector<vtkPlanePtr> mPlanes;
74  mPlanes = mMesh->getAllClipPlanes();
75  for (unsigned i=0; i<mPlanes.size(); ++i)
76  {
77  mGraphicalPolyDataPtr->getMapper()->AddClippingPlane(mPlanes[i]);
78  }
79 }
80 
82 {
83  if (!mMesh)
84  return;
85 
86  mGraphicalPolyDataPtr->getMapper()->RemoveAllClippingPlanes();
87 }
88 
90 {
91  return mMesh;
92 }
93 
95 {
96  mGraphicalPolyDataPtr->setRenderer(renderer);
97  mGraphicalGlyph3DDataPtr->setRenderer(renderer);
98 }
99 
101 {
102  m_rMrr = rMrr;
103  this->transformChangedSlot();
104 }
105 
106 void GraphicalGeometric::meshChangedSlot()
107 {
108  mGraphicalGlyph3DDataPtr->setVisibility(mMesh->showGlyph());
109  if(mMesh->showGlyph())
110  {
111  mGraphicalGlyph3DDataPtr->setData(mMesh->getVtkPolyData());
112  mGraphicalGlyph3DDataPtr->setOrientationArray(mMesh->getOrientationArray());
113  mGraphicalGlyph3DDataPtr->setColorArray(mMesh->getColorArray());
114  mGraphicalGlyph3DDataPtr->setColor(mMesh->getColor().redF(), mMesh->getColor().greenF(), mMesh->getColor().blueF());
115  mGraphicalGlyph3DDataPtr->setLUT(mMesh->getGlyphLUT());
116  mGraphicalGlyph3DDataPtr->setScaleFactor(mMesh->getVisSize());
117  }
118 
119  mMesh->updateVtkPolyDataWithTexture();
120  mGraphicalPolyDataPtr->setData(mMesh->getVtkPolyData());
121  mGraphicalPolyDataPtr->setTexture(mMesh->getVtkTexture());
122 
123  mGraphicalPolyDataPtr->setOpacity(1.0);
124  mGraphicalPolyDataPtr->setScalarVisibility(mMesh->getUseColorFromPolydataScalars());
125  if(!mMesh->getUseColorFromPolydataScalars())
126  {
127  mGraphicalPolyDataPtr->setColor(mMesh->getColor().redF(), mMesh->getColor().greenF(), mMesh->getColor().blueF());
128  mGraphicalPolyDataPtr->setOpacity(mMesh->getColor().alphaF());
129  }
130 
131  //Set other properties
132  vtkPropertyPtr dest = mGraphicalPolyDataPtr->getProperty();
133  const MeshPropertyData& src = mMesh->getProperties();
134 
135  dest->SetPointSize(src.mVisSize->getValue());
136  dest->SetBackfaceCulling(src.mBackfaceCulling->getValue());
137  dest->SetFrontfaceCulling(src.mFrontfaceCulling->getValue());
138  dest->SetRepresentation(src.mRepresentation->getValue().toInt());
139  dest->SetEdgeVisibility(src.mEdgeVisibility->getValue());
140  dest->SetEdgeColor(cx::getColorAsVector3D(src.mEdgeColor->getValue()).begin());
141  dest->SetAmbient(src.mAmbient->getValue());
142  dest->SetDiffuse(src.mDiffuse->getValue());
143  dest->SetSpecular(src.mSpecular->getValue());
144  dest->SetSpecularPower(src.mSpecularPower->getValue());
145 }
146 
149 void GraphicalGeometric::transformChangedSlot()
150 {
151  if (!mMesh)
152  {
153  return;
154  }
155 
156  Transform3D rrMd = mMesh->get_rMd();
157  Transform3D rMd = m_rMrr * rrMd;
158 
159  mGraphicalPolyDataPtr->setUserMatrix(rMd.getVtkMatrix());
160  mGraphicalGlyph3DDataPtr->setUserMatrix(rMd.getVtkMatrix());
161 }
162 
163 
164 //---------------------------------------------------------
165 //---------------------------------------------------------
166 //---------------------------------------------------------
167 
168 
170  RepImpl()
171 {
172  mGraphics.reset(new GraphicalGeometric());
173 }
175 {
176 }
178 {
179  return wrap_new(new GeometricRep(), uid);
180 }
181 
183 {
184  mGraphics->setRenderer(view->getRenderer());
185 }
186 
188 {
189  mGraphics->setRenderer(NULL);
190 }
191 
193 {
194  mGraphics->setMesh(mesh);
195 }
196 
198 {
199  return mGraphics->getMesh();
200 }
202 {
203  return mGraphics->getMesh() == mesh;
204 }
205 
206 //---------------------------------------------------------
207 }
208 // namespace cx
209 //---------------------------------------------------------
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:62
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:42
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.