Developer Documentation
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 //=============================================================================
45 //
46 // CLASS newClass - IMPLEMENTATION
47 //
48 //=============================================================================
49 
50 
51 //== INCLUDES =================================================================
52 
53 #include <OpenMesh/Tools/VDPM/ViewingParameters.hh>
54 #include <iostream>
55 
56 //== NAMESPACES ===============================================================
57 
58 namespace OpenMesh {
59 namespace VDPM {
60 
61 
62 //== IMPLEMENTATION ==========================================================
63 
64 
65 ViewingParameters::
66 ViewingParameters()
67 {
68  for ( unsigned int i = 0; i < 16; ++i)
69  modelview_matrix_[i] = 0.0;
70 
71  fovy_ = 45.0f;
72  aspect_ = 1.0f;
73  tolerance_square_ = 0.001f;
74 }
75 
76 void
77 ViewingParameters::
78 update_viewing_configurations()
79 {
80  // |a11 a12 a13|-1 | a33a22-a32a23 -(a33a12-a32a13) a23a12-a22a13 |
81  // |a21 a22 a23| = 1/DET*|-(a33a21-a31a23) a33a11-a31a13 -(a23a11-a21a13)|
82  // |a31 a32 a33| | a32a21-a31a22 -(a32a11-a31a12) a22a11-a21a12 |
83  // DET = a11(a33a22-a32a23)-a21(a33a12-a32a13)+a31(a23a12-a22a13)
84 
85  float invdet;
86  float a11, a12, a13, a21, a22, a23, a31, a32, a33;
87  Vec3f trans;
88 
89 // Workaround for internal compiler error on Visual Studio 2015 Update 1
90 #if ((defined(_MSC_VER) && (_MSC_VER >= 1800)) )
91  Vec3f inv_rot[3]{ {},{},{} };
92  Vec3f normal[4]{ {},{},{},{} };
93 #else
94  Vec3f inv_rot[3];
95  Vec3f normal[4];
96 #endif
97 
98  a11 = (float) modelview_matrix_[0];
99  a12 = (float) modelview_matrix_[4];
100  a13 = (float) modelview_matrix_[8];
101  trans[0] = (float) modelview_matrix_[12];
102 
103  a21 = (float) modelview_matrix_[1];
104  a22 = (float) modelview_matrix_[5];
105  a23 = (float) modelview_matrix_[9];
106  trans[1] = (float) modelview_matrix_[13];
107 
108  a31 = (float) modelview_matrix_[2];
109  a32 = (float) modelview_matrix_[6];
110  a33 = (float) modelview_matrix_[10];
111  trans[2] = (float) modelview_matrix_[14];
112 
113  invdet = a11*(a33*a22-a32*a23) - a21*(a33*a12-a32*a13) + a31*(a23*a12-a22*a13);
114  invdet= (float) 1.0/invdet;
115 
116  (inv_rot[0])[0] = (a33*a22-a32*a23) * invdet;
117  (inv_rot[0])[1] = -(a33*a12-a32*a13) * invdet;
118  (inv_rot[0])[2] = (a23*a12-a22*a13) * invdet;
119  (inv_rot[1])[0] = -(a33*a21-a31*a23) * invdet;
120  (inv_rot[1])[1] = (a33*a11-a31*a13) * invdet;
121  (inv_rot[1])[2] = -(a23*a11-a21*a13) * invdet;
122  (inv_rot[2])[0] = (a32*a21-a31*a22) * invdet;
123  (inv_rot[2])[1] = -(a32*a11-a31*a12) * invdet;
124  (inv_rot[2])[2] = (a22*a11-a21*a12) * invdet;
125 
126  eye_pos_ = - Vec3f(dot(inv_rot[0], trans),
127  dot(inv_rot[1], trans),
128  dot(inv_rot[2], trans));
129  right_dir_ = Vec3f(a11, a12, a13);
130  up_dir_ = Vec3f(a21, a22, a23);
131  view_dir_ = - Vec3f(a31, a32, a33);
132 
133  //float aspect = width() / height();
134  const float half_theta = fovy() * 0.5f;
135  const float half_phi = atanf(aspect() * tanf(half_theta));
136 
137  const float sin1 = sinf(half_theta);
138  const float cos1 = cosf(half_theta);
139  const float sin2 = sinf(half_phi);
140  const float cos2 = cosf(half_phi);
141 
142  normal[0] = cos2 * right_dir_ + sin2 * view_dir_;
143  normal[1] = -cos1 * up_dir_ - sin1 * view_dir_;
144  normal[2] = -cos2 * right_dir_ + sin2 * view_dir_;
145  normal[3] = cos1 * up_dir_ - sin1 * view_dir_;
146 
147  for (int i=0; i<4; i++)
148  frustum_plane_[i] = Plane3d(normal[i], eye_pos_);
149 }
150 
151 void
152 ViewingParameters::
153 PrintOut()
154 {
155  std::cout << " ModelView matrix: " << std::endl;
156  std::cout << " |" << modelview_matrix_[0] << " " << modelview_matrix_[4] << " " << modelview_matrix_[8] << " " << modelview_matrix_[12] << "|" << std::endl;
157  std::cout << " |" << modelview_matrix_[1] << " " << modelview_matrix_[5] << " " << modelview_matrix_[9] << " " << modelview_matrix_[13] << "|" << std::endl;
158  std::cout << " |" << modelview_matrix_[2] << " " << modelview_matrix_[6] << " " << modelview_matrix_[10] << " " << modelview_matrix_[14] << "|" << std::endl;
159  std::cout << " |" << modelview_matrix_[3] << " " << modelview_matrix_[7] << " " << modelview_matrix_[11] << " " << modelview_matrix_[15] << "|" << std::endl;
160  std::cout << " Fovy: " << fovy_ << std::endl;
161  std::cout << " Aspect: " << aspect_ << std::endl;
162  std::cout << " Tolerance^2: " << tolerance_square_ << std::endl;
163  std::cout << " Eye Pos: " << eye_pos_ << std::endl;
164  std::cout << " Right dir: " << right_dir_ << std::endl;
165  std::cout << " Up dir: " << up_dir_ << std::endl;
166  std::cout << " View dir: " << view_dir_ << std::endl;
167 }
168 
169 //=============================================================================
170 } // namespace VDPM
171 } // namespace OpenMesh
172 //=============================================================================
VectorT< float, 3 > Vec3f
Definition: Vector11T.hh:811
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.