Skip to content

Commit

Permalink
move Graphics::TriD::Rout::{combcoords,attract,repulse} -> ImageND, test
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Jan 3, 2025
1 parent 436ae7c commit ab68698
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 192 deletions.
176 changes: 1 addition & 175 deletions Rout/rout.pd
Original file line number Diff line number Diff line change
Expand Up @@ -12,184 +12,10 @@ PDL::Graphics::TriD::Rout - Helper routines for Three-dimensional graphics
=head1 DESCRIPTION

This module is for miscellaneous PP-defined utility routines for
the PDL::Graphics::TriD module. Currently, there are
the PDL::Graphics::TriD module.

EOD

pp_def(
'combcoords',
GenericTypes => ['F','D'],
DefaultFlow => 1,
Pars => 'x(); y(); z();
float [o]coords(tri=3);',
Code => '
$coords(tri => 0) = $x();
$coords(tri => 1) = $y();
$coords(tri => 2) = $z();
',
Doc => <<EOT
=for ref

Combine three coordinates into a single ndarray.

Combine x, y and z to a single ndarray the first dimension
of which is 3. This routine does dataflow automatically.
EOT

);

# checks all neighbouring boxes.
# Returns (r = |dist|+d) a*r^-2 + b*r^-1 + c*r^-0.5
pp_def(
'repulse',
GenericTypes => ['F','D'],
Pars => 'coords(nc,np);
[o]vecs(nc,np);
int [t]links(np);',
OtherPars => '
double boxsize;
int dmult;
double a;
double b;
double c;
double d;
',
Code => '
double a = $COMP(a);
double b = $COMP(b);
double c = $COMP(c);
double d = $COMP(d);
int ind; int x,y,z;
HV *hv = newHV();
double boxsize = $COMP(boxsize);
int dmult = $COMP(dmult);
loop(np) %{
int index = 0;
$links() = -1;
loop(nc) %{
$vecs() = 0;
index *= dmult;
index += (int)($coords()/boxsize);
%}
/* Repulse old (shame to use x,y,z...) */
for(x=-1; x<=1; x++) {
for(y=-1; y<=1; y++) {
for(z=-1; z<=1; z++) {
int ni = index + x + dmult * y +
dmult * dmult * z;
SV **svp = hv_fetch(hv, (char *)&ni, sizeof(int),
0);
if(svp && *svp) {
ind = SvIV(*svp) - 1;
while(ind>=0) {
double dist = 0;
double dist2;
double tmp;
double func;
loop(nc) %{
tmp =
($coords() -
$coords(np => ind));
dist += tmp * tmp;
%}
dist = sqrt(1/(sqrt(dist)+d));
func = c * dist;
dist2 = dist * dist;
func += b * dist2;
dist2 *= dist2;
func += a * dist2;
loop(nc) %{
tmp =
($coords() -
$coords(np => ind));
$vecs() -=
func * tmp;
$vecs(np => ind) +=
func * tmp;
%}
ind = $links(np => ind);
}
}
}
}
}
/* Store new */
SV **svp = hv_fetch(hv, (char *)&index, sizeof(index), 1);
if(!svp || !*svp)
$CROAK("Invalid sv from hvfetch");
SV *sv = *svp;
int npv;
if(SvOK(sv) && (npv = SvIV(sv))) {
npv --;
$links() = $links(np => npv);
$links(np => npv) = np;
} else {
sv_setiv(sv,np+1);
$links() = -1;
}
%}
hv_undef(hv);
', Doc => '
=for ref

Repulsive potential for molecule-like constructs.

C<repulse> uses a hash table of cubes to quickly calculate
a repulsive force that vanishes at infinity for many
objects. For use by the module L<PDL::Graphics::TriD::MathGraph>.
For definition of the potential, see the actual function.
'
);

pp_def(
'attract',
GenericTypes => ['F','D'],
Pars => 'coords(nc,np);
int from(nl);
int to(nl);
strength(nl);
[o]vecs(nc,np);',
OtherPars => '
double m;
double ms;
',
Code => '
double m = $COMP(m);
double ms = $COMP(ms);
loop(nc,np) %{ $vecs() = 0; %}
loop(nl) %{
int f = $from();
int t = $to();
double s = $strength();
double dist = 0;
double tmp;
loop(nc) %{
tmp = $coords(np => f) -
$coords(np => t);
dist += tmp * tmp;
%}
s *= ms * dist + m * sqrt(dist);
loop(nc) %{
tmp = $coords(np => f) -
$coords(np => t);
$vecs(np => f) -= tmp * s;
$vecs(np => t) += tmp * s;
%}
%}
', Doc => '
=for ref

Attractive potential for molecule-like constructs.

C<attract> is used to calculate
an attractive force for many
objects, of which some attract each other (in a way
like molecular bonds).
For use by the module L<PDL::Graphics::TriD::MathGraph>.
For definition of the potential, see the actual function.
'
);

sub trid {
my ($par,$ind) = @_;
join ',', map {"\$$par($ind => $_)"} (0..2);
Expand Down
4 changes: 2 additions & 2 deletions TriD.pm
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ use PDL::Graphics::TriD::ViewPort;
use PDL::Graphics::TriD::Graph;
use PDL::Graphics::TriD::Quaternion;
use PDL::Graphics::TriD::Objects;
use PDL::Graphics::TriD::Rout;
use PDL::ImageND;

# Then, see which display method are we using:

Expand Down Expand Up @@ -805,7 +805,7 @@ sub realcoords {
$c[$_] = $c[$_]*(PDL->ones($c[($_+1)%3]->dims));
}
}
my $g = PDL::Graphics::TriD::Rout::combcoords(@c);
my $g = PDL::ImageND::combcoords(@c);
$g->dump if $PDL::Graphics::TriD::verbose;
return $g;
}
Expand Down
21 changes: 6 additions & 15 deletions TriD/MathGraph.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sub get_valid_options {

package PDL::GraphEvolver;
use PDL::Lite;
use PDL::Graphics::TriD::Rout ":Func";
use PDL::ImageND ":Func";

sub new {
my($type,$coords) = @_;
Expand All @@ -62,17 +62,9 @@ sub new {
},$type;
}

sub set_links {
my($this,$from,$to,$strength) = @_;
$this->{From} = $from;
$this->{To} = $to;
$this->{Strength} = $strength;
}
sub set_links { my $this = shift; @$this{qw(From To Strength)} = @_; }

sub set_fixed {
my($this,$ind,$coord) = @_;
$this->{FInd} = $ind; $this->{FCoord} = $coord;
}
sub set_fixed { my $this = shift; @$this{qw(FInd FCoord)} = @_; }

sub step {
my($this) = @_;
Expand All @@ -84,12 +76,11 @@ sub step {
$this->{Velo} *=
(0.92*50/(50+$this->{Velo}->magnover->dummy(0)))**$tst;
$c += $tst * 0.05 * $this->{Velo};
(my $tmp = $c->transpose->index($this->{FInd}->dummy(0)))
.= $this->{FCoord}
if (defined $this->{FInd});
$c->transpose->index($this->{FInd}->dummy(0)) .= $this->{FCoord}
if (defined $this->{FInd});
print "C: $c\n" if $PDL::Graphics::TriD::verbose;
}

sub getcoords {return $_[0]{Coords}}
sub getcoords {$_[0]{Coords}}

1;

0 comments on commit ab68698

Please sign in to comment.