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 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
|
/*******************************************************************************
* csg.cpp
*
* This module implements routines for constructive solid geometry.
*
* ---------------------------------------------------------------------------
* Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
* Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
*
* POV-Ray is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* POV-Ray is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------------
* POV-Ray is based on the popular DKB raytracer version 2.12.
* DKBTrace was originally written by David K. Buck.
* DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
* ---------------------------------------------------------------------------
* $File: //depot/public/povray/3.x/source/backend/shape/csg.cpp $
* $Revision: #1 $
* $Change: 6069 $
* $DateTime: 2013/11/06 11:59:40 $
* $Author: chrisc $
*******************************************************************************/
// frame.h must always be the first POV file included (pulls in platform config)
#include "backend/frame.h"
#include "backend/math/vector.h"
#include "backend/bounding/bbox.h"
#include "backend/shape/csg.h"
#include "backend/math/matrices.h"
#include "backend/scene/objects.h"
#include "backend/shape/quadrics.h"
#include "backend/shape/hfield.h"
#include "backend/scene/threaddata.h"
#include "lightgrp.h" // TODO
#include <algorithm>
// this must be the last file included
#include "base/povdebug.h"
namespace pov
{
/*****************************************************************************
* Global preprocessor defines
******************************************************************************/
#define UNION_OBJECT (IS_COMPOUND_OBJECT | IS_CSG_OBJECT)
#define MERGE_OBJECT (IS_COMPOUND_OBJECT | IS_CSG_OBJECT)
#define INTERSECTION_OBJECT (IS_COMPOUND_OBJECT | IS_CSG_OBJECT)
inline bool Test_Ray_Flags(const Ray& ray, const ObjectBase* obj)
{
// CJC 2005 if ray is primary ray ignore NO_IMAGE_FLAG to support the trace() SDL function
// TODO FIXME - I uess it would be better to have the trace() function use a different ray type [CLi]
return ( ( !ray.IsPhotonRay() &&
(!Test_Flag(obj, NO_IMAGE_FLAG) || ray.IsImageRay() == false || ray.IsPrimaryRay() == true) &&
(!Test_Flag(obj, NO_REFLECTION_FLAG) || ray.IsReflectionRay() == false) &&
(!Test_Flag(obj, NO_RADIOSITY_FLAG) || ray.IsRadiosityRay() == false) ) ||
( ray.IsPhotonRay() && !Test_Flag(obj, NO_SHADOW_FLAG) ) );
}
inline bool Test_Ray_Flags_Shadow(const Ray& ray, const ObjectBase* obj)
{
// TODO CLARIFY - why does this function not ignore NO_IMAGE_FLAG for primary rays, as Test_Ray_Flags() does? [CLi]
return ( ( !ray.IsPhotonRay() &&
(!Test_Flag(obj, NO_IMAGE_FLAG) || ray.IsImageRay() == false) &&
(!Test_Flag(obj, NO_REFLECTION_FLAG) || ray.IsReflectionRay() == false) &&
(!Test_Flag(obj, NO_RADIOSITY_FLAG) || ray.IsRadiosityRay() == false) ) ||
( ray.IsPhotonRay() && !Test_Flag(obj, NO_SHADOW_FLAG) ) ||
( ray.IsShadowTestRay() && !Test_Flag(obj, NO_SHADOW_FLAG) ) );
}
/*****************************************************************************
*
* FUNCTION
*
* All_CSG_Union_Intersections
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* Sep 1994 : Added code to count intersection tests. [DB]
*
******************************************************************************/
bool CSGUnion::All_Intersections(const Ray& ray, IStack& Depth_Stack, TraceThreadData *Thread)
{
int Found;
Thread->Stats()[Ray_CSG_Union_Tests]++;
Found = false;
// Use shortcut if no clip.
if(Clip.empty())
{
for(vector<ObjectPtr>::const_iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
{
if(Test_Ray_Flags(ray, (*Current_Sib))) // TODO CLARIFY - why does CSGUnion use Test_Ray_Flags(), while CSGMerge uses Test_Ray_Flags_Shadow(), and CSGIntersection uses neither?
{
if((*Current_Sib)->Bound.empty() == true || Ray_In_Bound(ray, (*Current_Sib)->Bound, Thread))
{
if((*Current_Sib)->All_Intersections(ray, Depth_Stack, Thread))
Found = true;
}
}
}
}
else
{
IStack Local_Stack(Thread->stackPool);
assert(Local_Stack->empty()); // verify that the IStack pulled from the pool is in a cleaned-up condition
for(vector<ObjectPtr>::const_iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
{
if(Test_Ray_Flags(ray, (*Current_Sib))) // TODO CLARIFY - why does CSGUnion use Test_Ray_Flags(), while CSGMerge uses Test_Ray_Flags_Shadow(), and CSGIntersection uses neither?
{
if((*Current_Sib)->Bound.empty() == true || Ray_In_Bound(ray, (*Current_Sib)->Bound, Thread))
{
if((*Current_Sib)->All_Intersections (ray, Local_Stack, Thread))
{
while(Local_Stack->size() > 0)
{
if(Clip.empty() || Point_In_Clip(Local_Stack->top().IPoint, Clip, Thread))
{
Local_Stack->top().Csg = this;
Depth_Stack->push(Local_Stack->top());
Found = true;
}
Local_Stack->pop();
}
}
}
}
}
assert(Local_Stack->empty()); // verify that the IStack is in a cleaned-up condition (again)
}
if(Found)
Thread->Stats()[Ray_CSG_Union_Tests_Succeeded]++;
return (Found);
}
/*****************************************************************************
*
* FUNCTION
*
* All_CSG_Intersection_Intersections
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* Sep 1994 : Added code to count intersection tests. [DB]
*
******************************************************************************/
bool CSGIntersection::All_Intersections(const Ray& ray, IStack& Depth_Stack, TraceThreadData *Thread)
{
int Maybe_Found, Found;
IStack Local_Stack(Thread->stackPool);
assert(Local_Stack->empty()); // verify that the IStack pulled from the pool is in a cleaned-up condition
Thread->Stats()[Ray_CSG_Intersection_Tests]++;
Found = false;
for(vector<ObjectPtr>::const_iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
{
if ((*Current_Sib)->Bound.empty() == true || Ray_In_Bound(ray, (*Current_Sib)->Bound, Thread))
{
if((*Current_Sib)->All_Intersections(ray, Local_Stack, Thread))
{
while(Local_Stack->size() > 0)
{
Maybe_Found = true;
for(vector<ObjectPtr>::const_iterator Inside_Sib = children.begin(); Inside_Sib != children.end(); Inside_Sib++)
{
if(*Inside_Sib != *Current_Sib)
{
if(!((*Inside_Sib)->Type & LIGHT_SOURCE_OBJECT) || (!((LightSource *)(*Inside_Sib))->children.empty()))
{
if(!Inside_Object(Local_Stack->top().IPoint, *Inside_Sib, Thread))
{
Maybe_Found = false;
break;
}
}
}
}
if(Maybe_Found)
{
if(Clip.empty() || Point_In_Clip(Local_Stack->top().IPoint, Clip, Thread))
{
Local_Stack->top().Csg = this;
Depth_Stack->push(Local_Stack->top());
Found = true;
}
}
Local_Stack->pop();
}
}
}
}
if(Found)
Thread->Stats()[Ray_CSG_Intersection_Tests_Succeeded]++;
assert(Local_Stack->empty()); // verify that the IStack is in a cleaned-up condition (again)
return (Found);
}
/*****************************************************************************
*
* FUNCTION
*
* All_CSG_Merge_Intersections
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* Sep 1994 : Added code to count intersection tests. [DB]
*
******************************************************************************/
bool CSGMerge::All_Intersections(const Ray& ray, IStack& Depth_Stack, TraceThreadData *Thread)
{
int Found;
bool inside_flag;
IStack Local_Stack(Thread->stackPool);
assert(Local_Stack->empty()); // verify that the IStack pulled from the pool is in a cleaned-up condition
Thread->Stats()[Ray_CSG_Merge_Tests]++;
Found = false;
// FIXME - though the name is misleading, the OPTIMISE_SHADOW_TEST flag can be used to
// determine if we're in a shadow ray, but it SHOULD be renamed.
// We should probably change Optimization_Flags to a "ray-type" variable, that will tell
// us if it is primary, reflection, refraction, shadow, primary photon, photon refleciton, or photon refraction ray.
int shadow_flag = ray.IsShadowTestRay(); // TODO FIXME - why is this flag not used?!
for(vector<ObjectPtr>::const_iterator Sib1 = children.begin(); Sib1 != children.end(); Sib1++)
{
if ( Test_Ray_Flags_Shadow(ray, (*Sib1)) )// TODO CLARIFY - why does CSGUnion use Test_Ray_Flags(), while CSGMerge uses Test_Ray_Flags_Shadow(), and CSGIntersection uses neither?
{
if ((*Sib1)->Bound.empty() == true || Ray_In_Bound (ray, (*Sib1)->Bound, Thread))
{
if ((*Sib1)->All_Intersections (ray, Local_Stack, Thread))
{
while (Local_Stack->size() > 0)
{
if (Clip.empty() || Point_In_Clip (Local_Stack->top().IPoint, Clip, Thread))
{
inside_flag = true;
for(vector<ObjectPtr>::const_iterator Sib2 = children.begin(); (Sib2 != children.end()) && (inside_flag == true); Sib2++)
{
if (*Sib1 != *Sib2)
{
if (!((*Sib2)->Type & LIGHT_SOURCE_OBJECT) || (!((LightSource *)(*Sib2))->children.empty()))
{
if ( Test_Ray_Flags_Shadow(ray, (*Sib2)) )// TODO CLARIFY - why does CSGUnion use Test_Ray_Flags(), while CSGMerge uses Test_Ray_Flags_Shadow(), and CSGIntersection uses neither?
{
if (Inside_Object(Local_Stack->top().IPoint, *Sib2, Thread))
inside_flag = false;
}
}
}
}
if (inside_flag == true)
{
Local_Stack->top().Csg = this;
Found = true;
Depth_Stack->push(Local_Stack->top());
}
}
Local_Stack->pop();
}
}
}
}
}
if (Found)
Thread->Stats()[Ray_CSG_Merge_Tests_Succeeded]++;
assert(Local_Stack->empty()); // verify that the IStack is in a cleaned-up condition (again)
return (Found);
}
/*****************************************************************************
*
* FUNCTION
*
* Inside_CSG_Union
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
bool CSGUnion::Inside(const VECTOR IPoint, TraceThreadData *Thread) const
{
for(vector<ObjectPtr>::const_iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
{
if(!((*Current_Sib)->Type & LIGHT_SOURCE_OBJECT) || (!((LightSource *)(*Current_Sib))->children.empty()))
{
if(Inside_Object(IPoint, *Current_Sib, Thread))
return (true);
}
}
return (false);
}
/*****************************************************************************
*
* FUNCTION
*
* Inside_CSG_Intersection
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
bool CSGIntersection::Inside(const VECTOR IPoint, TraceThreadData *Thread) const
{
for(vector<ObjectPtr>::const_iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
if(!((*Current_Sib)->Type & LIGHT_SOURCE_OBJECT) || (!((LightSource *)(*Current_Sib))->children.empty()))
if(!Inside_Object(IPoint, (*Current_Sib), Thread))
return (false);
return (true);
}
/*****************************************************************************
*
* FUNCTION
*
* Translate_CSG
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
void CSG::Translate(const VECTOR Vector, const TRANSFORM *tr)
{
for(vector<ObjectPtr>::iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
Translate_Object (*Current_Sib, Vector, tr) ;
Recompute_BBox(&BBox, tr);
}
/*****************************************************************************
*
* FUNCTION
*
* Rotate_CSG
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
void CSG::Rotate(const VECTOR Vector, const TRANSFORM *tr)
{
for(vector<ObjectPtr>::iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
Rotate_Object (*Current_Sib, Vector, tr) ;
Recompute_BBox(&BBox, tr);
}
/*****************************************************************************
*
* FUNCTION
*
* Scale_CSG
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
void CSG::Scale(const VECTOR Vector, const TRANSFORM *tr)
{
for(vector<ObjectPtr>::iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
Scale_Object (*Current_Sib, Vector, tr) ;
Recompute_BBox(&BBox, tr);
}
/*****************************************************************************
*
* FUNCTION
*
* Transform_CSG
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
void CSG::Transform(const TRANSFORM *tr)
{
for(vector<ObjectPtr>::iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
Transform_Object(*Current_Sib, tr);
Recompute_BBox(&BBox, tr);
}
/*****************************************************************************
*
* FUNCTION
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
void CSG::Invert()
{
// REMINDER: Invert_Object will de-allocate the original object pointer and set it to NULL for any CSG children.
for(vector<ObjectPtr>::iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
{
if (((*Current_Sib)->Type & IS_CSG_OBJECT) != 0)
*Current_Sib = Invert_CSG_Object(*Current_Sib);
else
Invert_Object(*Current_Sib);
}
Invert_Flag(this, INVERTED_FLAG);
}
/*****************************************************************************
*
* FUNCTION
*
* Create_CSG_Union
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* 2000 : NK phmap
*
******************************************************************************/
CSGUnion::CSGUnion() : CSG(UNION_OBJECT)
{
do_split = true;
}
CSGUnion::CSGUnion(int t) : CSG(t)
{
do_split = true;
}
/*****************************************************************************
*
* FUNCTION
*
* Create_CSG_Merge
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
CSGMerge::CSGMerge() : CSGUnion(MERGE_OBJECT)
{
}
CSGMerge::CSGMerge(CompoundObject& o, bool transplant) : CSGUnion(MERGE_OBJECT, o, transplant)
{
}
/*****************************************************************************
*
* FUNCTION
*
* Create_CSG_Intersection
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
CSGIntersection::CSGIntersection(bool diff) : CSG(INTERSECTION_OBJECT), isDifference(diff)
{
do_split = false; // TODO - not necessary but makes debugging clearer
}
CSGIntersection::CSGIntersection(bool diff, CompoundObject& o, bool transplant) : CSG(INTERSECTION_OBJECT, o, transplant), isDifference(diff)
{
do_split = false; // TODO - not necessary but makes debugging clearer
}
/*****************************************************************************
*
* FUNCTION
*
* Copy_CSG
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
ObjectPtr CSGUnion::Copy()
{
CSGUnion *New = new CSGUnion();
Destroy_Transform(New->Trans);
*New = *this;
New->children.clear();
New->children.reserve(children.size());
for(vector<ObjectPtr>::iterator i(children.begin()); i != children.end(); i++)
New->children.push_back(Copy_Object(*i));
if(Type & LIGHT_GROUP_OBJECT)
{
New->LLights.clear();
Promote_Local_Lights(New);
}
return (New);
}
ObjectPtr CSGMerge::Copy()
{
CSGMerge *New = new CSGMerge();
Destroy_Transform(New->Trans);
*New = *this;
New->children.clear();
New->children.reserve(children.size());
for(vector<ObjectPtr>::iterator i(children.begin()); i != children.end(); i++)
New->children.push_back(Copy_Object(*i));
if(Type & LIGHT_GROUP_OBJECT)
{
New->LLights.clear();
Promote_Local_Lights(New);
}
return (New);
}
ObjectPtr CSGIntersection::Copy()
{
CSGIntersection *New = new CSGIntersection(false);
Destroy_Transform(New->Trans);
*New = *this;
New->children.clear();
New->children.reserve(children.size());
for(vector<ObjectPtr>::iterator i(children.begin()); i != children.end(); i++)
New->children.push_back(Copy_Object(*i));
if(Type & LIGHT_GROUP_OBJECT)
{
New->LLights.clear();
Promote_Local_Lights(New);
}
return (New);
}
CSG *CSGMerge::Morph(void)
{
CSGIntersection *New = new CSGIntersection(false, *this, true);
delete this ;
return (New);
}
CSG *CSGUnion::Morph(void)
{
CSGIntersection *New = new CSGIntersection(false, *this, true);
delete this ;
return (New);
}
CSG *CSGIntersection::Morph(void)
{
CSGMerge *New = new CSGMerge(*this, true);
delete this ;
return (New);
}
/*****************************************************************************
*
* FUNCTION
*
* Compute_CSG_BBox
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* Sep 1994 : Improved bounding of quadrics used in CSG intersections. [DB]
*
******************************************************************************/
void CSG::Compute_BBox()
{
DBL Old_Volume, New_Volume;
VECTOR NewMin, NewMax, TmpMin, TmpMax, Min, Max;
if(dynamic_cast<CSGIntersection *>(this) != NULL) // FIXME
{
/*
* Calculate the bounding box of a CSG intersection
* by intersecting the bounding boxes of all children.
*/
Make_Vector(NewMin, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
Make_Vector(NewMax, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
vector<Quadric *> Quadrics;
/* Process all children. */
for(vector<ObjectPtr>::iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
{
/* Inverted objects and height fields mustn't be considered */
if(!Test_Flag((*Current_Sib), INVERTED_FLAG) && (dynamic_cast<HField *>(*Current_Sib) == NULL)) // FIXME
{
/* We store quadrics since they'll be processed last, to benefit from confining them to a certain range */
if(dynamic_cast<Quadric *>(*Current_Sib) == NULL) // FIXME
{
if(dynamic_cast<Plane *>(*Current_Sib) != NULL) // FIXME
Quadric::Compute_Plane_Min_Max((Plane *)(*Current_Sib), TmpMin, TmpMax);
else
Make_min_max_from_BBox(TmpMin, TmpMax, (*Current_Sib)->BBox);
NewMin[X] = max(NewMin[X], TmpMin[X]);
NewMin[Y] = max(NewMin[Y], TmpMin[Y]);
NewMin[Z] = max(NewMin[Z], TmpMin[Z]);
NewMax[X] = min(NewMax[X], TmpMax[X]);
NewMax[Y] = min(NewMax[Y], TmpMax[Y]);
NewMax[Z] = min(NewMax[Z], TmpMax[Z]);
}
else
Quadrics.push_back(dynamic_cast<Quadric *>(*Current_Sib));
}
}
/* Process any quadrics. */
for(vector<Quadric *>::iterator i = Quadrics.begin(); i != Quadrics.end(); i++)
{
Quadric *q = *i;
Assign_Vector(Min, NewMin);
Assign_Vector(Max, NewMax);
q->Compute_BBox(Min, Max);
Make_min_max_from_BBox(TmpMin, TmpMax, q->BBox);
NewMin[X] = max(NewMin[X], TmpMin[X]);
NewMin[Y] = max(NewMin[Y], TmpMin[Y]);
NewMin[Z] = max(NewMin[Z], TmpMin[Z]);
NewMax[X] = min(NewMax[X], TmpMax[X]);
NewMax[Y] = min(NewMax[Y], TmpMax[Y]);
NewMax[Z] = min(NewMax[Z], TmpMax[Z]);
}
}
else
{
/* Calculate the bounding box of a CSG merge/union object. */
Make_Vector(NewMin, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
Make_Vector(NewMax, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
for(vector<ObjectPtr>::iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
{
Make_min_max_from_BBox(TmpMin, TmpMax, (*Current_Sib)->BBox);
NewMin[X] = min(NewMin[X], TmpMin[X]);
NewMin[Y] = min(NewMin[Y], TmpMin[Y]);
NewMin[Z] = min(NewMin[Z], TmpMin[Z]);
NewMax[X] = max(NewMax[X], TmpMax[X]);
NewMax[Y] = max(NewMax[Y], TmpMax[Y]);
NewMax[Z] = max(NewMax[Z], TmpMax[Z]);
}
}
if((NewMin[X] > NewMax[X]) || (NewMin[Y] > NewMax[Y]) || (NewMin[Z] > NewMax[Z]))
;// TODO MESSAGE Warning(0, "Degenerate CSG bounding box (not used!).");
else
{
New_Volume = (NewMax[X] - NewMin[X]) * (NewMax[Y] - NewMin[Y]) * (NewMax[Z] - NewMin[Z]);
BOUNDS_VOLUME(Old_Volume, BBox);
if(New_Volume < Old_Volume)
{
Make_BBox_from_min_max(BBox, NewMin, NewMax);
/* Beware of bounding boxes too large. */
if((BBox.Lengths[X] > CRITICAL_LENGTH) ||
(BBox.Lengths[Y] > CRITICAL_LENGTH) ||
(BBox.Lengths[Z] > CRITICAL_LENGTH))
Make_BBox(BBox, -BOUND_HUGE/2, -BOUND_HUGE/2, -BOUND_HUGE/2, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
}
}
}
/*****************************************************************************
*
* FUNCTION
*
* Determine_CSG_Textures
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
* POV-Ray Team
*
* DESCRIPTION
*
* -
*
* CHANGES
*
* -
*
******************************************************************************/
void CSG::Determine_Textures(Intersection *isect, bool hitinside, WeightedTextureVector& textures, TraceThreadData *threaddata)
{
if(!children.empty())
{
if(Type & CSG_DIFFERENCE_OBJECT)
{
// For CSG Differences, use only the first object in the chain
// (which is the first object in the POV file. All other objects
// are the ones that were "removed" from the first one, so their
// textures should NOT be used.
if(children[0]->Inside(isect->IPoint, threaddata))
{
if(children[0]->Type & IS_COMPOUND_OBJECT)
children[0]->Determine_Textures(isect, hitinside, textures, threaddata);
else if(children[0]->Texture != NULL)
textures.push_back(WeightedTexture(1.0, children[0]->Texture));
}
}
else
{
size_t firstinserted = textures.size();
for(vector<ObjectPtr>::const_iterator Current_Sib = children.begin(); Current_Sib != children.end(); Current_Sib++)
{
if((*Current_Sib)->Inside(isect->IPoint, threaddata))
{
if((*Current_Sib)->Type & IS_COMPOUND_OBJECT)
(*Current_Sib)->Determine_Textures(isect, hitinside, textures, threaddata);
else if((*Current_Sib)->Texture != NULL)
textures.push_back(WeightedTexture(1.0, (*Current_Sib)->Texture));
}
}
COLC weight = 1.0f / max(COLC(textures.size() - firstinserted), 1.0f);
for(size_t i = firstinserted; i < textures.size(); i++)
textures[i].weight = weight;
}
}
}
}
|