1 // Copyright (C) 2010-2011 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "vtkMedFraction.h"
22 #include "vtkObjectFactory.h"
24 #include "vtkIntArray.h"
25 #include "vtkDoubleArray.h"
27 // vtkCxxRevisionMacro(vtkMedFraction, "$Revision$")
28 vtkStandardNewMacro(vtkMedFraction)
30 vtkMedFraction::vtkMedFraction()
32 this->Coefficients = vtkDoubleArray::New();
33 this->Powers = vtkIntArray::New();
34 this->DenominatorCoefficients = vtkDoubleArray::New();
35 this->DenominatorPowers = vtkIntArray::New();
36 this->NumberOfVariable = 0;
39 vtkMedFraction::~vtkMedFraction()
41 this->Coefficients->Delete();
42 this->Powers->Delete();
43 this->DenominatorCoefficients->Delete();
44 this->DenominatorPowers->Delete();
47 vtkIntArray* vtkMedFraction::GetPowers()
52 vtkDoubleArray* vtkMedFraction::GetCoefficients()
54 return this->Coefficients;
57 vtkIntArray* vtkMedFraction::GetDenominatorPowers()
59 return this->DenominatorPowers;
62 vtkDoubleArray* vtkMedFraction::GetDenominatorCoefficients()
64 return this->DenominatorCoefficients;
67 void vtkMedFraction::SetNumberOfCoefficients(int ncoeff)
69 this->Powers->SetNumberOfTuples(ncoeff);
70 this->Coefficients->SetNumberOfTuples(ncoeff);
72 int* powers = this->Powers->GetPointer(0);
73 memset(powers, 0, ncoeff*this->Powers->GetNumberOfComponents()*sizeof(int));
75 double* coeffs = this->Coefficients->GetPointer(0);
76 memset(powers, 0, ncoeff*sizeof(double));
79 void vtkMedFraction::SetNumberOfDenominatorCoefficients(int ncoeff)
81 this->DenominatorPowers->SetNumberOfTuples(ncoeff);
82 this->DenominatorCoefficients->SetNumberOfTuples(ncoeff);
84 int* powers = this->DenominatorPowers->GetPointer(0);
86 ncoeff*this->DenominatorPowers->GetNumberOfComponents()*sizeof(int));
88 double* coeffs = this->DenominatorCoefficients->GetPointer(0);
89 memset(powers, 0, ncoeff*sizeof(double));
92 void vtkMedFraction::SetNumberOfVariable(int nbofvariable)
104 this->Powers->SetNumberOfComponents(nvar);
105 this->DenominatorPowers->SetNumberOfComponents(nvar);
107 // force an allocation
108 this->Powers->SetNumberOfTuples(
109 this->Powers->GetNumberOfTuples());
110 int* powers = this->Powers->GetPointer(0);
112 nvar*this->Powers->GetNumberOfComponents()*sizeof(int));
114 this->DenominatorPowers->SetNumberOfTuples(
115 this->DenominatorPowers->GetNumberOfTuples());
116 int* denom_powers = this->DenominatorPowers->GetPointer(0);
117 memset(denom_powers, 0,
118 nvar*this->DenominatorPowers->GetNumberOfComponents()*sizeof(int));
120 this->NumberOfVariable = nbofvariable;
123 double vtkMedFraction::Evaluate(double* coord)
125 if(this->Coefficients->GetNumberOfTuples() == 0)
130 if(this->NumberOfVariable == 0)
132 return this->Coefficients->GetValue(0);
136 for(int coeffid = 0; coeffid <
137 this->Coefficients->GetNumberOfTuples(); coeffid++)
139 double prod = this->Coefficients->GetValue(coeffid);
140 for(int varid=0; varid<this->NumberOfVariable; varid++)
142 prod *= pow(coord[varid],
143 this->Powers->GetValue(
144 this->NumberOfVariable*coeffid+varid));
149 double denom_res = 0.0;
151 if(this->DenominatorCoefficients->GetNumberOfTuples() == 0)
157 for(int coeffid = 0; coeffid <
158 this->DenominatorCoefficients->GetNumberOfTuples(); coeffid++)
160 double prod = this->DenominatorCoefficients->GetValue(coeffid);
161 for(int varid=0; varid<this->NumberOfVariable; varid++)
163 prod *= pow(coord[varid],
164 this->DenominatorPowers->GetValue(
165 this->NumberOfVariable*coeffid+varid));
171 return res / denom_res;
174 double vtkMedFraction::Evaluate1(double coord)
176 if(this->NumberOfVariable != 1)
178 vtkErrorMacro("Evaluate1 can only be called if the NumberOfVariable is 1");
181 return this->Evaluate(&coord);
184 double vtkMedFraction::Evaluate2(double x, double y)
186 if(this->NumberOfVariable != 2)
188 vtkErrorMacro("Evaluate2 can only be called if the NumberOfVariable is 2");
191 double coord[2] = {x, y};
193 return this->Evaluate(coord);
196 double vtkMedFraction::Evaluate3(double x, double y, double z)
198 if(this->NumberOfVariable != 3)
200 vtkErrorMacro("Evaluate3 can only be called if the NumberOfVariable is 3");
203 double coord[3] = {x, y, z};
205 return this->Evaluate(coord);
208 void vtkMedFraction::PrintSelf(ostream& os, vtkIndent indent)
210 this->Superclass::PrintSelf(os, indent);