www.pudn.com > final_6.rar > Operation.cpp


#include "stdafx.h" 
#include "CG.h" 
#include "math.h" 
#include "Shape.h" 
#include "Operation.h" 
#include "CGDoc.h" 
#include "Attach.h" 
#include "base.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
void COperation::CaculateFace(CGPlane *face, CGPoint x1,CGPoint x2,CGPoint x3,CGPoint x4) 
{   
	//由4个点计算他们所在的平面 
     face->a=0;face->b=0;face->c=0;face->d=0; 
	 face->a=(x1.y-x2.y)*(x1.z+x2.z)+(x2.y-x3.y)*(x2.z+x3.z)+(x3.y-x4.y)*(x3.z+x4.z)+(x4.y-x1.y)*(x4.z+x1.z); 
     face->b=(x1.z-x2.z)*(x1.x+x2.x)+(x2.z-x3.z)*(x2.x+x3.x)+(x3.z-x4.z)*(x3.x+x4.x)+(x4.z-x1.z)*(x4.x+x1.x); 
	 face->c=(x1.x-x2.x)*(x1.y+x2.y)+(x2.x-x3.x)*(x2.y+x3.y)+(x3.x-x4.x)*(x3.y+x4.y)+(x4.x-x1.x)*(x4.y+x1.y); 
     face->d=-(face->a*x1.x+face->b*x1.y+face->c*x1.z); 
} 
 
void COperation::ScreenToReal(CGPoint *mousePoint,CPoint point, int view)  
{  //场景坐标转换世界坐标 
    switch (view) 
	{ 
	   case TOP: 
		   { 
		     mousePoint->x=point.x-220;  
			 mousePoint->y=0; 
			 mousePoint->z=point.y-160; 
		   break; 
		   } 
	   case LEFT: 
		   { 
             mousePoint->x=0;  
			 mousePoint->y=(point.y-160); 
			 mousePoint->z=-(point.x-395/2-1); 
		   break; 
		   } 
	   case FRONT: 
		   { 
             mousePoint->x=point.x-440/2;  
			 mousePoint->y=point.y-318/2; 
			 mousePoint->z=0; 
      	   break; 
		   } 
       case PER: 
		   { 
             break; 
		   } 
	   default: 
		   { 
		   } 
	} 
} 
 
void COperation::CaculateVector(vector mouseVector, CGPoint *point1, CGPoint *point2) 
{ 
  //计算鼠标向量 
  //并且单位化该向量 
   double temp; 
   
   mouseVector[0]=point1->x-point2->x; 
   mouseVector[1]=point1->y-point2->y; 
   mouseVector[2]=point1->z-point2->z; 
   mouseVector[3]=1; 
   temp=sqrt(mouseVector[0]*mouseVector[0]+mouseVector[1]*mouseVector[1]+mouseVector[2]*mouseVector[2]); 
   mouseVector[0]=mouseVector[0]/temp; 
   mouseVector[1]=mouseVector[1]/temp; 
   mouseVector[2]=mouseVector[2]/temp; 
} 
 
BOOL COperation::Ray_Hit(CGPoint *startPoint, vector mouseVector,int view) 
{ 
   //计算是否和实体相交 
   //并且将全局变量curPointTo置为所相交的实体 
   //CObArray *shapeArray; 
   // shapeArray=&(g_pDoc->m_ShapeArray); 
   int x; 
   for (x=0;xm_ShapeArray[x], startPoint, mouseVector,view)) 
	 { 
		 selectNo=x; 
	     return TRUE; 
	 } 
   } 
   selectNo=-1; 
   return FALSE; 
} 
 
BOOL COperation::Ray_Hit_Entity(CShape *shape, CGPoint *startPoint, vector mouseVector,int view) 
{ 
    int x,y; 
	double dotProduct; 
	double t; 
	//光线与具体实体求交 
	 
	if(shape->IsKindOf(RUNTIME_CLASS(CColumn))) 
    { 
       CColumn *pColumn; 
	   pColumn=(CColumn*)shape; 
       double x1,y1,z1,distance,rad; 
	   double x2,y2,z2; 
	   dotProduct=dot(pColumn->BFace[8][0],mouseVector); 
	    if (dotProductBFace[8][0],startPoint)+pColumn->BFace[8][0].d)/dotProduct; 
           	if (t>-LIMIT_ZERO) 
			{ 
			   rad=pColumn->radius; 
			   x1=startPoint->x+t*mouseVector[0]; 
			   y1=startPoint->y+t*mouseVector[1]; 
			   z1=startPoint->z+t*mouseVector[2]; 
               x2=(pColumn->BPoint[0][0].x+pColumn->BPoint[0][8].x)/2; 
               y2=(pColumn->BPoint[0][0].y+pColumn->BPoint[0][8].y)/2; 
               z2=(pColumn->BPoint[0][0].z+pColumn->BPoint[0][8].z)/2; 
			   distance=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2); 
 		       if (distance<=rad*rad) 
			  { 
				  xx=8;yy=0;tt=t; 
				 return TRUE; 
			  } 
			} 
		 } 
          dotProduct=dot(pColumn->BFace[8][1],mouseVector); 
	  if (dotProductBFace[8][1],startPoint)+pColumn->BFace[8][1].d)/dotProduct; 
           	 if (t>-LIMIT_ZERO) 
			 { 
			    rad=pColumn->radius; 
			    x1=startPoint->x+t*mouseVector[0]; 
			    y1=startPoint->y+t*mouseVector[1]; 
		        z1=startPoint->z+t*mouseVector[2]; 
                x2=(pColumn->BPoint[8][0].x+pColumn->BPoint[8][8].x)/2; 
                y2=(pColumn->BPoint[8][0].y+pColumn->BPoint[8][8].y)/2; 
                z2=(pColumn->BPoint[8][0].z+pColumn->BPoint[8][8].z)/2; 
		        distance=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2); 
 		        if (distance<=rad*rad) 
				{ 
			        xx=8;yy=1;tt=t; 
			     	 return TRUE; 
				} 
			 } 
	 } 
	  for (x=0;x<=7;x++) 
	  { 
	    for (y=0;y<=15;y++) 
		{ 
         dotProduct=dot(pColumn->BFace[x][y],mouseVector); 
	 if (dotProductBFace[x][y],startPoint)+pColumn->BFace[x][y].d)/dotProduct; 
            if (t>-LIMIT_ZERO) 
			{ 
				if (PointOnFace(pColumn,x,y,t,startPoint,mouseVector,view)) 
				{  
                   // AfxMessageBox("OK"); 
                      xx=x;yy=y;tt=t; 
			    	 return TRUE; 
				} 
			} 
		} 
		} 
	  } 
      
    } 
    else if (shape->IsKindOf(RUNTIME_CLASS(CCuboid))){ 
	   
	  CShape *pColumn; 
	  pColumn=shape; 
	  for (x=0;x<=1;x++) 
	  { 
	     dotProduct=dot(pColumn->BFace[8][x],mouseVector); 
	    if (dotProductBFace[8][x],startPoint)+pColumn->BFace[8][x].d)/dotProduct; 
             if (t>-LIMIT_ZERO){ 
 
			 if (PointOnFace(pColumn,8,x,t,startPoint,mouseVector,view)) 
			 {  
                  
				 xx=8;yy=x;tt=t; 
				 return TRUE; 
   			 } 
			 } 
		 } 
 	  } 
	  for (x=0;x<=7;x++) 
	  { 
	    for (y=0;y<=15;y++) 
		{ 
         dotProduct=dot(pColumn->BFace[x][y],mouseVector); 
	 if (dotProductBFace[x][y],startPoint)+pColumn->BFace[x][y].d)/dotProduct; 
            if (t>-LIMIT_ZERO) 
			{ 
				if (PointOnFace(pColumn,x,y,t,startPoint,mouseVector,view)) 
				{  
                   // AfxMessageBox("OK"); 
                      xx=x;yy=y;tt=t; 
			    	 return TRUE; 
				} 
			} 
		 } 
	} 
	  } 
	} 
    else if (shape->IsKindOf(RUNTIME_CLASS(CBall))) 
	{ 
         CShape *pColumn; 
	     pColumn=shape; 
	     for (x=0;x<=7;x++) 
		 { 
	       for (y=0;y<=15;y++) 
		   { 
        dotProduct=dot(pColumn->BFace[x][y],mouseVector); 
	     if (dotProductBFace[x][y],startPoint)+pColumn->BFace[x][y].d)/dotProduct; 
               if (t>-LIMIT_ZERO) 
			   { 
			    	if (PointOnFace(pColumn,x,y,t,startPoint,mouseVector,view)) 
					{  
                        xx=x;yy=y;tt=t; 
						return TRUE; 
					} 
			   } 
		 } 
		   } 
		 } 
	}else if (shape->IsKindOf(RUNTIME_CLASS(CCone))) 
	{ 
	     CCone *pColumn; 
	     pColumn=(CCone*)shape; 
	     for (x=0;x<=7;x++) 
		 { 
	       for (y=0;y<=15;y++) 
		   { 
             dotProduct=dot(pColumn->BFace[x][y],mouseVector); 
	     if (dotProductBFace[x][y],startPoint)+pColumn->BFace[x][y].d)/dotProduct; 
               if (t>-LIMIT_ZERO) 
			   { 
			    	if (PointOnFace(pColumn,x,y,t,startPoint,mouseVector,view)) 
					{  
                        xx=x;yy=y;tt=t; 
					    return TRUE; 
					} 
			   } 
			 } 
   } 
		 } 
 		  
	 dotProduct=dot(pColumn->BFace[8][1],mouseVector); 
   if (dotProductBFace[8][1],startPoint)+pColumn->BFace[8][1].d)/dotProduct; 
           if (t>-LIMIT_ZERO) 
		   { 
			 double x1,y1,z1,distance,rad; 
			 rad=pColumn->radius; 
			 x1=startPoint->x+t*mouseVector[0]; 
			 y1=startPoint->y+t*mouseVector[1]; 
			 z1=startPoint->z+t*mouseVector[2]; 
             distance=(x1-pColumn->center.x)*(x1-pColumn->center.x) 
				     +(y1-pColumn->center.y)*(y1-pColumn->center.y) 
					 +(z1-pColumn->center.z)*(z1-pColumn->center.z); 
 		  if (distance<=rad*rad) 
			  { 
			     xx=8;yy=1;tt=t; 
				 return TRUE; 
			  } 
   } 
		 } 
	} 
	return FALSE; 
} 
 
double COperation::dot(CGPlane face, vector Vector) 
{ 
   //向量与面法向量乘积 
   return face.a*Vector[0]+face.b*Vector[1]+face.c*Vector[2]; 
} 
 
double COperation::dot(CGPlane face, CGPoint *point) 
{ 
   return face.a*point->x+face.b*point->y+face.c*point->z; 
} 
 
BOOL COperation::PointOnFace(CShape* shape,int x,int y,double t, CGPoint *startPoint, vector Vector,int view) 
{   
     //判断点是否在面上 
	 double planePoint[4][2]; 
	 double pointX,pointY; 
	 double a,b,c; 
	 int k; 
   
	 switch (view) 
	 { 
	 case TOP: //向上视图投影 
		 { 
     	   	 if((x!=8)&&(y!=15)){ 
		         /*planePoint[0][0]=shape->BPoint[x][y].x; planePoint[0][1]=shape->BPoint[x][y].z; 
	             planePoint[1][0]=shape->BPoint[x][y+1].x; planePoint[1][1]=shape->BPoint[x][y+1].z; 
	             planePoint[2][0]=shape->BPoint[x+1][y+1].x; planePoint[2][1]=shape->BPoint[x+1][y+1].z; 
                 planePoint[3][0]=shape->BPoint[x+1][y].x; planePoint[3][1]=shape->BPoint[x+1][y].z;*/ 
                 planePoint[0][0]=shape->BPoint[x][y].x; planePoint[0][1]=shape->BPoint[x][y].z; 
	             planePoint[1][0]=shape->BPoint[x+1][y].x; planePoint[1][1]=shape->BPoint[x+1][y].z; 
	             planePoint[2][0]=shape->BPoint[x+1][y+1].x; planePoint[2][1]=shape->BPoint[x+1][y+1].z; 
                 planePoint[3][0]=shape->BPoint[x][y+1].x; planePoint[3][1]=shape->BPoint[x][y+1].z; 
			 } 
			 else if (y==15){ 
			     /*planePoint[0][0]=shape->BPoint[x][y].x; planePoint[0][1]=shape->BPoint[x][y].z; 
	             planePoint[1][0]=shape->BPoint[x][0].x; planePoint[1][1]=shape->BPoint[x][0].z; 
	             planePoint[2][0]=shape->BPoint[x+1][0].x; planePoint[2][1]=shape->BPoint[x+1][0].z; 
                 planePoint[3][0]=shape->BPoint[x+1][y].x; planePoint[3][1]=shape->BPoint[x+1][y].z;*/ 
                 planePoint[0][0]=shape->BPoint[x][y].x; planePoint[0][1]=shape->BPoint[x][y].z; 
	             planePoint[1][0]=shape->BPoint[x+1][y].x; planePoint[1][1]=shape->BPoint[x+1][y].z; 
	             planePoint[2][0]=shape->BPoint[x+1][0].x; planePoint[2][1]=shape->BPoint[x+1][0].z; 
                 planePoint[3][0]=shape->BPoint[x][0].x; planePoint[3][1]=shape->BPoint[x][0].z; 
 
 
			 } 
	         else if ((x==8)&&(y==1)){ 
		           planePoint[0][0]=shape->BPoint[x][0].x; planePoint[0][1]=shape->BPoint[x][0].z; 
                   planePoint[1][0]=shape->BPoint[x][12].x; planePoint[1][1]=shape->BPoint[x][12].z; 
                   planePoint[2][0]=shape->BPoint[x][8].x; planePoint[2][1]=shape->BPoint[x][8].z; 
     	           planePoint[3][0]=shape->BPoint[x][4].x; planePoint[3][1]=shape->BPoint[x][4].z; 
			 } 
		     else if ((x==8)&&(y==0)) 
			 { 
                   planePoint[0][0]=shape->BPoint[0][0].x;planePoint[0][1]=shape->BPoint[0][0].z; 
                   planePoint[1][0]=shape->BPoint[0][4].x;planePoint[1][1]=shape->BPoint[0][4].z; 
                   planePoint[2][0]=shape->BPoint[0][8].x;planePoint[2][1]=shape->BPoint[0][8].z; 
     	           planePoint[3][0]=shape->BPoint[0][12].x; planePoint[3][1]=shape->BPoint[0][12].z; 
			 } 
		  
                  pointX=startPoint->x+t*Vector[0]; 
                  pointY=startPoint->z+t*Vector[2]; 
                  for (k=0;k<=2;k++) 
				  { 
	                 a=planePoint[k+1][1]-planePoint[k][1]; 
	                 b=planePoint[k][0]-planePoint[k+1][0]; 
	                 c=planePoint[k+1][0]*planePoint[k][1]-planePoint[k][0]*planePoint[k+1][1]; 
	            	if ((a*pointX+b*pointY+c)<-LIMIT_ZERO) 
		              	return FALSE; 
				  }  
                     a=planePoint[0][1]-planePoint[3][1]; 
                   	 b=planePoint[3][0]-planePoint[0][0]; 
                     c=planePoint[0][0]*planePoint[3][1]-planePoint[3][0]*planePoint[0][1]; 
	                if ((a*pointX+b*pointY+c)<-LIMIT_ZERO) 	return FALSE; 
				  break; 
			 } 
	 case LEFT://向左视图投影 
		 { 
		      
     	    
			 if((x!=8)&&(y!=15)){ 
		         planePoint[0][0]=shape->BPoint[x][y].z; planePoint[0][1]=shape->BPoint[x][y].y; 
	             planePoint[1][0]=shape->BPoint[x][y+1].z; planePoint[1][1]=shape->BPoint[x][y+1].y; 
	             planePoint[2][0]=shape->BPoint[x+1][y+1].z; planePoint[2][1]=shape->BPoint[x+1][y+1].y; 
                 planePoint[3][0]=shape->BPoint[x+1][y].z; planePoint[3][1]=shape->BPoint[x+1][y].y; 
			 } 
			 else if (y==15){ 
			     planePoint[0][0]=shape->BPoint[x][y].z; planePoint[0][1]=shape->BPoint[x][y].y; 
	             planePoint[1][0]=shape->BPoint[x][0].z; planePoint[1][1]=shape->BPoint[x][0].y; 
	             planePoint[2][0]=shape->BPoint[x+1][0].z; planePoint[2][1]=shape->BPoint[x+1][0].y; 
                 planePoint[3][0]=shape->BPoint[x+1][y].z; planePoint[3][1]=shape->BPoint[x+1][y].y; 
			 } 
			 else if ((x==8)&&(y==1)) { 
		           planePoint[0][0]=shape->BPoint[x][0].z; planePoint[0][1]=shape->BPoint[x][0].y; 
                   planePoint[1][0]=shape->BPoint[x][4].z; planePoint[1][1]=shape->BPoint[x][4].y; 
                   planePoint[2][0]=shape->BPoint[x][8].z; planePoint[2][1]=shape->BPoint[x][8].y; 
     	           planePoint[3][0]=shape->BPoint[x][12].z; planePoint[3][1]=shape->BPoint[x][12].y; 
			 } 
			 else if ((x==8)&&(y==0)) 
			 { 
                   planePoint[0][0]=shape->BPoint[0][0].z;planePoint[0][1]=shape->BPoint[0][0].y; 
                   planePoint[1][0]=shape->BPoint[0][12].z;planePoint[1][1]=shape->BPoint[0][12].y; 
                   planePoint[2][0]=shape->BPoint[0][8].z;planePoint[2][1]=shape->BPoint[0][8].y; 
     	           planePoint[3][0]=shape->BPoint[0][4].z; planePoint[3][1]=shape->BPoint[0][4].y; 
			 } 
		          pointX=startPoint->z+t*Vector[2]; 
                  pointY=startPoint->y+t*Vector[1]; 
           	   	   for (k=0;k<=2;k++) 
				   { 
	                  a=planePoint[k+1][1]-planePoint[k][1]; 
	                  b=planePoint[k][0]-planePoint[k+1][0]; 
	              	c=planePoint[k+1][0]*planePoint[k][1]-planePoint[k][0]*planePoint[k+1][1]; 
	              	if ((a*pointX+b*pointY+c)<-LIMIT_ZERO) 
		           	return FALSE; 
				   } 
                       a=planePoint[0][1]-planePoint[3][1]; 
                    	 b=planePoint[3][0]-planePoint[0][0]; 
	                c=planePoint[0][0]*planePoint[3][1]-planePoint[3][0]*planePoint[0][1]; 
	                 if ((a*pointX+b*pointY+c)<-LIMIT_ZERO) 	return FALSE; 
				  break; 
		 } 
	 case FRONT: //向前视图投影 
		 { 
		      if((x!=8)&&(y!=15)){ 
		         planePoint[0][0]=shape->BPoint[x][y].x; planePoint[0][1]=shape->BPoint[x][y].y; 
	             planePoint[1][0]=shape->BPoint[x+1][y].x; planePoint[1][1]=shape->BPoint[x+1][y].y; 
	             planePoint[2][0]=shape->BPoint[x+1][y+1].x; planePoint[2][1]=shape->BPoint[x+1][y+1].y; 
                 planePoint[3][0]=shape->BPoint[x][y+1].x; planePoint[3][1]=shape->BPoint[x][y+1].y; 
			 } 
			  else if (y==15){ 
			     planePoint[0][0]=shape->BPoint[x][y].x; planePoint[0][1]=shape->BPoint[x][y].y; 
	             planePoint[1][0]=shape->BPoint[x+1][y].x; planePoint[1][1]=shape->BPoint[x+1][y].y; 
	             planePoint[2][0]=shape->BPoint[x+1][0].x; planePoint[2][1]=shape->BPoint[x+1][0].y; 
                 planePoint[3][0]=shape->BPoint[x][0].x; planePoint[3][1]=shape->BPoint[x][0].y; 
			  } 
			  else if ((x==8)&&(y==1)){ 
		           planePoint[0][0]=shape->BPoint[x][0].x; planePoint[0][1]=shape->BPoint[x][0].y; 
                   planePoint[1][0]=shape->BPoint[x][12].x; planePoint[1][1]=shape->BPoint[x][12].y; 
                   planePoint[2][0]=shape->BPoint[x][8].x; planePoint[2][1]=shape->BPoint[x][8].y; 
     	           planePoint[3][0]=shape->BPoint[x][4].x; planePoint[3][1]=shape->BPoint[x][4].y; 
			 } 
		     else  if ((x==8)&&(y==0)) 
			 { 
                   planePoint[0][0]=shape->BPoint[0][0].x;planePoint[0][1]=shape->BPoint[0][0].y; 
                   planePoint[1][0]=shape->BPoint[0][4].x;planePoint[1][1]=shape->BPoint[0][4].y; 
                   planePoint[2][0]=shape->BPoint[0][8].x;planePoint[2][1]=shape->BPoint[0][8].y; 
     	           planePoint[3][0]=shape->BPoint[0][12].x; planePoint[3][1]=shape->BPoint[0][12].y; 
			 } 
		  
                  pointX=startPoint->x+t*Vector[0]; 
                  pointY=startPoint->y+t*Vector[1]; 
           	       
				   for (k=0;k<=2;k++) 
				   { 
	                  a=planePoint[k+1][1]-planePoint[k][1]; 
	                  b=planePoint[k][0]-planePoint[k+1][0]; 
	              	  c=planePoint[k+1][0]*planePoint[k][1]-planePoint[k][0]*planePoint[k+1][1]; 
	              	if ((a*pointX+b*pointY+c)<-LIMIT_ZERO) 
		           	return FALSE; 
				   } 
                       a=planePoint[0][1]-planePoint[3][1]; 
                       b=planePoint[3][0]-planePoint[0][0]; 
	                   c=planePoint[0][0]*planePoint[3][1]-planePoint[3][0]*planePoint[0][1]; 
	                 if ((a*pointX+b*pointY+c)<-LIMIT_ZERO) 	return FALSE; 
			 break; 
		 } 
	 default: 
		 { 
		 } 
	 } 
	  	return TRUE; 
} 
 
void COperation::MoveEntity(CPoint point, CPoint prePoint, int view) 
{ 
   //矩阵移动 
	switch (view) 
   { 
   case TOP: // 在上视图移动 
	 { 
	     moveMatrix[3][0]=point.x-prePoint.x; 
		 moveMatrix[3][2]=point.y-prePoint.y; 
         CShape *shape; 
		 shape=(CShape *)g_pDoc->m_ShapeArray[selectNo]; 
		 MatrixProduct(shape,moveMatrix); 
     	 shape->center.x=shape->center.x+moveMatrix[3][0]; 
		 shape->center.y=shape->center.y+moveMatrix[3][1]; 
		 shape->center.z=shape->center.z+moveMatrix[3][2]; 
		 shape->tempCenter.x = shape->center.x; 
		 shape->tempCenter.y = shape->center.y; 
		 shape->tempCenter.z = shape->center.z; 
		 shape->SetBodyFace(); 
		 moveMatrix[3][0]=0; 
		 moveMatrix[3][2]=0; 
		  
		 break; 
	 } 
   case LEFT: 
	   { 
	     moveMatrix[3][2]=-(point.x-prePoint.x); 
		 moveMatrix[3][1]=point.y-prePoint.y; 
         CShape *shape; 
		 shape=(CShape *)g_pDoc->m_ShapeArray[selectNo]; 
		 MatrixProduct(shape,moveMatrix); 
         shape->center.x=shape->center.x+moveMatrix[3][0]; 
		 shape->center.y=shape->center.y+moveMatrix[3][1]; 
		 shape->center.z=shape->center.z+moveMatrix[3][2]; 
	     shape->SetBodyFace(); 
		 moveMatrix[3][2]=0; 
		 moveMatrix[3][1]=0; 
		  
		 break; 
	   } 
   case FRONT: 
	   { 
	     moveMatrix[3][0]=point.x-prePoint.x; 
		 moveMatrix[3][1]=point.y-prePoint.y; 
         CShape *shape; 
		 shape=(CShape *)g_pDoc->m_ShapeArray[selectNo]; 
		 MatrixProduct(shape,moveMatrix); 
         shape->center.x=shape->center.x+moveMatrix[3][0]; 
		 shape->center.y=shape->center.y+moveMatrix[3][1]; 
		 shape->center.z=shape->center.z+moveMatrix[3][2]; 
	     shape->SetBodyFace(); 
		 moveMatrix[3][0]=0; 
		 moveMatrix[3][1]=0; 
         break; 
	   } 
   default: 
	   { 
	   } 
   } 
} 
 
void COperation::MatrixProduct(CShape *shape, double matrix[][4]) 
{ 
    //与矩阵相乘 
	int x,y; 
	for (x=0;x<=8;x++) 
	{ 
	  for (y=0;y<=15;y++) 
	  { 
	     PointProductMatrix(&shape->BPoint[x][y],matrix); 
	  } 
	} 
} 
 
void COperation::PointProductMatrix(CGPoint *point,double matrix[][4]) 
{ 
    //点与矩阵相乘 
	double x; 
	double y; 
	double z; 
	double m; 
    x=point->x; 
	y=point->y; 
	z=point->z; 
	m=point->m; 
    point->x=x*matrix[0][0]+y*matrix[1][0]+z*matrix[2][0]+m*matrix[3][0]; 
	point->y=x*matrix[0][1]+y*matrix[1][1]+z*matrix[2][1]+m*matrix[3][1]; 
	point->z=x*matrix[0][2]+y*matrix[1][2]+z*matrix[2][2]+m*matrix[3][2]; 
	point->m=1; 
} 
 
void COperation::RotateEntity(CPoint point, CPoint prePoint, int view) 
{ 
    //旋转物体 
	int dx; 
	double dAngel; 
	dx=point.x-prePoint.x; 
	switch (view) 
	{ 
	case TOP: 
		 {  // 以y轴绕轴 
             dAngel=(PI/270)*dx; 
			 yRotateM[0][0]=cos(dAngel);yRotateM[0][2]=-sin(dAngel); 
			 yRotateM[2][0]=sin(dAngel);yRotateM[2][2]=cos(dAngel); 
			 CShape*shape; 
			 shape=(CShape *)g_pDoc->m_ShapeArray[selectNo]; 
		     int k,j; 
			 for (k=0;k<=8;k++) 
				 for (j=0;j<=15;j++) 
				 { 
		        	 shape->BPoint[k][j]=shape->BPoint[k][j]-shape->center; 
				 } 
			 MatrixProduct(shape,yRotateM); 
             for (k=0;k<=8;k++) 
				 for (j=0;j<=15;j++) 
				 { 
		        	 shape->BPoint[k][j]=shape->BPoint[k][j]+shape->center; 
				 } 
	         shape->SetBodyFace(); 
             yRotateM[0][0]=1;yRotateM[0][2]=0; 
			 yRotateM[2][0]=0;yRotateM[2][2]=1; 
			 break; 
		 } 
	case LEFT: 
		 { 
		     // 以x轴绕轴 
             dAngel=-(PI/270)*dx; 
			 yRotateM[1][1]=cos(dAngel);yRotateM[1][2]=sin(dAngel); 
			 yRotateM[2][1]=-sin(dAngel);yRotateM[2][2]=cos(dAngel); 
			 CShape*shape; 
			 shape=(CShape *)g_pDoc->m_ShapeArray[selectNo]; 
		     int k,j; 
			 for (k=0;k<=8;k++) 
				 for (j=0;j<=15;j++) 
				 { 
		        	 shape->BPoint[k][j]=shape->BPoint[k][j]-shape->center; 
				 } 
			 MatrixProduct(shape,yRotateM); 
             for (k=0;k<=8;k++) 
				 for (j=0;j<=15;j++) 
				 { 
		        	 shape->BPoint[k][j]=shape->BPoint[k][j]+shape->center; 
				 } 
	         shape->SetBodyFace(); 
             yRotateM[1][1]=1;yRotateM[1][2]=0; 
			 yRotateM[2][1]=0;yRotateM[2][2]=1; 
					  
			 break; 
		 } 
	case FRONT: 
		 { 
              // 以x轴绕轴 
             dAngel=(PI/270)*dx; 
			 yRotateM[0][0]=cos(dAngel);yRotateM[0][1]=sin(dAngel); 
			 yRotateM[1][0]=-sin(dAngel);yRotateM[1][1]=cos(dAngel); 
			 CShape*shape; 
			 shape=(CShape *)g_pDoc->m_ShapeArray[selectNo]; 
		     int k,j; 
			 for (k=0;k<=8;k++) 
				 for (j=0;j<=15;j++) 
				 { 
		        	 shape->BPoint[k][j]=shape->BPoint[k][j]-shape->center; 
				 } 
			 MatrixProduct(shape,yRotateM); 
             for (k=0;k<=8;k++) 
				 for (j=0;j<=15;j++) 
				 { 
		        	 shape->BPoint[k][j]=shape->BPoint[k][j]+shape->center; 
				 } 
	         shape->SetBodyFace(); 
             yRotateM[0][0]=1;yRotateM[0][1]=0; 
			 yRotateM[1][0]=0;yRotateM[1][1]=1; 
		  break; 
		 } 
	default: 
		{ 
		} 
	} 
 
} 
 
BOOL COperation::PointOnLight(CPoint point, int view) 
{ 
   //判断鼠标是否在灯上 
   int x,y,z; 
   int n; 
    
   CGLight *light; 
   switch (view) 
   { 
   case TOP: //上视图操作 
		   { 
		     x=point.x-220;  
			 z=point.y-160; 
             for (n=0;nm_LightArray[n];  
				 bool haha; 
				 haha=((x<=light->position.x+5)&&(x>=light->position.x-5) 
					 &&(z<=light->position.z+5)&&(z>=light->position.z-5)); 
				  
				 if ((x<=light->position.x+5)&&(x>=light->position.x-5) 
					 &&(z<=light->position.z+5)&&(z>=light->position.z-5)) 
				 { 
				     selectLight=n; 
					 return TRUE; 
				 } 
			 } 
    	     break; 
		   } 
	   case LEFT://左视图操作 
		   { 
             y=(point.y-160); 
			 z=-(point.x-395/2-1); 
             for (n=0;nm_LightArray[n];  
				 if ((y<=light->position.y+5)&&(y>=light->position.y-5) 
					 &&(z<=light->position.z+5)&&(z>=light->position.z-5)) 
				 { 
				     selectLight=n; 
					 return TRUE; 
				 } 
			 } 
 
		   break; 
		   } 
	   case FRONT://前视图操作 
		   { 
             x=point.x-440/2;  
	         y=point.y-314/2; 
             for (n=0;nm_LightArray[n];  
				 if ((y<=light->position.y+5)&&(y>=light->position.y-5) 
					 &&(x<=light->position.x+5)&&(x>=light->position.x-5)) 
				 { 
				     selectLight=n; 
					 return TRUE; 
				 } 
			 } 
     	   break; 
		   } 
       case PER: 
		   { 
             break; 
		   } 
	   default: 
		   { 
		   } 
   } 
return FALSE; 
 
} 
 
void COperation::MoveLight(CPoint point, CPoint prePoint, int view) 
{ 
    //移动光源 
	int x,y,z; 
	 
	switch (view) 
	{ 
	case TOP:  
		{ 
           x=point.x-prePoint.x; 
		   z=point.y-prePoint.y; 
           CGLight *light; 
		   light=(CGLight *)g_pDoc->m_LightArray[selectLight]; 
	       light->position.x=light->position.x+x; 
		   light->position.z=light->position.z+z; 
     	   break; 
		} 
	case LEFT: 
		{  
           y=point.y-prePoint.y; 
		   z=prePoint.x-point.x; 
           CGLight *light; 
		   light=(CGLight *)g_pDoc->m_LightArray[selectLight]; 
	       light->position.y=light->position.y+y; 
		   light->position.z=light->position.z+z; 
			break; 
		} 
	default: //Front 
		{ 
		     x=point.x-prePoint.x;  
	         y=point.y-prePoint.y; 
             CGLight *light; 
		     light=(CGLight *)g_pDoc->m_LightArray[selectLight]; 
	         light->position.y=light->position.y+y; 
		     light->position.x=light->position.x+x; 
		} 
	 
	} 
 
 
 
 
} 
 
void COperation::Phong(int x, int xx, int yy, CGPoint *startPoint, vector directVector, double tt, color *rayColor) 
{ 
    //Phong光照模型 
	 
	CShape *shape; 
	CAttach *attach; 
    int lIndex;// light index; 
	CGPoint interSec;//交点 
	vector lightD; //光向量 
	vector normal,reflect;//法向量,反射向量 
	vector v1,v2,v3; 
	double angelLN,angelRV;// 夹角 
	double diffuseR,diffuseG,diffuseB;//漫反射系数 
	double specR,specG,specB;//反射系数 
	double orignalTT;//保存直线参数 
	double distance;//与光源距离 
	double f; //衰减系数 
	 
 
	diffuseR=0;diffuseG=0;diffuseB=0; 
	specR=0;specG=0;specB=0; 
    angelLN=0;angelRV=0; 
	 
	CGLight *light; 
 
 
	shape=(CShape *)g_pDoc->m_ShapeArray[x]; 
    attach=(CAttach *)g_pDoc->m_TextureArray[x]; 
	 
	interSec.x=startPoint->x+tt*directVector[0]; 
	interSec.y=startPoint->y+tt*directVector[1]; 
	interSec.z=startPoint->z+tt*directVector[2]; 
    orignalTT=tt; 
     
	if (shape->IsKindOf(RUNTIME_CLASS(CBall))){ 
	    normal[0]=interSec.x-shape->center.x; 
		normal[1]=interSec.y-shape->center.y; 
		normal[2]=interSec.z-shape->center.z; 
	} 
	else if (shape->IsKindOf(RUNTIME_CLASS(CCone))) 
    { 
	  if ((xx!=LATI-1)&&(yy!=1)) 
	  { 
		v1[0]=shape->BPoint[0][0].x-interSec.x; 
		v1[1]=shape->BPoint[0][0].y-interSec.y; 
		v1[2]=shape->BPoint[0][0].z-interSec.z; 
		v1[3]=1; 
        v2[0]=shape->center.x-interSec.x; 
		v2[1]=shape->center.y-interSec.y; 
		v2[2]=shape->center.z-interSec.z; 
		v2[3]=1; 
		COperation::VectorXProduct(v1,v2,v3); 
    	COperation::VectorXProduct(v1,v3,normal); 
	  } 
	  else  
	  { 
	     normal[0]=shape->BFace[xx][yy].a; 
         normal[1]=shape->BFace[xx][yy].b; 
         normal[2]=shape->BFace[xx][yy].c; 
	   
	  } 
	} 
	else if (shape->IsKindOf(RUNTIME_CLASS(CColumn))) 
	{ 
	    if ((xx!=LATI-1)&&((yy!=1)||(yy!=0))) 
		{ 
		 
		v1[0]=(shape->BPoint[0][0].x+shape->BPoint[0][LATI-1].x)/2-interSec.x; 
		v1[1]=(shape->BPoint[0][0].y+shape->BPoint[0][LATI-1].y)/2-interSec.y; 
		v1[2]=(shape->BPoint[0][0].z+shape->BPoint[0][LATI-1].z)/2-interSec.z; 
		v1[3]=1; 
        v2[0]=shape->center.x-interSec.x; 
		v2[1]=shape->center.y-interSec.y; 
		v2[2]=shape->center.z-interSec.z; 
		v2[3]=1; 
	    COperation::VectorXProduct(v1,v2,v3); 
		v1[0]=shape->BPoint[0][0].x-shape->BPoint[4][0].x; 
		v1[1]=shape->BPoint[0][0].y-shape->BPoint[4][0].y; 
		v1[2]=shape->BPoint[0][0].z-shape->BPoint[4][0].z; 
		v1[3]=1; 
        COperation::VectorXProduct(v1,v3,normal); 
		} 
		else  
		{ 
		  normal[0]=shape->BFace[xx][yy].a; 
          normal[1]=shape->BFace[xx][yy].b; 
          normal[2]=shape->BFace[xx][yy].c; 
		} 
 
	 
	} 
	else 
	{ 
	   normal[0]=shape->BFace[xx][yy].a; 
       normal[1]=shape->BFace[xx][yy].b; 
       normal[2]=shape->BFace[xx][yy].c; 
	} 
    GetNormalVector(normal); 
 
	for (lIndex=0;lIndexm_LightArray[lIndex]; 
	  CaculateVector(lightD,&light->position,&interSec); 
    
      angelLN=dot(normal,lightD); 
      if ((angelLN>0)&&(IsNotShadowed(lightD,&light->position,x,&interSec))) 
	  { 
	     distance=light->position.Distance(&interSec); 
         f=CaculateF(distance); 
			  
		 diffuseR=f*angelLN*light->lc.r+diffuseR; 
		 diffuseG=f*angelLN*light->lc.g+diffuseG; 
		 diffuseB=f*angelLN*light->lc.b+diffuseB; 
	   
		 CaculateReflect(reflect,angelLN,normal,lightD); 
		 GetNormalVector(reflect); 
         angelRV=dot(reflect,directVector); 
		 angelRV=power(angelRV,shape->ns); 
	      
		 specR=specR+f*angelRV*light->lc.r; 
		 specG=specG+f*angelRV*light->lc.g; 
		 specB=specB+f*angelRV*light->lc.b; 
   
	  } 
	} 
   if (attach->texture==NULL) 
   { 
  	   	rayColor->r=(int)(rayColor->r+shape->Ka*shape->cl.r+shape->Kd*diffuseR+shape->Ks*specR); 
	    rayColor->g=(int)(rayColor->g+shape->Ka*shape->cl.g+shape->Kd*diffuseG+shape->Ks*specG); 
	    rayColor->b=(int)(rayColor->b+shape->Ka*shape->cl.b+shape->Kd*diffuseB+shape->Ks*specB); 
   } 
   else 
   { 
       //贴图处理 
	    COLORREF tempCl; 
		int r,g,b; 
		float u,v; 
	    u=CaculateU(interSec,x); 
		v=CaculateV(interSec,x); 
        int width=attach->texture->width; 
	    int height=attach->texture->height; 
	    tempCl=attach->texture->GetColor(CGPoint(u*width,v*height,0)); 
        r=GetRValue(tempCl); 
		g=GetGValue(tempCl); 
		b=GetBValue(tempCl); 
		rayColor->r=(int)(rayColor->r+shape->Ka*r+shape->Kd*diffuseR+shape->Ks*specR); 
	    rayColor->g=(int)(rayColor->g+shape->Ka*g+shape->Kd*diffuseG+shape->Ks*specG); 
	    rayColor->b=(int)(rayColor->b+shape->Ka*b+shape->Kd*diffuseB+shape->Ks*specB); 
   } 
	 
    if (rayColor->r>255) rayColor->r=255; 
    if (rayColor->g>255) rayColor->g=255; 
    if (rayColor->b>255) rayColor->b=255; 
	tt=orignalTT; 
} 
 
void COperation::GetNormalVector(vector normal) 
{  //向量单位化 
	double temp; 
    temp=sqrt(normal[0]*normal[0]+normal[1]*normal[1]+normal[2]*normal[2]); 
    normal[0]=normal[0]/temp; 
    normal[1]=normal[1]/temp; 
    normal[2]=normal[2]/temp; 
} 
 
double COperation::dot(vector vector1, vector vector2) 
{ 
    //点积 
	return vector1[0]*vector2[0]+vector1[1]*vector2[1]+vector1[2]*vector2[2]; 
} 
 
void COperation::CaculateReflect(vector reflect, double angelLN, vector normal, vector lightD) 
{ 
     //计算反射向量 
	 reflect[0]=2*angelLN*normal[0]-lightD[0]; 
	 reflect[1]=2*angelLN*normal[1]-lightD[1]; 
	 reflect[2]=2*angelLN*normal[2]-lightD[2]; 
} 
 
double COperation::power(double angelRV, int ns) 
{ 
     //光亮度处理 
	 int x; 
	 double temp; 
	 temp=1; 
	 for (x=1;x<=ns;x++) 
	 { 
	     temp=temp*angelRV; 
	 } 
	 return temp; 
} 
 
BOOL COperation::IsNotShadowed(vector light, CGPoint *lightPoint, int entityID,CGPoint *interSec) 
{ 
   //是否光线被物体遮挡,而在当前显示物体产生阴影 
   int k; 
   CShape *shape; 
   vector lightVector; 
   double disInter; 
   double disTemp; 
   CGPoint tempPoint; 
 
   if (shadow==FALSE) 
  { 
     return TRUE; 
  } 
  else 
  { 
     lightVector[0]=-light[0]; 
	 lightVector[1]=-light[1]; 
	 lightVector[2]=-light[2]; 
	 for (k=0;km_ShapeArray[k]; 
			 if (Ray_Hit_Entity(shape, lightPoint, lightVector,TOP)||Ray_Hit_Entity(shape, lightPoint, lightVector,LEFT)) 
			 { 
			     tempPoint.x=lightPoint->x+tt*lightVector[0]; 
				 tempPoint.y=lightPoint->y+tt*lightVector[1]; 
                 tempPoint.z=lightPoint->z+tt*lightVector[2]; 
				 disInter=(interSec->x-lightPoint->x)*(interSec->x-lightPoint->x) 
					 +(interSec->y-lightPoint->y)*(interSec->y-lightPoint->y) 
					 +(interSec->z-lightPoint->z)*(interSec->z-lightPoint->z); 
				 disTemp=(tempPoint.x-lightPoint->x)*(tempPoint.x-lightPoint->x) 
					 +(tempPoint.y-lightPoint->y)*(tempPoint.y-lightPoint->y) 
					 +(tempPoint.z-lightPoint->z)*(tempPoint.z-lightPoint->z); 
				 if (disTempm_ShapeArray[k]; 
	   ScanLineEntity(shape,y,&tempMin,&tempMax); 
	   if (tempMin<*MinX) *MinX=(int)tempMin; 
	   if (tempMax>*MaxX) *MaxX=(int)tempMax+1; 
   } 
} 
 
void COperation::ScanLineEntity(CShape *shape, int y, double *tempMin, double *tempMax) 
{ 
   // 计算扫描线与物体相交情况 
 
	int i,j; 
  
   	if(shape->IsKindOf(RUNTIME_CLASS(CColumn))) 
	{ 
	    for (i=0;i<=7;i++) 
		{ 
		   for (j=0;j<=14;j++) 
		   { 
		      CaculateInterSec(&shape->BPoint[i][j],&shape->BPoint[i][j+1],tempMin,tempMax,y); 
              CaculateInterSec(&shape->BPoint[i][j],&shape->BPoint[i+1][j],tempMin,tempMax,y); 
		   } 
		} 
        for (i=0;i<=7;i++) 
		{ 
		   CaculateInterSec(&shape->BPoint[i][15],&shape->BPoint[i][0],tempMin,tempMax,y); 
           CaculateInterSec(&shape->BPoint[i][15],&shape->BPoint[i+1][15],tempMin,tempMax,y); 
		} 
        for (i=0;i<=14;i++) 
		{ 
		   CaculateInterSec(&shape->BPoint[8][i],&shape->BPoint[8][i+1],tempMin,tempMax,y); 
		} 
           CaculateInterSec(&shape->BPoint[8][15],&shape->BPoint[8][0],tempMin,tempMax,y); 
	} 
	else if (shape->IsKindOf(RUNTIME_CLASS(CBall))) 
	{ 
	     
        for (i=0;i<=15;i++) 
		{ 
		    CaculateInterSec(&shape->BPoint[0][0],&shape->BPoint[1][i],tempMin,tempMax,y); 
		} 
           
		for (i=1;i<=7;i++) 
			for (j=0;j<=14;j++) 
		{ 
		    CaculateInterSec(&shape->BPoint[i][j],&shape->BPoint[i][j+1],tempMin,tempMax,y); 
            CaculateInterSec(&shape->BPoint[i][j],&shape->BPoint[i+1][j],tempMin,tempMax,y); 
		}  
        for (i=1;i<=7;i++) 
		{ 
		    CaculateInterSec(&shape->BPoint[i][15],&shape->BPoint[i][0],tempMin,tempMax,y); 
			CaculateInterSec(&shape->BPoint[i][15],&shape->BPoint[i+1][15],tempMin,tempMax,y); 
		} 
	} 
	else if (shape->IsKindOf(RUNTIME_CLASS(CCuboid))) 
	{ 
	   for (j=0;j<12;j=j+4) 
	   { 
     	  CaculateInterSec(&shape->BPoint[0][j],&shape->BPoint[0][j+4],tempMin,tempMax,y); 
	      CaculateInterSec(&shape->BPoint[8][j],&shape->BPoint[8][j+4],tempMin,tempMax,y); 
	   } 
          CaculateInterSec(&shape->BPoint[0][12],&shape->BPoint[0][0],tempMin,tempMax,y); 
          CaculateInterSec(&shape->BPoint[8][12],&shape->BPoint[8][0],tempMin,tempMax,y); 
	   
	   for (j=0;j<=12;j=j+4) 
	   { 
	      CaculateInterSec(&shape->BPoint[0][j],&shape->BPoint[8][j],tempMin,tempMax,y); 
	   } 
	} 
	else if (shape->IsKindOf(RUNTIME_CLASS(CCone))) 
	{ 
	 
	   for (i=0;i<=15;i++) 
		{ 
		    CaculateInterSec(&shape->BPoint[0][0],&shape->BPoint[1][i],tempMin,tempMax,y); 
		} 
           
		for (i=1;i<=7;i++) 
			for (j=0;j<=14;j++) 
		{ 
		    CaculateInterSec(&shape->BPoint[i][j],&shape->BPoint[i][j+1],tempMin,tempMax,y); 
            CaculateInterSec(&shape->BPoint[i][j],&shape->BPoint[i+1][j],tempMin,tempMax,y); 
		}  
        for (i=1;i<=7;i++) 
		{ 
		    CaculateInterSec(&shape->BPoint[i][15],&shape->BPoint[i][0],tempMin,tempMax,y); 
			CaculateInterSec(&shape->BPoint[i][15],&shape->BPoint[i+1][15],tempMin,tempMax,y); 
		} 
          
        for (i=0;i<=14;i++) 
		{ 
		   CaculateInterSec(&shape->BPoint[8][i],&shape->BPoint[8][i+1],tempMin,tempMax,y); 
		} 
           CaculateInterSec(&shape->BPoint[8][15],&shape->BPoint[8][0],tempMin,tempMax,y); 
	} 
		  
} 
 
void COperation::CaculateInterSec(CGPoint *point1, CGPoint *point2, double *tempMin, double *tempMax,int y) 
{ 
   //计算交点 
   double interSecX; 
   if (point1->z!=point2->z)	  /* 该边不为水平边*/ 
		  { 
		     if (((point1->z>=y)&&(y>=point2->z)) 
				 ||((point1->z<=y)&&(y<=point2->z))) /* 扫描线与边相交 */ 
			 { 
			    interSecX=point1->x+(point2->x-point1->x)*(y-point1->z)/(point2->z-point1->z); 
                if (interSecX<*tempMin) *tempMin=interSecX; 
				if (interSecX>*tempMax) *tempMax=interSecX; 
			 } 
		  } 
} 
 
void COperation::ZoomEntity(CPoint point, CPoint prePoint, int view) 
{ 
	//缩放物体 
	CShape*shape; 
	shape=(CShape *)g_pDoc->m_ShapeArray[selectNo]; 
	for(int i=0; iBPoint[i][j] = shape->BPoint[i][j] - shape->tempCenter; 
		} 
	double delta = point.x - prePoint.x; 
	double k = 1; 
	double d = 0; 
	CGPoint *orgin = new CGPoint(0,0,0); 
	d =shape->BPoint[0][0].Distance(orgin); 
	k = (d+delta)/d; 
	for(i=0; iBPoint[i][j].x *= k; 
			shape->BPoint[i][j].y *= k; 
			shape->BPoint[i][j].z *= k; 
		} 
	for(i=0; iBPoint[i][j] = shape->BPoint[i][j] + shape->tempCenter; 
		} 
	shape->SetBodyFace(); 
} 
 
void COperation::Specify(CGPoint* before, CGPoint* after) 
{ 
  	double m = sqrt(before->x*before->x + before->y*before->y + before->z*before->z); 
	after->x = before->x/m; 
	after->y = before->y/m; 
	after->z = before->z/m; 
} 
 
void COperation::PerspectiveCal(CGPoint *orient, SPoint *view) 
{ 
	double sin_theta; 
	double cos_theta; 
	double sin_phi; 
	double cos_phi; 
	double xe; 
	double ye; 
	double ze; 
	 
	if(Oe->x==0&&Oe->y==0) 
	{ 
		sin_theta = 0; 
		cos_theta = 1; 
	} 
	else 
	{ 
		sin_theta = Oe->y / sqrt(Oe->x*Oe->x + Oe->y*Oe->y); 
		cos_theta = Oe->x / sqrt(Oe->x*Oe->x + Oe->y*Oe->y); 
	} 
	if(Oe->x==0&&Oe->y==0&&Oe->z==0) 
	{ 
		sin_phi = 0; 
		cos_phi = 1; 
	} 
	else 
	{ 
		cos_phi = Oe->z / sqrt(Oe->x*Oe->x + Oe->y*Oe->y + Oe->z*Oe->z); 
		sin_phi = sqrt(Oe->x*Oe->x + Oe->y*Oe->y) / sqrt(Oe->x*Oe->x + Oe->y*Oe->y + Oe->z*Oe->z); 
	} 
	xe = -orient->x*sin_theta + orient->y*cos_theta; 
	ye = -orient->x*cos_theta*cos_phi - orient->y*sin_theta*cos_phi + orient->z*sin_phi; 
	ze = -orient->x*cos_theta*sin_phi - orient->y*sin_theta*sin_phi - orient->z*cos_phi + sqrt(Oe->x*Oe->x + Oe->y*Oe->y + Oe->z*Oe->z); 
	 
	/* 
	double u = sqrt(Oe->x*Oe->x + Oe->y*Oe->y + Oe->z*Oe->z); 
	double v = sqrt(Oe->x*Oe->x + Oe->y*Oe->y); 
	xe = -orient->x*Oe->y/v + orient->y*Oe->x/v; 
    ye = -orient->x*Oe->x*Oe->z/u/v - orient->y*Oe->y*Oe->z/u/v + orient->z*v/u ; 
	ze = -orient->x*Oe->x/u - orient->y*Oe->y/u - orient->z*Oe->z/u + u; 
	*/ 
	 
	view->x = d*xe/ze; 
	view->y = d*ye/ze; 
} 
 
void COperation::PerViewPoints(CShape *shape) 
{ 
	for(int i=0;iBPoint[i][j],&shape->BPer[i][j]); 
} 
 
void COperation::VectorXProduct(vector v1, vector v2, vector v3) 
{ 
	v3[0]=v1[1]*v2[2]-v1[2]*v2[1]; 
	v3[1]=v1[2]*v2[0]-v1[0]*v2[2]; 
	v3[2]=v1[0]*v2[1]-v1[1]*v2[0];  
	v3[3]=1; 
} 
 
 
double COperation::CaculateF(double d) 
{ 
	//计算衰减系数 
	double temp; 
 if (attenuation) 
 { 
	temp=1/(0.25+0.0025*d+0.00005*d*d); 
	if (temp<1) return temp; 
	else return 1; 
 } 
 else 
 { 
    return 1; 
 } 
} 
 
void COperation::ExtrudeEntity(CPoint point, CPoint prePoint, int view) 
{ 
	CShape*shape; 
	shape=(CShape *)g_pDoc->m_ShapeArray[selectNo]; 
	for(int i=0; iBPoint[i][j] = shape->BPoint[i][j] - shape->tempCenter; 
		} 
	double deltax = point.x - prePoint.x; 
	double kx = 1; 
	double deltay = point.y - prePoint.y; 
	double ky = 1; 
	double d = 0; 
	CGPoint *orgin = new CGPoint(0,0,0); 
	d =shape->BPoint[0][0].Distance(orgin); 
	kx = (d+deltax)/d; 
	ky = (d+deltay)/d; 
	switch(view) 
	{ 
	case TOP: 
		{ 
			for(i=0; iBPoint[i][j].x *= kx; 
					//shape->BPoint[i][j].y *= k; 
					shape->BPoint[i][j].z *= ky; 
				} 
				break; 
		} 
	case LEFT: 
		{ 
			for(i=0; iBPoint[i][j].x *= k; 
					shape->BPoint[i][j].y *= ky; 
					shape->BPoint[i][j].z *= kx; 
				} 
				break; 
		} 
	case FRONT: 
		{ 
			for(i=0; iBPoint[i][j].x *= kx; 
					shape->BPoint[i][j].y *= ky; 
					//shape->BPoint[i][j].z *= k; 
				} 
				break; 
		} 
	} 
	for(i=0; iBPoint[i][j] = shape->BPoint[i][j] + shape->tempCenter; 
		} 
} 
 
COLORREF COperation::GammaCorrect(int R, int G, int B) 
{ 
  	double k=22.92;   
	//k依赖于具体CRT,这里我们取22。92 
	double dR=pow(R/k,2.3); 
	double dG=pow(G/k,2.3); 
	double dB=pow(B/k,2.3); 
    return (RGB(int(pow(dR,1/gammaR)*k), 
		        int(pow(dG,1/gammaG)*k), 
				int(pow(dB,1/gammaB)*k) 
				) 
				); 
} 
 
void COperation::LatheEntity(CShape *shape, CPoint point, int view, int axis) 
{ 
	double d = sqrt(point.x*point.x + point.y*point.y); 
	CColumn *pColumn=(CColumn*)shape; 
	switch(axis) 
	{ 
	case LATHEX: 
		{ 
			switch(view) 
			{ 
			case TOP: 
				{ 
					for(int i = opPointIndex; iBPoint[i][j].x = point.x; 
							pColumn->BPoint[i][j].y = fabs(point.y)*sin(2*PI/LONG * j); 
							pColumn->BPoint[i][j].z = fabs(point.y)*cos(2*PI/LONG * j); 
						} 
					break; 
				} 
			case LEFT: 
				{ 
					for(int i = opPointIndex; iBPoint[i][j].x = 0; 
							pColumn->BPoint[i][j].y = d*sin(2*PI/LONG * j); 
							pColumn->BPoint[i][j].z = d*cos(2*PI/LONG * j); 
						} 
					break; 
				} 
			case FRONT: 
				{ 
					for(int i = opPointIndex; iBPoint[i][j].x = point.x; 
							pColumn->BPoint[i][j].y = fabs(point.y)*sin(2*PI/LONG * j); 
							pColumn->BPoint[i][j].z = fabs(point.y)*cos(2*PI/LONG * j); 
						} 
					break; 
				} 
			}//view 
			break; 
		}//X 
	case LATHEY: 
		{ 
			switch(view) 
			{ 
			case TOP: 
				{ 
					for(int i = opPointIndex; iBPoint[i][j].x = d*sin(2*PI/LONG * j); 
							pColumn->BPoint[i][j].y = 0; 
							pColumn->BPoint[i][j].z = d*cos(2*PI/LONG * j); 
						} 
					break; 
				} 
			case LEFT: 
				{ 
					for(int i = opPointIndex; iBPoint[i][j].x = fabs(point.x)*sin(2*PI/LONG * j); 
							pColumn->BPoint[i][j].y = point.y; 
							pColumn->BPoint[i][j].z = fabs(point.x)*cos(2*PI/LONG * j); 
						} 
					break; 
				} 
			case FRONT: 
				{ 
					for(int i = opPointIndex; iBPoint[i][j].x = fabs(point.x)*sin(2*PI/LONG * j); 
							pColumn->BPoint[i][j].y = point.y; 
							pColumn->BPoint[i][j].z = fabs(point.x)*cos(2*PI/LONG * j); 
						} 
					break; 
				} 
			}//view 
			break; 
		}//Y 
	case LATHEZ: 
		{ 
			switch(view) 
			{ 
			case TOP: 
				{ 
					for(int i = opPointIndex; iBPoint[i][j].x = fabs(point.x)*sin(2*PI/LONG * j); 
							pColumn->BPoint[i][j].y = fabs(point.x)*cos(2*PI/LONG * j); 
							pColumn->BPoint[i][j].z = point.y; 
						} 
					break; 
				} 
			case LEFT: 
				{ 
					for(int i = opPointIndex; iBPoint[i][j].x = fabs(point.y)*sin(2*PI/LONG * j); 
							pColumn->BPoint[i][j].y = fabs(point.y)*cos(2*PI/LONG * j); 
							pColumn->BPoint[i][j].z = -point.x; 
						} 
					break; 
				} 
			case FRONT: 
				{ 
					for(int i = opPointIndex; iBPoint[i][j].x = d*sin(2*PI/LONG * j); 
							pColumn->BPoint[i][j].y = d*cos(2*PI/LONG * j); 
							pColumn->BPoint[i][j].z = 0; 
						} 
					break; 
				} 
			}//view 
			break; 
		}//Z 
	}//axis 
	pColumn->radius = (pColumn->BPoint[0][0].x + pColumn->BPoint[0][LONG/2].x)/2; 
	pColumn->center.x = ( (pColumn->BPoint[0][0].x + pColumn->BPoint[0][LONG/2].x)/2 
		+ (pColumn->BPoint[LATI-1][0].x + pColumn->BPoint[LATI-1][LONG/2].x)/2 )/2; 
	pColumn->center.y = ( (pColumn->BPoint[0][0].y + pColumn->BPoint[0][LONG/2].y)/2 
		+ (pColumn->BPoint[LATI-1][0].y + pColumn->BPoint[LATI-1][LONG/2].y)/2 )/2; 
	pColumn->center.z = ( (pColumn->BPoint[0][0].z + pColumn->BPoint[0][LONG/2].z)/2 
		+ (pColumn->BPoint[LATI-1][0].z + pColumn->BPoint[LATI-1][LONG/2].z)/2 )/2; 
	pColumn->tempCenter.x = pColumn->center.x; 
	pColumn->tempCenter.y = pColumn->center.y; 
	pColumn->tempCenter.z = pColumn->center.z; 
} 
 
void COperation::LoftSampleEntity(CShape *shape, CPoint point, int view) 
{ 
	switch(view) 
	{ 
	case TOP: 
		{ 
			for(int i = opPointIndex; iBPoint[j][i].x = point.x; 
					shape->BPoint[j][i].y = 0; 
					shape->BPoint[j][i].z = point.y; 
				} 
			break; 
		} 
	case FRONT: 
		{ 
			for(int i = opPointIndex; iBPoint[j][i].x = point.x; 
					shape->BPoint[j][i].y = point.y; 
					shape->BPoint[j][i].z = 0; 
				} 
			break; 
		} 
	case LEFT: 
		{ 
			for(int i = opPointIndex; iBPoint[j][i].x = 0; 
					shape->BPoint[j][i].y = point.y; 
					shape->BPoint[j][i].z = -point.x; 
				} 
			break; 
		} 
	} 
} 
 
void COperation::LoftTrackEntity(CShape *Shape, CPoint point, CPoint prePoint, int view) 
{ 
	CColumn* shape = (CColumn*)Shape; 
	if(&prePoint == NULL) 
	{ 
		prePoint.x = 0; 
		prePoint.y = 0; 
	} 
	switch(view) 
	{ 
	case TOP: 
		{ 
			for(int i = opPointIndex; iBPoint[i][j].x += point.x - prePoint.x; 
					shape->BPoint[i][j].y += 0; 
					shape->BPoint[i][j].z += point.y - prePoint.y; 
				} 
			break; 
		} 
	case FRONT: 
		{ 
			for(int i = opPointIndex; iBPoint[i][j].x += point.x - prePoint.x; 
					shape->BPoint[i][j].y += point.y - prePoint.y; 
					shape->BPoint[i][j].z += 0; 
				} 
			break; 
		} 
	case LEFT: 
		{ 
			for(int i = opPointIndex; iBPoint[i][j].x += 0; 
					shape->BPoint[i][j].y += point.y - prePoint.y; 
					shape->BPoint[i][j].z += -(point.x - prePoint.x); 
				} 
			break; 
		} 
	} 
	shape->radius = (shape->BPoint[0][0].x + shape->BPoint[0][LONG/2].x)/2; 
	shape->center.x = ( (shape->BPoint[0][0].x + shape->BPoint[0][LONG/2].x)/2 
		+ (shape->BPoint[LATI-1][0].x + shape->BPoint[LATI-1][LONG/2].x)/2 )/2; 
	shape->center.y = ( (shape->BPoint[0][0].y + shape->BPoint[0][LONG/2].y)/2 
		+ (shape->BPoint[LATI-1][0].y + shape->BPoint[LATI-1][LONG/2].y)/2 )/2; 
	shape->center.z = ( (shape->BPoint[0][0].z + shape->BPoint[0][LONG/2].z)/2 
		+ (shape->BPoint[LATI-1][0].z + shape->BPoint[LATI-1][LONG/2].z)/2 )/2; 
	shape->tempCenter.x = shape->center.x; 
	shape->tempCenter.y = shape->center.y; 
	shape->tempCenter.z = shape->center.z; 
} 
 
float COperation::CaculateV(CGPoint point, int x) 
{ 
  //贴图纹理中V的计算 
   CShape *shape; 
   shape=(CShape *)g_pDoc->m_ShapeArray[x]; 
   vector v1;  
   v1[0]=(shape->BPoint[0][0].x+shape->BPoint[8][0].x)/2-shape->center.x; 
   v1[1]=(shape->BPoint[0][0].y+shape->BPoint[8][0].y)/2-shape->center.y; 
   v1[2]=(shape->BPoint[0][0].z+shape->BPoint[8][0].z)/2-shape->center.z; 
   GetNormalVector(v1); 
 
   vector v2; 
   v2[0]=point.x-shape->center.x; 
   v2[1]=point.y-shape->center.y; 
   v2[2]=point.z-shape->center.z; 
   GetNormalVector(v2); 
   double cosV=dot(v1,v2); 
   return (float)acos(cosV)/PI; 
} 
 
float COperation::CaculateU(CGPoint point, int x) 
{   
   //贴图纹理中U的计算 
   CShape *shape; 
   shape=(CShape *)g_pDoc->m_ShapeArray[x]; 
   vector v1;  
   v1[0]=(shape->BPoint[0][0].x+shape->BPoint[0][8].x)/2-shape->center.x; 
   v1[1]=(shape->BPoint[0][0].y+shape->BPoint[0][8].y)/2-shape->center.y; 
   v1[2]=(shape->BPoint[0][0].z+shape->BPoint[0][8].z)/2-shape->center.z; 
   GetNormalVector(v1); 
 
   vector v2; 
   v2[0]=point.x-shape->center.x; 
   v2[1]=point.y-shape->center.y; 
   v2[2]=point.z-shape->center.z; 
   GetNormalVector(v2); 
   double cosU=dot(v1,v2); 
   return (float)acos(cosU)/PI; 
}