Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
tools
frackit
Commits
e6b692c5
Commit
e6b692c5
authored
Apr 20, 2020
by
Dennis Gläser
Browse files
[python][point] add ctor from list
parent
fbc428e6
Changes
1
Hide whitespace changes
Inline
Side-by-side
frackit/python/geometry/point.hh
View file @
e6b692c5
...
...
@@ -19,7 +19,9 @@
#ifndef FRACKIT_PYTHON_GEOMETRY_POINT_HH
#define FRACKIT_PYTHON_GEOMETRY_POINT_HH
#include
<array>
#include
<string>
#include
<algorithm>
#include
<pybind11/pybind11.h>
#include
<pybind11/operators.h>
...
...
@@ -36,13 +38,15 @@ namespace Detail {
template
<
class
Base
,
class
Impl
>
void
registerPointBase
(
py
::
module
&
module
)
{
static
constexpr
int
worldDim
=
Impl
::
worldDimension
();
static_assert
(
Base
::
worldDimension
()
==
worldDim
,
"dimension mismatch"
);
using
ctype
=
typename
Base
::
ctype
;
using
Vector
=
Frackit
::
Vector
<
ctype
,
Impl
::
worldDim
ension
()
>
;
using
Vector
=
Frackit
::
Vector
<
ctype
,
worldDim
>
;
// register class
and define constructor
std
::
string
className
(
"PointBase_"
+
std
::
to_string
(
Base
::
worldDim
ension
()
));
// register class
std
::
string
className
(
"PointBase_"
+
std
::
to_string
(
worldDim
));
py
::
class_
<
Base
,
Geometry
>
cls
(
module
,
className
.
c_str
());
cls
.
def
(
py
::
init
<>
());
// member function
cls
.
def
(
"name"
,
&
Base
::
name
,
"name of the geometry class"
);
...
...
@@ -75,6 +79,24 @@ namespace Detail {
std
::
string
className
(
"Point_"
+
std
::
to_string
(
worldDim
));
py
::
class_
<
Point
,
Base
>
cls
(
module
,
className
.
c_str
());
cls
.
def
(
py
::
init
<>
());
cls
.
def
(
py
::
init
(
[]
(
py
::
list
x
)
->
Point
{
// create array of matching size and fill with values
// from the list. Use zeros at the end if list is shorter.
std
::
array
<
ctype
,
worldDim
>
vals
;
std
::
fill
(
vals
.
begin
(),
vals
.
end
(),
0.0
);
unsigned
int
minDim
=
std
::
min
(
worldDim
,
static_cast
<
int
>
(
x
.
size
()));
for
(
unsigned
int
i
=
0
;
i
<
minDim
;
++
i
)
vals
[
i
]
=
x
[
i
].
template
cast
<
ctype
>();
if
constexpr
(
worldDim
==
1
)
return
{
vals
[
0
]};
else
if
constexpr
(
worldDim
==
2
)
return
{
vals
[
0
],
vals
[
1
]};
else
return
{
vals
[
0
],
vals
[
1
],
vals
[
2
]};
}
),
"coordinates"
_a
);
if
constexpr
(
worldDim
==
1
)
cls
.
def
(
py
::
init
<
ctype
>
(),
"x"
_a
);
else
if
constexpr
(
worldDim
==
2
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment