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

📄 ogreopcodemath.cpp

📁 opcode是功能强大
💻 CPP
📖 第 1 页 / 共 2 页
字号:

			tmp=a*xxyy;
			isect1[0]=tmp+b*x1*yy;
			isect1[1]=tmp+c*x0*yy;

			tmp=d*xxyy;
			isect2[0]=tmp+e*xx*y1;
			isect2[1]=tmp+f*xx*y0;

			SORT(isect1[0],isect1[1]);
			SORT(isect2[0],isect2[1]);

			if(isect1[1]<isect2[0] || isect2[1]<isect1[0]) return false;
			return true;
		}

		/* sort so that a<=b */
#define SORT2(a,b,smallest)       \
	if(a>b)       \
		{             \
		float c;    \
		c=a;        \
		a=b;        \
		b=c;        \
		smallest=1; \
		}             \
	else smallest=0;


		static inline void isect2(const Vector3 tri[3],float VV0,float VV1,float VV2,
			float D0,float D1,float D2,float *isect0,float *isect1,Vector3 isectline[2])
		{
			float tmp=D0/(D0-D1);          
			Vector3 diff;
			*isect0=VV0+(VV1-VV0)*tmp;         
			diff = tri[1] - tri[0];              
			diff = diff * tmp;               
			isectline[0] = diff + tri[0];        
			tmp=D0/(D0-D2);                    
			*isect1=VV0+(VV2-VV0)*tmp;          
			diff = tri[2] - tri[0];                   
			diff = diff * tmp;                 
			isectline[1] = tri[0] + diff;          
		}


		static inline bool compute_intervals_isectline(const Vector3 tri[3],
			float VV0,float VV1,float VV2,float D0,float D1,float D2,
			float D0D1,float D0D2,float *isect0,float *isect1,
			Vector3 isectline[2])
		{
			if(D0D1>0.0f)                                        
			{                                                    
				/* here we know that D0D2<=0.0 */                  
				/* that is D0, D1 are on the same side, D2 on the other or on the plane */
				Vector3 newTri[3] = {tri[2], tri[0], tri[1]};
				isect2(newTri,VV2,VV0,VV1,D2,D0,D1,isect0,isect1,isectline);
			} 
			else if(D0D2>0.0f)                                   
			{   
				Vector3 newTri[3] = {tri[1], tri[0], tri[2]};
				/* here we know that d0d1<=0.0 */             
				isect2(newTri,VV1,VV0,VV2,D1,D0,D2,isect0,isect1,isectline);
			}                                                  
			else if(D1*D2>0.0f || D0!=0.0f)   
			{        
				Vector3 newTri[3] = {tri[0], tri[1], tri[2]};
				/* here we know that d0d1<=0.0 or that D0!=0.0 */
				isect2(newTri,VV0,VV1,VV2,D0,D1,D2,isect0,isect1,isectline);   
			}                                                  
			else if(D1!=0.0f)                                  
			{             
				Vector3 newTri[3] = {tri[1], tri[0], tri[2]};
				isect2(newTri,VV1,VV0,VV2,D1,D0,D2,isect0,isect1,isectline); 
			}                                         
			else if(D2!=0.0f)                                  
			{                  
				Vector3 newTri[3] = {tri[2], tri[0], tri[1]};
				isect2(newTri,VV2,VV0,VV1,D2,D0,D1,isect0,isect1,isectline);     
			}                                                 
			else                                               
			{                                                   
				/* triangles are coplanar */    
				return 1;
			}
			return 0;
		}

#define COMPUTE_INTERVALS_ISECTLINE(VERT0,VERT1,VERT2,VV0,VV1,VV2,D0,D1,D2,D0D1,D0D2,isect0,isect1,isectpoint0,isectpoint1) \
	if(D0D1>0.0f)                                         \
		{                                                     \
		/* here we know that D0D2<=0.0 */                   \
		/* that is D0, D1 are on the same side, D2 on the other or on the plane */ \
		isect2(VERT2,VERT0,VERT1,VV2,VV0,VV1,D2,D0,D1,&isect0,&isect1,isectpoint0,isectpoint1);          \
		}                                                     
#if 0
	else if(D0D2>0.0f)                                    \
	{                                                     \
	/* here we know that d0d1<=0.0 */                   \
	isect2(VERT1,VERT0,VERT2,VV1,VV0,VV2,D1,D0,D2,&isect0,&isect1,isectpoint0,isectpoint1);          \
	}                                                     \
	else if(D1*D2>0.0f || D0!=0.0f)                       \
	{                                                     \
	/* here we know that d0d1<=0.0 or that D0!=0.0 */   \
	isect2(VERT0,VERT1,VERT2,VV0,VV1,VV2,D0,D1,D2,&isect0,&isect1,isectpoint0,isectpoint1);          \
	}                                                     \
	else if(D1!=0.0f)                                     \
	{                                                     \
	isect2(VERT1,VERT0,VERT2,VV1,VV0,VV2,D1,D0,D2,&isect0,&isect1,isectpoint0,isectpoint1);          \
	}                                                     \
	else if(D2!=0.0f)                                     \
	{                                                     \
	isect2(VERT2,VERT0,VERT1,VV2,VV0,VV1,D2,D0,D1,&isect0,&isect1,isectpoint0,isectpoint1);          \
	}                                                     \
	else                                                  \
	{                                                     \
	/* triangles are coplanar */                        \
	coplanar=1;                                         \
	return coplanar_tri_tri(N1,V0,V1,V2,U0,U1,U2);      \
	}
#endif

	/**
	* Tests if two triangles intersect and compute the line of intersection
	* (if they are not coplanar).
	*
	* @param tri1 Vertices of triangle 1
	* @param tri2 Vertices of triangle 2
	* @param[out] isectline The line segment where they intersect
	* @param[out] coplanar Returns whether the triangles are coplanar
	* @return true if the triangles intersect, otherwise false
	*
	*/

	bool Intersect3::triangleTriangle(const Vector3 tri1[3],
		const Vector3 tri2[3],
		Segment3& isectline, bool& coplanar)
	{
		Vector3 E1,E2;
		Vector3 N1,N2;
		float d1,d2;
		float du0,du1,du2,dv0,dv1,dv2;
		Vector3 D;
		float isect1[2] = {0,0}, isect2[2] = {0,0};
		Vector3 isectpointA[2];
		Vector3 isectpointB[2];
		float du0du1,du0du2,dv0dv1,dv0dv2;
		short index;
		float vp0,vp1,vp2;
		float up0,up1,up2;
		float b,c,max;
		int smallest1,smallest2;

		/* compute plane equation of triangle(tri1) */
		E1 = tri1[1] - tri1[0];
		E2 = tri1[2] - tri1[0];
		N1 = E1.crossProduct(E2);
		d1 =-(N1.dotProduct(tri1[0]));
		/* plane equation 1: N1.X+d1=0 */

		/* put tri2 into plane equation 1 to compute signed distances to the plane*/
		du0=(N1.dotProduct(tri2[0]))+d1;
		du1=(N1.dotProduct(tri2[1]))+d1;
		du2=(N1.dotProduct(tri2[2]))+d1;

		/* coplanarity robustness check */
#if USE_EPSILON_TEST==TRUE
		if(fabs(du0)<SMALL_EPSILON) du0=0.0;
		if(fabs(du1)<SMALL_EPSILON) du1=0.0;
		if(fabs(du2)<SMALL_EPSILON) du2=0.0;
#endif
		du0du1=du0*du1;
		du0du2=du0*du2;

		if(du0du1>0.0f && du0du2>0.0f) /* same sign on all of them + not equal 0 ? */
			return 0;                    /* no intersection occurs */

		/* compute plane of triangle (tri2) */
		E1 = tri2[1] - tri2[0];
		E2 = tri2[2] - tri2[0];
		N2 = E1.crossProduct(E2);
		d2=-(N2.dotProduct(tri2[0]));
		/* plane equation 2: N2.X+d2=0 */

		/* put tri1 into plane equation 2 */
		dv0=(N2.dotProduct(tri1[0]))+d2;
		dv1=(N2.dotProduct(tri1[1]))+d2;
		dv2=(N2.dotProduct(tri1[2]))+d2;

#if USE_EPSILON_TEST==TRUE
		if(fabs(dv0)<SMALL_EPSILON) dv0=0.0;
		if(fabs(dv1)<SMALL_EPSILON) dv1=0.0;
		if(fabs(dv2)<SMALL_EPSILON) dv2=0.0;
#endif

		dv0dv1=dv0*dv1;
		dv0dv2=dv0*dv2;

		if(dv0dv1>0.0f && dv0dv2>0.0f) /* same sign on all of them + not equal 0 ? */
			return 0;                    /* no intersection occurs */

		/* compute direction of intersection line */
		D = N1.crossProduct(N2);

		/* compute and index to the largest component of D */
		max=fabs(D[0]);
		index=0;
		b=fabs(D[1]);
		c=fabs(D[2]);
		if(b>max) max=b,index=1;
		if(c>max) max=c,index=2;

		/* this is the simplified projection onto L*/
		vp0=tri1[0][index];
		vp1=tri1[1][index];
		vp2=tri1[2][index];

		up0=tri2[0][index];
		up1=tri2[1][index];
		up2=tri2[2][index];

		/* compute interval for triangle 1 */
		coplanar=compute_intervals_isectline(tri1,vp0,vp1,vp2,dv0,dv1,dv2,
			dv0dv1,dv0dv2,&isect1[0],&isect1[1],isectpointA);
		if(coplanar) return coplanar_tri_tri(N1, tri1, tri2);     


		/* compute interval for triangle 2 */
		compute_intervals_isectline(tri2,up0,up1,up2,du0,du1,du2,
			du0du1,du0du2,&isect2[0],&isect2[1],isectpointB);

		SORT2(isect1[0],isect1[1],smallest1);
		SORT2(isect2[0],isect2[1],smallest2);

		if(isect1[1]<isect2[0] || isect2[1]<isect1[0]) return 0;

		/* at this point, we know that the triangles intersect */

		if(isect2[0]<isect1[0])
		{
			if(smallest1==0) { isectline.setStart(isectpointA[0]); }
			else { isectline.setStart(isectpointA[1]); }

			if(isect2[1]<isect1[1])
			{
				if(smallest2==0) { isectline.setEnd(isectpointB[1]); }
				else { isectline.setEnd(isectpointB[0]); }
			}
			else
			{
				if(smallest1==0) { isectline.setEnd(isectpointA[1]); }
				else { isectline.setEnd(isectpointA[0]); }
			}
		}
		else
		{
			if(smallest2==0) { isectline.setStart(isectpointB[0]); }
			else { isectline.setStart(isectpointB[1]); }

			if(isect2[1]>isect1[1])
			{
				if(smallest1==0) { isectline.setEnd(isectpointA[1]); }
				else { isectline.setEnd(isectpointA[0]); }      
			}
			else
			{
				if(smallest2==0) { isectline.setEnd(isectpointB[1]); }
				else { isectline.setEnd(isectpointB[0]); } 
			}
		}
		return true;
	}

		bool powerOf2(unsigned int i)
		{
			Ogre::String binary = decimalToBinary(i);
			int counter = 0;
			for( int index = 0; index < static_cast<int>(binary.size()); ++index )
				if( binary[index] == '1' ) ++counter;

			if( counter > 1 ) return false;
			else return true;
		}

		Ogre::String decimalToBinary(unsigned int i)
		{
			Ogre::String binary = "";
			Ogre::String temp = "";

			do{
				temp += (i % 2) + '0';
				i = i / 2;
			}while( i > 0 );

			for( int counter = static_cast<int>(temp.size()); counter > 0; --counter )
				binary += temp[counter];

			return binary;
		}

	}
}

⌨️ 快捷键说明

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