Skip to content

Commit

Permalink
PackedArray
Browse files Browse the repository at this point in the history
  - Fixed bug where set(index, point) overwrite the value of `point`
  - Added implementation specific variants of set( index , ... )
  • Loading branch information
lessthanoptimal committed Jan 5, 2025
1 parent b7bac9e commit 4824566
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 38 deletions.
2 changes: 2 additions & 0 deletions change.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Version : 1.1.8

- PackedArray
* Added isEquals()
* Fixed bug where set(index, point) overwrite the value of `point`
* Added implementation specific variants of set( index , ... )

---------------------
Date : 2024/Oct/05
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -39,6 +39,12 @@ public final void append( double x, double y ) {
array.add(y);
}

public void set( int index, double x, double y ) {
index *= 2;
array.data[index++] = x;
array.data[index] = y;
}

@Override public void append( Point2D_F64 element ) {
append(element.x, element.y);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -56,6 +56,12 @@ public final void append( int x, int y ) {
array.add(y);
}

public void set( int index, int x, int y ) {
index *= 2;
array.data[index++] = (short)x;
array.data[index] = (short)y;
}

@Override public void append( Point2D_I16 element ) {
append(element.x, element.y);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -56,6 +56,12 @@ public final void append( int x, int y ) {
array.add(y);
}

public void set( int index, int x, int y ) {
index *= 2;
array.data[index++] = x;
array.data[index] = y;
}

@Override public void append( Point2D_I32 element ) {
append(element.x, element.y);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -46,6 +46,13 @@ public void append( double x, double y, double z) {
array.add(z);
}

public void set( int index, double x, double y, double z ) {
index *= 3;
array.data[index++] = x;
array.data[index++] = y;
array.data[index] = z;
}

@Override public void set( int index, Point3D_F64 element ) {
index *= 3;
array.data[index++] = element.x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -56,6 +56,14 @@ public void append( double x, double y, double z, double w ) {
array.add(w);
}

public void set( int index, double x, double y, double z, double w ) {
index *= 4;
array.data[index++] = x;
array.data[index++] = y;
array.data[index++] = z;
array.data[index] = w;
}

@Override public void set( int index, Point4D_F64 element ) {
index *= 4;
array.data[index++] = element.x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -72,6 +72,14 @@ public void append( double x, double y ) {
array.add(y);
}

public void set( int index, double x, double y ) {
index *= DOF;
double[] block = array.getBlocks().get(index/array.getBlockSize());
int where = index%array.getBlockSize();
block[where] = x;
block[where + 1] = y;
}

@Override public void append( Point2D_F64 element ) {
array.add(element.x);
array.add(element.y);
Expand All @@ -81,8 +89,8 @@ public void append( double x, double y ) {
index *= DOF;
double[] block = array.getBlocks().get(index/array.getBlockSize());
int where = index%array.getBlockSize();
element.x = block[where];
element.y = block[where + 1];
block[where] = element.x;
block[where + 1] = element.y;
}

@Override public Point2D_F64 getTemp( int index ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -82,13 +82,22 @@ public void append( GeoTuple3D_F64<?> element ) {
append(element.x, element.y, element.z);
}

public void set( int index, double x, double y, double z ) {
index *= DOF;
double[] block = array.getBlocks().get(index/array.getBlockSize());
int where = index%array.getBlockSize();
block[where] = x;
block[where + 1] = y;
block[where + 2] = z;
}

@Override public void set( int index, Point3D_F64 element ) {
index *= DOF;
double[] block = array.getBlocks().get(index/array.getBlockSize());
int where = index%array.getBlockSize();
element.x = block[where];
element.y = block[where + 1];
element.z = block[where + 2];
block[where] = element.x;
block[where + 1] = element.y;
block[where + 2] = element.z;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -77,6 +77,16 @@ public void append( double x, double y, double z, double w ) {
array.add(w);
}

public void set( int index, double x, double y, double z, double w ) {
index *= DOF;
double[] block = array.getBlocks().get(index/array.getBlockSize());
int where = index%array.getBlockSize();
block[where] = x;
block[where + 1] = y;
block[where + 2] = z;
block[where + 3] = w;
}

@Override public void append( Point4D_F64 element ) {
array.add(element.x);
array.add(element.y);
Expand All @@ -88,10 +98,10 @@ public void append( double x, double y, double z, double w ) {
index *= DOF;
double[] block = array.getBlocks().get(index/array.getBlockSize());
int where = index%array.getBlockSize();
element.x = block[where];
element.y = block[where + 1];
element.z = block[where + 2];
element.w = block[where + 3];
block[where] = element.x;
block[where + 1] = element.y;
block[where + 2] = element.z;
block[where + 3] = element.w;
}

@Override public Point4D_F64 getTemp( int index ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -156,7 +156,15 @@ public abstract class GenericPackedArrayChecks<T> extends BoofStandardJUnit {
alg.append(createRandomPoint());

var p = createRandomPoint();
var copy = createRandomPoint();
alg.copy(p, copy);

alg.set(1, p);

// make sure set isn't writing to 'p'. yes this was a bug
checkEquals(p, copy);

// Retrieve the value and see if it's the same
T found = alg.getTemp(1);
checkEquals(p, found);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand All @@ -23,8 +23,7 @@
import org.ejml.UtilEjml;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.*;

public class TestPackedArrayPoint2D_F64 extends GenericPackedArrayChecks<Point2D_F64> {

Expand All @@ -34,8 +33,8 @@ public class TestPackedArrayPoint2D_F64 extends GenericPackedArrayChecks<Point2D

@Override protected Point2D_F64 createRandomPoint() {
var point = new Point2D_F64();
point.x = (double) rand.nextGaussian();
point.y = (double) rand.nextGaussian();
point.x = (double)rand.nextGaussian();
point.y = (double)rand.nextGaussian();
return point;
}

Expand All @@ -47,6 +46,14 @@ public class TestPackedArrayPoint2D_F64 extends GenericPackedArrayChecks<Point2D
assertNotEquals(0.0, a.distance(b), UtilEjml.TEST_F64);
}

@Test void setIndex_values() {
var alg = new PackedArrayPoint2D_F64();
alg.append(1, 2);
alg.append(2, 3);
alg.set(1, -1, -2);
assertTrue(alg.getTemp(1).isIdentical(-1, -2));
}

@Test void appendValues() {
var alg = new PackedArrayPoint2D_F64();
assertEquals(0, alg.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand All @@ -23,8 +23,7 @@
import org.ejml.UtilEjml;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.*;

public class TestPackedArrayPoint3D_F64 extends GenericPackedArrayChecks<Point3D_F64> {

Expand All @@ -48,6 +47,14 @@ public class TestPackedArrayPoint3D_F64 extends GenericPackedArrayChecks<Point3D
assertNotEquals(0.0, a.distance(b), UtilEjml.TEST_F64);
}

@Test void setIndex_values() {
var alg = new PackedArrayPoint3D_F64();
alg.append(1, 2, 3);
alg.append(2, 3, 4);
alg.set(1, -1, -2, -3);
assertTrue(alg.getTemp(1).isIdentical(-1, -2, -3));
}

@Test void appendValues() {
var alg = new PackedArrayPoint3D_F64();
assertEquals(0, alg.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand All @@ -23,8 +23,7 @@
import org.ejml.UtilEjml;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.*;

public class TestPackedArrayPoint4D_F64 extends GenericPackedArrayChecks<Point4D_F64> {

Expand All @@ -49,6 +48,14 @@ public class TestPackedArrayPoint4D_F64 extends GenericPackedArrayChecks<Point4D
assertNotEquals(0.0, a.distance(b), UtilEjml.TEST_F64);
}

@Test void setIndex_values() {
var alg = new PackedArrayPoint4D_F64();
alg.append(1, 2, 3, 4);
alg.append(2, 3, 4, 5);
alg.set(1, -1, -2, -3, -4);
assertTrue(alg.getTemp(1).isIdentical(-1, -2, -3, -4));
}

@Test void appendValues() {
var alg = new PackedArrayPoint4D_F64();
assertEquals(0, alg.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Abeles. All Rights Reserved.
* Copyright (c) 2025, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand All @@ -24,8 +24,7 @@
import org.ejml.UtilEjml;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.*;

public class TestPackedBigArrayPoint2D_F64 extends GenericPackedArrayChecks<Point2D_F64> {

Expand All @@ -48,6 +47,14 @@ public class TestPackedBigArrayPoint2D_F64 extends GenericPackedArrayChecks<Poin
assertNotEquals(0.0, a.distance(b), UtilEjml.TEST_F64);
}

@Test void setIndex_values() {
var alg = new PackedBigArrayPoint2D_F64();
alg.append(1, 2);
alg.append(2, 3);
alg.set(1, -1, -2);
assertTrue(alg.getTemp(1).isIdentical(-1, -2));
}

@Test void appendValues() {
var alg = new PackedBigArrayPoint2D_F64();
assertEquals(0, alg.size());
Expand Down
Loading

0 comments on commit 4824566

Please sign in to comment.