POV-Ray : Newsgroups : povray.newusers : Volume and Area Calculations in POV-Ray ? Server Time
26 Nov 2024 05:44:02 EST (-0500)
  Volume and Area Calculations in POV-Ray ? (Message 1 to 4 of 4)  
From: Hans-Werner
Subject: Volume and Area Calculations in POV-Ray ?
Date: 24 Sep 2004 18:45:00
Message: <web.4154a3219588c952536167b10@news.povray.org>
Can I use POV-Ray also for doing area and volume calculations ?
Here is an example:

Suppose the mathematical function y=exp(-x^2). Greetings to Gauss !
Now this function rotates around the z-Axis.
Now I cut off a cylinder from this volume.
Maybe along the x-Axis.
Is it possible to calculate the volume that was cutted off ?

Good night


Post a reply to this message

From: Mike Williams
Subject: Re: Volume and Area Calculations in POV-Ray ?
Date: 25 Sep 2004 06:53:06
Message: <LywuSCAq3UVBFw1O@econym.demon.co.uk>
Wasn't it Hans-Werner who wrote:
>Can I use POV-Ray also for doing area and volume calculations ?
>Here is an example:

>Suppose the mathematical function y=exp(-x^2). Greetings to Gauss !
>Now this function rotates around the z-Axis.
>Now I cut off a cylinder from this volume.
>Maybe along the x-Axis.
>Is it possible to calculate the volume that was cutted off ?

You can find a numerical approximation to the volume by firing lots of
trace() calls at it and adding up the volumes of the columns. The
smaller the Step size between the trace() positions, the more accurate
will be the calculated volume.

//-----------------
// Create the shape

#declare E = 2.71828;
#declare THING = intersection {
  isosurface {
    function { y-pow(E, -(x*x+z*z)) }
        max_gradient 1.4
        contained_by{box{<-3,0,-3><3,1,3>}}
    }
  cylinder {-x*3,x*3,1}  
}

// ----------------------------------------
// This bit is so that you can check that the
// THING is the shape that you expected it to be.
// It's not necessary for the volume calculation

camera { location  <3, 1, -4> look_at <0, 0, 0> angle 50}
light_source {<-100,200,-100> colour rgb 1}
object {THING
  pigment {rgb .9}
  finish {phong 0.5 phong_size 10}
}

// ----------------------------------------
// Set the range over which to iterate

#declare Xmin = min_extent(THING).x;
#declare Xmax = max_extent(THING).x;
#declare Zmin = min_extent(THING).z;
#declare Zmax = max_extent(THING).z;

// ----------------------------------------
// Trace the rays

#declare Norm = <0,0,0>;
#declare Total=0;
#declare Step = 0.05; // Set smaller for more accuracy
#declare X=Xmin;
#while (X<Xmax)
  #declare Z=Zmin;
  #while (Z<Zmax)
    #declare P1=trace(THING, <X,100,Z>, -y);
    #declare P2=trace(THING, <X,-100,Z>, y, Norm);
    // Test to see if the trace hit anything
    #if (vlength(Norm)>0)
      #declare Total = Total + P1.y - P2.y;
    #end
        #declare Z=Z+Step;
  #end
  #declare X=X+Step;
#end

//------------------------------------------
// Calculate the volume

#declare Volume = Total*Step*Step;
#debug concat ("Volume = ", str(Volume,-1,-1), " cubic POV units\n")


Post a reply to this message

From: Hans-Werner
Subject: Re: Volume and Area Calculations in POV-Ray ?
Date: 25 Sep 2004 11:35:01
Message: <web.41558ec6ea4355ba86e5b6190@news.povray.org>
First, thank you for your help.


My problem is:
I want to translate the tunnel (the cylinder) along the y-axis.
A translation of the cylinder inside the #declare THING does not work.


the CSG object cylinder or an isosurface.



Thank you in advance



#include "colors.inc"

#declare E = 2.71828;

#declare IsoFinish =
finish {
 ambient 0 diffuse 1
 specular 1 roughness 0.02
 brilliance 2
}


/*
#declare isocylinder =
isosurface
{
 function {sqrt(x*x + y*y) - 0.5}
 threshold 0
 contained_by {box {<-1,-1,-1>, < 1, 1, 1>}}
 texture
 {
  pigment {color Red}
  finish {IsoFinish}
 }
 translate < 1, 2, 0>
}

#declare isoepsilon =
isosurface
{
    function { y-pow(E, -(x*x+z*z)) }
    max_gradient 1.4
    accuracy 0.02
    contained_by{box{<-2,0,-2><2,1,2>}}
}
*/

#declare THING = difference
{

isosurface
{
    function { y-pow(E, -(x*x+z*z)) }
    max_gradient 1.4
    accuracy 0.01
    contained_by{box{<-2,0,-2><2,1,2>}}
}

isosurface
{
 function {sqrt(y*y + z*z) - 0.2}
 threshold 0
 contained_by {box {<-1,-1,-1>, < 1, 1, 1>}}
 texture
 {
  pigment {color Red}
  finish {IsoFinish}
 }
 translate < 0, 0.5, 0>
}

}


//-----------------
// Create the shape
/*
#declare E = 2.71828;
#declare THING = difference
{
  isosurface {
    function { y-pow(E, -(x*x+z*z)) }
        max_gradient 1.4
        contained_by{box{<-2,0,-2><2,1,2>}}
    }

  cylinder {-x*2,x*2,0.2}
}
*/




// ----------------------------------------
// This bit is so that you can check that the
// THING is the shape that you expected it to be.
// It's not necessary for the volume calculation

camera { location  <3, 1, -4> look_at <0, 0, 0> angle 50}
light_source {<-100,200,-100> colour rgb <0.9, 0.9, 1.0>}
object
{ THING
  pigment {rgb .9}
  finish {phong 0.5 phong_size 10}
}

// ----------------------------------------
// Set the range over which to iterate

#declare Xmin = min_extent(THING).x;
#declare Xmax = max_extent(THING).x;
#declare Zmin = min_extent(THING).z;
#declare Zmax = max_extent(THING).z;

// ----------------------------------------
// Trace the rays

#declare Norm = <0,0,0>;
#declare Total=0;
#declare Step = 0.05; // Set smaller for more accuracy
#declare X=Xmin;
#while (X<Xmax)
  #declare Z=Zmin;
  #while (Z<Zmax)
    #declare P1=trace(THING, <X,100,Z>, -y);
    #declare P2=trace(THING, <X,-100,Z>, y, Norm);
    // Test to see if the trace hit anything
    #if (vlength(Norm)>0)
      #declare Total = Total + P1.y - P2.y;
    #end
        #declare Z=Z+Step;
  #end
  #declare X=X+Step;
#end

//------------------------------------------
// Calculate the volume

#declare Volume = Total*Step*Step;
#debug concat ("Volume = ", str(Volume,-1,-1), " cubic POV unitsn")


Post a reply to this message

From: Mike Williams
Subject: Re: Volume and Area Calculations in POV-Ray ?
Date: 25 Sep 2004 12:31:10
Message: <z+BPAFAuzZVBFwj3@econym.demon.co.uk>
Wasn't it Hans-Werner who wrote:
>First, thank you for your help.


>My problem is:
>I want to translate the tunnel (the cylinder) along the y-axis.
>A translation of the cylinder inside the #declare THING does not work.


>the CSG object cylinder or an isosurface.



The problem that you're seeing with the rendered image is because you
need a higher max_trace (or all_intersections). NB Don't confuse this
with the similarly named max_trace_level, that's something completely
different.

A CSG cylinder will do the job just as well and will render and trace()
faster because POV can solve directly for the point where the ray hits
the surface rather than having to hunt for it with a series of guesses.

My tracing code assumed a convex object, it only looks for the first and
last surface it encounters when looking downwards. The easiest way to
fix this is to calculate the volume of the outside part and the volume
of the hole separately and then subtract one from the other.


#include "colors.inc"

#declare E = 2.71828;

#declare IsoFinish =
finish {
 ambient 0 diffuse 1
 specular 1 roughness 0.02
 brilliance 2
}


#declare THING1 = isosurface
{
    function { y-pow(E, -(x*x+z*z)) }
    max_gradient 1.4
    accuracy 0.01
    contained_by{box{<-2,0,-2><2,1,2>}}
    all_intersections
}

#declare THING2 = intersection {
  object {THING1}
  cylinder {<-3,0,0><3,0,0>,0.2 translate <0, 0.5, 0>}
}

// ----------------------------------------
// This bit is so that you can check that the
// THING is the shape that you expected it to be.
// It's not necessary for the volume calculation

camera { location  <3, 1, -4> look_at <0, 0, 0> angle 50}
light_source {<-100,200,-100> colour rgb <0.9, 0.9, 1.0>}

difference {
  object{ THING1
    pigment {rgb .9}
    finish {phong 0.5 phong_size 10}
  }
  cylinder {<-3,0,0><3,0,0>,0.2 translate <0, 0.5, 0>
    texture  {
      pigment {color Red}
      finish {IsoFinish}
    }
  }  
}

// ----------------------------------------
// Set the range over which to iterate
// Remember that THING1 is the bigger one

#declare Xmin = min_extent(THING1).x;
#declare Xmax = max_extent(THING1).x;
#declare Zmin = min_extent(THING1).z;
#declare Zmax = max_extent(THING1).z;

// ----------------------------------------
// Trace the rays

#declare Norm = <0,0,0>;
#declare Total1=0;
#declare Total2=0;
#declare Step = 0.05; // Set smaller for more accuracy
#declare X=Xmin;
#while (X<Xmax)
  #declare Z=Zmin;
  #while (Z<Zmax)
    // Trace THING1
    #declare P1=trace(THING1, <X,100,Z>, -y);
    #declare P2=trace(THING1, <X,-100,Z>, y, Norm);
    // Test to see if the trace hit anything
    #if (vlength(Norm)>0)
      #declare Total1 = Total1 + P1.y - P2.y;
    #end
    
    // Trace THING2
    #declare P1=trace(THING2, <X,100,Z>, -y);
    #declare P2=trace(THING2, <X,-100,Z>, y, Norm);
    // Test to see if the trace hit anything
    #if (vlength(Norm)>0)
      #declare Total2 = Total2 + P1.y - P2.y;
    #end

  #declare Z=Z+Step;
  #end
  #declare X=X+Step;
#end

//------------------------------------------
// Calculate the volume

#declare Volume1 = Total1*Step*Step;
#declare Volume2 = Total2*Step*Step;
#debug concat ("Outer volume = ", str(Volume1,-1,-1), 
" cubic POV units\n")
#debug concat ("Inner volume = ", str(Volume2,-1,-1), 
" cubic POV units\n")
#debug concat ("Volume = ", str(Volume1-Volume2,-1,-1), 
" cubic POV units\n")


Post a reply to this message

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