f751596e |
1 | // Created on: 2014-11-24 |
2 | // Created by: Varvara POSKONINA |
3 | // Copyright (c) 2005-2014 OPEN CASCADE SAS |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
12 | // |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
15 | |
16 | #include <SelectMgr_FrustumBuilder.hxx> |
17 | |
92efcf78 |
18 | IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_FrustumBuilder,Standard_Transient) |
19 | |
f751596e |
20 | #define DOT(A, B) (A.x() * B.x() + A.y() * B.y() + A.z() * B.z()) |
21 | #define LENGTH(A) (std::sqrt (A.x() * A.x() + A.y() * A.y() + A.z() * A.z())) |
22 | |
bf3977c9 |
23 | |
f751596e |
24 | //======================================================================= |
25 | // function : SelectMgr_FrustumBuilder |
26 | // purpose : Creates new frustum builder with empty matrices |
27 | //======================================================================= |
28 | SelectMgr_FrustumBuilder::SelectMgr_FrustumBuilder() |
51d4a4f9 |
29 | : myWidth (INT_MAX), |
f751596e |
30 | myHeight (INT_MAX), |
51d4a4f9 |
31 | myIsViewportSet (Standard_False) |
825aa485 |
32 | { |
33 | // |
34 | } |
f751596e |
35 | |
36 | //======================================================================= |
51d4a4f9 |
37 | // function : SetCamera |
38 | // purpose : |
825aa485 |
39 | //======================================================================= |
51d4a4f9 |
40 | void SelectMgr_FrustumBuilder::SetCamera (const Handle(Graphic3d_Camera)& theCamera) |
825aa485 |
41 | { |
51d4a4f9 |
42 | myCamera = theCamera; |
825aa485 |
43 | } |
44 | |
f751596e |
45 | //======================================================================= |
46 | // function : SetWindowSize |
47 | // purpose : Stores current window width and height |
48 | //======================================================================= |
49 | void SelectMgr_FrustumBuilder::SetWindowSize (const Standard_Integer theWidth, |
50 | const Standard_Integer theHeight) |
51 | { |
52 | myWidth = theWidth; |
53 | myHeight = theHeight; |
54 | } |
55 | |
56 | //======================================================================= |
57 | // function : SetViewport |
58 | // purpose : Stores current viewport coordinates |
59 | //======================================================================= |
60 | void SelectMgr_FrustumBuilder::SetViewport (const Standard_Real theX, |
61 | const Standard_Real theY, |
62 | const Standard_Real theWidth, |
63 | const Standard_Real theHeight) |
64 | { |
65 | myViewport = NCollection_Vec4<Standard_Real> (theX, theY, theWidth, theHeight); |
66 | myIsViewportSet = Standard_True; |
67 | } |
68 | |
91d96372 |
69 | //======================================================================= |
70 | // function : WindowSize |
71 | // purpose : |
72 | //======================================================================= |
73 | void SelectMgr_FrustumBuilder::WindowSize (Standard_Integer& theWidth, |
099f3513 |
74 | Standard_Integer& theHeight) const |
91d96372 |
75 | { |
76 | theWidth = myWidth; |
77 | theHeight = myHeight; |
78 | } |
79 | |
f751596e |
80 | //======================================================================= |
81 | // function : InvalidateViewport |
82 | // purpose : |
83 | //======================================================================= |
84 | void SelectMgr_FrustumBuilder::InvalidateViewport() |
85 | { |
86 | myIsViewportSet = Standard_False; |
87 | } |
88 | |
89 | //======================================================================= |
90 | // function : SignedPlanePntDist |
91 | // purpose : Calculates signed distance between plane with equation |
92 | // theEq and point thePnt |
93 | //======================================================================= |
94 | Standard_Real SelectMgr_FrustumBuilder::SignedPlanePntDist (const SelectMgr_Vec3& theEq, |
95 | const SelectMgr_Vec3& thePnt) const |
96 | { |
97 | const Standard_Real aNormLength = LENGTH (theEq); |
98 | const Standard_Real anInvNormLength = aNormLength < Precision::Confusion() ? 0.0 : 1.0 / aNormLength; |
99 | const Standard_Real anA = theEq.x() * anInvNormLength; |
100 | const Standard_Real aB = theEq.y() * anInvNormLength; |
101 | const Standard_Real aC = theEq.z() * anInvNormLength; |
102 | return anA * thePnt.x() + aB * thePnt.y() + aC * thePnt.z(); |
103 | } |
104 | |
f751596e |
105 | // ======================================================================= |
106 | // function : ProjectPntOnViewPlane |
107 | // purpose : Projects 2d screen point onto view frustum plane: |
108 | // theZ = 0 - near plane, |
109 | // theZ = 1 - far plane |
110 | // ======================================================================= |
3bf9a45f |
111 | gp_Pnt SelectMgr_FrustumBuilder::ProjectPntOnViewPlane (const Standard_Real& theX, |
112 | const Standard_Real& theY, |
113 | const Standard_Real& theZ) const |
f751596e |
114 | { |
51d4a4f9 |
115 | if (myCamera.IsNull()) |
116 | { |
117 | return gp_Pnt(); |
118 | } |
f751596e |
119 | // map coords to NDC |
e70625d6 |
120 | gp_Pnt anXYZ; |
f751596e |
121 | if (!myIsViewportSet) |
122 | { |
e70625d6 |
123 | anXYZ.SetCoord (2.0 * theX / myWidth - 1.0, |
124 | (myHeight - 1 - theY) / myHeight * 2.0 - 1.0, |
51d4a4f9 |
125 | myCamera->IsZeroToOneDepth() ? theZ : (2.0 * theZ - 1.0)); |
f751596e |
126 | } |
127 | else |
128 | { |
e70625d6 |
129 | anXYZ.SetCoord (2.0 * (theX - myWidth * myViewport.x()) / (myWidth * (myViewport.z() - myViewport.x())) - 1.0, |
130 | 2.0 * (theY - myHeight * myViewport.y()) / (myHeight * (myViewport.w() - myViewport.y())) - 1.0, |
131 | theZ); |
f751596e |
132 | } |
51d4a4f9 |
133 | return myCamera->UnProject (anXYZ); |
f751596e |
134 | } |