Draw 3d scene as 2d image with lighting and shadowing to assist the human observer in reconstructing the original 3d scene.
PhilipRBrenan@yahoo.com, 2004, Perl License
Example t/draw.t
#!perl -w #______________________________________________________________________ # Test drawing. # philiprbrenan@yahoo.com, 2004, Perl License #______________________________________________________________________ use Math::Zap::Draw; use Math::Zap::Cube unit=>'cu'; use Math::Zap::Triangle; use Math::Zap::Vector; use Test::Simple tests=>1; #_ Draw _______________________________________________________________ # Draw this set of objects. #______________________________________________________________________ $l = draw ->from (vector( 10, 10, 10)) ->to (vector( 0, 0, 0)) ->horizon (vector( 1, 0.5, 0)) ->light (vector( 20, 30, -20)) ->object(triangle(vector( 0, 0, 0), vector( 8, 0, 0), vector( 0, 8, 0)), 'red') ->object(triangle(vector( 0, 0, 0), vector( 0, 0, 8), vector( 0, 8, 0)), 'green') ->object(triangle(vector( 0, 0, 0), vector(12, 0, 0), vector( 0, 0, 12)) - vector(2.5, 0, 2.5), 'blue') ->object(triangle(vector( 0, 0, 0), vector( 8, 0, 0), vector( 0, -8, 0)), 'pink') ->object(triangle(vector( 0, 0, 0), vector( 0, 0, 8), vector( 0, -8, 0)), 'orange') ->object(cu()*2+vector(3,5,1), 'lightblue') ->print; $L = <<'END'; #!perl -w use Math::Zap::Draw; use Math::Zap::Triangle; use Math::Zap::Vector; draw ->from (vector(10, 10, 10)) ->to (vector(0, 0, 0)) ->horizon (vector(1, 0.5, 0)) ->light (vector(20, 30, -20)) ->object(triangle(vector(0, 0, 0), vector(8, 0, 0), vector(0, 8, 0)), 'red') ->object(triangle(vector(0, 0, 0), vector(0, 0, 8), vector(0, 8, 0)), 'green') ->object(triangle(vector(-2.5, 0, -2.5), vector(9.5, 0, -2.5), vector(-2.5, 0, 9.5)), 'blue') ->object(triangle(vector(0, 0, 0), vector(8, 0, 0), vector(0, -8, 0)), 'pink') ->object(triangle(vector(0, 0, 0), vector(0, 0, 8), vector(0, -8, 0)), 'orange') ->object(triangle(vector(3, 5, 1), vector(5, 5, 1), vector(3, 7, 1)), 'lightblue') ->object(triangle(vector(5, 7, 1), vector(5, 5, 1), vector(3, 7, 1)), 'lightblue') ->object(triangle(vector(3, 5, 3), vector(5, 5, 3), vector(3, 7, 3)), 'lightblue') ->object(triangle(vector(5, 7, 3), vector(5, 5, 3), vector(3, 7, 3)), 'lightblue') ->object(triangle(vector(3, 5, 1), vector(3, 7, 1), vector(3, 5, 3)), 'lightblue') ->object(triangle(vector(3, 7, 3), vector(3, 7, 1), vector(3, 5, 3)), 'lightblue') ->object(triangle(vector(5, 5, 1), vector(5, 7, 1), vector(5, 5, 3)), 'lightblue') ->object(triangle(vector(5, 7, 3), vector(5, 7, 1), vector(5, 5, 3)), 'lightblue') ->object(triangle(vector(3, 5, 1), vector(3, 5, 3), vector(5, 5, 1)), 'lightblue') ->object(triangle(vector(5, 5, 3), vector(3, 5, 3), vector(5, 5, 1)), 'lightblue') ->object(triangle(vector(3, 7, 1), vector(3, 7, 3), vector(5, 7, 1)), 'lightblue') ->object(triangle(vector(5, 7, 3), vector(3, 7, 3), vector(5, 7, 1)), 'lightblue') ->done; END ok($l eq $L);
This package supplies methods to draw a scene, containing three dimensional objects, as a two dimensional image, using lighting and shadowing to assist the human observer in reconstructing the original three dimensional scene.
There are many existing packages to perform this important task: this package Math::Zap::Is the only one to make the attempt in Pure Perl. Pending the $VERSION=1.07; power of Petaflop Parallel Perl (when we will be set free from C), this approach is slow. However, it is not so slow as to be completely useless for simple scenes as might be encountered inside, say for instance, beam lines used in high energy particle physics, the owners of which often have large Perl computers.
The key advantage of this package is that is open: you can manipulate both the objects to be drawn and the drawing itself all in Pure Perl.
Constructor
Set view point
Viewing this point
Sets the direction of the horizon.
Light source position
Display a window allowing the user to set to,from,horizon,light
Draw this object
Draw the complete object list
Print the complete object list as a triangles in a reusable manner.
Check its a drawing
Test its a drawing
Show fission fragments: the objects to be drawn are triangulated where-ever they may intersect. It is useful to see these sub triangles when debugging. See also "fission".
Fission the triangles that intersect. See "showFissionFragments"
New drawing - not a constructor
Canvas for drawing
Controls for drawing
Configuration of canvas has been changed
New drawing of objects
Shadows from a point of illumination
Z-order: order the fission triangles from the back ground to the point of view:
Compare each triangle with every other, recording for each triangle which triangles are behind it.
Place all triangles with no triangles behind them with at the start of the order.
Reprocess the remainder until none left (success) or a cycle is detected (bad algorithm).
The two triangles to be compared are projected on to the background: if their projections have no points in common they are unordered, otherwise use the distance to each triangle from the view point towards the common point as a measure of which is first.
fission() guarantees that no two triangles intersect, this algorithm should correctly order each pair of triangles.
Export "draw"
philiprbrenan@yahoo.com
philiprbrenan@yahoo.com, 2004
Perl License.
To install Math::Zap::Draw, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::Zap::Draw
CPAN shell
perl -MCPAN -e shell install Math::Zap::Draw
For more information on module installation, please visit the detailed CPAN module installation guide.