00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "mitkPlanarFigureMapper2D.h"
00020
00021 #include "mitkBaseRenderer.h"
00022 #include "mitkPlaneGeometry.h"
00023 #include "mitkColorProperty.h"
00024 #include "mitkProperties.h"
00025 #include "mitkGL.h"
00026 #include "mitkVtkPropRenderer.h"
00027
00028 mitk::PlanarFigureMapper2D::PlanarFigureMapper2D()
00029 {
00030 this->InitializeDefaultPlanarFigureProperties();
00031 }
00032
00033
00034 mitk::PlanarFigureMapper2D::~PlanarFigureMapper2D()
00035 {
00036 }
00037
00038
00039 void mitk::PlanarFigureMapper2D::Paint( mitk::BaseRenderer *renderer )
00040 {
00041 if ( !this->IsVisible( renderer ) )
00042 {
00043 return;
00044 }
00045
00046
00047 mitk::PlanarFigure *planarFigure = const_cast< mitk::PlanarFigure * >(
00048 static_cast< const mitk::PlanarFigure * >( this->GetData() ) );
00049
00050
00051 if ( !planarFigure->IsPlaced() )
00052 {
00053 return;
00054 }
00055
00056
00057 mitk::Geometry2D *planarFigureGeometry2D =
00058 dynamic_cast< Geometry2D * >( planarFigure->GetGeometry( 0 ) );
00059 if ( planarFigureGeometry2D == NULL )
00060 {
00061 MITK_ERROR << "PlanarFigure does not have valid Geometry2D!";
00062 return;
00063 }
00064
00065
00066 const mitk::Geometry2D *rendererGeometry2D = renderer->GetCurrentWorldGeometry2D();
00067
00068
00069
00070
00071 mitk::PlaneGeometry *planarFigurePlaneGeometry =
00072 dynamic_cast< PlaneGeometry * >( planarFigureGeometry2D );
00073 const mitk::PlaneGeometry *rendererPlaneGeometry =
00074 dynamic_cast< const PlaneGeometry * >( rendererGeometry2D );
00075
00076 if ( (planarFigurePlaneGeometry != NULL) && (rendererPlaneGeometry != NULL) )
00077 {
00078 double planeThickness = planarFigurePlaneGeometry->GetExtentInMM( 2 );
00079 if ( !planarFigurePlaneGeometry->IsParallel( rendererPlaneGeometry )
00080 || !(planarFigurePlaneGeometry->DistanceFromPlane(
00081 rendererPlaneGeometry ) < planeThickness / 2.0) )
00082 {
00083
00084
00085 return;
00086 }
00087 }
00088 else
00089 {
00090
00091 return;
00092 }
00093
00094
00095
00096 mitk::DisplayGeometry *displayGeometry = renderer->GetDisplayGeometry();
00097 assert( displayGeometry != NULL );
00098
00099
00100
00101 this->ApplyProperties( renderer );
00102
00103
00104 glEnable( GL_LINE_SMOOTH );
00105 glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
00106
00107
00108 const mitk::DataNode* node=this->GetDataNode();
00109 this->InitializePlanarFigurePropertiesFromDataNode( node );
00110
00111 PlanarFigureDisplayMode lineDisplayMode = PF_DEFAULT;
00112
00113 if ( m_IsSelected )
00114 {
00115 lineDisplayMode = PF_SELECTED;
00116 }
00117 else if ( m_IsHovering )
00118 {
00119 lineDisplayMode = PF_HOVER;
00120 }
00121
00122 mitk::Point2D firstPoint; firstPoint[0] = 0; firstPoint[1] = 1;
00123
00124 if ( m_DrawOutline )
00125 {
00126
00127 this->DrawMainLines( planarFigure,
00128 m_OutlineColor[lineDisplayMode],
00129 m_OutlineOpacity[lineDisplayMode],
00130 m_OutlineWidth,
00131 firstPoint,
00132 planarFigureGeometry2D, rendererGeometry2D, displayGeometry );
00133
00134
00135 this->DrawHelperLines( planarFigure,
00136 m_OutlineColor[lineDisplayMode],
00137 m_OutlineOpacity[lineDisplayMode],
00138 m_OutlineWidth,
00139 firstPoint,
00140 planarFigureGeometry2D, rendererGeometry2D, displayGeometry );
00141 }
00142
00143
00144
00145 this->DrawMainLines( planarFigure,
00146 m_LineColor[lineDisplayMode],
00147 m_LineOpacity[lineDisplayMode],
00148 m_LineWidth,
00149 firstPoint,
00150 planarFigureGeometry2D, rendererGeometry2D, displayGeometry );
00151
00152 double annotationOffset = 0.0;
00153
00154
00155 std::string name = node->GetName();
00156 if ( !name.empty() )
00157 {
00158 mitk::VtkPropRenderer* openGLrenderer = dynamic_cast<mitk::VtkPropRenderer*>( renderer );
00159 if ( openGLrenderer )
00160 {
00161 if ( m_IsSelected || m_IsHovering )
00162 {
00163 openGLrenderer->WriteSimpleText( name,
00164 firstPoint[0] + 5.0, firstPoint[1] + 5.0,
00165 m_LineColor[lineDisplayMode][0],
00166 m_LineColor[lineDisplayMode][1],
00167 m_LineColor[lineDisplayMode][2] );
00168 }
00169
00170
00171 annotationOffset -= 15.0;
00172 }
00173 }
00174
00175
00176 if ( m_DrawQuantities )
00177 {
00178 std::stringstream quantityString;
00179 quantityString.setf( ios::fixed, ios::floatfield );
00180 quantityString.precision( 1 );
00181
00182 bool firstActiveFeature = true;
00183 for ( unsigned int i = 0; i < planarFigure->GetNumberOfFeatures(); ++i )
00184 {
00185 if( planarFigure->IsFeatureActive(i) )
00186 {
00187 if ( ! firstActiveFeature )
00188 {
00189 quantityString << " / ";
00190 }
00191 quantityString << planarFigure->GetQuantity( i ) << " ";
00192 quantityString << planarFigure->GetFeatureUnit( i );
00193 firstActiveFeature = false;
00194 }
00195 }
00196
00197 mitk::VtkPropRenderer* openGLrenderer = dynamic_cast<mitk::VtkPropRenderer*>( renderer );
00198 if ( openGLrenderer )
00199 {
00200 openGLrenderer->WriteSimpleText( quantityString.str().c_str(),
00201 firstPoint[0] + 5.0, firstPoint[1] + 5.0 + annotationOffset,
00202 m_LineColor[lineDisplayMode][0],
00203 m_LineColor[lineDisplayMode][1],
00204 m_LineColor[lineDisplayMode][2] );
00205
00206
00207 annotationOffset -= 15.0;
00208 }
00209 }
00210
00211
00212
00213 this->DrawHelperLines( planarFigure,
00214 m_HelperlineColor[lineDisplayMode],
00215 m_HelperlineOpacity[lineDisplayMode],
00216 m_LineWidth,
00217 firstPoint,
00218 planarFigureGeometry2D, rendererGeometry2D, displayGeometry );
00219
00220
00221
00222
00223 for ( unsigned int i = 0; i < planarFigure->GetNumberOfControlPoints(); ++i )
00224 {
00225
00226 bool isEditable = true;
00227 m_DataNode->GetBoolProperty( "planarfigure.iseditable", isEditable );
00228
00229 PlanarFigureDisplayMode pointDisplayMode = PF_DEFAULT;
00230
00231
00232
00233 if ( isEditable )
00234 {
00235 if ( i == (unsigned int) planarFigure->GetSelectedControlPoint() )
00236 {
00237 pointDisplayMode = PF_SELECTED;
00238 }
00239 else if ( m_IsHovering )
00240 {
00241 pointDisplayMode = PF_HOVER;
00242 }
00243 }
00244
00245 this->DrawMarker( planarFigure->GetControlPoint( i ),
00246 m_MarkerlineColor[pointDisplayMode],
00247 m_MarkerlineOpacity[pointDisplayMode],
00248 m_MarkerColor[pointDisplayMode],
00249 m_MarkerOpacity[pointDisplayMode],
00250 m_LineWidth,
00251 m_ControlPointShape,
00252 planarFigureGeometry2D,
00253 rendererGeometry2D,
00254 displayGeometry );
00255 }
00256
00257 glLineWidth( 1.0f );
00258 }
00259
00260
00261 void mitk::PlanarFigureMapper2D::PaintPolyLine(
00262 const VertexContainerType* vertices,
00263 bool closed,
00264 float* color,
00265 float opacity,
00266 float lineWidth,
00267 Point2D& firstPoint,
00268 const Geometry2D* planarFigureGeometry2D,
00269 const Geometry2D* rendererGeometry2D,
00270 const DisplayGeometry* displayGeometry)
00271 {
00272 glColor4f( color[0], color[1], color[2], opacity );
00273 glLineWidth(lineWidth);
00274
00275 if ( closed )
00276 {
00277 glBegin( GL_LINE_LOOP );
00278 }
00279 else
00280 {
00281 glBegin( GL_LINE_STRIP );
00282 }
00283
00284 for ( VertexContainerType::ConstIterator it = vertices->Begin(); it != vertices->End(); ++it )
00285 {
00286
00287 mitk::Point2D displayPoint;
00288 this->TransformObjectToDisplay( it->Value(), displayPoint,
00289 planarFigureGeometry2D, rendererGeometry2D, displayGeometry );
00290
00291 if(it == vertices->Begin())
00292 firstPoint = displayPoint;
00293
00294 glVertex2f( displayPoint[0], displayPoint[1] );
00295
00296 }
00297
00298 glEnd();
00299 }
00300
00301
00302 void mitk::PlanarFigureMapper2D::DrawMainLines(
00303 mitk::PlanarFigure* figure,
00304 float* color,
00305 float opacity,
00306 float lineWidth,
00307 Point2D& firstPoint,
00308 const Geometry2D* planarFigureGeometry2D,
00309 const Geometry2D* rendererGeometry2D,
00310 const DisplayGeometry* displayGeometry)
00311 {
00312 for ( unsigned short loop = 0; loop < figure->GetPolyLinesSize(); ++loop )
00313 {
00314 const VertexContainerType* polyLine = figure->GetPolyLine(loop);
00315 this->PaintPolyLine( polyLine,
00316 figure->IsClosed(),
00317 color, opacity, lineWidth, firstPoint,
00318 planarFigureGeometry2D, rendererGeometry2D, displayGeometry );
00319 }
00320 }
00321
00322 void mitk::PlanarFigureMapper2D::DrawHelperLines(
00323 mitk::PlanarFigure* figure,
00324 float* color,
00325 float opacity,
00326 float lineWidth,
00327 Point2D& firstPoint,
00328 const Geometry2D* planarFigureGeometry2D,
00329 const Geometry2D* rendererGeometry2D,
00330 const DisplayGeometry* displayGeometry)
00331 {
00332
00333 for ( unsigned int loop = 0; loop < figure->GetHelperPolyLinesSize(); ++loop )
00334 {
00335
00336
00337 const VertexContainerType *polyLine = figure->GetHelperPolyLine( loop,
00338 displayGeometry->GetScaleFactorMMPerDisplayUnit(),
00339 displayGeometry->GetDisplayHeight() );
00340
00341
00342 if ( !figure->IsHelperToBePainted( loop ) )
00343 {
00344 continue;
00345 }
00346
00347 this->PaintPolyLine( polyLine, false,
00348 color, opacity, lineWidth, firstPoint,
00349 planarFigureGeometry2D, rendererGeometry2D, displayGeometry );
00350 }
00351 }
00352
00353
00354
00355 void mitk::PlanarFigureMapper2D::TransformObjectToDisplay(
00356 const mitk::Point2D &point2D,
00357 mitk::Point2D &displayPoint,
00358 const mitk::Geometry2D *objectGeometry,
00359 const mitk::Geometry2D *rendererGeometry,
00360 const mitk::DisplayGeometry *displayGeometry )
00361 {
00362 mitk::Point3D point3D;
00363
00364
00365 objectGeometry->Map( point2D, point3D );
00366
00367
00368 rendererGeometry->Map( point3D, displayPoint );
00369 displayGeometry->WorldToDisplay( displayPoint, displayPoint );
00370 }
00371
00372
00373 void mitk::PlanarFigureMapper2D::DrawMarker(
00374 const mitk::Point2D &point,
00375 float* lineColor,
00376 float lineOpacity,
00377 float* markerColor,
00378 float markerOpacity,
00379 float lineWidth,
00380 PlanarFigureControlPointStyleProperty::Shape shape,
00381 const mitk::Geometry2D *objectGeometry,
00382 const mitk::Geometry2D *rendererGeometry,
00383 const mitk::DisplayGeometry *displayGeometry )
00384 {
00385 mitk::Point2D displayPoint;
00386
00387 this->TransformObjectToDisplay(
00388 point, displayPoint,
00389 objectGeometry, rendererGeometry, displayGeometry );
00390
00391 glColor4f( markerColor[0], markerColor[1], markerColor[2], markerOpacity );
00392 glLineWidth( lineWidth );
00393
00394 switch ( shape )
00395 {
00396 case PlanarFigureControlPointStyleProperty::Square:
00397 default:
00398
00399
00400
00401 glDisable( GL_LINE_SMOOTH );
00402
00403 glRectf(
00404 displayPoint[0] - 4, displayPoint[1] - 4,
00405 displayPoint[0] + 4, displayPoint[1] + 4 );
00406
00407
00408 glColor4f( lineColor[0], lineColor[1], lineColor[2], lineOpacity );
00409 glBegin( GL_LINE_LOOP );
00410 glVertex2f( displayPoint[0] - 4, displayPoint[1] - 4 );
00411 glVertex2f( displayPoint[0] - 4, displayPoint[1] + 4 );
00412 glVertex2f( displayPoint[0] + 4, displayPoint[1] + 4 );
00413 glVertex2f( displayPoint[0] + 4, displayPoint[1] - 4 );
00414 glEnd();
00415 break;
00416
00417 case PlanarFigureControlPointStyleProperty::Circle:
00418
00419 glBegin( GL_POLYGON );
00420 float radius = 4.0;
00421 for ( int angle = 0; angle < 8; ++angle )
00422 {
00423 float angleRad = angle * (float) 3.14159 / 4.0;
00424 float x = displayPoint[0] + radius * (float)cos( angleRad );
00425 float y = displayPoint[1] + radius * (float)sin( angleRad );
00426 glVertex2f(x,y);
00427 }
00428 glEnd();
00429
00430
00431 glColor4f( lineColor[0], lineColor[1], lineColor[2], lineOpacity );
00432 glBegin( GL_LINE_LOOP );
00433 for ( int angle = 0; angle < 8; ++angle )
00434 {
00435 float angleRad = angle * (float) 3.14159 / 4.0;
00436 float x = displayPoint[0] + radius * (float)cos( angleRad );
00437 float y = displayPoint[1] + radius * (float)sin( angleRad );
00438 glVertex2f(x,y);
00439 }
00440 glEnd();
00441 break;
00442
00443 }
00444 }
00445
00446
00447 void mitk::PlanarFigureMapper2D::InitializeDefaultPlanarFigureProperties()
00448 {
00449 m_IsSelected = false;
00450 m_IsHovering = false;
00451 m_DrawOutline = false;
00452 m_DrawQuantities = false;
00453
00454 m_LineWidth = 1.0;
00455 m_OutlineWidth = 4.0;
00456 m_HelperlineWidth = 2.0;
00457
00458 m_ControlPointShape = PlanarFigureControlPointStyleProperty::Square;
00459
00460 this->SetColorProperty( m_LineColor, PF_DEFAULT, 1.0, 1.0, 1.0 );
00461 this->SetFloatProperty( m_LineOpacity, PF_DEFAULT, 1.0 );
00462 this->SetColorProperty( m_OutlineColor, PF_DEFAULT, 0.0, 0.0, 1.0 );
00463 this->SetFloatProperty( m_OutlineOpacity, PF_DEFAULT, 1.0 );
00464 this->SetColorProperty( m_HelperlineColor, PF_DEFAULT, 0.4, 0.8, 0.2 );
00465 this->SetFloatProperty( m_HelperlineOpacity, PF_DEFAULT, 0.4 );
00466 this->SetColorProperty( m_MarkerlineColor, PF_DEFAULT, 1.0, 1.0, 1.0 );
00467 this->SetFloatProperty( m_MarkerlineOpacity, PF_DEFAULT, 1.0 );
00468 this->SetColorProperty( m_MarkerColor, PF_DEFAULT, 1.0, 1.0, 1.0 );
00469 this->SetFloatProperty( m_MarkerOpacity, PF_DEFAULT, 0.0 );
00470
00471 this->SetColorProperty( m_LineColor, PF_HOVER, 1.0, 0.7, 0.0 );
00472 this->SetFloatProperty( m_LineOpacity, PF_HOVER, 1.0 );
00473 this->SetColorProperty( m_OutlineColor, PF_HOVER, 0.0, 0.0, 1.0 );
00474 this->SetFloatProperty( m_OutlineOpacity, PF_HOVER, 1.0 );
00475 this->SetColorProperty( m_HelperlineColor, PF_HOVER, 0.4, 0.8, 0.2 );
00476 this->SetFloatProperty( m_HelperlineOpacity, PF_HOVER, 0.4 );
00477 this->SetColorProperty( m_MarkerlineColor, PF_HOVER, 1.0, 1.0, 1.0 );
00478 this->SetFloatProperty( m_MarkerlineOpacity, PF_HOVER, 1.0 );
00479 this->SetColorProperty( m_MarkerColor, PF_HOVER, 1.0, 0.6, 0.0 );
00480 this->SetFloatProperty( m_MarkerOpacity, PF_HOVER, 0.2 );
00481
00482 this->SetColorProperty( m_LineColor, PF_SELECTED, 1.0, 0.0, 0.0 );
00483 this->SetFloatProperty( m_LineOpacity, PF_SELECTED, 1.0 );
00484 this->SetColorProperty( m_OutlineColor, PF_SELECTED, 0.0, 0.0, 1.0 );
00485 this->SetFloatProperty( m_OutlineOpacity, PF_SELECTED, 1.0 );
00486 this->SetColorProperty( m_HelperlineColor, PF_SELECTED, 0.4, 0.8, 0.2 );
00487 this->SetFloatProperty( m_HelperlineOpacity, PF_SELECTED, 0.4 );
00488 this->SetColorProperty( m_MarkerlineColor, PF_SELECTED, 1.0, 1.0, 1.0 );
00489 this->SetFloatProperty( m_MarkerlineOpacity, PF_SELECTED, 1.0 );
00490 this->SetColorProperty( m_MarkerColor, PF_SELECTED, 1.0, 0.6, 0.0 );
00491 this->SetFloatProperty( m_MarkerOpacity, PF_SELECTED, 1.0 );
00492 }
00493
00494
00495 void mitk::PlanarFigureMapper2D::InitializePlanarFigurePropertiesFromDataNode( const mitk::DataNode* node )
00496 {
00497 if ( node == NULL )
00498 {
00499 return;
00500 }
00501
00502 node->GetBoolProperty( "selected", m_IsSelected );
00503 node->GetBoolProperty( "planarfigure.ishovering", m_IsHovering );
00504 node->GetBoolProperty( "planarfigure.drawoutline", m_DrawOutline );
00505 node->GetBoolProperty( "planarfigure.drawquantities", m_DrawQuantities );
00506
00507 node->GetFloatProperty( "planarfigure.line.width", m_LineWidth );
00508 node->GetFloatProperty( "planarfigure.outline.width", m_OutlineWidth );
00509 node->GetFloatProperty( "planarfigure.helperline.width", m_HelperlineWidth );
00510
00511 PlanarFigureControlPointStyleProperty::Pointer styleProperty =
00512 dynamic_cast< PlanarFigureControlPointStyleProperty* >( node->GetProperty( "planarfigure.controlpointshape" ) );
00513 if ( styleProperty.IsNotNull() )
00514 {
00515 m_ControlPointShape = styleProperty->GetShape();
00516 }
00517
00518 node->GetColor( m_LineColor[PF_DEFAULT], NULL, "planarfigure.default.line.color" );
00519 node->GetFloatProperty( "planarfigure.default.line.opacity", m_LineOpacity[PF_DEFAULT] );
00520 node->GetColor( m_OutlineColor[PF_DEFAULT], NULL, "planarfigure.default.outline.color" );
00521 node->GetFloatProperty( "planarfigure.default.outline.opacity", m_OutlineOpacity[PF_DEFAULT] );
00522 node->GetColor( m_HelperlineColor[PF_DEFAULT], NULL, "planarfigure.default.helperline.color" );
00523 node->GetFloatProperty( "planarfigure.default.helperline.opacity", m_HelperlineOpacity[PF_DEFAULT] );
00524 node->GetColor( m_MarkerlineColor[PF_DEFAULT], NULL, "planarfigure.default.markerline.color" );
00525 node->GetFloatProperty( "planarfigure.default.markerline.opacity", m_MarkerlineOpacity[PF_DEFAULT] );
00526 node->GetColor( m_MarkerColor[PF_DEFAULT], NULL, "planarfigure.default.marker.color" );
00527 node->GetFloatProperty( "planarfigure.default.marker.opacity", m_MarkerOpacity[PF_DEFAULT] );
00528
00529 node->GetColor( m_LineColor[PF_HOVER], NULL, "planarfigure.hover.line.color" );
00530 node->GetFloatProperty( "planarfigure.hover.line.opacity", m_LineOpacity[PF_HOVER] );
00531 node->GetColor( m_OutlineColor[PF_HOVER], NULL, "planarfigure.hover.outline.color" );
00532 node->GetFloatProperty( "planarfigure.hover.outline.opacity", m_OutlineOpacity[PF_HOVER] );
00533 node->GetColor( m_HelperlineColor[PF_HOVER], NULL, "planarfigure.hover.helperline.color" );
00534 node->GetFloatProperty( "planarfigure.hover.helperline.opacity", m_HelperlineOpacity[PF_HOVER] );
00535 node->GetColor( m_MarkerlineColor[PF_HOVER], NULL, "planarfigure.hover.markerline.color" );
00536 node->GetFloatProperty( "planarfigure.hover.markerline.opacity", m_MarkerlineOpacity[PF_HOVER] );
00537 node->GetColor( m_MarkerColor[PF_HOVER], NULL, "planarfigure.hover.marker.color" );
00538 node->GetFloatProperty( "planarfigure.hover.marker.opacity", m_MarkerOpacity[PF_HOVER] );
00539
00540 node->GetColor( m_LineColor[PF_SELECTED], NULL, "planarfigure.selected.line.color" );
00541 node->GetFloatProperty( "planarfigure.selected.line.opacity", m_LineOpacity[PF_SELECTED] );
00542 node->GetColor( m_OutlineColor[PF_SELECTED], NULL, "planarfigure.selected.outline.color" );
00543 node->GetFloatProperty( "planarfigure.selected.outline.opacity", m_OutlineOpacity[PF_SELECTED] );
00544 node->GetColor( m_HelperlineColor[PF_SELECTED], NULL, "planarfigure.selected.helperline.color" );
00545 node->GetFloatProperty( "planarfigure.selected.helperline.opacity", m_HelperlineOpacity[PF_SELECTED] );
00546 node->GetColor( m_MarkerlineColor[PF_SELECTED], NULL, "planarfigure.selected.markerline.color" );
00547 node->GetFloatProperty( "planarfigure.selected.markerline.opacity", m_MarkerlineOpacity[PF_SELECTED] );
00548 node->GetColor( m_MarkerColor[PF_SELECTED], NULL, "planarfigure.selected.marker.color" );
00549 node->GetFloatProperty( "planarfigure.selected.marker.opacity", m_MarkerOpacity[PF_SELECTED] );
00550
00551 }
00552
00553
00554 void mitk::PlanarFigureMapper2D::SetDefaultProperties( mitk::DataNode* node, mitk::BaseRenderer* renderer, bool overwrite )
00555 {
00556 node->AddProperty( "visible", mitk::BoolProperty::New(true), renderer, overwrite );
00557 }