Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-xt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ag-ohlberger
dune-community
dune-xt
Commits
25939173
Unverified
Commit
25939173
authored
8 years ago
by
René Fritze
Browse files
Options
Downloads
Patches
Plain Diff
[math] clamp now also works for known vector types
parent
101c1413
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
dune/xt/common/math.hh
+14
-1
14 additions, 1 deletion
dune/xt/common/math.hh
dune/xt/common/test/math.cc
+18
-6
18 additions, 6 deletions
dune/xt/common/test/math.cc
with
32 additions
and
7 deletions
dune/xt/common/math.hh
+
14
−
1
View file @
25939173
...
...
@@ -35,6 +35,7 @@
#include
<dune/xt/common/reenable_warnings.hh>
#include
<dune/xt/common/type_traits.hh>
#include
<dune/xt/common/vector.hh>
#include
<dune/common/deprecated.hh>
namespace
Dune
{
...
...
@@ -177,11 +178,23 @@ protected:
//! \return var bounded in [min, max]
template
<
typename
T
>
T
clamp
(
const
T
var
,
const
T
min
,
const
T
max
)
typename
std
::
enable_if
<!
is_vector
<
T
>::
value
,
T
>::
type
clamp
(
const
T
var
,
const
T
min
,
const
T
max
)
{
return
(
var
<
min
)
?
min
:
(
var
>
max
)
?
max
:
var
;
}
template
<
typename
T
>
typename
std
::
enable_if
<
is_vector
<
T
>::
value
,
T
>::
type
clamp
(
const
T
var
,
const
T
min
,
const
T
max
)
{
auto
result
=
var
;
std
::
size_t
idx
=
0
;
for
(
auto
&&
element
:
var
)
{
result
[
idx
]
=
clamp
(
element
,
min
[
idx
],
max
[
idx
]);
++
idx
;
}
return
result
;
}
/**
* \returns: -1 iff val < 0
* 0 iff val == 0
...
...
This diff is collapsed.
Click to expand it.
dune/xt/common/test/math.cc
+
18
−
6
View file @
25939173
...
...
@@ -19,27 +19,39 @@
#include
<dune/xt/common/ranges.hh>
using
namespace
Dune
::
XT
::
Common
;
typedef
testing
::
Types
<
double
,
int
,
Dune
::
FieldVector
<
double
,
3
>
,
std
::
vector
<
double
>>
ClampTestTypes
;
typedef
testing
::
Types
<
double
,
int
>
TestTypes
;
typedef
testing
::
Types
<
std
::
complex
<
double
>
,
double
,
int
>
ComplexTestTypes
;
template
<
typename
T
>
static
typename
std
::
enable_if
<
is_vector
<
T
>::
value
,
T
>::
type
init_bound
(
int
val
)
{
const
auto
size
=
VectorAbstraction
<
T
>::
has_static_size
?
VectorAbstraction
<
T
>::
static_size
:
3u
;
return
VectorAbstraction
<
T
>::
create
(
size
,
val
);
}
template
<
typename
T
>
static
typename
std
::
enable_if
<!
is_vector
<
T
>::
value
,
T
>::
type
init_bound
(
int
val
)
{
return
T
(
val
);
}
template
<
class
T
>
struct
ClampTest
:
public
testing
::
Test
{
const
T
lower
;
const
T
upper
;
ClampTest
()
:
lower
(
T
(
-
1
))
,
upper
(
T
(
1
))
:
lower
(
init_bound
<
T
>
(
-
1
))
,
upper
(
init_bound
<
T
>
(
1
))
{
}
};
TYPED_TEST_CASE
(
ClampTest
,
TestTypes
);
TYPED_TEST_CASE
(
ClampTest
,
Clamp
TestTypes
);
TYPED_TEST
(
ClampTest
,
All
)
{
EXPECT_EQ
(
clamp
(
TypeParam
(
-
2
),
this
->
lower
,
this
->
upper
),
this
->
lower
);
EXPECT_EQ
(
clamp
(
TypeParam
(
2
),
this
->
lower
,
this
->
upper
),
this
->
upper
);
EXPECT_EQ
(
clamp
(
TypeParam
(
0
),
this
->
lower
,
this
->
upper
),
TypeParam
(
0
));
EXPECT_EQ
(
clamp
(
init_bound
<
TypeParam
>
(
-
2
),
this
->
lower
,
this
->
upper
),
this
->
lower
);
EXPECT_EQ
(
clamp
(
init_bound
<
TypeParam
>
(
2
),
this
->
lower
,
this
->
upper
),
this
->
upper
);
EXPECT_EQ
(
clamp
(
init_bound
<
TypeParam
>
(
0
),
this
->
lower
,
this
->
upper
),
init_bound
<
TypeParam
>
(
0
));
}
template
<
class
T
>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment