| 1 | ########################################################## | ||
| 2 | # to do's: | ||
| 3 | # 1) matrix correction for singular matrixes | ||
| 4 | # 2) tidy up yml files search - done | ||
| 5 | # 3) header generation - done (kindof) | ||
| 6 | # 4) bounding box calculation - not needed as povray does it | ||
| 7 | # 5) metallic colors | ||
| 8 | # 6) special color handling | ||
| 9 | # 7) perl macro comments | ||
| 10 | # | ||
| 11 | ########################################################## | ||
| 12 | |||
| 13 | package Lego::Ldraw::POV; | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; no warnings qw/void uninitialized/; | ||
| 17 | |||
| 18 | use Carp; | ||
| 19 | |||
| 20 | use Lego::Ldraw::Line; | ||
| 21 | use Lego::Ldraw; | ||
| 22 | |||
| 23 | use YAML; | ||
| 24 | use Template; | ||
| 25 | |||
| 26 | my $self = {}; | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my $proto = shift; | ||
| 30 | my $class = ref $proto || $proto; | ||
| 31 | |||
| 32 | my $ldraw = shift; | ||
| 33 | $self->{ldraw} = \$ldraw | ||
| 34 | if $ldraw; | ||
| 35 | |||
| 36 | $self->{primitives_file} = "primitives.yml"; | ||
| 37 | $self->{colors_file} = "colors.yml"; | ||
| 38 | |||
| 39 | bless ($self, $class); | ||
| 40 | return $self; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub model { | ||
| 44 | return $self->{model} | ||
| 45 | } | ||
| 46 | |||
| 47 | sub pov_color { | ||
| 48 | shift; | ||
| 49 | my $color = shift; | ||
| 50 | $color = 0 if (($color == 16) || ($color == 24)); | ||
| 51 | |||
| 52 | my $primitives; | ||
| 53 | unless ($self->{colordefs}) { | ||
| 54 | open DATA, $self->ymlfile('colors'); | ||
| 55 | $primitives = do { local $/; <DATA> }; | ||
| 56 | $self->{colordefs} = Load($primitives); | ||
| 57 | } | ||
| 58 | my $def = $self->{colordefs}->{rgb}->{$color} || 'pigment { rgb <0.5,0.5,0.5> }'; | ||
| 59 | return unless $def; | ||
| 60 | |||
| 61 | $self->{template} = Template->new | ||
| 62 | unless $self->{template}; | ||
| 63 | |||
| 64 | my $type = $def =~ /filter/ ? 'normal' : 'transparent'; | ||
| 65 | my $template = $self->{colordefs}->{colordecl}->{$type}; | ||
| 66 | my $vars = { | ||
| 67 | color_name => "Color$color", | ||
| 68 | color_def => $def | ||
| 69 | }; | ||
| 70 | |||
| 71 | my $output; | ||
| 72 | $self->{template}->process(\$template, $vars, \$output); | ||
| 73 | return $output; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _pov_name { | ||
| 77 | my $name = shift; | ||
| 78 | for ($name) { | ||
| 79 | s/^(\d)/_$1/; # initial digit | ||
| 80 | s/\./_dot_/; # dot in name | ||
| 81 | s/\-/_dash_/; # dash in name | ||
| 82 | } | ||
| 83 | return $name | ||
| 84 | } | ||
| 85 | |||
| 86 | sub toPOV { | ||
| 87 | shift; | ||
| 88 | my $part = shift; | ||
| 89 | my $ldraw; | ||
| 90 | my $ref = ref $part; | ||
| 91 | |||
| 92 | for ($ref) { | ||
| 93 | # just a part name | ||
| 94 | /^$/ && do { | ||
| 95 | $part = Lego::Ldraw::Line->new_from_part_name($part); | ||
| 96 | $part->model(${$self->{ldraw}}); | ||
| 97 | $ldraw = $part->explode; | ||
| 98 | last; | ||
| 99 | }; | ||
| 100 | # a part line | ||
| 101 | /Line$/ && do { | ||
| 102 | $part->model(${$self->{ldraw}}); | ||
| 103 | $ldraw = $part->explode; | ||
| 104 | last; | ||
| 105 | }; | ||
| 106 | # a model | ||
| 107 | $ldraw = $part; | ||
| 108 | }; | ||
| 109 | |||
| 110 | my $pov_name = $part->pov_name; | ||
| 111 | unless ($self->{primitives}) { | ||
| 112 | my $primitives; | ||
| 113 | open DATA, $self->ymlfile('primitives'); | ||
| 114 | $primitives = do { local $/; <DATA> }; | ||
| 115 | $self->{primitives} = Load($primitives); | ||
| 116 | } | ||
| 117 | |||
| 118 | if (my $primitive = $self->{primitives}->{$pov_name}) { | ||
| 119 | $self->{model} .= "$primitive\n\n"; | ||
| 120 | } else { | ||
| 121 | $self->{model} .= '#declare ' . $pov_name . " = union {\n"; | ||
| 122 | $self->{model} .= join '', '// ' , ($part->description || 'no description available'), "\n"; | ||
| 123 | my @mesh = grep { $_->type == 3 or $_->type == 4 } $ldraw->lines; | ||
| 124 | if (@mesh) { | ||
| 125 | $self->{model} .= "\tmesh {\n"; | ||
| 126 | for (@mesh) { | ||
| 127 | $self->{model} .= "\t\t" . $_->toPOV . "\n"; | ||
| 128 | $self->{colors}->{$_->color}++; | ||
| 129 | } | ||
| 130 | $self->{model} .= "\t}\n"; | ||
| 131 | } | ||
| 132 | for (grep { $_->type == 1 } $ldraw->lines) { | ||
| 133 | $self->{model} .= (join '', "\t", $_->toPOV, "\n"); | ||
| 134 | $self->{colors}->{$_->color}++; | ||
| 135 | } | ||
| 136 | $self->{model} .= "}\n\n"; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | sub colors { | ||
| 141 | shift; | ||
| 142 | return keys %{$self->{colors}} | ||
| 143 | } | ||
| 144 | |||
| 145 | sub colordef { | ||
| 146 | for ($self->colors) { | ||
| 147 | $self->{colordef} .= ($self->pov_color($_) . "\n\n"); | ||
| 148 | } | ||
| 149 | return $self->{colordef}; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub ymlfile { | ||
| 153 | shift; | ||
| 154 | my $type = shift; | ||
| 155 | carp "File type unknow" unless ($type =~ /^primitives$/ || $type =~ /^colou*rs$/); | ||
| 156 | |||
| 157 | local $_ = __PACKAGE__; | ||
| 158 | |||
| 159 | # get the directory the package resides in | ||
| 160 | s/::[^:]+$//; | ||
| 161 | s/::/\//g; | ||
| 162 | my $pkgdir = $_; | ||
| 163 | |||
| 164 | my $file; | ||
| 165 | for ('.', $ENV{'HOME'}, Lego::Ldraw->basedir, map { join '/', $_, $pkgdir } @INC) { | ||
| 166 | if (-e join '/', $_, "$type.yml") { | ||
| 167 | $file = join '/', $_, "$type.yml"; | ||
| 168 | last; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | return $file; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub header { | ||
| 175 | shift; | ||
| 176 | my $header = <<EOF; | ||
| 177 | #declare QUAL = 2; // Quality level, 0=BBox, 1=no refr, 2=normal, 3=studlogo | ||
| 178 | |||
| 179 | #declare SW = 0.5; // Width of seam between two bricks | ||
| 180 | |||
| 181 | #declare STUDS = 1; // 1=on 0=off | ||
| 182 | |||
| 183 | #declare BUMPS = 0; // 1=on 0=off | ||
| 184 | |||
| 185 | |||
| 186 | #declare BUMPNORMAL = normal { bumps 0.01 scale 20 } | ||
| 187 | #declare AMB = 0.4; | ||
| 188 | #declare DIF = 0.4; | ||
| 189 | |||
| 190 | |||
| 191 | #declare O7071 = sqrt(0.5); | ||
| 192 | |||
| 193 | #declare L3Logo = union { | ||
| 194 | sphere {<-59,0,-96>,6} | ||
| 195 | cylinder {<-59,0,-96>,<59,0,-122>,6 open} | ||
| 196 | sphere {<59,0,-122>,6} | ||
| 197 | cylinder {<59,0,-122>,<59,0,-84>,6 open} | ||
| 198 | sphere {<59,0,-84>,6} | ||
| 199 | |||
| 200 | sphere {<-59,0,-36>,6} | ||
| 201 | cylinder {<-59,0,-36>,<-59,0,1>,6 open} | ||
| 202 | sphere {<-59,0,1>,6} | ||
| 203 | cylinder {<0,0,-49>,<0,0,-25>,6 open} | ||
| 204 | sphere {<0,0,-25>,6} | ||
| 205 | sphere {<59,0,-62>,6} | ||
| 206 | cylinder {<59,0,-62>,<59,0,-24>,6 open} | ||
| 207 | sphere {<59,0,-24>,6} | ||
| 208 | cylinder {<-59,0,-36>,<59,0,-62>,6 open} | ||
| 209 | |||
| 210 | sphere {<-35.95,0,57>,6} | ||
| 211 | torus {18.45,6 clipped_by{plane{<40,0,-9>,0}} translate<-40,0,39>} | ||
| 212 | cylinder {<-44.05,0,21>,<35.95,0,3>,6 open} | ||
| 213 | torus {18.45,6 clipped_by{plane{<-40,0,9>,0}} translate<40,0,21>} | ||
| 214 | cylinder {<44.05,0,39>,<0,0,49>,6 open} | ||
| 215 | sphere {<0,0,49>,6} | ||
| 216 | cylinder {<0,0,49>,<0,0,34>,6 open} | ||
| 217 | sphere {<0,0,34>,6} | ||
| 218 | |||
| 219 | torus {18.45,6 clipped_by{plane{<40,0,-9>,0}} translate<-40,0,99>} | ||
| 220 | cylinder {<-44.05,0,81>,<35.95,0,63>,6 open} | ||
| 221 | torus {18.45,6 clipped_by{plane{<-40,0,9>,0}} translate<40,0,81>} | ||
| 222 | cylinder {<44.05,0,99>,<-35.95,0,117>,6 open} | ||
| 223 | |||
| 224 | scale 4.5/128 | ||
| 225 | } | ||
| 226 | EOF | ||
| 227 | return $header; | ||
| 228 | } | ||
| 229 | |||
| 230 | ########################################################## | ||
| 231 | # Lego::Ldraw subs | ||
| 232 | ########################################################## | ||
| 233 | |||
| 234 | sub Lego::Ldraw::POVdesc { | ||
| 235 | my $self = shift; | ||
| 236 | my $ldraw = $self->copy; | ||
| 237 | Lego::Ldraw::POV->new($ldraw); | ||
| 238 | my $callback = sub { Lego::Ldraw::POV->toPOV( shift ) }; | ||
| 239 | $self->build_tree($callback); | ||
| 240 | Lego::Ldraw::POV->toPOV($self); | ||
| 241 | return (join "\n\n", | ||
| 242 | ( | ||
| 243 | Lego::Ldraw::POV->header, | ||
| 244 | Lego::Ldraw::POV->colordef, | ||
| 245 | Lego::Ldraw::POV->model | ||
| 246 | ) | ||
| 247 | ); | ||
| 248 | } | ||
| 249 | |||
| 250 | sub Lego::Ldraw::pov_name { | ||
| 251 | return _pov_name(shift->name); | ||
| 252 | } | ||
| 253 | |||
| 254 | ########################################################## | ||
| 255 | # Lego::Ldraw::Line subs | ||
| 256 | ########################################################## | ||
| 257 | |||
| 258 | sub Lego::Ldraw::Line::pov_name { | ||
| 259 | return _pov_name(shift->name); | ||
| 260 | } | ||
| 261 | |||
| 262 | sub Lego::Ldraw::Line::pov_coords { | ||
| 263 | my @i = shift->coords; | ||
| 264 | my @p; | ||
| 265 | while (@i) { | ||
| 266 | my @d = splice @i, 0, 3; | ||
| 267 | push @p, (join ', ', @d); | ||
| 268 | } | ||
| 269 | return '<' . (join '>, <', @p) . '>'; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub Lego::Ldraw::Line::pov_matrix { | ||
| 273 | my @m = shift->transform_matrix; | ||
| 274 | my $m = join ', ', @m[3, 6, 9, 4, 7, 10, 5, 8, 11, 0, 1, 2]; | ||
| 275 | return "matrix <$m>"; | ||
| 276 | } | ||
| 277 | |||
| 278 | sub Lego::Ldraw::Line::pov_material { | ||
| 279 | my $self = shift; | ||
| 280 | return if (($self->color == 16) || ($self->color == 24)); | ||
| 281 | my $col = $self->color; | ||
| 282 | return "material { Color$col }"; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub Lego::Ldraw::Line::quad_to_triangs { | ||
| 286 | my $self = shift; | ||
| 287 | return unless $self->type == 4; | ||
| 288 | |||
| 289 | my $a = $self->new; | ||
| 290 | $a->{type} = 3; | ||
| 291 | my @f = qw/colour x1 y1 z1 x2 y2 z2 x3 y3 z3/; | ||
| 292 | for (@f) { | ||
| 293 | $a->{$_} = $self->{$_}; | ||
| 294 | } | ||
| 295 | |||
| 296 | my $b = $self->new; | ||
| 297 | $b->{type} = 3; | ||
| 298 | for (qw/colour x1 y1 z1 x3 y3 z3 x4 y4 z4/) { | ||
| 299 | $b->{shift @f} = $self->{$_}; | ||
| 300 | } | ||
| 301 | return ($a, $b); | ||
| 302 | } | ||
| 303 | |||
| 304 | sub Lego::Ldraw::Line::det { | ||
| 305 | return shift->_transform_matrix->det() | ||
| 306 | } | ||
| 307 | |||
| 308 | sub Lego::Ldraw::Line::toPOV { | ||
| 309 | my $self = shift; | ||
| 310 | for ($self->type) { | ||
| 311 | /1/ && do { | ||
| 312 | if ($self->det) { | ||
| 313 | return join ' ', ('object {', $self->pov_name, $self->pov_matrix, $self->pov_material, '}'); | ||
| 314 | } else { | ||
| 315 | return join ' ', ('// object {', $self->pov_name, $self->pov_matrix, '}'); | ||
| 316 | } | ||
| 317 | last; | ||
| 318 | }; | ||
| 319 | /3/ && do { | ||
| 320 | return ('triangle {' . $self->pov_coords . '}'); | ||
| 321 | last; | ||
| 322 | }; | ||
| 323 | /4/ && do { | ||
| 324 | my ($a, $b) = $self->quad_to_triangs; | ||
| 325 | return ('triangle {' . $a->pov_coords . "}\n\t\t\ttriangle {" . $b->pov_coords . "}"); | ||
| 326 | last; | ||
| 327 | }; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | |||
| 331 | 1 |