00001
00002
00003 #include <geom/Utils.hpp>
00004
00005 namespace jafar {
00006 namespace geom {
00007
00008
00009 template<int dimension>
00010 PolyLine<dimension>::PolyLineNM1Driver::PolyLineNM1Driver( const PolyLine<dimension-1>* pl, const RepereD* reference ) : Driver( ), m_polyLine(pl), m_reference(reference)
00011 {
00012 }
00013 template<int dimension>
00014 std::vector< typename PolyLine<dimension>::SegmentD > PolyLine<dimension>::PolyLineNM1Driver::segments() const
00015 {
00016 std::vector< SegmentD > segments;
00017 std::vector< HomogenousVecD > points_ = points();
00018 for(unsigned int i = 0; i < points_.size() - 1; ++i)
00019 {
00020 segments.push_back( SegmentD( new typename SegmentD::TwoPointsDriver( points_[ i ] , points_[ i + 1 ], this->reference() ) ) );
00021 }
00022 segments.push_back( SegmentD( new typename SegmentD::TwoPointsDriver( points_[ points_.size() - 1 ] , points_[ 0 ], this->reference() ) ) );
00023 return segments;
00024 }
00025 template<int dimension>
00026 std::vector< typename PolyLine<dimension>::HomogenousVecD > PolyLine<dimension>::PolyLineNM1Driver::points() const
00027 {
00028 std::vector< HomogenousVecNM1 > pointsNM1 = m_polyLine->points();
00029 std::vector< HomogenousVecD > points;
00030 ublas::range range0n(0,dimension);
00031 for( typename std::vector< HomogenousVecNM1 >::const_iterator it = pointsNM1.begin();
00032 it != pointsNM1.end(); ++it)
00033 {
00034 HomogenousVecD v;
00035 homogenous_vec_range(v, range0n ) = *it;
00036 v[dimension-1] = 0.0;
00037 v[dimension] = 1.0;
00038 points.push_back( v );
00039 }
00040 return points;
00041 }
00042 template<int dimension>
00043 typename PolyLine<dimension>::Driver* PolyLine<dimension>::PolyLineNM1Driver::clone() const
00044 {
00045 return new PolyLineNM1Driver( m_polyLine, m_reference );
00046 }
00047 template<int dimension>
00048 const Repere<dimension>* PolyLine<dimension>::PolyLineNM1Driver::reference( ) const
00049 {
00050 return m_reference;
00051 }
00052 template<int dimension>
00053 bool PolyLine<dimension>::PolyLineNM1Driver::hasCov() const
00054 {
00055 return m_polyLine->hasCov();
00056 }
00057
00058 template<int dimension>
00059 PolyLine<dimension>::PointsPointerDriver::PointsPointerDriver( const std::vector< const Point<dimension>* >& _points) : Driver( ), m_points(_points)
00060 {
00061 }
00062 template<int dimension>
00063 std::vector< typename PolyLine<dimension>::SegmentD > PolyLine<dimension>::PointsPointerDriver::segments() const
00064 {
00065 std::vector< SegmentD > segments;
00066 for(unsigned int i = 0; i < m_points.size() - 1; ++i)
00067 {
00068 segments.push_back( SegmentD( new typename SegmentD::TwoPointsPointerDriver( m_points[ i ] , m_points[ i + 1 ] ) ) );
00069 }
00070 segments.push_back( SegmentD( new typename SegmentD::TwoPointsPointerDriver( m_points[ m_points.size() - 1 ] , m_points[ 0 ] ) ) );
00071 return segments;
00072 }
00073 template<int dimension>
00074 std::vector< typename PolyLine<dimension>::HomogenousVecD > PolyLine<dimension>::PointsPointerDriver::points() const
00075 {
00076 std::vector< HomogenousVecD > points;
00077 const RepereD* ref = reference();
00078 for( typename std::vector< const Point<dimension>* >::const_iterator it = m_points.begin();
00079 it != m_points.end(); ++it)
00080 {
00081 points.push_back( (*it)->reference()->convertIn( (*it)->homogenousCoordinates(), ref) );
00082 }
00083 return points;
00084 }
00085 template<int dimension>
00086 typename PolyLine<dimension>::Driver* PolyLine<dimension>::PointsPointerDriver::clone() const
00087 {
00088 return new PointsPointerDriver( m_points );
00089 }
00090 template<int dimension>
00091 const Repere<dimension>* PolyLine<dimension>::PointsPointerDriver::reference( ) const
00092 {
00093 return m_points[0]->reference();
00094 }
00095 template<int dimension>
00096 bool PolyLine<dimension>::PointsPointerDriver::hasCov() const
00097 {
00098 return allPointersHasCov( m_points );
00099 }
00100
00101 template<int dimension>
00102 PolyLine<dimension>::PointsDriver::PointsDriver( const std::vector< HomogenousVecD >& _points, const RepereD* reference) : Driver(), m_points(_points), m_reference(reference)
00103 {
00104
00105 }
00106 template<int dimension>
00107 std::vector< typename PolyLine<dimension>::SegmentD > PolyLine<dimension>::PointsDriver::segments() const
00108 {
00109 std::vector< SegmentD > segments;
00110 for(unsigned int i = 0; i < m_points.size() - 1; ++i)
00111 {
00112 segments.push_back( SegmentD( new typename SegmentD::TwoPointsDriver( m_points[ i ] , m_points[ i + 1 ], this->reference() ) ) );
00113 }
00114 segments.push_back( SegmentD( new typename SegmentD::TwoPointsDriver( m_points[ m_points.size() - 1 ] , m_points[ 0 ], this->reference() ) ) );
00115 return segments;
00116 }
00117 template<int dimension>
00118 std::vector< typename PolyLine<dimension>::HomogenousVecD > PolyLine<dimension>::PointsDriver::points() const
00119 {
00120 return m_points;
00121 }
00122 template<int dimension>
00123 typename PolyLine<dimension>::Driver* PolyLine<dimension>::PointsDriver::clone() const
00124 {
00125 return new PointsDriver( m_points, this->reference() );
00126 }
00127 template<int dimension>
00128 void PolyLine<dimension>::PointsDriver::changeReference( const RepereD* reference )
00129 {
00130 HomogenousMatrixD m;
00131 this->m_reference->localToRepere(m, reference);
00132 for( typename std::vector< HomogenousVecD >::iterator it = m_points.begin();
00133 it != m_points.end(); ++it)
00134 {
00135 *it = ublas::prod(m, *it);
00136 }
00137 this->m_reference = reference;
00138 }
00139 template<int dimension>
00140 const Repere<dimension>* PolyLine<dimension>::PointsDriver::reference( ) const
00141 {
00142 return m_reference;
00143 }
00144
00145 template<int dimension>
00146 PolyLine<dimension>::PolyLine( Driver* driver ) : Atom<dimension>(driver), m_driver(driver)
00147 {
00148 }
00149 template<int dimension>
00150 PolyLine<dimension>::PolyLine(const PolyLine& pl) : Atom<dimension>(0), m_driver( pl.m_driver->clone() )
00151 {
00152 this->setDriver( m_driver );
00153 this->setId( pl.id() );
00154 }
00155 template<int dimension>
00156 PolyLine<dimension>& PolyLine<dimension>::operator=(const PolyLine& rhs)
00157 {
00158 m_driver = rhs.m_driver->clone();
00159 this->setDriver( m_driver );
00160 this->setId( rhs.id() );
00161 return *this;
00162 }
00163 template<int dimension>
00164 PolyLine<dimension>::~PolyLine()
00165 {
00166 }
00167 template<int dimension>
00168 std::vector< Segment<dimension> > PolyLine<dimension>::segments() const
00169 {
00170 return m_driver->segments();
00171 }
00172 template<int dimension>
00173 std::vector< typename PolyLine<dimension>::HomogenousVecD > PolyLine<dimension>::points() const
00174 {
00175 return m_driver->points();
00176 }
00177 template<int dimension>
00178 double PolyLine<dimension>::perimeter() const
00179 {
00180 std::vector< typename PolyLine<dimension>::SegmentD > segments_ = segments();
00181 double perimeter_ = 0.0;
00182 for( typename std::vector< typename PolyLine<dimension>::SegmentD >::iterator it = segments_.begin(); it != segments_.end(); ++it)
00183 {
00184 perimeter_ += it->length();
00185 }
00186 return perimeter_;
00187 }
00188 template<int dimension>
00189 BoundingBox<dimension> PolyLine<dimension>::boundingBox() const
00190 {
00191 std::vector< HomogenousVecD > points_ = m_driver->points();
00192 HomogenousVecD corner1 = points_[0] , corner2 = points_[0];
00193 for( typename std::vector< HomogenousVecD >::const_iterator it = points_.begin();
00194 it != points_.end(); ++it)
00195 {
00196 for(int i = 0; i < dimension; i ++)
00197 {
00198 double v = (*it)[i];
00199 if( v < corner1[i] ) corner1[i] = v;
00200 if( v > corner2[i] ) corner2[i] = v;
00201 }
00202 }
00203 return BoundingBox<dimension>( this->reference()->convertInGlobal( corner1 ), this->reference()->convertInGlobal( corner2 ) );
00204 }
00205 }
00206 }