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

Graph program

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "EllipsoidEdit.h"
  3. #include "..//resource.h"
  4. #include <math.h>
  5. CEllipsoidEditCommand::CEllipsoidEditCommand(sgCEllipsoid* edE, IApplicationInterface* appI):
  6. m_editable_ellipsoid(edE)
  7. , m_app(appI)
  8. , m_size_panel(NULL)
  9. , m_was_started(false)
  10. ,m_other_params_dlg(NULL)
  11. , m_matr(NULL)
  12. , m_scenar(0)
  13. {
  14. ASSERT(edE);
  15. ASSERT(m_app);
  16. m_base_pnt.x = m_base_pnt.y = m_base_pnt.z = 0.0;
  17. m_dir.x = m_dir.y = m_dir.z = 0.0;
  18. }
  19. CEllipsoidEditCommand::~CEllipsoidEditCommand()
  20. {
  21. m_app->GetCommandPanel()->RemoveAllDialogs();
  22. m_app->GetViewPort()->InvalidateViewPort();
  23. if(m_matr)
  24. delete m_matr;
  25. }
  26. void  CEllipsoidEditCommand::ReCalcDir()
  27. {
  28. m_dir.x = m_dir.y = m_dir.z = 0.0;
  29. switch(m_scenar) 
  30. {
  31. case 0:
  32. m_dir.x = 1.0;
  33. break;
  34. case 1:
  35. m_dir.y = 1.0;
  36. break;
  37. case 2:
  38. m_dir.z = 1.0;
  39. break;
  40. default:
  41. ASSERT(0);
  42. break;
  43. }
  44. if (m_matr)
  45. {
  46. SG_POINT ppp;
  47. ppp.x = ppp.y = ppp.z = 0.0;
  48. m_matr->ApplyMatrixToVector(ppp,m_dir);
  49. }
  50. else
  51. {
  52. ASSERT(0);
  53. }
  54. }
  55. bool    CEllipsoidEditCommand::PreTranslateMessage(MSG* pMsg)
  56. {
  57. /*if (pMsg->message==WM_KEYUP||
  58. pMsg->message==WM_CHAR)
  59. return false;*/
  60. if (pMsg->message==WM_KEYUP||pMsg->message==WM_KEYDOWN || 
  61. pMsg->message==WM_CHAR)
  62. {
  63. if (pMsg->wParam==VK_RETURN)
  64. {
  65. OnEnter();
  66. return true;
  67. }
  68. if (pMsg->wParam==VK_ESCAPE)
  69. {
  70. m_app->StopCommander();
  71. return true;
  72. }
  73. if (m_size_panel)
  74. {
  75. m_size_panel->GetWindow()->SendMessage(pMsg->message,
  76. pMsg->wParam,
  77. pMsg->lParam);
  78. }
  79. if (m_other_params_dlg)
  80. {
  81. m_other_params_dlg->SendMessage(pMsg->message,
  82. pMsg->wParam,
  83. pMsg->lParam);
  84. return false;
  85. }
  86. if (pMsg->message==WM_KEYDOWN)
  87. return false;
  88. else 
  89. return true;
  90. }
  91. else
  92. {
  93. if (pMsg->hwnd == m_app->GetViewPort()->GetWindow()->m_hWnd)
  94. {
  95. switch(pMsg->message) 
  96. {
  97. case WM_MOUSEMOVE:
  98. MouseMove(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  99. return true;
  100. case WM_LBUTTONUP:
  101. LeftClick(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  102. return true;
  103. default:
  104. return false;
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. void  CEllipsoidEditCommand::Start()
  111. {
  112. ASSERT(m_editable_ellipsoid);
  113. if (m_matr)
  114. delete m_matr;
  115. m_matr = new sgCMatrix(m_editable_ellipsoid->GetWorldMatrixData());
  116. m_matr->Transparent();
  117. m_matr->ApplyMatrixToPoint(m_base_pnt);
  118. SWITCH_RESOURCE
  119. CString lab;
  120. lab.LoadString(IDS_REDACTION);
  121. m_app->StartCommander(lab);
  122. m_app->GetCommandPanel()->RemoveAllDialogs();
  123. lab.LoadString(IDS_NEW_SIZE);
  124. m_size_panel = reinterpret_cast<IGetNumberPanel*>(m_app->
  125. GetCommandPanel()->
  126. AddDialog(IBaseInterfaceOfGetDialogs::GET_NUMBER_DLG,lab,false));
  127. lab.LoadString(IDS_ENTER_NEW_SIZE);
  128. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,lab);
  129. m_editable_ellipsoid->GetGeometry(m_ell_geo);
  130. switch(m_scenar) 
  131. {
  132. case 0:
  133. m_size_panel->SetNumber(m_ell_geo.Radius1);
  134. break;
  135. case 1:
  136. m_size_panel->SetNumber(m_ell_geo.Radius2);
  137. break;
  138. case 2:
  139. m_size_panel->SetNumber(m_ell_geo.Radius3);
  140. break;
  141. default:
  142. ASSERT(0);
  143. break;
  144. }
  145. ReCalcDir();
  146. m_app->GetCommandPanel()->SetActiveRadio(0);
  147. m_app->GetViewPort()->SetEditableObject(m_editable_ellipsoid);
  148. m_was_started = true;
  149. }
  150. void  CEllipsoidEditCommand::MouseMove(unsigned int nFlags,int pX,int pY)
  151. {
  152. if (!m_was_started)
  153. return;
  154. ASSERT(m_size_panel);
  155. if (!m_app->GetViewPort()->ProjectScreenPointOnLine(pX,pY,m_base_pnt,m_dir,m_projection))
  156. {
  157. switch(m_scenar) 
  158. {
  159. case 0:
  160. m_ell_geo.Radius1 = 0.0;
  161. break;
  162. case 1:
  163. m_ell_geo.Radius2 = 0.0;
  164. break;
  165. case 2:
  166. m_ell_geo.Radius3 = 0.0;
  167. break;
  168. default:
  169. ASSERT(0);
  170. break;
  171. }
  172. m_size_panel->SetNumber(0.0);
  173. return;
  174. }
  175. char sig = (((m_projection.x-m_base_pnt.x)*m_dir.x+
  176. (m_projection.y-m_base_pnt.y)*m_dir.y+
  177. (m_projection.z-m_base_pnt.z)*m_dir.z)>0)?1:-1;
  178. double  sz;
  179. sz = sgSpaceMath::PointsDistance(m_base_pnt,m_projection)*sig;
  180. sz = m_app->ApplyPrecision(sz);
  181. m_projection.x = m_base_pnt.x+m_dir.x*sz;
  182. m_projection.y = m_base_pnt.y+m_dir.y*sz;
  183. m_projection.z = m_base_pnt.z+m_dir.z*sz;
  184. switch(m_scenar) 
  185. {
  186. case 0:
  187. m_ell_geo.Radius1 = sz;
  188. break;
  189. case 1:
  190. m_ell_geo.Radius2 = sz;
  191. break;
  192. case 2:
  193. m_ell_geo.Radius3 = sz;
  194. break;
  195. default:
  196. ASSERT(0);
  197. break;
  198. }
  199. m_size_panel->SetNumber(sz);
  200. m_app->GetViewPort()->InvalidateViewPort();
  201. }
  202. void  CEllipsoidEditCommand::LeftClick(unsigned int nFlags,int pX,int pY)
  203. {
  204. if (!m_was_started)
  205. return;
  206. SWITCH_RESOURCE
  207. ASSERT(m_editable_ellipsoid);
  208. if (fabs(m_ell_geo.Radius1)<0.0001 ||
  209. fabs(m_ell_geo.Radius2)<0.0001 ||
  210. fabs(m_ell_geo.Radius3)<0.0001)
  211. {
  212. m_message.LoadString(IDS_ERROR_ZERO_SIZE);
  213. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  214. m_message);
  215. return;
  216. }
  217. if (m_ell_geo.Radius1<0) m_ell_geo.Radius1=-m_ell_geo.Radius1;
  218. if (m_ell_geo.Radius2<0) m_ell_geo.Radius2=-m_ell_geo.Radius2;
  219. if (m_ell_geo.Radius3<0) m_ell_geo.Radius3=-m_ell_geo.Radius3;
  220. sgCEllipsoid* elpsd = sgCreateEllipsoid(m_ell_geo.Radius1,
  221. m_ell_geo.Radius2,m_ell_geo.Radius3,m_ell_geo.MeridiansCount,
  222. m_ell_geo.ParallelsCount);
  223. if (!elpsd)
  224. return;
  225. elpsd->InitTempMatrix()->Multiply(*m_matr);
  226. elpsd->ApplyTempMatrix();
  227. elpsd->DestroyTempMatrix();
  228. sgGetScene()->StartUndoGroup();
  229. sgGetScene()->DetachObject(m_editable_ellipsoid);
  230. sgGetScene()->AttachObject(elpsd);
  231. sgGetScene()->EndUndoGroup();
  232. m_app->CopyAttributes(*elpsd,*m_editable_ellipsoid);
  233. m_app->GetViewPort()->InvalidateViewPort();
  234. m_app->StopCommander();
  235. }
  236. void  CEllipsoidEditCommand::Draw()
  237. {
  238. if (!m_was_started)
  239. return;
  240. float pC[3];
  241. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  242. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  243. m_app->GetViewPort()->GetPainter()->DrawPoint(m_projection);
  244. m_app->GetViewPort()->GetPainter()->GetUserColorLines(pC[0],pC[1],pC[2]);
  245. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  246. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(m_matr);
  247. m_app->GetViewPort()->GetPainter()->DrawEllipsoid(m_ell_geo.Radius1,
  248. m_ell_geo.Radius2,
  249. m_ell_geo.Radius3);
  250. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(NULL);
  251. }
  252. void  CEllipsoidEditCommand::OnEnter()
  253. {
  254. if (!m_was_started)
  255. return;
  256. SWITCH_RESOURCE
  257. ASSERT(m_editable_ellipsoid);
  258. double sz = m_size_panel->GetNumber();
  259. switch(m_scenar) 
  260. {
  261. case 0:
  262. m_ell_geo.Radius1 = sz;
  263. break;
  264. case 1:
  265. m_ell_geo.Radius2 = sz;
  266. break;
  267. case 2:
  268. m_ell_geo.Radius3 = sz;
  269. break;
  270. default:
  271. ASSERT(0);
  272. break;
  273. }
  274. if (fabs(m_ell_geo.Radius1)<0.0001 ||
  275. fabs(m_ell_geo.Radius2)<0.0001 ||
  276. fabs(m_ell_geo.Radius3)<0.0001)
  277. {
  278. m_message.LoadString(IDS_ERROR_ZERO_SIZE);
  279. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  280. m_message);
  281. return;
  282. }
  283. if (m_ell_geo.Radius1<0) m_ell_geo.Radius1=-m_ell_geo.Radius1;
  284. if (m_ell_geo.Radius2<0) m_ell_geo.Radius2=-m_ell_geo.Radius2;
  285. if (m_ell_geo.Radius3<0) m_ell_geo.Radius3=-m_ell_geo.Radius3;
  286. sgCEllipsoid* elpsd = sgCreateEllipsoid(m_ell_geo.Radius1,
  287. m_ell_geo.Radius2,m_ell_geo.Radius3,m_ell_geo.MeridiansCount,
  288. m_ell_geo.ParallelsCount);
  289. if (!elpsd)
  290. return;
  291. elpsd->InitTempMatrix()->Multiply(*m_matr);
  292. elpsd->ApplyTempMatrix();
  293. elpsd->DestroyTempMatrix();
  294. sgGetScene()->StartUndoGroup();
  295. sgGetScene()->DetachObject(m_editable_ellipsoid);
  296. sgGetScene()->AttachObject(elpsd);
  297. sgGetScene()->EndUndoGroup();
  298. m_app->CopyAttributes(*elpsd,*m_editable_ellipsoid);
  299. m_app->GetViewPort()->InvalidateViewPort();
  300. m_app->StopCommander();
  301. }
  302. unsigned int  CEllipsoidEditCommand::GetItemsCount()
  303. {
  304. return 4;
  305. }
  306. void         CEllipsoidEditCommand::GetItem(unsigned int itemID, CString& itSrt)
  307. {
  308. SWITCH_RESOURCE
  309. switch(itemID) {
  310. case 0:
  311. itSrt.LoadString(IDS_CHANGE_X_SIZE);
  312. break;
  313. case 1:
  314. itSrt.LoadString(IDS_CHANGE_Y_SIZE);
  315. break;
  316. case 2:
  317. itSrt.LoadString(IDS_CHANGE_Z_SIZE);
  318. break;
  319. case 3:
  320. itSrt.LoadString(IDS_OTHER_PARAMS);
  321. break;
  322. default:
  323. ASSERT(0);
  324. }
  325. }
  326. void     CEllipsoidEditCommand::GetItemState(unsigned int itemID, 
  327. bool& enbl, bool& checked)
  328. {
  329. enbl = true;
  330. if (!m_was_started)
  331. {
  332. checked = false;
  333. }
  334. else
  335. {
  336. if (m_scenar!=itemID)
  337. checked = false;
  338. else
  339. checked = true;
  340. }
  341. }
  342. HBITMAP     CEllipsoidEditCommand::GetItemBitmap(unsigned int)
  343. {
  344. return NULL;
  345. }
  346. void         CEllipsoidEditCommand::Run(unsigned int itemID)
  347. {
  348. if (itemID==3)
  349. {
  350. SWITCH_RESOURCE
  351. CSphereParamsDlg dlg;
  352. m_other_params_dlg = &dlg;
  353. ASSERT(m_editable_ellipsoid);
  354. dlg.SetDlgType(CSphereParamsDlg::ELLIPSOID_PARAMS);
  355. m_editable_ellipsoid->GetGeometry(m_ell_geo);
  356. dlg.SetParams(m_ell_geo.MeridiansCount, m_ell_geo.ParallelsCount);
  357. if (dlg.DoModal()==IDOK)
  358. {
  359. m_other_params_dlg = NULL;
  360. int mC,pC;
  361. dlg.GetParams(mC,pC);
  362. sgCMatrix spM(m_editable_ellipsoid->GetWorldMatrixData());
  363. spM.Transparent();
  364. sgCEllipsoid* el = 
  365. sgCreateEllipsoid(m_ell_geo.Radius1,m_ell_geo.Radius2,
  366. m_ell_geo.Radius3,mC,pC);
  367. el->InitTempMatrix()->Multiply(spM);
  368. el->ApplyTempMatrix();
  369. el->DestroyTempMatrix();
  370. sgGetScene()->StartUndoGroup();
  371. sgGetScene()->DetachObject(m_editable_ellipsoid);
  372. sgGetScene()->AttachObject(el);
  373. sgGetScene()->EndUndoGroup();
  374. m_app->CopyAttributes(*el,*m_editable_ellipsoid);
  375. m_app->GetViewPort()->InvalidateViewPort();
  376. }
  377. m_app->StopCommander();
  378. return;
  379. }
  380. else
  381. {
  382. m_scenar = itemID;
  383. Start();
  384. }
  385. }