Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ViewingParameters.cc
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Date$ *
46  * *
47 \*===========================================================================*/
48 
49 //=============================================================================
50 //
51 // CLASS newClass - IMPLEMENTATION
52 //
53 //=============================================================================
54 
55 
56 //== INCLUDES =================================================================
57 
58 #include <OpenMesh/Tools/VDPM/ViewingParameters.hh>
59 #include <iostream>
60 
61 //== NAMESPACES ===============================================================
62 
63 namespace OpenMesh {
64 namespace VDPM {
65 
66 
67 //== IMPLEMENTATION ==========================================================
68 
69 
70 ViewingParameters::
71 ViewingParameters()
72 {
73  for ( unsigned int i = 0; i < 16; ++i)
74  modelview_matrix_[i] = 0.0;
75 
76  fovy_ = 45.0f;
77  aspect_ = 1.0f;
78  tolerance_square_ = 0.001f;
79 }
80 
81 void
82 ViewingParameters::
83 update_viewing_configurations()
84 {
85  // |a11 a12 a13|-1 | a33a22-a32a23 -(a33a12-a32a13) a23a12-a22a13 |
86  // |a21 a22 a23| = 1/DET*|-(a33a21-a31a23) a33a11-a31a13 -(a23a11-a21a13)|
87  // |a31 a32 a33| | a32a21-a31a22 -(a32a11-a31a12) a22a11-a21a12 |
88  // DET = a11(a33a22-a32a23)-a21(a33a12-a32a13)+a31(a23a12-a22a13)
89 
90  float invdet;
91  float a11, a12, a13, a21, a22, a23, a31, a32, a33;
92  Vec3f trans;
93 
94 // Workaround for internal compiler error on Visual Studio 2015 Update 1
95 #if (_MSC_VER >= 1900 )
96  Vec3f inv_rot[3]{ {},{},{} };
97  Vec3f normal[4]{ {},{},{},{} };
98 #else
99  Vec3f inv_rot[3];
100  Vec3f normal[4];
101 #endif
102 
103  a11 = (float) modelview_matrix_[0];
104  a12 = (float) modelview_matrix_[4];
105  a13 = (float) modelview_matrix_[8];
106  trans[0] = (float) modelview_matrix_[12];
107 
108  a21 = (float) modelview_matrix_[1];
109  a22 = (float) modelview_matrix_[5];
110  a23 = (float) modelview_matrix_[9];
111  trans[1] = (float) modelview_matrix_[13];
112 
113  a31 = (float) modelview_matrix_[2];
114  a32 = (float) modelview_matrix_[6];
115  a33 = (float) modelview_matrix_[10];
116  trans[2] = (float) modelview_matrix_[14];
117 
118  invdet = a11*(a33*a22-a32*a23) - a21*(a33*a12-a32*a13) + a31*(a23*a12-a22*a13);
119  invdet= (float) 1.0/invdet;
120 
121  (inv_rot[0])[0] = (a33*a22-a32*a23) * invdet;
122  (inv_rot[0])[1] = -(a33*a12-a32*a13) * invdet;
123  (inv_rot[0])[2] = (a23*a12-a22*a13) * invdet;
124  (inv_rot[1])[0] = -(a33*a21-a31*a23) * invdet;
125  (inv_rot[1])[1] = (a33*a11-a31*a13) * invdet;
126  (inv_rot[1])[2] = -(a23*a11-a21*a13) * invdet;
127  (inv_rot[2])[0] = (a32*a21-a31*a22) * invdet;
128  (inv_rot[2])[1] = -(a32*a11-a31*a12) * invdet;
129  (inv_rot[2])[2] = (a22*a11-a21*a12) * invdet;
130 
131  eye_pos_ = - Vec3f(dot(inv_rot[0], trans),
132  dot(inv_rot[1], trans),
133  dot(inv_rot[2], trans));
134  right_dir_ = Vec3f(a11, a12, a13);
135  up_dir_ = Vec3f(a21, a22, a23);
136  view_dir_ = - Vec3f(a31, a32, a33);
137 
138  //float aspect = width() / height();
139  const float half_theta = fovy() * 0.5f;
140  const float half_phi = atanf(aspect() * tanf(half_theta));
141 
142  const float sin1 = sinf(half_theta);
143  const float cos1 = cosf(half_theta);
144  const float sin2 = sinf(half_phi);
145  const float cos2 = cosf(half_phi);
146 
147  normal[0] = cos2 * right_dir_ + sin2 * view_dir_;
148  normal[1] = -cos1 * up_dir_ - sin1 * view_dir_;
149  normal[2] = -cos2 * right_dir_ + sin2 * view_dir_;
150  normal[3] = cos1 * up_dir_ - sin1 * view_dir_;
151 
152  for (int i=0; i<4; i++)
153  frustum_plane_[i] = Plane3d(normal[i], eye_pos_);
154 }
155 
156 void
157 ViewingParameters::
158 PrintOut()
159 {
160  std::cout << " ModelView matrix: " << std::endl;
161  std::cout << " |" << modelview_matrix_[0] << " " << modelview_matrix_[4] << " " << modelview_matrix_[8] << " " << modelview_matrix_[12] << "|" << std::endl;
162  std::cout << " |" << modelview_matrix_[1] << " " << modelview_matrix_[5] << " " << modelview_matrix_[9] << " " << modelview_matrix_[13] << "|" << std::endl;
163  std::cout << " |" << modelview_matrix_[2] << " " << modelview_matrix_[6] << " " << modelview_matrix_[10] << " " << modelview_matrix_[14] << "|" << std::endl;
164  std::cout << " |" << modelview_matrix_[3] << " " << modelview_matrix_[7] << " " << modelview_matrix_[11] << " " << modelview_matrix_[15] << "|" << std::endl;
165  std::cout << " Fovy: " << fovy_ << std::endl;
166  std::cout << " Aspect: " << aspect_ << std::endl;
167  std::cout << " Tolerance^2: " << tolerance_square_ << std::endl;
168  std::cout << " Eye Pos: " << eye_pos_ << std::endl;
169  std::cout << " Right dir: " << right_dir_ << std::endl;
170  std::cout << " Up dir: " << up_dir_ << std::endl;
171  std::cout << " View dir: " << view_dir_ << std::endl;
172 }
173 
174 //=============================================================================
175 } // namespace VDPM
176 } // namespace OpenMesh
177 //=============================================================================
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
VectorT< float, 3 > Vec3f
Definition: Vector11T.hh:769