Skip to content

Commit

Permalink
ADDED: ZBuffer module
Browse files Browse the repository at this point in the history
The module lets use create a Z Buffer object.

The object stores
	- a 2D map rendering in a hash
	- a list of intersecting element in a hash

The object provides
	- a constructor, new, to which all Asciio elements are passed
	- a stub, cross_overlay, for the computation of crossing overlays
	- a sub, get_neighbors, that returns a hash of neighboring characters

For testing purpose a binding, Dz, was added
	- create a ZBuffer object, initializing it with asciio's elements
	- call the crossing overlay stub
  • Loading branch information
nkh committed Nov 5, 2023
1 parent 5f9a8cc commit b79aa09
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ lib/App/Asciio/Setup.pm
lib/App/Asciio/Stencil.pm
lib/App/Asciio/String.pm
lib/App/Asciio/Undo.pm
lib/App/Asciio/ZBuffer.pm
lib/App/Asciio/Actions/Align.pm
lib/App/Asciio/Actions/Asciio.pm
Expand All @@ -46,6 +47,7 @@ lib/App/Asciio/Actions/Presentation.pm
lib/App/Asciio/Actions/Ruler.pm
lib/App/Asciio/Actions/Shapes.pm
lib/App/Asciio/Actions/Unsorted.pm
lib/App/Asciio/Actions/ZBuffer.pm
lib/App/Asciio/stripes/angled_arrow.pm
lib/App/Asciio/stripes/center_connect_box.pm
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ lib/App/Asciio/Setup.pm
lib/App/Asciio/Stencil.pm
lib/App/Asciio/String.pm
lib/App/Asciio/Undo.pm
lib/App/Asciio/ZBuffer.pm

lib/App/Asciio/Actions/Align.pm
lib/App/Asciio/Actions/Arrow.pm
Expand All @@ -65,6 +66,7 @@ lib/App/Asciio/Actions/Presentation.pm
lib/App/Asciio/Actions/Ruler.pm
lib/App/Asciio/Actions/Shapes.pm
lib/App/Asciio/Actions/Unsorted.pm
lib/App/Asciio/Actions/ZBuffer.pm

lib/App/Asciio/stripes/angled_arrow.pm
lib/App/Asciio/stripes/center_connect_box.pm
Expand Down
2 changes: 1 addition & 1 deletion Todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ direction arrow
next direction, everything except favorite direction opposite

documentation format and structure
makdown
markdown
directory structure reflecting contents

output
Expand Down
2 changes: 1 addition & 1 deletion lib/App/Asciio.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use App::Asciio::Undo ;

#-----------------------------------------------------------------------------

our $VERSION = '1.9.02' ;
our $VERSION = '1.9.50' ;

#-----------------------------------------------------------------------------

Expand Down
27 changes: 27 additions & 0 deletions lib/App/Asciio/Actions/ZBuffer.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

package App::Asciio::Actions::ZBuffer ;

use strict ; use warnings ;

use App::Asciio::ZBuffer ;


#-----------------------------------------------------------------------------

sub dump_crossings
{
my ($asciio) = @_ ;

use Term::Size::Any qw(chars) ;

my $zbuffer = App::Asciio::ZBuffer->new(@{$asciio->{ELEMENTS}}) ;
$zbuffer->cross_overlay ;

$zbuffer->render_text ;
}


#-----------------------------------------------------------------------------

1 ;

199 changes: 199 additions & 0 deletions lib/App/Asciio/ZBuffer.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package App::Asciio::ZBuffer ;

use strict; use warnings;
use utf8;

# ------------------------------------------------------------------------------

sub new
{
my ($invocant, @elements) = @_;

my $class = ref($invocant) || $invocant;

my $self = bless { }, $class ;

$self->add_elements(@elements) ;

return $self;
}

# ------------------------------------------------------------------------------

sub add_elements
{
my ($self, @elements) = @_ ;

my $t0 = Time::HiRes::gettimeofday();

for my $element (@elements)
{
my $coordinates = $self->get_coordinates($element) ;

while ( my ($coordinate, $char) = each $coordinates->%* )
{
if($char eq ' ')
{
delete $self->{intersecting_elements}{$coordinate}
}
elsif( ($self->{coordinates}{$coordinate} // ' ') ne ' ')
{
$self->{intersecting_elements}{$coordinate} = [ $char, $self->{coordinates}{$coordinate} ] ;
}

$self->{coordinates}{$coordinate} = $char ;
}
}

my $t1 = Time::HiRes::gettimeofday();
printf "add time: %0.4f sec.\n", $t1 - $t0 ;
}

# ------------------------------------------------------------------------------

sub get_coordinates
{
my ($self, $element) = @_ ;
my %coordinates ;

for my $strip (@{$element->get_stripes})
{
my $line_index = 0 ;

for my $line (split /\n/, $strip->{TEXT})
{
my $character_index = 0 ;

for my $char ( split '', $line)
{
my $Y = $element->{Y} + $strip->{Y_OFFSET} + $line_index ;
my $X = $element->{X} + $strip->{X_OFFSET} + $character_index ;
$coordinates{"$Y;$X"} = $char ;

$character_index++ ;
}

$line_index++ ;
}
}

return \%coordinates ;
}

# ------------------------------------------------------------------------------

sub render_text
{
my ($self, $COLS, $ROWS) = @_ ;
($COLS, $ROWS) = (15, 10) ;

my $t0 = Time::HiRes::gettimeofday();
my $rendering = '' ;
my ($text, $previous_color , $color) = ('', '', '') ;

while ( my ($coordinate, $char) = each $self->{coordinates}->%*)
{
$rendering .= "\e[${coordinate}H$char" ;
# $rendering .= "${coordinate}->$char\n" ;
}

print "$rendering\e[m" ;

my $t1 = Time::HiRes::gettimeofday();
printf "render time: %0.4f sec.\n", $t1 - $t0 ;
}

# ------------------------------------------------------------------------------

sub remove_elements
{
# my ($self, @elements) = @_ ;
# my @new_elements ;

# for my $element (@elements)
# {
# push @new_elements, $element ;
# }
}

# ------------------------------------------------------------------------------

sub update_elements_list
{
# my ($self, @elements) = @_ ;
# my %new_list = map { $_ => $_ } @elements ;

# $self->remove_elements( grep { ! exists $new_list{$_} } keys %{$self->{elements}} ) ;
# $self->add_elements(@elements) ;
}

# ------------------------------------------------------------------------------

sub get_neighbors
{
my ($self, $coordinate) = @_ ;
my ($x, $y) = split ';', $coordinate ;

return
{
map
{
exists $self->{coordinates}{$_}
? (
$self->{coordinates}{$_} ne ' '
? ($_ => $self->{coordinates}{$_})
: ()
)
: () }
($x-1) .';'. ($y-1), $x .';'. ($y-1), ($x+1) .';'. ($y-1),
($x-1) .';'. $y, ($x+1) .';'. $y,
($x-1) .';'. ($y+1), $x .';'. ($y+1), ($x+1) .';'. ($y+1)
}
}

# ------------------------------------------------------------------------------

sub cross_overlay
{
my ($zbuffer) = @_ ;

use Data::TreeDumper ;

while( my($coordinate, $elements) = each $zbuffer->{intersecting_elements}->%*)
{
my $neighbors = $zbuffer->get_neighbors($coordinate) ;

print DumpTree { stack => $elements, neighbors => $neighbors }, $coordinate ;
# compute overlay
}
}

# ------------------------------------------------------------------------------

1 ;

__DATA__
element_uniq_id
do we clone elements?
element has its own 2d_hash
or a list of characters and then get the character from strips
updating z buffer
if element was removed, use element "shadow" to decrease position count
delete element which deletes the element's 2d map
if element is added, and z buffer already created
update z buffer with just that element
if elemnt modified
delete element and add it
finding out if element is modified is by asking the element itself
problem is the element in asciio is the dsame as the shadow element in z buffer
if element return a hash for it's state we can use the hash to check if it has changed
if element's depth hs changed
2 changes: 2 additions & 0 deletions setup/actions/default_bindings.pl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App::Asciio::Actions::Ruler ;
use App::Asciio::Actions::Shapes ;
use App::Asciio::Actions::Unsorted ;
use App::Asciio::Actions::ZBuffer ;


#----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -205,6 +206,7 @@
'Dump selected elements' => ['000-E', \&App::Asciio::Actions::Debug::dump_selected_elements ],
'Display numbered objects' => ['000-t', sub { $_[0]->{NUMBERED_OBJECTS} ^= 1 ; $_[0]->update_display() }],
'Test' => ['000-o', \&App::Asciio::Actions::Debug::test ],
'ZBuffer Test' => ['000-z', \&App::Asciio::Actions::ZBuffer::dump_crossings ],
},

'commands leader'=>
Expand Down

0 comments on commit b79aa09

Please sign in to comment.