mirror of
https://github.com/tengge1/ShadowEditor.git
synced 2026-01-25 15:08:11 +00:00
box2
This commit is contained in:
parent
a2ccf3e145
commit
3bb8ab65fa
@ -1,5 +1,3 @@
|
||||
// +build ignore
|
||||
|
||||
// Copyright 2017-2020 The ShadowEditor Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
@ -12,232 +10,184 @@
|
||||
|
||||
package three
|
||||
|
||||
/**
|
||||
* @author bhouston / http://clara.io
|
||||
*/
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
var _vector = Vector2{}
|
||||
|
||||
function Box2( min, max ) {
|
||||
|
||||
this.min = ( min !== undefined ) ? min : new Vector2( + Infinity, + Infinity );
|
||||
this.max = ( max !== undefined ) ? max : new Vector2( - Infinity, - Infinity );
|
||||
var _vector = NewVector2(0, 0)
|
||||
|
||||
// NewBox2 :
|
||||
func NewBox2(min, max Vector2) *Box2 {
|
||||
return &Box2{min, max}
|
||||
}
|
||||
|
||||
Object.assign( Box2.prototype, {
|
||||
|
||||
set: function ( min, max ) {
|
||||
|
||||
this.min.copy( min );
|
||||
this.max.copy( max );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setFromPoints: function ( points ) {
|
||||
|
||||
this.makeEmpty();
|
||||
|
||||
for ( var i = 0, il = points.length; i < il; i ++ ) {
|
||||
|
||||
this.expandByPoint( points[ i ] );
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
setFromCenterAndSize: function ( center, size ) {
|
||||
|
||||
var halfSize = _vector.copy( size ).multiplyScalar( 0.5 );
|
||||
this.min.copy( center ).sub( halfSize );
|
||||
this.max.copy( center ).add( halfSize );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
clone: function () {
|
||||
|
||||
return new this.constructor().copy( this );
|
||||
|
||||
},
|
||||
|
||||
copy: function ( box ) {
|
||||
|
||||
this.min.copy( box.min );
|
||||
this.max.copy( box.max );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
makeEmpty: function () {
|
||||
|
||||
this.min.x = this.min.y = + Infinity;
|
||||
this.max.x = this.max.y = - Infinity;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
isEmpty: function () {
|
||||
|
||||
// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
|
||||
|
||||
return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
|
||||
|
||||
},
|
||||
|
||||
getCenter: function ( target ) {
|
||||
|
||||
if ( target === undefined ) {
|
||||
|
||||
console.warn( 'THREE.Box2: .getCenter() target is now required' );
|
||||
target = new Vector2();
|
||||
|
||||
}
|
||||
|
||||
return this.isEmpty() ? target.set( 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
|
||||
|
||||
},
|
||||
|
||||
getSize: function ( target ) {
|
||||
|
||||
if ( target === undefined ) {
|
||||
|
||||
console.warn( 'THREE.Box2: .getSize() target is now required' );
|
||||
target = new Vector2();
|
||||
|
||||
}
|
||||
|
||||
return this.isEmpty() ? target.set( 0, 0 ) : target.subVectors( this.max, this.min );
|
||||
|
||||
},
|
||||
|
||||
expandByPoint: function ( point ) {
|
||||
|
||||
this.min.min( point );
|
||||
this.max.max( point );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
expandByVector: function ( vector ) {
|
||||
|
||||
this.min.sub( vector );
|
||||
this.max.add( vector );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
expandByScalar: function ( scalar ) {
|
||||
|
||||
this.min.addScalar( - scalar );
|
||||
this.max.addScalar( scalar );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
containsPoint: function ( point ) {
|
||||
|
||||
return point.x < this.min.x || point.x > this.max.x ||
|
||||
point.y < this.min.y || point.y > this.max.y ? false : true;
|
||||
|
||||
},
|
||||
|
||||
containsBox: function ( box ) {
|
||||
|
||||
return this.min.x <= box.min.x && box.max.x <= this.max.x &&
|
||||
this.min.y <= box.min.y && box.max.y <= this.max.y;
|
||||
|
||||
},
|
||||
|
||||
getParameter: function ( point, target ) {
|
||||
|
||||
// This can potentially have a divide by zero if the box
|
||||
// has a size dimension of 0.
|
||||
|
||||
if ( target === undefined ) {
|
||||
|
||||
console.warn( 'THREE.Box2: .getParameter() target is now required' );
|
||||
target = new Vector2();
|
||||
|
||||
}
|
||||
|
||||
return target.set(
|
||||
( point.x - this.min.x ) / ( this.max.x - this.min.x ),
|
||||
( point.y - this.min.y ) / ( this.max.y - this.min.y )
|
||||
);
|
||||
|
||||
},
|
||||
|
||||
intersectsBox: function ( box ) {
|
||||
|
||||
// using 4 splitting planes to rule out intersections
|
||||
|
||||
return box.max.x < this.min.x || box.min.x > this.max.x ||
|
||||
box.max.y < this.min.y || box.min.y > this.max.y ? false : true;
|
||||
|
||||
},
|
||||
|
||||
clampPoint: function ( point, target ) {
|
||||
|
||||
if ( target === undefined ) {
|
||||
|
||||
console.warn( 'THREE.Box2: .clampPoint() target is now required' );
|
||||
target = new Vector2();
|
||||
|
||||
}
|
||||
|
||||
return target.copy( point ).clamp( this.min, this.max );
|
||||
|
||||
},
|
||||
|
||||
distanceToPoint: function ( point ) {
|
||||
|
||||
var clampedPoint = _vector.copy( point ).clamp( this.min, this.max );
|
||||
return clampedPoint.sub( point ).length();
|
||||
|
||||
},
|
||||
|
||||
intersect: function ( box ) {
|
||||
|
||||
this.min.max( box.min );
|
||||
this.max.min( box.max );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
union: function ( box ) {
|
||||
|
||||
this.min.min( box.min );
|
||||
this.max.max( box.max );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
translate: function ( offset ) {
|
||||
|
||||
this.min.add( offset );
|
||||
this.max.add( offset );
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
equals: function ( box ) {
|
||||
|
||||
return box.min.equals( this.min ) && box.max.equals( this.max );
|
||||
|
||||
// Box2 :
|
||||
type Box2 struct {
|
||||
Min Vector2
|
||||
Max Vector2
|
||||
}
|
||||
|
||||
// Set :
|
||||
func (b Box2) Set(min, max Vector2) *Box2 {
|
||||
b.Min.Copy(min)
|
||||
b.Max.Copy(max)
|
||||
|
||||
return &b
|
||||
}
|
||||
|
||||
// SetFromPoints :
|
||||
func (b Box2) SetFromPoints(points []Vector2) *Box2 {
|
||||
b.MakeEmpty()
|
||||
|
||||
for i, il := 0, len(points); i < il; i++ {
|
||||
b.ExpandByPoint(points[i])
|
||||
}
|
||||
|
||||
} );
|
||||
return &b
|
||||
}
|
||||
|
||||
// SetFromCenterAndSize :
|
||||
func (b Box2) SetFromCenterAndSize(center, size Vector2) *Box2 {
|
||||
halfSize := _vector.Copy(size).MultiplyScalar(0.5)
|
||||
|
||||
b.Min.Copy(center).Sub(*halfSize)
|
||||
b.Max.Copy(center).Add(*halfSize)
|
||||
|
||||
return &b
|
||||
}
|
||||
|
||||
// Clone :
|
||||
func (b Box2) Clone() *Box2 {
|
||||
return b.Copy(b)
|
||||
}
|
||||
|
||||
// Copy :
|
||||
func (b Box2) Copy(box Box2) *Box2 {
|
||||
b.Min.Copy(box.Min)
|
||||
b.Max.Copy(box.Max)
|
||||
|
||||
return &b
|
||||
}
|
||||
|
||||
// MakeEmpty :
|
||||
func (b Box2) MakeEmpty() *Box2 {
|
||||
b.Min.X, b.Min.Y = math.Inf(1), math.Inf(1)
|
||||
b.Max.X, b.Max.Y = math.Inf(-1), math.Inf(-1)
|
||||
|
||||
return &b
|
||||
}
|
||||
|
||||
// IsEmpty :
|
||||
func (b Box2) IsEmpty() bool {
|
||||
// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
|
||||
return b.Max.X < b.Min.X || b.Max.Y < b.Min.Y
|
||||
}
|
||||
|
||||
// GetCenter :
|
||||
func (b Box2) GetCenter(target Vector2) *Vector2 {
|
||||
if b.IsEmpty() {
|
||||
return target.Set(0, 0)
|
||||
}
|
||||
return target.AddVectors(b.Min, b.Max).MultiplyScalar(0.5)
|
||||
}
|
||||
|
||||
// GetSize :
|
||||
func (b Box2) GetSize(target Vector2) *Vector2 {
|
||||
if b.IsEmpty() {
|
||||
return target.Set(0, 0)
|
||||
}
|
||||
return target.SubVectors(b.Max, b.Min)
|
||||
}
|
||||
|
||||
// ExpandByPoint :
|
||||
func (b Box2) ExpandByPoint(point Vector2) *Box2 {
|
||||
b.Min.Min(point)
|
||||
b.Max.Max(point)
|
||||
|
||||
return &b
|
||||
}
|
||||
|
||||
// ExpandByVector :
|
||||
func (b Box2) ExpandByVector(vector Vector2) *Box2 {
|
||||
b.Min.Sub(vector)
|
||||
b.Max.Add(vector)
|
||||
|
||||
return &b
|
||||
}
|
||||
|
||||
// ExpandByScalar :
|
||||
func (b Box2) ExpandByScalar(scalar float64) *Box2 {
|
||||
b.Min.AddScalar(-scalar)
|
||||
b.Max.AddScalar(scalar)
|
||||
|
||||
return &b
|
||||
}
|
||||
|
||||
// ContainsPoint :
|
||||
func (b Box2) ContainsPoint(point Vector2) bool {
|
||||
return !(point.X < b.Min.X || point.X > b.Max.X ||
|
||||
point.Y < b.Min.Y || point.Y > b.Max.Y)
|
||||
}
|
||||
|
||||
// ContainsBox :
|
||||
func (b Box2) ContainsBox(box Box2) bool {
|
||||
return b.Min.X <= box.Min.X && box.Max.X <= b.Max.X &&
|
||||
b.Min.Y <= box.Min.Y && box.Max.Y <= b.Max.Y
|
||||
}
|
||||
|
||||
// GetParameter :
|
||||
func (b Box2) GetParameter(point, target Vector2) *Vector2 {
|
||||
// This can potentially have a divide by zero if the box
|
||||
// has a size dimension of 0.
|
||||
return target.Set(
|
||||
(point.X-b.Min.X)/(b.Max.X-b.Min.X),
|
||||
(point.Y-b.Min.Y)/(b.Max.Y-b.Min.Y),
|
||||
)
|
||||
}
|
||||
|
||||
// IntersectsBox :
|
||||
func (b Box2) IntersectsBox(box Box2) bool {
|
||||
// using 4 splitting planes to rule out intersections
|
||||
return !(box.Max.X < b.Min.X || box.Min.X > b.Max.X ||
|
||||
box.Max.Y < b.Min.Y || box.Min.Y > b.Max.Y)
|
||||
}
|
||||
|
||||
// ClampPoint :
|
||||
func (b Box2) ClampPoint(point, target Vector2) *Vector2 {
|
||||
return target.Copy(point).Clamp(b.Min, b.Max)
|
||||
}
|
||||
|
||||
// DistanceToPoint :
|
||||
func (b Box2) DistanceToPoint(point Vector2) float64 {
|
||||
clampedPoint := _vector.Copy(point).Clamp(b.Min, b.Max)
|
||||
return clampedPoint.Sub(point).Length()
|
||||
}
|
||||
|
||||
// Intersect :
|
||||
func (b Box2) Intersect(box Box2) *Box2 {
|
||||
b.Min.Max(box.Min)
|
||||
b.Max.Min(box.Max)
|
||||
|
||||
return &b
|
||||
}
|
||||
|
||||
// Union :
|
||||
func (b Box2) Union(box Box2) *Box2 {
|
||||
b.Min.Min(box.Min)
|
||||
b.Max.Max(box.Max)
|
||||
|
||||
return &b
|
||||
}
|
||||
|
||||
// Translate :
|
||||
func (b Box2) Translate(offset Vector2) *Box2 {
|
||||
b.Min.Add(offset)
|
||||
b.Max.Add(offset)
|
||||
|
||||
return &b
|
||||
}
|
||||
|
||||
// Equals :
|
||||
func (b Box2) Equals(box Box2) bool {
|
||||
return box.Min.Equals(b.Min) && box.Max.Equals(b.Max)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user