[Kst] extragear/graphics/kst/src/plugins/phase

Adam Treat treat at kde.org
Fri Dec 8 21:02:09 CET 2006


SVN commit 611567 by treat:

* Another plugin conversion.  This was is a bit
different since I had to change the name of the tag
for the data out array.  Is this a problem?


 M  +7 -7      Makefile.am  
 A             kstobject_phase.desktop  
 M  +161 -122  phase.cpp  
 A             phase.h   [License: GPL (v2+)]
 D             phase.xml  


--- trunk/extragear/graphics/kst/src/plugins/phase/Makefile.am #611566:611567
@@ -1,11 +1,11 @@
-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=phase.la
+kde_module_LTLIBRARIES=kstobject_phase.la
 
-phase_la_LDFLAGS=-module $(KDE_PLUGIN) $(all_libraries)
-phase_la_SOURCES=phase.cpp
+kstobject_phase_la_LDFLAGS=$(all_libraries) -module -avoid-version
+kstobject_phase_la_SOURCES=phase.cpp
 
+services_DATA=kstobject_phase.desktop
+servicesdir=$(kde_servicesdir)/kst
+
 METASOURCES=AUTO
-
-install_DATA=phase.xml
--- trunk/extragear/graphics/kst/src/plugins/phase/phase.cpp #611566:611567
@@ -1,133 +1,172 @@
-/*
- *  Phase plugin for KST.
- *  Copyright 2003, The University of British Columbia
- *  Released under the terms of the GPL.
- *
- */
+/***************************************************************************
+                   phase.cpp
+                             -------------------
+    begin                : 12/08/06
+    copyright            : (C) 2006 The University of Toronto
+    email                :
+ ***************************************************************************/
 
-#include <stdlib.h>
-#include <math.h>
-#include <string.h>
-#define KST_UNUSED(x) if(x){};
+/***************************************************************************
+ *                                                                         *
+ *   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 "phase.h"
 
-void swap( double* pData[], int iOne, int iTwo );
-void quicksort( double* pData[], int iLeft, int iRight );
-extern "C" int phase( const double *const inArrays[], const int inArrayLens[],
-		const double inScalars[],
-		double *outArrays[], int outArrayLens[],
-		double outScalars[] );
+#include <kgenericfactory.h>
 
-void swap( double* pData[], int iOne, int iTwo )
-{
-    double dTemp;
-    int i;
-    
-    for( i=0; i<2; i++ )
-    {
-        dTemp = pData[i][iOne];
-        pData[i][iOne] = pData[i][iTwo];
-        pData[i][iTwo] = dTemp;
-    }
-}
+static const QString& TIME = KGlobal::staticQString("Time Array");
+static const QString& DATA_I = KGlobal::staticQString("Data In Array");
+static const QString& PERIOD = KGlobal::staticQString("Period");
+static const QString& ZERO = KGlobal::staticQString("Zero Phase");
+static const QString& PHASE = KGlobal::staticQString("Phase Array");
+static const QString& DATA_O = KGlobal::staticQString("Data Out Array");
 
-void quicksort( double* pData[], int iLeft, int iRight )
-{
-    double dVal = pData[0][iRight];
-    int i = iLeft - 1;
-    int j = iRight;
-    
-    if( iRight <= iLeft )
-    {
-        return;
-    }
-    
-    while( 1 )
-    {
-        while( pData[0][++i] < dVal )
-        {
+KST_KEY_DATAOBJECT_PLUGIN( phase )
+
+K_EXPORT_COMPONENT_FACTORY( kstobject_phase,
+                            KGenericFactory<Phase>( "kstobject_phase" ) )
+
+Phase::Phase( QObject */*parent*/, const char */*name*/, const QStringList &/*args*/ )
+    : KstBasicPlugin() {}
+
+
+Phase::~Phase() {}
+
+
+bool Phase::algorithm() {
+
+  KstVectorPtr time     = inputVector(TIME);
+  KstVectorPtr data_i   = inputVector(DATA_I);
+  KstScalarPtr period   = inputScalar(PERIOD);
+  KstScalarPtr zero     = inputScalar(ZERO);
+  KstVectorPtr phase    = outputVector(PHASE);
+  KstVectorPtr data_o   = outputVector(DATA_O);
+
+  double* pResult[2];
+  double  dPhasePeriod = period->value();
+  double dPhaseZero = zero->value();
+  int iLength;
+
+  bool iRetVal = false;
+
+  if (dPhasePeriod > 0.0) {
+    if (time->length() == data_i->length()) {
+      iLength = time->length();
+
+      if (phase->length() != iLength) {
+        phase->resize(iLength, true);
+        pResult[0] = (double*)realloc( phase->value(), iLength * sizeof( double ) );
+      } else {
+        pResult[0] = phase->value();
+      }
+
+      if (data_o->length() != iLength) {
+        data_o->resize(iLength, true);
+        pResult[1] = (double*)realloc( data_o->value(), iLength * sizeof( double ) );
+      } else {
+        pResult[1] = data_o->value();
+      }
+
+      if (pResult[0] != NULL && pResult[1] != NULL) {
+        for (int i = 0; i < phase->length(); ++i) {
+          phase->value()[i] = pResult[0][i];
         }
-        
-        while( dVal < pData[0][--j] )
-        {
-            if( j == iLeft )
-            {
-                break;
-            }
+        for (int i = 0; i < data_o->length(); ++i) {
+          data_o->value()[i] = pResult[1][i];
         }
-        if( i >= j )
-        {
-            break;
+
+        /*
+        determine the phase...
+        */
+        for (int i=0; i<iLength; i++) {
+          phase->value()[i] = fmod( ( time->value()[i] - dPhaseZero ) / dPhasePeriod, 1.0 );
         }
-        swap( pData, i, j );
+
+        /*
+        sort by phase...
+        */
+        memcpy( data_o->value(), data_i->value(), iLength * sizeof( double ) );
+        double* sort[2];
+        sort[0] = phase->value();
+        sort[1] = data_o->value();
+        quicksort( sort, 0, iLength-1 );
+
+        iRetVal = true;
+      }
     }
-    swap( pData, i, iRight );
-    quicksort( pData, iLeft, i-1 );
-    quicksort( pData, i+1, iRight );
+  }
+
+  return iRetVal;
 }
 
-int phase( const double *const inArrays[], const int inArrayLens[],
-		const double inScalars[],
-		double *outArrays[], int outArrayLens[],
-		double outScalars[] )
-{
-    KST_UNUSED( outScalars )
-    
-    double* pResult[2];
-    double	dPhasePeriod = inScalars[0];
-    double dPhaseZero = inScalars[1];
-    int iLength;
-    int iRetVal = -1;
-    int i;
-        
-    if( dPhasePeriod > 0.0 )
-    {
-        if( inArrayLens[0] == inArrayLens[1] )
-        {
-            iLength = inArrayLens[0];
-        
-            if( outArrayLens[0] != iLength )
-            {
-                pResult[0] = (double*)realloc( outArrays[0], iLength * sizeof( double ) );
-            }
-            else
-            {
-                pResult[0] = outArrays[0];
-            }
-        
-            if( outArrayLens[1] != iLength )
-            {
-                pResult[1] = (double*)realloc( outArrays[1], iLength * sizeof( double ) );
-            }
-            else
-            {
-                pResult[1] = outArrays[1];
-            }
-        
-            if( pResult[0] != NULL && pResult[1] != NULL )
-            {	
-                outArrays[0] = pResult[0];
-                outArrays[1] = pResult[1];
-                outArrayLens[0] = iLength;
-                outArrayLens[1] = iLength;
-                
-                /*
-                determine the phase...
-                */
-                for( i=0; i<iLength; i++ )
-                {
-                    outArrays[0][i] = fmod( ( inArrays[0][i] - dPhaseZero ) / dPhasePeriod, 1.0 );
-                }
-                
-                /*
-                sort by phase...
-                */
-                memcpy( outArrays[1], inArrays[1], iLength * sizeof( double ) );
-                quicksort( outArrays, 0, iLength-1 );
-                
-                iRetVal = 0;
-            }
-        }
-    }   
-    
-    return iRetVal;
+QStringList Phase::inputVectorList() const {
+  return QStringList( TIME ) << DATA_I;
 }
+
+
+QStringList Phase::inputScalarList() const {
+  return QStringList( PERIOD ) << ZERO;
+}
+
+
+QStringList Phase::inputStringList() const {
+  return QStringList();
+}
+
+
+QStringList Phase::outputVectorList() const {
+  return QStringList( PHASE ) << DATA_O;
+}
+
+
+QStringList Phase::outputScalarList() const {
+  return QStringList();
+}
+
+
+QStringList Phase::outputStringList() const {
+  return QStringList();
+}
+
+void Phase::swap(double* pData[], int iOne, int iTwo) {
+  double dTemp;
+
+  for (int i=0; i<2; i++) {
+    dTemp = pData[i][iOne];
+    pData[i][iOne] = pData[i][iTwo];
+    pData[i][iTwo] = dTemp;
+  }
+}
+
+void Phase::quicksort( double* pData[], int iLeft, int iRight) {
+  double dVal = pData[0][iRight];
+  int i = iLeft - 1;
+  int j = iRight;
+
+  if (iRight <= iLeft) {
+    return;
+  }
+
+  while (1) {
+    while (pData[0][++i] < dVal) {}
+
+    while (dVal < pData[0][--j]) {
+      if (j == iLeft) {
+        break;
+      }
+    }
+    if (i >= j) {
+      break;
+    }
+    swap( pData, i, j );
+  }
+  swap( pData, i, iRight );
+  quicksort( pData, iLeft, i-1 );
+  quicksort( pData, i+1, iRight );
+}
+
+#include "phase.moc"


More information about the Kst mailing list