Re: [GRASS-dev] How to: if undefined, set to NULL?
Nikos Alexandris: > Updated in sandbox, see: https://trac.osgeo.org/grass/changeset/69365 > Is it though correct? Vaclav Petras wrote: I was not looking at the logic but if (hue == -1.0) { is not safe. Although it will probably work most of the time if you set it to the exactly same value (`hue = -1;`), Glynn Clements[2016-09-05 16:46:31 +0100]: There's no "probably" about it. If you execute "hue = -1;", a subsequent "if (hue == -1)" comparison *will* succeed. Rounding error only occurs when the correct result isn't exactly representable, which isn't the case here. It doesn't occur randomly. If using -1 is good, it seems simpler (to me, the ignorant) than trying introducing yet another boolean flag. Nikos ___ grass-dev mailing list grass-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-dev
Re: [GRASS-dev] How to: if undefined, set to NULL?
Vaclav Petras wrote: > > Updated in sandbox, see: https://trac.osgeo.org/grass/changeset/69365 > > > > Is it though correct? > > > > I was not looking at the logic but > > if (hue == -1.0) { > > is not safe. Although it will probably work most of the time if you set it > to the exactly same value (`hue = -1;`), There's no "probably" about it. If you execute "hue = -1;", a subsequent "if (hue == -1)" comparison *will* succeed. Rounding error only occurs when the correct result isn't exactly representable, which isn't the case here. It doesn't occur randomly. -- Glynn Clements___ grass-dev mailing list grass-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-dev
Re: [GRASS-dev] How to: if undefined, set to NULL?
Nikos Alexandris: Updated in sandbox, see: https://trac.osgeo.org/grass/changeset/69365 Is it though correct? Vaclav Petras: I was not looking at the logic but if (hue == -1.0) { is not safe. Although it will probably work most of the time if you set it to the exactly same value (`hue = -1;`), but it is not portable because it is exact comparison for floats/doubles. Generally, you need to do something like if (fabs(hue - (-1.0)) < GRASS_EPSILON) { However, here it might be more appropriate to signal the good or bad state of hue by some other variable: hue_good = FALSE; /* setting hue here together with hue_good = TRUE */ if (!hue_good) { /* do the null set */ Thanks Vaclav. Will try that out -- a quick run tells me that I didn't understand yet how to do it. Will get to it. Also, it seems that the code is not consistent with FCELL versus DCELL. I can see DCELL *rowbuffer and Rast_is_d_null_value but also (FCELL)saturation as well as floats and doubles. Since the input claims to be always DCELL, use DCELL/double everywhere. See my other post, if you have some time. Ideally, it would be smart to have the input be autodetected, be it CELL or DCELL, ranging in [0, 2^bitness-of-input-data) while the output will be always FCELL (hue in [0.0,360.0] and saturation/lightness in [0.0,1.0]. Related to that, although I'm never sure about where is its appropriate to use DCELL and where double (DCELL currently gets evaluated to double: typedef double DCELL;). I would probably use DCELL everywhere in this function; just to make it clear that we deal purely with cell values and there is no need to switch to anything else. But using DCELL everywhere is a bit of waisting, if I understand correctly. I'd like to keep things clean, even if it appears to be unnecessary to do so on first sight. Nikos ___ grass-dev mailing list grass-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-dev
Re: [GRASS-dev] How to: if undefined, set to NULL?
On Sun, Sep 4, 2016 at 9:45 AM, Nikos Alexandriswrote: > Updated in sandbox, see: https://trac.osgeo.org/grass/changeset/69365 > > Is it though correct? > I was not looking at the logic but if (hue == -1.0) { is not safe. Although it will probably work most of the time if you set it to the exactly same value (`hue = -1;`), but it is not portable because it is exact comparison for floats/doubles. Generally, you need to do something like if (fabs(hue - (-1.0)) < GRASS_EPSILON) { However, here it might be more appropriate to signal the good or bad state of hue by some other variable: hue_good = FALSE; /* setting hue here together with hue_good = TRUE */ if (!hue_good) { /* do the null set */ Also, it seems that the code is not consistent with FCELL versus DCELL. I can see DCELL *rowbuffer and Rast_is_d_null_value but also (FCELL)saturation as well as floats and doubles. Since the input claims to be always DCELL, use DCELL/double everywhere. Related to that, although I'm never sure about where is its appropriate to use DCELL and where double (DCELL currently gets evaluated to double: typedef double DCELL;). I would probably use DCELL everywhere in this function; just to make it clear that we deal purely with cell values and there is no need to switch to anything else. And of course, congrats and kudos for your C efforts, Nikos! Vaclav ___ grass-dev mailing list grass-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-dev
Re: [GRASS-dev] How to: if undefined, set to NULL?
Nikos Alexandris: if (chroma == 0.0) { saturation = 0.0; /* undefined -- (how to) set to NULL? */ hue = 0.0; I'd like to set hue to NULL, whenever the condition is met. How do I do that? Markus Neteler: Something like: for (row = 0; row < nrows; row++) { G_percent(row, nrows, 5); for (col = 0; col < ncols; col++) { cell_buf[col] = bas[SEG_INDEX(ba_seg, row, col)]; if (cell_buf[col] == 0) Rast_set_null_value(_buf[col], 1, CELL_TYPE); Alright, that is: anything that `_buf[col]` points to, set the number of values `1` to NULL. Updated in sandbox, see: https://trac.osgeo.org/grass/changeset/69365 Is it though correct? Nikos ___ grass-dev mailing list grass-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-dev
Re: [GRASS-dev] How to: if undefined, set to NULL?
* Markus Neteler[2016-08-27 15:37:35 +0200]: On Sat, Aug 27, 2016 at 3:26 PM, Nikos Alexandris wrote: While modifying i.rgb.his, if (chroma == 0.0) { saturation = 0.0; /* undefined -- (how to) set to NULL? */ hue = 0.0; rowbuffer[0][sample] = (FCELL)hue; rowbuffer[1][sample] = (FCELL)intensity; rowbuffer[2][sample] = (FCELL)saturation; } I'd like to set hue to NULL, whenever the condition is met. How do I do that? hue = NULL; does not work. No, it is done withRast_set_null_value() Something like: for (row = 0; row < nrows; row++) { G_percent(row, nrows, 5); for (col = 0; col < ncols; col++) { cell_buf[col] = bas[SEG_INDEX(ba_seg, row, col)]; if (cell_buf[col] == 0) Rast_set_null_value(_buf[col], 1, CELL_TYPE); Alright, that is: anything that `_buf[col]` points to, set the number of values `1` to NULL. I need a few more examples to understand. What is the "number of values"? Could it be a series of multiple values? Nikos } Rast_put_row(basin_fd, cell_buf, CELL_TYPE); } (random example, taken from r.water.outlet/main.c) HTH, Markus -- Nikos Alexandris | Remote Sensing & Geomatics GPG Key Fingerprint 6F9D4506F3CA28380974D31A9053534B693C4FB3 ___ grass-dev mailing list grass-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-dev
Re: [GRASS-dev] How to: if undefined, set to NULL?
On Sat, Aug 27, 2016 at 3:26 PM, Nikos Alexandriswrote: > While modifying i.rgb.his, > >if (chroma == 0.0) { > >saturation = 0.0; > >/* undefined -- (how to) set to NULL? */ >hue = 0.0; > >rowbuffer[0][sample] = (FCELL)hue; >rowbuffer[1][sample] = (FCELL)intensity; >rowbuffer[2][sample] = (FCELL)saturation; >} > > > I'd like to set hue to NULL, whenever the condition is met. How do I do > that? > >hue = NULL; > > does not work. No, it is done withRast_set_null_value() Something like: for (row = 0; row < nrows; row++) { G_percent(row, nrows, 5); for (col = 0; col < ncols; col++) { cell_buf[col] = bas[SEG_INDEX(ba_seg, row, col)]; if (cell_buf[col] == 0) Rast_set_null_value(_buf[col], 1, CELL_TYPE); } Rast_put_row(basin_fd, cell_buf, CELL_TYPE); } (random example, taken from r.water.outlet/main.c) HTH, Markus ___ grass-dev mailing list grass-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-dev