Skip to content

Commit 6db6b87

Browse files
committed
Eliminate or clean up a few more instances of dynamic_cast.
1 parent 32f1606 commit 6db6b87

File tree

4 files changed

+29
-87
lines changed

4 files changed

+29
-87
lines changed

source/core/material/pattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ bool ColourImagePattern::Evaluate(TransColour& result, const Vector3d& EPoint, c
486486

487487
// If outside map coverage area, return clear
488488

489-
if (map_pos(EPoint, this, &xcoor, &ycoor))
489+
if (map_pos(EPoint, pImage, &xcoor, &ycoor))
490490
{
491491
result = ToTransColour(RGBFTColour(1.0, 1.0, 1.0, 0.0, 1.0));
492492
return false;

source/core/material/pigment.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/// ----------------------------------------------------------------------------
1515
///
1616
/// Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
17-
/// Copyright 1991-2016 Persistence of Vision Raytracer Pty. Ltd.
17+
/// Copyright 1991-2017 Persistence of Vision Raytracer Pty. Ltd.
1818
///
1919
/// POV-Ray is free software: you can redistribute it and/or modify
2020
/// it under the terms of the GNU Affero General Public License as
@@ -438,10 +438,8 @@ bool Compute_Pigment (TransColour& colour, const PIGMENT *Pigment, const Vector3
438438

439439
colour.Clear();
440440

441-
if (const ColourPattern* pattern = dynamic_cast<ColourPattern*>(Pigment->pattern.get()))
442-
Colour_Found = pattern->Evaluate(colour, TPoint, Intersect, ray, Thread);
443-
else
444-
POV_PATTERN_ASSERT(false);
441+
POV_PATTERN_ASSERT(dynamic_pointer_cast<ColourPattern>(Pigment->pattern));
442+
Colour_Found = static_pointer_cast<ColourPattern>(Pigment->pattern)->Evaluate(colour, TPoint, Intersect, ray, Thread);
445443

446444
break;
447445

source/core/support/imageutil.cpp

Lines changed: 19 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/// @parblock
99
///
1010
/// Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
11-
/// Copyright 1991-2016 Persistence of Vision Raytracer Pty. Ltd.
11+
/// Copyright 1991-2017 Persistence of Vision Raytracer Pty. Ltd.
1212
///
1313
/// POV-Ray is free software: you can redistribute it and/or modify
1414
/// it under the terms of the GNU Affero General Public License as
@@ -107,64 +107,6 @@ static void InterpolateBicubic(const ImageData *image, DBL xcoor, DBL ycoor, RGB
107107
* 1. Cylindrical mapping 2. Spherical mapping 3. Torus mapping
108108
*/
109109

110-
/*****************************************************************************
111-
*
112-
* FUNCTION
113-
*
114-
* image_map
115-
*
116-
* INPUT
117-
*
118-
* EPoint -- 3-D point at which function is evaluated
119-
* Pigment -- Pattern containing various parameters
120-
*
121-
* OUTPUT
122-
*
123-
* Colour -- color at EPoint
124-
*
125-
* RETURNS
126-
*
127-
* int - true, if current point on the image map
128-
* false, if current point is not on the image map
129-
*
130-
* AUTHOR
131-
*
132-
* POV-Ray Team
133-
*
134-
* DESCRIPTION : Determines color of a 3-D point from a 2-D bitmap
135-
*
136-
* CHANGES
137-
*
138-
******************************************************************************/
139-
140-
bool image_map(const Vector3d& EPoint, const PIGMENT *Pigment, TransColour& colour)
141-
{
142-
// TODO ALPHA - the caller does expect non-premultiplied data, but maybe he could profit from premultiplied data?
143-
144-
int reg_number;
145-
DBL xcoor = 0.0, ycoor = 0.0;
146-
147-
// If outside map coverage area, return clear
148-
149-
if(map_pos(EPoint, Pigment->pattern.get(), &xcoor, &ycoor))
150-
{
151-
colour = ToTransColour(RGBFTColour(1.0, 1.0, 1.0, 0.0, 1.0));
152-
return false;
153-
}
154-
else
155-
{
156-
RGBFTColour rgbft;
157-
if (const ImagePatternImpl *pattern = dynamic_cast<ImagePatternImpl*>(Pigment->pattern.get()))
158-
image_colour_at(pattern->pImage, xcoor, ycoor, rgbft, &reg_number, false);
159-
else
160-
POV_PATTERN_ASSERT(false);
161-
colour = ToTransColour(rgbft);
162-
return true;
163-
}
164-
}
165-
166-
167-
168110
/*****************************************************************************
169111
*
170112
* FUNCTION
@@ -199,14 +141,15 @@ TEXTURE *material_map(const Vector3d& EPoint, const TEXTURE *Texture)
199141
* texture index.
200142
*/
201143

202-
if(map_pos(EPoint, Texture->pattern.get(), &xcoor, &ycoor))
144+
// NB: Can't use `static_cast` here due to multi-inheritance in the pattern class hierarchy.
145+
const ImagePatternImpl *pattern = dynamic_cast<ImagePatternImpl*>(Texture->pattern.get());
146+
POV_PATTERN_ASSERT(pattern);
147+
148+
if(map_pos(EPoint, pattern->pImage, &xcoor, &ycoor))
203149
Material_Number = 0;
204150
else
205151
{
206-
if (const ImagePatternImpl *pattern = dynamic_cast<ImagePatternImpl*>(Texture->pattern.get()))
207-
image_colour_at(pattern->pImage, xcoor, ycoor, colour, &reg_number); // TODO ALPHA - we should decide whether we prefer premultiplied or non-premultiplied alpha
208-
else
209-
POV_PATTERN_ASSERT(false);
152+
image_colour_at(pattern->pImage, xcoor, ycoor, colour, &reg_number); // TODO ALPHA - we should decide whether we prefer premultiplied or non-premultiplied alpha
210153

211154
if(reg_number == -1)
212155
Material_Number = (int)(colour.red() * 255.0);
@@ -252,15 +195,16 @@ void bump_map(const Vector3d& EPoint, const TNORMAL *Tnormal, Vector3d& normal)
252195
DBL Amount = Tnormal->Amount;
253196
const ImageData *image;
254197

255-
if (const ImagePatternImpl *pattern = dynamic_cast<ImagePatternImpl*>(Tnormal->pattern.get()))
256-
image = pattern->pImage;
257-
else
258-
POV_PATTERN_ASSERT(false);
198+
// NB: Can't use `static_cast` here due to multi-inheritance in the pattern class hierarchy.
199+
const ImagePatternImpl *pattern = dynamic_cast<ImagePatternImpl*>(Tnormal->pattern.get());
200+
POV_PATTERN_ASSERT(pattern);
201+
202+
image = pattern->pImage;
259203

260204
// going to have to change this
261205
// need to know if bump point is off of image for all 3 points
262206

263-
if(map_pos(EPoint, Tnormal->pattern.get(), &xcoor, &ycoor))
207+
if(map_pos(EPoint, image, &xcoor, &ycoor))
264208
return;
265209
else
266210
image_colour_at(image, xcoor, ycoor, colour1, &index); // TODO ALPHA - we should decide whether we prefer premultiplied or non-premultiplied alpha
@@ -371,20 +315,20 @@ void bump_map(const Vector3d& EPoint, const TNORMAL *Tnormal, Vector3d& normal)
371315
*
372316
******************************************************************************/
373317

374-
DBL image_pattern(const Vector3d& EPoint, const BasicPattern* pPattern)
318+
DBL image_pattern(const Vector3d& EPoint, const ImagePattern* pPattern)
375319
{
376320
DBL xcoor = 0.0, ycoor = 0.0;
377321
int index = -1;
378322
RGBFTColour colour;
379-
const ImageData *image = dynamic_cast<const ImagePatternImpl*>(pPattern)->pImage;
323+
const ImageData *image = pPattern->pImage;
380324
DBL Value;
381325

382326
colour.Clear();
383327

384328
// going to have to change this
385329
// need to know if bump point is off of image for all 3 points
386330

387-
if(map_pos(EPoint, pPattern, &xcoor, &ycoor))
331+
if(map_pos(EPoint, pPattern->pImage, &xcoor, &ycoor))
388332
return 0.0;
389333
else
390334
image_colour_at(image, xcoor, ycoor, colour, &index); // TODO ALPHA - we should decide whether we prefer premultiplied or non-premultiplied alpha
@@ -978,11 +922,9 @@ static int planar_image_map(const Vector3d& EPoint, const ImageData *image, DBL
978922
*
979923
******************************************************************************/
980924

981-
int map_pos(const Vector3d& EPoint, const BasicPattern* pPattern, DBL *xcoor, DBL *ycoor)
925+
int map_pos(const Vector3d& EPoint, const ImageData* image, DBL *xcoor, DBL *ycoor)
982926
{
983-
const ImageData *image = dynamic_cast<const ImagePatternImpl*>(pPattern)->pImage;
984-
985-
// Now determine which mapper to use.
927+
// Determine which mapper to use.
986928

987929
switch(image->Map_Type)
988930
{
@@ -1024,7 +966,7 @@ int map_pos(const Vector3d& EPoint, const BasicPattern* pPattern, DBL *xcoor, DB
1024966
}
1025967

1026968
*xcoor = wrap( *xcoor, (DBL)(image->iwidth));
1027-
*ycoor = wrap(-*ycoor, (DBL)(image->iheight)); // (Compensate for y coordinates on the images being upsidedown)
969+
*ycoor = wrap(-*ycoor, (DBL)(image->iheight)); // (Compensate for y coordinates on the images being upside down)
1028970

1029971
return (0);
1030972
}

source/core/support/imageutil.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// @parblock
1010
///
1111
/// Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
12-
/// Copyright 1991-2016 Persistence of Vision Raytracer Pty. Ltd.
12+
/// Copyright 1991-2017 Persistence of Vision Raytracer Pty. Ltd.
1313
///
1414
/// POV-Ray is free software: you can redistribute it and/or modify
1515
/// it under the terms of the GNU Affero General Public License as
@@ -48,6 +48,9 @@
4848
namespace pov
4949
{
5050

51+
struct ImageData;
52+
struct ImagePattern;
53+
5154
//##############################################################################
5255
///
5356
/// @defgroup PovCoreSupportImageUtil Image Handling Utilities
@@ -125,15 +128,14 @@ class ImageData
125128

126129
typedef ImageData *ImageDataPtr;
127130

128-
DBL image_pattern(const Vector3d& EPoint, const BasicPattern* pPattern);
129-
bool image_map(const Vector3d& EPoint, const PIGMENT *Pigment, TransColour& colour);
131+
DBL image_pattern(const Vector3d& EPoint, const ImagePattern* pPattern); // TODO - move to pattern.cpp
130132
TEXTURE *material_map(const Vector3d& IPoint, const TEXTURE *Texture);
131133
void bump_map(const Vector3d& EPoint, const TNORMAL *Tnormal, Vector3d& normal);
132134
void image_colour_at(const ImageData *image, DBL xcoor, DBL ycoor, RGBFTColour& colour, int *index); // TODO ALPHA - caller should decide whether to prefer premultiplied or non-premultiplied alpha
133135
void image_colour_at(const ImageData *image, DBL xcoor, DBL ycoor, RGBFTColour& colour, int *index, bool premul);
134136
HF_VAL image_height_at(const ImageData *image, int x, int y);
135137
bool is_image_opaque(const ImageData *image);
136-
int map_pos(const Vector3d& EPoint, const BasicPattern* pPattern, DBL *xcoor, DBL *ycoor);
138+
int map_pos(const Vector3d& EPoint, const ImageData* pImage, DBL *xcoor, DBL *ycoor);
137139
ImageData *Copy_Image(ImageData *old);
138140
ImageData *Create_Image(void);
139141
void Destroy_Image(ImageData *image);

0 commit comments

Comments
 (0)