void FileIO::read_lab(PointSet* pointSet, const std::string& file_name) {
	std::ifstream input(file_name.c_str()) ;
	if(input.fail()) {
		Logger::output(title()) << "could not open file\'" << file_name << "\'" << Logger::endl();
		return ;
	}

	std::string dumy;
	int num_points;
	input >> dumy >> num_points;
	Vertex** vertices = new Vertex*[num_points];
	for (int i=0; i<num_points; ++i) {
		float x, y, z, nx, ny, nz, r, g, b, a;
		input >> x >> y >> z >> nx >> ny >> nz >> r >> g >> b >> a;
		vertices[i] = new Vertex(x, y, z, nx, ny, nz);
		vertices[i]->set_color(Colorf(r, g, b, a));
	}

	pointSet->set_vertices(vertices, num_points);
	pointSet->set_normals_exist(true);

	//////////////////////////////////////////////////////////////////////////

	int num_groups = 0;
	input >> dumy >> num_groups;
	for (int i=0; i<num_groups; ++i) {
		VertexGroup* g = lab_read_group(input, vertices);

		if (g->nb_vertice() >0) {
			g->set_point_set(pointSet);
			pointSet->add_vertex_group(g);
		}
		else
			delete g;
	}
}


VertexGroup* FileIO::lab_read_group(std::ifstream& input, Vertex** vts) {
	std::string dumy;
	std::string label;
	input >> dumy >> label;

	float r, g, b, a;
	input >> dumy >> r >> g >> b >> a;
	Colorf color(r, g, b, a);

	int num_points;
	input >> dumy >> num_points;

	VertexGroup* grp = new VertexGroup;
	for (int i=0; i<num_points; ++i) {
		int idx;
		input >> idx;
		Vertex* v = vts[idx];
		grp->push_back(v);
	}

	String::to_lowercase(label);
	grp->set_label(label);
	grp->set_color(color);

	return grp;
}



bool FileIO::load_segmentation(const std::string& file_name) {
	std::ifstream input(file_name.c_str()) ;
	if(input.fail()) {
		Logger::output(title()) << "could not open file\'" << file_name << "\'" << Logger::endl();
		return ;
	}	
	
	int num = 0;
	std::string dummy;
	input >> dummy >> num;

	std::vector<Patch*>	patches(num);
	for (unsigned int i=0; i<patches.size(); ++i) {
		Patch* patch = new Patch(source_image_->width(), source_image_->height(), i);
		patches[i] = patch;

		int idx = -1;
		input >> dummy >> idx; 
		patch->set_index(idx);

		std::string lab = "unknown";
		input >> dummy >> lab;
		patch->set_label(lab);

		int r(0), g(0), b(0), a(0);
		input >> dummy >> r >> g >> b >> a;
		patch->set_color(Colori(r, g, b, a));

		int patch_size = 0;
		input >> dummy >> patch_size;
		int boundary_size = 0;
		input >> dummy >> boundary_size;

		//////////////////////////////////////////////////////////////////////////

		for (int j=0; j<patch_size; ++j)	{
			int v(0);
			input >> v;
			patch->push_back(v);
		}

		//////////////////////////////////////////////////////////////////////////

		std::set<int> boundary;
		for (int j=0; j<boundary_size; ++j)	{
			int v(0);
			input >> v;
			boundary.insert(v);
		}
		patch->set_boundary(boundary);
	}

	return (patches.size() > 0);
}
