[Kst] extragear/graphics/kst/src/plugins/convolution/convolve
Adam Treat
treat at kde.org
Fri Dec 1 16:18:12 CET 2006
SVN commit 609664 by treat:
* Convert convolution plugin to new plugin architecture.
M +8 -8 Makefile.am
M +165 -119 convolve.cpp
A convolve.h [License: GPL (v2+)]
D convolve.xml
A kstobject_convolve.desktop
--- trunk/extragear/graphics/kst/src/plugins/convolution/convolve/Makefile.am #609663:609664
@@ -1,12 +1,12 @@
-installdir=$(kde_moduledir)/kstplugins
-INCLUDES=-I$(srcdir)/../../../kst $(all_includes)
+INCLUDES=-I$(top_srcdir)/kst -I$(top_srcdir)/kst/src/libkst -I$(top_srcdir)/kst/src/libkstmath $(all_includes)
-install_LTLIBRARIES = convolve.la
+kde_module_LTLIBRARIES=kstobject_convolve.la
-convolve_la_LDFLAGS = -module $(KDE_PLUGIN) $(all_libraries)
-convolve_la_LIBADD = $(GSL_LIBS)
-convolve_la_SOURCES = convolve.cpp
+kstobject_convolve_la_LDFLAGS=$(all_libraries) -module -avoid-version
+kstobject_convolve_la_SOURCES=convolve.cpp
+kstobject_convolve_la_LIBADD = $(GSL_LIBS)
+services_DATA=kstobject_convolve.desktop
+servicesdir=$(kde_servicesdir)/kst
+
METASOURCES=AUTO
-
-install_DATA=convolve.xml
--- trunk/extragear/graphics/kst/src/plugins/convolution/convolve/convolve.cpp #609663:609664
@@ -1,154 +1,200 @@
-/*
- * Convolution plugin for KST.
- * Copyright 2004, The University of British Columbia
- * Released under the terms of the GPL.
- */
+/***************************************************************************
+ convolve.cpp
+ -------------------
+ begin : 11/29/06
+ copyright : (C) 2006 The University of Toronto
+ email :
+ ***************************************************************************/
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <math.h>
+/***************************************************************************
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+#include "convolve.h"
+
#include <gsl/gsl_fft_real.h>
#include <gsl/gsl_fft_halfcomplex.h>
-extern "C" int convolve(const double *const inArrays[], const int inArrayLens[],
- const double inScalars[],
- double *outArrays[], int outArrayLens[],
- double outScalars[]);
+#include <kgenericfactory.h>
-int convolve(const double *const inArrays[], const int inArrayLens[],
- const double inScalars[],
- double *outArrays[], int outArrayLens[],
- double outScalars[])
-{
+static const QString& ARRAY_ONE = KGlobal::staticQString("Array One");
+static const QString& ARRAY_TWO = KGlobal::staticQString("Array Two");
+static const QString& CONVOLVED = KGlobal::staticQString("Convolved");
+
+KST_KEY_DATAOBJECT_PLUGIN( convolve )
+
+K_EXPORT_COMPONENT_FACTORY( kstobject_convolve,
+ KGenericFactory<Convolve>( "kstobject_convolve" ) )
+
+Convolve::Convolve( QObject */*parent*/, const char */*name*/, const QStringList &/*args*/ )
+ : KstBasicPlugin() {
+}
+
+
+Convolve::~Convolve() {
+}
+
+
+bool Convolve::algorithm() {
+
+ KstVectorPtr arrayOne = inputVector(ARRAY_ONE);
+ KstVectorPtr arrayTwo = inputVector(ARRAY_TWO);
+ KstVectorPtr convolved = outputVector(CONVOLVED);
+
+ if (arrayOne->length() <= 0 && arrayTwo->length() <= 0)
+ return false;
+
double* pdResponse;
double* pdConvolve;
double* pdResult;
double dReal;
double dImag;
- int i = 0;
+
+ KstVectorPtr response;
+ KstVectorPtr convolve;
+
int iLength;
int iLengthNew;
- int iReturn = -1;
+
+ bool iReturn = false;
int iResponseMidpoint;
- int iResponseIndex = 1;
- int iConvolveIndex = 0;
- if( inArrayLens[0] > 0 && inArrayLens[1] > 0 )
- {
- //
- // determine which is the response function:
- // i.e. which is shorter...
- //
- iLength = inArrayLens[0];
- if( inArrayLens[1] > iLength )
- {
- iResponseIndex = 0;
- iConvolveIndex = 1;
- }
+ //
+ // determine which is the response function:
+ // i.e. which is shorter...
+ //
+ if (arrayOne->length() < arrayTwo->length()) {
+ response = arrayOne;
+ convolve = arrayTwo;
+ } else {
+ response = arrayTwo;
+ convolve = arrayOne;
+ }
- iResponseMidpoint = inArrayLens[iResponseIndex] / 2;
- iLength = inArrayLens[iConvolveIndex] + iResponseMidpoint;
+ convolved->resize(convolve->length(), false);
- //
- // round iLength up to the nearest factor of two...
- //
- iLengthNew = 64;
- while( iLengthNew < iLength && iLengthNew > 0 )
- {
- iLengthNew *= 2;
- }
- iLength = iLengthNew;
+ iResponseMidpoint = response->length() / 2;
+ iLength = convolve->length() + iResponseMidpoint;
- if( iLength > 0 )
- {
- pdResponse = new double[iLength];
- pdConvolve = new double[iLength];
- if( pdResponse != NULL && pdConvolve != NULL )
- {
- //
- // sort the response function into wrap-around order...
- //
- memset( pdResponse, 0, iLength * sizeof( double ) );
+ //
+ // round iLength up to the nearest factor of two...
+ //
+ iLengthNew = 64;
+ while (iLengthNew < iLength && iLengthNew > 0) {
+ iLengthNew *= 2;
+ }
+ iLength = iLengthNew;
- for( i=0; i<iResponseMidpoint; i++ )
- {
- pdResponse[i] = inArrays[iResponseIndex][iResponseMidpoint+i];
- pdResponse[iLength-iResponseMidpoint+i] = inArrays[iResponseIndex][i];
- }
+ if (iLength > 0) {
+ pdResponse = new double[iLength];
+ pdConvolve = new double[iLength];
+ if (pdResponse != NULL && pdConvolve != NULL) {
+ //
+ // sort the response function into wrap-around order...
+ //
+ memset( pdResponse, 0, iLength * sizeof( double ) );
- //
- // handle the case where the response function has an odd number of points...
- //
- if( iResponseMidpoint % 2 == 1 )
- {
- pdResponse[iResponseMidpoint] = inArrays[iResponseIndex][inArrayLens[iResponseIndex]];
- }
+ for (int i = 0; i < iResponseMidpoint; i++) {
+ pdResponse[i] = response->value()[iResponseMidpoint+i];
+ pdResponse[iLength-iResponseMidpoint+i] = response->value()[i];
+ }
- //
- // zero-pad the convolve array...
- //
- memset( pdConvolve, 0, iLength * sizeof( double ) );
- memcpy( pdConvolve, inArrays[iConvolveIndex], inArrayLens[iConvolveIndex] * sizeof( double ) );
+ //
+ // handle the case where the response function has an odd number of points...
+ //
+ if (iResponseMidpoint % 2 == 1) {
+ pdResponse[iResponseMidpoint] = response->value()[response->length()];
+ }
- //
- // calculate the FFTs of the two functions...
- //
- if( gsl_fft_real_radix2_transform( pdResponse, 1, iLength ) == 0 )
- {
- if( gsl_fft_real_radix2_transform( pdConvolve, 1, iLength ) == 0 )
- {
- //
- // multiply the FFTs together...
- //
- for( i=0; i<iLength/2; i++ )
- {
- if( i==0 || i==(iLength/2)-1 )
- {
- pdResponse[i] = pdResponse[i] * pdConvolve[i];
- }
- else
- {
- dReal = pdResponse[i] * pdConvolve[i] - pdResponse[iLength-i] * pdConvolve[iLength-i];
- dImag = pdResponse[i] * pdConvolve[iLength-i] + pdResponse[iLength-i] * pdConvolve[i];
+ //
+ // zero-pad the convolve array...
+ //
+ memset( pdConvolve, 0, iLength * sizeof( double ) );
+ memcpy( pdConvolve, convolve->value(), convolve->length() * sizeof( double ) );
- pdResponse[i] = dReal;
- pdResponse[iLength-i] = dImag;
- }
+ //
+ // calculate the FFTs of the two functions...
+ //
+ if (gsl_fft_real_radix2_transform( pdResponse, 1, iLength ) == 0) {
+ if (gsl_fft_real_radix2_transform( pdConvolve, 1, iLength ) == 0) {
+ //
+ // multiply the FFTs together...
+ //
+ for (int i=0; i < iLength/2; i++) {
+ if (i==0 || i==(iLength/2)-1) {
+ pdResponse[i] = pdResponse[i] * pdConvolve[i];
+ } else {
+ dReal = pdResponse[i] * pdConvolve[i] - pdResponse[iLength-i] * pdConvolve[iLength-i];
+ dImag = pdResponse[i] * pdConvolve[iLength-i] + pdResponse[iLength-i] * pdConvolve[i];
+
+ pdResponse[i] = dReal;
+ pdResponse[iLength-i] = dImag;
}
+ }
- //
- // do the inverse FFT...
- //
- if( gsl_fft_halfcomplex_radix2_inverse( pdResponse, 1, iLength ) == 0 )
- {
- if( outArrayLens[0] != inArrayLens[iConvolveIndex] )
- {
- pdResult = (double*)realloc( outArrays[0], inArrayLens[iConvolveIndex] * sizeof( double ) );
+ //
+ // do the inverse FFT...
+ //
+ if (gsl_fft_halfcomplex_radix2_inverse( pdResponse, 1, iLength) == 0 ) {
+ if (convolved->length() != convolve->length()) {
+ pdResult = (double*)realloc( convolved->value(), convolve->length() * sizeof( double ) );
+ } else {
+ pdResult = convolved->value();
+ }
+
+ if (pdResult != NULL) {
+ for (int i = 0; i < convolve->length(); ++i) {
+ convolved->value()[i] = pdResult[i];
}
- else
- {
- pdResult = outArrays[0];
- }
- if( pdResult != NULL )
- {
- outArrays[0] = pdResult;
- outArrayLens[0] = inArrayLens[iConvolveIndex];
+ memcpy( pdResult, pdResponse, convolve->length() * sizeof( double ) );
- memcpy( pdResult, pdResponse, inArrayLens[iConvolveIndex] * sizeof( double ) );
-
- iReturn = 0;
- }
+ iReturn = true;
}
}
}
-
- delete[] pdResponse;
- delete[] pdConvolve;
}
+
+ delete[] pdResponse;
+ delete[] pdConvolve;
}
}
-
+
return iReturn;
}
+
+
+QStringList Convolve::inputVectorList() const {
+ return QStringList( ARRAY_ONE ) << ARRAY_TWO;
+}
+
+
+QStringList Convolve::inputScalarList() const {
+ return QStringList();
+}
+
+
+QStringList Convolve::inputStringList() const {
+ return QStringList();
+}
+
+
+QStringList Convolve::outputVectorList() const {
+ return QStringList( CONVOLVED );
+}
+
+
+QStringList Convolve::outputScalarList() const {
+ return QStringList();
+}
+
+
+QStringList Convolve::outputStringList() const {
+ return QStringList();
+}
+
+#include "convolve.moc"
More information about the Kst
mailing list