MEPP2 Project
BooleanOperationsPlugin.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published
6 // by the Free Software Foundation; either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 
12 #pragma once
13 
14 #if(_MSC_VER >= 1400)
15 #ifndef _SCL_SECURE_NO_WARNINGS
16 #define _SCL_SECURE_NO_WARNINGS
17 #endif
18 #endif
19 
21 
22 #include <QStringList>
24 
25 #ifndef Q_MOC_RUN // MT : very important to avoid the error : ' Parse error at
26  // "BOOST_JOIN" ' -> (qt4 pb with boost)
29 
31 
34 
36 
37 // Polyhedron is used internally
39 
40 #ifdef FEVV_USE_CGAL
43 #endif // FEVV_USE_CGAL
44 #ifdef FEVV_USE_OPENMESH
46 #endif // FEVV_USE_OPENMESH
47 #ifdef FEVV_USE_AIF
49 #endif // FEVV_USE_AIF
50 #endif
51 
52 namespace FEVV {
53 
54 class BooleanOperationsPlugin : public QObject,
56  public BasePluginQt
57 {
58  Q_OBJECT
59  Q_INTERFACES(FEVV::Generic_PluginInterface)
60 #if(FEVV_USE_QT5) // see at the end of .cpp for QT4
61  Q_PLUGIN_METADATA(IID "BooleanOperationsPlugin")
62 #endif
63 
64  /*public:
65  using BasePlugin::apply;*/
66 public:
69 
70 public:
71  void init() override
72  {
73  m_operation = "UNION";
74  m_output_mesh_void = nullptr;
75  }
76 
77  void reset() override
78  {
79  init();
80 
81  emit resetSignal();
82  }
83 
84  void addParameters(BaseWindow *_window) override
85  {
86  window = _window;
87  if(window == nullptr || !window->isInit())
88  {
89  std::cerr << "BaseWindow is null or not initialized." << std::endl;
90  return;
91  }
92  }
93 
94  template< typename HalfedgeGraph >
95  void process(HalfedgeGraph *mesh_A,
96  FEVV::PMapsContainer *pmaps_bag_A,
97  Eigen::Matrix4d &matrix_A,
98  HalfedgeGraph *mesh_B,
99  FEVV::PMapsContainer *pmaps_bag_B,
100  Eigen::Matrix4d &matrix_B)
101  {
102  std::cout << "Asking to apply BooleanOperations filter ! " << std::endl;
103 
104  // translate/rotate input meshes according to manipulators
105  auto pm_A = get(boost::vertex_point, *mesh_A);
106  FEVV::Filters::homogeneous_transform(*mesh_A, pm_A, matrix_A);
107  auto pm_B = get(boost::vertex_point, *mesh_B);
108  FEVV::Filters::homogeneous_transform(*mesh_B, pm_B, matrix_B);
109 
110  // normals of input meshes may be invalid due to mesh rotation
112  remove_property_map(FEVV::face_normal, *pmaps_bag_A);
114  remove_property_map(FEVV::face_normal, *pmaps_bag_B);
115 
116  // create output mesh
117  HalfedgeGraph *output_mesh = new HalfedgeGraph;
118 
119  try
120  {
121  // apply filter
122  if(m_operation == "UNION")
123  FEVV::Filters::boolean_union(*mesh_A, *mesh_B, *output_mesh);
124  else if(m_operation == "INTER")
125  FEVV::Filters::boolean_inter(*mesh_A, *mesh_B, *output_mesh);
126  else
127  FEVV::Filters::boolean_minus(*mesh_A, *mesh_B, *output_mesh);
128 
129  // store output mesh for later display
130  m_output_mesh_void = static_cast< void * >(output_mesh);
131 
132  // hide mesh A
133  auto m_gpm_A =
134  get_property_map(FEVV::mesh_guiproperties, *mesh_A, *pmaps_bag_A);
135  auto gui_props_A = get(m_gpm_A, 0);
136  //gui_props_A.is_visible = false;
137  // we finally use TIME mode (see function 'activate_time_mode' below...)
138  put(m_gpm_A, 0, gui_props_A);
139 
140  // hide mesh B
141  auto m_gpm_B =
142  get_property_map(FEVV::mesh_guiproperties, *mesh_B, *pmaps_bag_B);
143  auto gui_props_B = get(m_gpm_B, 0);
144  //gui_props_B.is_visible = false;
145  // we finally use TIME mode (see function 'activate_time_mode' below...)
146  put(m_gpm_B, 0, gui_props_B);
147 
148  // show output mesh
149  FEVV::Types::GuiProperties gui_props_output;
150  //gui_props_output.is_visible = true;
151  // not necessary because true by default...
152 
153  // create a property map and a bag to store output mesh GUI properties
154  auto m_gpm_output = make_property_map(FEVV::mesh_guiproperties,
155  *output_mesh);
156  put(m_gpm_output, 0, gui_props_output);
159  *output_mesh,
161  m_gpm_output);
162  }
163  catch(const std::exception &e)
164  {
165  std::cout << e.what() << std::endl;
166  }
167  }
168 
169  template< typename HalfedgeGraph >
170  void applyHG(BaseAdapterVisu *_adapter,
171  HalfedgeGraph * /*_mesh*/,
172  FEVV::PMapsContainer * /*pmaps_bag*/)
173  {
174  // retrieve the two input meshes in current viewer window,
175  // then apply the filter
176 
177  SimpleViewer *viewer =
178  dynamic_cast< SimpleViewer * >(_adapter->getViewer());
179 
180  if(!viewer)
181  {
182  // something goes wrong
183  QMessageBox::information(
184  0, "", QObject::tr("An error occurred during Boolean "
185  "Operation filter processing."));
186  return;
187  }
188 
189  MixedMeshesVector mixed_meshes = viewer->getMeshes();
190  std::vector< FEVV::PMapsContainer * > pmaps_bags =
191  viewer->get_properties_maps();
192  if(mixed_meshes.size() >= 2)
193  {
194  // check that the two input meshes have the same type
195  if( mixed_meshes[0].second == mixed_meshes[1].second )
196  {
197  // get filter parameters from dialog window
199  dial1.setParameters(m_operation);
200  if(dial1.exec() == QDialog::Accepted)
201  dial1.getParameters(m_operation);
202  else
203  return; // abort applying filter
204 
205  // retrieve the two input meshes
206  auto mA = static_cast< HalfedgeGraph * >(mixed_meshes[0].first);
207  auto pmaps_bagA = pmaps_bags[0];
208  auto matrix44_A = viewer->getTransformMatrixEigen(0);
209 
210  auto mB = static_cast< HalfedgeGraph * >(mixed_meshes[1].first);
211  auto pmaps_bagB = pmaps_bags[1];
212  auto matrix44_B = viewer->getTransformMatrixEigen(1);
213 
214  // apply filter
215  process(mA, pmaps_bagA, matrix44_A, mB, pmaps_bagB, matrix44_B);
216 
217  // reset transform matrix of A and B because transformation
218  // is now applied to mesh coordinates
219  viewer->resetTransformMatrix(0);
220  viewer->resetTransformMatrix(1);
221 
222  // redraw input meshes
223  // required because the user may have manually moved the meshes
224  viewer->draw_or_redraw_mesh(mA, pmaps_bagA, true, false);
225  viewer->draw_or_redraw_mesh(mB, pmaps_bagB, true, false);
226 
227  // draw output mesh
228  auto output_mesh = static_cast< HalfedgeGraph * >( m_output_mesh_void);
229  if(output_mesh)
230  {
231  // pmaps_bag is required for display
232  viewer->draw_or_redraw_mesh(output_mesh,
234  false,
235  false,
236  m_operation);
237 
238  viewer->activate_time_mode();
239  }
240  }
241  else
242  {
243  QMessageBox::information(0,
244  "",
245  QObject::tr("Boolean Operations filter "
246  "can not be applied on meshes "
247  "with different datastructures."));
248  }
249  }
250  else
251  {
252  QMessageBox::information(0,
253  "",
254  QObject::tr("Boolean Operations filter needs "
255  "two meshes opened "
256  "with the same datastructure."));
257  }
258 
259  // comment next line to keep parameters between calls
260  //reset();
261 
262 #if(FEVV_USE_QT5)
263  // empty
264 #else
265  viewer->frame(); // necessary or not ?
266 #endif
267  }
268 
269 #ifdef FEVV_USE_OPENMESH
270 #if 0 //TODO-elo-note compiling error in Cartesian_converter.h with OM
271  void apply(BaseAdapterVisu *_adapter,
272  MeshOpenMesh *_mesh,
273  FEVV::PMapsContainer *pmaps_bag) override
274  {
275  applyHG< MeshOpenMesh >(_adapter, _mesh, pmaps_bag);
276  }
277 #endif
278 #endif
279 
280 #ifdef FEVV_USE_CGAL
281  void apply(BaseAdapterVisu *_adapter,
282  MeshLCC *_mesh,
283  FEVV::PMapsContainer *pmaps_bag) override
284  {
285  applyHG< MeshLCC >(_adapter, _mesh, pmaps_bag);
286  }
287 
288  void apply(BaseAdapterVisu *_adapter,
289  MeshSurface *_mesh,
290  FEVV::PMapsContainer *pmaps_bag) override
291  {
292  applyHG< MeshSurface >(_adapter, _mesh, pmaps_bag);
293  }
294 
295  void apply(BaseAdapterVisu *_adapter,
296  MeshPolyhedron *_mesh,
297  FEVV::PMapsContainer *pmaps_bag) override
298  {
299  applyHG< MeshPolyhedron >(_adapter, _mesh, pmaps_bag);
300  }
301 #endif
302 
303 #ifdef FEVV_USE_AIF
304 #if 0
305  //TODO-elo-note: missing num_halfedges() used by CGAL::copy_face_graph()
306  // and compiling error in Cartesian_converter.h with AIF
307  void apply(BaseAdapterVisu *_adapter,
308  MeshAIF *_mesh,
309  FEVV::PMapsContainer *pmaps_bag) override
310  {
311  applyHG<MeshAIF>(_adapter, _mesh, pmaps_bag);
312  }
313 #endif
314 #endif
315 
316 
317  QStringList Generic_plugins() const override
318  {
319  return QStringList() << "BooleanOperationsPlugin";
320  }
321 
322  bool Generic_plugin(const QString &/*plugin*/) override
323  {
324  SimpleWindow *sw = static_cast< SimpleWindow * >(window);
325  // dynamic_cast fails under OS X
326  sw->onModificationParam("booleanoperations_qt_p", this);
327  sw->onApplyButton();
328 
329  return true;
330  }
331 
332 signals:
333  void resetSignal();
334 
335 protected:
336  // filter parameters
337  std::string m_operation;
338 
339  // filter output
342 };
343 
344 } // namespace FEVV
345 
FEVV::BaseWindow::isInit
virtual bool isInit() const
Definition: BaseWindow.h:103
FEVV::SimpleViewer::activate_time_mode
void activate_time_mode()
Definition: SimpleViewer.inl:3456
FEVV::BooleanOperationsPlugin::m_output_mesh_void
void * m_output_mesh_void
Definition: BooleanOperationsPlugin.h:340
FEVV::remove_property_map
void remove_property_map(PropertyT p, PMapsContainer &pmaps)
Definition: properties.h:697
FEVV::put_property_map
void put_property_map(PropertyT p, const MeshT &, PMapsContainer &pmaps, const typename PMap_traits< PropertyT, MeshT >::pmap_type &pmap)
Definition: properties.h:664
FEVV::BooleanOperationsPlugin::init
void init() override
Definition: BooleanOperationsPlugin.h:71
boolean_operations.hpp
FEVV::get_property_map
PMap_traits< PropertyT, MeshT >::pmap_type get_property_map(PropertyT p, const MeshT &, const PMapsContainer &pmaps)
Definition: properties.h:646
FEVV::BooleanOperationsPlugin::~BooleanOperationsPlugin
~BooleanOperationsPlugin()=default
FEVV::BooleanOperationsPlugin
Definition: BooleanOperationsPlugin.h:57
FEVV::SimpleWindow::onApplyButton
void onApplyButton()
Definition: SimpleWindow.inl:525
FEVV::MeshLCC
CGAL::Linear_cell_complex_for_combinatorial_map< 2, 3, CGALLCCTraits, CGALItem > MeshLCC
Definition: DataStructures_cgal_linear_cell_complex.h:43
FEVV::DialogBooleanOperations1::getParameters
void getParameters(std::string &operation)
Definition: DialogBooleanOperations1.cpp:42
FEVV::DialogBooleanOperations1::setParameters
void setParameters(const std::string &operation)
Definition: DialogBooleanOperations1.cpp:31
tr
double tr(double &n)
Truncate a number to 1/1000 (only if BOOLEAN_OPERATIONS_DEBUG is enable)
Definition: boolops_definitions.hpp:127
FEVV::BooleanOperationsPlugin::reset
void reset() override
Definition: BooleanOperationsPlugin.h:77
FEVV::Filters::boolean_union
void boolean_union(HalfedgeGraph &gA, HalfedgeGraph &gB, HalfedgeGraph &g_out, const GeometryTraits &)
Computes the union of two polyhedra.
Definition: boolean_operations.hpp:46
FEVV::BasePluginQt
This class is intended to provide some standard message boxes to all plugins.
Definition: BasePluginQt.h:30
SimpleWindow.h
FEVV::Filters::homogeneous_transform
void homogeneous_transform(const HalfedgeGraph &g, PointMap &pm, const Eigen::Matrix4d &mat, const GeometryTraits &gt)
Calculate the homogeneous transformation of the vertices coordinates using the provided 4x4 homogeneo...
Definition: homogeneous_transform.hpp:38
FEVV::BaseAdapterVisu::getViewer
virtual Viewer * getViewer()
Definition: BaseAdapterVisu.h:130
properties_linear_cell_complex.h
FEVV::MixedMeshesVector
Functions to retrieve the name of the datastructure according to the mesh type.
Definition: SimpleViewer.h:138
FEVV::MixedMeshesVector::size
std::size_t size(void)
Definition: SimpleViewer.h:158
FEVV::DialogBooleanOperations1
Definition: DialogBooleanOperations1.h:24
FEVV::MeshSurface
CGAL::Surface_mesh< CGALPoint > MeshSurface
Definition: DataStructures_cgal_surface_mesh.h:23
FEVV::BaseAdapterVisu
Definition: BaseAdapterVisu.h:27
FEVV::SimpleViewer::resetTransformMatrix
void resetTransformMatrix(unsigned int position)
Definition: SimpleViewer.inl:3400
properties_aif.h
FEVV::BooleanOperationsPlugin::process
void process(HalfedgeGraph *mesh_A, FEVV::PMapsContainer *pmaps_bag_A, Eigen::Matrix4d &matrix_A, HalfedgeGraph *mesh_B, FEVV::PMapsContainer *pmaps_bag_B, Eigen::Matrix4d &matrix_B)
Definition: BooleanOperationsPlugin.h:95
FEVV::SimpleWindow
SimpleWindow is a specialization of QMainWindow. This class the Main Window.
Definition: SimpleWindow.h:80
FEVV::PMapsContainer
std::map< std::string, boost::any > PMapsContainer
Definition: properties.h:99
DialogBooleanOperations1.h
FEVV::SimpleViewer::draw_or_redraw_mesh
void draw_or_redraw_mesh(HalfedgeGraph *_g, PMapsContainer *_pmaps, bool _redraw=false, bool _recomputeNT_if_redraw=false, std::string _mesh_filename=std::string(""), bool _recreateOSGobj_if_redraw=true, float _step=0.)
Definition: SimpleViewer.inl:3415
FEVV::BooleanOperationsPlugin::applyHG
void applyHG(BaseAdapterVisu *_adapter, HalfedgeGraph *, FEVV::PMapsContainer *)
Definition: BooleanOperationsPlugin.h:170
FEVV::get
FEVV::PCLPointCloudPointMap::value_type get(const FEVV::PCLPointCloudPointMap &pm, FEVV::PCLPointCloudPointMap::key_type key)
Specialization of get(point_map, key) for PCLPointCloud.
Definition: Graph_properties_pcl_point_cloud.h:117
FEVV::SimpleViewer
SimpleViewer is a specialization of osgViewer::CompositeViewer. This class is a widget where we are a...
Definition: SimpleViewer.h:180
properties_polyhedron_3.h
FEVV::SimpleViewer::getMeshes
MixedMeshesVector getMeshes()
Definition: SimpleViewer.inl:421
FEVV::SimpleWindow::onModificationParam
void onModificationParam(std::string _pluginName, BasePlugin *_plugin)
Definition: SimpleWindow.inl:505
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
FEVV::BooleanOperationsPlugin::resetSignal
void resetSignal()
properties_surface_mesh.h
FEVV::BooleanOperationsPlugin::output_pmaps_bag
FEVV::PMapsContainer * output_pmaps_bag
Definition: BooleanOperationsPlugin.h:341
SimpleViewer.h
FEVV::BooleanOperationsPlugin::Generic_plugin
bool Generic_plugin(const QString &) override
Definition: BooleanOperationsPlugin.h:322
FEVV::BooleanOperationsPlugin::m_operation
std::string m_operation
Definition: BooleanOperationsPlugin.h:337
FEVV::Filters::boolean_inter
void boolean_inter(HalfedgeGraph &gA, HalfedgeGraph &gB, HalfedgeGraph &g_out, const GeometryTraits &)
Computes the intersection of two polyhedra.
Definition: boolean_operations.hpp:115
FEVV::SimpleViewer::get_properties_maps
std::vector< PMapsContainer * > get_properties_maps()
Definition: SimpleViewer.inl:475
FEVV::BooleanOperationsPlugin::BooleanOperationsPlugin
BooleanOperationsPlugin()=default
FEVV::SimpleViewer::getTransformMatrixEigen
Eigen::Matrix4d getTransformMatrixEigen(unsigned int position)
Definition: SimpleViewer.inl:3379
BasePluginQt.h
properties_openmesh.h
FEVV::MeshOpenMesh
OpenMesh::PolyMesh_ArrayKernelT< MyTraits > MeshOpenMesh
Definition: DataStructures_openmesh.h:51
FEVV::put
void put(FEVV::PCLPointCloudPointMap &pm, FEVV::PCLPointCloudPointMap::key_type key, const FEVV::PCLPointCloudPointMap::value_type &value)
Specialization of put(point_map, key, value) for PCLPointCloud.
Definition: Graph_properties_pcl_point_cloud.h:126
FEVV::Generic_PluginInterface
Definition: PluginInterface.h:37
FEVV::BooleanOperationsPlugin::addParameters
void addParameters(BaseWindow *_window) override
Definition: BooleanOperationsPlugin.h:84
FEVV::BasePluginQt::apply
virtual void apply(BaseAdapterVisu *, void *, FEVV::PMapsContainer *) override
Definition: BasePluginQt.h:108
FEVV::Types::GuiProperties
Definition: Gui_properties.h:27
PluginInterface.h
FEVV::Filters::boolean_minus
void boolean_minus(HalfedgeGraph &gA, HalfedgeGraph &gB, HalfedgeGraph &g_out, const GeometryTraits &)
Computes the subtraction of two polyhedra.
Definition: boolean_operations.hpp:184
homogeneous_transform.hpp
FEVV::BooleanOperationsPlugin::Generic_plugins
QStringList Generic_plugins() const override
Definition: BooleanOperationsPlugin.h:317
FEVV::BasePlugin::window
BaseWindow * window
Definition: BasePlugin.h:138
FEVV::BaseWindow
Definition: BaseWindow.h:25
properties.h
FEVV::vertex_normal
@ vertex_normal
Definition: properties.h:35
FEVV::MeshPolyhedron
CGAL::Polyhedron_3< CGALKernel, CGAL::Polyhedron_items_with_id_3 > MeshPolyhedron
Definition: DataStructures_cgal_polyhedron_3.h:33
FEVV::make_property_map
PMap_traits< PropertyT, MeshT >::pmap_type make_property_map(PropertyT, const MeshT &m)
Definition: properties.h:630
FEVV::MeshAIF
FEVV::DataStructures::AIF::AIFMesh MeshAIF
Definition: DataStructures_aif.h:19
FEVV::mesh_guiproperties
@ mesh_guiproperties
Definition: properties.h:93
FEVV::face_normal
@ face_normal
Definition: properties.h:77