IFCCurve.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2010, ASSIMP Development 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 Development 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. /** @file IFCProfile.cpp
  34. * @brief Read profile and curves entities from IFC files
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
  38. #include "IFCUtil.h"
  39. namespace Assimp {
  40. namespace IFC {
  41. namespace {
  42. // --------------------------------------------------------------------------------
  43. // Conic is the base class for Circle and Ellipse
  44. // --------------------------------------------------------------------------------
  45. class Conic : public Curve
  46. {
  47. public:
  48. // --------------------------------------------------
  49. Conic(const IfcConic& entity, ConversionData& conv)
  50. : Curve(entity,conv)
  51. {
  52. aiMatrix4x4 trafo;
  53. ConvertAxisPlacement(trafo,*entity.Position,conv);
  54. // for convenience, extract the matrix rows
  55. location = aiVector3D(trafo.a4,trafo.b4,trafo.c4);
  56. p[0] = aiVector3D(trafo.a1,trafo.b1,trafo.c1);
  57. p[1] = aiVector3D(trafo.a2,trafo.b2,trafo.c2);
  58. p[2] = aiVector3D(trafo.a3,trafo.b3,trafo.c3);
  59. }
  60. public:
  61. // --------------------------------------------------
  62. bool IsClosed() const {
  63. return true;
  64. }
  65. // --------------------------------------------------
  66. size_t EstimateSampleCount(float a, float b) const {
  67. ai_assert(InRange(a) && InRange(b));
  68. a = fmod(a,360.f);
  69. b = fmod(b,360.f);
  70. return static_cast<size_t>( fabs(ceil(( b-a)) / conv.settings.conicSamplingAngle) );
  71. }
  72. // --------------------------------------------------
  73. ParamRange GetParametricRange() const {
  74. return std::make_pair(0.f,360.f);
  75. }
  76. protected:
  77. aiVector3D location, p[3];
  78. };
  79. // --------------------------------------------------------------------------------
  80. // Circle
  81. // --------------------------------------------------------------------------------
  82. class Circle : public Conic
  83. {
  84. public:
  85. // --------------------------------------------------
  86. Circle(const IfcCircle& entity, ConversionData& conv)
  87. : Conic(entity,conv)
  88. , entity(entity)
  89. {
  90. }
  91. public:
  92. // --------------------------------------------------
  93. aiVector3D Eval(float u) const {
  94. u = -conv.angle_scale * u;
  95. return location + entity.Radius*(::cos(u)*p[0] + ::sin(u)*p[1]);
  96. }
  97. private:
  98. const IfcCircle& entity;
  99. };
  100. // --------------------------------------------------------------------------------
  101. // Ellipse
  102. // --------------------------------------------------------------------------------
  103. class Ellipse : public Conic
  104. {
  105. public:
  106. // --------------------------------------------------
  107. Ellipse(const IfcEllipse& entity, ConversionData& conv)
  108. : Conic(entity,conv)
  109. , entity(entity)
  110. {
  111. }
  112. public:
  113. // --------------------------------------------------
  114. aiVector3D Eval(float u) const {
  115. u = -conv.angle_scale * u;
  116. return location + entity.SemiAxis1*::cos(u)*p[0] + entity.SemiAxis2*::sin(u)*p[1];
  117. }
  118. private:
  119. const IfcEllipse& entity;
  120. };
  121. // --------------------------------------------------------------------------------
  122. // Line
  123. // --------------------------------------------------------------------------------
  124. class Line : public Curve
  125. {
  126. public:
  127. // --------------------------------------------------
  128. Line(const IfcLine& entity, ConversionData& conv)
  129. : Curve(entity,conv)
  130. , entity(entity)
  131. {
  132. ConvertCartesianPoint(p,entity.Pnt);
  133. ConvertVector(v,entity.Dir);
  134. }
  135. public:
  136. // --------------------------------------------------
  137. bool IsClosed() const {
  138. return false;
  139. }
  140. // --------------------------------------------------
  141. aiVector3D Eval(float u) const {
  142. return p + u*v;
  143. }
  144. // --------------------------------------------------
  145. size_t EstimateSampleCount(float a, float b) const {
  146. ai_assert(InRange(a) && InRange(b));
  147. // two points are always sufficient for a line segment
  148. return a==b ? 1 : 2;
  149. }
  150. // --------------------------------------------------
  151. void SampleDiscrete(TempMesh& out,float a, float b) const
  152. {
  153. ai_assert(InRange(a) && InRange(b));
  154. if (a == b) {
  155. out.verts.push_back(Eval(a));
  156. return;
  157. }
  158. out.verts.reserve(out.verts.size()+2);
  159. out.verts.push_back(Eval(a));
  160. out.verts.push_back(Eval(b));
  161. }
  162. // --------------------------------------------------
  163. ParamRange GetParametricRange() const {
  164. const float inf = std::numeric_limits<float>::infinity();
  165. return std::make_pair(-inf,+inf);
  166. }
  167. private:
  168. const IfcLine& entity;
  169. aiVector3D p,v;
  170. };
  171. // --------------------------------------------------------------------------------
  172. // CompositeCurve joins multiple smaller, bounded curves
  173. // --------------------------------------------------------------------------------
  174. class CompositeCurve : public BoundedCurve
  175. {
  176. typedef std::pair< boost::shared_ptr< BoundedCurve >, bool > CurveEntry;
  177. public:
  178. // --------------------------------------------------
  179. CompositeCurve(const IfcCompositeCurve& entity, ConversionData& conv)
  180. : BoundedCurve(entity,conv)
  181. , entity(entity)
  182. , total()
  183. {
  184. curves.reserve(entity.Segments.size());
  185. BOOST_FOREACH(const IfcCompositeCurveSegment& curveSegment,entity.Segments) {
  186. // according to the specification, this must be a bounded curve
  187. boost::shared_ptr< Curve > cv(Curve::Convert(curveSegment.ParentCurve,conv));
  188. boost::shared_ptr< BoundedCurve > bc = boost::dynamic_pointer_cast<BoundedCurve>(cv);
  189. if (!bc) {
  190. IFCImporter::LogError("expected segment of composite curve to be a bounded curve");
  191. continue;
  192. }
  193. if ( (std::string)curveSegment.Transition != "CONTINUOUS" ) {
  194. IFCImporter::LogDebug("ignoring transition code on composite curve segment, only continuous transitions are supported");
  195. }
  196. curves.push_back( CurveEntry(bc,IsTrue(curveSegment.SameSense)) );
  197. total += bc->GetParametricRangeDelta();
  198. }
  199. if (curves.empty()) {
  200. throw CurveError("empty composite curve");
  201. }
  202. }
  203. public:
  204. // --------------------------------------------------
  205. aiVector3D Eval(float u) const {
  206. if (curves.empty()) {
  207. return aiVector3D();
  208. }
  209. float acc = 0;
  210. BOOST_FOREACH(const CurveEntry& entry, curves) {
  211. const ParamRange& range = entry.first->GetParametricRange();
  212. const float delta = range.second-range.first;
  213. if (u < acc+delta) {
  214. return entry.first->Eval( entry.second ? (u-acc) + range.first : range.second-(u-acc));
  215. }
  216. acc += delta;
  217. }
  218. // clamp to end
  219. return curves.back().first->Eval(curves.back().first->GetParametricRange().second);
  220. }
  221. // --------------------------------------------------
  222. size_t EstimateSampleCount(float a, float b) const {
  223. ai_assert(InRange(a) && InRange(b));
  224. size_t cnt = 0;
  225. float acc = 0;
  226. BOOST_FOREACH(const CurveEntry& entry, curves) {
  227. const ParamRange& range = entry.first->GetParametricRange();
  228. const float delta = range.second-range.first;
  229. if (a <= acc+delta && b >= acc) {
  230. const float at = std::max(0.f,a-acc), bt = std::min(delta,b-acc);
  231. cnt += entry.first->EstimateSampleCount( entry.second ? at + range.first : range.second - bt, entry.second ? bt + range.first : range.second - at );
  232. }
  233. acc += delta;
  234. }
  235. return cnt;
  236. }
  237. // --------------------------------------------------
  238. void SampleDiscrete(TempMesh& out,float a, float b) const
  239. {
  240. ai_assert(InRange(a) && InRange(b));
  241. const size_t cnt = EstimateSampleCount(a,b);
  242. out.verts.reserve(out.verts.size() + cnt);
  243. BOOST_FOREACH(const CurveEntry& entry, curves) {
  244. const size_t cnt = out.verts.size();
  245. entry.first->SampleDiscrete(out);
  246. if (!entry.second && cnt != out.verts.size()) {
  247. std::reverse(out.verts.begin()+cnt,out.verts.end());
  248. }
  249. }
  250. }
  251. // --------------------------------------------------
  252. ParamRange GetParametricRange() const {
  253. return std::make_pair(0.f,total);
  254. }
  255. private:
  256. const IfcCompositeCurve& entity;
  257. std::vector< CurveEntry > curves;
  258. float total;
  259. };
  260. // --------------------------------------------------------------------------------
  261. // TrimmedCurve can be used to trim an unbounded curve to a bounded range
  262. // --------------------------------------------------------------------------------
  263. class TrimmedCurve : public BoundedCurve
  264. {
  265. public:
  266. // --------------------------------------------------
  267. TrimmedCurve(const IfcTrimmedCurve& entity, ConversionData& conv)
  268. : BoundedCurve(entity,conv)
  269. , entity(entity)
  270. , ok()
  271. {
  272. base = boost::shared_ptr<const Curve>(Curve::Convert(entity.BasisCurve,conv));
  273. typedef boost::shared_ptr<const STEP::EXPRESS::DataType> Entry;
  274. // for some reason, trimmed curves can either specify a parametric value
  275. // or a point on the curve, or both. And they can even specify which of the
  276. // two representations they prefer, even though an information invariant
  277. // claims that they must be identical if both are present.
  278. // oh well.
  279. bool have_param = false, have_point = false;
  280. aiVector3D point;
  281. BOOST_FOREACH(const Entry sel,entity.Trim1) {
  282. if (const EXPRESS::REAL* const r = sel->ToPtr<EXPRESS::REAL>()) {
  283. range.first = *r;
  284. have_param = true;
  285. break;
  286. }
  287. else if (const IfcCartesianPoint* const r = sel->ResolveSelectPtr<IfcCartesianPoint>(conv.db)) {
  288. ConvertCartesianPoint(point,*r);
  289. have_point = true;
  290. }
  291. }
  292. if (!have_param) {
  293. if (!have_point || !base->ReverseEval(point,range.first)) {
  294. throw CurveError("IfcTrimmedCurve: failed to read first trim parameter, ignoring curve");
  295. }
  296. }
  297. have_param = false, have_point = false;
  298. BOOST_FOREACH(const Entry sel,entity.Trim2) {
  299. if (const EXPRESS::REAL* const r = sel->ToPtr<EXPRESS::REAL>()) {
  300. range.second = *r;
  301. have_param = true;
  302. break;
  303. }
  304. else if (const IfcCartesianPoint* const r = sel->ResolveSelectPtr<IfcCartesianPoint>(conv.db)) {
  305. ConvertCartesianPoint(point,*r);
  306. have_point = true;
  307. }
  308. }
  309. if (!have_param) {
  310. if (!have_point || !base->ReverseEval(point,range.second)) {
  311. throw CurveError("IfcTrimmedCurve: failed to read second trim parameter, ignoring curve");
  312. }
  313. }
  314. agree_sense = IsTrue(entity.SenseAgreement);
  315. if( !agree_sense ) {
  316. std::swap(range.first,range.second);
  317. }
  318. // "NOTE In case of a closed curve, it may be necessary to increment t1 or t2
  319. // by the parametric length for consistency with the sense flag."
  320. if (base->IsClosed()) {
  321. if( range.first > range.second ) {
  322. range.second += base->GetParametricRangeDelta();
  323. }
  324. }
  325. maxval = range.second-range.first;
  326. ai_assert(maxval >= 0);
  327. }
  328. public:
  329. // --------------------------------------------------
  330. aiVector3D Eval(float p) const {
  331. ai_assert(InRange(p));
  332. return base->Eval( TrimParam(p) );
  333. }
  334. // --------------------------------------------------
  335. size_t EstimateSampleCount(float a, float b) const {
  336. ai_assert(InRange(a) && InRange(b));
  337. return base->EstimateSampleCount(TrimParam(a),TrimParam(b));
  338. }
  339. // --------------------------------------------------
  340. ParamRange GetParametricRange() const {
  341. return std::make_pair(0.f,maxval);
  342. }
  343. private:
  344. // --------------------------------------------------
  345. float TrimParam(float f) const {
  346. return agree_sense ? f + range.first : range.second - f;
  347. }
  348. private:
  349. const IfcTrimmedCurve& entity;
  350. ParamRange range;
  351. float maxval;
  352. bool agree_sense;
  353. bool ok;
  354. boost::shared_ptr<const Curve> base;
  355. };
  356. // --------------------------------------------------------------------------------
  357. // PolyLine is a 'curve' defined by linear interpolation over a set of discrete points
  358. // --------------------------------------------------------------------------------
  359. class PolyLine : public BoundedCurve
  360. {
  361. public:
  362. // --------------------------------------------------
  363. PolyLine(const IfcPolyline& entity, ConversionData& conv)
  364. : BoundedCurve(entity,conv)
  365. , entity(entity)
  366. {
  367. points.reserve(entity.Points.size());
  368. aiVector3D t;
  369. BOOST_FOREACH(const IfcCartesianPoint& cp, entity.Points) {
  370. ConvertCartesianPoint(t,cp);
  371. points.push_back(t);
  372. }
  373. }
  374. public:
  375. // --------------------------------------------------
  376. aiVector3D Eval(float p) const {
  377. ai_assert(InRange(p));
  378. const size_t b = static_cast<size_t>(floor(p));
  379. if (b == points.size()-1) {
  380. return points.back();
  381. }
  382. const float d = p-static_cast<float>(b);
  383. return points[b+1] * d + points[b] * (1.f-d);
  384. }
  385. // --------------------------------------------------
  386. size_t EstimateSampleCount(float a, float b) const {
  387. ai_assert(InRange(a) && InRange(b));
  388. return static_cast<size_t>( ceil(b) - floor(a) );
  389. }
  390. // --------------------------------------------------
  391. ParamRange GetParametricRange() const {
  392. return std::make_pair(0.f,static_cast<float>(points.size()-1));
  393. }
  394. private:
  395. const IfcPolyline& entity;
  396. std::vector<aiVector3D> points;
  397. };
  398. } // anon
  399. // ------------------------------------------------------------------------------------------------
  400. Curve* Curve :: Convert(const IFC::IfcCurve& curve,ConversionData& conv)
  401. {
  402. if(curve.ToPtr<IfcBoundedCurve>()) {
  403. if(const IfcPolyline* c = curve.ToPtr<IfcPolyline>()) {
  404. return new PolyLine(*c,conv);
  405. }
  406. if(const IfcTrimmedCurve* c = curve.ToPtr<IfcTrimmedCurve>()) {
  407. return new TrimmedCurve(*c,conv);
  408. }
  409. if(const IfcCompositeCurve* c = curve.ToPtr<IfcCompositeCurve>()) {
  410. return new CompositeCurve(*c,conv);
  411. }
  412. //if(const IfcBSplineCurve* c = curve.ToPtr<IfcBSplineCurve>()) {
  413. // return new BSplineCurve(*c,conv);
  414. //}
  415. }
  416. if(curve.ToPtr<IfcConic>()) {
  417. if(const IfcCircle* c = curve.ToPtr<IfcCircle>()) {
  418. return new Circle(*c,conv);
  419. }
  420. if(const IfcEllipse* c = curve.ToPtr<IfcEllipse>()) {
  421. return new Ellipse(*c,conv);
  422. }
  423. }
  424. if(const IfcLine* c = curve.ToPtr<IfcLine>()) {
  425. return new Line(*c,conv);
  426. }
  427. // XXX OffsetCurve2D, OffsetCurve3D not currently supported
  428. return NULL;
  429. }
  430. #ifdef _DEBUG
  431. // ------------------------------------------------------------------------------------------------
  432. bool Curve :: InRange(float u) const
  433. {
  434. const ParamRange range = GetParametricRange();
  435. if (IsClosed()) {
  436. ai_assert(range.first != std::numeric_limits<float>::infinity() && range.second != std::numeric_limits<float>::infinity());
  437. u = range.first + fmod(u-range.first,range.second-range.first);
  438. }
  439. return u >= range.first && u <= range.second;
  440. }
  441. #endif
  442. // ------------------------------------------------------------------------------------------------
  443. float Curve :: GetParametricRangeDelta() const
  444. {
  445. const ParamRange& range = GetParametricRange();
  446. return range.second - range.first;
  447. }
  448. // ------------------------------------------------------------------------------------------------
  449. size_t Curve :: EstimateSampleCount(float a, float b) const
  450. {
  451. ai_assert(InRange(a) && InRange(b));
  452. // arbitrary default value, deriving classes should supply better suited values
  453. return 16;
  454. }
  455. // ------------------------------------------------------------------------------------------------
  456. float RecursiveSearch(const Curve* cv, const aiVector3D& val, float a, float b, unsigned int samples, float threshold, unsigned int recurse = 0, unsigned int max_recurse = 15)
  457. {
  458. ai_assert(samples>1);
  459. const float delta = (b-a)/samples, inf = std::numeric_limits<float>::infinity();
  460. float min_point[2] = {a,b}, min_diff[2] = {inf,inf};
  461. float runner = a;
  462. for (unsigned int i = 0; i < samples; ++i, runner += delta) {
  463. const float diff = (cv->Eval(runner)-val).SquareLength();
  464. if (diff < min_diff[0]) {
  465. min_diff[1] = min_diff[0];
  466. min_point[1] = min_point[0];
  467. min_diff[0] = diff;
  468. min_point[0] = runner;
  469. }
  470. else if (diff < min_diff[1]) {
  471. min_diff[1] = diff;
  472. min_point[1] = runner;
  473. }
  474. }
  475. ai_assert(min_diff[0] != inf && min_diff[1] != inf);
  476. if ( fabs(a-min_point[0]) < threshold || recurse >= max_recurse) {
  477. return min_point[0];
  478. }
  479. // fix for closed curves to take their wrap-over into account
  480. if (cv->IsClosed() && fabs(min_point[0]-min_point[1]) > cv->GetParametricRangeDelta()*0.5 ) {
  481. const Curve::ParamRange& range = cv->GetParametricRange();
  482. const float wrapdiff = (cv->Eval(range.first)-val).SquareLength();
  483. if (wrapdiff < min_diff[0]) {
  484. const float t = min_point[0];
  485. min_point[0] = min_point[1] > min_point[0] ? range.first : range.second;
  486. min_point[1] = t;
  487. }
  488. }
  489. return RecursiveSearch(cv,val,min_point[0],min_point[1],samples,threshold,recurse+1,max_recurse);
  490. }
  491. // ------------------------------------------------------------------------------------------------
  492. bool Curve :: ReverseEval(const aiVector3D& val, float& paramOut) const
  493. {
  494. // note: the following algorithm is not guaranteed to find the 'right' parameter value
  495. // in all possible cases, but it will always return at least some value so this function
  496. // will never fail in the default implementation.
  497. // XXX derive threshold from curve topology
  498. const float threshold = 1e-4f;
  499. const unsigned int samples = 16;
  500. const ParamRange& range = GetParametricRange();
  501. paramOut = RecursiveSearch(this,val,range.first,range.second,samples,threshold);
  502. return true;
  503. }
  504. // ------------------------------------------------------------------------------------------------
  505. void Curve :: SampleDiscrete(TempMesh& out,float a, float b) const
  506. {
  507. ai_assert(InRange(a) && InRange(b));
  508. const size_t cnt = std::max(static_cast<size_t>(0),EstimateSampleCount(a,b));
  509. out.verts.reserve( out.verts.size() + cnt );
  510. float p = a, delta = (b-a)/cnt;
  511. for(size_t i = 0; i < cnt; ++i, p += delta) {
  512. out.verts.push_back(Eval(p));
  513. }
  514. }
  515. // ------------------------------------------------------------------------------------------------
  516. bool BoundedCurve :: IsClosed() const
  517. {
  518. return false;
  519. }
  520. // ------------------------------------------------------------------------------------------------
  521. void BoundedCurve :: SampleDiscrete(TempMesh& out) const
  522. {
  523. const ParamRange& range = GetParametricRange();
  524. ai_assert(range.first != std::numeric_limits<float>::infinity() && range.second != std::numeric_limits<float>::infinity());
  525. return SampleDiscrete(out,range.first,range.second);
  526. }
  527. } // IFC
  528. } // Assimp
  529. #endif // ASSIMP_BUILD_NO_IFC_IMPORTER