MyGUI: MyGUI_GeometryUtility.cpp Source File

MyGUI 3.4.3

MyGUI_GeometryUtility.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_GeometryUtility.h"
9
10namespace MyGUI::geometry_utility
11{
12
13 VectorFloatPoint cropPolygon(FloatPoint* _baseVerticiesPos, size_t _size, const IntCoord& _cropRectangle)
14 {
15 VectorFloatPoint resultVerticiesPos;
16 resultVerticiesPos.resize(_size);
17 for (size_t i = 0; i < _size; ++i)
18 {
19 resultVerticiesPos[i] = _baseVerticiesPos[i];
20 }
21
22 cropPolygonSide(resultVerticiesPos, _cropRectangle.left, Left);
23 cropPolygonSide(resultVerticiesPos, _cropRectangle.right(), Right);
24 cropPolygonSide(resultVerticiesPos, _cropRectangle.top, Top);
25 cropPolygonSide(resultVerticiesPos, _cropRectangle.bottom(), Bottom);
26
27 return resultVerticiesPos;
28 }
29
30 void cropPolygonSide(VectorFloatPoint& _verticies, int _sideCoord, Side _side)
31 {
32 VectorFloatPoint newVerticies;
33 int invert = (_side == Right || _side == Bottom) ? -1 : 1;
34 for (size_t i = 0; i < _verticies.size(); ++i)
35 {
36 FloatPoint& v0 = _verticies[i];
37 FloatPoint& v1 = _verticies[(i + 1) % _verticies.size()];
38 switch (_side)
39 {
40 case Left:
41 case Right:
42 // both inside
43 if (invert * v0.left >= invert * _sideCoord && invert * v1.left >= invert * _sideCoord)
44 newVerticies.push_back(v0);
45 // intersect side (1st vertex in)
46 else if (invert * v0.left >= invert * _sideCoord && invert * v1.left < invert * _sideCoord)
47 {
48 newVerticies.push_back(v0);
49 float c = (v0.left - _sideCoord) / (_sideCoord - v1.left);
50 newVerticies.emplace_back((float)_sideCoord, (v0.top + c * v1.top) / (c + 1));
51 }
52 // intersect side (2nd vertex in)
53 else if (invert * v0.left <= invert * _sideCoord && invert * v1.left > invert * _sideCoord)
54 {
55 float c = (v0.left - _sideCoord) / (_sideCoord - v1.left);
56 newVerticies.emplace_back((float)_sideCoord, (v0.top + c * v1.top) / (c + 1));
57 }
58 // else don't add any verticies
59 break;
60 case Top:
61 case Bottom:
62 // both inside
63 if (invert * v0.top >= invert * _sideCoord && invert * v1.top >= invert * _sideCoord)
64 newVerticies.push_back(v0);
65 // intersect side (1st vertex in)
66 else if (invert * v0.top >= invert * _sideCoord && invert * v1.top < invert * _sideCoord)
67 {
68 newVerticies.push_back(v0);
69 float c = (v0.top - _sideCoord) / (_sideCoord - v1.top);
70 newVerticies.emplace_back((v0.left + c * v1.left) / (c + 1), (float)_sideCoord);
71 }
72 // intersect side (2nd vertex in)
73 else if (invert * v0.top <= invert * _sideCoord && invert * v1.top > invert * _sideCoord)
74 {
75 float c = (v0.top - _sideCoord) / (_sideCoord - v1.top);
76 newVerticies.emplace_back((v0.left + c * v1.left) / (c + 1), (float)_sideCoord);
77 }
78 // else don't add any verticies
79 break;
80 }
81 }
82
83 _verticies = newVerticies;
84 }
85
86 FloatPoint getPositionInsideRect(
87 const FloatPoint& _point,
88 const FloatPoint& _corner0,
89 const FloatPoint& _corner1,
90 const FloatPoint& _corner2)
91 {
92 FloatPoint point = _point - _corner0;
93 FloatPoint dirX = _corner1 - _corner0;
94 FloatPoint dirY = _corner2 - _corner0;
95
96 float div = dirX.left * dirY.top - dirX.top * dirY.left;
97 if (div == 0.0f)
98 return {};
99 return {
100 (point.top * dirX.left - point.left * dirX.top) / div,
101 (point.left * dirY.top - point.top * dirY.left) / div};
102 }
103
104 FloatPoint getUVFromPositionInsideRect(
105 const FloatPoint& _point,
106 const FloatPoint& _v0,
107 const FloatPoint& _v1,
108 const FloatPoint& _baseUV)
109 {
110 return {
111 _baseUV.left + _point.left * _v0.left + _point.top * _v1.left,
112 _baseUV.top + _point.left * _v0.top + _point.top * _v1.top};
113 }
114
115} // namespace MyGUI
MyGUI_GeometryUtility.h
MyGUI_Precompiled.h
MyGUI::geometry_utility
Definition MyGUI_GeometryUtility.h:19
MyGUI::geometry_utility::cropPolygonSide
void cropPolygonSide(VectorFloatPoint &_verticies, int _sideCoord, Side _side)
Definition MyGUI_GeometryUtility.cpp:30
MyGUI::geometry_utility::getUVFromPositionInsideRect
FloatPoint getUVFromPositionInsideRect(const FloatPoint &_point, const FloatPoint &_v0, const FloatPoint &_v1, const FloatPoint &_baseUV)
Definition MyGUI_GeometryUtility.cpp:104
MyGUI::geometry_utility::getPositionInsideRect
FloatPoint getPositionInsideRect(const FloatPoint &_point, const FloatPoint &_corner0, const FloatPoint &_corner1, const FloatPoint &_corner2)
Definition MyGUI_GeometryUtility.cpp:86
MyGUI::geometry_utility::Side
Side
Definition MyGUI_GeometryUtility.h:23
MyGUI::geometry_utility::Top
@ Top
Definition MyGUI_GeometryUtility.h:26
MyGUI::geometry_utility::Bottom
@ Bottom
Definition MyGUI_GeometryUtility.h:27
MyGUI::geometry_utility::Left
@ Left
Definition MyGUI_GeometryUtility.h:24
MyGUI::geometry_utility::Right
@ Right
Definition MyGUI_GeometryUtility.h:25
MyGUI::geometry_utility::cropPolygon
VectorFloatPoint cropPolygon(FloatPoint *_baseVerticiesPos, size_t _size, const IntCoord &_cropRectangle)
Definition MyGUI_GeometryUtility.cpp:13
MyGUI::FloatPoint
types::TPoint< float > FloatPoint
Definition MyGUI_Types.h:28
MyGUI::VectorFloatPoint
std::vector< FloatPoint > VectorFloatPoint
Definition MyGUI_GeometryUtility.h:16
MyGUI::IntCoord
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:36
MyGUI::types::TCoord::right
T right() const
Definition MyGUI_TCoord.h:125
MyGUI::types::TCoord::top
T top
Definition MyGUI_TCoord.h:21
MyGUI::types::TCoord::left
T left
Definition MyGUI_TCoord.h:20
MyGUI::types::TCoord::bottom
T bottom() const
Definition MyGUI_TCoord.h:130
MyGUI::types::TPoint::left
T left
Definition MyGUI_TPoint.h:19
MyGUI::types::TPoint::top
T top
Definition MyGUI_TPoint.h:20