www.pudn.com > cad3d.zip > Triangle_code.h


/* 
 
// TopTriangle.h: interface for the CTopTriangle class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#if !defined(AFX_TOPTRIANGLE_H__22F2FA03_FAAE_11D4_9717_89BB119E5810__INCLUDED_) 
#define AFX_TOPTRIANGLE_H__22F2FA03_FAAE_11D4_9717_89BB119E5810__INCLUDED_ 
 
#if _MSC_VER > 1000 
#pragma once 
#endif // _MSC_VER > 1000 
#include "3DMath\3DVector.h" 
#include "3DMath\3DSegment.h" 
#include "3DMath\3DMatrix.h" 
#include "3DMath\3DPlane.h" 
#include "Topology\Generic\LinkedPoly.h" 
 
class CTopTriangle; 
typedef std::list CTopTriangleList; 
 
class CTopTriangle : public CLinkedPoly 
//class CTopTriangle : public CLinkedPoly 
{ 
 
public: 
	CTopTriangle(C3DPoint *pPt0, C3DPoint *pPt1, C3DPoint *pPt2); 
	~CTopTriangle(); 
 
}; 
 
 
inline CTopTriangle::CTopTriangle(C3DPoint *pPt0, C3DPoint *pPt1, C3DPoint *pPt2) 
{ 
    SetVertex(0, pPt0); 
    SetVertex(1, pPt1); 
    SetVertex(2, pPt2); 
} 
 
inline CTopTriangle::~CTopTriangle() 
{ 
 
} 
 
*/ 
/* 
class CTriangle; 
typedef std::list CTriList; 
 
class CTriangle : public CPoly//public CLinkedPoly 
{ 
public: 
    C3DPoint	    m_color; 
 
    CTriangle() : CPoly() {}; 
 
     
    CTriangle(C3DPoint& pt0, C3DPoint& pt1, C3DPoint& pt2, C3DPoint color = C3DPoint()) 
    { 
	SetVertex(0,&pt0);	 
	SetVertex(1,&pt1);	 
	SetVertex(2,&pt2);	 
	m_color = color; 
    } 
     
    C3DVector	GetNormal() const 
    { 
	C3DVector v1( (*this)(1) - (*this)(0) );  
	C3DVector v2( (*this)(2) - (*this)(0) );  
	 
	C3DVector vz(0,0,1); 
 
	C3DVector n = v1^v2; 
	if ( n*vz < 0 ) n*= -1; 
	return n; 
 
    } 
 
    void	Project(C3DMatrix& rMatr, CTriangle& rTri, C3DPointList& rVertices) const 
    { 
	rTri.m_color = m_color; 
	for (int i = 0; i < 3; ++i ) 
	{ 
	    C3DPoint pt = rMatr*(*this)(i); 
	    rVertices.push_back(pt); 
	    rTri.SetVertex(i, &rVertices.back() ); 
	} 
    } 
 
    void	Draw() const 
    { 
    } 
 
     
    //returns the |*this|(i) (with respect to the i-th axe)  
    CDoubleRange	GetRange(int nId) const 
    { 
	CDoubleRange res( (*this)(0)(nId), (*this)(0)(nId) ); 
	for (int i = 1; i < 3; ++i) 
	{ 
	    if ( res.GetMin() > (*this)(i)(nId) )  
		res.SetMin( (*this)(i)(nId) ); 
	    if ( res.GetMax() < (*this)(i)(nId) )  
		res.SetMax( (*this)(i)(nId) ); 
	}  
	return res; 
    } 
 
    CTriangle   GetOxyProj(C3DPoint* vers) const 
    { 
	CTriangle proj; 
	vers[0] = C3DPoint( (*this)(0)(0), (*this)(0)(1), 0, 1); 
	vers[1] = C3DPoint( (*this)(1)(0), (*this)(1)(1), 0, 1); 
	vers[2] = C3DPoint( (*this)(2)(0), (*this)(2)(1), 0, 1); 
	proj.SetVertex(0, vers); 
	proj.SetVertex(1, vers + 1); 
	proj.SetVertex(2, vers + 2); 
	return proj; 
    } 
 
    C3DPlane	GetPlane() const  
    { 
	//asserts that the triangle points are finite... 
	CHECK( !(*this)(0).IsVector()); 
 
	C3DPlane pl( GetNormal() ); 
	pl(3) = - (pl*(*this)(0))/(*this)(0)(3); 
	return pl; 
    }; 
 
    //vertex/triangle's id find helpers 
    void	GetOtherIds(int nVerId, int& nVer1Id, int& nVer2Id) const 
    { 
	nVer1Id = (nVerId + 1) % 3; 
	nVer2Id = (nVerId + 2) % 3; 
    } 
 
    int		GetOtherId(int nVer1Id, int nVer2Id) const 
    { 
	CHECK( nVer1Id != nVer2Id ); 
	return (3 - nVer1Id - nVer2Id);// 0 + 1 + 2 = 3 :) 
    } 
 
    C3DSegment	GetEdge(int i) const 
    { 
	int i1,i2; 
	GetOtherIds(i,i1,i2); 
	return C3DSegment( (*this)(i1), (*this)(i2) ); 
    } 
	 
    int		Split(const C3DPlane& rPlane, CTriList& rResTris, C3DPointList& rVertices) const 
    { 
	C3DSegment edges[3]; 
	CDoubleRange ranges[3]; 
	C3DPoint interPts[3]; 
	CDoubleRange inSeg(0,1); 
	int i; 
	int nSplittedPts = 0; 
	int nNotSplittedId = -1; 
	int nLastSplittedId = -1; 
 
	for (i = 0; i < 3; ++i ) 
	{ 
	    bool bHasSplit = false; 
	    edges[i] = GetEdge(i); 
	    ranges[i] = edges[i].Intersect(rPlane); 
	    if ( ranges[i].IsPoint() && inSeg.IsSubRange(ranges[i]) ) 
	    { 
		interPts[i] = edges[i].Evaluate(ranges[i].GetMax() ); 
		if ( !inSeg.IsLimit(ranges[i].GetMax()) ) 
		{ 
		    nSplittedPts++; 
		    nLastSplittedId = i; 
		    bHasSplit = true; 
		} 
	    }	 
	    if (!bHasSplit) 
	    { 
		nNotSplittedId = i; 
	    } 
	} 
 
 
	if (nSplittedPts == 1) 
	{ 
	    rVertices.push_back(interPts[nLastSplittedId]); 
 
	    int i1, i2; 
	    GetOtherIds(nLastSplittedId, i1, i2); 
 
 
	    CTriangle tri0( (*this)(nLastSplittedId), (*this)(i1), rVertices.back(),m_color ); 
	    rResTris.push_back(tri0); 
 
	    CTriangle tri1( (*this)(nLastSplittedId), rVertices.back(),(*this)(i2), m_color); 
	    rResTris.push_back(tri1); 
 
	    return 2; 
	} 
	else if (nSplittedPts == 2) 
	{ 
	     
	    int i1,i2; 
	    C3DPoint* pIntPts[2]; 
	    GetOtherIds(nNotSplittedId, i1, i2); 
 
	    rVertices.push_back(interPts[i1]); 
	    pIntPts[0] = &rVertices.back(); 
	     
	    rVertices.push_back(interPts[i2]); 
	    pIntPts[1] = &rVertices.back(); 
 
	    CTriangle tri0((*this)(nNotSplittedId), *pIntPts[0], *pIntPts[1], m_color);	     
	    rResTris.push_back(tri0); 
 
	    CTriangle tri1((*this)(i1), *pIntPts[0], *pIntPts[1], m_color);	     
	    rResTris.push_back(tri1); 
 
	    //pIntPts[0] is the split pt opposite to [i1] vertex 	     
	    CTriangle tri2((*this)(i2), (*this)(i1), *pIntPts[0], m_color); 
	    rResTris.push_back(tri2); 
	    return 3; 
	} 
	return 0; 
	 
    } 
 
    bool	IsZOrder ( const CTriangle& rOther) const 
    { 
	CDoubleRange myRange = GetRange(2); 
	CDoubleRange otherRange = rOther.GetRange(2); 
 
	return	myRange.GetMax() < otherRange.GetMax() ||  
		Equal(myRange.GetMax(), otherRange.GetMax()); 
    } 
 
    //checks if all points of rOther are on the same side to this->GetPlane(); 
    bool	IsOneSide(const CTriangle& rOther) const 
    { 
	C3DPlane plane = GetPlane(); 
	double dSign = plane*rOther(0); 
	for (int i = 1; i < 2; i++) 
	{ 
	    double dCurrSign = plane*rOther(i); 
	    if ( IsLt(dSign*dCurrSign,0)  )  
	    { 
		return false; 
	    } 
	} 
	return true; 
		 
    } 
 
    bool	IsSortable(const CTriangle& rOther) const 
    { 
	int i; 
	for (i = 2; i >= 0; --i) 
	{ 
	    CDoubleRange range = GetRange(i).Intersect(rOther.GetRange(i)); 
	    if ( range.IsEmpty() ) 
		return true; 
	} 
	 
	if ( IsOneSide(rOther) || rOther.IsOneSide(*this) )  
	{ 
	    return true; 
	} 
	return false; 
 
    } 
 
}; 
#endif // !defined(AFX_TOPTRIANGLE_H__22F2FA03_FAAE_11D4_9717_89BB119E5810__INCLUDED_) 
*/