POV-Ray : Newsgroups : povray.general : Unit conversions in POV : Unit conversions in POV Server Time
21 Jun 2024 17:00:13 EDT (-0400)
  Unit conversions in POV  
From: Chris R
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

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_MILE = SCALE_FEET/5280;

#ifndef (Math_Default_Scale)
#declare Math_Default_Scale = SCALE_CM;

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

// 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)

// End Math_Scale

// End Metric scaling factors

-- Chris R.

Post a reply to this message

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