/
SPParksSitesWriter.cpp
330 lines (294 loc) · 12.5 KB
/
SPParksSitesWriter.cpp
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* ============================================================================
* Copyright (c) 2009-2016 BlueQuartz Software, LLC
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of BlueQuartz Software, the US Air Force, nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The code contained herein was partially funded by the followig contracts:
* United States Air Force Prime Contract FA8650-07-D-5800
* United States Air Force Prime Contract FA8650-10-D-5210
* United States Prime Contract Navy N00173-07-C-2068
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
#include "SPParksSitesWriter.h"
#include <fstream>
#include <QtCore/QDateTime>
#include <QtCore/QDir>
#include "SIMPLib/Common/Constants.h"
#include "SIMPLib/FilterParameters/AbstractFilterParametersReader.h"
#include "SIMPLib/FilterParameters/DataArraySelectionFilterParameter.h"
#include "SIMPLib/FilterParameters/OutputFileFilterParameter.h"
#include "SIMPLib/FilterParameters/SeparatorFilterParameter.h"
#include "SIMPLib/Geometry/ImageGeom.h"
#include "SIMPLib/Utilities/FileSystemPathHelper.h"
#include "SIMPLib/Utilities/TimeUtilities.h"
#include "ImportExport/ImportExportConstants.h"
#include "ImportExport/ImportExportVersion.h"
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
SPParksSitesWriter::SPParksSitesWriter()
: m_FeatureIdsArrayPath(SIMPL::Defaults::ImageDataContainerName, SIMPL::Defaults::CellAttributeMatrixName, SIMPL::CellData::FeatureIds)
{
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
SPParksSitesWriter::~SPParksSitesWriter() = default;
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void SPParksSitesWriter::setupFilterParameters()
{
FileWriter::setupFilterParameters();
FilterParameterVector parameters;
parameters.push_back(SIMPL_NEW_OUTPUT_FILE_FP("Output File", OutputFile, FilterParameter::Parameter, SPParksSitesWriter, "*.spparks", "SPParks Sites File"));
parameters.push_back(SeparatorFilterParameter::New("Cell Data", FilterParameter::RequiredArray));
{
DataArraySelectionFilterParameter::RequirementType req = DataArraySelectionFilterParameter::CreateRequirement(SIMPL::TypeNames::Int32, 1, AttributeMatrix::Type::Cell, IGeometry::Type::Image);
parameters.push_back(SIMPL_NEW_DA_SELECTION_FP("Feature Ids", FeatureIdsArrayPath, FilterParameter::RequiredArray, SPParksSitesWriter, req));
}
setFilterParameters(parameters);
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void SPParksSitesWriter::readFilterParameters(AbstractFilterParametersReader* reader, int index)
{
reader->openFilterGroup(this, index);
setFeatureIdsArrayPath(reader->readDataArrayPath("FeatureIdsArrayPath", getFeatureIdsArrayPath()));
setOutputFile(reader->readString("OutputFile", getOutputFile()));
reader->closeFilterGroup();
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void SPParksSitesWriter::initialize()
{
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void SPParksSitesWriter::dataCheck()
{
setErrorCondition(0);
setWarningCondition(0);
FileSystemPathHelper::CheckOutputFile(this, "Output SPPARKS File", getOutputFile(), true);
getDataContainerArray()->getPrereqGeometryFromDataContainer<ImageGeom, AbstractFilter>(this, getFeatureIdsArrayPath().getDataContainerName());
QVector<size_t> cDims(1, 1);
m_FeatureIdsPtr = getDataContainerArray()->getPrereqArrayFromPath<DataArray<int32_t>, AbstractFilter>(this, getFeatureIdsArrayPath(),
cDims); /* Assigns the shared_ptr<> to an instance variable that is a weak_ptr<> */
if(nullptr != m_FeatureIdsPtr.lock()) /* Validate the Weak Pointer wraps a non-nullptr pointer to a DataArray<T> object */
{
m_FeatureIds = m_FeatureIdsPtr.lock()->getPointer(0);
} /* Now assign the raw pointer to data from the DataArray<T> object */
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void SPParksSitesWriter::preflight()
{
setInPreflight(true);
emit preflightAboutToExecute();
emit updateFilterParameters(this);
dataCheck();
emit preflightExecuted();
setInPreflight(false);
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int32_t SPParksSitesWriter::writeHeader()
{
setErrorCondition(0);
setWarningCondition(0);
dataCheck();
if(getErrorCondition() < 0)
{
return getErrorCondition();
}
DataContainer::Pointer m = getDataContainerArray()->getDataContainer(m_FeatureIdsArrayPath.getDataContainerName());
size_t udims[3] = {0, 0, 0};
std::tie(udims[0], udims[1], udims[2]) = m->getGeometryAs<ImageGeom>()->getDimensions();
size_t totalpoints = m->getGeometryAs<ImageGeom>()->getNumberOfElements();
std::ofstream outfile;
outfile.open(getOutputFile().toLatin1().data(), std::ios_base::binary);
if(!outfile)
{
QString ss = QObject::tr("Error opening output file '%1'").arg(getOutputFile());
setErrorCondition(-100);
notifyErrorMessage(getHumanLabel(), ss, getErrorCondition());
return getErrorCondition();
}
outfile << "-"
<< "\n";
outfile << "3 dimension"
<< "\n";
outfile << totalpoints << " sites"
<< "\n";
outfile << "26 max neighbors"
<< "\n";
outfile << "0 " << udims[0] << " xlo xhi"
<< "\n";
outfile << "0 " << udims[1] << " ylo yhi"
<< "\n";
outfile << "0 " << udims[2] << " zlo zhi"
<< "\n";
outfile << "\n";
outfile << "Values"
<< "\n";
outfile << "\n";
outfile.close();
return 0;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int32_t SPParksSitesWriter::writeFile()
{
setErrorCondition(0);
setWarningCondition(0);
dataCheck();
if(getErrorCondition() < 0)
{
return getErrorCondition();
}
DataContainer::Pointer m = getDataContainerArray()->getDataContainer(m_FeatureIdsArrayPath.getDataContainerName());
size_t udims[3] = {0, 0, 0};
std::tie(udims[0], udims[1], udims[2]) = m->getGeometryAs<ImageGeom>()->getDimensions();
size_t totalpoints = m->getGeometryAs<ImageGeom>()->getNumberOfElements();
std::ofstream outfile;
outfile.open(getOutputFile().toLatin1().data(), std::ios_base::binary | std::ios_base::app);
if(!outfile)
{
QString ss = QObject::tr("Error opening output file '%1'").arg(getOutputFile());
setErrorCondition(-100);
notifyErrorMessage(getHumanLabel(), ss, getErrorCondition());
return getErrorCondition();
}
qint64 millis = QDateTime::currentMSecsSinceEpoch();
qint64 currentMillis = millis;
qint64 startMillis = millis;
qint64 estimatedTime = 0;
float timeDiff = 0.0f;
int64_t increment = static_cast<int64_t>(totalpoints * 0.01f);
size_t count = 0;
QString buf;
QTextStream ss(&buf);
// Buffer the output with 4096 Bytes which is typically the size of a "Block" on a
// modern Hard Drive. This should speed up the writes considerably
char buffer[4096];
outfile.rdbuf()->pubsetbuf(buffer, 4096);
for(size_t k = 0; k < totalpoints; k++)
{
if(count % increment == 0)
{
currentMillis = QDateTime::currentMSecsSinceEpoch();
if(currentMillis - millis > 1000)
{
buf.clear();
ss << getMessagePrefix() << " " << static_cast<int>((float)(k) / (float)(totalpoints)*100) << " % Completed ";
timeDiff = ((float)k / (float)(currentMillis - startMillis));
estimatedTime = (float)(totalpoints - k) / timeDiff;
ss << " || Est. Time Remain: " << DREAM3D::convertMillisToHrsMinSecs(estimatedTime);
notifyStatusMessage(getHumanLabel(), buf);
millis = QDateTime::currentMSecsSinceEpoch();
}
}
count++;
// double temp0 = 0.0;
// double temp1 = 0.0;
outfile << k + 1 << " "
<< m_FeatureIds[k]
/* << " " << temp0 << " " << temp1 */
<< "\n";
}
outfile.close();
return 0;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
AbstractFilter::Pointer SPParksSitesWriter::newFilterInstance(bool copyFilterParameters) const
{
SPParksSitesWriter::Pointer filter = SPParksSitesWriter::New();
if(copyFilterParameters)
{
copyFilterParameterInstanceVariables(filter.get());
}
return filter;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
const QString SPParksSitesWriter::getCompiledLibraryName() const
{
return ImportExportConstants::ImportExportBaseName;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
const QString SPParksSitesWriter::getBrandingString() const
{
return "IO";
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
const QString SPParksSitesWriter::getFilterVersion() const
{
QString version;
QTextStream vStream(&version);
vStream << ImportExport::Version::Major() << "." << ImportExport::Version::Minor() << "." << ImportExport::Version::Patch();
return version;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
const QString SPParksSitesWriter::getGroupName() const
{
return SIMPL::FilterGroups::IOFilters;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
const QUuid SPParksSitesWriter::getUuid()
{
return QUuid("{bcf2f246-610f-5575-a434-241d04114b9f}");
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
const QString SPParksSitesWriter::getSubGroupName() const
{
return SIMPL::FilterSubGroups::OutputFilters;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
const QString SPParksSitesWriter::getHumanLabel() const
{
return "Export SPParks Sites File";
}