Skip to content

Commit 3e21dce

Browse files
Add isUsgsCsmIsd() and isUsgsCsmState() quick peek functions
Add lightweight string-search functions to identify USGS CSM file types (ISD vs model state) and extract the model name without JSON parsing or model construction. isUsgsCsmIsd() checks for '{' start, ISD-only keys (body_rotation, instrument_position), and extracts the USGS_ASTRO model name. isUsgsCsmState() skips to first '{' (bypassing .sup preamble per stateAsJson() logic), checks for m_modelName, and extracts the name. Applied in usgscsm_cam_test loadCsmCameraModel(): replaces the plugin iteration loop with direct peek-then-construct for both ISD and state paths. Instead of trying all 5 model types (each building and discarding a full model), the model name is determined by a quick string search and only the matching model is constructed.
1 parent df4ff0f commit 3e21dce

3 files changed

Lines changed: 46 additions & 54 deletions

File tree

bin/usgscsm_cam_test.cc

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -191,63 +191,24 @@ bool loadCsmCameraModel(std::string const& model_file,
191191
return true;
192192
}
193193

194-
// Not an ISD. Try model state path (JSON state, .sup, etc.) via plugin iteration.
195-
bool success = false;
196-
csm::PluginList plugins = csm::Plugin::getList();
197-
for (auto iter = plugins.begin(); iter != plugins.end(); iter++) {
198-
199-
const csm::Plugin* csm_plugin = (*iter);
200-
std::cout << "Detected CSM plugin: " << csm_plugin->getPluginName() << "\n";
201-
202-
size_t num_models = csm_plugin->getNumModels();
203-
std::cout << "Number of models for this plugin: " << num_models << "\n";
204-
205-
csm::Model *csm = NULL;
206-
csm::WarningList *warnings = new csm::WarningList;
207-
for (size_t i = 0; i < num_models; i++) {
208-
209-
std::string model_name = (*iter)->getModelName(i);
210-
211-
if (csm_plugin->canModelBeConstructedFromState(model_name, model_state, warnings)) {
212-
csm = csm_plugin->constructModelFromState(model_state, warnings);
213-
std::cout << "Loaded a CSM model of type " << model_name
214-
<< " from model state file " << model_file << ".\n";
215-
success = true;
216-
} else {
217-
if (opt.verbose) {
218-
std::string startStr = "<<<<<< Warnings from " + model_name + " <<<<<<";
219-
std::cout << startStr << std::endl;
220-
for (auto warning : *warnings)
221-
std::cout << warning.getMessage() << std::endl;
222-
std::string endStr(startStr.size(), '<');
223-
std::cout << endStr << std::endl;
224-
}
225-
warnings->clear();
226-
continue;
227-
}
228-
229-
delete warnings;
230-
231-
csm::RasterGM *modelPtr = dynamic_cast<csm::RasterGM*>(csm);
232-
if (modelPtr == NULL) {
233-
// Normally earlier checks should be enough and this should not happen
234-
std::cerr << "Could not load correctly a CSM model.\n";
235-
return false;
236-
} else {
237-
// Assign to a smart pointer which will handle deallocation
238-
model = std::shared_ptr<csm::RasterGM>(modelPtr);
239-
std::cout << "Final model: " << model->getModelName() << std::endl;
240-
break;
241-
}
194+
// Quick peek for model state (JSON state or .sup)
195+
std::string state_model_name;
196+
if (isUsgsCsmState(model_state, state_model_name)) {
197+
std::cout << "Detected model state with model: " << state_model_name << "\n";
198+
UsgsAstroPlugin cameraPlugin;
199+
csm::Model *csm = cameraPlugin.constructModelFromState(model_state, NULL);
200+
if (!csm) {
201+
std::cerr << "Failed to construct model from state: " << model_file << ".\n";
202+
return false;
242203
}
204+
model = std::shared_ptr<csm::RasterGM>(dynamic_cast<csm::RasterGM*>(csm));
205+
std::cout << "Loaded a CSM model of type " << state_model_name
206+
<< " from model state file " << model_file << ".\n";
207+
return true;
243208
}
244209

245-
if (!success) {
246-
std::cerr << "Failed to load a CSM model from: " << model_file << ".\n";
247-
return false;
248-
}
249-
250-
return true;
210+
std::cerr << "Failed to load a CSM model from: " << model_file << ".\n";
211+
return false;
251212
}
252213

253214
bool updateSupModel(std::string& sup_string, std::string model) {

include/usgscsm/UsgsAstroPluginSupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ csm::RasterGM *getUsgsCsmModelFromState(const std::string &stringState, const st
1010
csm::RasterGM *getUsgsCsmModelFromJson(const nlohmann::json &j, const std::string &modelName, csm::WarningList *warnings);
1111
nlohmann::json getUsgsCsmModelJson(csm::RasterGM *model);
1212
bool isUsgsCsmIsd(const std::string &str, std::string &modelName);
13+
bool isUsgsCsmState(const std::string &str, std::string &modelName);
1314

1415
#endif // INCLUDE_USGSCSM_USGSASTROPLUGINSUPPORT_H_

src/UsgsAstroPluginSupport.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,33 @@ bool isUsgsCsmIsd(const std::string &str, std::string &modelName) {
178178
modelName = str.substr(pos + 1, end - pos - 1);
179179
return !modelName.empty();
180180
}
181+
182+
// Quick check if the given string is a USGS CSM model state (JSON state or GXP
183+
// .sup format). If yes, extract the model name. No JSON parsing is done, just
184+
// raw string searches. Following the logic in stateAsJson() (Utilities.cpp),
185+
// skip to the first '{' to bypass any .sup preamble, then search within the
186+
// JSON portion only.
187+
bool isUsgsCsmState(const std::string &str, std::string &modelName) {
188+
modelName.clear();
189+
190+
// Find the start of the JSON blob (skips .sup preamble if present)
191+
auto brace = str.find_first_of("{");
192+
if (brace == std::string::npos)
193+
return false;
194+
195+
// Model state uses m_modelName; ISDs use name_model
196+
if (str.find("\"m_modelName\"", brace) == std::string::npos)
197+
return false;
198+
199+
// Extract the model name value
200+
std::string prefix = "\"USGS_ASTRO_";
201+
auto pos = str.find(prefix, brace);
202+
if (pos == std::string::npos)
203+
return false;
204+
auto end = str.find("\"", pos + 1);
205+
if (end == std::string::npos)
206+
return false;
207+
208+
modelName = str.substr(pos + 1, end - pos - 1);
209+
return !modelName.empty();
210+
}

0 commit comments

Comments
 (0)