BOOST_AUTO_TEST_CASE_TEMPLATE(decorator_formatting, CharT, char_types) { typedef logging::record_view record_view; typedef logging::attribute_set attr_set; typedef std::basic_string< CharT > string; typedef logging::basic_formatting_ostream< CharT > osstream; typedef logging::basic_formatter< CharT > formatter; typedef test_strings< CharT > data; attrs::constant< string > attr1(data::printable_chars()); attr_set set1; set1[data::attr1()] = attr1; record_view rec = make_record_view(set1); { string str1, str2; osstream strm1(str1), strm2(str2); formatter f = expr::stream << expr::make_xml_decor< CharT >()[ expr::stream << expr::attr< string >(data::attr1()) ]; f(rec, strm1); strm2 << data::escaped_chars(); BOOST_CHECK(equal_strings(strm1.str(), strm2.str())); } }
// The test checks that Boost.Format formatting works BOOST_AUTO_TEST_CASE_TEMPLATE(formatting, CharT, char_types) { typedef logging::attribute_set attr_set; typedef std::basic_string< CharT > string; typedef logging::basic_formatting_ostream< CharT > osstream; typedef logging::record_view record_view; typedef logging::basic_formatter< CharT > formatter; typedef format_test_data< CharT > data; attrs::constant< int > attr1(10); attrs::constant< double > attr2(5.5); attr_set set1; set1[data::attr1()] = attr1; set1[data::attr2()] = attr2; record_view rec = make_record_view(set1); { string str1, str2; osstream strm1(str1), strm2(str2); formatter f = expr::format(data::format1()) % expr::attr< int >(data::attr1()) % expr::attr< double >(data::attr2()); f(rec, strm1); strm2 << 10 << ", " << 5.5; BOOST_CHECK(equal_strings(strm1.str(), strm2.str())); } }
// Test void pointer handling BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_pvoid_dump, CharT, char_types) { typedef CharT char_type; typedef std::basic_string< char_type > string_type; typedef std::basic_ostringstream< char_type > ostream_type; std::vector< unsigned char > data; data.push_back(1); data.push_back(2); data.push_back(3); data.push_back(100); data.push_back(110); data.push_back(120); data.push_back(200); data.push_back(210); data.push_back(220); ostream_type strm_dump; strm_dump << logging::dump((void*)&data[0], data.size()); ostream_type strm_correct; strm_correct << "01 02 03 64 6e 78 c8 d2 dc"; BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str())); }
VIOAPI Status mni_input_keyword_and_equal_sign( FILE *file, const char keyword[], BOOLEAN print_error_message ) { Status status; STRING str; status = mni_input_string( file, &str, (char) '=', (char) 0 ); if( status == END_OF_FILE ) return( status ); if( status != OK || !equal_strings( str, (STRING) keyword ) || mni_skip_expected_character( file, (char) '=' ) != OK ) { if( print_error_message ) print_error( "Expected \"%s =\"\n", keyword ); status = ERROR; } delete_string( str ); return( status ); }
// The test checks that Boost.Format formatting works BOOST_AUTO_TEST_CASE_TEMPLATE(formatting, CharT, char_types) { typedef logging::basic_attribute_set< CharT > attr_set; typedef std::basic_string< CharT > string; typedef std::basic_ostringstream< CharT > osstream; typedef logging::basic_record< CharT > record; typedef boost::function< void (osstream&, record const&) > formatter; typedef format_test_data< CharT > data; attrs::constant< int > attr1(10); attrs::constant< double > attr2(5.5); attr_set set1; set1[data::attr1()] = attr1; set1[data::attr2()] = attr2; record rec = make_record(set1); rec.message() = data::some_test_string(); { osstream strm1; formatter f = fmt::format(data::format1()) % fmt::attr< int >(data::attr1()) % fmt::attr< double >(data::attr2()); f(strm1, rec); osstream strm2; strm2 << 10 << ", " << 5.5; BOOST_CHECK(equal_strings(strm1.str(), strm2.str())); } }
// Test bounded dump BOOST_AUTO_TEST_CASE_TEMPLATE(bounded_binary_dump, CharT, char_types) { typedef CharT char_type; typedef std::basic_string< char_type > string_type; typedef std::basic_ostringstream< char_type > ostream_type; std::vector< unsigned char > data; ostream_type strm_correct; for (unsigned int i = 0; i < 1024; ++i) { unsigned char n = static_cast< unsigned char >(i); data.push_back(n); if (i < 500) { if (i > 0) strm_correct << " "; strm_correct << std::hex << std::setw(2) << std::setfill(static_cast< char_type >('0')) << static_cast< unsigned int >(n); } } strm_correct << std::dec << std::setfill(static_cast< char_type >(' ')) << " and " << 524u << " bytes more"; ostream_type strm_dump; strm_dump << logging::dump(&data[0], data.size(), 500); BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str())); }
// The test checks that message formatting work BOOST_AUTO_TEST_CASE_TEMPLATE(message_formatting, CharT, char_types) { typedef logging::attribute_set attr_set; typedef std::basic_string< CharT > string; typedef logging::basic_formatting_ostream< CharT > osstream; typedef logging::record_view record_view; typedef logging::basic_formatter< CharT > formatter; typedef message_test_data< CharT > data; attrs::constant< int > attr1(10); attrs::constant< double > attr2(5.5); attr_set set1; set1[data::attr1()] = attr1; set1[data::attr2()] = attr2; set1[data::message().get_name()] = attrs::constant< string >(data::some_test_string()); record_view rec = make_record_view(set1); { string str1, str2; osstream strm1(str1), strm2(str2); formatter f = expr::stream << data::message(); f(rec, strm1); strm2 << data::some_test_string(); BOOST_CHECK(equal_strings(strm1.str(), strm2.str())); } }
// Test bounded array dump BOOST_AUTO_TEST_CASE_TEMPLATE(bounded_element_dump, CharT, char_types) { typedef CharT char_type; typedef std::basic_string< char_type > string_type; typedef std::basic_ostringstream< char_type > ostream_type; std::vector< unsigned int > data; data.push_back(0x01020a0b); data.push_back(0x03040c0d); data.push_back(0x05060e0f); ostream_type strm_dump; strm_dump << logging::dump_elements(&data[0], data.size(), 2); ostream_type strm_correct; const unsigned char* p = reinterpret_cast< const unsigned char* >(&data[0]); std::size_t size = 2 * sizeof(unsigned int); for (unsigned int i = 0; i < size; ++i) { unsigned char n = p[i]; if (i > 0) strm_correct << " "; strm_correct << std::hex << std::setw(2) << std::setfill(static_cast< char_type >('0')) << static_cast< unsigned int >(n); } strm_correct << std::dec << std::setfill(static_cast< char_type >(' ')) << " and " << (data.size() - 2) * sizeof(unsigned int) << " bytes more"; BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str())); }
VIOAPI VIO_Status initialize_tag_file_input( FILE *file, int *n_volumes_ptr ) { VIO_STR line; int n_volumes; /* parameter checking */ if( file == NULL ) { print_error( "initialize_tag_file_input(): passed NULL FILE ptr.\n"); return( VIO_ERROR ); } /* okay read the header */ if( mni_input_string( file, &line, (char) 0, (char) 0 ) != VIO_OK || !equal_strings( line, (VIO_STR) TAG_FILE_HEADER ) ) { print_error( "input_tag_points(): invalid header in file.\n"); delete_string( line ); return( VIO_ERROR ); } delete_string( line ); /* now read the number of volumes */ if( mni_input_keyword_and_equal_sign( file, VOLUMES_STRING, TRUE ) != VIO_OK ) return( VIO_ERROR ); if( mni_input_int( file, &n_volumes ) != VIO_OK ) { print_error( "input_tag_points(): expected # volumes after %s.\n", VOLUMES_STRING ); return( VIO_ERROR ); } if( mni_skip_expected_character( file, (char) ';' ) != VIO_OK ) return( VIO_ERROR ); if( n_volumes != 1 && n_volumes != 2 ) { print_error( "input_tag_points(): invalid # volumes: %d \n", n_volumes ); return( VIO_ERROR ); } /* now read the tag points header */ if( mni_input_keyword_and_equal_sign( file, TAG_POINTS_STRING, TRUE ) != VIO_OK) return( VIO_ERROR ); if( n_volumes_ptr != NULL ) *n_volumes_ptr = n_volumes; return( VIO_OK ); }
// The test checks that time_duration formatting work BOOST_AUTO_TEST_CASE_TEMPLATE(time_duration, CharT, char_types) { typedef logging::attribute_set attr_set; typedef std::basic_string< CharT > string; typedef logging::basic_formatting_ostream< CharT > osstream; typedef logging::record_view record_view; typedef logging::basic_formatter< CharT > formatter; typedef test_data< CharT > data; typedef date_time_formats< CharT > formats; typedef boost::date_time::time_facet< ptime, CharT > facet; duration t1(14, 40, 15); attrs::constant< duration > attr1(t1); attr_set set1; set1[data::attr1()] = attr1; record_view rec = make_record_view(set1); // Check for various formats specification { string str1, str2; osstream strm1(str1), strm2(str2); formatter f = expr::stream << expr::format_date_time< duration >(data::attr1(), formats::default_time_duration_format().c_str()); f(rec, strm1); facet* fac = new facet(); fac->time_duration_format(formats::default_time_duration_format().c_str()); strm2.imbue(std::locale(strm2.getloc(), fac)); strm2 << t1; BOOST_CHECK(equal_strings(strm1.str(), strm2.str())); } { string str1, str2; osstream strm1(str1), strm2(str2); formatter f = expr::stream << expr::format_date_time< duration >(data::attr1(), formats::time_duration_format().c_str()); f(rec, strm1); facet* fac = new facet(); fac->time_duration_format(formats::time_duration_format().c_str()); strm2.imbue(std::locale(strm2.getloc(), fac)); strm2 << t1; BOOST_CHECK(equal_strings(strm1.str(), strm2.str())); } }
/*Look up a word in a dictionary*/ int lookup (struct entry dictionary[], char search[], int entries){ //int equal_strings (char s1[], char s2[]); int i; for (i=0; i < entries; ++i) if ( equal_strings (search, dictionary[i].word) ) return (i); return (-1); } // lookup
// Test operator forwarding BOOST_AUTO_TEST_CASE_TEMPLATE(operator_forwarding, CharT, char_types) { typedef CharT char_type; typedef std::basic_string< char_type > string_type; typedef std::basic_ostringstream< char_type > ostream_type; typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type; string_type str_fmt; formatting_ostream_type strm_fmt(str_fmt); const my_namespace::A a = my_namespace::A(); // const lvalue my_namespace::B b; // lvalue strm_fmt << a << b << my_namespace::C(); // rvalue strm_fmt.flush(); ostream_type strm_correct; strm_correct << a << b << my_namespace::C(); BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str())); }
read_file() { int i,j; int elem_count; char *elem_name; /*** Read in the original PLY object ***/ in_ply = read_ply (stdin); for (i = 0; i < in_ply->num_elem_types; i++) { /* prepare to read the i'th list of elements */ elem_name = setup_element_read_ply (in_ply, i, &elem_count); if (equal_strings ("vertex", elem_name)) { /* create a vertex list to hold all the vertices */ vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count); nverts = elem_count; /* set up for getting vertex elements */ setup_property_ply (in_ply, &vert_props[0]); setup_property_ply (in_ply, &vert_props[1]); setup_property_ply (in_ply, &vert_props[2]); vert_other = get_other_properties_ply (in_ply, offsetof(Vertex,other_props)); /* grab all the vertex elements */ for (j = 0; j < elem_count; j++) { vlist[j] = (Vertex *) malloc (sizeof (Vertex)); get_element_ply (in_ply, (void *) vlist[j]); } } else get_other_element_ply (in_ply); } close_ply (in_ply); }
// Test SIMD tail handling BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_tail_dump, CharT, char_types) { typedef CharT char_type; typedef std::basic_ostringstream< char_type > ostream_type; std::vector< unsigned char > data; ostream_type strm_correct; // 1023 makes it very unlikely for the buffer to end at 16 or 32 byte boundary, which makes the dump algorithm to process the tail in a special way for (unsigned int i = 0; i < 1023; ++i) { unsigned char n = static_cast< unsigned char >(i); data.push_back(n); if (i > 0) strm_correct << " "; strm_correct << std::hex << std::setw(2) << std::setfill(static_cast< char_type >('0')) << static_cast< unsigned int >(n); } ostream_type strm_dump; strm_dump << logging::dump(&data[0], data.size()); BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str())); }
// Test a short data region with uppercase formatting BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_uppercase_short_dump, CharT, char_types) { typedef CharT char_type; typedef std::basic_ostringstream< char_type > ostream_type; std::vector< unsigned char > data; data.push_back(1); data.push_back(2); data.push_back(3); data.push_back(100); data.push_back(110); data.push_back(120); data.push_back(200); data.push_back(210); data.push_back(220); ostream_type strm_dump; strm_dump << std::uppercase << logging::dump(&data[0], data.size()); ostream_type strm_correct; strm_correct << "01 02 03 64 6E 78 C8 D2 DC"; BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str())); }
DeformModel::DeformModel(const std::string &fname, unsigned int num_frame, float ply_scale) { Init(); // char ply_fname[256]; _num_frame = num_frame; for (unsigned int cur_f = 0; cur_f < _num_frame; cur_f++) { std::stringstream ply_fname; ply_fname << fname << cur_f << ".ply"; // sprintf(ply_fname, "%s%d.ply", fname, cur_f); std::cout << "Opening " << ply_fname.str() << std::endl; FILE *fp = fopen(ply_fname.str().c_str(), "rb"); assert(fp); // PLY object: PlyFile *ply; // PLY properties: char **elist; int nelems; // hand over the stream to the ply functions: ply = ply_read(fp, &nelems, &elist); assert(ply); int file_type; float version; ply_get_info(ply, &version, &file_type); for (int i=0; i<nelems; i++) { char *elem_name = elist[i]; int num_elems, nprops; PlyProperty **plist = ply_get_element_description(ply, elem_name, &num_elems, &nprops); bool has_vertex_x = false, has_vertex_y = false, has_vertex_z = false, has_colors = false; unsigned char color_components = 0; // this is a vertex: if (equal_strings ("vertex", elem_name)) { for (int j=0; j<nprops; j++) { if (equal_strings("x", plist[j]->name)) { ply_get_property (ply, elem_name, &vert_props[0]); /* x */ has_vertex_x = true; } else if (equal_strings("y", plist[j]->name)) { ply_get_property (ply, elem_name, &vert_props[1]); /* y */ has_vertex_y = true; } else if (equal_strings("z", plist[j]->name)) { ply_get_property (ply, elem_name, &vert_props[2]); /* z */ has_vertex_z = true; } else if (equal_strings("red", plist[j]->name)) { ply_get_property (ply, elem_name, &vert_props[3]); /* z */ color_components++; } else if (equal_strings("green", plist[j]->name)) { ply_get_property (ply, elem_name, &vert_props[4]); /* z */ color_components++; } else if (equal_strings("blue", plist[j]->name)) { ply_get_property (ply, elem_name, &vert_props[5]); /* z */ color_components++; } } has_colors = color_components == 3; // test for necessary properties if ((!has_vertex_x) || (!has_vertex_y) || (!has_vertex_z)) { logger->update("Warning: Vertex with less than 3 coordinated detected. Output will most likely be corrupt!"); continue; } // must be first frame, initialize structures: if (_num_vtx == 0) { _num_vtx = num_elems; _vtxs = new vec3f[_num_vtx*_num_frame]; _cur_vtxs = new vec3f[_num_vtx]; _prev_vtxs = new vec3f[_num_vtx]; _vtx_boxes = new BOX[_num_vtx]; _nrms = new vec3f[_num_vtx]; _vtx_fids = new id_list[_num_vtx]; cout << "Vtx # = " << _num_vtx << endl; if (has_colors) _colors = new color3[_num_vtx]; } // grab all the vertex elements PLYVertex plyNewVertex; for (int j=0; j<num_elems; j++) { ply_get_element(ply, (void *)&plyNewVertex); if (has_colors && cur_f == 0) { _colors[j].set(plyNewVertex.color); } _vtxs[cur_f*_num_vtx+j] = vec3f(plyNewVertex.coords) * ply_scale; if (cur_f == 0) { _prev_vtxs[j] = _cur_vtxs[j] = _vtxs[j]; } if (j != 0 && j%1000000 == 0) { cout << " - " << j << " of " << num_elems << " loaded." << endl; } } } // this is a face (and, hopefully, a triangle): else if (equal_strings ("face", elem_name) && _tris == NULL) { // I need this for..., otherwise error ... for (int j=0; j<nprops; j++) { if (equal_strings("vertex_indices", plist[j]->name)) { ply_get_property (ply, elem_name, &face_props[0]); /* vertex_indices */ } } /* grab all the face elements */ PLYFace plyFace; plyFace.other_props = NULL; list<edge2f> edgelist_temp; vector<tri3f> trilist_temp; for (int j = 0; j < num_elems; j++) { ply_get_element(ply, (void *)&plyFace); for (int fi = 0; fi < plyFace.nverts-2; fi++) { // // make a triangle in our format from PLY face + vertices // // copy vertex indices unsigned int id0, id1, id2; id0 = plyFace.verts[0]; id1 = plyFace.verts[fi+1]; id2 = plyFace.verts[fi+2]; tri3f tri(id0, id1, id2); // insert triangle into list trilist_temp.push_back(tri); unsigned int fid = (unsigned int)trilist_temp.size()-1; edgelist_temp.push_back(edge2f(id0, id1, fid)); edgelist_temp.push_back(edge2f(id1, id2, fid)); edgelist_temp.push_back(edge2f(id2, id0, fid)); } free(plyFace.verts); if (j != 0 && j%500000 == 0) { cout << " - " << j << " of " << num_elems << " loaded." << endl; } } edgelist_temp.sort(); list<edge2f> edge_unqie; for (list<edge2f>::iterator it=edgelist_temp.begin(); it!=edgelist_temp.end(); it++) { if (!edge_unqie.empty() && *it == edge_unqie.back()) { // find duplicated with other fid unsigned int fid = (*it).fid(0); assert(fid != -1); edge_unqie.back().set_fid2(fid); } else edge_unqie.push_back(*it); } edgelist_temp.clear(); vector<edge2f> edge_array; _num_edge = (unsigned int)edge_unqie.size(); _edges = new edge2f[_num_edge]; _edg_boxes = new BOX[_num_edge]; unsigned int t=0; for (list<edge2f>::iterator it=edge_unqie.begin(); it != edge_unqie.end(); it++) { _edges[t++] = *it; edge_array.push_back(*it); } // copy over temp list to static array _num_tri = (unsigned int)trilist_temp.size(); cout << "Allocating " << _num_tri*sizeof(tri3f) << " bytes of storage for triangles." << endl; _tris = new tri3f[_num_tri]; _tri_nrms = new vec3f[_num_tri]; _old_tri_nrms = NULL; _tri_edges = new tri3e[_num_tri]; _fac_boxes = new BOX[_num_tri]; _tri_flags = new char[_num_tri]; vector <edge2f>::iterator first = edge_array.begin(); vector <edge2f>::iterator last = edge_array.end(); for (t = 0; t < _num_tri; t++) { _tris[t] = trilist_temp[t]; vector <edge2f>::iterator it1 = lower_bound(first, last, edge2f(_tris[t].id0(), _tris[t].id1(), 0)); vector <edge2f>::iterator it2 = lower_bound(first, last, edge2f(_tris[t].id1(), _tris[t].id2(), 0)); vector <edge2f>::iterator it3 = lower_bound(first, last, edge2f(_tris[t].id2(), _tris[t].id0(), 0)); _tri_edges[t].set(it1-first, it2-first, it3-first); } cout << "Edge # = " << _num_edge << endl; cout << "Tri # = " << _num_tri << endl; } else // otherwise: skip all further NULL; } // PLY parsing ended, clean up vertex buffer and close the file // fclose(fp); ply_close(ply); } UpdateTriNorm(); for (unsigned t = 0; t < _num_tri; t++) for (int i=0; i<3; i++) { unsigned int vid = _tris[t].id(i); _vtx_fids[vid].push_back(t); } BufferAdjacent(); }
float CGameStateRecorder::RenderInfo(float y, bool bRecording) { float retY = 0; IRenderer *pRenderer = gEnv->pRenderer; if (bRecording) { float fColor[4] = {1,0,0,1}; pRenderer->Draw2dLabel( 1,y+retY, 1.3f, fColor,false," Recording game state"); retY +=15; } else { const float xp = 300; const float xr = 400; const float xri = 600; float fColor[4] = {0,1,0,1}; float fColorWarning[4] = {1,1,0,1}; const char* actorName = m_demo_actorInfo->GetString(); CActor *pActor = GetActorOfName(actorName); if(pActor) { pRenderer->Draw2dLabel( 1,y+retY, 1.3f, fColor,false," Game state - Actor: %s --------------------------------------------------",pActor->GetEntity()? pActor->GetEntity()->GetName(): "(no entity)"); retY +=15; if(m_itSingleActorGameState != m_GameStates.end() && pActor->GetEntity() && m_itSingleActorGameState->first == pActor->GetEntity()->GetId()) { ICVar *pVar = gEnv->pConsole->GetCVar( "demo_force_game_state" ); if(pVar) { int demo_forceGameState = pVar->GetIVal(); if(demo_forceGameState) { pRenderer->Draw2dLabel( 1,y+retY, 1.3f, fColor,false,demo_forceGameState==1 ? " Override mode = (health, suit energy)" : " Override mode = (all)"); retY +=15; } } pRenderer->Draw2dLabel( xp,y+retY, 1.3f, fColor,false,"Current"); pRenderer->Draw2dLabel( xr,y+retY, 1.3f, fColor,false,"Recorded"); retY +=15; SActorGameState& gstate = m_itSingleActorGameState->second; float recordedHealth = (float)gstate.health; float health = (float)pActor->GetHealth(); bool bError = CHECK_MISMATCH(health, recordedHealth,10); pRenderer->Draw2dLabel( 1,y+retY, 1.3f, fColor,false," Health:"); pRenderer->Draw2dLabel( xp,y+retY, 1.3f, bError? fColorWarning : fColor, false,"%d",(int)health); pRenderer->Draw2dLabel( xr,y+retY, 1.3f, bError? fColorWarning : fColor, false,"%d",(int)recordedHealth); retY +=15; // items pRenderer->Draw2dLabel( 1,y+retY, 1.3f, fColor,false," Inventory ---------------------------------------------------------------------------------------"); retY +=15; pRenderer->Draw2dLabel( xp,y+retY, 1.3f, fColor,false,"Current"); pRenderer->Draw2dLabel( xri,y+retY, 1.3f, fColor,false,"Recorded"); retY +=15; CInventory *pInventory = (CInventory*)(pActor->GetInventory()); if(pInventory) { int nInvItems = pInventory->GetCount(); TItemContainer& Items = gstate.Items; int nRecItems = Items.size(); int maxItems = max(nRecItems,nInvItems); int i=0; EntityId curSelectedId = pActor->GetCurrentItemId(); TItemName curSelClass = GetItemName(curSelectedId); bool bSelectedError = !equal_strings(gstate.itemSelected,curSelClass); for(; i< nInvItems; i++) { IItem *pItem = g_pGame->GetIGameFramework()->GetIItemSystem()->GetItem(pInventory->GetItem(i)); if(pItem) { TItemName szItemName = GetItemName(pItem->GetEntityId()); TItemContainer::iterator it = Items.find(szItemName); bError = it==Items.end(); pRenderer->Draw2dLabel( 1,y+retY, 1.3f, fColor,false," %2d)",i+1); EntityId curId = pItem->GetEntityId(); TItemName curClass = GetItemName(curId); if(equal_strings(curClass,curSelClass) ) pRenderer->Draw2dLabel( xp-16,y+retY, 1.3f,bSelectedError ? fColorWarning:fColor, false, "[]"); if(equal_strings(szItemName, gstate.itemSelected)) pRenderer->Draw2dLabel( xri-16,y+retY, 1.3f,bSelectedError ? fColorWarning:fColor, false, "[]"); char itemName[32]; const char* originalItemName = pItem->GetEntity() ? pItem->GetEntity()->GetName():"(null)"; int length = strlen(originalItemName); length = min(length,31); strncpy(itemName,originalItemName,length); itemName[length]=0; pRenderer->Draw2dLabel( 1,y+retY, 1.3f, bError? fColorWarning : fColor, false," %s",itemName); if(bError) pRenderer->Draw2dLabel( xp,y+retY, 1.3f, fColorWarning, false, "Missing"); else { SItemProperties& recItem = it->second; CWeapon *pWeapon = (CWeapon*)(pItem->GetIWeapon()); IEntityClass* pItemClass = pItem->GetEntity()->GetClass(); if(pItemClass && !strcmpi(pItemClass->GetName(),"binoculars")) pWeapon = NULL; // no fire mode or ammo recorded for binocular (which is a weapon) if(pWeapon) { int idx = 0; // ammo float xa = 0; for(SWeaponAmmo weaponAmmo = pWeapon->GetFirstAmmo(); weaponAmmo.pAmmoClass ; weaponAmmo = pWeapon->GetNextAmmo()) { int ammoCount = weaponAmmo.count; const char* ammoClass; if(weaponAmmo.pAmmoClass && (ammoClass = weaponAmmo.pAmmoClass->GetName())) { TAmmoContainer::iterator it = recItem.Ammo.find(ammoClass); if(it!=recItem.Ammo.end()) { int recAmmoCount = recItem.Ammo[ammoClass]; bool bError2 = ammoCount!=recAmmoCount; pRenderer->Draw2dLabel( xp+xa,y+retY, 1.3f, bError2? fColorWarning : fColor, false,"Am%d:%d",idx,ammoCount); pRenderer->Draw2dLabel( xri+xa,y+retY, 1.3f, bError2? fColorWarning : fColor, false,"Am%d:%d",idx,recAmmoCount); xa += 50; ++idx; if(idx%5 ==0) { xa=0; retY+=15; } } } } // current fire mode int curFireModeIdx = pWeapon->GetCurrentFireMode(); int recFireModeIdx = recItem.fireMode; bool bError3 = curFireModeIdx!= recFireModeIdx; pRenderer->Draw2dLabel( xp+xa,y+retY, 1.3f, bError3? fColorWarning : fColor, false,"FMode:%d",curFireModeIdx); pRenderer->Draw2dLabel( xri+xa,y+retY, 1.3f, bError3? fColorWarning : fColor, false,"FMode:%d",recFireModeIdx); } else { pRenderer->Draw2dLabel( xp,y+retY, 1.3f, fColor, false, "Ok"); } } } retY +=15; } /// Accessories int nInvAccessories = pInventory->GetAccessoryCount(); TAccessoryContainer& Accessories = gstate.Accessories; int nRecAccessories = Accessories.size(); if(nRecAccessories) { pRenderer->Draw2dLabel( 1,y+retY, 1.3f, bError? fColorWarning : fColor, false," Accessories"); retY +=15; } for(int j=0 ; j< nInvAccessories; j++,i++) { const char* accessory = pInventory->GetAccessory(j); if(accessory && strlen(accessory)) { IEntityClass* pClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(accessory); if(pClass) { TItemName szItemName = pClass->GetName(); TAccessoryContainer::iterator it = Accessories.find(szItemName); bError = it==Accessories.end(); pRenderer->Draw2dLabel( 1,y+retY, 1.3f, fColor,false," %2d)",i+1); char itemName[32]; int length = strlen(accessory); length = min(length,31); strncpy(itemName,accessory,length); itemName[length]=0; pRenderer->Draw2dLabel( 1,y+retY, 1.3f, bError? fColorWarning : fColor, false," %s",itemName); if(bError) pRenderer->Draw2dLabel( xp,y+retY, 1.3f, fColorWarning, false, "Missing"); else pRenderer->Draw2dLabel( xp,y+retY, 1.3f, fColor, false, "Ok"); retY +=15; } } } /// Ammo Mags TAmmoContainer& Ammo = gstate.AmmoMags; int nRecAmmo = Ammo.size(); int nInvAmmo = pInventory->GetAmmoPackCount(); if(nInvAmmo) { pRenderer->Draw2dLabel( 1,y+retY, 1.3f, bError? fColorWarning : fColor, false," Ammo Packs"); retY +=15; } pInventory->AmmoIteratorFirst(); for(int j=0 ; !pInventory->AmmoIteratorEnd(); j++, pInventory->AmmoIteratorNext()) { TAmmoContainer& Mags = gstate.AmmoMags; const IEntityClass* pAmmoClass = pInventory->AmmoIteratorGetClass(); if(pAmmoClass) { int invAmmoCount = pInventory->AmmoIteratorGetCount(); const char* ammoClassName = pAmmoClass->GetName(); bool bNotFound = Mags.find(ammoClassName) == Mags.end(); int recAmmoCount = bNotFound ? 0 :Mags[ammoClassName]; bool bError = bNotFound || invAmmoCount!= recAmmoCount; pRenderer->Draw2dLabel( 1,y+retY, 1.3f, bError? fColorWarning : fColor, false," %s:",ammoClassName); pRenderer->Draw2dLabel( xp,y+retY, 1.3f, bError? fColorWarning : fColor, false,"%d",invAmmoCount); if(bNotFound) pRenderer->Draw2dLabel( xr,y+retY, 1.3f, fColorWarning, false,"NotRecd"); else pRenderer->Draw2dLabel( xr,y+retY, 1.3f, bError? fColorWarning : fColor, false,"%d",recAmmoCount); retY +=15; } } } } else // m_itSingleActorGameState != m_GameStates.end() { pRenderer->Draw2dLabel( 1,y+retY, 1.3f, fColor,false, "<<Not Recorded>>"); retY +=15; } } else // pActor { pRenderer->Draw2dLabel( 1,y+retY, 1.3f, fColor,false, "<<Actor %s not in the map>>",actorName ? actorName:"(no name)"); retY +=15; } } return retY; }
LOD_Decimation_InfoPtr NewVertsFromFile( char * file_name, MT_Vector3 &min, MT_Vector3 &max ) { min = MT_Vector3(MT_INFINITY,MT_INFINITY,MT_INFINITY); max = MT_Vector3(-MT_INFINITY,-MT_INFINITY,-MT_INFINITY); PlyProperty vert_props[] = { /* list of property information for a vertex */ {"x", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,x), 0, 0, 0, 0}, {"y", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,y), 0, 0, 0, 0}, {"z", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,z), 0, 0, 0, 0}, }; PlyProperty face_props[] = { /* list of property information for a vertex */ {"intensity", PLY_UCHAR, PLY_UCHAR, offsetof(LoadFace,intensity), 0, 0, 0, 0}, {"vertex_indices", PLY_INT, PLY_INT, offsetof(LoadFace,verts), 1, PLY_UCHAR, PLY_UCHAR, offsetof(LoadFace,nverts)}, }; #if 0 MEM_SmartPtr<std::vector<float> > verts = new std::vector<float>; MEM_SmartPtr<std::vector<float> > vertex_normals = new std::vector<float>; MEM_SmartPtr<std::vector<int> > faces = new std::vector<int>; #else std::vector<float>* verts = new std::vector<float>; std::vector<float>* vertex_normals = new std::vector<float>; std::vector<int> * faces = new std::vector<int>; #endif int i,j; PlyFile *ply; int nelems; char **elist; int file_type; float version; int nprops; int num_elems; PlyProperty **plist; char *elem_name; LoadVertex load_vertex; LoadFace load_face; /* open a PLY file for reading */ ply = ply_open_for_reading(file_name, &nelems, &elist, &file_type, &version); if (ply == NULL) return NULL; /* go through each kind of element that we learned is in the file */ /* and read them */ for (i = 0; i < nelems; i++) { /* get the description of the first element */ elem_name = elist[i]; plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); /* print the name of the element, for debugging */ /* if we're on vertex elements, read them in */ if (equal_strings ("vertex", elem_name)) { /* set up for getting vertex elements */ ply_get_property (ply, elem_name, &vert_props[0]); ply_get_property (ply, elem_name, &vert_props[1]); ply_get_property (ply, elem_name, &vert_props[2]); // make some memory for the vertices verts->reserve(num_elems); /* grab all the vertex elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ ply_get_element (ply, (void *)&load_vertex); // pass the vertex into the mesh builder. if (load_vertex.x < min.x()) { min.x() = load_vertex.x; } else if (load_vertex.x > max.x()) { max.x()= load_vertex.x; } if (load_vertex.y < min.y()) { min.y() = load_vertex.y; } else if (load_vertex.y > max.y()) { max.y()= load_vertex.y; } if (load_vertex.z < min.z()) { min.z() = load_vertex.z; } else if (load_vertex.z > max.z()) { max.z()= load_vertex.z; } verts->push_back(load_vertex.x); verts->push_back(load_vertex.y); verts->push_back(load_vertex.z); vertex_normals->push_back(1.0f); vertex_normals->push_back(0.0f); vertex_normals->push_back(0.0f); } } /* if we're on face elements, read them in */ if (equal_strings ("face", elem_name)) { /* set up for getting face elements */ // ply_get_property (ply, elem_name, &face_props[0]); ply_get_property (ply, elem_name, &face_props[1]); /* grab all the face elements */ for (j = 0; j < num_elems; j++) { ply_get_element (ply, (void *)&load_face); faces->push_back(load_face.verts[0]); faces->push_back(load_face.verts[1]); faces->push_back(load_face.verts[2]); // free up the memory this pile of shit used to allocate the polygon's vertices free (load_face.verts); } } } /* close the PLY file */ ply_close (ply); LOD_Decimation_InfoPtr output = new LOD_Decimation_Info; output->vertex_buffer = verts->begin(); output->vertex_num = verts->size()/3; output->triangle_index_buffer = faces->begin(); output->face_num = faces->size()/3; output->intern = NULL; output->vertex_normal_buffer = vertex_normals->begin(); // memory leaks 'r' us #if 0 verts.Release(); vertex_normals.Release(); faces.Release(); #endif return output; }
void PlyUtility::readPlyFile(char *filename) { int i,j; /* open a PLY file for reading */ ply = ply_open_for_reading(filename, &nelems, &elist, &file_type, &version); /* go through each kind of element that we learned is in the file */ /* and read them */ for (i = 0; i < nelems; i++) { /* get the description of the first element */ elem_name = elist[i]; plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); /* if we're on vertex elements, read them in */ if (equal_strings ("vertex", elem_name)) { vertexCount=num_elems; /* create a vertex list to hold all the vertices */ vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems); /* set up for getting vertex elements */ ply_get_property (ply, elem_name, &vert_props[0]); ply_get_property (ply, elem_name, &vert_props[1]); ply_get_property (ply, elem_name, &vert_props[2]); /* grab all the vertex elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ vlist[j] = (Vertex *) malloc (sizeof (Vertex)); ply_get_element (ply, (void *) vlist[j]); if(vx_max<vlist[j]->x)vx_max=vlist[j]->x; if(vy_max<vlist[j]->y)vy_max=vlist[j]->y; if(vz_max<vlist[j]->z)vz_max=vlist[j]->z; if(vx_min>vlist[j]->x)vx_min=vlist[j]->x; if(vy_min>vlist[j]->y)vy_min=vlist[j]->y; if(vz_min>vlist[j]->z)vz_min=vlist[j]->z; } } /* if we're on face elements, read them in */ if (equal_strings ("face", elem_name)) { faceCount=num_elems; /* create a list to hold all the face elements */ flist = (Face **) malloc (sizeof (Face *) * num_elems); /* set up for getting face elements */ ply_get_property (ply, elem_name, &face_props[0]); ply_get_property (ply, elem_name, &face_props[1]); /* grab all the face elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ flist[j] = (Face *) malloc (sizeof (Face)); ply_get_element (ply, (void *) flist[j]); } } } /* grab and print out the comments in the file */ comments = ply_get_comments (ply, &num_comments); /* grab and print out the object information */ obj_info = ply_get_obj_info (ply, &num_obj_info); /* close the PLY file */ ply_close (ply); }
read_test() { int i,j,k; PlyFile *ply; int nelems; char **elist; int file_type; float version; int nprops; int num_elems; PlyProperty **plist; Vertex **vlist; Face **flist; char *elem_name; int num_comments; char **comments; int num_obj_info; char **obj_info; /* open a PLY file for reading */ ply = ply_open_for_reading("test", &nelems, &elist, &file_type, &version); /* print what we found out about the file */ printf ("version %f\n", version); printf ("type %d\n", file_type); /* go through each kind of element that we learned is in the file */ /* and read them */ for (i = 0; i < nelems; i++) { /* get the description of the first element */ elem_name = elist[i]; plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); /* print the name of the element, for debugging */ printf ("element %s %d\n", elem_name, num_elems); /* if we're on vertex elements, read them in */ if (equal_strings ("vertex", elem_name)) { /* create a vertex list to hold all the vertices */ vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems); /* set up for getting vertex elements */ ply_get_property (ply, elem_name, &vert_props[0]); ply_get_property (ply, elem_name, &vert_props[1]); ply_get_property (ply, elem_name, &vert_props[2]); /* grab all the vertex elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ vlist[j] = (Vertex *) malloc (sizeof (Vertex)); ply_get_element (ply, (void *) vlist[j]); /* print out vertex x,y,z for debugging */ printf ("vertex: %g %g %g\n", vlist[j]->x, vlist[j]->y, vlist[j]->z); } } /* if we're on face elements, read them in */ if (equal_strings ("face", elem_name)) { /* create a list to hold all the face elements */ flist = (Face **) malloc (sizeof (Face *) * num_elems); /* set up for getting face elements */ ply_get_property (ply, elem_name, &face_props[0]); ply_get_property (ply, elem_name, &face_props[1]); /* grab all the face elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ flist[j] = (Face *) malloc (sizeof (Face)); ply_get_element (ply, (void *) flist[j]); /* print out face info, for debugging */ printf ("face: %d, list = ", flist[j]->intensity); for (k = 0; k < flist[j]->nverts; k++) printf ("%d ", flist[j]->verts[k]); printf ("\n"); } } /* print out the properties we got, for debugging */ for (j = 0; j < nprops; j++) printf ("property %s\n", plist[j]->name); } /* grab and print out the comments in the file */ comments = ply_get_comments (ply, &num_comments); for (i = 0; i < num_comments; i++) printf ("comment = '%s'\n", comments[i]); /* grab and print out the object information */ obj_info = ply_get_obj_info (ply, &num_obj_info); for (i = 0; i < num_obj_info; i++) printf ("obj_info = '%s'\n", obj_info[i]); /* close the PLY file */ ply_close (ply); }
bool CPly::ImportPly(t_model& scene, const char* mfilename) { FILE *infile; char mefilename[FILENAMESIZE]; char buffer[250]; infile = fopen(mfilename, "r+"); if (infile == (FILE *) NULL) { wxLogInfo(_("File I/O Error: Cannot open file %s.\n"), mefilename); return false; } unsigned int sizeVertices=0; unsigned int sizeFaces=0; int nwords=0; char **words; char *orig_line; /* Lecture du fichier d'entete */ words = get_words (infile, &nwords, &orig_line); while (words) { //Pour chaque ligne if (nwords==3 && equal_strings (words[0], "element")) { //Le premier mot de la ligne est element if (equal_strings (words[1], "vertex")) //Nous indique le nombre de { sizeVertices=Convertor::ToInt(words[2]); }else if(equal_strings (words[1], "face")) { sizeFaces=Convertor::ToInt(words[2]); } } if (equal_strings (words[0], "end_header")) //Fin du bloc d'entete { break; } words = get_words (infile, &nwords, &orig_line); } //Lecture des vertices unsigned int idvec=0; char x[20]; char y[20]; char z[20]; scene.modelVertices.reserve(sizeVertices); while(idvec<sizeVertices && !feof(infile)) { vec3 newVec; fscanf(infile,"%20s %20s %20s\n",x,y,z); newVec[0]=Convertor::ToFloat(wxString(x)); newVec[1]=Convertor::ToFloat(wxString(y)); newVec[2]=Convertor::ToFloat(wxString(z)); scene.modelVertices.push_back(newVec); idvec++; } //Lectures des triangles unsigned int idtri=0; short dim; unsigned int a; unsigned int b; unsigned int c; unsigned int d; scene.modelFaces.reserve(sizeFaces); while(idtri<sizeFaces && !feof(infile)) { words = get_words (infile, &nwords, &orig_line); if(nwords>0) { dim=Convertor::ToInt(words[0]); if(dim==3) { t_face newFace; newFace.indicesSommets.set(Convertor::ToInt(words[1]),Convertor::ToInt(words[2]),Convertor::ToInt(words[3])); scene.modelFaces.push_back(newFace); }else if(dim==4) { t_face newFace; newFace.indicesSommets.set(Convertor::ToInt(words[1]),Convertor::ToInt(words[2]),Convertor::ToInt(words[3])); scene.modelFaces.push_back(newFace); newFace.indicesSommets.set(Convertor::ToInt(words[3]),Convertor::ToInt(words[4]),Convertor::ToInt(words[1])); scene.modelFaces.push_back(newFace); } /* fscanf(infile,"%5s %10s %10s %10s\n",dim,a,b,c); */ idtri++; } } fclose(infile); return true; }
/* reads in a Kernel from a file */ VIO_Status input_kernel(const char *kernel_file, Kernel * kernel) { int i, j; VIO_STR line; VIO_STR type_name; VIO_STR str; VIO_Real tmp_real; FILE *file; /* parameter checking */ if(kernel_file == NULL){ print_error("input_kernel(): passed NULL FILE.\n"); return (VIO_ERROR); } file = fopen(kernel_file, "r"); if(file == NULL){ print_error("input_kernel(): error opening Kernel file.\n"); return (VIO_ERROR); } /* okay read the header */ if(mni_input_string(file, &line, (char)0, (char)0) != VIO_OK){ delete_string(line); print_error("input_kernel(): could not read header in file.\n"); return (VIO_ERROR); } if(!equal_strings(line, KERNEL_FILE_HEADER)){ delete_string(line); print_error("input_kernel(): invalid header in file.\n"); return (VIO_ERROR); } /* --- read the type of Kernel */ if(mni_input_keyword_and_equal_sign(file, KERNEL_TYPE, FALSE) != VIO_OK){ return (VIO_ERROR); } if(mni_input_string(file, &type_name, (char)';', (char)0) != VIO_OK){ print_error("input_kernel(): missing kernel type.\n"); return (VIO_ERROR); } if(mni_skip_expected_character(file, (char)';') != VIO_OK){ return (VIO_ERROR); } if(!equal_strings(type_name, NORMAL_KERNEL)){ print_error("input_kernel(): invalid kernel type.\n"); delete_string(type_name); return (VIO_ERROR); } delete_string(type_name); /* --- read the next string */ if(mni_input_string(file, &str, (char)'=', (char)0) != VIO_OK) return (VIO_ERROR); if(!equal_strings(str, KERNEL)){ print_error("Expected %s =\n", KERNEL); delete_string(str); return (VIO_ERROR); } delete_string(str); if(mni_skip_expected_character(file, (char)'=') != VIO_OK){ return (VIO_ERROR); } /* now read the elements (lines) of the kernel */ if(verbose){ fprintf(stderr, "Reading [%s]", kernel_file); } for(i = 0; i < MAX_KERNEL_ELEMS; i++){ /* allocate a bit of memory */ SET_ARRAY_SIZE(kernel->K, kernel->nelems, kernel->nelems + 1, 10); ALLOC(kernel->K[i], KERNEL_DIMS + 1); /* get the 5 dimension vectors and the coefficient */ for(j = 0; j < 6; j++){ if(mni_input_real(file, &tmp_real) == VIO_OK){ kernel->K[i][j] = tmp_real; } else { /* check for end */ if(mni_skip_expected_character(file, (char)';') == VIO_OK){ kernel->nelems = i; if(verbose){ fprintf(stderr, " %dx%d Kernel elements read\n", i, kernel->nelems); } return (VIO_OK); } else { print_error("input_kernel(): error reading kernel [%d,%d]\n", i + 1, j + 1); return (VIO_ERROR); } } } kernel->nelems++; if(verbose){ fprintf(stderr, "."); fflush(stderr); } } /* SHOLDN'T BE REACHED */ print_error("input_kernel(): Glark! Something is amiss in the State of Kansas\n"); return (VIO_ERROR); }
void read_obj(const char* strName) { int i,j,k; FILE *fp; int nwords; char *comment_ptr; char *first_word; float x,y,z,w; /* read from standard input */ fp = fopen(strName,"r"); while (1) { comment_ptr = fetch_line (fp); if (comment_ptr == (char *) -1) /* end-of-file */ break; /* did we get a comment? */ if (comment_ptr) { make_comment (comment_ptr); continue; } /* if we get here, the line was not a comment */ nwords = fetch_words(); /* skip empty lines */ if (nwords == 0) continue; first_word = words[0]; if (equal_strings (first_word, "v")) { if (nwords < 4) { fprintf (stderr, "Too few coordinates: '%s'", str_orig); exit (-1); } #ifdef NEW_LOAD float3 pos; pos.x = atof (words[1]); pos.y = atof (words[2]); pos.z = atof (words[3]); vPosition.push_back(pos); #else x = atof (words[1]); y = atof (words[2]); z = atof (words[3]); if (nwords == 5) { w = atof (words[3]); has_w = 1; } else w = 1.0; make_vertex (x, y, z, w); #endif } else if (equal_strings (first_word, "vn")) { #ifdef NEW_LOAD float3 n; n.x = atof (words[1]); n.y = atof (words[2]); n.z = atof (words[3]); vNormal.push_back(n); has_normals = true; #endif } else if (equal_strings (first_word, "vt")) { #ifdef NEW_LOAD float2 t; t.x = atof (words[1]); t.y = atof (words[2]); vUV.push_back(t); texture_coords = true; #endif } else if (equal_strings (first_word, "f")) { #ifdef NEW_LOAD LoadFace(&words[1],nwords-1); #else make_face (&words[1], nwords-1); #endif } else { fprintf (stderr, "Do not recognize: '%s'\n", str_orig); } } nverts = vVertex.size(); nfaces = vIndex.size()/3; fclose(fp); }
bool TrisetObject::loadPLY(QString flnm) { m_position = Vec(0,0,0); m_scale = Vec(1,1,1); typedef struct Vertex { float x,y,z; float r,g,b; float nx,ny,nz; void *other_props; /* other properties */ } Vertex; typedef struct Face { unsigned char nverts; /* number of vertex indices in list */ int *verts; /* vertex index list */ void *other_props; /* other properties */ } Face; PlyProperty vert_props[] = { /* list of property information for a vertex */ {"x", Float32, Float32, offsetof(Vertex,x), 0, 0, 0, 0}, {"y", Float32, Float32, offsetof(Vertex,y), 0, 0, 0, 0}, {"z", Float32, Float32, offsetof(Vertex,z), 0, 0, 0, 0}, {"red", Float32, Float32, offsetof(Vertex,r), 0, 0, 0, 0}, {"green", Float32, Float32, offsetof(Vertex,g), 0, 0, 0, 0}, {"blue", Float32, Float32, offsetof(Vertex,b), 0, 0, 0, 0}, {"nx", Float32, Float32, offsetof(Vertex,nx), 0, 0, 0, 0}, {"ny", Float32, Float32, offsetof(Vertex,ny), 0, 0, 0, 0}, {"nz", Float32, Float32, offsetof(Vertex,nz), 0, 0, 0, 0}, }; PlyProperty face_props[] = { /* list of property information for a face */ {"vertex_indices", Int32, Int32, offsetof(Face,verts), 1, Uint8, Uint8, offsetof(Face,nverts)}, }; /*** the PLY object ***/ int nverts,nfaces; Vertex **vlist; Face **flist; PlyOtherProp *vert_other,*face_other; bool per_vertex_color = false; bool has_normals = false; int i,j; int elem_count; char *elem_name; PlyFile *in_ply; /*** Read in the original PLY object ***/ FILE *fp = fopen(flnm.toAscii().data(), "rb"); in_ply = read_ply (fp); for (i = 0; i < in_ply->num_elem_types; i++) { /* prepare to read the i'th list of elements */ elem_name = setup_element_read_ply (in_ply, i, &elem_count); if (equal_strings ("vertex", elem_name)) { /* create a vertex list to hold all the vertices */ vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count); nverts = elem_count; /* set up for getting vertex elements */ setup_property_ply (in_ply, &vert_props[0]); setup_property_ply (in_ply, &vert_props[1]); setup_property_ply (in_ply, &vert_props[2]); for (j = 0; j < in_ply->elems[i]->nprops; j++) { PlyProperty *prop; prop = in_ply->elems[i]->props[j]; if (equal_strings ("r", prop->name) || equal_strings ("red", prop->name)) { setup_property_ply (in_ply, &vert_props[3]); per_vertex_color = true; } if (equal_strings ("g", prop->name) || equal_strings ("green", prop->name)) { setup_property_ply (in_ply, &vert_props[4]); per_vertex_color = true; } if (equal_strings ("b", prop->name) || equal_strings ("blue", prop->name)) { setup_property_ply (in_ply, &vert_props[5]); per_vertex_color = true; } if (equal_strings ("nx", prop->name)) { setup_property_ply (in_ply, &vert_props[6]); has_normals = true; } if (equal_strings ("ny", prop->name)) { setup_property_ply (in_ply, &vert_props[7]); has_normals = true; } if (equal_strings ("nz", prop->name)) { setup_property_ply (in_ply, &vert_props[8]); has_normals = true; } } vert_other = get_other_properties_ply (in_ply, offsetof(Vertex,other_props)); /* grab all the vertex elements */ for (j = 0; j < elem_count; j++) { vlist[j] = (Vertex *) malloc (sizeof (Vertex)); get_element_ply (in_ply, (void *) vlist[j]); } } else if (equal_strings ("face", elem_name)) { /* create a list to hold all the face elements */ flist = (Face **) malloc (sizeof (Face *) * elem_count); nfaces = elem_count; /* set up for getting face elements */ setup_property_ply (in_ply, &face_props[0]); face_other = get_other_properties_ply (in_ply, offsetof(Face,other_props)); /* grab all the face elements */ for (j = 0; j < elem_count; j++) { flist[j] = (Face *) malloc (sizeof (Face)); get_element_ply (in_ply, (void *) flist[j]); } } else get_other_element_ply (in_ply); } close_ply (in_ply); free_ply (in_ply); if (Global::volumeType() == Global::DummyVolume) { float minX, maxX; float minY, maxY; float minZ, maxZ; minX = maxX = vlist[0]->x; minY = maxY = vlist[0]->y; minZ = maxZ = vlist[0]->z; for(int i=0; i<nverts; i++) { minX = qMin(minX, vlist[i]->x); maxX = qMax(maxX, vlist[i]->x); minY = qMin(minY, vlist[i]->y); maxY = qMax(maxY, vlist[i]->y); minZ = qMin(minZ, vlist[i]->z); maxZ = qMax(maxZ, vlist[i]->z); } minX = floor(minX); minY = floor(minY); minZ = floor(minZ); maxX = ceil(maxX); maxY = ceil(maxY); maxZ = ceil(maxZ); int h = maxX-minX+1; int w = maxY-minY+1; int d = maxZ-minZ+1; m_nX = d; m_nY = w; m_nZ = h; m_position = Vec(-minX, -minY, -minZ); // bool ok; // QString text = QInputDialog::getText(0, // "Please enter grid size", // "Grid Size", // QLineEdit::Normal, // QString("%1 %2 %3").\ // arg(d).arg(w).arg(h), // &ok); // if (!ok || text.isEmpty()) // { // QMessageBox::critical(0, "Cannot load PLY", "No grid"); // return false; // } // // int nx=0; // int ny=0; // int nz=0; // QStringList gs = text.split(" ", QString::SkipEmptyParts); // if (gs.count() > 0) nx = gs[0].toInt(); // if (gs.count() > 1) ny = gs[1].toInt(); // if (gs.count() > 2) nz = gs[2].toInt(); // if (nx > 0 && ny > 0 && nz > 0) // { // m_nX = nx; // m_nY = ny; // m_nZ = nz; // } // else // { // QMessageBox::critical(0, "Cannot load triset", "No grid"); // return false; // } // // if (d == m_nX && w == m_nY && h == m_nZ) // m_position = Vec(-minX, -minY, -minZ); } else { Vec dim = VolumeInformation::volumeInformation(0).dimensions; m_nZ = dim.x; m_nY = dim.y; m_nX = dim.z; } m_vertices.resize(nverts); for(int i=0; i<nverts; i++) m_vertices[i] = Vec(vlist[i]->x, vlist[i]->y, vlist[i]->z); m_normals.clear(); if (has_normals) { m_normals.resize(nverts); for(int i=0; i<nverts; i++) m_normals[i] = Vec(vlist[i]->nx, vlist[i]->ny, vlist[i]->nz); } m_vcolor.clear(); if (per_vertex_color) { m_vcolor.resize(nverts); for(int i=0; i<nverts; i++) m_vcolor[i] = Vec(vlist[i]->r/255.0f, vlist[i]->g/255.0f, vlist[i]->b/255.0f); } // only triangles considered int ntri=0; for (int i=0; i<nfaces; i++) { if (flist[i]->nverts >= 3) ntri++; } m_triangles.resize(3*ntri); int tri=0; for(int i=0; i<nfaces; i++) { if (flist[i]->nverts >= 3) { m_triangles[3*tri+0] = flist[i]->verts[0]; m_triangles[3*tri+1] = flist[i]->verts[1]; m_triangles[3*tri+2] = flist[i]->verts[2]; tri++; } } m_tvertices.resize(nverts); m_tnormals.resize(nverts); m_texValues.resize(nverts); Vec bmin = m_vertices[0]; Vec bmax = m_vertices[0]; for(int i=0; i<nverts; i++) { bmin = StaticFunctions::minVec(bmin, m_vertices[i]); bmax = StaticFunctions::maxVec(bmax, m_vertices[i]); } m_centroid = (bmin + bmax)/2; m_enclosingBox[0] = Vec(bmin.x, bmin.y, bmin.z); m_enclosingBox[1] = Vec(bmax.x, bmin.y, bmin.z); m_enclosingBox[2] = Vec(bmax.x, bmax.y, bmin.z); m_enclosingBox[3] = Vec(bmin.x, bmax.y, bmin.z); m_enclosingBox[4] = Vec(bmin.x, bmin.y, bmax.z); m_enclosingBox[5] = Vec(bmax.x, bmin.y, bmax.z); m_enclosingBox[6] = Vec(bmax.x, bmax.y, bmax.z); m_enclosingBox[7] = Vec(bmin.x, bmax.y, bmax.z); m_pointStep = qMax(1, nverts/50000); // QMessageBox::information(0, "", QString("%1 %2 %3\n%4 %5"). \ // arg(m_nX).arg(m_nY).arg(m_nZ). \ // arg(m_vertices.count()). \ // arg(m_triangles.count()/3)); m_fileName = flnm; return true; }
main(int argc, char *argv[]) { int i,j,k; PlyFile *in_ply; PlyFile *out_ply; int nelems; char **elist; int file_type; float version; int nprops; int num_elems; PlyProperty **plist; PlyProperty **plist_copy; PlyProperty *prop; char *elem_name; char *data; int offset; int *offset_list; int **lists; int *list_count; char **list_ptr; int verbose_flag = 0; #ifndef WRITE_ASCII #ifndef WRITE_BINARY fprintf (stderr, "'%s' compiled incorrectly.\n", argv[0]); fprintf (stderr, "Must have WRITE_ASCII or WRITE_BINARY defined during compile.\n"); exit (-1); #endif #endif /* maybe print out help message */ #ifdef WRITE_ASCII if (argc > 2 || (argc == 2 && !equal_strings (argv[1], "-p"))) { fprintf (stderr, "usage: %s [flags] <infile >outfile\n", argv[0]); fprintf (stderr, " -p (print element labels)\n"); exit (0); } #endif #ifdef WRITE_BINARY if (argc > 1) { fprintf (stderr, "usage: %s <infile >outfile\n", argv[0]); exit (0); } #endif if (argc == 2 && equal_strings (argv[1], "-p")) verbose_flag = 1; /* open the input and output files */ in_ply = read_ply (stdin); elist = get_element_list_ply (in_ply, &nelems); #ifdef WRITE_ASCII out_ply = write_ply (stdout, nelems, elist, PLY_ASCII); #endif #ifdef WRITE_BINARY out_ply = write_ply (stdout, nelems, elist, PLY_BINARY_BE); #endif /* allocate space for various lists that keep track of the elements */ plist_copy = (PlyProperty **) malloc (sizeof (PlyProperty *) * nelems); offset_list = (int *) malloc (sizeof (int) * nelems); lists = (int **) malloc (sizeof (int *) * nelems); list_count = (int *) malloc (sizeof (int)); /* go through each kind of element that we learned is in the file */ /* and come up with a list that has offsets for all properties */ for (i = 0; i < nelems; i++) { /* get the description of the element */ elem_name = elist[i]; plist = get_element_description_ply(in_ply, elem_name, &num_elems, &nprops); /* make room for a list of the lists in an element, so that */ /* we can later easily free up the space created by malloc'ed lists */ list_count[i] = 0; lists[i] = (int *) malloc (sizeof (int) * nprops); /* set up pointers into data */ offset = 0; for (j = 0; j < nprops; j++) { plist[j]->offset = offset; offset += 8; if (plist[j]->is_list) { plist[j]->count_offset = offset; lists[i][list_count[i]] = offset - 8; list_count[i]++; offset += 8; } } offset_list[i] = offset; /* copy the property list */ plist_copy[i] = (PlyProperty *) malloc (sizeof (PlyProperty) * nprops); prop = plist_copy[i]; for (j = 0; j < nprops; j++) { prop->name = plist[j]->name; prop->external_type = plist[j]->external_type; prop->internal_type = plist[j]->external_type; prop->offset = plist[j]->offset; prop->is_list = plist[j]->is_list; prop->count_external = plist[j]->count_external; prop->count_internal = plist[j]->count_external; prop->count_offset = plist[j]->count_offset; prop++; } element_layout_ply (out_ply, elem_name, num_elems, nprops, plist_copy[i]); } /* copy the comments and obj_info */ copy_comments_ply (out_ply, in_ply); copy_obj_info_ply (out_ply, in_ply); /* finish the header for the output file */ header_complete_ply (out_ply); /* copy all the element information */ for (i = 0; i < nelems; i++) { /* get the description of the element */ elem_name = elist[i]; plist = get_element_description_ply(in_ply, elem_name, &num_elems, &nprops); /* allocate space for an element */ data = (char *) malloc (8 * offset_list[i]); /* set up for getting elements */ get_element_setup_ply (in_ply, elem_name, nprops, plist_copy[i]); put_element_setup_ply (out_ply, elem_name); /* possibly print out name of element */ if (verbose_flag) fprintf (out_ply->fp, "%s:\n", elem_name); /* copy all the elements */ if (list_count[i]) { /* need to free the lists */ for (j = 0; j < num_elems; j++) { get_element_ply (in_ply, (void *) data); put_element_ply (out_ply, (void *) data); for (k = 0; k < list_count[i]; k++) { list_ptr = (char **) (data + lists[i][k]); free (*list_ptr); } } } else { /* no lists */ for (j = 0; j < num_elems; j++) { get_element_ply (in_ply, (void *) data); put_element_ply (out_ply, (void *) data); } } } /* close the PLY files */ close_ply (in_ply); close_ply (out_ply); }
// The test checks that default formatting work BOOST_AUTO_TEST_CASE_TEMPLATE(default_formatting, CharT, char_types) { typedef logging::record_view record_view; typedef logging::attribute_set attr_set; typedef std::basic_string< CharT > string; typedef logging::basic_formatting_ostream< CharT > osstream; typedef logging::basic_formatter< CharT > formatter; typedef test_data< CharT > data; attrs::constant< int > attr1(10); attrs::constant< double > attr2(5.5); attrs::constant< my_class > attr3(my_class(77)); attr_set set1; set1[data::attr1()] = attr1; set1[data::attr2()] = attr2; set1[data::attr3()] = attr3; record_view rec = make_record_view(set1); // Check for various modes of attribute value type specification { string str1, str2; osstream strm1(str1), strm2(str2); formatter f = expr::stream << expr::attr< int >(data::attr1()) << expr::attr< double >(data::attr2()); f(rec, strm1); strm2 << 10 << 5.5; BOOST_CHECK(equal_strings(strm1.str(), strm2.str())); } { string str1, str2; osstream strm1(str1), strm2(str2); formatter f = expr::stream << expr::attr< logging::numeric_types >(data::attr1()) << expr::attr< logging::numeric_types >(data::attr2()); f(rec, strm1); strm2 << 10 << 5.5; BOOST_CHECK(equal_strings(strm1.str(), strm2.str())); } // Check that custom types as attribute values are also supported { string str1, str2; osstream strm1(str1), strm2(str2); formatter f = expr::stream << expr::attr< my_class >(data::attr3()); f(rec, strm1); strm2 << my_class(77); BOOST_CHECK(equal_strings(strm1.str(), strm2.str())); } // Check how missing attribute values are handled { string str1; osstream strm1(str1); formatter f = expr::stream << expr::attr< int >(data::attr1()) << expr::attr< int >(data::attr4()).or_throw() << expr::attr< double >(data::attr2()); BOOST_CHECK_THROW(f(rec, strm1), logging::runtime_error); } { string str1, str2; osstream strm1(str1), strm2(str2); formatter f = expr::stream << expr::attr< int >(data::attr1()) << expr::attr< int >(data::attr4()) << expr::attr< double >(data::attr2()); f(rec, strm1); strm2 << 10 << 5.5; BOOST_CHECK(equal_strings(strm1.str(), strm2.str())); } }
void read_file(void) { int i,j; int elem_count; char *elem_name; PlyFile *in_ply; /*** Read in the original PLY object ***/ in_ply = read_ply (stdin); for (i = 0; i < in_ply->num_elem_types; i++) { /* prepare to read the i'th list of elements */ elem_name = setup_element_read_ply (in_ply, i, &elem_count); if (equal_strings ("vertex", elem_name)) { /* create a vertex list to hold all the vertices */ vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count); nverts = elem_count; /* set up for getting vertex elements */ setup_property_ply (in_ply, &vert_props[0]); setup_property_ply (in_ply, &vert_props[1]); setup_property_ply (in_ply, &vert_props[2]); for (j = 0; j < in_ply->elems[i]->nprops; j++) { PlyProperty *prop; prop = in_ply->elems[i]->props[j]; if (equal_strings ("r", prop->name)) { setup_property_ply (in_ply, &vert_props[3]); per_vertex_color = 1; } if (equal_strings ("g", prop->name)) { setup_property_ply (in_ply, &vert_props[4]); per_vertex_color = 1; } if (equal_strings ("b", prop->name)) { setup_property_ply (in_ply, &vert_props[5]); per_vertex_color = 1; } if (equal_strings ("nx", prop->name)) { setup_property_ply (in_ply, &vert_props[6]); has_normals = 1; } if (equal_strings ("ny", prop->name)) { setup_property_ply (in_ply, &vert_props[7]); has_normals = 1; } if (equal_strings ("nz", prop->name)) { setup_property_ply (in_ply, &vert_props[8]); has_normals = 1; } } vert_other = get_other_properties_ply (in_ply, offsetof(Vertex,other_props)); /* grab all the vertex elements */ for (j = 0; j < elem_count; j++) { vlist[j] = (Vertex *) malloc (sizeof (Vertex)); vlist[j]->r = 1; vlist[j]->g = 1; vlist[j]->b = 1; get_element_ply (in_ply, (void *) vlist[j]); } } else if (equal_strings ("face", elem_name)) { /* create a list to hold all the face elements */ flist = (Face **) malloc (sizeof (Face *) * elem_count); nfaces = elem_count; /* set up for getting face elements */ setup_property_ply (in_ply, &face_props[0]); face_other = get_other_properties_ply (in_ply, offsetof(Face,other_props)); /* grab all the face elements */ for (j = 0; j < elem_count; j++) { flist[j] = (Face *) malloc (sizeof (Face)); get_element_ply (in_ply, (void *) flist[j]); } } else get_other_element_ply (in_ply); } close_ply (in_ply); free_ply (in_ply); }
void read_file(FILE *inFile) { int i,j,k; PlyFile *ply; int nprops; int num_elems; PlyProperty **plist; char *elem_name; float version; int get_nx,get_ny,get_nz; /*** Read in the original PLY object ***/ ply = ply_read (inFile, &nelems, &elist); ply_get_info (ply, &version, &file_type); for (i = 0; i < nelems; i++) { /* get the description of the first element */ elem_name = elist[i]; plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); if (equal_strings ("vertex", elem_name)) { /* see if vertex holds any normal information */ get_nx = get_ny = get_nz = 0; for (j = 0; j < nprops; j++) { if (equal_strings ("nx", plist[j]->name)) get_nx = 1; if (equal_strings ("ny", plist[j]->name)) get_ny = 1; if (equal_strings ("nz", plist[j]->name)) get_nz = 1; } /* create a vertex list to hold all the vertices */ vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems); nverts = num_elems; /* set up for getting vertex elements */ ply_get_property (ply, elem_name, &vert_props[0]); ply_get_property (ply, elem_name, &vert_props[1]); ply_get_property (ply, elem_name, &vert_props[2]); if (get_nx) ply_get_property (ply, elem_name, &vert_props[3]); if (get_ny) ply_get_property (ply, elem_name, &vert_props[4]); if (get_nz) ply_get_property (ply, elem_name, &vert_props[5]); vert_other = ply_get_other_properties (ply, elem_name, offsetof(Vertex,other_props)); /* grab all the vertex elements */ for (j = 0; j < num_elems; j++) { vlist[j] = (Vertex *) malloc (sizeof (Vertex)); ply_get_element (ply, (void *) vlist[j]); } } else if (equal_strings ("face", elem_name)) { /* create a list to hold all the face elements */ flist = (Face **) malloc (sizeof (Face *) * num_elems); nfaces = num_elems; /* set up for getting face elements */ ply_get_property (ply, elem_name, &face_props[0]); face_other = ply_get_other_properties (ply, elem_name, offsetof(Face,other_props)); /* grab all the face elements */ for (j = 0; j < num_elems; j++) { flist[j] = (Face *) malloc (sizeof (Face)); ply_get_element (ply, (void *) flist[j]); } } else other_elements = ply_get_other_element (ply, elem_name, num_elems); } comments = ply_get_comments (ply, &num_comments); obj_info = ply_get_obj_info (ply, &num_obj_info); ply_close (ply); }
MEM_SmartPtr<BSP_TMesh> BSP_PlyLoader:: NewMeshFromFile( char * file_name, MT_Vector3 &min, MT_Vector3 &max ) { min = MT_Vector3(MT_INFINITY,MT_INFINITY,MT_INFINITY); max = MT_Vector3(-MT_INFINITY,-MT_INFINITY,-MT_INFINITY); PlyProperty vert_props[] = { /* list of property information for a vertex */ {"x", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,x), 0, 0, 0, 0}, {"y", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,y), 0, 0, 0, 0}, {"z", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,z), 0, 0, 0, 0}, }; PlyProperty face_props[] = { /* list of property information for a vertex */ {"vertex_indices", PLY_INT, PLY_INT, offsetof(LoadFace,verts), 1, PLY_UCHAR, PLY_UCHAR, offsetof(LoadFace,nverts)}, }; MEM_SmartPtr<BSP_TMesh> mesh = new BSP_TMesh; if (mesh == NULL) return NULL; int i,j; PlyFile *ply; int nelems; char **elist; int file_type; float version; int nprops; int num_elems; PlyProperty **plist; char *elem_name; LoadVertex load_vertex; LoadFace load_face; /* open a PLY file for reading */ ply = ply_open_for_reading( file_name, &nelems, &elist, &file_type, &version ); if (ply == NULL) return NULL; /* go through each kind of element that we learned is in the file */ /* and read them */ for (i = 0; i < nelems; i++) { /* get the description of the first element */ elem_name = elist[i]; plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); /* print the name of the element, for debugging */ /* if we're on vertex elements, read them in */ if (equal_strings ("vertex", elem_name)) { /* set up for getting vertex elements */ ply_get_property (ply, elem_name, &vert_props[0]); ply_get_property (ply, elem_name, &vert_props[1]); ply_get_property (ply, elem_name, &vert_props[2]); // make some memory for the vertices mesh->VertexSet().reserve(num_elems); /* grab all the vertex elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ ply_get_element (ply, (void *)&load_vertex); // pass the vertex into the mesh builder. if (load_vertex.x < min.x()) { min.x() = load_vertex.x; } else if (load_vertex.x > max.x()) { max.x()= load_vertex.x; } if (load_vertex.y < min.y()) { min.y() = load_vertex.y; } else if (load_vertex.y > max.y()) { max.y()= load_vertex.y; } if (load_vertex.z < min.z()) { min.z() = load_vertex.z; } else if (load_vertex.z > max.z()) { max.z()= load_vertex.z; } BSP_TVertex my_vert; my_vert.m_pos = MT_Vector3(load_vertex.x,load_vertex.y,load_vertex.z); mesh->VertexSet().push_back(my_vert); } } /* if we're on face elements, read them in */ if (equal_strings ("face", elem_name)) { /* set up for getting face elements */ ply_get_property (ply, elem_name, &face_props[0]); /* grab all the face elements */ for (j = 0; j < num_elems; j++) { ply_get_element (ply, (void *)&load_face); int v; for (v = 2; v< load_face.nverts; v++) { BSP_TFace f; f.m_verts[0] = load_face.verts[0]; f.m_verts[1] = load_face.verts[v-1]; f.m_verts[2] = load_face.verts[v]; mesh->BuildNormal(f); mesh->FaceSet().push_back(f); } // free up the memory this pile of shit used to allocate the polygon's vertices free (load_face.verts); } } } /* close the PLY file */ ply_close (ply); return mesh; }
void read_obj ( void ) /******************************************************************************/ /* Purpose: READ_OBJ reads in a Wavefront OBJ file. Author: Greg Turk */ { char *comment_ptr; char *first_word; FILE *fp; int i; int j; int k; int nwords; float w; float x; float y; float z; /* Read from standard input. */ fp = stdin; while (1) { comment_ptr = fetch_line ( fp ); /* End of file? */ if ( comment_ptr == ( char * ) -1 ) { break; } /* Did we actually get a comment? */ if ( comment_ptr ) { make_comment ( comment_ptr ); continue; } /* If we get here, the line was not a comment. */ nwords = fetch_words ( ); /* Skip empty lines. */ if ( nwords == 0 ) { continue; } first_word = words[0]; if (equal_strings (first_word, "v")) { if (nwords < 4) { fprintf (stderr, "Too few coordinates: '%s'", str_orig); exit (-1); } x = atof (words[1]); y = atof (words[2]); z = atof (words[3]); if (nwords == 5) { w = atof (words[3]); has_w = 1; } else { w = 1.0; } make_vertex ( x, y, z, w ); } else if (equal_strings (first_word, "vn")) { } else if (equal_strings (first_word, "vt")) { } else if (equal_strings (first_word, "f")) { make_face (&words[1], nwords-1); } else { fprintf (stderr, "Do not recognize: '%s'\n", str_orig); } } return; }