Function
I have this function to detect if a tetrahedron has almost zero volume, i.e. it’s flat:
import (
"math"
v3 "github.com/deadsy/sdfx/vec/v3"
)
// MATHEMATICA script is available here:
// https://math.stackexchange.com/a/4709610/197913
func isZeroVolume(a, b, c, d v3.Vec) (bool, float64) {
ab := b.Sub(a)
ac := c.Sub(a)
ad := d.Sub(a)
// Note that the `Norm` function of MATHEMATICA is equivalent to our `Length()` function.
nab := ab.Length()
ncd := ac.Sub(ad).Length()
nbd := ab.Sub(ad).Length()
nbc := ab.Sub(ac).Length()
nac := ac.Length()
nad := ad.Length()
// Check for 0 edge lengths
if nab == 0 || ncd == 0 ||
nbd == 0 || nbc == 0 ||
nac == 0 || nad == 0 {
return true, 0
}
volume := 1.0 / 6.0 * math.Abs(ab.Cross(ac).Dot(ad))
denom := (nab + ncd) * (nac + nbd) * (nad + nbc)
// Tolerance derived from here:
// https://math.stackexchange.com/a/4709610/197913
tolerance := 480.0
rho := tolerance * volume / denom
return rho < 1, volume
}
Input
I step through the code with these four input points:
{X: -1.572793602943422, Y: -4.157202807477221, Z: 5.603983008116483}
{X: -2.45160644054413, Y: -3.4214927673339854, Z: 6.135950530673543}
{X: -2.45160644054413, Y: -3.7163730403986044, Z: 5.603983008116483}
{X: -1.572793602943422, Y: -3.5355907043553003, Z: 6.482795845717191}
Debugger
Stepping through the code by VS Code debugger indicates that the local variable have these values:
Question
The values shown by the debugger make no sense. How can denom
and tolerance
be 0
? It makes no sense to me. Am I missing something?
2
Answers
Well, I just continued stepping through the code by the debugger and the values are now as expected. Like the constant value of
tolerance
:I don't know how and why it gets corrected by just continuing the stepping...
Here is a simplified version of the demo to show the issue:
And here is the output of a delve debug session:
You see that the instruction
0x4608de
is picked as the breakpoint at line:7
. At this point, the variablesa
andb
haven’t got their values yet (a
gets the value later at the instruction0x4608e2
, whileb
at0x4608e7
).That’s why you don’t get the correct value at first and it "fixes itself" later.
This issue has been reported as cmd/compile: bad DWARF location for variable #58813.