Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F103677304
svector.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
Mon, Mar 3, 23:06
Size
4 KB
Mime Type
text/x-c++
Expires
Wed, Mar 5, 23:06 (2 d)
Engine
blob
Format
Raw Data
Handle
24636481
Attached To
rSYMKIT symkit
svector.h
View Options
#ifndef __STATIC_VECTOR_H__
#define __STATIC_VECTOR_H__
#include <initializer_list>
#include <array>
#include <cmath>
// needed for sqrt
template
<
std
::
size_t
N
>
class
SVector
{
public:
typedef
std
::
size_t
size_t
;
/* Constructors */
SVector
(
bool
null
=
false
)
{
if
(
null
)
this
->
null
();
}
SVector
(
const
std
::
initializer_list
<
double
>
&
init
)
{
if
(
init
.
size
()
!=
N
)
return
;
// TODO throw exception
/* copy init list without include <algorithm> */
size_t
index
=
0
;
for
(
auto
c
:
init
)
this
->
components
[
index
++
]
=
c
;
}
SVector
(
const
SVector
<
N
>&
init
)
{
*
this
=
init
;
}
/* For auto implementation */
/* it allows to loop through components
*
* for (auto comp : v)
* {
* // loop content
* }
*/
typedef
typename
std
::
array
<
double
,
N
>::
iterator
iterator
;
typedef
typename
std
::
array
<
double
,
N
>::
const_iterator
const_iterator
;
iterator
begin
()
{
return
components
.
begin
();
}
iterator
end
()
{
return
components
.
end
();
}
const_iterator
begin
()
const
{
return
components
.
begin
();
}
const_iterator
end
()
const
{
return
components
.
end
();
}
/* Reduce to module = 1 */
SVector
<
N
>
unit
()
const
{
return
*
this
/
this
->
module
();
}
size_t
size
()
const
{
return
N
;
// quite basic
}
double
&
operator
[](
size_t
i
)
{
return
components
[
i
];
}
const
double
&
operator
[](
size_t
i
)
const
{
return
components
[
i
];
}
bool
operator
==
(
const
SVector
<
N
>&
v
)
const
{
for
(
size_t
i
=
0
;
i
<
N
;
++
i
)
{
if
(
components
[
i
]
!=
v
[
i
])
return
false
;
}
return
true
;
}
bool
operator
!=
(
const
SVector
<
N
>&
v
)
const
{
return
!
(
*
this
==
v
);
}
/* Plus / minus */
SVector
<
N
>&
operator
+=
(
const
SVector
<
N
>&
v
)
{
for
(
size_t
i
=
0
;
i
<
N
;
++
i
)
components
[
i
]
+=
v
[
i
];
return
*
this
;
}
SVector
<
N
>&
operator
-=
(
const
SVector
<
N
>&
v
)
{
for
(
size_t
i
=
0
;
i
<
N
;
++
i
)
components
[
i
]
-=
v
[
i
];
return
*
this
;
}
/* Scalar multiplication / division */
SVector
<
N
>&
operator
*=
(
double
k
)
{
for
(
size_t
i
=
0
;
i
<
N
;
++
i
)
components
[
i
]
*=
k
;
return
*
this
;
}
SVector
<
N
>&
operator
/=
(
double
k
)
{
for
(
size_t
i
=
0
;
i
<
N
;
++
i
)
components
[
i
]
/=
k
;
return
*
this
;
}
/* Dot product */
double
operator
*
(
const
SVector
<
N
>&
v
)
const
{
double
x
=
0
;
for
(
size_t
i
=
0
;
i
<
N
;
++
i
)
x
+=
this
->
components
[
i
]
*
v
[
i
];
return
x
;
}
SVector
<
N
>
operator
-
()
const
{
return
(
*
this
)
*
(
-
1
);
}
SVector
<
N
>&
operator
~
()
{
return
(
*
this
)
*=-
1
;
}
/* Return the module */
double
module
()
const
{
return
sqrt
(
this
->
sq_module
());
}
double
sq_module
()
const
{
double
x
;
for
(
auto
c
:
components
)
x
+=
c
*
c
;
return
x
;
}
void
null
()
{
components
.
fill
(
0
);
}
private:
std
::
array
<
double
,
N
>
components
;
};
#ifdef STATIC_VECTOR_IO
template
<
std
::
size_t
N
>
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
SVector
<
N
>
&
v
)
{
os
<<
"("
;
for
(
auto
comp
:
v
)
{
os
<<
comp
<<
", "
;
}
os
<<
")"
;
return
os
;
}
#endif
template
<
std
::
size_t
N
>
const
SVector
<
N
>
operator
+
(
SVector
<
N
>
v
,
const
SVector
<
N
>&
w
)
{
return
v
+=
w
;
}
template
<
std
::
size_t
N
>
const
SVector
<
N
>
operator
-
(
SVector
<
N
>
v
,
const
SVector
<
N
>&
w
)
{
return
v
-=
w
;
}
template
<
std
::
size_t
N
>
const
SVector
<
N
>
operator
*
(
SVector
<
N
>
v
,
double
k
)
{
return
v
*=
k
;
}
template
<
std
::
size_t
N
>
const
SVector
<
N
>
operator
/
(
SVector
<
N
>
v
,
double
k
)
{
return
v
/=
k
;
}
/*
* Prototipe for cross product
* Implementation in vector.cpp
*/
/*const SVector<3> operator^(const SVector<3>& v, const SVector<3>& w)
{
SVector<3> u = {0, 0, 0};
u[0] = v[1] * w[2] - v[2] * w[1];
u[1] = v[2] * w[0] - v[0] * w[2];
u[2] = v[0] * w[1] - v[1] * w[0];
return u;
}*/
const
SVector
<
3
>
operator
^
(
const
SVector
<
3
>&
v
,
const
SVector
<
3
>&
w
);
template
<
std
::
size_t
N
>
SVector
<
N
>
apply_matrix
(
const
double
(
&
matrix
)[
N
][
N
],
const
SVector
<
N
>&
v
)
{
SVector
<
N
>
out
;
for
(
std
::
size_t
i
=
0
;
i
<
N
;
++
i
)
{
for
(
std
::
size_t
j
=
0
;
j
<
N
;
++
j
)
out
[
i
]
+=
matrix
[
i
][
j
]
*
v
[
j
];
}
return
out
;
}
template
<
std
::
size_t
N
>
SVector
<
N
>
apply_homo_matrix
(
const
double
(
&
matrix
)[
N
+
1
][
N
+
1
],
const
SVector
<
N
>&
v
)
{
SVector
<
N
>
out
;
for
(
std
::
size_t
i
=
0
;
i
<
N
;
++
i
)
{
for
(
std
::
size_t
j
=
0
;
j
<
N
;
++
j
)
out
[
i
]
+=
matrix
[
i
][
j
]
*
v
[
j
];
/* homogene component */
out
[
i
]
+=
matrix
[
i
][
N
];
}
return
out
;
}
#endif
Event Timeline
Log In to Comment