Moka controlers
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
controler-gmap-undo-redo.cc
Go to the documentation of this file.
1 /*
2  * lib-controler-gmap : Le contrôleur de 3-G-cartes, surcouche de lib-controler.
3  * Copyright (C) 2004, Moka Team, Université de Poitiers, Laboratoire SIC
4  * http://www.sic.sp2mi.univ-poitiers.fr/
5  * Copyright (C) 2009, Guillaume Damiand, CNRS, LIRIS,
6  * guillaume.damiand@liris.cnrs.fr, http://liris.cnrs.fr/
7  *
8  * This file is part of lib-controler-gmap
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 //******************************************************************************
25 #include "controler-gmap.hh"
26 using namespace std;
27 using namespace GMap3d;
28 //******************************************************************************
29 ostringstream* CControlerGMap::saveModel()
30 {
33 
34  ostream* os;
35  string filename = getFilename(getNewFileIndex());
36 
37  if (FUndoOnFile)
38  os = new ofstream(filename.c_str());
39  else
40  os = new ostringstream;
41 
42  assert( os!=NULL );
43 
44  int nbLevels = getNbSelectionLevels();
45  int* lasts = new int[nbLevels];
46 
47  int i,j;
48 
49  // Recherche de l'index des brins "last" dans la liste de tous les brins :
50  for (i=0; i<nbLevels; ++i)
51  lasts[i] = -1;
52 
53  CDynamicCoverageAll it(FMap);
54 
55  for (i=0; it.cont(); ++it, ++i)
56  for (j=0; j<nbLevels; ++j)
57  if (*it == getLastSelectedDart(j))
58  lasts[j] = i;
59 
60  // On sauve les lasts
61  for (i=0; i<nbLevels; ++i)
62  (*os) << lasts[i] << " ";
63 
64  // Puis les opérations
65  (*os) << FLastOperation << " ";
66 
67  // Et enfin la carte
68  if (! FMap->save(*os, BinaryFormat))
69  assert(false);
70 
71  delete [] lasts;
72 
73  ostringstream * res;
74 
75  if (FUndoOnFile)
76  {
77  os->flush();
78  delete os;
79  res = new ostringstream;
80  (*res) << filename;
81  }
82  else
83  res = static_cast<ostringstream*>(os);
84 
85  return res;
86 }
87 //------------------------------------------------------------------------------
88 bool CControlerGMap::loadModel(ostringstream * AStream)
89 {
90  istream * is;
91 
92  if (FUndoOnFile)
93  {
94  is = new ifstream(AStream->str().c_str());
95  if (!is->good())
96  {
97  delete is;
98  return false;
99  }
100  }
101  else
102  is = new istringstream(AStream->str());
103 
104  int nbLevels = getNbSelectionLevels();
105  int* lasts = new int[nbLevels];
106  int i,k,n;
107 
108  // On charge les last
109  for (i=0; i<nbLevels; ++i)
110  (*is) >> lasts[i];
111 
112  // Puis les opérations
113  (*is) >> FLastOperation;
114 
115  is->get(); // Pour passer le dernier espace avant le contenu de la carte
116 
117  FMap->removeAllDarts();
118 
119  setModelChanged ();
121 
122  for (i=0; i<nbLevels; ++i)
124 
125  // Et la carte
126  if ( FMap->load(*is, BinaryFormat)==NULL )
127  {
128  FMap->removeAllDarts();
129  delete [] lasts;
130  delete is;
131  return false;
132  }
133 
134  CDynamicCoverageAll it(FMap);
135 
136  for (n=0; it.cont(); ++it)
137  ++n;
138 
139  for (i=0, it.reinit(); it.cont(); ++it, ++i)
140  for (k=0; k<nbLevels; ++k)
141  if (lasts[k] == n-i-1)
142  selectDart(*it, k); // Ce sera le dernier brin sélectionné
143 
144  delete [] lasts;
145  delete is;
146 
147  assert(FMap->checkTopology());
148  assert(FMap->checkEmbeddings(ORBIT_VERTEX, ATTRIBUTE_VERTEX, true));
149 
150  return true;
151 }
152 //******************************************************************************