File Coverage

blib/lib/VRML/Base.pm
Criterion Covered Total %
statement 66 250 26.4
branch 5 80 6.2
condition 0 18 0.0
subroutine 8 28 28.5
pod 9 19 47.3
total 88 395 22.2


line stmt bran cond sub pod time code
1             package VRML::Base;
2              
3             ############################## Copyright ##############################
4             # #
5             # This program is Copyright 1996,1998 by Hartmut Palm. #
6             # This program is free software; you can redistribute it and/or #
7             # modify it under the terms of the GNU General Public License #
8             # as published by the Free Software Foundation; either version 2 #
9             # of the License, or (at your option) any later version. #
10             # #
11             # This program is distributed in the hope that it will be useful, #
12             # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14             # GNU General Public License for more details. #
15             # #
16             # If you do not have a copy of the GNU General Public License write #
17             # to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, #
18             # MA 02139, USA. #
19             # #
20             #######################################################################
21              
22             require 5.000;
23 2     2   10 use strict;
  2         4  
  2         81  
24 2     2   12 use vars qw($VERSION $PI $PI_2);
  2         3  
  2         7144  
25              
26             $VERSION = "1.07";
27             $PI = 3.1415926;
28             $PI_2 = $PI/2;
29              
30             sub new {
31 12     12 1 10 my $class = shift;
32 12         14 my $tabs = shift;
33 12         16 my $self = {};
34 12 50       35 $self->{'TAB'} = defined $tabs ? "\t" x $tabs : "";
35 12         12 $self->{'TAB_VIEW'} = "";
36 12         15 $self->{'DEBUG'} = 0;
37 12         15 $self->{'VERSION'} = 0; # VRML specification
38 12         12 $self->{'CONVERT'} = 1; # convert degree to radiant
39 12         13 $self->{'BROWSER'} = "";# Which VRML + HTML browser
40 12         15 $self->{'VRML'} = []; # THE VRML array
41 12         24 $self->{'DEF'} = {}; # remember DEFs
42 12         12 $self->{'PROTO'} = {}; # remember PROTOs
43 12         25 $self->{'XYZ'} = [[0,0,0]];
44 12         11 $self->{'Xmax'} = 0;
45 12         65 $self->{'Ymax'} = 0;
46 12         15 $self->{'Zmax'} = 0;
47 12         10 $self->{'Xmin'} = 0;
48 12         10 $self->{'Ymin'} = 0;
49 12         25 $self->{'Zmin'} = 0;
50 12         15 $self->{'DX'} = 0;
51 12         10 $self->{'DY'} = 0;
52 12         10 $self->{'DZ'} = 0;
53 12         14 $self->{'ID'} = 0;
54 12         30 return bless $self, $class;
55             }
56              
57             sub browser {
58 0     0 0 0 my $self = shift;
59 0 0       0 return unless $_[0]; # @_ wouldn't work on PC
60 0         0 ($self->{'BROWSER'}) = join("+",@_);
61             $self->_put("# Set Browser to: '$self->{'browser'}'\n")
62 0 0       0 if $self->{'DEBUG'};
63 0         0 return $self;
64             }
65              
66             #####################################################################
67             # Methods to modify the VRML array
68             #
69             #####################################################################
70              
71             sub _init {
72 0     0   0 my $self = shift;
73 0         0 my $vrml = shift;
74 0         0 $self->{'VRML'} = [$vrml];
75 0         0 return $self;
76             }
77              
78             sub _add {
79 0     0   0 my $self = shift;
80 0         0 my $vrml = shift;
81 0         0 ${$self->{'VRML'}}[$#{$self->{'VRML'}}] .= $vrml;
  0         0  
  0         0  
82 0         0 return $self;
83             }
84              
85             sub _put {
86 0     0   0 my $self = shift;
87 0         0 my $vrml = shift;
88 0         0 push @{$self->{'VRML'}}, $vrml;
  0         0  
89 0         0 return $self;
90             }
91              
92             sub _row {
93 0     0   0 my $self = shift;
94 0         0 my ($row) = @_;
95 0         0 my $vrml = $self->{'TAB'}.$row;
96 0         0 push @{$self->{'VRML'}}, $vrml;
  0         0  
97 0         0 return $self;
98             }
99              
100             sub _swap {
101 0     0   0 my $self = shift;
102 0         0 my $element1 = pop @{$self->{'VRML'}};
  0         0  
103 0         0 my $element2 = pop @{$self->{'VRML'}};
  0         0  
104 0         0 push @{$self->{'VRML'}}, $element1;
  0         0  
105 0         0 push @{$self->{'VRML'}}, $element2;
  0         0  
106 0         0 return $self;
107             }
108              
109             sub _pos {
110 0     0   0 my $self = shift;
111 0         0 return $#{$self->{'VRML'}};
  0         0  
112             }
113              
114             sub _trim {
115 19     19   21 my $self = shift;
116 19         23 my ($index) = @_;
117 19 50       33 $index = $#{$self->{'VRML'}} unless defined $index;
  0         0  
118 19         121 $self->{'VRML'}[$index-1] =~ s/\s+$/ /;
119 19         65 $self->{'VRML'}[$index] =~ s/^\s+//;
120 19         34 return $self;
121             }
122              
123             sub debug {
124 0     0 1 0 my $self = shift;
125 0         0 $self->{'DEBUG'} = shift;
126 0         0 $self->_put("# Set Debug Level to $self->{'DEBUG'}\n");
127 0         0 return $self;
128             }
129              
130             sub display_vars {
131 0     0 0 0 my $self = shift;
132 0 0       0 my @keys = @_ ? @_ : sort keys %$self;
133 0         0 my $key;
134 0         0 foreach $key (@keys) {
135 0 0       0 unless (defined $self->{$key}) {
136 0         0 print "# $key => undef\n";
137 0         0 next;
138             }
139 0         0 print "# $key => $self->{$key}";
140 0         0 print " [".(join(', ',@{$self->{$key}}))."]" if defined
141 0 0 0     0 ref($self->{$key}) && ref($self->{$key}) eq "ARRAY" &&
      0        
142             $key ne "VRML";
143 0         0 print " [".(join(', ',sort keys %{$self->{$key}}))."]"
144 0 0 0     0 if defined ref($self->{$key}) && ref($self->{$key}) eq "HASH";
145 0         0 print "\n";
146             }
147 0         0 return $self;
148             }
149              
150             sub string_to_array {
151 8     8 0 10 my $self = shift;
152 8         11 my $pt = shift;
153 8 50       21 return @$pt if ref $pt eq 'ARRAY';
154             # remove leading/trailing spaces!
155 8         10 my $tmp = $pt; $tmp =~ s/^\s+//; $tmp =~ s/\s+$//;
  8         24  
  8         22  
156 8         38 return split(/\s+/,$tmp);
157             }
158              
159             #--------------------------------------------------------------------
160             # Insert Comments
161             #--------------------------------------------------------------------
162             sub comment {
163 0     0 1 0 my $self = shift;
164 0         0 my ($comment) = @_;
165 0 0       0 $comment = defined $comment ? "# ".$comment : "#";
166 0         0 push @{$self->{'VRML'}}, $comment."\n";
  0         0  
167 0         0 return $self;
168             }
169              
170             #--------------------------------------------------------------------
171             # In-/Output VRML
172             #--------------------------------------------------------------------
173             sub insert {
174 0     0 1 0 my $self = shift;
175 0         0 my $string = shift;
176 0         0 $string =~ s/^\s+|\s+$//g;
177 0         0 $string = $self->{'TAB'}.$string;
178 0         0 $string =~ s/\n/\n$self->{'TAB'}/g;
179 0         0 push @{$self->{'VRML'}}, $string."\n";
  0         0  
180 0         0 return $self;
181             }
182              
183             sub insert__DATA__ {
184 0     0 1 0 my $self = shift;
185 0 0       0 $self->{'DATApos'} = tell(main::DATA) unless defined $self->{'DATApos'};
186 0 0       0 print " self->{'DATApos'}=$self->{'DATApos'}\n" if $self->{'DEBUG'};
187 0         0 push @{$self->{'VRML'}}, ,"\n";
  0         0  
188 0         0 seek(main::DATA,$self->{'DATApos'},0);
189 0         0 return $self;
190             }
191              
192             sub include {
193 0     0 1 0 my $self = shift;
194 0         0 my @filename = @_;
195 0 0 0     0 return $self if !defined $filename[0] || $filename[0] eq "";
196 0         0 foreach (@filename) {
197 0 0       0 open(INCLUDE, "<$_") || die "Can't include \"$_\"\n$!\n";
198 0         0 push @{$self->{'VRML'}}, ;
  0         0  
199 0         0 push @{$self->{'VRML'}}, "\n";
  0         0  
200 0         0 close(INCLUDE);
201             }
202 0         0 return $self;
203             }
204              
205             sub format {
206 0     0 0 0 my $self = shift;
207 0         0 my $format = shift;
208 0 0 0     0 if (defined $format && $format eq "none") {
209 0         0 map { s/^[\t ]+//; s/\n[\t ]+/\n/g; s/\t+/ /g; s/ +/ /g; } @{$self->{'VRML'}};
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
210             } else {
211 0         0 map { s/ / /g; s/\t/ /g; } @{$self->{'VRML'}};
  0         0  
  0         0  
  0         0  
212             }
213 0         0 return $self;
214             }
215              
216             sub print {
217 0     0 1 0 my $self = shift;
218 0         0 my $mime = shift;
219 0         0 my $pipe = shift;
220 0         0 select STDOUT; $|=1;
  0         0  
221             print "Content-type: $self->{'Content-type'}\n\n"
222 0 0 0     0 if $self->{'Content-type'} && $mime;
223 0 0       0 if ($pipe) {
224 0 0       0 open(PIPE, "|$pipe") || die; select PIPE; $|=1;
  0         0  
  0         0  
225 0         0 for (@{$self->{'VRML'}}) { print; }
  0         0  
  0         0  
226 0         0 select STDOUT;
227 0         0 close(PIPE);
228             } else {
229 0         0 for (@{$self->{'VRML'}}) { print; }
  0         0  
  0         0  
230             }
231 0         0 return $self;
232             }
233              
234             sub save {
235 0     0 1 0 my $self = shift;
236 0         0 my $filename = shift;
237 0         0 my $pipe = shift;
238 0 0       0 unless (defined $filename) {
239 0         0 ($filename) = $0 =~ m/(.*)\./;
240 0         0 $filename .= ".wrl";
241             }
242 0 0       0 open(VRMLFILE, ">$filename") ||
243             die "Can't create file: \"$filename\" ($!)\n";
244 0 0       0 if ($pipe) {
245 0         0 print VRMLFILE "Can't pipe to \"$pipe\"\n";
246 0         0 close(VRMLFILE);
247 0 0       0 open(VRMLFILE, "| $pipe > $filename") || die;
248             }
249 0         0 for (@{$self->{'VRML'}}) { print VRMLFILE; }
  0         0  
  0         0  
250 0         0 close(VRMLFILE);
251 0         0 return $self;
252             }
253              
254             sub as_string {
255 12     12 1 15 my $self = shift;
256 12         12 my $vrml = "";
257 12         8 for (@{$self->{'VRML'}}) { $vrml .= $_ };
  12         22  
  81         99  
258 12         40 return $vrml;
259             }
260              
261             #--------------------------------------------------------------------
262              
263             sub escape {
264 0     0 0 0 my $self = shift;
265 0         0 shift;
266             }
267              
268             sub ascii {
269 1     1 0 2 my $self = shift;
270 1         1 local $_ = undef;
271 1         3 foreach (@_) {
272 1         2 s/[\204\344\365]/ae/g;
273 1         2 s/[\224\366]/oe/g;
274 1         1 s/[\201\374]/ue/g;
275 1         2 s/[\216\304]/Ae/g;
276 1         1 s/[\231\305\326]/Oe/g;
277 1         1 s/[\263\334]/Ue/g;
278 1         1 s/[\257\337]/sz/g;
279 1         2 s/[\000-\010\013-\037\177-\377]/_/g;
280             }
281 1 50       2 return (@_) if wantarray;
282 1         3 return $_[0];
283             }
284              
285             sub utf8 {
286 1     1 0 2 my $self = shift;
287 1         2 local $_ = undef;
288             #foreach (@_) {
289             # s/[\201\374]/\x00\x75\x03\x08/g;
290             # s/(.)/\x00$1/g;
291             #}
292 1 50       4 return (@_) if wantarray;
293 1         6 return $_[0];
294             }
295              
296             sub xyz {
297 0     0 0   my $self = shift;
298 0 0         return $self unless @_;
299 0           ($self->{'DX'}, $self->{'DY'}, $self->{'DZ'}) = @_;
300 0           ${$self->{'XYZ'}[0]}[0] += $self->{'DX'};
  0            
301 0           ${$self->{'XYZ'}[0]}[1] += $self->{'DY'};
  0            
302 0           ${$self->{'XYZ'}[0]}[2] += $self->{'DZ'};
  0            
303 0           print "XYZ = ".join(', ',@{$self->{'XYZ'}[0]})."\n"
304 0 0         if $self->{'DEBUG'} == 2;
305 0           $self->{'Xmax'} = ${$self->{'XYZ'}[0]}[0]
306 0 0         if $self->{'Xmax'} < ${$self->{'XYZ'}[0]}[0];
  0            
307 0           $self->{'Ymax'} = ${$self->{'XYZ'}[0]}[1]
308 0 0         if $self->{'Ymax'} < ${$self->{'XYZ'}[0]}[1];
  0            
309 0           $self->{'Zmax'} = ${$self->{'XYZ'}[0]}[2]
310 0 0         if $self->{'Zmax'} < ${$self->{'XYZ'}[0]}[2];
  0            
311 0           $self->{'Xmin'} = ${$self->{'XYZ'}[0]}[0]
312 0 0         if $self->{'Xmin'} > ${$self->{'XYZ'}[0]}[0];
  0            
313 0           $self->{'Ymin'} = ${$self->{'XYZ'}[0]}[1]
314 0 0         if $self->{'Ymin'} > ${$self->{'XYZ'}[0]}[1];
  0            
315 0           $self->{'Zmin'} = ${$self->{'XYZ'}[0]}[2]
316 0 0         if $self->{'Zmin'} > ${$self->{'XYZ'}[0]}[2];
  0            
317             }
318              
319             sub bboxCenter {
320 0     0 0   my $self = shift;
321 0           my ($x, $y, $z) = @_;
322 0 0         $x = ($self->{'Xmax'} + $self->{'Xmin'})/2 unless defined $x;
323 0 0         $y = ($self->{'Ymax'} + $self->{'Ymin'})/2 unless defined $y;
324 0 0         $z = ($self->{'Zmax'} + $self->{'Zmin'})/2 unless defined $z;
325 0 0         return ($x, $y, $z) if wantarray;
326 0           return "$x $y $z";
327             }
328              
329             sub bboxSize {
330 0     0 0   my $self = shift;
331 0           my ($dx, $dy, $dz) = @_;
332 0 0         $dx = $self->{'Xmax'} - $self->{'Xmin'} unless defined $dx;
333 0 0         $dy = $self->{'Ymax'} - $self->{'Ymin'} unless defined $dy;
334 0 0         $dz = $self->{'Zmax'} - $self->{'Zmin'} unless defined $dz;
335 0 0         return ($dx, $dy, $dz) if wantarray;
336 0           return "$dx $dy $dz";
337             }
338              
339             1;
340              
341             __END__