File: OFF_io_plugin.cpp

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (287 lines) | stat: -rw-r--r-- 8,415 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "Scene_surface_mesh_item.h"
#include "Scene_polygon_soup_item.h"
#include "Scene_points_with_normal_item.h"

#include <CGAL/Three/Three.h>
#include <CGAL/Three/CGAL_Lab_io_plugin_interface.h>
#include <CGAL/Three/Three.h>

#include <CGAL/exceptions.h>
#include <CGAL/IO/OFF.h>
#include <CGAL/IO/OBJ.h>
#include <CGAL/Polygon_mesh_processing/repair.h>

#include <QMessageBox>
#include <QApplication>

#include <iostream>
#include <fstream>
#include <limits>

using namespace CGAL::Three;

class CGAL_Lab_off_plugin :
  public QObject,
  public CGAL_Lab_io_plugin_interface
{
  Q_OBJECT
  Q_INTERFACES(CGAL::Three::CGAL_Lab_io_plugin_interface)
  Q_PLUGIN_METADATA(IID "com.geometryfactory.CGALLab.IOPluginInterface/1.90" FILE "off_io_plugin.json")

public:
  bool isDefaultLoader(const Scene_item *item) const override
  {
    if(qobject_cast<const Scene_surface_mesh_item*>(item)
       || qobject_cast<const Scene_polygon_soup_item*>(item))
      return true;
    return false;
  }
  bool isDefaultLoader(const QString& name) const override
  {
    if(name == QString("off"))
      return true;
    return false;
  }
  QString name() const override{ return "off_plugin"; }
  QString nameFilters() const override { return "OFF files (*.off);;Wavefront OBJ (*.obj)"; }
  bool canLoad(QFileInfo fileinfo) const override;
  QList<Scene_item*> load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true) override;
  CGAL::Three::Scene_item* load_off(QFileInfo fileinfo);
  CGAL::Three::Scene_item* load_obj(QFileInfo fileinfo);

  bool canSave(const CGAL::Three::Scene_item*) override;
  bool save(QFileInfo fileinfo,QList<CGAL::Three::Scene_item*>& ) override;
};

bool CGAL_Lab_off_plugin::canLoad(QFileInfo) const {
  return true;
}

QList<Scene_item*> CGAL_Lab_off_plugin::
load(QFileInfo fileinfo, bool& ok, bool add_to_scene) {

  if(fileinfo.size() == 0)
  {
    CGAL::Three::Three::warning( tr("The file you are trying to load is empty."));
    Scene_surface_mesh_item* item =
        new Scene_surface_mesh_item(SMesh());
    item->setName(fileinfo.completeBaseName());
    ok = true;
    if(add_to_scene)
      CGAL::Three::Three::scene()->addItem(item);
    return QList<Scene_item*>()<<item;
  }
  if(fileinfo.suffix().toLower() == "off"){
    Scene_item* item = load_off(fileinfo);
    if(item){
      ok = true;
      if(add_to_scene)
        CGAL::Three::Three::scene()->addItem(item);
      return QList<Scene_item*>()<<item;
    }
    else
    {
      ok = false;
      return QList<Scene_item*>();
    }
  } else if(fileinfo.suffix().toLower() == "obj"){

    Scene_item* item = load_obj(fileinfo);
    if(item)
    {
      ok = true;
      if(add_to_scene)
        CGAL::Three::Three::scene()->addItem(item);
      return QList<Scene_item*>()<<item;
    }
    else
    {
      ok = true;
      return QList<Scene_item*>();
    }
  }
  return QList<Scene_item*>();
}


CGAL::Three::Scene_item*
CGAL_Lab_off_plugin::load_off(QFileInfo fileinfo) {
  // Open file
  std::ifstream in(fileinfo.filePath().toUtf8());
  if(!in) {
    std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl;
    return nullptr;
  }


  CGAL::File_scanner_OFF scanner( in, false);

  // Try to read .off in a point set
  if (scanner.size_of_facets() == 0)
  {
    in.seekg(0);
    Scene_points_with_normal_item* item = new Scene_points_with_normal_item();
    item->setName(fileinfo.completeBaseName());
    if (scanner.size_of_vertices()==0) return item;
    if(!item->read_off_point_set(in))
    {
      delete item;
      return nullptr;
    }

    return item;
  }

  in.seekg(0);
  // Try to read .off in a surface_mesh
  SMesh *surface_mesh = new SMesh();
  try{
    if(!(in >> *surface_mesh))
    {
      surface_mesh->clear();
    }
  } catch(...)
  {
    surface_mesh->clear();
  }
  if(!in || surface_mesh->is_empty())
  {
    delete surface_mesh;
    in.close();
    // Try to read .off in a polygon soup
    Scene_polygon_soup_item* soup_item = new Scene_polygon_soup_item();
    soup_item->setName(fileinfo.completeBaseName());
    std::ifstream in2(fileinfo.filePath().toUtf8());
    if(!soup_item->load(in2)) {
      QMessageBox::warning(
            CGAL::Three::Three::mainWindow(),
            "Cannot Open File",
            QString("Cannot open file %1").arg((const char*)fileinfo.filePath().toUtf8()));
      delete soup_item;
      return nullptr;
    }
    QApplication::restoreOverrideCursor();
    QMessageBox::information(
          CGAL::Three::Three::mainWindow(),
          "Cannot Open File",
          "The facets don't seem to be oriented. Loading a Soup of polygons instead."
          "To convert it to a Surface_mesh, use Polygon Mesh Processing -> Orient polygon soup");
    return soup_item;
  }
  Scene_surface_mesh_item* item = new Scene_surface_mesh_item(surface_mesh);
  item->setName(fileinfo.completeBaseName());
  std::size_t isolated_v = 0;
  for(vertex_descriptor v : vertices(*surface_mesh))
  {
    if(surface_mesh->is_isolated(v))
    {
      ++isolated_v;
    }
  }
  if(isolated_v >0)
  {
    item->setNbIsolatedvertices(isolated_v);
    //needs two restore, it's not a typo
    QApplication::restoreOverrideCursor();
    QMessageBox::warning((QWidget*)nullptr,
                         tr("Isolated vertices"),
                         tr("%1 isolated vertices found")
                         .arg(item->getNbIsolatedvertices()));
  }
  typedef boost::function_output_iterator<CGAL::internal::Throw_at_output> OutputIterator;
  try{
    CGAL::Polygon_mesh_processing::non_manifold_vertices(*surface_mesh, OutputIterator());
  }
  catch( CGAL::internal::Throw_at_output_exception& )
  {

    QApplication::restoreOverrideCursor();
    QMessageBox::warning((QWidget*)nullptr,
                         tr("Non Manifold Vertices"),
                         tr("Non-manifold vertices have been found"));
  }

  if(item->isItemMulticolor())
    item->computeItemColorVectorAutomatically(true);
  return item;
}

CGAL::Three::Scene_item*
CGAL_Lab_off_plugin::load_obj(QFileInfo fileinfo) {
  // Open file
  std::ifstream in(fileinfo.filePath().toUtf8());
  if(!in) {
    std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl;
    return nullptr;
  }
  Scene_surface_mesh_item* item = new Scene_surface_mesh_item();
  item->setName(fileinfo.completeBaseName());
  if(item->load_obj(in))
    return item;
  //if not polygonmesh load in soup
  std::vector<Point_3> points;
  std::vector<std::vector<std::size_t> > polygons;
  if(CGAL::IO::read_OBJ(in, points, polygons))
  {
    Scene_polygon_soup_item* soup_item = new Scene_polygon_soup_item();
    soup_item->load(points, polygons);
    return soup_item;
  }
  return nullptr;
}

bool CGAL_Lab_off_plugin::canSave(const CGAL::Three::Scene_item* item)
{
  // This plugin supports surface_meshes and polygon soups
  return qobject_cast<const Scene_surface_mesh_item*>(item) ||
    qobject_cast<const Scene_polygon_soup_item*>(item) ||
    qobject_cast<const Scene_points_with_normal_item*>(item);
}


bool
CGAL_Lab_off_plugin::
save(QFileInfo fileinfo,QList<CGAL::Three::Scene_item*>& items)
{
  Scene_item* item = items.front();
  // This plugin supports point sets, surface_meshes and polygon soups
  const Scene_points_with_normal_item* points_item =
    qobject_cast<const Scene_points_with_normal_item*>(item);
  const Scene_surface_mesh_item* sm_item =
    qobject_cast<const Scene_surface_mesh_item*>(item);
  const Scene_polygon_soup_item* soup_item =
    qobject_cast<const Scene_polygon_soup_item*>(item);

  if(!sm_item && !soup_item && !points_item)
    return false;

  std::ofstream out(fileinfo.filePath().toUtf8());
  out.precision (std::numeric_limits<double>::digits10 + 2);

  if(fileinfo.suffix().toLower() == "off"){
    bool res = (sm_item && sm_item->save(out)) ||
      (soup_item && soup_item->save(out)) ||
      (points_item && points_item->write_off_point_set(out));
    if(res){
      items.pop_front();
      return true;
    }
    else{
      return false;
    }
  }
  if(fileinfo.suffix().toLower() == "obj"){
    bool res = (sm_item && sm_item->save_obj(out))
        || (soup_item && CGAL::IO::write_OBJ(out, soup_item->points(), soup_item->polygons()));
    if(res)
    {
      items.pop_front();
      return true;
    }
    else
      return false;
  }
  return false;
}

#include "OFF_io_plugin.moc"