Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/paravis.git] / src / Plugins / MedReader / IO / vtkMedFraction.cxx
1 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "vtkMedFraction.h"
21
22 #include "vtkObjectFactory.h"
23
24 #include "vtkIntArray.h"
25 #include "vtkDoubleArray.h"
26
27 vtkCxxRevisionMacro(vtkMedFraction, "$Revision$")
28 vtkStandardNewMacro(vtkMedFraction)
29
30 vtkMedFraction::vtkMedFraction()
31 {
32   this->Coefficients = vtkDoubleArray::New();
33   this->Powers = vtkIntArray::New();
34   this->DenominatorCoefficients = vtkDoubleArray::New();
35   this->DenominatorPowers = vtkIntArray::New();
36   this->NumberOfVariable = 0;
37 }
38
39 vtkMedFraction::~vtkMedFraction()
40 {
41   this->Coefficients->Delete();
42   this->Powers->Delete();
43   this->DenominatorCoefficients->Delete();
44   this->DenominatorPowers->Delete();
45 }
46
47 vtkIntArray* vtkMedFraction::GetPowers()
48 {
49   return this->Powers;
50 }
51
52 vtkDoubleArray* vtkMedFraction::GetCoefficients()
53 {
54   return this->Coefficients;
55 }
56
57 vtkIntArray* vtkMedFraction::GetDenominatorPowers()
58 {
59   return this->DenominatorPowers;
60 }
61
62 vtkDoubleArray* vtkMedFraction::GetDenominatorCoefficients()
63 {
64   return this->DenominatorCoefficients;
65 }
66
67 void vtkMedFraction::SetNumberOfCoefficients(int ncoeff)
68 {
69   this->Powers->SetNumberOfTuples(ncoeff);
70   this->Coefficients->SetNumberOfTuples(ncoeff);
71
72   int* powers = this->Powers->GetPointer(0);
73   memset(powers, 0, ncoeff*this->Powers->GetNumberOfComponents()*sizeof(int));
74
75   double* coeffs = this->Coefficients->GetPointer(0);
76   memset(powers, 0, ncoeff*sizeof(double));
77 }
78
79 void vtkMedFraction::SetNumberOfDenominatorCoefficients(int ncoeff)
80 {
81   this->DenominatorPowers->SetNumberOfTuples(ncoeff);
82   this->DenominatorCoefficients->SetNumberOfTuples(ncoeff);
83
84   int* powers = this->DenominatorPowers->GetPointer(0);
85   memset(powers, 0,
86          ncoeff*this->DenominatorPowers->GetNumberOfComponents()*sizeof(int));
87
88   double* coeffs = this->DenominatorCoefficients->GetPointer(0);
89   memset(powers, 0, ncoeff*sizeof(double));
90 }
91
92 void vtkMedFraction::SetNumberOfVariable(int nbofvariable)
93 {
94   int nvar;
95   if(nbofvariable <= 0)
96     {
97     nvar = 1;
98     }
99   else
100     {
101     nvar = nbofvariable;
102     }
103
104   this->Powers->SetNumberOfComponents(nvar);
105   this->DenominatorPowers->SetNumberOfComponents(nvar);
106
107   // force an allocation
108   this->Powers->SetNumberOfTuples(
109       this->Powers->GetNumberOfTuples());
110   int* powers = this->Powers->GetPointer(0);
111   memset(powers, 0,
112          nvar*this->Powers->GetNumberOfComponents()*sizeof(int));
113
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));
119
120   this->NumberOfVariable = nbofvariable;
121 }
122
123 double vtkMedFraction::Evaluate(double* coord)
124 {
125   if(this->Coefficients->GetNumberOfTuples() == 0)
126     {
127     return 0.0;
128     }
129
130   if(this->NumberOfVariable == 0)
131     {
132     return this->Coefficients->GetValue(0);
133     }
134
135   double res = 0.0;
136   for(int coeffid = 0; coeffid <
137       this->Coefficients->GetNumberOfTuples(); coeffid++)
138     {
139     double prod = this->Coefficients->GetValue(coeffid);
140     for(int varid=0; varid<this->NumberOfVariable; varid++)
141       {
142       prod *= pow(coord[varid],
143                   this->Powers->GetValue(
144                       this->NumberOfVariable*coeffid+varid));
145       }
146     res += prod;
147     }
148
149   double denom_res = 0.0;
150
151   if(this->DenominatorCoefficients->GetNumberOfTuples() == 0)
152     {
153     denom_res = 1.0;
154     }
155   else
156     {
157     for(int coeffid = 0; coeffid <
158         this->DenominatorCoefficients->GetNumberOfTuples(); coeffid++)
159       {
160       double prod = this->DenominatorCoefficients->GetValue(coeffid);
161       for(int varid=0; varid<this->NumberOfVariable; varid++)
162         {
163         prod *= pow(coord[varid],
164                     this->DenominatorPowers->GetValue(
165                         this->NumberOfVariable*coeffid+varid));
166         }
167       denom_res += prod;
168       }
169     }
170
171   return res / denom_res;
172 }
173
174 double vtkMedFraction::Evaluate1(double coord)
175 {
176   if(this->NumberOfVariable != 1)
177     {
178     vtkErrorMacro("Evaluate1 can only be called if the NumberOfVariable is 1");
179     }
180
181   return this->Evaluate(&coord);
182 }
183
184 double vtkMedFraction::Evaluate2(double x, double y)
185 {
186   if(this->NumberOfVariable != 2)
187     {
188     vtkErrorMacro("Evaluate2 can only be called if the NumberOfVariable is 2");
189     }
190
191   double coord[2] = {x, y};
192
193   return this->Evaluate(coord);
194 }
195
196 double vtkMedFraction::Evaluate3(double x, double y, double z)
197 {
198   if(this->NumberOfVariable != 3)
199     {
200     vtkErrorMacro("Evaluate3 can only be called if the NumberOfVariable is 3");
201     }
202
203   double coord[3] = {x, y, z};
204
205   return this->Evaluate(coord);
206 }
207
208 void vtkMedFraction::PrintSelf(ostream& os, vtkIndent indent)
209 {
210   this->Superclass::PrintSelf(os, indent);
211 }