⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 triangle.cpp

📁 java实现的简单的分形树。简单易学!是学习分形知识的很好的例子。其java语法简单
💻 CPP
字号:
// --------------------------------------------------------------------------
// Dingus project - a collection of subsystems for game/graphics applications
// --------------------------------------------------------------------------
#include "stdafx.h"

#include "Triangle.h"

using namespace dingus;


bool STriangle::intersect( const SLine3& line, float& ipos ) const
{
	const float tol = 1e-04f;
	SVector3	norm = getNormalNoNorm();
	float		denom = norm.dot( line.vec );
	
	if( denom >= -tol )
		return false; // backface or parallel
	
	// compute line t so that it intersects triangle's plane
	SVector3 kDiff0(line.pos - base);
	float time = -(norm.dot(kDiff0)) / denom;
	if( time<-tol || time>(1.0f+tol) )
		return false; // outside line range
	
	// diff of intersection point and vertex of triangle.
	SVector3 kDiff1( kDiff0 + line.vec*time );
	
	// see if inside tri
	float fE00 = edge0.lengthSq();	// 0 dot 0
	float fE01 = edge0.dot( edge1 );// 0 dot 1
	float fE11 = edge1.lengthSq();	// 1 dot 1
	float fDet = fabsf( fE00*fE11-fE01*fE01 );
	float fR0  = edge0.dot( kDiff1 );
	float fR1  = edge1.dot( kDiff1 );
	
	float fS0 = fE11*fR0 - fE01*fR1;
	float fS1 = fE00*fR1 - fE01*fR0;
	
	if( (fS0>=-tol) && (fS1>=-tol) && (fS0+fS1<=fDet+tol) ) {
		ipos = time;
		return true; // inside
	} else {
		return false; // outside
	}
}


bool STriangle::intersect2Sided( const SLine3& line, float& ipos ) const
{
	
	const float tol = 1e-04f;
	SVector3	norm = getNormalNoNorm();
	float		denom = norm.dot( line.vec );
	float llenSq = line.lengthSq();
	float nLenSq = norm.lengthSq();
	
	if( denom*denom <= tol*llenSq*nLenSq )
		return false;
	
	// compute line t so that it intersects triangle's plane
	SVector3 kDiff0(line.pos - base);
	float time = -(norm.dot(kDiff0)) / denom;
	if( time<-tol || time>(1.0f+tol) )
		return false; // outside line range
	
	// diff of intersection point and vertex of triangle.
	SVector3 kDiff1( kDiff0 + line.vec*time );
	
	// see if inside tri
	float fE00 = edge0.lengthSq();	// 0 dot 0
	float fE01 = edge0.dot( edge1 );// 0 dot 1
	float fE11 = edge1.lengthSq();	// 1 dot 1
	float fDet = fabsf( fE00*fE11-fE01*fE01 );
	float fR0  = edge0.dot( kDiff1 );
	float fR1  = edge1.dot( kDiff1 );
	
	float fS0 = fE11*fR0 - fE01*fR1;
	float fS1 = fE00*fR1 - fE01*fR0;
	
	if( (fS0>=-tol) && (fS1>=-tol) && (fS0+fS1<=fDet+tol) ) {
		ipos = time;
		return true; // inside
	} else {
		return false; // outside
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -