Unit1.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLS.BaseClasses"
  9. #pragma link "GLS.Cadencer"
  10. #pragma link "GLS.Coordinates"
  11. #pragma link "GLS.GeomObjects"
  12. #pragma link "GLS.Graph"
  13. #pragma link "GLS.Objects"
  14. #pragma link "GLS.Scene"
  15. #pragma link "GLS.SceneViewer"
  16. #pragma resource "*.dfm"
  17. TForm1 *Form1;
  18. const int SizePos = 10;
  19. const int ScaleSize = 3;
  20. const int TrigRect = 10;
  21. //---------------------------------------------------------------------------
  22. __fastcall TForm1::TForm1(TComponent* Owner)
  23. : TForm(Owner)
  24. {
  25. }
  26. //---------------------------------------------------------------------------
  27. void __fastcall TForm1::FormCreate(TObject *Sender)
  28. {
  29. Randomize;
  30. BoxScale = XYZVector;
  31. }
  32. //---------------------------------------------------------------------------
  33. void __fastcall TForm1::MakeRandomData()
  34. {
  35. int i;
  36. // Change position.
  37. if (CheckBox1->Checked)
  38. BoxPos = AffineVectorMake( Random()*SizePos -SizePos/2,
  39. Random()*SizePos -SizePos/2,
  40. Random()*SizePos -SizePos/2 );
  41. // Change scale.
  42. if (CheckBox2->Checked)
  43. BoxScale = AffineVectorMake( Random()*ScaleSize +ScaleSize/2,
  44. Random()*ScaleSize +ScaleSize/2,
  45. Random()*ScaleSize +ScaleSize/2 );
  46. // Change triange.
  47. if (CheckBox3->Checked)
  48. for (i = 0; i < 2; i++)
  49. TriangePos[i] = AffineVectorMake(Random()*TrigRect -TrigRect/2,
  50. Random()*TrigRect -TrigRect/2,
  51. Random()*TrigRect -TrigRect/2 );
  52. // Calc extends.
  53. MinExtend = VectorSubtract(BoxPos, VectorScale(BoxScale, 0.5));
  54. MaxExtend = VectorAdd(BoxPos, VectorScale(BoxScale, 0.5));
  55. }
  56. //---------------------------------------------------------------------------
  57. void __fastcall TForm1::DrawResult()
  58. {
  59. int i;
  60. GLPolygon1->Nodes->Clear();
  61. GLPolygon1->AddNode(TriangePos[0]);
  62. GLPolygon1->AddNode(TriangePos[1]);
  63. GLPolygon1->AddNode(TriangePos[2]);
  64. GLPoints1->Positions->Clear();
  65. GLPoints1->Colors->Add(1, 0, 0, 1);
  66. for (i = 0; i < 2; i++)
  67. GLPoints1->Positions->Add(TriangePos[i]);
  68. GLLines2->Nodes->Clear();
  69. GLLines2->Nodes->AddNode(TriangePos[0]);
  70. GLLines2->Nodes->AddNode(TriangePos[1]);
  71. GLLines2->Nodes->AddNode(TriangePos[2]);
  72. GLLines2->Nodes->AddNode(TriangePos[0]);
  73. DCCamTarget->Position->SetPoint(BoxPos);
  74. DCCamTarget->Scale->SetVector(BoxScale);
  75. GLCube1->Position->SetPoint(BoxPos);
  76. GLCube1->Scale->SetVector(BoxScale);
  77. }
  78. void __fastcall TForm1::Button1Click(TObject *Sender)
  79. {
  80. int IterCnt;
  81. bool Res1;
  82. IterCnt = 0;
  83. while (Res1)
  84. {
  85. MakeRandomData();
  86. Res1 = IntersectTriangleBox( TriangePos[0], TriangePos[1], TriangePos[2],
  87. MinExtend, MaxExtend);
  88. IterCnt++;
  89. if (IterCnt >= 10000)
  90. {
  91. DrawResult();
  92. ShowMessage("Intersection not found!");
  93. exit;
  94. }
  95. }
  96. DrawResult();
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TForm1::Button2Click(TObject *Sender)
  100. {
  101. int IterCnt;
  102. bool Res1;
  103. IterCnt = 0;
  104. while (!Res1)
  105. {
  106. MakeRandomData();
  107. Res1 = IntersectTriangleBox(TriangePos[0], TriangePos[1], TriangePos[2],
  108. MinExtend, MaxExtend);
  109. IterCnt++;
  110. if (IterCnt >= 10000)
  111. {
  112. DrawResult();
  113. ShowMessage("Intersection not found!");
  114. exit;
  115. }
  116. }
  117. DrawResult();
  118. }
  119. //---------------------------------------------------------------------------
  120. void __fastcall TForm1::CheckBox4Click(TObject *Sender)
  121. {
  122. GLCube1->Visible = CheckBox4->Checked;
  123. GLXYZGrid1->Visible = CheckBox6->Checked;
  124. GLLines1->Visible = CheckBox5->Checked;
  125. }
  126. //---------------------------------------------------------------------------
  127. void __fastcall TForm1::GLCadencerProgress(TObject *Sender, const double deltaTime,
  128. const double newTime)
  129. {
  130. if (Form1->Active)
  131. Viewer->Invalidate();
  132. }
  133. //---------------------------------------------------------------------------
  134. void __fastcall TForm1::ViewerMouseMove(TObject *Sender, TShiftState Shift, int X,
  135. int Y)
  136. {
  137. if (Shift.Contains(ssLeft))
  138. GLCamera1->MoveAroundTarget(mdy -Y, mdx -X);
  139. mdx = X;
  140. mdy = Y;
  141. }
  142. //---------------------------------------------------------------------------
  143. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  144. TPoint &MousePos, bool &Handled)
  145. {
  146. GLCamera1->AdjustDistanceToTarget(Power(1.02, WheelDelta/120));
  147. }
  148. //---------------------------------------------------------------------------
  149. void __fastcall TForm1::FormResize(TObject *Sender)
  150. {
  151. GLCamera1->FocalLength = MinInteger(Height, Width) / 10;
  152. }
  153. //---------------------------------------------------------------------------
  154. void __fastcall TForm1::FormKeyPress(TObject *Sender, System::WideChar &Key)
  155. {
  156. if (Key = 0x27)
  157. Close();
  158. }
  159. //---------------------------------------------------------------------------