sadist_lib_spherical_source.c (5323B)
1 /* Copyright (C) 2024 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include <stardis/stardis-prog-properties.h> 17 18 #include <rsys/cstr.h> 19 #include <rsys/mem_allocator.h> 20 21 #include <stdarg.h> /* va_list */ 22 #include <getopt.h> 23 24 struct source { 25 double position[3]; 26 double power; /* [W] */ 27 double diffuse_radiance; /* [W/m^2/sr] */ 28 }; 29 #define SOURCE_NULL__ {{0,0,0}, 0, 0} 30 static const struct source SOURCE_NULL = SOURCE_NULL__; 31 32 /******************************************************************************* 33 * Helper functions 34 ******************************************************************************/ 35 static void 36 print_usage(FILE* stream, const char* name) 37 { 38 ASSERT(name); 39 fprintf(stream, 40 "usage: %s [-h] [-d diffuse_radiance] [-p x,y,z] [-w power]\n", 41 name); 42 } 43 44 static res_T 45 parse_args 46 (const struct stardis_description_create_context *ctx, 47 struct source* source, 48 int argc, 49 char* argv[]) 50 { 51 size_t len = 0; 52 int opt = 0; 53 res_T res = RES_OK; 54 ASSERT(ctx && source); 55 56 optind = 1; 57 while((opt = getopt(argc, argv, "d:hp:w:")) != -1) { 58 switch(opt) { 59 case 'd': 60 res = cstr_to_double(optarg, &source->diffuse_radiance); 61 if(res == RES_OK && source->diffuse_radiance < 0) res = RES_BAD_ARG; 62 break; 63 case 'h': 64 print_usage(stdout, ctx->name); 65 break; 66 case 'p': 67 res = cstr_to_list_double(optarg, ',', source->position, &len, 3); 68 if(res == RES_OK && len < 3) res = RES_BAD_ARG; 69 break; 70 case 'w': 71 res = cstr_to_double(optarg, &source->power); 72 if(res == RES_OK && source->power < 0) res = RES_BAD_ARG; 73 break; 74 default: res = RES_BAD_ARG; break; 75 } 76 if(res != RES_OK) { 77 if(optarg) { 78 fprintf(stderr, "%s: invalid option argument '%s' -- '%c'\n", 79 ctx->name, optarg, opt); 80 } 81 goto error; 82 } 83 } 84 85 exit: 86 return res; 87 error: 88 goto exit; 89 } 90 91 /******************************************************************************* 92 * Create data 93 ******************************************************************************/ 94 void* 95 stardis_create_data 96 (const struct stardis_description_create_context *ctx, 97 void* libdata, 98 size_t argc, 99 char* argv[]) 100 { 101 struct source* source = NULL; 102 res_T res = RES_OK; 103 (void)libdata; 104 105 source = mem_alloc(sizeof(*source)); 106 if(!source) { 107 fprintf(stderr, "%s:%d: error allocating the external spherical source.\n", 108 __FILE__, __LINE__); 109 goto error; 110 } 111 *source = SOURCE_NULL; 112 113 res = parse_args(ctx, source, (int)argc, argv); 114 if(res != RES_OK) goto error; 115 116 exit: 117 return source; 118 error: 119 if(source) { 120 mem_rm(source); 121 source = NULL; 122 } 123 goto exit; 124 } 125 126 void 127 stardis_release_data(void* data) 128 { 129 ASSERT(data); 130 mem_rm(data); 131 } 132 133 /******************************************************************************* 134 * External source 135 ******************************************************************************/ 136 double* 137 stardis_spherical_source_position 138 (const double time, /* [s] */ 139 double position[3], 140 void* data) 141 { 142 struct source* source = data; 143 (void)time; /* Avoid "unused variable" warning */ 144 ASSERT(source); 145 position[0] = source->position[0]; 146 position[1] = source->position[1]; 147 position[2] = source->position[2]; 148 return position; 149 } 150 151 double /* [W] */ 152 stardis_spherical_source_power(const double time /* [s] */, void* data) 153 { 154 struct source* source = data; 155 (void)time; /* Avoid "unused variable" warning */ 156 ASSERT(source); 157 return source->power; /* [W] */ 158 } 159 160 double /* [W/m^2/sr] */ 161 stardis_spherical_source_diffuse_radiance 162 (const double time, /* [s] */ 163 const double dir[3], 164 void* data) 165 { 166 struct source* source = data; 167 (void)time, (void)dir; /* Avoid "unused variable" warning */ 168 ASSERT(source); 169 return source->diffuse_radiance; 170 } 171 172 /******************************************************************************* 173 * Legal notices 174 ******************************************************************************/ 175 const char* 176 get_copyright_notice(void* data) 177 { 178 (void)data; /* Avoid "unused variable" warnings */ 179 return "Copyright (C) 2024 |Méso|Star> (contact@meso-star.com)"; 180 } 181 182 const char* 183 get_license_short(void* data) 184 { 185 (void)data; /* Avoid "unused variable" warnings */ 186 return "GNU GPL version 3 or later <http://www.gnu.org/licenses/>"; 187 } 188 189 const char* 190 get_license_text(void* data) 191 { 192 (void)data; /* Avoid "unused variable" warnings */ 193 return 194 "This is free software released under the GPL v3+ license: GNU GPL\n" 195 "version 3 or later. You are welcome to redistribute it under certain\n" 196 "conditions; refer to <http://www.gnu.org/licenses/> for details."; 197 }