Re: Phobos function to remove all occurances from dynamic array?

2024-04-30 Thread Liam McGillivray via Digitalmars-d-learn

On Wednesday, 1 May 2024 at 01:24:55 UTC, Lance Bachmeier wrote:

Does filter do what you need?

https://dlang.org/phobos/std_algorithm_iteration.html#.filter


It seems to do it with the following line:
```
allObjects = allObjects.filter!(element => element !is 
this).array;

```

So I've found a way to do it. It's still rather strange that it's 
so difficult to find the solution to such a common problem in the 
documentation.


Re: Phobos function to remove all occurances from dynamic array?

2024-04-30 Thread Lance Bachmeier via Digitalmars-d-learn

On Wednesday, 1 May 2024 at 01:09:33 UTC, Liam McGillivray wrote:
This is presumably such a common task that I'm surprised it 
isn't easy to find the answer by searching;


Is there a standard library function that removes all elements 
from a dynamic array that matches an input argument?


In `std.array` there's the `replace` function which is supposed 
to replace all occurrences that match an input with another. It 
seems to work as described on strings, but I get compiler 
errors when using it on other array types. I've tried using it 
to replace occurrences of a certain object in an array with 
`[]` in order to remove all occurrences, but it's not allowed.


Is there a Phobos function that does what I want? It would be 
crazy if there isn't.


Does filter do what you need?

https://dlang.org/phobos/std_algorithm_iteration.html#.filter


Re: Find homography in D?

2024-04-30 Thread Jordan Wilson via Digitalmars-d-learn

On Sunday, 21 April 2024 at 14:57:33 UTC, Paolo Invernizzi wrote:

Hi,

Someone can point me to a D implementation of the classical 
OpenCV find homography matrix?


Thank you,
Paolo


Something I wrote awhile ago...

```
import kaleidic.lubeck : svd;
import gfm.math;
import mir.ndslice : sliced;

auto generateTransformationArray(int[] p) {
return generateTransformationArray(p[0],p[1],p[2],p[3]);
}

auto generateTransformationArray(int x, int y, int x_, int y_) {
return [-x, -y, -1, 0, 0, 0, x*x_, y*x_, x_,
0, 0, 0, -x, -y, -1, x*y_, y*y_, y_];
}

auto transformCoor (mat3d mat, vec3d vec) {
auto res = mat * vec;
return res / res[2];
}

auto findHomography (int[][] correspondances) {
auto a = correspondances
.map!(a => a.generateTransformationArray)
.joiner
.array
.sliced(8,9);

auto r = a.svd;
auto homog = r.vt.back;
return mat3d(homog.map!(a => a/homog.back).array);
}
```



Re: Find homography in D?

2024-04-30 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Sunday, 21 April 2024 at 14:57:33 UTC, Paolo Invernizzi wrote:

Hi,

Someone can point me to a D implementation of the classical 
OpenCV find homography matrix?


Thank you,
Paolo


Just for future records in the forum.

// 
https://math.stackexchange.com/questions/3509039/calculate-homography-with-and-without-svd


/+dub.sdl:
dependency "lubeck" version="~>1.5.4"
+/
import std;
import mir.ndslice;
import kaleidic.lubeck;

void main()
{
double[2] x_1 = [93,-7];
double[2] y_1 = [63,0];
double[2] x_2 = [293,3];
double[2] y_2 = [868,-6];
double[2] x_3 = [1207,7];
double[2] y_3 = [998,-4];
double[2] x_4 = [1218,3];
double[2] y_4 = [309,2];

auto A = [
-x_1[0], -y_1[0], -1, 0, 0, 0, x_1[0]*x_1[1], 
y_1[0]*x_1[1], x_1[1],
0, 0, 0, -x_1[0], -y_1[0], -1, x_1[0]*y_1[1], 
y_1[0]*y_1[1], y_1[1],
-x_2[0], -y_2[0], -1, 0, 0, 0, x_2[0]*x_2[1], 
y_2[0]*x_2[1], x_2[1],
0, 0, 0, -x_2[0], -y_2[0], -1, x_2[0]*y_2[1], 
y_2[0]*y_2[1], y_2[1],
-x_3[0], -y_3[0], -1, 0, 0, 0, x_3[0]*x_3[1], 
y_3[0]*x_3[1], x_3[1],
0, 0, 0, -x_3[0], -y_3[0], -1, x_3[0]*y_3[1], 
y_3[0]*y_3[1], y_3[1],
-x_4[0], -y_4[0], -1, 0, 0, 0, x_4[0]*x_4[1], 
y_4[0]*x_4[1], x_4[1],
0, 0, 0, -x_4[0], -y_4[0], -1, x_4[0]*y_4[1], 
y_4[0]*y_4[1], y_4[1]

].sliced(8, 9);

auto svdResult = svd(A);

auto homography = svdResult.vt[$-1].sliced(3, 3);
	auto transformedPoint = homography.mtimes([1679,  128, 
1].sliced.as!double.slice);

transformedPoint[] /= transformedPoint[2];

writeln(transformedPoint); //[4, 7, 1]
}