HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Complex.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Complex.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  *
10  * Defines a complex number and some simple complex operations.
11  *
12  */
13 
14 #ifndef __UT_COMPLEX__
15 #define __UT_COMPLEX__
16 
17 #include "UT_API.h"
18 #include "UT_Vector2.h"
19 
20 #include <SYS/SYS_Math.h>
21 #include <SYS/SYS_Types.h>
22 #include <SYS/SYS_TypeDecorate.h>
23 
24 
25 template <typename T> class UT_ComplexT;
28 // UT_Complex typedef for backward compatibility with old non-template version.
30 
31 // Declare the following types as POD so that UT_ValArray can be optimized
34 
35 
36 template <typename T>
37 class UT_ComplexT
38 {
39 public:
40  UT_ComplexT();
41  UT_ComplexT(T r,T i);
42  UT_ComplexT(const UT_ComplexT &);
44  : myReal(vec.x())
45  , myImag(vec.y()) {}
46 
47  void set(T r,T i);
48 
49  UT_ComplexT & operator=(const UT_ComplexT &);
50  int operator==(const UT_ComplexT &) const;
51 
52  UT_ComplexT operator+(T) const;
53  UT_ComplexT operator+(const UT_ComplexT &) const;
54  UT_ComplexT operator-(T) const;
55  UT_ComplexT operator-(const UT_ComplexT &) const;
56 
57  UT_ComplexT operator*(T) const;
58  UT_ComplexT operator*(const UT_ComplexT &) const;
59  UT_ComplexT operator/(T) const;
60  UT_ComplexT operator/(const UT_ComplexT &) const;
61 
62  operator UT_Vector2T<T>() const
63  { return UT_Vector2T<T>(myReal, myImag); }
64 
65  UT_ComplexT pow(T exp) const;
66 
67  T real() const { return myReal; }
68  T& real() { return myReal; }
69  T imaginary() const { return myImag; }
70  T& imaginary() { return myImag; }
71 
72  T magnitude() const;
73  T magnitude2() const;
74 
75  // Phase is returned in radians
76  T phase() const;
77 
78 
79 private:
80  T myReal;
81  T myImag;
82 };
83 
84 // Implementation
85 template <typename T>
86 inline
88 {
89  myReal = 0.0F;
90  myImag = 0.0F;
91 }
92 
93 template <typename T>
94 inline
96 {
97  myReal = r;
98  myImag = i;
99 }
100 
101 template <typename T>
102 inline
104 {
105  myReal = source.real();
106  myImag = source.imaginary();
107 }
108 
109 template <typename T>
110 inline void
112 {
113  myReal = r;
114  myImag = i;
115 }
116 
117 template <typename T>
118 inline UT_ComplexT<T> &
120 {
121  myReal = source.real();
122  myImag = source.imaginary();
123 
124  return *this;
125 }
126 
127 template <typename T>
128 inline int
130 {
131  if(SYSisEqual(source.real(), myReal) &&
132  SYSisEqual(source.imaginary(), myImag))
133  return 1;
134  return 0;
135 }
136 
137 template <typename T>
138 inline UT_ComplexT<T>
140 {
141  return UT_ComplexT(myReal + r, myImag);
142 }
143 
144 template <typename T>
145 inline UT_ComplexT<T>
147 {
148  return UT_ComplexT(myReal - r, myImag);
149 }
150 
151 template <typename T>
152 inline UT_ComplexT<T>
154 {
155  return UT_ComplexT(myReal + add.real(), myImag + add.imaginary());
156 }
157 
158 template <typename T>
159 inline UT_ComplexT<T>
161 {
162  return UT_ComplexT(myReal - sub.real(), myImag - sub.imaginary());
163 }
164 
165 template <typename T>
166 inline UT_ComplexT<T>
168 {
169  return UT_ComplexT(myReal * a, myImag * a);
170 }
171 
172 template <typename T>
173 inline UT_ComplexT<T>
175 {
176  T r,i;
177 
178  r = mult.real();
179  i = mult.imaginary();
180 
181  return UT_ComplexT(myReal * r - myImag * i,
182  myReal * i + myImag * r);
183 }
184 
185 template <typename T>
186 inline UT_ComplexT<T>
188 {
189  UT_ComplexT raised;
190  T mag,phs;
191 
192  mag = magnitude();
193  phs=phase();
194 
195  mag = SYSpow(mag, exp);
196 
197  phs *= exp;
198 
199  return UT_ComplexT( mag * SYScos(phs), mag * SYSsin(phs));
200 }
201 
202 template <typename T>
203 inline UT_ComplexT<T>
205 {
206  return UT_ComplexT(myReal/d,myImag/d);
207 }
208 
209 template <typename T>
210 inline UT_ComplexT<T>
212 {
213  T r,i,d;
214 
215  r = div.real();
216  i = div.imaginary();
217 
218  d = r*r + i*i;
219 
220  return UT_ComplexT((myReal * r + myImag * i)/d,
221  (myImag * r - myReal * i)/d);
222 }
223 
224 template <typename T>
225 inline T
227 {
228  return SYSsqrt(myReal*myReal + myImag *myImag);
229 }
230 
231 template <typename T>
232 inline T
234 {
235  return myReal*myReal + myImag *myImag;
236 }
237 
238 template <typename T>
239 inline T
241 {
242  return SYSatan(-myImag, myReal);
243 }
244 
245 #endif
246 
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Multiply each element of the given matrix by scalar and return the result.
Definition: Mat3.h:609
GA_API const UT_StringHolder div
T real() const
Definition: UT_Complex.h:67
UT_ComplexT operator*(T) const
Definition: UT_Complex.h:167
Mat3< typename promote< T0, T1 >::type > operator+(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Add corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:625
UT_ComplexT< fpreal64 > UT_Complex
Definition: UT_Complex.h:29
T & imaginary()
Definition: UT_Complex.h:70
UT_ComplexT operator+(T) const
Definition: UT_Complex.h:139
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
T magnitude2() const
Definition: UT_Complex.h:233
UT_ComplexT< fpreal32 > UT_ComplexF
Definition: UT_Complex.h:25
GLint y
Definition: glcorearb.h:102
2D Vector class.
Definition: UT_Vector2.h:138
SYS_API double pow(double x, double y)
Definition: SYS_FPUMath.h:101
png_uint_32 i
Definition: png.h:2877
UT_ComplexT pow(T exp) const
Definition: UT_Complex.h:187
VectorToScalarConverter< GridType >::Type::Ptr magnitude(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the magnitudes of the vectors of the given vector-valued grid.
T magnitude() const
Definition: UT_Complex.h:226
Mat3< typename promote< T0, T1 >::type > operator-(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Subtract corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:635
UT_ComplexT< fpreal64 > UT_ComplexD
Definition: UT_Complex.h:27
int operator==(const UT_ComplexT &) const
Definition: UT_Complex.h:129
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:802
T & real()
Definition: UT_Complex.h:68
SYS_API double exp(double x)
Definition: SYS_FPUMath.h:97
#define SYS_DECLARE_IS_POD(T)
Declare a type as POD.
UT_ComplexT operator/(T) const
Definition: UT_Complex.h:204
Quat< T > operator/(const Quat< T > &q1, const Quat< T > &q2)
Definition: ImathQuat.h:887
UT_ComplexT & operator=(const UT_ComplexT &)
Definition: UT_Complex.h:119
UT_ComplexT operator-(T) const
Definition: UT_Complex.h:146
T imaginary() const
Definition: UT_Complex.h:69
UT_ComplexT(const UT_Vector2T< T > &vec)
Definition: UT_Complex.h:43
T phase() const
Definition: UT_Complex.h:240
GLint GLenum GLint x
Definition: glcorearb.h:408
void set(T r, T i)
Definition: UT_Complex.h:111
GLboolean r
Definition: glcorearb.h:1221
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:590