类似功能产生不同的输出

时间:2015-02-20 16:40:19

标签: c++ class oop object namespaces

我正在尝试编译下面的文件。 PosLin.cpp包含以下SurTriAutogetSphere函数。虽然它们很相似,但我得到的结果并不相同。是因为"命名空间TPiecesNS"导致他们与众不同?

我有tpieces.h个文件

namespace TPiecesNS
{
  class TPieces
  {
  public:
    TPieces();
    //other stuff
  }
}

tpieces.cpp有:

void TPieces::addPoint(Vertex* point)
{
    Vertex* p = new Vertex();
    p->Point[0] = point->Point[0]; //similar for Point[1],[2]
    p->Normal[0] = point->Normal[0]; //same for 1,2
    m_Vertices.push_back(p);
}

geopar.h文件有

#include "tpieces.h"
#include "Geo/Geo.h"

class Geo;

namespace TPiecesNS
{
   class GeoPar;
   {
    public:
          GeoPar();
          TPieces* getSphere(Geo* geo);
          TPieces* getSphere(Geo* geo, int permu);
    private:
      TPieces* SurTriAuto(TPieces* boundary, Geo* geo,int permu);

   }
}

geopar.cpp文件有

#include "tpieces/geo.h"
#include "tpieces.h"
#include "Geo/Geo.h"

using namespace TPiecesNS;

TPieces* GeoPar::getSphere(Geo* geo) {
    return getSphere(geo, 0);
}

TPieces* GeoPar::getSphere(Geo* geo, int permu)
{
TPieces* boundary = new Sphere();
return SurTriAuto(boundary,geo,permu);
}

TPieces* GeoPar::SurTriAuto(TPieces* boundary, Geo* geo, int permu)
{
  double maxx, maxy, maxz, minx, miny, minz;
  double        x,y,z,f,nx,ny,nz;
  int number = 6;

  ofstream file;
  file.open("output.txt");

  boundary->numbpts = geo->m_NumTriVerts;
  boundary->numbtris = geo->m_NumTris; 
  file<<"NumVertices "<<boundary->numbpts<<endl;
  file<<"NumTrianlges "<<boundary->numbtris<<endl;

  for (i = 0 ; i < boundary->numbpts; i++)
  {
    x = geometry->m_TriVerts[i*3+0];
    //also equalities for y,z, but I don't want to type here in order to save space
    nx = geometry->m_TriVertNormals[i*3+0];
        //ny, nz also
    if (x < minx) minx = x;
        //comparisons for y,z also and comparing to maxx, maxy, maxz

    Vertex* point = new Vertex();
        point->Point[0] = x;
        point->Point[1] = y;
        point->Point[2] = z;

    point->Normal[0] = nx; //also assignments for ny, nz
    file<<"xyz normals: "<<point->Point[0]<<endl;
    //I also printed out  y,z,nx,ny,nz
        boundary->addPoint(point);
  }

      for (i = 0 ; i < boundary->numbtris; i++)
 {
    ii = geo->m_Tris[i*3+0]; //assignments for jj, kk also
        if (ii < jj && jj < kk)    { i1 = ii; i2 = jj;  i3 = kk; }
       //similar comparisons for jj and kk also here, but I want to save space
        //...
        if (kk < ii && ii < jj)    { i1 = kk; i2 = ii;  i3 = jj; }  // result in i1 <= i2 <= i3

       Face* facet = new Face();
       facet->Index[0] = i1; //i2, i3 are also assigned
      facet->IndexInR[0] = ii; //jj, kk also

      boundary->addFacet(facet);
    }   /* end facet (i) loop */

 for (i = 0 ; i <boundary->numbtris; i++)
 {
    for(int j=0;j<3;j++)
    {
        int index = boundary->m_Faces[i]->Index[j];
        for(int k=0;k<3;k++)
        {
            file<<boundary->m_Faces[i]->Normal[k]<<" "<<boundary->m_Vertices[index]->Normal[k]<<endl;
            boundary->m_Faces[i]->Normal[k] += boundary->m_Vertices[index]->Normal[k];   
            //ERROR IS  HERE
            file<<boundary->m_Faces[i]->Normal[k]<<endl;
        }
    }
 }  

return boundary;

}

PosLin.h

#include "TPieces/tpieces.h"
#include "TPieces/geoPar.h"
#include "Geo/Geo.h"

struct PosRotAndQ {

  TPiecesNS::TPieces* boundary;
};

class PS{
public: 

  PosExCode computation(Geo* geo, POpinion* opinion, PositionRotation* matterboundary)

  PositionRotation* matterboundary;

}

PosLin.cpp

#include "tpieces/tpieces.h"
#include "Geo/Geo.h"

PosExCode PS::computation(Geo* geo, POpinion* opinion, PositionRotation* matterboundary)

{
    TPiecesNS::GeoPar* perform = new TPiecesNS::GeoPar();
    TPiecesNS::TPieces* boundary = new TPiecesNS::Sphere();
    boundary->sphere = perform->SurTriAuto(boundary, geo,0);//if I comment this line out and the line below and un-comment the 2 getSphere lines below, they do not produce the same output
    boundary->sphereDark[0] = perform->SurTriAuto(boundary, geo,0); \
    //boundary->sphere = perform->getSphere(geo,0);
    //boundary->sphereDark[0] = perform->getSphere(geo,0);
}

我注意到getSphere和SurTriAuto获得了不同的输出,特别是在surface->m_Faces[i]->Normal[k] += surface->m_Vertices[index]->Normal[k];

在输出的文本文件中,在+=操作发生之前,值surface->m_Faces[i]->Normal[k]surface->m_Vertices[index]->Normal[k]与getSphere和SurTriAuto的值不同,即使所有其他值(例如因为x,y,z,索引值)是相同的。

我怀疑这是因为其中一个boundary指针在GeoPar.cpp中的getSphere {/ 1}}和/或PosLin.cpp中的TPieces* boundary = new Sphere();中丢失了值

1 个答案:

答案 0 :(得分:0)

在一种情况下,您同时使用相同的boundary对象。在另一种情况下,每个函数都使用一个新的boundary对象。

您尚未显示TPieces类的功能,但我认为addPointaddFacet会更改TPieces类的内容,以便在您编写时在第二次调用中faces到该文件时,您最终会获得第一次调用中保存的faces

要使这两种情况相同,请尝试在第二次调用中使用不同的boundary对象。像这样:

TPiecesNS::GeoPar* perform = new TPiecesNS::GeoPar();
TPiecesNS::TPieces* boundary = new TPiecesNS::Sphere();
boundary->sphere = perform->SurTriAuto(boundary, geo,0);//if I comment this line out and the line below and un-comment the 2 getSphere lines below, they do not produce the same output
TPiecesNS::TPieces* boundary2 = new TPiecesNS::Sphere();
boundary->sphereDark[0] = perform->SurTriAuto(boundary2, geo,0);
//boundary->sphere = perform->getSphere(geo,0);
//boundary->sphereDark[0] = perform->getSphere(geo,0);