GenVecAccessors.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/python
  2. # Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  3. # All rights reserved.
  4. # Code licensed under the BSD License.
  5. # http://www.anki3d.org/LICENSE
  6. def gen_vec_class_name(n):
  7. return "TVec<T, " + str(n) + ">"
  8. # Index to component string
  9. def index_to_component(idx):
  10. arr = ["x", "y", "z", "w"]
  11. return arr[idx]
  12. def gen_accessors():
  13. h = ""
  14. scalar = "T"
  15. # All swizzle accessors
  16. for vec_components in range(2, 4 + 1):
  17. for a in range(0, 4):
  18. for b in range(0, 4 if vec_components > 1 else 1):
  19. for c in range(0, 4 if vec_components > 2 else 1):
  20. for d in range(0, 4 if vec_components > 3 else 1):
  21. arr = [a, b, c, d]
  22. # Enable it
  23. max_comp = max(max(max(a, b), c), d)
  24. h += "\n\tANKI_ENABLE_IF_EXPRESSION(N > " + str(max_comp) + ")\n"
  25. # Return value
  26. h += "\t"
  27. if vec_components == 1:
  28. h += scalar + " "
  29. else:
  30. ret_type = gen_vec_class_name(vec_components)
  31. h += ret_type + " "
  32. # Method name
  33. method_name = ""
  34. for i in range(0, vec_components):
  35. method_name += index_to_component(arr[i])
  36. h += method_name
  37. # Write the body
  38. h += "() const\n"
  39. h += "\t{\n"
  40. h += "\t\treturn " + gen_vec_class_name(vec_components) + "("
  41. for i in range(0, vec_components):
  42. h += "m_carr[" + str(arr[i]) + ("], " if i < vec_components - 1 else "]);\n")
  43. h += "\t}\n"
  44. return h
  45. def main():
  46. print(gen_accessors())
  47. if __name__ == "__main__":
  48. main()