LAL  7.5.0.1-b72065a
LALMath3DPlot.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007 Chad Hanna, Benjamin Owen
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 
20 #include <lal/LALConfig.h>
21 #include <lal/LALMalloc.h>
22 #include <lal/LALMathematica.h>
23 #include <lal/LALStatusMacros.h>
24 #include <lal/LALStdlib.h>
25 #include <lal/LALStdio.h>
26 
27 #define INSTRUCTIONS fprintf(nb, "This notebook will produce an animated 3D plot of your template bank. See the next section to change any user variables before evaluating. The cells of this notebook must be evaluated sequentially. If you wish to evaluate the entire notebook at once press Ctrl+A then press Shift+Enter in most operating systems.")
28 
29 /**
30  * \brief This function is for plotting 3D template banks by creating a MATHEMATICA notebook.
31  * \author Hanna, C. R.
32  *
33  * The notebook renders the templates as
34  * points in a three dimensional lattice. The plot is animated so the user
35  * can see the template bank from different perspectives. See \ref LALMathematicaHplot1 "this figure".
36  *
37  * \anchor LALMathematicaHplot1
38  * \image html LALMathematicaHplot1.png "An example template bank produced by running InspiralSpinBankTest.c to generate roughly 5000 templates"
39  *
40  * Currently the plot doesn't show the contour of the templates; it renders them as
41  * spheres. In the case of metrics with disimilar scales along the
42  * principle directions you will notice considerable space between points
43  * accordingly.
44  *
45  * ### Notes ###
46  *
47  * <ul>
48  * <li> The output of this function is &quot;Math3DNotebook.nb&quot; and will appear
49  * in the directory of the program that called this function.</li>
50  * <li> Exported MATHEMATICA graphics will appear in your home directory
51  * for unix users and in the Mathematica directory for Windows
52  * users unless you have another path configured in your MATHEMATICA
53  * installation. It is necessary to change the file name within the notebook
54  * to avoid overwriting previous files.
55  * </li>
56  * </ul>
57  */
58 void
59 LALMath3DPlot ( LALStatus *stat, /**< LALStatus structure pointer */
60  Math3DPointList *first, /**< Math3DPointList stucture pointer */
61  INT4 *ntiles, /**< pointer to the number of templates you \e plan to plot.
62  * This may be called as NULL. If it is called with a
63  * value this function will check to see if the Math3DPointList has the
64  * correct number of templates. If it does not a warning will be printed. */
65  REAL4 *pointSize /**< \f$\epsilon[0,1]\f$ which specifies the relative
66  * size of each point to the final display area. (e.g. 1 would fill the
67  * entire plot.) This may be called as NULL and a calculated value will be
68  * assigned. (It's only a rough guess)
69  */
70  )
71 
72 {
73  FILE *nb; /* pointer to the notebook file */
74  INT4 jflag = 0; /* flag to justify the output data */
75  Math3DPointList *list; /* loop counter */
76  REAL4 PtSize = 0.02;
77  INT4 counter = 0;
78  REAL4 xmax, ymax, zmax; /* maximum values plotted */
79  INT2 xlog, ylog, zlog; /* log10 of axis scaling factors */
80 
81  INITSTATUS(stat);
82 
83  if (!first) {
84  ABORT(stat, LALMATHEMATICAH_ENULL, LALMATHEMATICAH_MSGENULL);
85  }
86 
87  if ((nb = LALFopen("Math3DNotebook.nb", "w")) == NULL) {
88  ABORT(stat, LALMATHEMATICAH_EFILE, LALMATHEMATICAH_MSGEFILE);
89  }
90 
91  if (!pointSize){
92  if (!ntiles){
93  list=first;
94  while(list->next){
95  counter++;
96  list=list->next;
97  }
98  ntiles = &counter;
99  }
100  else{
101  list=first;
102  while(list->next){
103  counter++;
104  list=list->next;
105  }
106  if (*ntiles != counter)
107  printf("\nWARNING!!! The value of argument ntiles (%i) != the Math3DPointList length (%i)\n",
108  *ntiles, counter);
109  }
110  if (*ntiles <=0) {
111  ABORT(stat, LALMATHEMATICAH_EVAL, LALMATHEMATICAH_MSGEVAL);
112  }
113  PtSize = 0.50*(1.0/(pow((*ntiles),0.333333)));
114  if (*ntiles > 10000)
115  printf("\nWARNING!!! More than 10,000 tiles may crash Mathematica:)\n");
116  }
117 
118  else{
119  if ((*pointSize <= 0.0) || (*pointSize >= 1.0)) {
120  printf("\nIllegal value of pointSize; it must be between 0 and 1.\n");
121  printf("The default value of 0.02 will be used");
122  PtSize = 0.02;
123  }
124  }
125 
126  /* Find scaling factors for axes from maximum absolute values. */
127  xmax = ymax = zmax = 0;
128  for( list = first; list != NULL; list = list->next )
129  {
130  if( fabs( list->x ) > xmax )
131  xmax = fabs( list->x );
132  if( fabs( list->y ) > ymax )
133  ymax = fabs( list->y );
134  if( fabs( list->z ) > zmax )
135  zmax = fabs( list->z );
136  }
137  xlog = (INT2)(log(xmax)/log(10));
138  ylog = (INT2)(log(ymax)/log(10));
139  zlog = (INT2)(log(zmax)/log(10));
140 
141  /* The code that generates the notebook */
142  BEG_NOTEBOOK;
144  fprintf(nb, "LALMath3D Output");
147  fprintf(nb, "Instructions");
149  BEG_TEXTCELL;
150  INSTRUCTIONS;
151  END_TEXTCELL;
154  fprintf(nb, "Initialization");
157  fprintf(nb, "Off[General::spell];");
160  fprintf(nb, "Off[General::spell1];");
164  fprintf(nb, "User Variables");
168  fprintf(nb, "AnimationPlot\t:=True");
171  fprintf(nb, "AnimationSize\t= {400,400};");
174  fprintf(nb, "StillSize\t= {600,600};");
177  fprintf(nb, "PtSize\t= %f;", PtSize);
180  fprintf(nb, "AnimationName\t:= \"AnimationTilePlot.gif\"");
183  fprintf(nb, "StillName\t:= \"StillTilePlot\"");
186  fprintf(nb, "StillType\t:=\"EPS\"");
189  fprintf(nb, "frames\t= 30;");
192  fprintf(nb, "FrameTime\t= 0.2;");
195  fprintf(nb, "XAxisLabel = \"Psi0 / 1e%d\"", xlog );
198  fprintf(nb, "YAxisLabel = \"Psi3 / 1e%d\"", ylog );
201  fprintf(nb, "ZAxisLabel = \"Beta / 1e%d\"", zlog );
203  BEG_TEXTCELL;
204  fprintf(nb, "AnimationPlot:\tFlag that determines whether to generate animations");
205  fprintf(nb, "AnimationSize:\tThe size of the final animation in PIXELS x PIXELS\n");
206  fprintf(nb, "StillSize:\t\tThe size of the final still image in PIXELS x PIXELS\n");
207  fprintf(nb, "PtSize:\t\tThe relative size of the template points to the final display width.\n");
208  fprintf(nb, "\t\t\tIt is given as a decimal part of one. (e.g. PtSize=0.02 is 1/20 of the display width)\n");
209  fprintf(nb, "AnimationName:\tWhat to name the final animation - must use .gif extension\n");
210  fprintf(nb, "StillName:\t\tWhat to name the final still image - extension determined by StillType\n");
211  fprintf(nb, "StillType:\t\tThe file type and extension for the still image\n");
212  fprintf(nb, "\t\t\tChoose any standard format (e.g. JPG, GIF, PDF, EPS, etc.)\n");
213  fprintf(nb, "frames:\t\tThe number of frames for each rotation of the image.\n");
214  fprintf(nb, "\t\t\tThe final image will have 2 times the number frames\n");
215  fprintf(nb, "FrameTime:\t\tSets the delay time in seconds between each frame in the animated gif\n");
216  fprintf(nb, "\t\t\tApplications seem to interpret this differently. You may have to adjust this setting\n");
217  fprintf(nb, "\t\t\tbased on the intended application of the animations.\n");
218  fprintf(nb, "XAxisLabel:\t\tSets the X-axis label\n");
219  fprintf(nb, "XAxisLabel:\t\tSets the Y-axis label\n");
220  fprintf(nb, "XAxisLabel:\t\tSets the Z-axis label");
226  fprintf(nb, "Point List");
229  fprintf(nb, "TILES = \n");
230  fprintf(nb, "Graphics3D[{PointSize[PtSize]");
231  list = first;
232  while(list->next)
233  {
234  fprintf( nb, ",{GrayLevel[%f], Point[{%f,%f,%f}]}",
235  list->grayLevel, list->x/pow(10,xlog),
236  list->y/pow(10,ylog), list->z/pow(10,zlog) );
237  if (jflag%2) fprintf(nb,"\n");
238  ++jflag;
239  list = list->next;
240  }
241  fprintf(nb, "}]");
246  fprintf(nb, "Image generation");
249  fprintf(nb, "still = Show[TILES, Background-> RGBColor[.93, .91, .89], ViewPoint -> {1, 1.3, 2.4}, ");
250  fprintf(nb, "ImageSize->StillSize, Axes->True, AxesLabel->{XAxisLabel, YAxisLabel, ZAxisLabel}];\n");
253  fprintf(nb, "If[AnimationPlot,{");
254  fprintf(nb, "Do[tile[T]=Show[TILES, Background -> RGBColor[.93, .91, .89], ");
255  fprintf(nb, "ViewPoint -> {1-(.99 T/frames)^2, T/(4 frames), 2 (T/frames)^2},ImageSize->AnimationSize], {T, 0, frames, 1}],\n");
256  fprintf(nb, "Do[tile[frames+T]=Show[TILES, Background -> RGBColor[.93, .91, .89], ViewPoint -> {.005+(T/frames)^2, ");
257  fprintf(nb, "0.25-T/(4 frames), 2-2 (.99 T/frames)^2},ImageSize->AnimationSize], {T, 0, frames, 1}]}];\n");
262  fprintf(nb, "Animation Generation");
265  fprintf(nb, "If[AnimationPlot,{images = Evaluate[Table[tile[j], {j, 0, 2 frames, 1}]]}];\n");
268  fprintf(nb, "Export[StillName<>\".\"<>ToLowerCase[StillType], still, StillType, ImageSize->StillSize, ");
269  fprintf(nb, "ConversionOptions->{\"ColorReductionDither\" -> False}]");
272  fprintf(nb, "If[AnimationPlot,");
273  fprintf(nb, "Export[AnimationName, images, \"GIF\", ImageSize -> AnimationSize, ");
274  fprintf(nb, "ConversionOptions -> {\"Loop\" -> True,\"AnimationDisplayTime\" -> FrameTime, ");
275  fprintf(nb, "\"ColorReductionDither\" -> False}]]");
279  END_NOTEBOOK;
280  fclose(nb);
281  RETURN(stat);
282 }
#define INSTRUCTIONS
Definition: LALMath3DPlot.c:27
#define ABORT(statusptr, code, mesg)
#define INITSTATUS(statusptr)
#define RETURN(statusptr)
#define fprintf
int16_t INT2
Two-byte signed integer.
int32_t INT4
Four-byte signed integer.
float REAL4
Single precision real floating-point number (4 bytes).
#define BEG_TITLECELL
#define END_GROUPCELLC
void LALMath3DPlot(LALStatus *stat, Math3DPointList *first, INT4 *ntiles, REAL4 *pointSize)
This function is for plotting 3D template banks by creating a MATHEMATICA notebook.
Definition: LALMath3DPlot.c:59
#define LALMATHEMATICAH_EVAL
Invalid parameter value.
#define LALMATHEMATICAH_EFILE
Could not open file to write a Mathematica Notebook.
#define END_NOTEBOOK
#define BEG_TEXTCELL
#define BEG_GROUPCELL
#define BEG_SECTIONCELL
#define END_TEXTCELL_
#define END_GROUPCELLC_
#define BEG_NOTEBOOK
#define LALMATHEMATICAH_ENULL
NULL pointer to a LALMathematica.h input structure.
#define END_TITLECELL
#define BEG_INPUTCELL
#define END_TEXTCELL
#define END_INPUTCELL_
#define END_SECTIONCELL
#define END_INPUTCELL
#define LALFopen
Definition: LALStdio.h:50
LAL status structure, see The LALStatus structure for more details.
Definition: LALDatatypes.h:947
This type is used by LALMath3DPlot.c as an input structure to plot 3-dimensional template banks.
struct tagMath3DPointList * next