VMTK
Loading...
Searching...
No Matches
vtkvmtkDoubleVector.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkDoubleVector.cxx,v $
5 Language: C++
6 Date: $Date: 2006/04/06 16:46:43 $
7 Version: $Revision: 1.3 $
8
9 Copyright (c) Luca Antiga
10
11 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
12 See LICENSE file for details.
13
14 Portions of this code are covered under the VTK copyright.
15 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
16 for details.
17
18 This software is distributed WITHOUT ANY WARRANTY; without even
19 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 PURPOSE. See the above copyright notices for more information.
21
22=========================================================================*/
23
24#include "vtkvmtkDoubleVector.h"
25#include "vtkvmtkConstants.h"
26#include "vtkObjectFactory.h"
27#include <cmath>
28
29
31
33{
35 this->NumberOfElements = 0;
37 this->NumberOfVariables = 0;
38 this->Array = NULL;
39 //this->Locked = NULL;
40}
41
43{
44 if (this->Array != NULL)
45 {
46 delete [] this->Array;
47 this->Array = NULL;
48 }
49 //if (this->Locked != NULL)
50 // {
51 // delete [] this->Locked;
52 // this->Locked = NULL;
53 // }
54}
55
56void vtkvmtkDoubleVector::Allocate(vtkIdType numberOfElementsPerVariable, vtkIdType numberOfVariables)
57{
58 if ( this->Array != NULL )
59 {
60 delete [] this->Array;
61 this->Array = NULL;
62 }
63
64 this->NumberOfElements = numberOfElementsPerVariable * numberOfVariables;
65 this->NumberOfElementsPerVariable = numberOfElementsPerVariable;
66 this->NumberOfVariables = numberOfVariables;
67 this->Array = new double[this->NumberOfElements];
68 //this->Locked = new bool[this->NumberOfElements];
69}
70
71void vtkvmtkDoubleVector::Fill(double value)
72{
73 for (vtkIdType i=0; i<this->NumberOfElements; i++)
74 {
75 this->Array[i] = value;
76 }
77}
78
79//void vtkvmtkDoubleVector::UnlockAll()
80//{
81// for (vtkIdType i=0; i<this->NumberOfElements; i++)
82// {
83// this->Locked[i] = false;
84// }
85//}
86
88{
89 if (this->NumberOfElements != src->NumberOfElements)
90 {
91 vtkErrorMacro(<< "Vectors have different number of elements.");
92 }
93
95 memcpy(this->Array, src->Array, src->NumberOfElements * sizeof(double));
96}
97
98void vtkvmtkDoubleVector::Assign(vtkIdType numberOfElements, const double *array)
99{
100 this->NumberOfElements = numberOfElements;
101 memcpy(this->Array, array, numberOfElements * sizeof(double));
102}
103
105{
106 vtkIdType i;
107 double norm = 0.0;
108 double value;
109
110 if (this->NormType==VTK_VMTK_L2_NORM)
111 {
112 norm = 0.0;
113 for (i=0; i<this->NumberOfElements; i++)
114 {
115 value = this->Array[i];
116 norm += value * value;
117 }
118 norm = std::sqrt(norm);
119 }
120 else if (this->NormType==VTK_VMTK_LINF_NORM)
121 {
122 norm = -VTK_VMTK_LARGE_DOUBLE;
123 for (i=0; i<this->NumberOfElements; i++)
124 {
125 value = this->Array[i];
126 if (value>norm)
127 {
128 norm = value;
129 }
130 }
131 }
132
133 return norm;
134}
135
137{
138 if (this->NumberOfElements != vectorToAdd->NumberOfElements)
139 {
140 vtkErrorMacro(<< "Vector to add does not have the same number of elements.");
141 return;
142 }
143
144 for (int i=0; i<this->NumberOfElements; i++)
145 {
146 this->Array[i] += vectorToAdd->GetElement(i);
147 }
148}
149
151{
152 if (this->NumberOfElements != vectorToSubtract->NumberOfElements)
153 {
154 vtkErrorMacro(<< "Vector to add does not have the same number of elements.");
155 return;
156 }
157
158 for (int i=0; i<this->NumberOfElements; i++)
159 {
160 this->Array[i] -= vectorToSubtract->GetElement(i);
161 }
162}
163
165{
166 for (int i=0; i<this->NumberOfElements; i++)
167 {
168 this->Array[i] *= scalar;
169 }
170}
171
173{
174 double dot;
175
176 if (this->NumberOfElements != vectorToDotWith->NumberOfElements)
177 {
178 vtkErrorMacro(<< "Vector to add does not have the same number of elements.");
179 return 0.0;
180 }
181
182 dot = 0.0;
183 for (int i=0; i<this->NumberOfElements; i++)
184 {
185 dot += this->Array[i] * vectorToDotWith->Array[i];
186 }
187
188 return dot;
189}
190
191void vtkvmtkDoubleVector::CopyIntoArrayComponent(vtkDataArray *array, int component)
192{
193 vtkIdType i;
194
195 if (array==NULL)
196 {
197 vtkErrorMacro(<<"NULL vtkDoubleArray provided!");
198 return;
199 }
200
201 if (component > array->GetNumberOfComponents()-1)
202 {
203 array->SetNumberOfComponents(component+1);
204 }
205
206 if (this->NumberOfElements>array->GetNumberOfTuples())
207 {
208 array->SetNumberOfTuples(this->NumberOfElements);
209 }
210
211 for (i=0; i<this->NumberOfElements; i++)
212 {
213 array->SetComponent(i,component,this->Array[i]);
214 }
215}
216
217void vtkvmtkDoubleVector::CopyVariableIntoArrayComponent(vtkDataArray *array, int variable, int component)
218{
219 vtkIdType i;
220
221 if (array==NULL)
222 {
223 vtkErrorMacro(<<"NULL vtkDoubleArray provided!");
224 return;
225 }
226
227 if (component > array->GetNumberOfComponents()-1)
228 {
229 array->SetNumberOfComponents(component+1);
230 }
231
232 if (this->NumberOfElementsPerVariable > array->GetNumberOfTuples())
233 {
234 array->SetNumberOfTuples(this->NumberOfElementsPerVariable);
235 }
236
237 for (i=0; i<this->NumberOfElementsPerVariable; i++)
238 {
239 array->SetComponent(i,component,this->Array[i+variable*this->NumberOfElementsPerVariable]);
240 }
241}
242
244{
248 if (this->Array != NULL)
249 {
250 delete [] this->Array;
251 this->Array = NULL;
252 }
253
254 this->Array = new double[src->NumberOfElements];
255 memcpy(this->Array, src->Array, src->NumberOfElements * sizeof(double));
256// memcpy(this->Locked, src->Locked, src->NumberOfElements * sizeof(bool));
257}
Serves as the base class for vectors utilized in finite-element and other PDE evaluations.
double Dot(vtkvmtkDoubleVector *vectorToDotWith)
void Subtract(vtkvmtkDoubleVector *vectorToSubtract)
void CopyVariableIntoArrayComponent(vtkDataArray *array, int variable, int component)
void DeepCopy(vtkvmtkDoubleVector *src)
void CopyIntoArrayComponent(vtkDataArray *array, int component)
void Fill(double value)
void Add(vtkvmtkDoubleVector *vectorToAdd)
double GetElement(vtkIdType i)
void Assign(vtkvmtkDoubleVector *src)
void Allocate(vtkIdType numberOfElementsPerVariable, vtkIdType numberOfVariables=1)
void MultiplyBy(double scalar)
#define VTK_VMTK_LARGE_DOUBLE
vtkStandardNewMacro(vtkvmtkDoubleVector)
#define VTK_VMTK_LINF_NORM
#define VTK_VMTK_L2_NORM