| 1 | package Graphics::Penplotter::GcodeXY::Geometry3D v0.9.4; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # Role::Tiny role adding 3-D geometry to Graphics::Penplotter::GcodeXY. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # Design constraints | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # ------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # The host class (GcodeXY) already defines: | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # gsave / grestore / moveto / movetoR / line / lineR / | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # translate / translateC / scale / rotate / initmatrix / currentpoint | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # Role::Tiny does NOT override methods that already exist in the consuming | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # class, so all of those that we need 3-D variants of are renamed with a | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # '3' suffix (translate3, scale3, rotate3, moveto3, line3, ...). | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # gsave / grestore are extended via 'after'/'before' modifiers so that the | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # 3-D CTM stack is kept in sync with the 2-D one transparently. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # All private 3-D state lives in $self->{_g3_*} keys; nothing collides with | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # the host's own hash slots. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 9 | ▶ | use v5.38.2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 9 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | use POSIX qw( floor ceil acos tan ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # The consuming class must provide these primitive hooks | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 9 | requires qw( | |||||||||||||||||||||||||||||||||||||||||
| 29 | penup pendown | ||||||||||||||||||||||||||||||||||||||||||
| 30 | _genfastmove _genslowmove | ||||||||||||||||||||||||||||||||||||||||||
| 31 | stroke | ||||||||||||||||||||||||||||||||||||||||||
| 32 | _croak | ||||||||||||||||||||||||||||||||||||||||||
| 33 | gsave grestore | ||||||||||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # Module-private constants (plain lexicals -- no Readonly dependency needed) | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 9 | my $PI = 3.14159265358979323846; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 9 | my $D2R = $PI / 180.0; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 9 | my $EPSILON = 1e-9; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # SECTION: Private (non-method) helpers | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub _g3_id4 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_g3_id4: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | return [ [1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1] ]; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub _g3_clone4 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_g3_clone4: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my ($m) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return defined $m ? [ map { [ @$_ ] } @$m ] : _g3_id4(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # Accessor that auto-initialises the 3-D CTM | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub _g3_ctm { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_g3_ctm: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $self->{_g3_CTM} //= _g3_id4(); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"_g3_CTM"} //= _g3_id4()
MC/DC0%0/2
$self->{"_g3_CTM"} //= _g3_id4()
$self->{"_g3_CTM"}
_g3_id4()
Truth table0%0/3
A //= B
A= $self->{"_g3_CTM"}B= _g3_id4()
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | return $self->{_g3_CTM}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # SECTION: Extend gsave / grestore with 3-D state | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | after 'gsave' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $self->{_g3_gstate} //= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"_g3_gstate"} //= []
MC/DC0%0/1
$self->{"_g3_gstate"} //= []
$self->{"_g3_gstate"}
Truth table0%0/2
A
A= $self->{"_g3_gstate"}
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | push @{ $self->{_g3_gstate} }, { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$self->{"_g3_posx"} // 0
$self->{"_g3_posy"} // 0
$self->{"_g3_posz"} // 0
MC/DC0%0/1
$self->{"_g3_posx"} // 0
$self->{"_g3_posx"}
MC/DC0%0/1
$self->{"_g3_posy"} // 0
$self->{"_g3_posy"}
MC/DC0%0/1
$self->{"_g3_posz"} // 0
$self->{"_g3_posz"}
Truth table0%0/2
A
A= $self->{"_g3_posx"}
Truth table0%0/2
A
A= $self->{"_g3_posy"}
Truth table0%0/2
A
A= $self->{"_g3_posz"}
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | CTM => _g3_clone4( $self->{_g3_CTM} ), | ||||||||||||||||||||||||||||||||||||||||||
| 72 | posx => $self->{_g3_posx} // 0, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | posy => $self->{_g3_posy} // 0, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | posz => $self->{_g3_posz} // 0, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | camera => $self->{_g3_camera}, # undef is fine; restores to no-camera | ||||||||||||||||||||||||||||||||||||||||||
| 76 | projection => $self->{_g3_projection}, # undef is fine; restores to no-projection | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | after 'grestore' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | return unless $self->{_g3_gstate} && @{ $self->{_g3_gstate} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"_g3_gstate"} and @{$self->{"_g3_gstate"};}
MC/DC0%0/2
$self->{"_g3_gstate"} and @{$self->{"_g3_gstate"};}
$self->{"_g3_gstate"}
@{$self->{"_g3_gstate"};}
Truth table0%0/3
A and B
A= $self->{"_g3_gstate"}B= @{$self->{"_g3_gstate"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my $rec = pop @{ $self->{_g3_gstate} }; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $self->{_g3_CTM} = _g3_clone4( $rec->{CTM} ); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $self->{_g3_posx} = $rec->{posx}; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $self->{_g3_posy} = $rec->{posy}; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $self->{_g3_posz} = $rec->{posz}; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | $self->{_g3_camera} = $rec->{camera}; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $self->{_g3_projection} = $rec->{projection}; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # SECTION: 3-D CTM management | ||||||||||||||||||||||||||||||||||||||||||
| 94 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # Reset the 3-D CTM to identity | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub initmatrix3 ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
initmatrix3: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $self->{_g3_CTM} = _g3_id4(); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | # Premultiply 4x4 matrix $aref into $bref (B := A x B) | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub _g3_premul4 ($self, $aref, $bref) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_g3_premul4: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my @tmp; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | for my $r (0..3) { | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | for my $c (0..3) { | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $tmp[$r][$c] = 0; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $tmp[$r][$c] += $aref->[$r][$_] * $bref->[$_][$c] for 0..3; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | for my $r (0..3) { $bref->[$r][$_] = $tmp[$r][$_] for 0..3 } | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | # Multiply two 4x4 matrices, returning a new matrix ref (A x B) | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub compose_matrix ($self, $aref, $bref) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compose_matrix: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | for my $r (0..3) { | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | for my $c (0..3) { | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $out[$r][$c] = 0; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | $out[$r][$c] += $aref->[$r][$_] * $bref->[$_][$c] for 0..3; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | return \@out; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | # Gauss-Jordan 4x4 matrix inversion. Returns matrix ref or undef on failure. | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub invert_matrix ($self, $mref) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
invert_matrix: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my @m = map { [ @$_ ] } @$mref; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | my @inv = ( [1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1] ); | |||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | for my $col (0..3) { | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | my $piv = $col; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | for my $r ($col+1..3) { | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $piv = $r if abs($m[$r][$col]) > abs($m[$piv][$col]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | return undef if abs($m[$piv][$col]) < $EPSILON; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ($piv != $col) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | @m[$col,$piv] = @m[$piv,$col]; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | @inv[$col,$piv] = @inv[$piv,$col]; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $d = $m[$col][$col]; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | for my $c (0..3) { $m[$col][$c] /= $d; $inv[$col][$c] /= $d } | |||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | for my $r (0..3) { | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | next if $r == $col; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | my $f = $m[$r][$col] or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | for my $c (0..3) { | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | $m[$r][$c] -= $f * $m[$col][$c]; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | $inv[$r][$c] -= $f * $inv[$col][$c]; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | return [ map { [ @$_ ] } @inv ]; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | # Translate the 3-D CTM by (tx, ty, tz) | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub translate3 ($self, $tx, $ty, $tz = 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
translate3: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | defined $tx && defined $ty | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $tx and defined $ty
MC/DC0%0/2
defined $tx and defined $ty
defined $tx
defined $ty
Truth table0%0/3
A and B
A= defined $txB= defined $ty
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | or $self->_croak('translate3: need tx, ty [, tz]'); | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my @m = ( [1,0,0,$tx],[0,1,0,$ty],[0,0,1,$tz],[0,0,0,1] ); | |||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | $self->_g3_premul4(\@m, _g3_ctm($self)); | |||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | # Move the 3-D origin to the current 3-D position | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub translateC3 ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
translateC3: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | my ($x,$y,$z) = $self->currentpoint3(); | |||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my $m = _g3_ctm($self); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | $m->[0][3] = $x; $m->[1][3] = $y; $m->[2][3] = $z; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | $self->currentpoint3(0,0,0); | |||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | # Scale the 3-D CTM. sy and sz default to sx. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | sub scale3 ($self, $sx, $sy = undef, $sz = undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
scale3: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | defined $sx or $self->_croak('scale3: need sx [, sy [, sz]]'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | $sy //= $sx; $sz //= $sx; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$sy //= $sx
$sz //= $sx
MC/DC0%0/2
$sy //= $sx
$sy
$sx
MC/DC0%0/2
$sz //= $sx
$sz
$sx
Truth table0%0/3
A //= B
A= $syB= $sx
Truth table0%0/3
A //= B
A= $szB= $sx
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my @m = ( [$sx,0,0,0],[0,$sy,0,0],[0,0,$sz,0],[0,0,0,1] ); | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | $self->_g3_premul4(\@m, _g3_ctm($self)); | |||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | # Rotate the 3-D CTM about an arbitrary axis. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | # Usage: $g->rotate3(axis => [ax,ay,az], deg => $angle) | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | sub rotate3 ($self, %args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rotate3: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | my $axis = $args{axis} | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | or $self->_croak('rotate3: need axis=>[ax,ay,az]'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | my $deg = $args{deg} // $args{angle} | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | // $self->_croak('rotate3: need deg=>angle'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
($args{"deg"} // $args{"angle"}) // $self->_croak("rotate3: need deg=>angle")
$args{"deg"} // $args{"angle"}
MC/DC0%0/3
($args{"deg"} // $args{"angle"}) // $self->_croak("rotate3: need deg=>angle")
$args{"deg"}
$args{"angle"}
$self->_croak("rotate3: need deg=>angle")
Truth table0%0/4
A // B // C
A= $args{"deg"}B= $args{"angle"}C= $self->_croak("rotate3: need deg=>angle")
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my ($ax,$ay,$az) = @$axis; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | my $len = sqrt($ax*$ax + $ay*$ay + $az*$az); | |||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | $self->_croak('rotate3: zero-length axis') if $len < $EPSILON; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | ($ax,$ay,$az) = ($ax/$len, $ay/$len, $az/$len); | |||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | my $rad = $deg * $D2R; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | my ($c,$s,$t) = (cos($rad), sin($rad), 1 - cos($rad)); | |||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | my @r = ( | |||||||||||||||||||||||||||||||||||||||||
| 196 | [ $t*$ax*$ax+$c, $t*$ax*$ay-$s*$az, $t*$ax*$az+$s*$ay, 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 197 | [ $t*$ax*$ay+$s*$az, $t*$ay*$ay+$c, $t*$ay*$az-$s*$ax, 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 198 | [ $t*$ax*$az-$s*$ay, $t*$ay*$az+$s*$ax, $t*$az*$az+$c, 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 199 | [ 0, 0, 0, 1 ], | ||||||||||||||||||||||||||||||||||||||||||
| 200 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | $self->_g3_premul4(\@r, _g3_ctm($self)); | |||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | # Euler rotation. Order is a string like 'XYZ', 'ZYX', etc. | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | sub rotate3_euler ($self, $rx, $ry, $rz, $order = 'XYZ') { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rotate3_euler: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | my ($sx,$cx) = (sin($rx*$D2R), cos($rx*$D2R)); | |||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | my ($sy,$cy) = (sin($ry*$D2R), cos($ry*$D2R)); | |||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | my ($sz,$cz) = (sin($rz*$D2R), cos($rz*$D2R)); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | my %mat = ( | |||||||||||||||||||||||||||||||||||||||||
| 211 | X => [ [1,0,0,0],[0,$cx,-$sx,0],[0,$sx,$cx,0],[0,0,0,1] ], | ||||||||||||||||||||||||||||||||||||||||||
| 212 | Y => [ [$cy,0,$sy,0],[0,1,0,0],[-$sy,0,$cy,0],[0,0,0,1] ], | ||||||||||||||||||||||||||||||||||||||||||
| 213 | Z => [ [$cz,-$sz,0,0],[$sz,$cz,0,0],[0,0,1,0],[0,0,0,1] ], | ||||||||||||||||||||||||||||||||||||||||||
| 214 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | $self->_g3_premul4($mat{$_}, _g3_ctm($self)) for split //, $order; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 220 | # SECTION: 3-D current point | ||||||||||||||||||||||||||||||||||||||||||
| 221 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | # Get or set the 3-D current point (stored independently of the host's 2-D one) | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | sub currentpoint3 ($self, $nx = undef, $ny = undef, $nz = undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
currentpoint3: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | if (defined $nx) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | $self->{_g3_posx} = $nx; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | $self->{_g3_posy} = $ny // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$ny // 0
MC/DC0%0/1
$ny // 0
$ny
Truth table0%0/2
A
A= $ny
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | $self->{_g3_posz} = $nz // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$nz // 0
MC/DC0%0/1
$nz // 0
$nz
Truth table0%0/2
A
A= $nz
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | return ( $self->{_g3_posx} // 0, | ||||||||||||||||||||||||||||||||||||||||||
| 232 | $self->{_g3_posy} // 0, | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | $self->{_g3_posz} // 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$self->{"_g3_posx"} // 0
$self->{"_g3_posy"} // 0
$self->{"_g3_posz"} // 0
MC/DC0%0/1
$self->{"_g3_posx"} // 0
$self->{"_g3_posx"}
MC/DC0%0/1
$self->{"_g3_posy"} // 0
$self->{"_g3_posy"}
MC/DC0%0/1
$self->{"_g3_posz"} // 0
$self->{"_g3_posz"}
Truth table0%0/2
A
A= $self->{"_g3_posx"}
Truth table0%0/2
A
A= $self->{"_g3_posy"}
Truth table0%0/2
A
A= $self->{"_g3_posz"}
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 237 | # SECTION: 3-D point transformation | ||||||||||||||||||||||||||||||||||||||||||
| 238 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | # Transform a single 3-D point through the current 3-D CTM. | ||||||||||||||||||||||||||||||||||||||||||
| 241 | # Input: arrayref [x,y,z] or flat list. | ||||||||||||||||||||||||||||||||||||||||||
| 242 | # Returns: list (tx, ty, tz) with perspective divide if w != 1. | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | sub transform_point ($self, $pt_or_x, $py = undef, $pz = undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
transform_point: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | my ($px,$y,$z); | |||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | if (ref $pt_or_x eq 'ARRAY') { ($px,$y,$z) = @$pt_or_x } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | else { ($px,$y,$z) = ($pt_or_x, $py//0, $pz//0) } | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
$py // 0
$pz // 0
MC/DC0%0/1
$py // 0
$py
MC/DC0%0/1
$pz // 0
$pz
Truth table0%0/2
A
A= $py
Truth table0%0/2
A
A= $pz
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | $z //= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$z //= 0
MC/DC0%0/1
$z //= 0
$z
Truth table0%0/2
A
A= $z
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | my $m = _g3_ctm($self); | |||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | my $tx = $m->[0][0]*$px + $m->[0][1]*$y + $m->[0][2]*$z + $m->[0][3]; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | my $ty = $m->[1][0]*$px + $m->[1][1]*$y + $m->[1][2]*$z + $m->[1][3]; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | my $tz = $m->[2][0]*$px + $m->[2][1]*$y + $m->[2][2]*$z + $m->[2][3]; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | my $tw = $m->[3][0]*$px + $m->[3][1]*$y + $m->[3][2]*$z + $m->[3][3]; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | if (defined $tw && $tw != 0 && $tw != 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $tw and $tw != 0 and $tw != 1
defined $tw and $tw != 0
MC/DC0%0/3
defined $tw and $tw != 0 and $tw != 1
defined $tw
$tw != 0
$tw != 1
Truth table0%0/4
A and B and C
A= defined $twB= $tw != 0C= $tw != 1
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | return ($tx/$tw, $ty/$tw, $tz/$tw); | |||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | return ($tx, $ty, $tz); | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | # Transform an arrayref of points, returning an arrayref of [tx,ty,tz]. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | sub transform_points ($self, $pts_ref) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
transform_points: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | return [ map { [ $self->transform_point($_) ] } @$pts_ref ]; | |||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 265 | # SECTION: 3-D drawing primitives | ||||||||||||||||||||||||||||||||||||||||||
| 266 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | # Move to a 3-D point. Transforms through CTM3, calls host's 2-D move hooks. | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | ▶ | sub moveto3 ($self, $x, $y, $z = 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
moveto3: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | my ($px,$py) = ($self->transform_point([$x,$y,$z]))[0,1]; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | $self->penup(); | |||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | $self->_genfastmove($px,$py); | |||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | $self->pendown(); | |||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | $self->currentpoint3($x,$y,$z); | |||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | # Relative moveto3 from current 3-D position | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | sub movetoR3 ($self, $dx, $dy, $dz = 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
movetoR3: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | my ($cx,$cy,$cz) = $self->currentpoint3(); | |||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | return $self->moveto3($cx+$dx, $cy+$dy, $cz+$dz); | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | # Draw a line in 3-D space. | ||||||||||||||||||||||||||||||||||||||||||
| 285 | # 6-arg form: line3(x1,y1,z1, x2,y2,z2) - moves to start, draws to end | ||||||||||||||||||||||||||||||||||||||||||
| 286 | # 3-arg form: line3(x,y,z) - pen assumed down, draws from current | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | sub line3 ($self, $x1, $y1, $z1 = 0, $x2 = undef, $y2 = undef, $z2 = undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
line3: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if (defined $x2 && defined $y2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $x2 and defined $y2
MC/DC0%0/2
defined $x2 and defined $y2
defined $x2
defined $y2
Truth table0%0/3
A and B
A= defined $x2B= defined $y2
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | $self->moveto3($x1,$y1,$z1); | |||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | ▶ | my ($px,$py) = ($self->transform_point([$x2,$y2,$z2//0]))[0,1]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$z2 // 0
MC/DC0%0/1
$z2 // 0
$z2
Truth table0%0/2
A
A= $z2
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | $self->_genslowmove($px,$py); | |||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | $self->currentpoint3($x2,$y2,$z2//0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$z2 // 0
MC/DC0%0/1
$z2 // 0
$z2
Truth table0%0/2
A
A= $z2
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | my ($px,$py) = ($self->transform_point([$x1,$y1,$z1]))[0,1]; | |||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | $self->_genslowmove($px,$py); | |||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | $self->currentpoint3($x1,$y1,$z1); | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | # Relative line3 from current 3-D position | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | sub lineR3 ($self, $dx, $dy, $dz = 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lineR3: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | my ($cx,$cy,$cz) = $self->currentpoint3(); | |||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | return $self->line3($cx+$dx, $cy+$dy, $cz+$dz); | |||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | # Open polygon: move to first point, draw to remaining points. | ||||||||||||||||||||||||||||||||||||||||||
| 308 | # Coordinates: flat list of triples (x1,y1,z1, x2,y2,z2, ...) | ||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | sub polygon3 ($self, @coords) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
polygon3: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | @coords >= 6 or $self->_croak('polygon3: need at least 6 numbers'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | @coords % 3 == 0 or $self->_croak('polygon3: coordinate count must be a multiple of 3'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | my ($x,$y,$z) = splice @coords, 0, 3; | |||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | $self->moveto3($x,$y,$z); | |||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | while (@coords) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | ($x,$y,$z) = splice @coords, 0, 3; | |||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | my ($px,$py) = ($self->transform_point([$x,$y,$z]))[0,1]; | |||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | $self->_genslowmove($px,$py); | |||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | $self->currentpoint3($x,$y,$z); | |||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | # Closed polygon: like polygon3 but auto-closes back to the first point. | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | sub polygon3C ($self, @coords) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
polygon3C: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | @coords >= 6 or $self->_croak('polygon3C: need at least 6 numbers'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | @coords % 3 == 0 or $self->_croak('polygon3C: coordinate count must be a multiple of 3'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | my ($x0,$y0,$z0) = @coords[0,1,2]; | |||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | $self->polygon3(@coords); | |||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | my ($px,$py) = ($self->transform_point([$x0,$y0,$z0]))[0,1]; | |||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | $self->_genslowmove($px,$py); | |||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | $self->currentpoint3($x0,$y0,$z0); | |||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | # Relative polygon: like polygon3 but each triple is relative to the current point. | ||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | sub polygon3R ($self, @deltas) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
polygon3R: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | @deltas >= 3 or $self->_croak('polygon3R: need at least 3 numbers'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | @deltas % 3 == 0 or $self->_croak('polygon3R: coordinate count must be a multiple of 3'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | while (@deltas) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | my ($cx,$cy,$cz) = $self->currentpoint3(); | |||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | my ($dx,$dy,$dz) = splice @deltas, 0, 3; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | $self->line3($cx+$dx, $cy+$dy, $cz+$dz); | |||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 348 | # SECTION: 3-D mesh constructor and solid primitives | ||||||||||||||||||||||||||||||||||||||||||
| 349 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | # Construct a mesh data structure { verts => \@v, faces => \@f } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | ▶ | sub mesh ($self, $verts_ref, $faces_ref) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mesh: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | ▶ | return { verts => $verts_ref // [], faces => $faces_ref // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
$verts_ref // []
$faces_ref // []
MC/DC0%0/1
$verts_ref // []
$verts_ref
MC/DC0%0/1
$faces_ref // []
$faces_ref
Truth table0%0/2
A
A= $verts_ref
Truth table0%0/2
A
A= $faces_ref
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 357 | # Wireframe drawing primitives (draw directly, return 1) | ||||||||||||||||||||||||||||||||||||||||||
| 358 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | # Wireframe axis-aligned box | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | sub box3 ($self, $x1,$y1,$z1,$x2,$y2,$z2) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
box3: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | $self->polygon3C($x1,$y1,$z1, $x2,$y1,$z1, $x2,$y2,$z1, $x1,$y2,$z1); # bottom face | |||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | $self->polygon3C($x1,$y1,$z2, $x2,$y1,$z2, $x2,$y2,$z2, $x1,$y2,$z2); # top face | |||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | $self->line3($x1,$y1,$z1,$x1,$y1,$z2); # vertical edges | |||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | $self->line3($x2,$y1,$z1,$x2,$y1,$z2); | |||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | $self->line3($x2,$y2,$z1,$x2,$y2,$z2); | |||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | $self->line3($x1,$y2,$z1,$x1,$y2,$z2); | |||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | # Wireframe cube centred at (cx,cy,cz) | ||||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | ▶ | sub cube ($self, $cx,$cy,$cz,$side) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cube: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | my $h = $side / 2.0; | |||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | return $self->box3($cx-$h,$cy-$h,$cz-$h, $cx+$h,$cy+$h,$cz+$h); | |||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | # Three-axis wireframe gizmo. Draws X, Y, Z axes as lines with arrow cones. | ||||||||||||||||||||||||||||||||||||||||||
| 378 | # $len is the full axis length (body + arrowhead). | ||||||||||||||||||||||||||||||||||||||||||
| 379 | # $cone_r / $cone_h default to 5% / 15% of $len. | ||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | sub axis_gizmo ($self, $cx,$cy,$cz,$len=1,$cone_r=undef,$cone_h=undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
axis_gizmo: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | $cone_r //= $len * 0.05; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$cone_r //= $len * "0.05"
MC/DC0%0/1
$cone_r //= $len * "0.05"
$cone_r
Truth table0%0/2
A
A= $cone_r
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | ▶ | $cone_h //= $len * 0.15; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$cone_h //= $len * "0.15"
MC/DC0%0/1
$cone_h //= $len * "0.15"
$cone_h
Truth table0%0/2
A
A= $cone_h
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | my $body = $len - $cone_h; | |||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | my $cseg = 8; | |||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | # Draw one axis: direction (dx,dy,dz), radial basis r1 and r2 | ||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | my $draw_arrow = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | my ($dx,$dy,$dz,$r1x,$r1y,$r1z,$r2x,$r2y,$r2z) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 389 | # Axis body | ||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | $self->line3($cx, $cy, $cz, | |||||||||||||||||||||||||||||||||||||||||
| 391 | $cx+$dx*$body, $cy+$dy*$body, $cz+$dz*$body); | ||||||||||||||||||||||||||||||||||||||||||
| 392 | # Cone base ring | ||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | my @ring; | |||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | for my $i (0..$cseg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | my $t = 2*$PI*$i/$cseg; | |||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | my $ca = $cone_r * cos($t); | |||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | my $sa = $cone_r * sin($t); | |||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | push @ring, | |||||||||||||||||||||||||||||||||||||||||
| 399 | $cx + $dx*$body + $r1x*$ca + $r2x*$sa, | ||||||||||||||||||||||||||||||||||||||||||
| 400 | $cy + $dy*$body + $r1y*$ca + $r2y*$sa, | ||||||||||||||||||||||||||||||||||||||||||
| 401 | $cz + $dz*$body + $r1z*$ca + $r2z*$sa; | ||||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | $self->polygon3C(@ring); | |||||||||||||||||||||||||||||||||||||||||
| 404 | # Cone lines from ring to tip | ||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | my ($tipx,$tipy,$tipz) = ($cx+$dx*$len, $cy+$dy*$len, $cz+$dz*$len); | |||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | for my $i (0..$cseg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | $self->line3(@ring[$i*3..$i*3+2], $tipx,$tipy,$tipz); | |||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | $draw_arrow->(1,0,0, 0,1,0, 0,0,1); # X axis, radials in Y/Z | |||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | $draw_arrow->(0,1,0, 1,0,0, 0,0,1); # Y axis, radials in X/Z | |||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | $draw_arrow->(0,0,1, 1,0,0, 0,1,0); # Z axis, radials in X/Y | |||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 418 | # Mesh-returning solid primitives (all return { verts=>\@v, faces=>\@f }) | ||||||||||||||||||||||||||||||||||||||||||
| 419 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | # Rectangular prism (box) mesh centred at (cx,cy,cz), dimensions w x h x d. | ||||||||||||||||||||||||||||||||||||||||||
| 422 | # Covers the cube as a special case (w == h == d). | ||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | ▶ | sub prism ($self, $cx,$cy,$cz,$w,$h,$d) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prism: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | my ($hw,$hh,$hd) = ($w/2.0, $h/2.0, $d/2.0); | |||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | my @v = ( | |||||||||||||||||||||||||||||||||||||||||
| 426 | [$cx-$hw, $cy-$hh, $cz-$hd], # 0: left bottom front | ||||||||||||||||||||||||||||||||||||||||||
| 427 | [$cx+$hw, $cy-$hh, $cz-$hd], # 1: right bottom front | ||||||||||||||||||||||||||||||||||||||||||
| 428 | [$cx+$hw, $cy+$hh, $cz-$hd], # 2: right top front | ||||||||||||||||||||||||||||||||||||||||||
| 429 | [$cx-$hw, $cy+$hh, $cz-$hd], # 3: left top front | ||||||||||||||||||||||||||||||||||||||||||
| 430 | [$cx-$hw, $cy-$hh, $cz+$hd], # 4: left bottom back | ||||||||||||||||||||||||||||||||||||||||||
| 431 | [$cx+$hw, $cy-$hh, $cz+$hd], # 5: right bottom back | ||||||||||||||||||||||||||||||||||||||||||
| 432 | [$cx+$hw, $cy+$hh, $cz+$hd], # 6: right top back | ||||||||||||||||||||||||||||||||||||||||||
| 433 | [$cx-$hw, $cy+$hh, $cz+$hd], # 7: left top back | ||||||||||||||||||||||||||||||||||||||||||
| 434 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | my @f = ( | |||||||||||||||||||||||||||||||||||||||||
| 436 | [0,1,2],[0,2,3], # front (-z) | ||||||||||||||||||||||||||||||||||||||||||
| 437 | [5,4,7],[5,7,6], # back (+z) | ||||||||||||||||||||||||||||||||||||||||||
| 438 | [4,0,3],[4,3,7], # left (-x) | ||||||||||||||||||||||||||||||||||||||||||
| 439 | [1,5,6],[1,6,2], # right (+x) | ||||||||||||||||||||||||||||||||||||||||||
| 440 | [4,5,1],[4,1,0], # bottom (-y) | ||||||||||||||||||||||||||||||||||||||||||
| 441 | [3,2,6],[3,6,7], # top (+y) | ||||||||||||||||||||||||||||||||||||||||||
| 442 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | return $self->mesh(\@v, \@f); | |||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | # UV-sphere tessellation mesh. lat x lon quads, triangulated. | ||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | ▶ | sub sphere ($self, $cx,$cy,$cz,$r,$lat=12,$lon=24) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sphere: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | my @verts; | |||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | for my $i (0..$lat) { | |||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | my $phi = $PI * $i / $lat; # 0 .. pi | |||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | for my $j (0..$lon-1) { | |||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | my $theta = 2*$PI * $j / $lon; # 0 .. 2*pi | |||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | push @verts, [ | |||||||||||||||||||||||||||||||||||||||||
| 454 | $cx + $r * sin($phi) * cos($theta), | ||||||||||||||||||||||||||||||||||||||||||
| 455 | $cy + $r * sin($phi) * sin($theta), | ||||||||||||||||||||||||||||||||||||||||||
| 456 | $cz + $r * cos($phi), | ||||||||||||||||||||||||||||||||||||||||||
| 457 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 460 | 0 | my @faces; | |||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | for my $i (0..$lat-1) { | |||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | for my $j (0..$lon-1) { | |||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | my $p0 = $i * $lon + $j; | |||||||||||||||||||||||||||||||||||||||||
| 464 | 0 | my $p1 = ($i+1) * $lon + $j; | |||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | my $p2 = $i * $lon + ($j+1) % $lon; | |||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | my $p3 = ($i+1) * $lon + ($j+1) % $lon; | |||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | ▶ | push @faces, [$p0,$p1,$p2] if $i != 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | ▶ | push @faces, [$p2,$p1,$p3] if $i != $lat-1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | return $self->mesh(\@verts, \@faces); | |||||||||||||||||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | # Icosphere mesh. Starts from a regular icosahedron and subdivides each | ||||||||||||||||||||||||||||||||||||||||||
| 475 | # triangle into four by midpoint bisection, then projects back onto the sphere. | ||||||||||||||||||||||||||||||||||||||||||
| 476 | # $subdivisions defaults to 2, giving 320 faces. | ||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | ▶ | sub icosphere ($self, $cx,$cy,$cz,$r,$subdivisions=2) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
icosphere: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | my $phi = (1 + sqrt(5)) / 2; # golden ratio | |||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | # 12 icosahedron vertices (normalised to unit sphere) | ||||||||||||||||||||||||||||||||||||||||||
| 481 | my @v = map { | ||||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | my $l = sqrt($_->[0]**2+$_->[1]**2+$_->[2]**2); | |||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | [ map { $_/$l } @$_ ] | |||||||||||||||||||||||||||||||||||||||||
| 484 | } ( | ||||||||||||||||||||||||||||||||||||||||||
| 485 | [-1, $phi, 0], [ 1, $phi, 0], [-1, -$phi, 0], [ 1, -$phi, 0], | ||||||||||||||||||||||||||||||||||||||||||
| 486 | [ 0, -1, $phi], [ 0, 1, $phi], [ 0, -1, -$phi], [ 0, 1, -$phi], | ||||||||||||||||||||||||||||||||||||||||||
| 487 | [ $phi, 0, -1], [ $phi, 0, 1], [-$phi, 0, -1], [-$phi, 0, 1], | ||||||||||||||||||||||||||||||||||||||||||
| 488 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | # 20 icosahedron faces | ||||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | my @f = ( | |||||||||||||||||||||||||||||||||||||||||
| 492 | [0,11,5],[0,5,1],[0,1,7],[0,7,10],[0,10,11], | ||||||||||||||||||||||||||||||||||||||||||
| 493 | [1,5,9],[5,11,4],[11,10,2],[10,7,6],[7,1,8], | ||||||||||||||||||||||||||||||||||||||||||
| 494 | [3,9,4],[3,4,2],[3,2,6],[3,6,8],[3,8,9], | ||||||||||||||||||||||||||||||||||||||||||
| 495 | [4,9,5],[2,4,11],[6,2,10],[8,6,7],[9,8,1], | ||||||||||||||||||||||||||||||||||||||||||
| 496 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | # Midpoint cache: maps "$i,$j" => vertex index | ||||||||||||||||||||||||||||||||||||||||||
| 499 | 0 | my %mid; | |||||||||||||||||||||||||||||||||||||||||
| 500 | 0 | ▶ | my $midpoint = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 0 | my ($a,$b) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | ▶ | my $key = $a < $b ? "$a,$b" : "$b,$a"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | ▶ | unless (exists $mid{$key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | my @m = map { ($v[$a][$_] + $v[$b][$_]) / 2 } 0..2; | |||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | ▶ | my $l = sqrt($m[0]**2+$m[1]**2+$m[2]**2) || 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
sqrt $m[0] ** 2 + $m[1] ** 2 + $m[2] ** 2 || 1
MC/DC0%0/1
sqrt $m[0] ** 2 + $m[1] ** 2 + $m[2] ** 2 || 1
sqrt $m[0] ** 2 + $m[1] ** 2 + $m[2] ** 2
Truth table0%0/2
A
A= sqrt $m[0] ** 2 + $m[1] ** 2 + $m[2] ** 2
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | 0 | push @v, [map{$_/$l}@m]; | |||||||||||||||||||||||||||||||||||||||||
| 507 | 0 | $mid{$key} = $#v; | |||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||
| 509 | 0 | return $mid{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | 0 | for (1..$subdivisions) { | |||||||||||||||||||||||||||||||||||||||||
| 513 | 0 | my @nf; | |||||||||||||||||||||||||||||||||||||||||
| 514 | 0 | for my $face (@f) { | |||||||||||||||||||||||||||||||||||||||||
| 515 | 0 | my ($a,$b,$c) = @$face; | |||||||||||||||||||||||||||||||||||||||||
| 516 | 0 | my $ab = $midpoint->($a,$b); | |||||||||||||||||||||||||||||||||||||||||
| 517 | 0 | my $bc = $midpoint->($b,$c); | |||||||||||||||||||||||||||||||||||||||||
| 518 | 0 | my $ca = $midpoint->($c,$a); | |||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | push @nf, [$a,$ab,$ca], [$b,$bc,$ab], [$c,$ca,$bc], [$ab,$bc,$ca]; | |||||||||||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||||||||||||
| 521 | 0 | @f = @nf; | |||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | 0 | my @verts = map { [$cx+$r*$_->[0], $cy+$r*$_->[1], $cz+$r*$_->[2]] } @v; | |||||||||||||||||||||||||||||||||||||||||
| 525 | 0 | return $self->mesh(\@verts, \@f); | |||||||||||||||||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | # Cylinder mesh. $base_ref and $top_ref are [x,y,z] centre points. | ||||||||||||||||||||||||||||||||||||||||||
| 529 | # Side walls only (no end caps). | ||||||||||||||||||||||||||||||||||||||||||
| 530 | 0 | ▶ | sub cylinder ($self, $base_ref, $top_ref, $r, $seg=24) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cylinder: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | my ($bx,$by,$bz) = @$base_ref; | |||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | my ($tx,$ty,$tz) = @$top_ref; | |||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | my @verts; | |||||||||||||||||||||||||||||||||||||||||
| 534 | 0 | for my $i (0..$seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 535 | 0 | my $theta = 2*$PI*$i/$seg; | |||||||||||||||||||||||||||||||||||||||||
| 536 | 0 | push @verts, [$bx + $r*cos($theta), $by + $r*sin($theta), $bz]; | |||||||||||||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||||||||||||
| 538 | 0 | for my $i (0..$seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 539 | 0 | my $theta = 2*$PI*$i/$seg; | |||||||||||||||||||||||||||||||||||||||||
| 540 | 0 | push @verts, [$tx + $r*cos($theta), $ty + $r*sin($theta), $tz]; | |||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||
| 542 | 0 | my @faces; | |||||||||||||||||||||||||||||||||||||||||
| 543 | 0 | for my $i (0..$seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | my $n = ($i+1) % $seg; | |||||||||||||||||||||||||||||||||||||||||
| 545 | 0 | push @faces, [$i, $seg+$i, $seg+$n]; | |||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | push @faces, [$i, $seg+$n, $n]; | |||||||||||||||||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||||||||||||||||
| 548 | 0 | return $self->mesh(\@verts, \@faces); | |||||||||||||||||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | # General frustum (truncated cone) mesh centred at (cx,cy,cz). | ||||||||||||||||||||||||||||||||||||||||||
| 552 | # $r_bot: bottom ring radius, $r_top: top ring radius, $height: total height. | ||||||||||||||||||||||||||||||||||||||||||
| 553 | # When $r_top == 0 this is a cone; when $r_bot == $r_top it is a cylinder. | ||||||||||||||||||||||||||||||||||||||||||
| 554 | # Both end caps are included in the mesh. | ||||||||||||||||||||||||||||||||||||||||||
| 555 | # Vertex layout: | ||||||||||||||||||||||||||||||||||||||||||
| 556 | # [0..$seg-1] : bottom ring | ||||||||||||||||||||||||||||||||||||||||||
| 557 | # [$seg] : bottom centre | ||||||||||||||||||||||||||||||||||||||||||
| 558 | # [$seg+1..$seg*2] : top ring | ||||||||||||||||||||||||||||||||||||||||||
| 559 | # [$seg*2+1] : top centre | ||||||||||||||||||||||||||||||||||||||||||
| 560 | 0 | ▶ | sub frustum ($self, $cx,$cy,$cz,$r_bot,$r_top,$height,$seg=24) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
frustum: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 561 | 0 | my $hz = $height / 2.0; | |||||||||||||||||||||||||||||||||||||||||
| 562 | 0 | my @v; | |||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | # Bottom ring | ||||||||||||||||||||||||||||||||||||||||||
| 565 | 0 | for my $i (0..$seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 566 | 0 | my $t = 2*$PI*$i/$seg; | |||||||||||||||||||||||||||||||||||||||||
| 567 | 0 | push @v, [$cx + $r_bot*cos($t), $cy + $r_bot*sin($t), $cz - $hz]; | |||||||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||||||
| 569 | 0 | my $bc = scalar @v; # bottom centre index | |||||||||||||||||||||||||||||||||||||||||
| 570 | 0 | push @v, [$cx, $cy, $cz - $hz]; | |||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | # Top ring | ||||||||||||||||||||||||||||||||||||||||||
| 573 | 0 | for my $i (0..$seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 574 | 0 | my $t = 2*$PI*$i/$seg; | |||||||||||||||||||||||||||||||||||||||||
| 575 | 0 | push @v, [$cx + $r_top*cos($t), $cy + $r_top*sin($t), $cz + $hz]; | |||||||||||||||||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||||||||||||||||
| 577 | 0 | my $tc = scalar @v; # top centre index | |||||||||||||||||||||||||||||||||||||||||
| 578 | 0 | push @v, [$cx, $cy, $cz + $hz]; | |||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||
| 580 | 0 | my @f; | |||||||||||||||||||||||||||||||||||||||||
| 581 | 0 | for my $i (0..$seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 582 | 0 | my $n = ($i+1) % $seg; | |||||||||||||||||||||||||||||||||||||||||
| 583 | 0 | my $ti = $seg + 1 + $i; # top ring index for i | |||||||||||||||||||||||||||||||||||||||||
| 584 | 0 | my $tn = $seg + 1 + $n; # top ring index for n | |||||||||||||||||||||||||||||||||||||||||
| 585 | # Side quad as two triangles | ||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | push @f, [$i, $ti, $tn]; | |||||||||||||||||||||||||||||||||||||||||
| 587 | 0 | push @f, [$i, $tn, $n ]; | |||||||||||||||||||||||||||||||||||||||||
| 588 | # Bottom cap (fan, winding outward / facing -Z) | ||||||||||||||||||||||||||||||||||||||||||
| 589 | 0 | push @f, [$bc, $n, $i ]; | |||||||||||||||||||||||||||||||||||||||||
| 590 | # Top cap (fan, winding outward / facing +Z) | ||||||||||||||||||||||||||||||||||||||||||
| 591 | 0 | push @f, [$tc, $ti, $tn]; | |||||||||||||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||||||||||||
| 593 | 0 | return $self->mesh(\@v, \@f); | |||||||||||||||||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | # Cone mesh: convenience wrapper for frustum with $r_top = 0. | ||||||||||||||||||||||||||||||||||||||||||
| 597 | 0 | ▶ | sub cone ($self, $cx,$cy,$cz,$r,$height,$seg=24) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cone: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 598 | 0 | return $self->frustum($cx,$cy,$cz,$r,0,$height,$seg); | |||||||||||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | # Capsule mesh: a cylinder capped with hemispherical ends. | ||||||||||||||||||||||||||||||||||||||||||
| 602 | # (cx,cy,cz) is the geometric centre. | ||||||||||||||||||||||||||||||||||||||||||
| 603 | # $height is the length of the cylindrical body (not counting the caps). | ||||||||||||||||||||||||||||||||||||||||||
| 604 | # $seg_r: longitudinal (radial) segments; $seg_h: latitudinal segments per hemisphere. | ||||||||||||||||||||||||||||||||||||||||||
| 605 | 0 | ▶ | sub capsule ($self, $cx,$cy,$cz,$r,$height,$seg_r=16,$seg_h=8) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
capsule: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 606 | 0 | my $hz = $height / 2.0; | |||||||||||||||||||||||||||||||||||||||||
| 607 | 0 | my @v; | |||||||||||||||||||||||||||||||||||||||||
| 608 | my @f; | ||||||||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||||||||
| 610 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 611 | # Top hemisphere: phi from 0 (pole) to pi/2 (equator), body offset +hz | ||||||||||||||||||||||||||||||||||||||||||
| 612 | # Ring layout: pole index 0, then seg_h rings of seg_r vertices each | ||||||||||||||||||||||||||||||||||||||||||
| 613 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 614 | 0 | push @v, [$cx, $cy, $cz + $hz + $r]; # top pole, index 0 | |||||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | for my $i (1..$seg_h) { | |||||||||||||||||||||||||||||||||||||||||
| 617 | 0 | my $phi = ($PI / 2) * $i / $seg_h; # 0 < phi <= pi/2 | |||||||||||||||||||||||||||||||||||||||||
| 618 | 0 | for my $j (0..$seg_r-1) { | |||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | my $theta = 2*$PI*$j/$seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 620 | 0 | push @v, [ | |||||||||||||||||||||||||||||||||||||||||
| 621 | $cx + $r * sin($phi) * cos($theta), | ||||||||||||||||||||||||||||||||||||||||||
| 622 | $cy + $r * sin($phi) * sin($theta), | ||||||||||||||||||||||||||||||||||||||||||
| 623 | $cz + $hz + $r * cos($phi), | ||||||||||||||||||||||||||||||||||||||||||
| 624 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||||||||||||||||
| 627 | # top_equator_start: first vertex index of the equator ring (last top-hem ring) | ||||||||||||||||||||||||||||||||||||||||||
| 628 | 0 | my $top_eq = 1 + ($seg_h-1)*$seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||
| 630 | # Pole fan | ||||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | for my $j (0..$seg_r-1) { | |||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | my $n = ($j+1) % $seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 633 | 0 | push @f, [0, 1+$j, 1+$n]; | |||||||||||||||||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||||||||||||||||
| 635 | # Hemisphere bands | ||||||||||||||||||||||||||||||||||||||||||
| 636 | 0 | for my $i (1..$seg_h-1) { | |||||||||||||||||||||||||||||||||||||||||
| 637 | 0 | my $ra = 1 + ($i-1)*$seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 638 | 0 | my $rb = 1 + $i *$seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 639 | 0 | for my $j (0..$seg_r-1) { | |||||||||||||||||||||||||||||||||||||||||
| 640 | 0 | my $n = ($j+1) % $seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 641 | 0 | push @f, [$ra+$j, $rb+$j, $rb+$n]; | |||||||||||||||||||||||||||||||||||||||||
| 642 | 0 | push @f, [$ra+$j, $rb+$n, $ra+$n]; | |||||||||||||||||||||||||||||||||||||||||
| 643 | } | ||||||||||||||||||||||||||||||||||||||||||
| 644 | } | ||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||
| 646 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 647 | # Cylinder body: equator-top to equator-bottom | ||||||||||||||||||||||||||||||||||||||||||
| 648 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 649 | 0 | my $bot_eq = scalar @v; # start index of bottom equator ring | |||||||||||||||||||||||||||||||||||||||||
| 650 | 0 | for my $j (0..$seg_r-1) { | |||||||||||||||||||||||||||||||||||||||||
| 651 | 0 | my $theta = 2*$PI*$j/$seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 652 | 0 | push @v, [ | |||||||||||||||||||||||||||||||||||||||||
| 653 | $cx + $r * cos($theta), | ||||||||||||||||||||||||||||||||||||||||||
| 654 | $cy + $r * sin($theta), | ||||||||||||||||||||||||||||||||||||||||||
| 655 | $cz - $hz, | ||||||||||||||||||||||||||||||||||||||||||
| 656 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||||||||||||||||
| 658 | 0 | for my $j (0..$seg_r-1) { | |||||||||||||||||||||||||||||||||||||||||
| 659 | 0 | my $n = ($j+1) % $seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 660 | 0 | push @f, [$top_eq+$j, $bot_eq+$j, $bot_eq+$n]; | |||||||||||||||||||||||||||||||||||||||||
| 661 | 0 | push @f, [$top_eq+$j, $bot_eq+$n, $top_eq+$n]; | |||||||||||||||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||
| 664 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 665 | # Bottom hemisphere: phi from pi/2 (equator) to pi (pole), body offset -hz | ||||||||||||||||||||||||||||||||||||||||||
| 666 | # seg_h-1 additional rings, then the pole | ||||||||||||||||||||||||||||||||||||||||||
| 667 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 668 | 0 | my $bot_hem_start = scalar @v; | |||||||||||||||||||||||||||||||||||||||||
| 669 | 0 | for my $i (1..$seg_h-1) { | |||||||||||||||||||||||||||||||||||||||||
| 670 | 0 | my $phi = ($PI/2) + ($PI/2) * $i / $seg_h; | |||||||||||||||||||||||||||||||||||||||||
| 671 | 0 | for my $j (0..$seg_r-1) { | |||||||||||||||||||||||||||||||||||||||||
| 672 | 0 | my $theta = 2*$PI*$j/$seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 673 | 0 | push @v, [ | |||||||||||||||||||||||||||||||||||||||||
| 674 | $cx + $r * sin($phi) * cos($theta), | ||||||||||||||||||||||||||||||||||||||||||
| 675 | $cy + $r * sin($phi) * sin($theta), | ||||||||||||||||||||||||||||||||||||||||||
| 676 | $cz - $hz + $r * cos($phi), | ||||||||||||||||||||||||||||||||||||||||||
| 677 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||||||||||||||||
| 679 | } | ||||||||||||||||||||||||||||||||||||||||||
| 680 | 0 | push @v, [$cx, $cy, $cz - $hz - $r]; # bottom pole | |||||||||||||||||||||||||||||||||||||||||
| 681 | 0 | my $bot_pole = $#v; | |||||||||||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||||||||||||
| 683 | # Connect body bottom equator to first bottom-hemisphere ring (or directly to pole) | ||||||||||||||||||||||||||||||||||||||||||
| 684 | 0 | ▶ | if ($seg_h > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 685 | 0 | my $ra = $bot_eq; | |||||||||||||||||||||||||||||||||||||||||
| 686 | 0 | my $rb = $bot_hem_start; | |||||||||||||||||||||||||||||||||||||||||
| 687 | 0 | for my $j (0..$seg_r-1) { | |||||||||||||||||||||||||||||||||||||||||
| 688 | 0 | my $n = ($j+1) % $seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 689 | 0 | push @f, [$ra+$j, $rb+$j, $rb+$n]; | |||||||||||||||||||||||||||||||||||||||||
| 690 | 0 | push @f, [$ra+$j, $rb+$n, $ra+$n]; | |||||||||||||||||||||||||||||||||||||||||
| 691 | } | ||||||||||||||||||||||||||||||||||||||||||
| 692 | 0 | for my $i (1..$seg_h-2) { | |||||||||||||||||||||||||||||||||||||||||
| 693 | 0 | my $ra = $bot_hem_start + ($i-1)*$seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 694 | 0 | my $rb = $bot_hem_start + $i *$seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 695 | 0 | for my $j (0..$seg_r-1) { | |||||||||||||||||||||||||||||||||||||||||
| 696 | 0 | my $n = ($j+1) % $seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 697 | 0 | push @f, [$ra+$j, $rb+$j, $rb+$n]; | |||||||||||||||||||||||||||||||||||||||||
| 698 | 0 | push @f, [$ra+$j, $rb+$n, $ra+$n]; | |||||||||||||||||||||||||||||||||||||||||
| 699 | } | ||||||||||||||||||||||||||||||||||||||||||
| 700 | } | ||||||||||||||||||||||||||||||||||||||||||
| 701 | # Fan to bottom pole from last bottom-hemisphere ring | ||||||||||||||||||||||||||||||||||||||||||
| 702 | 0 | my $last_ring = $bot_hem_start + ($seg_h-2)*$seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 703 | 0 | for my $j (0..$seg_r-1) { | |||||||||||||||||||||||||||||||||||||||||
| 704 | 0 | my $n = ($j+1) % $seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 705 | 0 | push @f, [$bot_pole, $last_ring+$n, $last_ring+$j]; | |||||||||||||||||||||||||||||||||||||||||
| 706 | } | ||||||||||||||||||||||||||||||||||||||||||
| 707 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 708 | # Only one hemisphere ring (the equator itself) -- fan directly to pole | ||||||||||||||||||||||||||||||||||||||||||
| 709 | 0 | for my $j (0..$seg_r-1) { | |||||||||||||||||||||||||||||||||||||||||
| 710 | 0 | my $n = ($j+1) % $seg_r; | |||||||||||||||||||||||||||||||||||||||||
| 711 | 0 | push @f, [$bot_pole, $bot_eq+$n, $bot_eq+$j]; | |||||||||||||||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||||||||||||||||
| 713 | } | ||||||||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||||||||
| 715 | 0 | return $self->mesh(\@v, \@f); | |||||||||||||||||||||||||||||||||||||||||
| 716 | } | ||||||||||||||||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||||||||||||||||
| 718 | # Flat rectangular plane mesh in the XY plane centred at (cx,cy,cz). | ||||||||||||||||||||||||||||||||||||||||||
| 719 | # $w / $h: width / height; $segs_w / $segs_h: subdivision counts. | ||||||||||||||||||||||||||||||||||||||||||
| 720 | 0 | ▶ | sub plane ($self, $cx,$cy,$cz,$w,$h,$segs_w=1,$segs_h=1) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
plane: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 721 | 0 | ▶ | $segs_w >= 1 or $self->_croak('plane: segs_w must be >= 1'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 722 | 0 | ▶ | $segs_h >= 1 or $self->_croak('plane: segs_h must be >= 1'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 723 | 0 | my ($hw,$hh) = ($w/2.0, $h/2.0); | |||||||||||||||||||||||||||||||||||||||||
| 724 | 0 | my @v; | |||||||||||||||||||||||||||||||||||||||||
| 725 | 0 | for my $j (0..$segs_h) { | |||||||||||||||||||||||||||||||||||||||||
| 726 | 0 | my $y = $cy - $hh + $h * $j / $segs_h; | |||||||||||||||||||||||||||||||||||||||||
| 727 | 0 | for my $i (0..$segs_w) { | |||||||||||||||||||||||||||||||||||||||||
| 728 | 0 | my $x = $cx - $hw + $w * $i / $segs_w; | |||||||||||||||||||||||||||||||||||||||||
| 729 | 0 | push @v, [$x, $y, $cz]; | |||||||||||||||||||||||||||||||||||||||||
| 730 | } | ||||||||||||||||||||||||||||||||||||||||||
| 731 | } | ||||||||||||||||||||||||||||||||||||||||||
| 732 | 0 | my @f; | |||||||||||||||||||||||||||||||||||||||||
| 733 | 0 | my $stride = $segs_w + 1; | |||||||||||||||||||||||||||||||||||||||||
| 734 | 0 | for my $j (0..$segs_h-1) { | |||||||||||||||||||||||||||||||||||||||||
| 735 | 0 | for my $i (0..$segs_w-1) { | |||||||||||||||||||||||||||||||||||||||||
| 736 | 0 | my $tl = $j * $stride + $i; | |||||||||||||||||||||||||||||||||||||||||
| 737 | 0 | my $tr = $tl + 1; | |||||||||||||||||||||||||||||||||||||||||
| 738 | 0 | my $bl = $tl + $stride; | |||||||||||||||||||||||||||||||||||||||||
| 739 | 0 | my $br = $bl + 1; | |||||||||||||||||||||||||||||||||||||||||
| 740 | 0 | push @f, [$tl, $bl, $br]; | |||||||||||||||||||||||||||||||||||||||||
| 741 | 0 | push @f, [$tl, $br, $tr]; | |||||||||||||||||||||||||||||||||||||||||
| 742 | } | ||||||||||||||||||||||||||||||||||||||||||
| 743 | } | ||||||||||||||||||||||||||||||||||||||||||
| 744 | 0 | return $self->mesh(\@v, \@f); | |||||||||||||||||||||||||||||||||||||||||
| 745 | } | ||||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||
| 747 | # Torus mesh in the XY plane centred at (cx,cy,cz). | ||||||||||||||||||||||||||||||||||||||||||
| 748 | # $R: major radius (torus centre to tube centre). | ||||||||||||||||||||||||||||||||||||||||||
| 749 | # $r: minor radius (tube radius). | ||||||||||||||||||||||||||||||||||||||||||
| 750 | # $maj_seg / $min_seg: major / minor segment counts. | ||||||||||||||||||||||||||||||||||||||||||
| 751 | 0 | ▶ | sub torus ($self, $cx,$cy,$cz,$R,$r,$maj_seg=24,$min_seg=12) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
torus: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 752 | 0 | my @v; | |||||||||||||||||||||||||||||||||||||||||
| 753 | 0 | for my $i (0..$maj_seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 754 | 0 | my $theta = 2*$PI*$i/$maj_seg; | |||||||||||||||||||||||||||||||||||||||||
| 755 | 0 | for my $j (0..$min_seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 756 | 0 | my $phi = 2*$PI*$j/$min_seg; | |||||||||||||||||||||||||||||||||||||||||
| 757 | 0 | push @v, [ | |||||||||||||||||||||||||||||||||||||||||
| 758 | $cx + ($R + $r*cos($phi)) * cos($theta), | ||||||||||||||||||||||||||||||||||||||||||
| 759 | $cy + ($R + $r*cos($phi)) * sin($theta), | ||||||||||||||||||||||||||||||||||||||||||
| 760 | $cz + $r * sin($phi), | ||||||||||||||||||||||||||||||||||||||||||
| 761 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||||||||||||||||
| 763 | } | ||||||||||||||||||||||||||||||||||||||||||
| 764 | 0 | my @f; | |||||||||||||||||||||||||||||||||||||||||
| 765 | 0 | for my $i (0..$maj_seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 766 | 0 | my $ni = ($i+1) % $maj_seg; | |||||||||||||||||||||||||||||||||||||||||
| 767 | 0 | for my $j (0..$min_seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 768 | 0 | my $nj = ($j+1) % $min_seg; | |||||||||||||||||||||||||||||||||||||||||
| 769 | 0 | my $a = $i *$min_seg + $j; | |||||||||||||||||||||||||||||||||||||||||
| 770 | 0 | my $b = $i *$min_seg + $nj; | |||||||||||||||||||||||||||||||||||||||||
| 771 | 0 | my $c = $ni *$min_seg + $j; | |||||||||||||||||||||||||||||||||||||||||
| 772 | 0 | my $d = $ni *$min_seg + $nj; | |||||||||||||||||||||||||||||||||||||||||
| 773 | 0 | push @f, [$a,$c,$d]; | |||||||||||||||||||||||||||||||||||||||||
| 774 | 0 | push @f, [$a,$d,$b]; | |||||||||||||||||||||||||||||||||||||||||
| 775 | } | ||||||||||||||||||||||||||||||||||||||||||
| 776 | } | ||||||||||||||||||||||||||||||||||||||||||
| 777 | 0 | return $self->mesh(\@v, \@f); | |||||||||||||||||||||||||||||||||||||||||
| 778 | } | ||||||||||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||||||||||
| 780 | # Flat disk (filled circle) mesh in the XY plane centred at (cx,cy,cz). | ||||||||||||||||||||||||||||||||||||||||||
| 781 | # Vertex 0 is the centre; vertices 1..$seg are the rim. | ||||||||||||||||||||||||||||||||||||||||||
| 782 | 0 | ▶ | sub disk ($self, $cx,$cy,$cz,$r,$seg=24) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
disk: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 783 | 0 | my @v = ([$cx,$cy,$cz]); # centre | |||||||||||||||||||||||||||||||||||||||||
| 784 | 0 | for my $i (0..$seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 785 | 0 | my $theta = 2*$PI*$i/$seg; | |||||||||||||||||||||||||||||||||||||||||
| 786 | 0 | push @v, [$cx + $r*cos($theta), $cy + $r*sin($theta), $cz]; | |||||||||||||||||||||||||||||||||||||||||
| 787 | } | ||||||||||||||||||||||||||||||||||||||||||
| 788 | 0 | my @f; | |||||||||||||||||||||||||||||||||||||||||
| 789 | 0 | for my $i (0..$seg-1) { | |||||||||||||||||||||||||||||||||||||||||
| 790 | 0 | my $n = ($i+1) % $seg; | |||||||||||||||||||||||||||||||||||||||||
| 791 | 0 | push @f, [0, $i+1, $n+1]; | |||||||||||||||||||||||||||||||||||||||||
| 792 | } | ||||||||||||||||||||||||||||||||||||||||||
| 793 | 0 | return $self->mesh(\@v, \@f); | |||||||||||||||||||||||||||||||||||||||||
| 794 | } | ||||||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||||||
| 796 | # Regular polygon-base pyramid mesh. | ||||||||||||||||||||||||||||||||||||||||||
| 797 | # (cx,cy,cz): base centre; $r: circumradius of base; $height: height in +Z. | ||||||||||||||||||||||||||||||||||||||||||
| 798 | # $sides: number of base vertices (default 4 = square pyramid). | ||||||||||||||||||||||||||||||||||||||||||
| 799 | # Vertex layout: [0..$sides-1] base ring, [$sides] apex, [$sides+1] base centre. | ||||||||||||||||||||||||||||||||||||||||||
| 800 | 0 | ▶ | sub pyramid ($self, $cx,$cy,$cz,$r,$height,$sides=4) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pyramid: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 801 | 0 | ▶ | $sides >= 3 or $self->_croak('pyramid: sides must be >= 3'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 802 | 0 | my @v; | |||||||||||||||||||||||||||||||||||||||||
| 803 | 0 | for my $i (0..$sides-1) { | |||||||||||||||||||||||||||||||||||||||||
| 804 | 0 | my $theta = 2*$PI*$i/$sides; | |||||||||||||||||||||||||||||||||||||||||
| 805 | 0 | push @v, [$cx + $r*cos($theta), $cy + $r*sin($theta), $cz]; | |||||||||||||||||||||||||||||||||||||||||
| 806 | } | ||||||||||||||||||||||||||||||||||||||||||
| 807 | 0 | my $apex = scalar @v; push @v, [$cx, $cy, $cz + $height]; | |||||||||||||||||||||||||||||||||||||||||
| 808 | 0 | my $base_c = scalar @v; push @v, [$cx, $cy, $cz]; | |||||||||||||||||||||||||||||||||||||||||
| 809 | 0 | my @f; | |||||||||||||||||||||||||||||||||||||||||
| 810 | 0 | for my $i (0..$sides-1) { | |||||||||||||||||||||||||||||||||||||||||
| 811 | 0 | my $n = ($i+1) % $sides; | |||||||||||||||||||||||||||||||||||||||||
| 812 | 0 | push @f, [$i, $n, $apex ]; # side face | |||||||||||||||||||||||||||||||||||||||||
| 813 | 0 | push @f, [$base_c, $n, $i ]; # base fan (faces -Z) | |||||||||||||||||||||||||||||||||||||||||
| 814 | } | ||||||||||||||||||||||||||||||||||||||||||
| 815 | 0 | return $self->mesh(\@v, \@f); | |||||||||||||||||||||||||||||||||||||||||
| 816 | } | ||||||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||||||
| 818 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 819 | # SECTION: Quaternions | ||||||||||||||||||||||||||||||||||||||||||
| 820 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||||||||
| 822 | # Construct a unit quaternion [w,x,y,z] from axis + angle (degrees) | ||||||||||||||||||||||||||||||||||||||||||
| 823 | 0 | ▶ | sub quat_from_axis_angle ($self, $axis_ref, $deg) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
quat_from_axis_angle: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 824 | 0 | my ($ax,$ay,$az) = @$axis_ref; | |||||||||||||||||||||||||||||||||||||||||
| 825 | 0 | my $len = sqrt($ax*$ax + $ay*$ay + $az*$az); | |||||||||||||||||||||||||||||||||||||||||
| 826 | 0 | ▶ | $self->_croak('quat_from_axis_angle: zero-length axis') if $len < $EPSILON; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 827 | 0 | ($ax,$ay,$az) = ($ax/$len,$ay/$len,$az/$len); | |||||||||||||||||||||||||||||||||||||||||
| 828 | 0 | my $half = $deg * $D2R / 2.0; | |||||||||||||||||||||||||||||||||||||||||
| 829 | 0 | my $s = sin($half); | |||||||||||||||||||||||||||||||||||||||||
| 830 | 0 | return [ cos($half), $ax*$s, $ay*$s, $az*$s ]; | |||||||||||||||||||||||||||||||||||||||||
| 831 | } | ||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||
| 833 | # Convert quaternion to 4x4 rotation matrix | ||||||||||||||||||||||||||||||||||||||||||
| 834 | 0 | ▶ | sub quat_to_matrix ($self, $q) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
quat_to_matrix: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 835 | 0 | my ($w,$x,$y,$z) = @$q; | |||||||||||||||||||||||||||||||||||||||||
| 836 | return [ | ||||||||||||||||||||||||||||||||||||||||||
| 837 | 0 | [1-2*($y*$y+$z*$z), 2*($x*$y-$w*$z), 2*($x*$z+$w*$y), 0], | |||||||||||||||||||||||||||||||||||||||||
| 838 | [2*($x*$y+$w*$z), 1-2*($x*$x+$z*$z), 2*($y*$z-$w*$x), 0], | ||||||||||||||||||||||||||||||||||||||||||
| 839 | [2*($x*$z-$w*$y), 2*($y*$z+$w*$x), 1-2*($x*$x+$y*$y), 0], | ||||||||||||||||||||||||||||||||||||||||||
| 840 | [0, 0, 0, 1], | ||||||||||||||||||||||||||||||||||||||||||
| 841 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 842 | } | ||||||||||||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||||||||||||
| 844 | # Spherical linear interpolation between two quaternions at parameter t (0 <= t <= 1) | ||||||||||||||||||||||||||||||||||||||||||
| 845 | 0 | ▶ | sub quat_slerp ($self, $q1, $q2, $t) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
quat_slerp: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 846 | 0 | my ($w1,$x1,$y1,$z1) = @$q1; | |||||||||||||||||||||||||||||||||||||||||
| 847 | 0 | my ($w2,$x2,$y2,$z2) = @$q2; | |||||||||||||||||||||||||||||||||||||||||
| 848 | 0 | my $dot = $w1*$w2 + $x1*$x2 + $y1*$y2 + $z1*$z2; | |||||||||||||||||||||||||||||||||||||||||
| 849 | 0 | ▶ | if ($dot < 0) { ($w2,$x2,$y2,$z2) = (-$w2,-$x2,-$y2,-$z2); $dot = -$dot } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 850 | 0 | ▶ | if ($dot > 0.9995) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 851 | 0 | my @r = ($w1+$t*($w2-$w1), $x1+$t*($x2-$x1), | |||||||||||||||||||||||||||||||||||||||||
| 852 | $y1+$t*($y2-$y1), $z1+$t*($z2-$z1)); | ||||||||||||||||||||||||||||||||||||||||||
| 853 | 0 | ▶ | my $l = sqrt($r[0]**2+$r[1]**2+$r[2]**2+$r[3]**2) || 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
sqrt $r[0] ** 2 + $r[1] ** 2 + $r[2] ** 2 + $r[3] ** 2 || 1
MC/DC0%0/1
sqrt $r[0] ** 2 + $r[1] ** 2 + $r[2] ** 2 + $r[3] ** 2 || 1
sqrt $r[0] ** 2 + $r[1] ** 2 + $r[2] ** 2 + $r[3] ** 2
Truth table0%0/2
A
A= sqrt $r[0] ** 2 + $r[1] ** 2 + $r[2] ** 2 + $r[3] ** 2
| |||||||||||||||||||||||||||||||||||||||||||
| 854 | 0 | $_/=$l for @r; | |||||||||||||||||||||||||||||||||||||||||
| 855 | 0 | return \@r; | |||||||||||||||||||||||||||||||||||||||||
| 856 | } | ||||||||||||||||||||||||||||||||||||||||||
| 857 | 0 | my $theta0 = acos($dot); | |||||||||||||||||||||||||||||||||||||||||
| 858 | 0 | my $theta = $theta0 * $t; | |||||||||||||||||||||||||||||||||||||||||
| 859 | 0 | my $st0 = sin($theta0); | |||||||||||||||||||||||||||||||||||||||||
| 860 | 0 | my $s1 = cos($theta) - $dot * sin($theta) / $st0; | |||||||||||||||||||||||||||||||||||||||||
| 861 | 0 | my $s2 = sin($theta) / $st0; | |||||||||||||||||||||||||||||||||||||||||
| 862 | 0 | return [ $w1*$s1+$w2*$s2, $x1*$s1+$x2*$s2, | |||||||||||||||||||||||||||||||||||||||||
| 863 | $y1*$s1+$y2*$s2, $z1*$s1+$z2*$s2 ]; | ||||||||||||||||||||||||||||||||||||||||||
| 864 | } | ||||||||||||||||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||||||||||||||||
| 866 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 867 | # SECTION: Mesh utilities | ||||||||||||||||||||||||||||||||||||||||||
| 868 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||||||||||||||||
| 870 | # Bounding box of a mesh or arrayref of points. | ||||||||||||||||||||||||||||||||||||||||||
| 871 | # Returns ([$minx,$miny,$minz], [$maxx,$maxy,$maxz]) | ||||||||||||||||||||||||||||||||||||||||||
| 872 | 0 | ▶ | sub bbox3 ($self, $arg) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bbox3: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 873 | my @pts = (ref $arg eq 'HASH' && $arg->{verts}) | ||||||||||||||||||||||||||||||||||||||||||
| 874 | 0 | ▶ | ? @{ $arg->{verts} } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $arg eq "HASH" and $arg->{"verts"}
MC/DC0%0/2
ref $arg eq "HASH" and $arg->{"verts"}
ref $arg eq "HASH"
$arg->{"verts"}
Truth table0%0/3
A and B
A= ref $arg eq "HASH"B= $arg->{"verts"}
| |||||||||||||||||||||||||||||||||||||||||||
| 875 | : @$arg; | ||||||||||||||||||||||||||||||||||||||||||
| 876 | 0 | my ($mnx,$mny,$mnz,$mxx,$mxy,$mxz) = (1e99,1e99,1e99,-1e99,-1e99,-1e99); | |||||||||||||||||||||||||||||||||||||||||
| 877 | 0 | for my $p (@pts) { | |||||||||||||||||||||||||||||||||||||||||
| 878 | 0 | ▶ | $mnx=$p->[0] if $p->[0]<$mnx; $mxx=$p->[0] if $p->[0]>$mxx; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 879 | 0 | ▶ | $mny=$p->[1] if $p->[1]<$mny; $mxy=$p->[1] if $p->[1]>$mxy; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 880 | 0 | ▶ | $mnz=$p->[2] if $p->[2]<$mnz; $mxz=$p->[2] if $p->[2]>$mxz; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 881 | } | ||||||||||||||||||||||||||||||||||||||||||
| 882 | 0 | return ([$mnx,$mny,$mnz], [$mxx,$mxy,$mxz]); | |||||||||||||||||||||||||||||||||||||||||
| 883 | } | ||||||||||||||||||||||||||||||||||||||||||
| 884 | |||||||||||||||||||||||||||||||||||||||||||
| 885 | # Compute face normals and averaged vertex normals; stores them in the mesh. | ||||||||||||||||||||||||||||||||||||||||||
| 886 | 0 | ▶ | sub compute_normals ($self, $mesh) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compute_normals: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 887 | 0 | my @v = @{ $mesh->{verts} }; | |||||||||||||||||||||||||||||||||||||||||
| 888 | 0 | my @fn; | |||||||||||||||||||||||||||||||||||||||||
| 889 | 0 | my @vn = map { [0,0,0] } @v; | |||||||||||||||||||||||||||||||||||||||||
| 890 | 0 | my @vc = (0) x @v; | |||||||||||||||||||||||||||||||||||||||||
| 891 | 0 | for my $f (@{ $mesh->{faces} }) { | |||||||||||||||||||||||||||||||||||||||||
| 892 | 0 | my ($a,$b,$c) = @$f; | |||||||||||||||||||||||||||||||||||||||||
| 893 | 0 | my ($ux,$uy,$uz) = map { $v[$b][$_]-$v[$a][$_] } 0..2; | |||||||||||||||||||||||||||||||||||||||||
| 894 | 0 | my ($vx,$vy,$vz) = map { $v[$c][$_]-$v[$a][$_] } 0..2; | |||||||||||||||||||||||||||||||||||||||||
| 895 | 0 | my ($nx,$ny,$nz) = ($uy*$vz-$uz*$vy, $uz*$vx-$ux*$vz, $ux*$vy-$uy*$vx); | |||||||||||||||||||||||||||||||||||||||||
| 896 | 0 | ▶ | my $l = sqrt($nx*$nx+$ny*$ny+$nz*$nz) || 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
sqrt $nx * $nx + $ny * $ny + $nz * $nz || 1
MC/DC0%0/1
sqrt $nx * $nx + $ny * $ny + $nz * $nz || 1
sqrt $nx * $nx + $ny * $ny + $nz * $nz
Truth table0%0/2
A
A= sqrt $nx * $nx + $ny * $ny + $nz * $nz
| |||||||||||||||||||||||||||||||||||||||||||
| 897 | 0 | push @fn, [$nx/$l,$ny/$l,$nz/$l]; | |||||||||||||||||||||||||||||||||||||||||
| 898 | 0 | for my $i ($a,$b,$c) { | |||||||||||||||||||||||||||||||||||||||||
| 899 | 0 | $vn[$i][$_] += $fn[-1][$_] for 0..2; | |||||||||||||||||||||||||||||||||||||||||
| 900 | 0 | $vc[$i]++; | |||||||||||||||||||||||||||||||||||||||||
| 901 | } | ||||||||||||||||||||||||||||||||||||||||||
| 902 | } | ||||||||||||||||||||||||||||||||||||||||||
| 903 | 0 | for my $i (0..$#v) { | |||||||||||||||||||||||||||||||||||||||||
| 904 | 0 | ▶ | next unless $vc[$i]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 905 | 0 | ▶ | my $l = sqrt($vn[$i][0]**2 + $vn[$i][1]**2 + $vn[$i][2]**2) || 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
sqrt $vn[$i][0] ** 2 + $vn[$i][1] ** 2 + $vn[$i][2] ** 2 || 1
MC/DC0%0/1
sqrt $vn[$i][0] ** 2 + $vn[$i][1] ** 2 + $vn[$i][2] ** 2 || 1
sqrt $vn[$i][0] ** 2 + $vn[$i][1] ** 2 + $vn[$i][2] ** 2
Truth table0%0/2
A
A= sqrt $vn[$i][0] ** 2 + $vn[$i][1] ** 2 + $vn[$i][2] ** 2
| |||||||||||||||||||||||||||||||||||||||||||
| 906 | 0 | $vn[$i] = [ map { $_/$l } @{$vn[$i]} ]; | |||||||||||||||||||||||||||||||||||||||||
| 907 | } | ||||||||||||||||||||||||||||||||||||||||||
| 908 | 0 | $mesh->{face_normals} = \@fn; | |||||||||||||||||||||||||||||||||||||||||
| 909 | 0 | $mesh->{vertex_normals} = \@vn; | |||||||||||||||||||||||||||||||||||||||||
| 910 | 0 | return $mesh; | |||||||||||||||||||||||||||||||||||||||||
| 911 | } | ||||||||||||||||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||||||||||||||||
| 913 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 914 | # SECTION: Visibility -- back-face culling and z-buffer occlusion | ||||||||||||||||||||||||||||||||||||||||||
| 915 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||
| 917 | # Back-face culling. Returns arrayref of visible face indices. | ||||||||||||||||||||||||||||||||||||||||||
| 918 | # Option view_dir: camera direction vector. If omitted, uses the forward | ||||||||||||||||||||||||||||||||||||||||||
| 919 | # vector stored by set_camera(); falls back to [0,0,-1] if no camera is set. | ||||||||||||||||||||||||||||||||||||||||||
| 920 | 0 | ▶ | sub backface_cull ($self, $mesh, %opts) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
backface_cull: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 921 | my $vd = $opts{view_dir} | ||||||||||||||||||||||||||||||||||||||||||
| 922 | 0 | ▶ | // ( $self->{_g3_camera} ? $self->{_g3_camera}{fwd} : [0,0,-1] ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opts{"view_dir"} // ($self->{"_g3_camera"} ? $self->{"_g3_camera"}{"fwd"} : [0, 0, -1])
MC/DC0%0/2
$opts{"view_dir"} // ($self->{"_g3_camera"} ? $self->{"_g3_camera"}{"fwd"} : [0, 0, -1])
$opts{"view_dir"}
($self->{"_g3_camera"} ? $self->{"_g3_camera"}{"fwd"} : [0, 0, -1])
Truth table0%0/3
A // B
A= $opts{"view_dir"}B= ($self->{"_g3_camera"} ? $self->{"_g3_camera"}{"fwd"} : [0, 0, -1])
| |||||||||||||||||||||||||||||||||||||||||||
| 923 | 0 | my @v = @{ $mesh->{verts} }; | |||||||||||||||||||||||||||||||||||||||||
| 924 | 0 | my @visible; | |||||||||||||||||||||||||||||||||||||||||
| 925 | 0 | for my $fi (0..$#{ $mesh->{faces} }) { | |||||||||||||||||||||||||||||||||||||||||
| 926 | 0 | my ($a,$b,$c) = @{ $mesh->{faces}[$fi] }; | |||||||||||||||||||||||||||||||||||||||||
| 927 | 0 | my ($ux,$uy,$uz) = map { $v[$b][$_]-$v[$a][$_] } 0..2; | |||||||||||||||||||||||||||||||||||||||||
| 928 | 0 | my ($wx,$wy,$wz) = map { $v[$c][$_]-$v[$a][$_] } 0..2; | |||||||||||||||||||||||||||||||||||||||||
| 929 | 0 | my ($nx,$ny,$nz) = ($uy*$wz-$uz*$wy, $uz*$wx-$ux*$wz, $ux*$wy-$uy*$wx); | |||||||||||||||||||||||||||||||||||||||||
| 930 | 0 | my $dot = $nx*$vd->[0] + $ny*$vd->[1] + $nz*$vd->[2]; | |||||||||||||||||||||||||||||||||||||||||
| 931 | 0 | ▶ | push @visible, $fi if $dot < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 932 | } | ||||||||||||||||||||||||||||||||||||||||||
| 933 | 0 | return \@visible; | |||||||||||||||||||||||||||||||||||||||||
| 934 | } | ||||||||||||||||||||||||||||||||||||||||||
| 935 | |||||||||||||||||||||||||||||||||||||||||||
| 936 | # Z-buffer occlusion clip. | ||||||||||||||||||||||||||||||||||||||||||
| 937 | # Projects mesh through the current CTM3, rasterises into a $res x $res buffer, | ||||||||||||||||||||||||||||||||||||||||||
| 938 | # and returns an arrayref of visible edge segments [[p1,p2], ...] where each | ||||||||||||||||||||||||||||||||||||||||||
| 939 | # point is an arrayref [$x,$y] in projected (2-D) space. | ||||||||||||||||||||||||||||||||||||||||||
| 940 | # Option: res => N (default 256) | ||||||||||||||||||||||||||||||||||||||||||
| 941 | 0 | ▶ | sub occlusion_clip ($self, $mesh, %opts) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
occlusion_clip: not called CC=33 SCAR=70.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 942 | 0 | ▶ | my $res = $opts{res} // 256; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts{"res"} // 256
MC/DC0%0/1
$opts{"res"} // 256
$opts{"res"}
Truth table0%0/2
A
A= $opts{"res"}
| |||||||||||||||||||||||||||||||||||||||||||
| 943 | 0 | ▶ | my $occluders = $opts{occluders} // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts{"occluders"} // []
MC/DC0%0/1
$opts{"occluders"} // []
$opts{"occluders"}
Truth table0%0/2
A
A= $opts{"occluders"}
| |||||||||||||||||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||||||||||||||||
| 945 | 0 | my @v = @{ $mesh->{verts} }; | |||||||||||||||||||||||||||||||||||||||||
| 946 | 0 | my @f = @{ $mesh->{faces} }; | |||||||||||||||||||||||||||||||||||||||||
| 947 | |||||||||||||||||||||||||||||||||||||||||||
| 948 | # Project target mesh vertices | ||||||||||||||||||||||||||||||||||||||||||
| 949 | 0 | my (@proj, @zdepth); | |||||||||||||||||||||||||||||||||||||||||
| 950 | 0 | for my $p (@v) { | |||||||||||||||||||||||||||||||||||||||||
| 951 | 0 | my ($tx,$ty,$tz) = $self->transform_point($p); | |||||||||||||||||||||||||||||||||||||||||
| 952 | 0 | push @proj, [$tx,$ty]; | |||||||||||||||||||||||||||||||||||||||||
| 953 | 0 | push @zdepth, $tz; | |||||||||||||||||||||||||||||||||||||||||
| 954 | } | ||||||||||||||||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||||||||||||||||
| 956 | # Project occluder vertices | ||||||||||||||||||||||||||||||||||||||||||
| 957 | 0 | my (@oproj, @ozdepth, @of_all); | |||||||||||||||||||||||||||||||||||||||||
| 958 | 0 | for my $om (@$occluders) { | |||||||||||||||||||||||||||||||||||||||||
| 959 | 0 | my $base = scalar @oproj; | |||||||||||||||||||||||||||||||||||||||||
| 960 | 0 | for my $p (@{ $om->{verts} }) { | |||||||||||||||||||||||||||||||||||||||||
| 961 | 0 | my ($tx,$ty,$tz) = $self->transform_point($p); | |||||||||||||||||||||||||||||||||||||||||
| 962 | 0 | push @oproj, [$tx,$ty]; | |||||||||||||||||||||||||||||||||||||||||
| 963 | 0 | push @ozdepth, $tz; | |||||||||||||||||||||||||||||||||||||||||
| 964 | } | ||||||||||||||||||||||||||||||||||||||||||
| 965 | 0 | for my $face (@{ $om->{faces} }) { | |||||||||||||||||||||||||||||||||||||||||
| 966 | 0 | push @of_all, [ map { $_ + $base } @$face ]; | |||||||||||||||||||||||||||||||||||||||||
| 967 | } | ||||||||||||||||||||||||||||||||||||||||||
| 968 | } | ||||||||||||||||||||||||||||||||||||||||||
| 969 | |||||||||||||||||||||||||||||||||||||||||||
| 970 | # Bounding box from TARGET mesh only. | ||||||||||||||||||||||||||||||||||||||||||
| 971 | # Anchoring to the target keeps all 256 pixels focused on the region that | ||||||||||||||||||||||||||||||||||||||||||
| 972 | # matters. Occluder triangles that extend outside are still rasterised | ||||||||||||||||||||||||||||||||||||||||||
| 973 | # within the grid because the per-triangle pixel loops clamp to [0, res-1]. | ||||||||||||||||||||||||||||||||||||||||||
| 974 | 0 | my ($mnx,$mny,$mxx,$mxy) = (1e99,1e99,-1e99,-1e99); | |||||||||||||||||||||||||||||||||||||||||
| 975 | 0 | for my $p (@proj) { | |||||||||||||||||||||||||||||||||||||||||
| 976 | 0 | ▶ | $mnx=$p->[0] if $p->[0]<$mnx; $mxx=$p->[0] if $p->[0]>$mxx; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 977 | 0 | ▶ | $mny=$p->[1] if $p->[1]<$mny; $mxy=$p->[1] if $p->[1]>$mxy; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 978 | } | ||||||||||||||||||||||||||||||||||||||||||
| 979 | 0 | ▶ | my $rngx = $mxx-$mnx || 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$mxx - $mnx || 1
MC/DC0%0/1
$mxx - $mnx || 1
$mxx - $mnx
Truth table0%0/2
A
A= $mxx - $mnx
| |||||||||||||||||||||||||||||||||||||||||||
| 980 | 0 | ▶ | my $rngy = $mxy-$mny || 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$mxy - $mny || 1
MC/DC0%0/1
$mxy - $mny || 1
$mxy - $mny
Truth table0%0/2
A
A= $mxy - $mny
| |||||||||||||||||||||||||||||||||||||||||||
| 981 | 0 | my $sx = ($res-1) / $rngx; | |||||||||||||||||||||||||||||||||||||||||
| 982 | 0 | my $sy = ($res-1) / $rngy; | |||||||||||||||||||||||||||||||||||||||||
| 983 | |||||||||||||||||||||||||||||||||||||||||||
| 984 | 0 | my @zbuf = map { [(1e99) x $res] } 0..$res-1; | |||||||||||||||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||||||||||||||||
| 986 | # --- inner rasteriser: writes z to zbuf only --- | ||||||||||||||||||||||||||||||||||||||||||
| 987 | 0 | ▶ | my $rasterise = sub ($px_arr, $py_arr, $pz_arr) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=24 SCAR=64.0
| |||||||||||||||||||||||||||||||||||||||||||
| 988 | 0 | my ($x1,$y1,$z1) = ($px_arr->[0],$py_arr->[0],$pz_arr->[0]); | |||||||||||||||||||||||||||||||||||||||||
| 989 | 0 | my ($x2,$y2,$z2) = ($px_arr->[1],$py_arr->[1],$pz_arr->[1]); | |||||||||||||||||||||||||||||||||||||||||
| 990 | 0 | my ($x3,$y3,$z3) = ($px_arr->[2],$py_arr->[2],$pz_arr->[2]); | |||||||||||||||||||||||||||||||||||||||||
| 991 | |||||||||||||||||||||||||||||||||||||||||||
| 992 | 0 | ▶ | my $pxlo = POSIX::floor((($x1<$x2&&$x1<$x3?$x1:($x2<$x3?$x2:$x3))-$mnx)*$sx); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$x1 < $x2 and $x1 < $x3
MC/DC0%0/2
$x1 < $x2 and $x1 < $x3
$x1 < $x2
$x1 < $x3
Truth table0%0/3
A and B
A= $x1 < $x2B= $x1 < $x3
| |||||||||||||||||||||||||||||||||||||||||||
| 993 | 0 | ▶ | my $pxhi = POSIX::ceil( (($x1>$x2&&$x1>$x3?$x1:($x2>$x3?$x2:$x3))-$mnx)*$sx); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$x1 > $x2 and $x1 > $x3
MC/DC0%0/2
$x1 > $x2 and $x1 > $x3
$x1 > $x2
$x1 > $x3
Truth table0%0/3
A and B
A= $x1 > $x2B= $x1 > $x3
| |||||||||||||||||||||||||||||||||||||||||||
| 994 | 0 | ▶ | my $pylo = POSIX::floor((($y1<$y2&&$y1<$y3?$y1:($y2<$y3?$y2:$y3))-$mny)*$sy); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$y1 < $y2 and $y1 < $y3
MC/DC0%0/2
$y1 < $y2 and $y1 < $y3
$y1 < $y2
$y1 < $y3
Truth table0%0/3
A and B
A= $y1 < $y2B= $y1 < $y3
| |||||||||||||||||||||||||||||||||||||||||||
| 995 | 0 | ▶ | my $pyhi = POSIX::ceil( (($y1>$y2&&$y1>$y3?$y1:($y2>$y3?$y2:$y3))-$mny)*$sy); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$y1 > $y2 and $y1 > $y3
MC/DC0%0/2
$y1 > $y2 and $y1 > $y3
$y1 > $y2
$y1 > $y3
Truth table0%0/3
A and B
A= $y1 > $y2B= $y1 > $y3
| |||||||||||||||||||||||||||||||||||||||||||
| 996 | 0 | ▶ | $pxlo = 0 if $pxlo < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 997 | 0 | ▶ | $pylo = 0 if $pylo < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 998 | 0 | ▶ | $pxhi = $res-1 if $pxhi >= $res; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 999 | 0 | ▶ | $pyhi = $res-1 if $pyhi >= $res; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1000 | |||||||||||||||||||||||||||||||||||||||||||
| 1001 | 0 | for my $px ($pxlo..$pxhi) { | |||||||||||||||||||||||||||||||||||||||||
| 1002 | 0 | for my $py ($pylo..$pyhi) { | |||||||||||||||||||||||||||||||||||||||||
| 1003 | 0 | my $fx = $mnx + $px/$sx; | |||||||||||||||||||||||||||||||||||||||||
| 1004 | 0 | my $fy = $mny + $py/$sy; | |||||||||||||||||||||||||||||||||||||||||
| 1005 | 0 | my $den = ($y2-$y3)*($x1-$x3) + ($x3-$x2)*($y1-$y3); | |||||||||||||||||||||||||||||||||||||||||
| 1006 | 0 | ▶ | next if abs($den) < 1e-12; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1007 | 0 | my $ba = (($y2-$y3)*($fx-$x3)+($x3-$x2)*($fy-$y3)) / $den; | |||||||||||||||||||||||||||||||||||||||||
| 1008 | 0 | my $bb = (($y3-$y1)*($fx-$x3)+($x1-$x3)*($fy-$y3)) / $den; | |||||||||||||||||||||||||||||||||||||||||
| 1009 | 0 | my $bc = 1 - $ba - $bb; | |||||||||||||||||||||||||||||||||||||||||
| 1010 | 0 | ▶ | next if $ba < -1e-6 || $bb < -1e-6 || $bc < -1e-6; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$ba < "-1e-06" or $bb < "-1e-06" or $bc < "-1e-06"
$ba < "-1e-06" or $bb < "-1e-06"
MC/DC0%0/3
$ba < "-1e-06" or $bb < "-1e-06" or $bc < "-1e-06"
$ba < "-1e-06"
$bb < "-1e-06"
$bc < "-1e-06"
Truth table0%0/4
A or B or C
A= $ba < "-1e-06"B= $bb < "-1e-06"C= $bc < "-1e-06"
| |||||||||||||||||||||||||||||||||||||||||||
| 1011 | 0 | my $z = $ba*$z1 + $bb*$z2 + $bc*$z3; | |||||||||||||||||||||||||||||||||||||||||
| 1012 | 0 | ▶ | $zbuf[$py][$px] = $z if $z < $zbuf[$py][$px]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1013 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1014 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1015 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 1016 | |||||||||||||||||||||||||||||||||||||||||||
| 1017 | # Pass 1: rasterise occluder triangles (depth only) | ||||||||||||||||||||||||||||||||||||||||||
| 1018 | 0 | for my $face (@of_all) { | |||||||||||||||||||||||||||||||||||||||||
| 1019 | 0 | my ($i1,$i2,$i3) = @$face; | |||||||||||||||||||||||||||||||||||||||||
| 1020 | 0 | $rasterise->( | |||||||||||||||||||||||||||||||||||||||||
| 1021 | [$oproj[$i1][0], $oproj[$i2][0], $oproj[$i3][0]], | ||||||||||||||||||||||||||||||||||||||||||
| 1022 | [$oproj[$i1][1], $oproj[$i2][1], $oproj[$i3][1]], | ||||||||||||||||||||||||||||||||||||||||||
| 1023 | [$ozdepth[$i1], $ozdepth[$i2], $ozdepth[$i3]], | ||||||||||||||||||||||||||||||||||||||||||
| 1024 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1025 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1026 | |||||||||||||||||||||||||||||||||||||||||||
| 1027 | # Pass 2: rasterise target triangles (depth only). | ||||||||||||||||||||||||||||||||||||||||||
| 1028 | # After this pass zbuf contains the minimum z at each pixel across both | ||||||||||||||||||||||||||||||||||||||||||
| 1029 | # occluders and the target surface itself. The edge-midpoint test below | ||||||||||||||||||||||||||||||||||||||||||
| 1030 | # then uses this combined depth to decide visibility. | ||||||||||||||||||||||||||||||||||||||||||
| 1031 | 0 | for my $fi (0..$#f) { | |||||||||||||||||||||||||||||||||||||||||
| 1032 | 0 | my ($i1,$i2,$i3) = @{$f[$fi]}; | |||||||||||||||||||||||||||||||||||||||||
| 1033 | 0 | $rasterise->( | |||||||||||||||||||||||||||||||||||||||||
| 1034 | [$proj[$i1][0], $proj[$i2][0], $proj[$i3][0]], | ||||||||||||||||||||||||||||||||||||||||||
| 1035 | [$proj[$i1][1], $proj[$i2][1], $proj[$i3][1]], | ||||||||||||||||||||||||||||||||||||||||||
| 1036 | [$zdepth[$i1], $zdepth[$i2], $zdepth[$i3]], | ||||||||||||||||||||||||||||||||||||||||||
| 1037 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1038 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1039 | |||||||||||||||||||||||||||||||||||||||||||
| 1040 | # --- Build edge list --- | ||||||||||||||||||||||||||||||||||||||||||
| 1041 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1042 | # Old approach (WRONG for partially-occluded faces): | ||||||||||||||||||||||||||||||||||||||||||
| 1043 | # mark faces vis=1 if any pixel wins zbuf, then emit all edges of vis faces. | ||||||||||||||||||||||||||||||||||||||||||
| 1044 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1045 | # New approach: test each edge midpoint against the zbuffer. | ||||||||||||||||||||||||||||||||||||||||||
| 1046 | # - After pass 2, zbuf holds the closest z at every pixel (occluder OR target). | ||||||||||||||||||||||||||||||||||||||||||
| 1047 | # - An edge midpoint on the VISIBLE surface has z_mid ≈ zbuf[pixel] (the | ||||||||||||||||||||||||||||||||||||||||||
| 1048 | # target face wrote that value in pass 2). | ||||||||||||||||||||||||||||||||||||||||||
| 1049 | # - An edge midpoint BEHIND an occluder has z_mid > zbuf[pixel] (the | ||||||||||||||||||||||||||||||||||||||||||
| 1050 | # occluder wrote a smaller value in pass 1 that pass 2 couldn't overwrite). | ||||||||||||||||||||||||||||||||||||||||||
| 1051 | # The epsilon (1e-5) handles floating-point noise while staying well below | ||||||||||||||||||||||||||||||||||||||||||
| 1052 | # the typical z-gap between adjacent objects (≥ 1e-4 in this library's scenes). | ||||||||||||||||||||||||||||||||||||||||||
| 1053 | |||||||||||||||||||||||||||||||||||||||||||
| 1054 | # Compute face normals for coplanar diagonal suppression | ||||||||||||||||||||||||||||||||||||||||||
| 1055 | 0 | my %edge_tris; | |||||||||||||||||||||||||||||||||||||||||
| 1056 | my @fnorm; | ||||||||||||||||||||||||||||||||||||||||||
| 1057 | 0 | for my $fi (0..$#f) { | |||||||||||||||||||||||||||||||||||||||||
| 1058 | 0 | my ($i1,$i2,$i3) = @{$f[$fi]}; | |||||||||||||||||||||||||||||||||||||||||
| 1059 | 0 | my ($ux,$uy,$uz) = map { $v[$i2][$_]-$v[$i1][$_] } 0..2; | |||||||||||||||||||||||||||||||||||||||||
| 1060 | 0 | my ($wx,$wy,$wz) = map { $v[$i3][$_]-$v[$i1][$_] } 0..2; | |||||||||||||||||||||||||||||||||||||||||
| 1061 | 0 | my ($nx,$ny,$nz) = ($uy*$wz-$uz*$wy, $uz*$wx-$ux*$wz, $ux*$wy-$uy*$wx); | |||||||||||||||||||||||||||||||||||||||||
| 1062 | 0 | ▶ | my $nl = sqrt($nx*$nx+$ny*$ny+$nz*$nz) || 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
sqrt $nx * $nx + $ny * $ny + $nz * $nz || 1
MC/DC0%0/1
sqrt $nx * $nx + $ny * $ny + $nz * $nz || 1
sqrt $nx * $nx + $ny * $ny + $nz * $nz
Truth table0%0/2
A
A= sqrt $nx * $nx + $ny * $ny + $nz * $nz
| |||||||||||||||||||||||||||||||||||||||||||
| 1063 | 0 | $fnorm[$fi] = [$nx/$nl, $ny/$nl, $nz/$nl]; | |||||||||||||||||||||||||||||||||||||||||
| 1064 | 0 | for my $edge ([$i1,$i2],[$i2,$i3],[$i3,$i1]) { | |||||||||||||||||||||||||||||||||||||||||
| 1065 | 0 | ▶ | my $key = $edge->[0] < $edge->[1] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1066 | ? "$edge->[0],$edge->[1]" : "$edge->[1],$edge->[0]"; | ||||||||||||||||||||||||||||||||||||||||||
| 1067 | 0 | push @{ $edge_tris{$key} }, $fi; | |||||||||||||||||||||||||||||||||||||||||
| 1068 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1069 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1070 | |||||||||||||||||||||||||||||||||||||||||||
| 1071 | 0 | my %suppress; | |||||||||||||||||||||||||||||||||||||||||
| 1072 | 0 | for my $key (keys %edge_tris) { | |||||||||||||||||||||||||||||||||||||||||
| 1073 | 0 | my @tris = @{ $edge_tris{$key} }; | |||||||||||||||||||||||||||||||||||||||||
| 1074 | 0 | ▶ | next unless @tris == 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1075 | 0 | my ($na,$nb) = ($fnorm[$tris[0]], $fnorm[$tris[1]]); | |||||||||||||||||||||||||||||||||||||||||
| 1076 | 0 | my $dot = $na->[0]*$nb->[0] + $na->[1]*$nb->[1] + $na->[2]*$nb->[2]; | |||||||||||||||||||||||||||||||||||||||||
| 1077 | 0 | ▶ | $suppress{$key} = 1 if abs($dot) > 0.9999; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1078 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1079 | |||||||||||||||||||||||||||||||||||||||||||
| 1080 | 0 | my %drawn; | |||||||||||||||||||||||||||||||||||||||||
| 1081 | my @polylines; | ||||||||||||||||||||||||||||||||||||||||||
| 1082 | 0 | for my $fi (0..$#f) { | |||||||||||||||||||||||||||||||||||||||||
| 1083 | 0 | my ($i1,$i2,$i3) = @{$f[$fi]}; | |||||||||||||||||||||||||||||||||||||||||
| 1084 | 0 | for my $edge ([$i1,$i2],[$i2,$i3],[$i3,$i1]) { | |||||||||||||||||||||||||||||||||||||||||
| 1085 | 0 | my ($a,$b) = @$edge; | |||||||||||||||||||||||||||||||||||||||||
| 1086 | 0 | ▶ | my $key = $a < $b ? "$a,$b" : "$b,$a"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1087 | 0 | ▶ | next if $suppress{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1088 | 0 | ▶ | next if $drawn{$key}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1089 | |||||||||||||||||||||||||||||||||||||||||||
| 1090 | # Test edge midpoint visibility against the combined zbuffer | ||||||||||||||||||||||||||||||||||||||||||
| 1091 | 0 | my $mx = ($proj[$a][0] + $proj[$b][0]) * 0.5; | |||||||||||||||||||||||||||||||||||||||||
| 1092 | 0 | my $my = ($proj[$a][1] + $proj[$b][1]) * 0.5; | |||||||||||||||||||||||||||||||||||||||||
| 1093 | 0 | my $mz = ($zdepth[$a] + $zdepth[$b]) * 0.5; | |||||||||||||||||||||||||||||||||||||||||
| 1094 | 0 | my $px = POSIX::floor(($mx - $mnx) * $sx + 0.5); | |||||||||||||||||||||||||||||||||||||||||
| 1095 | 0 | my $py = POSIX::floor(($my - $mny) * $sy + 0.5); | |||||||||||||||||||||||||||||||||||||||||
| 1096 | 0 | ▶ | $px = 0 if $px < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1097 | 0 | ▶ | $px = $res-1 if $px >= $res; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1098 | 0 | ▶ | $py = 0 if $py < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1099 | 0 | ▶ | $py = $res-1 if $py >= $res; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1100 | 0 | ▶ | next if $mz > $zbuf[$py][$px] + 1e-5; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1101 | |||||||||||||||||||||||||||||||||||||||||||
| 1102 | 0 | push @polylines, [ [@{$proj[$a]}], [@{$proj[$b]}] ]; | |||||||||||||||||||||||||||||||||||||||||
| 1103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1105 | 0 | return \@polylines; | |||||||||||||||||||||||||||||||||||||||||
| 1106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1107 | |||||||||||||||||||||||||||||||||||||||||||
| 1108 | |||||||||||||||||||||||||||||||||||||||||||
| 1109 | # High-level hidden-line removal: backface cull, then z-buffer occlusion clip. | ||||||||||||||||||||||||||||||||||||||||||
| 1110 | # Accepts occluders => \@meshes to populate the z-buffer from other objects. | ||||||||||||||||||||||||||||||||||||||||||
| 1111 | 0 | ▶ | sub hidden_line_remove ($self, $mesh, %opts) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hidden_line_remove: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1112 | 0 | my $culled = $self->backface_cull($mesh, %opts); | |||||||||||||||||||||||||||||||||||||||||
| 1113 | my $visible_mesh = $self->mesh( | ||||||||||||||||||||||||||||||||||||||||||
| 1114 | $mesh->{verts}, | ||||||||||||||||||||||||||||||||||||||||||
| 1115 | 0 | [ @{$mesh->{faces}}[@$culled] ], | |||||||||||||||||||||||||||||||||||||||||
| 1116 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1117 | # occluders option flows through to occlusion_clip | ||||||||||||||||||||||||||||||||||||||||||
| 1118 | 0 | return $self->occlusion_clip($visible_mesh, %opts); | |||||||||||||||||||||||||||||||||||||||||
| 1119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||||||||||||||||
| 1121 | |||||||||||||||||||||||||||||||||||||||||||
| 1122 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1123 | # SECTION: Projection to 2-D plotter output | ||||||||||||||||||||||||||||||||||||||||||
| 1124 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1125 | |||||||||||||||||||||||||||||||||||||||||||
| 1126 | # Flatten a mesh or arrayref-of-polylines to 2-D edge segments via transform_point. | ||||||||||||||||||||||||||||||||||||||||||
| 1127 | # Returns arrayref of [[x1,y1],[x2,y2]] segments. | ||||||||||||||||||||||||||||||||||||||||||
| 1128 | 0 | ▶ | sub flatten_to_2d ($self, $geometry, %opts) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flatten_to_2d: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1129 | 0 | my @segs; | |||||||||||||||||||||||||||||||||||||||||
| 1130 | 0 | ▶ | if (ref $geometry eq 'HASH' && $geometry->{verts}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
ref $geometry eq "HASH" and $geometry->{"verts"}
MC/DC0%0/2
ref $geometry eq "HASH" and $geometry->{"verts"}
ref $geometry eq "HASH"
$geometry->{"verts"}
Truth table0%0/3
A and B
A= ref $geometry eq "HASH"B= $geometry->{"verts"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1131 | 0 | my @v = @{ $geometry->{verts} }; | |||||||||||||||||||||||||||||||||||||||||
| 1132 | 0 | my %drawn; | |||||||||||||||||||||||||||||||||||||||||
| 1133 | 0 | for my $face (@{ $geometry->{faces} }) { | |||||||||||||||||||||||||||||||||||||||||
| 1134 | 0 | my @idx = @$face; | |||||||||||||||||||||||||||||||||||||||||
| 1135 | 0 | for my $ei (0..$#idx) { | |||||||||||||||||||||||||||||||||||||||||
| 1136 | 0 | my ($a,$b) = ($idx[$ei], $idx[($ei+1)%@idx]); | |||||||||||||||||||||||||||||||||||||||||
| 1137 | 0 | ▶ | my $key = $a<$b ? "$a,$b" : "$b,$a"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1138 | 0 | ▶ | next if $drawn{$key}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1139 | 0 | my ($x1,$y1) = ($self->transform_point($v[$a]))[0,1]; | |||||||||||||||||||||||||||||||||||||||||
| 1140 | 0 | my ($x2,$y2) = ($self->transform_point($v[$b]))[0,1]; | |||||||||||||||||||||||||||||||||||||||||
| 1141 | 0 | push @segs, [[$x1,$y1],[$x2,$y2]]; | |||||||||||||||||||||||||||||||||||||||||
| 1142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1144 | } elsif (ref $geometry eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 1145 | 0 | @segs = @$geometry; | |||||||||||||||||||||||||||||||||||||||||
| 1146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1147 | 0 | return \@segs; | |||||||||||||||||||||||||||||||||||||||||
| 1148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1149 | |||||||||||||||||||||||||||||||||||||||||||
| 1150 | # Draw an arrayref of [[p1,p2],...] edge segments via the host's 2-D pen hooks | ||||||||||||||||||||||||||||||||||||||||||
| 1151 | 0 | ▶ | sub draw_polylines ($self, $polylines_ref) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_polylines: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1152 | 0 | for my $seg (@$polylines_ref) { | |||||||||||||||||||||||||||||||||||||||||
| 1153 | 0 | my ($p1,$p2) = @$seg; | |||||||||||||||||||||||||||||||||||||||||
| 1154 | 0 | $self->penup(); | |||||||||||||||||||||||||||||||||||||||||
| 1155 | 0 | $self->_genfastmove($p1->[0], $p1->[1]); | |||||||||||||||||||||||||||||||||||||||||
| 1156 | 0 | $self->pendown(); | |||||||||||||||||||||||||||||||||||||||||
| 1157 | 0 | $self->_genslowmove($p2->[0], $p2->[1]); | |||||||||||||||||||||||||||||||||||||||||
| 1158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1159 | 0 | $self->stroke(); | |||||||||||||||||||||||||||||||||||||||||
| 1160 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1162 | |||||||||||||||||||||||||||||||||||||||||||
| 1163 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1164 | # SECTION: SVG projection output | ||||||||||||||||||||||||||||||||||||||||||
| 1165 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1166 | |||||||||||||||||||||||||||||||||||||||||||
| 1167 | 0 | ▶ | sub project_to_svg ($self, $obj, %opts) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
project_to_svg: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1168 | 0 | my $polylines; | |||||||||||||||||||||||||||||||||||||||||
| 1169 | 0 | ▶ | if (ref $obj eq 'HASH' && $obj->{verts}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $obj eq "HASH" and $obj->{"verts"}
MC/DC0%0/2
ref $obj eq "HASH" and $obj->{"verts"}
ref $obj eq "HASH"
$obj->{"verts"}
Truth table0%0/3
A and B
A= ref $obj eq "HASH"B= $obj->{"verts"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1170 | 0 | $polylines = $self->occlusion_clip($obj, %opts); | |||||||||||||||||||||||||||||||||||||||||
| 1171 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1172 | 0 | $polylines = $obj; | |||||||||||||||||||||||||||||||||||||||||
| 1173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1174 | 0 | my ($mnx,$mny,$mxx,$mxy) = (1e99,1e99,-1e99,-1e99); | |||||||||||||||||||||||||||||||||||||||||
| 1175 | 0 | for my $seg (@$polylines) { | |||||||||||||||||||||||||||||||||||||||||
| 1176 | 0 | for my $p (@$seg) { | |||||||||||||||||||||||||||||||||||||||||
| 1177 | 0 | ▶ | $mnx=$p->[0] if $p->[0]<$mnx; $mxx=$p->[0] if $p->[0]>$mxx; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1178 | 0 | ▶ | $mny=$p->[1] if $p->[1]<$mny; $mxy=$p->[1] if $p->[1]>$mxy; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1181 | 0 | ▶ | $mnx=$mny=0 unless $mnx<$mxx; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1182 | 0 | ▶ | $mxx=$mxy=100 unless $mny<$mxy; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1183 | 0 | my $svg = sprintf( | |||||||||||||||||||||||||||||||||||||||||
| 1184 | "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"%.4f %.4f %.4f %.4f\">\n", | ||||||||||||||||||||||||||||||||||||||||||
| 1185 | $mnx, $mny, $mxx-$mnx, $mxy-$mny | ||||||||||||||||||||||||||||||||||||||||||
| 1186 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1187 | 0 | for my $seg (@$polylines) { | |||||||||||||||||||||||||||||||||||||||||
| 1188 | 0 | my ($x1,$y1) = @{$seg->[0]}; | |||||||||||||||||||||||||||||||||||||||||
| 1189 | 0 | my ($x2,$y2) = @{$seg->[1]}; | |||||||||||||||||||||||||||||||||||||||||
| 1190 | 0 | $svg .= sprintf( | |||||||||||||||||||||||||||||||||||||||||
| 1191 | "<line x1=\"%.4f\" y1=\"%.4f\" x2=\"%.4f\" y2=\"%.4f\" " | ||||||||||||||||||||||||||||||||||||||||||
| 1192 | ."stroke=\"black\" stroke-width=\"0.5\"/>\n", | ||||||||||||||||||||||||||||||||||||||||||
| 1193 | $x1,$y1,$x2,$y2 | ||||||||||||||||||||||||||||||||||||||||||
| 1194 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1196 | 0 | $svg .= "</svg>\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1197 | 0 | return $svg; | |||||||||||||||||||||||||||||||||||||||||
| 1198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||||||||||||||||
| 1200 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1201 | # SECTION: Mesh I/O -- OBJ | ||||||||||||||||||||||||||||||||||||||||||
| 1202 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1203 | |||||||||||||||||||||||||||||||||||||||||||
| 1204 | 0 | ▶ | sub mesh_to_obj ($self, $mesh, $name='object') { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mesh_to_obj: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1205 | 0 | my $out = "# Generated by Graphics::Penplotter::GcodeXY::Geometry3D\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1206 | 0 | $out .= "o $name\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1207 | 0 | for my $v (@{ $mesh->{verts} }) { | |||||||||||||||||||||||||||||||||||||||||
| 1208 | 0 | $out .= sprintf("v %.6f %.6f %.6f\n", @$v); | |||||||||||||||||||||||||||||||||||||||||
| 1209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1210 | 0 | for my $f (@{ $mesh->{faces} }) { | |||||||||||||||||||||||||||||||||||||||||
| 1211 | 0 | $out .= sprintf("f %d %d %d\n", $f->[0]+1, $f->[1]+1, $f->[2]+1); | |||||||||||||||||||||||||||||||||||||||||
| 1212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1213 | 0 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 1214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1215 | |||||||||||||||||||||||||||||||||||||||||||
| 1216 | 0 | ▶ | sub mesh_from_obj ($self, $str) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mesh_from_obj: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1217 | 0 | my (@verts, @faces); | |||||||||||||||||||||||||||||||||||||||||
| 1218 | 0 | for my $line (split /\n/, $str) { | |||||||||||||||||||||||||||||||||||||||||
| 1219 | 0 | $line =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 1220 | 0 | ▶ | if ($line =~ /^v\s+([\d.eE+\-]+)\s+([\d.eE+\-]+)\s+([\d.eE+\-]+)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1221 | 0 | push @verts, [$1+0, $2+0, $3+0]; | |||||||||||||||||||||||||||||||||||||||||
| 1222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1223 | elsif ($line =~ /^f\s+(\S+)\s+(\S+)\s+(\S+)/) { | ||||||||||||||||||||||||||||||||||||||||||
| 1224 | 0 | my @idx = map { (split m{/}, $_)[0] - 1 } ($1,$2,$3); | |||||||||||||||||||||||||||||||||||||||||
| 1225 | 0 | push @faces, \@idx; | |||||||||||||||||||||||||||||||||||||||||
| 1226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1228 | 0 | return $self->mesh(\@verts, \@faces); | |||||||||||||||||||||||||||||||||||||||||
| 1229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1230 | |||||||||||||||||||||||||||||||||||||||||||
| 1231 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1232 | # SECTION: Mesh I/O -- STL (ASCII) | ||||||||||||||||||||||||||||||||||||||||||
| 1233 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1234 | |||||||||||||||||||||||||||||||||||||||||||
| 1235 | 0 | ▶ | sub mesh_to_stl ($self, $mesh, $name='solid') { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mesh_to_stl: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1236 | 0 | my $out = "solid $name\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1237 | 0 | my @v = @{ $mesh->{verts} }; | |||||||||||||||||||||||||||||||||||||||||
| 1238 | 0 | for my $f (@{ $mesh->{faces} }) { | |||||||||||||||||||||||||||||||||||||||||
| 1239 | 0 | my ($a,$b,$c) = @$f; | |||||||||||||||||||||||||||||||||||||||||
| 1240 | 0 | my ($ux,$uy,$uz) = map { $v[$b][$_]-$v[$a][$_] } 0..2; | |||||||||||||||||||||||||||||||||||||||||
| 1241 | 0 | my ($wx,$wy,$wz) = map { $v[$c][$_]-$v[$a][$_] } 0..2; | |||||||||||||||||||||||||||||||||||||||||
| 1242 | 0 | my ($nx,$ny,$nz) = ($uy*$wz-$uz*$wy, $uz*$wx-$ux*$wz, $ux*$wy-$uy*$wx); | |||||||||||||||||||||||||||||||||||||||||
| 1243 | 0 | ▶ | my $l = sqrt($nx*$nx+$ny*$ny+$nz*$nz) || 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
sqrt $nx * $nx + $ny * $ny + $nz * $nz || 1
MC/DC0%0/1
sqrt $nx * $nx + $ny * $ny + $nz * $nz || 1
sqrt $nx * $nx + $ny * $ny + $nz * $nz
Truth table0%0/2
A
A= sqrt $nx * $nx + $ny * $ny + $nz * $nz
| |||||||||||||||||||||||||||||||||||||||||||
| 1244 | 0 | $out .= sprintf(" facet normal %.6f %.6f %.6f\n", $nx/$l,$ny/$l,$nz/$l); | |||||||||||||||||||||||||||||||||||||||||
| 1245 | 0 | $out .= " outer loop\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1246 | 0 | $out .= sprintf(" vertex %.6f %.6f %.6f\n", @{$v[$a]}); | |||||||||||||||||||||||||||||||||||||||||
| 1247 | 0 | $out .= sprintf(" vertex %.6f %.6f %.6f\n", @{$v[$b]}); | |||||||||||||||||||||||||||||||||||||||||
| 1248 | 0 | $out .= sprintf(" vertex %.6f %.6f %.6f\n", @{$v[$c]}); | |||||||||||||||||||||||||||||||||||||||||
| 1249 | 0 | $out .= " endloop\n endfacet\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1251 | 0 | $out .= "endsolid $name\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1252 | 0 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 1253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1254 | |||||||||||||||||||||||||||||||||||||||||||
| 1255 | 0 | ▶ | sub mesh_from_stl ($self, $str) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mesh_from_stl: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1256 | 0 | my (@verts, @faces, %seen, @pending); | |||||||||||||||||||||||||||||||||||||||||
| 1257 | 0 | ▶ | my $vi = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1258 | 0 | my ($x,$y,$z) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1259 | 0 | my $key = sprintf("%.9f,%.9f,%.9f", $x,$y,$z); | |||||||||||||||||||||||||||||||||||||||||
| 1260 | 0 | ▶ | unless (exists $seen{$key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1261 | 0 | $seen{$key} = scalar @verts; | |||||||||||||||||||||||||||||||||||||||||
| 1262 | 0 | push @verts, [$x,$y,$z]; | |||||||||||||||||||||||||||||||||||||||||
| 1263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1264 | 0 | return $seen{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 1265 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 1266 | 0 | for my $line (split /\n/, $str) { | |||||||||||||||||||||||||||||||||||||||||
| 1267 | 0 | $line =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 1268 | 0 | ▶ | if ($line =~ /^vertex\s+([\d.eE+\-]+)\s+([\d.eE+\-]+)\s+([\d.eE+\-]+)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1269 | 0 | push @pending, $vi->($1+0,$2+0,$3+0); | |||||||||||||||||||||||||||||||||||||||||
| 1270 | 0 | ▶ | if (@pending == 3) { push @faces, [@pending]; @pending = () } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1273 | 0 | return $self->mesh(\@verts, \@faces); | |||||||||||||||||||||||||||||||||||||||||
| 1274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1275 | |||||||||||||||||||||||||||||||||||||||||||
| 1276 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1277 | # SECTION: Numeric configuration | ||||||||||||||||||||||||||||||||||||||||||
| 1278 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1279 | |||||||||||||||||||||||||||||||||||||||||||
| 1280 | 0 | ▶ | sub set_tolerance ($self, $eps) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_tolerance: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1281 | 0 | ▶ | $eps > 0 or $self->_croak('set_tolerance: eps must be positive'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1282 | 0 | $self->{_g3_tolerance} = $eps; | |||||||||||||||||||||||||||||||||||||||||
| 1283 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1285 | |||||||||||||||||||||||||||||||||||||||||||
| 1286 | 0 | ▶ | sub get_tolerance ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_tolerance: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1287 | 0 | ▶ | return $self->{_g3_tolerance} // $EPSILON; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"_g3_tolerance"} // $EPSILON
MC/DC0%0/2
$self->{"_g3_tolerance"} // $EPSILON
$self->{"_g3_tolerance"}
$EPSILON
Truth table0%0/3
A // B
A= $self->{"_g3_tolerance"}B= $EPSILON
| |||||||||||||||||||||||||||||||||||||||||||
| 1288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1289 | |||||||||||||||||||||||||||||||||||||||||||
| 1290 | 0 | ▶ | sub set_units ($self, $units) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_units: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1291 | 0 | $self->{_g3_units} = $units; | |||||||||||||||||||||||||||||||||||||||||
| 1292 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1294 | |||||||||||||||||||||||||||||||||||||||||||
| 1295 | 0 | ▶ | sub set_coordinate_convention ($self, %opts) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_coordinate_convention: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1296 | 0 | ▶ | $self->{_g3_handedness} = $opts{handedness} if defined $opts{handedness}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1297 | 0 | ▶ | $self->{_g3_euler_order} = $opts{euler_order} if defined $opts{euler_order}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1298 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1300 | |||||||||||||||||||||||||||||||||||||||||||
| 1301 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1302 | # SECTION: Camera | ||||||||||||||||||||||||||||||||||||||||||
| 1303 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1304 | |||||||||||||||||||||||||||||||||||||||||||
| 1305 | # Set the camera using a gluLookAt-style interface. | ||||||||||||||||||||||||||||||||||||||||||
| 1306 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1307 | # Named arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1308 | # eye => [$ex,$ey,$ez] Camera (eye) position in world space. Default [0,0,1]. | ||||||||||||||||||||||||||||||||||||||||||
| 1309 | # center => [$cx,$cy,$cz] Point the camera looks at. Default [0,0,0]. | ||||||||||||||||||||||||||||||||||||||||||
| 1310 | # up => [$ux,$uy,$uz] World-space up hint vector. Default [0,1,0]. | ||||||||||||||||||||||||||||||||||||||||||
| 1311 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1312 | # The method computes an orthonormal right-handed camera basis | ||||||||||||||||||||||||||||||||||||||||||
| 1313 | # (right, up, -forward) and stores: | ||||||||||||||||||||||||||||||||||||||||||
| 1314 | # _g3_camera->{eye} the eye position | ||||||||||||||||||||||||||||||||||||||||||
| 1315 | # _g3_camera->{center} the look-at point | ||||||||||||||||||||||||||||||||||||||||||
| 1316 | # _g3_camera->{up} the recomputed, orthogonalised up vector | ||||||||||||||||||||||||||||||||||||||||||
| 1317 | # _g3_camera->{fwd} the unit forward vector (from eye toward center) | ||||||||||||||||||||||||||||||||||||||||||
| 1318 | # _g3_camera->{view} the 4x4 world-to-camera view matrix | ||||||||||||||||||||||||||||||||||||||||||
| 1319 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1320 | # After calling set_camera() you can optionally call camera_to_ctm() to | ||||||||||||||||||||||||||||||||||||||||||
| 1321 | # bake the view matrix into the 3-D CTM so that transform_point() and all | ||||||||||||||||||||||||||||||||||||||||||
| 1322 | # drawing calls automatically include the camera transform. | ||||||||||||||||||||||||||||||||||||||||||
| 1323 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1324 | # backface_cull() automatically picks up the stored forward vector when no | ||||||||||||||||||||||||||||||||||||||||||
| 1325 | # explicit view_dir option is supplied. | ||||||||||||||||||||||||||||||||||||||||||
| 1326 | |||||||||||||||||||||||||||||||||||||||||||
| 1327 | 0 | ▶ | sub set_camera ($self, %args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_camera: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1328 | 0 | ▶ | my $eye = $args{eye} // [0, 0, 1]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"eye"} // [0, 0, 1]
MC/DC0%0/1
$args{"eye"} // [0, 0, 1]
$args{"eye"}
Truth table0%0/2
A
A= $args{"eye"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1329 | 0 | ▶ | my $center = $args{center} // [0, 0, 0]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"center"} // [0, 0, 0]
MC/DC0%0/1
$args{"center"} // [0, 0, 0]
$args{"center"}
Truth table0%0/2
A
A= $args{"center"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1330 | 0 | ▶ | my $up = $args{up} // [0, 1, 0]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"up"} // [0, 1, 0]
MC/DC0%0/1
$args{"up"} // [0, 1, 0]
$args{"up"}
Truth table0%0/2
A
A= $args{"up"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1331 | |||||||||||||||||||||||||||||||||||||||||||
| 1332 | 0 | ▶ | ref $eye eq 'ARRAY' && @$eye == 3 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $eye eq "ARRAY" and @$eye == 3
MC/DC0%0/2
ref $eye eq "ARRAY" and @$eye == 3
ref $eye eq "ARRAY"
@$eye == 3
Truth table0%0/3
A and B
A= ref $eye eq "ARRAY"B= @$eye == 3
| |||||||||||||||||||||||||||||||||||||||||||
| 1333 | or $self->_croak('set_camera: eye must be an arrayref [x,y,z]'); | ||||||||||||||||||||||||||||||||||||||||||
| 1334 | 0 | ▶ | ref $center eq 'ARRAY' && @$center == 3 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $center eq "ARRAY" and @$center == 3
MC/DC0%0/2
ref $center eq "ARRAY" and @$center == 3
ref $center eq "ARRAY"
@$center == 3
Truth table0%0/3
A and B
A= ref $center eq "ARRAY"B= @$center == 3
| |||||||||||||||||||||||||||||||||||||||||||
| 1335 | or $self->_croak('set_camera: center must be an arrayref [x,y,z]'); | ||||||||||||||||||||||||||||||||||||||||||
| 1336 | 0 | ▶ | ref $up eq 'ARRAY' && @$up == 3 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $up eq "ARRAY" and @$up == 3
MC/DC0%0/2
ref $up eq "ARRAY" and @$up == 3
ref $up eq "ARRAY"
@$up == 3
Truth table0%0/3
A and B
A= ref $up eq "ARRAY"B= @$up == 3
| |||||||||||||||||||||||||||||||||||||||||||
| 1337 | or $self->_croak('set_camera: up must be an arrayref [x,y,z]'); | ||||||||||||||||||||||||||||||||||||||||||
| 1338 | |||||||||||||||||||||||||||||||||||||||||||
| 1339 | # Forward vector: eye -> center | ||||||||||||||||||||||||||||||||||||||||||
| 1340 | 0 | my ($fx,$fy,$fz) = map { $center->[$_] - $eye->[$_] } 0..2; | |||||||||||||||||||||||||||||||||||||||||
| 1341 | 0 | my $fl = sqrt($fx*$fx + $fy*$fy + $fz*$fz); | |||||||||||||||||||||||||||||||||||||||||
| 1342 | 0 | ▶ | $self->_croak('set_camera: eye and center cannot be the same point') if $fl < $EPSILON; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1343 | 0 | ($fx,$fy,$fz) = ($fx/$fl, $fy/$fl, $fz/$fl); | |||||||||||||||||||||||||||||||||||||||||
| 1344 | |||||||||||||||||||||||||||||||||||||||||||
| 1345 | # Right vector: forward × up | ||||||||||||||||||||||||||||||||||||||||||
| 1346 | 0 | my ($ux,$uy,$uz) = @$up; | |||||||||||||||||||||||||||||||||||||||||
| 1347 | 0 | my ($rx,$ry,$rz) = ( | |||||||||||||||||||||||||||||||||||||||||
| 1348 | $fy*$uz - $fz*$uy, | ||||||||||||||||||||||||||||||||||||||||||
| 1349 | $fz*$ux - $fx*$uz, | ||||||||||||||||||||||||||||||||||||||||||
| 1350 | $fx*$uy - $fy*$ux, | ||||||||||||||||||||||||||||||||||||||||||
| 1351 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1352 | 0 | my $rl = sqrt($rx*$rx + $ry*$ry + $rz*$rz); | |||||||||||||||||||||||||||||||||||||||||
| 1353 | 0 | ▶ | $self->_croak('set_camera: up vector is parallel to the view direction') if $rl < $EPSILON; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1354 | 0 | ($rx,$ry,$rz) = ($rx/$rl, $ry/$rl, $rz/$rl); | |||||||||||||||||||||||||||||||||||||||||
| 1355 | |||||||||||||||||||||||||||||||||||||||||||
| 1356 | # Recompute orthogonal up: right × forward | ||||||||||||||||||||||||||||||||||||||||||
| 1357 | 0 | ($ux,$uy,$uz) = ( | |||||||||||||||||||||||||||||||||||||||||
| 1358 | $ry*$fz - $rz*$fy, | ||||||||||||||||||||||||||||||||||||||||||
| 1359 | $rz*$fx - $rx*$fz, | ||||||||||||||||||||||||||||||||||||||||||
| 1360 | $rx*$fy - $ry*$fx, | ||||||||||||||||||||||||||||||||||||||||||
| 1361 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1362 | |||||||||||||||||||||||||||||||||||||||||||
| 1363 | # World-to-camera view matrix (row-major, same convention as the rest of the role). | ||||||||||||||||||||||||||||||||||||||||||
| 1364 | # Column layout: [right | up | -forward | translation] | ||||||||||||||||||||||||||||||||||||||||||
| 1365 | 0 | my $tx = -($rx*$eye->[0] + $ry*$eye->[1] + $rz*$eye->[2]); | |||||||||||||||||||||||||||||||||||||||||
| 1366 | 0 | my $ty = -($ux*$eye->[0] + $uy*$eye->[1] + $uz*$eye->[2]); | |||||||||||||||||||||||||||||||||||||||||
| 1367 | 0 | my $tz = $fx*$eye->[0] + $fy*$eye->[1] + $fz*$eye->[2]; # +fwd row | |||||||||||||||||||||||||||||||||||||||||
| 1368 | 0 | my $view = [ | |||||||||||||||||||||||||||||||||||||||||
| 1369 | [ $rx, $ry, $rz, $tx ], | ||||||||||||||||||||||||||||||||||||||||||
| 1370 | [ $ux, $uy, $uz, $ty ], | ||||||||||||||||||||||||||||||||||||||||||
| 1371 | [-$fx, -$fy, -$fz, $tz ], | ||||||||||||||||||||||||||||||||||||||||||
| 1372 | [ 0, 0, 0, 1 ], | ||||||||||||||||||||||||||||||||||||||||||
| 1373 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 1374 | |||||||||||||||||||||||||||||||||||||||||||
| 1375 | $self->{_g3_camera} = { | ||||||||||||||||||||||||||||||||||||||||||
| 1376 | 0 | eye => [ @$eye ], | |||||||||||||||||||||||||||||||||||||||||
| 1377 | center => [ @$center ], | ||||||||||||||||||||||||||||||||||||||||||
| 1378 | up => [ $ux, $uy, $uz ], | ||||||||||||||||||||||||||||||||||||||||||
| 1379 | fwd => [ $fx, $fy, $fz ], | ||||||||||||||||||||||||||||||||||||||||||
| 1380 | view => $view, | ||||||||||||||||||||||||||||||||||||||||||
| 1381 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1382 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1383 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1384 | |||||||||||||||||||||||||||||||||||||||||||
| 1385 | # Return the camera record set by set_camera(), or undef if none has been set. | ||||||||||||||||||||||||||||||||||||||||||
| 1386 | 0 | ▶ | sub get_camera ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_camera: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1387 | 0 | return $self->{_g3_camera}; | |||||||||||||||||||||||||||||||||||||||||
| 1388 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1389 | |||||||||||||||||||||||||||||||||||||||||||
| 1390 | # Bake the stored view matrix into the 3-D CTM by pre-multiplying. | ||||||||||||||||||||||||||||||||||||||||||
| 1391 | # After this call transform_point() automatically includes the camera transform. | ||||||||||||||||||||||||||||||||||||||||||
| 1392 | # Typically called once after set_camera() and before any drawing commands. | ||||||||||||||||||||||||||||||||||||||||||
| 1393 | 0 | ▶ | sub camera_to_ctm ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
camera_to_ctm: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1394 | my $cam = $self->{_g3_camera} | ||||||||||||||||||||||||||||||||||||||||||
| 1395 | 0 | ▶ | or $self->_croak('camera_to_ctm: no camera set; call set_camera() first'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1396 | 0 | $self->_g3_premul4($cam->{view}, _g3_ctm($self)); | |||||||||||||||||||||||||||||||||||||||||
| 1397 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1398 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1399 | |||||||||||||||||||||||||||||||||||||||||||
| 1400 | |||||||||||||||||||||||||||||||||||||||||||
| 1401 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1402 | # SECTION: Perspective projection | ||||||||||||||||||||||||||||||||||||||||||
| 1403 | # ========================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1404 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1405 | # A perspective projection matrix introduces a 1/z foreshortening term. | ||||||||||||||||||||||||||||||||||||||||||
| 1406 | # When pre-multiplied into the CTM via perspective_to_ctm(), transform_point() | ||||||||||||||||||||||||||||||||||||||||||
| 1407 | # computes the homogeneous weight tw = -z (for objects in front of a camera | ||||||||||||||||||||||||||||||||||||||||||
| 1408 | # looking down -Z), and the existing perspective divide (tw != 0,1 branch) | ||||||||||||||||||||||||||||||||||||||||||
| 1409 | # produces correct perspective-foreshortened x and y coordinates. | ||||||||||||||||||||||||||||||||||||||||||
| 1410 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1411 | # Typical workflow: | ||||||||||||||||||||||||||||||||||||||||||
| 1412 | # $g->initmatrix3(); | ||||||||||||||||||||||||||||||||||||||||||
| 1413 | # $g->set_camera(eye => [5,5,10], center => [0,0,0]); | ||||||||||||||||||||||||||||||||||||||||||
| 1414 | # $g->camera_to_ctm(); # bake view matrix | ||||||||||||||||||||||||||||||||||||||||||
| 1415 | # $g->set_perspective(fov => 45, aspect => 1.0, near => 0.1, far => 100); | ||||||||||||||||||||||||||||||||||||||||||
| 1416 | # $g->perspective_to_ctm(); # bake projection matrix (P x V x CTM) | ||||||||||||||||||||||||||||||||||||||||||
| 1417 | # # ... drawing calls now include full perspective ... | ||||||||||||||||||||||||||||||||||||||||||
| 1418 | |||||||||||||||||||||||||||||||||||||||||||
| 1419 | # Build and store a symmetric perspective projection matrix (gluPerspective | ||||||||||||||||||||||||||||||||||||||||||
| 1420 | # style). Does NOT modify the CTM; call perspective_to_ctm() to apply it. | ||||||||||||||||||||||||||||||||||||||||||
| 1421 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1422 | # Named arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1423 | # fov => degrees Vertical field of view (default 45). | ||||||||||||||||||||||||||||||||||||||||||
| 1424 | # aspect => ratio Width / height ratio (default 1.0). | ||||||||||||||||||||||||||||||||||||||||||
| 1425 | # near => distance Near clip distance, > 0 (default 0.1). | ||||||||||||||||||||||||||||||||||||||||||
| 1426 | # far => distance Far clip distance, > near (default 100). | ||||||||||||||||||||||||||||||||||||||||||
| 1427 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1428 | # The resulting 4x4 matrix (row-major, column-vector convention) is: | ||||||||||||||||||||||||||||||||||||||||||
| 1429 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1430 | # [ f/aspect 0 0 0 ] | ||||||||||||||||||||||||||||||||||||||||||
| 1431 | # [ 0 f 0 0 ] | ||||||||||||||||||||||||||||||||||||||||||
| 1432 | # [ 0 0 (far+near)/(near-far) 2*far*near/(near-far) ] | ||||||||||||||||||||||||||||||||||||||||||
| 1433 | # [ 0 0 -1 0 ] | ||||||||||||||||||||||||||||||||||||||||||
| 1434 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1435 | # where f = cot(fov/2). Row 3 sets tw = -z, triggering the perspective | ||||||||||||||||||||||||||||||||||||||||||
| 1436 | # divide in transform_point() for every drawn point. | ||||||||||||||||||||||||||||||||||||||||||
| 1437 | |||||||||||||||||||||||||||||||||||||||||||
| 1438 | 0 | ▶ | sub set_perspective ($self, %args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_perspective: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1439 | 0 | ▶ | my $fov = $args{fov} // 45.0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"fov"} // 45
MC/DC0%0/1
$args{"fov"} // 45
$args{"fov"}
Truth table0%0/2
A
A= $args{"fov"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1440 | 0 | ▶ | my $aspect = $args{aspect} // 1.0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"aspect"} // 1
MC/DC0%0/1
$args{"aspect"} // 1
$args{"aspect"}
Truth table0%0/2
A
A= $args{"aspect"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1441 | 0 | ▶ | my $near = $args{near} // 0.1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"near"} // "0.1"
MC/DC0%0/1
$args{"near"} // "0.1"
$args{"near"}
Truth table0%0/2
A
A= $args{"near"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1442 | 0 | ▶ | my $far = $args{far} // 100.0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"far"} // 100
MC/DC0%0/1
$args{"far"} // 100
$args{"far"}
Truth table0%0/2
A
A= $args{"far"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1443 | |||||||||||||||||||||||||||||||||||||||||||
| 1444 | 0 | ▶ | $self->_croak('set_perspective: fov must be > 0 and < 180') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$fov > 0 and $fov < 180
MC/DC0%0/2
$fov > 0 and $fov < 180
$fov > 0
$fov < 180
Truth table0%0/3
A and B
A= $fov > 0B= $fov < 180
| |||||||||||||||||||||||||||||||||||||||||||
| 1445 | unless $fov > 0 && $fov < 180; | ||||||||||||||||||||||||||||||||||||||||||
| 1446 | 0 | ▶ | $self->_croak('set_perspective: aspect must be > 0') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1447 | unless $aspect > 0; | ||||||||||||||||||||||||||||||||||||||||||
| 1448 | 0 | ▶ | $self->_croak('set_perspective: near must be > 0') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1449 | unless $near > 0; | ||||||||||||||||||||||||||||||||||||||||||
| 1450 | 0 | ▶ | $self->_croak('set_perspective: far must be > near') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1451 | unless $far > $near; | ||||||||||||||||||||||||||||||||||||||||||
| 1452 | |||||||||||||||||||||||||||||||||||||||||||
| 1453 | 0 | my $f = 1.0 / tan( $fov * $D2R / 2.0 ); | |||||||||||||||||||||||||||||||||||||||||
| 1454 | 0 | my $nf = 1.0 / ($near - $far); | |||||||||||||||||||||||||||||||||||||||||
| 1455 | |||||||||||||||||||||||||||||||||||||||||||
| 1456 | $self->{_g3_projection} = [ | ||||||||||||||||||||||||||||||||||||||||||
| 1457 | 0 | [ $f/$aspect, 0, 0, 0 ], | |||||||||||||||||||||||||||||||||||||||||
| 1458 | [ 0, $f, 0, 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 1459 | [ 0, 0, ($far+$near)*$nf, 2*$far*$near*$nf ], | ||||||||||||||||||||||||||||||||||||||||||
| 1460 | [ 0, 0, -1, 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 1461 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 1462 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1463 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1464 | |||||||||||||||||||||||||||||||||||||||||||
| 1465 | # Build and store an asymmetric (off-axis) perspective projection matrix | ||||||||||||||||||||||||||||||||||||||||||
| 1466 | # (glFrustum style). Does NOT modify the CTM; call perspective_to_ctm() | ||||||||||||||||||||||||||||||||||||||||||
| 1467 | # to apply it. | ||||||||||||||||||||||||||||||||||||||||||
| 1468 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1469 | # Named arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1470 | # left => x coordinate of the left clipping plane at near distance | ||||||||||||||||||||||||||||||||||||||||||
| 1471 | # right => x coordinate of the right clipping plane at near distance | ||||||||||||||||||||||||||||||||||||||||||
| 1472 | # bottom => y coordinate of the bottom clipping plane at near distance | ||||||||||||||||||||||||||||||||||||||||||
| 1473 | # top => y coordinate of the top clipping plane at near distance | ||||||||||||||||||||||||||||||||||||||||||
| 1474 | # near => near clip distance (> 0) | ||||||||||||||||||||||||||||||||||||||||||
| 1475 | # far => far clip distance (> near) | ||||||||||||||||||||||||||||||||||||||||||
| 1476 | # | ||||||||||||||||||||||||||||||||||||||||||
| 1477 | # Useful for stereo rendering, anamorphic projections, or any off-centre | ||||||||||||||||||||||||||||||||||||||||||
| 1478 | # viewport. The symmetric case (left=-right, bottom=-top) is equivalent to | ||||||||||||||||||||||||||||||||||||||||||
| 1479 | # set_perspective() with the corresponding fov and aspect ratio. | ||||||||||||||||||||||||||||||||||||||||||
| 1480 | |||||||||||||||||||||||||||||||||||||||||||
| 1481 | 0 | ▶ | sub set_frustum ($self, %args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_frustum: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1482 | 0 | my $left = $args{left}; | |||||||||||||||||||||||||||||||||||||||||
| 1483 | 0 | my $right = $args{right}; | |||||||||||||||||||||||||||||||||||||||||
| 1484 | 0 | my $bottom = $args{bottom}; | |||||||||||||||||||||||||||||||||||||||||
| 1485 | 0 | my $top = $args{top}; | |||||||||||||||||||||||||||||||||||||||||
| 1486 | 0 | ▶ | my $near = $args{near} // 0.1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"near"} // "0.1"
MC/DC0%0/1
$args{"near"} // "0.1"
$args{"near"}
Truth table0%0/2
A
A= $args{"near"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1487 | 0 | ▶ | my $far = $args{far} // 100.0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"far"} // 100
MC/DC0%0/1
$args{"far"} // 100
$args{"far"}
Truth table0%0/2
A
A= $args{"far"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1488 | |||||||||||||||||||||||||||||||||||||||||||
| 1489 | 0 | for my $name (qw(left right bottom top)) { | |||||||||||||||||||||||||||||||||||||||||
| 1490 | $self->_croak("set_frustum: '$name' is required") | ||||||||||||||||||||||||||||||||||||||||||
| 1491 | 0 | ▶ | unless defined $args{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1492 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1493 | 0 | ▶ | $self->_croak('set_frustum: near must be > 0') unless $near > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1494 | 0 | ▶ | $self->_croak('set_frustum: far must be > near') unless $far > $near; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1495 | 0 | ▶ | $self->_croak('set_frustum: left must not equal right') unless $right != $left; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1496 | 0 | ▶ | $self->_croak('set_frustum: bottom must not equal top') unless $top != $bottom; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1497 | |||||||||||||||||||||||||||||||||||||||||||
| 1498 | 0 | my $rl = 1.0 / ($right - $left); | |||||||||||||||||||||||||||||||||||||||||
| 1499 | 0 | my $tb = 1.0 / ($top - $bottom); | |||||||||||||||||||||||||||||||||||||||||
| 1500 | 0 | my $nf = 1.0 / ($near - $far); | |||||||||||||||||||||||||||||||||||||||||
| 1501 | 0 | my $n2 = 2.0 * $near; | |||||||||||||||||||||||||||||||||||||||||
| 1502 | |||||||||||||||||||||||||||||||||||||||||||
| 1503 | $self->{_g3_projection} = [ | ||||||||||||||||||||||||||||||||||||||||||
| 1504 | 0 | [ $n2*$rl, 0, ($right+$left)*$rl, 0 ], | |||||||||||||||||||||||||||||||||||||||||
| 1505 | [ 0, $n2*$tb, ($top+$bottom)*$tb, 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 1506 | [ 0, 0, ($far+$near)*$nf, 2*$far*$near*$nf ], | ||||||||||||||||||||||||||||||||||||||||||
| 1507 | [ 0, 0, -1, 0 ], | ||||||||||||||||||||||||||||||||||||||||||
| 1508 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 1509 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1510 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1511 | |||||||||||||||||||||||||||||||||||||||||||
| 1512 | # Pre-multiply the stored projection matrix into the 3-D CTM (CTM := P x CTM). | ||||||||||||||||||||||||||||||||||||||||||
| 1513 | # After this call transform_point() applies the full camera + projection | ||||||||||||||||||||||||||||||||||||||||||
| 1514 | # pipeline to every point. Croaks if set_perspective() or set_frustum() | ||||||||||||||||||||||||||||||||||||||||||
| 1515 | # has not been called first. | ||||||||||||||||||||||||||||||||||||||||||
| 1516 | |||||||||||||||||||||||||||||||||||||||||||
| 1517 | 0 | ▶ | sub perspective_to_ctm ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
perspective_to_ctm: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1518 | my $proj = $self->{_g3_projection} | ||||||||||||||||||||||||||||||||||||||||||
| 1519 | 0 | ▶ | or $self->_croak( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1520 | 'perspective_to_ctm: no projection set; ' | ||||||||||||||||||||||||||||||||||||||||||
| 1521 | . 'call set_perspective() or set_frustum() first' | ||||||||||||||||||||||||||||||||||||||||||
| 1522 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1523 | 0 | $self->_g3_premul4($proj, _g3_ctm($self)); | |||||||||||||||||||||||||||||||||||||||||
| 1524 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1525 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1526 | |||||||||||||||||||||||||||||||||||||||||||
| 1527 | # Return the stored projection matrix, or undef if none has been set. | ||||||||||||||||||||||||||||||||||||||||||
| 1528 | |||||||||||||||||||||||||||||||||||||||||||
| 1529 | 0 | ▶ | sub get_projection ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_projection: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1530 | 0 | return $self->{_g3_projection}; | |||||||||||||||||||||||||||||||||||||||||
| 1531 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1532 | |||||||||||||||||||||||||||||||||||||||||||
| 1533 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 1534 | |||||||||||||||||||||||||||||||||||||||||||
| 1535 | __END__ | ||||||||||||||||||||||||||||||||||||||||||