32#include <libdap/BaseType.h>
33#include <libdap/Int32.h>
34#include <libdap/Str.h>
35#include <libdap/Array.h>
36#include <libdap/Structure.h>
38#include <libdap/D4RValue.h>
39#include <libdap/Error.h>
40#include <libdap/debug.h>
41#include <libdap/util.h>
42#include <libdap/ServerFunctionsList.h>
44#include "BBoxUnionFunction.h"
72function_dap2_bbox_union(
int argc, BaseType *argv[], DDS &, BaseType **btpp)
74 const string wrong_args =
"Wrong number of arguments to bbox_union(). Expected one or more bounding boxes and a string naming the operation (2+ arguments)";
76 unsigned int rank = 0;
77 string operation =
"";
83 throw Error(malformed_expr, wrong_args);
87 rank = roi_valid_bbox(argv[0]);
91 for (
int i = 1; i < argc-1; ++i)
92 if (roi_valid_bbox(argv[0]) != rank)
93 throw Error(malformed_expr,
"In function bbox_union(): All bounding boxes must be the same shape to form their union.");
95 operation = extract_string_argument(argv[argc-1]);
105 vector<slice> result(rank);
107 for (
unsigned int i = 0; i < rank; ++i) {
112 roi_bbox_get_slice_data(
static_cast<Array*
>(argv[0]), i, start, stop, name);
114 result.at(i).start = start;
115 result.at(i).stop = stop;
116 result.at(i).name = name;
120 for (
int i = 1; i < argc-1; ++i) {
122 Array *bbox =
static_cast<Array*
>(argv[i]);
124 for (
unsigned int i = 0; i < rank; ++i) {
128 roi_bbox_get_slice_data(bbox, i, start, stop, name);
130 if (result.at(i).name != name)
131 throw Error(
"In function bbox_union(): named dimensions must match in the bounding boxes");
133 if (operation ==
"union") {
134 result.at(i).start = min(result.at(i).start, start);
135 result.at(i).stop = max(result.at(i).stop, stop);
137 else if (operation ==
"inter" || operation ==
"intersection") {
138 result.at(i).start = max(result.at(i).start, start);
139 result.at(i).stop = min(result.at(i).stop, stop);
141 if (result.at(i).stop < result.at(i).start)
142 throw Error(
"In bbox_union(): The intersection of the bounding boxes is empty (dimension " + long_to_string(i) +
").");
145 throw Error(malformed_expr,
"In bbox_union(): Unknown operator '" + operation +
"'; expected 'union', 'intersection' or 'inter'.");
151 unique_ptr<Array> response = roi_bbox_build_empty_bbox(rank, operation);
152 for (
unsigned int i = 0; i < rank; ++i) {
153 Structure *
slice = roi_bbox_build_slice(result.at(i).start, result.at(i).stop, result.at(i).name);
154 response->set_vec_nocopy(i,
slice);
158 *btpp = response.release();
173BaseType *function_dap4_bbox_union(D4RValueList *, DMR &)
177 throw Error(malformed_expr,
"Not yet implemented for DAP4 functions.");