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 #include <Maquina/Scene.h>
21 
22  namespace maquina
23  {
24  class Value;
25  class Node;
26  class Plug;
27 
29  template<class V, class M>
30  inline V mul_pos(const V& src, const M& m)
31  {
32  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];
33  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];
34  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];
35 
36  return {a, b, c};
37  }
38 
40  template<class V, class M>
41  inline V mul_dir(const V& src, const M& m)
42  {
43  const auto a = src[0] * m.x[0][0] + src[1] * m.x[1][0] + src[2] * m.x[2][0];
44  const auto b = src[0] * m.x[0][1] + src[1] * m.x[1][1] + src[2] * m.x[2][1];
45  const auto c = src[0] * m.x[0][2] + src[1] * m.x[1][2] + src[2] * m.x[2][2];
46 
47  return {a, b, c};
48  }
49 
51  template<class T>
52  inline bool is_identity(const Imath::Matrix44<T>& m)
53  {
54  return
55  m.x[0][0] == T(1) &&
56  m.x[0][1] == T(0) &&
57  m.x[0][2] == T(0) &&
58  m.x[0][3] == T(0) &&
59 
60  m.x[1][0] == T(0) &&
61  m.x[1][1] == T(1) &&
62  m.x[1][2] == T(0) &&
63  m.x[1][3] == T(0) &&
64 
65  m.x[2][0] == T(0) &&
66  m.x[2][1] == T(0) &&
67  m.x[2][2] == T(1) &&
68  m.x[2][3] == T(0) &&
69 
70  m.x[3][0] == T(0) &&
71  m.x[3][1] == T(0) &&
72  m.x[3][2] == T(0) &&
73  m.x[3][3] == T(1);
74  }
75 
76  // Compute the closest point to p on a triangle and possibly returns its barycentric coordinates
79  MAQUINA_EXPORT Imath::V3f closest_point_on_triangle( const Imath::V3f *triangle, const Imath::V3f &p, float* s_out = nullptr, float* t_out = nullptr);
80 
82 
91  MAQUINA_EXPORT int intersect_ray_sphere(const Imath::V3d& o, const Imath::V3d& d, const Imath::V3d& c, float r, float& t0, float& t1);
92 
93  // Computes the point on the first segment which is the closest to the second segment
96 
98 
103 
105  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);
106 
107  // Computes two vectors orthogonal to the first one
109 
111 
115 
117 
119  MAQUINA_EXPORT Imath::M44f lerp(const Imath::M44f& a, const Imath::M44f& b, float w);
120  MAQUINA_EXPORT Imath::M44d lerp(const Imath::M44d& a, const Imath::M44d& b, double w);
121 
123  const Imath::V3d& p0,
124  const Imath::V3d& p1,
125  const Imath::V3d& p2,
126  const Imath::V3d& goal,
127  const Imath::V3d& normal,
128  Imath::Quatd& r0,
129  double& a1,
130  Imath::V3d& axis1,
131  bool negate_normal = false // Whether the original normal should be negated or not
132  );
133 
135  MAQUINA_EXPORT int solve_cubic(float a, float b, float c, float d, float* roots);
136 
138  MAQUINA_EXPORT int solve_cubic(double a, double b, double c, double d, double* roots);
139 
141  MAQUINA_EXPORT std::vector<uint8_t> deflate(const uint8_t* data, size_t size);
142 
144  MAQUINA_EXPORT std::vector<uint8_t> inflate(const uint8_t* data, size_t size);
145 
147  MAQUINA_EXPORT std::string base64_encode(const uint8_t* data, size_t size);
148 
150  MAQUINA_EXPORT std::vector<uint8_t> base64_decode(const char* text, size_t size);
151 
153  MAQUINA_EXPORT std::wstring expand_string(const wchar_t* path);
154 
156 
157  MAQUINA_EXPORT std::wstring system_path(const wchar_t* path);
158 
160 
161  MAQUINA_EXPORT std::string system_path(const std::string_view& path);
162 
178  MAQUINA_EXPORT std::wstring project_path(const wchar_t* system_path);
179 
181 
182  MAQUINA_EXPORT std::string system_path_local(const wchar_t* path);
183 
185 
188  MAQUINA_EXPORT std::string comparable_path(std::string s);
189 
191 
202  MAQUINA_EXPORT bool same_file(std::string a, std::string b);
203 
205 
207  MAQUINA_EXPORT const std::vector<std::pair<std::string, std::wstring>>& path_variables();
208 
210 
212  MAQUINA_EXPORT void set_path_variables(const std::vector<std::pair<std::string, std::wstring>>& path_vars);
213 
215  MAQUINA_EXPORT std::wstring utf8_to_wstring(const char* uft8);
216 
218  MAQUINA_EXPORT std::string wstring_to_utf8(const wchar_t* str);
219 
221  MAQUINA_EXPORT std::wstring local_to_wstring(const char* str);
222 
224  MAQUINA_EXPORT std::string wstring_to_local(const wchar_t* str);
225 
227  MAQUINA_EXPORT std::string to_log(const std::wstring& s);
228 
230  MAQUINA_EXPORT std::wstring getenv_w(const char* name);
231 
232  MAQUINA_EXPORT bool list_directory_files(const wchar_t* directory, std::vector<std::wstring>& files, bool relative=false);
233 
235  MAQUINA_EXPORT bool file_exists(const wchar_t* file);
236 
238  MAQUINA_EXPORT std::string file_extension(const wchar_t* file);
239 
241  MAQUINA_EXPORT std::wstring file_name(const wchar_t* file);
242 
244  MAQUINA_EXPORT std::wstring parent_path(const wchar_t* file);
245 
247 
249  MAQUINA_EXPORT std::wstring project_directory();
250 
252 
254  MAQUINA_EXPORT void set_project_directory(const wchar_t* path);
255 
257  MAQUINA_EXPORT void drm_init(const std::string& software_name, const std::wstring& license_file_path, const std::string& license_server);
258 
261 
263  MAQUINA_EXPORT std::pair<bool, std::string> drm_status();
264 
266  MAQUINA_EXPORT std::string drm_hostid();
267 
269  MAQUINA_EXPORT bool has_tag(const std::string& tags, const char* tag);
270 
272  MAQUINA_EXPORT std::string add_tag(const std::string& tags, const char* tag);
273 
275  MAQUINA_EXPORT bool has_tag(const Node& node, const char* tag);
276 
278  {
279  friend TagsIteration tags(const std::string_view& tags_list);
280 
281  TagsIteration(const std::string_view& tags);
282 
283  class MAQUINA_EXPORT Iterator
284  {
285  public:
286  using iterator_category = std::forward_iterator_tag;
287  using value_type = std::string_view;
288  using difference_type = std::ptrdiff_t;
289  using pointer = std::string_view;
290  using reference = std::string_view;
291 
292  reference operator*() const;
293  bool operator==(const Iterator& other) const;
294  bool operator!=(const Iterator &other) const;
295  Iterator& operator++();
296  Iterator(const std::string_view& tags, size_t start);
297  private:
298  void _init(size_t start);
299  const std::string_view _tags;
300  size_t _start, _end;
301  };
302 
303  public:
304  Iterator begin() const;
305  Iterator end() const;
306  private:
307  const std::string_view _tags;
308  };
309 
311  TagsIteration tags(const std::string_view& tags_list);
312 
314  MAQUINA_EXPORT void parallel_for(uint32_t first,
315  uint32_t last,
316  uint32_t batch_size,
317  const std::function<void(uint32_t first, uint32_t last)>& function
318  );
319 
321  MAQUINA_EXPORT Imath::Box3f compute_aabb(const gsl::span<const Imath::V3f>& points);
322 
324  MAQUINA_EXPORT Imath::V3f compute_average(const gsl::span<const Imath::V3f>& points, const gsl::span<const uint32_t>& indices);
325 
327 
330  MAQUINA_EXPORT Value setting(const char* path);
331 
333 
336  MAQUINA_EXPORT Plug setting_plug(const std::string_view& path);
337 
339 
342  MAQUINA_EXPORT void set_setting(const char* path, const Value& value);
343 
356  MAQUINA_EXPORT Plug add_setting(const std::string_view& path, const Value& value, std::string description="");
357 
359 
361  MAQUINA_EXPORT Scene remap_attribute(const SceneConst& new_scene, const SceneConst& old_scene, const SceneConst& old_attribute,
362  const std::string& attribute_name, const Shape::Topology& attribute_topology, bool interpolate_vertex);
363 
364 
366  MAQUINA_EXPORT std::vector<std::string> split(const StringView& s, const StringView& sep);
367 
369  MAQUINA_EXPORT std::string join(const std::vector<std::string>& strings, const StringView& sep);
370 
372  {
374  std::string path;
376  };
377 
378  // TODO use otional here
380 
382  MAQUINA_EXPORT std::shared_ptr<LazySceneChildResult> lazy_scene_child(const SceneConst& scene, const std::string& path, bool shapes_only=false);
383 
385  MAQUINA_EXPORT std::pair<std::vector<Node>, std::vector<Plug>> _document_paths(const Node& node);
386 
387  }
MAQUINA_EXPORT void drm_init(const std::string &software_name, const std::wstring &license_file_path, const std::string &license_server)
Initialize the drm.
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.
Topology
The different attribute topologies.
Definition: Shape.h:38
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:41
MAQUINA_EXPORT bool same_file(std::string a, std::string b)
Return true if the two document paths are pointing the same filesystem file.
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.
MAQUINA_EXPORT std::shared_ptr< LazySceneChildResult > lazy_scene_child(const SceneConst &scene, const std::string &path, bool shapes_only=false)
Return the child scene node matching the path or if not found the only one matching the final path na...
Definition: ImathFrame.h:42
TagsIteration tags(const std::string_view &tags_list)
Return a tag iteration from a string with comma, semi-column or space separated tokens.
MAQUINA_EXPORT Plug setting_plug(const std::string_view &path)
Get a setting plug.
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)
Imath::M44d world_matrix
The path of the child.
Definition: Utils.h:375
MAQUINA_EXPORT std::wstring project_directory()
Return the project directory.
A mutable scene node value.
Definition: Scene.h:292
std::string path
The found scene child.
Definition: Utils.h:374
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.
Iterator begin() const
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:30
MAQUINA_EXPORT bool file_exists(const wchar_t *file)
Return true if a file exist.
MAQUINA_EXPORT Imath::M44f lerp(const Imath::M44f &a, const Imath::M44f &b, float w)
Interplate two matrices.
Definition: Utils.h:277
MAQUINA_EXPORT std::pair< bool, std::string > drm_status()
Return the drm status.
Iterator end() const
MAQUINA_EXPORT std::string comparable_path(std::string s)
Normalize and return a path in order to be comparaded to another comparable path. ...
MAQUINA_EXPORT Imath::Box3f compute_aabb(const gsl::span< const Imath::V3f > &points)
Compute a 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.
Definition: Utils.h:371
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.
MAQUINA_EXPORT std::string add_tag(const std::string &tags, const char *tag)
Add a tag to a tag string.
MAQUINA_EXPORT std::string join(const std::vector< std::string > &strings, const StringView &sep)
Return the concatenation of the strings separated by the optional sep string.
Definition: ImathQuat.h:71
MAQUINA_EXPORT Node reference(Node &root, const std::wstring &filepath, const std::string &reference_root_name="", const ProgressCallback &progress={})
Reference the content of a file into the project.
#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:52
A node plug.
Definition: Plug.h:59
MAQUINA_EXPORT Scene remap_attribute(const SceneConst &new_scene, const SceneConst &old_scene, const SceneConst &old_attribute, const std::string &attribute_name, const Shape::Topology &attribute_topology, bool interpolate_vertex)
Remaps an attribute from a deprecated Scene version to a new one.
MAQUINA_EXPORT std::wstring project_path(const wchar_t *system_path)
Converts a file system path to a project-relative 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 std::vector< std::string > split(const StringView &s, const StringView &sep)
Return a list of the words in the string, using the sep string as delimiter.
An immutable scene node value.
Definition: Scene.h:35
MAQUINA_EXPORT Imath::M44f blend_to_identity(const Imath::M44f &a, float w)
Blend a matrix to identity.
bool operator!=(const C *str, const StringViewBase< C, STDS > &sv) noexcept
Definition: StringView.h:306
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 bool list_directory_files(const wchar_t *directory, std::vector< std::wstring > &files, bool relative=false)
bool operator==(const C *str, const StringViewBase< C, STDS > &sv) noexcept
Definition: StringView.h:274
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 Value setting(const char *path)
Get a setting value.
MAQUINA_EXPORT Imath::V3f compute_average(const gsl::span< const Imath::V3f > &points, const gsl::span< const uint32_t > &indices)
Compute the average of indexed points.
MAQUINA_EXPORT std::wstring expand_string(const wchar_t *path)
Expand a string containing
Color4< T > operator*(S a, const Color4< T > &v)
Definition: ImathColor.h:727
MAQUINA_EXPORT void drm_release()
Release the drm.
MAQUINA_EXPORT Plug add_setting(const std::string_view &path, const Value &value, std::string description="")
Creates a new setting.
friend TagsIteration tags(const std::string_view &tags_list)
Return a tag iteration from a string with comma, semi-column or space separated tokens.
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
SceneConst scene
Definition: Utils.h:373
Base class of all Rumba nodes.
Definition: Node.h:37
Base class of all Rumba values.
Definition: Value.h:34
MAQUINA_EXPORT bool has_tag(const std::string &tags, const char *tag)
Check a tag exist in a comma separate tag list.