STEPFile.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #ifndef INCLUDED_AI_STEPFILE_H
  34. #define INCLUDED_AI_STEPFILE_H
  35. #include <bitset>
  36. #include <map>
  37. #include <memory>
  38. #include <set>
  39. #include <typeinfo>
  40. #include <vector>
  41. #include "AssetLib/FBX/FBXDocument.h" //ObjectMap::value_type
  42. #include <assimp/DefaultLogger.hpp>
  43. #ifdef _MSC_VER
  44. # pragma warning(push)
  45. # pragma warning(disable : 4127 4456 4245 4512 )
  46. #endif // _MSC_VER
  47. //
  48. #if _MSC_VER > 1500 || (defined __GNUC___)
  49. # define ASSIMP_STEP_USE_UNORDERED_MULTIMAP
  50. #else
  51. # define step_unordered_map map
  52. # define step_unordered_multimap multimap
  53. #endif
  54. #ifdef ASSIMP_STEP_USE_UNORDERED_MULTIMAP
  55. # include <unordered_map>
  56. # if _MSC_VER > 1600
  57. # define step_unordered_map unordered_map
  58. # define step_unordered_multimap unordered_multimap
  59. # else
  60. # define step_unordered_map tr1::unordered_map
  61. # define step_unordered_multimap tr1::unordered_multimap
  62. # endif
  63. #endif
  64. #include <assimp/LineSplitter.h>
  65. // uncomment this to have the loader evaluate all entities upon loading.
  66. // this is intended as stress test - by default, entities are evaluated
  67. // lazily and therefore not unless needed.
  68. //#define ASSIMP_IFC_TEST
  69. namespace Assimp {
  70. // ********************************************************************************
  71. // before things get complicated, this is the basic outline:
  72. namespace STEP {
  73. namespace EXPRESS {
  74. // base data types known by EXPRESS schemata - any custom data types will derive one of those
  75. class DataType;
  76. class UNSET; /*: public DataType */
  77. class ISDERIVED; /*: public DataType */
  78. // class REAL; /*: public DataType */
  79. class ENUM; /*: public DataType */
  80. // class STRING; /*: public DataType */
  81. // class INTEGER; /*: public DataType */
  82. class ENTITY; /*: public DataType */
  83. class LIST; /*: public DataType */
  84. // class SELECT; /*: public DataType */
  85. // a conversion schema is not exactly an EXPRESS schema, rather it
  86. // is a list of pointers to conversion functions to build up the
  87. // object tree from an input file.
  88. class ConversionSchema;
  89. } // namespace EXPRESS
  90. struct HeaderInfo;
  91. class Object;
  92. class LazyObject;
  93. class DB;
  94. typedef Object *(*ConvertObjectProc)(const DB &db, const EXPRESS::LIST &params);
  95. } // namespace STEP
  96. // ********************************************************************************
  97. namespace STEP {
  98. // -------------------------------------------------------------------------------
  99. /** Exception class used by the STEP loading & parsing code. It is typically
  100. * coupled with a line number. */
  101. // -------------------------------------------------------------------------------
  102. struct SyntaxError : DeadlyImportError {
  103. enum : uint64_t {
  104. LINE_NOT_SPECIFIED = 0xfffffffffffffffLL
  105. };
  106. SyntaxError(const std::string &s, uint64_t line = LINE_NOT_SPECIFIED);
  107. };
  108. // -------------------------------------------------------------------------------
  109. /** Exception class used by the STEP loading & parsing code when a type
  110. * error (i.e. an entity expects a string but receives a bool) occurs.
  111. * It is typically coupled with both an entity id and a line number.*/
  112. // -------------------------------------------------------------------------------
  113. struct TypeError : DeadlyImportError {
  114. enum : uint64_t {
  115. ENTITY_NOT_SPECIFIED = 0xffffffffffffffffUL,
  116. ENTITY_NOT_SPECIFIED_32 = 0x00000000ffffffff
  117. };
  118. TypeError(const std::string &s, uint64_t entity = ENTITY_NOT_SPECIFIED, uint64_t line = SyntaxError::LINE_NOT_SPECIFIED);
  119. };
  120. // hack to make a given member template-dependent
  121. template <typename T, typename T2>
  122. T2 &Couple(T2 &in) {
  123. return in;
  124. }
  125. namespace EXPRESS {
  126. // -------------------------------------------------------------------------------
  127. //** Base class for all STEP data types */
  128. // -------------------------------------------------------------------------------
  129. class DataType {
  130. public:
  131. typedef std::shared_ptr<const DataType> Out;
  132. public:
  133. virtual ~DataType() {
  134. }
  135. public:
  136. template <typename T>
  137. const T &To() const {
  138. return dynamic_cast<const T &>(*this);
  139. }
  140. template <typename T>
  141. T &To() {
  142. return dynamic_cast<T &>(*this);
  143. }
  144. template <typename T>
  145. const T *ToPtr() const {
  146. return dynamic_cast<const T *>(this);
  147. }
  148. template <typename T>
  149. T *ToPtr() {
  150. return dynamic_cast<T *>(this);
  151. }
  152. // utilities to deal with SELECT entities, which currently lack automatic
  153. // conversion support.
  154. template <typename T>
  155. const T &ResolveSelect(const DB &db) const {
  156. return Couple<T>(db).MustGetObject(To<EXPRESS::ENTITY>())->template To<T>();
  157. }
  158. template <typename T>
  159. const T *ResolveSelectPtr(const DB &db) const {
  160. const EXPRESS::ENTITY *e = ToPtr<EXPRESS::ENTITY>();
  161. return e ? Couple<T>(db).MustGetObject(*e)->template ToPtr<T>() : (const T *)0;
  162. }
  163. public:
  164. /** parse a variable from a string and set 'inout' to the character
  165. * behind the last consumed character. An optional schema enables,
  166. * if specified, automatic conversion of custom data types.
  167. *
  168. * @throw SyntaxError
  169. */
  170. static std::shared_ptr<const EXPRESS::DataType> Parse(const char *&inout,
  171. uint64_t line = SyntaxError::LINE_NOT_SPECIFIED,
  172. const EXPRESS::ConversionSchema *schema = NULL);
  173. public:
  174. };
  175. typedef DataType SELECT;
  176. typedef DataType LOGICAL;
  177. // -------------------------------------------------------------------------------
  178. /** Sentinel class to represent explicitly unset (optional) fields ($) */
  179. // -------------------------------------------------------------------------------
  180. class UNSET : public DataType {
  181. public:
  182. private:
  183. };
  184. // -------------------------------------------------------------------------------
  185. /** Sentinel class to represent explicitly derived fields (*) */
  186. // -------------------------------------------------------------------------------
  187. class ISDERIVED : public DataType {
  188. public:
  189. private:
  190. };
  191. // -------------------------------------------------------------------------------
  192. /** Shared implementation for some of the primitive data type, i.e. int, float */
  193. // -------------------------------------------------------------------------------
  194. template <typename T>
  195. class PrimitiveDataType : public DataType {
  196. public:
  197. // This is the type that will cd ultimatively be used to
  198. // expose this data type to the user.
  199. typedef T Out;
  200. PrimitiveDataType() {}
  201. PrimitiveDataType(const T &val) :
  202. val(val) {}
  203. PrimitiveDataType(const PrimitiveDataType &o) {
  204. (*this) = o;
  205. }
  206. operator const T &() const {
  207. return val;
  208. }
  209. PrimitiveDataType &operator=(const PrimitiveDataType &o) {
  210. val = o.val;
  211. return *this;
  212. }
  213. protected:
  214. T val;
  215. };
  216. typedef PrimitiveDataType<int64_t> INTEGER;
  217. typedef PrimitiveDataType<double> REAL;
  218. typedef PrimitiveDataType<double> NUMBER;
  219. typedef PrimitiveDataType<std::string> STRING;
  220. // -------------------------------------------------------------------------------
  221. /** Generic base class for all enumerated types */
  222. // -------------------------------------------------------------------------------
  223. class ENUMERATION : public STRING {
  224. public:
  225. ENUMERATION(const std::string &val) :
  226. STRING(val) {}
  227. private:
  228. };
  229. typedef ENUMERATION BOOLEAN;
  230. // -------------------------------------------------------------------------------
  231. /** This is just a reference to an entity/object somewhere else */
  232. // -------------------------------------------------------------------------------
  233. class ENTITY : public PrimitiveDataType<uint64_t> {
  234. public:
  235. ENTITY(uint64_t val) :
  236. PrimitiveDataType<uint64_t>(val) {
  237. ai_assert(val != 0);
  238. }
  239. ENTITY() :
  240. PrimitiveDataType<uint64_t>(TypeError::ENTITY_NOT_SPECIFIED) {
  241. // empty
  242. }
  243. private:
  244. };
  245. // -------------------------------------------------------------------------------
  246. /** Wrap any STEP aggregate: LIST, SET, ... */
  247. // -------------------------------------------------------------------------------
  248. class LIST : public DataType {
  249. public:
  250. // access a particular list index, throw std::range_error for wrong indices
  251. std::shared_ptr<const DataType> operator[](size_t index) const {
  252. return members[index];
  253. }
  254. size_t GetSize() const {
  255. return members.size();
  256. }
  257. public:
  258. /** @see DaraType::Parse */
  259. static std::shared_ptr<const EXPRESS::LIST> Parse(const char *&inout,
  260. uint64_t line = SyntaxError::LINE_NOT_SPECIFIED,
  261. const EXPRESS::ConversionSchema *schema = NULL);
  262. private:
  263. typedef std::vector<std::shared_ptr<const DataType>> MemberList;
  264. MemberList members;
  265. };
  266. class BINARY : public PrimitiveDataType<uint32_t> {
  267. public:
  268. BINARY(uint32_t val) :
  269. PrimitiveDataType<uint32_t>(val) {
  270. // empty
  271. }
  272. BINARY() :
  273. PrimitiveDataType<uint32_t>(TypeError::ENTITY_NOT_SPECIFIED_32) {
  274. // empty
  275. }
  276. };
  277. // -------------------------------------------------------------------------------
  278. /* Not exactly a full EXPRESS schema but rather a list of conversion functions
  279. * to extract valid C++ objects out of a STEP file. Those conversion functions
  280. * may, however, perform further schema validations. */
  281. // -------------------------------------------------------------------------------
  282. class ConversionSchema {
  283. public:
  284. struct SchemaEntry {
  285. SchemaEntry(const char *name, ConvertObjectProc func) :
  286. mName(name), mFunc(func) {
  287. // empty
  288. }
  289. const char *mName;
  290. ConvertObjectProc mFunc;
  291. };
  292. typedef std::map<std::string, ConvertObjectProc> ConverterMap;
  293. template <size_t N>
  294. explicit ConversionSchema(const SchemaEntry (&schemas)[N]) {
  295. *this = schemas;
  296. }
  297. ConversionSchema() {
  298. }
  299. ConvertObjectProc GetConverterProc(const std::string &name) const {
  300. ConverterMap::const_iterator it = converters.find(name);
  301. return it == converters.end() ? nullptr : (*it).second;
  302. }
  303. bool IsKnownToken(const std::string &name) const {
  304. return converters.find(name) != converters.end();
  305. }
  306. const char *GetStaticStringForToken(const std::string &token) const {
  307. ConverterMap::const_iterator it = converters.find(token);
  308. return it == converters.end() ? nullptr : (*it).first.c_str();
  309. }
  310. template <size_t N>
  311. const ConversionSchema &operator=(const SchemaEntry (&schemas)[N]) {
  312. for (size_t i = 0; i < N; ++i) {
  313. const SchemaEntry &schema = schemas[i];
  314. converters[schema.mName] = schema.mFunc;
  315. }
  316. return *this;
  317. }
  318. private:
  319. ConverterMap converters;
  320. };
  321. } // namespace EXPRESS
  322. // ------------------------------------------------------------------------------
  323. /** Bundle all the relevant info from a STEP header, parts of which may later
  324. * be plainly dumped to the logfile, whereas others may help the caller pick an
  325. * appropriate loading strategy.*/
  326. // ------------------------------------------------------------------------------
  327. struct HeaderInfo {
  328. std::string timestamp;
  329. std::string app;
  330. std::string fileSchema;
  331. };
  332. // ------------------------------------------------------------------------------
  333. /** Base class for all concrete object instances */
  334. // ------------------------------------------------------------------------------
  335. class Object {
  336. public:
  337. Object(const char *classname = "unknown") :
  338. id(0), classname(classname) {
  339. // empty
  340. }
  341. virtual ~Object() {
  342. // empty
  343. }
  344. // utilities to simplify casting to concrete types
  345. template <typename T>
  346. const T &To() const {
  347. return dynamic_cast<const T &>(*this);
  348. }
  349. template <typename T>
  350. T &To() {
  351. return dynamic_cast<T &>(*this);
  352. }
  353. template <typename T>
  354. const T *ToPtr() const {
  355. return dynamic_cast<const T *>(this);
  356. }
  357. template <typename T>
  358. T *ToPtr() {
  359. return dynamic_cast<T *>(this);
  360. }
  361. uint64_t GetID() const {
  362. return id;
  363. }
  364. std::string GetClassName() const {
  365. return classname;
  366. }
  367. void SetID(uint64_t newval) {
  368. id = newval;
  369. }
  370. private:
  371. uint64_t id;
  372. const char *const classname;
  373. };
  374. template <typename T>
  375. size_t GenericFill(const STEP::DB &db, const EXPRESS::LIST &params, T *in);
  376. // (intentionally undefined)
  377. // ------------------------------------------------------------------------------
  378. /** CRTP shared base class for use by concrete entity implementation classes */
  379. // ------------------------------------------------------------------------------
  380. template <typename TDerived, size_t arg_count>
  381. struct ObjectHelper : virtual Object {
  382. ObjectHelper() :
  383. aux_is_derived(0) {
  384. // empty
  385. }
  386. static Object *Construct(const STEP::DB &db, const EXPRESS::LIST &params) {
  387. // make sure we don't leak if Fill() throws an exception
  388. std::unique_ptr<TDerived> impl(new TDerived());
  389. // GenericFill<T> is undefined so we need to have a specialization
  390. const size_t num_args = GenericFill<TDerived>(db, params, &*impl);
  391. (void)num_args;
  392. // the following check is commented because it will always trigger if
  393. // parts of the entities are generated with dummy wrapper code.
  394. // This is currently done to reduce the size of the loader
  395. // code.
  396. //if (num_args != params.GetSize() && impl->GetClassName() != "NotImplemented") {
  397. // DefaultLogger::get()->debug("STEP: not all parameters consumed");
  398. //}
  399. return impl.release();
  400. }
  401. // note that this member always exists multiple times within the hierarchy
  402. // of an individual object, so any access to it must be disambiguated.
  403. std::bitset<arg_count> aux_is_derived;
  404. };
  405. // ------------------------------------------------------------------------------
  406. /** Class template used to represent OPTIONAL data members in the converted schema */
  407. // ------------------------------------------------------------------------------
  408. template <typename T>
  409. struct Maybe {
  410. Maybe() :
  411. have() {
  412. // empty
  413. }
  414. explicit Maybe(const T &ptr) :
  415. ptr(ptr), have(true) {
  416. // empty
  417. }
  418. void flag_invalid() {
  419. have = false;
  420. }
  421. void flag_valid() {
  422. have = true;
  423. }
  424. bool operator!() const {
  425. return !have;
  426. }
  427. operator bool() const {
  428. return have;
  429. }
  430. operator const T &() const {
  431. return Get();
  432. }
  433. const T &Get() const {
  434. ai_assert(have);
  435. return ptr;
  436. }
  437. Maybe &operator=(const T &_ptr) {
  438. ptr = _ptr;
  439. have = true;
  440. return *this;
  441. }
  442. private:
  443. template <typename T2>
  444. friend struct InternGenericConvert;
  445. operator T &() {
  446. return ptr;
  447. }
  448. T ptr;
  449. bool have;
  450. };
  451. // ------------------------------------------------------------------------------
  452. /** A LazyObject is created when needed. Before this happens, we just keep
  453. the text line that contains the object definition. */
  454. // -------------------------------------------------------------------------------
  455. class LazyObject {
  456. friend class DB;
  457. public:
  458. LazyObject(DB &db, uint64_t id, uint64_t line, const char *type, const char *args);
  459. ~LazyObject();
  460. Object &operator*() {
  461. if (!obj) {
  462. LazyInit();
  463. ai_assert(obj);
  464. }
  465. return *obj;
  466. }
  467. const Object &operator*() const {
  468. if (!obj) {
  469. LazyInit();
  470. ai_assert(obj);
  471. }
  472. return *obj;
  473. }
  474. template <typename T>
  475. const T &To() const {
  476. return dynamic_cast<const T &>(**this);
  477. }
  478. template <typename T>
  479. T &To() {
  480. return dynamic_cast<T &>(**this);
  481. }
  482. template <typename T>
  483. const T *ToPtr() const {
  484. return dynamic_cast<const T *>(&**this);
  485. }
  486. template <typename T>
  487. T *ToPtr() {
  488. return dynamic_cast<T *>(&**this);
  489. }
  490. Object *operator->() {
  491. return &**this;
  492. }
  493. const Object *operator->() const {
  494. return &**this;
  495. }
  496. bool operator==(const std::string &atype) const {
  497. return type == atype;
  498. }
  499. bool operator!=(const std::string &atype) const {
  500. return type != atype;
  501. }
  502. uint64_t GetID() const {
  503. return id;
  504. }
  505. private:
  506. void LazyInit() const;
  507. private:
  508. mutable uint64_t id;
  509. const char *const type;
  510. DB &db;
  511. mutable const char *args;
  512. mutable Object *obj;
  513. };
  514. template <typename T>
  515. inline bool operator==(std::shared_ptr<LazyObject> lo, T whatever) {
  516. return *lo == whatever; // XXX use std::forward if we have 0x
  517. }
  518. template <typename T>
  519. inline bool operator==(const std::pair<uint64_t, std::shared_ptr<LazyObject>> &lo, T whatever) {
  520. return *(lo.second) == whatever; // XXX use std::forward if we have 0x
  521. }
  522. // ------------------------------------------------------------------------------
  523. /** Class template used to represent lazily evaluated object references in the converted schema */
  524. // ------------------------------------------------------------------------------
  525. template <typename T>
  526. struct Lazy {
  527. typedef Lazy Out;
  528. Lazy(const LazyObject *obj = nullptr) :
  529. obj(obj) {
  530. // empty
  531. }
  532. operator const T *() const {
  533. return obj->ToPtr<T>();
  534. }
  535. operator const T &() const {
  536. return obj->To<T>();
  537. }
  538. const T &operator*() const {
  539. return obj->To<T>();
  540. }
  541. const T *operator->() const {
  542. return &obj->To<T>();
  543. }
  544. const LazyObject *obj;
  545. };
  546. // ------------------------------------------------------------------------------
  547. /** Class template used to represent LIST and SET data members in the converted schema */
  548. // ------------------------------------------------------------------------------
  549. template <typename T, uint64_t min_cnt, uint64_t max_cnt = 0uL>
  550. struct ListOf : public std::vector<typename T::Out> {
  551. typedef typename T::Out OutScalar;
  552. typedef ListOf Out;
  553. ListOf() {
  554. static_assert(min_cnt <= max_cnt || !max_cnt, "min_cnt <= max_cnt || !max_cnt");
  555. }
  556. };
  557. // ------------------------------------------------------------------------------
  558. template <typename TOut>
  559. struct PickBaseType {
  560. typedef EXPRESS::PrimitiveDataType<TOut> Type;
  561. };
  562. template <typename TOut>
  563. struct PickBaseType<Lazy<TOut>> {
  564. typedef EXPRESS::ENTITY Type;
  565. };
  566. template <>
  567. struct PickBaseType<std::shared_ptr<const EXPRESS::DataType>>;
  568. // ------------------------------------------------------------------------------
  569. template <typename T>
  570. struct InternGenericConvert {
  571. void operator()(T &out, const std::shared_ptr<const EXPRESS::DataType> &in, const STEP::DB & /*db*/) {
  572. try {
  573. out = dynamic_cast<const typename PickBaseType<T>::Type &>(*in);
  574. } catch (std::bad_cast &) {
  575. throw TypeError("type error reading literal field");
  576. }
  577. }
  578. };
  579. template <>
  580. struct InternGenericConvert<std::shared_ptr<const EXPRESS::DataType>> {
  581. void operator()(std::shared_ptr<const EXPRESS::DataType> &out, const std::shared_ptr<const EXPRESS::DataType> &in, const STEP::DB & /*db*/) {
  582. out = in;
  583. }
  584. };
  585. template <typename T>
  586. struct InternGenericConvert<Maybe<T>> {
  587. void operator()(Maybe<T> &out, const std::shared_ptr<const EXPRESS::DataType> &in, const STEP::DB &db) {
  588. GenericConvert((T &)out, in, db);
  589. out.flag_valid();
  590. }
  591. };
  592. #if _MSC_VER > 1920
  593. #pragma warning(push)
  594. #pragma warning(disable : 4127)
  595. #endif // _WIN32
  596. template <typename T, uint64_t min_cnt, uint64_t max_cnt>
  597. struct InternGenericConvertList {
  598. void operator()(ListOf<T, min_cnt, max_cnt> &out, const std::shared_ptr<const EXPRESS::DataType> &inp_base, const STEP::DB &db) {
  599. const EXPRESS::LIST *inp = dynamic_cast<const EXPRESS::LIST *>(inp_base.get());
  600. if (!inp) {
  601. throw TypeError("type error reading aggregate");
  602. }
  603. // XXX is this really how the EXPRESS notation ([?:3],[1:3]) is intended?
  604. const size_t len = inp->GetSize();
  605. if (0 != max_cnt && len > max_cnt) {
  606. ASSIMP_LOG_WARN("too many aggregate elements");
  607. } else if (len < min_cnt) {
  608. ASSIMP_LOG_WARN("too few aggregate elements");
  609. }
  610. out.reserve(inp->GetSize());
  611. for (size_t i = 0; i < inp->GetSize(); ++i) {
  612. out.push_back(typename ListOf<T, min_cnt, max_cnt>::OutScalar());
  613. try {
  614. GenericConvert(out.back(), (*inp)[i], db);
  615. } catch (const TypeError &t) {
  616. throw TypeError(t.what() + std::string(" of aggregate"));
  617. }
  618. }
  619. }
  620. };
  621. template <typename T>
  622. struct InternGenericConvert<Lazy<T>> {
  623. void operator()(Lazy<T> &out, const std::shared_ptr<const EXPRESS::DataType> &in_base, const STEP::DB &db) {
  624. const EXPRESS::ENTITY *in = dynamic_cast<const EXPRESS::ENTITY *>(in_base.get());
  625. if (!in) {
  626. throw TypeError("type error reading entity");
  627. }
  628. out = Couple<T>(db).GetObject(*in);
  629. }
  630. };
  631. template <typename T1>
  632. inline void GenericConvert(T1 &a, const std::shared_ptr<const EXPRESS::DataType> &b, const STEP::DB &db) {
  633. return InternGenericConvert<T1>()(a, b, db);
  634. }
  635. template <typename T1, uint64_t N1, uint64_t N2>
  636. inline void GenericConvert(ListOf<T1, N1, N2> &a, const std::shared_ptr<const EXPRESS::DataType> &b, const STEP::DB &db) {
  637. return InternGenericConvertList<T1, N1, N2>()(a, b, db);
  638. }
  639. // ------------------------------------------------------------------------------
  640. /** Lightweight manager class that holds the map of all objects in a
  641. * STEP file. DB's are exclusively maintained by the functions in
  642. * STEPFileReader.h*/
  643. // -------------------------------------------------------------------------------
  644. class DB {
  645. friend DB *ReadFileHeader(std::shared_ptr<IOStream> stream);
  646. friend void ReadFile(DB &db, const EXPRESS::ConversionSchema &scheme,
  647. const char *const *types_to_track, size_t len,
  648. const char *const *inverse_indices_to_track, size_t len2);
  649. friend class LazyObject;
  650. public:
  651. // objects indexed by ID - this can grow pretty large (i.e some hundred million
  652. // entries), so use raw pointers to avoid *any* overhead.
  653. typedef std::map<uint64_t, const LazyObject *> ObjectMap;
  654. // objects indexed by their declarative type, but only for those that we truly want
  655. typedef std::set<const LazyObject *> ObjectSet;
  656. typedef std::map<std::string, ObjectSet> ObjectMapByType;
  657. // list of types for which to keep inverse indices for all references
  658. // that the respective objects keep.
  659. // the list keeps pointers to strings in static storage
  660. typedef std::set<const char *> InverseWhitelist;
  661. // references - for each object id the ids of all objects which reference it
  662. // this is used to simulate STEP inverse indices for selected types.
  663. typedef std::step_unordered_multimap<uint64_t, uint64_t> RefMap;
  664. typedef std::pair<RefMap::const_iterator, RefMap::const_iterator> RefMapRange;
  665. private:
  666. DB(std::shared_ptr<StreamReaderLE> reader) :
  667. reader(reader), splitter(*reader, true, true), evaluated_count(), schema(nullptr) {}
  668. public:
  669. ~DB() {
  670. for (ObjectMap::value_type &o : objects) {
  671. delete o.second;
  672. }
  673. }
  674. uint64_t GetObjectCount() const {
  675. return objects.size();
  676. }
  677. uint64_t GetEvaluatedObjectCount() const {
  678. return evaluated_count;
  679. }
  680. const HeaderInfo &GetHeader() const {
  681. return header;
  682. }
  683. const EXPRESS::ConversionSchema &GetSchema() const {
  684. return *schema;
  685. }
  686. const ObjectMap &GetObjects() const {
  687. return objects;
  688. }
  689. const ObjectMapByType &GetObjectsByType() const {
  690. return objects_bytype;
  691. }
  692. const RefMap &GetRefs() const {
  693. return refs;
  694. }
  695. bool KeepInverseIndicesForType(const char *const type) const {
  696. return inv_whitelist.find(type) != inv_whitelist.end();
  697. }
  698. // get the yet unevaluated object record with a given id
  699. const LazyObject *GetObject(uint64_t id) const {
  700. const ObjectMap::const_iterator it = objects.find(id);
  701. if (it != objects.end()) {
  702. return (*it).second;
  703. }
  704. return nullptr;
  705. }
  706. // get an arbitrary object out of the soup with the only restriction being its type.
  707. const LazyObject *GetObject(const std::string &type) const {
  708. const ObjectMapByType::const_iterator it = objects_bytype.find(type);
  709. if (it != objects_bytype.end() && (*it).second.size()) {
  710. return *(*it).second.begin();
  711. }
  712. return NULL;
  713. }
  714. // same, but raise an exception if the object doesn't exist and return a reference
  715. const LazyObject &MustGetObject(uint64_t id) const {
  716. const LazyObject *o = GetObject(id);
  717. if (!o) {
  718. throw TypeError("requested entity is not present", id);
  719. }
  720. return *o;
  721. }
  722. const LazyObject &MustGetObject(const std::string &type) const {
  723. const LazyObject *o = GetObject(type);
  724. if (!o) {
  725. throw TypeError("requested entity of type " + type + "is not present");
  726. }
  727. return *o;
  728. }
  729. #ifdef ASSIMP_IFC_TEST
  730. // evaluate *all* entities in the file. this is a power test for the loader
  731. void EvaluateAll() {
  732. for (ObjectMap::value_type &e : objects) {
  733. **e.second;
  734. }
  735. ai_assert(evaluated_count == objects.size());
  736. }
  737. #endif
  738. private:
  739. // full access only offered to close friends - they should
  740. // use the provided getters rather than messing around with
  741. // the members directly.
  742. LineSplitter &GetSplitter() {
  743. return splitter;
  744. }
  745. void InternInsert(const LazyObject *lz) {
  746. objects[lz->GetID()] = lz;
  747. const ObjectMapByType::iterator it = objects_bytype.find(lz->type);
  748. if (it != objects_bytype.end()) {
  749. (*it).second.insert(lz);
  750. }
  751. }
  752. void SetSchema(const EXPRESS::ConversionSchema &_schema) {
  753. schema = &_schema;
  754. }
  755. void SetTypesToTrack(const char *const *types, size_t N) {
  756. for (size_t i = 0; i < N; ++i) {
  757. objects_bytype[types[i]] = ObjectSet();
  758. }
  759. }
  760. void SetInverseIndicesToTrack(const char *const *types, size_t N) {
  761. for (size_t i = 0; i < N; ++i) {
  762. const char *const sz = schema->GetStaticStringForToken(types[i]);
  763. ai_assert(sz);
  764. inv_whitelist.insert(sz);
  765. }
  766. }
  767. HeaderInfo &GetHeader() {
  768. return header;
  769. }
  770. void MarkRef(uint64_t who, uint64_t by_whom) {
  771. refs.insert(std::make_pair(who, by_whom));
  772. }
  773. private:
  774. HeaderInfo header;
  775. ObjectMap objects;
  776. ObjectMapByType objects_bytype;
  777. RefMap refs;
  778. InverseWhitelist inv_whitelist;
  779. std::shared_ptr<StreamReaderLE> reader;
  780. LineSplitter splitter;
  781. uint64_t evaluated_count;
  782. const EXPRESS::ConversionSchema *schema;
  783. };
  784. #ifdef _MSC_VER
  785. #pragma warning(pop)
  786. #endif // _MSC_VER
  787. } // namespace STEP
  788. } // namespace Assimp
  789. #endif // INCLUDED_AI_STEPFILE_H