Rumba C++ SDK
Utils.h
Go to the documentation of this file.
1 /*
2 
3  *
4  ***
5  *****
6  ********************* Mercenaries Engineering SARL
7  ***************** Copyright (C) 2018
8  *************
9  ********* http://www.mercenaries-engineering.com
10  ***********
11  **** ****
12  ** **
13 
14 */
15 #pragma once
16 
17 #include "Export.h"
18 #include <functional>
19 #include <gsl/gsl>
20 
21  namespace maquina
22  {
23  class Value;
24 
26  template<class V, class M>
27  inline V mul_pos(const V& src, const M& m)
28  {
29  const auto a = src[0] * m.x[0][0] + src[1] * m.x[1][0] + src[2] * m.x[2][0] + m.x[3][0];
30  const auto b = src[0] * m.x[0][1] + src[1] * m.x[1][1] + src[2] * m.x[2][1] + m.x[3][1];
31  const auto c = src[0] * m.x[0][2] + src[1] * m.x[1][2] + src[2] * m.x[2][2] + m.x[3][2];
32 
33  return {a, b, c};
34  }
35 
37  template<class V, class M>
38  inline V mul_dir(const V& src, const M& m)
39  {
40  const auto a = src[0] * m.x[0][0] + src[1] * m.x[1][0] + src[2] * m.x[2][0];
41  const auto b = src[0] * m.x[0][1] + src[1] * m.x[1][1] + src[2] * m.x[2][1];
42  const auto c = src[0] * m.x[0][2] + src[1] * m.x[1][2] + src[2] * m.x[2][2];
43 
44  return {a, b, c};
45  }
46 
48  template<class T>
49  inline bool is_identity(const Imath::Matrix44<T>& m)
50  {
51  return
52  m.x[0][0] == T(1) &&
53  m.x[0][1] == T(0) &&
54  m.x[0][2] == T(0) &&
55  m.x[0][3] == T(0) &&
56 
57  m.x[1][0] == T(0) &&
58  m.x[1][1] == T(1) &&
59  m.x[1][2] == T(0) &&
60  m.x[1][3] == T(0) &&
61 
62  m.x[2][0] == T(0) &&
63  m.x[2][1] == T(0) &&
64  m.x[2][2] == T(1) &&
65  m.x[2][3] == T(0) &&
66 
67  m.x[3][0] == T(0) &&
68  m.x[3][1] == T(0) &&
69  m.x[3][2] == T(0) &&
70  m.x[3][3] == T(1);
71  }
72 
73  // Compute the closest point to p on a triangle and possibly returns its barycentric coordinates
76  MAQUINA_EXPORT Imath::V3f closest_point_on_triangle( const Imath::V3f *triangle, const Imath::V3f &p, float* s_out = nullptr, float* t_out = nullptr);
77 
79 
88  MAQUINA_EXPORT int intersect_ray_sphere(const Imath::V3d& o, const Imath::V3d& d, const Imath::V3d& c, float r, float& t0, float& t1);
89 
90  // Computes the point on the first segment which is the closest to the second segment
93 
95 
100 
102  MAQUINA_EXPORT void closest_point_between_lines(const Imath::V3f& o0, const Imath::V3f& d0, const Imath::V3f& o1, const Imath::V3f& d1, float& t0, float& t1);
103 
104  // Computes two vectors orthogonal to the first one
106 
108 
112 
114 
116  MAQUINA_EXPORT Imath::M44d lerp(const Imath::M44d& a, const Imath::M44d& b, double w);
117 
119  const Imath::V3d& p0,
120  const Imath::V3d& p1,
121  const Imath::V3d& p2,
122  const Imath::V3d& goal,
123  const Imath::V3d& normal,
124  Imath::Quatd& r0,
125  double& a1,
126  Imath::V3d& axis1,
127  bool negate_normal = false // Whether the original normal should be negated or not
128  );
129 
131  MAQUINA_EXPORT int solve_cubic(float a, float b, float c, float d, float* roots);
132 
134  MAQUINA_EXPORT int solve_cubic(double a, double b, double c, double d, double* roots);
135 
137  MAQUINA_EXPORT std::vector<uint8_t> deflate(const uint8_t* data, size_t size);
138 
140  MAQUINA_EXPORT std::vector<uint8_t> inflate(const uint8_t* data, size_t size);
141 
143  MAQUINA_EXPORT std::string base64_encode(const uint8_t* data, size_t size);
144 
146  MAQUINA_EXPORT std::vector<uint8_t> base64_decode(const char* text, size_t size);
147 
149  MAQUINA_EXPORT std::wstring system_path(const wchar_t* path);
150 
152 
153  MAQUINA_EXPORT std::string system_path_local(const wchar_t* path);
154 
156 
158  MAQUINA_EXPORT const std::vector<std::pair<std::string, std::wstring>>& path_variables();
159 
161 
163  MAQUINA_EXPORT void set_path_variables(const std::vector<std::pair<std::string, std::wstring>>& path_vars);
164 
166 
169  MAQUINA_EXPORT std::wstring project_path(const wchar_t* system_path);
170 
172  MAQUINA_EXPORT std::wstring utf8_to_wstring(const char* uft8);
173 
175  MAQUINA_EXPORT std::string wstring_to_utf8(const wchar_t* str);
176 
178  MAQUINA_EXPORT std::wstring local_to_wstring(const char* str);
179 
181  MAQUINA_EXPORT std::string wstring_to_local(const wchar_t* str);
182 
184  MAQUINA_EXPORT std::string to_log(const std::wstring& s);
185 
187  MAQUINA_EXPORT std::wstring getenv_w(const char* name);
188 
189  MAQUINA_EXPORT bool list_directory_files(const wchar_t* directory, std::vector<std::wstring>& files);
190 
192  MAQUINA_EXPORT bool file_exists(const wchar_t* file);
193 
195  MAQUINA_EXPORT std::string file_extension(const wchar_t* file);
196 
198  MAQUINA_EXPORT std::wstring file_name(const wchar_t* file);
199 
201  MAQUINA_EXPORT std::wstring parent_path(const wchar_t* file);
202 
204 
206  MAQUINA_EXPORT std::wstring project_directory();
207 
209 
211  MAQUINA_EXPORT void set_project_directory(const wchar_t* path);
212 
214  MAQUINA_EXPORT void drm_init(const std::wstring& path, const std::string& license_server);
215 
218 
220  MAQUINA_EXPORT std::pair<bool, std::string> drm_status();
221 
223  MAQUINA_EXPORT std::string drm_hostid();
224 
226  MAQUINA_EXPORT bool has_tag(const std::string& tags, const char* tag);
227 
229  MAQUINA_EXPORT void parallel_for(uint32_t first,
230  uint32_t last,
231  uint32_t batch_size,
232  const std::function<void(uint32_t first, uint32_t last)>& function
233  );
234 
236  MAQUINA_EXPORT Imath::Box3f compute_aabb(const gsl::span<const Imath::V3f>& points);
237 
239 
242  MAQUINA_EXPORT Value setting(const char* path);
243 
245 
248  MAQUINA_EXPORT void set_setting(const char* path, const Value& value);
249  }
Definition: ImathBox.h:71
MAQUINA_EXPORT std::string base64_encode(const uint8_t *data, size_t size)
Encode a bunch of binary data in a base64 text.
MAQUINA_EXPORT std::wstring file_name(const wchar_t *file)
Return a file name.
V mul_dir(const V &src, const M &m)
A faster alternative to multiply a direction by a 4x4 matrix, ignoring the homogeneous normalization...
Definition: Utils.h:38
MAQUINA_EXPORT std::vector< uint8_t > inflate(const uint8_t *data, size_t size)
Uncompress a bunch of binary data with zlib.
MAQUINA_EXPORT std::vector< uint8_t > deflate(const uint8_t *data, size_t size)
Compress a bunch of binary data with zlib.
Definition: ImathFrame.h:42
MAQUINA_EXPORT bool list_directory_files(const wchar_t *directory, std::vector< std::wstring > &files)
Definition: ImathFrame.h:43
MAQUINA_EXPORT Imath::V3f closest_point_segment_to_segment(const Imath::V3f *s1, const Imath::V3f *s2)
MAQUINA_EXPORT void ik_3_joints(const Imath::V3d &p0, const Imath::V3d &p1, const Imath::V3d &p2, const Imath::V3d &goal, const Imath::V3d &normal, Imath::Quatd &r0, double &a1, Imath::V3d &axis1, bool negate_normal=false)
MAQUINA_EXPORT std::wstring project_directory()
Return the project directory.
MAQUINA_EXPORT std::string system_path_local(const wchar_t *path)
Expand a document file path in a file system path.
MAQUINA_EXPORT int solve_cubic(float a, float b, float c, float d, float *roots)
Solve a cubic equation.
MAQUINA_EXPORT std::string wstring_to_local(const wchar_t *str)
Convert a std::wstring into a string in the default system charset.
MAQUINA_EXPORT std::wstring system_path(const wchar_t *path)
Expand a document file path in a file system path.
V mul_pos(const V &src, const M &m)
A faster alternative to multiply a position by a 4x4 matrix, ignoring the homogeneous normalization...
Definition: Utils.h:27
MAQUINA_EXPORT bool file_exists(const wchar_t *file)
Return true if a file exist.
MAQUINA_EXPORT std::pair< bool, std::string > drm_status()
Return the drm status.
MAQUINA_EXPORT Imath::Box3f compute_aabb(const gsl::span< const Imath::V3f > &points)
Compute points bounding box.
This version of the SDK is unstable, i-e, it may change with no warning.
Definition: AddCurveAction.h:20
MAQUINA_EXPORT std::string to_log(const std::wstring &s)
Convert a wide string in string to use in the logger functions.
MAQUINA_EXPORT void set_setting(const char *path, const Value &value)
Set a setting value.
MAQUINA_EXPORT void parallel_for(uint32_t first, uint32_t last, uint32_t batch_size, const std::function< void(uint32_t first, uint32_t last)> &function)
Perform a for loop using threads.
MAQUINA_EXPORT int intersect_ray_sphere(const Imath::V3d &o, const Imath::V3d &d, const Imath::V3d &c, float r, float &t0, float &t1)
Compute the intersections between an infinit ray and a sphere.
MAQUINA_EXPORT std::wstring parent_path(const wchar_t *file)
Return a file parent path.
MAQUINA_EXPORT std::string drm_hostid()
Return the system host id.
Definition: ImathQuat.h:71
#define MAQUINA_EXPORT
Definition: Export.h:31
bool is_identity(const Imath::Matrix44< T > &m)
Returns true if the matrix is the identity.
Definition: Utils.h:49
MAQUINA_EXPORT std::wstring project_path(const wchar_t *system_path)
Reduce a file system path to a project file path.
MAQUINA_EXPORT const std::vector< std::pair< std::string, std::wstring > > & path_variables()
Return the currently registered path variables.
MAQUINA_EXPORT void set_project_directory(const wchar_t *path)
Set the project directory.
MAQUINA_EXPORT Imath::M44f blend_to_identity(const Imath::M44f &a, float w)
Blend a matrix to identity.
MAQUINA_EXPORT Imath::V3d closest_point_to_line(const Imath::V3d &o, const Imath::V3d &d, const Imath::V3d &p)
Compute the closest point to p on a line.
MAQUINA_EXPORT std::wstring utf8_to_wstring(const char *uft8)
Convert an utf-8 string into a std::wstring.
MAQUINA_EXPORT std::string file_extension(const wchar_t *file)
Return a file extension.
MAQUINA_EXPORT void set_path_variables(const std::vector< std::pair< std::string, std::wstring >> &path_vars)
Register the path variables.
MAQUINA_EXPORT void closest_point_between_lines(const Imath::V3f &o0, const Imath::V3f &d0, const Imath::V3f &o1, const Imath::V3f &d1, float &t0, float &t1)
Compute the closest point between two lines.
MAQUINA_EXPORT Imath::V3f closest_point_on_triangle(const Imath::V3f *triangle, const Imath::V3f &p, float *s_out=nullptr, float *t_out=nullptr)
MAQUINA_EXPORT std::string wstring_to_utf8(const wchar_t *str)
Convert a std::wstring into an utf-8 string.
MAQUINA_EXPORT std::wstring getenv_w(const char *name)
Return the content of an environment variable.
MAQUINA_EXPORT void get_orthogonal_vectors(const Imath::V3d &fx, Imath::V3d &fy, Imath::V3d &fz)
MAQUINA_EXPORT std::vector< uint8_t > base64_decode(const char *text, size_t size)
Decode a bunch of binary data from a base64 text.
MAQUINA_EXPORT void drm_init(const std::wstring &path, const std::string &license_server)
Initialize the drm.
MAQUINA_EXPORT Value setting(const char *path)
Get a setting value.
MAQUINA_EXPORT void drm_release()
Release the drm.
MAQUINA_EXPORT Imath::M44d lerp(const Imath::M44d &a, const Imath::M44d &b, double w)
Interplate two matrices.
MAQUINA_EXPORT std::wstring local_to_wstring(const char *str)
Convert a string in the default system charset into a std::wstring.
T x[4][4]
Definition: ImathMatrix.h:404
MAQUINA_EXPORT bool has_tag(const std::string &tags, const char *tag)
Check a tag exist in a comma separate tag list.