-
Notifications
You must be signed in to change notification settings - Fork 0
/
orientation.cpp
256 lines (217 loc) · 7.06 KB
/
orientation.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
/*
* The information in this file is
* Copyright(c) 2007 Ball Aerospace & Technologies Corporation
* and is subject to the terms and conditions of the
* GNU Lesser General Public License Version 2.1
* The license text is available from
* http://www.gnu.org/licenses/lgpl.html
*/
#include "AoiElement.h"
#include "AppConfig.h"
#include "AppVerify.h"
#include "BitMask.h"
#include "DataAccessor.h"
#include "DataAccessorImpl.h"
#include "DataRequest.h"
#include "DesktopServices.h"
#include "MessageLogResource.h"
#include "ModelServices.h"
#include "ObjectResource.h"
#include "PlugInArgList.h"
#include "PlugInManagerServices.h"
#include "PlugInResource.h"
#include "PlugInRegistration.h"
#include "Progress.h"
#include "RasterDataDescriptor.h"
#include "RasterElement.h"
#include "StringUtilities.h"
#include "switchOnEncoding.h"
#include "ORIENTATION.h"
#include "TypeConverter.h"
#include <limits>
#include <vector>
#include <QtCore/QStringList>
#include <QtGui/QInputDialog>
#include <sstream>
#include "BitMaskIterator.h"
#include <math.h>
REGISTER_PLUGIN_BASIC(OpticksTutorial, ORIENTATION);
void updateNumerator(int col, int row, int meanx, int numerator)
{
numerator+=(col-meanx)*row;
}
void updateDenominator(int row, int meany, int denominator)
{
denominator+=(row-meany)*(row-meany);
}
ORIENTATION::ORIENTATION()
{
setDescriptorId("{BEB0B654-710E-41D1-B3FC-A4459C6EBCA1}");
setName("Orientation");
setDescription("Using AOIs.");
setCreator("Opticks Community");
setVersion("Sample");
setCopyright("Copyright (C) 2008, Ball Aerospace & Technologies Corp.");
setProductionStatus(false);
setType("Sample");
setSubtype("Statistics");
setMenuLocation("[Tutorial]/Orientation");
setAbortSupported(true);
}
ORIENTATION::~ORIENTATION()
{
}
bool ORIENTATION::getInputSpecification(PlugInArgList*& pInArgList)
{
VERIFY(pInArgList = Service<PlugInManagerServices>()->getPlugInArgList());
pInArgList->addArg<Progress>(Executable::ProgressArg(), NULL, "Progress reporter");
pInArgList->addArg<RasterElement>(Executable::DataElementArg(), "Generate statistics for this raster element");
if (isBatch())
{
pInArgList->addArg<AoiElement>("AOI", NULL, "The AOI to calculate statistics over");
}
return true;
}
bool ORIENTATION::getOutputSpecification(PlugInArgList*& pOutArgList)
{
VERIFY(pOutArgList = Service<PlugInManagerServices>()->getPlugInArgList());
pOutArgList->addArg<double>("m", "gradient");
return true;
}
bool ORIENTATION::execute(PlugInArgList* pInArgList, PlugInArgList* pOutArgList)
{
StepResource pStep("Orientation", "app", "6988470F-DEAA-4AD4-A1F5-6E0E138BE5CE");
if (pInArgList == NULL || pOutArgList == NULL)
{
return false;
}
Progress* pProgress = pInArgList->getPlugInArgValue<Progress>(Executable::ProgressArg());
RasterElement* pCube = pInArgList->getPlugInArgValue<RasterElement>(Executable::DataElementArg());
if (pCube == NULL)
{
std::string msg = "A raster cube must be specified.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
RasterDataDescriptor* pDesc = static_cast<RasterDataDescriptor*>(pCube->getDataDescriptor());
VERIFY(pDesc != NULL);
AoiElement* pAoi = NULL;
if (isBatch())
{
pAoi = pInArgList->getPlugInArgValue<AoiElement>("AOI");
}
else
{
Service<ModelServices> pModel;
std::vector<DataElement*> pAois = pModel->getElements(pCube, TypeConverter::toString<AoiElement>());
if (!pAois.empty())
{
QStringList aoiNames("<none>");
for (std::vector<DataElement*>::iterator it = pAois.begin(); it != pAois.end(); ++it)
{
aoiNames << QString::fromStdString((*it)->getName());
}
QString aoi = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),
"Select an AOI", "Select an AOI for processing", aoiNames);
// select AOI
if (aoi != "<none>")
{
std::string strAoi = aoi.toStdString();
for (std::vector<DataElement*>::iterator it = pAois.begin(); it != pAois.end(); ++it)
{
if ((*it)->getName() == strAoi)
{
pAoi = static_cast<AoiElement*>(*it);
break;
}
}
if (pAoi == NULL)
{
std::string msg = "Invalid AOI.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
}
}
} // end if
const BitMask* pPoints = (pAoi == NULL) ? NULL : pAoi->getSelectedPoints();
BitMaskIterator it(pPoints, pCube);
int startRow = it.getBoundingBoxStartRow();
int startColumn = it.getBoundingBoxStartColumn();
int endRow = it.getBoundingBoxEndRow();
int endColumn = it.getBoundingBoxEndColumn();
FactoryResource<DataRequest> pRequest;
pRequest->setRows(pDesc->getActiveRow(startRow), pDesc->getActiveRow(endRow));
pRequest->setColumns(pDesc->getActiveColumn(startColumn), pDesc->getActiveColumn(endColumn));
pRequest->setInterleaveFormat(BSQ);
DataAccessor pAcc = pCube->getDataAccessor(pRequest.release());
double total = 0.0;
int count = 0;
int totalx=0;
int totaly=0;
int numerator=0;
int denominator=0;
int totalxy=0;
int totalx_sq=0;
for ( int row = startRow; row <= endRow; ++row)
{
if (isAborted())
{
std::string msg = getName() + " has been aborted.";
pStep->finalize(Message::Abort, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ABORT);
}
return false;
}
if (!pAcc.isValid())
{
std::string msg = "Unable to access the cube data.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
if (pProgress != NULL)
{
pProgress->updateProgress("Calculating statistics", 0.5*row * 100 / pDesc->getRowCount(), NORMAL);
}
for ( int col = startColumn; col <= endColumn; ++col)
{
if (pPoints == NULL || pPoints->getPixel(col, row))
{
if(pAcc->getColumnAsInteger()!=0)
{
totalxy+=(-1)*col*row;
totalx+=col;
totaly-=row;
++count;
totalx_sq+=col*col;
}
}
pAcc->nextColumn();
}
pAcc->nextRow();
}
double m = atan(((totalxy-((totalx*totaly)/count))/((totalx_sq/1.0)-((totalx*totalx)/count))))*180/3.14159265;
if (pProgress != NULL)
{
std::string msg = "Orientation: " + StringUtilities::toDisplayString(m) + " degrees from horizontal"+ "\n";
pProgress->updateProgress(msg, 100, NORMAL);
}
pStep->addProperty("Orientation", m);
pOutArgList->setPlugInArgValue("gradient", &m);
pStep->finalize();
return true;
}