| 1 | 6 | ▶ | use Modern::Perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | package Intertangle::Yarn::Graphene; | ||
| 3 | # ABSTRACT: Load the Graphene graphic types library | ||
| 4 | $Intertangle::Yarn::Graphene::VERSION = '0.002'; | ||
| 5 | 0 | ▶ | use Glib::Object::Introspection; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use DynaLoader; | ||
| 7 | |||
| 8 | my $_GRAPHENE_BASENAME = 'Graphene'; | ||
| 9 | my $_GRAPHENE_VERSION = '1.0'; | ||
| 10 | my $_GRAPHENE_PACKAGE = __PACKAGE__; | ||
| 11 | |||
| 12 | my @_FLATTEN_ARRAY_REF_RETURN_FOR = qw/ | ||
| 13 | /; | ||
| 14 | |||
| 15 | use Env qw(@GI_TYPELIB_PATH @PATH); | ||
| 16 | use Alien::Graphene; | ||
| 17 | |||
| 18 | sub import { | ||
| 19 | my @search_path = (); | ||
| 20 | if( Alien::Graphene->install_type eq 'share' ) { | ||
| 21 | unshift @GI_TYPELIB_PATH, Alien::Graphene->gi_typelib_path; | ||
| 22 | @search_path = ( search_path => Alien::Graphene->gi_typelib_path ); | ||
| 23 | if( $^O eq 'MSWin32' ) { | ||
| 24 | push @PATH, Alien::Graphene->rpath; | ||
| 25 | } else { | ||
| 26 | push @DynaLoader::dl_library_path, Alien::Graphene->rpath; | ||
| 27 | my @files = DynaLoader::dl_findfile("-lgraphene-1.0"); | ||
| 28 | DynaLoader::dl_load_file($files[0]) if @files; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | Glib::Object::Introspection->setup( | ||
| 33 | basename => $_GRAPHENE_BASENAME, | ||
| 34 | version => $_GRAPHENE_VERSION, | ||
| 35 | package => $_GRAPHENE_PACKAGE, | ||
| 36 | flatten_array_ref_return_for => \@_FLATTEN_ARRAY_REF_RETURN_FOR, | ||
| 37 | @search_path, | ||
| 38 | ); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub Inline { | ||
| 42 | return unless $_[-1] eq 'C'; | ||
| 43 | |||
| 44 | require Intertangle::API::Glib; | ||
| 45 | require Hash::Merge; | ||
| 46 | my $glib = Intertangle::API::Glib->Inline($_[-1]); | ||
| 47 | |||
| 48 | my @nosearch = $^O eq 'MSWin32' ? (':nosearch') : (); | ||
| 49 | my @search = $^O eq 'MSWin32' ? ( ':search' ) : (); | ||
| 50 | my $graphene = { | ||
| 51 | CCFLAGSEX => join(" ", delete $glib->{CCFLAGSEX}, Alien::Graphene->cflags), | ||
| 52 | LIBS => join(" ", @nosearch, delete $glib->{LIBS}, Alien::Graphene->libs, @search), | ||
| 53 | AUTO_INCLUDE => <<C, | ||
| 54 | #include <graphene.h> | ||
| 55 | #include <graphene-gobject.h> | ||
| 56 | C | ||
| 57 | }; | ||
| 58 | |||
| 59 | my $merge = Hash::Merge->new('RETAINMENT_PRECEDENT'); | ||
| 60 | $merge->merge( $glib, $graphene ); | ||
| 61 | } | ||
| 62 | |||
| 63 | package Intertangle::Yarn::Graphene::DataPrinterRole { | ||
| 64 | use Role::Tiny; | ||
| 65 | use Module::Load; | ||
| 66 | |||
| 67 | BEGIN { | ||
| 68 | eval { | ||
| 69 | autoload Data::Printer::Filter; | ||
| 70 | autoload Term::ANSIColor; | ||
| 71 | autoload Package::Stash; | ||
| 72 | }; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _data_printer { | ||
| 76 | my ($self, $ddp) = @_; | ||
| 77 | |||
| 78 | my $FIELDS = Package::Stash->new( ref $self )->get_symbol( '@FIELDS' ); | ||
| 79 | my $data = { | ||
| 80 | map { $_ => $self->$_ } @$FIELDS | ||
| 81 | }; | ||
| 82 | |||
| 83 | my $text = ''; | ||
| 84 | |||
| 85 | $text .= "("; | ||
| 86 | $text .= $ddp->maybe_colorize( ref($self), 'class' ); | ||
| 87 | $text .= ") "; | ||
| 88 | my $ml_save = $ddp->multiline(0); | ||
| 89 | $text .= $ddp->parse($data); | ||
| 90 | $ddp->multiline($ml_save); | ||
| 91 | |||
| 92 | $text; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | $Intertangle::Yarn::Graphene::DataPrinterRole::VERSION = '0.002'; | ||
| 96 | |||
| 97 | |||
| 98 | package Intertangle::Yarn::Graphene::Size { | ||
| 99 | our @FIELDS = qw(width height); | ||
| 100 | use Role::Tiny::With; | ||
| 101 | with 'Intertangle::Yarn::Graphene::DataPrinterRole'; | ||
| 102 | use overload | ||
| 103 | '""' => \&op_str, | ||
| 104 | 'eq' => \&op_eq, | ||
| 105 | '==' => \&op_eq; | ||
| 106 | |||
| 107 | sub op_str { | ||
| 108 | "[w: @{[ $_[0]->width ]}, h: @{[ $_[0]->height ]}]" | ||
| 109 | } | ||
| 110 | |||
| 111 | sub op_eq { | ||
| 112 | $_[0]->width == (Scalar::Util::blessed $_[1] ? $_[1]->width : $_[1]->[0] ) | ||
| 113 | && $_[0]->height == (Scalar::Util::blessed $_[1] ? $_[1]->height : $_[1]->[1] ) | ||
| 114 | } | ||
| 115 | |||
| 116 | sub to_HashRef { | ||
| 117 | +{ map { $_ => $_[0]->$_ } @FIELDS }; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | $Intertangle::Yarn::Graphene::Size::VERSION = '0.002'; | ||
| 121 | |||
| 122 | package Intertangle::Yarn::Graphene::Point { | ||
| 123 | our @FIELDS = qw(x y); | ||
| 124 | use Scalar::Util; | ||
| 125 | use Role::Tiny::With; | ||
| 126 | with 'Intertangle::Yarn::Graphene::DataPrinterRole'; | ||
| 127 | use overload | ||
| 128 | '""' => \&op_str, | ||
| 129 | 'eq' => \&op_eq, | ||
| 130 | '==' => \&op_eq, | ||
| 131 | 'neg' => \&op_neg; | ||
| 132 | |||
| 133 | sub op_str { | ||
| 134 | "[x: @{[ $_[0]->x ]}, y: @{[ $_[0]->y ]}]"; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub op_eq { | ||
| 138 | $_[0]->x == (Scalar::Util::blessed $_[1] ? $_[1]->x : $_[1]->[0] ) | ||
| 139 | && $_[0]->y == (Scalar::Util::blessed $_[1] ? $_[1]->y : $_[1]->[1] ) | ||
| 140 | } | ||
| 141 | |||
| 142 | sub op_neg { | ||
| 143 | Intertangle::Yarn::Graphene::Point->new( | ||
| 144 | x => - $_[0]->x, | ||
| 145 | y => - $_[0]->y, | ||
| 146 | ); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub to_HashRef { | ||
| 150 | +{ map { $_ => $_[0]->$_ } @FIELDS }; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub to_ArrayRef { | ||
| 154 | [ map { $_[0]->$_ } @FIELDS ]; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub to_Point3D { | ||
| 158 | my ($self) = @_; | ||
| 159 | my $point3d = Intertangle::Yarn::Graphene::Point3D->new( | ||
| 160 | x => $self->x, | ||
| 161 | y => $self->y, | ||
| 162 | z => 0, | ||
| 163 | ); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | $Intertangle::Yarn::Graphene::Point::VERSION = '0.002'; | ||
| 167 | |||
| 168 | package Intertangle::Yarn::Graphene::Point3D { | ||
| 169 | our @FIELDS = qw(x y z); | ||
| 170 | use Scalar::Util; | ||
| 171 | use Role::Tiny::With; | ||
| 172 | with 'Intertangle::Yarn::Graphene::DataPrinterRole'; | ||
| 173 | use overload | ||
| 174 | '""' => \&op_str, | ||
| 175 | 'eq' => \&op_eq, | ||
| 176 | '==' => \&op_eq; | ||
| 177 | |||
| 178 | sub op_str { | ||
| 179 | "[x: @{[ $_[0]->x ]}, y: @{[ $_[0]->y ]}], y: @{[ $_[0]->z ]}"; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub op_eq { | ||
| 183 | $_[0]->x == (Scalar::Util::blessed $_[1] ? $_[1]->x : $_[1]->[0] ) | ||
| 184 | && $_[0]->y == (Scalar::Util::blessed $_[1] ? $_[1]->y : $_[1]->[1] ) | ||
| 185 | && $_[0]->z == (Scalar::Util::blessed $_[1] ? $_[1]->z : $_[1]->[1] ) | ||
| 186 | } | ||
| 187 | |||
| 188 | sub to_HashRef { | ||
| 189 | +{ map { $_ => $_[0]->$_ } @FIELDS }; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | $Intertangle::Yarn::Graphene::Point3D::VERSION = '0.002'; | ||
| 193 | |||
| 194 | package Intertangle::Yarn::Graphene::Vec2 { | ||
| 195 | our @FIELDS = qw(x y); | ||
| 196 | use Scalar::Util; | ||
| 197 | use Role::Tiny::With; | ||
| 198 | with 'Intertangle::Yarn::Graphene::DataPrinterRole'; | ||
| 199 | use overload | ||
| 200 | '""' => \&op_str, | ||
| 201 | 'eq' => \&op_eq, | ||
| 202 | '==' => \&op_eq, | ||
| 203 | 'neg' => \&op_neg; | ||
| 204 | |||
| 205 | sub new { | ||
| 206 | my ($class, %args) = @_; | ||
| 207 | |||
| 208 | my $vec2 = Intertangle::Yarn::Graphene::Vec2->alloc(); | ||
| 209 | $vec2->init( $args{x}, $args{y} ); | ||
| 210 | |||
| 211 | $vec2; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub x { | ||
| 215 | $_[0]->dot( Intertangle::Yarn::Graphene::Vec2::x_axis() ); | ||
| 216 | } | ||
| 217 | |||
| 218 | sub y { | ||
| 219 | $_[0]->dot( Intertangle::Yarn::Graphene::Vec2::y_axis() ); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub op_str { | ||
| 223 | "[x: @{[ $_[0]->x ]}, y: @{[ $_[0]->y ]}]"; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub op_eq { | ||
| 227 | $_[0]->x == (Scalar::Util::blessed $_[1] ? $_[1]->x : $_[1]->[0] ) | ||
| 228 | && $_[0]->y == (Scalar::Util::blessed $_[1] ? $_[1]->y : $_[1]->[1] ) | ||
| 229 | } | ||
| 230 | |||
| 231 | sub op_neg { | ||
| 232 | $_[0]->negate; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub to_HashRef { | ||
| 236 | +{ map { $_ => $_[0]->$_ } @FIELDS }; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub to_Vec3 { | ||
| 240 | my ($self) = @_; | ||
| 241 | my $point3d = Intertangle::Yarn::Graphene::Vec3->new( | ||
| 242 | x => $self->x, | ||
| 243 | y => $self->y, | ||
| 244 | z => 0, | ||
| 245 | ); | ||
| 246 | } | ||
| 247 | } | ||
| 248 | $Intertangle::Yarn::Graphene::Vec2::VERSION = '0.002'; | ||
| 249 | |||
| 250 | package Intertangle::Yarn::Graphene::Vec3 { | ||
| 251 | our @FIELDS = qw(x y z); | ||
| 252 | use Scalar::Util; | ||
| 253 | use Role::Tiny::With; | ||
| 254 | with 'Intertangle::Yarn::Graphene::DataPrinterRole'; | ||
| 255 | use overload | ||
| 256 | '""' => \&op_str, | ||
| 257 | 'eq' => \&op_eq, | ||
| 258 | '==' => \&op_eq; | ||
| 259 | |||
| 260 | sub new { | ||
| 261 | my ($class, %args) = @_; | ||
| 262 | |||
| 263 | my $vec3 = Intertangle::Yarn::Graphene::Vec3->alloc(); | ||
| 264 | $vec3->init( $args{x}, $args{y}, $args{z} ); | ||
| 265 | |||
| 266 | $vec3; | ||
| 267 | } | ||
| 268 | |||
| 269 | sub x { | ||
| 270 | $_[0]->dot( Intertangle::Yarn::Graphene::Vec3::x_axis() ); | ||
| 271 | } | ||
| 272 | |||
| 273 | sub y { | ||
| 274 | $_[0]->dot( Intertangle::Yarn::Graphene::Vec3::y_axis() ); | ||
| 275 | } | ||
| 276 | |||
| 277 | sub z { | ||
| 278 | $_[0]->dot( Intertangle::Yarn::Graphene::Vec3::z_axis() ); | ||
| 279 | } | ||
| 280 | |||
| 281 | sub op_str { | ||
| 282 | "[x: @{[ $_[0]->x ]}, y: @{[ $_[0]->y ]}, z: @{[ $_[0]->z ]}]"; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub op_eq { | ||
| 286 | $_[0]->x == (Scalar::Util::blessed $_[1] ? $_[1]->x : $_[1]->[0] ) | ||
| 287 | && $_[0]->y == (Scalar::Util::blessed $_[1] ? $_[1]->y : $_[1]->[1] ) | ||
| 288 | && $_[0]->z == (Scalar::Util::blessed $_[1] ? $_[1]->z : $_[1]->[2] ) | ||
| 289 | } | ||
| 290 | |||
| 291 | sub to_HashRef { | ||
| 292 | +{ map { $_ => $_[0]->$_ } @FIELDS }; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | $Intertangle::Yarn::Graphene::Vec3::VERSION = '0.002'; | ||
| 296 | |||
| 297 | package Intertangle::Yarn::Graphene::Rect { | ||
| 298 | our @FIELDS = qw(origin size); | ||
| 299 | use Role::Tiny::With; | ||
| 300 | with 'Intertangle::Yarn::Graphene::DataPrinterRole'; | ||
| 301 | |||
| 302 | sub new { | ||
| 303 | my ($class, %args) = @_; | ||
| 304 | |||
| 305 | my $rect = Intertangle::Yarn::Graphene::Rect::alloc(); | ||
| 306 | $rect->init( | ||
| 307 | $args{origin}->x, $args{origin}->y, | ||
| 308 | $args{size}->width, $args{size}->height, | ||
| 309 | ); | ||
| 310 | |||
| 311 | $rect; | ||
| 312 | } | ||
| 313 | } | ||
| 314 | $Intertangle::Yarn::Graphene::Rect::VERSION = '0.002'; | ||
| 315 | |||
| 316 | package Intertangle::Yarn::Graphene::Matrix { | ||
| 317 | use Module::Load; | ||
| 318 | use overload | ||
| 319 | '""' => \&op_str, | ||
| 320 | '*' => \&op_transform, | ||
| 321 | 'x' => \&op_matmult; | ||
| 322 | |||
| 323 | sub new_from_arrayref { | ||
| 324 | my ($class, $data) = @_; | ||
| 325 | my $obj = $class->new; | ||
| 326 | $obj->init_from_arrayref( $data ); | ||
| 327 | |||
| 328 | $obj; | ||
| 329 | } | ||
| 330 | |||
| 331 | sub new_from_float { | ||
| 332 | my ($class, $data) = @_; | ||
| 333 | my $obj = $class->new; | ||
| 334 | $obj->init_from_float( $data ); | ||
| 335 | |||
| 336 | $obj; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub init_from_arrayref { | ||
| 340 | my ($class, $data) = @_; | ||
| 341 | |||
| 342 | unless( | ||
| 343 | @$data == 4 | ||
| 344 | && @{ $data->[0] } == 4 | ||
| 345 | && @{ $data->[1] } == 4 | ||
| 346 | && @{ $data->[2] } == 4 | ||
| 347 | && @{ $data->[3] } == 4 | ||
| 348 | ) { | ||
| 349 | die "Matrix data must be a 4x4 ArrayRef"; | ||
| 350 | } | ||
| 351 | |||
| 352 | $class->init_from_float( | ||
| 353 | [ | ||
| 354 | @{ $data->[0] }, | ||
| 355 | @{ $data->[1] }, | ||
| 356 | @{ $data->[2] }, | ||
| 357 | @{ $data->[3] }, | ||
| 358 | ] | ||
| 359 | ); | ||
| 360 | } | ||
| 361 | |||
| 362 | sub to_ArrayRef { | ||
| 363 | my ($self) = @_; | ||
| 364 | my $data = [ | ||
| 365 | map { | ||
| 366 | my $row = $self->get_row($_); | ||
| 367 | [ $row->get_x, $row->get_y, $row->get_z, $row->get_w ] | ||
| 368 | } 0..3 | ||
| 369 | ]; | ||
| 370 | |||
| 371 | $data; | ||
| 372 | } | ||
| 373 | |||
| 374 | sub _data_printer { | ||
| 375 | my ($self, $ddp) = @_; | ||
| 376 | |||
| 377 | BEGIN { | ||
| 378 | eval { | ||
| 379 | autoload Data::Printer::Filter; | ||
| 380 | autoload Term::ANSIColor; | ||
| 381 | }; | ||
| 382 | } | ||
| 383 | |||
| 384 | my $text = ''; | ||
| 385 | |||
| 386 | $text .= "("; | ||
| 387 | $text .= $ddp->maybe_colorize( ref($self), 'class' ); | ||
| 388 | $text .= ") "; | ||
| 389 | my $ml_save = $ddp->multiline(0); | ||
| 390 | $text .= $ddp->parse($self->to_ArrayRef); | ||
| 391 | $ddp->multiline($ml_save); | ||
| 392 | |||
| 393 | $text; | ||
| 394 | } | ||
| 395 | |||
| 396 | sub op_str { | ||
| 397 | my $row_text = sub { "@{[ $_[0]->get_x ]} @{[ $_[0]->get_y ]} @{[ $_[0]->get_z ]} @{[ $_[0]->get_w ]}" }; | ||
| 398 | my $text = ""; | ||
| 399 | $text .= "[\n"; | ||
| 400 | $text .= " "x4 . $row_text->( $_[0]->get_row(0) ) . "\n"; | ||
| 401 | $text .= " "x4 . $row_text->( $_[0]->get_row(1) ) . "\n"; | ||
| 402 | $text .= " "x4 . $row_text->( $_[0]->get_row(2) ) . "\n"; | ||
| 403 | $text .= " "x4 . $row_text->( $_[0]->get_row(3) ) . "\n"; | ||
| 404 | $text .= "]\n"; | ||
| 405 | } | ||
| 406 | |||
| 407 | sub transform_point { | ||
| 408 | my ($self, $point ) = @_; | ||
| 409 | |||
| 410 | my $point3d = $point->to_Point3D; | ||
| 411 | my $t_point3d = $self->transform_point3d( $point3d ); | ||
| 412 | |||
| 413 | return Intertangle::Yarn::Graphene::Point->new( | ||
| 414 | x => $t_point3d->x, | ||
| 415 | y => $t_point3d->y, | ||
| 416 | ); | ||
| 417 | }; | ||
| 418 | |||
| 419 | sub transform { | ||
| 420 | my ($matrix, $other) = @_; | ||
| 421 | |||
| 422 | my $result; | ||
| 423 | |||
| 424 | if( $other->isa('Intertangle::Yarn::Graphene::Vec4') ) { $result = $matrix->transform_vec4( $other ) | ||
| 425 | } elsif( $other->isa('Intertangle::Yarn::Graphene::Vec3') ) { $result = $matrix->transform_vec3( $other ) | ||
| 426 | } elsif( $other->isa('Intertangle::Yarn::Graphene::Point') ) { $result = $matrix->transform_point( $other ) | ||
| 427 | } elsif( $other->isa('Intertangle::Yarn::Graphene::Point3D') ) { $result = $matrix->transform_point3d( $other ) | ||
| 428 | } elsif( $other->isa('Intertangle::Yarn::Graphene::Rect') ) { $result = $matrix->transform_rect( $other ) | ||
| 429 | } elsif( $other->isa('Intertangle::Yarn::Graphene::Bounds') ) { $result = $matrix->transform_bounds( $other ) | ||
| 430 | } elsif( $other->isa('Intertangle::Yarn::Graphene::Box') ) { $result = $matrix->transform_box( $other ) | ||
| 431 | } elsif( $other->isa('Intertangle::Yarn::Graphene::Sphere') ) { $result = $matrix->transform_sphere( $other ) | ||
| 432 | } else { | ||
| 433 | die "Unknown type for transformation: @{[ ref $other ]}"; | ||
| 434 | } | ||
| 435 | |||
| 436 | |||
| 437 | $result; | ||
| 438 | } | ||
| 439 | |||
| 440 | sub op_transform { | ||
| 441 | $_[0]->transform( $_[1] ); | ||
| 442 | } | ||
| 443 | |||
| 444 | sub op_matmult { | ||
| 445 | $_[0]->multiply( $_[1] ); | ||
| 446 | } | ||
| 447 | } | ||
| 448 | $Intertangle::Yarn::Graphene::Matrix::VERSION = '0.002'; | ||
| 449 | |||
| 450 | 1; | ||
| 451 | |||
| 452 | __END__ | ||