00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef SGLineSegment_H
00019 #define SGLineSegment_H
00020
00021 template<typename T>
00022 class SGLineSegment {
00023 public:
00024 SGLineSegment()
00025 { }
00026 SGLineSegment(const SGVec3<T>& start, const SGVec3<T>& end) :
00027 _start(start),
00028 _direction(end - start)
00029 { }
00030 template<typename S>
00031 explicit SGLineSegment(const SGLineSegment<S>& lineSegment) :
00032 _start(lineSegment.getStart()),
00033 _direction(lineSegment.getDirection())
00034 { }
00035
00036 void set(const SGVec3<T>& start, const SGVec3<T>& end)
00037 { _start = start; _direction = end - start; }
00038
00039 const SGVec3<T>& getStart() const
00040 { return _start; }
00041 SGVec3<T> getEnd() const
00042 { return _start + _direction; }
00043 const SGVec3<T>& getDirection() const
00044 { return _direction; }
00045 SGVec3<T> getNormalizedDirection() const
00046 { return normalize(getDirection()); }
00047
00048 SGVec3<T> getCenter() const
00049 { return _start + T(0.5)*_direction; }
00050
00051 SGLineSegment<T> transform(const SGMatrix<T>& matrix) const
00052 {
00053 SGLineSegment<T> lineSegment;
00054 lineSegment._start = matrix.xformPt(_start);
00055 lineSegment._direction = matrix.xformVec(_direction);
00056 return lineSegment;
00057 }
00058
00059 private:
00060 SGVec3<T> _start;
00061 SGVec3<T> _direction;
00062 };
00063
00065 template<typename char_type, typename traits_type, typename T>
00066 inline
00067 std::basic_ostream<char_type, traits_type>&
00068 operator<<(std::basic_ostream<char_type, traits_type>& s,
00069 const SGLineSegment<T>& lineSegment)
00070 {
00071 return s << "line segment: start = " << lineSegment.getStart()
00072 << ", end = " << lineSegment.getEnd();
00073 }
00074
00075 #endif