diff --git a/Modules/PlanarFigure/src/DataManagement/mitkPlanarBezierCurve.cpp b/Modules/PlanarFigure/src/DataManagement/mitkPlanarBezierCurve.cpp index 62cb9e8139..552c60ba0a 100644 --- a/Modules/PlanarFigure/src/DataManagement/mitkPlanarBezierCurve.cpp +++ b/Modules/PlanarFigure/src/DataManagement/mitkPlanarBezierCurve.cpp @@ -1,162 +1,160 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkPlanarBezierCurve.h" #include #include mitk::PlanarBezierCurve::PlanarBezierCurve() : FEATURE_ID_LENGTH(Superclass::AddFeature("Length", "mm")), m_NumberOfSegments(100) { this->ResetNumberOfControlPoints(2); this->SetNumberOfPolyLines(1); this->SetNumberOfHelperPolyLines(1); } void mitk::PlanarBezierCurve::EvaluateFeaturesInternal() { double length = 0.0; for (unsigned int i = 0; i < m_NumberOfSegments; ++i) length += static_cast(m_PolyLines[0][i]).EuclideanDistanceTo(static_cast(m_PolyLines[0][i + 1])); this->SetQuantity(FEATURE_ID_LENGTH, length); } unsigned int mitk::PlanarBezierCurve::GetNumberOfSegments() const { return m_NumberOfSegments; } void mitk::PlanarBezierCurve::SetNumberOfSegments(unsigned int numSegments) { m_NumberOfSegments = std::max(1U, numSegments); if (this->IsPlaced()) { this->GeneratePolyLine(); this->Modified(); } } void mitk::PlanarBezierCurve::GenerateHelperPolyLine(double, unsigned int) { this->ClearHelperPolyLines(); unsigned int numHelperPolyLinePoints = m_ControlPoints.size(); for (unsigned int i = 0; i < numHelperPolyLinePoints; ++i) this->AppendPointToHelperPolyLine(0, m_ControlPoints[i]); } void mitk::PlanarBezierCurve::GeneratePolyLine() { this->ClearPolyLines(); const unsigned int numPolyLinePoints = m_NumberOfSegments + 1; for (unsigned int i = 0; i < numPolyLinePoints; ++i) this->AppendPointToPolyLine(0, this->ComputeDeCasteljauPoint(i / static_cast(m_NumberOfSegments))); } mitk::Point2D mitk::PlanarBezierCurve::ComputeDeCasteljauPoint(mitk::ScalarType t) { unsigned int n = m_ControlPoints.size() - 1; if (m_DeCasteljauPoints.size() != n) m_DeCasteljauPoints.resize(n); for (unsigned int i = 0; i < n; ++i) { m_DeCasteljauPoints[i][0] = (1 - t) * m_ControlPoints[i][0] + t * m_ControlPoints[i + 1][0]; m_DeCasteljauPoints[i][1] = (1 - t) * m_ControlPoints[i][1] + t * m_ControlPoints[i + 1][1]; } for (--n; n > 0; --n) { for (unsigned int i = 0; i < n; ++i) { m_DeCasteljauPoints[i][0] = (1 - t) * m_DeCasteljauPoints[i][0] + t * m_DeCasteljauPoints[i + 1][0]; m_DeCasteljauPoints[i][1] = (1 - t) * m_DeCasteljauPoints[i][1] + t * m_DeCasteljauPoints[i + 1][1]; } } return m_DeCasteljauPoints[0]; } int mitk::PlanarBezierCurve::GetControlPointForPolylinePoint( int indexOfPolylinePoint, int polyLineIndex ) const { - mitk::PlanarFigure::PolyLineType polyLine = GetPolyLine( polyLineIndex ); - if ( indexOfPolylinePoint > static_cast(polyLine.size()) ) - { + mitk::PlanarFigure::PolyLineType polyLine = GetPolyLine(polyLineIndex); + + if (indexOfPolylinePoint < 0 || indexOfPolylinePoint > static_cast(polyLine.size())) return -1; - } mitk::PlanarFigure::ControlPointListType::const_iterator elem; mitk::PlanarFigure::ControlPointListType::const_iterator first = m_ControlPoints.cbegin(); mitk::PlanarFigure::ControlPointListType::const_iterator end = m_ControlPoints.cend(); mitk::PlanarFigure::PolyLineType::const_iterator polyLineIter; mitk::PlanarFigure::PolyLineType::const_iterator polyLineEnd = polyLine.cend(); mitk::PlanarFigure::PolyLineType::const_iterator polyLineStart = polyLine.cbegin(); polyLineStart += indexOfPolylinePoint; - for ( polyLineIter = polyLineStart; polyLineIter != polyLineEnd; ++polyLineIter ) + for (polyLineIter = polyLineStart; polyLineIter != polyLineEnd; ++polyLineIter) { - elem = std::find( first, end, *polyLineIter ); - if ( elem != end ) - { - return std::distance( first, elem ); - } + elem = std::find(first, end, *polyLineIter); + + if (elem != end) + return std::distance(first, elem); } return GetNumberOfControlPoints(); } unsigned int mitk::PlanarBezierCurve::GetMaximumNumberOfControlPoints() const { return std::numeric_limits::max(); } unsigned int mitk::PlanarBezierCurve::GetMinimumNumberOfControlPoints() const { return 2; } bool mitk::PlanarBezierCurve::IsHelperToBePainted(unsigned int index) { return index == 0 && m_ControlPoints.size() > 2; } bool mitk::PlanarBezierCurve::Equals(const PlanarFigure &other) const { const mitk::PlanarBezierCurve* otherBezierCurve = dynamic_cast(&other); if ( otherBezierCurve ) { if( this->m_NumberOfSegments != otherBezierCurve->m_NumberOfSegments ) return false; if( this->m_DeCasteljauPoints != otherBezierCurve->m_DeCasteljauPoints ) return false; return Superclass::Equals(other); } else { return false; } } diff --git a/Modules/PlanarFigure/src/DataManagement/mitkPlanarSubdivisionPolygon.cpp b/Modules/PlanarFigure/src/DataManagement/mitkPlanarSubdivisionPolygon.cpp index 80d8626b77..a5ca8ed6b5 100644 --- a/Modules/PlanarFigure/src/DataManagement/mitkPlanarSubdivisionPolygon.cpp +++ b/Modules/PlanarFigure/src/DataManagement/mitkPlanarSubdivisionPolygon.cpp @@ -1,173 +1,171 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkPlanarSubdivisionPolygon.h" #include "mitkPlaneGeometry.h" #include "mitkProperties.h" // stl related includes #include mitk::PlanarSubdivisionPolygon::PlanarSubdivisionPolygon(): m_TensionParameter(0.0625), m_SubdivisionRounds(5) { // Polygon is subdivision (in contrast to parent class PlanarPolygon this->SetProperty( "closed", mitk::BoolProperty::New( true ) ); this->SetProperty( "subdivision", mitk::BoolProperty::New( true ) ); // Other properties are inherited / already initialized by parent class PlanarPolygon } void mitk::PlanarSubdivisionPolygon::GeneratePolyLine() { this->ClearPolyLines(); ControlPointListType subdivisionPoints; ControlPointListType newSubdivisionPoints; subdivisionPoints.clear(); subdivisionPoints = m_ControlPoints; if( m_ControlPoints.size() >= GetMinimumNumberOfControlPoints() ) { for( unsigned int i=0; i < GetSubdivisionRounds(); i++ ) { // Indices unsigned int index, indexPrev, indexNext, indexNextNext; unsigned int numberOfPoints = subdivisionPoints.size(); Point2D newPoint; // Keep cycling our array indices forward until they wrap around at the end for ( index = 0; index < numberOfPoints; ++index ) { // Create new subdivision point according to formula // p_new = (0.5 + tension) * (p_here + p_next) - tension * (p_prev + p_nextnext) indexPrev = (numberOfPoints + index - 1) % numberOfPoints; indexNext = (index + 1) % numberOfPoints; indexNextNext = (index + 2) % numberOfPoints; newPoint[0] = (0.5 + GetTensionParameter()) * (double)( subdivisionPoints[index][0] + subdivisionPoints[indexNext][0] ) - GetTensionParameter() * (double)( subdivisionPoints[indexPrev][0] + subdivisionPoints[indexNextNext][0]); newPoint[1] = (0.5 + GetTensionParameter()) * (double)( subdivisionPoints[index][1] + subdivisionPoints[indexNext][1] ) - GetTensionParameter() * (double)( subdivisionPoints[indexPrev][1] + subdivisionPoints[indexNextNext][1]); newSubdivisionPoints.push_back( newPoint ); } ControlPointListType mergedSubdivisionPoints; ControlPointListType::iterator it, itNew; for ( it = subdivisionPoints.begin() , itNew = newSubdivisionPoints.begin(); it != subdivisionPoints.end(); ++it, ++itNew ) { mergedSubdivisionPoints.push_back( *it ); mergedSubdivisionPoints.push_back( *itNew ); } subdivisionPoints = mergedSubdivisionPoints; newSubdivisionPoints.clear(); } } bool isInitiallyPlaced = this->GetProperty("initiallyplaced"); unsigned int i; ControlPointListType::iterator it; for ( it = subdivisionPoints.begin(), i = 0; it != subdivisionPoints.end(); ++it, ++i ) { // Determine the index of the control point FOLLOWING this poly-line element // (this is needed by PlanarFigureInteractor to insert new points at the correct position, // namely BEFORE the next control point) unsigned int nextIndex; if ( i == 0 ) { // For the FIRST polyline point, use the index of the LAST control point // (it will used to check if the mouse is near the very last polyline element) nextIndex = m_ControlPoints.size() - 1; } else { // For all other polyline points, use the index of the control point succeeding it // (for polyline points lying on control points, the index of the previous control point // is used) nextIndex = (((i - 1) >> this->GetSubdivisionRounds()) + 1) % m_ControlPoints.size(); if(!isInitiallyPlaced && nextIndex > m_ControlPoints.size()-2) { this->AppendPointToPolyLine( 0, m_ControlPoints[m_ControlPoints.size()-1] ); break; } } this->AppendPointToPolyLine( 0, *it ); } subdivisionPoints.clear(); } bool mitk::PlanarSubdivisionPolygon::Equals(const mitk::PlanarFigure& other) const { const mitk::PlanarSubdivisionPolygon* otherSubDivPoly = dynamic_cast(&other); if ( otherSubDivPoly ) { if ( this->m_SubdivisionRounds != otherSubDivPoly->m_SubdivisionRounds) return false; if ( std::abs(this->m_TensionParameter - otherSubDivPoly->m_TensionParameter) > mitk::eps) return false; return Superclass::Equals(other); } else { return false; } } int mitk::PlanarSubdivisionPolygon::GetControlPointForPolylinePoint( int indexOfPolylinePoint, int polyLineIndex ) const { mitk::PlanarFigure::PolyLineType polyLine = GetPolyLine( polyLineIndex ); - if ( indexOfPolylinePoint > static_cast(polyLine.size()) ) - { + + if (indexOfPolylinePoint < 0 || indexOfPolylinePoint > static_cast(polyLine.size())) return -1; - } mitk::PlanarFigure::ControlPointListType::const_iterator elem; mitk::PlanarFigure::ControlPointListType::const_iterator first = m_ControlPoints.cbegin(); mitk::PlanarFigure::ControlPointListType::const_iterator end = m_ControlPoints.cend(); mitk::PlanarFigure::PolyLineType::const_iterator polyLineIter; mitk::PlanarFigure::PolyLineType::const_iterator polyLineEnd = polyLine.cend(); mitk::PlanarFigure::PolyLineType::const_iterator polyLineStart = polyLine.cbegin(); polyLineStart += indexOfPolylinePoint; - for ( polyLineIter = polyLineStart; polyLineIter != polyLineEnd; ++polyLineIter ) + for (polyLineIter = polyLineStart; polyLineIter != polyLineEnd; ++polyLineIter) { - elem = std::find( first, end, *polyLineIter ); - if ( elem != end ) - { - return std::distance( first, elem ); - } + elem = std::find(first, end, *polyLineIter); + + if (elem != end) + return std::distance(first, elem); } return GetNumberOfControlPoints(); }