Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ecomod
zmt-pathfinding
Commits
3fe9a55b
Commit
3fe9a55b
authored
Mar 23, 2016
by
mey
Browse files
Kernel.java: added add method
added hashCode and equals updated test
parent
5ce256fe
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/java/de/zmt/pathfinding/filter/Kernel.java
View file @
3fe9a55b
package
de.zmt.pathfinding.filter
;
import
java.io.Serializable
;
import
java.util.Arrays
;
/**
* Immutable Kernel for operation on double grids. A similar class already
...
...
@@ -53,7 +54,22 @@ public class Kernel implements Serializable {
}
/**
* Multiplies all weights by {@code scalar} and return result with a new
* Adds {@code value} to all weights and return results within a new object.
*
* @param value
* @return new kernel object with added {@code value}
*/
public
Kernel
add
(
double
value
)
{
double
[]
addedWeights
=
new
double
[
weights
.
length
];
for
(
int
i
=
0
;
i
<
weights
.
length
;
i
++)
{
addedWeights
[
i
]
=
weights
[
i
]
+
value
;
}
return
new
Kernel
(
width
,
height
,
addedWeights
);
}
/**
* Multiplies all weights by {@code scalar} and return results within a new
* object.
*
* @param scalar
...
...
@@ -120,6 +136,36 @@ public class Kernel implements Serializable {
return
weights
;
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
int
result
=
1
;
result
=
prime
*
result
+
Arrays
.
hashCode
(
weights
);
result
=
prime
*
result
+
width
;
return
result
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
{
return
true
;
}
if
(
obj
==
null
)
{
return
false
;
}
if
(
getClass
()
!=
obj
.
getClass
())
{
return
false
;
}
Kernel
other
=
(
Kernel
)
obj
;
if
(!
Arrays
.
equals
(
weights
,
other
.
weights
))
{
return
false
;
}
if
(
width
!=
other
.
width
)
{
return
false
;
}
return
true
;
}
@Override
public
String
toString
()
{
StringBuilder
builder
=
new
StringBuilder
();
...
...
src/test/java/de/zmt/pathfinding/filter/KernelTest.java
View file @
3fe9a55b
...
...
@@ -10,12 +10,13 @@ import org.junit.Test;
public
class
KernelTest
{
private
static
final
int
KERNEL_EXTENT
=
3
;
private
static
final
double
MAX_ERROR
=
1
E
-
14
d
;
private
static
final
double
DOUBLE_SUM
=
KERNEL_EXTENT
*
KERNEL_EXTENT
*
2
d
;
private
Kernel
kernel
;
@Before
public
void
setUp
()
throws
Exception
{
kernel
=
KernelFactory
.
createConstant
(
3
,
KERNEL_EXTENT
);
kernel
=
KernelFactory
.
createConstant
(
KERNEL_EXTENT
,
KERNEL_EXTENT
);
}
@Test
...
...
@@ -28,4 +29,17 @@ public class KernelTest {
assertThat
(
kernel
.
normalize
().
sum
(),
is
(
closeTo
(
1
,
MAX_ERROR
)));
}
@Test
public
void
add
()
{
assertThat
(
kernel
.
add
(
1
).
sum
(),
is
(
DOUBLE_SUM
));
}
@Test
public
void
multiply
()
{
assertThat
(
kernel
.
multiply
(
2
).
sum
(),
is
(
DOUBLE_SUM
));
}
public
void
equals
()
{
assertThat
(
new
Kernel
(
1
,
1
,
new
double
[]
{
1
}),
is
(
new
Kernel
(
1
,
1
,
new
double
[]
{
1
})));
}
}
Write
Preview
Markdown
is supported
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