CustusX  22.04-rc5
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 #include "cxLogger.h"
27 
28 #include "cxTypeConversions.h"
29 #include "cxVtkHelperClasses.h"
30 
31 namespace cx
32 {
33 
35 {
36  m_rMrr = Transform3D::Identity();
39 }
41 {
42 }
43 
45 {
46  if (mesh == mMesh)
47  return;
48  if (mMesh)
49  {
50  disconnect(mMesh.get(), &Mesh::meshChanged, this, &GraphicalGeometric::meshChangedSlot);
51  disconnect(mMesh.get(), &Data::transformChanged, this, &GraphicalGeometric::transformChangedSlot);
52  disconnect(mMesh.get(), &Data::clipPlanesChanged, this, &GraphicalGeometric::clipPlanesChangedSlot);
53  }
54  mMesh = mesh;
55  if (mMesh)
56  {
57  connect(mMesh.get(), &Mesh::meshChanged, this, &GraphicalGeometric::meshChangedSlot);
58  connect(mMesh.get(), &Data::transformChanged, this, &GraphicalGeometric::transformChangedSlot);
59  connect(mMesh.get(), &Data::clipPlanesChanged, this, &GraphicalGeometric::clipPlanesChangedSlot);
60  this->meshChangedSlot();
61  this->transformChangedSlot();
62  this->clipPlanesChangedSlot();
63  }
64 }
65 
66 //Copied from ImageMapperMonitor (used by VolumetricRep)
67 void GraphicalGeometric::clipPlanesChangedSlot()
68 {
69  this->clearClipping();
70 
71  if (!mMesh)
72  return;
73 
74  std::vector<vtkPlanePtr> mPlanes;
75  mPlanes = mMesh->getAllClipPlanes();
76  for (unsigned i=0; i<mPlanes.size(); ++i)
77  {
78  mGraphicalPolyDataPtr->getMapper()->AddClippingPlane(mPlanes[i]);
79  }
80 }
81 
83 {
84  if (!mMesh)
85  return;
86 
87  mGraphicalPolyDataPtr->getMapper()->RemoveAllClippingPlanes();
88 }
89 
91 {
92  return mMesh;
93 }
94 
96 {
97  mGraphicalPolyDataPtr->setRenderer(renderer);
98  mGraphicalGlyph3DDataPtr->setRenderer(renderer);
99 }
100 
102 {
103  m_rMrr = rMrr;
104  this->transformChangedSlot();
105 }
106 
107 void GraphicalGeometric::meshChangedSlot()
108 {
109  mGraphicalGlyph3DDataPtr->setVisibility(mMesh->showGlyph());
110  if(mMesh->showGlyph())
111  {
112  mGraphicalGlyph3DDataPtr->setData(mMesh->getVtkPolyData());
113  mGraphicalGlyph3DDataPtr->setOrientationArray(mMesh->getOrientationArray());
114  mGraphicalGlyph3DDataPtr->setColorArray(mMesh->getColorArray());
115  mGraphicalGlyph3DDataPtr->setColor(mMesh->getColor().redF(), mMesh->getColor().greenF(), mMesh->getColor().blueF());
116  mGraphicalGlyph3DDataPtr->setLUT(mMesh->getGlyphLUT());
117  mGraphicalGlyph3DDataPtr->setScaleFactor(mMesh->getVisSize());
118  }
119 
120  mMesh->updateVtkPolyDataWithTexture();
121  mGraphicalPolyDataPtr->setData(mMesh->getVtkPolyData());
122  mGraphicalPolyDataPtr->setTexture(mMesh->getVtkTexture());
123 
124  mGraphicalPolyDataPtr->setOpacity(1.0);
125  if(mMesh->getUseColorFromPolydataScalars())
126  {
127  mGraphicalPolyDataPtr->setScalarVisibility(true);
128  mGraphicalPolyDataPtr->setScalarModeToUseCellData();
129  }
130  else
131  {
132  mGraphicalPolyDataPtr->setScalarVisibility(false);
133  mGraphicalPolyDataPtr->setColor(mMesh->getColor().redF(), mMesh->getColor().greenF(), mMesh->getColor().blueF());
134  }
135  mGraphicalPolyDataPtr->setOpacity(mMesh->getColor().alphaF());
136 
137  //Set other properties
138  vtkPropertyPtr dest = mGraphicalPolyDataPtr->getProperty();
139  const MeshPropertyData& src = mMesh->getProperties();
140 
141  dest->SetPointSize(src.mVisSize->getValue());
142  dest->SetBackfaceCulling(src.mBackfaceCulling->getValue());
143  dest->SetFrontfaceCulling(src.mFrontfaceCulling->getValue());
144  dest->SetRepresentation(src.mRepresentation->getValue().toInt());
145  dest->SetEdgeVisibility(src.mEdgeVisibility->getValue());
146  dest->SetEdgeColor(cx::getColorAsVector3D(src.mEdgeColor->getValue()).begin());
147  dest->SetAmbient(src.mAmbient->getValue());
148  dest->SetDiffuse(src.mDiffuse->getValue());
149  dest->SetSpecular(src.mSpecular->getValue());
150  dest->SetSpecularPower(src.mSpecularPower->getValue());
151  dest->SetLineWidth(src.mLineWidth->getValue());
152 }
153 
156 void GraphicalGeometric::transformChangedSlot()
157 {
158  if (!mMesh)
159  {
160  return;
161  }
162 
163  Transform3D rrMd = mMesh->get_rMd();
164  Transform3D rMd = m_rMrr * rrMd;
165 
166  mGraphicalPolyDataPtr->setUserMatrix(rMd.getVtkMatrix());
167  mGraphicalGlyph3DDataPtr->setUserMatrix(rMd.getVtkMatrix());
168 }
169 
170 
171 //---------------------------------------------------------
172 //---------------------------------------------------------
173 //---------------------------------------------------------
174 
175 
177  RepImpl()
178 {
179  mGraphics.reset(new GraphicalGeometric());
180 }
182 {
183 }
185 {
186  return wrap_new(new GeometricRep(), uid);
187 }
188 
190 {
191  mGraphics->setRenderer(view->getRenderer());
192 }
193 
195 {
196  mGraphics->setRenderer(NULL);
197 }
198 
200 {
201  mGraphics->setMesh(mesh);
202 }
203 
205 {
206  return mGraphics->getMesh();
207 }
209 {
210  return mGraphics->getMesh() == mesh;
211 }
212 
213 //---------------------------------------------------------
214 }
215 // namespace cx
216 //---------------------------------------------------------
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 mLineWidth
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.