Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\HocrParser\Data;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
class BoundingBox implements JsonSerializable
|
||||
{
|
||||
/** @var array $data */
|
||||
private $data;
|
||||
private $tlx;
|
||||
private $tly;
|
||||
private $brx;
|
||||
private $bry;
|
||||
|
||||
/**
|
||||
* @param int $tlx
|
||||
* @param int $tly
|
||||
* @param int $brx
|
||||
* @param int $bry
|
||||
* @param array $data Zusätzliche Nutzdaten
|
||||
*/
|
||||
public function __construct($tlx, $tly, $brx, $bry, array $data = [])
|
||||
{
|
||||
$this->tlx = $tlx;
|
||||
$this->tly = $tly;
|
||||
$this->brx = $brx;
|
||||
$this->bry = $bry;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetDataAll()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function GetData($name)
|
||||
{
|
||||
return $this->data[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function SetData($name, $value)
|
||||
{
|
||||
$this->data[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function HasData($name)
|
||||
{
|
||||
return isset($this->data[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Liegt Punkt innerhalb der Box?
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function IsPointWithin($x, $y)
|
||||
{
|
||||
if ($x > $this->tlx && $x < $this->brx &&
|
||||
$y > $this->tly && $y < $this->bry) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Befindet sich Box rechts von übergebenen Koordinaten
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function IsRightFromPoint($x, $y)
|
||||
{
|
||||
$isSameHeight = ($y > $this->tly && $y < $this->bry) ? true : false;
|
||||
if ($isSameHeight === true) {
|
||||
return ($x < $this->tlx) ? true : false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Befindet sich Box rechts von übergebenen Koordinaten
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function IsLeftFromPoint($x, $y)
|
||||
{
|
||||
$isSameHeight = ($y > $this->tly && $y < $this->bry) ? true : false;
|
||||
if ($isSameHeight === true) {
|
||||
return ($x > $this->brx) ? true : false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Befindet sich die Box oberhalb der übergebenen Koordinaten
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function IsAbovePoint($x, $y)
|
||||
{
|
||||
$isSameColumn = ($x > $this->tlx && $x < $this->brx) ? true : false;
|
||||
if ($isSameColumn === true) {
|
||||
return ($this->bry < $y) ? true : false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Befindet sich die Box unterhalb der übergebenen Koordinaten
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function IsBelowPoint($x, $y)
|
||||
{
|
||||
$isSameColumn = ($x > $this->tlx && $x < $this->brx) ? true : false;
|
||||
if ($isSameColumn === true) {
|
||||
return ($this->tly > $y) ? true : false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Berechnet die Entfernung zwischen Mittelpunkt der Box und dem übergebenen Punkt
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function GetDistanceFromPoint($x, $y)
|
||||
{
|
||||
$center = $this->GetCenterPoint();
|
||||
|
||||
return sqrt(pow($center['x'] - $x, 2) + pow($center['y'] - $y, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt Mittelpunkt der Box zurück
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function GetCenterPoint()
|
||||
{
|
||||
return [
|
||||
'x' => (int)($this->tlx + (($this->brx - $this->tlx) / 2)),
|
||||
'y' => (int)($this->tly + (($this->bry - $this->tly) / 2)),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'tlx' => $this->tlx,
|
||||
'tly' => $this->tly,
|
||||
'brx' => $this->brx,
|
||||
'bry' => $this->bry,
|
||||
'data' => $this->data,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\HocrParser\Data;
|
||||
|
||||
use Xentral\Components\ScanbotApi\Exception\RuntimeException;
|
||||
|
||||
class BoundingBoxCollection
|
||||
{
|
||||
/** @var array|BoundingBox[] $boxes */
|
||||
private $boxes;
|
||||
|
||||
/**
|
||||
* @param array|BoundingBox[] $boxes
|
||||
*/
|
||||
public function __construct(array $boxes = [])
|
||||
{
|
||||
$this->boxes = $boxes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|BoundingBox[]
|
||||
*/
|
||||
public function GetBoxes()
|
||||
{
|
||||
return $this->boxes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Box mit der kürzesten Entfernung rechts vom übergebenen Punkt zurückgeben
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return BoundingBox|false
|
||||
*/
|
||||
public function GetNearestBoxRightFromPoint($x, $y)
|
||||
{
|
||||
// Erstmal alle Elemente rechts vom Punkt finden; inkl. Entfernung zum Punkt
|
||||
$candidates = $this->GetBoxesRightFromPoint($x, $y);
|
||||
|
||||
return $this->GetBoxWithLowestDistance($candidates);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alle Boxen zurückgeben die sich rechts vom übergebenen Punkt befinden; inkl. Entfernung
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return array|BoundingBox[]
|
||||
*/
|
||||
public function GetBoxesRightFromPoint($x, $y)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($this->boxes as $box) {
|
||||
if ($box->IsRightFromPoint($x, $y)) {
|
||||
$distance = $box->GetDistanceFromPoint($x, $y);
|
||||
$box->SetData('distance', $distance);
|
||||
$result[] = $box;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Box mit dem geringsten Abstand ermitteln
|
||||
*
|
||||
* Hinweis: Data-Eigenschaft "distance" muss dafür gesetzt sein.
|
||||
*
|
||||
* @param array|BoundingBox[] $boxes
|
||||
*
|
||||
* @return BoundingBox|false
|
||||
*/
|
||||
private function GetBoxWithLowestDistance(array $boxes)
|
||||
{
|
||||
// Index neu aufbauen
|
||||
$boxes = array_values($boxes);
|
||||
|
||||
// Keine Box gefunden
|
||||
if (count($boxes) === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Nur eine Box gefunden
|
||||
if (count($boxes) === 1) {
|
||||
return $boxes[0];
|
||||
}
|
||||
|
||||
$shortestDistance = $boxes[0]->GetData('distance');
|
||||
$shortestDistanceKey = 0;
|
||||
|
||||
// Prüfen ob Entfernung gesetzt sind
|
||||
if ($shortestDistance === null) {
|
||||
throw new RuntimeException('Distance not set.');
|
||||
}
|
||||
|
||||
// Box mit der geringsten Entfernung suchen
|
||||
/** @var BoundingBox $box */
|
||||
foreach ($boxes as $key => $box) {
|
||||
$boxDistance = $box->GetData('distance');
|
||||
if ($boxDistance < $shortestDistance) {
|
||||
$shortestDistance = $boxDistance;
|
||||
$shortestDistanceKey = $key;
|
||||
}
|
||||
}
|
||||
|
||||
return $boxes[$shortestDistanceKey];
|
||||
}
|
||||
|
||||
/**
|
||||
* Box mit der kürzesten Entfernung links vom übergebenen Punkt zurückgeben
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return BoundingBox|false
|
||||
*/
|
||||
public function GetNearestBoxLeftFromPoint($x, $y)
|
||||
{
|
||||
// Erstmal alle Elemente rechts vom Punkt finden; inkl. Entfernung zum Punkt
|
||||
$candidates = $this->GetBoxesLeftFromPoint($x, $y);
|
||||
|
||||
return $this->GetBoxWithLowestDistance($candidates);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alle Boxen zurückgeben die sich links vom übergebenen Punkt befinden; inkl. Entfernung
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return array|BoundingBox[]
|
||||
*/
|
||||
public function GetBoxesLeftFromPoint($x, $y)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($this->boxes as $box) {
|
||||
if ($box->IsLeftFromPoint($x, $y)) {
|
||||
$distance = $box->GetDistanceFromPoint($x, $y);
|
||||
$box->SetData('distance', $distance);
|
||||
$result[] = $box;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Box mit der kürzesten Entfernung oberhalb vom übergebenen Punkt zurückgeben
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return BoundingBox|false
|
||||
*/
|
||||
public function GetNearestBoxAboveFromPoint($x, $y)
|
||||
{
|
||||
$candidates = $this->GetBoxesAboveFromPoint($x, $y);
|
||||
|
||||
return $this->GetBoxWithLowestDistance($candidates);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alle Boxen zurückgeben die sich oberhalb vom übergebenen Punkt befinden; inkl. Entfernung
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return array|BoundingBox[]
|
||||
*/
|
||||
public function GetBoxesAboveFromPoint($x, $y)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($this->boxes as $box) {
|
||||
if ($box->IsAbovePoint($x, $y)) {
|
||||
$distance = $box->GetDistanceFromPoint($x, $y);
|
||||
$box->SetData('distance', $distance);
|
||||
$result[] = $box;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Box mit der kürzesten Entfernung unterhalb vom übergebenen Punkt zurückgeben
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return BoundingBox|false
|
||||
*/
|
||||
public function GetNearestBoxBelowFromPoint($x, $y)
|
||||
{
|
||||
$candidates = $this->GetBoxesBelowFromPoint($x, $y);
|
||||
|
||||
return $this->GetBoxWithLowestDistance($candidates);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alle Boxen zurückgeben die sich unterhalb vom übergebenen Punkt befinden; inkl. Entfernung
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return array|BoundingBox[]
|
||||
*/
|
||||
public function GetBoxesBelowFromPoint($x, $y)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($this->boxes as $box) {
|
||||
if ($box->IsBelowPoint($x, $y)) {
|
||||
$distance = $box->GetDistanceFromPoint($x, $y);
|
||||
$box->SetData('distance', $distance);
|
||||
$result[] = $box;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user