Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use templated type for real_t #233

Open
syoyo opened this issue Nov 6, 2019 · 2 comments
Open

Use templated type for real_t #233

syoyo opened this issue Nov 6, 2019 · 2 comments

Comments

@syoyo
Copy link
Collaborator

syoyo commented Nov 6, 2019

Currently we use ifdef(`TINYOBJLOADER_USE_DOUBLE) to swtich real type

https://github.com/syoyo/tinyobjloader/blob/master/tiny_obj_loader.h#L130

It is better to use templated type for real type, for example:

template<typename T>
struct attrib_t
{
  std::vector<T> vertices;
  ...
}

This may beak existing API, so we may need to introduce new API for it.

@noma
Copy link
Contributor

noma commented Nov 8, 2019

I thought about this when I added the double support, but did not want to break the API. I think a template solution only makes sense, if:

  • users are interested in using more than one real_t in the same program
  • more than the two real types, or user-defined types would be of interest

The current API could be kept, e.g. by using the following pattern:

template<typename T>
struct attrib_generic_t
{
  std::vector<T> vertices;
  ...
}

using attrib_float_t = attrib_generic_t<float>;
using attrib_double_t = attrib_generic_t<double>;

using attrib_t = attrib_float_t; // default to float for the old typename

I think this would not break the API, but I am still not sure what the advantage for users would be. Other than that we could get rid of the TINYOBJLOADER_USE_DOUBLE CMake option. It would probably also move a lot of code to the header because it depends on the template types. So if you decide to go that path, it might be worth thinking about making the library header-only, which makes inclusion into projects trivial, but adds to the compile time.

@syoyo
Copy link
Collaborator Author

syoyo commented Nov 8, 2019

Thanks!

I think this would not break the API

Yes, I thought similar solution

more than the two real types, or user-defined types would be of interest

I'm thinking about this. I think there is almost no demand to support real type other than float and
double, so we can use templates for struct, then simply provide LoadObj with double type signature explicitly may suffice(with this, we don't need TINYOBJLOADER_USE_DOUBLE flag anymore).

bool tinyobj::LoadObj(std::vector<double> &attrib, ... std::vector<material_generic_t<double> > &materials, ...);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants