1919 * Steven G. Johnson can be contacted at stevenj@alum.mit.edu.
2020 */
2121
22+ #define _GNU_SOURCE
2223#include <stdlib.h>
2324#include <stdio.h>
2425#include <string.h>
2526#include <math.h>
27+ #include <stdarg.h>
2628
2729#ifndef LIBCTLGEOM
2830# include "ctl-io.h"
@@ -75,6 +77,25 @@ static void display_prism_info(int indentby, geometric_object *o);
7577static void init_prism (geometric_object * o );
7678/**************************************************************************/
7779
80+ /* Allows writing to Python's stdout when running from Meep's Python interface */
81+ void (* ctl_printf_callback )(const char * s ) = NULL ;
82+
83+ void ctl_printf (const char * fmt , ...)
84+ {
85+ va_list ap ;
86+ va_start (ap , fmt );
87+ if (ctl_printf_callback ) {
88+ char * s ;
89+ CHECK (vasprintf (& s , fmt , ap ) >= 0 , "vasprintf failed" );
90+ ctl_printf_callback (s );
91+ free (s );
92+ } else {
93+ vprintf (fmt , ap );
94+ fflush (stdout );
95+ }
96+ va_end (ap );
97+ }
98+
7899/* If v is a vector in the lattice basis, normalize v so that
79100 its cartesian length is unity. */
80101static void lattice_normalize (vector3 * v )
@@ -666,71 +687,71 @@ material_type material_of_point(vector3 p)
666687void CTLIO display_geometric_object_info (int indentby , geometric_object o )
667688{
668689 geom_fix_object_ptr (& o );
669- printf ("%*s" , indentby , "" );
690+ ctl_printf ("%*s" , indentby , "" );
670691 switch (o .which_subclass ) {
671692 case GEOM CYLINDER :
672693 switch (o .subclass .cylinder_data -> which_subclass ) {
673694 case CYL WEDGE :
674- printf ("wedge" );
695+ ctl_printf ("wedge" );
675696 break ;
676697 case CYL CONE :
677- printf ("cone" );
698+ ctl_printf ("cone" );
678699 break ;
679700 case CYL CYLINDER_SELF :
680- printf ("cylinder" );
701+ ctl_printf ("cylinder" );
681702 break ;
682703 }
683704 break ;
684705 case GEOM SPHERE :
685- printf ("sphere" );
706+ ctl_printf ("sphere" );
686707 break ;
687708 case GEOM BLOCK :
688709 switch (o .subclass .block_data -> which_subclass ) {
689710 case BLK ELLIPSOID :
690- printf ("ellipsoid" );
711+ ctl_printf ("ellipsoid" );
691712 break ;
692713 case BLK BLOCK_SELF :
693- printf ("block" );
714+ ctl_printf ("block" );
694715 break ;
695716 }
696717 break ;
697718 case GEOM PRISM :
698- printf ("prism" );
719+ ctl_printf ("prism" );
699720 break ;
700721 case GEOM COMPOUND_GEOMETRIC_OBJECT :
701- printf ("compound object" );
722+ ctl_printf ("compound object" );
702723 break ;
703724 default :
704- printf ("geometric object" );
725+ ctl_printf ("geometric object" );
705726 break ;
706727 }
707- printf (", center = (%g,%g,%g)\n" ,
728+ ctl_printf (", center = (%g,%g,%g)\n" ,
708729 o .center .x , o .center .y , o .center .z );
709730 switch (o .which_subclass ) {
710731 case GEOM CYLINDER :
711- printf ("%*s radius %g, height %g, axis (%g, %g, %g)\n" ,
732+ ctl_printf ("%*s radius %g, height %g, axis (%g, %g, %g)\n" ,
712733 indentby , "" , o .subclass .cylinder_data -> radius ,
713734 o .subclass .cylinder_data -> height ,
714735 o .subclass .cylinder_data -> axis .x ,
715736 o .subclass .cylinder_data -> axis .y ,
716737 o .subclass .cylinder_data -> axis .z );
717738 if (o .subclass .cylinder_data -> which_subclass == CYL CONE )
718- printf ("%*s radius2 %g\n" , indentby , "" ,
739+ ctl_printf ("%*s radius2 %g\n" , indentby , "" ,
719740 o .subclass .cylinder_data -> subclass .cone_data -> radius2 );
720741 else if (o .subclass .cylinder_data -> which_subclass == CYL WEDGE )
721- printf ("%*s wedge-theta %g\n" , indentby , "" ,
742+ ctl_printf ("%*s wedge-theta %g\n" , indentby , "" ,
722743 o .subclass .cylinder_data -> subclass .wedge_data -> wedge_angle );
723744 break ;
724745 case GEOM SPHERE :
725- printf ("%*s radius %g\n" , indentby , "" ,
746+ ctl_printf ("%*s radius %g\n" , indentby , "" ,
726747 o .subclass .sphere_data -> radius );
727748 break ;
728749 case GEOM BLOCK :
729- printf ("%*s size (%g,%g,%g)\n" , indentby , "" ,
750+ ctl_printf ("%*s size (%g,%g,%g)\n" , indentby , "" ,
730751 o .subclass .block_data -> size .x ,
731752 o .subclass .block_data -> size .y ,
732753 o .subclass .block_data -> size .z );
733- printf ("%*s axes (%g,%g,%g), (%g,%g,%g), (%g,%g,%g)\n" ,
754+ ctl_printf ("%*s axes (%g,%g,%g), (%g,%g,%g), (%g,%g,%g)\n" ,
734755 indentby , "" ,
735756 o .subclass .block_data -> e1 .x ,
736757 o .subclass .block_data -> e1 .y ,
@@ -752,7 +773,7 @@ void CTLIO display_geometric_object_info(int indentby, geometric_object o)
752773 -> component_objects .num_items ;
753774 geometric_object * os = o .subclass .compound_geometric_object_data
754775 -> component_objects .items ;
755- printf ("%*s %d components:\n" , indentby , "" , n );
776+ ctl_printf ("%*s %d components:\n" , indentby , "" , n );
756777 for (i = 0 ; i < n ; ++ i )
757778 display_geometric_object_info (indentby + 5 , os [i ]);
758779 break ;
@@ -1926,16 +1947,16 @@ void display_geom_box_tree(int indentby, geom_box_tree t)
19261947
19271948 if (!t )
19281949 return ;
1929- printf ("%*sbox (%g..%g, %g..%g, %g..%g)\n" , indentby , "" ,
1950+ ctl_printf ("%*sbox (%g..%g, %g..%g, %g..%g)\n" , indentby , "" ,
19301951 t -> b .low .x , t -> b .high .x ,
19311952 t -> b .low .y , t -> b .high .y ,
19321953 t -> b .low .z , t -> b .high .z );
19331954 for (i = 0 ; i < t -> nobjects ; ++ i ) {
1934- printf ("%*sbounding box (%g..%g, %g..%g, %g..%g)\n" , indentby + 5 , "" ,
1955+ ctl_printf ("%*sbounding box (%g..%g, %g..%g, %g..%g)\n" , indentby + 5 , "" ,
19351956 t -> objects [i ].box .low .x , t -> objects [i ].box .high .x ,
19361957 t -> objects [i ].box .low .y , t -> objects [i ].box .high .y ,
19371958 t -> objects [i ].box .low .z , t -> objects [i ].box .high .z );
1938- printf ("%*sshift object by (%g, %g, %g)\n" , indentby + 5 , "" ,
1959+ ctl_printf ("%*sshift object by (%g, %g, %g)\n" , indentby + 5 , "" ,
19391960 t -> objects [i ].shiftby .x , t -> objects [i ].shiftby .y ,
19401961 t -> objects [i ].shiftby .z );
19411962 display_geometric_object_info (indentby + 5 , * t -> objects [i ].o );
@@ -2547,11 +2568,11 @@ void display_prism_info(int indentby, geometric_object *o)
25472568 vector3 * vs = prsm -> vertices .items ;
25482569 int num_vertices = prsm -> vertices .num_items ;
25492570
2550- printf ("%*s height %g, axis (%g,%g,%g), %i vertices:\n" ,
2571+ ctl_printf ("%*s height %g, axis (%g,%g,%g), %i vertices:\n" ,
25512572 indentby , "" ,prsm -> height ,prsm -> axis .x ,prsm -> axis .y ,prsm -> axis .z ,num_vertices );
25522573 int nv ;
25532574 for (nv = 0 ; nv < num_vertices ; nv ++ )
2554- printf ("%*s (%g,%g,%g)\n" ,indentby ,"" ,vs [nv ].x ,vs [nv ].y ,vs [nv ].z );
2575+ ctl_printf ("%*s (%g,%g,%g)\n" ,indentby ,"" ,vs [nv ].x ,vs [nv ].y ,vs [nv ].z );
25552576}
25562577
25572578/***************************************************************/
0 commit comments