abc_util.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * Contributor(s): Esteban Tovagliari, Cedric Paille, Kevin Dietrich
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. #ifndef __ABC_UTIL_H__
  23. #define __ABC_UTIL_H__
  24. #include <Alembic/Abc/All.h>
  25. #include <Alembic/AbcGeom/All.h>
  26. #ifdef _MSC_VER
  27. # define ABC_INLINE static __forceinline
  28. #else
  29. # define ABC_INLINE static inline
  30. #endif
  31. /**
  32. * \brief The CacheReader struct is only used for anonymous pointers,
  33. * to interface between C and C++ code. This library only creates
  34. * pointers to AbcObjectReader (or subclasses thereof).
  35. */
  36. struct CacheReader {
  37. int unused;
  38. };
  39. using Alembic::Abc::chrono_t;
  40. class AbcObjectReader;
  41. struct ImportSettings;
  42. struct ID;
  43. struct Object;
  44. std::string get_id_name(const ID * const id);
  45. std::string get_id_name(const Object * const ob);
  46. std::string get_object_dag_path_name(const Object * const ob, Object *dupli_parent);
  47. bool object_selected(Object *ob);
  48. bool parent_selected(Object *ob);
  49. Imath::M44d convert_matrix(float mat[4][4]);
  50. typedef enum {
  51. ABC_MATRIX_WORLD = 1,
  52. ABC_MATRIX_LOCAL = 2,
  53. } AbcMatrixMode;
  54. void create_transform_matrix(Object *obj, float r_transform_mat[4][4],
  55. AbcMatrixMode mode, Object *proxy_from);
  56. void split(const std::string &s, const char delim, std::vector<std::string> &tokens);
  57. template<class TContainer>
  58. bool begins_with(const TContainer &input, const TContainer &match)
  59. {
  60. return input.size() >= match.size()
  61. && std::equal(match.begin(), match.end(), input.begin());
  62. }
  63. void convert_matrix(const Imath::M44d &xform, Object *ob, float r_mat[4][4]);
  64. template <typename Schema>
  65. void get_min_max_time_ex(const Schema &schema, chrono_t &min, chrono_t &max)
  66. {
  67. const Alembic::Abc::TimeSamplingPtr &time_samp = schema.getTimeSampling();
  68. if (!schema.isConstant()) {
  69. const size_t num_samps = schema.getNumSamples();
  70. if (num_samps > 0) {
  71. const chrono_t min_time = time_samp->getSampleTime(0);
  72. min = std::min(min, min_time);
  73. const chrono_t max_time = time_samp->getSampleTime(num_samps - 1);
  74. max = std::max(max, max_time);
  75. }
  76. }
  77. }
  78. template <typename Schema>
  79. void get_min_max_time(const Alembic::AbcGeom::IObject &object, const Schema &schema, chrono_t &min, chrono_t &max)
  80. {
  81. get_min_max_time_ex(schema, min, max);
  82. const Alembic::AbcGeom::IObject &parent = object.getParent();
  83. if (parent.valid() && Alembic::AbcGeom::IXform::matches(parent.getMetaData())) {
  84. Alembic::AbcGeom::IXform xform(parent, Alembic::AbcGeom::kWrapExisting);
  85. get_min_max_time_ex(xform.getSchema(), min, max);
  86. }
  87. }
  88. bool has_property(const Alembic::Abc::ICompoundProperty &prop, const std::string &name);
  89. float get_weight_and_index(float time,
  90. const Alembic::AbcCoreAbstract::TimeSamplingPtr &time_sampling,
  91. int samples_number,
  92. Alembic::AbcGeom::index_t &i0,
  93. Alembic::AbcGeom::index_t &i1);
  94. AbcObjectReader *create_reader(const Alembic::AbcGeom::IObject &object, ImportSettings &settings);
  95. /* ************************** */
  96. /* TODO(kevin): for now keeping these transformations hardcoded to make sure
  97. * everything works properly, and also because Alembic is almost exclusively
  98. * used in Y-up software, but eventually they'll be set by the user in the UI
  99. * like other importers/exporters do, to support other axis. */
  100. /* Copy from Y-up to Z-up. */
  101. ABC_INLINE void copy_zup_from_yup(float zup[3], const float yup[3])
  102. {
  103. const float old_yup1 = yup[1]; /* in case zup == yup */
  104. zup[0] = yup[0];
  105. zup[1] = -yup[2];
  106. zup[2] = old_yup1;
  107. }
  108. ABC_INLINE void copy_zup_from_yup(short zup[3], const short yup[3])
  109. {
  110. const short old_yup1 = yup[1]; /* in case zup == yup */
  111. zup[0] = yup[0];
  112. zup[1] = -yup[2];
  113. zup[2] = old_yup1;
  114. }
  115. /* Copy from Z-up to Y-up. */
  116. ABC_INLINE void copy_yup_from_zup(float yup[3], const float zup[3])
  117. {
  118. const float old_zup1 = zup[1]; /* in case yup == zup */
  119. yup[0] = zup[0];
  120. yup[1] = zup[2];
  121. yup[2] = -old_zup1;
  122. }
  123. ABC_INLINE void copy_yup_from_zup(short yup[3], const short zup[3])
  124. {
  125. const short old_zup1 = zup[1]; /* in case yup == zup */
  126. yup[0] = zup[0];
  127. yup[1] = zup[2];
  128. yup[2] = -old_zup1;
  129. }
  130. /* Names are given in (dst, src) order, just like
  131. * the parameters of copy_m44_axis_swap() */
  132. typedef enum {
  133. ABC_ZUP_FROM_YUP = 1,
  134. ABC_YUP_FROM_ZUP = 2,
  135. } AbcAxisSwapMode;
  136. /* Create a rotation matrix for each axis from euler angles.
  137. * Euler angles are swaped to change coordinate system. */
  138. void create_swapped_rotation_matrix(
  139. float rot_x_mat[3][3], float rot_y_mat[3][3],
  140. float rot_z_mat[3][3], const float euler[3],
  141. AbcAxisSwapMode mode);
  142. void copy_m44_axis_swap(float dst_mat[4][4], float src_mat[4][4], AbcAxisSwapMode mode);
  143. /* *************************** */
  144. #undef ABC_DEBUG_TIME
  145. class ScopeTimer {
  146. const char *m_message;
  147. double m_start;
  148. public:
  149. ScopeTimer(const char *message);
  150. ~ScopeTimer();
  151. };
  152. #ifdef ABC_DEBUG_TIME
  153. # define SCOPE_TIMER(message) ScopeTimer prof(message)
  154. #else
  155. # define SCOPE_TIMER(message)
  156. #endif
  157. /* *************************** */
  158. /**
  159. * Utility class whose purpose is to more easily log related informations. An
  160. * instance of the SimpleLogger can be created in any context, and will hold a
  161. * copy of all the strings passed to its output stream.
  162. *
  163. * Different instances of the class may be accessed from different threads,
  164. * although accessing the same instance from different threads will lead to race
  165. * conditions.
  166. */
  167. class SimpleLogger {
  168. std::ostringstream m_stream;
  169. public:
  170. /**
  171. * Check whether or not the SimpleLogger's stream is empty.
  172. */
  173. bool empty();
  174. /**
  175. * Return a copy of the string contained in the SimpleLogger's stream.
  176. */
  177. std::string str() const;
  178. /**
  179. * Remove the bits set on the SimpleLogger's stream and clear its string.
  180. */
  181. void clear();
  182. /**
  183. * Return a reference to the SimpleLogger's stream, in order to e.g. push
  184. * content into it.
  185. */
  186. std::ostringstream &stream();
  187. };
  188. #define ABC_LOG(logger) logger.stream()
  189. /**
  190. * Pass the content of the logger's stream to the specified std::ostream.
  191. */
  192. std::ostream &operator<<(std::ostream &os, const SimpleLogger &logger);
  193. #endif /* __ABC_UTIL_H__ */