OpenWalnut  1.4.0
WLine.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WLINE_H
00026 #define WLINE_H
00027 
00028 #include <vector>
00029 
00030 #include "../WBoundingBox.h"
00031 
00032 #include "../WMixinVector.h"
00033 #include "linearAlgebra/WPosition.h"
00034 
00035 // forward declarations
00036 class WLineTest;
00037 
00038 /**
00039  * A line is an ordered sequence of WPositions.
00040  */
00041 class WLine : public WMixinVector< WPosition >
00042 {
00043 public:
00044     /**
00045      * Generates a new line out of a sequence of points.
00046      *
00047      * \param points Point sequence
00048      */
00049     explicit WLine( const std::vector< WPosition > &points );
00050 
00051     /**
00052      * Creates an empty line.
00053      */
00054     WLine();
00055 
00056     /**
00057      * Resample this line so it has a number of given points afterwards.
00058      * \warning This changes your line!
00059      *
00060      * \param numPoints Number of sampling points.
00061      */
00062     void resampleByNumberOfPoints( size_t numPoints );
00063 
00064     /**
00065      * Resample this line so there are only segements of the given length.
00066      *
00067      * \warning This may shorten fib if new segment length is bigger than the remainder of the original fiber under the new sampling.
00068      *
00069      * \param newSegementLength
00070      */
00071     void resampleBySegmentLength( double newSegementLength );
00072 
00073     /**
00074      * Reverses the order of the points. (mirroring)
00075      */
00076     void reverseOrder();
00077 
00078     /**
00079      * Collapse samplepoints which are equal and neighboured.
00080      */
00081     void removeAdjacentDuplicates();
00082 
00083     /**
00084      * Put the line into reverse ordering if the reverse ordering would have a
00085      * similar direction to the given line. That means if the start point (or
00086      * multiple selected sample points) of the given line will better match to
00087      * end point (or multiple selected sample points) of this line (in term of
00088      * direction) the line is reordered.
00089      *
00090      * \param other The line giving the direction to align this line to.
00091      */
00092     void unifyDirectionBy( const WLine& other );
00093 };
00094 
00095 // Some convinience functions as non-member non-friend functions
00096 
00097 /**
00098  * Computes a AABB (axis aligned bounding box) for all positions inside this line.
00099  *
00100  * \param line The line to compute the bounding box for.
00101  *
00102  * \return The AABB for this line.
00103  */
00104 WBoundingBox computeBoundingBox( const WLine& line );
00105 
00106 /**
00107  * Computes the length of a line in terms of accumulated segment lengths.
00108  *
00109  * \param line The line which used for computations
00110  *
00111  * \return Sum of all line segment lengths
00112  */
00113 double pathLength( const WLine& line );
00114 
00115 /**
00116  * Returns the point in the middle of a line. In case of an even sized
00117  * line the mid point is the same as if there were only size()-1 many
00118  * elements present.
00119  *
00120  * \param line The line to compute the mid point for.
00121  *
00122  * \throws WOutOfBounds In case its called on an empty line
00123  *
00124  * \return Const reference to the midpoint element.
00125  */
00126 const WPosition& midPoint( const WLine& line );
00127 
00128 /**
00129  * Compares two lines with each other point wise upto a given delta.
00130  *
00131  * \param line The first line
00132  * \param other The other line
00133  * \param delta Specifying the environment upto this two points are considered to be the same
00134  *
00135  * \return -1 in case of the two fibers are considered equal, otherwise the first position on which they differ is returned.
00136  */
00137 int equalsDelta( const WLine& line, const WLine& other, double delta );
00138 
00139 /**
00140  * Compute the maximal segment length of all segements of a line. If there are no segements meaning
00141  * zero or one point, zero is returned.
00142  *
00143  * \param line The line used for computation of the max segment length
00144  *
00145  * \return Max segement length or zero if there aren't any.
00146  */
00147 double maxSegmentLength( const WLine& line );
00148 
00149 /**
00150  * Boolean predicate indicating that the first line has more points then
00151  * the second one.
00152  *
00153  * \param first First line
00154  * \param second Second line
00155  * \return True if the first line has more points than the second
00156  */
00157 bool hasMorePointsThen( const WLine& first, const WLine& second );
00158 
00159 inline bool hasMorePointsThen( const WLine& first, const WLine& second )
00160 {
00161     return first.size() > second.size();
00162 }
00163 
00164 #endif  // WLINE_H