Decimater.hh 11.6 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 43
	for (size_t i = 0; i < infos.size(); ++i) {
		res.append(infos[i]);
	}
	return res;
}

template <class Mesh>
Alexander Dielen's avatar
Alexander Dielen committed
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
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::BaseDecimaterT<Mesh> BaseDecimater;
	typedef OM::Decimater::DecimaterT<Mesh> Decimater;
69 70 71 72 73 74 75 76 77 78

	typedef typename ModProgMesh::Info Info;
	typedef std::vector<Info> InfoList;

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

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

Alexander Dielen's avatar
Alexander Dielen committed
79 80 81
	py::class_<Decimater>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
		.def("decimate", &Decimater::decimate, py::arg("n_collapses")=0)
82
		.def("decimate_to", &Decimater::decimate_to)
Alexander Dielen's avatar
Alexander Dielen committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
		.def("decimate_to_faces", &Decimater::decimate_to_faces,
			py::arg("n_vertices")=0, py::arg("n_faces")=0)

		.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)
118 119 120 121 122 123 124
		;

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

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

Alexander Dielen's avatar
Alexander Dielen committed
125 126
	py::class_<ModBase>(m, buffer)
		.def("name", &ModBase::name, py::return_value_policy::copy)
127 128 129 130 131 132 133 134 135 136 137 138 139 140
		.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
141 142
	py::class_<ModAspectRatio, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
143 144 145 146 147
		.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
148
	expose_module_handle<ModAspectRatioHandle>(m, buffer);
149 150 151 152 153 154

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

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

Alexander Dielen's avatar
Alexander Dielen committed
155 156
	py::class_<ModEdgeLength, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
157 158 159 160 161
		.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
162
	expose_module_handle<ModEdgeLengthHandle>(m, buffer);
163 164 165 166 167 168

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

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

Alexander Dielen's avatar
Alexander Dielen committed
169 170
	py::class_<ModHausdorff, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
171 172 173 174 175
		.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
176
	expose_module_handle<ModHausdorffHandle>(m, buffer);
177 178 179 180 181 182

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

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

Alexander Dielen's avatar
Alexander Dielen committed
183 184 185
	py::class_<ModIndependentSets, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
		;
186 187

	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModIndependentSetsHandle");
Alexander Dielen's avatar
Alexander Dielen committed
188
	expose_module_handle<ModIndependentSetsHandle>(m, buffer);
189 190 191 192 193 194

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

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

Alexander Dielen's avatar
Alexander Dielen committed
195 196
	py::class_<ModNormalDeviation, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
197 198 199 200 201
		.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
202
	expose_module_handle<ModNormalDeviationHandle>(m, buffer);
203 204 205 206 207 208

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

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

Alexander Dielen's avatar
Alexander Dielen committed
209 210
	py::class_<ModNormalFlipping, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
211 212 213 214 215
		.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
216
	expose_module_handle<ModNormalFlippingHandle>(m, buffer);
217 218 219 220

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

Alexander Dielen's avatar
Alexander Dielen committed
221 222 223
	snprintf(buffer, sizeof buffer, "%s%s", _name, "ModProgMeshInfo");

	py::class_<Info>(m, buffer)
224 225 226 227 228 229 230 231
		.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
232 233
	py::class_<ModProgMesh, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
234 235 236 237 238 239
		.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
240
	expose_module_handle<ModProgMeshHandle>(m, buffer);
241 242 243 244 245 246

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

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

Alexander Dielen's avatar
Alexander Dielen committed
247 248 249 250
	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)
251 252 253 254 255
		.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
256
	expose_module_handle<ModQuadricHandle>(m, buffer);
257 258 259 260 261 262

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

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

Alexander Dielen's avatar
Alexander Dielen committed
263 264
	py::class_<ModRoundness, ModBase>(m, buffer)
		.def(py::init<Mesh&>(), py::keep_alive<1,2>())
265
		.def("set_min_angle", &ModRoundness::set_min_angle)
Alexander Dielen's avatar
Alexander Dielen committed
266 267
		.def("set_min_roundness", &ModRoundness::set_min_roundness,
			py::arg("min_roundness"), py::arg("binary")=true)
268 269 270 271 272
		.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
273
	expose_module_handle<ModRoundnessHandle>(m, buffer);
274 275 276
}

#endif