File: triangulation_3_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 (95 lines) | stat: -rw-r--r-- 2,782 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
#include <CGAL/IO/io.h>
#include <CGAL/Three/Three.h>
#include <CGAL/Three/CGAL_Lab_io_plugin_interface.h>
#include "T3_type.h"
#include <iostream>
#include <fstream>
#include "Scene_triangulation_3_item.h"

class Triangulation_3_io_plugin :
    public QObject,
    public CGAL::Three::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 "triangulation_3_io_plugin.json")

public:

  QString name() const override{ return "triangulation_3_io_plugin"; }

  QString nameFilters() const override{ return "T3 files(*.ascii.cgal);;T3 binary files (*.binary.cgal)"; }


  bool canLoad(QFileInfo) const override{ return true; }

  QList<CGAL::Three::Scene_item*> load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true) override{

    // Open file
    std::ifstream ifs(fileinfo.filePath().toUtf8());
    if(!ifs) {
      std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl;
      ok = false;
      return QList<CGAL::Three::Scene_item*>();
    }
    T3 tr;;

    if(fileinfo.absoluteFilePath().endsWith(".binary.cgal"))
      CGAL::IO::set_binary_mode(ifs);
    ifs >> tr;
    if(ifs.fail() || !tr.is_valid(false)) {
      std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl;
      ok = false;
      return QList<CGAL::Three::Scene_item*>();
    }
    Scene_triangulation_3_item* new_item = new Scene_triangulation_3_item(tr);
    new_item->setName(fileinfo.fileName());
    new_item->invalidateOpenGLBuffers();
    if(add_to_scene)
      CGAL::Three::Three::scene()->addItem(new_item);
    ok = true;
    return QList<CGAL::Three::Scene_item*>()<<new_item;
  }


  bool canSave(const CGAL::Three::Scene_item* item)override
  {
    return qobject_cast<const Scene_triangulation_3_item*>(item);
  }

  bool save(QFileInfo fileinfo, QList<Scene_item *> &items)override{
    for(int id : CGAL::Three::Three::scene()->selectionIndices())
    {
      Scene_item* item = CGAL::Three::Three::scene()->item(id);
      const Scene_triangulation_3_item* t3_item = qobject_cast<const Scene_triangulation_3_item*>(item);
      if (!t3_item)
      {
        continue;
      }

      QString path = fileinfo.absoluteFilePath();

      std::ofstream out(fileinfo.filePath().toUtf8());
      if(path.endsWith(".binary.cgal"))
      {
        CGAL::IO::set_binary_mode(out);
      }
      else
      {
        CGAL::IO::set_ascii_mode(out);
      }
      out << t3_item->triangulation();
      if( out.fail())
        return false;
      else
      {
        items.pop_front();
        return true;
      }
    }
    return false;
  }

};

#include "triangulation_3_io_plugin.moc"