source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/vrippack-0.31/src/vrip/perspective.cc @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 4.1 KB
Line 
1/*
2
3Brian Curless
4
5Computer Graphics Laboratory
6Stanford University
7
8---------------------------------------------------------------------
9
10Copyright (1997) The Board of Trustees of the Leland Stanford Junior
11University. Except for commercial resale, lease, license or other
12commercial transactions, permission is hereby given to use, copy,
13modify this software for academic purposes only.  No part of this
14software or any derivatives thereof may be used in the production of
15computer models for resale or for use in a commercial
16product. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND
17CONCERNING THIS SOFTWARE.  No support is implied or provided.
18
19*/
20
21
22#include "perspective.h"
23#include "vrip.h"
24#include "configure.h"
25#include "vripGlobals.h"
26
27
28Vec3f thePerspViewRay;
29Vec3f theCOP;
30OrthoShear *thePerspShear;
31float theFocalLength;
32
33OrthoShear *
34initPersp(Quaternion &quat, Vec3f &trans, Vec3f &center, 
35          float resolution)
36{
37    Vec3f vec, dir, ctr;
38    Matrix4f mrot, mtrans, mpermute, mflipz;
39    Matrix4f mrotz, mtransyz, mshear, mprod;
40    Matrix4f mrotzinv, mtransyzinv, mshearinv;
41    float dot;
42    Matrix4f mrotx, mrotxinv;
43
44    ctr = center;
45    thePerspViewRay.setValue(PerspectiveDir);
46    theCOP.setValue(PerspectiveCOP);
47
48    // Set up the pose matrices
49    quat.toMatrix(mrot);
50    mtrans.makeIdentity();
51    mtrans.translate(trans);
52
53    // Transform the COP and center ray
54    mrot.multVec(thePerspViewRay, vec);
55    thePerspViewRay.setValue(vec);
56    mrot.multVec(theCOP, vec);
57    theCOP.setValue(vec);
58    theCOP += trans;
59
60    if (Verbose) {
61       printf("\nCOP: (%f, %f, %f)\n", theCOP.x, theCOP.y, theCOP.z);
62       printf("View direction: (%f, %f, %f)\n", thePerspViewRay.x, thePerspViewRay.y, thePerspViewRay.z);
63    }
64
65    vec3f rayDir;
66    rayDir[0] = thePerspViewRay.x;
67    rayDir[1] = thePerspViewRay.y;
68    rayDir[2] = thePerspViewRay.z;
69    OrthoShear *shear = computeShear(rayDir);
70    thePerspShear = shear;
71   
72    // Set up permutation matrix
73
74    mpermute.makeIdentity();
75    if (shear->axis == X_AXIS) {
76        mpermute.m[0][0] = 0;
77        mpermute.m[0][2] = 1;
78        mpermute.m[2][2] = 0;
79        mpermute.m[2][0] = 1;
80    } 
81    else if (shear->axis == Y_AXIS) {
82        mpermute.m[1][1] = 0;
83        mpermute.m[1][2] = 1;
84        mpermute.m[2][2] = 0;
85        mpermute.m[2][1] = 1;
86    }
87
88    // Update the COP and center of image plane in the new coordinate system
89    mpermute.multVec(theCOP, vec);
90    theCOP.setValue(vec);
91    mpermute.multVec(ctr, vec);
92    ctr.setValue(vec);
93
94    // Set up flip matrix
95    mflipz.makeIdentity();
96    if (shear->flip)
97        mflipz.m[2][2] = -1;
98   
99
100    // Update the COP and image center in the new flipped coordinate system
101    mflipz.multVec(theCOP, vec);
102    theCOP.setValue(vec);
103    mflipz.multVec(ctr, vec);
104    ctr.setValue(vec);
105
106    Matrix4f msh;
107    mshear.makeIdentity();
108    mshear.m[0][2] = shear->sx;
109    mshear.m[1][2] = shear->sy;
110
111    // Update the COP and image center
112    mshear.multVec(theCOP, vec);
113    theCOP.setValue(vec);
114    mshear.multVec(ctr, vec);
115    ctr.setValue(vec);
116
117    theFocalLength = ctr.z - theCOP.z;
118
119    if (Verbose) {
120        printf("Axis = %d (x=%d, y=%d, z=%d),   flip = %d,  sx = %f,  sy = %f\n", 
121               shear->axis, X_AXIS, Y_AXIS, Z_AXIS, shear->flip, shear->sx, shear->sy);
122        printf("\nTransformed COP: (%f, %f, %f)\n", theCOP.x, theCOP.y, theCOP.z);
123        printf("\nFocal length: %f\n", theFocalLength);
124    }
125
126   return shear;
127}
128
129
130void 
131applyPersp(Vec3f &vin, Vec3f &vout) {
132   // apply shear
133   vout.x = vin.x + vin.z*thePerspShear->sx;
134   vout.y = vin.y + vin.z*thePerspShear->sy;
135
136   // translate eye to origin
137   vout.x = vout.x - theCOP.x;
138   vout.y = vout.y - theCOP.y;
139
140   // apply projection
141   vout.x = vout.x*theFocalLength/(vin.z - theCOP.z);
142   vout.y = vout.y*theFocalLength/(vin.z - theCOP.z);
143
144   vout.z = vin.z;
145}
146
147
148void 
149applyInvPersp(Vec3f &vin, Vec3f &vout) 
150{
151   // un-project
152   vout.x = vin.x*(vin.z - theCOP.z)/theFocalLength;
153   vout.y = vin.y*(vin.z - theCOP.z)/theFocalLength;
154
155   // undo translation of eye to origin
156   vout.x = vout.x + theCOP.x;
157   vout.y = vout.y + theCOP.y;
158
159   // un-apply shear
160   vout.x = vout.x - vin.z*thePerspShear->sx;
161   vout.y = vout.y - vin.z*thePerspShear->sy;
162
163   vout.z = vin.z;
164}
165
Note: See TracBrowser for help on using the repository browser.