1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkTensor.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 // .NAME vtkTensor - supporting class to enable assignment and referencing of tensors
16 // .SECTION Description
17 // vtkTensor is a floating point representation of an nxn tensor. vtkTensor
18 // provides methods for assignment and reference of tensor components. It
19 // does it in such a way as to minimize data copying.
20 //
21 // .SECTION Caveats
22 // vtkTensor performs its operations using pointer reference. You are
23 // responsible for supplying data storage (if necessary) if local copies
24 // of data are being made.
25 
26 #ifndef vtkTensor_h
27 #define vtkTensor_h
28 
29 #include "vtkCommonDataModelModule.h" // For export macro
30 #include "vtkObject.h"
31 
32 class VTKCOMMONDATAMODEL_EXPORT vtkTensor : public vtkObject
33 {
34 public:
35   static vtkTensor *New();
36   vtkTypeMacro(vtkTensor,vtkObject);
37   void PrintSelf(ostream& os, vtkIndent indent);
38 
39   // Description:
40   // Initialize tensor components to 0.0.
41   void Initialize();
42 
43   // Description:
44   // Get the tensor component (i,j).
GetComponent(int i,int j)45   double GetComponent(int i, int j) {return this->T[i+3*j];};
46 
47   // Description:
48   // Set the value of the tensor component (i,j).
SetComponent(int i,int j,double v)49   void SetComponent(int i, int j, double v) {if (i > 2 || j > 2) {vtkErrorMacro("trying to set tensor component i or j > 2: i = " << i << ", j = " << j); return;}; this->T[i+3*j] = v;};
50 
51   // Description:
52   // Add to the value of the tensor component at location (i,j).
AddComponent(int i,int j,double v)53   void AddComponent(int i, int j, double v) { if (i > 2 || j > 2) {vtkErrorMacro("trying to add tensor component i or j > 2: i = " << i << ", j = " << j); return;}; this->T[i+3*j] += v;};
54 
55   // Description:
56   // Return column vector from tensor. (Assumes 2D matrix form and 0-offset.)
GetColumn(int j)57   double *GetColumn(int j) { if (j > 2) {vtkErrorMacro("trying to get tensor column j > 2: j = " << j); return NULL;}; return this->T + 3*j;};
58 
59   // Description:
60   // Deep copy of one tensor to another tensor.
61   void DeepCopy(vtkTensor *t);
62 
63   // Description:
64   // Provide double * type conversion.
65   operator double*() {return this->T;};
66 
67   // Description:
68   // Data member left public for efficiency.
69   double *T;
70 
71 protected:
72   vtkTensor();
~vtkTensor()73   ~vtkTensor() {}
74 
75   double Storage[9];
76 private:
77   vtkTensor(const vtkTensor&);  // Not implemented.
78   void operator=(const vtkTensor&);  // Not implemented.
79 };
80 
81 //----------------------------------------------------------------------------
Initialize()82 inline void vtkTensor::Initialize()
83 {
84   for (int j=0; j<3; j++)
85     {
86     for (int i=0; i<3; i++)
87       {
88       this->T[i+j*3] = 0.0;
89       }
90     }
91 }
92 
93 //----------------------------------------------------------------------------
DeepCopy(vtkTensor * t)94 inline void vtkTensor::DeepCopy(vtkTensor *t)
95 {
96   for (int j=0; j < 3; j++)
97     {
98     for (int i=0; i < 3; i++)
99       {
100       this->T[i+3*j] = t->T[i+3*j];
101       }
102     }
103 }
104 
105 #endif
106