Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
How does the set of "unintersected edges" computed by your algorithm differ from a Delaunay triangulation? If the difference is insignificant for your application (or can be quickly computed), you might be better off starting with a fast Delaunay triangulation (e.g., using Qhull) and adapting its output to your needs.

Update: Take a look at Aurenhammer and Xu's "Optimal Triangulations". It points out that the greedy triangulation – what you are computing – is not necessarily the minimum-weight triangulation (MWT). The paper also notes, however, that for uniformly distributed point sets both the greedy and Delaunay triangulations yield satisfactory approximations to the MWT.

Second update: The following code uses Qhull and computes an (approximate) DT-derived solution almost instantly:

#!/usr/bin/perl # solve.pl use warnings; use strict; use YAML; use File::Temp 'tempfile'; use List::Util 'sum'; my $points = YAML::Load( do { local $/; <> } ); my $triangles = compute_delaunay_triangulation( $points ); my $segs = unique_segments( $triangles ); my @seg_endpoints = map [ @{$points}[@$_] ], @$segs; my @seg_lengths = map seg_length($_), @seg_endpoints; my $count = @$segs; my $mean = sum(@seg_lengths) / $count; print "edge count = $count\n"; print "average edge length = $mean\n\n"; print "edge endpoints follow in YAML format\n\n"; print YAML::Dump( @seg_endpoints ); sub compute_delaunay_triangulation { # use Qhull's qdelaunay for this my ($points) = @_; my $fh = tempfile(); die "cannot open tempfile: $!" unless $fh; my $pid = open my $write_to_child, "|-"; die "cannot fork: $!" unless defined $pid; if ($pid) { # parent print $write_to_child "2\n", scalar @$points, "\n", map "@$_\n", @$points; close $write_to_child || warn "kid exited $?"; } else { # child open STDOUT, '>&', $fh or die "could not redirect stdout: $!" +; close $fh or die "error on close of tempfile: $! +"; exec qw(qdelaunay Qt i) or die "could not exec qdelaunay: $!"; } wait; seek $fh, 0, 0 or die "could not seek to start of tempfile: $!"; my $results = do { local $/; <$fh> }; close $fh; (undef, my @triangles) = split /\n/, $results; return [ map [split], @triangles ]; } sub unique_segments { my ($triangles) = @_; my %segs; for my $tri (@$triangles) { for ([0,1],[1,2],[2,0]) { my @points = sort @{$tri}[@$_]; $segs{"@points"} = \@points; } } [ values %segs ]; } sub seg_length { my ($seg) = @_; my ($x,$y,$x1,$y1) = ( $seg->[0][0], $seg->[0][1] , $seg->[1][0], $seg->[1][1] ); sqrt( ($x - $x1) ** 2 + ($y - $y1) ** 2); }

Example: solve dump_301.yml. Output:

edge count = 889 average edge length = 123.835184709118 edge endpoints follow in YAML format --- - - 2590 - 3183 - - 2707 - 3257 --- - - 2560 - 2850 - - 2586 - 2869 ... other edges omitted ...

Cheers,
Tom

Tom Moertel : Blog / Talks / CPAN / LectroTest / PXSL / Coffee / Movie Rating Decoder


In reply to Delaunay triangulation by tmoertel
in thread Millions of line segment intersection calcs: Looking for speed tips by drewhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-19 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found