| 1 | package CAD::Mesh3D; | ||||||||||||||||||||
| 2 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 9 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 9 | ▶ | use 5.010; # M::V::R requires 5.010, so might as well make use of the defined-or // notation :-) | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 9 | ▶ | use Math::Vector::Real 0.18; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 9 | ▶ | use CAD::Format::STL qw//; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 9 | our $VERSION = '0.006001'; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | CAD::Mesh3D - Create and Manipulate 3D Vertexes and Meshes and output for 3D printing | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | use CAD::Mesh3D qw(+STL :create :formats); | ||||||||||||||||||||
| 17 | my $vect = createVertex(); | ||||||||||||||||||||
| 18 | my $tri = createFacet($v1, $v2, $v3); | ||||||||||||||||||||
| 19 | my $mesh = createMesh(); | ||||||||||||||||||||
| 20 | $mesh->addToMesh($tri); | ||||||||||||||||||||
| 21 | ... | ||||||||||||||||||||
| 22 | $mesh->output(STL => $filehandle_or_filename, $ascii_or_binary); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | A framework to create and manipulate 3D vertexes and meshes, suitable for generating STL files | ||||||||||||||||||||
| 27 | (or other similar formats) for 3D printing. | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | A B<Mesh> is the container for the surface of the shape or object being generated. The surface is broken down | ||||||||||||||||||||
| 30 | into locally-flat pieces known as B<Facets>. Each B<Facet> is a triangle made from three points, called | ||||||||||||||||||||
| 31 | B<Vertexes> (also spelled as vertices). Each B<Vertex> is made up of three x, y, and z B<coordinates>, which | ||||||||||||||||||||
| 32 | are just floating-point values to represent the position in 3D space. | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | ################################################################ | ||||||||||||||||||||
| 37 | # Exports | ||||||||||||||||||||
| 38 | ################################################################ | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 9 | ▶ | use Exporter 5.57 (); # v5.57 was needed for getting import() without @ISA (# use Exporter 5.57 'import';) | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | 9 | our @ISA = qw/Exporter/; | |||||||||||||||||||
| 42 | 9 | our @EXPORT_CREATE = qw(createVertex createFacet createQuadrangleFacets createMesh addToMesh); | |||||||||||||||||||
| 43 | 9 | our @EXPORT_VERTEX = qw(createVertex getx gety getz); | |||||||||||||||||||
| 44 | 9 | our @EXPORT_MATH = qw(unitDelta unitCross unitNormal facetNormal); | |||||||||||||||||||
| 45 | 9 | our @EXPORT_FORMATS = qw(enableFormat output input); | |||||||||||||||||||
| 46 | 9 | our @EXPORT_OK = (@EXPORT_CREATE, @EXPORT_MATH, @EXPORT_FORMATS, @EXPORT_VERTEX); | |||||||||||||||||||
| 47 | 9 | our @EXPORT = @EXPORT_FORMATS; | |||||||||||||||||||
| 48 | 9 | our %EXPORT_TAGS = ( | |||||||||||||||||||
| 49 | create => \@EXPORT_CREATE, | ||||||||||||||||||||
| 50 | vertex => \@EXPORT_VERTEX, | ||||||||||||||||||||
| 51 | math => \@EXPORT_MATH, | ||||||||||||||||||||
| 52 | formats => \@EXPORT_FORMATS, | ||||||||||||||||||||
| 53 | all => \@EXPORT_OK, | ||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | sub import | ||||||||||||||||||||
| 57 | { | ||||||||||||||||||||
| 58 | 13 | ▶ | my @list = @_; | ||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 59 | 13 | my @passthru; | |||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | # pass most arguments thru, but if it starts with +, then try to enable that format | ||||||||||||||||||||
| 62 | 13 | foreach my $arg (@list) { | |||||||||||||||||||
| 63 | 29 | ▶ | if( $arg =~ /^\+/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 3 | $arg =~ s/^\+//; | |||||||||||||||||||
| 65 | 3 | enableFormat($arg); | |||||||||||||||||||
| 66 | 3 | next; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | 26 | push @passthru, $arg; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 13 | CAD::Mesh3D->export_to_level(1, @passthru); | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | ################################################################ | ||||||||||||||||||||
| 74 | # "object" creation | ||||||||||||||||||||
| 75 | ################################################################ | ||||||||||||||||||||
| 76 | 9 | ▶ | use constant { XCOORD=>0, YCOORD=>1, ZCOORD=>2 }; # avoid magic numbers | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | ################################################################ | ||||||||||||||||||||
| 79 | # "object" creation | ||||||||||||||||||||
| 80 | ################################################################ | ||||||||||||||||||||
| 81 | # TODO = make the error checking into self-contained routines -- there's | ||||||||||||||||||||
| 82 | # too much duplicated work | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =head1 FUNCTIONS | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =head2 OBJECT CREATION | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | The following functions will create the B<Mesh>, B<Triangle>, and B<Vertex> array-references. | ||||||||||||||||||||
| 89 | They can be imported into your script I<en masse> using the C<:create> tag. | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =head3 createVertex | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | my $v = createVertex( $x, $y, $z ); | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | Creates a B<Vertex> using the given C<$x, $y, $z> floating-point values | ||||||||||||||||||||
| 96 | to represent the x, y, and z coordinates in 3D space. | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 9 | @CAD::Mesh3D::Vertex::ISA = qw/Math::Vector::Real/; | |||||||||||||||||||
| 101 | sub createVertex { | ||||||||||||||||||||
| 102 | 135 | ▶ | croak sprintf("!ERROR! createVertex(x,y,z): requires 3 coordinates; you supplied %d", scalar @_) | ||||||||||||||||||
|
Subroutine
createVertex: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 103 | unless 3==@_; | ||||||||||||||||||||
| 104 | 132 | return bless V(@_), 'CAD::Mesh3D::Vertex'; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =head3 createFacet | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | my $f = createFacet( $a, $b, $c ); | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | Creates a B<Facet> using the three B<Vertex> arguments as the corner points of the triangle. | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | Note that the order of the B<Facet>'s B<Vertexes> matters, and follows the | ||||||||||||||||||||
| 114 | L<right-hand rule|https://en.wikipedia.org/wiki/Right-hand_rule> to determine the "outside" of | ||||||||||||||||||||
| 115 | the B<Facet>: if you are looking at the B<Facet> such that the points are arranged in a | ||||||||||||||||||||
| 116 | counter-clockwise order, then everything from the B<Facet> towards you (and behind you) is | ||||||||||||||||||||
| 117 | "outside" the surface, and everything beyond the B<Facet> is "inside" the surface. | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | sub createFacet { | ||||||||||||||||||||
| 122 | 68 | ▶ | croak sprintf("!ERROR! createFacet(t1,t2,t3): requires 3 Vertexes; you supplied %d", scalar @_) | ||||||||||||||||||
|
Subroutine
createFacet: called CC=7 SCAR=19.5
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 123 | unless 3==@_; | ||||||||||||||||||||
| 124 | 65 | foreach my $v ( @_ ) { | |||||||||||||||||||
| 125 | 180 | ▶ | croak sprintf("!ERROR! createFacet(t1,t2,t3): each Vertex must be an array ref or equivalent object; you supplied a scalar\"%s\"", $v//'<undef>') | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$v // "<undef>"
MC/DC100%1/1
$v // "<undef>"
$v
Truth table100%2/2
A
A= $v
| |||||||||||||||||||||
| 126 | unless ref $v; | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 174 | ▶ | croak sprintf("!ERROR! createFacet(t1,t2,t3): each Vertex must be an array ref or equivalent object; you supplied \"%s\"", ref $v) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | unless UNIVERSAL::isa($v,'ARRAY'); # use function notation, in case $v is not blessed | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 171 | ▶ | croak sprintf("!ERROR! createFacet(t1,t2,t3): each Vertex requires 3 coordinates; you supplied %d: <%s>", scalar @$v, join(",", @$v)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | unless 3==@$v; | ||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | 50 | return bless [@_[0..2]], __PACKAGE__."::Facet"; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =head4 createQuadrangleFacets | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | my @f = createQuadrangleFacets( $a, $b, $c, $d ); | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | Creates two B<Facets> using the four B<Vertex> arguments as the corners of a quadrangle | ||||||||||||||||||||
| 142 | (like with C<createFacet>, the arguments are ordered by the right-hand rule). This returns | ||||||||||||||||||||
| 143 | a list of two triangular B<Facets>, for the triangles B<ABC> and B<ACD>. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | sub createQuadrangleFacets { | ||||||||||||||||||||
| 148 | 4 | ▶ | croak sprintf("!ERROR! createQuadrangleFacets(t1,t2,t3,t4): requires 4 Vertexes; you supplied %d", scalar @_) | ||||||||||||||||||
|
Subroutine
createQuadrangleFacets: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 149 | unless 4==@_; | ||||||||||||||||||||
| 150 | 1 | my ($a,$b,$c,$d) = @_; | |||||||||||||||||||
| 151 | 1 | return ( createFacet($a,$b,$c), createFacet($a,$c,$d) ); | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head4 getx | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =head4 gety | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head4 getz | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | my $v = createVertex(1,2,3); | ||||||||||||||||||||
| 161 | my $x = getx($v); # 1 | ||||||||||||||||||||
| 162 | my $y = getx($v); # 2 | ||||||||||||||||||||
| 163 | my $z = getx($v); # 3 | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | Grabs the individual x, y, or z coordinate from a vertex | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =cut | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 1 | ▶ | sub getx($) { shift()->[XCOORD] } | ||||||||||||||||||
|
Subroutine
getx: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 170 | 1 | ▶ | sub gety($) { shift()->[YCOORD] } | ||||||||||||||||||
|
Subroutine
gety: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 171 | 1 | ▶ | sub getz($) { shift()->[ZCOORD] } | ||||||||||||||||||
|
Subroutine
getz: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | =head3 createMesh | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | my $m = createMesh(); # empty | ||||||||||||||||||||
| 176 | my $s = createMesh($f, ...); # pre-populated | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | Creates a B<Mesh>, optionally pre-populating the Mesh with the supplied B<Facets>. | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | sub createMesh { | ||||||||||||||||||||
| 183 | 19 | ▶ | foreach my $tri ( @_ ) { | ||||||||||||||||||
|
Subroutine
createMesh: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||
| 184 | 55 | ▶ | croak sprintf("!ERROR! createMesh(...): each triangle must be defined; this one was undef") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | unless defined $tri; | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 54 | ▶ | croak sprintf("!ERROR! createMesh(...): each triangle requires 3 Vertexes; you supplied %d: <%s>", scalar @$tri, join(",", @$tri)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 188 | unless 3==@$tri; | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 51 | foreach my $v ( @$tri ) { | |||||||||||||||||||
| 191 | 140 | ▶ | croak sprintf("!ERROR! createMesh(...): each Vertex must be an array ref or equivalent object; you supplied a scalar\"%s\"", $v//'<undef>') | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$v // "<undef>"
MC/DC100%1/1
$v // "<undef>"
$v
Truth table100%2/2
A
A= $v
| |||||||||||||||||||||
| 192 | unless ref $v; | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 136 | ▶ | croak sprintf("!ERROR! createMesh(...): each Vertex must be an array ref or equivalent object; you supplied \"%s\"", ref $v) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 195 | unless UNIVERSAL::isa($v, 'ARRAY'); | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 135 | ▶ | croak sprintf("!ERROR! createMesh(...): each Vertex in each triangle requires 3 coordinates; you supplied %d: <%s>", scalar @$v, join(",", @$v)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 198 | unless 3==@$v; | ||||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | } | ||||||||||||||||||||
| 201 | 7 | return bless [@_]; | |||||||||||||||||||
| 202 | } | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | =head4 addToMesh | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | $mesh->addToMesh($f); | ||||||||||||||||||||
| 207 | $mesh->addToMesh($f1, ... $fN); | ||||||||||||||||||||
| 208 | addToMesh($mesh, $f1, ... $fN); | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | Adds B<Facets> to an existing B<Mesh>. | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | =cut | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | sub addToMesh { | ||||||||||||||||||||
| 215 | 11 | ▶ | my $mesh = shift; | ||||||||||||||||||
|
Subroutine
addToMesh: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 216 | 11 | ▶ | croak sprintf("!ERROR! addToMesh(\$mesh, \@triangles): mesh must have already been created") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 217 | unless UNIVERSAL::isa($mesh, 'ARRAY'); | ||||||||||||||||||||
| 218 | 10 | foreach my $tri ( @_ ) { | |||||||||||||||||||
| 219 | 10 | ▶ | croak sprintf("!ERROR! addToMesh(...): each triangle must be an array ref or equivalent object; you supplied a scalar \"%s\"", $tri//'<undef>') | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$tri // "<undef>"
MC/DC100%1/1
$tri // "<undef>"
$tri
Truth table100%2/2
A
A= $tri
| |||||||||||||||||||||
| 220 | unless ref $tri; | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 8 | ▶ | croak sprintf("!ERROR! addToMesh(...): each triangle must be an array ref or equivalent object; you supplied \"%s\"", ref $tri) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 223 | unless UNIVERSAL::isa($tri, 'ARRAY'); | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | 7 | ▶ | croak sprintf("!ERROR! addToMesh(...): each triangle requires 3 Vertexes; you supplied %d: <%s>", scalar @$tri, join(",", @$tri)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 226 | unless 3==@$tri; | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 6 | foreach my $v ( @$tri ) { | |||||||||||||||||||
| 229 | 15 | ▶ | croak sprintf("!ERROR! addToMesh(...): each Vertex must be an array ref or equivalent object; you supplied a scalar \"%s\"", $v//'<undef>') | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$v // "<undef>"
MC/DC100%1/1
$v // "<undef>"
$v
Truth table100%2/2
A
A= $v
| |||||||||||||||||||||
| 230 | unless ref $v; | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | 13 | ▶ | croak sprintf("!ERROR! addToMesh(...): each Vertex must be an array ref or equivalent object; you supplied \"%s\"", ref $v) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 233 | unless UNIVERSAL::isa($v, 'ARRAY'); | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | 12 | ▶ | croak sprintf("!ERROR! addToMesh(...): each Vertex in each triangle requires 3 coordinates; you supplied %d: <%s>", scalar @$v, join(",", @$v)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 236 | unless 3==@$v; | ||||||||||||||||||||
| 237 | } | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | 2 | push @$mesh, $tri; | |||||||||||||||||||
| 240 | } | ||||||||||||||||||||
| 241 | 2 | return $mesh; | |||||||||||||||||||
| 242 | } | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | ################################################################ | ||||||||||||||||||||
| 245 | # math | ||||||||||||||||||||
| 246 | ################################################################ | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | =head2 MATH FUNCTIONS | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | use CAD::Mesh3D qw/:math/; | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | Most of the math on the three-dimensional B<Vertexes> are handled by | ||||||||||||||||||||
| 253 | L<Math::Matrix::Real>; all the matrix methods will work on B<Vertexes>, | ||||||||||||||||||||
| 254 | as documented for L<Math::Matrix::Real>. | ||||||||||||||||||||
| 255 | However, three-dimensional math can take some special functions that | ||||||||||||||||||||
| 256 | aren't included in the generic matrix library. CAD::Mesh3D implements | ||||||||||||||||||||
| 257 | a few of these special-purpose functions for you. | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | They can be called as methods on the B<Vertex> variables, or | ||||||||||||||||||||
| 260 | imported as functions into your script using the C<:math> tag. | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | =head3 unitDelta | ||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | my $uAB = unitDelta( $A, $B ); | ||||||||||||||||||||
| 265 | # or | ||||||||||||||||||||
| 266 | my $uAB = $A->unitDelta($B); | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | Returns a vector (using same structure as a B<Vertex>), which gives the | ||||||||||||||||||||
| 269 | direction from B<Vertex A> to B<Vertex B>. This is scaled so that | ||||||||||||||||||||
| 270 | the vector has a magnitude of 1.0. | ||||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | =cut | ||||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | sub CAD::Mesh3D::Vertex::unitDelta { | ||||||||||||||||||||
| 275 | # TODO = argument checking | ||||||||||||||||||||
| 276 | 11 | ▶ | my ($beg, $end) = @_; | ||||||||||||||||||
|
Subroutine
unitDelta: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 277 | 11 | my $dx = $end->[XCOORD] - $beg->[XCOORD]; | |||||||||||||||||||
| 278 | 11 | my $dy = $end->[YCOORD] - $beg->[YCOORD]; | |||||||||||||||||||
| 279 | 11 | my $dz = $end->[ZCOORD] - $beg->[ZCOORD]; | |||||||||||||||||||
| 280 | 11 | my $m = sqrt( $dx*$dx + $dy*$dy + $dz*$dz ); | |||||||||||||||||||
| 281 | 11 | ▶ | return $m ? [ $dx/$m, $dy/$m, $dz/$m ] : [0,0,0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 282 | } | ||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | sub unitDelta { | ||||||||||||||||||||
| 285 | # this is the exportable wrapper at the Mesh3D level | ||||||||||||||||||||
| 286 | 11 | ▶ | croak "usage: unitDelta( \$vertexA, \$vertexB)" if UNIVERSAL::isa($_[0], 'CAD::Mesh3D'); # don't allow method calls on ::Mesh3D objects: ie, die on $m->unitDelta($A,$B) | ||||||||||||||||||
|
Subroutine
unitDelta: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 287 | 10 | CAD::Mesh3D::Vertex::unitDelta(@_) | |||||||||||||||||||
| 288 | } | ||||||||||||||||||||
| 289 | |||||||||||||||||||||
| 290 | =head3 unitCross | ||||||||||||||||||||
| 291 | |||||||||||||||||||||
| 292 | my $uN = unitCross( $uAB, $uBC ); | ||||||||||||||||||||
| 293 | # or | ||||||||||||||||||||
| 294 | my $uN = $uAB->unitCross($uBC); | ||||||||||||||||||||
| 295 | |||||||||||||||||||||
| 296 | Returns the cross product for the two vectors, which gives a vector | ||||||||||||||||||||
| 297 | perpendicular to both. This is scaled so that the vector has a | ||||||||||||||||||||
| 298 | magnitude of 1.0. | ||||||||||||||||||||
| 299 | |||||||||||||||||||||
| 300 | A typical usage would be for finding the direction to the "outside" | ||||||||||||||||||||
| 301 | (the normal-vector) using the right-hand rule. For a B<Facet> with | ||||||||||||||||||||
| 302 | points A, B, and C, first, find the direction from A to B, and from B | ||||||||||||||||||||
| 303 | to C; the C<unitCross> of those two deltas gives you the normal-vector | ||||||||||||||||||||
| 304 | (and, in fact, that's how S<C<facetNormal()>> is implemented). | ||||||||||||||||||||
| 305 | |||||||||||||||||||||
| 306 | my $uAB = unitDelta( $A, $B ); | ||||||||||||||||||||
| 307 | my $uBC = unitDelta( $B, $C ); | ||||||||||||||||||||
| 308 | my $uN = unitCross( $uAB, $uBC ); | ||||||||||||||||||||
| 309 | |||||||||||||||||||||
| 310 | =cut | ||||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | sub CAD::Mesh3D::Vertex::unitCross { | ||||||||||||||||||||
| 313 | # TODO = argument checking | ||||||||||||||||||||
| 314 | 9 | ▶ | my ($v1, $v2) = @_; # two vectors | ||||||||||||||||||
|
Subroutine
unitCross: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 315 | 9 | my $dx = $v1->[1]*$v2->[2] - $v1->[2]*$v2->[1]; | |||||||||||||||||||
| 316 | 9 | my $dy = $v1->[2]*$v2->[0] - $v1->[0]*$v2->[2]; | |||||||||||||||||||
| 317 | 9 | my $dz = $v1->[0]*$v2->[1] - $v1->[1]*$v2->[0]; | |||||||||||||||||||
| 318 | 9 | my $m = sqrt( $dx*$dx + $dy*$dy + $dz*$dz ); | |||||||||||||||||||
| 319 | 9 | ▶ | return $m ? [ $dx/$m, $dy/$m, $dz/$m ] : [0,0,0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 320 | } | ||||||||||||||||||||
| 321 | |||||||||||||||||||||
| 322 | sub unitCross { | ||||||||||||||||||||
| 323 | # this is the exportable wrapper at the Mesh3D level | ||||||||||||||||||||
| 324 | 9 | ▶ | croak "usage: unitCross( \$vertexA, \$vertexB)" if UNIVERSAL::isa($_[0], 'CAD::Mesh3D'); # don't allow method calls on ::Mesh3D objects: ie, die on $m->unitCross($A,$B) | ||||||||||||||||||
|
Subroutine
unitCross: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 325 | 8 | CAD::Mesh3D::Vertex::unitCross(@_) | |||||||||||||||||||
| 326 | } | ||||||||||||||||||||
| 327 | |||||||||||||||||||||
| 328 | =head3 facetNormal | ||||||||||||||||||||
| 329 | |||||||||||||||||||||
| 330 | =head3 unitNormal | ||||||||||||||||||||
| 331 | |||||||||||||||||||||
| 332 | my $uN = facetNormal( $facet ); | ||||||||||||||||||||
| 333 | # or | ||||||||||||||||||||
| 334 | my $uN = $facet->normal(); | ||||||||||||||||||||
| 335 | # or | ||||||||||||||||||||
| 336 | my $uN = unitNormal( $vertex1, $vertex2, $vertex3 ) | ||||||||||||||||||||
| 337 | |||||||||||||||||||||
| 338 | Uses S<C<unitDelta()>> and S<C<unitCross()>> to find the normal-vector | ||||||||||||||||||||
| 339 | for the given B<Facet>, given the right-hand rule order for the B<Facet>'s | ||||||||||||||||||||
| 340 | vertexes. | ||||||||||||||||||||
| 341 | |||||||||||||||||||||
| 342 | =cut | ||||||||||||||||||||
| 343 | |||||||||||||||||||||
| 344 | sub CAD::Mesh3D::Facet::normal($) { | ||||||||||||||||||||
| 345 | # TODO = argument checking | ||||||||||||||||||||
| 346 | 4 | ▶ | my ($A,$B,$C) = @{ shift() }; # three vertexes of the facet | ||||||||||||||||||
|
Subroutine
normal: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 347 | 4 | my $uAB = unitDelta( $A, $B ); | |||||||||||||||||||
| 348 | 4 | my $uBC = unitDelta( $B, $C ); | |||||||||||||||||||
| 349 | 4 | return unitCross( $uAB, $uBC ); | |||||||||||||||||||
| 350 | } | ||||||||||||||||||||
| 351 | |||||||||||||||||||||
| 352 | sub facetNormal { | ||||||||||||||||||||
| 353 | # this is the exportable wrapper at the Mesh3D level | ||||||||||||||||||||
| 354 | 3 | ▶ | croak "usage: facetNormal( \$facetF )" if UNIVERSAL::isa($_[0], 'CAD::Mesh3D'); # don't allow method calls on ::Mesh3D objects: ie, die on $m->facetNormal($F) | ||||||||||||||||||
|
Subroutine
facetNormal: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 355 | 2 | CAD::Mesh3D::Facet::normal($_[0]) | |||||||||||||||||||
| 356 | } | ||||||||||||||||||||
| 357 | |||||||||||||||||||||
| 358 | sub unitNormal { | ||||||||||||||||||||
| 359 | # this is the exportable wrapper at the Mesh3D level | ||||||||||||||||||||
| 360 | 2 | ▶ | croak "usage: unitNormal( \$vertexA, \$vertexB, \$vertexC )" if UNIVERSAL::isa($_[0], 'CAD::Mesh3D'); # don't allow method calls on ::Mesh3D objects: ie, die on $m->unitNormal(@$F) | ||||||||||||||||||
|
Subroutine
unitNormal: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 361 | 1 | CAD::Mesh3D::Facet::normal( createFacet(@_) ) | |||||||||||||||||||
| 362 | } | ||||||||||||||||||||
| 363 | |||||||||||||||||||||
| 364 | ################################################################ | ||||||||||||||||||||
| 365 | # enabled formats | ||||||||||||||||||||
| 366 | ################################################################ | ||||||||||||||||||||
| 367 | 9 | our %EnabledFormats = (); | |||||||||||||||||||
| 368 | |||||||||||||||||||||
| 369 | =head2 FORMATS | ||||||||||||||||||||
| 370 | |||||||||||||||||||||
| 371 | If you want to be able to output your mesh into a format, or input a mesh from a format, you need to enable them. | ||||||||||||||||||||
| 372 | This makes it simple to incorporate an add-on C<CAD::Mesh3D::NiftyFormat>. | ||||||||||||||||||||
| 373 | |||||||||||||||||||||
| 374 | Note to developers: L<CAD::Mesh3D::ProvideNewFormat> documents how to write a submodule (usually in the C<CAD::Mesh3D> | ||||||||||||||||||||
| 375 | namespace) to provide the appropriate input and/or output functions for a given format. L<CAD::Mesh3D:STL> is a | ||||||||||||||||||||
| 376 | format that ships with B<CAD::Mesh3D>, and provides an example of how to implement a format module. | ||||||||||||||||||||
| 377 | |||||||||||||||||||||
| 378 | The C<enableFormat>, C<output>, and C<input> functions can be imported using the C<:formats> tag. | ||||||||||||||||||||
| 379 | |||||||||||||||||||||
| 380 | =head3 enableFormat | ||||||||||||||||||||
| 381 | |||||||||||||||||||||
| 382 | use CAD::Mesh3D qw/+STL :formats/; # for the format 'STL' | ||||||||||||||||||||
| 383 | # or | ||||||||||||||||||||
| 384 | enableFormat( $format ) | ||||||||||||||||||||
| 385 | # or | ||||||||||||||||||||
| 386 | enableFormat( $format => $moduleName ) | ||||||||||||||||||||
| 387 | |||||||||||||||||||||
| 388 | C<$moduleName> should be the name of the module that will provide the C<$format> routines. It will default to 'CAD::Mesh3D::$format'. | ||||||||||||||||||||
| 389 | The C<$format> is case-sensitive, so C<enableFormat( 'Stl' ); enableFormat( 'STL' );> will try to enable two separate formats. | ||||||||||||||||||||
| 390 | |||||||||||||||||||||
| 391 | =cut | ||||||||||||||||||||
| 392 | |||||||||||||||||||||
| 393 | sub enableFormat { | ||||||||||||||||||||
| 394 | 9 | ▶ | my $formatName = defined $_[0] ? $_[0] : croak "!ERROR! enableFormat(...): requires name of format"; | ||||||||||||||||||
|
Subroutine
enableFormat: called CC=8 SCAR=20.8
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 395 | 8 | ▶ | my $formatModule = defined $_[1] ? $_[1] : "CAD::Mesh3D::$formatName"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 396 | 8 | (my $key = $formatModule . '.pm') =~ s{::}{/}g; | |||||||||||||||||||
| 397 | 8 | ▶ | eval { require $key unless exists $INC{$key}; 1; } or do { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 398 | 1 | local $" = ", "; | |||||||||||||||||||
| 399 | 1 | croak "!ERROR! enableFormat( @_ ): \n\tcould not import $formatModule\n\t$@"; | |||||||||||||||||||
| 400 | }; | ||||||||||||||||||||
| 401 | 7 | my %io = (); | |||||||||||||||||||
| 402 | 7 | ▶ | eval { %io = $formatModule->_io_functions(); 1; } or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 403 | 1 | local $" = ", "; | |||||||||||||||||||
| 404 | 1 | croak "!ERROR! enableFormat( @_ ): \n\t$formatModule doesn't seem to correctly provide the input and/or output functions\n\t"; | |||||||||||||||||||
| 405 | }; | ||||||||||||||||||||
| 406 | 6 | ▶ | $io{input} = sub { croak "Input function for $formatName is not available" } unless defined $io{input}; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||
| 407 | 6 | ▶ | $io{output} = sub { croak "Output function for $formatName is not available" } unless defined $io{output}; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||
| 408 | # carp "STL input() = $io{input}" if defined $io{input}; | ||||||||||||||||||||
| 409 | # carp "STL output() = $io{output}" if defined $io{output}; | ||||||||||||||||||||
| 410 | # see https://subversion.assembla.com/svn/pryrt/trunk/perl/perlmonks/mesh3d-unasked-question-20190215.pl for workaround using function | ||||||||||||||||||||
| 411 | |||||||||||||||||||||
| 412 | 6 | $EnabledFormats{$formatName} = { %io, module => $formatModule }; | |||||||||||||||||||
| 413 | } | ||||||||||||||||||||
| 414 | |||||||||||||||||||||
| 415 | ################################################################ | ||||||||||||||||||||
| 416 | # file output | ||||||||||||||||||||
| 417 | ################################################################ | ||||||||||||||||||||
| 418 | |||||||||||||||||||||
| 419 | =head3 output | ||||||||||||||||||||
| 420 | |||||||||||||||||||||
| 421 | Output the B<Mesh> to a 3D output file in the given format | ||||||||||||||||||||
| 422 | |||||||||||||||||||||
| 423 | use CAD::Mesh3D qw/+STL :formats/; | ||||||||||||||||||||
| 424 | $mesh->output('STL' => $file); | ||||||||||||||||||||
| 425 | $mesh->output('STL' => $file, @args ); | ||||||||||||||||||||
| 426 | |||||||||||||||||||||
| 427 | Outputs the given C<$mesh> to the indicated file. | ||||||||||||||||||||
| 428 | |||||||||||||||||||||
| 429 | The C<$file> argument is either an already-opened filehandle, or the name of the file | ||||||||||||||||||||
| 430 | (if the full path is not specified, it will default to your script's directory), | ||||||||||||||||||||
| 431 | or "STDOUT" or "STDERR" to direct the output to the standard handles. | ||||||||||||||||||||
| 432 | |||||||||||||||||||||
| 433 | You will need to look at the documentation for your selected format to see what additional | ||||||||||||||||||||
| 434 | C<@args> it might want. Often, the args will be used for setting format options, like | ||||||||||||||||||||
| 435 | picking between ASCII and binary file formats, or similar. | ||||||||||||||||||||
| 436 | |||||||||||||||||||||
| 437 | You also may need to whether your chosen format even supports file output; it is possible | ||||||||||||||||||||
| 438 | that some do not. (For example, some formats may have a binary structure that is free | ||||||||||||||||||||
| 439 | to read, but requires paying a license to write.) | ||||||||||||||||||||
| 440 | |||||||||||||||||||||
| 441 | =cut | ||||||||||||||||||||
| 442 | |||||||||||||||||||||
| 443 | sub output { | ||||||||||||||||||||
| 444 | 18 | ▶ | my ($mesh, $format, @file_and_args) = @_; | ||||||||||||||||||
|
Subroutine
output: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 445 | 18 | $EnabledFormats{$format}{output}->( $mesh, @file_and_args ); | |||||||||||||||||||
| 446 | } | ||||||||||||||||||||
| 447 | |||||||||||||||||||||
| 448 | =head3 input | ||||||||||||||||||||
| 449 | |||||||||||||||||||||
| 450 | use CAD::Mesh3D qw/+STL :formats/; | ||||||||||||||||||||
| 451 | my $mesh = input( 'STL' => $file, @args ); | ||||||||||||||||||||
| 452 | |||||||||||||||||||||
| 453 | Creates a B<Mesh> by reading the given file using the specified format. | ||||||||||||||||||||
| 454 | |||||||||||||||||||||
| 455 | The C<$file> argument is either an already-opened filehandle, or the name of the file | ||||||||||||||||||||
| 456 | (if the full path is not specified, it will default to your script's directory), | ||||||||||||||||||||
| 457 | or "STDIN" to grab the input from the standard input handle. | ||||||||||||||||||||
| 458 | |||||||||||||||||||||
| 459 | You will need to look at the documentation for your selected format to see what additional | ||||||||||||||||||||
| 460 | C<@args> it might want. Often, the args will be used for setting format options, like | ||||||||||||||||||||
| 461 | picking between ASCII and binary file formats, or similar. | ||||||||||||||||||||
| 462 | |||||||||||||||||||||
| 463 | You also may need to whether your chosen format even supports file input; it is possible | ||||||||||||||||||||
| 464 | that some do not. (For example, some formats, like a PNG image, may not contain the | ||||||||||||||||||||
| 465 | necessary 3d information to create a mesh.) | ||||||||||||||||||||
| 466 | |||||||||||||||||||||
| 467 | =cut | ||||||||||||||||||||
| 468 | |||||||||||||||||||||
| 469 | sub input { | ||||||||||||||||||||
| 470 | 8 | ▶ | my ($format, @file_and_args) = @_; | ||||||||||||||||||
|
Subroutine
input: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 471 | 8 | $EnabledFormats{$format}{input}->( @file_and_args ); | |||||||||||||||||||
| 472 | } | ||||||||||||||||||||
| 473 | |||||||||||||||||||||
| 474 | =head1 SEE ALSO | ||||||||||||||||||||
| 475 | |||||||||||||||||||||
| 476 | =over | ||||||||||||||||||||
| 477 | |||||||||||||||||||||
| 478 | =item * L<Math::Vector::Real> - This provides matrix math | ||||||||||||||||||||
| 479 | |||||||||||||||||||||
| 480 | The B<Vertexes> were implemented using this module, to easily handle the | ||||||||||||||||||||
| 481 | B<Vertex> and B<Facet> calculations. | ||||||||||||||||||||
| 482 | |||||||||||||||||||||
| 483 | =item * L<CAD::Format::STL> - This provides simple input and output between | ||||||||||||||||||||
| 484 | STL files and an array-of-arrays perl data structure. | ||||||||||||||||||||
| 485 | |||||||||||||||||||||
| 486 | Adding more features to this module (especially the math on the B<Vertexes> and C<Facets>) | ||||||||||||||||||||
| 487 | and making a generic interface (which can be made to work with other formats) were the two | ||||||||||||||||||||
| 488 | primary motivators behind the CAD::Mesh3D development. | ||||||||||||||||||||
| 489 | |||||||||||||||||||||
| 490 | This module is still used as the backend for the L<CAD::Mesh3D::STL> format-module. | ||||||||||||||||||||
| 491 | |||||||||||||||||||||
| 492 | =back | ||||||||||||||||||||
| 493 | |||||||||||||||||||||
| 494 | =head1 TODO | ||||||||||||||||||||
| 495 | |||||||||||||||||||||
| 496 | =over | ||||||||||||||||||||
| 497 | |||||||||||||||||||||
| 498 | =item * Add more math for B<Vertexes> and B<Facets>, as new functions are identified | ||||||||||||||||||||
| 499 | as being useful. | ||||||||||||||||||||
| 500 | |||||||||||||||||||||
| 501 | =back | ||||||||||||||||||||
| 502 | |||||||||||||||||||||
| 503 | =head1 AUTHOR | ||||||||||||||||||||
| 504 | |||||||||||||||||||||
| 505 | Peter C. Jones C<E<lt>petercj AT cpan DOT orgE<gt>> | ||||||||||||||||||||
| 506 | |||||||||||||||||||||
| 507 | =begin html | ||||||||||||||||||||
| 508 | |||||||||||||||||||||
| 509 | <a href="https://github.com/pryrt/CAD-Mesh3D/issues"><img src="https://img.shields.io/github/issues/pryrt/CAD-Mesh3D.svg" alt="issues" title="issues"></a> | ||||||||||||||||||||
| 510 | <a href="https://ci.appveyor.com/project/pryrt/CAD-Mesh3D"><img src="https://ci.appveyor.com/api/projects/status/bc5jt6b2bjmpig5x?svg=true" alt="appveyor build status" title="appveyor build status"></a> | ||||||||||||||||||||
| 511 | <a href="https://travis-ci.org/pryrt/CAD-Mesh3D"><img src="https://travis-ci.org/pryrt/CAD-Mesh3D.svg?branch=master" alt="travis build status" title="travis build status"></a> | ||||||||||||||||||||
| 512 | <a href='https://coveralls.io/github/pryrt/CAD-Mesh3D?branch=master'><img src='https://coveralls.io/repos/github/pryrt/CAD-Mesh3D/badge.svg?branch=master' alt='Coverage Status' title='Coverage Status' /></a> | ||||||||||||||||||||
| 513 | |||||||||||||||||||||
| 514 | =end html | ||||||||||||||||||||
| 515 | |||||||||||||||||||||
| 516 | =head1 COPYRIGHT | ||||||||||||||||||||
| 517 | |||||||||||||||||||||
| 518 | Copyright (C) 2017,2018,2019,2020,2021 Peter C. Jones | ||||||||||||||||||||
| 519 | |||||||||||||||||||||
| 520 | =head1 LICENSE | ||||||||||||||||||||
| 521 | |||||||||||||||||||||
| 522 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 523 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||
| 524 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||
| 525 | |||||||||||||||||||||
| 526 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||
| 527 | |||||||||||||||||||||
| 528 | =cut | ||||||||||||||||||||
| 529 | |||||||||||||||||||||
| 530 | 9 | 1; | |||||||||||||||||||