1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 // -------------------------------------------------------------------------
4 // File name: PolygonClipContext.cpp
7 // -------------------------------------------------------------------------
10 ////////////////////////////////////////////////////////////////////////////
13 #include "PolygonClipContext.h"
15 CPolygonClipContext::CPolygonClipContext()
19 void CPolygonClipContext::Reset()
21 stl::free_container(m_lstPolygonA
);
22 stl::free_container(m_lstPolygonB
);
25 int CPolygonClipContext::ClipEdge(const Vec3
& v1
, const Vec3
& v2
, const Plane
& ClipPlane
, Vec3
& vRes1
, Vec3
& vRes2
)
27 float d1
= -ClipPlane
.DistFromPlane(v1
);
28 float d2
= -ClipPlane
.DistFromPlane(v2
);
30 return 0; // all clipped = do not add any vertices
32 if (d1
>= 0 && d2
>= 0)
35 return 1; // both not clipped - add second vertex
38 // calculate new vertex
39 Vec3 vIntersectionPoint
= v1
+ (v2
- v1
) * (abs(d1
) / (abs(d2
) + abs(d1
)));
42 float fNewDist
= -ClipPlane
.DistFromPlane(vIntersectionPoint
);
43 assert(abs(fNewDist
) < 0.01f
);
46 if (d1
>= 0 && d2
< 0)
49 vRes1
= vIntersectionPoint
;
52 else if (d1
< 0 && d2
>= 0)
54 // from not vis to vis
55 vRes1
= vIntersectionPoint
;
64 void CPolygonClipContext::ClipPolygon(PodArray
<Vec3
>& PolygonOut
, const PodArray
<Vec3
>& pPolygon
, const Plane
& ClipPlane
)
67 // clip edges, make list of new vertices
68 for (int i
= 0; i
< pPolygon
.Count(); i
++)
70 Vec3
vNewVert1(0, 0, 0), vNewVert2(0, 0, 0);
71 if (int nNewVertNum
= ClipEdge(pPolygon
.GetAt(i
), pPolygon
.GetAt((i
+ 1) % pPolygon
.Count()), ClipPlane
, vNewVert1
, vNewVert2
))
73 PolygonOut
.Add(vNewVert1
);
75 PolygonOut
.Add(vNewVert2
);
80 #if defined(USE_CRY_ASSERT)
81 for (int i
= 0; i
< PolygonOut
.Count(); i
++)
83 float d1
= -ClipPlane
.DistFromPlane(PolygonOut
.GetAt(i
));
88 assert(PolygonOut
.Count() == 0 || PolygonOut
.Count() >= 3);
91 const PodArray
<Vec3
>& CPolygonClipContext::Clip(const PodArray
<Vec3
>& poly
, const Plane
* planes
, size_t numPlanes
)
93 m_lstPolygonA
.Clear();
94 m_lstPolygonB
.Clear();
96 m_lstPolygonA
.AddList(poly
);
98 PodArray
<Vec3
>* src
= &m_lstPolygonA
, * dst
= &m_lstPolygonB
;
100 for (size_t i
= 0; i
< numPlanes
&& src
->Count() >= 3; std::swap(src
, dst
), i
++)
101 ClipPolygon(*dst
, *src
, planes
[i
]);
106 const PodArray
<Vec3
>& CPolygonClipContext::Clip(const Vec3
& a
, const Vec3
& b
, const Vec3
& c
, const Plane
* planes
, size_t numPlanes
)
108 m_lstPolygonA
.Clear();
109 m_lstPolygonB
.Clear();
111 m_lstPolygonA
.Add(a
);
112 m_lstPolygonA
.Add(b
);
113 m_lstPolygonA
.Add(c
);
115 PodArray
<Vec3
>* src
= &m_lstPolygonA
, * dst
= &m_lstPolygonB
;
117 for (size_t i
= 0; i
< numPlanes
&& src
->Count() >= 3; std::swap(src
, dst
), i
++)
118 ClipPolygon(*dst
, *src
, planes
[i
]);
123 void CPolygonClipContext::GetMemoryUsage(ICrySizer
* pSizer
) const
125 pSizer
->AddObject(m_lstPolygonA
);
126 pSizer
->AddObject(m_lstPolygonB
);