Esempio n. 1
0
void GeoTreeView::mapGeometry()
{
    TreeItem* item = static_cast<GeoTreeModel*>(model())->getItem(
            this->selectionModel()->currentIndex());
    std::string geo_name (item->data(0).toString().toStdString());
    emit geometryMappingRequested(geo_name);
}
int main (int argc, char* argv[])
{
	LOGOG_INITIALIZE();
	logog::Cout* logog_cout (new logog::Cout);
	BaseLib::LogogSimpleFormatter *custom_format (new BaseLib::LogogSimpleFormatter);
	logog_cout->SetFormatter(*custom_format);

	TCLAP::CmdLine cmd("Add EMI data as a scalar cell array to a 2d mesh.", ' ', "0.1");

	// I/O params
	TCLAP::ValueArg<std::string> poly_out("o", "polydata-output-file",
	                                      "the name of the file the data will be written to", true,
	                                      "", "file name of polydata file");
	cmd.add(poly_out);
	TCLAP::ValueArg<std::string> csv_in("i", "csv-input-file",
	                                    "csv-file containing EMI data", true,
	                                    "", "name of the csv input file");
	cmd.add(csv_in);
	TCLAP::ValueArg<std::string> dem_in("s", "DEM-file",
	                                    "Surface DEM for mapping ERT data", false,
	                                    "", "file name of the Surface DEM");
	cmd.add(dem_in);
	cmd.parse(argc, argv);

	MeshLib::Mesh* mesh (nullptr);
	if (dem_in.isSet())
	{
		mesh = FileIO::VtuInterface::readVTUFile(dem_in.getValue());
		if (mesh == nullptr)
		{
			ERR ("Error reading mesh file.");
			return -2;
		}

		if (mesh->getDimension() != 2)
		{
			ERR ("This utility can handle only 2d meshes at this point.");
			delete mesh;
			return -3;
		}
		INFO("Surface mesh read: %d nodes, %d elements.", mesh->getNNodes(), mesh->getNElements());
	}

	GeoLib::GEOObjects geo_objects;
	FileIO::XmlGmlInterface xml(geo_objects);
	//std::vector<GeoLib::Polyline*> *lines = new std::vector<GeoLib::Polyline*>;
	std::array<char, 2> dipol = {{ 'H', 'V' }};
	std::array<char,3> const regions = {{'A', 'B', 'C'}};
	for (std::size_t j=0; j<dipol.size(); ++j)
	{
		std::vector<GeoLib::Point*> *points   = new std::vector<GeoLib::Point*>;
		for (std::size_t i=0; i<regions.size(); ++i)
		{
			//std::size_t const start_idx (points->size());
			getPointsFromFile(*points, csv_in.getValue(), dipol[j], regions[i]);
			//std::size_t const end_idx (points->size());
			//GeoLib::Polyline* line = new GeoLib::Polyline(*points);
			//for (std::size_t j=start_idx; j<end_idx; ++j)
			//	line->addPoint(j);
			//lines->push_back(line);
		}
		std::string geo_name (std::string("EMI Data ").append(1,dipol[j]));
		geo_objects.addPointVec(points, geo_name);
		//geo_objects.addPolylineVec(lines, geo_name);

		if (mesh != nullptr)
		{
			GeoMapper mapper(geo_objects, geo_name);
			mapper.mapOnMesh(mesh);
		}
		
		xml.setNameForExport(geo_name);
		std::string const output_name = poly_out.getValue() + "_" + dipol[j] + ".gml";
		xml.writeToFile(output_name);
		
		std::vector<double> emi;
		for (std::size_t i=0; i<regions.size(); ++i)
			getMeasurements(emi, csv_in.getValue(), dipol[j], regions[i]);
		writeMeasurementsToFile(emi, poly_out.getValue(), dipol[j]);
		std::for_each(points->begin(), points->end(), std::default_delete<GeoLib::Point>());
		delete points;
	}
	
	delete mesh;
	delete custom_format;
	delete logog_cout;
	LOGOG_SHUTDOWN();

	return 0;
}
int main (int argc, char* argv[])
{
	ApplicationsLib::LogogSetup logog_setup;

	TCLAP::CmdLine cmd(
		"Creates boundary conditions for mesh nodes along polylines."
		"The documentation is available at https://docs.opengeosys.org/docs/tools/model-preparation/create-boundary-conditions-along-a-polyline",
		' ',
		"0.1");
	TCLAP::ValueArg<bool> gml_arg("", "gml",
		"if switched on write found nodes to file in gml format", false, 0, "bool");
	cmd.add(gml_arg);

	TCLAP::ValueArg<std::string> output_base_fname("o", "output-base-file-name",
		"the base name of the file the output (geometry (gli) and boundary"\
		"condition (bc)) will be written to", true,
		"", "file name");
	cmd.add(output_base_fname);

	TCLAP::ValueArg<std::string> bc_type("t", "type",
		"the process type the boundary condition will be written for "\
		"currently LIQUID_FLOW (primary variable PRESSURE1) and "\
		"GROUNDWATER_FLOW (primary variable HEAD, default) are supported", true,
		"",
		"process type as string (LIQUID_FLOW or GROUNDWATER_FLOW (default))");
	cmd.add(bc_type);

	TCLAP::ValueArg<double> search_length_arg("s", "search-length",
		"The size of the search length. The default value is "
		"std::numeric_limits<double>::epsilon()", false,
		std::numeric_limits<double>::epsilon(), "floating point number");
	cmd.add(search_length_arg);

	TCLAP::ValueArg<std::string> geometry_fname("i", "input-geometry",
		"the name of the file containing the input geometry", true,
		"", "file name");
	cmd.add(geometry_fname);

	TCLAP::ValueArg<std::string> mesh_arg("m", "mesh-file",
		"the name of the file containing the mesh", true,
		"", "file name");
	cmd.add(mesh_arg);

	cmd.parse(argc, argv);

	// *** read mesh
	INFO("Reading mesh \"%s\" ... ", mesh_arg.getValue().c_str());
	MeshLib::Mesh * subsurface_mesh(FileIO::readMeshFromFile(mesh_arg.getValue()));
	INFO("done.");
	INFO("Extracting top surface of mesh \"%s\" ... ",
		mesh_arg.getValue().c_str());
	const MathLib::Vector3 dir(0,0,-1);
	double const angle(90);
	std::unique_ptr<MeshLib::Mesh> surface_mesh(
	    MeshLib::MeshSurfaceExtraction::getMeshSurface(*subsurface_mesh, dir,
	                                                   angle));
	INFO("done.");
	delete subsurface_mesh;
	subsurface_mesh = nullptr;

	// *** read geometry
	GeoLib::GEOObjects geometries;
	FileIO::readGeometryFromFile(geometry_fname.getValue(), geometries);

	std::string geo_name;
	{
		std::vector<std::string> geo_names;
		geometries.getGeometryNames(geo_names);
		geo_name = geo_names[0];
	}

	// *** check if the data is usable
	// *** get vector of polylines
	std::vector<GeoLib::Polyline*> const* plys(geometries.getPolylineVec(geo_name));
	if (!plys) {
		ERR("Could not get vector of polylines out of geometry \"%s\".",
			geo_name.c_str());
		return -1;
	}

	MeshGeoToolsLib::SearchLength search_length_strategy;
	if (search_length_arg.isSet()) {
		search_length_strategy =
			MeshGeoToolsLib::SearchLength(search_length_arg.getValue());
	}

	GeoLib::GEOObjects geometry_sets;
	MeshGeoToolsLib::MeshNodeSearcher mesh_searcher(*surface_mesh,
		search_length_strategy);
	for(std::size_t k(0); k<plys->size(); k++) {
		std::vector<std::size_t> ids
			(mesh_searcher.getMeshNodeIDsAlongPolyline(*((*plys)[k])));
		if (ids.empty())
			continue;
		std::string geo_name("Polyline-"+std::to_string(k));
		convertMeshNodesToGeometry(surface_mesh->getNodes(), ids, geo_name,
			geometry_sets);
	}

	// merge all together
	std::vector<std::string> geo_names;
	geometry_sets.getGeometryNames(geo_names);
	if (geo_names.empty()) {
		ERR("Did not find mesh nodes along polylines.");
		return -1;
	}

	std::string merge_name("AllMeshNodesAlongPolylines");
	if (geometry_sets.mergeGeometries(geo_names, merge_name) == 2)
		merge_name = geo_names[0];

	GeoLib::PointVec const* pnt_vec(geometry_sets.getPointVecObj(merge_name));
	std::vector<GeoLib::Point*> const* merged_pnts(pnt_vec->getVector());

	std::vector<GeoLib::Point> pnts_with_id;
	const std::size_t n_merged_pnts(merged_pnts->size());
	for(std::size_t k(0); k<n_merged_pnts; ++k) {
		pnts_with_id.emplace_back(*((*merged_pnts)[k]), k);
	}

	std::sort(pnts_with_id.begin(), pnts_with_id.end(),
		[](GeoLib::Point const& p0, GeoLib::Point const& p1)
			{ return p0 < p1; }
	);

	double const eps (std::numeric_limits<double>::epsilon());
	auto surface_pnts = std::unique_ptr<std::vector<GeoLib::Point*>>(
	    new std::vector<GeoLib::Point*>);
	std::map<std::string, std::size_t> *name_id_map(
		new std::map<std::string, std::size_t>
	);

	// insert first point
	surface_pnts->push_back(
		new GeoLib::Point(pnts_with_id[0], surface_pnts->size()));
	std::string element_name;
	pnt_vec->getNameOfElementByID(0, element_name);
	name_id_map->insert(
		std::pair<std::string, std::size_t>(element_name,0)
	);
	for (std::size_t k(1); k < n_merged_pnts; ++k) {
		const GeoLib::Point& p0 (pnts_with_id[k-1]);
		const GeoLib::Point& p1 (pnts_with_id[k]);
		if (std::abs (p0[0] - p1[0]) > eps || std::abs (p0[1] - p1[1]) > eps) {
			surface_pnts->push_back(new GeoLib::Point(pnts_with_id[k],
				surface_pnts->size()));
			std::string element_name;
			pnt_vec->getNameOfElementByID(k, element_name);
			name_id_map->insert(
				std::pair<std::string, std::size_t>(element_name,
				surface_pnts->size()-1)
			);
		}
	}

	std::string surface_name(BaseLib::dropFileExtension(mesh_arg.getValue())+"-MeshNodesAlongPolylines");
	geometry_sets.addPointVec(std::move(surface_pnts), surface_name, name_id_map, 1e-6);

	// write the BCs and the merged geometry set to file
	std::string const base_fname(
	    BaseLib::dropFileExtension(output_base_fname.getValue()));
	writeBCsAndGeometry(geometry_sets, surface_name, base_fname,
	                    bc_type.getValue(), gml_arg.getValue());
	return 0;
}
Esempio n. 4
0
TEST(FileIO, XmlGmlWriterReaderTest)
{
	// Writer test
	std::string test_data_file(BaseLib::BuildInfo::source_path + "/Tests/FileIO/xmlgmltestdata.gml");

	GeoLib::GEOObjects geo_objects;

	//setup test data
	std::string geo_name("TestData");

	{   // Create points.
		auto points = std::unique_ptr<std::vector<GeoLib::Point*>>(
		    new std::vector<GeoLib::Point*>(10));

		(*points)[0] = new GeoLib::Point(1, 1, 0);
		(*points)[1] = new GeoLib::Point(1, 1, 0);
		(*points)[2] = new GeoLib::Point(1, 2, 0);
		(*points)[3] = new GeoLib::Point(1, 3, 0);
		(*points)[4] = new GeoLib::Point(2, 1, 0);
		(*points)[5] = new GeoLib::Point(2, 2, 0);
		(*points)[6] = new GeoLib::Point(2, 3, 0);
		(*points)[7] = new GeoLib::Point(3, 1, 0);
		(*points)[8] = new GeoLib::Point(3, 2, 0);
		(*points)[9] = new GeoLib::Point(3, 3, 0);
		geo_objects.addPointVec(std::move(points), geo_name);
	}
	auto const points = geo_objects.getPointVec(geo_name);

	const std::vector<std::size_t> pnt_id_map (geo_objects.getPointVecObj(geo_name)->getIDMap());

	{   // Create polylines.
		auto lines = std::unique_ptr<std::vector<GeoLib::Polyline*>>(
		    new std::vector<GeoLib::Polyline*>(5));
		std::map<std::string, std::size_t>* ply_names =
		    new std::map<std::string, std::size_t>;
		(*lines)[0] = new GeoLib::Polyline(*points);
		(*lines)[0]->addPoint(pnt_id_map[0]);
		(*lines)[0]->addPoint(pnt_id_map[2]);
		(*lines)[0]->addPoint(pnt_id_map[3]);
		ply_names->insert(std::pair<std::string, std::size_t>("left", 0));
		(*lines)[1] = new GeoLib::Polyline(*points);
		(*lines)[1]->addPoint(pnt_id_map[4]);
		(*lines)[1]->addPoint(pnt_id_map[5]);
		(*lines)[1]->addPoint(pnt_id_map[6]);
		ply_names->insert(std::pair<std::string, std::size_t>("center", 1));
		(*lines)[2] = new GeoLib::Polyline(*points);
		(*lines)[2]->addPoint(pnt_id_map[1]);
		(*lines)[2]->addPoint(pnt_id_map[4]);
		(*lines)[3] = new GeoLib::Polyline(*points);
		(*lines)[3]->addPoint(pnt_id_map[4]);
		(*lines)[3]->addPoint(pnt_id_map[7]);
		(*lines)[4] = new GeoLib::Polyline(*points);
		(*lines)[4]->addPoint(pnt_id_map[7]);
		(*lines)[4]->addPoint(pnt_id_map[8]);
		(*lines)[4]->addPoint(pnt_id_map[9]);
		ply_names->insert(std::pair<std::string, std::size_t>("right", 4));
		geo_objects.addPolylineVec(std::move(lines), geo_name, ply_names);
	}

	{   // Create surfaces.
		auto sfcs = std::unique_ptr<std::vector<GeoLib::Surface*>>(
		    new std::vector<GeoLib::Surface*>(2));
		(*sfcs)[0] = new GeoLib::Surface(*points);
		(*sfcs)[0]->addTriangle(pnt_id_map[1], pnt_id_map[4], pnt_id_map[2]);
		(*sfcs)[0]->addTriangle(pnt_id_map[2], pnt_id_map[4], pnt_id_map[5]);
		(*sfcs)[0]->addTriangle(pnt_id_map[2], pnt_id_map[5], pnt_id_map[3]);
		(*sfcs)[0]->addTriangle(pnt_id_map[3], pnt_id_map[5], pnt_id_map[6]);
		(*sfcs)[1] = new GeoLib::Surface(*points);
		(*sfcs)[1]->addTriangle(pnt_id_map[4], pnt_id_map[7], pnt_id_map[9]);
		(*sfcs)[1]->addTriangle(pnt_id_map[4], pnt_id_map[9], pnt_id_map[6]);
		geo_objects.addSurfaceVec(std::move(sfcs), geo_name);
	}

	FileIO::XmlGmlInterface xml(geo_objects);
	xml.setNameForExport(geo_name);
	int result = xml.writeToFile(test_data_file);
	ASSERT_EQ(result, 1);

	// Reader test
	result = xml.readFile(QString::fromStdString(test_data_file));
	ASSERT_EQ(result, 1);

	const std::vector<GeoLib::Point*> *readerPoints = geo_objects.getPointVec(geo_name);
	const GeoLib::PolylineVec *line_vec = geo_objects.getPolylineVecObj(geo_name);
	const std::vector<GeoLib::Polyline*> *readerLines = geo_objects.getPolylineVec(geo_name);
	const std::vector<GeoLib::Surface*> *readerSfcs = geo_objects.getSurfaceVec(geo_name);
	ASSERT_EQ(9u, readerPoints->size());
	ASSERT_EQ(5u, readerLines->size());
	ASSERT_EQ(2u, readerSfcs->size());

	GeoLib::Point* pnt = (*readerPoints)[7];
	ASSERT_EQ(3.0, (*pnt)[0]);
	ASSERT_EQ(2.0, (*pnt)[1]);
	ASSERT_EQ(0.0, (*pnt)[2]);

	GeoLib::Polyline* line = (*readerLines)[4];
	ASSERT_EQ(3u, line->getNumberOfPoints());
	ASSERT_EQ(6u, line->getPointID(0));
	ASSERT_EQ(7u, line->getPointID(1));
	ASSERT_EQ(8u, line->getPointID(2));
	std::string line_name("");
	line_vec->getNameOfElementByID(4, line_name);
	ASSERT_EQ("right", line_name);

	GeoLib::Surface* sfc = (*readerSfcs)[1];
	ASSERT_EQ(2u, sfc->getNTriangles());
	const GeoLib::Triangle* tri = (*sfc)[1];
	ASSERT_EQ(3u, (*tri)[0]);
	ASSERT_EQ(8u, (*tri)[1]);
	ASSERT_EQ(5u, (*tri)[2]);

	boost::filesystem::remove(test_data_file);
	test_data_file += ".md5";
	boost::filesystem::remove(test_data_file);
}