📄 glbox.c
字号:
/****************************************************************************** $Id: glbox.cpp,v 1.5.2.1 1999/09/08 08:30:38 aavit Exp $**** Implementation of vGLBox** This is a simple QGLWidget displaying an openGL wireframe box**** The OpenGL code is mostly borrowed from Brian Pauls "spin" example** in the Mesa distribution******************************************************************************/#include <stdlib.h>#include <stdio.h>#include <iostream>#include <ctype.h>#include <math.h>#include <time.h>#include <qapplication.h>#include <assert.h>#include <GL/glut.h>//#include "dmalloc.h"//#include "scape/vector3d.h"#include "_generic.h"#include "sarray.h"#include "real.h"//#include "line.h"#include "point.h"#include "bbox.h"#include "ftree.h"//#include "TreeDiam.h"//#include "glwindow.h"#include "glbox.h"#include "misc.h"//#include "terrain.h"#include "distance.h"#include "algorithm.h"#include "3dsload.h"#include "glwindow_geometry.h"//#include "sarray.t"#include "strech.h"#if defined(_CC_MSVC_)#pragma warning(disable:4305) // init: truncation from const double to float#endifstatic double glb_translate_x, glb_translate_y, glb_translate_z;static double glb_scale;void set_global_translate( double x, double y, double z ){ glb_translate_x = x; glb_translate_y = y; glb_translate_z = z;}//void set_global_translate( double x, double y, double z )void set_global_scale( double sc ){ glb_scale = sc;}/*! Create a GLBox widget*/void pnt_array_trans( ArrPoint3d & arr, const Point3d & pnt ){ for ( int ind = 0; ind < arr.size(); ind++ ) arr[ ind ].add( pnt );}void pnt_array_scale( ArrPoint3d & arr, const real & scale ){ for ( int ind = 0; ind < arr.size(); ind++ ) arr[ ind ].scale( scale );}void pnt_array_normalize( ArrPoint3d & arr ){ BBox3d bb; real scale; pnt_array_bound( arr, bb ); Point3d pnt; pnt.setCoord( 0, -bb.get_min_coord( 0 ) ); pnt.setCoord( 1, -bb.get_min_coord( 1 ) ); pnt.setCoord( 2, -bb.get_min_coord( 2 ) ); pnt_array_trans( arr, pnt ); scale = bb.getLongestEdge(); if ( scale != 0.0 ) pnt_array_scale( arr, 1.0 / scale ); set_global_translate( -bb.get_min_coord( 0 ), -bb.get_min_coord( 1 ), -bb.get_min_coord( 2 ) ); set_global_scale( 1.0 / scale ); for ( int ind = 0; ind < arr.size(); ind++ ) arr[ ind ].setID( ind );}static void read_3ds_vertices( FILE * f, long fomax, ArrPoint3d & arr ){ unsigned short id; unsigned int len; long fo; //printf( "sizeof(id): %d, sizeof( len): %d\n", // (int)sizeof( id ), // (int)sizeof( len ) ); //printf( "read_3ds_vertices( ?, %ld, ? )\n", fomax ); // look through blocks until end file offset reached while(fo = ftell(f), fo < fomax){ // get block ID if(fread(&id, sizeof(id), 1, f) != 1) return; // get block size if(fread(&len, sizeof(len), 1, f) != 1 || !len) return; //printf( "len: %X\n", len ); if(id == 0x4000){ // object int c; // skip through string identifier while ((c = fgetc(f)) != EOF && c != '\0') ; } if(id == 0x4110){ // vertex list unsigned short nv; // get # of vertices if(fread(&nv, sizeof(nv), 1, f) != 1) return; // stash in Verts while(nv-- > 0){ float Verts[ 10 ]; if(fread( Verts, 3*sizeof(float), 1, f) != 1) return; Point3d pnt; pnt.setCoord( 0, Verts[ 0 ] ); pnt.setCoord( 1, Verts[ 1 ] ); pnt.setCoord( 2, Verts[ 2 ] ); arr.pushx( pnt ); } } // descend hierarchy for appropriate composite groups if(id == 0x3D3D || id == 0x4000 || id == 0x4100 || id == 0x4D4D) read_3ds_vertices( f, fo + len, arr ); // go to end of block fseek(f, fo + len, SEEK_SET); if(feof(f) || ferror(f)) return; }}void read_3ds( const char * file_name, ArrPoint3d & arr ){ FILE * f; long flen; arr.init( 10, 3443 ); f = fopen( file_name, "rb"); if (f == NULL) { printf("Can't open %s!\n", file_name ); exit( -1 ); } fseek(f, 0, SEEK_END); flen = ftell(f); fseek(f, 0, SEEK_SET); read_3ds_vertices( f, flen, arr ); fclose( f );}bool is_prefix( const char * filename, const char * prefix ){ if ( strlen( filename ) < strlen( prefix ) ) return false; return ( strcmp( filename + (strlen( filename ) - strlen( prefix) ), prefix ) == 0 );}void * read_poly_file( const char * filename ){ PolyModel * pm = new PolyModel; assert( pm != NULL ); pm->init(); pm->read_poly_file( filename ); pm->init_vertices_bboxes(); pm->find_nn_all(); pm->compute_longest_edges(); pm->print_stat( filename ); pm->computeBBox(); //pm->computeWorstVertex(); return (void *)pm;}void extract_vertices( PolyModel * poly_scene, ArrPoint3d & arr ){ ArrPoint3d & verts( poly_scene->getVertices() ); for ( int ind = 0; ind < verts.size(); ind++ ) { arr.pushx( verts[ ind ] ); }}void GLBox::load( const char * filename ){ printf( "GLBox::load\n" ); if ( p_diam_pair != NULL ) { delete p_diam_pair; p_diam_pair = NULL; } p_arr->reset(); if ( is_prefix(filename, "3ds" ) ) { read_3ds( filename, *p_arr ); scene = (void *)ThreeDS_ReadFromFile( filename ); } else { poly_scene = read_poly_file( filename ); extract_vertices( (PolyModel *)poly_scene, *p_arr ); //read_poly( filename, *p_arr ); } pnt_array_normalize( *p_arr ); printf( "resetDisplay!\n" ); resetDisplay(); prepareView(); repaint();}GLBox::GLBox( QWidget* parent, const char* name ) : GLWindow( parent, name ){ //sites = (Vector3d *)malloc( sizeof( Vector3d ) * SITES_SIZE ); //assert( sites != NULL ); //memset( sites, 0, sizeof( sites ) ); //sites_num = 0; f_update_gl = true; p_diam_pair = NULL; //scale = 0.125; // default object scale f_draw_direct = false; f_smooth = false; scene = NULL; poly_scene = NULL; p_arr = new ArrPoint3d; //read_poly_file( *p_arr, "inputs/x" ); //bunny.ply" ); //read_3ds( "inputs/bench1.3ds", *p_arr ); //read_3ds( "inputs/duck.3ds", *p_arr ); //read_3ds( "inputs/dragon.3ds", *p_arr ); //fill_array_sphere( *p_arr, 1000 ); //fill_array_two_arcs( *p_arr, 100000 ); //fill_array_sphere_caps( *p_arr, 4000, 0.000001 ); //fill_array_cube( *p_arr, 300 ); //fill_array_box( *p_arr, 800, 1, 0.5, 0.25 ); //fill_array_mutated_ellipsoid( *p_arr, 8000, 1, 0.99, 0.98 ); //fill_array_strange_cube_diag( *p_arr, 1000 ); fill_array_strange_cube( *p_arr, 1000 ); printf( "points: %d\n", p_arr->size() ); pnt_array_normalize( *p_arr );}void GLBox::getBBox( GBBox * p_bb ){ printf( "GLBox::getBBox!\n" ); p_bb->init(); if ( p_arr->size() == 0 ) return; for ( int ind = 0; ind < p_arr->size(); ind++ ) { p_bb->bound( (*p_arr)[ ind ][ 0 ], (*p_arr)[ ind ][ 1 ], (*p_arr)[ ind ][ 2 ] ); } p_bb->dump();}/*! Release allocated resources*/GLBox::~GLBox(){}#ifdef GOGIvoid GLBox::prepareGL(){ glLoadIdentity(); glTranslatef( 0.0, 0.0, -10.0 ); glScalef( scale, scale, scale ); glScalef( zoom, zoom, zoom );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -