7 #include "fwRenderVTK/vtk/fwVtkWheelRepresentation.hpp" 9 #include <fwServices/macros.hpp> 11 #include <glm/common.hpp> 12 #include <glm/gtc/constants.hpp> 13 #include <glm/gtc/vec1.hpp> 15 #include <vtkCellArray.h> 16 #include <vtkCellData.h> 17 #include <vtkDoubleArray.h> 18 #include <vtkPolyData.h> 19 #include <vtkPolyDataMapper2D.h> 21 #include <vtkRenderer.h> 22 #include <vtkRenderWindow.h> 30 fwVtkWheelRepresentation::fwVtkWheelRepresentation() :
31 m_wheelPoints(vtkPoints::New()),
32 m_wheelActor(vtkActor2D::New()),
33 m_colors(vtkUnsignedCharArray::New()),
39 this->m_center.x = -std::numeric_limits<double>::infinity();
40 this->m_center.y = -std::numeric_limits<double>::infinity();
42 this->BuildRepresentation();
47 fwVtkWheelRepresentation::~fwVtkWheelRepresentation()
49 m_wheelPoints->Delete();
50 m_wheelActor->Delete();
61 const double color[4] = {0., 0., 0., 0.};
64 this->m_colors->SetNumberOfComponents(4);
65 this->m_colors->SetName(
"Colors");
68 vtkSmartPointer<vtkCellArray> quads = vtkSmartPointer<vtkCellArray>::New();
71 this->m_wheelPoints = vtkPoints::New();
74 for(
unsigned int i = 0; i < this->m_nSectors * 2; i++)
76 vtkSmartPointer<vtkQuad> quad = vtkSmartPointer<vtkQuad>::New();
78 quad->GetPointIds()->SetId(0, (i * 2) + 0);
79 quad->GetPointIds()->SetId(1, (i * 2) + 1);
80 quad->GetPointIds()->SetId(2, (i * 2) + 3);
81 quad->GetPointIds()->SetId(3, (i * 2) + 2);
83 quads->InsertNextCell(quad);
85 this->m_colors->InsertNextTuple(color);
89 for(
unsigned int i = 0; i < (this->m_nSectors + 1) * 4; i++)
91 this->m_wheelPoints->InsertNextPoint(0, 0, 0.0);
95 vtkSmartPointer<vtkPolyData> wheelPolyData = vtkSmartPointer<vtkPolyData>::New();
96 wheelPolyData->SetPoints(this->m_wheelPoints);
97 wheelPolyData->SetPolys(quads);
98 wheelPolyData->GetCellData()->SetScalars(this->m_colors);
101 vtkSmartPointer<vtkPolyDataMapper2D> wheelMapper = vtkSmartPointer<vtkPolyDataMapper2D>::New();
102 wheelMapper->SetInputData(wheelPolyData);
103 this->m_wheelActor->SetMapper(wheelMapper);
104 this->m_wheelActor->PickableOn();
106 const ::glm::dvec2 actorPos(this->m_wheelActor->GetPosition()[0], this->m_wheelActor->GetPosition()[1]);
113 if ( this->GetMTime() > this->BuildTime ||
114 (this->Renderer && this->Renderer->GetVTKWindow() &&
115 this->Renderer->GetVTKWindow()->GetMTime() > this->BuildTime) )
117 const auto viewportSize = this->GetRenderer()->GetRenderWindow()->GetSize();
118 const int viewportWidth = viewportSize[0];
119 const int viewportHeight = viewportSize[1];
122 const double defaultColor[4] = {70., 100., 140., 192.};
123 const double hoverColor[4] = {255., 255., 255., 255.};
126 ::glm::dvec2 actorPos(this->m_wheelActor->GetPosition()[0], this->m_wheelActor->GetPosition()[1]);
128 if(this->m_center.x == -std::numeric_limits<double>::infinity() &&
129 this->m_center.y == -std::numeric_limits<double>::infinity() )
131 this->m_center.x =
static_cast<double>(viewportWidth) / 2.0;
132 this->m_center.y =
static_cast<double>(viewportHeight) / 2.0;
133 this->m_widgetToCenterTranslation = this->m_center - actorPos;
136 actorPos.x = ::glm::clamp(actorPos.x, -this->m_widgetToCenterTranslation.x,
137 static_cast<double>(viewportWidth) - this->m_widgetToCenterTranslation.x);
138 actorPos.y = ::glm::clamp(actorPos.y, -this->m_widgetToCenterTranslation.y,
139 static_cast<double>(viewportHeight) - this->m_widgetToCenterTranslation.y);
141 this->m_wheelActor->SetPosition(actorPos.x, actorPos.y);
144 const double deltaAngle = 2.0 * ::glm::pi<double>() / static_cast<double>(this->m_nSectors);
145 double initAngle = -deltaAngle / 2.0 + m_orientation;
147 const double minViewportComp = std::min(viewportWidth, viewportHeight);
148 this->m_wheelInnerRadius = 2.0 / 3.0 * minViewportComp / 2.0;
149 this->m_wheelOuterRadius = this->m_wheelInnerRadius + 0.02 * minViewportComp;
150 const double outerMarkedRadius = this->m_wheelOuterRadius + 0.01 * minViewportComp;
153 c = std::cos(initAngle + 0.0);
154 s = std::sin(initAngle + 0.0);
157 this->m_wheelPoints->SetPoint(0, this->m_center.x + this->m_wheelInnerRadius * c,
158 this->m_center.y + this->m_wheelInnerRadius * s, 0.0);
159 this->m_wheelPoints->SetPoint(1, this->m_center.x + this->m_wheelOuterRadius * c,
160 this->m_center.y + this->m_wheelOuterRadius * s, 0.0);
163 for(
unsigned int i = 0; i < this->m_nSectors; i++)
165 c = std::cos(initAngle + static_cast<double>(i + 1) * deltaAngle);
166 s = std::sin(initAngle + static_cast<double>(i + 1) * deltaAngle);
168 const unsigned int j = i + 1;
169 this->m_wheelPoints->SetPoint(j*2, this->m_center.x + this->m_wheelInnerRadius * c,
170 this->m_center.y + this->m_wheelInnerRadius * s, 0.0);
173 if(i % this->m_nMarkedSectors == 6 || i % this->m_nMarkedSectors == 7)
175 this->m_wheelPoints->SetPoint(j*2 + 1, this->m_center.x + outerMarkedRadius * c,
176 this->m_center.y + outerMarkedRadius * s, 0.0);
180 this->m_wheelPoints->SetPoint(j*2 + 1, this->m_center.x + this->m_wheelOuterRadius * c,
181 this->m_center.y + this->m_wheelOuterRadius * s, 0.0);
190 this->m_colors->SetTuple(i, hoverColor);
194 this->m_colors->SetTuple(i, defaultColor);
200 this->m_centerOuterRadius = 0.05 * this->m_wheelOuterRadius;
201 this->m_centerInnerRadius = 0.2 * this->m_centerOuterRadius;
203 initAngle = -deltaAngle / 2.0;
204 c = std::cos(initAngle + 0.0);
205 s = std::sin(initAngle + 0.0);
208 this->m_wheelPoints->SetPoint((this->m_nSectors + 1) * 2, this->m_center.x + this->m_centerInnerRadius * c,
209 this->m_center.y + this->m_centerInnerRadius * s, 0.0);
211 this->m_wheelPoints->SetPoint((this->m_nSectors + 1) * 2 + 1, this->m_center.x + this->m_centerOuterRadius * c,
212 this->m_center.y + this->m_centerOuterRadius * s, 0.0);
214 const double holeThickness = 0.15;
216 for(
unsigned int i = this->m_nSectors + 1; i < this->m_nSectors * 2; i++)
218 c = std::cos(initAngle + static_cast<double>(i + 1) * deltaAngle);
219 s = std::sin(initAngle + static_cast<double>(i + 1) * deltaAngle);
221 const unsigned int j = i + 1;
223 this->m_wheelPoints->SetPoint(j * 2, this->m_center.x + this->m_centerInnerRadius * c,
224 this->m_center.y + this->m_centerInnerRadius * s, 0.0);
226 this->m_wheelPoints->SetPoint(j * 2 + 1, this->m_center.x + this->m_centerOuterRadius * c,
227 this->m_center.y + this->m_centerOuterRadius * s, 0.0);
229 if( !(c < holeThickness && c > -holeThickness) &&
230 !(s < holeThickness && s > -holeThickness) )
234 this->m_colors->SetTuple(i, hoverColor);
238 this->m_colors->SetTuple(i, defaultColor);
243 this->m_wheelPoints->Modified();
244 this->BuildTime.Modified();
252 pc->AddItem(this->m_wheelActor);
259 this->m_wheelActor->ReleaseGraphicsResources(w);
260 this->Superclass::ReleaseGraphicsResources(w);
267 this->UpdateRepresentation();
268 if(!this->m_wheelActor->GetVisibility() )
272 return this->m_wheelActor->RenderOverlay(v);
277 int fwVtkWheelRepresentation::RenderOpaqueGeometry(vtkViewport* w)
279 this->UpdateRepresentation();
280 if ( !this->m_wheelActor->GetVisibility() )
284 return this->m_wheelActor->RenderOpaqueGeometry(w);
289 int fwVtkWheelRepresentation::RenderTranslucentPolygonalGeometry(vtkViewport* w)
291 this->UpdateRepresentation();
292 if ( !this->m_wheelActor->GetVisibility() )
296 return this->m_wheelActor->RenderTranslucentPolygonalGeometry(w);
303 this->UpdateRepresentation();
304 if ( !this->m_wheelActor->GetVisibility() )
308 return this->m_wheelActor->HasTranslucentPolygonalGeometry();
315 this->m_orientation = ::glm::mod(orientation, ::glm::two_pi<double>());
317 this->UpdateRepresentation();
324 this->m_hover = hover;
326 this->UpdateRepresentation();
333 const ::glm::dvec2 actorPos(this->m_wheelActor->GetPosition()[0], this->m_wheelActor->GetPosition()[1]);
334 return actorPos + this->m_widgetToCenterTranslation;
341 bool inCenter =
false;
343 if(this->Visibility != 0)
345 const ::glm::dvec2 actorPos(this->m_wheelActor->GetPosition()[0], this->m_wheelActor->GetPosition()[1]);
347 const ::glm::dvec2 delta = ::glm::dvec2(X, Y) - (actorPos + this->m_widgetToCenterTranslation);
349 const double squaredDistance = delta.x * delta.x + delta.y * delta.y;
351 const double squaredRadius = this->m_centerOuterRadius * this->m_centerOuterRadius;
353 inCenter = (squaredDistance < squaredRadius);
363 bool pointOnWheel =
false;
365 if(this->Visibility != 0)
367 const ::glm::dvec2 actorPos(this->m_wheelActor->GetPosition()[0], this->m_wheelActor->GetPosition()[1]);
369 const ::glm::dvec2 delta = ::glm::dvec2(X, Y) - (actorPos + this->m_widgetToCenterTranslation);
371 const double squaredDistance = delta.x * delta.x + delta.y * delta.y;
373 const double squaredOuterRadius = this->m_wheelOuterRadius * this->m_wheelOuterRadius;
374 const double squaredInnerRadius = this->m_wheelInnerRadius * this->m_wheelInnerRadius;
376 pointOnWheel = (squaredDistance < squaredOuterRadius) && (squaredDistance > squaredInnerRadius);
386 SLM_WARN(
"Method not implemented.");
FWRENDERVTK_API int HasTranslucentPolygonalGeometry() VTK_OVERRIDE
Check if this class holds any translucent geometry. (Not implemented)
FWRENDERVTK_API void UpdateRepresentation()
Updates wheel geometry using the center, radius and orientation.
FWRENDERVTK_API void GetActors2D(vtkPropCollection *pc) VTK_OVERRIDE
Adds the wheel actor to the prop collection.
FWRENDERVTK_API int RenderOverlay(vtkViewport *) VTK_OVERRIDE
Standard VTK render methods.
void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
Prints class information. Not implemented.
#define SLM_WARN(message)
FWRENDERVTK_API::glm::dvec2 GetCenterInScreenSpace() const
Returns the center in viewport coordinates.
FWRENDERVTK_API void BuildRepresentation() VTK_OVERRIDE
Generates the representation using the center, radius and orientation.
FWRENDERVTK_API void SetOrientation(double orientation)
Set the wheel orientation, expressed in radians.
FWRENDERVTK_API void SetHovering(bool hover)
Set the wheel hovering.
FWRENDERVTK_API void ReleaseGraphicsResources(vtkWindow *) VTK_OVERRIDE
Releases graphic resources used by this class' actor.
FWRENDERVTK_API bool isOnWheel(int X, int Y) const
Check if the (X, Y) screen position is on the wheel.
FWRENDERVTK_API bool isInCenter(int X, int Y) const
Check if the (X, Y) screen position is inside the wheel center.
Representation of a wheel widget.