Skip to content

Commit df4ff0f

Browse files
Add isUsgsCsmIsd() quick peek to avoid expensive model construction
isUsgsCsmIsd() checks if a string is a USGS JSON ISD using raw string searches (no JSON parsing). Checks for '{' start, ISD-only keys (body_rotation, instrument_position), and extracts the model name. Used in usgscsm_cam_test to skip the plugin iteration loop for ISDs. Instead of trying all 5 model types (each building and discarding a full model), the ISD path now peeks for the model name and constructs only the matching model.
1 parent 20780bc commit df4ff0f

3 files changed

Lines changed: 53 additions & 16 deletions

File tree

bin/usgscsm_cam_test.cc

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,31 @@ bool loadCsmCameraModel(std::string const& model_file,
168168
return true;
169169
}
170170

171-
// Try to read the model as an ISD
172-
csm::Isd isd(model_file);
173-
174-
// Read the model in a string, for potentially finding parsing the
175-
// model state from it.
171+
// Read the file into a string
176172
std::string model_state;
177173
if (!readFileInString(model_file, model_state))
178174
return false;
179175

180-
// Check if loading the model worked
181-
bool success = false;
176+
// Quick peek: if this is a JSON ISD, we know the model name without
177+
// expensive trial-and-error. Construct only the matching model.
178+
std::string isd_model_name;
179+
if (isUsgsCsmIsd(model_state, isd_model_name)) {
180+
std::cout << "Detected JSON ISD with model: " << isd_model_name << "\n";
181+
csm::Isd isd(model_file);
182+
UsgsAstroPlugin cameraPlugin;
183+
csm::Model *csm = cameraPlugin.constructModelFromISD(isd, isd_model_name, NULL);
184+
if (!csm) {
185+
std::cerr << "Failed to construct model from ISD: " << model_file << ".\n";
186+
return false;
187+
}
188+
model = std::shared_ptr<csm::RasterGM>(dynamic_cast<csm::RasterGM*>(csm));
189+
std::cout << "Loaded a CSM model of type " << isd_model_name
190+
<< " from ISD file " << model_file << ".\n";
191+
return true;
192+
}
182193

183-
// Try all detected plugins and all models for each plugin.
194+
// Not an ISD. Try model state path (JSON state, .sup, etc.) via plugin iteration.
195+
bool success = false;
184196
csm::PluginList plugins = csm::Plugin::getList();
185197
for (auto iter = plugins.begin(); iter != plugins.end(); iter++) {
186198

@@ -196,14 +208,7 @@ bool loadCsmCameraModel(std::string const& model_file,
196208

197209
std::string model_name = (*iter)->getModelName(i);
198210

199-
if (csm_plugin->canModelBeConstructedFromISD(isd, model_name, warnings)) {
200-
// Try to construct the model from the ISD
201-
csm = csm_plugin->constructModelFromISD(isd, model_name, warnings);
202-
std::cout << "Loaded a CSM model of type " << model_name << " from ISD file "
203-
<< model_file << ".\n";
204-
success = true;
205-
} else if (csm_plugin->canModelBeConstructedFromState(model_name, model_state, warnings)) {
206-
// Try to construct it from the model state (handles .sup files)
211+
if (csm_plugin->canModelBeConstructedFromState(model_name, model_state, warnings)) {
207212
csm = csm_plugin->constructModelFromState(model_state, warnings);
208213
std::cout << "Loaded a CSM model of type " << model_name
209214
<< " from model state file " << model_file << ".\n";

include/usgscsm/UsgsAstroPluginSupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ csm::RasterGM *getUsgsCsmModelFromIsd(const std::string &stringIsd, const std::s
99
csm::RasterGM *getUsgsCsmModelFromState(const std::string &stringState, const std::string &modelName, csm::WarningList *warnings);
1010
csm::RasterGM *getUsgsCsmModelFromJson(const nlohmann::json &j, const std::string &modelName, csm::WarningList *warnings);
1111
nlohmann::json getUsgsCsmModelJson(csm::RasterGM *model);
12+
bool isUsgsCsmIsd(const std::string &str, std::string &modelName);
1213

1314
#endif // INCLUDE_USGSCSM_USGSASTROPLUGINSUPPORT_H_

src/UsgsAstroPluginSupport.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,34 @@ nlohmann::json getUsgsCsmModelJson(csm::RasterGM *model) {
147147
std::string aFunction = "UsgsAstroPluginSupport::getUsgsCsmModelJson()";
148148
throw csm::Error(aErrorType, aMessage, aFunction);
149149
}
150+
151+
// Quick check if the given string is a USGS CSM JSON ISD (not a model state,
152+
// .sup, etc.). If yes, extract the model name. No JSON parsing is done, just
153+
// raw string searches.
154+
bool isUsgsCsmIsd(const std::string &str, std::string &modelName) {
155+
modelName.clear();
156+
157+
// Model state strings have a text prefix before '{'. ISDs start with '{'.
158+
auto pos = str.find_first_not_of(" \t\n\r");
159+
if (pos == std::string::npos || str[pos] != '{')
160+
return false;
161+
162+
// Check for ISD-only keys (never present in model state)
163+
if (str.find("\"body_rotation\"") == std::string::npos)
164+
return false;
165+
if (str.find("\"instrument_position\"") == std::string::npos)
166+
return false;
167+
168+
// Extract the name_model value. Search for the model name prefix
169+
// which immediately follows the "name_model" key's value quote.
170+
std::string prefix = "\"USGS_ASTRO_";
171+
pos = str.find(prefix);
172+
if (pos == std::string::npos)
173+
return false;
174+
auto end = str.find("\"", pos + 1);
175+
if (end == std::string::npos)
176+
return false;
177+
178+
modelName = str.substr(pos + 1, end - pos - 1);
179+
return !modelName.empty();
180+
}

0 commit comments

Comments
 (0)