Repository: NVlabs/instant-ngp
File: include/neural-graphics-primitives/json_binding.h (C/C++)

Code before:

inline void from_json(const nlohmann::json& j, NerfDataset& dataset) {
	dataset.n_images = j.at("n_images");
	dataset.metadata.resize(dataset.n_images);
	dataset.xforms.resize(dataset.n_images);

	for (size_t i = 0; i < dataset.n_images; ++i) {
		// read global defaults first
		if (j.contains("camera_distortion")) from_json(j.at("camera_distortion"), dataset.metadata[i].camera_distortion);
		if (j.contains("principal_point")) from_json(j.at("principal_point"), dataset.metadata[i].principal_point);
		if (j.contains("rolling_shutter")) from_json(j.at("rolling_shutter"), dataset.metadata[i].rolling_shutter);
		if (j.contains("focal_length")) from_json(j.at("focal_length"), dataset.metadata[i].focal_length);
		if (j.contains("image_resolution")) from_json(j.at("image_resolution"), dataset.metadata[i].resolution);

		from_json(j.at("xforms").at(i), dataset.xforms[i]);
		if (j.contains("focal_lengths")) from_json(j.at("focal_lengths").at(i), dataset.metadata[i].focal_length);
		if (j.contains("metadata")) {
			auto &ji = j["metadata"].at(i);
			from_json(ji.at("resolution"), dataset.metadata[i].resolution);
			from_json(ji.at("focal_length"), dataset.metadata[i].focal_length);
			from_json(ji.at("principal_point"), dataset.metadata[i].principal_point);
			from_json(ji.at("rolling_shutter"), dataset.metadata[i].rolling_shutter);
			from_json(ji.at("camera_distortion"), dataset.metadata[i].camera_distortion);
		}
		if (j.contains("paths")) {
			dataset.paths.push_back(j["paths"].at(i));
		} else {
			dataset.paths.push_back(std::string(""));
		}
	}

	dataset.render_aabb = j.at("render_aabb");
	dataset.render_aabb_to_local = Eigen::Matrix3f::Identity();
	if (j.contains("render_aabb_to_local")) from_json(j.at("render_aabb_to_local"), dataset.render_aabb_to_local);

	from_json(j.at("up"), dataset.up);
	from_json(j.at("offset"), dataset.offset);
	from_json(j.at("envmap_resolution"), dataset.envmap_resolution);
	dataset.scale = j.at("scale");
	dataset.aabb_scale = j.at("aabb_scale");
	dataset.from_mitsuba = j.at("from_mitsuba");
	dataset.is_hdr = j.value("is_hdr", false);
	if (j.contains("wants_importance_sampling")) {
		dataset.wants_importance_sampling = j.at("wants_importance_sampling");
	} else {
		dataset.wants_importance_sampling = true;
	}
}

NGP_NAMESPACE_END


Review comment (bug):
I recommend not using `push_back` in case `from_json` is called multiple times on the same dataset (which would result in an incorrect number of paths). Instead, above the for loop, I would do the following:

```cpp
dataset.paths.resize(dataset.n_images, "");
if (j.contains("paths")) dataset.paths = j["paths"];
```

Code after:

inline void from_json(const nlohmann::json& j, NerfDataset& dataset) {
	dataset.n_images = j.at("n_images");
	dataset.metadata.resize(dataset.n_images);
	dataset.xforms.resize(dataset.n_images);
	dataset.paths.resize(dataset.n_images, "");
	if (j.contains("paths")) dataset.paths = j["paths"].get<std::vector<std::string>>();

	for (size_t i = 0; i < dataset.n_images; ++i) {
		// read global defaults first
		if (j.contains("camera_distortion")) from_json(j.at("camera_distortion"), dataset.metadata[i].camera_distortion);
		if (j.contains("principal_point")) from_json(j.at("principal_point"), dataset.metadata[i].principal_point);
		if (j.contains("rolling_shutter")) from_json(j.at("rolling_shutter"), dataset.metadata[i].rolling_shutter);
		if (j.contains("focal_length")) from_json(j.at("focal_length"), dataset.metadata[i].focal_length);
		if (j.contains("image_resolution")) from_json(j.at("image_resolution"), dataset.metadata[i].resolution);

		from_json(j.at("xforms").at(i), dataset.xforms[i]);
		if (j.contains("focal_lengths")) from_json(j.at("focal_lengths").at(i), dataset.metadata[i].focal_length);
		if (j.contains("metadata")) {
			auto &ji = j["metadata"].at(i);
			from_json(ji.at("resolution"), dataset.metadata[i].resolution);
			from_json(ji.at("focal_length"), dataset.metadata[i].focal_length);
			from_json(ji.at("principal_point"), dataset.metadata[i].principal_point);
			from_json(ji.at("rolling_shutter"), dataset.metadata[i].rolling_shutter);
			from_json(ji.at("camera_distortion"), dataset.metadata[i].camera_distortion);
		}
	}

	dataset.render_aabb = j.at("render_aabb");
	dataset.render_aabb_to_local = Eigen::Matrix3f::Identity();
	if (j.contains("render_aabb_to_local")) from_json(j.at("render_aabb_to_local"), dataset.render_aabb_to_local);

	from_json(j.at("up"), dataset.up);
	from_json(j.at("offset"), dataset.offset);
	from_json(j.at("envmap_resolution"), dataset.envmap_resolution);
	dataset.scale = j.at("scale");
	dataset.aabb_scale = j.at("aabb_scale");
	dataset.from_mitsuba = j.at("from_mitsuba");
	dataset.is_hdr = j.value("is_hdr", false);
	if (j.contains("wants_importance_sampling")) {
		dataset.wants_importance_sampling = j.at("wants_importance_sampling");
	} else {
		dataset.wants_importance_sampling = true;
	}
}

NGP_NAMESPACE_END