Planes.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 15k
Category:

Graph program

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "..//Solidgraph.h"
  3. #include "Planes.h"
  4. #include "..//Dialogs//WorkPlanesSetupsDlg.h"
  5. CPlanes::CPlanes()
  6. {
  7. m_x_WP.enable = m_y_WP.enable = m_z_WP.enable = FALSE;
  8. m_x_WP.visible = m_y_WP.visible = m_z_WP.visible = TRUE;
  9. m_z_WP.enable = TRUE;
  10. m_x_WP.position = m_y_WP.position = m_z_WP.position = 0.0f;
  11. m_x_WP.r = 1.0f;
  12. m_x_WP.g = 0.0f;
  13. m_x_WP.b = 0.0f;
  14. m_x_WP.a = 0.2f;
  15. m_y_WP.r = 0.0f;
  16. m_y_WP.g = 1.0f;
  17. m_y_WP.b = 0.0f;
  18. m_y_WP.a = 0.2f;
  19. m_z_WP.r = 0.0f;
  20. m_z_WP.g = 0.0f;
  21. m_z_WP.b = 1.0f;
  22. m_z_WP.a = 0.2f;
  23. m_x_WP.step = m_y_WP.step = m_z_WP.step = 1.0f;
  24. m_active_work_plane = NULL;
  25. m_snap_point = NULL;
  26. m_grid_lines_count = 2;
  27. m_use_plane_point = false;
  28. }
  29. CPlanes::~CPlanes()
  30. {
  31. if (m_snap_point)
  32. delete m_snap_point;
  33. }
  34. void  CPlanes::SetupDialog()
  35. {
  36. CWorkPlanesSetupsDlg dlg;
  37. dlg.m_x_pos = m_x_WP.position;
  38. dlg.m_y_pos = m_y_WP.position;
  39. dlg.m_z_pos = m_z_WP.position;
  40. dlg.m_xy_vis = m_z_WP.visible;
  41. dlg.m_xz_vis = m_y_WP.visible;
  42. dlg.m_yz_vis = m_x_WP.visible;
  43. if (m_z_WP.enable)
  44. dlg.m_cur_work_plane = 0;
  45. else
  46. if (m_y_WP.enable)
  47. dlg.m_cur_work_plane = 1;
  48. else
  49. if (m_x_WP.enable)
  50. dlg.m_cur_work_plane = 2;
  51. else
  52. {
  53. ASSERT(0);
  54. }
  55. if (dlg.DoModal()==IDOK)
  56. {
  57. m_x_WP.position = dlg.m_x_pos ;
  58. m_y_WP.position = dlg.m_y_pos ;
  59. m_z_WP.position = dlg.m_z_pos ;
  60. m_x_WP.visible = dlg.m_yz_vis ;
  61. m_y_WP.visible = dlg.m_xz_vis ;
  62. m_z_WP.visible = dlg.m_xy_vis ;
  63. m_x_WP.enable = m_y_WP.enable = m_z_WP.enable = FALSE;
  64. if (dlg.m_cur_work_plane == 0)
  65. m_z_WP.enable = TRUE;
  66. else
  67. if (dlg.m_cur_work_plane == 1)
  68. m_y_WP.enable = TRUE;
  69. else
  70. if (dlg.m_cur_work_plane == 2)
  71. m_x_WP.enable = TRUE;
  72. else
  73. {
  74. ASSERT(0);
  75. }
  76. }
  77. }
  78. //#include "glMath.h"
  79. #include <float.h>
  80. void  CPlanes::DrawAxes(C3dCamera* cam)
  81. {
  82. }
  83. double  EyeToGridStep(double dist)
  84. {
  85. if (dist<=0.001 && dist>=-0.001)
  86. return 0.0001;
  87. if (dist<=0.01 && dist>=-0.01)
  88. return 0.001;
  89. if (dist<=0.1 && dist>=-0.1)
  90. return 0.01;
  91. if (dist<=1.0 && dist>=-1.0)
  92. return 0.1;
  93. if (dist<=10.0 && dist>=-10.0)
  94. return 1.0;
  95. if (dist<=100.0 && dist>=-100.0)
  96. return 10.0;
  97. if (dist<=1000.0 && dist>=-1000.0)
  98. return 100.0;
  99. if (dist<=10000.0 && dist>=-10000.0)
  100. return 1000.0;
  101. if (dist<=100000.0 && dist>=-100000.0)
  102. return 10000.0;
  103. return 100000.0;
  104. }
  105. void  CPlanes::DrawWorkPlanes(C3dCamera* cam,bool isCommanderRegime)
  106. {
  107. glPushAttrib(GL_ENABLE_BIT);
  108. glEnable(GL_BLEND);
  109. glDisable(GL_CULL_FACE);
  110. glDisable(GL_LIGHTING);
  111. glDisable(GL_TEXTURE_2D);
  112. glDisable(GL_LINE_SMOOTH);
  113. glDisable(GL_DEPTH_TEST);
  114. SG_POINT EyePos;
  115. cam->GetEyePos(&EyePos.x,&EyePos.y,&EyePos.z);
  116. SG_POINT LookAtPos;
  117. cam->GetLookAtPos(&LookAtPos.x,&LookAtPos.y,&LookAtPos.z);
  118. if (m_x_WP.enable && m_x_WP.visible)
  119. {
  120. double plD = 0.0;
  121. SG_POINT plP = {m_x_WP.position, 0.0, 0.0};
  122. SG_VECTOR plN = {1.0, 0.0, 0.0};
  123. sgSpaceMath::PlaneFromNormalAndPoint(plP, plN, plD);
  124. SG_POINT resPnt;
  125. if (cam->ProjectCenterOfScreenOnPlane(plD,plN, resPnt))
  126. {
  127. bool  actP = m_active_work_plane==&m_x_WP && m_use_plane_point && isCommanderRegime;
  128. if (actP)
  129. glColor4f(m_x_WP.r, m_x_WP.g, m_x_WP.b, m_x_WP.a+0.4f);
  130. else
  131. glColor4f(m_x_WP.r, m_x_WP.g, m_x_WP.b, m_x_WP.a+0.1f);
  132. double curPos, tmpDbl;
  133. m_x_WP.step = (float)EyeToGridStep(sgSpaceMath::PointsDistance(resPnt,EyePos));
  134. double  begVal1 = FloorGrid(resPnt.z-m_x_WP.step*7.0,(float)m_x_WP.step);
  135. double  endVal1 = FloorGrid(resPnt.z+m_x_WP.step*7.0,(float)m_x_WP.step);
  136. double  begVal2 = FloorGrid(resPnt.y-m_x_WP.step*7.0,(float)m_x_WP.step);
  137. double  endVal2 = FloorGrid(resPnt.y+m_x_WP.step*7.0,(float)m_x_WP.step);
  138. glBegin(GL_LINES);
  139. for (curPos = begVal1; curPos<=endVal1; curPos+=m_x_WP.step)
  140. {
  141. bool isCel = false;
  142. if (fabs(modf(curPos/(double)(m_x_WP.step*10.0),&tmpDbl))<0.00001)
  143. {
  144. if (actP)
  145. glColor4f(0.0f, 0.0f, 0.0f, m_x_WP.a+0.6f);
  146. else
  147. glColor4f(0.0f, 0.0f, 0.0f, m_x_WP.a+0.3f);
  148. isCel = true;
  149. }
  150. glVertex3d(resPnt.x, begVal2, curPos);
  151. glVertex3d(resPnt.x, endVal2, curPos);
  152. if (isCel)
  153. {
  154. if (actP)
  155. glColor4f(m_x_WP.r, m_x_WP.g, m_x_WP.b, m_x_WP.a+0.4f);
  156. else
  157. glColor4f(m_x_WP.r, m_x_WP.g, m_x_WP.b, m_x_WP.a+0.1f);
  158. }
  159. }
  160. for (curPos = begVal2; curPos<=endVal2; curPos+=m_x_WP.step)
  161. {
  162. bool isCel = false;
  163. if (fabs(modf(curPos/(double)(m_x_WP.step*10.0),&tmpDbl))<0.00001)
  164. {
  165. if (actP)
  166. glColor4f(0.0f, 0.0f, 0.0f, m_x_WP.a+0.6f);
  167. else
  168. glColor4f(0.0f, 0.0f, 0.0f, m_x_WP.a+0.3f);
  169. isCel = true;
  170. }
  171. glVertex3d(resPnt.x,curPos,  begVal1 );
  172. glVertex3d(resPnt.x,curPos,  endVal1 );
  173. if (isCel)
  174. {
  175. if (actP)
  176. glColor4f(m_x_WP.r, m_x_WP.g, m_x_WP.b, m_x_WP.a+0.4f);
  177. else
  178. glColor4f(m_x_WP.r, m_x_WP.g, m_x_WP.b, m_x_WP.a+0.1f);
  179. }
  180. }
  181. glEnd();
  182. }
  183. }
  184. if (m_y_WP.enable && m_y_WP.visible)
  185. {
  186. double plD = 0.0;
  187. SG_POINT plP = {0.0, m_y_WP.position, 0.0};
  188. SG_VECTOR plN = {0.0, 1.0, 0.0};
  189. sgSpaceMath::PlaneFromNormalAndPoint(plP, plN, plD);
  190. SG_POINT resPnt;
  191. if (cam->ProjectCenterOfScreenOnPlane(plD,plN, resPnt))
  192. {
  193. bool  actP = m_active_work_plane==&m_y_WP && m_use_plane_point && isCommanderRegime;
  194. if (actP)
  195. glColor4f(m_y_WP.r, m_y_WP.g, m_y_WP.b, m_y_WP.a+0.4f);
  196. else
  197. glColor4f(m_y_WP.r, m_y_WP.g, m_y_WP.b, m_y_WP.a+0.1f);
  198. double curPos, tmpDbl;
  199. m_y_WP.step = (float)EyeToGridStep(sgSpaceMath::PointsDistance(resPnt,EyePos));
  200. double  begVal1 = FloorGrid(resPnt.z-m_y_WP.step*7.0,(float)m_y_WP.step);
  201. double  endVal1 = FloorGrid(resPnt.z+m_y_WP.step*7.0,(float)m_y_WP.step);
  202. double  begVal2 = FloorGrid(resPnt.x-m_y_WP.step*7.0,(float)m_y_WP.step);
  203. double  endVal2 = FloorGrid(resPnt.x+m_y_WP.step*7.0,(float)m_y_WP.step);
  204. glBegin(GL_LINES);
  205. for (curPos = begVal1; curPos<=endVal1; curPos+=m_y_WP.step)
  206. {
  207. bool isCel = false;
  208. if (fabs(modf(curPos/(double)(m_y_WP.step*10.0),&tmpDbl))<0.00001)
  209. {
  210. if (actP)
  211. glColor4f(0.0f, 0.0f, 0.0f, m_y_WP.a+0.6f);
  212. else
  213. glColor4f(0.0f, 0.0f, 0.0f, m_y_WP.a+0.3f);
  214. isCel = true;
  215. }
  216. glVertex3d(begVal2, resPnt.y, curPos);
  217. glVertex3d(endVal2, resPnt.y, curPos);
  218. if (isCel)
  219. {
  220. if (actP)
  221. glColor4f(m_y_WP.r, m_y_WP.g, m_y_WP.b, m_y_WP.a+0.4f);
  222. else
  223. glColor4f(m_y_WP.r, m_y_WP.g, m_y_WP.b, m_y_WP.a+0.1f);
  224. }
  225. }
  226. for (curPos = begVal2; curPos<=endVal2; curPos+=m_y_WP.step)
  227. {
  228. bool isCel = false;
  229. if (fabs(modf(curPos/(double)(m_y_WP.step*10.0),&tmpDbl))<0.00001)
  230. {
  231. if (actP)
  232. glColor4f(0.0f, 0.0f, 0.0f, m_y_WP.a+0.6f);
  233. else
  234. glColor4f(0.0f, 0.0f, 0.0f, m_y_WP.a+0.3f);
  235. isCel = true;
  236. }
  237. glVertex3d(curPos, resPnt.y, begVal1 );
  238. glVertex3d(curPos, resPnt.y, endVal1 );
  239. if (isCel)
  240. {
  241. if (actP)
  242. glColor4f(m_y_WP.r, m_y_WP.g, m_y_WP.b, m_y_WP.a+0.4f);
  243. else
  244. glColor4f(m_y_WP.r, m_y_WP.g, m_y_WP.b, m_y_WP.a+0.1f);
  245. }
  246. }
  247. glEnd();
  248. }
  249. }
  250. if (m_z_WP.enable && m_z_WP.visible)
  251. {
  252. double plD = 0.0;
  253. SG_POINT plP = {0.0,0.0,m_z_WP.position};
  254. SG_VECTOR plN = {0.0, 0.0, 1.0};
  255. sgSpaceMath::PlaneFromNormalAndPoint(plP, plN, plD);
  256. SG_POINT resPnt;
  257. if (cam->ProjectCenterOfScreenOnPlane(plD,plN, resPnt))
  258. {
  259. bool  actP = m_active_work_plane==&m_z_WP && m_use_plane_point && isCommanderRegime;
  260. if (actP)
  261. glColor4f(m_z_WP.r, m_z_WP.g, m_z_WP.b, m_z_WP.a+0.4f);
  262. else
  263. glColor4f(m_z_WP.r, m_z_WP.g, m_z_WP.b, m_z_WP.a+0.1f);
  264. double curPos, tmpDbl;
  265. m_z_WP.step = (float)EyeToGridStep(sgSpaceMath::PointsDistance(resPnt,EyePos));
  266. double  begVal1 = FloorGrid(resPnt.y-m_z_WP.step*7.0,(float)m_z_WP.step);
  267. double  endVal1 = FloorGrid(resPnt.y+m_z_WP.step*7.0,(float)m_z_WP.step);
  268. double  begVal2 = FloorGrid(resPnt.x-m_z_WP.step*7.0,(float)m_z_WP.step);
  269. double  endVal2 = FloorGrid(resPnt.x+m_z_WP.step*7.0,(float)m_z_WP.step);
  270. glBegin(GL_LINES);
  271. for (curPos = begVal1; curPos<=endVal1; curPos+=m_z_WP.step)
  272. {
  273. bool isCel = false;
  274. if (fabs(modf(curPos/(double)(m_z_WP.step*10.0),&tmpDbl))<0.00001)
  275. {
  276. if (actP)
  277. glColor4f(0.0f, 0.0f, 0.0f, m_z_WP.a+0.6f);
  278. else
  279. glColor4f(0.0f, 0.0f, 0.0f, m_z_WP.a+0.3f);
  280. isCel = true;
  281. }
  282. glVertex3d(begVal2, curPos, resPnt.z);
  283. glVertex3d(endVal2, curPos, resPnt.z);
  284. if (isCel)
  285. {
  286. if (actP)
  287. glColor4f(m_z_WP.r, m_z_WP.g, m_z_WP.b, m_z_WP.a+0.4f);
  288. else
  289. glColor4f(m_z_WP.r, m_z_WP.g, m_z_WP.b, m_z_WP.a+0.1f);
  290. }
  291. }
  292. for (curPos = begVal2; curPos<=endVal2; curPos+=m_z_WP.step)
  293. {
  294. bool isCel = false;
  295. if (fabs(modf(curPos/(double)(m_z_WP.step*10.0),&tmpDbl))<0.00001)
  296. {
  297. if (actP)
  298. glColor4f(0.0f, 0.0f,0.0f, m_z_WP.a+0.6f);
  299. else
  300. glColor4f(0.0f, 0.0f, 0.0f, m_z_WP.a+0.3f);
  301. isCel = true;
  302. }
  303. glVertex3d(curPos, begVal1, resPnt.z);
  304. glVertex3d(curPos, endVal1, resPnt.z);
  305. if (isCel)
  306. {
  307. if (actP)
  308. glColor4f(m_z_WP.r, m_z_WP.g, m_z_WP.b, m_z_WP.a+0.4f);
  309. else
  310. glColor4f(m_z_WP.r, m_z_WP.g, m_z_WP.b, m_z_WP.a+0.1f);
  311. }
  312. }
  313. glEnd();
  314. }
  315. }
  316. DrawAxes(cam);
  317. if (isCommanderRegime && m_snap_point && m_active_work_plane->step>FLT_EPSILON && m_grid_lines_count>0
  318.  && m_use_plane_point)
  319. {
  320. glBegin(GL_LINES);
  321. glColor4f(m_active_work_plane->r, m_active_work_plane->g, m_active_work_plane->b, 1.0f);
  322. if (m_active_work_plane==&m_x_WP)
  323. {
  324. glVertex3d(m_snap_point->x,m_snap_point->y-m_active_work_plane->step*0.5,m_snap_point->z);
  325. glVertex3d(m_snap_point->x,m_snap_point->y+m_active_work_plane->step*0.5,m_snap_point->z);
  326. glVertex3d(m_snap_point->x,m_snap_point->y,m_snap_point->z-m_active_work_plane->step*0.5);
  327. glVertex3d(m_snap_point->x,m_snap_point->y,m_snap_point->z+m_active_work_plane->step*0.5);
  328. }
  329. if (m_active_work_plane==&m_y_WP)
  330. {
  331. glVertex3d(m_snap_point->x-m_active_work_plane->step*0.5,m_snap_point->y,m_snap_point->z);
  332. glVertex3d(m_snap_point->x+m_active_work_plane->step*0.5,m_snap_point->y,m_snap_point->z);
  333. glVertex3d(m_snap_point->x,m_snap_point->y,m_snap_point->z-m_active_work_plane->step*0.5);
  334. glVertex3d(m_snap_point->x,m_snap_point->y,m_snap_point->z+m_active_work_plane->step*0.5);
  335. }
  336. if (m_active_work_plane==&m_z_WP)
  337. {
  338. glVertex3d(m_snap_point->x-m_active_work_plane->step*0.5,m_snap_point->y,m_snap_point->z);
  339. glVertex3d(m_snap_point->x+m_active_work_plane->step*0.5,m_snap_point->y,m_snap_point->z);
  340. glVertex3d(m_snap_point->x,m_snap_point->y-m_active_work_plane->step*0.5,m_snap_point->z);
  341. glVertex3d(m_snap_point->x,m_snap_point->y+m_active_work_plane->step*0.5,m_snap_point->z);
  342. }
  343. glEnd();
  344. }
  345. glPopAttrib();
  346. }
  347. #include <float.h>
  348. SG_POINT*  CPlanes::GetPointOnWorkPlanes( SG_POINT* begAxePnt, SG_VECTOR* naprCos)
  349. {
  350. SG_VECTOR planeNorm;
  351. SG_POINT  tmpPnt;
  352. double    dist = DBL_MAX;
  353. SG_POINT  resPnt;
  354. double    tmpDst ;
  355. m_active_work_plane = NULL;
  356. if (m_snap_point)
  357. {
  358. delete m_snap_point;
  359. m_snap_point = NULL;
  360. }
  361. #define   COSINUS_60    0.5
  362. bool   second_prohod = false;
  363. sec_pr:
  364. if (m_x_WP.enable)
  365. {
  366. planeNorm.x = 1.0;
  367. planeNorm.y = 0.0; 
  368. planeNorm.z = 0.0;
  369. if ((second_prohod || fabs(naprCos->x)>COSINUS_60) && sgSpaceMath::IntersectPlaneAndLine(planeNorm, -m_x_WP.position, 
  370. *begAxePnt, *naprCos,
  371. tmpPnt)==1)
  372. {
  373. if (((tmpPnt.x-begAxePnt->x)*naprCos->x+
  374.  (tmpPnt.y-begAxePnt->y)*naprCos->y+
  375.  (tmpPnt.z-begAxePnt->z)*naprCos->z)>0.00001)
  376. {
  377. tmpDst = sgSpaceMath::PointsDistance(tmpPnt,*begAxePnt);
  378. if (tmpDst>0.00001)
  379. if (tmpDst<dist)
  380. {
  381. dist = tmpDst;
  382. m_active_work_plane = &m_x_WP;
  383. resPnt = tmpPnt;
  384. }
  385. }
  386. }
  387. }
  388. if (m_y_WP.enable)
  389. {
  390. planeNorm.x = 0.0;
  391. planeNorm.y = 1.0; 
  392. planeNorm.z = 0.0;
  393. if ((second_prohod || fabs(naprCos->y)>COSINUS_60) && sgSpaceMath::IntersectPlaneAndLine(planeNorm, -m_y_WP.position, 
  394. *begAxePnt, *naprCos,
  395. tmpPnt)==1)
  396. {
  397. if (((tmpPnt.x-begAxePnt->x)*naprCos->x+
  398.  (tmpPnt.y-begAxePnt->y)*naprCos->y+
  399.  (tmpPnt.z-begAxePnt->z)*naprCos->z)>0.00001)
  400. {
  401. tmpDst = sgSpaceMath::PointsDistance(tmpPnt,*begAxePnt);
  402. if (tmpDst>0.00001)
  403. if (tmpDst<dist)
  404. {
  405. dist = tmpDst;
  406. m_active_work_plane = &m_y_WP;
  407. resPnt = tmpPnt;
  408. }
  409. }
  410. }
  411. }
  412. if (m_z_WP.enable)
  413. {
  414. planeNorm.x = 0.0;
  415. planeNorm.y = 0.0; 
  416. planeNorm.z = 1.0;
  417. if ((second_prohod || fabs(naprCos->z)>COSINUS_60) && sgSpaceMath::IntersectPlaneAndLine(planeNorm, -m_z_WP.position, 
  418. *begAxePnt, *naprCos,
  419. tmpPnt)==1)
  420. {
  421. if (((tmpPnt.x-begAxePnt->x)*naprCos->x+
  422. (tmpPnt.y-begAxePnt->y)*naprCos->y+
  423. (tmpPnt.z-begAxePnt->z)*naprCos->z)>0.00001)
  424. {
  425. tmpDst = sgSpaceMath::PointsDistance(tmpPnt,*begAxePnt);
  426. if (tmpDst>0.00001)
  427. if (tmpDst<dist)
  428. {
  429. dist = tmpDst;
  430. m_active_work_plane = &m_z_WP;
  431. resPnt = tmpPnt;
  432. }
  433. }
  434. }
  435. }
  436. if (!second_prohod && m_active_work_plane==NULL)
  437. {
  438. second_prohod = true;
  439. goto sec_pr;
  440. }
  441. if (m_active_work_plane)
  442. {
  443. m_snap_point = new SG_POINT;
  444. *m_snap_point = resPnt;
  445. }
  446. return m_snap_point;
  447. }
  448. void   CPlanes::GetCurrentWorkPlane(double& d, SG_VECTOR& nrml)
  449. {
  450. if (!m_active_work_plane)
  451. {
  452. ASSERT(0);
  453. return;
  454. }
  455. d = m_active_work_plane->position;
  456. if (m_active_work_plane==&m_x_WP)
  457. {
  458. nrml.x = 1.0;
  459. nrml.y = 0.0;
  460. nrml.z = 0.0;
  461. }
  462. else
  463. if (m_active_work_plane==&m_y_WP)
  464. {
  465. nrml.x = 0.0;
  466. nrml.y = 1.0;
  467. nrml.z = 0.0;
  468. }
  469. else
  470. if (m_active_work_plane==&m_z_WP)
  471. {
  472. nrml.x = 0.0;
  473. nrml.y = 0.0;
  474. nrml.z = 1.0;
  475. }
  476. else
  477. {
  478. ASSERT(0);
  479. }
  480. }
  481. float  CPlanes::GetActiveWorkPlaneGridSize()
  482. {
  483. if (m_z_WP.enable)
  484. return m_z_WP.step;
  485. if (m_y_WP.enable)
  486. return m_y_WP.step;
  487. if (m_x_WP.enable)
  488. return m_x_WP.step;
  489. ASSERT(0);
  490. return 0.0f;
  491. }