-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCBoundaryMarker.cpp
183 lines (132 loc) · 3.12 KB
/
CBoundaryMarker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// CBoundaryMarker.cpp
//
// CBoundaryMarker class
#include "PreComp.h"
static CObjectClass<CBoundaryMarker>g_BoundaryMarkerClass(OBJID_CBOUNDARYMARKER, NULL);
CBoundaryMarker::CBoundaryMarker (void) : CSpaceObject(&g_BoundaryMarkerClass)
// CBoundaryMarker constructor
{
}
void CBoundaryMarker::AddSegment (const CVector &vEndpoint)
// AddSegment
//
// Adds a segment
{
m_Path.Insert(vEndpoint);
}
void CBoundaryMarker::CloseBoundary (void)
// CloseBoundary
//
// Closes the path
{
}
ALERROR CBoundaryMarker::Create (CSystem *pSystem,
const CVector &vStartPos,
CBoundaryMarker **retpMarker)
// Create
//
// Create a boundary marker
{
ALERROR error;
CBoundaryMarker *pMarker;
pMarker = new CBoundaryMarker;
if (pMarker == NULL)
return ERR_MEMORY;
pMarker->Place(vStartPos);
pMarker->SetCannotBeHit();
// Initialize
pMarker->m_Path.Insert(vStartPos);
// Add to system
if (error = pMarker->AddToSystem(pSystem))
{
delete pMarker;
return error;
}
// Done
if (retpMarker)
*retpMarker = pMarker;
return NOERROR;
}
bool CBoundaryMarker::FindIntersectSegment (const CVector &vStart, const CVector &vEnd, CVector *retvSegInt, CVector *retvSegEnd, int *retiSeg)
// FindIntersectSegment
//
// Sees if the given line intersects any of the segments in the boundary.
{
int i;
int iCount = m_Path.GetCount();
if (iCount < 2)
return false;
// Find the line with the closest intersection
Metric rNearestIntersect = 1.0;
int iNearestIntersect = -1;
CVector vStartD = m_Path[0];
for (i = 0; i < m_Path.GetCount(); i++)
{
CVector vEndD = m_Path[(i + 1) % iCount];
Metric rPos;
if (IntersectLine(vStart, vEnd, vStartD, vEndD, NULL, &rPos)
&& rPos < rNearestIntersect)
{
iNearestIntersect = i;
rNearestIntersect = rPos;
}
}
// Return
if (iNearestIntersect != -1)
{
if (retvSegInt)
*retvSegInt = vStart + (rNearestIntersect * (vEnd - vStart));
if (retvSegEnd)
*retvSegEnd = m_Path[(iNearestIntersect + 1) % iCount];
if (retiSeg)
*retiSeg = iNearestIntersect;
return true;
}
else
return false;
}
void CBoundaryMarker::GetSegment (int iSeg, CVector *retvStart, CVector *retvEnd)
// GetSegment
//
// Returns the given segment
{
int iCount = m_Path.GetCount();
if (iCount == 0)
return;
if (retvStart)
*retvStart = m_Path[iSeg % iCount];
if (retvEnd)
*retvEnd = m_Path[(iSeg + 1) % iCount];
}
void CBoundaryMarker::OnReadFromStream (SLoadCtx &Ctx)
// OnReadFromStream
//
// Read the object's data from a stream
//
// DWORD Number of vectors
// CVector path
{
int i;
DWORD dwLoad;
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
for (i = 0; i < (int)dwLoad; i++)
{
CVector vPos;
Ctx.pStream->Read((char *)&vPos, sizeof(CVector));
m_Path.Insert(vPos);
}
}
void CBoundaryMarker::OnWriteToStream (IWriteStream *pStream)
// OnWriteToStream
//
// Write the object's data to a stream
//
// DWORD Number of vectors
// CVector path
{
int i;
DWORD dwSave = m_Path.GetCount();
pStream->Write((char *)&dwSave, sizeof(DWORD));
for (i = 0; i < m_Path.GetCount(); i++)
pStream->Write((char *)&m_Path[i], sizeof(CVector));
}