Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/CLine.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download) (as text)
Sun Aug 15 01:53:13 2010 UTC (13 years, 9 months ago) by okadu
File MIME type: text/x-c++src
File size: 11974 byte(s)


1 #include "stdafx.h"
2 #include "CLine.h"
3 #include "CScene.h"
4 #include "CLinePlugin.h"
5 #include "CPolePlugin.h"
6
7 // 外部グローバル
8 extern bool g_ShowPoleSelect;
9 extern bool g_ShowLineSelect;
10 extern MAT8 g_MatSelect[];
11 extern MAT8 g_MatSelectA[];
12
13 /*
14 * コンストラクタ
15 */
16 CPoleLink::CPoleLink(
17 int side, // サイド
18 int track, // 軌道番号
19 CPole *pole // 接続架線柱
20 ){
21 m_Side = side;
22 m_Track = track;
23 m_Link = pole;
24 }
25
26 /*
27 * 接続点描画
28 */
29 void CPoleLink::Render(
30 D3DCOLOR color // 線色
31 ){
32 Draw3DPointAs2DRect(GetPos(), color, 5);
33 }
34
35 /*
36 * アドレス復元
37 */
38 void CPoleLink::RestoreAddress(){
39 m_Link = (CPole *)ReplaceAdr(m_Link);
40 }
41
42 /*
43 * 読込
44 */
45 char *CPoleLink::Read(
46 char *str, // 対象文字列
47 char *pref // プレフィックス
48 ){
49 char *eee;
50 if(!(str = Assignment(eee = str, pref))) throw CSynErr(eee);
51 if(!(str = ConstInteger(eee = str, &m_Side))) throw CSynErr(eee);
52 if(!(str = Character2(eee = str, ','))) throw CSynErr(eee);
53 if(!(str = ConstInteger(eee = str, &m_Track))) throw CSynErr(eee);
54 if(!(str = Character2(eee = str, ','))) throw CSynErr(eee);
55 if(!(str = HexPointer(eee = str, (void **)&m_Link))) throw CSynErr(eee);
56 if(!(str = Character2(eee = str, ';'))) throw CSynErr(eee);
57 return str;
58 }
59
60 /*
61 * 保存
62 */
63 void CPoleLink::Save(
64 FILE *df, // ファイル
65 char *pref // プレフィックス
66 ){
67 fprintf(df, "%s%d, %d, %p;\n", pref, m_Side, m_Track, m_Link);
68 }
69
70 ////////////////////////////////////////////////////////////////////////////////
71 ////////////////////////////////////////////////////////////////////////////////
72
73 // static メンバ
74 CPole **CPole::ms_Root = NULL;
75 float CPole::ms_MinDist;
76 CPoleLink CPole::ms_Detect;
77
78 /*
79 * [static]
80 * 接続点を表示
81 */
82 bool CPole::RenderLink(){
83 if(ms_MinDist<0.0f) return false;
84 devSetTexture(0, NULL);
85 devSetLighting(FALSE);
86 devResetMatrix();
87 ms_Detect.Render(0xff00ffff);
88 return true;
89 }
90
91 /*
92 * コンストラクタ (読込用)
93 */
94 CPole::CPole(){
95 m_Selected = 0;
96 m_PolePlugin = NULL;
97 m_Next = NULL;
98 }
99
100 /*
101 * コンストラクタ
102 */
103 CPole::CPole(
104 const VEC3 &pos, // 座標
105 const VEC3 &dir, // 方向
106 CPolePlugin *pp // 架線柱プラグイン
107 ){
108 m_Selected = 0;
109 m_Pos = pos;
110 m_OrigDir = dir;
111 m_Up = V3UP;
112 m_Dir = VEC3(m_OrigDir.x, 0.0f, m_OrigDir.z);
113 V3NormAxis(&m_Right, &m_Up, &m_Dir);
114 if(m_PolePlugin = pp){
115 m_Object.SetMesh(m_PolePlugin->m_Mesh, m_Pos, m_PolePlugin->m_ModelScale);
116 m_Object.SetDir(m_Dir, m_Up);
117 }else{
118 m_Object.SetMesh(NULL, m_Pos, 1.0f);
119 m_Object.SetDir(m_Dir, m_Up);
120 }
121 m_Next = *ms_Root;
122 *ms_Root = this;
123 }
124
125 /*
126 * デストラクタ
127 */
128 CPole::~CPole(){
129 DELETE_V(m_Next);
130 }
131
132 /*
133 * リストから削除
134 */
135 void CPole::Delete(
136 CScene *scene // シーン
137 ){
138 while(m_LineList.size()) scene->DeleteLine(*m_LineList.begin());
139 g_Scene->DeletePoleLink(this);
140 m_Next = NULL;
141 delete this;
142 }
143
144 /*
145 * 支持点座標取得
146 */
147 VEC3 CPole::GetJointPos(
148 int track // 軌道番号
149 ){
150 return m_PolePlugin ? m_Pos+m_Right*(m_PolePlugin->m_TrackInterval
151 *(track-0.5f*(m_PolePlugin->m_TrackNum-1))) : m_Pos;
152 }
153
154 /*
155 * 入力チェック
156 */
157 void CPole::ScanInput(
158 int mode, // モード
159 VEC3 &rect1, // 領域始点
160 VEC3 &rect2 // 領域終点
161 ){
162 if(mode==1){ // scan pole joint link
163 int i, n = m_PolePlugin ? m_PolePlugin->GetTrackNum() : 1;
164 for(i = 0; i<n; i++){
165 VEC3 p = WorldToScreen(GetJointPos(i));
166 if(p.z<0.0f) continue;
167 float dist = V3Len(&(rect1-p));
168 if(dist<DETECT_2D_MAX && (ms_MinDist<0.0f || dist<ms_MinDist)){
169 ms_MinDist = dist;
170 ms_Detect = CreateLink(0, i);
171 }
172 }
173 return;
174 }
175 VEC3 center = m_Object.GetCenter();
176 VEC3 sc = WorldToScreen(center);
177 if(sc.z<0.0f) return;
178 switch(mode){
179 case 2:
180 if(rect1.x<=sc.x && sc.x<=rect2.x && rect1.y<=sc.y && sc.y<=rect2.y)
181 m_Selected |= 1;
182 else m_Selected &= 2;
183 break;
184 case 3:
185 if(CheckCtrl()){
186 m_Selected &= ~m_Selected<<1;
187 }else{
188 if(CheckShift()) m_Selected |= m_Selected<<1;
189 else m_Selected = m_Selected<<1;
190 }
191 m_Selected &= 2;
192 break;
193 case 4:
194 float dist = V3Len(&(rect1-sc));
195 float th = 500.0f*m_Object.GetRadius()/(V3Len(&(GetVPos()-center))*g_FovRatio);
196 if(th<DETECT_2D_MIN) th = DETECT_2D_MIN;
197 if(dist<th && (ms_MinDist<0.0f || ms_MinDist>dist)){
198 ms_MinDist = dist;
199 ms_Detect = CreateLink(0, 0);
200 }
201 m_Selected &= 2;
202 break;
203 }
204 }
205
206 /*
207 * コンストラクタ
208 */
209 void CPole::Render(){
210 if(!m_PolePlugin) return;
211 m_Object.ResetMatFlag();
212 if(g_ShowPoleSelect && m_Selected) m_Object.RenderSC(&g_MatSelect[m_Selected]);
213 else m_Object.Render();
214 CastShadow(&m_Object);
215 }
216
217 /*
218 * アドレス復元
219 */
220 void CPole::RestoreAddress(){
221 IPLine ipl = m_LineList.begin();
222 for(; ipl!=m_LineList.end(); ipl++) *ipl = (CLine *)ReplaceAdr(*ipl);
223 }
224
225 /*
226 * 読込
227 */
228 char *CPole::Read(
229 char *str // 対象文字列
230 ){
231 char *eee, *tmp;
232 if(!(str = BeginBlock(str, "Pole"))){
233 delete this;
234 return NULL;
235 }
236 void *oldadr;
237 if(!(str = AsgnPointer(eee = str, "Address", &oldadr))) throw CSynErr(eee);
238 g_AddressMap[oldadr] = this;
239 string pid;
240 if(!(str = AsgnString(eee = str, "PolePlugin", &pid))) throw CSynErr(eee);
241 m_PolePlugin = g_PolePluginList->FindPlugin(pid.c_str(), true);
242 if(!(str = AsgnVector3D(eee = str, "Pos", &m_Pos))) throw CSynErr(eee);
243 if(!(str = AsgnVector3D(eee = str, "OrigDir", &m_OrigDir))) throw CSynErr(eee);
244 m_Up = V3UP;
245 m_Dir = VEC3(m_OrigDir.x, 0.0f, m_OrigDir.z);
246 V3NormAxis(&m_Right, &m_Up, &m_Dir);
247 if(m_PolePlugin){
248 m_Object.SetMesh(m_PolePlugin->m_Mesh, m_Pos, m_PolePlugin->m_ModelScale);
249 m_Object.SetDir(m_Dir, m_Up);
250 }else{
251 m_Object.SetMesh(NULL, m_Pos, 1.0f);
252 m_Object.SetDir(m_Dir, m_Up);
253 }
254 if(tmp = Assignment(str, "LineList")){
255 str = tmp;
256 do{
257 if(m_LineList.size() && !(str = Character2(eee = str, ','))) throw CSynErr(eee);
258 CLine *line;
259 if(!(str = HexPointer(eee = str, (void **)&line))) throw CSynErr(eee);
260 m_LineList.push_back(line);
261 } while(!(tmp = Character2(str, ';')));
262 str = tmp;
263 }
264 if(!(str = EndBlock(eee = str))) throw CSynErr(eee, ERR_ENDBLOCK);
265 *ms_Root = this;
266 ms_Root = &m_Next;
267 return str;
268 }
269
270 /*
271 * 保存
272 */
273 void CPole::Save(
274 FILE *df // ファイル
275 ){
276 fprintf(df, "\t\t\tPole{\n");
277 fprintf(df, "\t\t\t\tAddress = %p;\n", this);
278 fprintf(df, "\t\t\t\tPolePlugin = \"%s\";\n", CheckPluginID(m_PolePlugin));
279 fprintf(df, "\t\t\t\tPos = "); V3Save(df, m_Pos, ";\n");
280 fprintf(df, "\t\t\t\tOrigDir = "); V3Save(df, m_OrigDir, ";\n");
281 if(m_LineList.size()){
282 fprintf(df, "\t\t\t\tLineList = ");
283 IPLine ipl = m_LineList.begin();
284 for(; ipl!=m_LineList.end(); ipl++) fprintf(df,
285 ipl==m_LineList.begin() ? "%p" : ", %p", *ipl);
286 fprintf(df, ";\n");
287 }
288 fprintf(df, "\t\t\t}\n");
289 }
290
291 ////////////////////////////////////////////////////////////////////////////////
292 ////////////////////////////////////////////////////////////////////////////////
293
294 // static メンバ
295 CLine **CLine::ms_Root = NULL;
296 float CLine::ms_MinDist;
297 CLine *CLine::ms_Detect;
298
299 /*
300 * コンストラクタ (読込用)
301 */
302 CLine::CLine(){
303 m_Selected = 0;
304 m_LinePlugin = NULL;
305 m_Next = NULL;
306 }
307
308 /*
309 * コンストラクタ
310 */
311 CLine::CLine(
312 CPoleLink link1, // 始点
313 CPoleLink link2, // 終点
314 CLinePlugin *lp // 架線プラグイン
315 ){
316 m_Selected = 0;
317 m_Link[0] = link1;
318 m_Link[1] = link2;
319 m_Link[0].Connect(this);
320 m_Link[1].Connect(this);
321 m_LinePlugin = lp;
322 VEC3 up = V3UP;
323 m_Dir = link1.GetPos()-link2.GetPos();
324 float len = V3Len(&m_Dir);
325 m_Dir.y = 0.0f;
326 V3NormAxis(&m_Right, &up, &m_Dir);
327 if(m_LinePlugin){
328 m_LinePlugin->CopyMapTemp(m_LineMapV);
329 m_LinePlugin->AddMapTemp(V3Len(&(link1.GetPos()-link2.GetPos())));
330 }
331 m_Next = *ms_Root;
332 *ms_Root = this;
333 }
334
335 /*
336 * デストラクタ
337 */
338 CLine::~CLine(){
339 DELETE_V(m_Next);
340 }
341
342 /*
343 * リストから削除
344 */
345 void CLine::Delete(){
346 m_Link[0].Disconnect(this);
347 m_Link[1].Disconnect(this);
348 m_Next = NULL;
349 delete this;
350 }
351
352 /*
353 * 入力チェック
354 */
355 void CLine::ScanInput(
356 int mode, // モード
357 VEC3 &rect1, // 領域始点
358 VEC3 &rect2 // 領域終点
359 ){
360 VEC3 center = 0.5f*(m_Link[0].GetPos()+m_Link[1].GetPos());
361 VEC3 sc = WorldToScreen(center);
362 if(sc.z<0.0f) return;
363 switch(mode){
364 case 2:
365 if(rect1.x<=sc.x && sc.x<=rect2.x && rect1.y<=sc.y && sc.y<=rect2.y)
366 m_Selected |= 1;
367 else m_Selected &= 2;
368 break;
369 case 3:
370 if(CheckCtrl()){
371 m_Selected &= ~m_Selected<<1;
372 }else{
373 if(CheckShift()) m_Selected |= m_Selected<<1;
374 else m_Selected = m_Selected<<1;
375 }
376 m_Selected &= 2;
377 break;
378 case 4:
379 float dist = V3Len(&(rect1-sc));
380 float th = 250.0f*V3Len(&(m_Link[0].GetPos()-m_Link[1].GetPos()))
381 /(V3Len(&(GetVPos()-center))*g_FovRatio);
382 if(th<DETECT_2D_MIN) th = DETECT_2D_MIN;
383 if(dist<th && (ms_MinDist<0.0f || ms_MinDist>dist)){
384 ms_MinDist = dist;
385 ms_Detect = this;
386 }
387 m_Selected &= 2;
388 break;
389 }
390 }
391
392 /*
393 * 断面をダンプ
394 */
395 void CLine::Dump(){
396 if(!m_LinePlugin) return;
397 m_LinePlugin->SetMapTemp(m_LineMapV);
398 VEC3 p1 = m_Link[0].GetPos()-m_LinePlugin->m_Height*V3UP;
399 VEC3 p2 = m_Link[1].GetPos()-m_LinePlugin->m_Height*V3UP;
400 m_LinePlugin->Dump(
401 p1, R2L(-m_Link[0].GetRight()), R2L(m_Link[0].GetUp()),
402 p1, R2L(-m_Link[0].GetRight()), R2L(m_Link[0].GetUp()),
403 p2, R2L(m_Link[1].GetRight()), R2L(m_Link[1].GetUp()),
404 p2, R2L(m_Link[1].GetRight()), R2L(m_Link[1].GetUp()), V3Len(&(p2-p1)), 4);
405 }
406
407 /*
408 * 等間隔オブジェクトをレンダリング
409 */
410 void CLine::Render(){
411 if(!g_ShadowNeeded && !g_ShowLineSelect
412 && !(m_LinePlugin && m_LinePlugin->HasInterval())) return;
413 VEC3 p1 = m_Link[0].GetPos()-m_LinePlugin->m_Height*V3UP;
414 VEC3 p2 = m_Link[1].GetPos()-m_LinePlugin->m_Height*V3UP;
415 float seglen = V3Len(&(p2-p1));
416 MAT8 *altmat = m_Selected ? &g_MatSelect[m_Selected] : NULL;
417 MAT8 *altmat2 = m_Selected ? &g_MatSelectA[m_Selected] : NULL;
418 if(g_ShowLineSelect && m_Selected){
419 devSetTexture(0, NULL);
420 devSetMaterial(altmat2);
421 devResetMatrix();
422 m_LinePlugin->Dump(p1, m_Right, R2L(V3UP), p1, m_Right, R2L(V3UP),
423 p2, m_Right, R2L(V3UP), p2, m_Right, R2L(V3UP), seglen, 1);
424 }
425 m_LinePlugin->SetMapTemp(m_LineMapV);
426 m_LinePlugin->Render(p1, m_Right, R2L(V3UP), m_Dir, p1, m_Right, R2L(V3UP),
427 p2, m_Right, R2L(V3UP), m_Dir, p2, m_Right, R2L(V3UP), 3, seglen, altmat);
428 }
429
430 /*
431 * アドレス復元
432 */
433 void CLine::RestoreAddress(){
434 m_Link[0].RestoreAddress();
435 m_Link[1].RestoreAddress();
436 }
437
438 /*
439 * 読込
440 */
441 char *CLine::Read(
442 char *str // 対象文字列
443 ){
444 char *eee;
445 if(!(str = BeginBlock(str, "Line"))){
446 delete this;
447 return NULL;
448 }
449 void *oldadr;
450 if(!(str = AsgnPointer(eee = str, "Address", &oldadr))) throw CSynErr(eee);
451 g_AddressMap[oldadr] = this;
452 string pid;
453 if(!(str = AsgnString(eee = str, "LinePlugin", &pid))) throw CSynErr(eee);
454 m_LinePlugin = g_LinePluginList->FindPlugin(pid.c_str(), true);
455 if(!(str = ReadMapVector(eee = str, "LineMapV", m_LineMapV))) throw CSynErr(eee);
456 if(!(str = AsgnVector3D(eee = str, "Right", &m_Right))) throw CSynErr(eee);
457 if(!(str = AsgnVector3D(eee = str, "Dir", &m_Dir))) throw CSynErr(eee);
458 if(!(str = m_Link[0].Read(eee = str, "Link0"))) throw CSynErr(eee);
459 if(!(str = m_Link[1].Read(eee = str, "Link1"))) throw CSynErr(eee);
460 if(!(str = EndBlock(eee = str))) throw CSynErr(eee, ERR_ENDBLOCK);
461 *ms_Root = this;
462 ms_Root = &m_Next;
463 return str;
464 }
465
466 /*
467 * 保存
468 */
469 void CLine::Save(
470 FILE *df // ファイル
471 ){
472 fprintf(df, "\t\t\tLine{\n");
473 fprintf(df, "\t\t\t\tAddress = %p;\n", this);
474 fprintf(df, "\t\t\t\tLinePlugin = \"%s\";\n", CheckPluginID(m_LinePlugin));
475 SaveMapVector(df, "\t\t\t\tLineMapV = ", m_LineMapV);
476 fprintf(df, "\t\t\t\tRight = "); V3Save(df, m_Right, ";\n");
477 fprintf(df, "\t\t\t\tDir = "); V3Save(df, m_Dir, ";\n");
478 m_Link[0].Save(df, "\t\t\t\tLink0 = ");
479 m_Link[1].Save(df, "\t\t\t\tLink1 = ");
480 fprintf(df, "\t\t\t}\n");
481 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26