Skip to content

Casting from dualnum to float for use in Lua functions #3

@chrisranderson

Description

@chrisranderson

So I'm trying to do some stuff in Lua and send it back to Terra (maybe sounds familiar from my past stuff). Long story short, I modified ad.t to get it to work, I'm just checking to see if I'm thinking on the right lines.

Here's my model, with the cast of my lua function right before it:

    local terra_get_image_diff = terralib.cast({float, float, float, float, float, float, float, float} -> int8 , get_image_difference)

    return terra()

      var x_center = qs.gaussian(IMAGE_WIDTH/2.0, 10.0, {struc=false})
      var y_center = qs.gaussian(IMAGE_HEIGHT/2.0, 10.0, {struc=false})

      var width = qs.gaussian(30.0, 1.0, {struc=false})
      var height = qs.gaussian(30.0, 1.0, {struc=false})

      var floor_intensity = qs.uniform(0.0, 1.0, {struc=false})
      var ceiling_intensity = qs.uniform(0.0, 1.0, {struc=false})
      var wall_intensity = qs.uniform(0.0, 1.0, {struc=false})
      var square_intensity = qs.uniform(0.0, 1.0, {struc=false})

      var difference = terra_get_image_diff(x_center, y_center, width, height, floor_intensity, ceiling_intensity, wall_intensity, square_intensity)

      -- should take into account previous difference
      qs.factor(-difference/25.0)

      return 0

(full file here)

I would run it and get this error:

terra hallway-match.t 
src/terralib.lua:575: Errors reported during compilation.
hallway-match.t:219: invalid conversion from dualnum to float
      var difference = terra_get_image_diff(x_center, y_center, width, height, floor_intensity, ceiling_intensity, wall_intensity, square_intensity)

I fixed it like this, at this line in ad.t:

function num.metamethods.__cast(from, to, exp)

    if from:isfloat() and to == num then
        return `num { DualNumBase.new(exp, nil) }
    elseif from == num and to:isfloat() then
        return `[to](exp:val())
    else
        error(string.format("ad.t: Cannot cast '%s' to '%s'", tostring(from), tostring(to)))
    end
end

Is this close to what needs to happen?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions