我需要计算实际框与预期框的重叠程度。我有以下信息:
{
"expectedBox": {
"height": 66,
"length": 60,
"width": 69,
"xAxis": 159,
"yAxis": 24,
"zAxis": 147
},
"actualBox": {
"height": 66,
"length": 60,
"width": 69,
"xAxis": 159,
"yAxis": 24,
"zAxis": 147
}
}
目前我有以下内容:
$obj1 = new StdClass;
$obj1->height = 66;
$obj1->length = 60;
$obj1->width = 69;
$obj1->xAxis = 159;
$obj1->yAxis = 24;
$obj1->zAxis = 147;
$obj2 = new StdClass;
$obj2->height = 68;
$obj2->length = 67;
$obj2->width = 69;
$obj2->xAxis = 260;
$obj2->yAxis = 23;
$obj2->zAxis = 142;
echo "<strong>RESULT</strong>";
echo "<pre>";
print_r(recursive_array_diff(json_decode(json_encode($obj1), true),
json_decode(json_encode($obj2), true)));
echo "</pre>";
echo "<strong>EXPECTED</strong>";
echo "<pre>";
print_r((array)$obj1);
echo "</pre>";
echo "<strong>ACTUAL</strong>";
echo "<pre>";
print_r((array)$obj2);
echo "</pre>";
function recursive_array_diff($a1, $a2) {
$r = new StdClass;
$r->diff = new StdClass;
$r->diff->data = array();
$r->diff->expected = new StdClass;
$r->diff->actual = new StdClass;
// Expected box - Calculate volume length, width, height
$r->diff->expected->volume = ($a1['length'] * $a1['width'] * $a1['height']);
// Actual box - Calculate volume length, width, height
$r->diff->actual->volume = ($a2['length'] * $a2['width'] * $a2['height']);
// Expected box/Actual box - Calculate difference between values length, width, height, xAxis, yAxis, zAxis.
foreach ($a1 as $k => $v) {
if (array_key_exists($k, $a2)) {
$r->diff->data[$k] = abs($v - $a2[$k]);
} else {
$r->diff->data[$k] = null;
}
}
$r->diff->actual->volumeAtXyzExpected = abs($a2['length'] - $a1['zAxis']) * abs($a2['width'] - $a1['zAxis']) * abs($a2['height'] - $a1['yAxis']);
$r->diff->actual->percentageOutsideExpected = (abs($r->diff->expected->volume - ($r->diff->actual->volume - $r->diff->actual->volumeAtXyzExpected)) / ($r->diff->expected->volume) *100);
return json_encode($r,JSON_PRETTY_PRINT);
}
这是正确的做法吗?