Skip to content

Commit 3f1de0d

Browse files
committed
Fix GitHub issue #114 (FlySpray 298).
1 parent 7d9671d commit 3f1de0d

File tree

3 files changed

+82
-66
lines changed

3 files changed

+82
-66
lines changed

changes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ Reported via GitHub:
234234
- #49 ("Latest version from Git won't build with MS Visual Studio 2015")
235235
- #50 ("Make error in Ubuntu 14.04")
236236
- #53 ("Standard include files may mess up the language version")
237+
- #114 ("Port of FS298 - The warning for isosurface does not appears as often as with 3.6.1")
237238
- #129 ("Port of FS331 - Intersection causes quadric to disappear")
238239
- #153 ("vector list trailing comma error sor lathe sphere_sweep")
239240

@@ -242,6 +243,7 @@ Reported via FlySpray:
242243
- FS#254 ("Mesh_camera type 0 output seems to be incorrect")
243244
- FS#261 ("mesh_camera distribution type 3 output image is placed 0.5 pixels to wrong")
244245
- FS#270 ("render abort-continue (+C) sometimes skips blocks")
246+
FS#298 ("the warning for isosurface does not appears as often as with 3.6.1")
245247
- FS#313 ("radiosity.cpp pov::RadiosityFunction::BeforeTile assertion failed")
246248
- FS#314 ("reading Gif is bugged in 3.7")
247249
- FS#315 ("inverse keyword does not work properly with quadrics")

source/core/shape/isosurface.cpp

Lines changed: 74 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,33 @@ namespace pov
5757
* Local preprocessor defines
5858
******************************************************************************/
5959

60+
struct ISO_Max_Gradient
61+
{
62+
DBL max_gradient, gradient;
63+
DBL eval_max, eval_cnt, eval_gradient_sum, eval_var;
64+
bool reported;
65+
66+
ISO_Max_Gradient() :
67+
max_gradient(0.0),
68+
gradient(0.0),
69+
eval_max(0.0),
70+
eval_cnt(0.0),
71+
eval_gradient_sum(0.0),
72+
reported(false),
73+
mRefCounter(0)
74+
{}
75+
76+
bool IsShared() const { return mRefCounter > 1; }
77+
78+
private:
79+
mutable size_t mRefCounter;
80+
friend void intrusive_ptr_add_ref(ISO_Max_Gradient* f);
81+
friend void intrusive_ptr_release(ISO_Max_Gradient* f);
82+
};
83+
84+
inline void intrusive_ptr_add_ref(ISO_Max_Gradient* f) { ++f->mRefCounter; }
85+
inline void intrusive_ptr_release(ISO_Max_Gradient* f) { if (!(--f->mRefCounter)) delete f; }
86+
6087

6188
/*****************************************************************************
6289
*
@@ -557,19 +584,12 @@ IsoSurface::IsoSurface() :
557584
eval_param[2] = 0.0; // 0.99; // not necessary
558585
eval = false;
559586
closed = true;
560-
isCopy = false;
561587

562588
max_gradient = 1.1;
563589
gradient = 0.0;
564590
threshold = 0.0;
565591

566-
mginfo = reinterpret_cast<ISO_Max_Gradient *>(POV_MALLOC(sizeof(ISO_Max_Gradient), "isosurface max_gradient info"));
567-
mginfo->refcnt = 1;
568-
mginfo->max_gradient = 0.0;
569-
mginfo->gradient = 0.0; // not really necessary yet [trf]
570-
mginfo->eval_max = 0.0;
571-
mginfo->eval_cnt = 0.0;
572-
mginfo->eval_gradient_sum = 0.0;
592+
mginfo = intrusive_ptr<ISO_Max_Gradient>(new ISO_Max_Gradient());
573593
}
574594

575595

@@ -610,10 +630,6 @@ ObjectPtr IsoSurface::Copy()
610630
New->Trans = Copy_Transform(Trans);
611631

612632
New->mginfo = mginfo;
613-
New->mginfo->refcnt++;
614-
615-
// mark it as copy for use by max_gradient warning code
616-
New->isCopy = true;
617633

618634
New->positivePolarity = positivePolarity;
619635

@@ -651,8 +667,6 @@ ObjectPtr IsoSurface::Copy()
651667

652668
IsoSurface::~IsoSurface()
653669
{
654-
if(--mginfo->refcnt == 0)
655-
POV_FREE(mginfo);
656670
delete Function;
657671
}
658672

@@ -692,60 +706,63 @@ void IsoSurface::DispatchShutdownMessages(GenericMessenger& messenger)
692706
mginfo->gradient = max(gradient, mginfo->gradient);
693707
mginfo->max_gradient = max((DBL)temp_max_gradient, mginfo->max_gradient);
694708

695-
if (isCopy == false)
709+
if (mginfo->IsShared())
696710
{
697-
const FunctionSourceInfo* fnInfo = Function->GetSourceInfo();
711+
// Other instances shall take care of reporting the max gradient.
712+
mginfo.reset();
713+
return;
714+
}
715+
716+
const FunctionSourceInfo* fnInfo = Function->GetSourceInfo();
698717

699-
if (fnInfo != NULL)
718+
if (fnInfo != NULL)
719+
{
720+
if (eval == false)
700721
{
701-
if (eval == false)
722+
// Only show the warning if necessary!
723+
// BTW, not being too picky here is a feature and not a bug ;-) [trf]
724+
if ((mginfo->gradient > EPSILON) && (mginfo->max_gradient > EPSILON))
702725
{
703-
// Only show the warning if necessary!
704-
// BTW, not being too picky here is a feature and not a bug ;-) [trf]
705-
if ((mginfo->gradient > EPSILON) && (mginfo->max_gradient > EPSILON))
706-
{
707-
DBL diff = mginfo->max_gradient - mginfo->gradient;
708-
DBL prop = fabs(mginfo->max_gradient / mginfo->gradient);
726+
DBL diff = mginfo->max_gradient - mginfo->gradient;
727+
DBL prop = fabs(mginfo->max_gradient / mginfo->gradient);
709728

710-
if (((prop <= 0.9) && (diff <= -0.5)) || (((prop <= 0.95) || (diff <= -0.1)) && (mginfo->max_gradient < 10.0)))
711-
{
712-
messenger.WarningAt(kWarningGeneral, fnInfo->filename, fnInfo->filepos.lineno, 0, fnInfo->filepos.offset,
713-
"The maximum gradient found was %0.3f, but max_gradient of the\n"
714-
"isosurface was set to %0.3f. The isosurface may contain holes!\n"
715-
"Adjust max_gradient to get a proper rendering of the isosurface.",
716-
(float)(mginfo->gradient),
717-
(float)(mginfo->max_gradient));
718-
}
719-
else if ((diff >= 10.0) || ((prop >= 1.1) && (diff >= 0.5)))
720-
{
721-
messenger.WarningAt(kWarningGeneral, fnInfo->filename, fnInfo->filepos.lineno, 0, fnInfo->filepos.offset,
722-
"The maximum gradient found was %0.3f, but max_gradient of\n"
723-
"the isosurface was set to %0.3f. Adjust max_gradient to\n"
724-
"get a faster rendering of the isosurface.",
725-
(float)(mginfo->gradient),
726-
(float)(mginfo->max_gradient));
727-
}
729+
if (((prop <= 0.9) && (diff <= -0.5)) || (((prop <= 0.95) || (diff <= -0.1)) && (mginfo->max_gradient < 10.0)))
730+
{
731+
messenger.WarningAt(kWarningGeneral, fnInfo->filename, fnInfo->filepos.lineno, 0, fnInfo->filepos.offset,
732+
"The maximum gradient found was %0.3f, but max_gradient of the\n"
733+
"isosurface was set to %0.3f. The isosurface may contain holes!\n"
734+
"Adjust max_gradient to get a proper rendering of the isosurface.",
735+
(float)(mginfo->gradient),
736+
(float)(mginfo->max_gradient));
728737
}
729-
}
730-
else
731-
{
732-
DBL diff = (mginfo->eval_max / max(mginfo->eval_max - mginfo->eval_var, EPSILON));
733-
734-
if ((eval_param[0] > mginfo->eval_max) || (eval_param[1] > diff))
738+
else if ((diff >= 10.0) || ((prop >= 1.1) && (diff >= 0.5)))
735739
{
736-
mginfo->eval_cnt = max(mginfo->eval_cnt, 1.0); // make sure it won't be zero
737-
738-
messenger.InfoAt(fnInfo->filename, fnInfo->filepos.lineno, 0, fnInfo->filepos.offset,
739-
"Evaluate found a maximum gradient of %0.3f and an average\n"
740-
"gradient of %0.3f. The maximum gradient variation was %0.3f.\n",
741-
(float)(mginfo->eval_max),
742-
(float)(mginfo->eval_gradient_sum / mginfo->eval_cnt),
743-
(float)(mginfo->eval_var));
740+
messenger.WarningAt(kWarningGeneral, fnInfo->filename, fnInfo->filepos.lineno, 0, fnInfo->filepos.offset,
741+
"The maximum gradient found was %0.3f, but max_gradient of\n"
742+
"the isosurface was set to %0.3f. Adjust max_gradient to\n"
743+
"get a faster rendering of the isosurface.",
744+
(float)(mginfo->gradient),
745+
(float)(mginfo->max_gradient));
744746
}
745747
}
746748
}
747-
}
749+
else
750+
{
751+
DBL diff = (mginfo->eval_max / max(mginfo->eval_max - mginfo->eval_var, EPSILON));
748752

753+
if ((eval_param[0] > mginfo->eval_max) || (eval_param[1] > diff))
754+
{
755+
mginfo->eval_cnt = max(mginfo->eval_cnt, 1.0); // make sure it won't be zero
756+
757+
messenger.InfoAt(fnInfo->filename, fnInfo->filepos.lineno, 0, fnInfo->filepos.offset,
758+
"Evaluate found a maximum gradient of %0.3f and an average\n"
759+
"gradient of %0.3f. The maximum gradient variation was %0.3f.\n",
760+
(float)(mginfo->eval_max),
761+
(float)(mginfo->eval_gradient_sum / mginfo->eval_cnt),
762+
(float)(mginfo->eval_var));
763+
}
764+
}
765+
}
749766
}
750767

751768
/*****************************************************************************

source/core/shape/isosurface.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
// Module config header file must be the first file included within POV-Ray unit header files
4343
#include "core/configcore.h"
4444

45+
#include <boost/intrusive_ptr.hpp>
46+
4547
#include "core/coretypes.h"
4648
#include "core/scene/object.h"
4749

@@ -79,12 +81,7 @@ typedef unsigned char IsosurfaceMaxTrace;
7981

8082
struct ISO_Pair { DBL t,f; };
8183

82-
struct ISO_Max_Gradient
83-
{
84-
unsigned int refcnt;
85-
DBL max_gradient, gradient;
86-
DBL eval_max, eval_cnt, eval_gradient_sum, eval_var;
87-
};
84+
struct ISO_Max_Gradient;
8885

8986
struct ISO_ThreadData
9087
{
@@ -104,15 +101,14 @@ class IsoSurface : public ObjectBase
104101
public:
105102

106103
GenericScalarFunctionPtr Function;
107-
volatile DBL max_gradient; // global in eval
104+
volatile DBL max_gradient; // modified during render if `eval` is set
108105
DBL gradient;
109106
DBL threshold;
110107
DBL accuracy;
111108
DBL eval_param[3];
112109
IsosurfaceMaxTrace max_trace;
113110
bool closed : 1;
114111
bool eval : 1;
115-
bool isCopy : 1;
116112
bool positivePolarity : 1; ///< `true` if values above threshold are considered inside, `false` if considered outside.
117113

118114
shared_ptr<ContainedByShape> container;
@@ -144,7 +140,8 @@ class IsoSurface : public ObjectBase
144140
inline bool IsInside (GenericScalarFunctionInstance& fn, Vector3d& p) const;
145141

146142
private:
147-
ISO_Max_Gradient *mginfo; // global, but just a statistic (read: not thread safe but we don't care) [trf]
143+
144+
intrusive_ptr<ISO_Max_Gradient> mginfo; // global, but just a statistic (read: not thread safe but we don't care) [trf]
148145
};
149146

150147
/// @}

0 commit comments

Comments
 (0)