SphericBand.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 "SphericBand.h"
  3. #include "..//resource.h"
  4. #include <math.h>
  5.  int     spheric_band_name_index=1;
  6. SphericBandCommand::SphericBandCommand(IApplicationInterface* appI):
  7. m_app(appI)
  8. ,m_base_point_panel(NULL)
  9. ,m_r_panel(NULL)
  10. ,m_k1_panel(NULL)
  11. ,m_k2_panel(NULL)
  12. , m_step(0)
  13. {
  14. ASSERT(m_app);
  15. m_rad = 0.0;
  16. m_matrix = new sgCMatrix;
  17. }
  18. SphericBandCommand::~SphericBandCommand()
  19. {
  20. if (m_matrix)
  21. delete m_matrix;
  22. m_app->GetCommandPanel()->RemoveAllDialogs();
  23. m_app->GetViewPort()->InvalidateViewPort();
  24. }
  25. bool    SphericBandCommand::PreTranslateMessage(MSG* pMsg)
  26. {
  27. /*if (pMsg->message==WM_KEYUP||
  28. pMsg->message==WM_CHAR)
  29. return false;*/
  30. if (pMsg->message==WM_KEYUP||pMsg->message==WM_KEYDOWN || 
  31. pMsg->message==WM_CHAR)
  32. {
  33. if (pMsg->wParam==VK_RETURN)
  34. {
  35. OnEnter();
  36. return true;
  37. }
  38. if (pMsg->wParam==VK_ESCAPE)
  39. {
  40. m_app->StopCommander();
  41. return true;
  42. }
  43. switch(m_step) {
  44. case 0:
  45. if (m_base_point_panel)
  46. m_base_point_panel->GetWindow()->SendMessage(pMsg->message,
  47. pMsg->wParam,
  48. pMsg->lParam);
  49. break;
  50. case 1:
  51. if (m_r_panel)
  52. m_r_panel->GetWindow()->SendMessage(pMsg->message,
  53. pMsg->wParam,
  54. pMsg->lParam);
  55. break;
  56. case 2:
  57. if (m_k1_panel)
  58. m_k1_panel->GetWindow()->SendMessage(pMsg->message,
  59. pMsg->wParam,
  60. pMsg->lParam);
  61. break;
  62. case 3:
  63. if (m_k2_panel)
  64. m_k2_panel->GetWindow()->SendMessage(pMsg->message,
  65. pMsg->wParam,
  66. pMsg->lParam);
  67. break;
  68. default:
  69. return false;
  70. }
  71. if (pMsg->message==WM_KEYDOWN)
  72. return false;
  73. else 
  74. return true;
  75. }
  76. else
  77. {
  78. if (pMsg->hwnd == m_app->GetViewPort()->GetWindow()->m_hWnd)
  79. {
  80. switch(pMsg->message) 
  81. {
  82. case WM_MOUSEMOVE:
  83. MouseMove(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  84. return true;
  85. case WM_LBUTTONDOWN:
  86. LeftClick(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  87. return true;
  88. default:
  89. return false;
  90. }
  91. }
  92. }
  93. return false;;
  94. }
  95. void   SphericBandCommand::SendCommanderMessage(ICommander::COMMANDER_MESSAGE mes, void* params)
  96. {
  97. if (mes==ICommander::CM_SWITCH_ROLLUP_DIALOG)
  98. {
  99. ASSERT(params!=NULL);
  100. int   newActiveDlg = *(reinterpret_cast<int*>(params));
  101. ASSERT(newActiveDlg<=3);
  102. m_step = (unsigned int)newActiveDlg;
  103. for (unsigned int i=m_step+1;i<=3;i++)
  104. m_app->GetCommandPanel()->EnableRadio(i,false);
  105. SWITCH_RESOURCE
  106. switch(m_step) 
  107. {
  108. case 0:
  109. m_message.LoadString(IDS_SPH_ENTER_CENTER);
  110. break;
  111. case 1:
  112. m_message.LoadString(IDS_SPH_ENTER_RAD);
  113. break;
  114. case 2:
  115. m_message.LoadString(IDS_SP_B_ENTER_F_C);
  116. break;
  117. case 3:
  118. m_message.LoadString(IDS_SP_B_ENTER_S_C);
  119. break;
  120. default:
  121. ASSERT(0);
  122. break;
  123. }
  124. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  125. }
  126. }
  127. void  SphericBandCommand::Start()
  128. {
  129. NewScenar();
  130. SWITCH_RESOURCE
  131. CString nm;
  132. nm.LoadString(IDS_TOOLTIP_SIXTH);
  133. m_app->StartCommander(nm);
  134. }
  135. void  SphericBandCommand::MouseMove(unsigned int nFlags,int pX,int pY)
  136. {
  137. switch(m_step) 
  138. {
  139. case 0:
  140. {
  141. ASSERT(m_base_point_panel);
  142. IViewPort::GET_SNAP_IN in_arg;
  143. in_arg.scrX = pX;
  144. in_arg.scrY = pY;
  145. in_arg.snapType = SNAP_SYSTEM;
  146. in_arg.XFix = m_base_point_panel->IsXFixed();
  147. in_arg.YFix = m_base_point_panel->IsYFixed();
  148. in_arg.ZFix = m_base_point_panel->IsZFixed();
  149. double tmpFl[3];
  150. m_base_point_panel->GetPoint(tmpFl[0],tmpFl[1],tmpFl[2]);
  151. in_arg.FixPoint.x = tmpFl[0];
  152. in_arg.FixPoint.y = tmpFl[1];
  153. in_arg.FixPoint.z = tmpFl[2];
  154. IViewPort::GET_SNAP_OUT out_arg;
  155. m_app->GetViewPort()->GetWorldPointAfterSnap(in_arg,out_arg);
  156. m_cur_pnt = out_arg.result_point;
  157. m_base_point_panel->SetPoint(m_cur_pnt.x,m_cur_pnt.y,m_cur_pnt.z);
  158. }
  159. break;
  160. case 1:
  161. {
  162. ASSERT(m_r_panel);
  163. double plD;
  164. SG_VECTOR PlNorm;
  165. m_app->GetViewPort()->GetViewPortNormal(PlNorm);
  166. sgSpaceMath::PlaneFromNormalAndPoint(m_first_pnt,PlNorm,plD);
  167. if (m_app->GetViewPort()->ProjectScreenPointOnPlane(pX,pY,PlNorm,plD,m_cur_pnt))
  168. {
  169. m_rad = m_app->ApplyPrecision(
  170. sgSpaceMath::PointsDistance(m_first_pnt,m_cur_pnt)
  171. );
  172. }
  173. else
  174. m_rad = 0.0;
  175. m_r_panel->SetNumber(m_rad);
  176. }
  177. break;
  178. case 2:
  179. {
  180. ASSERT(m_k1_panel);
  181. SG_VECTOR dir; dir.z = 1.0; dir.y = 0.0; dir.x = 0.0;
  182. if (!m_app->GetViewPort()->ProjectScreenPointOnLine(pX,pY,m_first_pnt,dir,m_projection))
  183. {
  184. m_coef[0] = 0.0;
  185. m_k1_panel->SetNumber(m_coef[0]);
  186. break;
  187. }
  188. char sig = (((m_projection.x-m_first_pnt.x)*dir.x+
  189. (m_projection.y-m_first_pnt.y)*dir.y+
  190. (m_projection.z-m_first_pnt.z)*dir.z)>0)?1:-1;
  191. m_coef[0] = sgSpaceMath::PointsDistance(m_first_pnt,m_projection)*sig/m_rad;
  192. if (m_coef[0]>1.0) m_coef[0] = 1.0;
  193. if (m_coef[0]<-1.0) m_coef[0] = -1.0;
  194. m_projection.z = m_first_pnt.z+m_rad*m_coef[0];
  195. m_k1_panel->SetNumber(m_coef[0]);
  196. m_fir_projection = m_projection;
  197. }
  198. break;
  199. case 3:
  200. {
  201. ASSERT(m_k2_panel);
  202. SG_VECTOR dir; dir.z = 1.0; dir.y = 0.0; dir.x = 0.0;
  203. if (!m_app->GetViewPort()->ProjectScreenPointOnLine(pX,pY,m_first_pnt,dir,m_projection))
  204. {
  205. m_coef[1] = 0.0;
  206. m_k2_panel->SetNumber(m_coef[1]);
  207. break;
  208. }
  209. char sig = (((m_projection.x-m_first_pnt.x)*dir.x+
  210. (m_projection.y-m_first_pnt.y)*dir.y+
  211. (m_projection.z-m_first_pnt.z)*dir.z)>0)?1:-1;
  212. m_coef[1] = sgSpaceMath::PointsDistance(m_first_pnt,m_projection)*sig/m_rad;
  213. if (m_coef[1]>1.0) m_coef[1] = 1.0;
  214. if (m_coef[1]<-1.0) m_coef[1] = -1.0;
  215. m_projection.z = m_first_pnt.z+m_rad*m_coef[1];
  216. m_k2_panel->SetNumber(m_coef[1]);
  217. }
  218. break;
  219. default:
  220. ASSERT(0);
  221. }
  222. m_app->GetViewPort()->InvalidateViewPort();
  223. }
  224. void  SphericBandCommand::LeftClick(unsigned int nFlags,int pX,int pY)
  225. {
  226. SWITCH_RESOURCE
  227. switch(m_step) 
  228. {
  229. case 0:
  230. m_first_pnt=m_cur_pnt;
  231. m_matrix->Identity();
  232. m_matrix->Translate(m_first_pnt);
  233. m_step++;
  234. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  235. m_message.LoadString(IDS_SPH_ENTER_RAD);
  236. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  237. break;
  238. case 1:
  239. {
  240. double rd = sgSpaceMath::PointsDistance(m_first_pnt,m_cur_pnt);
  241. rd = m_app->ApplyPrecision(rd);
  242. if (fabs(rd)<0.0001)
  243. {
  244. m_message.LoadString(IDS_ERROR_ZERO_RAD);
  245. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  246. m_message);
  247. return;
  248. }
  249. else
  250. if (rd<-0.0001)
  251. {
  252. m_message.LoadString(IDS_ERROR_RAD_MUST_BE_POSIT);
  253. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  254. m_message);
  255. return;
  256. }
  257. m_rad = rd;
  258. m_step++;
  259. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  260. m_message.LoadString(IDS_SP_B_ENTER_F_C);
  261. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  262. }
  263. break;
  264. case 2:
  265. {
  266. m_step++;
  267. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  268. m_message.LoadString(IDS_SP_B_ENTER_S_C);
  269. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  270. }
  271. break;
  272. case 3:
  273. {
  274. if (fabs(m_coef[0]-m_coef[1])<0.0001)
  275. {
  276. m_message.LoadString(IDS_COEF_IS_EQ);
  277. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  278. m_message);
  279. return;
  280. }
  281. if (m_coef[0]>=m_coef[1])
  282. {
  283. double ttt = m_coef[0];
  284. m_coef[0] = m_coef[1];
  285. m_coef[1] = ttt;
  286. }
  287. sgCSphericBand* spb = sgCreateSphericBand(m_rad,m_coef[0],m_coef[1],24);
  288. if (!spb)
  289. return;
  290. spb->InitTempMatrix()->Translate(m_first_pnt);
  291. spb->ApplyTempMatrix();
  292. spb->DestroyTempMatrix();
  293. CString nm;
  294. nm.LoadString(IDS_TOOLTIP_SIXTH);
  295. CString nmInd;
  296. nmInd.Format("%i",spheric_band_name_index);
  297. nm+=nmInd;
  298. spb->SetName(nm);
  299. sgGetScene()->StartUndoGroup();
  300. sgGetScene()->AttachObject(spb);
  301. m_app->ApplyAttributes(spb);
  302. sgGetScene()->EndUndoGroup();
  303. spheric_band_name_index++;
  304. m_app->GetViewPort()->InvalidateViewPort();
  305. NewScenar();
  306. }
  307. break;
  308. default:
  309. ASSERT(0);
  310. break;
  311. }
  312. }
  313. void  SphericBandCommand::Draw()
  314. {
  315. switch(m_step) 
  316. {
  317. case 0:
  318. {
  319. float pC[3];
  320. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  321. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  322. m_app->GetViewPort()->GetPainter()->DrawPoint(m_cur_pnt);
  323. }
  324. break;
  325. case 1:
  326. {
  327. float pC[3];
  328. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  329. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  330. m_app->GetViewPort()->GetPainter()->DrawPoint(m_cur_pnt);
  331. m_app->GetViewPort()->GetPainter()->DrawPoint(m_first_pnt);
  332. m_app->GetViewPort()->GetPainter()->GetUserColorLines(pC[0],pC[1],pC[2]);
  333. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  334. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(m_matrix);
  335. m_app->GetViewPort()->GetPainter()->DrawSphere(m_rad);
  336. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(NULL);
  337. }
  338. break;
  339. case 2:
  340. {
  341. float pC[3];
  342. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  343. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  344. m_app->GetViewPort()->GetPainter()->DrawPoint(m_first_pnt);
  345. m_app->GetViewPort()->GetPainter()->DrawPoint(m_fir_projection);
  346. m_app->GetViewPort()->GetPainter()->GetUserColorLines(pC[0],pC[1],pC[2]);
  347. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  348. SG_LINE ln;
  349. ln.p1 = m_first_pnt;
  350. ln.p2 = m_fir_projection;
  351. m_app->GetViewPort()->GetPainter()->DrawLine(ln);
  352. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(m_matrix);
  353. m_app->GetViewPort()->GetPainter()->DrawSphericBand(m_rad,m_coef[0],m_coef[0]);
  354. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(NULL);
  355. }
  356. break;
  357. case 3:
  358. {
  359. float pC[3];
  360. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  361. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  362. m_app->GetViewPort()->GetPainter()->DrawPoint(m_fir_projection);
  363. m_app->GetViewPort()->GetPainter()->DrawPoint(m_projection);
  364. m_app->GetViewPort()->GetPainter()->GetUserColorLines(pC[0],pC[1],pC[2]);
  365. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  366. SG_LINE ln;
  367. ln.p1 = m_fir_projection;
  368. ln.p2 = m_projection;
  369. m_app->GetViewPort()->GetPainter()->DrawLine(ln);
  370. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(m_matrix);
  371. m_app->GetViewPort()->GetPainter()->DrawSphericBand(m_rad,m_coef[0],m_coef[1]);
  372. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(NULL);
  373. }
  374. break;
  375. default:
  376. ASSERT(0);
  377. }
  378. }
  379. void  SphericBandCommand::OnEnter()
  380. {
  381. SWITCH_RESOURCE
  382. switch(m_step) 
  383. {
  384. case 0:
  385. {
  386. ASSERT(m_base_point_panel);
  387. m_base_point_panel->GetPoint(m_first_pnt.x,
  388. m_first_pnt.y,
  389. m_first_pnt.z);
  390. m_matrix->Identity();
  391. m_matrix->Translate(m_first_pnt);
  392. m_step++;
  393. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  394. m_message.LoadString(IDS_SPH_ENTER_RAD);
  395. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  396. }
  397. break;
  398. case 1:
  399. {
  400. double rd = m_r_panel->GetNumber();
  401. if (fabs(rd)<0.0001)
  402. {
  403. m_message.LoadString(IDS_ERROR_ZERO_RAD);
  404. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  405. m_message);
  406. return;
  407. }
  408. else
  409. if (rd<-0.0001)
  410. {
  411. m_message.LoadString(IDS_ERROR_RAD_MUST_BE_POSIT);
  412. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  413. m_message);
  414. return;
  415. }
  416. m_rad = rd;
  417. m_step++;
  418. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  419. m_message.LoadString(IDS_SP_B_ENTER_F_C);
  420. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  421. }
  422. break;
  423. case 2:
  424. {
  425. double rd = m_k1_panel->GetNumber();
  426. if (fabs(rd)>1.000)
  427. {
  428. m_message.LoadString(IDS_ERROR_COEF_RANGE);
  429. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  430. m_message);
  431. return;
  432. }
  433. m_coef[0] = rd;
  434. m_step++;
  435. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  436. m_message.LoadString(IDS_SP_B_ENTER_S_C);
  437. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  438. }
  439. break;
  440. case 3:
  441. {
  442. double rd = m_k2_panel->GetNumber();
  443. if (fabs(rd)>1.000)
  444. {
  445. m_message.LoadString(IDS_ERROR_COEF_RANGE);
  446. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  447. m_message);
  448. return;
  449. }
  450. m_coef[1] = rd;
  451. if (fabs(m_coef[0]-m_coef[1])<0.0001)
  452. {
  453. m_message.LoadString(IDS_COEF_IS_EQ);
  454. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  455. m_message);
  456. return;
  457. }
  458. if (m_coef[0]>=m_coef[1])
  459. {
  460. double ttt = m_coef[0];
  461. m_coef[0] = m_coef[1];
  462. m_coef[1] = ttt;
  463. }
  464. sgCSphericBand* spb = sgCreateSphericBand(m_rad,m_coef[0],m_coef[1],24);
  465. if (!spb)
  466. return;
  467. spb->InitTempMatrix()->Translate(m_first_pnt);
  468. spb->ApplyTempMatrix();
  469. spb->DestroyTempMatrix();
  470. CString nm;
  471. nm.LoadString(IDS_TOOLTIP_SIXTH);
  472. CString nmInd;
  473. nmInd.Format("%i",spheric_band_name_index);
  474. nm+=nmInd;
  475. spb->SetName(nm);
  476. sgGetScene()->StartUndoGroup();
  477. sgGetScene()->AttachObject(spb);
  478. m_app->ApplyAttributes(spb);
  479. sgGetScene()->EndUndoGroup();
  480. spheric_band_name_index++;
  481. m_app->GetViewPort()->InvalidateViewPort();
  482. NewScenar();
  483. }
  484. break;
  485. default:
  486. ASSERT(0);
  487. break;
  488. }
  489. }
  490. unsigned int  SphericBandCommand::GetItemsCount()
  491. {
  492. return 0;
  493. }
  494. void         SphericBandCommand::GetItem(unsigned int, CString&)
  495. {
  496. //SWITCH_RESOURCE
  497. }
  498. void     SphericBandCommand::GetItemState(unsigned int, bool&, bool&)
  499. {
  500. }
  501. HBITMAP     SphericBandCommand::GetItemBitmap(unsigned int)
  502. {
  503. return NULL;
  504. }
  505. void         SphericBandCommand::Run(unsigned int)
  506. {
  507. }
  508. void  SphericBandCommand::NewScenar()
  509. {
  510. SWITCH_RESOURCE
  511. m_app->GetCommandPanel()->RemoveAllDialogs();
  512. CString lab;
  513. lab.LoadString(IDS_CENTER);
  514. m_base_point_panel = reinterpret_cast<IGetPointPanel*>(m_app->
  515. GetCommandPanel()->
  516. AddDialog(IBaseInterfaceOfGetDialogs::GET_POINT_DLG,lab,true));
  517. lab.LoadString(IDS_RADIUS);
  518. m_r_panel = reinterpret_cast<IGetNumberPanel*>(m_app->
  519. GetCommandPanel()->
  520. AddDialog(IBaseInterfaceOfGetDialogs::GET_NUMBER_DLG,lab,true));
  521. lab.LoadString(IDS_FIRST_COEF);
  522. m_k1_panel = reinterpret_cast<IGetNumberPanel*>(m_app->
  523. GetCommandPanel()->
  524. AddDialog(IBaseInterfaceOfGetDialogs::GET_NUMBER_DLG,lab,true));
  525. lab.LoadString(IDS_SECOND_COEF);
  526. m_k2_panel = reinterpret_cast<IGetNumberPanel*>(m_app->
  527. GetCommandPanel()->
  528. AddDialog(IBaseInterfaceOfGetDialogs::GET_NUMBER_DLG,lab,true));
  529. m_app->GetCommandPanel()->SetActiveRadio(0);
  530. m_step=0;
  531. m_message.LoadString(IDS_SPH_ENTER_CENTER);
  532. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  533. memset(m_coef,0,sizeof(double)*2);
  534. }