Ejemplo n.º 1
0
bool load_frame( int frame_num, bool &stop ){

	int n_meshes = meshes.size();
	int n_obs = obstacles.size();

	// Winding order matters for whatever is reading in the abc file.
	bool ccw_output = false;

	std::string framestr = pad_leading_zeros(6,frame_num);

	// Load all deforming meshes
	for( int m=0; m<n_meshes; ++m ){
		std::string meshstr = pad_leading_zeros(2,m);
		std::string objfile = dir + framestr + '_' + meshstr + ".obj";

		// If the file doesn't exists, we have reached the end of the sim
		if( !file_exists(objfile) ){ 
			stop = true;
			break;
		}

		// Load the deforming mesh
		TriangleMesh *mesh = meshes[m].get();
		if( !meshio::load_obj( mesh, objfile, false, false, false ) ){
			std::cerr << "\n**arcsimeToAlembic Error: Failed to load " << objfile << "\n" << std::endl;
			return false;
		}
		mesh->need_normals();

		// Add it to the exporter
		exporter.add_frame( deform_handles[m], &mesh->vertices[0][0], mesh->vertices.size(),
			&mesh->faces[0][0], mesh->faces.size(), ccw_output );

	} // end loop meshes

	// Load all obstacle transforms
	for( int o=0; o<n_obs && !stop; ++o ){
		std::string meshstr = pad_leading_zeros(2,o);
		std::string xformfile = dir + framestr + "obs" + meshstr + ".txt";
		pugi::xml_document doc;
		pugi::xml_parse_result result = doc.load_file(xformfile.c_str());
		if( !result ){
			std::cerr << "\n**arcsimeToAlembic Error: Unable to load " << xformfile << std::endl;
			return false;
		}

		TriangleMesh *mesh = obstacles[o].get();
		Eigen::AlignedBox<float,3> aabb = obs_aabb[o];
		Vec3f obs_center = aabb.center();

		pugi::xml_node::iterator node_iter = doc.first_child();
		for( ; node_iter != doc.end(); node_iter++ ){
			pugi::xml_node curr_node = *node_iter;
			std::string name = curr_node.name();
			XForm<float> xf; xf.setIdentity();
			if( name == "rotate" ){
				float angle = curr_node.attribute("angle").as_float() * (180.f/M_PI);
				float x = curr_node.attribute("x").as_float();
				float y = curr_node.attribute("y").as_float();
				float z = curr_node.attribute("z").as_float();

				// Translate obs to origin before rotation
				XForm<float> xf0 = xform::make_trans<float>(-obs_center);
				XForm<float> xf1 = xform::make_rot<float>( angle, Vec3f(x,y,z) );
				XForm<float> xf2 = xform::make_trans<float>(obs_center);
				xf = xf2 * xf1 * xf0;
			}
			else if( name == "scale" ){
				float s = curr_node.attribute("value").as_float();
				xf = xform::make_scale<float>(s,s,s);
			}
			else if( name == "translate" ){
				float x = curr_node.attribute("x").as_float();
				float y = curr_node.attribute("y").as_float();
				float z = curr_node.attribute("z").as_float();
				xf = xform::make_trans<float>(x,y,z);
			}
			mesh->apply_xform(xf);
		} // end load xform

		// Add it to the exporter
		exporter.add_frame( obs_handles[o], &mesh->vertices[0][0], mesh->vertices.size(),
			&mesh->faces[0][0], mesh->faces.size(), ccw_output );

	} // end loop obstacles

	return true;
}