Skip to content

Commit

Permalink
增加获取直系子节点和获取父节点计算边界框的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
SimingLiu committed Dec 8, 2024
1 parent 991b79d commit 96a2789
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
**/.idea/
.local
27 changes: 25 additions & 2 deletions geom.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2012 Daniel Connelly. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// 为了简单表述,下面注释中把所有包络框或者叫边界框,叫做最小外接矩形边框

package rtreego

Expand Down Expand Up @@ -142,7 +143,8 @@ func (p Point) minMaxDist(r Rect) float64 {
// Rect represents a subset of n-dimensional Euclidean space of the form
// [a1, b1] x [a2, b2] x ... x [an, bn], where ai < bi for all 1 <= i <= n.
type Rect struct {
p, q Point // Enforced by NewRect: p[i] <= q[i] for all i.
p, q Point // Enforced by NewRect: p[i] <= q[i] for all i.
parent *node
}

// PointCoord returns the coordinate of the point of the rectangle at i
Expand Down Expand Up @@ -181,6 +183,23 @@ func (r Rect) String() string {
return strings.Join(s, "x")
}

func (r Rect) SetParent(parent *node) Spatial {
r.parent = parent
return r
}

func (r Rect) GetParent() *node {
return r.parent
}

func (r Rect) Bounds() Rect {
return r
}

func (r Rect) StartEnd() []Point {
return []Point{r.p, r.q}
}

// NewRect constructs and returns a pointer to a Rect given a corner point and
// the lengths of each dimension. The point p should be the most-negative point
// on the rectangle (in every dimension) and every length should be positive.
Expand Down Expand Up @@ -227,6 +246,8 @@ func NewRectFromPoints(minPoint, maxPoint Point) (r Rect, err error) {
}

// Size computes the measure of a rectangle (the product of its side lengths).
//
// 返回矩形的测度如面积/体积等。
func (r Rect) Size() float32 {
size := float32(1.0)
for i, a := range r.p {
Expand Down Expand Up @@ -345,10 +366,12 @@ func (p Point) ToRect(tol float32) Rect {
a[i] = p[i] - tol
b[i] = p[i] + tol
}
return Rect{a, b}
return Rect{a, b, nil}
}

// boundingBox constructs the smallest rectangle containing both r1 and r2.
//
// 构建出同时包含 r1 和 r2 的最小矩形。
func boundingBox(r1, r2 Rect) (bb Rect) {
dim := len(r1.p)
bb.p = make([]float32, dim)
Expand Down
4 changes: 2 additions & 2 deletions geom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func TestMinDistZero(t *testing.T) {

func TestMinDistPositive(t *testing.T) {
p := Point{1, 2, 3}
r := Rect{Point{-1, -4, 7}, Point{2, -2, 9}}
r := Rect{Point{-1, -4, 7}, Point{2, -2, 9}, nil}
expected := float32((-2-2)*(-2-2) + (7-3)*(7-3))
if d := p.minDist(r); math.Abs(d-float64(expected)) > EPS {
t.Errorf("Expected %v.minDist(%v) == %v, got %v", p, r, expected, d)
Expand All @@ -354,7 +354,7 @@ func TestMinDistPositive(t *testing.T) {

func TestMinMaxdist(t *testing.T) {
p := Point{-3, -2, -1}
r := Rect{Point{0, 0, 0}, Point{1, 2, 3}}
r := Rect{Point{0, 0, 0}, Point{1, 2, 3}, nil}

// furthest points from p on the faces closest to p in each dimension
q1 := Point{0, 2, 3}
Expand Down
Loading

0 comments on commit 96a2789

Please sign in to comment.