Decimater.hh 15.4 KB
Newer Older
1 2 3
#ifndef OPENMESH_PYTHON_DECIMATER_HH
#define OPENMESH_PYTHON_DECIMATER_HH

Alexander Dielen's avatar
Alexander Dielen committed
4
#include "MeshTypes.hh"
5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <OpenMesh/Tools/Decimater/ModBaseT.hh>
#include <OpenMesh/Tools/Decimater/ModAspectRatioT.hh>
#include <OpenMesh/Tools/Decimater/ModEdgeLengthT.hh>
#include <OpenMesh/Tools/Decimater/ModHausdorffT.hh>
#include <OpenMesh/Tools/Decimater/ModIndependentSetsT.hh>
#include <OpenMesh/Tools/Decimater/ModNormalDeviationT.hh>
#include <OpenMesh/Tools/Decimater/ModNormalFlippingT.hh>
#include <OpenMesh/Tools/Decimater/ModProgMeshT.hh>
#include <OpenMesh/Tools/Decimater/ModQuadricT.hh>
#include <OpenMesh/Tools/Decimater/ModRoundnessT.hh>
#include <OpenMesh/Tools/Decimater/DecimaterT.hh>

#include <cstdio>

Alexander Dielen's avatar
Alexander Dielen committed
19
#include <pybind11/pybind11.h>
20

Alexander Dielen's avatar
Alexander Dielen committed
21 22
namespace py = pybind11;
namespace OM = OpenMesh;
23 24 25


template <class Handle>
Alexander Dielen's avatar
Alexander Dielen committed
26 27 28
void expose_module_handle(py::module& m, const char *_name) {
	py::class_<Handle>(m, _name)
		.def(py::init<>())
29 30 31 32 33
		.def("is_valid", &Handle::is_valid)
		;
}

template <class Module>
Alexander Dielen's avatar
Alexander Dielen committed
34
py::list infolist(Module& _self) {
35
	const typename Module::InfoList& infos = _self.infolist();
Alexander Dielen's avatar
Alexander Dielen committed
36
	py::list res;
37 38 39 40 41 42
	for (size_t i = 0; i < infos.size(); ++i) {
		res.append(infos[i]);
	}
	return res;
}

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
template <class Mesh>
void copy_locked_vertex_prop(Mesh& _mesh, const std::string& _name) {
	if (!_mesh.template py_has_property<OM::VertexHandle>(_name)) {
		const std::string msg = "Vertex property \"" + _name + "\" does not exist.";
		PyErr_SetString(PyExc_RuntimeError, msg.c_str());
		throw py::error_already_set();
	}
	for (auto vh : _mesh.vertices()) {
		const auto val = _mesh.template py_property<OM::VertexHandle, typename Mesh::VPropHandle>(_name, vh);
		// BaseDecimaterT ctor requests vertex status
		_mesh.status(vh).set_locked(py::cast<bool>(val));
	}
}

template <class Mesh>
void copy_feature_vertex_prop(Mesh& _mesh, const std::string& _name) {
	if (!_mesh.template py_has_property<OM::VertexHandle>(_name)) {
		const std::string msg = "Vertex property \"" + _name + "\" does not exist.";
		PyErr_SetString(PyExc_RuntimeError, msg.c_str());
		throw py::error_already_set();
	}
	for (auto vh : _mesh.vertices()) {
		const auto val = _mesh.template py_property<OM::VertexHandle, typename Mesh::VPropHandle>(_name, vh);
		// BaseDecimaterT ctor requests vertex status
		_mesh.status(vh).set_feature(py::cast<bool>(val));
	}
}

template <class Mesh>
void copy_feature_edge_prop(Mesh& _mesh, const std::string& _name) {
	if (!_mesh.template py_has_property<OM::EdgeHandle>(_name)) {
		const std::string msg = "Edge property \"" + _name + "\" does not exist.";
		PyErr_SetString(PyExc_RuntimeError, msg.c_str());
		throw py::error_already_set();
	}
	for (auto eh : _mesh.edges()) {
		const auto val = _mesh.template py_property<OM::EdgeHandle, typename Mesh::EPropHandle>(_name, eh);
		// BaseDecimaterT ctor requests edge status
		_mesh.status(eh).set_feature(py::cast<bool>(val));
	}
}

template <class Mesh>
void copy_status_props(Mesh& _mesh,
                       py::object _locked_vertex_propname,
                       py::object _feature_vertex_propname,
                       py::object _feature_edge_propname)
{
	if (!_locked_vertex_propname.is_none())
		copy_locked_vertex_prop(_mesh, py::cast<std::string>(_locked_vertex_propname));
	if (!_feature_vertex_propname.is_none())
		copy_feature_vertex_prop(_mesh, py::cast<std::string>(_feature_vertex_propname));
	if (!_feature_edge_propname.is_none())
		copy_feature_edge_prop(_mesh, py::cast<std::string>(_feature_edge_propname));
}

99
template <class Mesh>
Alexander Dielen's avatar
Alexander Dielen committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
void expose_decimater(py::module& m, const char *_name) {

	typedef OM::Decimater::ModBaseT<Mesh> ModBase;
	typedef OM::Decimater::ModAspectRatioT<Mesh> ModAspectRatio;
	typedef OM::Decimater::ModEdgeLengthT<Mesh> ModEdgeLength;
	typedef OM::Decimater::ModHausdorffT<Mesh> ModHausdorff;
	typedef OM::Decimater::ModIndependentSetsT<Mesh> ModIndependentSets;
	typedef OM::Decimater::ModNormalDeviationT<Mesh> ModNormalDeviation;
	typedef OM::Decimater::ModNormalFlippingT<Mesh> ModNormalFlipping;
	typedef OM::Decimater::ModProgMeshT<Mesh> ModProgMesh;
	typedef OM::Decimater::ModQuadricT<Mesh> ModQuadric;
	typedef OM::Decimater::ModRoundnessT<Mesh> ModRoundness;

	typedef OM::Decimater::ModHandleT<ModAspectRatio> ModAspectRatioHandle;
	typedef OM::Decimater::ModHandleT<ModEdgeLength> ModEdgeLengthHandle;
	typedef OM::Decimater::ModHandleT<ModHausdorff> ModHausdorffHandle;
	typedef OM::Decimater::ModHandleT<ModIndependentSets> ModIndependentSetsHandle;
	typedef OM::Decimater::ModHandleT<ModNormalDeviation> ModNormalDeviationHandle;
	typedef OM::Decimater::ModHandleT<ModNormalFlipping> ModNormalFlippingHandle;
	typedef OM::Decimater::ModHandleT<ModProgMesh> ModProgMeshHandle;
	typedef OM::Decimater::ModHandleT<ModQuadric> ModQuadricHandle;
	typedef OM::Decimater::ModHandleT<ModRoundness> ModRoundnessHandle;

	typedef OM::Decimater::DecimaterT<Mesh> Decimater;
124 125 126 127 128 129 130 131
	typedef typename ModProgMesh::Info Info;

	// Decimater
	// ----------------------------------------

	char buffer[64];
	snprintf(buffer, sizeof buffer, "%s%s", _name, "Decimater");

Alexander Dielen's avatar
Alexander Dielen committed
132 133
	py::class_<Decimater>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

		.def("decimate", [](Decimater& _self,
		                    size_t _n_collapses,
		                    py::object _locked_vertex_propname,
		                    py::object _feature_vertex_propname,
		                    py::object _feature_edge_propname)
			{
				copy_status_props(_self.mesh(), _locked_vertex_propname,
				                  _feature_vertex_propname, _feature_edge_propname);
				_self.decimate(_n_collapses);
			},
			py::arg("n_collapses")=0,
			py::arg("locked_vertex_propname")=py::none(),
			py::arg("feature_vertex_propname")=py::none(),
			py::arg("feature_edge_propname")=py::none())

		.def("decimate_to", [](Decimater& _self,
		                       size_t _n_vertices,
		                       py::object _locked_vertex_propname,
		                       py::object _feature_vertex_propname,
		                       py::object _feature_edge_propname)
			{
				copy_status_props(_self.mesh(), _locked_vertex_propname,
				                  _feature_vertex_propname, _feature_edge_propname);
				_self.decimate_to(_n_vertices);
			},
			py::arg("n_vertices"),
			py::arg("locked_vertex_propname")=py::none(),
			py::arg("feature_vertex_propname")=py::none(),
			py::arg("feature_edge_propname")=py::none())

		.def("decimate_to_faces", [](Decimater& _self,
		                             size_t _n_vertices,
		                             size_t _n_faces,
		                             py::object _locked_vertex_propname,
		                             py::object _feature_vertex_propname,
		                             py::object _feature_edge_propname)
			{
				copy_status_props(_self.mesh(), _locked_vertex_propname,
				                  _feature_vertex_propname, _feature_edge_propname);
				_self.decimate_to_faces(_n_vertices, _n_faces);
			},
			py::arg("n_vertices")=0,
			py::arg("n_faces")=0,
			py::arg("locked_vertex_propname")=py::none(),
			py::arg("feature_vertex_propname")=py::none(),
			py::arg("feature_edge_propname")=py::none())
Alexander Dielen's avatar
Alexander Dielen committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

		.def("initialize", [](Decimater& _self) { return _self.initialize(); })
		.def("is_initialized", [](Decimater& _self) { return _self.is_initialized(); })

		.def("add", [](Decimater& _self, ModAspectRatioHandle& _mod) { return _self.add(_mod); })
		.def("add", [](Decimater& _self, ModEdgeLengthHandle& _mod) { return _self.add(_mod); })
		.def("add", [](Decimater& _self, ModHausdorffHandle& _mod) { return _self.add(_mod); })
		.def("add", [](Decimater& _self, ModIndependentSetsHandle& _mod) { return _self.add(_mod); })
		.def("add", [](Decimater& _self, ModNormalDeviationHandle& _mod) { return _self.add(_mod); })
		.def("add", [](Decimater& _self, ModNormalFlippingHandle& _mod) { return _self.add(_mod); })
		.def("add", [](Decimater& _self, ModProgMeshHandle& _mod) { return _self.add(_mod); })
		.def("add", [](Decimater& _self, ModQuadricHandle& _mod) { return _self.add(_mod); })
		.def("add", [](Decimater& _self, ModRoundnessHandle& _mod) { return _self.add(_mod); })

		.def("remove", [](Decimater& _self, ModAspectRatioHandle& _mod) { return _self.remove(_mod); })
		.def("remove", [](Decimater& _self, ModEdgeLengthHandle& _mod) { return _self.remove(_mod); })
		.def("remove", [](Decimater& _self, ModHausdorffHandle& _mod) { return _self.remove(_mod); })
		.def("remove", [](Decimater& _self, ModIndependentSetsHandle& _mod) { return _self.remove(_mod); })
		.def("remove", [](Decimater& _self, ModNormalDeviationHandle& _mod) { return _self.remove(_mod); })
		.def("remove", [](Decimater& _self, ModNormalFlippingHandle& _mod) { return _self.remove(_mod); })
		.def("remove", [](Decimater& _self, ModProgMeshHandle& _mod) { return _self.remove(_mod); })
		.def("remove", [](Decimater& _self, ModQuadricHandle& _mod) { return _self.remove(_mod); })
		.def("remove", [](Decimater& _self, ModRoundnessHandle& _mod) { return _self.remove(_mod); })

		.def("module", [](Decimater& _self, ModAspectRatioHandle& _mod) -> ModAspectRatio& { return _self.module(_mod); }, py::return_value_policy::reference)
		.def("module", [](Decimater& _self, ModEdgeLengthHandle& _mod) -> ModEdgeLength& { return _self.module(_mod); }, py::return_value_policy::reference)
		.def("module", [](Decimater& _self, ModHausdorffHandle& _mod) -> ModHausdorff& { return _self.module(_mod); }, py::return_value_policy::reference)
		.def("module", [](Decimater& _self, ModIndependentSetsHandle& _mod) -> ModIndependentSets& { return _self.module(_mod); }, py::return_value_policy::reference)
		.def("module", [](Decimater& _self, ModNormalDeviationHandle& _mod) -> ModNormalDeviation& { return _self.module(_mod); }, py::return_value_policy::reference)
		.def("module", [](Decimater& _self, ModNormalFlippingHandle& _mod) -> ModNormalFlipping& { return _self.module(_mod); }, py::return_value_policy::reference)
		.def("module", [](Decimater& _self, ModProgMeshHandle& _mod) -> ModProgMesh& { return _self.module(_mod); }, py::return_value_policy::reference)
		.def("module", [](Decimater& _self, ModQuadricHandle& _mod) -> ModQuadric& { return _self.module(_mod); }, py::return_value_policy::reference)
		.def("module", [](Decimater& _self, ModRoundnessHandle& _mod) -> ModRoundness& { return _self.module(_mod); }, py::return_value_policy::reference)
214 215 216 217 218 219 220
		;

	// ModBase
	// ----------------------------------------

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModBase");

Alexander Dielen's avatar
Alexander Dielen committed
221 222
	py::class_<ModBase>(m, buffer)
		.def("name", &ModBase::name, py::return_value_policy::copy)
223 224 225 226 227 228 229 230 231 232 233 234 235 236
		.def("is_binary", &ModBase::is_binary)
		.def("set_binary", &ModBase::set_binary)
		.def("initialize", &ModBase::initialize) // TODO VIRTUAL
		.def("collapse_priority", &ModBase::collapse_priority) // TODO VIRTUAL
		.def("preprocess_collapse", &ModBase::preprocess_collapse) // TODO VIRTUAL
		.def("postprocess_collapse", &ModBase::postprocess_collapse) // TODO VIRTUAL
		.def("set_error_tolerance_factor", &ModBase::set_error_tolerance_factor) // TODO VIRTUAL
		;

	// ModAspectRatio
	// ----------------------------------------

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModAspectRatio");

Alexander Dielen's avatar
Alexander Dielen committed
237 238
	py::class_<ModAspectRatio, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
239 240 241 242 243
		.def("aspect_ratio", &ModAspectRatio::aspect_ratio)
		.def("set_aspect_ratio", &ModAspectRatio::set_aspect_ratio)
		;

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModAspectRatioHandle");
Alexander Dielen's avatar
Alexander Dielen committed
244
	expose_module_handle<ModAspectRatioHandle>(m, buffer);
245 246 247 248 249 250

	// ModEdgeLength
	// ----------------------------------------

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModEdgeLength");

Alexander Dielen's avatar
Alexander Dielen committed
251 252
	py::class_<ModEdgeLength, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
253 254 255 256 257
		.def("edge_length", &ModEdgeLength::edge_length)
		.def("set_edge_length", &ModEdgeLength::set_edge_length)
		;

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModEdgeLengthHandle");
Alexander Dielen's avatar
Alexander Dielen committed
258
	expose_module_handle<ModEdgeLengthHandle>(m, buffer);
259 260 261 262 263 264

	// ModHausdorff
	// ----------------------------------------

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModHausdorff");

Alexander Dielen's avatar
Alexander Dielen committed
265 266
	py::class_<ModHausdorff, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
267 268 269 270 271
		.def("tolerance", &ModHausdorff::tolerance)
		.def("set_tolerance", &ModHausdorff::set_tolerance)
		;

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModHausdorffHandle");
Alexander Dielen's avatar
Alexander Dielen committed
272
	expose_module_handle<ModHausdorffHandle>(m, buffer);
273 274 275 276 277 278

	// ModIndependentSets
	// ----------------------------------------

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModIndependentSets");

Alexander Dielen's avatar
Alexander Dielen committed
279 280 281
	py::class_<ModIndependentSets, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
		;
282 283

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModIndependentSetsHandle");
Alexander Dielen's avatar
Alexander Dielen committed
284
	expose_module_handle<ModIndependentSetsHandle>(m, buffer);
285 286 287 288 289 290

	// ModNormalDeviation
	// ----------------------------------------

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModNormalDeviation");

Alexander Dielen's avatar
Alexander Dielen committed
291 292
	py::class_<ModNormalDeviation, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
293 294 295 296 297
		.def("normal_deviation", &ModNormalDeviation::normal_deviation)
		.def("set_normal_deviation", &ModNormalDeviation::set_normal_deviation)
		;

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModNormalDeviationHandle");
Alexander Dielen's avatar
Alexander Dielen committed
298
	expose_module_handle<ModNormalDeviationHandle>(m, buffer);
299 300 301 302 303 304

	// ModNormalFlipping
	// ----------------------------------------

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModNormalFlipping");

Alexander Dielen's avatar
Alexander Dielen committed
305 306
	py::class_<ModNormalFlipping, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
307 308 309 310 311
		.def("max_normal_deviation", &ModNormalFlipping::max_normal_deviation)
		.def("set_max_normal_deviation", &ModNormalFlipping::set_max_normal_deviation)
		;

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModNormalFlippingHandle");
Alexander Dielen's avatar
Alexander Dielen committed
312
	expose_module_handle<ModNormalFlippingHandle>(m, buffer);
313 314 315 316

	// ModProgMesh
	// ----------------------------------------

Alexander Dielen's avatar
Alexander Dielen committed
317 318 319
	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModProgMeshInfo");

	py::class_<Info>(m, buffer)
320 321 322 323 324 325 326 327
		.def_readwrite("v0", &Info::v0)
		.def_readwrite("v1", &Info::v1)
		.def_readwrite("vl", &Info::vl)
		.def_readwrite("vr", &Info::vr)
		;

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModProgMesh");

Alexander Dielen's avatar
Alexander Dielen committed
328 329
	py::class_<ModProgMesh, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
330 331 332 333 334 335
		.def("pmi", &infolist<ModProgMesh>)
		.def("infolist", &infolist<ModProgMesh>)
		.def("write", &ModProgMesh::write)
		;

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModProgMeshHandle");
Alexander Dielen's avatar
Alexander Dielen committed
336
	expose_module_handle<ModProgMeshHandle>(m, buffer);
337 338 339 340 341 342

	// ModQuadric
	// ----------------------------------------

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModQuadric");

Alexander Dielen's avatar
Alexander Dielen committed
343 344 345 346
	py::class_<ModQuadric, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
		.def("set_max_err", &ModQuadric::set_max_err,
			py::arg("err"), py::arg("binary")=true)
347 348 349 350 351
		.def("unset_max_err", &ModQuadric::unset_max_err)
		.def("max_err", &ModQuadric::max_err)
		;

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModQuadricHandle");
Alexander Dielen's avatar
Alexander Dielen committed
352
	expose_module_handle<ModQuadricHandle>(m, buffer);
353 354 355 356 357 358

	// ModRoundness
	// ----------------------------------------

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModRoundness");

Alexander Dielen's avatar
Alexander Dielen committed
359 360
	py::class_<ModRoundness, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
361
		.def("set_min_angle", &ModRoundness::set_min_angle)
Alexander Dielen's avatar
Alexander Dielen committed
362 363
		.def("set_min_roundness", &ModRoundness::set_min_roundness,
			py::arg("min_roundness"), py::arg("binary")=true)
364 365 366 367 368
		.def("unset_min_roundness", &ModRoundness::unset_min_roundness)
		.def("roundness", &ModRoundness::roundness)
		;

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModRoundnessHandle");
Alexander Dielen's avatar
Alexander Dielen committed
369
	expose_module_handle<ModRoundnessHandle>(m, buffer);
370 371 372
}

#endif