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

📄 vc_octreedoc.cpp

📁 用c++实现的八叉树建立
💻 CPP
字号:
// vc_octreeDoc.cpp : implementation of the CVc_octreeDoc class
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
//#include <stdio.h>
//#include <stdlib.h>
#include "vc_octree.h"

#include "vc_octreeDoc.h"
#include "octree.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
using namespace std;
/////////////////////////////////////////////////////////////////////////////
// CVc_octreeDoc

IMPLEMENT_DYNCREATE(CVc_octreeDoc,CDocument)

BEGIN_MESSAGE_MAP(CVc_octreeDoc,CDocument)
	//{{AFX_MSG_MAP(CVc_octreeDoc)
	ON_COMMAND(ID_TT,OnTt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/*------------------------------------------------------*/
FILE *outfile=fopen("qq.obj","w");
int aa;
double vex_xmin,vex_ymin,vex_zmin,vex_xmax,vex_ymax,vex_zmax;
void debug_octree( int depth, Octree* debug );
void leaf_node( int depth, Octree* leaf );
void count_cubes( Octree* root, int depth, int* n_cubes );
void measure_octree( Octree* root, int max_depth );
void read_data();
/*------------------------------------------------------*/
/////////////////////////////////////////////////////////////////////////////
// CVc_octreeDoc construction/destruction

CVc_octreeDoc::CVc_octreeDoc()
{
	// TODO: add one-time construction code here

}

CVc_octreeDoc::~CVc_octreeDoc()
{
}

BOOL CVc_octreeDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CVc_octreeDoc serialization

void CVc_octreeDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CVc_octreeDoc diagnostics

#ifdef _DEBUG
void CVc_octreeDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CVc_octreeDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CVc_octreeDoc commands

void CVc_octreeDoc::OnTt() 
{
  const int min_depth = 1;
  const int max_depth = 6;
  read_data();
  //Vec3 min = makeVec3( -1, -1, -1 );
  //Vec3 max = makeVec3( 1, 1, 1 );
  Vec3 min = makeVec3(vex_xmin,vex_ymin,vex_zmin );
  Vec3 max = makeVec3( vex_xmax,vex_ymax,vex_zmax);
  Octree* top = make_octree( min, max );
  isoface(top);
  int depth = subdivide_octree( min_depth, max_depth, top );
  aa=0;
  debug_octree( 0, top ); 
  //leaf_node(0,top);
  measure_octree( top, depth );
  marchingcube(0, top);
  fclose(outfile);		
}
void debug_octree( int depth, Octree* debug )
{
  int c;//, d;
  //for(d=0; d < depth; d++)
   // printf("  ");
  
  //printf("%.3lf,%.3lf,%.3lf ... %.3lf,%.3lf,%.3lf\n"<<"  "<<
	 //debug->min[2]<<"  "<<debug->min[1]<<"  "<<debug->min[0],
	 //debug->max[2]<<"  "<<debug->max[1]<<"  "<<debug->max[0]<<endl;
  //fprintf(outfile," \n");
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", debug->min[0], debug->min[1], debug->min[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", debug->min[0], debug->max[1], debug->min[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", debug->max[0], debug->max[1], debug->min[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", debug->max[0], debug->min[1], debug->min[2]);

 
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", debug->min[0], debug->min[1], debug->max[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", debug->max[0], debug->min[1], debug->max[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", debug->max[0], debug->max[1], debug->max[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", debug->min[0], debug->max[1], debug->max[2]);

  fprintf(outfile,"f %d %d %d %d \n",aa+1,aa+2,aa+3,aa+4);
  fprintf(outfile,"f %d %d %d %d \n",aa+5,aa+6,aa+7,aa+8);
  fprintf(outfile,"f %d %d %d %d \n",aa+4,aa+3,aa+7,aa+6);
  fprintf(outfile,"f %d %d %d %d \n",aa+1,aa+5,aa+8,aa+2);
  fprintf(outfile,"f %d %d %d %d \n",aa+3,aa+2,aa+8,aa+7);
  fprintf(outfile,"f %d %d %d %d \n",aa+4,aa+6,aa+5,aa+1);

  if(debug->children != 0)
  {
    for(c=0; c < 8; c++)
    { aa+=8;
      debug_octree( depth + 1, debug->children[ c ] );
	  
    }
  }
}
void leaf_node( int depth, Octree* leaf )
{
	int c;
	if(leaf->children==0&&leaf->vex.GetCount()!=0)
	{
		fprintf(outfile,"v %.3lf %.3lf %.3lf\n", leaf->min[0], leaf->min[1], leaf->min[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", leaf->min[0], leaf->max[1], leaf->min[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", leaf->max[0], leaf->max[1], leaf->min[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", leaf->max[0], leaf->min[1], leaf->min[2]);

 
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", leaf->min[0], leaf->min[1], leaf->max[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", leaf->max[0], leaf->min[1], leaf->max[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", leaf->max[0], leaf->max[1], leaf->max[2]);
  fprintf(outfile,"v %.3lf %.3lf %.3lf\n", leaf->min[0], leaf->max[1], leaf->max[2]);

  fprintf(outfile,"f %d %d %d %d \n",aa+1,aa+2,aa+3,aa+4);
  fprintf(outfile,"f %d %d %d %d \n",aa+5,aa+6,aa+7,aa+8);
  fprintf(outfile,"f %d %d %d %d \n",aa+4,aa+3,aa+7,aa+6);
  fprintf(outfile,"f %d %d %d %d \n",aa+1,aa+5,aa+8,aa+2);
  fprintf(outfile,"f %d %d %d %d \n",aa+3,aa+2,aa+8,aa+7);
  fprintf(outfile,"f %d %d %d %d \n",aa+4,aa+6,aa+5,aa+1);
	}
	if(leaf->children!=0)
	{
       for(c=0; c < 8; c++)
	   {   aa+=8;
           leaf_node( depth + 1, leaf->children[ c ] );
	   }
	}
}
void count_cubes( Octree* root, int depth, int* n_cubes )
{
  int c, cd;

  if(root->children == 0)
  {
    n_cubes[ depth ] ++;
  }
  else
  {
    cd = depth+1;
    for(c=0; c < 8; c++)
    {
      count_cubes(  root->children[ c ], cd, n_cubes );
    }
  }
}

void measure_octree( Octree* root, int max_depth )
{
  int d;
  int* n_cubes = (int*) malloc(sizeof(int) * max_depth);
  
  for(d=0; d < max_depth; d++)
    n_cubes[d] = 0;

  count_cubes( root, 0, n_cubes );

  for(d=0; d < max_depth; d++)
    printf( "  %d:\t%d\n", d, n_cubes[d] );
}
void read_data()
{
    char bf[2];
	//CPtrList vexlist,norlist;
	float v1,v2,v3;
	vex_xmax=-99999.0;vex_ymax=-99999.0;vex_zmax=-99999.0;
	vex_xmin=99999.0;vex_ymin=99999.0;vex_zmin=99999.0;
	ifstream file("ww9.obj");
	
   	if(!file.fail())
	{
		while(!file.eof())
		{
			file>>bf>>v1>>v2>>v3;
			if(bf[0]=='v'&&bf[1]=='n')
			{                         }
				//norlist.AddHead(new pvex_nor(v1,v2,v3));
			else
			{
				//vexlist.AddHead(new pvex_nor(v1,v2,v3));
				if(v1>vex_xmax) vex_xmax=v1;
				if(v2>vex_ymax) vex_ymax=v2;
				if(v3>vex_zmax) vex_zmax=v3;
				if(v1<vex_xmin) vex_xmin=v1;
				if(v2<vex_ymin) vex_ymin=v2;
				if(v3<vex_zmin) vex_zmin=v3;
			}
		}
		
	}

    file.close();

	
}

⌨️ 快捷键说明

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