• 90.57% Rate
  • 48 Hits
  • 5 Missed
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 9x
  • 2x
  • 7x
  • 1x
  • 6x
  • 6x
  • 6x
  • 1x
  • 5x
  • 1x
  • 4x
  • 4x
  • 4x
  • 4x
  • 3x
  • 6x
  • 3x
  • 3x
  • 3x
  • 3x
  • 6x
  • 3x
  • 3x
  • 3x
  • 3x
  • 9x
  • 12x
  • 6x
  • 6x
  • 6x
  • 6x
  • 3x
  • 3x
  • 3x
  • 2x
  • 2x
  • 1x
  • 1x
  • -------------------------------------------------------------------------------
  • -- Composite materials' wrapper for PUMAS Physics
  • -- Author: Valentin Niess
  • -- License: GNU LGPL-3.0
  • -------------------------------------------------------------------------------
  • -------------------------------------------------------------------------------
  • local ffi = require('ffi')
  • local compat = require('pumas.compat')
  • local clib = require('pumas.clib')
  • local error = require('pumas.error')
  • local metatype = require('pumas.metatype')
  • local composite = {}
  • -------------------------------------------------------------------------------
  • -- The CompositeMaterials metatype
  • -------------------------------------------------------------------------------
  • local CompositeMaterials = {}
  • do
  • local function pairs_ (self)
  • if not self then
  • error.raise{fname = 'CompositeMaterials', argnum = 1,
  • expected = 'a CompositeMaterials table', got = metatype.a(self)}
  • end
  • return pairs(self._fractions)
  • end
  • function CompositeMaterials:__index (k)
  • if k == '__metatype' then
  • return 'CompositeMaterials'
  • elseif k == 'pairs' then
  • return pairs_
  • else
  • local v = self._fractions[k]
  • if not v then
  • error.raise{['type'] = 'CompositeMaterials', bad_member = k}
  • else
  • return v
  • end
  • end
  • end
  • end
  • function CompositeMaterials:__newindex (k, v)
  • if not rawget(self._fractions, k) then
  • error.raise{['type'] = 'CompositeMaterials', bad_member = k}
  • end
  • if type(v) ~= 'number' then
  • error.raise{fname = 'CompositeMaterials', argname = k,
  • expected = 'a number', got = metatype.a(v)}
  • end
  • rawset(self._fractions, k, v)
  • rawset(self, '_needs_update', true)
  • rawset(self._physics, '_update_composites', true)
  • end
  • -------------------------------------------------------------------------------
  • -- Constructor for a CompositeMaterials table
  • -------------------------------------------------------------------------------
  • do
  • local function new (cls, physics_, index)
  • local n_materials = ffi.new('int [1]')
  • clib.pumas_physics_composite_properties(
  • physics_._c[0], index, n_materials, nil, nil)
  • n_materials = tonumber(n_materials[0])
  • local indices = ffi.new('int [?]', n_materials)
  • local fractions = ffi.new('double [?]', n_materials)
  • clib.pumas_physics_composite_properties(
  • physics_._c[0], index, nil, indices, fractions)
  • local name_ = ffi.new('const char *[1]')
  • local fractions_ = compat.table_new(0, n_materials)
  • local names = compat.table_new(n_materials, 0) -- XXX is this needed?
  • for i = 1, n_materials do
  • clib.pumas_physics_material_name(
  • physics_._c[0], indices[i - 1], name_)
  • local k = ffi.string(name_[0])
  • fractions_[k] = tonumber(fractions[i - 1])
  • names[i] = k
  • end
  • local self = {_names = names, _fractions = fractions_,
  • _n = n_materials, _physics = physics_, _needs_update = false}
  • return setmetatable(self, cls)
  • end
  • composite.CompositeMaterials = setmetatable(
  • CompositeMaterials, {__call = new})
  • error.register('CompositeMaterials', CompositeMaterials)
  • end
  • -------------------------------------------------------------------------------
  • -- Return the sub-package
  • -------------------------------------------------------------------------------
  • return composite