-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNodetree.cpp
More file actions
1256 lines (1152 loc) · 38.1 KB
/
Copy pathNodetree.cpp
File metadata and controls
1256 lines (1152 loc) · 38.1 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// (C) Copyright 1994-1995 Taco van Ieperen
//
//
#include "stdafx.h"
#include "nodetree.h"
#include "surveyleg.h"
#include "node.h"
#include "surfacedata.h"
#include "onstationdoc.h"
#include "onstation.h"
#include "waitforclosuredialog.h"
#include "jpclose.h"
#include "colortool.h"
#include "realfolder.h"
extern COnStationApp theApp;
extern CSettings * pSettings_G;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern COnStationDoc * pDocument_G;
CNode * CNodeTree::MakeUpAStartingNode(CRealFolder *myroot,int iColorScheme)
{
//No starting point, so try and find a default one.
m_iBlunderMode = 0;
CFolder *folder;
myroot->GotoTop();
folder=myroot->GetCurrent();
while (folder!=NULL)
{
if (folder->IsFolder())
{
CNode *node=MakeUpAStartingNode((CRealFolder *)folder,iColorScheme);
if (node!=NULL)
{
return node;
}
}
else
{
CSurveyLeg *leg=(CSurveyLeg *)folder;
if (leg->GetDrawColor(iColorScheme)!=COLOR_INACTIVE)
{
CSurveyShotArray *shots=leg->GetShotArray();
if (shots->GetSize()!=0)
{
CSurveyShot *shot=shots->GetAt(0);
if (shot->GetFromStationName()!=NULL)
{
return m_MyDocument->GetNodeTree()->FindNode(shot->GetFromStationName());
}
}
}
}
folder=myroot->GetNext();
}
return NULL;
}
void CNodeTree::AddShot(CSurveyShot *pShot)
{
ASSERT(pShot->m_FromNode==NULL);
pShot->m_FromNode=FindNode(pShot->m_szFromStationName,TRUE); //Add if not found
pShot->m_FromNode->AddShot(pShot);
ASSERT(pShot->m_ToNode==NULL);
if (pShot->m_szToStationName[0]!=0) //no to name is set since this is a simple wall vector shot
{
pShot->m_ToNode=FindNode(pShot->m_szToStationName,TRUE); //Add if it can't be found
pShot->m_ToNode->AddShot(pShot);
}
}
void CNodeTree::RemoveShot(CSurveyShot *pShot)
{
if (pShot->m_FromNode!=NULL)
{
BOOL bEmpty=pShot->m_FromNode->RemoveShot(pShot);
if (bEmpty)
{
RemoveNode(pShot->m_FromNode);
delete pShot->m_FromNode;
pShot->m_FromNode=NULL;
}
}
if (pShot->m_ToNode!=NULL) //Old one was emtpy.
{
BOOL bEmpty=pShot->m_ToNode->RemoveShot(pShot);
if (bEmpty)
{
RemoveNode(pShot->m_ToNode);
delete pShot->m_ToNode;
pShot->m_ToNode=NULL;
}
}
}
CNodeTree::CNodeTree(COnStationDoc *document)
{
m_MyDocument=document;
for (int i=0;i<HASHSIZE;i++)
{
m_HashTable[i]=NULL;
}
for (i=0;i<TOTAL_COLOR_SCHEMES;i++)
{
m_bIsClosureDataDirty[i]=TRUE;
}
m_iNumberOfNodes=0;
}
CNodeTree::~CNodeTree()
{
DeleteContents();
}
WORD CNodeTree::HashName(LPCSTR szName)
{
static long iMultiplies[8]={7,11,19,23,31,57,83,101};
long lReturn=0;
int i=0;
while (szName[i]!=NULL)
{
lReturn=lReturn+((long)szName[i])*iMultiplies[i];
i++;
}
if (lReturn<0)
lReturn=0;
return (WORD)(lReturn%HASHSIZE);
}
//Finds a node in the node tree. Creates one if requested if the node is not
//there already.
CNode * CNodeTree::FindNode(LPCSTR szName,BOOL bCreateIfMissing)
{
POSITION pos;
//This code is also used below in AddNewNode.
WORD w=HashName(szName);
if (m_HashTable[w]==NULL)
{
if (bCreateIfMissing)
{
m_HashTable[w]=new CPtrList();
goto MakeNode;
}
else
{
return NULL;
}
}
pos=m_HashTable[w]->GetHeadPosition();
while (pos!=NULL)
{
CNode *node=(CNode *)(m_HashTable[w]->GetNext(pos));
if (lstrcmpi(node->GetName(),szName)==0)
return node;
}
if (!bCreateIfMissing)
{
return NULL;
}
MakeNode:
CNode *node=new CNode(szName);
m_HashTable[w]->AddTail((CObject *)node);
m_iNumberOfNodes++;
return node;
}
void CNodeTree::RemoveNode(CNode *node)
{
// ASSERT(AfxCheckMemory());
WORD wHash=HashName(node->GetName());
ASSERT(m_HashTable[wHash]!=NULL);
POSITION pos=m_HashTable[wHash]->Find((CObject *)node);
ASSERT(pos!=NULL);
m_HashTable[wHash]->RemoveAt(pos);
if (m_HashTable[wHash]->IsEmpty())
{
delete m_HashTable[wHash];
m_HashTable[wHash]=NULL;
m_iNumberOfNodes--;
}
// ASSERT(AfxCheckMemory());
}
void CNodeTree::CalculateRawPositions(CSurfaceData *SurfaceData,int iColorScheme)
{
m_MyDocument->BeginWaitCursor();
UnmarkAllNodes();
m_bIsClosureDataDirty[iColorScheme]=TRUE;
SurfaceData->GlobalSetSurfacePositions();
//First calculate all of the "known" stuff based on our FIXED POINTS.
CalculateRawPositionsHelper(NULL,iColorScheme,NMT_MAINSURVEY);
m_NodesNotOnSurvey.RemoveAll();
//Now calculate all of the stuff that was not part of this survey.
//This is so that we can list it in the "ORPHANS" dialog box.
for (int i=0;i<HASHSIZE;i++)
{
if (m_HashTable[i]!=NULL)
{
POSITION pos=m_HashTable[i]->GetHeadPosition();
while (pos!=NULL)
{
CNode *node=(CNode *)(m_HashTable[i]->GetNext(pos));
if (node->GetMarkType()==NMT_NOTHING && node->GetLeg()->IsActive(iColorScheme))
{
node->SetConstrainedPosition(0.0f,0.0f,0.0f);
m_NodesNotOnSurvey.AddTail((LPVOID)node);
//calculate everything reachable from this node.
//This stops us from repeating information by accident.
CalculateRawPositionsHelper(node,iColorScheme,NMT_MISSINGSECTION);
}
}
}
}
m_MyDocument->EndWaitCursor();
}
void CNodeTree::CalculateRawPositionsHelper(CNode *start,int iColorScheme,NODE_MARKTYPE NMT)
{
int iRingHead=1;
int iRingTail=0;
CNode ** MyQueue=new CNode * [MAX_GWORLD_QDEPTH];
if (start!=NULL)
{
//If a node is suggested we simply start with that in our queue and solve from
//there.
MyQueue[0]=start;
start->SetMarkType(NMT);
}
else
{
//No node was suggested. This happens only on the main closure loop for
//the main system. What we do is put all of the fixed points onto our
//queue and solve from there. This allows us to spread the errors out as far
//from our fixed points as possible since we start at a series of known positions.
int iIndex=0;
CNode *node;
while ((node=m_MyDocument->GetSurface()->GetSuggestedStartingNode(iIndex))!=NULL)
{
TRACE("CalculateRawPositionHelper::Got a fixed point %s\n",node->GetName());
MyQueue[iIndex++]=node;
node->SetMarkType(NMT);
}
if (iIndex==0) //no fixed points, so make up one as best as we can and
//set it at the center of the surface
{
node=MakeUpAStartingNode(m_MyDocument->GetSurveyFolder(),iColorScheme);
if (node==NULL)
{
delete[] MyQueue;
return; //nothing to solve
}
TRACE("Made up a fake fixed point %s\n",node->GetName());
//Set starting position to middle of surface
//if not, then set it to 0,0,0
CSurfaceData *Surf=m_MyDocument->GetSurface();
node->SetConstrainedPosition(0.0f,0.0f,Surf->GetMiddleAltitude());
node->SetMarkType(NMT);
MyQueue[0]=node;
iRingHead=1;
}
else
{
iRingHead=iIndex;
}
}
//Use a array to eliminate recursion
while (iRingTail!=iRingHead)
{
CNode *node=MyQueue[iRingTail];
int iMax=node->GetNumberOfShots(NST_ANYSHOT);
for (int i=0;i<iMax;i++)
{
CSurveyShot *pShot=node->GetShotByIndex(i,NST_ANYSHOT);
// We add every shot we get to to the queue.
if (pShot->m_ToNode==node) //backsight from here
{
//Have we visited that node before
if (pShot->m_FromNode->GetMarkType()==NMT_NOTHING)
{
CPosMatrix *Pos=node->GetPosition(FALSE);
CPosMatrix *PosFrom=pShot->m_FromNode->GetPosition(FALSE);
Pos->Subtract(PosFrom,pShot->m_Delta);
pShot->m_FromNode->SetMarkType(NMT);
MyQueue[iRingHead]=pShot->m_FromNode;
iRingHead=(iRingHead+1)%MAX_GWORLD_QDEPTH;
}
}
else //Front sight from here
{
ASSERT(pShot->m_FromNode==node); //one has to match
if (pShot->m_ToNode->GetMarkType()==NMT_NOTHING) //TO a new node so add it and draw
{
CPosMatrix *Pos=node->GetPosition(FALSE);
CPosMatrix *PosTo=pShot->m_ToNode->GetPosition(FALSE);
Pos->Add(PosTo,pShot->m_Delta);
pShot->m_ToNode->SetMarkType(NMT);
MyQueue[iRingHead]=pShot->m_ToNode;
iRingHead=(iRingHead+1)%MAX_GWORLD_QDEPTH;
}
}
}
iRingTail=(iRingTail+1)%MAX_GWORLD_QDEPTH;
}
delete[] MyQueue;
}
//To fill the gworld we merely walk through the entire tree that we calculated
//and add all of the forward shots to the GWorld.
void CNodeTree::FillGWorld(CGWorld *GW)
{
m_MyDocument->BeginWaitCursor();
GW->Empty();
int iColorScheme=pSettings_G->m_iColorSchemeIndex;
for (int i=0;i<HASHSIZE;i++)
{
if (m_HashTable[i]!=NULL)
{
POSITION pos=m_HashTable[i]->GetHeadPosition();
while (pos!=NULL)
{
CNode *node=(CNode *)(m_HashTable[i]->GetNext(pos));
//Is the node marked?
if (node->GetMarkType()==NMT_MAINSURVEY)
{
int iMax=node->GetNumberOfShots(NST_ANYSHOT);
for (int i=0;i<iMax;i++)
{
CSurveyShot *pShot=node->GetShotByIndex(i,NST_ANYSHOT);
//Shots with total exclusion should not be in the Node Tree
if (pShot->m_FromNode==node)
{
//We can't just check the color scheme for the node because the shot
//color scheme is more accurate since only shots are stored in a
//given surveys where as nodes may be present in several surveys
COLORREF crColor;
if (pSettings_G->m_iColorSchemeIndex!=COLORSCHEME_BLUNDER)
{
crColor=pShot->GetLeg()->GetDrawColor(pSettings_G->m_iColorSchemeIndex);
}
else
{
crColor=pSettings_G->GetBlunderColor((int)pShot->GetStressDeviation());
}
if (crColor>=0)
{
//Discard hidden surveys as well as surface surveys if our view options are to not show them.
if (pShot->GetLeg()->GetDrawColor(pSettings_G->m_iColorSchemeIndex)>=0 && (pSettings_G->m_bSurfaceSurveys[pSettings_G->m_iColorSchemeIndex] || !(pShot->GetShotFlags()&SHOT_SURFACE)))
{
RenderShot(GW,pShot);
}
}
}
}
//Is the primary leg for this node visible? If so, show it.
//We couldn't filter this out outside the loop because the node
//may be in more then one leg so we want to ensure that we fairly
//represent all effected legs.
if (node->GetLeg()->GetDrawColor(iColorScheme)>=0)
{
if (node->GetLeg()->GetDrawColor(pSettings_G->m_iColorSchemeIndex)>=0)
{
CPosMatrix *Pos=node->GetPosition(pSettings_G->m_bShowClosedLoops[pSettings_G->m_iColorSchemeIndex]);
AddLabelToGWorld(GW,node,*Pos);
}
}
}
}
}
}
m_MyDocument->EndWaitCursor();
}
//This also adds the flags for the constrained positions
void CNodeTree::AddLabelToGWorld(CGWorld *GW,CNode *node,CPosMatrix& Pos)
{
CSurveyLeg *Leg=node->GetLeg();
//Don't add surface nodes if surfaces are turned off
if (!pSettings_G->m_bSurfaceSurveys[pSettings_G->m_iColorSchemeIndex] && node->IsSurfaceNode())
{
return;
}
BOOL bIntersect=node->NeedATextLabel(pSettings_G->m_iColorSchemeIndex);
if (node->MatchesQuery())
{
GW->AddConstraint(Pos,pSettings_G->m_crQueryColor,node,TRUE);
}
if (pSettings_G->m_iColorSchemeIndex==COLORSCHEME_DEPTH)
{
int iToZone=-1*(int)((Pos.GetZ()-pSettings_G->m_fStartDepth)/pSettings_G->m_fDepthIncrements);
GW->AddLabel(node,Pos,node->GetName(),bIntersect,pSettings_G->GetDepthColor(iToZone));
}
else
{
COLORREF crColor=Leg->GetDrawColor(pSettings_G->m_iColorSchemeIndex);
if (crColor!=COLOR_INVISIBLE)
{
GW->AddLabel(node,Pos,node->GetName(),bIntersect,crColor);
}
}
}
/*
void CNodeTree::AddRectangleToGWorld(CGWorld *GW,CSurveyLeg *Leg,CPosMatrix& topLeft,CPosMatrix& topRight,CPosMatrix& bottomRight,CPosMatrix& bottomLeft,BOOL bMatchesQuery,LIGHTINGMODIFIER LM)
{
ASSERT(Leg!=NULL);
COLORREF crColor;
if (!bMatchesQuery)
{
crColor=Leg->GetDrawColor(ppSettings_G->m_iColorSchemeIndex);
}
else
{
crColor=ppSettings_G->m_iQueryColor;
}
//We fake nice lighting if the system is not doing
//the lighting calculations itself. Otherwise we rely on
//the system's lighting.
if (!ppSettings_G->m_bMultipleLights)
{
switch (LM)
{
case TOP_LIGHTING:
crColor=CGlobalColorManager::GetLighterShade(crColor);
break;
case SIDE_LIGHTING:
crColor=CGlobalColorManager::GetNeutralShade(crColor);
break;
case BOTTOM_LIGHTING:
crColor=CGlobalColorManager::GetDarkerShade(crColor);
break;
case NATURAL_LIGHTING:
break; //don't mess with the color.
};
}
GW->AddRectangle(topLeft,topRight,bottomRight,bottomLeft,crColor);
}
*/
void CNodeTree::AddLineToGWorld(CGWorld *GW,COLORREF crColor,CPosMatrix& From,CPosMatrix& To,BOOL bMatchesQuery,LINE_TYPE lineType)
{
if (From.IsEqual(To))
{
return; //Empty line. This check saves us some work
//in the cross section generating code.
}
if (!bMatchesQuery)
{
if (pSettings_G->m_iColorSchemeIndex!=COLORSCHEME_DEPTH)
{
GW->AddLineSegment(From,To,crColor,lineType);
}
else
{
AddDepthLineToGWorld(GW,From,To,lineType);
}
}
else
{
GW->AddLineSegment(From,To,pSettings_G->m_crQueryColor,lineType);
}
}
//We assume that any discarding of this shot would already have been
//done were it not to be displayed in the current view.
void CNodeTree::AddDepthLineToGWorld(CGWorld *GW,CPosMatrix& From,CPosMatrix& To,LINE_TYPE lineType)
{
CPosMatrix newFrom,newTo;
//Make sure that shots always go down. newTo Z must be less then newFrom Z
if (To.GetZ()>From.GetZ())
{
newTo=From;
newFrom=To;
}
else
{
newTo=To;
newFrom=From;
}
//If it is outside of the depth color arena, we don't need to segment it.
if (newFrom.GetZ()>=(pSettings_G->m_fStartDepth-pSettings_G->m_fDepthIncrements*MAX_DEPTH_COLORS)
&& newTo.GetZ()<=pSettings_G->m_fStartDepth)
{
//Start outside of the depth area. Draw the area first and then update the from position
//to be right on the boundary where the depth area starts.
if (newFrom.GetZ()>pSettings_G->m_fStartDepth)
{
CPosMatrix Temp;
newFrom.CalculateIntermediatePosFromZ(pSettings_G->m_fStartDepth,newTo,Temp);
if (pSettings_G->GetDepthColor(0)>=RGB(0,0,0))
{
GW->AddLineSegment(newFrom,Temp,pSettings_G->GetDepthColor(0),lineType);
}
newFrom=Temp;
}
if (newTo.GetZ()<pSettings_G->m_fStartDepth-pSettings_G->m_fDepthIncrements*MAX_DEPTH_COLORS)
{
CPosMatrix Temp;
newFrom.CalculateIntermediatePosFromZ(pSettings_G->m_fStartDepth-pSettings_G->m_fDepthIncrements*MAX_DEPTH_COLORS,newTo,Temp);
if (pSettings_G->GetDepthColor(MAX_DEPTH_COLORS-1)>=RGB(0,0,0))
{
GW->AddLineSegment(Temp,newTo,pSettings_G->GetDepthColor(MAX_DEPTH_COLORS-1),lineType);
}
newTo=Temp;
}
int iFromIndex=(int)(-1.0*(newFrom.GetZ()-pSettings_G->m_fStartDepth)/pSettings_G->m_fDepthIncrements);
int iToIndex=(int)(-1.0*(newTo.GetZ()-pSettings_G->m_fStartDepth)/pSettings_G->m_fDepthIncrements);
ASSERT(iFromIndex<=iToIndex);
while (iFromIndex!=iToIndex)
{
CPosMatrix Temp;
newFrom.CalculateIntermediatePosFromZ(pSettings_G->m_fStartDepth-pSettings_G->m_fDepthIncrements*(iFromIndex+1),newTo,Temp);
if (pSettings_G->GetDepthColor(iFromIndex)>=RGB(0,0,0))
{
GW->AddLineSegment(newFrom,Temp,pSettings_G->GetDepthColor(iFromIndex),lineType);
}
newFrom=Temp;
iFromIndex++;
}
if (pSettings_G->GetDepthColor(iFromIndex)>=RGB(0,0,0))
{
GW->AddLineSegment(newFrom,newTo,pSettings_G->GetDepthColor(iFromIndex),lineType);
}
}
else
{
//Set "above" or "below" color based on position
int iZone=MAX_DEPTH_COLORS-1;
if (newTo.GetZ()>=pSettings_G->m_fStartDepth)
{
iZone=0;
}
if (pSettings_G->GetDepthColor(iZone)>=0)
{
GW->AddLineSegment(newFrom,newTo,pSettings_G->GetDepthColor(iZone),lineType);
}
}
}
/*
void CNodeTree::AddDepthRectangleToGWorld(CGWorld *GW,CSurveyLeg *Leg,CPosMatrix& topLeft,CPosMatrix& topRight,CPosMatrix& bottomRight,CPosMatrix& bottomLeft)
{
float fAverageZ=(topLeft.GetZ()+bottomRight.GetZ())/2.0f;
CDocumentSettings *set=m_MyDocument->GetDocumentSettings();
int iColor=-1;
if (fAverageZ<=pSettings_G->m_fStartDepth)
{
iColor=pSettings_G->GetDepthColor(0);
}
else if (fAverageZ>=pSettings_G->m_fStartDepth-pSettings_G->m_fDepthIncrements*pSettings_G->m_iNumDepthColors)
{
iColor=pSettings_G->GetDepthColor(pSettings_G->m_iNumDepthColors);
}
else
{
int iIndex=(int)((fAverageZ-pSettings_G->m_fStartDepth)/pSettings_G->m_fDepthIncrements);
iColor=pSettings_G->GetDepthColor(iIndex);
}
if (iColor>=0)
{
GW->AddRectangle(topLeft,topRight,bottomRight,bottomLeft,iColor);
}
}
*/
//Make sure that every node in the system is unmarked.
void CNodeTree::UnmarkAllNodes()
{
for (int i=0;i<HASHSIZE;i++)
{
if (m_HashTable[i]!=NULL)
{
POSITION pos=m_HashTable[i]->GetHeadPosition();
while (pos!=NULL)
{
CNode *node=(CNode *)(m_HashTable[i]->GetNext(pos));
node->SetMarkType(NMT_NOTHING);
}
}
}
}
BOOL CNodeTree::UnmarkQueryAllNodes()
{
BOOL bReturn=FALSE;
for (int i=0;i<HASHSIZE;i++)
{
if (m_HashTable[i]!=NULL)
{
POSITION pos=m_HashTable[i]->GetHeadPosition();
while (pos!=NULL)
{
CNode *node=(CNode *)(m_HashTable[i]->GetNext(pos));
if (node->MatchesQuery())
{
node->SetMatchesQuery(FALSE);
bReturn=TRUE;
}
}
}
}
return bReturn;
}
CNode * CNodeTree::GetMissingSectionNode(int iIndex)
{
ASSERT(iIndex<GetNumberOfMissingSections());
POSITION P=m_NodesNotOnSurvey.FindIndex(iIndex);
return (CNode *)m_NodesNotOnSurvey.GetAt(P);
}
int CNodeTree::GetNumberOfMissingSections() const
{
return m_NodesNotOnSurvey.GetCount();
}
//This function draws a passage from one point to an other. It does
//this by calculating cross sections for the two points and then linking
//them. Cross sections are cubes if we are at a passage intersection or
//they are simple rectangles if we are in a linear passage. This means
//that linear caves like castleguard tend to look a lot better then caves
//such as jewel cave.
void CNodeTree::RenderShot(CGWorld *GW,CSurveyShot *pShot)
{
BOOL bMatchesQuery= ((pShot->m_FromNode->MatchesQuery() && pSettings_G->m_bQueryFrom) ||
(pShot->m_ToNode->MatchesQuery() && pSettings_G->m_bQueryTo));
PASSAGE_TYPE ptDrawType=pSettings_G->m_PassageType[pSettings_G->m_iColorSchemeIndex];
//Surface surveys are always draw as line plots
if (pShot->GetShotFlags()&SHOT_SURFACE)
{
ptDrawType=PT_LINE_PLOT;
}
CSurveyLeg *Leg=pShot->GetLeg();
int iColorScheme=pSettings_G->m_iColorSchemeIndex;
if (Leg->GetAssignedColor(iColorScheme)==COLOR_INVISIBLE)
{
return;
}
CNodePositionInfo * PosFrom=pShot->m_FromNode->GetNodePositionInfo();
CNodePositionInfo * PosTo=pShot->m_ToNode->GetNodePositionInfo();
//Calculate the to position again if we are not closing loops. The reason is that
//if we have a closure error simply drawing from node to node is going to in effect
//connect the loop as opposed to showing the error. Worst of all, this connection
//will happen no matter how bad the loop closure error is. Better to have a little
//extra processing time and do it right.
//
CPosMatrix FakeTo;
CPosMatrix * DrawTo=NULL;
CPosMatrix *DrawFrom=NULL;
if (pSettings_G->m_bShowClosedLoops[iColorScheme])
{
DrawTo=&PosTo->m_ClosedPosition;
DrawFrom=&PosFrom->m_ClosedPosition;
}
else
{
DrawFrom=&PosFrom->m_RawPosition;
DrawFrom->Add(&FakeTo,pShot->m_Delta);
DrawTo=&FakeTo;
if (!FakeTo.IsEqual(PosTo->m_RawPosition))
{
//Actually, it is better to always add the closure errors
//or we will need to recalculate everything when we toggle
GW->AddClosureError(PosTo->m_RawPosition,FakeTo);
}
}
//Step 1: Do the From and To junctions
CJunctionBox junctionFrom;
CJunctionBox junctionTo;
//This line is a special line which is used when we are rotating the system to provide
//a quick wireframe.
COLORREF crShot;
if (pSettings_G->m_iColorSchemeIndex!=COLORSCHEME_BLUNDER)
{
crShot=Leg->GetDrawColor(pSettings_G->m_iColorSchemeIndex);
}
else
{
crShot=pSettings_G->GetBlunderColor((int)pShot->GetStressDeviation());
}
AddLineToGWorld(GW,crShot,*DrawFrom,*DrawTo,bMatchesQuery,LT_WIREFRAME);
switch (ptDrawType)
{
case PT_LINE_PLOT:
AddLineToGWorld(GW,crShot,*DrawFrom,*DrawTo,bMatchesQuery,(pShot->GetShotFlags()&SHOT_SURFACE)? LT_SURFACESHOT: LT_SHOT);
break;
case PT_WIDTHS:
case PT_HEIGHTS:
case PT_CROSS_SECTIONS:
case PT_FULL_PASSAGES:
case PT_FULL_PASSAGES_SKINNY:
{
//Step one, calculate junction geometry at the from and to positions.
//In the case of complex junctions the cross section is actually al little bit
//away from the junction so that we can draw volume around the intersection
//of the passages by joining cross sections in a clockwise direction.
if (pShot->m_FromNode->HasSimpleJunction())
{
pShot->m_FromNode->CreateSimpleJunctionBoxOutgoing(pShot,&junctionFrom);
junctionFrom.SetPosition(*DrawFrom);
}
else
{
pShot->m_FromNode->CreateComplexJunctionBoxOutgoing(pShot,&junctionFrom);
CPosMatrix newPos;
newPos.Set( (DrawTo->GetX()*OUTSIDE_WEIGHT+DrawFrom->GetX()*CENTER_WEIGHT),
(DrawTo->GetY()*OUTSIDE_WEIGHT+DrawFrom->GetY()*CENTER_WEIGHT),
(DrawTo->GetZ()*OUTSIDE_WEIGHT+DrawFrom->GetZ()*CENTER_WEIGHT));
junctionFrom.SetPosition(newPos);
}
//TO NODE
if (pShot->m_ToNode->HasSimpleJunction())
{
pShot->m_ToNode->CreateSimpleJunctionBoxIncoming(pShot,&junctionTo);
junctionTo.SetPosition(*DrawTo);
}
else
{
pShot->m_ToNode->CreateComplexJunctionBoxIncoming(pShot,&junctionTo);
//fix closure bug
CPosMatrix newPos;
newPos.Set( (DrawTo->GetX()*CENTER_WEIGHT+DrawFrom->GetX()*OUTSIDE_WEIGHT),
(DrawTo->GetY()*CENTER_WEIGHT+DrawFrom->GetY()*OUTSIDE_WEIGHT),
(DrawTo->GetZ()*CENTER_WEIGHT+DrawFrom->GetZ()*OUTSIDE_WEIGHT));
junctionTo.SetPosition(newPos);
}
}
break;
default:
ASSERT(FALSE);
}
//Step 2: Draw connections between the junctions, as well as the junctions
//themselves at the From Station
switch (ptDrawType)
{
case PT_LINE_PLOT: //already finished in the From section
break;
case PT_WIDTHS:
AddLineToGWorld(GW,crShot,junctionTo.m_Left,junctionFrom.m_Left,bMatchesQuery,LT_PASSAGEWIDTH);
AddLineToGWorld(GW,crShot,junctionTo.m_Right,junctionFrom.m_Right,bMatchesQuery,LT_PASSAGEWIDTH);
AddLineToGWorld(GW,crShot,junctionFrom.m_Left,junctionFrom.m_Right,bMatchesQuery,LT_PASSAGEWIDTH);
AddLineToGWorld(GW,crShot,junctionFrom.m_Middle,junctionTo.m_Middle,bMatchesQuery,LT_SHOT);
break;
case PT_HEIGHTS:
AddLineToGWorld(GW,crShot,junctionTo.m_Top,junctionFrom.m_Top,bMatchesQuery,LT_PASSAGEHEIGHT);
AddLineToGWorld(GW,crShot,junctionTo.m_Bottom,junctionFrom.m_Bottom,bMatchesQuery,LT_PASSAGEHEIGHT);
AddLineToGWorld(GW,crShot,junctionFrom.m_Top,junctionFrom.m_Bottom,bMatchesQuery,LT_PASSAGEHEIGHT);
AddLineToGWorld(GW,crShot,junctionFrom.m_Middle,junctionTo.m_Middle,bMatchesQuery,LT_SHOT);
break;
case PT_CROSS_SECTIONS:
AddLineToGWorld(GW,crShot,junctionFrom.m_TopLeft,junctionFrom.m_TopRight,bMatchesQuery,LT_CROSSSECTION);
AddLineToGWorld(GW,crShot,junctionFrom.m_TopRight,junctionFrom.m_BottomRight,bMatchesQuery,LT_CROSSSECTION);
AddLineToGWorld(GW,crShot,junctionFrom.m_BottomRight,junctionFrom.m_BottomLeft,bMatchesQuery,LT_CROSSSECTION);
AddLineToGWorld(GW,crShot,junctionFrom.m_BottomLeft,junctionFrom.m_TopLeft,bMatchesQuery,LT_CROSSSECTION);
AddLineToGWorld(GW,crShot,junctionFrom.m_Middle,junctionTo.m_Middle,bMatchesQuery,LT_SHOT);
break;
case PT_FULL_PASSAGES:
case PT_FULL_PASSAGES_SKINNY:
{
if (!bMatchesQuery)
{
if (pSettings_G->m_iColorSchemeIndex!=COLORSCHEME_DEPTH)
{
crShot=Leg->GetDrawColor(pSettings_G->m_iColorSchemeIndex);
}
else
{
crShot=pSettings_G->GetDepthColor((junctionFrom.m_Middle.GetZ()+junctionTo.m_Middle.GetZ())/2.0f);
}
}
else
{
crShot=pSettings_G->m_crQueryColor;
}
CGWorldTriangleStrip * pTriangleStrip=new CGWorldTriangleStrip(10,crShot);
pTriangleStrip->SetVertex(0,junctionFrom.m_TopLeft.GetDirectPointer());
pTriangleStrip->SetVertex(1,junctionTo.m_TopLeft.GetDirectPointer());
pTriangleStrip->SetVertex(2,junctionFrom.m_TopRight.GetDirectPointer());
pTriangleStrip->SetVertex(3,junctionTo.m_TopRight.GetDirectPointer());
pTriangleStrip->SetVertex(4,junctionFrom.m_BottomRight.GetDirectPointer());
pTriangleStrip->SetVertex(5,junctionTo.m_BottomRight.GetDirectPointer());
pTriangleStrip->SetVertex(6,junctionFrom.m_BottomLeft.GetDirectPointer());
pTriangleStrip->SetVertex(7,junctionTo.m_BottomLeft.GetDirectPointer());
pTriangleStrip->SetVertex(8,junctionFrom.m_TopLeft.GetDirectPointer());
pTriangleStrip->SetVertex(9,junctionTo.m_TopLeft.GetDirectPointer());
pTriangleStrip->SetRotationStripNormals();
GW->AddTriangleGroup(pTriangleStrip);
}
break;
default:
ASSERT(FALSE);
}
//Draw the junction at the other end if it is a complex junction or if
//there are no outgoing shots on the other end.
if ((!pShot->m_ToNode->HasSimpleJunction()) ||
(pShot->m_ToNode->GetNumberOfShots(NST_OUTGOINGSHOT)==0 && pShot->m_ToNode->GetShotByIndex(0,NST_INCOMINGSHOT)==pShot))
{
switch (pSettings_G->m_PassageType[pSettings_G->m_iColorSchemeIndex])
{
case PT_LINE_PLOT: //already finished in the From section
break;
case PT_WIDTHS:
AddLineToGWorld(GW,crShot,junctionTo.m_Left,junctionTo.m_Right,bMatchesQuery,LT_PASSAGEWIDTH);
break;
case PT_HEIGHTS:
AddLineToGWorld(GW,crShot,junctionTo.m_Top,junctionTo.m_Bottom,bMatchesQuery,LT_PASSAGEHEIGHT);
break;
case PT_CROSS_SECTIONS:
AddLineToGWorld(GW,crShot,junctionTo.m_TopLeft,junctionTo.m_TopRight,bMatchesQuery,LT_CROSSSECTION);
AddLineToGWorld(GW,crShot,junctionTo.m_TopRight,junctionTo.m_BottomRight,bMatchesQuery,LT_CROSSSECTION);
AddLineToGWorld(GW,crShot,junctionTo.m_BottomRight,junctionTo.m_BottomLeft,bMatchesQuery,LT_CROSSSECTION);
AddLineToGWorld(GW,crShot,junctionTo.m_BottomLeft,junctionTo.m_TopLeft,bMatchesQuery,LT_CROSSSECTION);
break;
case PT_FULL_PASSAGES:
case PT_FULL_PASSAGES_SKINNY:
//No need for anything extra here
break;
default:
ASSERT(FALSE);
}
}
//Finally, draw the junctions. This is the hardest part of the code.
//Since multiple shots could be responsible for calling the junction drawing code we
//adopt the rule that the first outgoing shot for the junction draws the junction,
//or, if there are no outgoing shots, the first incoming shot does it.
if ((pShot->GetShotFlags()&SHOT_SURFACE)==0)
{
//First outgoing shot
if (!pShot->m_FromNode->HasSimpleJunction() && pShot->m_FromNode->GetShotByIndex(0,NST_OUTGOINGSHOT)==pShot)
{
GenerateNodeJunctionGeometry(pShot->m_FromNode,GW,Leg,bMatchesQuery);
}
//First incoming shot of the other junction
if (!pShot->m_ToNode->HasSimpleJunction() && pShot->m_ToNode->GetNumberOfShots(NST_OUTGOINGSHOT)==0 && pShot->m_ToNode->GetShotByIndex(0,NST_INCOMINGSHOT)==pShot)
{
GenerateNodeJunctionGeometry(pShot->m_ToNode,GW,Leg,bMatchesQuery);
}
}
}
void CNodeTree::GenerateNodeJunctionGeometry(CNode *pNode,CGWorld *GW,CSurveyLeg *pLeg,BOOL bMatchesQuery)
{
if (pSettings_G->m_PassageType[pSettings_G->m_iColorSchemeIndex]==PT_LINE_PLOT)
{
return;
}
CJunctionGeometryBox junctionBox;
pNode->CreateComplexJunctionGeometry(&junctionBox,pSettings_G->m_bShowClosedLoops[pSettings_G->m_iColorSchemeIndex]);
int iNumRects=junctionBox.m_junctionBoxArray.GetSize();
COLORREF crShot;
if (pSettings_G->m_iColorSchemeIndex!=COLORSCHEME_BLUNDER)
{
crShot=pLeg->GetDrawColor(pSettings_G->m_iColorSchemeIndex);
}
else
{
pSettings_G->GetBlunderColor(0);
}
switch (pSettings_G->m_PassageType[pSettings_G->m_iColorSchemeIndex])
{
case PT_HEIGHTS:
{
for (int i=0;i<iNumRects;i++)
{
AddLineToGWorld(GW,crShot,junctionBox.m_junctionBoxArray[i]->m_Top,junctionBox.m_pCentralHeightOnlyJunctionBox->m_Top,bMatchesQuery,LT_PASSAGEHEIGHT);
AddLineToGWorld(GW,crShot,junctionBox.m_junctionBoxArray[i]->m_Bottom,junctionBox.m_pCentralHeightOnlyJunctionBox->m_Bottom,bMatchesQuery,LT_PASSAGEHEIGHT);
AddLineToGWorld(GW,crShot,junctionBox.m_junctionBoxArray[i]->m_Middle,junctionBox.m_pCentralHeightOnlyJunctionBox->m_Middle,bMatchesQuery,LT_PASSAGEHEIGHT);
//Draw the central junction, but do it only once
if (i==0)
{
AddLineToGWorld(GW,crShot,junctionBox.m_pCentralHeightOnlyJunctionBox->m_Top,junctionBox.m_pCentralHeightOnlyJunctionBox->m_Bottom,bMatchesQuery,LT_PASSAGEHEIGHT);
}
}
}
break;
case PT_WIDTHS:
{
for (int i=0;i<iNumRects;i++)
{
AddLineToGWorld(GW,crShot,junctionBox.m_junctionBoxArray[i]->m_Right,junctionBox.m_junctionBoxArray[(i+1)%iNumRects]->m_Left,bMatchesQuery,LT_PASSAGEWIDTH);
AddLineToGWorld(GW,crShot,junctionBox.m_junctionBoxArray[i]->m_Middle,junctionBox.m_pCentralHeightOnlyJunctionBox->m_Middle,bMatchesQuery,LT_PASSAGEWIDTH);
}
}
break;
case PT_CROSS_SECTIONS:
{
for (int i=0;i<iNumRects;i++)
{
AddLineToGWorld(GW,crShot,junctionBox.m_junctionBoxArray[i]->m_Middle,junctionBox.m_pCentralHeightOnlyJunctionBox->m_Middle,bMatchesQuery,LT_CROSSSECTION);
}
}
break;
case PT_FULL_PASSAGES:
case PT_FULL_PASSAGES_SKINNY:
{
if (!bMatchesQuery)
{
if (pSettings_G->m_iColorSchemeIndex!=COLORSCHEME_DEPTH)
{
crShot=pLeg->GetDrawColor(pSettings_G->m_iColorSchemeIndex);
}
else
{
crShot=pSettings_G->GetDepthColor(junctionBox.m_pCentralHeightOnlyJunctionBox->m_Middle.GetZ());
}
}
else
{
crShot=pSettings_G->m_crQueryColor;
}
CGWorldTriangleFan * pFanTop=new CGWorldTriangleFan(iNumRects*2+2,crShot);
CGWorldTriangleFan * pFanBottom=new CGWorldTriangleFan(iNumRects*2+2,crShot);
pFanTop->SetVertex(0,junctionBox.m_pCentralHeightOnlyJunctionBox->m_Top.GetDirectPointer());
pFanBottom->SetVertex(0,junctionBox.m_pCentralHeightOnlyJunctionBox->m_Bottom.GetDirectPointer());
float fUpNormal[3]={0.0f,0.0f,1.0f};
float fDownNormal[3]={-1.0f,0.0f,0.0f};
//We calculate the top backwards from the bottom to give them both the same
//winding. Otherwise our normals will all be backfacing for one set of these and that
//results in ugly drawing of vertices
for (int i=0;i<iNumRects;i++)
{
pFanTop->SetVertex(i*2+1,junctionBox.m_junctionBoxArray[iNumRects-1-i]->m_TopRight.GetDirectPointer(),fUpNormal);
pFanTop->SetVertex(i*2+2,junctionBox.m_junctionBoxArray[iNumRects-1-i]->m_TopLeft.GetDirectPointer(),fUpNormal);
pFanBottom->SetVertex(i*2+1,junctionBox.m_junctionBoxArray[i]->m_BottomLeft.GetDirectPointer(),fDownNormal);
pFanBottom->SetVertex(i*2+2,junctionBox.m_junctionBoxArray[i]->m_BottomRight.GetDirectPointer(),fDownNormal);
//TODO: these things still draw facing the wrong way.
//I don't know wether this is a vertex problem or a normal problem.
//Set CULLING on and see what happens to test this.
CGWorldTriangleStrip * pStrip=new CGWorldTriangleStrip(4,crShot);
pStrip->SetVertex(0,junctionBox.m_junctionBoxArray[i]->m_TopRight.GetDirectPointer());
pStrip->SetVertex(1,junctionBox.m_junctionBoxArray[i]->m_BottomRight.GetDirectPointer());
pStrip->SetVertex(2,junctionBox.m_junctionBoxArray[(i+1)%iNumRects]->m_TopLeft.GetDirectPointer());
pStrip->SetVertex(3,junctionBox.m_junctionBoxArray[(i+1)%iNumRects]->m_BottomLeft.GetDirectPointer());
pStrip->SetRectNormals();
GW->AddTriangleGroup(pStrip);
}
pFanTop->SetVertex(i*2+1,junctionBox.m_junctionBoxArray[iNumRects-1]->m_TopRight.GetDirectPointer(),fUpNormal);
pFanBottom->SetVertex(i*2+1,junctionBox.m_junctionBoxArray[0]->m_BottomLeft.GetDirectPointer(),fDownNormal);