Skip to content

Commit 19b5145

Browse files
committed
add tests for Quaternion, normalize_this -> fluent normalise
1 parent c6074de commit 19b5145

File tree

7 files changed

+38
-18
lines changed

7 files changed

+38
-18
lines changed

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ MANIFEST This list of files
4545
MANIFEST.SKIP
4646
t/arcball.t
4747
t/opengl.t
48+
t/quaternion.t

lib/PDL/Graphics/TriD/ArcBall.pm

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ sub normxy2qua {
8686
if ($dist > 1.0) {$x /= $dist; $y /= $dist; $dist = 1.0;}
8787
my $z = 1-$dist;
8888
my $qua = PDL::Graphics::TriD::Quaternion->new(0,$x,$y,$z);
89-
$qua->normalize_this();
90-
return $qua;
89+
$qua->normalise;
9190
}
9291

9392
# Tjl's version2: a bowl -- angle is proportional to displacement.
@@ -103,8 +102,7 @@ sub normxy2qua {
103102
if ($dist > 1.0) {$x /= $dist; $y /= $dist; $dist = 1.0;}
104103
my $z = cos($dist*3.142/2);
105104
my $qua = PDL::Graphics::TriD::Quaternion->new(0,$x,$y,$z);
106-
$qua->normalize_this();
107-
return $qua;
105+
$qua->normalise;
108106
}
109107

110108
1;

lib/PDL/Graphics/TriD/Control3D.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ sub new{
2525
}
2626

2727
sub normalize { my($this) = @_;
28-
$this->{WRotation}->normalize_this();
29-
$this->{CRotation}->normalize_this();
28+
$this->{WRotation}->normalise;
29+
$this->{CRotation}->normalise;
3030
}
3131

3232
sub reset {

lib/PDL/Graphics/TriD/GL.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ sub PDL::Graphics::TriD::Quaternion::togl {
175175
my($this) = @_;
176176
if(abs($this->[0]) == 1) { return ; }
177177
if(abs($this->[0]) >= 1) {
178-
$this->normalize_this();
178+
$this->normalise;
179179
}
180180
glRotatef(2*POSIX::acos($this->[0])/3.14*180, @{$this}[1..3]);
181181
}

lib/PDL/Graphics/TriD/Quaternion.pm

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# Stored as [c,x,y,z].
88
#
9-
# XXX REMEMBER!!!! First component = cos(angle*2), *NOT* cos(angle)
9+
# XXX REMEMBER!!!! First component = cos(angle/2), *NOT* cos(angle)
1010

1111
package PDL::Graphics::TriD::Quaternion;
1212

@@ -29,15 +29,14 @@ sub new {
2929
}
3030

3131
sub copy {
32-
return PDL::Graphics::TriD::Quaternion->new(@{$_[0]});
32+
PDL::Graphics::TriD::Quaternion->new(@{$_[0]});
3333
}
3434

3535
sub new_vrmlrot {
3636
my($type,$x,$y,$z,$c) = @_;
3737
my $l = sqrt($x**2+$y**2+$z**2);
3838
my $this = bless [cos($c/2),map {sin($c/2)*$_/$l} $x,$y,$z],$type;
39-
$this->normalize_this();
40-
return $this;
39+
$this->normalise;
4140
}
4241

4342
sub to_vrmlrot {
@@ -123,15 +122,16 @@ sub invert_rotation_this {
123122
$this->[0] = - $this->[0];
124123
}
125124

126-
sub normalize_this {
127-
my($this) = @_;
128-
my $abs = sqrt($this->abssq());
129-
@$this = map {$_/$abs} @$this;
125+
sub normalise {
126+
my($this) = @_;
127+
my $abs = sqrt($this->abssq);
128+
@$this = map $_/$abs, @$this;
129+
$this;
130130
}
131131

132132
sub rotate {
133133
my($this,$vec) = @_;
134-
my $q = (PDL::Graphics::TriD::Quaternion)->new(0,@$vec);
134+
my $q = PDL::Graphics::TriD::Quaternion->new(0,@$vec);
135135
my $m = $this->multiply($q->multiply($this->invert));
136136
return [@$m[1..3]];
137137
}

lib/PDL/Graphics/TriD/VRML.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ sub PDL::Graphics::TriD::Transformation::tovrml {
153153
sub PDL::Graphics::TriD::Quaternion::tovrml {my($this) = @_;
154154
if(abs($this->[0]) == 1) { return ; }
155155
if(abs($this->[0]) >= 1) {
156-
# die "Unnormalized Quaternion!\n";
157-
$this->normalize_this();
156+
# die "Unnormalised Quaternion!\n";
157+
$this->normalise;
158158
}
159159
PDL::Graphics::VRMLNode->new('Transform',
160160
'rotation',vrml3v(@{$this}[1..3])." $this->[0]");

t/quaternion.t

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use strict;
2+
use warnings;
3+
use Test::More;
4+
5+
use PDL::Graphics::TriD::Quaternion;
6+
use PDL::LiteF;
7+
use Test::PDL;
8+
9+
sub is_qua {
10+
local $Test::Builder::Level = $Test::Builder::Level + 1;
11+
my ($got, $exp) = map PDL->pdl(@$_), @_;
12+
is_pdl $got, $exp;
13+
}
14+
15+
my $q = PDL::Graphics::TriD::Quaternion->new(0,0,0,1);
16+
isa_ok $q, 'PDL::Graphics::TriD::Quaternion';
17+
is_qua $q, [0,0,0,1];
18+
19+
is_qua +PDL::Graphics::TriD::Quaternion->new(0,0,0,2)->normalise, [0,0,0,1];
20+
21+
done_testing;

0 commit comments

Comments
 (0)