From a8293dda83eb527dd34b86ee2df7cbd5b88955e0 Mon Sep 17 00:00:00 2001
From: rahv <karsten.rink@ufz.de>
Date: Tue, 3 Feb 2015 10:58:01 +0100
Subject: [PATCH] added tool for triangulating polylines to create surfaces

---
 Applications/Utils/CMakeLists.txt             |   1 +
 Applications/Utils/GeoTools/CMakeLists.txt    |  21 +++
 .../Utils/GeoTools/TriangulatePolyline.cpp    | 123 ++++++++++++++++++
 3 files changed, 145 insertions(+)
 create mode 100644 Applications/Utils/GeoTools/CMakeLists.txt
 create mode 100644 Applications/Utils/GeoTools/TriangulatePolyline.cpp

diff --git a/Applications/Utils/CMakeLists.txt b/Applications/Utils/CMakeLists.txt
index 333890eabce..56386eb5b5c 100644
--- a/Applications/Utils/CMakeLists.txt
+++ b/Applications/Utils/CMakeLists.txt
@@ -1,4 +1,5 @@
 ADD_SUBDIRECTORY( FileConverter )
+ADD_SUBDIRECTORY( GeoTools )
 ADD_SUBDIRECTORY( MeshEdit )
 ADD_SUBDIRECTORY( SimpleMeshCreation )
 
diff --git a/Applications/Utils/GeoTools/CMakeLists.txt b/Applications/Utils/GeoTools/CMakeLists.txt
new file mode 100644
index 00000000000..3420b9ddb39
--- /dev/null
+++ b/Applications/Utils/GeoTools/CMakeLists.txt
@@ -0,0 +1,21 @@
+
+INCLUDE_DIRECTORIES(
+	${CMAKE_SOURCE_DIR}
+	${CMAKE_SOURCE_DIR}/BaseLib
+	${CMAKE_SOURCE_DIR}/GeoLib
+	${CMAKE_SOURCE_DIR}/FileIO
+	${CMAKE_SOURCE_DIR}/MathLib
+)
+
+IF(QT4_FOUND)
+ADD_EXECUTABLE( TriangulatePolyline TriangulatePolyline.cpp )
+TARGET_LINK_LIBRARIES( TriangulatePolyline
+	BaseLib
+	FileIO
+	MathLib
+	${QT_LIBRARIES}
+)
+ENDIF() # QT4_FOUND
+
+SET_TARGET_PROPERTIES(TriangulatePolyline PROPERTIES FOLDER Utilities)
+
diff --git a/Applications/Utils/GeoTools/TriangulatePolyline.cpp b/Applications/Utils/GeoTools/TriangulatePolyline.cpp
new file mode 100644
index 00000000000..d5e37e495d5
--- /dev/null
+++ b/Applications/Utils/GeoTools/TriangulatePolyline.cpp
@@ -0,0 +1,123 @@
+/**
+ * \file   TriangulatePolyline.h
+ * \author Karsten Rink
+ * \date   2015-02-02
+ * \brief  Utility for triangulating polylines.
+ *
+ * Copyright (c) 2012-2015, OpenGeoSys Community (http://www.opengeosys.org)
+ *            Distributed under a Modified BSD License.
+ *              See accompanying file LICENSE.txt or
+ *              http://www.opengeosys.org/LICENSE.txt
+ *
+ */
+
+#include <string>
+
+#include "logog/include/logog.hpp"
+#include "tclap/CmdLine.h"
+
+#include "BaseLib/BuildInfo.h"
+#include "BaseLib/LogogSimpleFormatter.h"
+#include "FileIO/XmlIO/QT/XmlGmlInterface.h"
+#include "GeoLib/AnalyticalGeometry.h"
+#include "GeoLib/GEOObjects.h"
+#include "GeoLib/Polyline.h"
+
+#include <QApplication>
+
+
+std::string output_question()
+{
+	WARN ("Given polyline is not closed. Close polyline now?");
+	WARN ("Enter (Y)es for closing the line or (N)o for abort and press ENTER.");
+	std::string input;
+	std::cin >> input;
+	return input;
+}
+
+int main(int argc, char *argv[])
+{
+	QApplication app(argc, argv, false);
+
+	LOGOG_INITIALIZE();
+	BaseLib::LogogSimpleFormatter *custom_format (new BaseLib::LogogSimpleFormatter);
+	logog::Cout *logogCout(new logog::Cout);
+	logogCout->SetFormatter(*custom_format);
+
+	TCLAP::CmdLine cmd("Triangulates the specified polyline in the given geometry file.", ' ', BaseLib::BuildInfo::git_describe);
+	TCLAP::ValueArg<std::string>  input_arg("i", "input",  "GML input file (*.gml)", true, "", "string");
+	TCLAP::ValueArg<std::string>   name_arg("n", "name",   "Name of polyline in given file", true, "", "string");
+	TCLAP::ValueArg<std::string> output_arg("o", "output", "GML output file (*.gml)", true, "", "string");
+	cmd.add( input_arg );
+	cmd.add( name_arg );
+	cmd.add( output_arg );
+	cmd.parse( argc, argv );
+
+	std::string const& file_name(input_arg.getValue());
+	std::string const& polyline_name(name_arg.getValue());
+
+	GeoLib::GEOObjects geo_objects;
+	FileIO::XmlGmlInterface xml(geo_objects);
+	if (!xml.readFile(file_name))
+	{
+		ERR ("Failed to load geometry file.");
+		return 1;
+	}
+
+	std::vector<std::string> geo_names;
+	geo_objects.getGeometryNames(geo_names);
+	GeoLib::PolylineVec const*const line_vec (geo_objects.getPolylineVecObj(geo_names[0]));
+	GeoLib::Polyline* line = const_cast<GeoLib::Polyline*>(line_vec->getElementByName(polyline_name));
+
+	// check if line exists
+	if (line == nullptr)
+	{
+		ERR ("No polyline found with name \"%s\"", polyline_name.c_str());
+		return 1;
+	}
+
+	// check if polyline can be triangulated (i.e. closed + coplanar)
+	if (!line->isCoplanar())
+		return 1;
+
+	if (!line->isClosed())
+	{
+		std::string input ("");
+		while (input != "y" && input != "Y" && input != "n" && input != "N")
+			input = output_question();
+		
+		if (input == "y" || input == "Y")
+		{
+			line->closePolyline();
+			INFO ("Polyline closed.");
+		}
+		else
+			return 1;
+	}
+
+		// create surface
+	INFO ("Triangulating surface...");
+	std::vector<GeoLib::Surface*> *new_sfc = new std::vector<GeoLib::Surface*>;
+	new_sfc->push_back(GeoLib::Surface::createSurface(*line));
+
+	GeoLib::SurfaceVec* sfc_vec (geo_objects.getSurfaceVecObj(geo_names[0]));
+	if (sfc_vec == nullptr)
+		geo_objects.addSurfaceVec(new_sfc, geo_names[0]);
+	else
+		geo_objects.appendSurfaceVec(*new_sfc, geo_names[0]);
+	std::string const surface_name (polyline_name + "_surface");
+	std::size_t id (0);
+	sfc_vec = geo_objects.getSurfaceVecObj(geo_names[0]);
+	sfc_vec->getElementIDByName(surface_name, id);
+	sfc_vec->setNameForElement(id, surface_name);
+	
+	// write new file
+	xml.setNameForExport(geo_names[0]);
+	xml.writeToFile(output_arg.getValue());
+	INFO ("...done.");
+
+	delete logogCout;
+	delete custom_format;
+	LOGOG_SHUTDOWN();
+	return 0;
+}
-- 
GitLab