Sfoglia il codice sorgente

Cleanup ctkCoordinatesWidget

Factorize code and add squaredNorm utility method.
Julien Finet 12 anni fa
parent
commit
5b183852b9
2 ha cambiato i file con 30 aggiunte e 10 eliminazioni
  1. 27 10
      Libs/Widgets/ctkCoordinatesWidget.cpp
  2. 3 0
      Libs/Widgets/ctkCoordinatesWidget.h

+ 27 - 10
Libs/Widgets/ctkCoordinatesWidget.cpp

@@ -19,8 +19,9 @@
 =========================================================================*/
 
 // Qt includes
-#include <QHBoxLayout>
+#include <QDebug>
 #include <QDoubleSpinBox>
+#include <QHBoxLayout>
 
 // CTK includes
 #include "ctkCoordinatesWidget.h"
@@ -36,17 +37,14 @@ ctkCoordinatesWidget::ctkCoordinatesWidget(QWidget* _parent) :QWidget(_parent)
   this->Minimum = -100000.;
   this->Maximum = 100000.;
   this->Normalized = false;
-  this->Dimension = 3;
-  this->Coordinates = new double [this->Dimension];
+  this->Dimension = 0;
+  this->Coordinates = 0;
 
   QHBoxLayout* hboxLayout = new QHBoxLayout(this);
-  this->setLayout(hboxLayout);
-  for (int i = 0; i < this->Dimension; ++i)
-    {
-    this->Coordinates[i] = 0.;
-    this->addSpinBox();
-    }
   hboxLayout->setContentsMargins(0, 0, 0, 0);
+  this->setLayout(hboxLayout);
+
+  this->setDimension(3);
 }
 
 //------------------------------------------------------------------------------
@@ -357,6 +355,7 @@ void ctkCoordinatesWidget::updateCoordinate(double coordinate)
       mult = false;
       den = sqrt((1. - coordinate*coordinate) / (this->Dimension - 1));
       }
+    // Normalize coordinates
     double* normalizedCoordinates = new double[this->Dimension];
     for (int i = 0; i < this->Dimension; ++i)
       {
@@ -420,12 +419,30 @@ double ctkCoordinatesWidget::normalize(double* coordinates, int dimension)
 }
 
 //------------------------------------------------------------------------------
+double ctkCoordinatesWidget::norm()const
+{
+  return ctkCoordinatesWidget::norm(this->Coordinates, this->Dimension);
+}
+
+//------------------------------------------------------------------------------
 double ctkCoordinatesWidget::norm(double* coordinates, int dimension)
 {
+  return sqrt(ctkCoordinatesWidget::squaredNorm(coordinates, dimension));
+}
+
+//------------------------------------------------------------------------------
+double ctkCoordinatesWidget::squaredNorm()const
+{
+  return ctkCoordinatesWidget::squaredNorm(this->Coordinates, this->Dimension);
+}
+
+//------------------------------------------------------------------------------
+double ctkCoordinatesWidget::squaredNorm(double* coordinates, int dimension)
+{
   double sum = 0.;
   for (int i = 0; i < dimension; ++i)
     {
     sum += coordinates[i] * coordinates[i];
     }
-  return sqrt(sum);
+  return sum;
 }

+ 3 - 0
Libs/Widgets/ctkCoordinatesWidget.h

@@ -87,6 +87,8 @@ public:
 
   /// Return the norm of the coordinates.
   double norm()const;
+  /// Return the squared norm of the coordinates.
+  double squaredNorm()const;
 
   /// Set/Get the coordinates. Use commas to separate elements, spaces are
   /// allowed: e.g. "0,0.0, 0."
@@ -123,6 +125,7 @@ protected:
 
   /// Compute the norm of a coordinates \a dimension vector
   static double norm(double* coordinates, int dimension);
+  static double squaredNorm(double* coordinates, int dimension);
 
   int     Decimals;
   double  SingleStep;