Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F82271442
Vector.h
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Sep 10, 12:21
Size
14 KB
Mime Type
text/x-c++
Expires
Thu, Sep 12, 12:21 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
20674452
Attached To
R9411 tisue modeling
Vector.h
View Options
#ifndef __VECTOR_H__
#define __VECTOR_H__
//#include "Macros.h"
#include <math.h>
#include <iostream>
#include <cstdlib>
#include "macros.h"
#include "Macros.h"
#ifndef NULL
#define NULL 0
#endif
#ifdef USE_T_EXTENSIONS
template
<
unsigned
int
ROW
>
class
TVector
;
#endif
class
Vector
{
friend
class
Matrix
;
#ifdef USE_T_EXTENSIONS
template
<
unsigned
int
ROW
>
friend
class
TVector
;
#endif
protected:
static
float
undef
;
unsigned
int
row
;
float
*
_
;
public:
inline
Vector
()
{
row
=
0
;
_
=
NULL
;
}
inline
virtual
~
Vector
(){
Release
();
}
inline
Vector
(
const
Vector
&
vector
)
{
row
=
0
;
_
=
NULL
;
Resize
(
vector
.
row
,
false
);
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
_
[
i
]
=
vector
.
_
[
i
];
}
inline
Vector
(
unsigned
int
size
,
bool
clear
=
true
)
{
row
=
0
;
_
=
NULL
;
Resize
(
size
,
false
);
if
(
clear
)
Zero
();
}
inline
Vector
(
const
float
_
[],
unsigned
int
size
)
{
row
=
0
;
this
->
_
=
NULL
;
Resize
(
size
,
false
);
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
this
->
_
[
i
]
=
_
[
i
];
}
#ifdef USE_T_EXTENSIONS
template
<
unsigned
int
ROW
>
inline
Vector
(
const
TVector
<
ROW
>
&
vector
)
{
row
=
0
;
_
=
NULL
;
Resize
(
ROW
,
false
);
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
_
[
i
]
=
vector
.
_
[
i
];
}
#endif
inline
Vector
&
Zero
()
{
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
_
[
i
]
=
0.0f
;
return
*
this
;
}
inline
Vector
&
One
()
{
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
_
[
i
]
=
1.0f
;
return
*
this
;
}
inline
Vector
&
Random
(){
for
(
unsigned
int
j
=
0
;
j
<
row
;
j
++
)
_
[
j
]
=
((
float
)
rand
())
/
((
float
)(
RAND_MAX
+
1.0
));
return
*
this
;
}
inline
Vector
&
RandomNormal
(
float
mean
,
float
var
){
for
(
unsigned
int
j
=
0
;
j
<
row
;
j
++
)
_
[
j
]
=
norm_sample
(
mean
,
var
);
return
*
this
;
}
inline
unsigned
int
Size
()
const
{
return
row
;
}
inline
float
*
GetArray
()
const
{
return
_
;
}
inline
float
At
(
const
unsigned
int
row
)
const
{
if
(
row
<
this
->
row
)
return
_
[
row
];
return
undef
;
}
inline
float
&
operator
[]
(
const
unsigned
int
row
)
{
if
(
row
<
this
->
row
)
return
_
[
row
];
return
undef
;
}
inline
float
&
operator
[]
(
const
unsigned
int
row
)
const
{
if
(
row
<
this
->
row
)
return
_
[
row
];
return
undef
;
}
inline
float
&
operator
()
(
const
unsigned
int
row
)
{
if
(
row
<
this
->
row
)
return
_
[
row
];
return
undef
;
}
inline
Vector
operator
-
()
const
{
Vector
result
(
row
,
false
);
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
result
.
_
[
i
]
=
-
_
[
i
];
return
result
;
}
inline
Vector
&
Set
(
const
Vector
&
vector
){
return
(
*
this
)
=
vector
;
}
inline
Vector
&
operator
=
(
const
Vector
&
vector
)
{
Resize
(
vector
.
row
,
false
);
const
unsigned
int
k
=
(
row
<=
vector
.
row
?
row
:
vector
.
row
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
)
_
[
i
]
=
vector
.
_
[
i
];
for
(
unsigned
int
i
=
k
;
i
<
row
;
i
++
)
_
[
i
]
=
0
;
return
*
this
;
}
inline
Vector
&
operator
+=
(
const
Vector
&
vector
)
{
const
unsigned
int
k
=
(
row
<=
vector
.
row
?
row
:
vector
.
row
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
)
_
[
i
]
+=
vector
.
_
[
i
];
return
*
this
;
}
inline
Vector
&
operator
-=
(
const
Vector
&
vector
)
{
const
unsigned
int
k
=
(
row
<=
vector
.
row
?
row
:
vector
.
row
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
)
_
[
i
]
-=
vector
.
_
[
i
];
return
*
this
;
}
inline
Vector
&
operator
^=
(
const
Vector
&
vector
)
{
const
unsigned
int
k
=
(
row
<=
vector
.
row
?
row
:
vector
.
row
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
)
_
[
i
]
*=
vector
.
_
[
i
];
return
*
this
;
}
inline
Vector
&
operator
/=
(
const
Vector
&
vector
)
{
const
unsigned
int
k
=
(
row
<=
vector
.
row
?
row
:
vector
.
row
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
)
_
[
i
]
/=
vector
.
_
[
i
];
return
*
this
;
}
inline
Vector
&
operator
+=
(
float
scalar
)
{
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
_
[
i
]
+=
scalar
;
return
*
this
;
}
inline
Vector
&
operator
-=
(
float
scalar
)
{
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
_
[
i
]
-=
scalar
;
return
*
this
;
}
inline
Vector
&
operator
*=
(
float
scalar
)
{
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
_
[
i
]
*=
scalar
;
return
*
this
;
}
inline
Vector
&
operator
/=
(
float
scalar
)
{
scalar
=
1.0f
/
scalar
;
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
_
[
i
]
*=
scalar
;
return
*
this
;
}
inline
Vector
operator
+
(
const
Vector
&
vector
)
const
{
Vector
result
(
row
,
false
);
return
Add
(
vector
,
result
);
}
inline
Vector
&
Add
(
const
Vector
&
vector
,
Vector
&
result
)
const
{
result
.
Resize
(
row
,
false
);
const
unsigned
int
k
=
(
row
<=
vector
.
row
?
row
:
vector
.
row
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
)
result
.
_
[
i
]
=
_
[
i
]
+
vector
.
_
[
i
];
for
(
unsigned
int
i
=
k
;
i
<
row
;
i
++
)
result
.
_
[
i
]
=
_
[
i
];
return
result
;
}
inline
Vector
operator
-
(
const
Vector
&
vector
)
const
{
Vector
result
(
row
,
false
);
return
Sub
(
vector
,
result
);
}
inline
Vector
&
Sub
(
const
Vector
&
vector
,
Vector
&
result
)
const
{
result
.
Resize
(
row
,
false
);
const
unsigned
int
k
=
(
row
<=
vector
.
row
?
row
:
vector
.
row
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
)
result
.
_
[
i
]
=
_
[
i
]
-
vector
.
_
[
i
];
for
(
unsigned
int
i
=
k
;
i
<
row
;
i
++
)
result
.
_
[
i
]
=
_
[
i
];
return
result
;
}
inline
Vector
operator
^
(
const
Vector
&
vector
)
const
{
Vector
result
(
row
,
false
);
return
PMult
(
vector
,
result
);
}
inline
Vector
&
PMult
(
const
Vector
&
vector
,
Vector
&
result
)
const
{
result
.
Resize
(
row
,
false
);
const
unsigned
int
k
=
(
row
<=
vector
.
row
?
row
:
vector
.
row
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
)
result
.
_
[
i
]
=
_
[
i
]
*
vector
.
_
[
i
];
for
(
unsigned
int
i
=
k
;
i
<
row
;
i
++
)
result
.
_
[
i
]
=
_
[
i
];
return
result
;
}
inline
Vector
operator
/
(
const
Vector
&
vector
)
const
{
Vector
result
(
row
,
false
);
return
PDiv
(
vector
,
result
);
}
inline
Vector
&
PDiv
(
const
Vector
&
vector
,
Vector
&
result
)
const
{
result
.
Resize
(
row
,
false
);
const
unsigned
int
k
=
(
row
<=
vector
.
row
?
row
:
vector
.
row
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
)
result
.
_
[
i
]
=
_
[
i
]
/
vector
.
_
[
i
];
for
(
unsigned
int
i
=
k
;
i
<
row
;
i
++
)
result
.
_
[
i
]
=
_
[
i
];
return
result
;
}
inline
float
operator
*
(
const
Vector
&
vector
)
const
{
return
this
->
Dot
(
vector
);
}
inline
Vector
operator
+
(
float
scalar
)
const
{
Vector
result
(
row
,
false
);
return
Add
(
scalar
,
result
);
}
inline
Vector
&
Add
(
float
scalar
,
Vector
&
result
)
const
{
result
.
Resize
(
row
,
false
);
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
result
.
_
[
i
]
=
_
[
i
]
+
scalar
;
return
result
;
}
inline
Vector
operator
-
(
float
scalar
)
const
{
Vector
result
(
row
,
false
);
return
Sub
(
scalar
,
result
);
}
inline
Vector
&
Sub
(
float
scalar
,
Vector
&
result
)
const
{
result
.
Resize
(
row
,
false
);
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
result
.
_
[
i
]
=
_
[
i
]
-
scalar
;
return
result
;
}
inline
Vector
operator
*
(
float
scalar
)
const
{
Vector
result
(
row
,
false
);
return
Mult
(
scalar
,
result
);
}
inline
Vector
&
Mult
(
float
scalar
,
Vector
&
result
)
const
{
result
.
Resize
(
row
,
false
);
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
result
.
_
[
i
]
=
_
[
i
]
*
scalar
;
return
result
;
}
inline
Vector
operator
/
(
float
scalar
)
const
{
Vector
result
(
row
,
false
);
return
Div
(
scalar
,
result
);
}
inline
Vector
&
Div
(
float
scalar
,
Vector
&
result
)
const
{
result
.
Resize
(
row
,
false
);
scalar
=
1.0f
/
scalar
;
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
result
.
_
[
i
]
=
_
[
i
]
*
scalar
;
return
result
;
}
inline
bool
operator
==
(
const
Vector
&
vector
)
const
{
if
(
row
!=
vector
.
row
)
return
false
;
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
if
(
_
[
i
]
!=
vector
.
_
[
i
])
return
false
;
return
true
;
}
inline
bool
operator
!=
(
const
Vector
&
vector
)
const
{
return
!
(
*
this
==
vector
);
}
inline
float
Sum
()
const
{
float
result
=
0.0f
;
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
result
+=
_
[
i
];
return
result
;
}
inline
float
Norm
()
const
{
return
sqrtf
(
Norm2
());
}
inline
float
Norm2
()
const
{
float
result
=
0.0f
;
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
result
+=
_
[
i
]
*
_
[
i
];
return
result
;
}
inline
void
Normalize
()
{
float
n
=
Norm
();
// if(n==0) {throw "Dividing by zero";}
if
(
n
<
EPSILON
)
{
throw
"Dividing by zero"
;}
float
scalar
=
1.0f
/
Norm
();
(
*
this
)
*=
scalar
;
}
inline
float
Distance
(
const
Vector
&
vector
)
const
{
return
(
*
this
-
vector
).
Norm
();
}
inline
float
Distance2
(
const
Vector
&
vector
)
const
{
return
(
*
this
-
vector
).
Norm2
();
}
inline
float
Dot
(
const
Vector
&
vector
)
const
{
const
unsigned
int
k
=
(
row
<=
vector
.
row
?
row
:
vector
.
row
);
float
result
=
0.0f
;
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
)
result
+=
_
[
i
]
*
vector
.
_
[
i
];
return
result
;
}
inline
Vector
&
SetSubVector
(
unsigned
int
startPos
,
const
Vector
&
vector
)
{
if
(
startPos
<
row
){
const
unsigned
int
k
=
(
row
-
startPos
<=
vector
.
row
?
row
-
startPos
:
vector
.
row
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
){
_
[
startPos
+
i
]
=
vector
.
_
[
i
];
}
}
return
*
this
;
}
inline
Vector
GetSubVector
(
unsigned
int
startPos
,
unsigned
int
len
)
{
Vector
result
(
len
,
false
);
return
GetSubVector
(
startPos
,
len
,
result
);
}
inline
Vector
&
GetSubVector
(
unsigned
int
startPos
,
unsigned
int
len
,
Vector
&
result
)
{
result
.
Resize
(
len
,
false
);
if
(
startPos
<
row
){
const
unsigned
int
k
=
(
row
-
startPos
<=
len
?
row
-
startPos
:
len
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
){
result
[
i
]
=
_
[
startPos
+
i
];
}
for
(
unsigned
int
i
=
k
;
i
<
len
;
i
++
){
result
[
i
]
=
0.0f
;
}
}
else
{
result
.
Zero
();
}
return
result
;
}
inline
float
Max
(){
if
(
row
==
0
)
return
0.0f
;
float
res
=
_
[
0
];
for
(
unsigned
int
i
=
1
;
i
<
row
;
i
++
){
if
(
_
[
i
]
>
res
)
res
=
_
[
i
];
}
return
res
;
}
inline
float
Min
(){
if
(
row
==
0
)
return
0.0f
;
float
res
=
_
[
0
];
for
(
unsigned
int
i
=
1
;
i
<
row
;
i
++
){
if
(
_
[
i
]
<
res
)
res
=
_
[
i
];
}
return
res
;
}
inline
int
MaxId
(){
if
(
row
==
0
)
return
-
1
;
float
mx
=
_
[
0
];
int
res
=
0
;
for
(
unsigned
int
i
=
1
;
i
<
row
;
i
++
){
if
(
_
[
i
]
>
mx
){
mx
=
_
[
i
];
res
=
i
;}
}
return
res
;
}
inline
Vector
Log
(){
Vector
result
(
row
);
return
Log
(
result
);
}
inline
Vector
&
Log
(
Vector
&
result
)
const
{
result
.
Resize
(
row
,
false
);
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
){
result
.
_
[
i
]
=
log
(
_
[
i
]);
}
return
result
;
}
inline
Vector
Exp
(){
Vector
result
(
row
);
return
Exp
(
result
);
}
inline
Vector
&
Exp
(
Vector
&
result
)
const
{
result
.
Resize
(
row
,
false
);
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
){
result
.
_
[
i
]
=
exp
(
_
[
i
]);
}
return
result
;
}
inline
Vector
Abs
(){
Vector
result
(
row
);
return
Abs
(
result
);
}
inline
Vector
&
Abs
(
Vector
&
result
)
const
{
result
.
Resize
(
row
,
false
);
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
){
result
.
_
[
i
]
=
fabs
(
_
[
i
]);
}
return
result
;
}
inline
Vector
&
GetSubVector
(
const
Vector
&
ids
,
Vector
&
result
)
const
{
const
unsigned
int
k
=
ids
.
Size
();
result
.
Resize
(
k
);
for
(
unsigned
int
i
=
0
;
i
<
k
;
i
++
){
const
unsigned
int
g
=
(
unsigned
int
)(
fabs
(
ROUND
(
ids
.
_
[
i
])));
if
(
g
<
row
){
result
.
_
[
i
]
=
_
[
g
];
}
else
{
result
.
_
[
i
]
=
0.0f
;
}
}
return
result
;
}
void
Print
()
const
{
std
::
cout
<<
"Vector "
<<
row
<<
std
::
endl
;;
for
(
unsigned
int
i
=
0
;
i
<
row
;
i
++
)
std
::
cout
<<
_
[
i
]
<<
" "
;
std
::
cout
<<
std
::
endl
;
}
protected:
inline
void
Release
(){
if
(
_
!=
NULL
)
delete
[]
_
;
row
=
0
;
_
=
NULL
;
}
public:
inline
virtual
void
Resize
(
unsigned
int
size
,
bool
copy
=
true
){
if
(
row
!=
size
){
if
(
size
){
float
*
arr
=
new
float
[
size
];
if
(
copy
){
unsigned
int
m
=
(
row
<
size
?
row
:
size
);
for
(
unsigned
int
i
=
0
;
i
<
m
;
i
++
)
arr
[
i
]
=
_
[
i
];
for
(
unsigned
int
i
=
m
;
i
<
size
;
i
++
)
arr
[
i
]
=
0.0f
;
}
if
(
_
!=
NULL
)
delete
[]
_
;
_
=
arr
;
row
=
size
;
}
else
{
Release
();
}
}
}
};
std
::
ostream
&
operator
<<
(
std
::
ostream
&
out
,
const
Vector
&
v
);
class
Vec2
:
public
Vector
{
public:
Vec2
()
:
Vector
(
2
,
false
){}
Vec2
(
float
x
,
float
y
)
:
Vector
(
2
,
false
){
_
[
0
]
=
x
;
_
[
1
]
=
y
;}
Vec2
operator
-
(
const
Vec2
&
v
)
const
{
return
Vec2
(
At
(
0
)
-
v
.
At
(
0
),
At
(
1
)
-
v
.
At
(
1
));}
Vec2
operator
+
(
const
Vec2
&
v
)
const
{
return
Vec2
(
At
(
0
)
+
v
.
At
(
0
),
At
(
1
)
+
v
.
At
(
1
));}
Vec2
&
operator
*=
(
const
float
f
){
_
[
0
]
*=
f
;
_
[
1
]
*=
f
;
return
*
this
;}
Vec2
&
operator
*=
(
const
Vec2
&
v
){
_
[
0
]
*=
v
[
0
];
_
[
1
]
*=
v
[
1
];
return
*
this
;}
Vec2
&
operator
/=
(
const
float
f
){
_
[
0
]
/=
f
;
_
[
1
]
/=
f
;
return
*
this
;}
// Vec2& operator =(const Vec2& v){return (Vec2& ) ((*this) =v);}
// Vec2& operator =(const Vector& v){return (Vec2& ) ((Vector)(*this) =v);}
void
set
(
float
x
,
float
y
){
_
[
0
]
=
x
;
_
[
1
]
=
y
;}
void
set
(
double
x
,
double
y
){
_
[
0
]
=
(
float
)
x
;
_
[
1
]
=
(
float
)
y
;}
float
cosAngle
(
const
Vec2
&
v
,
bool
norm
=
false
)
//using cos(a-b) = cosa*cosb+sina*sinb
{
float
l
=
1
,
lv
=
1
;
if
(
!
norm
){
try
{
l
=
Norm
();
lv
=
v
.
Norm
();}
catch
(
const
char
*
msf
){
return
0.0
;}}
return
(
_
[
0
]
*
v
.
At
(
0
)
+
_
[
1
]
*
v
.
At
(
1
))
/
(
l
*
lv
);}
// returns sin(v-this)
float
sinAngle
(
const
Vec2
&
v
,
bool
norm
=
false
){
//using sin(a-b) = sina*cosb-cosa*sinb
float
l
=
1
,
lv
=
1
;
if
(
!
norm
){
try
{
l
=
Norm
();
lv
=
v
.
Norm
();}
catch
(
const
char
*
msf
){
return
0.0
;}}
float
ml
=
l
*
lv
;
if
(
ml
<
0.00000001
)
return
0
;
return
-
(
_
[
1
]
*
v
.
At
(
0
)
-
_
[
0
]
*
v
.
At
(
1
))
/
ml
;
}
float
direction
(){
float
angle
=
asin
(
sinAngle
(
Vec2
(
1
,
0
)));
if
(
_
[
0
]
<
0.0
){
angle
=
PIf
-
angle
;}
//if abs(angle)>pi/2
return
angle
;
}
bool
clockwise
(
Vec2
&
v
){
return
(
_
[
0
]
*
v
.
At
(
1
)
-
_
[
1
]
*
v
.
At
(
0
)
<=
0
);
}
float
weightedNorm
(
Vec2
w
){
return
sqrtf
(
_
[
0
]
*
_
[
0
]
*
w
[
0
]
*
w
[
0
]
+
_
[
1
]
*
_
[
1
]
*
w
[
1
]
*
w
[
1
]);}
float
cosDoubleAngle
(
Vec2
&
v
,
bool
norm
=
false
){
//using cos(2a) = cos2(a)-sin2(a)
Vec2
v1
=*
this
;
/* if(!norm){try{v1.Normalize();v.Normalize();}catch(const char *msf){return 0.0;}} */
/* float c = v1.cosAngle(v,true);float s = v1.sinAngle(v,true); */
//return c*c-s*s;
return
0.0
;
}
};
#endif
Event Timeline
Log In to Comment