1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
|
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2012, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
/** @file IFCProfile.cpp
* @brief Read profile and curves entities from IFC files
*/
#include "AssimpPCH.h"
#ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
#include "IFCUtil.h"
namespace Assimp {
namespace IFC {
namespace {
// --------------------------------------------------------------------------------
// Conic is the base class for Circle and Ellipse
// --------------------------------------------------------------------------------
class Conic : public Curve
{
public:
// --------------------------------------------------
Conic(const IfcConic& entity, ConversionData& conv)
: Curve(entity,conv)
{
IfcMatrix4 trafo;
ConvertAxisPlacement(trafo,*entity.Position,conv);
// for convenience, extract the matrix rows
location = IfcVector3(trafo.a4,trafo.b4,trafo.c4);
p[0] = IfcVector3(trafo.a1,trafo.b1,trafo.c1);
p[1] = IfcVector3(trafo.a2,trafo.b2,trafo.c2);
p[2] = IfcVector3(trafo.a3,trafo.b3,trafo.c3);
}
public:
// --------------------------------------------------
bool IsClosed() const {
return true;
}
// --------------------------------------------------
size_t EstimateSampleCount(IfcFloat a, IfcFloat b) const {
ai_assert(InRange(a) && InRange(b));
a = math::fmod(a,static_cast<IfcFloat>( 360. ));
b = math::fmod(b,static_cast<IfcFloat>( 360. ));
return static_cast<size_t>( math::fabs(math::ceil(( b-a)) / conv.settings.conicSamplingAngle) );
}
// --------------------------------------------------
ParamRange GetParametricRange() const {
return std::make_pair(static_cast<IfcFloat>( 0. ), static_cast<IfcFloat>( 360. ));
}
protected:
IfcVector3 location, p[3];
};
// --------------------------------------------------------------------------------
// Circle
// --------------------------------------------------------------------------------
class Circle : public Conic
{
public:
// --------------------------------------------------
Circle(const IfcCircle& entity, ConversionData& conv)
: Conic(entity,conv)
, entity(entity)
{
}
public:
// --------------------------------------------------
IfcVector3 Eval(IfcFloat u) const {
u = -conv.angle_scale * u;
return location + static_cast<IfcFloat>(entity.Radius)*(static_cast<IfcFloat>(math::cos(u))*p[0] +
static_cast<IfcFloat>(math::sin(u))*p[1]);
}
private:
const IfcCircle& entity;
};
// --------------------------------------------------------------------------------
// Ellipse
// --------------------------------------------------------------------------------
class Ellipse : public Conic
{
public:
// --------------------------------------------------
Ellipse(const IfcEllipse& entity, ConversionData& conv)
: Conic(entity,conv)
, entity(entity)
{
}
public:
// --------------------------------------------------
IfcVector3 Eval(IfcFloat u) const {
u = -conv.angle_scale * u;
return location + static_cast<IfcFloat>(entity.SemiAxis1)*static_cast<IfcFloat>(math::cos(u))*p[0] +
static_cast<IfcFloat>(entity.SemiAxis2)*static_cast<IfcFloat>(math::sin(u))*p[1];
}
private:
const IfcEllipse& entity;
};
// --------------------------------------------------------------------------------
// Line
// --------------------------------------------------------------------------------
class Line : public Curve
{
public:
// --------------------------------------------------
Line(const IfcLine& entity, ConversionData& conv)
: Curve(entity,conv)
, entity(entity)
{
ConvertCartesianPoint(p,entity.Pnt);
ConvertVector(v,entity.Dir);
}
public:
// --------------------------------------------------
bool IsClosed() const {
return false;
}
// --------------------------------------------------
IfcVector3 Eval(IfcFloat u) const {
return p + u*v;
}
// --------------------------------------------------
size_t EstimateSampleCount(IfcFloat a, IfcFloat b) const {
ai_assert(InRange(a) && InRange(b));
// two points are always sufficient for a line segment
return a==b ? 1 : 2;
}
// --------------------------------------------------
void SampleDiscrete(TempMesh& out,IfcFloat a, IfcFloat b) const
{
ai_assert(InRange(a) && InRange(b));
if (a == b) {
out.verts.push_back(Eval(a));
return;
}
out.verts.reserve(out.verts.size()+2);
out.verts.push_back(Eval(a));
out.verts.push_back(Eval(b));
}
// --------------------------------------------------
ParamRange GetParametricRange() const {
const IfcFloat inf = std::numeric_limits<IfcFloat>::infinity();
return std::make_pair(-inf,+inf);
}
private:
const IfcLine& entity;
IfcVector3 p,v;
};
// --------------------------------------------------------------------------------
// CompositeCurve joins multiple smaller, bounded curves
// --------------------------------------------------------------------------------
class CompositeCurve : public BoundedCurve
{
typedef std::pair< boost::shared_ptr< BoundedCurve >, bool > CurveEntry;
public:
// --------------------------------------------------
CompositeCurve(const IfcCompositeCurve& entity, ConversionData& conv)
: BoundedCurve(entity,conv)
, entity(entity)
, total()
{
curves.reserve(entity.Segments.size());
BOOST_FOREACH(const IfcCompositeCurveSegment& curveSegment,entity.Segments) {
// according to the specification, this must be a bounded curve
boost::shared_ptr< Curve > cv(Curve::Convert(curveSegment.ParentCurve,conv));
boost::shared_ptr< BoundedCurve > bc = boost::dynamic_pointer_cast<BoundedCurve>(cv);
if (!bc) {
IFCImporter::LogError("expected segment of composite curve to be a bounded curve");
continue;
}
if ( (std::string)curveSegment.Transition != "CONTINUOUS" ) {
IFCImporter::LogDebug("ignoring transition code on composite curve segment, only continuous transitions are supported");
}
curves.push_back( CurveEntry(bc,IsTrue(curveSegment.SameSense)) );
total += bc->GetParametricRangeDelta();
}
if (curves.empty()) {
throw CurveError("empty composite curve");
}
}
public:
// --------------------------------------------------
IfcVector3 Eval(IfcFloat u) const {
if (curves.empty()) {
return IfcVector3();
}
IfcFloat acc = 0;
BOOST_FOREACH(const CurveEntry& entry, curves) {
const ParamRange& range = entry.first->GetParametricRange();
const IfcFloat delta = range.second-range.first;
if (u < acc+delta) {
return entry.first->Eval( entry.second ? (u-acc) + range.first : range.second-(u-acc));
}
acc += delta;
}
// clamp to end
return curves.back().first->Eval(curves.back().first->GetParametricRange().second);
}
// --------------------------------------------------
size_t EstimateSampleCount(IfcFloat a, IfcFloat b) const {
ai_assert(InRange(a) && InRange(b));
size_t cnt = 0;
IfcFloat acc = 0;
BOOST_FOREACH(const CurveEntry& entry, curves) {
const ParamRange& range = entry.first->GetParametricRange();
const IfcFloat delta = range.second-range.first;
if (a <= acc+delta && b >= acc) {
const IfcFloat at = std::max(static_cast<IfcFloat>( 0. ),a-acc), bt = std::min(delta,b-acc);
cnt += entry.first->EstimateSampleCount( entry.second ? at + range.first : range.second - bt, entry.second ? bt + range.first : range.second - at );
}
acc += delta;
}
return cnt;
}
// --------------------------------------------------
void SampleDiscrete(TempMesh& out,IfcFloat a, IfcFloat b) const
{
ai_assert(InRange(a) && InRange(b));
const size_t cnt = EstimateSampleCount(a,b);
out.verts.reserve(out.verts.size() + cnt);
BOOST_FOREACH(const CurveEntry& entry, curves) {
const size_t cnt = out.verts.size();
entry.first->SampleDiscrete(out);
if (!entry.second && cnt != out.verts.size()) {
std::reverse(out.verts.begin()+cnt,out.verts.end());
}
}
}
// --------------------------------------------------
ParamRange GetParametricRange() const {
return std::make_pair(static_cast<IfcFloat>( 0. ),total);
}
private:
const IfcCompositeCurve& entity;
std::vector< CurveEntry > curves;
IfcFloat total;
};
// --------------------------------------------------------------------------------
// TrimmedCurve can be used to trim an unbounded curve to a bounded range
// --------------------------------------------------------------------------------
class TrimmedCurve : public BoundedCurve
{
public:
// --------------------------------------------------
TrimmedCurve(const IfcTrimmedCurve& entity, ConversionData& conv)
: BoundedCurve(entity,conv)
, entity(entity)
, ok()
{
base = boost::shared_ptr<const Curve>(Curve::Convert(entity.BasisCurve,conv));
typedef boost::shared_ptr<const STEP::EXPRESS::DataType> Entry;
// for some reason, trimmed curves can either specify a parametric value
// or a point on the curve, or both. And they can even specify which of the
// two representations they prefer, even though an information invariant
// claims that they must be identical if both are present.
// oh well.
bool have_param = false, have_point = false;
IfcVector3 point;
BOOST_FOREACH(const Entry sel,entity.Trim1) {
if (const EXPRESS::REAL* const r = sel->ToPtr<EXPRESS::REAL>()) {
range.first = *r;
have_param = true;
break;
}
else if (const IfcCartesianPoint* const r = sel->ResolveSelectPtr<IfcCartesianPoint>(conv.db)) {
ConvertCartesianPoint(point,*r);
have_point = true;
}
}
if (!have_param) {
if (!have_point || !base->ReverseEval(point,range.first)) {
throw CurveError("IfcTrimmedCurve: failed to read first trim parameter, ignoring curve");
}
}
have_param = false, have_point = false;
BOOST_FOREACH(const Entry sel,entity.Trim2) {
if (const EXPRESS::REAL* const r = sel->ToPtr<EXPRESS::REAL>()) {
range.second = *r;
have_param = true;
break;
}
else if (const IfcCartesianPoint* const r = sel->ResolveSelectPtr<IfcCartesianPoint>(conv.db)) {
ConvertCartesianPoint(point,*r);
have_point = true;
}
}
if (!have_param) {
if (!have_point || !base->ReverseEval(point,range.second)) {
throw CurveError("IfcTrimmedCurve: failed to read second trim parameter, ignoring curve");
}
}
agree_sense = IsTrue(entity.SenseAgreement);
if( !agree_sense ) {
std::swap(range.first,range.second);
}
// "NOTE In case of a closed curve, it may be necessary to increment t1 or t2
// by the parametric length for consistency with the sense flag."
if (base->IsClosed()) {
if( range.first > range.second ) {
range.second += base->GetParametricRangeDelta();
}
}
maxval = range.second-range.first;
ai_assert(maxval >= 0);
}
public:
// --------------------------------------------------
IfcVector3 Eval(IfcFloat p) const {
ai_assert(InRange(p));
return base->Eval( TrimParam(p) );
}
// --------------------------------------------------
size_t EstimateSampleCount(IfcFloat a, IfcFloat b) const {
ai_assert(InRange(a) && InRange(b));
return base->EstimateSampleCount(TrimParam(a),TrimParam(b));
}
// --------------------------------------------------
ParamRange GetParametricRange() const {
return std::make_pair(static_cast<IfcFloat>( 0. ),maxval);
}
private:
// --------------------------------------------------
IfcFloat TrimParam(IfcFloat f) const {
return agree_sense ? f + range.first : range.second - f;
}
private:
const IfcTrimmedCurve& entity;
ParamRange range;
IfcFloat maxval;
bool agree_sense;
bool ok;
boost::shared_ptr<const Curve> base;
};
// --------------------------------------------------------------------------------
// PolyLine is a 'curve' defined by linear interpolation over a set of discrete points
// --------------------------------------------------------------------------------
class PolyLine : public BoundedCurve
{
public:
// --------------------------------------------------
PolyLine(const IfcPolyline& entity, ConversionData& conv)
: BoundedCurve(entity,conv)
, entity(entity)
{
points.reserve(entity.Points.size());
IfcVector3 t;
BOOST_FOREACH(const IfcCartesianPoint& cp, entity.Points) {
ConvertCartesianPoint(t,cp);
points.push_back(t);
}
}
public:
// --------------------------------------------------
IfcVector3 Eval(IfcFloat p) const {
ai_assert(InRange(p));
const size_t b = static_cast<size_t>(math::floor(p));
if (b == points.size()-1) {
return points.back();
}
const IfcFloat d = p-static_cast<IfcFloat>(b);
return points[b+1] * d + points[b] * (static_cast<IfcFloat>( 1. )-d);
}
// --------------------------------------------------
size_t EstimateSampleCount(IfcFloat a, IfcFloat b) const {
ai_assert(InRange(a) && InRange(b));
return static_cast<size_t>( math::ceil(b) - math::floor(a) );
}
// --------------------------------------------------
ParamRange GetParametricRange() const {
return std::make_pair(static_cast<IfcFloat>( 0. ),static_cast<IfcFloat>(points.size()-1));
}
private:
const IfcPolyline& entity;
std::vector<IfcVector3> points;
};
} // anon
// ------------------------------------------------------------------------------------------------
Curve* Curve :: Convert(const IFC::IfcCurve& curve,ConversionData& conv)
{
if(curve.ToPtr<IfcBoundedCurve>()) {
if(const IfcPolyline* c = curve.ToPtr<IfcPolyline>()) {
return new PolyLine(*c,conv);
}
if(const IfcTrimmedCurve* c = curve.ToPtr<IfcTrimmedCurve>()) {
return new TrimmedCurve(*c,conv);
}
if(const IfcCompositeCurve* c = curve.ToPtr<IfcCompositeCurve>()) {
return new CompositeCurve(*c,conv);
}
//if(const IfcBSplineCurve* c = curve.ToPtr<IfcBSplineCurve>()) {
// return new BSplineCurve(*c,conv);
//}
}
if(curve.ToPtr<IfcConic>()) {
if(const IfcCircle* c = curve.ToPtr<IfcCircle>()) {
return new Circle(*c,conv);
}
if(const IfcEllipse* c = curve.ToPtr<IfcEllipse>()) {
return new Ellipse(*c,conv);
}
}
if(const IfcLine* c = curve.ToPtr<IfcLine>()) {
return new Line(*c,conv);
}
// XXX OffsetCurve2D, OffsetCurve3D not currently supported
return NULL;
}
#ifdef _DEBUG
// ------------------------------------------------------------------------------------------------
bool Curve :: InRange(IfcFloat u) const
{
const ParamRange range = GetParametricRange();
if (IsClosed()) {
ai_assert(range.first != std::numeric_limits<IfcFloat>::infinity() && range.second != std::numeric_limits<IfcFloat>::infinity());
u = range.first + math::fmod(u-range.first,range.second-range.first);
}
return u >= range.first && u <= range.second;
}
#endif
// ------------------------------------------------------------------------------------------------
IfcFloat Curve :: GetParametricRangeDelta() const
{
const ParamRange& range = GetParametricRange();
return range.second - range.first;
}
// ------------------------------------------------------------------------------------------------
size_t Curve :: EstimateSampleCount(IfcFloat a, IfcFloat b) const
{
ai_assert(InRange(a) && InRange(b));
// arbitrary default value, deriving classes should supply better suited values
return 16;
}
// ------------------------------------------------------------------------------------------------
IfcFloat RecursiveSearch(const Curve* cv, const IfcVector3& val, IfcFloat a, IfcFloat b, unsigned int samples, IfcFloat threshold, unsigned int recurse = 0, unsigned int max_recurse = 15)
{
ai_assert(samples>1);
const IfcFloat delta = (b-a)/samples, inf = std::numeric_limits<IfcFloat>::infinity();
IfcFloat min_point[2] = {a,b}, min_diff[2] = {inf,inf};
IfcFloat runner = a;
for (unsigned int i = 0; i < samples; ++i, runner += delta) {
const IfcFloat diff = (cv->Eval(runner)-val).SquareLength();
if (diff < min_diff[0]) {
min_diff[1] = min_diff[0];
min_point[1] = min_point[0];
min_diff[0] = diff;
min_point[0] = runner;
}
else if (diff < min_diff[1]) {
min_diff[1] = diff;
min_point[1] = runner;
}
}
ai_assert(min_diff[0] != inf && min_diff[1] != inf);
if ( math::fabs(a-min_point[0]) < threshold || recurse >= max_recurse) {
return min_point[0];
}
// fix for closed curves to take their wrap-over into account
if (cv->IsClosed() && math::fabs(min_point[0]-min_point[1]) > cv->GetParametricRangeDelta()*0.5 ) {
const Curve::ParamRange& range = cv->GetParametricRange();
const IfcFloat wrapdiff = (cv->Eval(range.first)-val).SquareLength();
if (wrapdiff < min_diff[0]) {
const IfcFloat t = min_point[0];
min_point[0] = min_point[1] > min_point[0] ? range.first : range.second;
min_point[1] = t;
}
}
return RecursiveSearch(cv,val,min_point[0],min_point[1],samples,threshold,recurse+1,max_recurse);
}
// ------------------------------------------------------------------------------------------------
bool Curve :: ReverseEval(const IfcVector3& val, IfcFloat& paramOut) const
{
// note: the following algorithm is not guaranteed to find the 'right' parameter value
// in all possible cases, but it will always return at least some value so this function
// will never fail in the default implementation.
// XXX derive threshold from curve topology
const IfcFloat threshold = 1e-4f;
const unsigned int samples = 16;
const ParamRange& range = GetParametricRange();
paramOut = RecursiveSearch(this,val,range.first,range.second,samples,threshold);
return true;
}
// ------------------------------------------------------------------------------------------------
void Curve :: SampleDiscrete(TempMesh& out,IfcFloat a, IfcFloat b) const
{
ai_assert(InRange(a) && InRange(b));
const size_t cnt = std::max(static_cast<size_t>(0),EstimateSampleCount(a,b));
out.verts.reserve( out.verts.size() + cnt );
IfcFloat p = a, delta = (b-a)/cnt;
for(size_t i = 0; i < cnt; ++i, p += delta) {
out.verts.push_back(Eval(p));
}
}
// ------------------------------------------------------------------------------------------------
bool BoundedCurve :: IsClosed() const
{
return false;
}
// ------------------------------------------------------------------------------------------------
void BoundedCurve :: SampleDiscrete(TempMesh& out) const
{
const ParamRange& range = GetParametricRange();
ai_assert(range.first != std::numeric_limits<IfcFloat>::infinity() && range.second != std::numeric_limits<IfcFloat>::infinity());
return SampleDiscrete(out,range.first,range.second);
}
} // IFC
} // Assimp
#endif // ASSIMP_BUILD_NO_IFC_IMPORTER
|