STEPFile.h 31 KB

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