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