GeoTessCPPExamples  2.0
GeoTessModelExtended.cc
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
4 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
5 //- retains certain rights in this software.
6 //-
7 //- BSD Open Source License.
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- * Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //- * Redistributions in binary form must reproduce the above copyright
16 //- notice, this list of conditions and the following disclaimer in the
17 //- documentation and/or other materials provided with the distribution.
18 //- * Neither the name of Sandia National Laboratories nor the names of its
19 //- contributors may be used to endorse or promote products derived from
20 //- this software without specific prior written permission.
21 //-
22 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //- POSSIBILITY OF SUCH DAMAGE.
33 //-
34 //- ****************************************************************************
35 
36 #include "GeoTessModelExtended.h"
37 
38 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
39 
40 namespace geotess {
41 
42 // **** _EXPLICIT TEMPLATE INSTANTIATIONS_ *************************************
43 
44 // **** _STATIC INITIALIZATIONS_************************************************
45 
46 // **** _FUNCTION IMPLEMENTATIONS_ *********************************************
47 
48 /**
49  * This class is an example of a class that extends GeoTessModel.
50  * It does everything a GeoTessModel can do but adds an extra
51  * data item to the model. In this example, the extra data is
52  * just a simple String, but in real models that extend
53  * GeoTessModel, it could be anything.
54  *
55  * <p>Classes that extend GeoTessModel should provide
56  * implementations of all the GeoTessModel constructors with
57  * the first thing that they do is call the super class
58  * constructor.
59  * <p>In addition, classes that extend GeoTessModel should
60  * override 4 IO functions: loadModelBinary, writeModelBinary,
61  * loadModelAscii, writeModelAscii.
62  * See examples below.
63  * <p>The first thing that these methods do is call the super
64  * class implementations to read/write the standard
65  * GeoTessModel information. After that, the methods
66  * may read/write the application specific data from/to
67  * the end of the standard GeoTessModel file.
68  * @author sballar
69  *
70  */
71 
72 /**
73  * Extend basemodel constructor by initializing extraData.
74  */
76 : GeoTessModel()
77 {
79 }
80 
81 /**
82  * Extend basemodel constructor by initializing extraData.
83  */
84 GeoTessModelExtended::GeoTessModelExtended(const string& modelInputFile, const string& relativeGridPath)
85 : GeoTessModel()
86 {
88  loadModel(modelInputFile, relativeGridPath);
89 }
90 
91 /**
92  * Extend basemodel constructor by initializing extraData.
93  */
94 GeoTessModelExtended::GeoTessModelExtended(const string& modelInputFile)
95 : GeoTessModel()
96 {
98  loadModel(modelInputFile, ".");
99 }
100 
101 /**
102  * Extend basemodel constructor by initializing extraData.
103  */
104 GeoTessModelExtended::GeoTessModelExtended(vector<int>& attributeFilter)
105 : GeoTessModel(attributeFilter)
106 {
107  initializeData();
108 }
109 
110 /**
111  * Extend basemodel constructor by initializing extraData.
112  */
113 GeoTessModelExtended::GeoTessModelExtended(const string& modelInputFile, const string& relativeGridPath,
114  vector<int>& attributeFilter)
115 : GeoTessModel(attributeFilter)
116 {
117  initializeData();
118  loadModel(modelInputFile, relativeGridPath);
119 }
120 
121 /**
122  * Extend basemodel constructor by initializing extraData.
123  */
124 GeoTessModelExtended::GeoTessModelExtended(const string& modelInputFile, vector<int>& attributeFilter)
125 : GeoTessModel(attributeFilter)
126 {
127  initializeData();
128  loadModel(modelInputFile, ".");
129 }
130 
131 /**
132  * Extend basemodel constructor by initializing extraData.
133  */
134 GeoTessModelExtended::GeoTessModelExtended(const string& gridFileName, GeoTessMetaData* metaData)
135 : GeoTessModel(gridFileName, metaData)
136 {
137  initializeData();
138 }
139 
140 /**
141  * Extend basemodel constructor by initializing extraData.
142  */
143 GeoTessModelExtended::GeoTessModelExtended(GeoTessGrid* grid, GeoTessMetaData* metaData)
144 : GeoTessModel(grid, metaData)
145 {
146  initializeData();
147 }
148 
149 /**
150  * Construct a new GeoTessModelExtended by making a deep copy of an
151  * existing GeoTessModel and initializing the extra data with default
152  * values.
153  * @param baseModel pointer to an existing GeoTessModel.
154  */
156 : GeoTessModel(&baseModel->getGrid(), baseModel->getMetaData().copy())
157 {
158  // a model has been constructed with a reference to the same grid
159  // as the baseModel and a deep copy of the meta data. Profiles
160  // are currently all NULL. Populate the array of Profiles in this
161  // extended model with deep copies of the profiles from the baseModel.
162  for (int i=0; i<baseModel->getNVertices(); ++i)
163  for (int j=0; j<baseModel->getNLayers(); ++j)
164  setProfile(i, j, baseModel->getProfile(i, j)->copy());
165 
166  // initialize the extraData with default value.
167  initializeData();
168 }
169 
170 /**
171  * Destructor.
172  */
174 {
175  // if this derived class allocated any memory,
176  // it must be deleted here.
177 }
178 
179 /**
180  * Override GeoTessModel::loadModelAscii()
181  *
182  * @param input ascii stream that provides input
183  * @param inputDirectory the directory where the model file resides
184  * @param relGridFilePath the relative path from the directory where
185  * the model file resides to the directory where the grid file resides.
186  * @throws GeoTessException
187  */
188 void GeoTessModelExtended::loadModelAscii(IFStreamAscii& input, const string& inputDirectory,
189  const string& relGridFilePath)
190 {
191  // read all base class GeoTessModel information from the input.
192  GeoTessModel::loadModelAscii(input, inputDirectory, relGridFilePath);
193 
194  // input pointer is now positioned just past the end of the base class
195  // GeoTessModel information. The derived class can now read whatever is appropriate.
196 
197  // it is good practice, but not required, to store the class
198  // name as the first thing added by the extending class.
199  string className;
200  input.readLine(className);
201 
202  if (className != class_name())
203  {
204  ostringstream os;
205  os << endl << "ERROR in GeoTessModelExtended::loadModelAscii()"<< endl
206  << "className loaded from file = " << className << endl
207  << "but expecting " << class_name() << endl;
208  throw GeoTessException(os, __FILE__, __LINE__, 12001);
209  }
210 
211  // it is good practice, but not required, to store a format
212  // version number as the second thing added by the extending class.
213  // With this information, if the format changes in a future release
214  // it may be possible to make the class backward compatible.
215  int fileFormat;
216  input.readInteger(fileFormat);
217 
218  if (fileFormat != 1)
219  {
220  ostringstream os;
221  os << endl << "ERROR in GeoTessModelExtended::loadModelAscii()"<< endl
222  << "File format version " << fileFormat << " is not supported." << endl;
223  throw GeoTessException(os, __FILE__, __LINE__, 12002);
224  }
225 
226  // read the extraData from the file. In this simple example, the extra
227  // data is a single string. In a real application there could be much more.
228  input.readLine(extraData);
229 }
230 
231 /**
232  * Override GeoTessModel::loadModelBinary()
233  *
234  * @param input binary stream that provides input
235  * @param inputDirectory the directory where the model file resides
236  * @param relGridFilePath the relative path from the directory where
237  * the model file resides to the directory where the grid file resides.
238  * @throws GeoTessException
239  */
240 void GeoTessModelExtended::loadModelBinary(IFStreamBinary& input, const string& inputDirectory,
241  const string& relGridFilePath)
242 {
243  // read all base class GeoTessModel information from the input.
244  GeoTessModel::loadModelBinary(input, inputDirectory,relGridFilePath);
245 
246  // input pointer is now positioned just past the end of the base class
247  // GeoTessModel information. The derived class can now read whatever is appropriate.
248 
249  // it is good practice, but not required, to store the class
250  // name as the first thing added by the extending class.
251  string className;
252  input.readString(className);
253 
254  if (className != class_name())
255  {
256  ostringstream os;
257  os << endl << "ERROR in GeoTessModelExtended::loadModelBinary()"<< endl
258  << "className loaded from file = " << className << endl
259  << "but expecting " << class_name() << endl;
260  throw GeoTessException(os, __FILE__, __LINE__, 12003);
261  }
262 
263  // it is good practice, but not required, to store a format
264  // version number as the second thing added by the extending class.
265  // With this information, if the format changes in a future release
266  // it may be possible to make the class backward compatible.
267  int fileFormat = input.readInt();
268 
269  if (fileFormat != 1)
270  {
271  ostringstream os;
272  os << endl << "ERROR in GeoTessModelExtended::loadModelAscii()"<< endl
273  << "File format version " << fileFormat << " is not supported." << endl;
274  throw GeoTessException(os, __FILE__, __LINE__, 12004);
275  }
276 
277  // read the extraData from the file. In this simple example, the extra
278  // data is a single string. In a real application there could be much more.
279  input.readString(extraData);
280 }
281 
282 /**
283  * Override GeoTessModel::writeModelAscii()
284  * Applications don't call this protected method directly.
285  * It is call from GeoTessModel.writeModel().
286  *
287  * @param output the output ascii stream to which model is written.
288  * @param gridFileName
289  *
290  */
291 void GeoTessModelExtended::writeModelAscii(IFStreamAscii& output, const string& gridFileName)
292 {
293  // write all the base class GeoTessModel information to output.
294  GeoTessModel::writeModelAscii(output, gridFileName);
295 
296  // output pointer is now positioned just past the end of the base class
297  // GeoTessModel information. The derived class can now write whatever is appropriate.
298 
299  // it is good practice, but not required, to store the class
300  // name as the first thing added by the extending class.
301  output.writeStringNL(class_name());
302 
303  // it is good practice, but not required, to store a format
304  // version number as the second thing added by the extending class.
305  // With this information, if the format of information added by the derived class
306  // changes in a future release it may be possible to make the class backward compatible.
307  output.writeIntNL(1);
308 
309  output.writeStringNL(extraData);
310 }
311 
312 /**
313  * Override GeoTessModel::writeModelBinary()
314  * Applications don't call this protected method directly.
315  * It is call from GeoTessModel.writeModel().
316  *
317  * @param output the output ascii stream to which model is written.
318  * @param gridFileName
319  */
320 void GeoTessModelExtended::writeModelBinary(IFStreamBinary& output, const string& gridFileName)
321 {
322  // write all the base class GeoTessModel information to output.
323  GeoTessModel::writeModelBinary(output, gridFileName);
324 
325  // output pointer is now positioned just past the end of the base class
326  // GeoTessModel information. The derived class can now write whatever is appropriate.
327 
328  // it is good practice, but not required, to store the class
329  // name as the first thing added by the extending class.
330  output.writeString(class_name());
331 
332  // it is good practice, but not required, to store a format
333  // version number as the second thing added by the extending class.
334  // With this information, if the format of information added by the derived class
335  // changes in a future release it may be possible to make the class backward compatible.
336  output.writeInt(1);
337 
338  output.writeString(extraData);
339 }
340 
341 /**
342  * Retrieve the extra data stored by this derived class.
343  */
345 { return extraData; }
346 
347 /**
348  * Modify the extra data stored by this derived class
349  */
350 void GeoTessModelExtended::setExtraData(const string& xData)
351 { extraData = xData; }
352 
353 } // end namespace geotess
geotess
Definition: AK135Model.h:55
geotess::GeoTessModelExtended::writeModelAscii
void writeModelAscii(IFStreamAscii &output, const string &gridFileName)
Definition: GeoTessModelExtended.cc:291
geotess::GeoTessModelExtended::GeoTessModelExtended
GeoTessModelExtended()
Definition: GeoTessModelExtended.cc:75
geotess::GeoTessModelExtended::loadModelAscii
void loadModelAscii(IFStreamAscii &input, const string &inputDirectory, const string &relGridFilePath)
Definition: GeoTessModelExtended.cc:188
geotess::GeoTessModelExtended::~GeoTessModelExtended
virtual ~GeoTessModelExtended()
Definition: GeoTessModelExtended.cc:173
GeoTessModelExtended.h
geotess::GeoTessModelExtended::initializeData
void initializeData()
Definition: GeoTessModelExtended.h:116
geotess::GeoTessModelExtended::extraData
string extraData
Definition: GeoTessModelExtended.h:111
geotess::GeoTessModelExtended::getExtraData
string getExtraData()
Definition: GeoTessModelExtended.cc:344
geotess::GeoTessModelExtended::writeModelBinary
void writeModelBinary(IFStreamBinary &output, const string &gridFileName)
Definition: GeoTessModelExtended.cc:320
geotess::GeoTessModelExtended::setExtraData
void setExtraData(const string &xData)
Definition: GeoTessModelExtended.cc:350
geotess::GeoTessModelExtended::loadModelBinary
void loadModelBinary(IFStreamBinary &input, const string &inputDirectory, const string &relGridFilePath)
Definition: GeoTessModelExtended.cc:240
geotess::GeoTessModelExtended::class_name
static string class_name()
Definition: GeoTessModelExtended.h:175