Back to index

salome-paravis  6.5.0
vtkMedFraction.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "vtkMedFraction.h"
00021 
00022 #include "vtkObjectFactory.h"
00023 
00024 #include "vtkIntArray.h"
00025 #include "vtkDoubleArray.h"
00026 
00027 vtkCxxRevisionMacro(vtkMedFraction, "$Revision: 1.1.4.4.2.1 $")
00028 vtkStandardNewMacro(vtkMedFraction)
00029 
00030 vtkMedFraction::vtkMedFraction()
00031 {
00032   this->Coefficients = vtkDoubleArray::New();
00033   this->Powers = vtkIntArray::New();
00034   this->DenominatorCoefficients = vtkDoubleArray::New();
00035   this->DenominatorPowers = vtkIntArray::New();
00036   this->NumberOfVariable = 0;
00037 }
00038 
00039 vtkMedFraction::~vtkMedFraction()
00040 {
00041   this->Coefficients->Delete();
00042   this->Powers->Delete();
00043   this->DenominatorCoefficients->Delete();
00044   this->DenominatorPowers->Delete();
00045 }
00046 
00047 vtkIntArray* vtkMedFraction::GetPowers()
00048 {
00049   return this->Powers;
00050 }
00051 
00052 vtkDoubleArray* vtkMedFraction::GetCoefficients()
00053 {
00054   return this->Coefficients;
00055 }
00056 
00057 vtkIntArray* vtkMedFraction::GetDenominatorPowers()
00058 {
00059   return this->DenominatorPowers;
00060 }
00061 
00062 vtkDoubleArray* vtkMedFraction::GetDenominatorCoefficients()
00063 {
00064   return this->DenominatorCoefficients;
00065 }
00066 
00067 void vtkMedFraction::SetNumberOfCoefficients(int ncoeff)
00068 {
00069   this->Powers->SetNumberOfTuples(ncoeff);
00070   this->Coefficients->SetNumberOfTuples(ncoeff);
00071 
00072   int* powers = this->Powers->GetPointer(0);
00073   memset(powers, 0, ncoeff*this->Powers->GetNumberOfComponents()*sizeof(int));
00074 
00075   double* coeffs = this->Coefficients->GetPointer(0);
00076   memset(powers, 0, ncoeff*sizeof(double));
00077 }
00078 
00079 void vtkMedFraction::SetNumberOfDenominatorCoefficients(int ncoeff)
00080 {
00081   this->DenominatorPowers->SetNumberOfTuples(ncoeff);
00082   this->DenominatorCoefficients->SetNumberOfTuples(ncoeff);
00083 
00084   int* powers = this->DenominatorPowers->GetPointer(0);
00085   memset(powers, 0,
00086          ncoeff*this->DenominatorPowers->GetNumberOfComponents()*sizeof(int));
00087 
00088   double* coeffs = this->DenominatorCoefficients->GetPointer(0);
00089   memset(powers, 0, ncoeff*sizeof(double));
00090 }
00091 
00092 void vtkMedFraction::SetNumberOfVariable(int nbofvariable)
00093 {
00094   int nvar;
00095   if(nbofvariable <= 0)
00096     {
00097     nvar = 1;
00098     }
00099   else
00100     {
00101     nvar = nbofvariable;
00102     }
00103 
00104   this->Powers->SetNumberOfComponents(nvar);
00105   this->DenominatorPowers->SetNumberOfComponents(nvar);
00106 
00107   // force an allocation
00108   this->Powers->SetNumberOfTuples(
00109       this->Powers->GetNumberOfTuples());
00110   int* powers = this->Powers->GetPointer(0);
00111   memset(powers, 0,
00112          nvar*this->Powers->GetNumberOfComponents()*sizeof(int));
00113 
00114   this->DenominatorPowers->SetNumberOfTuples(
00115       this->DenominatorPowers->GetNumberOfTuples());
00116   int* denom_powers = this->DenominatorPowers->GetPointer(0);
00117   memset(denom_powers, 0,
00118          nvar*this->DenominatorPowers->GetNumberOfComponents()*sizeof(int));
00119 
00120   this->NumberOfVariable = nbofvariable;
00121 }
00122 
00123 double vtkMedFraction::Evaluate(double* coord)
00124 {
00125   if(this->Coefficients->GetNumberOfTuples() == 0)
00126     {
00127     return 0.0;
00128     }
00129 
00130   if(this->NumberOfVariable == 0)
00131     {
00132     return this->Coefficients->GetValue(0);
00133     }
00134 
00135   double res = 0.0;
00136   for(int coeffid = 0; coeffid <
00137       this->Coefficients->GetNumberOfTuples(); coeffid++)
00138     {
00139     double prod = this->Coefficients->GetValue(coeffid);
00140     for(int varid=0; varid<this->NumberOfVariable; varid++)
00141       {
00142       prod *= pow(coord[varid],
00143                   this->Powers->GetValue(
00144                       this->NumberOfVariable*coeffid+varid));
00145       }
00146     res += prod;
00147     }
00148 
00149   double denom_res = 0.0;
00150 
00151   if(this->DenominatorCoefficients->GetNumberOfTuples() == 0)
00152     {
00153     denom_res = 1.0;
00154     }
00155   else
00156     {
00157     for(int coeffid = 0; coeffid <
00158         this->DenominatorCoefficients->GetNumberOfTuples(); coeffid++)
00159       {
00160       double prod = this->DenominatorCoefficients->GetValue(coeffid);
00161       for(int varid=0; varid<this->NumberOfVariable; varid++)
00162         {
00163         prod *= pow(coord[varid],
00164                     this->DenominatorPowers->GetValue(
00165                         this->NumberOfVariable*coeffid+varid));
00166         }
00167       denom_res += prod;
00168       }
00169     }
00170 
00171   return res / denom_res;
00172 }
00173 
00174 double vtkMedFraction::Evaluate1(double coord)
00175 {
00176   if(this->NumberOfVariable != 1)
00177     {
00178     vtkErrorMacro("Evaluate1 can only be called if the NumberOfVariable is 1");
00179     }
00180 
00181   return this->Evaluate(&coord);
00182 }
00183 
00184 double vtkMedFraction::Evaluate2(double x, double y)
00185 {
00186   if(this->NumberOfVariable != 2)
00187     {
00188     vtkErrorMacro("Evaluate2 can only be called if the NumberOfVariable is 2");
00189     }
00190 
00191   double coord[2] = {x, y};
00192 
00193   return this->Evaluate(coord);
00194 }
00195 
00196 double vtkMedFraction::Evaluate3(double x, double y, double z)
00197 {
00198   if(this->NumberOfVariable != 3)
00199     {
00200     vtkErrorMacro("Evaluate3 can only be called if the NumberOfVariable is 3");
00201     }
00202 
00203   double coord[3] = {x, y, z};
00204 
00205   return this->Evaluate(coord);
00206 }
00207 
00208 void vtkMedFraction::PrintSelf(ostream& os, vtkIndent indent)
00209 {
00210   this->Superclass::PrintSelf(os, indent);
00211 }