www.pudn.com > cad3d.zip > 3DSegment.h


// 3DSegment.h: interface for the C3DSegment class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#if !defined(AFX_3DSEGMENT_H__B3F5E583_F61B_11D4_9706_900C9F26A011__INCLUDED_) 
#define AFX_3DSEGMENT_H__B3F5E583_F61B_11D4_9706_900C9F26A011__INCLUDED_ 
 
#if _MSC_VER > 1000 
#pragma once 
#endif // _MSC_VER > 1000 
 
#include "3DMath/3DMath.h" 
#include "3DMath/3DVector.h" 
#include "3DMath/Range.h" 
                                
class C3DPlane; 
 
class MATH3D_API C3DSegment   
{ 
private: 
    // segment and line representation 
    // m_pts[0] is the first point A 
    // m_pts[1] is the second point B 
    // the used linear combination is (1 - dParam)*A + dParam*B 
    // so Evaluate(0) = A = GetPtRef(0) && Evaluate(1) = B = GetPtRef(1) 
     
    C3DPoint			m_pts[2]; 
     
public: 
    inline C3DSegment(const C3DPoint& rPt0 = C3DPoint(), const C3DPoint& rPt1 = C3DPoint());  
     
    inline void			SetPt(int i, const C3DPoint& rPt); 
    inline const C3DPoint&	GetPtRef(int i)	const;  
    inline C3DVector		GetVector() const; 
  
    //evaluates the linear combination dParam*A + (1-dParam)*B  
    inline C3DPoint		Evaluate(math_real dParam) const; 
    inline C3DSegment		GetSubSegment(const CDoubleRange& rRange) const; 
    inline bool			IsPoint() const; 
     
    //intersects the two segments(lines) giving the resulting intersection ranges for *this rOther segments 
    //usually the resulting ranges are points (math_real) which can be evaluated for the math_real intersection point 
    //when the segments(lines) doesn't intersect the returned range IsEmpty() 
    //when the segments share a common line the returned segments can be non - point 
    //use GetSubSegment() to receive the corresponding common subset 
    CDoubleRange		Intersect(const C3DSegment& rOther, CDoubleRange& rOtherRange ) const; 
    CDoubleRange		Intersect(const C3DPlane& rPlane); 
     
    //gets the dParam which gives rPt when using Evaluate(math_real) 
    //fast version  
    //	    : doesn't check if rPt is on the line defined by the segment 
    //	    : doesn't check if rPt is normalized  
    //	    : doesn't check if *this IsPoint() 
    inline math_real		GetEvalParamFast(const C3DPoint& rPt) const; 
 
    //returns the evaluation parameter, but checks if rPt is on the line 
    bool			GetEvalParam(const C3DPoint& rPt, math_real& dParam) const; 
 
}; 
 
inline C3DSegment::C3DSegment(const C3DPoint& rPt0, const C3DPoint& rPt1)  
{  
    SetPt(0,rPt0);  
    SetPt(1,rPt1); 
} 
 
inline void C3DSegment::SetPt(int i, const C3DPoint& rPt)  
{  
    m_pts[i] = rPt;  
    if ( !m_pts[i].IsVector() )//allows vector to create segment?? 
    { 
		m_pts[i].Normalize(); 
    } 
} 
 
inline const C3DPoint& C3DSegment::GetPtRef(int i)	const  
{  
    return m_pts[i];  
} 
 
inline C3DVector C3DSegment::GetVector() const  
{  
    return C3DVector(m_pts[1] - m_pts[0]);  
} 
 
inline C3DPoint	C3DSegment::Evaluate(math_real dParam) const 
{ 
    return m_pts[0]*(1 - dParam)  + m_pts[1]*dParam; 
} 
 
inline C3DSegment C3DSegment::GetSubSegment(const CDoubleRange& rRange) const 
{	 
    return C3DSegment( Evaluate(rRange.GetMin()) , Evaluate(rRange.GetMax()) ); 
} 
 
inline bool C3DSegment::IsPoint() const 
{ 
    return m_pts[0] == m_pts[1]; 
} 
 
inline math_real C3DSegment::GetEvalParamFast(const C3DPoint& rPt) const 
{ 
    C3DVector v1( GetVector() ); 
    C3DVector v2( rPt - m_pts[0] ); 
 
    return (v1*v2)/v1.Norm2(); 
//    return sqrt( v2.Norm2()/v1.Norm2() ); 
 
} 
 
#endif // !defined(AFX_3DSEGMENT_H__B3F5E583_F61B_11D4_9706_900C9F26A011__INCLUDED_)