0024001: Stereographic rendering support
[occt.git] / samples / mfc / standard / Common / OCC_StereoConfigDlg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // OCC_StereoConfigDlg.cpp : source file
3 ///////////////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "OCC_StereoConfigDlg.h"
7 #include <Graphic3d_Camera.hxx>
8
9 BEGIN_MESSAGE_MAP (OCC_StereoConfigDlg, CDialog)
10
11   ON_WM_HSCROLL()
12   ON_NOTIFY (UDN_DELTAPOS, IDC_SPIN_FOCUS, OnSpinFocus)
13   ON_NOTIFY (UDN_DELTAPOS, IDC_SPIN_IOD, OnSpinIOD)
14   ON_BN_CLICKED (IDC_CHECK_FOCUS_RELATIVE, OnCheckFocus)
15   ON_BN_CLICKED (IDC_CHECK_IOD_RELATIVE, OnCheckIOD)
16   ON_EN_CHANGE (IDC_EDIT_FOCUS, OnChangeFocus)
17   ON_EN_CHANGE (IDC_EDIT_IOD, OnChangeIOD)
18
19 END_MESSAGE_MAP()
20
21 // round up value macro
22 #define ROUND_UP(X) X = (Round(X * 10000.0) / 10000.0)
23
24 // slider tick conversion
25 #define TO_SLIDER(X) (Standard_Integer)Round(X * 10)
26
27 // back conversion from slider ticks
28 #define FROM_SLIDER(X) X / 10.0
29
30 // ============================================================================
31 // function: SetView
32 // purpose:
33 // ============================================================================
34 void OCC_StereoConfigDlg::SetView (const Handle(V3d_View)& theView)
35 {
36   myView = theView;
37
38   // access initial values
39   myIOD = myView->Camera()->IOD();
40   myFocus = myView->Camera()->ZFocus();
41   mySliderFocus = TO_SLIDER(myFocus);
42   myIsRelativeIOD   = (myView->Camera()->GetIODType() == Graphic3d_Camera::IODType_Relative);
43   myIsRelativeFocus = (myView->Camera()->ZFocusType() == Graphic3d_Camera::FocusType_Relative);
44 }
45
46 // ============================================================================
47 // function: DoDataExchange
48 // purpose:
49 // ============================================================================
50 void OCC_StereoConfigDlg::DoDataExchange(CDataExchange* theDX)
51 {
52   CDialog::DoDataExchange(theDX);
53
54   DDX_Text (theDX, IDC_EDIT_IOD, myIOD);
55   DDV_MinMaxDouble (theDX, myIOD, -DBL_MAX, DBL_MAX);
56
57   if (myIsRelativeFocus)
58   {
59     // do slider data exchange
60     DDX_Slider (theDX, IDC_SLIDER_FOCUS, (int&)mySliderFocus);
61     DDV_MinMaxSlider (theDX, mySliderFocus, TO_SLIDER(0.1), TO_SLIDER(10));
62
63     // show up value in edit field
64     Standard_Real aEditValue = FROM_SLIDER (mySliderFocus);
65     DDX_Text (theDX, IDC_EDIT_FOCUS, aEditValue);
66
67     // update focus value correspondingly
68     myFocus = FROM_SLIDER (mySliderFocus);
69   }
70   else
71   {
72     DDX_Text (theDX, IDC_EDIT_FOCUS, myFocus);
73     DDV_MinMaxDouble (theDX, myFocus, 50, DBL_MAX);
74
75     mySliderFocus = TO_SLIDER(1.0);
76     DDX_Slider (theDX, IDC_SLIDER_FOCUS, (int&)mySliderFocus);
77   }
78
79   DDX_Check (theDX, IDC_CHECK_FOCUS_RELATIVE, (int&)myIsRelativeFocus);
80   DDX_Check (theDX, IDC_CHECK_IOD_RELATIVE, (int&)myIsRelativeIOD);
81
82   CEdit* aFocusEdit = (CEdit*) GetDlgItem (IDC_EDIT_FOCUS);
83   aFocusEdit->EnableWindow (myIsRelativeFocus != Standard_True);
84
85   CSliderCtrl* aSlider = (CSliderCtrl*) GetDlgItem (IDC_SLIDER_FOCUS);
86   aSlider->EnableWindow (myIsRelativeFocus == Standard_True);
87 }
88
89 // ============================================================================
90 // function: OnHScroll
91 // purpose:
92 // ============================================================================
93 void OCC_StereoConfigDlg::OnHScroll(UINT theSBCode, UINT thePos, CScrollBar* theScrollBar)
94 {
95   UpdateData (true);
96   UpdateData (false);
97   UpdateCamera();
98   CWnd::OnHScroll(theSBCode, thePos, theScrollBar);
99 }
100
101 // ============================================================================
102 // function: UpdateCamera
103 // purpose:
104 // ============================================================================
105 void OCC_StereoConfigDlg::UpdateCamera()
106 {
107   // update camera properties and redraw view
108   Handle(Graphic3d_Camera)& aCamera = myView->Camera();
109   if (aCamera.IsNull())
110     return;
111
112   // change IOD
113   Graphic3d_Camera::IODType aIODType = 
114     myIsRelativeIOD ? Graphic3d_Camera::IODType_Relative :
115                       Graphic3d_Camera::IODType_Absolute;
116
117   aCamera->SetIOD (aIODType, myIOD);
118
119   // change Focus
120   Graphic3d_Camera::FocusType aFocusType = 
121     myIsRelativeFocus ? Graphic3d_Camera::FocusType_Relative :
122                         Graphic3d_Camera::FocusType_Absolute;
123
124   aCamera->SetZFocus (aFocusType, myFocus);
125
126   // redraw view
127   myView->Redraw();
128 }
129
130 // ============================================================================
131 // function: OnCheckFocus
132 // purpose:
133 // ============================================================================
134 void OCC_StereoConfigDlg::OnCheckFocus()
135 {
136   UpdateData (true);
137
138   // change focus to some predefined values
139   if (myIsRelativeFocus)
140     myFocus = 1.0;
141   else
142     myFocus = 100.0;
143
144   UpdateData (false);
145   UpdateCamera();
146 }
147
148 // ============================================================================
149 // function: OnCheckIOD
150 // purpose:
151 // ============================================================================
152 void OCC_StereoConfigDlg::OnCheckIOD()
153 {
154   UpdateData (true);
155   UpdateCamera();
156 }
157
158 // ============================================================================
159 // function: OnChangeFocus
160 // purpose:
161 // ============================================================================
162 void OCC_StereoConfigDlg::OnChangeFocus()
163 {
164   // keep previous value
165   Standard_Real aPrevFocus = myFocus;
166
167   // read data from ui controls
168   if (UpdateData (true))
169   {
170     UpdateCamera();
171   }
172   else
173   {
174     // revert back
175     myFocus = aPrevFocus;
176     UpdateData (false);
177   }
178 }
179
180 // ============================================================================
181 // function: OnChangeIOD
182 // purpose:
183 // ============================================================================
184 void OCC_StereoConfigDlg::OnChangeIOD()
185 {
186   // keep previous value
187   Standard_Real aPrevIOD = myIOD;
188
189   // read data from ui controls
190   if (UpdateData (true))
191   {
192     UpdateCamera();
193   }
194   else
195   {
196     // revert back
197     myIOD = aPrevIOD;
198     UpdateData (false);
199   }
200 }
201
202 // ============================================================================
203 // function: OnSpinFocus
204 // purpose:
205 // ============================================================================
206 void OCC_StereoConfigDlg::OnSpinFocus (NMHDR* theNMHDR, LRESULT* theResult)
207 {
208   NM_UPDOWN* aNMUpDown = (NM_UPDOWN*)theNMHDR;
209
210   const double aStep = 0.1; // use small incremental step
211   const double aDelta = aNMUpDown->iDelta * aStep; // get delta
212
213   // changes value
214   myFocus -= (Standard_Real)aDelta;
215
216   // round up value
217   ROUND_UP (myFocus);
218
219   // actualize view & ui controls
220   UpdateData (false);
221   UpdateCamera();
222
223   *theResult = 0;
224 }
225
226 // ============================================================================
227 // function: OnSpinIOD
228 // purpose:
229 // ============================================================================
230 void OCC_StereoConfigDlg::OnSpinIOD (NMHDR* theNMHDR, LRESULT* theResult)
231 {
232   NM_UPDOWN* aNMUpDown = (NM_UPDOWN*)theNMHDR;
233
234   const double aStep = 0.01; // use small incremental step
235   const double aDelta = aNMUpDown->iDelta * aStep; // get delta
236
237   // changes value
238   myIOD -= (Standard_Real)aDelta;
239
240   // round up value
241   ROUND_UP (myIOD);
242
243   // actualize view & ui controls
244   UpdateData (false);
245   UpdateCamera();
246
247   *theResult = 0;
248 }