# Class: UtilFunctions

### LuxModule: luxinialuacore

The UtilFunctions class is a collection of miscellanous useful lua functions.

## Methods:

### Method overview:

- color3byte
- color3hsv
- color3rgb2hsv
- color4byte
- dofile
- fileexists
- getModelColMesh
- loadCollisionDataFile
- loadFontFile
- loadfile
- luaDoFile
- luaLoadFile
- luafilename
- luafilepath
- pairsByKeys
- printf
- setTableValue
- smallLogoPos
- tabletotalcount

color3byte (float r,g,b)
  returns: (float r,g,b)
  converts the incoming color values 0-255, to 0-1 floats
color3hsv (float h,s,v)
  returns: (float r,g,b)
  converts the incoming hsv to color to 0-1 rgb floats
color3rgb2hsv (float r,g,b)
  returns: (float h,s,v)
  converts the rgb color to a hsv color value
color4byte (float r,g,b,a)
  returns: (float r,g,b,a)
  converts the incoming color values 0-255, to 0-1 floats
dofile (string path,...)
  returns: ()
  similiar to original dofile except that this function trys to find the file in different locations relative to all loaded luafiles. This function replaces the original dofile function.
fileexists (string path)
  returns: (boolean exists)

  tries out if the given file exists. This is not very clean since it only trys to open and close the file and if it doesn't throw an error it is assumed that the file exists.

getModelColMesh (model,[boolean needrawtable],[matrix4x4],[boolean visibleonly],[boolean neednotrimeshdata])
  returns: (tableBase,tableHit,[tableRaw])

  Creates a geom trimeshdata from the given model. Optionally transforms vertices.
  tableBase : {data = dgeomtrimeshdata, aabox = {min{x,y,z},max{x,y,z}, center = {x,y,z}}
  tableHit : {table similar to base, but for every "hitmesh" = meshes that contain "hitzone" in the material name
  tableRaw : { inds = indices{}, verts = vertices{}, tris = tridata{{mesh=meshid,orig=original index}...} }, table with all indices/vertices and a table with the tridata for every face in the model

loadCollisionDataFile ([string path,table],[boolean needoriginaltable],[boolean createl3ds])
  returns: (dgeom table,[extratable])

  loads dgeoms from the collision data lua file. extratable can contain the tables: "original" and/or "visual".Currently exported by 3dsmax.

loadFontFile (string fontname)
  returns: (fontset,texture)
  loads a font generated by the Font Bitmap Generator. The fontname must not contain a suffix (.lua). The function trys to load a fontfile that must reside in a 'fonts' directory. You must not delete the fontset yourself. All characters in the fontset that are not set by the font itself are mapped to the glyphid 0 (lefttopmost glyph)
loadfile (string path,...)
  returns: ()
  similiar to original loadfile except that this function trys to find the file in different locations relative to all loaded luafiles. This function replaces the original loadfile function.
luaDoFile
  original lua dofile function
luaLoadFile
  original lua loadfile function
luafilename (int level)
  returns: (string filename)

  retrieves the filename of a luafunction, if possible. The level is the functioncallerstack and should be >=1 returns filename and level of the found path.

luafilepath (int level)
  returns: (string filepath)
  like luafilename, but removes the filename itself
pairsByKeys (table,[comperator])
  returns: (function)
  returns an iterator function to traverse the table in sorted order. The comperator function can be used to use another ordering for traversion. The returned function returns three values if it is called: the key, the value, a number that tells the current index and another number that tells how many indeces are left to be be iterated. The comperator should take care about both values it gets - these could either be numbers, functions, strings, tables, etc.

  #### Example:

       for key,value in UtilFunctions.pairsByKeys({cde=1, abc=2})
       	do print(key,value) end

  #### Output:

       abc  2
       cde  3

printf (string format, ...)
  returns: ()

  prints out a formated string as the rules of string.format describe. This function is also stored in the global variable named printf

setTableValue (table,value,[newvalue])
  returns: ([index,oldvalue])

  searches a val in the given table. The first hit is replaced by newvalue, which removes the tableentry if it is nil. You can use it therefore to remove a value from a table. The index of the removed element and the value is then returned.

  If no match was found, nothing is returned.

smallLogoPos (int n)
  returns: ()

  Sets the position of the small Luxinia Logo at one of the four corners (whatever you like more). Topright = 1, rightbottom = 2, bottomleft = 3, topleft = 4.

tabletotalcount (table)
  returns: (n)

  returns the number of elements in that table and all of its childs it also traverses into the metatables, but ignores tables that are weak typed. It also counts metatables as element.

  This function should help to find memory leaks, which can occure if table references are not removed.