Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Q
QcmpCompressionLibrary
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
BioinformaticDataCompression
QcmpCompressionLibrary
Commits
60fa2eea
Commit
60fa2eea
authored
5 years ago
by
Vojtech Moravec
Browse files
Options
Downloads
Patches
Plain Diff
Cleaned code in Chunk2D
parent
bc5cceec
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/azgracompress/data/Chunk2D.java
+131
-146
131 additions, 146 deletions
src/main/java/azgracompress/data/Chunk2D.java
with
131 additions
and
146 deletions
src/main/java/azgracompress/data/Chunk2D.java
+
131
−
146
View file @
60fa2eea
...
...
@@ -7,6 +7,13 @@ public class Chunk2D {
private
final
V2i
dims
;
private
final
V2l
offset
;
/**
* Create the Chunk2D of given dimensions, offset and initialize it with data.
*
* @param dims Dimensions of the chunk.
* @param offset Offset of the chunk.
* @param data Chunk data.
*/
public
Chunk2D
(
final
V2i
dims
,
final
V2l
offset
,
final
int
[]
data
)
{
this
.
dims
=
dims
;
this
.
data
=
data
;
...
...
@@ -14,8 +21,14 @@ public class Chunk2D {
assert
(
data
.
length
==
(
dims
.
getX
()
*
dims
.
getY
()))
:
"Wrong box data."
;
}
public
Chunk2D
(
final
V2i
chunkDdims
,
final
V2l
offset
)
{
this
(
chunkDdims
,
offset
,
new
int
[
chunkDdims
.
getX
()
*
chunkDdims
.
getY
()]);
/**
* Create Chunk2D of given dimensions and offset. Allocates the data.
*
* @param dims Dimensions of the chunk.
* @param offset Offset of the chunk.
*/
public
Chunk2D
(
final
V2i
dims
,
final
V2l
offset
)
{
this
(
dims
,
offset
,
new
int
[
dims
.
getX
()
*
dims
.
getY
()]);
}
/**
...
...
@@ -70,23 +83,67 @@ public class Chunk2D {
return
String
.
format
(
"2D shape %s %d values"
,
dims
.
toString
(),
data
.
length
);
}
// public Chunk2D[] divideIntoChunks(final V2i chunkDims) {
// final int xSize = dims.getX();
// final int ySize = dims.getY();
//
// final int chunkCount = calculateRequiredChunkCountPerPlane(chunkDims);
//
// Chunk2D[] chunks = new Chunk2D[chunkCount];
// int chunkIndex = 0;
//
// for (int chunkYOffset = 0; chunkYOffset < ySize; chunkYOffset += chunkDims.getY()) {
// for (int chunkXOffset = 0; chunkXOffset < xSize; chunkXOffset += chunkDims.getX()) {
// chunks[chunkIndex++] = copyToChunk(chunkDims, new V2i(chunkXOffset, chunkYOffset));
// }
// }
// return chunks;
// }
/**
* Divide this Chunk to 1D row vector of given length.
*
* @param vectorSize Vector length.
* @return Array of row vectors.
*/
public
int
[][]
divideInto1DVectors
(
final
int
vectorSize
)
{
final
int
rowVectorCount
=
(
int
)
Math
.
ceil
(
dims
.
getX
()
/
(
float
)
vectorSize
);
final
int
vectorCount
=
rowVectorCount
*
dims
.
getY
();
int
[][]
imageVectors
=
new
int
[
vectorCount
][
vectorSize
];
int
vec
=
0
;
int
srcX
;
for
(
int
row
=
0
;
row
<
dims
.
getY
();
row
++)
{
for
(
int
vecIndex
=
0
;
vecIndex
<
rowVectorCount
;
vecIndex
++)
{
for
(
int
x
=
0
;
x
<
vectorSize
;
x
++)
{
srcX
=
(
vecIndex
*
vectorSize
)
+
x
;
imageVectors
[
vec
][
x
]
=
isInside
(
srcX
,
row
)
?
data
[
index
(
srcX
,
row
)]
:
FILL_VALUE
;
}
++
vec
;
}
}
return
imageVectors
;
}
/**
* Reconstruct this Chunk from array of 1D row vectors.
*
* @param vectors Array of 1D row vectors.
*/
public
void
reconstructFromVectors
(
int
[][]
vectors
)
{
if
(
vectors
.
length
==
0
)
{
return
;
}
final
int
vectorSize
=
vectors
[
0
].
length
;
final
int
rowVectorCount
=
(
int
)
Math
.
ceil
(
dims
.
getX
()
/
(
float
)
vectorSize
);
final
int
vectorCount
=
rowVectorCount
*
dims
.
getY
();
assert
(
vectors
.
length
==
vectorCount
)
:
"Wrong vector count in reconstruct."
;
int
vec
=
0
;
int
dstX
;
for
(
int
dstY
=
0
;
dstY
<
dims
.
getY
();
dstY
++)
{
for
(
int
vecIndex
=
0
;
vecIndex
<
rowVectorCount
;
vecIndex
++)
{
for
(
int
x
=
0
;
x
<
vectorSize
;
x
++)
{
dstX
=
(
vecIndex
*
vectorSize
)
+
x
;
if
(
isInside
(
dstX
,
dstY
))
{
data
[
index
(
dstX
,
dstY
)]
=
vectors
[
vec
][
x
];
}
}
++
vec
;
}
}
}
/**
* Divide this Chunk to 2D matrices of given dimensions.
*
* @param qVectorDims Matrix dimension.
* @return Array of matrix data.
*/
public
int
[][]
divideInto2DVectors
(
final
V2i
qVectorDims
)
{
final
int
xSize
=
dims
.
getX
();
final
int
ySize
=
dims
.
getY
();
...
...
@@ -108,6 +165,12 @@ public class Chunk2D {
return
vectors
;
}
/**
* Reconstruct this Chunk (copy data) from matrix vectors.
*
* @param vectors Matrix vector data.
* @param qVectorDims Matrix dimensions.
*/
public
void
reconstructFrom2DVectors
(
final
int
[][]
vectors
,
final
V2i
qVectorDims
)
{
final
int
xSize
=
dims
.
getX
();
final
int
ySize
=
dims
.
getY
();
...
...
@@ -126,10 +189,23 @@ public class Chunk2D {
}
/**
* Calculate the number of required 2D matrices for plane.
*
* @param chunkDims Matrix dimension.
* @return Number of required chunks.
*/
private
int
calculateRequiredChunkCountPerPlane
(
final
V2i
chunkDims
)
{
return
calculateRequiredChunkCountPerPlane
(
dims
,
chunkDims
);
}
/**
* Calculate the number of required 2D matrices for plane of given dimensions.
*
* @param imageDims Plane dimensions.
* @param chunkDims Matrix dimension.
* @return Number of required chunks.
*/
public
static
int
calculateRequiredChunkCountPerPlane
(
final
V2i
imageDims
,
final
V2i
chunkDims
)
{
final
int
xChunkCount
=
(
int
)
Math
.
ceil
(
imageDims
.
getX
()
/
(
double
)
chunkDims
.
getX
());
final
int
yChunkCount
=
(
int
)
Math
.
ceil
(
imageDims
.
getY
()
/
(
double
)
chunkDims
.
getY
());
...
...
@@ -137,61 +213,25 @@ public class Chunk2D {
return
(
xChunkCount
*
yChunkCount
);
}
// public void reconstructFromChunks(final Chunk2D[] chunks) {
// assert (chunks.length > 0) : "No chunks in reconstruct";
// final V2i chunkDims = chunks[0].getDims();
//
// assert (calculateRequiredChunkCountPerPlane(chunkDims) == chunks.length) : "Wrong chunk count in reconstruct";
// for (final Chunk2D chunk : chunks) {
// copyFromChunk(chunk);
// }
// }
/**
* Check if point [x,y] is inside this chunk boundaries.
*
* @param x X coordinate (width).
* @param y Y coordinate (height).
* @return True if point is inside.
*/
private
boolean
isInside
(
final
int
x
,
final
int
y
)
{
return
(((
x
>=
0
)
&&
(
x
<
dims
.
getX
()))
&&
(
y
>=
0
)
&&
(
y
<
dims
.
getY
()));
}
// private void copyFromChunk(final Chunk2D chunk) {
//
// final V2i chunkDims = chunk.getDims();
// final V2l localOffset = chunk.getOffset();
// int dstX, dstY;
//
// for (int chunkY = 0; chunkY < chunkDims.getY(); chunkY++) {
// dstY = (int) localOffset.getY() + chunkY;
// for (int chunkX = 0; chunkX < chunkDims.getX(); chunkX++) {
// dstX = (int) localOffset.getX() + chunkX;
//
// // NOTE(Moravec): Negating this expression!
// // If dst coordinates are NOT outside bounds, copy the value.
// if (!(dstX >= dims.getX() || dstY >= dims.getY())) {
// setValueAt(dstX, dstY, chunk.getValueAt(chunkX, chunkY));
// }
// }
// }
// }
// private Chunk2D copyToChunk(final V2i chunkDims, final V2i chunkOffset) {
// int[] chunkData = new int[(chunkDims.getX() * chunkDims.getY())];
//
// int srcX, srcY;
//
// for (int y = 0; y < chunkDims.getY(); y++) {
// srcY = chunkOffset.getY() + y;
// for (int x = 0; x < chunkDims.getX(); x++) {
// srcX = chunkOffset.getX() + x;
// final int dstIndex = index(x, y, chunkDims);
// // NOTE(Moravec): Try repeat last value instead of FILL_VALUE.
// chunkData[dstIndex] = isInside(srcX, srcY) ? data[index(srcX, srcY)] : FILL_VALUE;
// }
// }
//
// // NOTE(Moravec): We will save only local offset inside current box, which will be used
// // to reconstruct the original box.
// return new Chunk2D(chunkDims, chunkOffset.toV2l(), chunkData);
// }
/**
* Copy this chunk data to chunk vector.
*
* @param vector Chunk vector.
* @param qVectorDims Dimensions of the vector.
* @param chunkXOffset Chunk X offset
* @param chunkYOffset Chunk Y offset.
*/
private
void
copyDataToVector
(
int
[]
vector
,
final
V2i
qVectorDims
,
final
int
chunkXOffset
,
final
int
chunkYOffset
)
{
int
srcX
,
srcY
;
final
int
qVecYSize
=
qVectorDims
.
getY
();
...
...
@@ -206,6 +246,14 @@ public class Chunk2D {
}
}
/**
* Copy data from chunk vector to this chunk.
*
* @param vector Chunk vector.
* @param qVectorDims Chunk dimensions.
* @param chunkXOffset Chunk X offset.
* @param chunkYOffset Chunk Y offset.
*/
private
void
copyDataFromVector
(
int
[]
vector
,
final
V2i
qVectorDims
,
final
int
chunkXOffset
,
...
...
@@ -226,6 +274,12 @@ public class Chunk2D {
}
}
/**
* Check whether two Chunk2D are equal in data.
*
* @param obj Other object
* @return True if same.
*/
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
obj
instanceof
Chunk2D
)
{
...
...
@@ -247,88 +301,19 @@ public class Chunk2D {
}
}
/**
* Get this chunk data.
*
* @return Data array.
*/
public
int
[]
getData
()
{
return
data
;
}
public
int
[][]
divideInto1DVectors
(
final
int
vectorSize
)
{
final
int
rowVectorCount
=
(
int
)
Math
.
ceil
(
dims
.
getX
()
/
(
float
)
vectorSize
);
final
int
vectorCount
=
rowVectorCount
*
dims
.
getY
();
int
[][]
imageVectors
=
new
int
[
vectorCount
][
vectorSize
];
int
vec
=
0
;
int
srcX
;
for
(
int
row
=
0
;
row
<
dims
.
getY
();
row
++)
{
for
(
int
vecIndex
=
0
;
vecIndex
<
rowVectorCount
;
vecIndex
++)
{
for
(
int
x
=
0
;
x
<
vectorSize
;
x
++)
{
srcX
=
(
vecIndex
*
vectorSize
)
+
x
;
imageVectors
[
vec
][
x
]
=
isInside
(
srcX
,
row
)
?
data
[
index
(
srcX
,
row
)]
:
0
;
// imageVectors[row][x - 1];
}
++
vec
;
}
}
return
imageVectors
;
}
public
void
reconstructFromVectors
(
int
[][]
vectors
)
{
if
(
vectors
.
length
==
0
)
{
return
;
}
final
int
vectorSize
=
vectors
[
0
].
length
;
final
int
rowVectorCount
=
(
int
)
Math
.
ceil
(
dims
.
getX
()
/
(
float
)
vectorSize
);
final
int
vectorCount
=
rowVectorCount
*
dims
.
getY
();
assert
(
vectors
.
length
==
vectorCount
)
:
"Wrong vector count in reconstruct."
;
int
vec
=
0
;
int
dstX
;
for
(
int
dstY
=
0
;
dstY
<
dims
.
getY
();
dstY
++)
{
for
(
int
vecIndex
=
0
;
vecIndex
<
rowVectorCount
;
vecIndex
++)
{
for
(
int
x
=
0
;
x
<
vectorSize
;
x
++)
{
dstX
=
(
vecIndex
*
vectorSize
)
+
x
;
if
(
isInside
(
dstX
,
dstY
))
{
data
[
index
(
dstX
,
dstY
)]
=
vectors
[
vec
][
x
];
}
}
++
vec
;
}
}
}
/**
* Convert Chunk2D to ImageU16 one-to-one.
*/
public
ImageU16
asImageU16
()
{
// return new ImageU16(dims.getX(), dims.getY(), TypeConverter.intArrayToShortArray(data));
return
new
ImageU16
(
dims
.
getX
(),
dims
.
getY
(),
data
);
}
private
void
updateData
(
int
[]
newData
)
{
assert
(
data
.
length
==
newData
.
length
)
:
"Data length mismatch!"
;
data
=
newData
;
}
// public static int[][] chunksAsImageVectors(final Chunk2D[] chunks) {
// if (chunks.length == 0) {
// return new int[0][0];
// }
// final int vectorCount = chunks.length;
// final int vectorSize = chunks[0].data.length;
// int[][] imageVectors = new int[vectorCount][vectorSize];
//
// for (int i = 0; i < vectorCount; i++) {
// assert (chunks[i].data.length == vectorSize);
// System.arraycopy(chunks[i].data, 0, imageVectors[i], 0, vectorSize);
// }
// return imageVectors;
// }
public
static
void
updateChunkData
(
Chunk2D
[]
chunks
,
final
int
[][]
newData
)
{
assert
(
chunks
.
length
==
newData
.
length
)
:
"chunks len newData len mismatch."
;
for
(
int
i
=
0
;
i
<
chunks
.
length
;
i
++)
{
chunks
[
i
].
updateData
(
newData
[
i
]);
}
}
}
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