123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- using Island.StandardLib.Storage;
- using System;
- namespace Island.StandardLib.Math
- {
- public struct Cube3f : IStorable
- {
- float _XStart, _XEnd, _YStart, _YEnd, _ZStart, _ZEnd;
- public float XStart
- {
- get
- {
- return _XStart;
- }
- set
- {
- _XStart = value;
- if (_XEnd < _XStart)
- {
- float f = _XEnd;
- _XEnd = _XStart;
- _XStart = f;
- }
- }
- }
- public float YStart
- {
- get
- {
- return _YStart;
- }
- set
- {
- _YStart = value;
- if (_YEnd < _YStart)
- {
- float f = _YEnd;
- _YEnd = _YStart;
- _YStart = f;
- }
- }
- }
- public float ZStart
- {
- get
- {
- return _ZStart;
- }
- set
- {
- _ZStart = value;
- if (_ZEnd < _ZStart)
- {
- float f = _ZEnd;
- _ZEnd = _ZStart;
- _ZStart = f;
- }
- }
- }
- public float XEnd
- {
- get
- {
- return _XEnd;
- }
- set
- {
- _XEnd = value;
- if (_XEnd < _XStart)
- {
- float f = _XEnd;
- _XEnd = _XStart;
- _XStart = f;
- }
- }
- }
- public float YEnd
- {
- get
- {
- return _YEnd;
- }
- set
- {
- _YEnd = value;
- if (_YEnd < _YStart)
- {
- float f = _YEnd;
- _YEnd = _YStart;
- _YStart = f;
- }
- }
- }
- public float ZEnd
- {
- get
- {
- return _ZEnd;
- }
- set
- {
- _ZEnd = value;
- if (_ZEnd < _ZStart)
- {
- float f = _ZEnd;
- _ZEnd = _ZStart;
- _ZStart = f;
- }
- }
- }
- /// <summary>
- /// 以完整格式初始化<see cref="Cube3f"/>
- /// </summary>
- /// <param name="xStart">X起始点</param>
- /// <param name="xEnd">X终点</param>
- /// <param name="yStart">Y起始点</param>
- /// <param name="yEnd">Y终点</param>
- /// <param name="zStart">Z起始点</param>
- /// <param name="zEnd">Z终点</param>
- public Cube3f(float xStart, float xEnd, float yStart, float yEnd, float zStart, float zEnd)
- {
- _XStart = xStart;
- _XEnd = xEnd;
- _YStart = yStart;
- _YEnd = yEnd;
- _ZStart = zStart;
- _ZEnd = zEnd;
- Trim();
- }
- /// <summary>
- /// 以三边长度初始化<see cref="Cube3f"/>,并将起始点设为<see cref="Vector3.Zero"/>
- /// </summary>
- /// <param name="xlen">X长度</param>
- /// <param name="ylen">Y长度</param>
- /// <param name="zlen">Z长度</param>
- public Cube3f(float xlen, float ylen, float zlen)
- {
- _XStart = _YStart = _ZStart = 0f;
- _XEnd = xlen;
- _YEnd = ylen;
- _ZEnd = zlen;
- }
- /// <summary>
- /// 以三边长度初始化<see cref="Cube3f"/>,并将起始点设为<see cref="Vector3.Zero"/>
- /// </summary>
- /// <param name="size">三边长度</param>
- public Cube3f(Vector3 size)
- {
- _XStart = _YStart = _ZStart = 0f;
- _XEnd = size.x;
- _YEnd = size.y;
- _ZEnd = size.z;
- }
- void Trim()
- {
- if (_XEnd < _XStart)
- {
- float f = _XEnd;
- _XEnd = _XStart;
- _XStart = f;
- }
- if (_YEnd < _YStart)
- {
- float f = _YEnd;
- _YEnd = _YStart;
- _YStart = f;
- }
- if (_ZEnd < _ZStart)
- {
- float f = _ZEnd;
- _ZEnd = _ZStart;
- _ZStart = f;
- }
- }
- /// <summary>
- /// 获取三边长度
- /// </summary>
- public Vector3 Length
- {
- get
- {
- return new Vector3(_XEnd - _XStart, _YEnd - _YStart, _ZEnd - _ZStart);
- }
- }
- /// <summary>
- /// 获取或设置起始点
- /// </summary>
- public Vector3 Start
- {
- get
- {
- return new Vector3(_XStart, _YStart, _ZStart);
- }
- set
- {
- Vector3 delta = value.RED(Start);
- _XEnd += delta.x;
- _YEnd += delta.y;
- _ZEnd += delta.z;
- _XStart = value.x;
- _YStart = value.y;
- _ZStart = value.z;
- }
- }
- /// <summary>
- /// 获取和设置终点
- /// </summary>
- public Vector3 End
- {
- get
- {
- return new Vector3(_XEnd, _YEnd, _ZEnd);
- }
- set
- {
- Vector3 delta = value.RED(End);
- _XStart += delta.x;
- _YStart += delta.y;
- _ZStart += delta.z;
- _XEnd = value.x;
- _YEnd = value.y;
- _ZEnd = value.z;
- }
- }
- /// <summary>
- /// 获取和设置底面中心点
- /// </summary>
- public Vector3 BottomCenter
- {
- get
- {
- Vector3 hfsize = Length.MUL(0.5f);
- return new Vector3(_XStart + hfsize.x, _YStart, _ZStart + hfsize.z);
- }
- set
- {
- Vector3 hfsize = Length.MUL(0.5f);
- hfsize.y = 0;
- Start = value.RED(hfsize);
- }
- }
- /// <summary>
- /// 测试此<see cref="Cube3f"/>是否包含指定的点
- /// </summary>
- /// <param name="pt"></param>
- /// <returns></returns>
- public bool ContainPoint(Vector3 pt)
- {
- return pt.x >= _XStart && pt.x <= _XEnd && pt.y >= _YStart && pt.y <= _YEnd && pt.z >= _ZStart && pt.z <= _ZEnd;
- }
- /// <summary>
- /// 测试此<see cref="Cube3f"/>是否与指定的<see cref="Cube3f"/>相交
- /// </summary>
- /// <param name="cb"></param>
- /// <returns></returns>
- public bool ContainCube(Cube3f cb)
- {
- if (cb.XStart > XEnd)
- return false;
- if (cb.XEnd < XStart)
- return false;
- if (cb.YStart > YEnd)
- return false;
- if (cb.YEnd < YStart)
- return false;
- if (cb.ZStart > ZEnd)
- return false;
- if (cb.ZEnd < ZStart)
- return false;
- return true;
- }
- public void WriteToData(DataStorage data)
- {
- data.Write(_XStart);
- data.Write(_XEnd);
- data.Write(_YStart);
- data.Write(_YEnd);
- data.Write(_ZStart);
- data.Write(_ZEnd);
- }
- public void ReadFromData(DataStorage data)
- {
- data.Read(out _XStart);
- data.Read(out _XEnd);
- data.Read(out _YStart);
- data.Read(out _YEnd);
- data.Read(out _ZStart);
- data.Read(out _ZEnd);
- }
- public static Cube3f operator +(Cube3f cube, Vector3 vec)
- {
- return new Cube3f(
- cube.XStart + vec.x, cube.XEnd + vec.x,
- cube.YStart + vec.y, cube.YEnd + vec.y,
- cube.ZStart + vec.z, cube.ZEnd + vec.z);
- }
- public static Cube3f operator -(Cube3f cube, Vector3 vec)
- {
- return new Cube3f(
- cube.XStart - vec.x, cube.XEnd - vec.x,
- cube.YStart - vec.y, cube.YEnd - vec.y,
- cube.ZStart - vec.z, cube.ZEnd - vec.z);
- }
- }
- }
|