-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelliptical.cpp
419 lines (354 loc) · 11.4 KB
/
elliptical.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* 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 "ELLIPTICAL.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, ELLIPTICAL);
ELLIPTICAL::ELLIPTICAL()
{
setDescriptorId("{1046535B-7646-443A-9971-3D1C3C713D99}");
setName("Elliptical");
setDescription("Using AOIs.");
setCreator("Opticks Community");
setVersion("Sample");
setCopyright("Copyright (C) 2008, Ball Aerospace & Technologies Corp.");
setProductionStatus(false);
setType("Sample");
setSubtype("Statistics");
setMenuLocation("[Tutorial]/Elliptical");
setAbortSupported(true);
}
ELLIPTICAL::~ELLIPTICAL()
{
}
bool ELLIPTICAL::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 ELLIPTICAL::getOutputSpecification(PlugInArgList*& pOutArgList)
{
VERIFY(pOutArgList = Service<PlugInManagerServices>()->getPlugInArgList());
pOutArgList->addArg<double>("orientation", "orientation");
pOutArgList->addArg<double>("semi major axis", "semi major axis");
pOutArgList->addArg<double>("semi minor axis", "semi minor axis");
pOutArgList->addArg<double>("effective area", "effective area");
return true;
}
bool ELLIPTICAL::execute(PlugInArgList* pInArgList, PlugInArgList* pOutArgList)
{
StepResource pStep("Elliptical", "app", "E4B5D29C-9964-4615-8258-5BB9915768F7");
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());
FactoryResource<DataRequest> pRequest2;
pRequest2->setRows(pDesc->getActiveRow(startRow), pDesc->getActiveRow(endRow));
pRequest2->setColumns(pDesc->getActiveColumn(startColumn), pDesc->getActiveColumn(endColumn));
pRequest2->setInterleaveFormat(BSQ);
DataAccessor pAcc2 = pCube->getDataAccessor(pRequest2.release());
double total = 0.0;
int count = 0;
int totalx=0;
int totaly=0;
int numerator=0;
int denominator=0;
double m=0.0;
double totalxy=0;
int totalx_sq=0;
double totalxx=0;
double totalyy=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)
{
totalx+=col;
totaly-=row;
++count;
}
}
pAcc->nextColumn();
}
pAcc->nextRow();
}
double xcm=totalx/count;
double ycm=totaly/count;
/*
std::string p;
std::stringstream out4;
out4 << xcm;
p = out4.str();
pProgress->updateProgress("xcm "+p,0, ERRORS);
std::string p2;
std::stringstream out5;
out5 << ycm;
p2 = out5.str();
pProgress->updateProgress("ycm "+p2,0, ERRORS);
*/
//pAcc->toPixel(startRow, startColumn);
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 (!pAcc2.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(pAcc2->getColumnAsInteger()!=0)
{
totalxx+=(col-xcm)*(col-xcm)/count;
totalyy+=(-row-ycm)*(-row-ycm)/count;
totalxy-=(col-xcm)*(-row-ycm)/count;
/*
std::string s;
std::stringstream out;
out << totalxx;
s = out.str();
pProgress->updateProgress("totalxx"+s,0, ERRORS);
std::string q;
std::stringstream out1;
out1 << totalyy;
q = out1.str();
pProgress->updateProgress("totalyy"+q,0, ERRORS);
std::string f;
std::stringstream out2;
out2 << totalxy;
f = out2.str();
pProgress->updateProgress("totalxy"+f,0, ERRORS);
*/
}
}
pAcc2->nextColumn();
}
pAcc2->nextRow();
}
double t=totalxx+totalyy;
double d = (totalxx*totalyy)-(totalxy*totalxy);
double l1 = (t/2)+sqrt((t*t/4)-d);
double l2 = (t/2)-sqrt((t*t/4)-d);
/*
std::string e;
std::stringstream out12;
out12 << l1;
e = out12.str();
pProgress->updateProgress("l1 "+e,0, ERRORS);
std::string e1;
std::stringstream out13;
out13 << l2;
e1 = out13.str();
pProgress->updateProgress("l2 "+e1,0, ERRORS);*/
double eigenvector_1x=0.0;
double eigenvector_1y=0.0;
double eigenvector_2x=0.0;
double eigenvector_2y=0.0;
if(totalxy==0.0)
{
eigenvector_1x=1.0;
eigenvector_1y=0.0;
eigenvector_2x=0.0;
eigenvector_2y=1.0;
}
else
{
eigenvector_1x=l1-totalyy;
eigenvector_1y=totalxy;
eigenvector_2x=l2-totalyy;
eigenvector_2y=totalxy;
}
/*
double tester = eigenvector_2y/eigenvector_2x;
std::string f1;
std::stringstream out9;
out9 << tester;
f1 = out9.str();
pProgress->updateProgress("tester "+f1,0, ERRORS);
std::string v;
std::stringstream out10;
out10 << eigenvector_2y;
v = out10.str();
pProgress->updateProgress("eigenvector_2y "+v,0, ERRORS);
std::string l;
std::stringstream out11;
out11 << eigenvector_2x;
l = out11.str();
pProgress->updateProgress("eigenvector_2x "+l,0, ERRORS);
*/
double orientation = -((atan(eigenvector_2y/eigenvector_2x)*180/3.14159265)-90.0);
double semimajor = sqrt(l1)*2.0;
double semiminor = sqrt(l2)*2.0;
double area = 3.14159265*semimajor*semiminor;
if (pProgress != NULL)
{
std::string msg = "Orientation: " + StringUtilities::toDisplayString(orientation) + " degrees from horizontal"+ "\n"
+"Semi Major Axis: "+ StringUtilities::toDisplayString(semimajor)+ " pixels"+ "\n"
+"Semi Minor Axis: "+StringUtilities::toDisplayString(semiminor)+ " pixels" + "\n"
+"Effective area of ship: "+StringUtilities::toDisplayString(area)+" pixels^2";
pProgress->updateProgress(msg, 100, NORMAL);
}
pStep->addProperty("Orientation", orientation);
pStep->addProperty("Semi Major Axis", semimajor);
pStep->addProperty("Semi Minor Axis", semiminor);
pStep->addProperty("Effective area of ship", area);
pOutArgList->setPlugInArgValue("Orientation", &orientation);
pOutArgList->setPlugInArgValue("Semi Major Axis", &semimajor);
pOutArgList->setPlugInArgValue("Semi Minor Axis", &semiminor);
pOutArgList->setPlugInArgValue("Effective area of ship", &area);
pStep->finalize();
return true;
}