POV-Ray : Newsgroups : povray.general : Unit conversions in POV Server Time
3 Jun 2023 10:53:18 EDT (-0400)
  Unit conversions in POV (Message 1 to 3 of 3)  
From: Chris R
Subject: Unit conversions in POV
Date: 10 May 2023 11:30:00
Message: <web.645bb7925c9bd5f913355b545cc1b6e@news.povray.org>
I have been using this little snippet of code in a standard include for all of
my scenes for a long time and thought I'd share it as I find it very useful.
It's too small to bother posting as a file in the text groups, and everyone
probably has their own "include this in every scene" file where you could insert
it.

There were a couple of problems I was trying to solve with this.  First, I tend
to reuse my code in more than one scene, so keeping track of what the numbers
represent over time can be a problem.  Second, I reuse the objects in scenes of
different scales, and the limitations of numeric ranges in POV mean that I can't
always use 1 unit = 1 meter, (larger scenes) or 1 unit = 1 mm, (closeups of
small objects) without running into those limitations.  So, I needed a way to
easily change the values in my object libraries depending on the size of the
scene where they were used.

The basic idea is that I never use unitless numbers in my code anymore.
Everything representing a size is declared using the Math_Scale() macro below.
I include this code in every scene file and every include file.  One of the
first things I do in the main scene file, then, is redefine the
Math_Default_Scale variable to the appropriate value for the scene, if needed.
Then include all of the other object model include files, and they will
automatically be scaled for the scene appropriately.

This does mean, that when I borrow code from others, I sometimes have to comb
through it for any constants that assume a particular scale and insert my macros
for it.  I did this with the Camera35mm macros to fix how the blur aperture was
being computed since my scenes are seldom created at mm unit scale.

The macros work either with float constants or vectors:

#declare Box_size = Math_Scale(SCALE_CM, <1, 1, 1>);
#declare Box_thickness = Math_Scale(SCALE_MM, 2);

Here's the code.  Feel free to use it or modify it.  I don't need any credit,
it's not that big of a deal.

//=============================================================================
// Metric scaling factors
//
// The basic, default, scale is 1 unit = 1 cm.  Other scales are provided in
// terms of the basic scale.
//
#declare SCALE_CM   = 1.0;
#declare SCALE_DM   = 0.1 * SCALE_CM;
#declare SCALE_MM   = 10 * SCALE_CM;
#declare SCALE_M    = .01 * SCALE_CM;
#declare SCALE_KM   = .001 * SCALE_M;

#declare SCALE_INCH = SCALE_CM/2.54;
#declare SCALE_FEET = SCALE_INCH/12;
#declare SCALE_YARD = SCALE_FEET/3;
#declare SCALE_MILE = SCALE_FEET/5280;

#ifndef (Math_Default_Scale)
#declare Math_Default_Scale = SCALE_CM;
#end

//-----------------------------------------------------------------------------
// Math_Convert(ScaleFrom,ScaleTo,Units)
//
// Converts Units in the given ScaleFrom to units in the given ScaleTo.
//
#macro Math_Convert(ScaleFrom,ScaleTo,Units)
(Units*(ScaleTo/ScaleFrom))
#end

// End Math_Convert
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Math_Scale(Scale,Units)
//
// Converts Units in the given Scale to units in the default scale as specified
// by the Math_Default_Scale.  For example, if the default scale is SCALE_CM
// and the supplied scale is SCALE_M, Units is multiplied by 100.
//
#macro Math_Scale(Scale,Units)
Math_Convert(Scale,Math_Default_Scale,Units)
#end

// End Math_Scale
//-----------------------------------------------------------------------------

// End Metric scaling factors
//=============================================================================



-- Chris R.


Post a reply to this message

From: Thomas de Groot
Subject: Re: Unit conversions in POV
Date: 11 May 2023 04:09:37
Message: <645ca2c1$1@news.povray.org>
Op 10-5-2023 om 17:26 schreef Chris R:
> I have been using this little snippet of code in a standard include for all of
> my scenes for a long time and thought I'd share it as I find it very useful.
> It's too small to bother posting as a file in the text groups, and everyone
> probably has their own "include this in every scene" file where you could insert
> it.
> 
Thanks for this indeed. I have been using something similar I borrowed 
from Bruno Cabasson some time ago. This is more complete.

> ....  I don't need any credit, it's not that big of a deal.
> 
Of course you don't! :-) Nonetheless, I shall credit you when and where 
I use it, if only because I need to remember /where/ I got my snippets...

-- 
Thomas


Post a reply to this message

From: William F Pokorny
Subject: Re: Unit conversions in POV
Date: 11 May 2023 08:06:12
Message: <645cda34$1@news.povray.org>
On 5/10/23 11:26, Chris R wrote:
> Here's the code.  Feel free to use it or modify it.  I don't need any credit,
> it's not that big of a deal.

Think I'll add this - with credit - to the future, non-core povr include 
file collection tar ball.

Bill P.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.