查看: 2301|回复: 4
收起左侧

[其他] 帮忙给我写了一半C++代码看看有什么毛病

[复制链接]
zhangjianqwe
发表于 2011-8-7 21:01:17 | 显示全部楼层 |阅读模式
本帖最后由 zhangjianqwe 于 2011-8-9 22:23 编辑
  1. //#include <fstream>

  2. extern ALog g_Log;

  3. namespace CHBasedCD
  4. {
  5.         
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////

  9. C2DObstruct::C2DObstruct()
  10. {

  11. }

  12. C2DObstruct::~C2DObstruct()
  13. {

  14. }

  15. void C2DObstruct::Init(const vector<A3DVECTOR3>& Vertices, float fHeight)
  16. {
  17.         if(Vertices.size()<=3) return;                //至少要有3个顶点
  18.         int vNum=Vertices.size();

  19.         //添加顶点
  20.         for(int i=0;i<vNum;i++)
  21.         {
  22.                 A3DVECTOR3 v(Vertices[i]);
  23.                 v.y=0.0f;
  24.                 AddVertex(v);
  25.                 v.y=fHeight;
  26.                 AddVertex(v);
  27.         }

  28.         //添加底面
  29.         CFace face;
  30.         face.SetNormal(A3DVECTOR3(0.0f,-1.0f,0.0f));                //法向为-Y
  31.         face.SetD(0.0f);
  32.         for(i=0;i<vNum;i++)
  33.         {
  34.                 int vid=2*i;
  35.                 CHalfSpace hs;
  36.                 if(i!=vNum-1)        
  37.                         hs.Set(m_arrVertices[vid],m_arrVertices[vid+2],m_arrVertices[vid+2]+face.GetNormal());
  38.                 else
  39.                         hs.Set(m_arrVertices[vid],m_arrVertices[0],m_arrVertices[0]+face.GetNormal());
  40.                 face.AddElement(vid,hs);
  41.         }
  42.         AddFace(face);
  43.         
  44.         //添加顶面
  45.         face.Reset();
  46.         face.SetNormal(A3DVECTOR3(0.0f,1.0f,0.0f));                //法向为+Y
  47.         face.SetD(fHeight);
  48.         for(i=vNum-1;i>=0;i--)
  49.         {
  50.                 int vid=2*i+1;
  51.                 CHalfSpace hs;
  52.                 if (i!=0)
  53.                         hs.Set(m_arrVertices[vid],m_arrVertices[vid-2],m_arrVertices[vid-2]+face.GetNormal());
  54.                 else
  55.                         hs.Set(m_arrVertices[vid],m_arrVertices[vNum-1],m_arrVertices[vNum-1]+face.GetNormal());
  56.                 face.AddElement(vid,hs);
  57.         }
  58.         AddFace(face);

  59.         //添加各个侧面
  60.         for(i=0;i<vNum;i++)
  61.         {
  62.                 //每个侧面均为一个矩形!
  63.                 int vid1,vid2,vid3,vid4;
  64.                 if(i!=vNum-1)
  65.                 {
  66.                         vid1=2*i;
  67.                         vid2=2*i+1;
  68.                         vid3=2*i+3;
  69.                         vid4=2*i+2;
  70.                 }
  71.                 else
  72.                 {
  73.                         vid1=2*i;
  74.                         vid2=2*i+1;
  75.                         vid3=1;
  76.                         vid4=0;
  77.                 }
  78.                
  79.                 face.Reset();
  80.                 face.Set(m_arrVertices[vid1],m_arrVertices[vid2],m_arrVertices[vid3]);
  81.                 CHalfSpace hs;

  82.                 hs.Set(m_arrVertices[vid1],m_arrVertices[vid2],m_arrVertices[vid2]+face.GetNormal());
  83.                 face.AddElement(vid1,hs);

  84.                 hs.Set(m_arrVertices[vid2],m_arrVertices[vid3],m_arrVertices[vid3]+face.GetNormal());
  85.                 face.AddElement(vid2,hs);

  86.                 hs.Set(m_arrVertices[vid3],m_arrVertices[vid4],m_arrVertices[vid4]+face.GetNormal());
  87.                 face.AddElement(vid3,hs);

  88.                 hs.Set(m_arrVertices[vid4],m_arrVertices[vid1],m_arrVertices[vid1]+face.GetNormal());
  89.                 face.AddElement(vid4,hs);

  90.                 AddFace(face);
  91.         }        

  92.         //依次添加膨胀向量
  93.         
  94.         //构造2*2线性方程组
  95.         CMatrix        mtxCoef(2,2);                // 系数矩阵
  96.         CMatrix mtxConst(2,1);                // 常数矩阵
  97.         CMatrix        mtxResult(2,1);                // 结果        
  98.         
  99.         //初始化常数矩阵
  100.         mtxConst.SetElement(0,0,1.0);
  101.         mtxConst.SetElement(1,0,1.0);

  102.         CFace *pf1,*pf2;
  103.         for(i=0;i<vNum;i++)
  104.         {
  105.                 pf1=GetFaceByVID(2*i,CLOCKWISE);
  106.                 pf2=GetFaceByVID(2*i,ANTICLOCKWISE);
  107.                 if(pf1->GetNormal()==pf2->GetNormal())
  108.                 {
  109.                         //如果两面片的法向相同,则膨胀向量即为该法向量
  110.                         m_arrInflateVecs.Add(pf1->GetNormal());
  111.                         continue;
  112.                 }
  113.                 //其他情况,求解方程组以计算膨胀向量
  114.                 mtxCoef.SetElement(0,0,pf1->GetNormal().x);
  115.                 mtxCoef.SetElement(0,1,pf1->GetNormal().z);

  116.                 mtxCoef.SetElement(1,0,pf2->GetNormal().x);
  117.                 mtxCoef.SetElement(1,1,pf2->GetNormal().z);

  118.                 CLEquations le(mtxCoef,mtxConst);
  119.                 if(!le.GetRootsetGauss(mtxResult))
  120.                         assert(1);                                //确保求解成功
  121.                 A3DVECTOR3 v(mtxResult.GetElement(0,0),0.0f,mtxResult.GetElement(1,0));
  122.                 m_arrInflateVecs.Add(v);

  123.         }

  124. }

  125. void C2DObstruct::GetCRInfo(CFace* pCDFace,const A3DVECTOR3& Dir,int& RotateDir,int& RelayVID,int CurRotateDir)
  126. {
  127.         if(!pCDFace) return;
  128.         
  129.         if(CurRotateDir>0)
  130.         {
  131.                 //当前已处于绕行状态
  132.                 RotateDir=CurRotateDir;
  133.                 RelayVID=(RotateDir==ANTICLOCKWISE )?pCDFace->GetVID(3):pCDFace->GetVID(0);
  134.                 return;
  135.         }
  136.         ///////////////////////////////////////////////
  137.         //                        v1 ----> v2
  138.         //                   /|\                 |
  139.         //                     |             \|/
  140.         //                        v0 <---- v3
  141.         ///////////////////////////////////////////////
  142.         
  143.         //根据运动方向和碰撞面的切线决定绕向
  144.         A3DVECTOR3 vAnticlockwise=GetVertex(pCDFace->GetVID(3))-GetVertex(pCDFace->GetVID(0));
  145.         if(DotProduct(Dir,vAnticlockwise)>0.0f)
  146.         {
  147.                 RotateDir=ANTICLOCKWISE;
  148.                 RelayVID=pCDFace->GetVID(3);
  149.         }
  150.         else
  151.         {
  152.                 RotateDir=CLOCKWISE ;
  153.                 RelayVID=pCDFace->GetVID(0);
  154.         }               
  155. }

  156. bool C2DObstruct::IsVertexConcave(int vid)
  157. {
  158.         //注意:这里的vid应该是在棱拄中的id,而不是二维点集的id,因此必须
  159.         //对vid作一个>>1+2的操作!

  160.         // 顺时针方向的后继点
  161.         int nextVID=GetNextVID(vid,CLOCKWISE);
  162.         
  163.         // 如果该点在vid对应的面片外侧,则说明该点是一个凹点
  164.         int fid=(vid>>1)+2;
  165.         return m_arrFaces[fid]->Outside(m_arrVertices[nextVID]);
  166. }

  167. bool C2DObstruct::LoadFromStream(FILE* InFile)
  168. {
  169.         if(!CConvexHullData::LoadFromStream(InFile))
  170.                 return false;
  171.         
  172.         if(!InFile) return false;
  173.         
  174.         fscanf(InFile,"\n");
  175.         fscanf(InFile,"Inflate Vectors:\n");
  176.         
  177.         int vecNum=GetVertexNum()>>1;
  178.         
  179.         for(int i=0;i<vecNum;i++)
  180.         {
  181.                 A3DVECTOR3 v;
  182.                 fscanf(InFile,"%f %f %f\n",&v.x,&v.y,&v.z);
  183.                 m_arrInflateVecs.Add(v);
  184.         }
  185.         
  186.         //消除一个换行
  187.         fscanf(InFile,"\n");
  188.         
  189.         return true;
  190. }

  191. bool C2DObstruct::SaveToStream(FILE* OutFile) const
  192. {
  193.         if(!CConvexHullData::SaveToStream(OutFile))
  194.                 return false;
  195.         
  196.         if(!OutFile) return false;

  197.         //保存膨胀向量
  198.         fprintf(OutFile,"\n");                                                        //换行
  199.         fprintf(OutFile,"Inflate Vectors:\n");          //标志

  200.         for(int i=0;i<m_arrInflateVecs.GetSize();i++)
  201.                 fprintf(OutFile,"  %f  %f  %f\n",m_arrInflateVecs[i].x,m_arrInflateVecs[i].y,m_arrInflateVecs[i].z);
  202.         
  203.         fprintf(OutFile,"\n");                                                        //换行

  204.         return true;
  205. }

  206. /************************************************************************
  207. // Removed
  208. // 避免引擎中出现std::fstream的库,由于其所需要VC的支持!

  209. bool C2DObstruct::LoadFromStream(std::ifstream& InFile)
  210. {
  211.         if(!CConvexHullData::LoadFromStream(InFile))
  212.                 return false;
  213.         
  214.         if(!InFile.is_open()) return false;

  215.         char buf[MAX_LEN];
  216.         //换行及标志
  217.         InFile.getline(buf,MAX_LEN);
  218.         InFile.getline(buf,MAX_LEN);

  219.         int vecNum=GetVertexNum()>>1;
  220.         
  221.         for(int i=0;i<vecNum;i++)
  222.         {
  223.                 InFile.getline(buf,MAX_LEN);                //读出一行字符串
  224.                 A3DVECTOR3 v;
  225.                 sscanf(buf,"%f %f %f",&v.x,&v.y,&v.z);
  226.                 m_arrInflateVecs.Add(v);
  227.         }

  228.         InFile.getline(buf,MAX_LEN);                //消除一个换行

  229.         return true;
  230. }

  231. bool C2DObstruct::SaveToStream(std::ofstream& OutFile) const
  232. {
  233.         using namespace std;

  234.         if(!CConvexHullData::SaveToStream(OutFile))
  235.                 return false;

  236.         if(!OutFile.is_open())
  237.                 return false;
  238.         
  239.         //换行及标志
  240.         OutFile<<"\n";
  241.         OutFile<<"Inflate Vectors:\n";

  242.         for(int i=0;i<m_arrInflateVecs.GetSize();i++)
  243.                 OutFile<<"  "<<m_arrInflateVecs[i].x<<"  "<<m_arrInflateVecs[i].y<<"  "<<m_arrInflateVecs[i].z<<endl;

  244.         OutFile<<"\n";                                                //换行

  245.         return true;
  246. }

  247. /************************************************************************/

  248. int C2DObstruct::ComputeBufSize() const
  249. {
  250.         int BufSize=CConvexHullData::ComputeBufSize();
  251.         BufSize+=3*sizeof(float)*m_arrInflateVecs.GetSize();
  252.         return BufSize;
  253. }

  254. bool C2DObstruct::WriteToBuf(char* buf) const
  255. {
  256.         if(!CConvexHullData::WriteToBuf(buf))
  257.                 return false;

  258.         //再写入膨胀向量
  259.         const int FloatSize=sizeof(float);
  260.         int cur=CConvexHullData::ComputeBufSize();
  261.         float fWrite;

  262.         for(int i=0;i<m_arrInflateVecs.GetSize();i++)
  263.         {
  264.                 fWrite=m_arrInflateVecs[i].x;
  265.                 memcpy(buf+cur,&fWrite,FloatSize);
  266.                 cur+=FloatSize;

  267.                 fWrite=m_arrInflateVecs[i].y;
  268.                 memcpy(buf+cur,&fWrite,FloatSize);
  269.                 cur+=FloatSize;

  270.                 fWrite=m_arrInflateVecs[i].z;
  271.                 memcpy(buf+cur,&fWrite,FloatSize);
  272.                 cur+=FloatSize;               
  273.         }
  274.         
  275.         return true;
  276. }

  277. bool C2DObstruct::ReadFromBuf(char* buf)
  278. {
  279.         if(!CConvexHullData::ReadFromBuf(buf))
  280.                 return false;
  281.         
  282.         //读出膨胀向量
  283.         int vecNum=GetVertexNum()>>1;
  284.         
  285.         const int FloatSize=sizeof(float);        
  286.         int cur=CConvexHullData::ComputeBufSize();
  287.         float fRead;
  288.         
  289.         for(int i=0;i<vecNum;i++)
  290.         {
  291.                 A3DVECTOR3 v;
  292.                 memcpy(&fRead,buf+cur,FloatSize);
  293.                 cur+=FloatSize;
  294.                 v.x=fRead;

  295.                 memcpy(&fRead,buf+cur,FloatSize);
  296.                 cur+=FloatSize;
  297.                 v.y=fRead;

  298.                 memcpy(&fRead,buf+cur,FloatSize);
  299.                 cur+=FloatSize;
  300.                 v.z=fRead;

  301.                 m_arrInflateVecs.Add(v);
  302.         }        
  303.         
  304.         return true;
  305. }

  306. void C2DObstruct::Transform(const A3DMATRIX4& mtxTrans)
  307. {
  308.         //调用父类方法
  309.         CConvexHullData::Transform(mtxTrans);

  310.         //对膨胀向量进行变换
  311.         //由于膨胀向量仅由面片的法向决定,因此只需用变换中的旋转分量对其进行变换即可
  312.         A3DMATRIX4 mtxRotate(mtxTrans);
  313.         mtxRotate.SetRow(3,A3DVECTOR3(0.0f,0.0f,0.0f));
  314.         float fScale=mtxTrans.GetCol(0).Magnitude();
  315.         mtxRotate/=fScale;

  316.         for(int i=0;i<m_arrInflateVecs.GetSize();i++)
  317.                 m_arrInflateVecs[i]=m_arrInflateVecs[i]*mtxRotate;


  318. }

  319. }        // end namespace
复制代码
3125816
发表于 2011-8-7 21:44:16 | 显示全部楼层
看不懂    只会 VB
zhangjianqwe
 楼主| 发表于 2011-8-7 22:05:00 | 显示全部楼层
3125816 发表于 2011-8-7 21:44
看不懂    只会 VB

哦。。。我写的是游戏方面的东西
liuhaotian0520
发表于 2011-8-11 12:39:59 | 显示全部楼层
zhangjianqwe 发表于 2011-8-7 22:05
哦。。。我写的是游戏方面的东西

好长啊……下午有事情,晚上或者明天再来看看
zhangjianqwe
 楼主| 发表于 2011-8-11 12:40:30 | 显示全部楼层
liuhaotian0520 发表于 2011-8-11 12:39
好长啊……下午有事情,晚上或者明天再来看看

可以哦
您需要登录后才可以回帖 登录 | 快速注册

本版积分规则

手机版|杀毒软件|软件论坛| 卡饭论坛

Copyright © KaFan  KaFan.cn All Rights Reserved.

Powered by Discuz! X3.4( 沪ICP备2020031077号-2 ) GMT+8, 2024-7-3 06:42 , Processed in 0.130404 second(s), 16 queries .

卡饭网所发布的一切软件、样本、工具、文章等仅限用于学习和研究,不得将上述内容用于商业或者其他非法用途,否则产生的一切后果自负,本站信息来自网络,版权争议问题与本站无关,您必须在下载后的24小时之内从您的电脑中彻底删除上述信息,如有问题请通过邮件与我们联系。

快速回复 客服 返回顶部 返回列表