File indexing completed on 2024-12-22 04:18:07
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2007 The University of Toronto * 0004 * netterfield@astro.utoronto.ca * 0005 * copyright : (C) 2005 University of British Columbia * 0006 * dscott@phas.ubc.ca * 0007 * * 0008 * This program is free software; you can redistribute it and/or modify * 0009 * it under the terms of the GNU General Public License as published by * 0010 * the Free Software Foundation; either version 2 of the License, or * 0011 * (at your option) any later version. * 0012 * * 0013 ***************************************************************************/ 0014 0015 /* FIXME: figure out what this plugin is supposed to do, and comment 0016 accordingly. */ 0017 0018 #include "phase.h" 0019 #include "objectstore.h" 0020 #include "ui_phaseconfig.h" 0021 0022 static const QString& VECTOR_IN_TIME = "Vector In Time"; 0023 static const QString& VECTOR_IN_DATA = "Vector In Data"; 0024 static const QString& SCALAR_IN_PERIOD = "Period"; 0025 static const QString& SCALAR_IN_ZEROPHASE = "Zero Phase"; 0026 0027 static const QString& VECTOR_OUT_PHASE = "Phase"; 0028 static const QString& VECTOR_OUT_DATA = "Data Out"; 0029 0030 class ConfigPhasePlugin : public Kst::DataObjectConfigWidget, public Ui_PhaseConfig { 0031 public: 0032 ConfigPhasePlugin(QSettings* cfg) : DataObjectConfigWidget(cfg), Ui_PhaseConfig() { 0033 _store = 0; 0034 setupUi(this); 0035 } 0036 0037 ~ConfigPhasePlugin() {} 0038 0039 void setObjectStore(Kst::ObjectStore* store) { 0040 _store = store; 0041 _vectorTime->setObjectStore(store); 0042 _vectorData->setObjectStore(store); 0043 _scalarPeriod->setObjectStore(store); 0044 _scalarZeroPhase->setObjectStore(store); 0045 _scalarPeriod->setDefaultValue(0); 0046 _scalarZeroPhase->setDefaultValue(0); 0047 } 0048 0049 void setupSlots(QWidget* dialog) { 0050 if (dialog) { 0051 connect(_vectorTime, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0052 connect(_vectorData, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0053 connect(_scalarPeriod, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0054 connect(_scalarZeroPhase, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0055 } 0056 } 0057 0058 Kst::VectorPtr selectedVectorTime() { return _vectorTime->selectedVector(); }; 0059 void setSelectedVectorTime(Kst::VectorPtr vector) { return _vectorTime->setSelectedVector(vector); }; 0060 0061 Kst::VectorPtr selectedVectorData() { return _vectorData->selectedVector(); }; 0062 void setSelectedVectorData(Kst::VectorPtr vector) { return _vectorData->setSelectedVector(vector); }; 0063 0064 Kst::ScalarPtr selectedScalarPeriod() { return _scalarPeriod->selectedScalar(); }; 0065 void setSelectedScalarPeriod(Kst::ScalarPtr scalar) { return _scalarPeriod->setSelectedScalar(scalar); }; 0066 0067 Kst::ScalarPtr selectedScalarZeroPhase() { return _scalarZeroPhase->selectedScalar(); }; 0068 void setSelectedScalarZeroPhase(Kst::ScalarPtr scalar) { return _scalarZeroPhase->setSelectedScalar(scalar); }; 0069 0070 virtual void setupFromObject(Kst::Object* dataObject) { 0071 if (PhaseSource* source = static_cast<PhaseSource*>(dataObject)) { 0072 setSelectedVectorTime(source->vectorTime()); 0073 setSelectedVectorData(source->vectorData()); 0074 setSelectedScalarPeriod(source->scalarPeriod()); 0075 setSelectedScalarZeroPhase(source->scalarZeroPhase()); 0076 } 0077 } 0078 0079 virtual bool configurePropertiesFromXml(Kst::ObjectStore *store, QXmlStreamAttributes& attrs) { 0080 Q_UNUSED(store); 0081 Q_UNUSED(attrs); 0082 0083 bool validTag = true; 0084 0085 // QStringRef av; 0086 // av = attrs.value("value"); 0087 // if (!av.isNull()) { 0088 // _configValue = QVariant(av.toString()).toBool(); 0089 // } 0090 0091 return validTag; 0092 } 0093 0094 public slots: 0095 virtual void save() { 0096 if (_cfg) { 0097 _cfg->beginGroup("Phase DataObject Plugin"); 0098 _cfg->setValue("Input Vector Time", _vectorTime->selectedVector()->Name()); 0099 _cfg->setValue("Input Vector Data", _vectorData->selectedVector()->Name()); 0100 _cfg->setValue("Input Scalar Period", _scalarPeriod->selectedScalar()->Name()); 0101 _cfg->setValue("Input Scalar Zero Phase", _scalarZeroPhase->selectedScalar()->Name()); 0102 _cfg->endGroup(); 0103 } 0104 } 0105 0106 virtual void load() { 0107 if (_cfg && _store) { 0108 _cfg->beginGroup("Phase DataObject Plugin"); 0109 QString vectorName = _cfg->value("Input Vector Time").toString(); 0110 Kst::Object* object = _store->retrieveObject(vectorName); 0111 Kst::Vector* vectorTime = static_cast<Kst::Vector*>(object); 0112 if (vectorTime) { 0113 setSelectedVectorTime(vectorTime); 0114 } 0115 vectorName = _cfg->value("Input Vector Data").toString(); 0116 object = _store->retrieveObject(vectorName); 0117 Kst::Vector* vectorData = static_cast<Kst::Vector*>(object); 0118 if (vectorData) { 0119 setSelectedVectorData(vectorData); 0120 } 0121 QString scalarName = _cfg->value("Input Scalar Period").toString(); 0122 object = _store->retrieveObject(scalarName); 0123 Kst::Scalar* scalarPeriod = static_cast<Kst::Scalar*>(object); 0124 if (scalarPeriod) { 0125 setSelectedScalarPeriod(scalarPeriod); 0126 } 0127 scalarName = _cfg->value("Input Scalar Zero Phase").toString(); 0128 object = _store->retrieveObject(scalarName); 0129 Kst::Scalar* scalarZeroPhase = static_cast<Kst::Scalar*>(object); 0130 if (scalarZeroPhase) { 0131 setSelectedScalarZeroPhase(scalarZeroPhase); 0132 } 0133 _cfg->endGroup(); 0134 } 0135 } 0136 0137 private: 0138 Kst::ObjectStore *_store; 0139 0140 }; 0141 0142 0143 PhaseSource::PhaseSource(Kst::ObjectStore *store) 0144 : Kst::BasicPlugin(store) { 0145 } 0146 0147 0148 PhaseSource::~PhaseSource() { 0149 } 0150 0151 0152 QString PhaseSource::_automaticDescriptiveName() const { 0153 return tr("Phase Plugin Object"); 0154 } 0155 0156 0157 void PhaseSource::change(Kst::DataObjectConfigWidget *configWidget) { 0158 if (ConfigPhasePlugin* config = static_cast<ConfigPhasePlugin*>(configWidget)) { 0159 setInputVector(VECTOR_IN_TIME, config->selectedVectorTime()); 0160 setInputVector(VECTOR_IN_DATA, config->selectedVectorData()); 0161 setInputScalar(SCALAR_IN_PERIOD, config->selectedScalarPeriod()); 0162 setInputScalar(SCALAR_IN_ZEROPHASE, config->selectedScalarZeroPhase()); 0163 } 0164 } 0165 0166 0167 void PhaseSource::setupOutputs() { 0168 setOutputVector(VECTOR_OUT_PHASE, ""); 0169 setOutputVector(VECTOR_OUT_DATA, ""); 0170 } 0171 0172 0173 bool PhaseSource::algorithm() { 0174 Kst::VectorPtr inputVectorTime = _inputVectors[VECTOR_IN_TIME]; 0175 Kst::VectorPtr inputVectorData = _inputVectors[VECTOR_IN_DATA]; 0176 Kst::ScalarPtr inputScalarPeriod = _inputScalars[SCALAR_IN_PERIOD]; 0177 Kst::ScalarPtr inputScalarZeroPhase = _inputScalars[SCALAR_IN_ZEROPHASE]; 0178 0179 Kst::VectorPtr outputVectorPhase = _outputVectors[VECTOR_OUT_PHASE]; 0180 Kst::VectorPtr outputVectorDataOut = _outputVectors[VECTOR_OUT_DATA]; 0181 0182 double* pResult[2]; 0183 double dPhasePeriod = inputScalarPeriod->value(); 0184 double dPhaseZero = inputScalarZeroPhase->value(); 0185 int iLength; 0186 0187 bool bReturn = false; 0188 0189 if (dPhasePeriod <= 0.0) { 0190 _errorString = tr("Error: Input Scalar Phase must be greater than zero."); 0191 return false; 0192 } 0193 0194 if (inputVectorTime->length() != inputVectorData->length()) { 0195 _errorString = tr("Error: Input Vector lengths do not match."); 0196 return false; 0197 } 0198 0199 iLength = inputVectorTime->length(); 0200 0201 outputVectorPhase->resize(iLength, true); 0202 pResult[0] = outputVectorPhase->raw_V_ptr(); 0203 0204 outputVectorDataOut->resize(iLength, true); 0205 pResult[1] = outputVectorDataOut->raw_V_ptr(); 0206 0207 if (pResult[0] != NULL && pResult[1] != NULL) { 0208 for (int i = 0; i < outputVectorPhase->length(); ++i) { 0209 outputVectorPhase->raw_V_ptr()[i] = pResult[0][i]; 0210 } 0211 for (int i = 0; i < outputVectorDataOut->length(); ++i) { 0212 outputVectorDataOut->raw_V_ptr()[i] = pResult[1][i]; 0213 } 0214 0215 /* 0216 determine the outputVectorPhase... 0217 */ 0218 for (int i=0; i<iLength; i++) { 0219 outputVectorPhase->raw_V_ptr()[i] = fmod( ( inputVectorTime->raw_V_ptr()[i] - dPhaseZero ) / dPhasePeriod, 1.0 ); 0220 } 0221 0222 /* 0223 sort by outputVectorPhase... 0224 */ 0225 memcpy( outputVectorDataOut->raw_V_ptr(), inputVectorData->value(), iLength * sizeof( double ) ); 0226 double* sort[2]; 0227 sort[0] = outputVectorPhase->raw_V_ptr(); 0228 sort[1] = outputVectorDataOut->raw_V_ptr(); 0229 quicksort( sort, 0, iLength-1 ); 0230 0231 bReturn = true; 0232 } 0233 0234 return bReturn; 0235 } 0236 0237 0238 void PhaseSource::swap(double* pData[], int iOne, int iTwo) { 0239 double dTemp; 0240 0241 for (int i=0; i<2; i++) { 0242 dTemp = pData[i][iOne]; 0243 pData[i][iOne] = pData[i][iTwo]; 0244 pData[i][iTwo] = dTemp; 0245 } 0246 } 0247 0248 0249 void PhaseSource::quicksort( double* pData[], int iLeft, int iRight) { 0250 double dVal = pData[0][iRight]; 0251 int i = iLeft - 1; 0252 int j = iRight; 0253 0254 if (iRight <= iLeft) { 0255 return; 0256 } 0257 0258 while (1) { 0259 while (pData[0][++i] < dVal) {} 0260 0261 while (dVal < pData[0][--j]) { 0262 if (j == iLeft) { 0263 break; 0264 } 0265 } 0266 if (i >= j) { 0267 break; 0268 } 0269 swap( pData, i, j ); 0270 } 0271 swap( pData, i, iRight ); 0272 quicksort( pData, iLeft, i-1 ); 0273 quicksort( pData, i+1, iRight ); 0274 } 0275 0276 0277 Kst::VectorPtr PhaseSource::vectorTime() const { 0278 return _inputVectors[VECTOR_IN_TIME]; 0279 } 0280 0281 0282 Kst::VectorPtr PhaseSource::vectorData() const { 0283 return _inputVectors[VECTOR_IN_DATA]; 0284 } 0285 0286 0287 Kst::ScalarPtr PhaseSource::scalarPeriod() const { 0288 return _inputScalars[SCALAR_IN_PERIOD]; 0289 } 0290 0291 0292 Kst::ScalarPtr PhaseSource::scalarZeroPhase() const { 0293 return _inputScalars[SCALAR_IN_ZEROPHASE]; 0294 } 0295 0296 0297 QStringList PhaseSource::inputVectorList() const { 0298 QStringList vectors(VECTOR_IN_TIME); 0299 vectors += VECTOR_IN_DATA; 0300 return vectors; 0301 } 0302 0303 0304 QStringList PhaseSource::inputScalarList() const { 0305 QStringList scalars(SCALAR_IN_PERIOD); 0306 scalars += SCALAR_IN_ZEROPHASE; 0307 return scalars; 0308 } 0309 0310 0311 QStringList PhaseSource::inputStringList() const { 0312 return QStringList( /*STRING_IN*/ ); 0313 } 0314 0315 0316 QStringList PhaseSource::outputVectorList() const { 0317 QStringList vectors(VECTOR_OUT_PHASE); 0318 vectors += VECTOR_OUT_DATA; 0319 return vectors; 0320 } 0321 0322 0323 QStringList PhaseSource::outputScalarList() const { 0324 return QStringList( /*SCALAR_OUT*/ ); 0325 } 0326 0327 0328 QStringList PhaseSource::outputStringList() const { 0329 return QStringList( /*STRING_OUT*/ ); 0330 } 0331 0332 0333 void PhaseSource::saveProperties(QXmlStreamWriter &s) { 0334 Q_UNUSED(s); 0335 // s.writeAttribute("value", _configValue); 0336 } 0337 0338 0339 QString PhasePlugin::pluginName() const { return tr("Phase"); } 0340 QString PhasePlugin::pluginDescription() const { return tr("Phases a given data set to the specified period and zero outputVectorPhase."); } 0341 0342 0343 Kst::DataObject *PhasePlugin::create(Kst::ObjectStore *store, Kst::DataObjectConfigWidget *configWidget, bool setupInputsOutputs) const { 0344 0345 if (ConfigPhasePlugin* config = static_cast<ConfigPhasePlugin*>(configWidget)) { 0346 0347 PhaseSource* object = store->createObject<PhaseSource>(); 0348 0349 if (setupInputsOutputs) { 0350 object->setInputScalar(SCALAR_IN_PERIOD, config->selectedScalarPeriod()); 0351 object->setInputScalar(SCALAR_IN_ZEROPHASE, config->selectedScalarZeroPhase()); 0352 object->setupOutputs(); 0353 object->setInputVector(VECTOR_IN_TIME, config->selectedVectorTime()); 0354 object->setInputVector(VECTOR_IN_DATA, config->selectedVectorData()); 0355 } 0356 0357 object->setPluginName(pluginName()); 0358 0359 object->writeLock(); 0360 object->registerChange(); 0361 object->unlock(); 0362 0363 return object; 0364 } 0365 return 0; 0366 } 0367 0368 0369 Kst::DataObjectConfigWidget *PhasePlugin::configWidget(QSettings *settingsObject) const { 0370 ConfigPhasePlugin *widget = new ConfigPhasePlugin(settingsObject); 0371 return widget; 0372 } 0373 0374 #ifndef QT5 0375 Q_EXPORT_PLUGIN2(kstplugin_BinPlugin, PhasePlugin) 0376 #endif 0377 0378 // vim: ts=2 sw=2 et