[37] | 1 | /* |
---|
| 2 | |
---|
| 3 | Brian Curless |
---|
| 4 | |
---|
| 5 | Computer Graphics Laboratory |
---|
| 6 | Stanford University |
---|
| 7 | |
---|
| 8 | --------------------------------------------------------------------- |
---|
| 9 | |
---|
| 10 | Copyright (1997) The Board of Trustees of the Leland Stanford Junior |
---|
| 11 | University. Except for commercial resale, lease, license or other |
---|
| 12 | commercial transactions, permission is hereby given to use, copy, |
---|
| 13 | modify this software for academic purposes only. No part of this |
---|
| 14 | software or any derivatives thereof may be used in the production of |
---|
| 15 | computer models for resale or for use in a commercial |
---|
| 16 | product. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND |
---|
| 17 | CONCERNING THIS SOFTWARE. No support is implied or provided. |
---|
| 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | #include "linePersp.h" |
---|
| 23 | #include "vrip.h" |
---|
| 24 | #include "vripGlobals.h" |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | Vec3f theRayDir; |
---|
| 28 | Vec3f thePoint; |
---|
| 29 | Matrix4f theMat1, theMat1Inverse; |
---|
| 30 | Matrix4f theMat2, theMat2Inverse; |
---|
| 31 | float thePerspA, thePerspB; |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | OrthoShear * |
---|
| 35 | initLinePersp(Quaternion &quat, Vec3f &trans, Vec3f ¢er, float resolution) |
---|
| 36 | { |
---|
| 37 | Vec3f vec, dir; |
---|
| 38 | Matrix4f mrot, mtrans, mpermute, mflipz; |
---|
| 39 | Matrix4f mrotz, mtransyz, mshear, mprod; |
---|
| 40 | Matrix4f mrotzinv, mtransyzinv, mshearinv; |
---|
| 41 | float dot; |
---|
| 42 | |
---|
| 43 | // Set up the laser projection line |
---|
| 44 | |
---|
| 45 | theRayDir.setValue(LASER_LINE_DIR_X, LASER_LINE_DIR_Y, LASER_LINE_DIR_Z); |
---|
| 46 | thePoint.setValue(LASER_LINE_AT_T0_X, LASER_LINE_AT_T0_Y, |
---|
| 47 | LASER_LINE_AT_T0_Z); |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | // Set up the pose matrices |
---|
| 52 | quat.toMatrix(mrot); |
---|
| 53 | mtrans.makeIdentity(); |
---|
| 54 | mtrans.translate(trans); |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | // Transform the line |
---|
| 58 | |
---|
| 59 | mrot.multVec(theRayDir, vec); |
---|
| 60 | theRayDir.setValue(vec); |
---|
| 61 | mrot.multVec(thePoint, vec); |
---|
| 62 | thePoint.setValue(vec); |
---|
| 63 | thePoint += trans; |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | // Choose permutation and flip |
---|
| 67 | |
---|
| 68 | vec = thePoint - center; |
---|
| 69 | dot = -vec.dot(theRayDir); |
---|
| 70 | dir.setValue(theRayDir); |
---|
| 71 | dir *= dot; |
---|
| 72 | dir += vec; |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | OrthoShear *shear = new OrthoShear; |
---|
| 76 | |
---|
| 77 | shear->flip = FALSE; |
---|
| 78 | if (fabs(dir[0]) > fabs(dir[1])) { |
---|
| 79 | if (fabs(dir[0]) > fabs(dir[2])) { |
---|
| 80 | shear->axis = X_AXIS; |
---|
| 81 | shear->flip = dir[0] < 0; |
---|
| 82 | } |
---|
| 83 | else { |
---|
| 84 | shear->axis = Z_AXIS; |
---|
| 85 | shear->flip = dir[2] < 0; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | else if (fabs(dir[1]) > fabs(dir[2])) { |
---|
| 89 | shear->axis = Y_AXIS; |
---|
| 90 | shear->flip = dir[1] < 0; |
---|
| 91 | } |
---|
| 92 | else { |
---|
| 93 | shear->axis = Z_AXIS; |
---|
| 94 | shear->flip = dir[2] < 0; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | // Set up permutation matrix |
---|
| 99 | |
---|
| 100 | mpermute.makeIdentity(); |
---|
| 101 | if (shear->axis == X_AXIS) { |
---|
| 102 | mpermute.m[0][0] = 0; |
---|
| 103 | mpermute.m[0][2] = 1; |
---|
| 104 | mpermute.m[2][2] = 0; |
---|
| 105 | mpermute.m[2][0] = 1; |
---|
| 106 | } |
---|
| 107 | else if (shear->axis == Y_AXIS) { |
---|
| 108 | mpermute.m[1][1] = 0; |
---|
| 109 | mpermute.m[1][2] = 1; |
---|
| 110 | mpermute.m[2][2] = 0; |
---|
| 111 | mpermute.m[2][1] = 1; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | // Update the line into the new permuted coordinate system |
---|
| 116 | |
---|
| 117 | mpermute.multVec(theRayDir, vec); |
---|
| 118 | theRayDir.setValue(vec); |
---|
| 119 | mpermute.multVec(thePoint, vec); |
---|
| 120 | thePoint.setValue(vec); |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | // Set up flip matrix |
---|
| 124 | |
---|
| 125 | mflipz.makeIdentity(); |
---|
| 126 | if (shear->flip) |
---|
| 127 | mflipz.m[2][2] = -1; |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | // Update the line into the new flipped coordinate system |
---|
| 131 | |
---|
| 132 | mflipz.multVec(theRayDir, vec); |
---|
| 133 | theRayDir.setValue(vec); |
---|
| 134 | mflipz.multVec(thePoint, vec); |
---|
| 135 | thePoint.setValue(vec); |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | // Rotate the direction parallel to the xz plane |
---|
| 139 | |
---|
| 140 | mrotz.makeIdentity(); |
---|
| 141 | float mag = 1/sqrt(theRayDir.x*theRayDir.x + theRayDir.y*theRayDir.y); |
---|
| 142 | mrotz.m[0][0] = theRayDir.x*mag; |
---|
| 143 | mrotz.m[0][1] = theRayDir.y*mag; |
---|
| 144 | mrotz.m[1][0] = -theRayDir.y*mag; |
---|
| 145 | mrotz.m[1][1] = theRayDir.x*mag; |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | // Update the laser line |
---|
| 149 | |
---|
| 150 | mrotz.multVec(theRayDir, vec); |
---|
| 151 | theRayDir.setValue(vec); |
---|
| 152 | mrotz.multVec(thePoint, vec); |
---|
| 153 | thePoint.setValue(vec); |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | // Translate into the x-z plane and move in depth so that the |
---|
| 157 | // image plane is at z = 0 |
---|
| 158 | |
---|
| 159 | mtransyz.makeIdentity(); |
---|
| 160 | mtransyz.translate(0, -thePoint.y, -center.z); |
---|
| 161 | |
---|
| 162 | // Update the laser line |
---|
| 163 | |
---|
| 164 | mtransyz.multVec(thePoint, vec); |
---|
| 165 | thePoint.setValue(vec); |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | // Shear the projection planes to be perpendicular to the x-y plane |
---|
| 169 | |
---|
| 170 | mshear.makeIdentity(); |
---|
| 171 | mshear.m[0][2] = theRayDir.z/theRayDir.x; |
---|
| 172 | |
---|
| 173 | // Update the laser line |
---|
| 174 | |
---|
| 175 | mshear.multVec(theRayDir, vec); |
---|
| 176 | theRayDir.setValue(vec); |
---|
| 177 | mshear.multVec(thePoint, vec); |
---|
| 178 | thePoint.setValue(vec); |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | // Compute the front end matrix |
---|
| 182 | |
---|
| 183 | theMat1.makeIdentity(); |
---|
| 184 | theMat1.multLeft(mrotz); |
---|
| 185 | theMat1.multLeft(mtransyz); |
---|
| 186 | theMat1.multLeft(mshear); |
---|
| 187 | |
---|
| 188 | mrotzinv.setValue(mrotz); |
---|
| 189 | mrotzinv.transpose(); |
---|
| 190 | |
---|
| 191 | mtransyzinv.setValue(mtransyz); |
---|
| 192 | mtransyzinv.m[0][3] = -mtransyzinv.m[0][3]; |
---|
| 193 | mtransyzinv.m[1][3] = -mtransyzinv.m[1][3]; |
---|
| 194 | mtransyzinv.m[2][3] = -mtransyzinv.m[2][3]; |
---|
| 195 | |
---|
| 196 | mshearinv.setValue(mshear); |
---|
| 197 | mshearinv.m[0][2] = -mshearinv.m[0][2]; |
---|
| 198 | |
---|
| 199 | theMat1Inverse.makeIdentity(); |
---|
| 200 | theMat1Inverse.multLeft(mshearinv); |
---|
| 201 | theMat1Inverse.multLeft(mtransyzinv); |
---|
| 202 | theMat1Inverse.multLeft(mrotzinv); |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | // Compute the components of the perspective-in-y transformation |
---|
| 206 | |
---|
| 207 | thePerspA = theRayDir.z/theRayDir.x; |
---|
| 208 | thePerspB = thePoint.z - thePerspA*thePoint.x; |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | // Compute the back end transformation |
---|
| 212 | |
---|
| 213 | theMat2.makeIdentity(); |
---|
| 214 | theMat2.multLeft(mtransyzinv); |
---|
| 215 | theMat2.multLeft(mrotzinv); |
---|
| 216 | |
---|
| 217 | theMat2Inverse.makeIdentity(); |
---|
| 218 | theMat2Inverse.multLeft(mrotz); |
---|
| 219 | theMat2Inverse.multLeft(mtransyz); |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | // Extract the approximate shear |
---|
| 223 | mprod.setValue(theMat1); |
---|
| 224 | mprod.multLeft(theMat2); |
---|
| 225 | shear->sx = mprod.m[0][2]; |
---|
| 226 | shear->sy = mprod.m[1][2]; |
---|
| 227 | |
---|
| 228 | if (Verbose) { |
---|
| 229 | printf("Axis = %d, flip = %d, sx = %f, sy = %f\n", |
---|
| 230 | shear->axis, shear->flip, shear->sx, shear->sy); |
---|
| 231 | |
---|
| 232 | printf("Matrix 1:\n"); |
---|
| 233 | theMat1.print(); |
---|
| 234 | |
---|
| 235 | printf("\nPersp: a = %f b = %f\n", thePerspA, thePerspB); |
---|
| 236 | |
---|
| 237 | printf("\nMatrix 2:\n"); |
---|
| 238 | theMat2.print(); |
---|
| 239 | |
---|
| 240 | printf("\nMatrix 2 * matrix 1:\n"); |
---|
| 241 | mprod.print(); |
---|
| 242 | |
---|
| 243 | mprod.setValue(theMat1); |
---|
| 244 | mprod.multLeft(theMat1Inverse); |
---|
| 245 | printf("\nInv(matrix 1) * matrix 1:\n"); |
---|
| 246 | mprod.print(); |
---|
| 247 | |
---|
| 248 | mprod.setValue(theMat2); |
---|
| 249 | mprod.multLeft(theMat2Inverse); |
---|
| 250 | printf("\nInv(matrix 2) * matrix 2:\n"); |
---|
| 251 | mprod.print(); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | #if 0 |
---|
| 255 | Vec3f temp(1,2,3); |
---|
| 256 | Vec3f temp2, temp3; |
---|
| 257 | applyLinePersp(temp, temp2); |
---|
| 258 | applyInvLinePersp(temp2, temp3); |
---|
| 259 | printf("%f, %f, %f -> %f, %f, %f\n", |
---|
| 260 | temp.x, temp.y, temp.z, temp3.x, temp3.y, temp3.z); |
---|
| 261 | #endif |
---|
| 262 | |
---|
| 263 | return shear; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | void |
---|
| 268 | applyLinePersp(Vec3f &vin, Vec3f &vout) |
---|
| 269 | { |
---|
| 270 | Vec3f vec; |
---|
| 271 | |
---|
| 272 | theMat1.multVec(vin, vec); |
---|
| 273 | |
---|
| 274 | vec.y = vec.y/(1-vec.z/(thePerspA*vec.x + thePerspB)); |
---|
| 275 | |
---|
| 276 | theMat2.multVec(vec, vout); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | |
---|
| 280 | void |
---|
| 281 | applyInvLinePersp(Vec3f &vin, Vec3f &vout) |
---|
| 282 | { |
---|
| 283 | Vec3f vec; |
---|
| 284 | |
---|
| 285 | theMat2Inverse.multVec(vin, vec); |
---|
| 286 | |
---|
| 287 | vec.y = vec.y*(1-vec.z/(thePerspA*vec.x + thePerspB)); |
---|
| 288 | |
---|
| 289 | theMat1Inverse.multVec(vec, vout); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | |
---|