Skip to content

Comments

r.horizon: use absolute value for angles < GRASS_EPSILON#3403

Merged
nilason merged 4 commits intoOSGeo:mainfrom
nilason:fix_r_horizon
Feb 6, 2024
Merged

r.horizon: use absolute value for angles < GRASS_EPSILON#3403
nilason merged 4 commits intoOSGeo:mainfrom
nilason:fix_r_horizon

Conversation

@nilason
Copy link
Contributor

@nilason nilason commented Feb 4, 2024

Use absolute value for r.horizon print angles < GRASS_EPSILON.

Apple ARM processor results :

printangle = angle * rad2deg - 90.;

with a printangle == -0. to which is added 360 in the next lines:

if (printangle < 0.)
printangle += 360;

Closes #3397

@nilason nilason added bug Something isn't working macOS macOS specific C Related code is in C backport to 8.3 labels Feb 4, 2024
@nilason nilason added this to the 8.4.0 milestone Feb 4, 2024
@nilason nilason requested a review from petrasovaa February 4, 2024 19:41
@github-actions github-actions bot added raster Related to raster data processing module and removed macOS macOS specific labels Feb 4, 2024
@nilason nilason added the macOS macOS specific label Feb 4, 2024
@nilason
Copy link
Contributor Author

nilason commented Feb 4, 2024

@echoix The bot removed the manually added macOS label. Do you have any suggestion/idea how to prevent that?

@echoix
Copy link
Member

echoix commented Feb 4, 2024

@echoix The bot removed the manually added macOS label. Do you have any suggestion/idea how to prevent that?

#531 (comment) It is the sync labels. Its the same way that if a file isn't changed anymore, the label will be removed.

@nilason
Copy link
Contributor Author

nilason commented Feb 4, 2024

@echoix The bot removed the manually added macOS label. Do you have any suggestion/idea how to prevent that?

#531 (comment) It is the sync labels. Its the same way that if a file isn't changed anymore, the label will be removed.

Thanks! Thought so. Let's see if this evolve into a problem / proper PITA.

@echoix
Copy link
Member

echoix commented Feb 4, 2024

Once it is ran, we can always change it, I don't remember if it removes it again after another commit, or it is only the ones added before opening a PR that get removed.

@petrasovaa
Copy link
Contributor

petrasovaa commented Feb 5, 2024

Wouldn't it be better to avoid moving from degrees <-> radians? Something like this (I didn't test):

diff --git a/raster/r.horizon/main.c b/raster/r.horizon/main.c
index 0694658449..65c96092ce 100644
--- a/raster/r.horizon/main.c
+++ b/raster/r.horizon/main.c
@@ -790,6 +790,7 @@ void calculate_shadow(void)
     yp = ymin + yy0;
 
     angle = (single_direction * deg2rad) + pihalf;
+    printangle = single_direction;
 
     maxlength = fixedMaxLength;
     fprintf(fp, "azimuth,horizon_height\n");
@@ -843,7 +844,6 @@ void calculate_shadow(void)
         if (degreeOutput) {
             shadow_angle *= rad2deg;
         }
-        printangle = angle * rad2deg - 90.;
         if (printangle < 0.)
             printangle += 360;
         else if (printangle >= 360.)
@@ -862,6 +862,7 @@ void calculate_shadow(void)
         }
 
         angle += dfr_rad;
+        printangle += step;
 
         if (angle < 0.)
             angle += twopi;

@github-actions github-actions bot removed the macOS macOS specific label Feb 6, 2024
@nilason
Copy link
Contributor Author

nilason commented Feb 6, 2024

Wouldn't it be better to avoid moving from degrees <-> radians? Something like this (I didn't test):

That's a very good suggestion, works fine!

@nilason
Copy link
Contributor Author

nilason commented Feb 6, 2024

Wouldn't it be better to avoid moving from degrees <-> radians? Something like this (I didn't test):

That's a very good suggestion, works fine!

I was too quick to evaluate. Unfortunately this doesn't work as it is. Too much arithmetic on angle and printangle. I haven't been able to come up with a solution without risk of breaking something. Reverted to my initial solution.

@petrasovaa
Copy link
Contributor

Wouldn't it be better to avoid moving from degrees <-> radians? Something like this (I didn't test):

That's a very good suggestion, works fine!

I was too quick to evaluate. Unfortunately this doesn't work as it is. Too much arithmetic on angle and printangle. I haven't been able to come up with a solution without risk of breaking something. Reverted to my initial solution.

You mean it's incorrect in terms of algorithm, or just that it doesn't help with the arm issue?

@nilason
Copy link
Contributor Author

nilason commented Feb 6, 2024

Wouldn't it be better to avoid moving from degrees <-> radians? Something like this (I didn't test):

That's a very good suggestion, works fine!

I was too quick to evaluate. Unfortunately this doesn't work as it is. Too much arithmetic on angle and printangle. I haven't been able to come up with a solution without risk of breaking something. Reverted to my initial solution.

You mean it's incorrect in terms of algorithm, or just that it doesn't help with the arm issue?

Yes, it not working at all, independent on architecture. The CI tests went all red.

@petrasovaa
Copy link
Contributor

petrasovaa commented Feb 6, 2024

This solution runs at least on my machine:

diff --git a/raster/r.horizon/main.c b/raster/r.horizon/main.c
index 0694658449..c07c08c78a 100644
--- a/raster/r.horizon/main.c
+++ b/raster/r.horizon/main.c
@@ -790,6 +790,7 @@ void calculate_shadow(void)
     yp = ymin + yy0;
 
     angle = (single_direction * deg2rad) + pihalf;
+    printangle = single_direction;
 
     maxlength = fixedMaxLength;
     fprintf(fp, "azimuth,horizon_height\n");
@@ -843,11 +844,6 @@ void calculate_shadow(void)
         if (degreeOutput) {
             shadow_angle *= rad2deg;
         }
-        printangle = angle * rad2deg - 90.;
-        if (printangle < 0.)
-            printangle += 360;
-        else if (printangle >= 360.)
-            printangle -= 360;
 
         if (compassOutput) {
             double tmpangle;
@@ -862,11 +858,17 @@ void calculate_shadow(void)
         }
 
         angle += dfr_rad;
+        printangle += step;
 
         if (angle < 0.)
             angle += twopi;
         else if (angle > twopi)
             angle -= twopi;
+
+        if (printangle < 0.)
+            printangle += 360;
+        else if (printangle > 360.)
+            printangle -= 360;
     } /* end of for loop over angles */
 }

@nilason
Copy link
Contributor Author

nilason commented Feb 6, 2024

This solution runs at least on my machine:

And now on mine too! Pushed.

@nilason nilason merged commit ae869f5 into OSGeo:main Feb 6, 2024
@nilason
Copy link
Contributor Author

nilason commented Feb 6, 2024

Thanks! Shall we back port to 8.3?

@petrasovaa
Copy link
Contributor

Thanks! Shall we back port to 8.3?

I would say no, since it's not really a bug fix. But if you plan to backport #3395, it may be easier to backport this as well.

@nilason
Copy link
Contributor Author

nilason commented Feb 6, 2024

Thanks! Shall we back port to 8.3?

I would say no, since it's not really a bug fix.

I’d say it is a bug fix, at least for (apple) arm machines. The question is rather, is the fix safe enough? (It seems to me be the case).

@petrasovaa
Copy link
Contributor

Thanks! Shall we back port to 8.3?

I would say no, since it's not really a bug fix.

I’d say it is a bug fix, at least for (apple) arm machines. The question is rather, is the fix safe enough? (It seems to me be the case).

Yes, I believe it's safe.

landam pushed a commit that referenced this pull request Feb 9, 2024
Back and forth conversion of 0.0 deg to rad to deg resulted in -0.0 leading to
erroneous print out.

Problem pinpointed by Nicklas, solution provided by Anna.

Co-authored-by: Anna Petrasova <[email protected]>
@landam
Copy link
Member

landam commented Feb 9, 2024

Backported to 8.3 in 9150097

@landam landam modified the milestones: 8.4.0, 8.3.2 Feb 9, 2024
@nilason nilason deleted the fix_r_horizon branch February 17, 2024 20:04
jadenabrams100 pushed a commit to ncsu-csc472-spring2024/grass-CI-playground that referenced this pull request Feb 21, 2024
Back and forth conversion of 0.0 deg to rad to deg resulted in -0.0 leading to
erroneous print out.

Problem pinpointed by Nicklas, solution provided by Anna.

Co-authored-by: Anna Petrasova <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working C Related code is in C module raster Related to raster data processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] r.horizon: test fails on Mac with arm64

5 participants