!I integrate from //ce/main...
[CRYENGINE.git] / Code / CryEngine / Cry3DEngine / PolygonClipContext.cpp
blobf35e7373368a607292d0f4263fdc202803d9ed62
1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 // -------------------------------------------------------------------------
4 // File name: PolygonClipContext.cpp
5 // Created:
6 // Description:
7 // -------------------------------------------------------------------------
8 // History:
9 //
10 ////////////////////////////////////////////////////////////////////////////
12 #include "StdAfx.h"
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);
29 if (d1 < 0 && d2 < 0)
30 return 0; // all clipped = do not add any vertices
32 if (d1 >= 0 && d2 >= 0)
34 vRes1 = v2;
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)));
41 #ifdef _DEBUG
42 float fNewDist = -ClipPlane.DistFromPlane(vIntersectionPoint);
43 assert(abs(fNewDist) < 0.01f);
44 #endif
46 if (d1 >= 0 && d2 < 0)
48 // from vis to no vis
49 vRes1 = vIntersectionPoint;
50 return 1;
52 else if (d1 < 0 && d2 >= 0)
54 // from not vis to vis
55 vRes1 = vIntersectionPoint;
56 vRes2 = v2;
57 return 2;
60 assert(0);
61 return 0;
64 void CPolygonClipContext::ClipPolygon(PodArray<Vec3>& PolygonOut, const PodArray<Vec3>& pPolygon, const Plane& ClipPlane)
66 PolygonOut.Clear();
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);
74 if (nNewVertNum > 1)
75 PolygonOut.Add(vNewVert2);
79 // check result
80 #if defined(USE_CRY_ASSERT)
81 for (int i = 0; i < PolygonOut.Count(); i++)
83 float d1 = -ClipPlane.DistFromPlane(PolygonOut.GetAt(i));
84 assert(d1 >= -0.01f);
86 #endif
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]);
103 return *src;
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]);
120 return *src;
123 void CPolygonClipContext::GetMemoryUsage(ICrySizer* pSizer) const
125 pSizer->AddObject(m_lstPolygonA);
126 pSizer->AddObject(m_lstPolygonB);