www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is this code correct?

reply z <z z.com> writes:
Is this code correct or logically sound?

```D
import std;
enum {////side/depth/height and side/height
x,//0
y,//1
z //2
}
/**
Indicates wether a triangle faces an imaginary view point.
*/
bool triangleFacesCamera(float[3] tA, float[3] tB, float[3] tC) {
float r1, r2;

r1 = atan2(tB[x] - tA[x], tB[z] - tA[z]);//tried swapping 
parameters without success
r2 = atan2(tC[x] - tB[x], tC[z] - tB[z]);//also tried with tA as 
substraction 2nd operand, with same/similar results.
//trying with both is still broken, but appears to change the 
breakage pattern.

r2 -= r1;

return r2 > 0;
}
```
For context, it is trying to reproduce an old algorithm that does 
the same thing :
```D
//in D pseudo code
extern short sus(byte A, byte B){
r0 = sus(tB[y] - tA[y], tB[x] - tA[x]);
r1 = sus(tC[y] - tA[y], tC[x] - tA[x]);

r1 -= r0;
return r1 > 0;
}
```
`sus` is named so because it makes use of `atan` and a premade 
table that *looks like* `tan(a*(a*5.09...))` but is impossible to 
exactly reproduce for me.(on a graph it looks like tan but 
crammed to loop only four/sixteen/sixtyfour times at τ/4,π,τ)

Trying to parse both `atan2` and `sus` with `(sin(angle), 
cos(angle))` as arguments[1] shows that atan2 outputs it as an 
angle in `-π~π` angle representation but `sus`'s output differs 
in that it looks like `sin(x*2)`.

I'd add images to better illustrate it but do not know how...

Big thanks

[1] https://run.dlang.io/is/xR8TcE

ps: is it normal that sometimes posting has a very long delay? 
when it happens it is as if the post has gone nowhere but then it 
suddenly appears much later, which can cause confusion and 
duplicate posting, oof.
Mar 30 2023
parent reply Dennis <dkorpel gmail.com> writes:
On Thursday, 30 March 2023 at 10:29:25 UTC, z wrote:
 Is this code correct or logically sound?
You need to be exact on what 'correct' is. The comment above `triangleFacesCamera` says:
 Indicates wether a triangle faces an imaginary view point.
There's no view point / camera position defined anywhere in your code. Instead, all it's doing is comparing the angle of one triangle side (A->B) with another (B->C) in the XZ plane. This suggests you want to know the triangle winding order: clockwise or counter clockwise. If you search for "triangle winding order" you'll find simple and correct ways to do that. Your code needlessly computes angles, only considers the XZ plane, and doesn't compare the angles correctly.
 r2 -= r1;

 return r2 > 0;
You need to wrap (r2 - r1) into [-τ/2, τ/2] range, then you can compare with 0.
Mar 30 2023
parent reply z <z z.com> writes:
On Thursday, 30 March 2023 at 12:59:31 UTC, Dennis wrote:
 On Thursday, 30 March 2023 at 10:29:25 UTC, z wrote:
 Is this code correct or logically sound?
You need to be exact on what 'correct' is. The comment above `triangleFacesCamera` says:
 Indicates wether a triangle faces an imaginary view point.
There's no view point / camera position defined anywhere in your code. Instead, all it's doing is comparing the angle of one triangle side (A->B) with another (B->C) in the XZ plane. This suggests you want to know the triangle winding order: clockwise or counter clockwise. If you search for "triangle winding order" you'll find simple and correct ways to do that. Your code needlessly computes angles, only considers the XZ plane, and doesn't compare the angles correctly.
 r2 -= r1;

 return r2 > 0;
You need to wrap (r2 - r1) into [-τ/2, τ/2] range, then you can compare with 0.
Yes, the viewpoint/cam should be [0,0,0] for now. The old algorithm completely discards depth information after scaling triangle point data depending on depth distance from 0 hence why i ignored it. I've tried to search before but was only able to find articles for 3D triangles, and documentation for OpenGL, which i don't use. My description of the old algorithm was also badly written(it is confusing!) so here it is rewritten to be more understandable : ```D enum {x, y}//leftright/side, updown/height alias tri = byte[2]; /** Determines if a triangle is visible. */ extern bool oldfunction(tri tA, tri tB, tri tC) { short r0, r1;//r, "result" r0 = cast(short) ((tB[y]-tA[y]) * (tC[x]-tB[x])); r1 = cast(short) ((tC[y]-tA[y]) * (tB[x]-tA[x])); r1 -= r0; return r1 > 0;//"visible" means result is neither negative or zero } ``` (replacing my code with this but in float point "works" but there is an apparent "angle bias" and other problems that are difficult to pinpoint or describe.) Does this match anything known? It doesn't look like cross product to me because the subtractions occur earlier than the multiplications. By correct i mean that it can be used exactly like the old algorithm. (to ignore subshapes from a 3D model) Thanks again
Mar 31 2023
parent reply Dennis <dkorpel gmail.com> writes:
On Friday, 31 March 2023 at 13:11:58 UTC, z wrote:
 I've tried to search before but was only able to find articles 
 for 3D triangles, and documentation for OpenGL, which i don't 
 use.
The first function you posted takes a 3D triangle as input, so I assumed you're working in 3D. What are you working on?
 Determines if a triangle is visible.
You haven't defined what 'visible' means for a geometric triangle.
Apr 01 2023
parent z <z z.com> writes:
On Saturday, 1 April 2023 at 15:32:27 UTC, Dennis wrote:
 On Friday, 31 March 2023 at 13:11:58 UTC, z wrote:
 I've tried to search before but was only able to find articles 
 for 3D triangles, and documentation for OpenGL, which i don't 
 use.
The first function you posted takes a 3D triangle as input, so I assumed you're working in 3D. What are you working on?
 Determines if a triangle is visible.
You haven't defined what 'visible' means for a geometric triangle.
explained to the best of my ability : https://d.godbolt.org/z/4a8zPGsGo the "angle bias" i was trying to explain about is present, when `rot = 0` the `mask` is supposed to only have three `true` values.(that of the "front" triangle.)
You haven't defined what 'visible' means for a geometric 
triangle.
Problem is, all i have is an assembly dump and the data it interacts with, both of which are very(very) old.
Apr 07 2023