chiark / gitweb /
compute initial layout - not yet checked
authorIan Jackson <ian@davenant.relativity.greenend.org.uk>
Sun, 30 Dec 2007 17:18:58 +0000 (17:18 +0000)
committerIan Jackson <ian@davenant.relativity.greenend.org.uk>
Sun, 30 Dec 2007 17:18:58 +0000 (17:18 +0000)
.bzrignore
Makefile
common.c
common.h
energy.c
generator [new file with mode: 0755]
mgraph.h
primer.c [new file with mode: 0644]

index 22710ca154c9c8f423dca680ab537dcb27c04c4b..51d35b478a2a7dbf8e31652e9a2abe3f8ba585be 100644 (file)
@@ -1 +1,3 @@
 minimise
+primer
+initial
index d0e7f2e70cf70e28642d7f640cc7a1d1cc591da1..fc74681e0e4b665f103753745e045dc5df7cd339 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 
-TARGETS=minimise
+TARGETS= minimise primer initial
 
 CWARNS=        -Wall -Wwrite-strings -Wpointer-arith -Werror
 
@@ -7,11 +7,21 @@ OPTIMISE=     -O2
 CFLAGS=                $(CWARNS) $(OPTIMISE) -g
 CXXFLAGS=      $(CWARNS) $(OPTIMISE) -g
 
+LIBGSL= -lgsl -lgslcblas
+
 all:           $(TARGETS)
 
 minimise:      energy.o bgl.o common.o mgraph.o
-               $(CXX) $(CXXFLAGS) -o $@ $^ -lgsl -lgslcblas
+               $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBGSL)
+
+primer:                primer.o common.o
+               $(CC) $(CFLAGS) -o $@ $^ $(LIBGSL)
+
+initial:       generator primer sgtatham/z.typescript
+               ./$^ -o$@
 
 clean:
-               rm -f *.o $(TARGETS) *.new
+               rm -f *.o $(TARGETS) *.new *.tmp
                rm -f best initial
+
+%.o::          common.h mgraph.h bgl.h
index 900102513cf2c7305b31b85deb223a4c9b36f872..b87dedb3110950267c54157283a03d9592f5c1b0 100644 (file)
--- a/common.c
+++ b/common.c
@@ -45,3 +45,6 @@ void xprod(double r[D3], const double a[D3], const double b[D3]) {
   r[1]= a[2]*b[0] - a[0]*b[2];
   r[2]= a[0]*b[1] - a[1]*b[0];
 }
+
+void diee(const char *what) { perror(what); exit(16); }
+void flushoutput(void) { if (fflush(stdout)||ferror(stdout)) diee("stdout"); }
index f890f840420fac32d9dacb4e4d437429d1ecd3f1..a64221318b1112a9f951e271f64bab2e179790de 100644 (file)
--- a/common.h
+++ b/common.h
@@ -25,6 +25,9 @@ double hypotD2plus(const double p[D3], const double q[D3], double add);
 double magnD(const double pq[D3]);
 void xprod(double r[D3], const double a[D3], const double b[D3]);
 
+void flushoutput(void);
+void diee(const char *what);
+
 #define FOR_COORD(k) \
   for ((k)=0; (k)<D3; (k)++)
 
index e0ebbe24a90beb61890b1c6767f54992938d5810..aa1c7f4d29ee7820c7abe784e414e8cac99ebd20 100644 (file)
--- a/energy.c
+++ b/energy.c
@@ -17,8 +17,6 @@ static double noncircular_rim_cost(const Vertices vertices);
 
 static void compute_vertex_areas(const Vertices vertices, double areas[N]);
 static double best_energy= DBL_MAX;
-static void flushoutput(void);
-static void diee(const char *what) { perror(what); exit(16); }
 
 static void cost(double *energy, double tweight, double tcost);
 #define COST(weight, compute) cost(&energy, (weight), (compute))
@@ -60,10 +58,6 @@ static void cost(double *energy, double tweight, double tcost) {
   *energy += tenergy;
 }
 
-static void flushoutput(void) {
-  if (fflush(stdout) || ferror(stdout)) diee("stdout");
-}
-
 static void compute_vertex_areas(const Vertices vertices, double areas[N]) {
   int v0,v1,v2, e1,e2, k;
   
@@ -124,8 +118,6 @@ static gsl_multimin_fminimizer *minimiser;
 
 static const double stop_epsilon= 1e-4;
 
-#define DIM (N*D3)
-
 static double minfunc_f(const gsl_vector *x, void *params) {
   assert(x->size == DIM);
   assert(x->stride == 1);
diff --git a/generator b/generator
new file mode 100755 (executable)
index 0000000..1933c55
--- /dev/null
+++ b/generator
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+die unless @ARGV==3;
+($primer,$ztype,$output) = @ARGV;
+$output =~ s/^\-o// or die "$output ?";
+
+$gnuplot= "$output.gnuplot.tmp";
+$initdata= "$output.initdata.tmp";
+
+open Z, "$ztype" or die "$ztype $!";
+open G, ">$gnuplot" or die $!;
+
+sub pg ($$) { printf G "%s(u,v) = %s\n", @_ or die $!; }
+
+for (;;) {
+    $!=0; defined($_= <Z>) or die "$ztype $!";
+    s/\r$//;
+    next unless m/^splot\s+([^,]+),([^,]+),([^,]+)\s*$/;
+    pg('x',$1); pg('y',$2); pg('z',$3);
+    last;
+}
+
+printf G "set print \"-\"\n" or die $!;
+close G or die $!;
+
+sub run ($) {
+    print "    $_[0]\n";
+    $!=0; system($_[0]); die "$! $?" if $! or $?;
+}
+
+run("./$primer >>$gnuplot");
+run("gnuplot $gnuplot >$initdata");
+
+open I, "$initdata" or die "$initdata $!";
+open B, ">$output.new" or die "$output.new $!";
+
+$_= <I>;
+m/^(\d+) .*/ or die "$_ ?";
+
+$dim= $1;
+for ($i=0; $i<$dim; $i++) {
+    $!=0; defined($_= <I>) or die "$initdata $!";
+    print B pack "d", $_ or die $!;
+}
+
+close B or die $!;
+rename "$output.new",$output or die $!;
+
+print "    wrote $output\n";
index 85577e92c3e325b79ba5a2a3a2762b3f2b4bc740..a33014b3dfe345f27c91a76758ab1b708cfaa8ae 100644 (file)
--- a/mgraph.h
+++ b/mgraph.h
@@ -69,6 +69,8 @@
 #define Y1 (1 << YSHIFT)
 #define YMASK ((Y-1) << YSHIFT)
 
+#define DIM (N*D3)
+
 #define V6 6
 
 #define FOR_VERTEX(v) \
diff --git a/primer.c b/primer.c
new file mode 100644 (file)
index 0000000..eaa3459
--- /dev/null
+++ b/primer.c
@@ -0,0 +1,28 @@
+/*
+ * Generates the repetitive part of the gnuplot input
+ * for generating the initial triangle vertices from SGT's
+ * model.
+ */
+
+#include "common.h"
+#include "mgraph.h"
+
+int main(int argc, const char **argv) {
+  int v, k;
+  
+  if (argc>1) { fputs("no args please\n",stderr); exit(8); }
+
+  printf("print %d, %d, %d, %d, %d\n", DIM, N, X, Y, D3);
+  
+  FOR_VERTEX(v) {
+    int x= v & XMASK; /* distance along strip */
+    int y= y >> YSHIFT; /* distance across strip */
+    double u= y * 1.0 / (Y-1);
+    double v= x * M_PI / (X-1); /* SGT's u runs 0..pi along the strip */
+    K printf("print %c(%.*g,%.*g)\n",
+            "xyz"[k],
+            DBL_DIG+2,u, DBL_DIG+2,v);
+  }
+  flushoutput();
+  return 0;
+}